fix(hermes): improve TWAP reliability - non-optional price selection and consistent time windows #2521
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR refactors how Hermes fetches the data for the TWAP calculation to improve reliability.
Rationale
window_seconds: u64
andend_time: RequestTime
, instead ofstart_time: RequestTime
andend_time: RequestTime
.start_time
based on the publish time of the end messages. Previously, the actual time difference between start and end messages could differ from the intended window size since the latest timestamp in the cache could be older than the wall time. This change guarantees that the start and end messages arewindow_seconds
apart.RequestTime::LatestTimeEarliestSlot
request mode to the cache that selects messages with the latest publish time but earliest slot number. This follows the same semantics asFirstAfter
, and helps choose non-optional (canonical) prices.prev_publish_time == publish_time
. This is done to avoid optionality of prices, and we consider only the message whereprev_publish_time < publish_time
as the canonical price for that timestamp. Previously we were using theRequestTime::Latest
mode, which just returns the latest message for a feed, NOT the latest canonical message. The new mode provides these semantics.How has this been tested?
test_get_verified_twaps_with_update_data_uses_non_optional_prices
to verify correct message selectiontest_latest_time_earliest_slot_request_works
to test theLatestTimeEarliestSlot
cache request modewindow_seconds
apart.