From 070f117d3c5b18d4e1e13f2117a89d7a57948940 Mon Sep 17 00:00:00 2001 From: Jordan Hrycaj Date: Mon, 21 Oct 2024 18:01:45 +0000 Subject: [PATCH] Suppress beacon sync unless potential actions (#2765) * Update comments & logs * Do not start beacon sync unless there is possibly something to do why: It would continue polling without having any effect other than logging. Now it will not start unless there is RPC available or there was a previously interrupted sync to be resumed. --- nimbus/nimbus_desc.nim | 4 ++-- nimbus/nimbus_execution_client.nim | 12 ++++++------ nimbus/sync/beacon.nim | 18 +++++++++++------- nimbus/sync/beacon/worker/start_stop.nim | 2 +- .../sync/beacon/worker/start_stop/ticker.nim | 6 +++--- nimbus/sync/beacon/worker_desc.nim | 2 +- 6 files changed, 24 insertions(+), 20 deletions(-) diff --git a/nimbus/nimbus_desc.nim b/nimbus/nimbus_desc.nim index 2a642789f..cb2f51264 100644 --- a/nimbus/nimbus_desc.nim +++ b/nimbus/nimbus_desc.nim @@ -63,8 +63,8 @@ proc stop*(nimbus: NimbusNode, conf: NimbusConf) {.async, gcsafe.} = await nimbus.networkLoop.cancelAndWait() if nimbus.peerManager.isNil.not: await nimbus.peerManager.stop() - #if nimbus.snapSyncRef.isNil.not: - # nimbus.snapSyncRef.stop() + if nimbus.beaconSyncRef.isNil.not: + nimbus.beaconSyncRef.stop() if nimbus.metricsServer.isNil.not: await nimbus.metricsServer.stop() diff --git a/nimbus/nimbus_execution_client.nim b/nimbus/nimbus_execution_client.nim index 65bd002bc..6222637f6 100644 --- a/nimbus/nimbus_execution_client.nim +++ b/nimbus/nimbus_execution_client.nim @@ -110,17 +110,13 @@ proc setupP2P(nimbus: NimbusNode, conf: NimbusConf, nimbus.ethNode.peerPool, nimbus.chainRef, nimbus.txPool) - #of ProtocolFlag.Snap: - # nimbus.ethNode.addSnapHandlerCapability( - # nimbus.ethNode.peerPool, - # nimbus.chainRef) # Cannot do without minimal `eth` capability if ProtocolFlag.Eth notin protocols: nimbus.ethNode.addEthHandlerCapability( nimbus.ethNode.peerPool, nimbus.chainRef) - # Always start syncer -- will throttle itself unless needed + # Always initialise beacon syncer nimbus.beaconSyncRef = BeaconSyncRef.init( nimbus.ethNode, nimbus.chainRef, conf.maxPeers, conf.beaconChunkSize) @@ -225,7 +221,11 @@ proc run(nimbus: NimbusNode, conf: NimbusConf) = setupRpc(nimbus, conf, com, protocols) if conf.maxPeers > 0: - nimbus.beaconSyncRef.start + # Not starting syncer if there is definitely no way to run it. This + # avoids polling (i.e. waiting for instructions) and some logging. + let resumeOnly = not conf.engineApiServerEnabled() + if not nimbus.beaconSyncRef.start(resumeOnly): + nimbus.beaconSyncRef = BeaconSyncRef(nil) if nimbus.state == NimbusState.Starting: # it might have been set to "Stopping" with Ctrl+C diff --git a/nimbus/sync/beacon.nim b/nimbus/sync/beacon.nim index 5cd3a52d9..8de012520 100644 --- a/nimbus/sync/beacon.nim +++ b/nimbus/sync/beacon.nim @@ -14,7 +14,7 @@ import pkg/[chronicles, chronos, eth/p2p, results], pkg/stew/[interval_set, sorted_set], ../core/chain, - ./beacon/[worker, worker_desc], + ./beacon/[worker, worker_desc, worker/db], "."/[sync_desc, sync_sched, protocol] logScope: @@ -62,16 +62,20 @@ proc init*( var desc = T() desc.initSync(ethNode, maxPeers) desc.ctx.pool.nBodiesBatch = chunkSize - # Initalise for `persistBlocks()` desc.ctx.pool.chain = chain desc -proc start*(ctx: BeaconSyncRef) = - ## Beacon Sync always begin with stop mode - doAssert ctx.startSync() # Initialize subsystems +proc start*(desc: BeaconSyncRef; resumeOnly = false): bool = + ## Start beacon sync. If `resumeOnly` is set `true` the syncer will only + ## start up if it can resume work, e.g. after being previously interrupted. + if resumeOnly: + desc.ctx.dbLoadSyncStateLayout() + if not desc.ctx.layout.headLocked: + return false + desc.startSync() -proc stop*(ctx: BeaconSyncRef) = - ctx.stopSync() +proc stop*(desc: BeaconSyncRef) = + desc.stopSync() # ------------------------------------------------------------------------------ # End diff --git a/nimbus/sync/beacon/worker/start_stop.nim b/nimbus/sync/beacon/worker/start_stop.nim index 359e7baa8..b616d3f0b 100644 --- a/nimbus/sync/beacon/worker/start_stop.nim +++ b/nimbus/sync/beacon/worker/start_stop.nim @@ -98,7 +98,7 @@ proc setupDatabase*(ctx: BeaconCtxRef) = # Load initial state from database if there is any ctx.dbLoadSyncStateLayout() - # Set blocks batch import value for `persistBlocks()` + # Set blocks batch import value for block import if ctx.pool.nBodiesBatch < nFetchBodiesRequest: if ctx.pool.nBodiesBatch == 0: ctx.pool.nBodiesBatch = nFetchBodiesBatchDefault diff --git a/nimbus/sync/beacon/worker/start_stop/ticker.nim b/nimbus/sync/beacon/worker/start_stop/ticker.nim index 84938ff29..e5d40b28f 100644 --- a/nimbus/sync/beacon/worker/start_stop/ticker.nim +++ b/nimbus/sync/beacon/worker/start_stop/ticker.nim @@ -18,7 +18,7 @@ import ../helpers logScope: - topics = "ticker" + topics = "beacon ticker" type TickerStatsUpdater* = proc: TickerStats {.gcsafe, raises: [].} @@ -108,10 +108,10 @@ proc tickerLogger(t: TickerRef) {.gcsafe.} = t.visited = now if data.stored == data.base: - info "Sync state", up, peers, + debug "Sync state", up, peers, B, L, C, D, F, H, T, hS, hU, bS, bU, rrg, mem else: - info "Sync state", up, peers, + debug "Sync state", up, peers, S=data.stored.bnStr, B, L, C, D, F, H, T, hS, hU, bS, bU, rrg, mem diff --git a/nimbus/sync/beacon/worker_desc.nim b/nimbus/sync/beacon/worker_desc.nim index b71046d20..d462f107a 100644 --- a/nimbus/sync/beacon/worker_desc.nim +++ b/nimbus/sync/beacon/worker_desc.nim @@ -130,7 +130,7 @@ type blkSync*: BlocksImportSync ## For importing/executing blocks nextUpdate*: Moment ## For updating metrics - # Blocks import/execution settings for running `persistBlocks()` with + # Blocks import/execution settings for importing with # `nBodiesBatch` blocks in each round (minimum value is # `nFetchBodiesRequest`.) chain*: ForkedChainRef ## Database