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

x/gamm: Catch overflow panics on CalculateSpotPrice #2405

Merged
merged 6 commits into from
Aug 25, 2022
Merged

Conversation

AlpinYukseloglu
Copy link
Contributor

@AlpinYukseloglu AlpinYukseloglu commented Aug 14, 2022

Closes: #2207, #2445

What is the purpose of the change

This PR prevents SpotPrice from panicing and makes it return an error for invalid or overflowing spot prices instead. It also adds tests to ensure spot prices stay within the set bounds.

Brief Changelog

  • SpotPrice now recovers panics and returns an error. It also sets a max spot price of 2^160

Testing and Verifying

  • New tests can be found in x/gamm/pool-models/balancer/pool_suite_test.go

Documentation and Release Note

  • Does this pull request introduce a new feature or user-facing behavior changes? (yes / no)
  • Is a relevant changelog entry added to the Unreleased section in CHANGELOG.md? (yes / no)
  • How is the feature or change documented? (not applicable / specification (x/<module>/spec/) / Osmosis docs repo / not documented)

@github-actions github-actions bot added the C:x/gamm Changes, features and bugs related to the gamm module. label Aug 14, 2022
@AlpinYukseloglu AlpinYukseloglu changed the title add overflow tests x/gamm: Add overflow tests to balancer spot price Aug 14, 2022
@AlpinYukseloglu AlpinYukseloglu marked this pull request as ready for review August 22, 2022 15:09
@AlpinYukseloglu AlpinYukseloglu requested a review from a team August 22, 2022 15:09
// defer function to escape the panic when spot price overflows
if r := recover(); r != nil {
spotPrice = sdk.Dec{}
err = types.ErrSpotPriceOverflow
Copy link
Member

Choose a reason for hiding this comment

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

Lets mark this as a spot price internal error

Comment on lines 635 to 637
spotPrice = osmomath.SigFigRound(fullRatio, types.SigFigs)

return spotPrice, err
Copy link
Member

Choose a reason for hiding this comment

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

Needs to add an overflow check here right? (E.g. a maximum of 2128 or 2160)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right! I just added a check that capped at 2^160, although we could technically increase it to 2^196 without overflowing on equal weight pools. I personally think the buffer is useful regardless but would be curious to hear your thoughts

@AlpinYukseloglu
Copy link
Contributor Author

Comments should all be addressed!

spotPrice = osmomath.SigFigRound(fullRatio, types.SigFigs)
if spotPrice.GT(sdk.NewDec(2).Power(160)) {
spotPrice = sdk.Dec{}
err = errors.New("spot price overflow")
Copy link
Member

@ValarDragon ValarDragon Aug 25, 2022

Choose a reason for hiding this comment

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

err, the prior error type was good! We just need two errors:
ErrSpotPriceInternal, ErrSpotPriceOverflow

Copy link
Contributor Author

@AlpinYukseloglu AlpinYukseloglu Aug 25, 2022

Choose a reason for hiding this comment

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

Ah I see what you meant now – should be fixed!

Copy link
Member

@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 once we re-instate the error types!

@AlpinYukseloglu AlpinYukseloglu changed the title x/gamm: Add overflow tests to balancer spot price x/gamm: Catch overflow panics on CalculateSpotPrice Aug 25, 2022
Copy link
Member

@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.

Nice work on this, thanks!

We should also update the spec + code comment, but we can do that in a follow up PR

@ValarDragon ValarDragon merged commit bd683a3 into main Aug 25, 2022
@ValarDragon ValarDragon deleted the twap-overflow branch August 25, 2022 11:10
@ValarDragon
Copy link
Member

Oh I didn't catch this, this was done at the balancer level, which is the wrong place to do it.

This should be done at the keeper level, will move in a follow-up PR.

@ValarDragon
Copy link
Member

Yikes, spot price rounding is also at the wrong layer of abstraction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:x/gamm Changes, features and bugs related to the gamm module.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

gamm: Create tests to find the smallest and largest spot prices
2 participants