From 9443fc10515c6d69dfd6322a8a926898c9f9590e Mon Sep 17 00:00:00 2001 From: atheeshp <59333759+atheeshp@users.noreply.github.com> Date: Thu, 3 Aug 2023 17:01:14 +0530 Subject: [PATCH] refactor(x/staking): migrate `ValidatorByConsAddr` key to collections (#17260) --- CHANGELOG.md | 11 ++++++----- x/staking/keeper/keeper.go | 20 ++++++++++++++------ x/staking/keeper/validator.go | 12 ++++++------ x/staking/migrations/v2/keys.go | 18 ++++++++++++++++-- x/staking/migrations/v2/store_test.go | 2 +- x/staking/types/keys.go | 12 +++--------- 6 files changed, 46 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a78ac86f0e61..78630e85b540 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,18 +60,19 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes +* (x/staking) [#17260](https://github.com/cosmos/cosmos-sdk/pull/17260) Use collections for `ValidatorByConsAddr`: + * remove from `types`: `GetValidatorByConsAddrKey` * (x/staking) [#17248](https://github.com/cosmos/cosmos-sdk/pull/17248) Use collections for `UnbondingType`. * remove from `types`: `GetUnbondingTypeKey`. * (client) [#17259](https://github.com/cosmos/cosmos-sdk/pull/17259) Remove deprecated `clientCtx.PrintObjectLegacy`. Use `clientCtx.PrintProto` or `clientCtx.PrintRaw` instead. * (x/distribution) [#17115](https://github.com/cosmos/cosmos-sdk/pull/17115) Use collections for `PreviousProposer` and `ValidatorSlashEvents`: * remove from `Keeper`: `GetPreviousProposerConsAddr`, `SetPreviousProposerConsAddr`, `GetValidatorHistoricalReferenceCount`, `GetValidatorSlashEvent`, `SetValidatorSlashEvent`. -* (x/slashing) [17063](https://github.com/cosmos/cosmos-sdk/pull/17063) Use collections for `HistoricalInfo`: -* (x/feegrant) [16535](https://github.com/cosmos/cosmos-sdk/pull/16535) Use collections for `FeeAllowance`, `FeeAllowanceQueue`. -* (x/staking) [17063](https://github.com/cosmos/cosmos-sdk/pull/17063) Use collections for `HistoricalInfo`: +* (x/feegrant) [#16535](https://github.com/cosmos/cosmos-sdk/pull/16535) Use collections for `FeeAllowance`, `FeeAllowanceQueue`. +* (x/staking) [#17063](https://github.com/cosmos/cosmos-sdk/pull/17063) Use collections for `HistoricalInfo`: * remove `Keeper`: `GetHistoricalInfo`, `SetHistoricalInfo`, -* (x/staking) [17062](https://github.com/cosmos/cosmos-sdk/pull/17062) Use collections for `ValidatorUpdates`: +* (x/staking) [#17062](https://github.com/cosmos/cosmos-sdk/pull/17062) Use collections for `ValidatorUpdates`: * remove `Keeper`: `SetValidatorUpdates`, `GetValidatorUpdates` -* (x/slashing) [17023](https://github.com/cosmos/cosmos-sdk/pull/17023) Use collections for `ValidatorSigningInfo`: +* (x/slashing) [#17023](https://github.com/cosmos/cosmos-sdk/pull/17023) Use collections for `ValidatorSigningInfo`: * remove `Keeper`: `SetValidatorSigningInfo`, `GetValidatorSigningInfo`, `IterateValidatorSigningInfos` * (x/staking) [#17026](https://github.com/cosmos/cosmos-sdk/pull/17026) Use collections for `LastTotalPower`: * remove `Keeper`: `SetLastTotalPower`, `GetLastTotalPower` diff --git a/x/staking/keeper/keeper.go b/x/staking/keeper/keeper.go index d2f6f04c83b4..938dfa855ece 100644 --- a/x/staking/keeper/keeper.go +++ b/x/staking/keeper/keeper.go @@ -5,6 +5,7 @@ import ( "fmt" "cosmossdk.io/collections" + collcodec "cosmossdk.io/collections/codec" addresscodec "cosmossdk.io/core/address" storetypes "cosmossdk.io/core/store" "cosmossdk.io/log" @@ -32,12 +33,13 @@ type Keeper struct { validatorAddressCodec addresscodec.Codec consensusAddressCodec addresscodec.Codec - Schema collections.Schema - HistoricalInfo collections.Map[uint64, types.HistoricalInfo] - LastTotalPower collections.Item[math.Int] - ValidatorUpdates collections.Item[types.ValidatorUpdates] - DelegationsByValidator collections.Map[collections.Pair[sdk.ValAddress, sdk.AccAddress], []byte] - UnbondingType collections.Map[uint64, uint64] + Schema collections.Schema + HistoricalInfo collections.Map[uint64, types.HistoricalInfo] + LastTotalPower collections.Item[math.Int] + ValidatorUpdates collections.Item[types.ValidatorUpdates] + DelegationsByValidator collections.Map[collections.Pair[sdk.ValAddress, sdk.AccAddress], []byte] + ValidatorByConsensusAddress collections.Map[sdk.ConsAddress, sdk.ValAddress] + UnbondingType collections.Map[uint64, uint64] } // NewKeeper creates a new staking Keeper instance @@ -87,6 +89,12 @@ func NewKeeper( collections.PairKeyCodec(sdk.LengthPrefixedAddressKey(sdk.ValAddressKey), sdk.AccAddressKey), // nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility collections.BytesValue, ), + ValidatorByConsensusAddress: collections.NewMap( + sb, types.ValidatorsByConsAddrKey, + "validator_by_cons_addr", + sdk.LengthPrefixedAddressKey(sdk.ConsAddressKey), // nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility + collcodec.KeyToValueCodec(sdk.ValAddressKey), + ), UnbondingType: collections.NewMap(sb, types.UnbondingTypeKey, "unbonding_type", collections.Uint64Key, collections.Uint64Value), } diff --git a/x/staking/keeper/validator.go b/x/staking/keeper/validator.go index 4db228ec1ecb..e5dd3c85513d 100644 --- a/x/staking/keeper/validator.go +++ b/x/staking/keeper/validator.go @@ -9,6 +9,7 @@ import ( gogotypes "github.com/cosmos/gogoproto/types" + "cosmossdk.io/collections" corestore "cosmossdk.io/core/store" errorsmod "cosmossdk.io/errors" "cosmossdk.io/math" @@ -44,9 +45,8 @@ func (k Keeper) mustGetValidator(ctx context.Context, addr sdk.ValAddress) types // GetValidatorByConsAddr gets a single validator by consensus address func (k Keeper) GetValidatorByConsAddr(ctx context.Context, consAddr sdk.ConsAddress) (validator types.Validator, err error) { - store := k.storeService.OpenKVStore(ctx) - opAddr, err := store.Get(types.GetValidatorByConsAddrKey(consAddr)) - if err != nil { + opAddr, err := k.ValidatorByConsensusAddress.Get(ctx, consAddr) + if err != nil && !errors.Is(err, collections.ErrNotFound) { return validator, err } @@ -79,8 +79,8 @@ func (k Keeper) SetValidatorByConsAddr(ctx context.Context, validator types.Vali if err != nil { return err } - store := k.storeService.OpenKVStore(ctx) - return store.Set(types.GetValidatorByConsAddrKey(consPk), validator.GetOperator()) + + return k.ValidatorByConsensusAddress.Set(ctx, consPk, validator.GetOperator()) } // SetValidatorByPowerIndex sets a validator by power index @@ -219,7 +219,7 @@ func (k Keeper) RemoveValidator(ctx context.Context, address sdk.ValAddress) err return err } - if err = store.Delete(types.GetValidatorByConsAddrKey(valConsAddr)); err != nil { + if err = k.ValidatorByConsensusAddress.Remove(ctx, valConsAddr); err != nil { return err } diff --git a/x/staking/migrations/v2/keys.go b/x/staking/migrations/v2/keys.go index 57ccb0aebeea..b115ed5492fd 100644 --- a/x/staking/migrations/v2/keys.go +++ b/x/staking/migrations/v2/keys.go @@ -1,15 +1,29 @@ package v2 -import "strconv" +import ( + "strconv" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/address" +) const ( // ModuleName is the name of the module ModuleName = "staking" ) -var HistoricalInfoKey = []byte{0x50} // prefix for the historical info +var ( + ValidatorsByConsAddrKey = []byte{0x22} // prefix for validators by consensus address + HistoricalInfoKey = []byte{0x50} // prefix for the historical info +) // GetHistoricalInfoKey returns a key prefix for indexing HistoricalInfo objects. func GetHistoricalInfoKey(height int64) []byte { return append(HistoricalInfoKey, []byte(strconv.FormatInt(height, 10))...) } + +// GetValidatorByConsAddrKey creates the key for the validator with pubkey +// VALUE: validator operator address ([]byte) +func GetValidatorByConsAddrKey(addr sdk.ConsAddress) []byte { + return append(ValidatorsByConsAddrKey, address.MustLengthPrefix(addr)...) +} diff --git a/x/staking/migrations/v2/store_test.go b/x/staking/migrations/v2/store_test.go index 20c55e5bfcbb..da808b2a3aa8 100644 --- a/x/staking/migrations/v2/store_test.go +++ b/x/staking/migrations/v2/store_test.go @@ -59,7 +59,7 @@ func TestStoreMigration(t *testing.T) { { "ValidatorsByConsAddrKey", v1.GetValidatorByConsAddrKey(consAddr), - types.GetValidatorByConsAddrKey(consAddr), + v2.GetValidatorByConsAddrKey(consAddr), }, { "ValidatorsByPowerIndexKey", diff --git a/x/staking/types/keys.go b/x/staking/types/keys.go index 4bbc84f3babb..8572eec7b033 100644 --- a/x/staking/types/keys.go +++ b/x/staking/types/keys.go @@ -34,9 +34,9 @@ var ( LastValidatorPowerKey = []byte{0x11} // prefix for each key to a validator index, for bonded validators LastTotalPowerKey = collections.NewPrefix(18) // prefix for the total power - ValidatorsKey = []byte{0x21} // prefix for each key to a validator - ValidatorsByConsAddrKey = []byte{0x22} // prefix for each key to a validator index, by pubkey - ValidatorsByPowerIndexKey = []byte{0x23} // prefix for each key to a validator index, sorted by power + ValidatorsKey = []byte{0x21} // prefix for each key to a validator + ValidatorsByConsAddrKey = collections.NewPrefix(34) // prefix for each key to a validator index, by pubkey + ValidatorsByPowerIndexKey = []byte{0x23} // prefix for each key to a validator index, sorted by power DelegationKey = []byte{0x31} // key for a delegation UnbondingDelegationKey = []byte{0x32} // key for an unbonding-delegation @@ -84,12 +84,6 @@ func GetValidatorKey(operatorAddr sdk.ValAddress) []byte { return append(ValidatorsKey, address.MustLengthPrefix(operatorAddr)...) } -// GetValidatorByConsAddrKey creates the key for the validator with pubkey -// VALUE: validator operator address ([]byte) -func GetValidatorByConsAddrKey(addr sdk.ConsAddress) []byte { - return append(ValidatorsByConsAddrKey, address.MustLengthPrefix(addr)...) -} - // AddressFromValidatorsKey creates the validator operator address from ValidatorsKey func AddressFromValidatorsKey(key []byte) []byte { kv.AssertKeyAtLeastLength(key, 3)