Skip to content

Commit

Permalink
refactor(x/staking): Use gogotypes Int64Value instead of bytes for La…
Browse files Browse the repository at this point in the history
…stValidatorPower (#17604)
  • Loading branch information
likhita-809 authored Sep 1, 2023
1 parent 5621d9d commit eb85311
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 37 deletions.
3 changes: 2 additions & 1 deletion x/staking/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions x/staking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions x/staking/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 4 additions & 2 deletions x/staking/keeper/val_state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
35 changes: 6 additions & 29 deletions x/staking/keeper/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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")
Expand Down

0 comments on commit eb85311

Please sign in to comment.