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/slashing): do not error when val has zero tokens #20977

Merged
merged 2 commits into from
Jul 24, 2024

Conversation

facundomedica
Copy link
Member

@facundomedica facundomedica commented Jul 17, 2024

Description

This bug was introduced in #20688, if a validator has no tokens left, then we should not error and just continue


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, you can find examples of the prefixes below:
  • 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.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

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

  • Tests

    • Added a new test to ensure correct handling when a validator is left with no tokens due to slashing and redelegation.
  • Bug Fixes

    • Improved error handling in the slashing mechanism to optimize control flow when dealing with zero tokens.

Copy link
Contributor

coderabbitai bot commented Jul 17, 2024

Walkthrough

Walkthrough

This update introduces a new test to validate slashing and redelegation behavior in scenarios where a validator's tokens are completely exhausted. Additionally, it refines error handling in the SlashRedelegation method to optimize control flow when dealing with zero tokens, ensuring smoother operations during slashing processes.

Changes

Files Change Summary
tests/integration/slashing/keeper/slash_redelegation_test.go Added a test TestSlashRedelegation_ValidatorLeftWithNoTokens to simulate slashing and redelegation scenarios.
x/staking/keeper/slash.go Modified SlashRedelegation method to optimize error handling for SharesFromTokensTruncated when tokens are zero.

Sequence Diagram(s)

sequenceDiagram
    participant Tester
    participant Keeper
    participant Validator
    participant Account
    Tester->>Validator: Create Validator
    Tester->>Account: Fund Account
    Tester->>Validator: Delegate Tokens
    Tester->>Validator: Commit Infraction
    Tester->>Validator: Undelegate Tokens
    Tester->>Keeper: Slash Validator
    Keeper->>Validator: Reduce Tokens
    Tester->>Tester: Assert Invariants
Loading

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 testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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 testing code 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 and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@facundomedica facundomedica marked this pull request as ready for review July 18, 2024 11:06
@facundomedica facundomedica requested a review from a team as a code owner July 18, 2024 11:06
Copy link
Contributor

@facundomedica your pull request is missing a changelog!

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.

Actionable comments posted: 0

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7d2a6b9 and 96d268e.

Files selected for processing (2)
  • tests/integration/slashing/keeper/slash_redelegation_test.go (1 hunks)
  • x/staking/keeper/slash.go (1 hunks)
Additional context used
Path-based instructions (2)
x/staking/keeper/slash.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

tests/integration/slashing/keeper/slash_redelegation_test.go (3)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern tests/**/*: "Assess the integration and e2e test code assessing sufficient code coverage for the changes associated in the pull request"


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

Additional comments not posted (14)
x/staking/keeper/slash.go (1)

377-377: Ensure proper handling of zero tokens scenario.

Ignoring the error from SharesFromTokensTruncated when tokens are zero is appropriate here to streamline control flow. However, ensure that this does not mask any other potential issues that could arise from different contexts.

tests/integration/slashing/keeper/slash_redelegation_test.go (13)

369-389: Good setup for the test environment.

The setup for various keepers and the application initialization is well-structured.


392-396: Ensure context and message server are correctly initialized.

The initialization of the SDK context and the staking message server is correct. Ensure that the bond denomination retrieval does not fail in different environments.


398-407: Proper creation and funding of validators.

The creation and funding of the source and destination validators are correctly implemented.


419-432: Correct delegation of user tokens to validators.

The delegation of user tokens to the source and destination validators is correctly implemented.


437-443: Accurate recording of infraction height for DST validator.

The recording of the infraction height for the destination validator is accurate.


448-451: Correct undelegation of user tokens from DST validator.

The undelegation of user tokens from the destination validator is correctly implemented.


453-459: Accurate recording of infraction height for SRC validator.

The recording of the infraction height for the source validator is accurate.


464-467: Proper redelegation of user tokens from SRC to DST validator.

The redelegation of user tokens from the source validator to the destination validator is correctly implemented.


469-472: Correct undelegation of self-delegation from DST validator.

The undelegation of self-delegation from the destination validator is correctly implemented.


477-479: Proper undelegation of user tokens from DST validator.

The undelegation of user tokens from the destination validator is correctly implemented.


481-484: Correct assertion for zero tokens in DST validator.

The assertion to check that the destination validator has zero tokens is correctly implemented.


486-491: Proper slashing of infractions for DST and SRC validators.

The slashing of infractions for the destination and source validators is correctly implemented.


493-501: Correct assertion of invariants to ensure correctness.

The assertions to ensure correctness of the slashing process are correctly implemented.

Copy link
Member

@julienrbrt julienrbrt left a comment

Choose a reason for hiding this comment

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

lgtm!


sharesToUnbond, _ := dstVal.SharesFromTokensTruncated(slashAmount) // ignore the error as it only errors when tokens == 0
Copy link
Member

Choose a reason for hiding this comment

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

Seems like it would be safer to do the error check but ignore when sharesToUnbond.IsZero(). If that isn't the case, it could be some other error. This is more future proof

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed it to

		sharesToUnbond, err := dstVal.SharesFromTokensTruncated(slashAmount) // ignore the error as it only errors when tokens == 0
		if sharesToUnbond.IsZero() {
			continue
		} else if err != nil {
			return math.ZeroInt(), err
		}

although in the future we should stop returning an error in SharesFromTokensTruncated, because we are only handling the error if we get a value != 0 and an error.

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.

Actionable comments posted: 0

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 96d268e and 57cfad1.

Files selected for processing (1)
  • x/staking/keeper/slash.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • x/staking/keeper/slash.go

@facundomedica facundomedica added this pull request to the merge queue Jul 24, 2024
Merged via the queue into main with commit e8222c8 Jul 24, 2024
75 checks passed
@facundomedica facundomedica deleted the facu/fixslashing2 branch July 24, 2024 11:11
@facundomedica facundomedica added the backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release label Jul 24, 2024
mergify bot pushed a commit that referenced this pull request Jul 24, 2024
julienrbrt pushed a commit that referenced this pull request Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release C:x/staking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants