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 - remove delegation with amount is zero #10254

Merged
merged 19 commits into from
Oct 9, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### State Machine Breaking

* (x/staking) [#10254](https://github.com/cosmos/cosmos-sdk/pull/10254) Instead of using the shares to determine if a delegation should be removed, use the truncated (token) amount.
* (store) [#10247](https://github.com/cosmos/cosmos-sdk/pull/10247) Charge gas for the key length in gas meter.
* (store) [#10218](https://github.com/cosmos/cosmos-sdk/pull/10218) Charge gas even when there are no entries while seeking.
* (x/auth)[\#9596](https://github.com/cosmos/cosmos-sdk/pull/9596) Enable creating periodic vesting accounts with a transactions instead of requiring them to be created in genesis.
Expand Down
13 changes: 13 additions & 0 deletions x/staking/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ func InitGenesis(
keeper.SetParams(ctx, data.Params)
keeper.SetLastTotalPower(ctx, data.LastTotalPower)

valCache := make(map[string]types.Validator, len(data.Validators))
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved

for _, validator := range data.Validators {
keeper.SetValidator(ctx, validator)
valCache[validator.OperatorAddress] = validator

// Manually set indices for the first time
keeper.SetValidatorByConsAddr(ctx, validator)
Expand Down Expand Up @@ -64,6 +67,16 @@ func InitGenesis(
}

for _, delegation := range data.Delegations {
validator, ok := valCache[delegation.GetValidatorAddr().String()]
if !ok {
panic(fmt.Sprintf("expected %s validator to be found", delegation.GetValidatorAddr()))
}

// skip importing delegations with non-zero shares but zero token amounts
if !delegation.Shares.IsZero() && validator.TokensFromShares(delegation.Shares).TruncateInt().IsZero() {
continue
}

delegatorAddress, err := sdk.AccAddressFromBech32(delegation.DelegatorAddress)
if err != nil {
panic(err)
Expand Down
7 changes: 5 additions & 2 deletions x/staking/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,8 +680,11 @@ func (k Keeper) Unbond(
validator = k.mustGetValidator(ctx, validator.GetOperator())
}

// remove the delegation
if delegation.Shares.IsZero() {
// Remove the delegation if the resulting shares yield a truncated zero amount
// of tokens in the delegation. Note, in this this case, the shares themselves
// may not be zero, but rather a small fractional amount. Otherwise, we update
// the delegation object.
if validator.TokensFromShares(delegation.Shares).TruncateInt().IsZero() || delegation.Shares.IsZero() {
Comment on lines +683 to +687
Copy link
Contributor

Choose a reason for hiding this comment

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

@alexanderbez do you think this could break delegator shares invariant, causing #10750 ?

@aleem1314 did some debugging on this, here's some logs:

++++++++++++++++++++++++++++++++++++
TOKENS-FROM-SHARES =  0.937500000000000000  validator.TokensFromShares(delegation.Shares).TruncateInt() =  0
DELEGATIONS SHARES =  0.937500000000000000
VALIDATOR =  cosmosvaloper14rmvn8ymw7mdjwyjhmkzestgkcdskvkx6ke9e5
DELEGATOR =  cosmos15rxvlc2xaf6sjq5fjq45gfapdjsjr97jy6wmjc
BLOCKHEIGHT =  32
BLOCKTIME =  2103-10-01 17:18:06 +0000 UTC
++++++++++++++++++++++++++++++++++++

just reverting this line and simulations pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm could be! But what invariant is broken exactly? The delegation should be deleted here. Is there some invariant that expects this not to be deleted?

Copy link
Contributor

@amaury1093 amaury1093 Dec 13, 2021

Choose a reason for hiding this comment

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

The DelegatorSharesInvariant one. Maybe it should be updated to take TruncateInt into account? The error message is:

broken delegator shares invariance:
        validator.DelegatorShares: 34148342106.937500000000000000
        sum of Delegator.Shares: 34148342106.000000000000000000

which soulds like some truncating error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, let's keep the discussion in the new issue created 👍

err = k.RemoveDelegation(ctx, delegation)
} else {
k.SetDelegation(ctx, delegation)
Expand Down
2 changes: 0 additions & 2 deletions x/staking/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,11 @@ func PositiveDelegationInvariant(k Keeper) sdk.Invariant {
for _, delegation := range delegations {
if delegation.Shares.IsNegative() {
count++

msg += fmt.Sprintf("\tdelegation with negative shares: %+v\n", delegation)
}

if delegation.Shares.IsZero() {
count++

msg += fmt.Sprintf("\tdelegation with zero shares: %+v\n", delegation)
}
}
Expand Down