From eb853117a97cb07ec1b6d487ad24f7a14488a23e Mon Sep 17 00:00:00 2001 From: Likhita Polavarapu <78951027+likhita-809@users.noreply.github.com> Date: Sat, 2 Sep 2023 02:58:17 +0530 Subject: [PATCH] refactor(x/staking): Use gogotypes Int64Value instead of bytes for LastValidatorPower (#17604) --- x/staking/genesis.go | 3 ++- x/staking/keeper/keeper.go | 7 ++++-- x/staking/keeper/keeper_test.go | 6 ++--- x/staking/keeper/val_state_change.go | 6 +++-- x/staking/keeper/validator.go | 35 +++++----------------------- 5 files changed, 20 insertions(+), 37 deletions(-) diff --git a/x/staking/genesis.go b/x/staking/genesis.go index 005ec107a5b4..8081b117c99f 100644 --- a/x/staking/genesis.go +++ b/x/staking/genesis.go @@ -4,6 +4,7 @@ import ( "fmt" cmttypes "github.com/cometbft/cometbft/types" + gogotypes "github.com/cosmos/gogoproto/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -13,7 +14,7 @@ import ( // WriteValidators returns a slice of bonded genesis validators. func WriteValidators(ctx sdk.Context, keeper *keeper.Keeper) (vals []cmttypes.GenesisValidator, returnErr error) { - err := keeper.LastValidatorPower.Walk(ctx, nil, func(key, value []byte) (bool, error) { + err := keeper.LastValidatorPower.Walk(ctx, nil, func(key []byte, _ gogotypes.Int64Value) (bool, error) { validator, err := keeper.GetValidator(ctx, key) if err != nil { return true, err diff --git a/x/staking/keeper/keeper.go b/x/staking/keeper/keeper.go index c389dc012e28..2221feab94c8 100644 --- a/x/staking/keeper/keeper.go +++ b/x/staking/keeper/keeper.go @@ -5,6 +5,8 @@ import ( "fmt" "time" + gogotypes "github.com/cosmos/gogoproto/types" + "cosmossdk.io/collections" collcodec "cosmossdk.io/collections/codec" addresscodec "cosmossdk.io/core/address" @@ -51,7 +53,7 @@ type Keeper struct { RedelegationsByValDst collections.Map[collections.Triple[[]byte, []byte, []byte], []byte] RedelegationsByValSrc collections.Map[collections.Triple[[]byte, []byte, []byte], []byte] UnbondingDelegationByValIndex collections.Map[collections.Pair[[]byte, []byte], []byte] - LastValidatorPower collections.Map[[]byte, []byte] + LastValidatorPower collections.Map[[]byte, gogotypes.Int64Value] } // NewKeeper creates a new staking Keeper instance @@ -147,7 +149,8 @@ func NewKeeper( ), collections.BytesValue, ), - LastValidatorPower: collections.NewMap(sb, types.LastValidatorPowerKey, "last_validator_power", sdk.LengthPrefixedBytesKey, collections.BytesValue), // sdk.LengthPrefixedBytesKey is needed to retain state compatibility + // key format is: 17 | lengthPrefixedBytes(valAddr) | power + LastValidatorPower: collections.NewMap(sb, types.LastValidatorPowerKey, "last_validator_power", sdk.LengthPrefixedBytesKey, codec.CollValue[gogotypes.Int64Value](cdc)), // sdk.LengthPrefixedBytesKey is needed to retain state compatibility // key format is: 54 | lengthPrefixedBytes(DstValAddr) | lengthPrefixedBytes(AccAddr) | lengthPrefixedBytes(SrcValAddr) RedelegationsByValDst: collections.NewMap( sb, types.RedelegationByValDstIndexKey, diff --git a/x/staking/keeper/keeper_test.go b/x/staking/keeper/keeper_test.go index 8b5fd08fc07e..6266960a827e 100644 --- a/x/staking/keeper/keeper_test.go +++ b/x/staking/keeper/keeper_test.go @@ -226,10 +226,10 @@ func (s *KeeperTestSuite) TestLastTotalPowerMigrationToColls() { s.key, 100, func(i int64) { - bz, err := s.cdc.Marshal(&gogotypes.Int64Value{Value: i}) - s.Require().NoError(err) + var intV gogotypes.Int64Value + intV.Value = i - err = s.stakingKeeper.LastValidatorPower.Set(s.ctx, valAddrs[i], bz) + err = s.stakingKeeper.LastValidatorPower.Set(s.ctx, valAddrs[i], intV) s.Require().NoError(err) }, "f28811f2b0a0ab9db60cdcae93680faff9dbadd4a3a8a2d088bb19b0428ad3a9", diff --git a/x/staking/keeper/val_state_change.go b/x/staking/keeper/val_state_change.go index c0c996a28edb..e73f4f869df1 100644 --- a/x/staking/keeper/val_state_change.go +++ b/x/staking/keeper/val_state_change.go @@ -463,13 +463,15 @@ type validatorsByAddr map[string][]byte func (k Keeper) getLastValidatorsByAddr(ctx context.Context) (validatorsByAddr, error) { last := make(validatorsByAddr) - err := k.LastValidatorPower.Walk(ctx, nil, func(key, value []byte) (bool, error) { + err := k.LastValidatorPower.Walk(ctx, nil, func(key []byte, value gogotypes.Int64Value) (bool, error) { valAddrStr, err := k.validatorAddressCodec.BytesToString(key) if err != nil { return true, err } - last[valAddrStr] = value + intV := value.GetValue() + bz := k.cdc.MustMarshal(&gogotypes.Int64Value{Value: intV}) + last[valAddrStr] = bz return false, nil }) if err != nil { diff --git a/x/staking/keeper/validator.go b/x/staking/keeper/validator.go index a37ccd2947ae..fb82f1929a79 100644 --- a/x/staking/keeper/validator.go +++ b/x/staking/keeper/validator.go @@ -337,31 +337,13 @@ func (k Keeper) ValidatorsPowerStoreIterator(ctx context.Context) (corestore.Ite // GetLastValidatorPower loads the last validator power. // Returns zero if the operator was not a validator last block. func (k Keeper) GetLastValidatorPower(ctx context.Context, operator sdk.ValAddress) (power int64, err error) { - bz, err := k.LastValidatorPower.Get(ctx, operator) - if err != nil { - return 0, err - } - - if bz == nil { - return 0, nil - } - - intV := gogotypes.Int64Value{} - err = k.cdc.Unmarshal(bz, &intV) - if err != nil { - return 0, err - } - - return intV.GetValue(), nil + intV, err := k.LastValidatorPower.Get(ctx, operator) + return intV.GetValue(), err } // SetLastValidatorPower sets the last validator power. func (k Keeper) SetLastValidatorPower(ctx context.Context, operator sdk.ValAddress, power int64) error { - bz, err := k.cdc.Marshal(&gogotypes.Int64Value{Value: power}) - if err != nil { - return err - } - return k.LastValidatorPower.Set(ctx, operator, bz) + return k.LastValidatorPower.Set(ctx, operator, gogotypes.Int64Value{Value: power}) } // DeleteLastValidatorPower deletes the last validator power. @@ -371,15 +353,10 @@ func (k Keeper) DeleteLastValidatorPower(ctx context.Context, operator sdk.ValAd // IterateLastValidatorPowers iterates over last validator powers. func (k Keeper) IterateLastValidatorPowers(ctx context.Context, handler func(operator sdk.ValAddress, power int64) (stop bool)) error { - err := k.LastValidatorPower.Walk(ctx, nil, func(key, value []byte) (bool, error) { + err := k.LastValidatorPower.Walk(ctx, nil, func(key []byte, value gogotypes.Int64Value) (bool, error) { addr := sdk.ValAddress(key) - intV := &gogotypes.Int64Value{} - - if err := k.cdc.Unmarshal(value, intV); err != nil { - return true, err - } - if handler(addr, intV.GetValue()) { + if handler(addr, value.GetValue()) { return true, nil } return false, nil @@ -401,7 +378,7 @@ func (k Keeper) GetLastValidators(ctx context.Context) (validators []types.Valid validators = make([]types.Validator, maxValidators) i := 0 - err = k.LastValidatorPower.Walk(ctx, nil, func(key, value []byte) (bool, error) { + err = k.LastValidatorPower.Walk(ctx, nil, func(key []byte, _ gogotypes.Int64Value) (bool, error) { // sanity check if i >= int(maxValidators) { panic("more validators than maxValidators found")