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

Conversation

JoowonYun
Copy link
Contributor

@JoowonYun JoowonYun commented Dec 20, 2023

Description

Closes: #13086

Amount less than 1stake remains in the share, delegation is removed.

PS. If you agree with this policy, I will add test code and migration details.


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
  • confirmed ! in 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
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • 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 all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • New Features
    • Introduced a more efficient handling of small delegation shares during the undelegation process to avoid leaving dust shares.
  • Bug Fixes
    • Ensured complete unbonding of small amounts that are less than the minimum share tolerance.
  • Tests
    • Added tests to ensure the correct undelegation behavior with small delegation shares.
  • Documentation
    • Updated CHANGELOG.md with details on significant changes, including:
      • State machine and API breaking changes.
      • Updates to CLI commands and balance query endpoint compatibility.
      • Introduction of a flag to disable fast node migration.

@JoowonYun JoowonYun requested a review from a team as a code owner December 20, 2023 10:16
Copy link
Contributor

coderabbitai bot commented Dec 20, 2023

Walkthrough

Walkthrough

The code changes aim to address the persistence of "dust shares" on validators after delegation removal. By implementing a tolerance-based condition to fully unbond small delegation shares, the update ensures that residual dust shares are effectively cleared, enhancing user experience and minimizing unnecessary gas fees.

Changes

File Path Change Summary
x/staking/keeper/delegation.go Replaced the logic for capping shares with a tolerance condition to fully unbond small delegation shares.
x/staking/keeper/delegation_test.go Added TestUndelegateWithDustShare to test the handling of small delegation shares during undelegation.
CHANGELOG.md Updated to reflect changes related to addressing delegations with dust shares.

Assessment against linked issues

