-
Notifications
You must be signed in to change notification settings - Fork 607
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
Conversation
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.
LGTM 👍
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.
Left some nit comments, otherwise LGTM.
Also curious about whether this change does not cause conflicts with existing records
@@ -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 { |
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.
just curious, why is it called "canonical" time?
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.
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) |
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.
can we add a one line comment above here on why we use canonical time for the time delta?
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.
Since this is called in multiple places, I elaborated in the method spec instead:
937d148
Co-authored-by: Matt, Park <[email protected]>
* 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
…#4359) (#4374) Co-authored-by: Roman <[email protected]>
* 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]>
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:
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
types.CanonicalTimeMs
functionTesting and Verifying
This change added tests and is covered by e2e
Documentation and Release Note
Unreleased
section inCHANGELOG.md
? yes