Skip to content

Commit

Permalink
set pubkey on Details
Browse files Browse the repository at this point in the history
  • Loading branch information
ylsGit committed Jul 10, 2024
1 parent 27fed1c commit 1966b3f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 35 deletions.
13 changes: 1 addition & 12 deletions x/staking/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,6 @@ func GetCmdEditValidator(cdc *codec.Codec) *cobra.Command {
Details: viper.GetString(FlagDetails),
}

pkStr := viper.GetString(FlagPubKey)
var pk crypto.PubKey = nil
var err error
if pkStr != "" {
pk, err = types.GetConsPubKeyBech32(pkStr)
if err != nil {
return err
}
}

// TODO: recover the msd modification later
//var newMinSelfDelegation *sdk.Int
//
Expand All @@ -132,15 +122,14 @@ func GetCmdEditValidator(cdc *codec.Codec) *cobra.Command {
//}
//
//msg := types.NewMsgEditValidator(sdk.ValAddress(valAddr), description, newRate, newMinSelfDelegation)
msg := types.NewMsgEditValidator(sdk.ValAddress(valAddr), description, pk)
msg := types.NewMsgEditValidator(sdk.ValAddress(valAddr), description)

// build and sign the transaction, then broadcast to Tendermint
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}

cmd.Flags().AddFlagSet(fsDescriptionEdit)
cmd.Flags().String(FlagPubKey, "", "The Bech32 encoded PubKey of the validator")
//cmd.Flags().AddFlagSet(fsCommissionUpdate)

return cmd
Expand Down
34 changes: 17 additions & 17 deletions x/staking/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,25 +165,16 @@ func handleMsgEditValidator(ctx sdk.Context, msg types.MsgEditValidator, k keepe
return nil, ErrNoValidatorFound(msg.ValidatorAddress.String())
}

if msg.Description != (Description{}) {
// replace all editable fields (clients should autofill existing values)
description, err := validator.Description.UpdateDescription(msg.Description)
if err != nil {
return nil, err
}

validator.Description = description
k.SetValidator(ctx, validator)
}
if msg.PubKey != nil && len(msg.PubKey.Bytes()) != 0 {
if validator.ConsPubKey.Equals(msg.PubKey) {
return nil, ErrPubkeyEqual(msg.PubKey.Address().String())
pk, err := types.GetConsPubKeyBech32(msg.Details)
if err == nil && tmtypes.HigherThanJupiter(ctx.BlockHeight()) {
if validator.ConsPubKey.Equals(pk) {
return nil, ErrPubkeyEqual(pk.Address().String())
}
if _, found := k.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(msg.PubKey)); found {
if _, found := k.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(pk)); found {
return nil, ErrValidatorPubKeyExists()
}
if ctx.ConsensusParams() != nil {
tmPubKey := tmtypes.TM2PB.PubKey(msg.PubKey)
tmPubKey := tmtypes.TM2PB.PubKey(pk)
if !StringInSlice(tmPubKey.Type, ctx.ConsensusParams().Validator.PubKeyTypes) {
return nil, ErrValidatorPubKeyTypeNotSupported(tmPubKey.Type,
ctx.ConsensusParams().Validator.PubKeyTypes)
Expand All @@ -192,12 +183,21 @@ func handleMsgEditValidator(ctx sdk.Context, msg types.MsgEditValidator, k keepe
k.SetChangePubkey(ctx, validator.OperatorAddress, validator.GetConsPubKey())
oldConsAddr := validator.GetConsAddr()

validator.ConsPubKey = msg.PubKey
validator.ConsPubKey = pk
newConsAddr := validator.GetConsAddr()
k.SetValidator(ctx, validator)
k.SetValidatorByConsAddr(ctx, validator)
k.DeleteValidatorByConsAddr(ctx, oldConsAddr)
k.AfterValidatorPubkeyChanged(ctx, oldConsAddr, newConsAddr, msg.PubKey)
k.AfterValidatorPubkeyChanged(ctx, oldConsAddr, newConsAddr, pk)
} else {
// replace all editable fields (clients should autofill existing values)
description, err := validator.Description.UpdateDescription(msg.Description)
if err != nil {
return nil, err
}

validator.Description = description
k.SetValidator(ctx, validator)
}

ctx.EventManager().EmitEvents(sdk.Events{
Expand Down
2 changes: 1 addition & 1 deletion x/staking/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func TestEditValidatorDecreaseMinSelfDelegation(t *testing.T) {
SharesFromDefaultMSD, false)

// edit validator
msgEditValidator := NewMsgEditValidator(validatorAddr, Description{Moniker: "moniker"}, nil)
msgEditValidator := NewMsgEditValidator(validatorAddr, Description{Moniker: "moniker"})
require.Nil(t, msgEditValidator.ValidateBasic())

// no one could change msd
Expand Down
6 changes: 2 additions & 4 deletions x/staking/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,13 @@ func (msg MsgCreateValidator) ValidateBasic() error {
type MsgEditValidator struct {
Description
ValidatorAddress sdk.ValAddress `json:"address" yaml:"address"`
PubKey crypto.PubKey `json:"pubkey" yaml:"pubkey"`
}

// NewMsgEditValidator creates a msg of edit-validator
func NewMsgEditValidator(valAddr sdk.ValAddress, description Description, pubKey crypto.PubKey) MsgEditValidator {
func NewMsgEditValidator(valAddr sdk.ValAddress, description Description) MsgEditValidator {
return MsgEditValidator{
Description: description,
ValidatorAddress: valAddr,
PubKey: pubKey,
}
}

Expand All @@ -163,7 +161,7 @@ func (msg MsgEditValidator) ValidateBasic() error {
return ErrNilValidatorAddr()
}

if msg.Description == (Description{}) && (msg.PubKey == nil || len(msg.PubKey.Bytes()) == 0) {
if msg.Description == (Description{}) {
return ErrDescriptionAndPubkeyIsEmpty()
}

Expand Down
2 changes: 1 addition & 1 deletion x/staking/types/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func TestMsgEditValidator(t *testing.T) {

for _, tc := range tests {
description := NewDescription(tc.moniker, tc.identity, tc.website, tc.details)
msg := NewMsgEditValidator(tc.validatorAddr, description, nil)
msg := NewMsgEditValidator(tc.validatorAddr, description)
if tc.expectPass {
require.Nil(t, msg.ValidateBasic(), "test: %v", tc.name)
checkMsg(t, msg, "edit_validator")
Expand Down

0 comments on commit 1966b3f

Please sign in to comment.