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(x/twap): incorrect time delta due to nanoseconds in time #4359

Merged
merged 9 commits into from
Feb 20, 2023

Conversation

p0mvn
Copy link
Member

@p0mvn p0mvn commented Feb 17, 2023

Closes: #4342

What is the purpose of the change

This PR fixes twap jitter that is caused by nanoseconds being accounted for in the time delta calculations.

In twap, we assume the time delta in milliseconds. However, the block time is given in nanoseconds. As a result, there were some cases such as:

timeDelta := endRecord.Time.Sub(startRecord.Time)

where if startRecord.Time contained just a few nanoseconds, it would cause the timeDelta to fall by 1 ms below the expected value, causing a larger denominator in arithmetic mean computations. This would lead the final twap being off in the 10^-4-10^-2 digit.

To fix this, a function types.CanonicalTimeMs is introduced that converts the time stored in state into unix milliseconds with monotonic clock stripped away. This is consistent with how CometBFT handles time.

Brief Changelog

  • observed random failures in e2e twap tests in Consensus min gas fee of .0025 uosmo #4244
  • debugged with various logs added and manual Python recomputation of every step
  • observed problems related to nanoseconds when computing time delta
  • reproduced in unit tests
  • refactored to strip away nanoseconds with the use of types.CanonicalTimeMs function

Testing and Verifying

This change added tests and is covered by e2e

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
  • How is the feature or change documented? not applicable

@github-actions github-actions bot added the C:x/twap Changes to the twap module label Feb 17, 2023
@p0mvn p0mvn added A:backport/v15.x backport patches to v15.x branch V:state/compatible/backport State machine compatible PR, should be backported labels Feb 17, 2023
@p0mvn p0mvn marked this pull request as ready for review February 17, 2023 18:07
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.

LGTM 👍

x/twap/types/utils.go Outdated Show resolved Hide resolved
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 nit comments, otherwise LGTM.

Also curious about whether this change does not cause conflicts with existing records

x/twap/types/utils.go Outdated Show resolved Hide resolved
@@ -62,6 +60,12 @@ func LexicographicalOrderDenoms(denom0, denom1 string) (string, string, error) {
return denom0, denom1, nil
}

// CanonicalTimeMs returns the canonical time in milleseconds used for twap
// math computations in UTC. Removes any monotonic clock reading prior to conversion to ms.
func CanonicalTimeMs(twapTime time.Time) int64 {
Copy link
Member

Choose a reason for hiding this comment

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

just curious, why is it called "canonical" time?

Copy link
Member Author

Choose a reason for hiding this comment

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

Canonical with respect to block times. It strips away any mononic part from the clock and returns the time in UTC,

This should already be done given that we use ctx.BlockTime(). However, this is useful for tests since we might not be setting up the time correctly in tests

@@ -182,7 +182,7 @@ func recordWithUpdatedAccumulators(record types.TwapRecord, newTime time.Time) t
return record
}
newRecord := record
timeDelta := newTime.Sub(record.Time)
timeDelta := types.CanonicalTimeMs(newTime) - types.CanonicalTimeMs(record.Time)
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 add a one line comment above here on why we use canonical time for the time delta?

Copy link
Member Author

Choose a reason for hiding this comment

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

Since this is called in multiple places, I elaborated in the method spec instead:
937d148

@ValarDragon ValarDragon merged commit f56280f into main Feb 20, 2023
@ValarDragon ValarDragon deleted the roman/fix-twap branch February 20, 2023 16:10
mergify bot pushed a commit that referenced this pull request Feb 20, 2023
* fix(x/twap): incorrect time delta due to nanoseconds in time

* remove logs

* more clean ups

* changelog

* restore arithmetic twap test

* remove old

* unskip geometric

* Update x/twap/types/utils.go

Co-authored-by: Matt, Park <[email protected]>

* comment

---------

Co-authored-by: Matt, Park <[email protected]>
(cherry picked from commit f56280f)

# Conflicts:
#	CHANGELOG.md
pysel pushed a commit that referenced this pull request Feb 21, 2023
* fix(x/twap): incorrect time delta due to nanoseconds in time

* remove logs

* more clean ups

* changelog

* restore arithmetic twap test

* remove old

* unskip geometric

* Update x/twap/types/utils.go

Co-authored-by: Matt, Park <[email protected]>

* comment

---------

Co-authored-by: Matt, Park <[email protected]>
@p0mvn p0mvn mentioned this pull request Sep 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:backport/v15.x backport patches to v15.x branch C:x/twap Changes to the twap module V:state/compatible/backport State machine compatible PR, should be backported
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

fix(e2e) fix TestArithmeticTWAP
4 participants