Skip to content

Commit

Permalink
imp(staking): use slices.Contains above instead of manually looping
Browse files Browse the repository at this point in the history
  • Loading branch information
luchenqun committed Nov 21, 2023
1 parent de5a9dd commit 6cc1bbb
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions x/staking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"context"
"errors"
"slices"
"strconv"
"time"

Expand Down Expand Up @@ -61,21 +62,14 @@ func (k msgServer) CreateValidator(ctx context.Context, msg *types.MsgCreateVali

pk, ok := msg.Pubkey.GetCachedValue().(cryptotypes.PubKey)
if !ok {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidType, "Expecting cryptotypes.PubKey, got %T", pk)
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidType, "Expecting cryptotypes.PubKey, got %T", msg.Pubkey.GetCachedValue())
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
cp := sdkCtx.ConsensusParams()
if cp.Validator != nil {
pkType := pk.Type()
hasKeyType := false
for _, keyType := range cp.Validator.PubKeyTypes {
if pkType == keyType {
hasKeyType = true
break
}
}
if !hasKeyType {
if !slices.Contains(cp.Validator.PubKeyTypes, pkType) {
return nil, errorsmod.Wrapf(
types.ErrValidatorPubKeyTypeNotSupported,
"got: %s, expected: %s", pk.Type(), cp.Validator.PubKeyTypes,
Expand Down

0 comments on commit 6cc1bbb

Please sign in to comment.