Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(x/staking): migrate UnbondingType to collections #17248

Merged
merged 11 commits into from
Aug 3, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,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`.
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
* (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`:
Expand Down
2 changes: 2 additions & 0 deletions x/staking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
22 changes: 3 additions & 19 deletions x/staking/keeper/unbonding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
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))
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
}

// GetUnbondingDelegationByUnbondingID returns a unbonding delegation that has an unbonding delegation entry with a certain ID
Expand Down
5 changes: 3 additions & 2 deletions x/staking/keeper/unbonding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper_test
import (
"time"

"cosmossdk.io/collections"
"cosmossdk.io/math"

addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
Expand Down Expand Up @@ -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)
}
})
}
Expand Down Expand Up @@ -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))
Expand Down
1 change: 0 additions & 1 deletion x/staking/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
13 changes: 3 additions & 10 deletions x/staking/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down