From 9cd02a35e176e701caaae2049a0c950573d3581e Mon Sep 17 00:00:00 2001 From: lukema95 <867273263@qq.com> Date: Fri, 8 Dec 2023 05:46:22 +0800 Subject: [PATCH] feat: check StakeAuthorization params --- x/staking/types/authz.go | 10 ++++++++++ x/staking/types/authz_test.go | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/x/staking/types/authz.go b/x/staking/types/authz.go index cacf5e225dbb..d3c7425d6ade 100644 --- a/x/staking/types/authz.go +++ b/x/staking/types/authz.go @@ -164,14 +164,24 @@ func validateAllowAndDenyValidators(allowed, denied []sdk.ValAddress) ([]string, allowedValidators := make([]string, len(allowed)) if len(allowed) > 0 { + foundAllowedValidators := make(map[string]bool, len(allowed)) for i, validator := range allowed { + if foundAllowedValidators[validator.String()] { + return nil, nil, sdkerrors.ErrInvalidRequest.Wrapf("duplicate allowed validator address: %s", validator.String()) + } + foundAllowedValidators[validator.String()] = true allowedValidators[i] = validator.String() } return allowedValidators, nil, nil } deniedValidators := make([]string, len(denied)) + foundDeniedValidators := make(map[string]bool, len(denied)) for i, validator := range denied { + if foundDeniedValidators[validator.String()] { + return nil, nil, sdkerrors.ErrInvalidRequest.Wrapf("duplicate denied validator address: %s", validator.String()) + } + foundDeniedValidators[validator.String()] = true deniedValidators[i] = validator.String() } diff --git a/x/staking/types/authz_test.go b/x/staking/types/authz_test.go index 8ef9a9b3868e..fa6185293ec5 100644 --- a/x/staking/types/authz_test.go +++ b/x/staking/types/authz_test.go @@ -42,6 +42,14 @@ func TestAuthzAuthorizations(t *testing.T) { _, err = stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{val1}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, &coin100) require.Error(t, err) + // error duplicate allow list + _, err = stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val1}, []sdk.ValAddress{}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, &coin100) + require.ErrorContains(t, err, "duplicate allowed validator address") + + // error duplicate denied list + _, err = stakingtypes.NewStakeAuthorization([]sdk.ValAddress{}, []sdk.ValAddress{val1, val1}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, &coin100) + require.ErrorContains(t, err, "duplicate denied validator address") + // verify MethodName undelAuth, _ := stakingtypes.NewStakeAuthorization([]sdk.ValAddress{val1, val2}, []sdk.ValAddress{}, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_UNDELEGATE, &coin100) require.Equal(t, undelAuth.MsgTypeURL(), sdk.MsgTypeURL(&stakingtypes.MsgUndelegate{}))