Removed obsolete `finalHash` and `final` block number maintenance

why:
  Not needed anymore, `forkChoice()` as has been integrated into
  `importBlock()`.
This commit is contained in:
Jordan Hrycaj 2024-12-19 12:02:20 +00:00
parent 0ea1a959d8
commit c3810253fc
No known key found for this signature in database
GPG Key ID: 4101B9BC5A0DB080
11 changed files with 22 additions and 111 deletions

View File

@ -110,7 +110,7 @@ proc forkchoiceUpdated*(ben: BeaconEngineRef,
# Update sync header (if any) # Update sync header (if any)
com.syncReqNewHead(header) com.syncReqNewHead(header)
com.reqBeaconSyncTargetCB(header, update.finalizedBlockHash) com.reqBeaconSyncTargetCB(header)
return simpleFCU(PayloadExecutionStatus.syncing) return simpleFCU(PayloadExecutionStatus.syncing)

View File

@ -43,7 +43,7 @@ type
SyncReqNewHeadCB* = proc(header: Header) {.gcsafe, raises: [].} SyncReqNewHeadCB* = proc(header: Header) {.gcsafe, raises: [].}
## Update head for syncing ## Update head for syncing
ReqBeaconSyncTargetCB* = proc(header: Header; finHash: Hash32) {.gcsafe, raises: [].} ReqBeaconSyncTargetCB* = proc(header: Header) {.gcsafe, raises: [].}
## Ditto (for beacon sync) ## Ditto (for beacon sync)
NotifyBadBlockCB* = proc(invalid, origin: Header) {.gcsafe, raises: [].} NotifyBadBlockCB* = proc(invalid, origin: Header) {.gcsafe, raises: [].}
@ -350,10 +350,10 @@ proc syncReqNewHead*(com: CommonRef; header: Header)
if not com.syncReqNewHead.isNil: if not com.syncReqNewHead.isNil:
com.syncReqNewHead(header) com.syncReqNewHead(header)
proc reqBeaconSyncTargetCB*(com: CommonRef; header: Header; finHash: Hash32) = proc reqBeaconSyncTargetCB*(com: CommonRef; header: Header) =
## Used by RPC updater ## Used by RPC updater
if not com.reqBeaconSyncTargetCB.isNil: if not com.reqBeaconSyncTargetCB.isNil:
com.reqBeaconSyncTargetCB(header, finHash) com.reqBeaconSyncTargetCB(header)
proc notifyBadBlock*(com: CommonRef; invalid, origin: Header) proc notifyBadBlock*(com: CommonRef; invalid, origin: Header)
{.gcsafe, raises: [].} = {.gcsafe, raises: [].} =

View File

@ -219,7 +219,7 @@ Running the sync process for *MainNet*
-------------------------------------- --------------------------------------
For syncing, a beacon node is needed that regularly informs via *RPC* of a For syncing, a beacon node is needed that regularly informs via *RPC* of a
recently finalised block header. recent target block header.
The beacon node program used here is the *nimbus_beacon_node* binary from the The beacon node program used here is the *nimbus_beacon_node* binary from the
*nimbus-eth2* project (any other, e.g.the *light client* will do.) *nimbus-eth2* project (any other, e.g.the *light client* will do.)
@ -230,7 +230,7 @@ The beacon node program used here is the *nimbus_beacon_node* binary from the
--jwt-secret=/tmp/jwtsecret --jwt-secret=/tmp/jwtsecret
where *http://127.0.0.1:8551* is the URL of the sync process that receives the where *http://127.0.0.1:8551* is the URL of the sync process that receives the
finalised block header (here on the same physical machine) and `/tmp/jwtsecret` target block headers (here on the same physical machine) and `/tmp/jwtsecret`
is the shared secret file needed for mutual communication authentication. is the shared secret file needed for mutual communication authentication.
It will take a while for *nimbus_beacon_node* to catch up (see the It will take a while for *nimbus_beacon_node* to catch up (see the

View File

@ -170,10 +170,6 @@ proc runPeer*(
buddy.only.multiRunIdle = Moment.now() - buddy.only.stoppedMultiRun buddy.only.multiRunIdle = Moment.now() - buddy.only.stoppedMultiRun
buddy.only.nMultiLoop.inc # statistics/debugging buddy.only.nMultiLoop.inc # statistics/debugging
# Update consensus header target when needed. It comes with a finalised
# header hash where we need to complete the block number.
await buddy.headerStagedUpdateTarget info
if not await buddy.napUnlessSomethingToFetch(): if not await buddy.napUnlessSomethingToFetch():
# #
# Layout of a triple of linked header chains (see `README.md`) # Layout of a triple of linked header chains (see `README.md`)

View File

@ -342,7 +342,7 @@ proc blocksStagedImport*(
ctx.updateMetrics() ctx.updateMetrics()
debug info & ": import done", iv, nBlocks, B=ctx.chain.baseNumber.bnStr, debug info & ": import done", iv, nBlocks, B=ctx.chain.baseNumber.bnStr,
L=ctx.chain.latestNumber.bnStr, F=ctx.layout.final.bnStr L=ctx.chain.latestNumber.bnStr
return true return true
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------

View File

@ -92,10 +92,6 @@ proc dbLoadSyncStateLayout*(ctx: BeaconCtxRef; info: static[string]): bool =
# If there was a manual import after a previous sync, then saved state # If there was a manual import after a previous sync, then saved state
# might be outdated. # might be outdated.
if rc.isOk and if rc.isOk and
# The base number is the least record of the FCU chains/tree. So the
# finalised entry must not be smaller.
ctx.chain.baseNumber() <= rc.value.final and
# If the latest FCU number is not larger than the head, there is nothing # If the latest FCU number is not larger than the head, there is nothing
# to do (might also happen after a manual import.) # to do (might also happen after a manual import.)
latest < rc.value.head and latest < rc.value.head and

View File

@ -19,7 +19,7 @@ import
../worker_desc, ../worker_desc,
./update/metrics, ./update/metrics,
./headers_staged/[headers, linked_hchain], ./headers_staged/[headers, linked_hchain],
"."/[headers_unproc, update] ./headers_unproc
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Private functions # Private functions
@ -53,35 +53,6 @@ proc fetchAndCheck(
# Public functions # Public functions
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
proc headerStagedUpdateTarget*(
buddy: BeaconBuddyRef;
info: static[string];
) {.async: (raises: []).} =
## Fetch finalised beacon header if there is an update available
let
ctx = buddy.ctx
peer = buddy.peer
if ctx.layout.lastState == idleSyncState and
ctx.target.final == 0 and
ctx.target.finalHash != zeroHash32 and
not ctx.target.locked:
const iv = BnRange.new(1u,1u) # dummy interval
ctx.target.locked = true
let rc = await buddy.headersFetchReversed(iv, ctx.target.finalHash, info)
ctx.target.locked = false
if rc.isOk:
let hash = rlp.encode(rc.value[0]).keccak256
if hash != ctx.target.finalHash:
# Oops
buddy.ctrl.zombie = true
trace info & ": finalised header hash mismatch", peer, hash,
expected=ctx.target.finalHash
else:
ctx.updateFinalBlockHeader(rc.value[0], ctx.target.finalHash, info)
proc headersStagedCollect*( proc headersStagedCollect*(
buddy: BeaconBuddyRef; buddy: BeaconBuddyRef;
info: static[string]; info: static[string];

View File

@ -38,7 +38,7 @@ when enableTicker:
head: ctx.layout.head, head: ctx.layout.head,
headOk: ctx.layout.lastState != idleSyncState, headOk: ctx.layout.lastState != idleSyncState,
target: ctx.target.consHead.number, target: ctx.target.consHead.number,
targetOk: ctx.target.final != 0, targetOk: ctx.target.changed,
nHdrStaged: ctx.headersStagedQueueLen(), nHdrStaged: ctx.headersStagedQueueLen(),
hdrStagedTop: ctx.headersStagedQueueTopKey(), hdrStagedTop: ctx.headersStagedQueueTopKey(),
@ -62,31 +62,13 @@ proc updateBeaconHeaderCB(
): ReqBeaconSyncTargetCB = ): ReqBeaconSyncTargetCB =
## Update beacon header. This function is intended as a call back function ## Update beacon header. This function is intended as a call back function
## for the RPC module. ## for the RPC module.
return proc(h: Header; f: Hash32) {.gcsafe, raises: [].} = return proc(h: Header) {.gcsafe, raises: [].} =
if ctx.chain.baseNumber() < h.number and # sanity check
# Check whether there is an update running (otherwise take next upate)
if not ctx.target.locked and # ignore if currently updating
ctx.target.final == 0 and # ignore if complete already
f != zeroHash32 and # finalised hash is set
ctx.layout.head < h.number and # update is advancing ctx.layout.head < h.number and # update is advancing
ctx.target.consHead.number < h.number: # .. ditto ctx.target.consHead.number < h.number: # .. ditto
ctx.target.consHead = h ctx.target.consHead = h
ctx.target.finalHash = f ctx.target.changed = true # enable this dataset
ctx.target.changed = true ctx.updateFromHibernating info # wake up if sleeping
# Check whether `FC` knows about the finalised block already.
#
# On a full node, all blocks before the current state are stored on the
# database which is also accessed by `FC`. So one can already decude here
# whether `FC` id capable of handling that finalised block (the number of
# must be at least the `base` from `FC`.)
#
# Otherwise the block header will need to be fetched from a peer when
# available and checked there (see `headerStagedUpdateTarget()`.)
#
let finHdr = ctx.chain.headerByHash(f).valueOr: return
ctx.updateFinalBlockHeader(finHdr, f, info)
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Public functions # Public functions

View File

@ -156,8 +156,6 @@ proc setupCollectingHeaders(ctx: BeaconCtxRef; info: static[string]) =
ctx.sst.layout = SyncStateLayout( ctx.sst.layout = SyncStateLayout(
coupler: c, coupler: c,
dangling: h, dangling: h,
final: ctx.target.final,
finalHash: ctx.target.finalHash,
head: h, head: h,
lastState: collectingHeaders) # state transition lastState: collectingHeaders) # state transition
@ -282,8 +280,7 @@ proc updateSyncState*(ctx: BeaconCtxRef; info: static[string]) =
# Check whether the system has been idle and a new header download # Check whether the system has been idle and a new header download
# session can be set up # session can be set up
if prevState == idleSyncState and if prevState == idleSyncState and
ctx.target.changed and # and there is a new target from CL ctx.target.changed: # and there is a new target from CL
ctx.target.final != 0: # .. ditto
ctx.setupCollectingHeaders info # set up new header sync ctx.setupCollectingHeaders info # set up new header sync
return return
# Notreached # Notreached
@ -313,33 +310,14 @@ proc updateSyncState*(ctx: BeaconCtxRef; info: static[string]) =
ctx.startHibernating info ctx.startHibernating info
proc updateFinalBlockHeader*( proc updateFromHibernating*(ctx: BeaconCtxRef; info: static[string]) =
ctx: BeaconCtxRef; ## Activate syncer if hibernating.
finHdr: Header;
finHash: Hash32;
info: static[string];
) =
## Update the finalised header cache. If the finalised header is acceptable,
## the syncer will be activated from hibernation if necessary.
##
let
b = ctx.chain.baseNumber()
f = finHdr.number
if f < b:
trace info & ": finalised block # too low",
B=b.bnStr, finalised=f.bnStr, delta=(b - f)
ctx.target.reset
else:
ctx.target.final = f
ctx.target.finalHash = finHash
# Activate running (unless done yet)
if ctx.hibernate: if ctx.hibernate:
ctx.hibernate = false ctx.hibernate = false # activates syncer
debug info & ": activating syncer", B=b.bnStr, debug info & ": activating syncer", T=ctx.target.consHead.bnStr
finalised=f.bnStr, head=ctx.target.consHead.bnStr
# Re-calculate sync state
ctx.updateSyncState info
# Update, so it can be followed nicely # Update, so it can be followed nicely
ctx.updateMetrics() ctx.updateMetrics()

View File

@ -128,10 +128,6 @@ const
## entry block number is too high and so leaves a gap to the ledger state ## entry block number is too high and so leaves a gap to the ledger state
## block number.) ## block number.)
finaliserChainLengthMax* = 32
## When importing with `importBlock()`, finalise after at most this many
## invocations of `importBlock()`.
# ---------------------- # ----------------------
static: static:

View File

@ -72,11 +72,8 @@ type
SyncStateTarget* = object SyncStateTarget* = object
## Beacon state to be implicitely updated by RPC method ## Beacon state to be implicitely updated by RPC method
locked*: bool ## Don't update while fetching header
changed*: bool ## Tell that something has changed changed*: bool ## Tell that something has changed
consHead*: Header ## Consensus head consHead*: Header ## Consensus head
final*: BlockNumber ## Finalised block number
finalHash*: Hash32 ## Finalised hash
SyncStateLayout* = object SyncStateLayout* = object
## Layout of a linked header chains defined by the triple `(C,D,H)` as ## Layout of a linked header chains defined by the triple `(C,D,H)` as
@ -95,14 +92,9 @@ type
## ##
coupler*: BlockNumber ## Bottom end `C` of full chain `(C,H]` coupler*: BlockNumber ## Bottom end `C` of full chain `(C,H]`
dangling*: BlockNumber ## Left end `D` of linked chain `[D,H]` dangling*: BlockNumber ## Left end `D` of linked chain `[D,H]`
head*: BlockNumber ## `H`, block num of some finalised block head*: BlockNumber ## `H`, block number of some target block
lastState*: SyncLayoutState ## Last known layout state lastState*: SyncLayoutState ## Last known layout state
# Legacy entries, will be removed some time. This is currently needed
# for importing blocks into `FC` the support of which will be deprecated.
final*: BlockNumber ## Finalised block number `F`
finalHash*: Hash32 ## Hash of `F`
SyncState* = object SyncState* = object
## Sync state for header and block chains ## Sync state for header and block chains
target*: SyncStateTarget ## Consensus head, see `T` in `README.md` target*: SyncStateTarget ## Consensus head, see `T` in `README.md`