-
Notifications
You must be signed in to change notification settings - Fork 25
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
Validator historical state restoration #922
Merged
Merged
Changes from 68 commits
Commits
Show all changes
71 commits
Select commit
Hold shift + click to select a range
94e61f9
adds a new overload of queryPastEvents allowing to query past events …
marcinczenko e080295
adds state restoration to validator
marcinczenko 0587461
refactors a bit to get the tests back to work
marcinczenko 0c2dd1d
replaces deprecated generic methods from Market with methods for spec…
marcinczenko c19c775
Refactors binary search
marcinczenko b8b2b49
adds market tests for querying past SlotFilled events and binary search
marcinczenko 7fc2072
Takes into account that <<earliest>> block available is not necessari…
marcinczenko e462511
Adds more logging and makes testing earliest block boundary more reli…
marcinczenko 62d6935
adds validation tests for historical state restoration
marcinczenko 981afcd
adds mockprovider to simplify and improve testing of the edge conditions
marcinczenko a00702a
adds slot reservation to the new tests after rebasing
marcinczenko 7bd76ac
adds validation groups and group index in logs of validator
marcinczenko d369ebc
adds integration test with two validators
marcinczenko 6a61f0c
adds comment on how to enable logging in integration test executable …
marcinczenko 4d1efa7
testIntegration: makes list is running nodes injected and available i…
marcinczenko 5c48d0f
validation: adds integration test for historical state
marcinczenko da88109
adds more logging to validator
marcinczenko 11d6a76
integration test: validator only looks 30 days back for historical state
marcinczenko babf1fe
adds logging of the slotState when removing slots during validation
marcinczenko 3ae8685
review and refactor validator integration tests
marcinczenko a0a43f1
adds validation to the set of integration tests
marcinczenko 509af57
Fixes mistyped name of the mock provider module in testMarket
marcinczenko 9ab5c1c
Fixes a typo in the name of the validation suite in integration tests
marcinczenko 4fccdc4
Makes validation unit test a bit easier to follow
marcinczenko c31943e
better use of logScopes to reduce duplication
marcinczenko afb444c
improves timing and clarifies the test conditions
marcinczenko c32eac1
uses http as default RPC provider for nodes running in integration te…
marcinczenko 0534380
simplifies the validation integration tests by waiting for failed req…
marcinczenko 375b65b
adds config option allowing selectively to set different provider url
marcinczenko 00e7d8c
Brings back the default settings for RPC provider in integration tests
marcinczenko 7c51846
use http RPC provider for clients in validation integration tests
marcinczenko 6407714
fine-tune the tests
marcinczenko ad0b8b6
Makes validator integration test more robust - adds extra tracking
marcinczenko c7fd863
brings tracking of marketplace event back to validator integration test
marcinczenko 32ebc5e
refactors integration tests
marcinczenko f48a939
deletes tmp file
marcinczenko df1eea2
adds <<return>> after forcing integration test to fail preliminarily
marcinczenko c2eccc5
re-enables all integration tests and matrix
marcinczenko 1a8a148
stops debug output in CI
marcinczenko 02da9de
allows to choose a different RPC provider for a given integration tes…
marcinczenko 75e86ba
fixes signature of <<getBlock>> method in mockProvider
marcinczenko 5d1c1fe
adds missing import which seem to be braking integration tests on win…
marcinczenko e6e0db1
makes sure that clients, SPs, and validators use the same provider url
marcinczenko 9eb68a0
makes validator integration tests using http at 127.0.0.1:8545
marcinczenko 36ad92b
testvalidator: stop resubscribing as we are now using http polling as…
marcinczenko 321212d
applying review comments
marcinczenko a5a006d
groups queryPastStorage overrides together (review comment)
marcinczenko 79325c3
groups the historical validation tests into a sub suite
marcinczenko fba5cea
removes the temporary extensions in marketplacesuite and multinodesui…
marcinczenko 580bc5c
simplifies validation integration tests
marcinczenko bb784a5
Removes debug logs when waiting for request to fail
marcinczenko a9a1f50
Renaming waitForRequestFailed => waitForRequestToFail
marcinczenko 044251e
renames blockNumberForBlocksAgo to pastBlockTag and makes it private
marcinczenko abdf711
removes redundant debugging logs
marcinczenko d7736f1
refines logging in validation
marcinczenko bccfcc4
removes dev logging from mockmarket
marcinczenko 9459508
improves exception handling in provider helper procs and prepares for…
marcinczenko f57d6e3
Uses chronos instead of std/times for Duration
marcinczenko dc1869a
extracts provider and binary search helpers to a separate module
marcinczenko cfc91f2
removes redundant log entry params from validator
marcinczenko a09f07d
unifies the notation to consistently use method call syntax
marcinczenko 14c308d
reuses ProviderError from nim-ethers in the provider extension
marcinczenko 539877a
clarifies the comment in multinodesuite
marcinczenko fa9d6e1
uses == operator to check the predefined tags and raises exception wh…
marcinczenko c9f66a0
when waiting for request to fail, we break on any request state that …
marcinczenko 19e576e
removes tests that were moved to testProvider from testMarket
marcinczenko e625918
extracts tests that use MockProvider to a separate async suite
marcinczenko a43e1e0
improves performance of the historical state restoration
marcinczenko 5c48ca4
removing redundant log messages in validator (groupIndex and groups)
marcinczenko 217b588
adds testProvider to testContracts group
marcinczenko 8b91c68
removes unused import in testMarket
marcinczenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import pkg/ethers/provider | ||
import pkg/chronos | ||
import pkg/questionable | ||
|
||
import ../logutils | ||
|
||
from ../clock import SecondsSince1970 | ||
|
||
logScope: | ||
topics = "marketplace onchain provider" | ||
|
||
proc raiseProviderError(message: string) {.raises: [ProviderError].} = | ||
raise newException(ProviderError, message) | ||
|
||
proc blockNumberAndTimestamp*(provider: Provider, blockTag: BlockTag): | ||
Future[(UInt256, UInt256)] {.async: (raises: [ProviderError]).} = | ||
without latestBlock =? await provider.getBlock(blockTag): | ||
raiseProviderError("Could not get latest block") | ||
|
||
without latestBlockNumber =? latestBlock.number: | ||
raiseProviderError("Could not get latest block number") | ||
|
||
return (latestBlockNumber, latestBlock.timestamp) | ||
|
||
proc binarySearchFindClosestBlock( | ||
provider: Provider, | ||
epochTime: int, | ||
low: UInt256, | ||
high: UInt256): Future[UInt256] {.async: (raises: [ProviderError]).} = | ||
let (_, lowTimestamp) = | ||
await provider.blockNumberAndTimestamp(BlockTag.init(low)) | ||
let (_, highTimestamp) = | ||
await provider.blockNumberAndTimestamp(BlockTag.init(high)) | ||
if abs(lowTimestamp.truncate(int) - epochTime) < | ||
abs(highTimestamp.truncate(int) - epochTime): | ||
return low | ||
else: | ||
return high | ||
|
||
proc binarySearchBlockNumberForEpoch( | ||
provider: Provider, | ||
epochTime: UInt256, | ||
latestBlockNumber: UInt256, | ||
earliestBlockNumber: UInt256): Future[UInt256] | ||
{.async: (raises: [ProviderError]).} = | ||
var low = earliestBlockNumber | ||
var high = latestBlockNumber | ||
|
||
while low <= high: | ||
if low == 0 and high == 0: | ||
return low | ||
let mid = (low + high) div 2 | ||
let (midBlockNumber, midBlockTimestamp) = | ||
await provider.blockNumberAndTimestamp(BlockTag.init(mid)) | ||
|
||
if midBlockTimestamp < epochTime: | ||
low = mid + 1 | ||
elif midBlockTimestamp > epochTime: | ||
high = mid - 1 | ||
else: | ||
return midBlockNumber | ||
# NOTICE that by how the binary search is implemented, when it finishes | ||
# low is always greater than high - this is why we use high, where | ||
# intuitively we would use low: | ||
await provider.binarySearchFindClosestBlock( | ||
epochTime.truncate(int), low=high, high=low) | ||
|
||
proc blockNumberForEpoch*( | ||
provider: Provider, | ||
epochTime: SecondsSince1970): Future[UInt256] | ||
{.async: (raises: [ProviderError]).} = | ||
let epochTimeUInt256 = epochTime.u256 | ||
let (latestBlockNumber, latestBlockTimestamp) = | ||
await provider.blockNumberAndTimestamp(BlockTag.latest) | ||
let (earliestBlockNumber, earliestBlockTimestamp) = | ||
await provider.blockNumberAndTimestamp(BlockTag.earliest) | ||
|
||
# Initially we used the average block time to predict | ||
# the number of blocks we need to look back in order to find | ||
# the block number corresponding to the given epoch time. | ||
# This estimation can be highly inaccurate if block time | ||
# was changing in the past or is fluctuating and therefore | ||
# we used that information initially only to find out | ||
# if the available history is long enough to perform effective search. | ||
# It turns out we do not have to do that. There is an easier way. | ||
# | ||
# First we check if the given epoch time equals the timestamp of either | ||
# the earliest or the latest block. If it does, we just return the | ||
# block number of that block. | ||
# | ||
# Otherwise, if the earliest available block is not the genesis block, | ||
# we should check the timestamp of that earliest block and if it is greater | ||
# than the epoch time, we should issue a warning and return | ||
# that earliest block number. | ||
# In all other cases, thus when the earliest block is not the genesis | ||
# block but its timestamp is not greater than the requested epoch time, or | ||
# if the earliest available block is the genesis block, | ||
# (which means we have the whole history available), we should proceed with | ||
# the binary search. | ||
# | ||
# Additional benefit of this method is that we do not have to rely | ||
# on the average block time, which not only makes the whole thing | ||
# more reliable, but also easier to test. | ||
|
||
# Are lucky today? | ||
if earliestBlockTimestamp == epochTimeUInt256: | ||
return earliestBlockNumber | ||
if latestBlockTimestamp == epochTimeUInt256: | ||
return latestBlockNumber | ||
|
||
if earliestBlockNumber > 0 and earliestBlockTimestamp > epochTimeUInt256: | ||
let availableHistoryInDays = | ||
(latestBlockTimestamp - earliestBlockTimestamp) div | ||
1.days.secs.u256 | ||
warn "Short block history detected.", earliestBlockTimestamp = | ||
earliestBlockTimestamp, days = availableHistoryInDays | ||
return earliestBlockNumber | ||
|
||
return await provider.binarySearchBlockNumberForEpoch( | ||
epochTimeUInt256, latestBlockNumber, earliestBlockNumber) | ||
|
||
proc pastBlockTag*(provider: Provider, | ||
blocksAgo: int): | ||
Future[BlockTag] {.async: (raises: [ProviderError]).} = | ||
let head = await provider.getBlockNumber() | ||
return BlockTag.init(head - blocksAgo.abs.u256) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
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.
Might be beneficial to preserve the original exception otherwise the underlying reason for the failure will be lost:
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.
Used like:
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.
Also, callers can pull out the parent exception msg using
msdDetail
like this: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.
We cannot use:
because (as you yourself pointed out earlier)
getBlock
returns anOption
and not aResult
. Right?So if there was an original
ProviderError
caused bygetBlock
it will naturally bubble-up so, we do not have to do anything about it I suppose. But thengetBlock
, which is defined as this:can still return
None
becauseeth_getBlockByNumber
is defined like this:This is why I am raising
ProviderError
when this happen, but we have no data about underlying exception that causedeth_getBlockByNumber
to returnNone
.So I think in this case everything can stays as it is, right?
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.
Good point, sorry I missed that
getBlock
returns anOption
👍I don't think it's an error really, just that the ethereum client can return null if the block doesn't exist.
What you have looks good 👍