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

Update record sanity check #2741

Merged
merged 27 commits into from
May 6, 2023
Merged

Update record sanity check #2741

merged 27 commits into from
May 6, 2023

Conversation

hieuvubk
Copy link
Contributor

Closes: #2686

What is the purpose of the change

Add sanity check in updateRecord to compare record time and height against ctx

Brief Changelog

  • Created new error type
  • Add err handle into updateRecord & UpdateRecords
  • Adjusted test

@hieuvubk hieuvubk requested a review from a team September 14, 2022 15:28
@github-actions github-actions bot added the C:x/twap Changes to the twap module label Sep 14, 2022
@hieuvubk hieuvubk marked this pull request as draft September 14, 2022 15:37
@hieuvubk hieuvubk marked this pull request as ready for review September 23, 2022 14:58
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.

Looks nice! I think we need a test case for checking error on block height and then should be good to merge!

Also curious if the issue / question you asked on the original issue is resolved or if its something that you need further answer on

spotPriceA: tPlus10sp5Record.P0LastSpotPrice,
spotPriceB: tPlus10sp5Record.P1LastSpotPrice,
},
expectError: types.InvalidRecordTimeError{
Copy link
Member

Choose a reason for hiding this comment

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

Can we also add a test case for having block height greater than the last record's time and checking the error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes of course

@hieuvubk hieuvubk requested a review from mattverse September 27, 2022 06:25
x/twap/types/errors.go Outdated Show resolved Hide resolved
@@ -1234,6 +1253,9 @@ func (s *TestSuite) TestUpdateRecords() {
s.SetupTest()
twapKeeper := s.App.TwapKeeper
ctx := s.Ctx.WithBlockTime(tc.blockTime)
if tc.blockHeight != 0 {
ctx = s.Ctx.WithBlockHeader(tmproto.Header{Time: tc.blockTime, Height: tc.blockHeight})
Copy link
Member

Choose a reason for hiding this comment

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

Can we use the WithBlockTime and WithBlockHeight API instead?

// should always be greater than the last record's time.
"two-asset; pre-set at t and t + 1, new record inserted between existing": {
"new record can't be inserted before the last record's update time": {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"new record can't be inserted before the last record's update time": {
"new record can't be inserted prior to the last record's update time": {

@@ -62,3 +62,14 @@ type InvalidRecordCountError struct {
func (e InvalidRecordCountError) Error() string {
return fmt.Sprintf("The number of records do not match, expected: %d\n got: %d", e.Expected, e.Actual)
}

type InvalidRecordTimeError struct {
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if we should change this to InvalidUpdateRecordError or something similar, since this error does not only happen when it has a invalid record time but it also includes invlaid record height as well. Lmk what you think

@hieuvubk
Copy link
Contributor Author

nice thank u

@hieuvubk hieuvubk requested a review from mattverse October 13, 2022 09:52
@hieuvubk hieuvubk requested a review from p0mvn April 6, 2023 06:28
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.

Nice work, logic lgtm, requesting for additional test cases before we can merge!

x/twap/logic.go Outdated
// Returns error for last updated records in the same block.
// Exception: record is initialized when creating a pool,
// then the TwapAccumulator variables are zero.
if (record.Height >= ctx.BlockHeight() || !record.Time.Before(ctx.BlockTime())) &&
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (record.Height >= ctx.BlockHeight() || !record.Time.Before(ctx.BlockTime())) &&
if (record.Height > ctx.BlockHeight() || !record.Time.Before(ctx.BlockTime())) &&

Why is it not > but >=? Can the record not have same height as blockheight by design?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Its a case when creating new pool. New twap records will be created and updated immediately. I limit this by adding the condition ArithmeticTwapAccumulator must be zero (init twap record). But I should split it up

Copy link
Member

Choose a reason for hiding this comment

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

sweet, can we add this context as inline comment?

x/twap/logic.go Show resolved Hide resolved
@github-actions github-actions bot removed the Stale label Apr 7, 2023
@hieuvubk hieuvubk requested a review from mattverse April 10, 2023 08:13
@mattverse mattverse added the V:state/breaking State machine breaking PR label Apr 10, 2023
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.

One comment regarding returned error type. Otherwise lgtm

x/twap/logic.go Outdated
if (record.Height == ctx.BlockHeight() || record.Time.Equal(ctx.BlockTime())) &&
!record.P1ArithmeticTwapAccumulator.Equal(sdk.ZeroDec()) &&
!record.P0ArithmeticTwapAccumulator.Equal(sdk.ZeroDec()) {
return types.TwapRecord{}, fmt.Errorf("Invalid zero twap accumulator")
Copy link
Member

Choose a reason for hiding this comment

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

we should be using return types.TwapRecord{}, types.InvalidUpdateRecordError{ here as well

@hieuvubk hieuvubk requested a review from nicolaslara as a code owner April 11, 2023 21:03
@mattverse
Copy link
Member

Sweet! Let's merge this after updating CHANGELOG please @hieuvubk

@mattverse mattverse self-assigned this Apr 19, 2023
@github-actions
Copy link
Contributor

github-actions bot commented May 4, 2023

This pull request has been automatically marked as stale because it has not had any recent activity. It will be closed if no further activity occurs. Thank you!

@github-actions github-actions bot added the Stale label May 4, 2023
@hieuvubk
Copy link
Contributor Author

hieuvubk commented May 5, 2023

@mattverse hey sr I forgot about this pr :((. Changelog updated

@github-actions github-actions bot removed the Stale label May 6, 2023
@czarcas7ic czarcas7ic merged commit 3608fe9 into main May 6, 2023
@czarcas7ic czarcas7ic deleted the update_record_sanity_check branch May 6, 2023 01:59
pysel pushed a commit that referenced this pull request Jun 6, 2023
* add new error type

* add condition to ensure ctx time > record time

* format

* replace

* fix unused import

* update co conditions, bypass the case when creating pool

* add cmt

* update test for checking block height

* format

* update err message

* update ctx & err

* resolve conflict

* split condition

* update condition & add more test

* lint

* update return err

* update changelog

---------

Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Matt, Park <45252226+mattverse@users.noreply.github.com>
@github-actions github-actions bot mentioned this pull request Feb 15, 2024
@github-actions github-actions bot mentioned this pull request Mar 1, 2024
@github-actions github-actions bot mentioned this pull request Mar 15, 2024
@github-actions github-actions bot mentioned this pull request May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:x/twap Changes to the twap module T:ice-box V:state/breaking State machine breaking PR
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

refactor(twap): add sanity check in updateRecord to compare record time and height against ctx
4 participants