Skip to content

Commit 225ef5e

Browse files
authored
partially revert #5765 (#5833)
1 parent 5c54eb5 commit 225ef5e

File tree

7 files changed

+310
-187
lines changed

7 files changed

+310
-187
lines changed

beacon_chain/gossip_processing/block_processor.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type
6161
blobs*: Opt[BlobSidecars]
6262
maybeFinalized*: bool
6363
## The block source claims the block has been finalized already
64-
resfut*: Future[Result[void, VerifierError]].Raising([CancelledError])
64+
resfut*: Future[Result[void, VerifierError]]
6565
queueTick*: Moment # Moment when block was enqueued
6666
validationDur*: Duration # Time it took to perform gossip validation
6767
src*: MsgSource
@@ -385,7 +385,7 @@ proc checkBloblessSignature(self: BlockProcessor,
385385
proc enqueueBlock*(
386386
self: var BlockProcessor, src: MsgSource, blck: ForkedSignedBeaconBlock,
387387
blobs: Opt[BlobSidecars],
388-
resfut: Future[Result[void, VerifierError]].Raising([CancelledError]) = nil,
388+
resfut: Future[Result[void, VerifierError]] = nil,
389389
maybeFinalized = false,
390390
validationDur = Duration()) =
391391
withBlck(blck):
@@ -756,7 +756,7 @@ proc storeBlock(
756756
proc addBlock*(
757757
self: var BlockProcessor, src: MsgSource, blck: ForkedSignedBeaconBlock,
758758
blobs: Opt[BlobSidecars], maybeFinalized = false,
759-
validationDur = Duration()): Future[Result[void, VerifierError]] {.async: (raises: [CancelledError], raw: true).} =
759+
validationDur = Duration()): Future[Result[void, VerifierError]] =
760760
## Enqueue a Gossip-validated block for consensus verification
761761
# Backpressure:
762762
# There is no backpressure here - producers must wait for `resfut` to

beacon_chain/networking/peer_pool.nim

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,12 @@ template outgoingEvent(eventType: EventType): AsyncEvent =
108108
pool.outNotFullEvent
109109
110110
proc waitForEvent[A, B](pool: PeerPool[A, B], eventType: EventType,
111-
filter: set[PeerType]) {.async: (raises: [CancelledError]).} =
111+
filter: set[PeerType]) {.async.} =
112112
if filter == {PeerType.Incoming, PeerType.Outgoing} or filter == {}:
113113
var fut1 = incomingEvent(eventType).wait()
114114
var fut2 = outgoingEvent(eventType).wait()
115115
try:
116-
try:
117-
discard await one(fut1, fut2)
118-
except ValueError:
119-
raiseAssert "one precondition satisfied"
116+
discard await one(fut1, fut2)
120117
if fut1.finished():
121118
if not(fut2.finished()):
122119
await fut2.cancelAndWait()
@@ -141,11 +138,11 @@ proc waitForEvent[A, B](pool: PeerPool[A, B], eventType: EventType,
141138
outgoingEvent(eventType).clear()
142139
143140
proc waitNotEmptyEvent[A, B](pool: PeerPool[A, B],
144-
filter: set[PeerType]) {.async: (raises: [CancelledError], raw: true).} =
141+
filter: set[PeerType]): Future[void] =
145142
pool.waitForEvent(EventType.NotEmptyEvent, filter)
146143
147144
proc waitNotFullEvent[A, B](pool: PeerPool[A, B],
148-
filter: set[PeerType]){.async: (raises: [CancelledError], raw: true).} =
145+
filter: set[PeerType]): Future[void] =
149146
pool.waitForEvent(EventType.NotFullEvent, filter)
150147
151148
proc newPeerPool*[A, B](maxPeers = -1, maxIncomingPeers = -1,
@@ -454,15 +451,15 @@ proc getPeerSpaceMask[A, B](pool: PeerPool[A, B],
454451
{PeerType.Outgoing}
455452
456453
proc waitForEmptySpace*[A, B](pool: PeerPool[A, B],
457-
peerType: PeerType) {.async: (raises: [CancelledError]).} =
454+
peerType: PeerType) {.async.} =
458455
## This procedure will block until ``pool`` will have an empty space for peer
459456
## of type ``peerType``.
460457
let mask = pool.getPeerSpaceMask(peerType)
461458
while pool.lenSpace({peerType}) == 0:
462459
await pool.waitNotFullEvent(mask)
463460
464461
proc addPeer*[A, B](pool: PeerPool[A, B],
465-
peer: A, peerType: PeerType): Future[PeerStatus] {.async: (raises: [CancelledError]).} =
462+
peer: A, peerType: PeerType): Future[PeerStatus] {.async.} =
466463
## Add peer ``peer`` of type ``peerType`` to PeerPool ``pool``.
467464
##
468465
## This procedure will wait for an empty space in PeerPool ``pool``, if
@@ -536,7 +533,7 @@ proc acquireItemImpl[A, B](pool: PeerPool[A, B],
536533

537534
proc acquire*[A, B](pool: PeerPool[A, B],
538535
filter = {PeerType.Incoming,
539-
PeerType.Outgoing}): Future[A] {.async: (raises: [CancelledError]).} =
536+
PeerType.Outgoing}): Future[A] {.async.} =
540537
## Acquire peer from PeerPool ``pool``, which match the filter ``filter``.
541538
mixin getKey
542539
doAssert(filter != {}, "Filter must not be empty")
@@ -589,7 +586,7 @@ proc release*[A, B](pool: PeerPool[A, B], peers: openArray[A]) {.inline.} =
589586
proc acquire*[A, B](pool: PeerPool[A, B],
590587
number: int,
591588
filter = {PeerType.Incoming,
592-
PeerType.Outgoing}): Future[seq[A]] {.async: (raises: [CancelledError]).} =
589+
PeerType.Outgoing}): Future[seq[A]] {.async.} =
593590
## Acquire ``number`` number of peers from PeerPool ``pool``, which match the
594591
## filter ``filter``.
595592
doAssert(filter != {}, "Filter must not be empty")
@@ -738,7 +735,7 @@ proc clear*[A, B](pool: PeerPool[A, B]) =
738735
pool.acqIncPeersCount = 0
739736
pool.acqOutPeersCount = 0
740737
741-
proc clearSafe*[A, B](pool: PeerPool[A, B]) {.async: (raises: [CancelledError]).} =
738+
proc clearSafe*[A, B](pool: PeerPool[A, B]) {.async.} =
742739
## Performs "safe" clear. Safe means that it first acquires all the peers
743740
## in PeerPool, and only after that it will reset storage.
744741
var acquired = newSeq[A]()

