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: Flip existing twapRecords base/quote price denoms #5939

Merged
merged 21 commits into from
Aug 9, 2023
Merged

Conversation

stackman27
Copy link
Contributor

@stackman27 stackman27 commented Aug 1, 2023

Closes: #XXX

What is the purpose of the change

since twap depends on spot-price query and spot-price query base/quote denom is switched. We switch that back in TWAP too

Testing and Verifying

added test for upgrade handler

existing twap records

❯ osmosisd q twap arithmetic 1066 uosmo 1690755728 24h  --node=https://rpc.osmosis.zone:443
arithmetic_twap: "0.000000000002056044"
❯ osmosisd q twap arithmetic 1066 ibc/0CD3A0285E1341859B5E86B6AB7682F023D03E97607CCC1DC95706411D866DF7 1690755728 24h  --node=https://rpc.osmosis.zone:443
arithmetic_twap: "486397499707.552675212311200837"

should be flipped

Documentation and Release Note

  • Does this pull request introduce a new feature or user-facing behavior changes?
  • Changelog entry added to Unreleased section of CHANGELOG.md?

Where is the change documented?

  • Specification (x/{module}/README.md)
  • Osmosis documentation site
  • Code comments?
  • N/A

@github-actions github-actions bot added C:app-wiring Changes to the app folder C:x/twap Changes to the twap module labels Aug 1, 2023
x/twap/store.go Outdated Show resolved Hide resolved
@stackman27 stackman27 added the V:state/breaking State machine breaking PR label Aug 1, 2023
@stackman27 stackman27 marked this pull request as ready for review August 1, 2023 21:02
@czarcas7ic
Copy link
Member

@stackman27 does anything need to change now that #5863 was merged?

Copy link
Member

@czarcas7ic czarcas7ic left a comment

Choose a reason for hiding this comment

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

Will do another pass when these are addressed, thank you!

app/upgrades/v17/upgrades.go Outdated Show resolved Hide resolved
app/upgrades/v17/upgrades.go Show resolved Hide resolved
app/upgrades/v17/upgrades_test.go Outdated Show resolved Hide resolved
@stackman27
Copy link
Contributor Author

stackman27 commented Aug 2, 2023

@stackman27 does anything need to change now that #5863 was merged?

i dont think so, i compared mainnet twap records with mainnet spot price. So if mainnet spot price is wrong twap should be wrong

for ex: in the call below osmo is base and we are quoting dai incorrectly

it's showing 1osmo ~ 2.056044dai
and 1dai ~ 0.4863974997075527 osmo

❯ osmosisd q twap arithmetic 1066 uosmo 1690755728 24h  --node=https://rpc.osmosis.zone:443
arithmetic_twap: "0.000000000002056044"

@stackman27 stackman27 requested a review from czarcas7ic August 3, 2023 16:03
CHANGELOG.md Show resolved Hide resolved
app/upgrades/v16/upgrades.go Outdated Show resolved Hide resolved
x/concentrated-liquidity/pool.go Outdated Show resolved Hide resolved
app/upgrades/v17/upgrades.go Show resolved Hide resolved
x/twap/store.go Outdated Show resolved Hide resolved
app/upgrades/v17/upgrades.go Outdated Show resolved Hide resolved
app/upgrades/v17/upgrades.go Show resolved Hide resolved
x/twap/store.go Outdated Show resolved Hide resolved
Copy link
Member

@czarcas7ic czarcas7ic left a comment

Choose a reason for hiding this comment

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

This looks good, but we should add the upgrade handler test described in comment before merging. Will approve after that, then will need a second approval from there. Thanks!

