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

fix(x/staking): dust share #18841

Merged
merged 7 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (simulation) [#18196](https://github.com/cosmos/cosmos-sdk/pull/18196) Fix the problem of `validator set is empty after InitGenesis` in simulation test.
* (baseapp) [#18551](https://github.com/cosmos/cosmos-sdk/pull/18551) Fix SelectTxForProposal the calculation method of tx bytes size is inconsistent with CometBFT
* (baseapp) [#18895](https://github.com/cosmos/cosmos-sdk/pull/18895) Fix de-duplicating vote extensions during validation in ValidateVoteExtensions.
* (x/staking) [#18841](https://github.com/cosmos/cosmos-sdk/pull/18841) Fix delegation state when it has dust share. Change the condition of unbond to remove delegation with less than minimum share

### API Breaking Changes

Expand Down Expand Up @@ -197,6 +198,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

* (x/upgrade) [#16244](https://github.com/cosmos/cosmos-sdk/pull/16244) Upgrade module no longer stores the app version but gets and sets the app version stored in the `ParamStore` of baseapp.
* (x/staking) [#17655](https://github.com/cosmos/cosmos-sdk/pull/17655) `HistoricalInfo` was replaced with `HistoricalRecord`, it removes the validator set and comet header and only keep what is needed for IBC.
* (x/staking) [#18841](https://github.com/cosmos/cosmos-sdk/pull/18841) Delegations with less than the minimum share removed.

## [v0.50.2](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.50.2) - 2023-12-11

Expand Down
14 changes: 9 additions & 5 deletions x/staking/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -1197,11 +1197,15 @@ func (k Keeper) ValidateUnbondAmount(
return shares, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid shares amount")
}

// Cap the shares at the delegation's shares. Shares being greater could occur
// due to rounding, however we don't want to truncate the shares or take the
// minimum because we want to allow for the full withdraw of shares from a
// delegation.
if shares.GT(delShares) {
// Depending on the share, amount can be smaller than unit amount(1stake).
// If the remain amount after unbonding is smaller than the minimum share,
// it's completely unbonded to avoid leaving dust shares.
tolerance, err := validator.SharesFromTokens(math.OneInt())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update the comment above

Copy link
Contributor Author

@JoowonYun JoowonYun Dec 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if err != nil {
return shares, err
}

if delShares.Sub(shares).LT(tolerance) {
shares = delShares
}

Expand Down
48 changes: 48 additions & 0 deletions x/staking/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1170,3 +1170,51 @@ func (s *KeeperTestSuite) TestSetUnbondingDelegationEntry() {
// 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() {
ctx, keeper := s.ctx, s.stakingKeeper
require := s.Require()

addrDels, valAddrs := createValAddrs(2)

s.accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes()

// construct the validators[0] & slash 1stake
amt := math.NewInt(100)
validator := testutil.NewValidator(s.T(), valAddrs[0], PKs[0])
validator, _ = validator.AddTokensFromDel(amt)
validator = validator.RemoveTokens(math.NewInt(1))
validator = stakingkeeper.TestingUpdateValidator(keeper, ctx, validator, true)

// first add a validators[0] to delegate too
bond1to1 := stakingtypes.NewDelegation(addrDels[0].String(), valAddrs[0].String(), math.LegacyNewDec(100))
require.NoError(keeper.SetDelegation(ctx, bond1to1))
resBond, err := keeper.Delegations.Get(ctx, collections.Join(addrDels[0], valAddrs[0]))
require.NoError(err)
require.Equal(bond1to1, resBond)

// second delegators[1] add a validators[0] to delegate
bond2to1 := stakingtypes.NewDelegation(addrDels[1].String(), valAddrs[0].String(), math.LegacyNewDec(1))
validator, delegatorShare := validator.AddTokensFromDel(math.NewInt(1))
bond2to1.Shares = delegatorShare
_ = stakingkeeper.TestingUpdateValidator(keeper, ctx, validator, true)
require.NoError(keeper.SetDelegation(ctx, bond2to1))
resBond, err = keeper.Delegations.Get(ctx, collections.Join(addrDels[1], valAddrs[0]))
require.NoError(err)
require.Equal(bond2to1, resBond)

// check delegation state
delegations, err := keeper.GetValidatorDelegations(ctx, valAddrs[0])
require.NoError(err)
require.Equal(2, len(delegations))

// undelegate all delegator[0]'s delegate
_, err = s.msgServer.Undelegate(ctx, stakingtypes.NewMsgUndelegate(addrDels[0].String(), valAddrs[0].String(), sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(99))))
require.NoError(err)

// remain only delegator[1]'s delegate
delegations, err = keeper.GetValidatorDelegations(ctx, valAddrs[0])
require.NoError(err)
require.Equal(1, len(delegations))
require.Equal(delegations[0].DelegatorAddress, addrDels[1].String())
}