-
Notifications
You must be signed in to change notification settings - Fork 314
Wire up blob fetching with request manager #4527
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2a27446
Wire up blob fetching and request manager
henridf ec5c9b2
Update beacon_chain/sync/request_manager.nim
henridf 2c4b94f
Review feedback: remove redundant RequestManager field.
henridf 856fcd9
Fix syntax
henridf 26b8cc9
Improve blob fetching logic
henridf d624a35
Merge branch 'unstable' into eip4844-sync
henridf 827020a
fix previous commit
henridf d78ec80
rman.isBlobsTime(): Remove spurious parameter
henridf 2bbf147
Review feedback
henridf 8a17601
Merge branch 'unstable' into eip4844-sync
henridf ccfc99d
expressionify an if
henridf 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ import | |
| ../networking/eth2_network, | ||
| ../consensus_object_pools/block_quarantine, | ||
| "."/sync_protocol, "."/sync_manager | ||
| from ../beacon_clock import GetBeaconTimeFn | ||
| export block_quarantine, sync_manager | ||
|
|
||
| logScope: | ||
|
|
@@ -31,11 +32,17 @@ type | |
| BlockVerifier* = | ||
| proc(signedBlock: ForkedSignedBeaconBlock): | ||
| Future[Result[void, VerifierError]] {.gcsafe, raises: [Defect].} | ||
| BlockBlobsVerifier* = | ||
| proc(signedBlock: ForkedSignedBeaconBlock, blobs: eip4844.BlobsSidecar): | ||
| Future[Result[void, VerifierError]] {.gcsafe, raises: [Defect].} | ||
|
|
||
| RequestManager* = object | ||
| network*: Eth2Node | ||
| inpQueue*: AsyncQueue[FetchRecord] | ||
| cfg: RuntimeConfig | ||
| getBeaconTime: GetBeaconTimeFn | ||
| blockVerifier: BlockVerifier | ||
| blockBlobsVerifier: BlockBlobsVerifier | ||
| loopFuture: Future[void] | ||
|
|
||
| func shortLog*(x: seq[Eth2Digest]): string = | ||
|
|
@@ -45,13 +52,33 @@ func shortLog*(x: seq[FetchRecord]): string = | |
| "[" & x.mapIt(shortLog(it.root)).join(", ") & "]" | ||
|
|
||
| proc init*(T: type RequestManager, network: Eth2Node, | ||
| blockVerifier: BlockVerifier): RequestManager = | ||
| cfg: RuntimeConfig, | ||
| getBeaconTime: GetBeaconTimeFn, | ||
| blockVerifier: BlockVerifier, | ||
| blockBlobsVerifier: BlockBlobsVerifier): RequestManager = | ||
| RequestManager( | ||
| network: network, | ||
| inpQueue: newAsyncQueue[FetchRecord](), | ||
| blockVerifier: blockVerifier | ||
| cfg: cfg, | ||
| getBeaconTime: getBeaconTime, | ||
| blockVerifier: blockVerifier, | ||
| blockBlobsVerifier: blockBlobsVerifier, | ||
| ) | ||
|
|
||
| proc checkResponse(roots: openArray[Eth2Digest], | ||
| blocks: openArray[ref SignedBeaconBlockAndBlobsSidecar]): bool = | ||
| ## This procedure checks peer's response. | ||
| var checks = @roots | ||
| if len(blocks) > len(roots): | ||
| return false | ||
| for item in blocks: | ||
|
henridf marked this conversation as resolved.
|
||
| let res = checks.find(item[].beacon_block.root) | ||
| if res == -1: | ||
| return false | ||
| else: | ||
| checks.del(res) | ||
| true | ||
|
|
||
| proc checkResponse(roots: openArray[Eth2Digest], | ||
| blocks: openArray[ref ForkedSignedBeaconBlock]): bool = | ||
| ## This procedure checks peer's response. | ||
|
|
@@ -138,6 +165,92 @@ proc fetchAncestorBlocksFromNetwork(rman: RequestManager, | |
| if not(isNil(peer)): | ||
| rman.network.peerPool.release(peer) | ||
|
|
||
| proc fetchAncestorBlocksAndBlobsFromNetwork(rman: RequestManager, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (This has a lot of duplication with |
||
| items: seq[Eth2Digest]) {.async.} = | ||
| var peer: Peer | ||
| try: | ||
| peer = await rman.network.peerPool.acquire() | ||
| debug "Requesting blocks by root", peer = peer, blocks = shortLog(items), | ||
| peer_score = peer.getScore() | ||
|
|
||
| let blocks = (await beaconBlockAndBlobsSidecarByRoot_v1(peer, BlockRootsList items)) | ||
|
|
||
| if blocks.isOk: | ||
| let ublocks = blocks.get() | ||
| if checkResponse(items, ublocks.asSeq()): | ||
| var | ||
| gotGoodBlock = false | ||
| gotUnviableBlock = false | ||
|
|
||
| for b in ublocks: | ||
| let ver = await rman.blockBlobsVerifier(ForkedSignedBeaconBlock.init(b[].beacon_block), b[].blobs_sidecar) | ||
| if ver.isErr(): | ||
| case ver.error() | ||
| of VerifierError.MissingParent: | ||
| # Ignoring because the order of the blocks that | ||
| # we requested may be different from the order in which we need | ||
| # these blocks to apply. | ||
| discard | ||
| of VerifierError.Duplicate: | ||
| # Ignoring because these errors could occur due to the | ||
| # concurrent/parallel requests we made. | ||
| discard | ||
| of VerifierError.UnviableFork: | ||
| # If they're working a different fork, we'll want to descore them | ||
| # but also process the other blocks (in case we can register the | ||
| # other blocks as unviable) | ||
| gotUnviableBlock = true | ||
| of VerifierError.Invalid: | ||
| # We stop processing blocks because peer is either sending us | ||
| # junk or working a different fork | ||
| notice "Received invalid block", | ||
| peer = peer, blocks = shortLog(items), | ||
| peer_score = peer.getScore() | ||
| peer.updateScore(PeerScoreBadValues) | ||
|
|
||
| return # Stop processing this junk... | ||
| else: | ||
| gotGoodBlock = true | ||
|
|
||
| if gotUnviableBlock: | ||
| notice "Received blocks from an unviable fork", | ||
| peer = peer, blocks = shortLog(items), | ||
| peer_score = peer.getScore() | ||
| peer.updateScore(PeerScoreUnviableFork) | ||
| elif gotGoodBlock: | ||
| # We reward peer only if it returns something. | ||
| peer.updateScore(PeerScoreGoodValues) | ||
| else: | ||
| let err = blocks.error() | ||
| case err.kind | ||
| of ReceivedErrorResponse: | ||
| if err.responseCode == ResourceUnavailable: | ||
| if not(isNil(peer)): | ||
| rman.network.peerPool.release(peer) | ||
| await rman.fetchAncestorBlocksFromNetwork(items) | ||
| return | ||
| else: | ||
| peer.updateScore(PeerScoreBadResponse) | ||
| else: | ||
| discard | ||
| else: | ||
| peer.updateScore(PeerScoreNoValues) | ||
|
|
||
| except CancelledError as exc: | ||
| raise exc | ||
| except CatchableError as exc: | ||
| peer.updateScore(PeerScoreNoValues) | ||
| debug "Error while fetching ancestor blocks", exc = exc.msg, | ||
| items = shortLog(items), peer = peer, peer_score = peer.getScore() | ||
| raise exc | ||
| finally: | ||
| if not(isNil(peer)): | ||
| rman.network.peerPool.release(peer) | ||
|
|
||
|
|
||
| proc isBlobsTime(rman: RequestManager): bool = | ||
| rman.getBeaconTime().slotOrZero.epoch >= rman.cfg.EIP4844_FORK_EPOCH | ||
|
|
||
| proc requestManagerLoop(rman: RequestManager) {.async.} = | ||
| var rootList = newSeq[Eth2Digest]() | ||
| var workers = newSeq[Future[void]](PARALLEL_REQUESTS) | ||
|
|
@@ -154,8 +267,17 @@ proc requestManagerLoop(rman: RequestManager) {.async.} = | |
|
|
||
| let start = SyncMoment.now(0) | ||
|
|
||
| # As soon as EIP4844_FORK_EPOCH comes around in wall time, we | ||
| # switch to requesting blocks and blobs. In the vicinity of the | ||
| # transition, that means that we *may* request blobs for a | ||
| # pre-eip4844. In that case, we get ResourceUnavailable from the | ||
| # peer and fall back to requesting blocks only. | ||
| let getBlobs = rman.isBlobsTime() | ||
| for i in 0 ..< PARALLEL_REQUESTS: | ||
| workers[i] = rman.fetchAncestorBlocksFromNetwork(rootList) | ||
| workers[i] = if getBlobs: | ||
| rman.fetchAncestorBlocksAndBlobsFromNetwork(rootList) | ||
| else: | ||
| rman.fetchAncestorBlocksFromNetwork(rootList) | ||
|
|
||
| # We do not care about | ||
| await allFutures(workers) | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.