Skip to content

Commit

Permalink
refactor(x/staking)!: removing unbonding queue index (#22795)
Browse files Browse the repository at this point in the history
(cherry picked from commit 234cdc4)

# Conflicts:
#	api/cosmos/staking/v1beta1/staking.pulsar.go
#	tests/integration/staking/keeper/unbonding_test.go
#	x/staking/CHANGELOG.md
#	x/staking/testutil/expected_keepers_mocks.go
#	x/staking/types/staking.pb.go
  • Loading branch information
tac0turtle authored and mergify[bot] committed Dec 16, 2024
1 parent 854c024 commit ddeafad
Show file tree
Hide file tree
Showing 26 changed files with 17,902 additions and 973 deletions.
16,943 changes: 16,943 additions & 0 deletions api/cosmos/staking/v1beta1/staking.pulsar.go

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func TestAppImportExport(t *testing.T) {
skipPrefixes := map[string][][]byte{
stakingtypes.StoreKey: {
stakingtypes.UnbondingQueueKey, stakingtypes.RedelegationQueueKey, stakingtypes.ValidatorQueueKey,
stakingtypes.UnbondingIDKey, stakingtypes.UnbondingIndexKey, stakingtypes.UnbondingTypeKey,
},
authzkeeper.StoreKey: {authzkeeper.GrantQueuePrefix},
feegrant.StoreKey: {feegrant.FeeAllowanceQueueKeyPrefix},
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/staking/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ func TestGRPCValidatorUnbondingDelegations(t *testing.T) {
ValidatorAddr: validator.OperatorAddress,
}

testdata.DeterministicIterations(t, f.ctx, req, f.queryClient.ValidatorUnbondingDelegations, 3719, false)
testdata.DeterministicIterations(t, f.ctx, req, f.queryClient.ValidatorUnbondingDelegations, 3707, false)
}

func TestGRPCDelegation(t *testing.T) {
Expand Down Expand Up @@ -607,7 +607,7 @@ func TestGRPCUnbondingDelegation(t *testing.T) {
DelegatorAddr: delegator1,
}

testdata.DeterministicIterations(t, f.ctx, req, f.queryClient.UnbondingDelegation, 1621, false)
testdata.DeterministicIterations(t, f.ctx, req, f.queryClient.UnbondingDelegation, 1615, false)
}

func TestGRPCDelegatorDelegations(t *testing.T) {
Expand Down Expand Up @@ -730,7 +730,7 @@ func TestGRPCDelegatorUnbondingDelegations(t *testing.T) {
DelegatorAddr: delegator1,
}

testdata.DeterministicIterations(t, f.ctx, req, f.queryClient.DelegatorUnbondingDelegations, 1302, false)
testdata.DeterministicIterations(t, f.ctx, req, f.queryClient.DelegatorUnbondingDelegations, 1290, false)
}

func TestGRPCDelegatorValidators(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion tests/integration/staking/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func TestCancelUnbondingDelegation(t *testing.T) {
delegatorAddr, validatorAddr, 10,
ctx.HeaderInfo().Time.Add(time.Minute*10),
unbondingAmount.Amount,
0,
address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"),
)

Expand Down
12 changes: 6 additions & 6 deletions tests/integration/staking/keeper/slash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestSlashUnbondingDelegation(t *testing.T) {
// set an unbonding delegation with expiration timestamp (beyond which the
// unbonding delegation shouldn't be slashed)
ubd := types.NewUnbondingDelegation(addrDels[0], addrVals[0], 0,
time.Unix(5, 0), math.NewInt(10), 0, address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))
time.Unix(5, 0), math.NewInt(10), address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))

assert.NilError(t, f.stakingKeeper.SetUnbondingDelegation(f.sdkCtx, ubd))

Expand Down Expand Up @@ -138,7 +138,7 @@ func TestSlashRedelegation(t *testing.T) {
// set a redelegation with an expiration timestamp beyond which the
// redelegation shouldn't be slashed
rd := types.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 0,
time.Unix(5, 0), math.NewInt(10), math.LegacyNewDec(10), 0, address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))
time.Unix(5, 0), math.NewInt(10), math.LegacyNewDec(10), address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))

