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: prevent panic in RedeemTokensForShares when too few tokens are redeemed #19076

Merged
merged 7 commits into from
Jan 17, 2024

Conversation

sainoe
Copy link
Contributor

@sainoe sainoe commented Jan 16, 2024

Description

In RedeemTokensForShares(), this line establishes a 1:1 mapping between the redeemed token amount and shares.
However, this mapping is accurate only when a validator hasn't received any rewards and hasn't been slashed.

This PR changes the mapping by calculating shares as a fraction of the redeemed tokens
and the total share token supply, akin to the approach taken here.

Edit: This PR updates RedeemTokensForShares to return an error when the redemption amount is too small and truncates to 0 shares. That prevents a panic in SendCoins.

cc @sampocs


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

@sainoe sainoe requested a review from a team as a code owner January 16, 2024 18:14
x/staking/types/errors.go Outdated Show resolved Hide resolved
@sainoe sainoe requested a review from mpoke January 16, 2024 18:27
Comment on lines 858 to 859
shareDenomSupply := k.bankKeeper.GetSupply(ctx, shareToken.Denom)
shares := delegation.Shares.Mul(sdk.NewDecFromInt(shareToken.Amount)).QuoInt(shareDenomSupply.Amount)
Copy link
Contributor

Choose a reason for hiding this comment

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

Apologies for the confusion, but can you elaborate on why this is inaccurate in the case of a validator having pending rewards or being slashed?

The LSM token is intended to map 1:1 with shares (it is minted based on shares, not tokens), so I was under the assumption that it would naturally capture rewards/slashing.

Copy link
Contributor Author

@sainoe sainoe Jan 17, 2024

Choose a reason for hiding this comment

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

The LSM token is intended to map 1:1 with shares (it is minted based on shares, not tokens), so I was under the assumption that it would naturally capture rewards/slashing.

You're 100% right. Sorry, I got it the wrong way. The simulation tests were failing due to these lines, which don't use a 1:1 mapping between LSM tokens and shares.

@sainoe sainoe merged commit 85b42a5 into feature/v0.47.x-ics-lsm Jan 17, 2024
22 of 24 checks passed
@sainoe sainoe deleted the sainoe/tiny-redeem-fix branch January 17, 2024 14:29
JayT106 pushed a commit to JayT106/cosmos-sdk that referenced this pull request Feb 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🥳 Done
Development

Successfully merging this pull request may close these issues.

3 participants