From 07de7d4fdeed6df36629543b02d65bc58bba0eca Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Tue, 1 Aug 2023 17:13:46 +0530 Subject: [PATCH 1/5] migrate UnbondingType to collections --- x/staking/keeper/keeper.go | 2 ++ x/staking/keeper/unbonding.go | 22 +++------------------- x/staking/keeper/unbonding_test.go | 5 +++-- x/staking/types/errors.go | 1 - x/staking/types/keys.go | 13 +++---------- 5 files changed, 11 insertions(+), 32 deletions(-) diff --git a/x/staking/keeper/keeper.go b/x/staking/keeper/keeper.go index 64ad687d1f6f..a96517c286e1 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, int64] } // 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.Int64Value), } schema, err := sb.Build() diff --git a/x/staking/keeper/unbonding.go b/x/staking/keeper/unbonding.go index 1813797b9761..67c7782befc5 100644 --- a/x/staking/keeper/unbonding.go +++ b/x/staking/keeper/unbonding.go @@ -44,30 +44,14 @@ 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 { - return unbondingType, types.ErrNoUnbondingType - } - - return types.UnbondingType(binary.BigEndian.Uint64(bz)), nil + ubdType, err := k.UnbondingType.Get(ctx, id) + 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, int64(unbondingType)) } // GetUnbondingDelegationByUnbondingID returns a unbonding delegation that has an unbonding delegation entry with a certain ID diff --git a/x/staking/keeper/unbonding_test.go b/x/staking/keeper/unbonding_test.go index 65e7c9de835b..444a3249d3e7 100644 --- a/x/staking/keeper/unbonding_test.go +++ b/x/staking/keeper/unbonding_test.go @@ -3,6 +3,7 @@ package keeper_test import ( "time" + "cosmossdk.io/collections" "cosmossdk.io/math" addresscodec "github.com/cosmos/cosmos-sdk/codec/address" @@ -53,7 +54,7 @@ func (s *KeeperTestSuite) TestUnbondingTypeAccessors() { require.NoError(err) require.Equal(tc.expected, unbondingType) } else { - require.ErrorIs(err, types.ErrNoUnbondingType) + require.ErrorIs(err, collections.ErrNotFound) } }) } @@ -277,7 +278,7 @@ func (s *KeeperTestSuite) TestUnbondingCanComplete() { // no unbondingID set err := s.stakingKeeper.UnbondingCanComplete(s.ctx, unbondingID) - require.ErrorIs(err, types.ErrNoUnbondingType) + require.ErrorIs(err, collections.ErrNotFound) // unbonding delegation require.NoError(s.stakingKeeper.SetUnbondingType(s.ctx, unbondingID, types.UnbondingType_UnbondingDelegation)) diff --git a/x/staking/types/errors.go b/x/staking/types/errors.go index 403d779c09ed..d2af47cdab78 100644 --- a/x/staking/types/errors.go +++ b/x/staking/types/errors.go @@ -46,5 +46,4 @@ var ( ErrUnbondingOnHoldRefCountNegative = errors.Register(ModuleName, 42, "cannot un-hold unbonding operation that is not on hold") ErrInvalidSigner = errors.Register(ModuleName, 43, "expected authority account as only signer for proposal message") ErrBadRedelegationSrc = errors.Register(ModuleName, 44, "redelegation source validator not found") - ErrNoUnbondingType = errors.Register(ModuleName, 45, "unbonding type not found") ) 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) From 54309b2b114bb33e0d9f75f1efcba3697395dab2 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Tue, 1 Aug 2023 17:20:08 +0530 Subject: [PATCH 2/5] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57155b5bdf7b..61efd3411b31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,7 @@ 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`. * (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`: From 012beca5e79ef9b7921604a9938339d11a293416 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Wed, 2 Aug 2023 10:42:00 +0530 Subject: [PATCH 3/5] change unbondingtype --- x/staking/keeper/keeper.go | 4 ++-- x/staking/keeper/unbonding.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/x/staking/keeper/keeper.go b/x/staking/keeper/keeper.go index a96517c286e1..d2f6f04c83b4 100644 --- a/x/staking/keeper/keeper.go +++ b/x/staking/keeper/keeper.go @@ -37,7 +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, int64] + UnbondingType collections.Map[uint64, uint64] } // NewKeeper creates a new staking Keeper instance @@ -87,7 +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.Int64Value), + 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 67c7782befc5..25d3ec89630f 100644 --- a/x/staking/keeper/unbonding.go +++ b/x/staking/keeper/unbonding.go @@ -51,7 +51,7 @@ func (k Keeper) GetUnbondingType(ctx context.Context, id uint64) (unbondingType // 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 { - return k.UnbondingType.Set(ctx, id, int64(unbondingType)) + return k.UnbondingType.Set(ctx, id, uint64(unbondingType)) } // GetUnbondingDelegationByUnbondingID returns a unbonding delegation that has an unbonding delegation entry with a certain ID From 3bb6e1dbc322ac71e1b2a54636f0d5d65a7ab4bd Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Wed, 2 Aug 2023 14:45:12 +0530 Subject: [PATCH 4/5] address review comments --- CHANGELOG.md | 1 + x/staking/keeper/unbonding.go | 3 +++ x/staking/keeper/unbonding_test.go | 5 ++--- x/staking/types/errors.go | 1 + 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2374b03bbb0c..b2454e64b6e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,6 +61,7 @@ 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`. * (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`: diff --git a/x/staking/keeper/unbonding.go b/x/staking/keeper/unbonding.go index 25d3ec89630f..27ba14383df3 100644 --- a/x/staking/keeper/unbonding.go +++ b/x/staking/keeper/unbonding.go @@ -45,6 +45,9 @@ func (k Keeper) DeleteUnbondingIndex(ctx context.Context, id uint64) error { // {UnbondingDelegation | Redelegation | ValidatorUnbonding} func (k Keeper) GetUnbondingType(ctx context.Context, id uint64) (unbondingType types.UnbondingType, err error) { ubdType, err := k.UnbondingType.Get(ctx, id) + if ubdType == 0 { + return unbondingType, types.ErrNoUnbondingType + } return types.UnbondingType(ubdType), err } diff --git a/x/staking/keeper/unbonding_test.go b/x/staking/keeper/unbonding_test.go index 444a3249d3e7..65e7c9de835b 100644 --- a/x/staking/keeper/unbonding_test.go +++ b/x/staking/keeper/unbonding_test.go @@ -3,7 +3,6 @@ package keeper_test import ( "time" - "cosmossdk.io/collections" "cosmossdk.io/math" addresscodec "github.com/cosmos/cosmos-sdk/codec/address" @@ -54,7 +53,7 @@ func (s *KeeperTestSuite) TestUnbondingTypeAccessors() { require.NoError(err) require.Equal(tc.expected, unbondingType) } else { - require.ErrorIs(err, collections.ErrNotFound) + require.ErrorIs(err, types.ErrNoUnbondingType) } }) } @@ -278,7 +277,7 @@ func (s *KeeperTestSuite) TestUnbondingCanComplete() { // no unbondingID set err := s.stakingKeeper.UnbondingCanComplete(s.ctx, unbondingID) - require.ErrorIs(err, collections.ErrNotFound) + require.ErrorIs(err, types.ErrNoUnbondingType) // unbonding delegation require.NoError(s.stakingKeeper.SetUnbondingType(s.ctx, unbondingID, types.UnbondingType_UnbondingDelegation)) diff --git a/x/staking/types/errors.go b/x/staking/types/errors.go index d2af47cdab78..403d779c09ed 100644 --- a/x/staking/types/errors.go +++ b/x/staking/types/errors.go @@ -46,4 +46,5 @@ var ( ErrUnbondingOnHoldRefCountNegative = errors.Register(ModuleName, 42, "cannot un-hold unbonding operation that is not on hold") ErrInvalidSigner = errors.Register(ModuleName, 43, "expected authority account as only signer for proposal message") ErrBadRedelegationSrc = errors.Register(ModuleName, 44, "redelegation source validator not found") + ErrNoUnbondingType = errors.Register(ModuleName, 45, "unbonding type not found") ) From 4a308fb077ed53c80cbd399dd7e98c99ff275709 Mon Sep 17 00:00:00 2001 From: Likhita Polavarapu <78951027+likhita-809@users.noreply.github.com> Date: Wed, 2 Aug 2023 10:33:44 +0000 Subject: [PATCH 5/5] nit --- x/staking/keeper/unbonding.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x/staking/keeper/unbonding.go b/x/staking/keeper/unbonding.go index 27ba14383df3..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" @@ -45,7 +47,7 @@ func (k Keeper) DeleteUnbondingIndex(ctx context.Context, id uint64) error { // {UnbondingDelegation | Redelegation | ValidatorUnbonding} func (k Keeper) GetUnbondingType(ctx context.Context, id uint64) (unbondingType types.UnbondingType, err error) { ubdType, err := k.UnbondingType.Get(ctx, id) - if ubdType == 0 { + if errors.Is(err, collections.ErrNotFound) { return unbondingType, types.ErrNoUnbondingType } return types.UnbondingType(ubdType), err