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 UnbondingID to collections #17256

Merged
merged 10 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 @@ -60,6 +60,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### API Breaking Changes

* (x/staking) [#17256](https://github.com/cosmos/cosmos-sdk/pull/17256) Use collections for `UnbondingID`.
* (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.
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]
UnbondingID collections.Sequence
UnbondingType collections.Map[uint64, uint64]
}

Expand Down Expand Up @@ -87,6 +88,7 @@ func NewKeeper(
collections.PairKeyCodec(sdk.LengthPrefixedAddressKey(sdk.ValAddressKey), sdk.AccAddressKey), // nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility
collections.BytesValue,
),
UnbondingID: collections.NewSequence(sb, types.UnbondingIDKey, "unbonding_id"),
UnbondingType: collections.NewMap(sb, types.UnbondingTypeKey, "unbonding_type", collections.Uint64Key, collections.Uint64Value),
}

Expand Down
15 changes: 3 additions & 12 deletions x/staking/keeper/unbonding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import (
"context"
"encoding/binary"

Check failure on line 5 in x/staking/keeper/unbonding.go

View workflow job for this annotation

GitHub Actions / dependency-review

"encoding/binary" imported and not used

Check failure on line 5 in x/staking/keeper/unbonding.go

View workflow job for this annotation

GitHub Actions / tests (00)

"encoding/binary" imported and not used

Check failure on line 5 in x/staking/keeper/unbonding.go

View workflow job for this annotation

GitHub Actions / tests (00)

"encoding/binary" imported and not used

Check failure on line 5 in x/staking/keeper/unbonding.go

View workflow job for this annotation

GitHub Actions / tests (00)

"encoding/binary" imported and not used

Check failure on line 5 in x/staking/keeper/unbonding.go

View workflow job for this annotation

GitHub Actions / tests (00)

"encoding/binary" imported and not used

Check failure on line 5 in x/staking/keeper/unbonding.go

View workflow job for this annotation

GitHub Actions / tests (00)

"encoding/binary" imported and not used

Check failure on line 5 in x/staking/keeper/unbonding.go

View workflow job for this annotation

GitHub Actions / tests (00)

"encoding/binary" imported and not used

Check failure on line 5 in x/staking/keeper/unbonding.go

View workflow job for this annotation

GitHub Actions / tests (01)

"encoding/binary" imported and not used

Check failure on line 5 in x/staking/keeper/unbonding.go

View workflow job for this annotation

GitHub Actions / tests (01)

"encoding/binary" imported and not used

Check failure on line 5 in x/staking/keeper/unbonding.go

View workflow job for this annotation

GitHub Actions / tests (01)

"encoding/binary" imported and not used

Check failure on line 5 in x/staking/keeper/unbonding.go

View workflow job for this annotation

GitHub Actions / tests (02)

"encoding/binary" imported and not used

Check failure on line 5 in x/staking/keeper/unbonding.go

View workflow job for this annotation

GitHub Actions / tests (02)

"encoding/binary" imported and not used

Check failure on line 5 in x/staking/keeper/unbonding.go

View workflow job for this annotation

GitHub Actions / tests (03)

"encoding/binary" imported and not used

Check failure on line 5 in x/staking/keeper/unbonding.go

View workflow job for this annotation

GitHub Actions / tests (03)

"encoding/binary" imported and not used

Check failure on line 5 in x/staking/keeper/unbonding.go

View workflow job for this annotation

GitHub Actions / tests (03)

"encoding/binary" imported and not used

Check failure on line 5 in x/staking/keeper/unbonding.go

View workflow job for this annotation

GitHub Actions / tests (03)

"encoding/binary" imported and not used

Check failure on line 5 in x/staking/keeper/unbonding.go

View workflow job for this annotation

GitHub Actions / tests (03)

"encoding/binary" imported and not used
"errors"

"cosmossdk.io/collections"
Expand All @@ -14,23 +14,14 @@

// IncrementUnbondingID increments and returns a unique ID for an unbonding operation
func (k Keeper) IncrementUnbondingID(ctx context.Context) (unbondingID uint64, err error) {
store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get(types.UnbondingIDKey)
unbondingID, err = k.UnbondingID.Next(ctx)
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return 0, err
}

if bz != nil {
unbondingID = binary.BigEndian.Uint64(bz)
}

unbondingID++

// Convert back into bytes for storage
bz = make([]byte, 8)
binary.BigEndian.PutUint64(bz, unbondingID)

if err = store.Set(types.UnbondingIDKey, bz); err != nil {
err = k.UnbondingID.Set(ctx, unbondingID)
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return 0, err
}

Expand Down
2 changes: 1 addition & 1 deletion x/staking/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ 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
UnbondingIDKey = collections.NewPrefix(55) // 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

Expand Down
Loading