-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathblocks_import.nim
More file actions
68 lines (57 loc) · 2.42 KB
/
Copy pathblocks_import.nim
File metadata and controls
68 lines (57 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Nimbus
# Copyright (c) 2023-2026 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at
# https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at
# https://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed
# except according to those terms.
{.push raises:[].}
import
pkg/[chronicles, chronos, results],
pkg/eth/common,
../../../wire_protocol,
../worker_desc
logScope:
topics = "beacon sync"
# ------------------------------------------------------------------------------
# Public handler
# ------------------------------------------------------------------------------
proc importBlockCB*(
buddy: BeaconPeerRef;
blk: EthBlock;
effPeerID: Hash;
): Future[Result[Duration,BeaconError]]
{.async: (raises: []).} =
## Wrapper around blocks importer
let
start = Moment.now()
ctx = buddy.ctx
peer {.inject,used.} = $buddy.peer # logging only
if blk.header.number <= ctx.chain.baseNumber:
trace "Ignoring block less eq. base", peer, blk=blk.header.number,
B=ctx.chain.baseNumber, L=ctx.chain.latestNumber
else:
try:
# TODO: The block access list needs to be passed in when available over devp2p
# and when the block falls within the BAL retention period.
(await ctx.chain.queueImportBlock(blk, Opt.none(BlockAccessListRef))).isOkOr:
return err((ENoException, "", error, Moment.now() - start))
except CancelledError as e:
return err((ECancelledError,$e.name,e.msg,Moment.now()-start))
# Allow thread switch by issuing a short wait request. A minimum time
# distance to the last task switch sleep request is maintained (see
# `asyncThreadSwitchGap`.)
if ctx.pool.nextAsyncNanoSleep < Moment.now():
try:
await sleepAsync asyncThreadSwitchTimeSlot
except CancelledError as e:
return err((ECancelledError,$e.name,e.msg,Moment.now()-start))
if not ctx.daemon: # Daemon will be up unless shutdown
return err((ESyncerTermination,"","",Moment.now()-start))
ctx.pool.nextAsyncNanoSleep = Moment.now() + asyncThreadSwitchGap
return ok(Moment.now()-start)
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------