assert.NilError(t, f.stakingKeeper.SetRedelegation(f.sdkCtx, rd))

Expand Down Expand Up @@ -285,7 +285,7 @@ func TestSlashWithUnbondingDelegation(t *testing.T) {
// set an unbonding delegation with expiration timestamp beyond which the
// unbonding delegation shouldn't be slashed
ubdTokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, 4)
ubd := types.NewUnbondingDelegation(addrDels[0], addrVals[0], 11, time.Unix(0, 0), ubdTokens, 0, address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))
ubd := types.NewUnbondingDelegation(addrDels[0], addrVals[0], 11, time.Unix(0, 0), ubdTokens, address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))
assert.NilError(t, f.stakingKeeper.SetUnbondingDelegation(f.sdkCtx, ubd))

// slash validator for the first time
Expand Down Expand Up @@ -415,7 +415,7 @@ func TestSlashWithRedelegation(t *testing.T) {

// set a redelegation
rdTokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, 6)
rd := types.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 11, time.Unix(0, 0), rdTokens, math.LegacyNewDecFromInt(rdTokens), 0, address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))
rd := types.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 11, time.Unix(0, 0), rdTokens, math.LegacyNewDecFromInt(rdTokens), address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))
assert.NilError(t, f.stakingKeeper.SetRedelegation(f.sdkCtx, rd))

// set the associated delegation
Expand Down Expand Up @@ -573,7 +573,7 @@ func TestSlashBoth(t *testing.T) {
// set a redelegation with expiration timestamp beyond which the
// redelegation shouldn't be slashed
rdATokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, 6)
rdA := types.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 11, time.Unix(0, 0), rdATokens, math.LegacyNewDecFromInt(rdATokens), 0, address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))
rdA := types.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 11, time.Unix(0, 0), rdATokens, math.LegacyNewDecFromInt(rdATokens), address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))
assert.NilError(t, f.stakingKeeper.SetRedelegation(f.sdkCtx, rdA))

// set the associated delegation
Expand All @@ -584,7 +584,7 @@ func TestSlashBoth(t *testing.T) {
// unbonding delegation shouldn't be slashed)
ubdATokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, 4)
ubdA := types.NewUnbondingDelegation(addrDels[0], addrVals[0], 11,
time.Unix(0, 0), ubdATokens, 0, address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))
time.Unix(0, 0), ubdATokens, address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))
assert.NilError(t, f.stakingKeeper.SetUnbondingDelegation(f.sdkCtx, ubdA))

bondedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, rdATokens.MulRaw(2)))
Expand Down
11 changes: 11 additions & 0 deletions x/staking/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,22 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [#17335](https://github.com/cosmos/cosmos-sdk/pull/17335) Remove usage of `"cosmossdk.io/x/staking/types".Infraction_*` in favour of `"cosmossdk.io/api/cosmos/staking/v1beta1".Infraction_` in order to remove dependency between modules on staking
* [#20295](https://github.com/cosmos/cosmos-sdk/pull/20295) `GetValidatorByConsAddr` now returns the Cosmos SDK `cryptotypes.Pubkey` instead of `cometcrypto.Publickey`. The caller is responsible to translate the returned value to the expected type.
* Remove `CmtConsPublicKey()` and `TmConsPublicKey()` from `Validator` interface and as methods on the `Validator` struct.
<<<<<<< HEAD
* [#21480](https://github.com/cosmos/cosmos-sdk/pull/21480) ConsensusKeeper is required to be passed to the keeper.

=======
* [#21480](https://github.com/cosmos/cosmos-sdk/pull/21480) ConsensusKeeper is required to be passed to the keeper.
* [#21315](https://github.com/cosmos/cosmos-sdk/pull/21315) New struct `Metadata` to store extra validator information.
* New field `Metadata` introduced in `types`: `Description`.
* The signature of `NewDescription` has changed to accept an extra argument of type `Metadata`.
* [#22795](https://github.com/cosmos/cosmos-sdk/pull/22795) `NewUnbondingDelegationEntry`, `NewUnbondingDelegation`, `AddEntry`, `NewRedelegationEntry`, `NewRedelegation` and `NewRedelegationEntryResponse` no longer take an ID in there function signatures.
* [#22795](https://github.com/cosmos/cosmos-sdk/pull/22795) AfterUnbondingInitiated hook has been removed as it is no longer required by ICS.

>>>>>>> 234cdc45f (refactor(x/staking)!: removing unbonding queue index (#22795))
### State Breaking changes

* [#18841](https://github.com/cosmos/cosmos-sdk/pull/18841) In a undelegation or redelegation if the shares being left delegated correspond to less than 1 token (in base denom) the entire delegation gets removed.
* [#18142](https://github.com/cosmos/cosmos-sdk/pull/18142) Introduce `key_rotation_fee` param to calculate fees while rotating the keys
* [#19740](https://github.com/cosmos/cosmos-sdk/pull/19740) `InitGenesis` and `ExportGenesis` module code and keeper code do not panic but return errors.
* [#20845](https://github.com/cosmoc/cosmos-sdk/pull/20845) Remove HistoricalInfo from the staking modules storage
* [#22795](https://github.com/cosmos/cosmos-sdk/pull/22795) Keys `stakingtypes.UnbondingIDKey, stakingtypes.UnbondingIndexKey, stakingtypes.UnbondingTypeKey` have been removed as they are no longer required by ICS.
50 changes: 6 additions & 44 deletions x/staking/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,17 +310,11 @@ func (k Keeper) SetUnbondingDelegationEntry(
ctx context.Context, delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress,
creationHeight int64, minTime time.Time, balance math.Int,
) (types.UnbondingDelegation, error) {
id, err := k.IncrementUnbondingID(ctx)
if err != nil {
return types.UnbondingDelegation{}, err
}

isNewUbdEntry := true
ubd, err := k.GetUnbondingDelegation(ctx, delegatorAddr, validatorAddr)
if err == nil {
isNewUbdEntry = ubd.AddEntry(creationHeight, minTime, balance, id)
ubd.AddEntry(creationHeight, minTime, balance)
} else if errors.Is(err, types.ErrNoUnbondingDelegation) {
ubd = types.NewUnbondingDelegation(delegatorAddr, validatorAddr, creationHeight, minTime, balance, id, k.validatorAddressCodec, k.authKeeper.AddressCodec())
ubd = types.NewUnbondingDelegation(delegatorAddr, validatorAddr, creationHeight, minTime, balance, k.validatorAddressCodec, k.authKeeper.AddressCodec())
} else {
return ubd, err
}
Expand All @@ -329,18 +323,6 @@ func (k Keeper) SetUnbondingDelegationEntry(
return ubd, err
}

// only call the hook for new entries since
// calls to AfterUnbondingInitiated are not idempotent
if isNewUbdEntry {
// Add to the UBDByUnbondingOp index to look up the UBD by the UBDE ID
if err = k.SetUnbondingDelegationByUnbondingID(ctx, ubd, id); err != nil {
return ubd, err
}

if err := k.Hooks().AfterUnbondingInitiated(ctx, id); err != nil {
return ubd, fmt.Errorf("failed to call after unbonding initiated hook: %w", err)
}
}
return ubd, nil
}

Expand Down Expand Up @@ -528,17 +510,12 @@ func (k Keeper) SetRedelegationEntry(ctx context.Context,
minTime time.Time, balance math.Int,
sharesSrc, sharesDst math.LegacyDec,
) (types.Redelegation, error) {
id, err := k.IncrementUnbondingID(ctx)
if err != nil {
return types.Redelegation{}, err
}

red, err := k.Redelegations.Get(ctx, collections.Join3(delegatorAddr.Bytes(), validatorSrcAddr.Bytes(), validatorDstAddr.Bytes()))
if err == nil {
red.AddEntry(creationHeight, minTime, balance, sharesDst, id)
red.AddEntry(creationHeight, minTime, balance, sharesDst)
} else if errors.Is(err, collections.ErrNotFound) {
red = types.NewRedelegation(delegatorAddr, validatorSrcAddr,
validatorDstAddr, creationHeight, minTime, balance, sharesDst, id, k.validatorAddressCodec, k.authKeeper.AddressCodec())
validatorDstAddr, creationHeight, minTime, balance, sharesDst, k.validatorAddressCodec, k.authKeeper.AddressCodec())
} else {
return types.Redelegation{}, err
}
Expand All @@ -547,15 +524,6 @@ func (k Keeper) SetRedelegationEntry(ctx context.Context,
return types.Redelegation{}, err
}

// Add to the UBDByEntry index to look up the UBD by the UBDE ID
if err = k.SetRedelegationByUnbondingID(ctx, red, id); err != nil {
return types.Redelegation{}, err
}

if err := k.Hooks().AfterUnbondingInitiated(ctx, id); err != nil {
return types.Redelegation{}, fmt.Errorf("failed to call after unbonding initiated hook: %w", err)
}

return red, nil
}

Expand Down Expand Up @@ -996,12 +964,9 @@ func (k Keeper) CompleteUnbonding(ctx context.Context, delAddr sdk.AccAddress, v
// loop through all the entries and complete unbonding mature entries
for i := 0; i < len(ubd.Entries); i++ {
entry := ubd.Entries[i]
if entry.IsMature(ctxTime) && !entry.OnHold() {
if entry.IsMature(ctxTime) {
ubd.RemoveEntry(int64(i))
i--
if err = k.DeleteUnbondingIndex(ctx, entry.UnbondingId); err != nil {
return nil, err
}

// track undelegation only when remaining or truncated shares are non-zero
if !entry.Balance.IsZero() {
Expand Down Expand Up @@ -1136,12 +1101,9 @@ func (k Keeper) CompleteRedelegation(
// loop through all the entries and complete mature redelegation entries
for i := 0; i < len(red.Entries); i++ {
entry := red.Entries[i]
if entry.IsMature(ctxTime) && !entry.OnHold() {
if entry.IsMature(ctxTime) {
red.RemoveEntry(int64(i))
i--
if err = k.DeleteUnbondingIndex(ctx, entry.UnbondingId); err != nil {
return nil, err
}

if !entry.InitialBalance.IsZero() {
balances = balances.Add(sdk.NewCoin(bondDenom, entry.InitialBalance))
Expand Down
30 changes: 11 additions & 19 deletions x/staking/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ func (s *KeeperTestSuite) TestUnbondingDelegation() {
0,
time.Unix(0, 0).UTC(),
math.NewInt(5),
0,
address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"),
)

Expand Down Expand Up @@ -343,8 +342,8 @@ func (s *KeeperTestSuite) TestUnbondingDelegationsFromValidator() {
0,
time.Unix(0, 0).UTC(),
math.NewInt(5),
0,
address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"),
address.NewBech32Codec("cosmosvaloper"),
address.NewBech32Codec("cosmos"),
)

// set and retrieve a record
Expand Down Expand Up @@ -716,7 +715,7 @@ func (s *KeeperTestSuite) TestGetRedelegationsFromSrcValidator() {

rd := stakingtypes.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 0,
time.Unix(0, 0), math.NewInt(5),
math.LegacyNewDec(5), 0, address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))
math.LegacyNewDec(5), address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))

// set and retrieve a record
err := keeper.SetRedelegation(ctx, rd)
Expand Down Expand Up @@ -746,7 +745,7 @@ func (s *KeeperTestSuite) TestRedelegation() {

rd := stakingtypes.NewRedelegation(addrDels[0], addrVals[0], addrVals[1], 0,
time.Unix(0, 0).UTC(), math.NewInt(5),
math.LegacyNewDec(5), 0, address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))
math.LegacyNewDec(5), address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"))

// test shouldn't have and redelegations
has, err := keeper.HasReceivingRedelegation(ctx, addrDels[0], addrVals[1])
Expand Down Expand Up @@ -1107,14 +1106,14 @@ func (s *KeeperTestSuite) TestUnbondingDelegationAddEntry() {
creationHeight,
time.Unix(0, 0).UTC(),
math.NewInt(10),
0,
address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"),
address.NewBech32Codec("cosmosvaloper"),
address.NewBech32Codec("cosmos"),
)
var initialEntries []stakingtypes.UnbondingDelegationEntry
initialEntries = append(initialEntries, ubd.Entries...)
require.Len(initialEntries, 1)

isNew := ubd.AddEntry(creationHeight, time.Unix(0, 0).UTC(), math.NewInt(5), 1)
isNew := ubd.AddEntry(creationHeight, time.Unix(0, 0).UTC(), math.NewInt(5))
require.False(isNew)
require.Len(ubd.Entries, 1) // entry was merged
require.NotEqual(initialEntries, ubd.Entries)
Expand All @@ -1123,15 +1122,14 @@ func (s *KeeperTestSuite) TestUnbondingDelegationAddEntry() {
require.Equal(ubd.Entries[0].Balance, math.NewInt(15)) // 10 from previous + 5 from merged

newCreationHeight := int64(11)
isNew = ubd.AddEntry(newCreationHeight, time.Unix(1, 0).UTC(), math.NewInt(5), 2)
isNew = ubd.AddEntry(newCreationHeight, time.Unix(1, 0).UTC(), math.NewInt(5))
require.True(isNew)
require.Len(ubd.Entries, 2) // entry was appended
require.NotEqual(initialEntries, ubd.Entries)
require.Equal(creationHeight, ubd.Entries[0].CreationHeight)
require.Equal(newCreationHeight, ubd.Entries[1].CreationHeight)
require.Equal(ubd.Entries[0].Balance, math.NewInt(15))
require.Equal(ubd.Entries[1].Balance, math.NewInt(5))
require.NotEqual(ubd.Entries[0].UnbondingId, ubd.Entries[1].UnbondingId) // appended entry has a new unbondingID
}

func (s *KeeperTestSuite) TestSetUnbondingDelegationEntry() {
Expand All @@ -1149,8 +1147,8 @@ func (s *KeeperTestSuite) TestSetUnbondingDelegationEntry() {
creationHeight,
time.Unix(0, 0).UTC(),
math.NewInt(5),
0,
address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmos"),
address.NewBech32Codec("cosmosvaloper"),
address.NewBech32Codec("cosmos"),
)

// set and retrieve a record
Expand Down Expand Up @@ -1180,8 +1178,7 @@ func (s *KeeperTestSuite) TestSetUnbondingDelegationEntry() {
require.Len(resUnbonding.Entries, 1)
require.NotEqual(initialEntries, resUnbonding.Entries)
require.Equal(creationHeight, resUnbonding.Entries[0].CreationHeight)
require.Equal(initialEntries[0].UnbondingId, resUnbonding.Entries[0].UnbondingId) // initial unbondingID remains unchanged
require.Equal(resUnbonding.Entries[0].Balance, math.NewInt(10)) // 5 from previous entry + 5 from merged entry
require.Equal(resUnbonding.Entries[0].Balance, math.NewInt(10)) // 5 from previous entry + 5 from merged entry

// set unbonding delegation entry for newCreationHeight
// new entry is expected to be appended to the existing entries
Expand All @@ -1202,11 +1199,6 @@ func (s *KeeperTestSuite) TestSetUnbondingDelegationEntry() {
require.NotEqual(resUnbonding.Entries[0], resUnbonding.Entries[1])
require.Equal(creationHeight, resUnbonding.Entries[0].CreationHeight)
require.Equal(newCreationHeight, resUnbonding.Entries[1].CreationHeight)

// unbondingID is incremented on every call to SetUnbondingDelegationEntry
// unbondingID == 1 was skipped because the entry was merged with the existing entry with unbondingID == 0
// unbondingID comes from a global counter -> gaps in unbondingIDs are OK as long as every unbondingID is unique
require.Equal(uint64(2), resUnbonding.Entries[1].UnbondingId)
}

func (s *KeeperTestSuite) TestUndelegateWithDustShare() {
Expand Down
1 change: 0 additions & 1 deletion x/staking/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,6 @@ func redelegationsToRedelegationResponses(ctx context.Context, k *Keeper, redels
entry.SharesDst,
entry.InitialBalance,
val.TokensFromShares(entry.SharesDst).TruncateInt(),
entry.UnbondingId,
)
}

Expand Down
Loading

0 comments on commit ddeafad

Please sign in to comment.