Skip to content

Commit

Permalink
review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
atheeshp committed Nov 15, 2023
1 parent 45d97ef commit da671f1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
27 changes: 8 additions & 19 deletions x/staking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ type msgServer struct {
*Keeper
}

// distributionModuleName duplicates the distribution module's name to avoid a cyclic dependency with x/distribution.
// It should be synced with the distribution module's name if it is ever changed.
// See: https://github.com/cosmos/cosmos-sdk/blob/912390d5fc4a32113ea1aacc98b77b2649aea4c2/x/distribution/types/keys.go#L15
const distributionModuleName = "distribution"

// NewMsgServerImpl returns an implementation of the staking MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper *Keeper) types.MsgServer {
Expand Down Expand Up @@ -627,28 +622,28 @@ func (k msgServer) RotateConsPubKey(ctx context.Context, msg *types.MsgRotateCon

if rotatedTo != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidAddress,
"the address is already present in rotation history, please try with new one")
"the new public key is already present in rotation history, please try with a different one")
}

newConsAddr := sdk.ConsAddress(pk.Address())

// checks if NewPubKey is not duplicated on ValidatorsByConsAddr
validator, _ := k.Keeper.ValidatorByConsAddr(ctx, newConsAddr)
if validator != nil {
validator1, _ := k.Keeper.ValidatorByConsAddr(ctx, newConsAddr)
if validator1 != nil {
return nil, types.ErrConsensusPubKeyAlreadyUsedForValidator
}

valAddr, err := k.validatorAddressCodec.StringToBytes(validator.GetOperator())
valAddr, err := k.validatorAddressCodec.StringToBytes(msg.ValidatorAddress)
if err != nil {
return nil, err
}

validator, err = k.Keeper.GetValidator(ctx, valAddr)
validator2, err := k.Keeper.GetValidator(ctx, valAddr)
if err != nil {
return nil, types.ErrNoValidatorFound
}

if status := validator.GetStatus(); status != types.Bonded {
if status := validator2.GetStatus(); status != types.Bonded {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidType, "validator status is not bonded, got %s", status.String())
}

Expand All @@ -666,22 +661,16 @@ func (k msgServer) RotateConsPubKey(ctx context.Context, msg *types.MsgRotateCon
return nil, err
}

err = k.Keeper.bankKeeper.SendCoinsFromAccountToModule(ctx, sdk.AccAddress(valAddr), distributionModuleName, sdk.NewCoins(params.KeyRotationFee))
err = k.Keeper.bankKeeper.SendCoinsFromAccountToModule(ctx, sdk.AccAddress(valAddr), types.DistributionModuleName, sdk.NewCoins(params.KeyRotationFee))
if err != nil {
return nil, err
}

// overwrites NewPubKey in validator.ConsPubKey
val, ok := validator.(types.Validator)
if !ok {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidType, "Expecting types.Validator, got %T", validator)
}

// Add ConsPubKeyRotationHistory for tracking rotation
err = k.setConsPubKeyRotationHistory(
ctx,
valAddr,
val.ConsensusPubkey,
validator2.ConsensusPubkey,
msg.NewPubkey,
params.KeyRotationFee,
)
Expand Down
1 change: 0 additions & 1 deletion x/staking/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
legacy.RegisterAminoMsg(cdc, &MsgBeginRedelegate{}, "cosmos-sdk/MsgBeginRedelegate")
legacy.RegisterAminoMsg(cdc, &MsgCancelUnbondingDelegation{}, "cosmos-sdk/MsgCancelUnbondingDelegation")
legacy.RegisterAminoMsg(cdc, &MsgUpdateParams{}, "cosmos-sdk/x/staking/MsgUpdateParams")
legacy.RegisterAminoMsg(cdc, &MsgRotateConsPubKey{}, "cosmos-sdk/MsgRotateConsPubKey")

cdc.RegisterInterface((*isStakeAuthorization_Validators)(nil), nil)
cdc.RegisterConcrete(&StakeAuthorization_AllowList{}, "cosmos-sdk/StakeAuthorization/AllowList", nil)
Expand Down
5 changes: 5 additions & 0 deletions x/staking/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const (

// GovModuleName is the name of the gov module
GovModuleName = "gov"

// distributionModuleName duplicates the distribution module's name to avoid a cyclic dependency with x/distribution.
// It should be synced with the distribution module's name if it is ever changed.
// See: https://github.com/cosmos/cosmos-sdk/blob/912390d5fc4a32113ea1aacc98b77b2649aea4c2/x/distribution/types/keys.go#L15
DistributionModuleName = "distribution"
)

var (
Expand Down

0 comments on commit da671f1

Please sign in to comment.