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

Conversation

alexanderbez
Copy link
Contributor

@alexanderbez alexanderbez commented Sep 28, 2021

Description

Closes: #10216

Instead of using the shares to determine if a delegation should be removed, use the truncated (token) amount.


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

@codecov
Copy link

codecov bot commented Sep 28, 2021

Codecov Report

Merging #10254 (0051730) into master (88f39ad) will increase coverage by 0.00%.
The diff coverage is 66.66%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master   #10254   +/-   ##
=======================================
  Coverage   64.15%   64.15%           
=======================================
  Files         572      572           
  Lines       53983    53989    +6     
=======================================
+ Hits        34632    34636    +4     
- Misses      17375    17377    +2     
  Partials     1976     1976           
Impacted Files Coverage Δ
x/staking/keeper/invariants.go 62.40% <ø> (+0.92%) ⬆️
x/staking/migrations/v040/types.go 0.00% <0.00%> (ø)
x/staking/keeper/migrations.go 50.00% <60.00%> (ø)
x/staking/genesis.go 60.32% <100.00%> (+0.43%) ⬆️
x/staking/keeper/delegation.go 72.66% <100.00%> (-0.05%) ⬇️
x/staking/module.go 63.88% <100.00%> (+0.50%) ⬆️

@amaury1093
Copy link
Contributor

looks good. This needs an in-place store migration (and possible a json dump migration too) imo. @alexanderbez I'll let you decide if you want to do it here or in a follow-up PR

@robert-zaremba
Copy link
Collaborator

Do we have a policy about when we should do a JSON migration? I think in-place migration should be present for each cleanup / state change.

@amaury1093
Copy link
Contributor

Do we have a policy about when we should do a JSON migration?

Imo it's not that much a human policy, but just to make sure that one version's ExportGenesis can be plugged into the next version's InitGenesis. If no, then we need a JSON migration.

@ValarDragon
Copy link
Contributor

ValarDragon commented Sep 29, 2021

I don't think this needs to change anything in the export JSON format / merits a migration there. I'd recommend just making the import logic ignore / not initialize things with zero amounts, but non-zero shares.

In place state migration would be a nice state cleanup though!

x/staking/genesis.go Outdated Show resolved Hide resolved
Copy link
Contributor

@ValarDragon ValarDragon left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for fixing this

Copy link
Contributor

@amaury1093 amaury1093 left a comment

Choose a reason for hiding this comment

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

lgtm! the module's ConsensusVersion needs to be bumped, and I think an in-place migration would be useful to purge the state (both can be done in a follow-up though)

@alexanderbez
Copy link
Contributor Author

lgtm! the module's ConsensusVersion needs to be bumped, and I think an in-place migration would be useful to purge the state (both can be done in a follow-up though)

I'll do it in this PR along with fixing tests.

Copy link
Contributor

@amaury1093 amaury1093 left a comment

Choose a reason for hiding this comment

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

nice!

@amaury1093
Copy link
Contributor

2 sims related to import/exports are failing though

@alexanderbez
Copy link
Contributor Author

Yeah I've been trying to debug them @AmauryM. The issue lies in the import genesis changes. It doesn't like the fact that I manually remove delegations.

@alexanderbez
Copy link
Contributor Author

@ValarDragon so turns out there are cases, at least in the simulation with seed 1, where in InitGenesis there is a validator with zero tokens, but non-zero shares and delegation shares. In this case, we can't apply this check because TokensFromShares won't give us the result we want. So

  1. What cases can a validator have zero tokens but non-zero shares? Maybe this is okay...
  2. If so, should we just skip over cases where validator tokens are zero?

@ValarDragon
Copy link
Contributor

hrmm, I guess you can have 0 tokens and shares if you had 1uatom self-bond, and then get slashed to 0uatom.

I don't see why the validator has to have shares tho

@alexanderbez
Copy link
Contributor Author

@ValarDragon I'm going to undo the InitGenesis changes and leave it as-is. The reason mainly being is that we don't have existing logic already like that in InitGenesis (i.e. skipping on empty shares). In addition, if I actually remove the core changes in this PR but still add the InitGenesis changes, it still fails.

LMK if this is OK with you and we can merge this PR.

@ValarDragon
Copy link
Contributor

That sounds good to me! Then this is fixing things going forward, which is already a significant improvement =)

@alexanderbez alexanderbez merged commit 3458f64 into master Oct 9, 2021
@alexanderbez alexanderbez deleted the bez/10216-del-removal branch October 9, 2021 02:02
Comment on lines +683 to +687
// 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() {
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 👍

mergify bot pushed a commit that referenced this pull request Dec 15, 2021
## Description

Revert #10254

Closes: #10750

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Empty amount delegation stored after unbonding/redelegation
4 participants