From ff7ed27ad03d38979552d632c0f754378e89d514 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Fri, 4 Aug 2023 17:32:51 +0530 Subject: [PATCH 1/2] migrate UnbondingIndex to use collections --- x/staking/keeper/keeper.go | 2 ++ x/staking/keeper/unbonding.go | 31 ++++++++++++++++++------------- x/staking/types/keys.go | 13 +++---------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/x/staking/keeper/keeper.go b/x/staking/keeper/keeper.go index 64ad687d1f6f..5e58119c22cd 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] + UnbondingIndex collections.Map[uint64, []byte] } // 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, ), + UnbondingIndex: collections.NewMap(sb, types.UnbondingIndexKey, "unbonding_index", collections.Uint64Key, collections.BytesValue), } schema, err := sb.Build() diff --git a/x/staking/keeper/unbonding.go b/x/staking/keeper/unbonding.go index 1813797b9761..69c8f02319e4 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" @@ -37,8 +39,7 @@ func (k Keeper) IncrementUnbondingID(ctx context.Context) (unbondingID uint64, e // DeleteUnbondingIndex removes a mapping from UnbondingId to unbonding operation func (k Keeper) DeleteUnbondingIndex(ctx context.Context, id uint64) error { - store := k.storeService.OpenKVStore(ctx) - return store.Delete(types.GetUnbondingIndexKey(id)) + return k.UnbondingIndex.Remove(ctx, id) } // GetUnbondingType returns the enum type of unbonding which is any of @@ -74,8 +75,11 @@ func (k Keeper) SetUnbondingType(ctx context.Context, id uint64, unbondingType t func (k Keeper) GetUnbondingDelegationByUnbondingID(ctx context.Context, id uint64) (ubd types.UnbondingDelegation, err error) { store := k.storeService.OpenKVStore(ctx) - ubdKey, err := store.Get(types.GetUnbondingIndexKey(id)) + ubdKey, err := k.UnbondingIndex.Get(ctx, id) if err != nil { + if errors.Is(err, collections.ErrNotFound) { + return types.UnbondingDelegation{}, types.ErrNoUnbondingDelegation + } return types.UnbondingDelegation{}, err } @@ -105,8 +109,11 @@ func (k Keeper) GetUnbondingDelegationByUnbondingID(ctx context.Context, id uint func (k Keeper) GetRedelegationByUnbondingID(ctx context.Context, id uint64) (red types.Redelegation, err error) { store := k.storeService.OpenKVStore(ctx) - redKey, err := store.Get(types.GetUnbondingIndexKey(id)) + redKey, err := k.UnbondingIndex.Get(ctx, id) if err != nil { + if errors.Is(err, collections.ErrNotFound) { + return types.Redelegation{}, types.ErrNoRedelegation + } return types.Redelegation{}, err } @@ -136,8 +143,11 @@ func (k Keeper) GetRedelegationByUnbondingID(ctx context.Context, id uint64) (re func (k Keeper) GetValidatorByUnbondingID(ctx context.Context, id uint64) (val types.Validator, err error) { store := k.storeService.OpenKVStore(ctx) - valKey, err := store.Get(types.GetUnbondingIndexKey(id)) + valKey, err := k.UnbondingIndex.Get(ctx, id) if err != nil { + if errors.Is(err, collections.ErrNotFound) { + return types.Validator{}, types.ErrNoValidatorFound + } return types.Validator{}, err } @@ -167,7 +177,6 @@ func (k Keeper) GetValidatorByUnbondingID(ctx context.Context, id uint64) (val t // by the unbondingID of an UnbondingDelegationEntry that it contains Note, it does not // set the unbonding delegation itself, use SetUnbondingDelegation(ctx, ubd) for that func (k Keeper) SetUnbondingDelegationByUnbondingID(ctx context.Context, ubd types.UnbondingDelegation, id uint64) error { - store := k.storeService.OpenKVStore(ctx) delAddr, err := k.authKeeper.AddressCodec().StringToBytes(ubd.DelegatorAddress) if err != nil { return err @@ -178,7 +187,7 @@ func (k Keeper) SetUnbondingDelegationByUnbondingID(ctx context.Context, ubd typ } ubdKey := types.GetUBDKey(delAddr, valAddr) - if err = store.Set(types.GetUnbondingIndexKey(id), ubdKey); err != nil { + if err = k.UnbondingIndex.Set(ctx, id, ubdKey); err != nil { return err } @@ -189,8 +198,6 @@ func (k Keeper) SetUnbondingDelegationByUnbondingID(ctx context.Context, ubd typ // SetRedelegationByUnbondingID sets an index to look up an Redelegation by the unbondingID of an RedelegationEntry that it contains // Note, it does not set the redelegation itself, use SetRedelegation(ctx, red) for that func (k Keeper) SetRedelegationByUnbondingID(ctx context.Context, red types.Redelegation, id uint64) error { - store := k.storeService.OpenKVStore(ctx) - delAddr, err := k.authKeeper.AddressCodec().StringToBytes(red.DelegatorAddress) if err != nil { return err @@ -207,7 +214,7 @@ func (k Keeper) SetRedelegationByUnbondingID(ctx context.Context, red types.Rede } redKey := types.GetREDKey(delAddr, valSrcAddr, valDstAddr) - if err = store.Set(types.GetUnbondingIndexKey(id), redKey); err != nil { + if err = k.UnbondingIndex.Set(ctx, id, redKey); err != nil { return err } @@ -218,15 +225,13 @@ func (k Keeper) SetRedelegationByUnbondingID(ctx context.Context, red types.Rede // SetValidatorByUnbondingID sets an index to look up a Validator by the unbondingID corresponding to its current unbonding // Note, it does not set the validator itself, use SetValidator(ctx, val) for that func (k Keeper) SetValidatorByUnbondingID(ctx context.Context, val types.Validator, id uint64) error { - store := k.storeService.OpenKVStore(ctx) - valAddr, err := k.validatorAddressCodec.StringToBytes(val.OperatorAddress) if err != nil { return err } valKey := types.GetValidatorKey(valAddr) - if err = store.Set(types.GetUnbondingIndexKey(id), valKey); err != nil { + if err = k.UnbondingIndex.Set(ctx, id, valKey); err != nil { return err } diff --git a/x/staking/types/keys.go b/x/staking/types/keys.go index 067073c1bdff..a9956cdbad20 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 = collections.NewPrefix(56) // 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 UnbondingQueueKey = []byte{0x41} // prefix for the timestamps in unbonding queue RedelegationQueueKey = []byte{0x42} // prefix for the timestamps in redelegations queue @@ -78,13 +78,6 @@ func GetUnbondingTypeKey(id uint64) []byte { 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) - binary.BigEndian.PutUint64(bz, id) - return append(UnbondingIndexKey, bz...) -} - // GetValidatorKey creates the key for the validator with address // VALUE: staking/Validator func GetValidatorKey(operatorAddr sdk.ValAddress) []byte { From 5e03c4c4b7c89de847be5e8c30199f768170cbba Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Fri, 4 Aug 2023 17:39:15 +0530 Subject: [PATCH 2/2] add changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ca0b3f5c998..6a08e4f91de5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,6 +61,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes +* (x/staking) [#17288](https://github.com/cosmos/cosmos-sdk/pull/17288) Use collections for `UnbondingIndex`: + * remove from `types`: `GetUnbondingIndexKey`. * (x/staking) [#17256](https://github.com/cosmos/cosmos-sdk/pull/17256) Use collections for `UnbondingID`. * (x/staking) [#17260](https://github.com/cosmos/cosmos-sdk/pull/17260) Use collections for `ValidatorByConsAddr`: * remove from `types`: `GetValidatorByConsAddrKey`