Skip to content

Commit 232e756

Browse files
authored
Beacon sync move fork choice logic to fc sub module (#3162)
* Move fc-update receptor call back function to `FC` sub-module why The `FC` sub-module `chain_header_cache` keeps track of an header ancestry chain. As the `head` of the header chain is always set from the fork-choice update head, the call back function receiving the head is moved here. The client app using the`chain_header_cache` sub-module can be informed in an event driven way by a notification call back function (or fall back to polling.) This module also maintains the `finalized` hash value that is associated with the fork-choice update head. While the heads of subsequent fork-choice updates are ignored after a chain is initialised, the `finalized` hash values are kept in a cache for resolving headers over time while the `chain_header_cache` is active. Only the child-most finalised header is kept and available for further use by the client app. * Rename `reqBeaconSyncerTarget()` to `fcHeaderClUpdate()` why This naming does reflect the new owner of this hook. Currently, it is only used by the `FC` sub-module `chain_header_cache`. * Move function calls `importBlocks()/forkChoice()` to `FC` sub-module details This function pair is replaced by a single convenience wrapper function. This is a step into further integration into the `FC` module proper. why This moves the details of when to call `forkChoice()` away from the syncer code. It also gets rid of handling the `finalized` variable within the syncer. * Remove public exposure of `finalized` value from `FC` sub-module why Is used internally only by the `fcHeaderImportBlock()` wrapper function. * Remove crufty `syncReqNewHead()` hook why Neither used not referenced since 29/09/2024 when the `beacon sync (#2666)` predecessor was removed * Code cosmetics, docu update, logging update, etc.
1 parent d80f530 commit 232e756

File tree

20 files changed

+436
-350
lines changed

20 files changed

+436
-350
lines changed

execution_chain/beacon/api_handler/api_forkchoice.nim

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,8 @@ proc forkchoiceUpdated*(ben: BeaconEngineRef,
108108
number = header.number,
109109
hash = headHash.short
110110

111-
# Update sync header (if any)
112-
com.syncReqNewHead(header)
113-
114-
# Ask the syncer to install missing blcks via RLPX
115-
com.reqBeaconSyncerTarget(header, update.finalizedBlockHash)
111+
# Inform the header cache (used by the syncer)
112+
com.fcHeaderClUpdate(header, update.finalizedBlockHash)
116113

117114
return simpleFCU(PayloadExecutionStatus.syncing)
118115

execution_chain/beacon/beacon_engine.nim

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,4 @@ proc delayPayloadImport*(ben: BeaconEngineRef, header: Header): PayloadStatusV1
259259
# at a later time.
260260
ben.put(blockHash, header)
261261

262-
# Although we don't want to trigger a sync, if there is one already in
263-
# progress, try to extend it with the current payload request to relieve
264-
# some strain from the forkchoice update.
265-
ben.com.syncReqNewHead(header)
266-
267262
PayloadStatusV1(status: PayloadExecutionStatus.syncing)

execution_chain/common/common.nim

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@ type
3636
current: BlockNumber
3737
highest: BlockNumber
3838

39-
SyncReqNewHeadCB* = proc(header: Header) {.gcsafe, raises: [].}
40-
## Update head for syncing
41-
42-
ReqBeaconSyncerTargetCB* = proc(header: Header; finHash: Hash32) {.gcsafe, raises: [].}
43-
## Ditto (for beacon sync)
39+
FcHeaderClUpdateCB* = proc(header: Header; finHash: Hash32) {.gcsafe, raises: [].}
40+
## Inform `CL` sub-module `chain_header_cache` about new head.
4441

4542
BeaconSyncerProgressCB* = proc(): tuple[start, current, target: BlockNumber] {.gcsafe, raises: [].}
4643
## Query syncer status
@@ -70,11 +67,7 @@ type
7067
# synchronizer need this
7168
syncProgress: SyncProgress
7269

73-
syncReqNewHead: SyncReqNewHeadCB
74-
## Call back function for the sync processor. This function stages
75-
## the arguent header to a private aerea for subsequent processing.
76-
77-
reqBeaconSyncerTargetCB: ReqBeaconSyncerTargetCB
70+
fcHeaderClUpdateCB: FcHeaderClUpdateCB
7871
## Call back function for a sync processor that returns the canonical
7972
## header.
8073

@@ -340,16 +333,10 @@ proc proofOfStake*(com: CommonRef, header: Header, txFrame: CoreDbTxRef): bool =
340333
func depositContractAddress*(com: CommonRef): Address =
341334
com.config.depositContractAddress.get(default(Address))
342335

343-
proc syncReqNewHead*(com: CommonRef; header: Header)
344-
{.gcsafe, raises: [].} =
336+
proc fcHeaderClUpdate*(com: CommonRef; header: Header; finHash: Hash32) =
345337
## Used by RPC updater
346-
if not com.syncReqNewHead.isNil:
347-
com.syncReqNewHead(header)
348-
349-
proc reqBeaconSyncerTarget*(com: CommonRef; header: Header; finHash: Hash32) =
350-
## Used by RPC updater
351-
if not com.reqBeaconSyncerTargetCB.isNil:
352-
com.reqBeaconSyncerTargetCB(header, finHash)
338+
if not com.fcHeaderClUpdateCB.isNil:
339+
com.fcHeaderClUpdateCB(header, finHash)
353340

354341
proc beaconSyncerProgress*(com: CommonRef): tuple[start, current, target: BlockNumber] =
355342
## Query syncer status
@@ -460,13 +447,9 @@ func setTTD*(com: CommonRef, ttd: Opt[DifficultyInt]) =
460447
# rebuild the MergeFork piece of the forkTransitionTable
461448
com.forkTransitionTable.mergeForkTransitionThreshold = com.config.mergeForkTransitionThreshold
462449

463-
func `syncReqNewHead=`*(com: CommonRef; cb: SyncReqNewHeadCB) =
464-
## Activate or reset a call back handler for syncing.
465-
com.syncReqNewHead = cb
466-
467-
func `reqBeaconSyncerTarget=`*(com: CommonRef; cb: ReqBeaconSyncerTargetCB) =
450+
func `fcHeaderClUpdate=`*(com: CommonRef; cb: FcHeaderClUpdateCB) =
468451
## Activate or reset a call back handler for syncing.
469-
com.reqBeaconSyncerTargetCB = cb
452+
com.fcHeaderClUpdateCB = cb
470453

471454
func `beaconSyncerProgress=`*(com: CommonRef; cb: BeaconSyncerProgressCB) =
472455
## Activate or reset a call back handler for querying syncer.

execution_chain/config.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,13 @@ type
339339
desc: "Number of worker threads (\"0\" = use as many threads as there are CPU cores available)"
340340
name: "num-threads" .}: int
341341

342-
beaconSyncScrumFile* {.
342+
beaconSyncTargetFile* {.
343343
hidden
344344
desc: "Load a file containg an rlp-encoded object \"(Header,Hash32)\" " &
345345
"to be used " &
346-
"as the first scrum target before any other request from the CL " &
347-
" is accepted"
348-
name: "debug-beacon-sync-scrum-file" .}: Option[InputFile]
346+
"as the first target before any other request from the CL " &
347+
"is accepted"
348+
name: "debug-beacon-sync-target-file" .}: Option[InputFile]
349349

350350
beaconSyncBlocksQueueHwm* {.
351351
hidden

execution_chain/core/chain.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
# according to those terms.
1010

1111
import
12-
./chain/[persist_blocks, forked_chain]
12+
./chain/[persist_blocks, forked_chain, forked_chain/chain_header_cache]
1313

1414
export
15+
chain_header_cache,
1516
persist_blocks,
1617
forked_chain
1718

execution_chain/core/chain/forked_chain.nim

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import
1919
../../evm/state,
2020
../validate,
2121
../executor/process_block,
22-
./forked_chain/[chain_desc, chain_header_cache, chain_branch]
22+
./forked_chain/[chain_desc, chain_branch]
2323

2424
from std/sequtils import mapIt
2525

@@ -29,7 +29,6 @@ logScope:
2929
export
3030
BlockDesc,
3131
ForkedChainRef,
32-
chain_header_cache,
3332
common,
3433
core_db
3534

0 commit comments

Comments
 (0)