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

Move spot price safety checks to keeper layer #2508

Merged
merged 11 commits into from
Sep 3, 2022

Conversation

ValarDragon
Copy link
Member

@ValarDragon ValarDragon commented Aug 25, 2022

Closes: #2507

What is the purpose of the change

  • Move safety checks for spot price against internal logic issues, sig fig rounding, and overflow to the keeper layer
  • Fix bug where grpc query uses pool spot price instead of keeper method
  • Document these safety guarantees

This is currently blocked on #2405 , as create pool panics right now

Testing and Verifying

This change should be moving + adding new test cases in total, each more modularly guaranteeing a component.

Documentation and Release Note

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

@github-actions github-actions bot added C:app-wiring Changes to the app folder C:docs Improvements or additions to documentation C:x/gamm Changes, features and bugs related to the gamm module. labels Aug 25, 2022
@ValarDragon ValarDragon changed the title Move spot price safety checks to keeper layer + safety checks Move spot price safety checks to keeper layer Aug 25, 2022
app/apptesting/gamm.go Outdated Show resolved Hide resolved
@czarcas7ic czarcas7ic added this to the V12 Blockers milestone Sep 1, 2022
@github-actions github-actions bot added the C:x/twap Changes to the twap module label Sep 1, 2022
@ValarDragon ValarDragon marked this pull request as ready for review September 1, 2022 20:22
@ValarDragon ValarDragon requested a review from a team September 1, 2022 20:22
Copy link
Member

@mattverse mattverse left a comment

Choose a reason for hiding this comment

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

Left some questions and comments, otherwise lgtm!

x/gamm/keeper/pool_service.go Outdated Show resolved Hide resolved
// spot_price = (Base_supply / Weight_base) / (Quote_supply / Weight_quote)
// spot_price = (weight_quote / weight_base) * (base_supply / quote_supply)
invWeightRatio := quote.Weight.ToDec().Quo(base.Weight.ToDec())
supplyRatio := base.Token.Amount.ToDec().Quo(quote.Token.Amount.ToDec())
fullRatio := supplyRatio.Mul(invWeightRatio)
Copy link
Member

Choose a reason for hiding this comment

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

confirmed this logic has been correctly moved

@@ -10,6 +10,8 @@ import (
"github.com/osmosis-labs/osmosis/v11/osmoutils"
)

var MaxSpotPrice = sdk.NewDec(2).Power(128).Sub(sdk.OneDec())
Copy link
Member

Choose a reason for hiding this comment

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

what's the reason we don't import it / use it directly from gamm module but declare it twice both in twap module and gamm module?

Copy link
Member Author

Choose a reason for hiding this comment

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

imports basically, introduces a dependency on its types package. (Maybe this doesn't make sense though)

I think it is ok for the two to diverge with time

@@ -53,7 +53,12 @@ func getSpotPrices(ctx sdk.Context, k types.AmmInterface, poolId uint64, denom0,
sp1 = sdk.ZeroDec()
}
}
// if sp0.GT(gammtypes.MaxSpotPrice)
if sp0.GT(types.MaxSpotPrice) {
Copy link
Member

Choose a reason for hiding this comment

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

this is just a safety check since CalculateSpotPrice shouldnt return values over types.MaxSpotPrice right?

Copy link
Member Author

Choose a reason for hiding this comment

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

yup! In case a different spot price backend gets put in

Co-authored-by: Matt, Park <45252226+mattverse@users.noreply.github.com>
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.

LGTM!

Copy link
Member

@p0mvn p0mvn left a comment

Choose a reason for hiding this comment

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

Great work!

Before merging, I would like to understand whether we need to update the latestErrTime when getting spot prices in TWAP and assuming a different backend.

Also, to discuss and plan increasing test coverage for the keeper's CalculateSpotPrice

x/twap/logic.go Outdated Show resolved Hide resolved
x/twap/logic.go Outdated Show resolved Hide resolved
x/gamm/keeper/pool_service.go Show resolved Hide resolved
x/gamm/keeper/pool_service.go Show resolved Hide resolved
x/gamm/keeper/pool_service.go Show resolved Hide resolved
@@ -6,6 +6,9 @@ var pointOne = sdk.OneDec().QuoInt64(10)

// SigFigRound rounds to a specified significant figure.
func SigFigRound(d sdk.Dec, tenToSigFig sdk.Int) sdk.Dec {
if d.IsZero() {
return d
Copy link
Member

Choose a reason for hiding this comment

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

We are completely missing tests for this function. We should add them

Copy link
Member Author

@ValarDragon ValarDragon Sep 2, 2022

Choose a reason for hiding this comment

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

Agreed, should be done in a separate PR, and is not v12 blocking

if spotPrice.GT(types.MaxSpotPrice) {
return types.MaxSpotPrice, types.ErrSpotPriceOverflow
} else if !spotPrice.IsPositive() {
return sdk.Dec{}, types.ErrSpotPriceInternal
Copy link
Member

Choose a reason for hiding this comment

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

It seems that the only time we are hitting this branch during the tests is when we attempt to set up a pool. Can we have a test case directly covering this branch?

Copy link
Member Author

@ValarDragon ValarDragon Sep 2, 2022

Choose a reason for hiding this comment

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

I don't think this matters that much to directly test, as a blocker for this PR. Its a safety check to give a clear pre-condition for successful results.

// defer to catch panics, in case something internal overflows.
defer func() {
if r := recover(); r != nil {
spotPrice = sdk.Dec{}
Copy link
Member

Choose a reason for hiding this comment

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

We don't have a test case covering this branch. Is it possible to add it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not easily, I tried.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually I tried harder, it is!

Thanks for pointing this out :)

x/gamm/keeper/pool_service_test.go Outdated Show resolved Hide resolved
@ValarDragon ValarDragon merged commit 83ed9ea into main Sep 3, 2022
@ValarDragon ValarDragon deleted the dev/spot_price_keeper_layer_move branch September 3, 2022 12:54
@p0mvn p0mvn mentioned this pull request Sep 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:app-wiring Changes to the app folder C:docs Improvements or additions to documentation C:x/gamm Changes, features and bugs related to the gamm module. C:x/twap Changes to the twap module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move spot price error handling, panic catching, and rounding to keeper layer
6 participants