beacon_chain/nimbus_beacon_node.nim

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ proc initFullNode(
351351
blobQuarantine, getBeaconTime)
352352
blockVerifier = proc(signedBlock: ForkedSignedBeaconBlock,
353353
blobs: Opt[BlobSidecars], maybeFinalized: bool):
354-
Future[Result[void, VerifierError]] {.async: (raises: [CancelledError], raw: true).} =
354+
Future[Result[void, VerifierError]] =
355355
# The design with a callback for block verification is unusual compared
356356
# to the rest of the application, but fits with the general approach
357357
# taken in the sync/request managers - this is an architectural compromise
@@ -360,23 +360,27 @@ proc initFullNode(
360360
MsgSource.gossip, signedBlock, blobs, maybeFinalized = maybeFinalized)
361361
rmanBlockVerifier = proc(signedBlock: ForkedSignedBeaconBlock,
362362
maybeFinalized: bool):
363-
Future[Result[void, VerifierError]] {.async: (raises: [CancelledError]).} =
363+
Future[Result[void, VerifierError]] =
364364
withBlck(signedBlock):
365-
when consensusFork >= ConsensusFork.Deneb:
365+
when typeof(forkyBlck).kind >= ConsensusFork.Deneb:
366366
if not blobQuarantine[].hasBlobs(forkyBlck):
367367
# We don't have all the blobs for this block, so we have
368368
# to put it in blobless quarantine.
369369
if not quarantine[].addBlobless(dag.finalizedHead.slot, forkyBlck):
370-
err(VerifierError.UnviableFork)
370+
Future.completed(
371+
Result[void, VerifierError].err(VerifierError.UnviableFork),
372+
"rmanBlockVerifier")
371373
else:
372-
err(VerifierError.MissingParent)
374+
Future.completed(
375+
Result[void, VerifierError].err(VerifierError.MissingParent),
376+
"rmanBlockVerifier")
373377
else:
374378
let blobs = blobQuarantine[].popBlobs(forkyBlck.root, forkyBlck)
375-
await blockProcessor[].addBlock(MsgSource.gossip, signedBlock,
379+
blockProcessor[].addBlock(MsgSource.gossip, signedBlock,
376380
Opt.some(blobs),
377381
maybeFinalized = maybeFinalized)
378382
else:
379-
await blockProcessor[].addBlock(MsgSource.gossip, signedBlock,
383+
blockProcessor[].addBlock(MsgSource.gossip, signedBlock,
380384
Opt.none(BlobSidecars),
381385
maybeFinalized = maybeFinalized)
382386

beacon_chain/sync/request_manager.nim

Lines changed: 74 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const
3939
type
4040
BlockVerifierFn* =
4141
proc(signedBlock: ForkedSignedBeaconBlock, maybeFinalized: bool):
42-
Future[Result[void, VerifierError]] {.async: (raises: [CancelledError]).}
42+
Future[Result[void, VerifierError]] {.gcsafe, raises: [].}
4343
InhibitFn* = proc: bool {.gcsafe, raises:[].}
4444

4545
RequestManager* = object
@@ -49,8 +49,8 @@ type
4949
quarantine: ref Quarantine
5050
blobQuarantine: ref BlobQuarantine
5151
blockVerifier: BlockVerifierFn
52-
blockLoopFuture: Future[void].Raising([CancelledError])
53-
blobLoopFuture: Future[void].Raising([CancelledError])
52+
blockLoopFuture: Future[void]
53+
blobLoopFuture: Future[void]
5454

5555
func shortLog*(x: seq[Eth2Digest]): string =
5656
"[" & x.mapIt(shortLog(it)).join(", ") & "]"
@@ -104,7 +104,7 @@ proc checkResponse(idList: seq[BlobIdentifier],
104104
return false
105105
true
106106

107-
proc requestBlocksByRoot(rman: RequestManager, items: seq[Eth2Digest]) {.async: (raises: [CancelledError]).} =
107+
proc requestBlocksByRoot(rman: RequestManager, items: seq[Eth2Digest]) {.async.} =
108108
var peer: Peer
109109
try:
110110
peer = await rman.network.peerPool.acquire()
@@ -171,21 +171,27 @@ proc requestBlocksByRoot(rman: RequestManager, items: seq[Eth2Digest]) {.async:
171171
peer = peer, blocks = shortLog(items), err = blocks.error()
172172
peer.updateScore(PeerScoreNoValues)
173173

174+
except CancelledError as exc:
175+
raise exc
176+
except CatchableError as exc:
177+
peer.updateScore(PeerScoreNoValues)
178+
debug "Error while fetching blocks by root", exc = exc.msg,
179+
items = shortLog(items), peer = peer, peer_score = peer.getScore()
180+
raise exc
174181
finally:
175182
if not(isNil(peer)):
176183
rman.network.peerPool.release(peer)
177184

178185
proc fetchBlobsFromNetwork(self: RequestManager,
179-
idList: seq[BlobIdentifier])
180-
{.async: (raises: [CancelledError]).} =
186+
idList: seq[BlobIdentifier]) {.async.} =
181187
var peer: Peer
182188

183189
try:
184190
peer = await self.network.peerPool.acquire()
185191
debug "Requesting blobs by root", peer = peer, blobs = shortLog(idList),
186192
peer_score = peer.getScore()
187193

188-
let blobs = await blobSidecarsByRoot(peer, BlobIdentifierList idList)
194+
let blobs = (await blobSidecarsByRoot(peer, BlobIdentifierList idList))
189195

190196
if blobs.isOk:
191197
let ublobs = blobs.get()
@@ -213,11 +219,18 @@ proc fetchBlobsFromNetwork(self: RequestManager,
213219
peer = peer, blobs = shortLog(idList), err = blobs.error()
214220
peer.updateScore(PeerScoreNoValues)
215221

222+
except CancelledError as exc:
223+
raise exc
224+
except CatchableError as exc:
225+
peer.updateScore(PeerScoreNoValues)
226+
debug "Error while fetching blobs by root", exc = exc.msg,
227+
idList = shortLog(idList), peer = peer, peer_score = peer.getScore()
228+
raise exc
216229
finally:
217230
if not(isNil(peer)):
218231
self.network.peerPool.release(peer)
219232

220-
proc requestManagerBlockLoop(rman: RequestManager) {.async: (raises: [CancelledError]).} =
233+
proc requestManagerBlockLoop(rman: RequestManager) {.async.} =
221234
while true:
222235
# TODO This polling could be replaced with an AsyncEvent that is fired
223236
# from the quarantine when there's work to do
@@ -232,19 +245,33 @@ proc requestManagerBlockLoop(rman: RequestManager) {.async: (raises: [CancelledE
232245
continue
233246

234247
debug "Requesting detected missing blocks", blocks = shortLog(blocks)
235-
let start = SyncMoment.now(0)
248+
try:
249+
let start = SyncMoment.now(0)
236250

237-
var workers: array[PARALLEL_REQUESTS, Future[void].Raising([CancelledError])]
251+
var workers: array[PARALLEL_REQUESTS, Future[void]]
238252

239-
for i in 0 ..< PARALLEL_REQUESTS:
240-
workers[i] = rman.requestBlocksByRoot(blocks)
253+
for i in 0 ..< PARALLEL_REQUESTS:
254+
workers[i] = rman.requestBlocksByRoot(blocks)
241255

242-
await allFutures(workers)
256+
await allFutures(workers)
243257

244-
let finish = SyncMoment.now(uint64(len(blocks)))
258+
let finish = SyncMoment.now(uint64(len(blocks)))
259+
260+
var succeed = 0
261+
for worker in workers:
262+
if worker.completed():
263+
inc(succeed)
264+
265+
debug "Request manager block tick", blocks = shortLog(blocks),
266+
succeed = succeed,
267+
failed = (len(workers) - succeed),
268+
sync_speed = speed(start, finish)
269+
270+
except CancelledError:
271+
break
272+
except CatchableError as exc:
273+
warn "Unexpected error in request manager block loop", exc = exc.msg
245274

246-
debug "Request manager block tick", blocks = shortLog(blocks),
247-
sync_speed = speed(start, finish)
248275

249276
proc getMissingBlobs(rman: RequestManager): seq[BlobIdentifier] =
250277
let
@@ -281,28 +308,42 @@ proc getMissingBlobs(rman: RequestManager): seq[BlobIdentifier] =
281308
rman.quarantine[].removeBlobless(blobless)
282309
fetches
283310

284-
proc requestManagerBlobLoop(rman: RequestManager) {.async: (raises: [CancelledError]).} =
311+
312+
proc requestManagerBlobLoop(rman: RequestManager) {.async.} =
285313
while true:
286-
# TODO This polling could be replaced with an AsyncEvent that is fired
287-
# from the quarantine when there's work to do
314+
# TODO This polling could be replaced with an AsyncEvent that is fired
315+
# from the quarantine when there's work to do
288316
await sleepAsync(POLL_INTERVAL)
289317
if rman.inhibit():
290318
continue
291319

292-
let fetches = rman.getMissingBlobs()
293-
if fetches.len > 0:
294-
debug "Requesting detected missing blobs", blobs = shortLog(fetches)
295-
let start = SyncMoment.now(0)
296-
var workers: array[PARALLEL_REQUESTS, Future[void].Raising([CancelledError])]
297-
for i in 0 ..< PARALLEL_REQUESTS:
298-
workers[i] = rman.fetchBlobsFromNetwork(fetches)
299-
300-
await allFutures(workers)
301-
let finish = SyncMoment.now(uint64(len(fetches)))
302-
303-
debug "Request manager blob tick",
304-
blobs_count = len(fetches),
305-
sync_speed = speed(start, finish)
320+
let fetches = rman.getMissingBlobs()
321+
if fetches.len > 0:
322+
debug "Requesting detected missing blobs", blobs = shortLog(fetches)
323+
try:
324+
let start = SyncMoment.now(0)
325+
var workers: array[PARALLEL_REQUESTS, Future[void]]
326+
for i in 0 ..< PARALLEL_REQUESTS:
327+
workers[i] = rman.fetchBlobsFromNetwork(fetches)
328+
329+
await allFutures(workers)
330+
let finish = SyncMoment.now(uint64(len(fetches)))
331+
332+
var succeed = 0
333+
for worker in workers:
334+
if worker.finished() and not(worker.failed()):
335+
inc(succeed)
336+
337+
debug "Request manager blob tick",
338+
blobs_count = len(fetches),
339+
succeed = succeed,
340+
failed = (len(workers) - succeed),
341+
sync_speed = speed(start, finish)
342+
343+
except CancelledError:
344+
break
345+
except CatchableError as exc:
346+
warn "Unexpected error in request manager blob loop", exc = exc.msg
306347

307348
proc start*(rman: var RequestManager) =
308349
## Start Request Manager's loops.

0 commit comments

Comments
 (0)