Skip to content

Commit

Permalink
refactor(x/staking): migrate UnbondingType to collections (#17248)
Browse files Browse the repository at this point in the history
  • Loading branch information
likhita-809 authored Aug 3, 2023
1 parent 228c286 commit a7977b8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
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, uint64]
}

// 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.Uint64Value),
}

schema, err := sb.Build()
Expand Down
23 changes: 6 additions & 17 deletions x/staking/keeper/unbonding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
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

0 comments on commit a7977b8

Please sign in to comment.