Comment on lines 155 to 158
func FlipTwapSpotPriceRecords(ctx sdk.Context, poolIds []uint64, keepers *keepers.AppKeepers) error {
for _, poolId := range poolIds {
// check that this is a cl pool
_, err := keepers.ConcentratedLiquidityKeeper.GetConcentratedPoolById(ctx, poolId)
Copy link
Member

Choose a reason for hiding this comment

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

Lets just take in an array of PoolI without the type check. Then in the upgrade handler we just call GetPools and pass in the result of GetPools directly in. No need to

  1. Get pools
  2. Extract pool IDs
  3. Get pool again

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed! 21a7552

app/upgrades/v17/upgrades.go Outdated Show resolved Hide resolved
Comment on lines 155 to 181
clPoolTwapRecordPreUpgrade, err := keepers.TwapKeeper.GetAllMostRecentRecordsForPool(ctx, existingPool.GetId())
suite.Require().NoError(err)

clPoolTwapRecordHistoricalPoolIndex, err := keepers.TwapKeeper.GetAllHistoricalPoolIndexedTWAPsForPoolId(ctx, existingPool.GetId())
suite.Require().NoError(err)
Copy link
Member

Choose a reason for hiding this comment

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

Its good that we added this here, but we need to add logic in the success cases that checks the new record post upgrade to ensure both records were changed as expected. I would also create more than one historical record. Then, after the upgrade, you can iterate over the historical records to see if the values were swapped, then just compare the current record to ensure that flipped as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh yea sorry i had this test created but forgot to push 78c7cec#diff-8d0951a4357ce3ef3f57168835dcc72078bd30337d8f9882c3b0d1e64a815e77R180-R195

Copy link
Contributor Author

@stackman27 stackman27 Aug 8, 2023

Choose a reason for hiding this comment

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

the above test contains >1 historical records. lmk if this suffice, i can write more test too!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added more detailed test here: 4ea33fa

app/upgrades/v17/upgrades.go Outdated Show resolved Hide resolved
app/upgrades/v17/upgrades.go Show resolved Hide resolved
x/twap/store.go Outdated Show resolved Hide resolved
x/twap/store.go Outdated Show resolved Hide resolved
x/twap/types/keys.go Outdated Show resolved Hide resolved
x/twap/store.go Outdated Show resolved Hide resolved
@p0mvn
Copy link
Member

p0mvn commented Aug 8, 2023

What I'm missing from approval is the answer to this question in the review.

I think that after the earlier suggested refactor was complete, it is not present but would like a confirmation.

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.

This cycle focuses on tests. I think the tests should be expanded prior to merge

app/upgrades/v17/upgrades_test.go Outdated Show resolved Hide resolved
app/upgrades/v17/upgrades_test.go Outdated Show resolved Hide resolved
app/upgrades/v17/upgrades_test.go Outdated Show resolved Hide resolved
app/upgrades/v17/upgrades_test.go Show resolved Hide resolved
@stackman27 stackman27 requested a review from p0mvn August 8, 2023 17:11
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.

This LGTM after the latest test requests are addressed

app/upgrades/v17/upgrades_test.go Show resolved Hide resolved
app/upgrades/v17/upgrades_test.go Outdated Show resolved Hide resolved
@stackman27 stackman27 requested a review from p0mvn August 8, 2023 17:59
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.

Nice work on working through a long series of feedbacks! LGTM

Have two more minor comments + a general suggestion that test code could benefit from more comments explaining the invariants being tested. Bumping up our recently updated guidelines here


},
func(ctx sdk.Context, keepers *keepers.AppKeepers, expectedCoinsUsedInUpgradeHandler sdk.Coins, lastPoolID uint64) {
func(ctx sdk.Context, keepers *keepers.AppKeepers, expectedCoinsUsedInUpgradeHandler sdk.Coins, lastPoolID uint64, clPoolTwapRecordPreUpgrade []types.TwapRecord) {
Copy link
Member

Choose a reason for hiding this comment

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

Consider avoiding passing in clPoolTwapRecordPreUpgrade here and returning them from pre-upgrade logic. Instead, use the pool id similar to the other pool

Copy link
Contributor Author

@stackman27 stackman27 Aug 8, 2023

Choose a reason for hiding this comment

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

okay that sounds good too

addressed 8460510

Comment on lines 268 to 273
if (clPoolsTwapRecordHistoricalTimeIndexPostUpgrade[i].PoolId == lastPoolID) || (clPoolsTwapRecordHistoricalTimeIndexPostUpgrade[i].PoolId == lastPoolIdMinusOne) {
suite.Require().Equal(clPoolsTwapRecordHistoricalTimeIndexPreUpgrade[i].Asset0Denom, clPoolsTwapRecordHistoricalTimeIndexPostUpgrade[i].Asset1Denom)
suite.Require().Equal(clPoolsTwapRecordHistoricalTimeIndexPreUpgrade[i].Asset1Denom, clPoolsTwapRecordHistoricalTimeIndexPostUpgrade[i].Asset0Denom)
suite.Require().Equal(clPoolsTwapRecordHistoricalTimeIndexPreUpgrade[i].P0LastSpotPrice, clPoolsTwapRecordHistoricalTimeIndexPostUpgrade[i].P1LastSpotPrice)
suite.Require().Equal(clPoolsTwapRecordHistoricalTimeIndexPreUpgrade[i].P1LastSpotPrice, clPoolsTwapRecordHistoricalTimeIndexPostUpgrade[i].P0LastSpotPrice)
}
Copy link
Member

Choose a reason for hiding this comment

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

Please also add an else case here and check that for other pools these are not switched. Please make sure that at least one other pool has historical time indexed twap record

Copy link
Contributor Author

@stackman27 stackman27 Aug 8, 2023

Choose a reason for hiding this comment

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

since all CL pool gets flipped & we delete old index and create new index this was a bit hard to check. I checked with balancer pool and confirmed records are not flipped

Copy link
Member

@czarcas7ic czarcas7ic 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! The changes you made to the test file make me feel much better about this change.

Side note, when we upgrade testnet, can you check that this flipped the testnet record as expected as well?

app/upgrades/v17/upgrades.go Outdated Show resolved Hide resolved
Comment on lines 244 to 249
// check that all TWAP records aren't empty
suite.Require().NotEmpty(clPool1TwapRecordPostUpgrade, "Most recent TWAP records should not be empty after upgrade.")
suite.Require().NotEmpty(clPool1TwapRecordHistoricalPoolIndexPostUpgrade, "Historical Pool Index TWAP record should not be empty after upgrade.")
suite.Require().NotEmpty(clPool2TwapRecordPostUpgrade, "Most recent TWAP records should not be empty after upgrade.")
suite.Require().NotEmpty(clPool2TwapRecordHistoricalPoolIndexPostUpgrade, "Historical Pool Index TWAP record should not be empty after upgrade.")
suite.Require().NotEmpty(clPoolsTwapRecordHistoricalTimeIndexPostUpgrade, "Historical Time Index TWAP record should not be empty after upgrade.")
Copy link
Member

Choose a reason for hiding this comment

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

nit: i think this is just unnecessary verbosity and that gets indirectly checked below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed the text but kept the check!

app/upgrades/v17/upgrades_test.go Outdated Show resolved Hide resolved
@stackman27
Copy link
Contributor Author

Merging since it has 2 approvals!!

@stackman27 stackman27 merged commit aae894f into main Aug 9, 2023
@stackman27 stackman27 deleted the stack/fix-twap branch August 9, 2023 01:55
p0mvn pushed a commit that referenced this pull request Aug 29, 2023
* added logic

* added upgrade handler test

* added changelog

* flip spot price too

* adams suggestions

* rebased

* adams final suggestion

* roman comments

* roman suggestion

* cleanup

* romans suggestion

* lint

* misc cleanup

* added test

* more tests

* added verbose tests

* nit

* romans comment for different twap request

* nit

* roman suggestion

* adams suggestion
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:x/twap Changes to the twap module V:state/breaking State machine breaking PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants