refactor(x/staking): Use gogotypes Int64Value instead of bytes for LastValidatorPower (#17604)
This commit is contained in:
parent
5621d9d807
commit
eb853117a9
@ -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
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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",
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user