Skip to content

Commit

Permalink
update validator msg
Browse files Browse the repository at this point in the history
  • Loading branch information
Keefe Liu committed Dec 6, 2022
1 parent 0d22fb2 commit 69a8246
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 32 deletions.
5 changes: 4 additions & 1 deletion x/genutil/types/genesis_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
var (
pk1 = ed25519.GenPrivKey().PubKey()
pk2 = ed25519.GenPrivKey().PubKey()

// TODO: change to random key
blsPk = "ac1e598ae0ccbeeaafa31bc6faefa85c2ae3138699cac79169cd718f1a38445201454ec092a86f200e08a15266bdc6e9"
)

func TestNetGenesisState(t *testing.T) {
Expand Down Expand Up @@ -59,7 +62,7 @@ func TestValidateGenesisMultipleMessages(t *testing.T) {
func TestValidateGenesisBadMessage(t *testing.T) {
desc := stakingtypes.NewDescription("testname", "", "", "", "")

msg1 := stakingtypes.NewMsgEditValidator(sdk.ValAddress(pk1.Address()), desc, nil, nil)
msg1 := stakingtypes.NewMsgEditValidator(sdk.ValAddress(pk1.Address()), sdk.AccAddress(pk1.Address()), blsPk, desc, nil, nil)

txGen := simapp.MakeTestEncodingConfig().TxConfig
txBuilder := txGen.NewTxBuilder()
Expand Down
2 changes: 1 addition & 1 deletion x/staking/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestStakingMsgs(t *testing.T) {

// edit the validator
description = types.NewDescription("bar_moniker", "", "", "", "")
editValidatorMsg := types.NewMsgEditValidator(sdk.ValAddress(addr1), description, nil, nil)
editValidatorMsg := types.NewMsgEditValidator(sdk.ValAddress(addr1), sdk.AccAddress(addr1), blsPk, description, nil, nil)

header = tmproto.Header{Height: app.LastBlockHeight() + 1}
_, _, err = simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, []sdk.Msg{editValidatorMsg}, "", []uint64{0}, []uint64{1}, true, true, priv1)
Expand Down
3 changes: 3 additions & 0 deletions x/staking/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const (
FlagGenesisFormat = "genesis-format"
FlagNodeID = "node-id"
FlagIP = "ip"

FlagSelfDelegator = "self-delegator"
FlagBlsPubkey = "bls-pubkey"
)

// common flagsets to add to various functions
Expand Down
13 changes: 12 additions & 1 deletion x/staking/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,18 @@ func NewEditValidatorCmd() *cobra.Command {
newMinSelfDelegation = &msb
}

msg := types.NewMsgEditValidator(sdk.ValAddress(valAddr), description, newRate, newMinSelfDelegation)
selfDelegator, _ := cmd.Flags().GetString(FlagSelfDelegator)
delegator, err := sdk.AccAddressFromBech32(selfDelegator)
if err != nil {
return fmt.Errorf("invalid self delegator address: %v", err)
}

blsPk, _ := cmd.Flags().GetString(FlagBlsPubkey)
if len(blsPk) == 0 {
return fmt.Errorf("empty validator bls public key")
}

msg := types.NewMsgEditValidator(sdk.ValAddress(valAddr), delegator, blsPk, description, newRate, newMinSelfDelegation)

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
Expand Down
3 changes: 3 additions & 0 deletions x/staking/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ var (
commissionRates = types.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())

PKs = simapp.CreateTestPubKeys(500)

// TODO: change to random key
blsPk = "ac1e598ae0ccbeeaafa31bc6faefa85c2ae3138699cac79169cd718f1a38445201454ec092a86f200e08a15266bdc6e9"
)

// getBaseSimappWithCustomKeeper Returns a simapp with custom StakingKeeper
Expand Down
5 changes: 4 additions & 1 deletion x/staking/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,10 @@ func SimulateMsgEditValidator(ak types.AccountKeeper, bk types.BankKeeper, k kee
simtypes.RandStringOfLength(r, 10),
)

msg := types.NewMsgEditValidator(address, description, &newCommissionRate, nil)
// TODO: generate random bls pubkey
blsPk := "ac1e598ae0ccbeeaafa31bc6faefa85c2ae3138699cac79169cd718f1a38445201454ec092a86f200e08a15266bdc6e9"

msg := types.NewMsgEditValidator(address, sdk.AccAddress(address), blsPk, description, &newCommissionRate, nil)

txCtx := simulation.OperationInput{
R: r,
Expand Down
1 change: 1 addition & 0 deletions x/staking/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ var (
ErrValidatorBlsPubkeyExists = sdkerrors.Register(ModuleName, 43, "validator already exist for this bls pubkey; must use new validator bls pubkey")
ErrDelegationNotAllowed = sdkerrors.Register(ModuleName, 44, "delegation is not allowed")
ErrRedelegationNotAllowed = sdkerrors.Register(ModuleName, 45, "redelegation is not allowed")
ErrEmptyValidatorBlsPubKey = sdkerrors.Register(ModuleName, 46, "empty validator bls public key")
)
61 changes: 34 additions & 27 deletions x/staking/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ var (
// NewMsgCreateValidator creates a new MsgCreateValidator instance.
// Delegator address and validator address are the same.
func NewMsgCreateValidator(
valAddr sdk.ValAddress, pubKey cryptotypes.PubKey, //nolint:interfacer
from sdk.AccAddress,
valAddr sdk.ValAddress, selfDelAddr sdk.AccAddress, pubKey cryptotypes.PubKey, blsPubkey string, //nolint:interfacer
selfDelegation sdk.Coin, description Description, commission CommissionRates, minSelfDelegation math.Int,
) (*MsgCreateValidator, error) {
var pkAny *codectypes.Any
Expand All @@ -46,12 +47,14 @@ func NewMsgCreateValidator(
}
return &MsgCreateValidator{
Description: description,
DelegatorAddress: sdk.AccAddress(valAddr).String(),
DelegatorAddress: selfDelAddr.String(),
ValidatorAddress: valAddr.String(),
Pubkey: pkAny,
Value: selfDelegation,
Commission: commission,
MinSelfDelegation: minSelfDelegation,
From: from.String(),
BlsPubkey: blsPubkey,
}, nil
}

Expand All @@ -63,20 +66,9 @@ func (msg MsgCreateValidator) Type() string { return TypeMsgCreateValidator }

// GetSigners implements the sdk.Msg interface. It returns the address(es) that
// must sign over msg.GetSignBytes().
// If the validator address is not same as delegator's, then the validator must
// sign the msg as well.
func (msg MsgCreateValidator) GetSigners() []sdk.AccAddress {
// delegator is first signer so delegator pays fees
delegator, _ := sdk.AccAddressFromBech32(msg.DelegatorAddress)
addrs := []sdk.AccAddress{delegator}
valAddr, _ := sdk.ValAddressFromBech32(msg.ValidatorAddress)

valAccAddr := sdk.AccAddress(valAddr)
if !delegator.Equals(valAccAddr) {
addrs = append(addrs, valAccAddr)
}

return addrs
from, _ := sdk.AccAddressFromBech32(msg.From)
return []sdk.AccAddress{from}
}

// GetSignBytes returns the message bytes to sign over.
Expand All @@ -88,22 +80,24 @@ func (msg MsgCreateValidator) GetSignBytes() []byte {
// ValidateBasic implements the sdk.Msg interface.
func (msg MsgCreateValidator) ValidateBasic() error {
// note that unmarshaling from bech32 ensures both non-empty and valid
delAddr, err := sdk.AccAddressFromBech32(msg.DelegatorAddress)
if err != nil {
if _, err := sdk.ValAddressFromBech32(msg.From); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid from address: %s", err)
}
if _, err := sdk.AccAddressFromBech32(msg.DelegatorAddress); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid delegator address: %s", err)
}
valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddress)
if err != nil {
if _, err := sdk.ValAddressFromBech32(msg.ValidatorAddress); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid validator address: %s", err)
}
if !sdk.AccAddress(valAddr).Equals(delAddr) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "validator address is invalid")
}

if msg.Pubkey == nil {
return ErrEmptyValidatorPubKey
}

if len(msg.BlsPubkey) == 0 {
return ErrEmptyValidatorBlsPubKey
}

if !msg.Value.IsValid() || !msg.Value.Amount.IsPositive() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid delegation amount")
}
Expand Down Expand Up @@ -143,12 +137,17 @@ func (msg MsgCreateValidator) UnpackInterfaces(unpacker codectypes.AnyUnpacker)
// NewMsgEditValidator creates a new MsgEditValidator instance
//
//nolint:interfacer
func NewMsgEditValidator(valAddr sdk.ValAddress, description Description, newRate *sdk.Dec, newMinSelfDelegation *math.Int) *MsgEditValidator {
func NewMsgEditValidator(
valAddr sdk.ValAddress, delegator sdk.AccAddress, blsPubkey string,
description Description, newRate *sdk.Dec, newMinSelfDelegation *math.Int,
) *MsgEditValidator {
return &MsgEditValidator{
Description: description,
CommissionRate: newRate,
ValidatorAddress: valAddr.String(),
MinSelfDelegation: newMinSelfDelegation,
Description: description,
CommissionRate: newRate,
ValidatorAddress: valAddr.String(),
MinSelfDelegation: newMinSelfDelegation,
SelfDelegationAddress: delegator.String(),
BlsPubkey: blsPubkey,
}
}

Expand Down Expand Up @@ -176,6 +175,14 @@ func (msg MsgEditValidator) ValidateBasic() error {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid validator address: %s", err)
}

if _, err := sdk.ValAddressFromBech32(msg.SelfDelegationAddress); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid validator self delegation address: %s", err)
}

if len(msg.BlsPubkey) == 0 {
return ErrEmptyValidatorBlsPubKey
}

if msg.Description == (Description{}) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "empty description")
}
Expand Down
5 changes: 4 additions & 1 deletion x/staking/types/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ func TestMsgEditValidator(t *testing.T) {
description := types.NewDescription(tc.moniker, tc.identity, tc.website, tc.securityContact, tc.details)
newRate := sdk.ZeroDec()

msg := types.NewMsgEditValidator(tc.validatorAddr, description, &newRate, &tc.minSelfDelegation)
// TODO: move bls pubkey into tests struct
blsPk := "ac1e598ae0ccbeeaafa31bc6faefa85c2ae3138699cac79169cd718f1a38445201454ec092a86f200e08a15266bdc6e9"

msg := types.NewMsgEditValidator(tc.validatorAddr, sdk.AccAddress(tc.validatorAddr), blsPk, description, &newRate, &tc.minSelfDelegation)
if tc.expectPass {
require.Nil(t, msg.ValidateBasic(), "test: %v", tc.name)
} else {
Expand Down

0 comments on commit 69a8246

Please sign in to comment.