Skip to content

Commit 1403470

Browse files
authored
Beacon sync maint update (#3608)
* Command line option to set the min number of peers for first activation why This option is used to force the syncer to begin not before enough active peers are available. This allows straight forward multi-peer testing to observe the particular behaviour (e.g. logging floods, or the opposite.) * Move `workers_*.nim` up one folder -> `workers/workers_*.nim` why The place parallel to the folder `worker` was a legacy outlier. Typically these files would live inside the folder for other sub-modules. * Extract `BeaconSyncRef` object from `beacon.nim` into `beacon_desc.nim` why Allows to import object definition w/o importing `beacon.nim` methods * Add logging for missing peers while syncer is activated why This patch causes sporadic logs about missing sync peers. So to keep one informed that the syncer is still alive. * Make sure that info label status messages do appear semi-regularly why This is an attempt to provide useful sync state update without spamming.
1 parent 37895c3 commit 1403470

26 files changed

+197
-58
lines changed

execution_chain/config.nim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,13 @@ type
493493
defaultValueDesc: "\"jwt.hex\" in the data directory (see --data-dir)"
494494
name: "jwt-secret" .}: Option[InputFile]
495495

496+
beaconSyncInitPeersMin* {.
497+
hidden
498+
defaultValue: 0
499+
desc: "Minimal number of peers needed for activating the first" &
500+
" syncer session"
501+
name: "debug-beacon-sync-init-peers-min" .}: int
502+
496503
beaconSyncTarget* {.
497504
hidden
498505
desc: "Manually set the initial sync target specified by its 32 byte" &

execution_chain/nimbus_execution_client.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ proc setupP2P(nimbus: NimbusNode, conf: NimbusConf,
118118
nimbus.beaconSyncRef = BeaconSyncRef.init(
119119
nimbus.ethNode, nimbus.fc, conf.maxPeers)
120120

121+
# Min peers (if set)
122+
if 0 < conf.beaconSyncInitPeersMin:
123+
nimbus.beaconSyncRef.peersMinInit conf.beaconSyncInitPeersMin
124+
121125
# Optional for pre-setting the sync target (i.e. debugging)
122126
if conf.beaconSyncTarget.isSome():
123127
let hex = conf.beaconSyncTarget.unsafeGet

execution_chain/sync/beacon.nim

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@ import
1616
../core/chain,
1717
../networking/p2p,
1818
./beacon/worker/headers/headers_target,
19-
./beacon/[worker, worker_desc],
20-
./[sync_desc, sync_sched, wire_protocol]
19+
./beacon/[beacon_desc, worker],
20+
./[sync_sched, wire_protocol]
2121

22+
export
23+
beacon_desc
2224

2325
logScope:
2426
topics = "beacon sync"
2527

26-
type
27-
BeaconSyncRef* = RunnerSyncRef[BeaconCtxData,BeaconBuddyData]
28-
2928
# ------------------------------------------------------------------------------
3029
# Virtual methods/interface, `mixin` functions
3130
# ------------------------------------------------------------------------------
@@ -69,6 +68,11 @@ proc init*(
6968
desc.ctx.pool.chain = chain
7069
desc
7170

71+
proc peersMinInit*(desc: BeaconSyncRef; nBuddies: int) =
72+
## Set the minimum number of active peers to start with for the first
73+
## syncer activation.
74+
desc.ctx.pool.minInitBuddies = nBuddies
75+
7276
proc targetInit*(desc: BeaconSyncRef; hex: string; isFinal: bool): bool =
7377
## Set up inital target sprint (if any, mainly for debugging)
7478
try:
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Nimbus
2+
# Copyright (c) 2023-2025 Status Research & Development GmbH
3+
# Licensed under either of
4+
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
5+
# http://www.apache.org/licenses/LICENSE-2.0)
6+
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
7+
# http://opensource.org/licenses/MIT)
8+
# at your option. This file may not be copied, modified, or distributed
9+
# except according to those terms.
10+
11+
{.push raises: [].}
12+
13+
import
14+
./worker/worker_desc,
15+
../[sync_desc, sync_sched]
16+
17+
export
18+
sync_desc, worker_desc
19+
20+
type
21+
BeaconSyncRef* = RunnerSyncRef[BeaconCtxData,BeaconBuddyData]
22+
23+
# End

execution_chain/sync/beacon/worker.nim

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import
1515
pkg/eth/common,
1616
pkg/stew/[interval_set, sorted_set],
1717
../../common,
18+
../../networking/p2p,
1819
./worker/headers/headers_target,
1920
./worker/update/[metrics, ticker],
20-
./worker/[blocks, headers, start_stop, update],
21-
./worker_desc
21+
./worker/[blocks, headers, start_stop, update, worker_desc]
2222

2323
# ------------------------------------------------------------------------------
2424
# Private functions
@@ -77,9 +77,19 @@ proc stop*(buddy: BeaconBuddyRef; info: static[string]) =
7777
proc runTicker*(ctx: BeaconCtxRef; info: static[string]) =
7878
## Global background job that is started every few seconds. It is to be
7979
## intended for updating metrics, debug logging etc.
80+
##
8081
ctx.updateMetrics()
8182
ctx.updateTicker()
8283

84+
# Inform if there are no peers active while syncing
85+
if not ctx.hibernate and ctx.pool.nBuddies < 1:
86+
let now = Moment.now()
87+
if ctx.pool.lastNoPeersLog + noPeersLogWaitInterval < now:
88+
ctx.pool.lastNoPeersLog = now
89+
debug info & ": no sync peers yet",
90+
elapsed=(now - ctx.pool.lastPeerSeen).toStr,
91+
nOtherPeers=ctx.node.peerPool.connectedNodes.len
92+
8393

8494
template runDaemon*(ctx: BeaconCtxRef; info: static[string]): Duration =
8595
## Async/template

execution_chain/sync/beacon/worker/blocks.nim

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import
1515
pkg/eth/common,
1616
pkg/stew/[interval_set, sorted_set],
1717
../../../networking/p2p,
18-
../worker_desc,
19-
./blocks/[blocks_blocks, blocks_helpers, blocks_queue, blocks_unproc]
18+
./blocks/[blocks_blocks, blocks_helpers, blocks_queue, blocks_unproc],
19+
./worker_desc
2020

2121
export
2222
blocks_queue, blocks_unproc
@@ -64,6 +64,7 @@ template blocksCollect*(
6464
break body # no action
6565

6666
var
67+
importedOK = false # imported some blocks
6768
nImported {.inject.} = 0u64 # statistics, to be updated
6869
nQueued {.inject.} = 0 # ditto
6970

@@ -113,6 +114,21 @@ template blocksCollect*(
113114
# Import blocks (no staging), async/template
114115
nImported += buddy.blocksImport(blocks, buddy.peerID, info)
115116

117+
# Sync status logging
118+
if 0 < nImported:
119+
importedOK = true
120+
if ctx.pool.lastSyncUpdLog + syncUpdateLogWaitInterval < Moment.now():
121+
chronicles.info "Imported blocks", nImported,
122+
nUnproc=ctx.nUnprocStr(),
123+
nStagedQ=ctx.blk.staged.len,
124+
base=ctx.chain.baseNumber.bnStr,
125+
head=ctx.chain.latestNumber.bnStr,
126+
target=ctx.subState.head.bnStr,
127+
targetHash=ctx.subState.headHash.short,
128+
nSyncPeers=ctx.pool.nBuddies
129+
ctx.pool.lastSyncUpdLog = Moment.now()
130+
nImported = 0
131+
116132
# Import may be incomplete, so a partial roll back may be needed
117133
let lastBn = blocks[^1].header.number
118134
if ctx.subState.top < lastBn:
@@ -151,12 +167,19 @@ template blocksCollect*(
151167

152168
# End block: `fetchBlocksBody`
153169

154-
if 0 < nImported:
155-
chronicles.info "Imported blocks", nImported, nUnproc=ctx.nUnprocStr(),
156-
nStagedQ=ctx.blk.staged.len,
157-
base=ctx.chain.baseNumber.bnStr, head=ctx.chain.latestNumber.bnStr,
158-
target=ctx.subState.head.bnStr, targetHash=ctx.subState.headHash.short,
159-
nSyncPeers=ctx.pool.nBuddies
170+
if importedOK:
171+
# Sync status logging.
172+
if 0 < nImported:
173+
# Note that `nImported` might have been reset above.
174+
chronicles.info "Imported blocks", nImported,
175+
nUnproc=ctx.nUnprocStr(),
176+
nStagedQ=ctx.blk.staged.len,
177+
base=ctx.chain.baseNumber.bnStr,
178+
head=ctx.chain.latestNumber.bnStr,
179+
target=ctx.subState.head.bnStr,
180+
targetHash=ctx.subState.headHash.short,
181+
nSyncPeers=ctx.pool.nBuddies
182+
ctx.pool.lastSyncUpdLog = Moment.now()
160183

161184
elif nQueued == 0 and
162185
not ctx.pool.seenData and
@@ -210,6 +233,7 @@ template blocksUnstage*(
210233
var
211234
peer {.inject.} = buddy.peer
212235
nImported {.inject.} = 0u64 # statistics
236+
importedOK = false # imported some blocks
213237
switchPeer {.inject.} = false # for return code
214238

215239
while ctx.pool.lastState == SyncState.blocks:
@@ -236,19 +260,41 @@ template blocksUnstage*(
236260
# Import blocks list, async/template
237261
nImported += buddy.blocksImport(qItem.data.blocks,qItem.data.peerID, info)
238262

263+
# Sync status logging
264+
if 0 < nImported:
265+
importedOK = true
266+
if ctx.pool.lastSyncUpdLog + syncUpdateLogWaitInterval < Moment.now():
267+
chronicles.info "Imported blocks", nImported,
268+
nUnproc=ctx.nUnprocStr(),
269+
nStagedQ=ctx.blk.staged.len,
270+
base=ctx.chain.baseNumber.bnStr,
271+
head=ctx.chain.latestNumber.bnStr,
272+
target=ctx.subState.head.bnStr,
273+
targetHash=ctx.subState.headHash.short,
274+
nSyncPeers=ctx.pool.nBuddies
275+
ctx.pool.lastSyncUpdLog = Moment.now()
276+
nImported = 0
277+
239278
# Import probably incomplete, so a partial roll back may be needed
240279
let lastBn = qItem.data.blocks[^1].header.number
241280
if ctx.subState.top < lastBn:
242281
ctx.blocksUnprocAppend(ctx.subState.top + 1, lastBn)
243282

244283
# End while loop
245284

246-
if 0 < nImported:
247-
chronicles.info "Imported blocks", nImported, nUnproc=ctx.nUnprocStr(),
248-
nStagedQ=ctx.blk.staged.len,
249-
base=ctx.chain.baseNumber.bnStr, head=ctx.chain.latestNumber.bnStr,
250-
target=ctx.subState.head.bnStr, targetHash=ctx.subState.headHash.short,
251-
nSyncPeers=ctx.pool.nBuddies
285+
if importedOK:
286+
# Sync status logging
287+
if 0 < nImported:
288+
# Note that `nImported` might have been reset above.
289+
chronicles.info "Imported blocks", nImported,
290+
nUnproc=ctx.nUnprocStr(),
291+
nStagedQ=ctx.blk.staged.len,
292+
base=ctx.chain.baseNumber.bnStr,
293+
head=ctx.chain.latestNumber.bnStr,
294+
target=ctx.subState.head.bnStr,
295+
targetHash=ctx.subState.headHash.short,
296+
nSyncPeers=ctx.pool.nBuddies
297+
ctx.pool.lastSyncUpdLog = Moment.now()
252298

253299
elif switchPeer or 0 < ctx.blk.staged.len:
254300
trace info & ": no blocks unqueued", peer,

execution_chain/sync/beacon/worker/blocks/blocks_blocks.nim

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ import
1616
pkg/stew/interval_set,
1717
../../../../networking/p2p,
1818
../../../wire_protocol/types,
19-
../../worker_desc,
20-
../update,
19+
../[update, worker_desc],
2120
./[blocks_fetch, blocks_helpers, blocks_import, blocks_unproc]
2221

2322
# ------------------------------------------------------------------------------

execution_chain/sync/beacon/worker/blocks/blocks_fetch.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import
1515
pkg/eth/common,
1616
pkg/stew/interval_set,
1717
../../../wire_protocol,
18-
../../worker_desc,
18+
../worker_desc,
1919
./blocks_helpers
2020

2121
# ------------------------------------------------------------------------------

execution_chain/sync/beacon/worker/blocks/blocks_helpers.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
{.push raises:[].}
1212

1313
import
14-
../../worker_desc
14+
../worker_desc
1515

1616
# ------------------------------------------------------------------------------
1717
# Public functions

execution_chain/sync/beacon/worker/blocks/blocks_import.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import
1414
pkg/[chronicles, chronos, results],
1515
pkg/eth/common,
1616
../../../wire_protocol,
17-
../../worker_desc
17+
../worker_desc
1818

1919
# ------------------------------------------------------------------------------
2020
# Public function

0 commit comments

Comments
 (0)