-
Notifications
You must be signed in to change notification settings - Fork 610
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
chore: twap mutation fixes part 1 #2468
Conversation
// correct ordering of args for db | ||
if asset1Denom > asset0Denom { | ||
asset0Denom, asset1Denom = asset1Denom, asset0Denom | ||
} | ||
return k.getMostRecentRecord(ctx, poolId, asset0Denom, asset1Denom) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to reviewer: This gets checked/modified in getMostRecentRecord
which is called in the return statement which is why I removed it here.
@@ -27,16 +28,18 @@ func (s *TestSuite) TestGetBeginBlockAccumulatorRecord() { | |||
poolId uint64 | |||
quoteDenom string | |||
baseDenom string | |||
expError bool | |||
expError error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to reviewer: changed this to specific error type since I realized things were passing with an error, but not the right error we desired. Might want to eventually have an errors type file in twap so we can specify specific error codes instead of manually specifying expected errors here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we should make error types. (Lets not do that move this PR, other parallel PRs will conflict that change that logic) We don't need a code though, golang errors.Is
will suffice for our usecase here
@@ -41,7 +41,7 @@ func (s *TestSuite) SetupTest() { | |||
// sets up a new two asset pool, with spot price 1 | |||
func (s *TestSuite) setupDefaultPool() (poolId uint64, denomA, denomB string) { | |||
poolId = s.PrepareUni2PoolWithAssets(defaultUniV2Coins[0], defaultUniV2Coins[1]) | |||
denomA, denomB = defaultUniV2Coins[1].Denom, defaultUniV2Coins[0].Denom |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to reviewer: This is where a majority of the issue was coming from, we were setting denomA as if it were lexicographically smaller when in reality it was the larger.
x/twap/listeners_test.go
Outdated
@@ -50,6 +50,7 @@ func (s *TestSuite) TestSwapAndEndBlockTriggeringSave() { | |||
s.Commit() // clear transient store | |||
// Now on a clean state after a create pool | |||
s.Require().Equal(baseTime.Add(time.Second), s.Ctx.BlockTime()) | |||
s.RunBasicJoin(poolId) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to reviewer: This is needed to test the JoinPool twap hook (mutation tests caught this!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Err, this should be its own test. We need to test on a clean instance, a join in a block will trigger the end tracking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job! Using the right order + this helper really cleans things up!
One change needed for the RunBasicJoin
test, then I think its good to merge :)
Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com>
x/twap/listeners_test.go
Outdated
denomPairs0, denomPairs1 := types.GetAllUniqueDenomPairs(denoms) | ||
expectedRecords := []types.TwapRecord{} | ||
for i := 0; i < len(denomPairs0); i++ { | ||
expectedRecord, err := types.NewTwapRecord(s.App.GAMMKeeper, s.Ctx, poolId, denomPairs0[i], denomPairs1[i]) | ||
for i := len(denomPairs0); i < 0; i-- { | ||
expectedRecord, err := twap.NewTwapRecord(s.App.GAMMKeeper, s.Ctx, poolId, denomPairs0[i], denomPairs1[i]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we iterating in reverse order?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just realized why I thought this was necessary. GetAllUniqueDenomPairs
returns the denom pairs in non lexicographical order and NewTwapRecord
auto rearranges them. Then when we get down to GetMostRecentRecordStoreRepresentation
, the order of input matters and does not get auto corrected. I am correcting this now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, fixed in most recent commit. Will need to write in spec about these guarantees still though
if !(denom0 > denom1) { | ||
return TwapRecord{}, fmt.Errorf("precondition denom0 > denom1 not satisfied. denom0 %s | denom1 %s", denom0, denom1) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we remove this check here, I think we need to be clear in the spec / code comments about guarantees we expect. (Which methods expect correct order, which ones will swap order)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
x/twap/README.md
Outdated
All functions that call `LexicographicalOrderDenoms` (such as `getMostRecentRecordStoreRepresentation`, `getRecordAtOrBeforeTime`, and `NewTwapRecord`), both directly and indirectly, will swap the provided denoms to be in ascending lexicographical order. This means that regardless of input, asset0Denom is guaranteed to be lexicographically smaller than asset1Denom. | ||
If this does not occur, an error will be returned, likely indicating that the two denoms provided are equivalent (which is not allowed). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
err, this doesn;t really make sense at the spec level / at least not the API layer: Its an abstraction leak for the caller! It would only apply for the store layer.
Part Of: #2426
What is the purpose of the change
This PR brings the mutation test score for the twap module from ~33% to ~46%
PRing this now to prevent a large diff for the next / upcoming mutation fixes
Brief Changelog
RunBasicJoin
as a GAMM keeper test helpergetMostRecentRecord
switches orderGetBeginBlockAccumulatorRecord
(this is done ingetMostRecentRecord
which this function calls/returns. Better to have the logic at the higher level)TestGetBeginBlockAccumulatorRecord
Testing and Verifying
This change is already covered by existing tests
Documentation and Release Note
Unreleased
section inCHANGELOG.md
? no