Skip to content

Commit 3b8aea5

Browse files
authored
feat(staking): skip minimum self delegation check at genesis block (#15)
1 parent 417d34d commit 3b8aea5

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

x/staking/client/cli/tx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ func newBuildCreateValidatorMsg(clientCtx client.Context, txf tx.Factory, fs *fl
411411
if err != nil {
412412
return txf, nil, err
413413
}
414-
if err := msg.Validate(valAc); err != nil {
414+
if err := msg.Validate(valAc, false); err != nil {
415415
return txf, nil, err
416416
}
417417

x/staking/keeper/msg_server.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ func (k msgServer) CreateValidator(ctx context.Context, msg *types.MsgCreateVali
3737
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid validator address: %s", err)
3838
}
3939

40-
if err := msg.Validate(k.validatorAddressCodec); err != nil {
40+
sdkCtx := sdk.UnwrapSDKContext(ctx)
41+
42+
// skip validation that if the token is greater than minimum self delegation amount at genesis block
43+
skipMinSelfDelValidation := sdkCtx.BlockHeight() == 0
44+
if err := msg.Validate(k.validatorAddressCodec, skipMinSelfDelValidation); err != nil {
4145
return nil, err
4246
}
4347

@@ -79,7 +83,6 @@ func (k msgServer) CreateValidator(ctx context.Context, msg *types.MsgCreateVali
7983
return nil, err
8084
}
8185

82-
sdkCtx := sdk.UnwrapSDKContext(ctx)
8386
cp := sdkCtx.ConsensusParams()
8487
if cp.Validator != nil {
8588
pkType := pk.Type()
@@ -149,7 +152,7 @@ func (k msgServer) CreateValidator(ctx context.Context, msg *types.MsgCreateVali
149152
}
150153

151154
// delegation amount must be greater than or equal to minimum self delegation when creating validator
152-
if msg.Value.Amount.LT(validator.MinSelfDelegation) {
155+
if msg.Value.Amount.LT(validator.MinSelfDelegation) && !skipMinSelfDelValidation {
153156
return nil, types.ErrSelfDelegationBelowMinimum
154157
}
155158

x/staking/keeper/msg_server_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ func (s *KeeperTestSuite) TestMsgCreateValidator() {
238238
for _, tc := range testCases {
239239
tc := tc
240240
s.T().Run(tc.name, func(t *testing.T) {
241-
_, err := msgServer.CreateValidator(ctx, tc.input)
241+
cachedCtx := ctx.WithBlockHeight(1)
242+
_, err := msgServer.CreateValidator(cachedCtx, tc.input)
242243
if tc.expErr {
243244
require.Error(err)
244245
require.Contains(err.Error(), tc.expErrMsg)

x/staking/types/msg.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func NewMsgCreateValidator(
4848
}
4949

5050
// Validate validates the MsgCreateValidator sdk msg.
51-
func (msg MsgCreateValidator) Validate(ac address.Codec) error {
51+
func (msg MsgCreateValidator) Validate(ac address.Codec, skipMinSelfDelValidation bool) error {
5252
// note that unmarshaling from bech32 ensures both non-empty and valid
5353
_, err := ac.StringToBytes(msg.ValidatorAddress)
5454
if err != nil {
@@ -82,7 +82,7 @@ func (msg MsgCreateValidator) Validate(ac address.Codec) error {
8282
)
8383
}
8484

85-
if msg.Value.Amount.LT(msg.MinSelfDelegation) {
85+
if msg.Value.Amount.LT(msg.MinSelfDelegation) && !skipMinSelfDelValidation {
8686
return ErrSelfDelegationBelowMinimum
8787
}
8888

0 commit comments

Comments
 (0)