Objective Addressed Explanation
Address the issue of "dust" shares remaining after delegation removal ( #13086 )
Ensure that undelegating, redelegating, or delegating a larger amount removes the "dust" shares ( #13086 )
Provide examples of affected accounts to illustrate the occurrence of the issue ( #13086 ) Providing specific examples is not a code change; it would typically be part of testing or documentation.
The issue should be addressed across multiple validators, not specific to one ( #13086 ) The fix is universally applied to handle dust shares on all validators, not limited to specific instances.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link
Contributor

@alexanderbez alexanderbez left a comment

Choose a reason for hiding this comment

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

@JoowonYun can you please provide a changelog entry, along with a unit test or update an existing unit test case?

@julienrbrt julienrbrt changed the title fix: dust share fix(x/staking): dust share Dec 20, 2023
@JoowonYun
Copy link
Contributor Author

JoowonYun commented Dec 21, 2023

@JoowonYun can you please provide a changelog entry, along with a unit test or update an existing unit test case?

Of course. I'll add it.

@facundomedica facundomedica self-assigned this Dec 21, 2023
Copy link
Contributor

@alexanderbez alexanderbez left a comment

Choose a reason for hiding this comment

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

utACK. I think the changelog entries could be a bit more explanatory.

@@ -1201,7 +1201,12 @@ func (k Keeper) ValidateUnbondAmount(
// 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) {
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.

CHANGELOG.md Outdated
@@ -89,6 +89,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (simulation) [#17911](https://github.com/cosmos/cosmos-sdk/pull/17911) Fix all problems with executing command `make test-sim-custom-genesis-fast` for simulation test.
* (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
* (x/staking) [#18841](https://github.com/cosmos/cosmos-sdk/pull/18841) Fix delegation when undelegate dust share
Copy link
Member

Choose a reason for hiding this comment

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

this deserves a more thorough explanation. "implement a minimum shares per delegation" or something

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.

CHANGELOG.md Outdated
@@ -193,6 +194,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) The existing dust share delegation must be removed.
Copy link
Member

Choose a reason for hiding this comment

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

same as 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.

Comment on lines 1200 to 1210
// Depending on the share, amount can be smaller than unit amount(1stake).
// All shares smaller than the minimum share are unboned.
tolerance, err := validator.SharesFromTokens(math.OneInt())
if err != nil {
return shares, err
}

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

Choose a reason for hiding this comment

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

The logic introduced to handle dust shares during unbonding appears to be correct. It checks if the remaining shares after unbonding are less than the tolerance (which is the smallest unit of shares that can be unbonded) and if so, it sets the shares to be unbonded to the total delegation shares. This ensures that no dust shares remain.

However, the comment on line 1201 could be improved for clarity. It currently reads "All shares smaller than the minimum share are unboned." which might be a typo and could be confusing. It should clearly state that any remaining shares smaller than the minimum share unit will be completely unbonded to avoid dust shares.

- // All shares smaller than the minimum share are unboned.
+ // If the remaining shares after unbonding are smaller than the minimum share unit, 
+ // they are considered dust and will be completely unbonded to avoid leaving dust shares.

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
// Depending on the share, amount can be smaller than unit amount(1stake).
// All shares smaller than the minimum share are unboned.
tolerance, err := validator.SharesFromTokens(math.OneInt())
if err != nil {
return shares, err
}
if delShares.Sub(shares).LT(tolerance) {
shares = delShares
}
// Depending on the share, amount can be smaller than unit amount(1stake).
// If the remaining shares after unbonding are smaller than the minimum share unit,
// they are considered dust and will be completely unbonded to avoid leaving dust shares.
tolerance, err := validator.SharesFromTokens(math.OneInt())
if err != nil {
return shares, err
}
if delShares.Sub(shares).LT(tolerance) {
shares = delShares
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

@atheeshp atheeshp left a comment

Choose a reason for hiding this comment

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

lgtm

CHANGELOG.md Outdated Show resolved Hide resolved
alexanderbez and others added 2 commits December 28, 2023 13:25
Co-authored-by: atheeshp <59333759+atheeshp@users.noreply.github.com>
@alexanderbez
Copy link
Contributor

LGTM. @facundomedica mind taking a quick look please?

@JoowonYun
Copy link
Contributor Author

I have a one question.
To remove dust delegation in the existing chain, it should be migration like below.
https://github.com/xpladev/xpla/pull/109/files#diff-85cee45041d35d29fecd2c334c7e4635cc2536f09f9bb1ecc1788243ab70d933R19-R37

For this purpose, I am curious about the policy on whether it is correct to up the ConsensusVersion of the staking module and create a migration function, or whether it through the upgrade handler like above.

@alexanderbez
Copy link
Contributor

alexanderbez commented Jan 3, 2024

If you want a migration to happen in a module, like the one you linked, you need to bump the consensus version yes. Otherwise, the migration won't be triggered.

Alternatively, you can manually call this migration in your app's upgrade handler.

Copy link
Contributor

github-actions bot commented Feb 3, 2024

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the Stale label Feb 3, 2024
@julienrbrt julienrbrt removed the Stale label Feb 6, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 3

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between ff6e240 and 5b229d5.
Files selected for processing (1)
  • CHANGELOG.md (3 hunks)
Additional comments: 2
CHANGELOG.md (2)
  • 100-100: The API Breaking Changes section mentions a significant change in the x/consensus module creation process. This entry is clear and specifies the PR for reference. It's important to ensure that all downstream dependencies and documentation are updated to reflect this change.
Verification successful

The search results indicate that the x/consensus module and its changes are mentioned in several places within the project, including the CHANGELOG.md, README.md, UPGRADING.md, and a specific README.md within the x/consensus directory itself. These references include details about the changes made to the x/consensus module, such as the introduction of a new module to handle Tendermint consensus parameters, changes in method signatures (e.g., NewKeeper now takes a KVStoreService), and guidance for migrating existing parameters from the deprecated x/params to the new x/consensus module.

Given this context, it appears that the documentation and upgrade guides have been updated to reflect the significant changes in the x/consensus module creation process. The presence of detailed entries in UPGRADING.md and README.md files, specifically addressing the x/consensus module, suggests that efforts have been made to ensure downstream dependencies and documentation are aligned with these changes.

Based on the information gathered, it seems reasonable to conclude that the necessary documentation and examples have been updated to account for the changes in the x/consensus module creation process.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for documentation or examples that might need updating due to the consensus module creation change.
rg --type md 'x/consensus' --exec cat {}

Length of output: 451



Script:

#!/bin/bash
# Corrected search for documentation or examples mentioning 'x/consensus' in markdown files.
rg --type md 'x/consensus'

Length of output: 1985

* 2797-2797: The link to the previous versions' CHANGELOG is a helpful resource for users looking to understand the historical context of changes. Ensure this link is kept up to date with the correct branch or tag to maintain accessibility.
Verification successful

The verification process has successfully confirmed that the linked CHANGELOG.md for previous versions in the cosmos-sdk repository is accessible and returns a HTTP/2 200 status code, indicating that the link points to a valid resource. Therefore, the link is correctly maintained and accessible, aligning with the review comment's suggestion to ensure the link is kept up to date for user accessibility.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the linked CHANGELOG.md for previous versions is accessible and points to the correct branch or tag.
curl -sI https://github.com/cosmos/cosmos-sdk/blob/c17c3caab86a1426a1eef4541e8203f5f54a1a54/CHANGELOG.md | grep HTTP/2 200

Length of output: 161



Script:

#!/bin/bash
# Verify that the linked CHANGELOG.md for previous versions is accessible and points to the correct branch or tag.
curl -sI https://github.com/cosmos/cosmos-sdk/blob/c17c3caab86a1426a1eef4541e8203f5f54a1a54/CHANGELOG.md | grep "HTTP/2 200"

Length of output: 138

CHANGELOG.md Outdated
@@ -94,6 +94,10 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i
* (crypto) [#19371](https://github.com/cosmos/cosmos-sdk/pull/19371) Avoid cli redundant log in stdout, log to stderr instead.
* (client) [#19393](https://github.com/cosmos/cosmos-sdk/pull/19393/) Add `ReadDefaultValuesFromDefaultClientConfig` to populate the default values from the default client config in client.Context without creating a app folder.

### State Machine Breaking

* (x/staking) [#18841](https://github.com/cosmos/cosmos-sdk/pull/18841) In a undelegation or redelegation if the shares being un/re-delegated left correspond to less than 1 token (in base denom) the entire delegation gets removed.
Copy link
Contributor

Choose a reason for hiding this comment

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

The description of the change under the State Machine Breaking section for the x/staking module is clear and directly references the PR addressing the issue. However, it would be beneficial to briefly explain the impact on existing delegations and the rationale behind removing entire delegations for clarity to the reader.

Consider adding a brief explanation of the impact on users and the rationale behind this change for better clarity.

@@ -1234,7 +1238,7 @@
* (x/group) [#13214](https://github.com/cosmos/cosmos-sdk/pull/13214) Add `withdraw-proposal` command to group module's CLI transaction commands.
* (x/auth) [#13048](https://github.com/cosmos/cosmos-sdk/pull/13048) Add handling of AccountNumberStoreKeyPrefix to the simulation decoder.
* (simapp) [#13107](https://github.com/cosmos/cosmos-sdk/pull/13107) Call `SetIAVLCacheSize` with the configured value in simapp.
* [#13301](https://github.com/cosmos/cosmos-sdk/pull/13301) Keep the balance query endpoint compatible with legacy blocks
* [#13301](https://github.com/cosmos/cosmos-sdk/pull/13301) Keep the balance query endpoint compatible with legacy blocks
Copy link
Contributor

Choose a reason for hiding this comment

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

The addition regarding the balance query endpoint compatibility with legacy blocks is a valuable update for users relying on historical data. Ensure that this change is well-documented in the relevant API documentation to avoid confusion.

Recommend updating the API documentation to clearly reflect this compatibility change.

CHANGELOG.md Outdated
Comment on lines 94 to 103
* (crypto) [#19371](https://github.com/cosmos/cosmos-sdk/pull/19371) Avoid cli redundant log in stdout, log to stderr instead.
* (client) [#19393](https://github.com/cosmos/cosmos-sdk/pull/19393/) Add `ReadDefaultValuesFromDefaultClientConfig` to populate the default values from the default client config in client.Context without creating a app folder.

### State Machine Breaking

* (x/staking) [#18841](https://github.com/cosmos/cosmos-sdk/pull/18841) In a undelegation or redelegation if the shares being un/re-delegated left correspond to less than 1 token (in base denom) the entire delegation gets removed.

### API Breaking Changes

* (x/consensus) [#19488](https://github.com/cosmos/cosmos-sdk/pull/19488) Consensus module creation takes `appmodule.Environment` instead of individual services.
Copy link
Contributor

Choose a reason for hiding this comment

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

📝 NOTE
This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [13321-13321]

The introduction of a flag to disable fast node migration and usage is an important feature that could affect deployment and operational strategies. It would be helpful to provide more context or a link to documentation on when and why to use this flag.

Consider adding more context or a link to documentation for the flag to disable fast node migration and usage, helping users understand its purpose and application scenarios.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between 5b229d5 and 82b9fa4.
Files selected for processing (1)
  • CHANGELOG.md (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • CHANGELOG.md

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.

Shares dust in the delegations
5 participants