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

This commit is contained in:
Likhita Polavarapu 2023-08-03 15:41:23 +05:30 committed by GitHub
parent 228c28687b
commit a7977b8633
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 27 deletions

View File

@ -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`.

View File

@ -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()

View File

@ -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

View File

@ -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)