Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(x/staking): check duplicate addresses in StakeAuthorization's params #18655

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions x/staking/types/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
8 changes: 8 additions & 0 deletions x/staking/types/authz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}))
Expand Down
Loading