From a7977b8633c8aef9673fc13c51af9d49b68f7fe7 Mon Sep 17 00:00:00 2001 From: Likhita Polavarapu <78951027+likhita-809@users.noreply.github.com> Date: Thu, 3 Aug 2023 15:41:23 +0530 Subject: [PATCH] refactor(x/staking): migrate UnbondingType to collections (#17248) --- CHANGELOG.md | 2 ++ x/staking/keeper/keeper.go | 2 ++ x/staking/keeper/unbonding.go | 23 ++++++----------------- x/staking/types/keys.go | 13 +++---------- 4 files changed, 13 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9908d7d61d4c..a78ac86f0e61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes +* (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`. diff --git a/x/staking/keeper/keeper.go b/x/staking/keeper/keeper.go index 64ad687d1f6f..d2f6f04c83b4 100644 --- a/x/staking/keeper/keeper.go +++ b/x/staking/keeper/keeper.go @@ -37,6 +37,7 @@ type Keeper struct { 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] } // NewKeeper creates a new staking Keeper instance @@ -86,6 +87,7 @@ func NewKeeper( collections.PairKeyCodec(sdk.LengthPrefixedAddressKey(sdk.ValAddressKey), sdk.AccAddressKey), // nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility collections.BytesValue, ), + UnbondingType: collections.NewMap(sb, types.UnbondingTypeKey, "unbonding_type", collections.Uint64Key, collections.Uint64Value), } schema, err := sb.Build() diff --git a/x/staking/keeper/unbonding.go b/x/staking/keeper/unbonding.go index 1813797b9761..0c457733c869 100644 --- a/x/staking/keeper/unbonding.go +++ b/x/staking/keeper/unbonding.go @@ -3,7 +3,9 @@ package keeper import ( "context" "encoding/binary" + "errors" + "cosmossdk.io/collections" errorsmod "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" @@ -44,30 +46,17 @@ func (k Keeper) DeleteUnbondingIndex(ctx context.Context, id uint64) error { // GetUnbondingType returns the enum type of unbonding which is any of // {UnbondingDelegation | Redelegation | ValidatorUnbonding} func (k Keeper) GetUnbondingType(ctx context.Context, id uint64) (unbondingType types.UnbondingType, err error) { - store := k.storeService.OpenKVStore(ctx) - - bz, err := store.Get(types.GetUnbondingTypeKey(id)) - if err != nil { - return unbondingType, err - } - - if bz == nil { + ubdType, err := k.UnbondingType.Get(ctx, id) + if errors.Is(err, collections.ErrNotFound) { return unbondingType, types.ErrNoUnbondingType } - - return types.UnbondingType(binary.BigEndian.Uint64(bz)), nil + return types.UnbondingType(ubdType), err } // SetUnbondingType sets the enum type of unbonding which is any of // {UnbondingDelegation | Redelegation | ValidatorUnbonding} func (k Keeper) SetUnbondingType(ctx context.Context, id uint64, unbondingType types.UnbondingType) error { - store := k.storeService.OpenKVStore(ctx) - - // Convert into bytes for storage - bz := make([]byte, 8) - binary.BigEndian.PutUint64(bz, uint64(unbondingType)) - - return store.Set(types.GetUnbondingTypeKey(id), bz) + return k.UnbondingType.Set(ctx, id, uint64(unbondingType)) } // GetUnbondingDelegationByUnbondingID returns a unbonding delegation that has an unbonding delegation entry with a certain ID diff --git a/x/staking/types/keys.go b/x/staking/types/keys.go index 067073c1bdff..4bbc84f3babb 100644 --- a/x/staking/types/keys.go +++ b/x/staking/types/keys.go @@ -45,9 +45,9 @@ var ( RedelegationByValSrcIndexKey = []byte{0x35} // prefix for each key for an redelegation, by source validator operator RedelegationByValDstIndexKey = []byte{0x36} // prefix for each key for an redelegation, by destination validator operator - UnbondingIDKey = []byte{0x37} // key for the counter for the incrementing id for UnbondingOperations - UnbondingIndexKey = []byte{0x38} // prefix for an index for looking up unbonding operations by their IDs - UnbondingTypeKey = []byte{0x39} // prefix for an index containing the type of unbonding operations + UnbondingIDKey = []byte{0x37} // key for the counter for the incrementing id for UnbondingOperations + UnbondingIndexKey = []byte{0x38} // prefix for an index for looking up unbonding operations by their IDs + UnbondingTypeKey = collections.NewPrefix(57) // prefix for an index containing the type of unbonding operations UnbondingQueueKey = []byte{0x41} // prefix for the timestamps in unbonding queue RedelegationQueueKey = []byte{0x42} // prefix for the timestamps in redelegations queue @@ -71,13 +71,6 @@ const ( UnbondingType_ValidatorUnbonding ) -// GetUnbondingTypeKey returns a key for an index containing the type of unbonding operations -func GetUnbondingTypeKey(id uint64) []byte { - bz := make([]byte, 8) - binary.BigEndian.PutUint64(bz, id) - return append(UnbondingTypeKey, bz...) -} - // GetUnbondingIndexKey returns a key for the index for looking up UnbondingDelegations by the UnbondingDelegationEntries they contain func GetUnbondingIndexKey(id uint64) []byte { bz := make([]byte, 8)