2022-08-04 08:04:30 +00:00
|
|
|
# Nimbus
|
2022-07-21 12:14:41 +00:00
|
|
|
# Copyright (c) 2018-2021 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.
|
|
|
|
|
|
|
|
import
|
2022-09-30 08:22:14 +00:00
|
|
|
std/[options],
|
2022-07-21 12:14:41 +00:00
|
|
|
chronicles,
|
|
|
|
chronos,
|
|
|
|
eth/[common/eth_types, p2p],
|
2022-08-04 08:04:30 +00:00
|
|
|
".."/[protocol, sync_desc],
|
2022-09-30 08:22:14 +00:00
|
|
|
../misc/[best_pivot, block_queue],
|
2022-08-04 08:04:30 +00:00
|
|
|
./ticker
|
2022-07-21 12:14:41 +00:00
|
|
|
|
|
|
|
{.push raises:[Defect].}
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "full-sync"
|
|
|
|
|
|
|
|
type
|
2022-08-04 08:04:30 +00:00
|
|
|
BuddyData* = object
|
2022-07-21 12:14:41 +00:00
|
|
|
## Local descriptor data extension
|
2022-09-30 08:22:14 +00:00
|
|
|
pivot: BestPivotWorkerRef ## Local pivot worker descriptor
|
|
|
|
bQueue: BlockQueueWorkerRef ## Block queue worker
|
2022-08-04 08:04:30 +00:00
|
|
|
bestNumber: Option[BlockNumber] ## Largest block number reported
|
2022-07-21 12:14:41 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
CtxData* = object
|
2022-07-21 12:14:41 +00:00
|
|
|
## Globally shared data extension
|
2022-09-30 08:22:14 +00:00
|
|
|
rng*: ref HmacDrbgContext ## Random generator, pre-initialised
|
|
|
|
pivot: BestPivotCtxRef ## Global pivot descriptor
|
|
|
|
bCtx: BlockQueueCtxRef ## Global block queue descriptor
|
2022-08-04 08:04:30 +00:00
|
|
|
ticker: TickerRef ## Logger ticker
|
|
|
|
|
|
|
|
FullBuddyRef* = ##\
|
|
|
|
## Extended worker peer descriptor
|
|
|
|
BuddyRef[CtxData,BuddyData]
|
|
|
|
|
|
|
|
FullCtxRef* = ##\
|
|
|
|
## Extended global descriptor
|
|
|
|
CtxRef[CtxData]
|
2022-07-21 12:14:41 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc pp(n: BlockNumber): string =
|
|
|
|
## Dedicated pretty printer (`$` is defined elsewhere using `UInt256`)
|
2022-09-30 08:22:14 +00:00
|
|
|
if n == high(BlockNumber): "high" else:"#" & $n
|
2022-07-21 12:14:41 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-09-30 08:22:14 +00:00
|
|
|
# Private functions
|
2022-07-21 12:14:41 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
proc topUsedNumber(
|
|
|
|
ctx: FullCtxRef;
|
|
|
|
backBlocks = maxHeadersFetch;
|
|
|
|
): Result[BlockNumber,void] =
|
|
|
|
var
|
|
|
|
top = 0.toBlockNumber
|
2022-07-21 12:14:41 +00:00
|
|
|
try:
|
|
|
|
let
|
|
|
|
bestNumber = ctx.chain.getBestBlockHeader.blockNumber
|
|
|
|
nBackBlocks = backBlocks.toBlockNumber
|
|
|
|
# Initialise before best block number
|
2022-09-30 08:22:14 +00:00
|
|
|
if nBackBlocks < bestNumber:
|
|
|
|
top = bestNumber - nBackBlocks
|
2022-07-21 12:14:41 +00:00
|
|
|
except CatchableError as e:
|
|
|
|
error "Best block header problem", backBlocks, error=($e.name), msg=e.msg
|
2022-09-30 08:22:14 +00:00
|
|
|
return err()
|
2022-07-21 12:14:41 +00:00
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
ok(top)
|
2022-07-21 12:14:41 +00:00
|
|
|
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc tickerUpdater(ctx: FullCtxRef): TickerStatsUpdater =
|
2022-07-21 12:14:41 +00:00
|
|
|
result = proc: TickerStats =
|
2022-09-30 08:22:14 +00:00
|
|
|
var stats: BlockQueueStats
|
|
|
|
ctx.data.bCtx.blockQueueStats(stats)
|
2022-07-21 12:14:41 +00:00
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
TickerStats(
|
|
|
|
topPersistent: stats.topAccepted,
|
|
|
|
nextStaged: stats.nextStaged,
|
|
|
|
nextUnprocessed: stats.nextUnprocessed,
|
|
|
|
nStagedQueue: stats.nStagedQueue,
|
|
|
|
reOrg: stats.reOrg)
|
2022-07-21 12:14:41 +00:00
|
|
|
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc processStaged(buddy: FullBuddyRef): bool =
|
2022-07-21 12:14:41 +00:00
|
|
|
## Fetch a work item from the `staged` queue an process it to be
|
|
|
|
## stored on the persistent block chain.
|
|
|
|
let
|
2022-08-04 08:04:30 +00:00
|
|
|
ctx = buddy.ctx
|
2022-07-21 12:14:41 +00:00
|
|
|
peer = buddy.peer
|
|
|
|
chainDb = buddy.ctx.chain
|
2022-09-30 08:22:14 +00:00
|
|
|
bq = buddy.data.bQueue
|
2022-08-04 08:04:30 +00:00
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
# Get a work item, a list of headers + bodies
|
|
|
|
wi = block:
|
|
|
|
let rc = bq.blockQueueFetchStaged()
|
|
|
|
if rc.isErr:
|
|
|
|
return false
|
|
|
|
rc.value
|
2022-07-21 12:14:41 +00:00
|
|
|
|
|
|
|
startNumber = wi.headers[0].blockNumber
|
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
# Store in persistent database
|
2022-07-21 12:14:41 +00:00
|
|
|
try:
|
|
|
|
if chainDb.persistBlocks(wi.headers, wi.bodies) == ValidationResult.OK:
|
2022-09-30 08:22:14 +00:00
|
|
|
bq.blockQueueAccept(wi)
|
2022-07-21 12:14:41 +00:00
|
|
|
return true
|
|
|
|
except CatchableError as e:
|
|
|
|
error "Storing persistent blocks failed", peer, range=($wi.blocks),
|
|
|
|
error = $e.name, msg = e.msg
|
|
|
|
except Defect as e:
|
|
|
|
# Pass through
|
|
|
|
raise e
|
|
|
|
except Exception as e:
|
|
|
|
# Notorious case where the `Chain` reference applied to
|
|
|
|
# `persistBlocks()` has the compiler traced a possible `Exception`
|
|
|
|
# (i.e. `ctx.chain` could be uninitialised.)
|
|
|
|
error "Exception while storing persistent blocks", peer,
|
|
|
|
range=($wi.blocks), error=($e.name), msg=e.msg
|
|
|
|
raise (ref Defect)(msg: $e.name & ": " & e.msg)
|
|
|
|
|
|
|
|
# Something went wrong. Recycle work item (needs to be re-fetched, anyway)
|
|
|
|
let
|
2022-09-30 08:22:14 +00:00
|
|
|
parentHoN = HashOrNum(isHash: true, hash: wi.headers[0].parentHash)
|
2022-07-21 12:14:41 +00:00
|
|
|
try:
|
|
|
|
# Check whether hash of the first block is consistent
|
|
|
|
var parent: BlockHeader
|
|
|
|
if chainDb.getBlockHeader(parentHoN, parent):
|
|
|
|
# First block parent is ok, so there might be other problems. Re-fetch
|
|
|
|
# the blocks from another peer.
|
2022-09-30 08:22:14 +00:00
|
|
|
trace "Storing persistent blocks failed", peer, range=($wi.blocks)
|
|
|
|
bq.blockQueueRecycle(wi)
|
2022-07-21 12:14:41 +00:00
|
|
|
buddy.ctrl.zombie = true
|
|
|
|
return false
|
|
|
|
except CatchableError as e:
|
|
|
|
error "Failed to access parent blocks", peer,
|
|
|
|
blockNumber=wi.headers[0].blockNumber.pp, error=($e.name), msg=e.msg
|
|
|
|
|
|
|
|
# Parent block header problem, so we might be in the middle of a re-org.
|
|
|
|
# Set single mode backtrack following the offending parent hash.
|
2022-09-30 08:22:14 +00:00
|
|
|
bq.blockQueueBacktrackFrom(wi)
|
2022-07-21 12:14:41 +00:00
|
|
|
buddy.ctrl.multiOk = false
|
|
|
|
|
|
|
|
if wi.topHash.isNone:
|
|
|
|
# Assuming that currently staged entries are on the wrong branch
|
2022-09-30 08:22:14 +00:00
|
|
|
bq.blockQueueRecycleStaged()
|
|
|
|
notice "Starting chain re-org backtrack work item", peer, range=($wi.blocks)
|
2022-07-21 12:14:41 +00:00
|
|
|
else:
|
|
|
|
# Leave that block range in the staged list
|
2022-09-30 08:22:14 +00:00
|
|
|
trace "Resuming chain re-org backtrack work item", peer, range=($wi.blocks)
|
2022-07-21 12:14:41 +00:00
|
|
|
discard
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public start/stop and admin functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc setup*(ctx: FullCtxRef; tickerOK: bool): bool =
|
2022-07-21 12:14:41 +00:00
|
|
|
## Global set up
|
2022-09-30 08:22:14 +00:00
|
|
|
ctx.data.pivot = BestPivotCtxRef.init(ctx.data.rng)
|
2022-07-21 12:14:41 +00:00
|
|
|
if tickerOK:
|
2022-08-04 08:04:30 +00:00
|
|
|
ctx.data.ticker = TickerRef.init(ctx.tickerUpdater)
|
2022-07-21 12:14:41 +00:00
|
|
|
else:
|
|
|
|
debug "Ticker is disabled"
|
2022-09-30 08:22:14 +00:00
|
|
|
let rc = ctx.topUsedNumber(backBlocks = 0)
|
|
|
|
if rc.isErr:
|
|
|
|
ctx.data.bCtx = BlockQueueCtxRef.init()
|
|
|
|
return false
|
|
|
|
ctx.data.bCtx = BlockQueueCtxRef.init(rc.value + 1)
|
|
|
|
true
|
2022-07-21 12:14:41 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc release*(ctx: FullCtxRef) =
|
2022-07-21 12:14:41 +00:00
|
|
|
## Global clean up
|
2022-09-30 08:22:14 +00:00
|
|
|
ctx.data.pivot = nil
|
2022-08-04 08:04:30 +00:00
|
|
|
if not ctx.data.ticker.isNil:
|
|
|
|
ctx.data.ticker.stop()
|
2022-07-21 12:14:41 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc start*(buddy: FullBuddyRef): bool =
|
2022-07-21 12:14:41 +00:00
|
|
|
## Initialise worker peer
|
2022-09-30 08:22:14 +00:00
|
|
|
let
|
|
|
|
ctx = buddy.ctx
|
|
|
|
peer = buddy.peer
|
|
|
|
if peer.supports(protocol.eth) and
|
|
|
|
peer.state(protocol.eth).initialized:
|
2022-08-04 08:04:30 +00:00
|
|
|
if not ctx.data.ticker.isNil:
|
|
|
|
ctx.data.ticker.startBuddy()
|
2022-09-30 08:22:14 +00:00
|
|
|
buddy.data.pivot =
|
|
|
|
BestPivotWorkerRef.init(ctx.data.pivot, buddy.ctrl, buddy.peer)
|
|
|
|
buddy.data.bQueue = BlockQueueWorkerRef.init(
|
|
|
|
ctx.data.bCtx, buddy.ctrl, peer)
|
2022-07-21 12:14:41 +00:00
|
|
|
return true
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc stop*(buddy: FullBuddyRef) =
|
2022-07-21 12:14:41 +00:00
|
|
|
## Clean up this peer
|
|
|
|
buddy.ctrl.stopped = true
|
2022-09-30 08:22:14 +00:00
|
|
|
buddy.data.pivot.clear()
|
|
|
|
if not buddy.ctx.data.ticker.isNil:
|
|
|
|
buddy.ctx.data.ticker.stopBuddy()
|
2022-07-21 12:14:41 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc runSingle*(buddy: FullBuddyRef) {.async.} =
|
2022-07-21 12:14:41 +00:00
|
|
|
## This peer worker is invoked if the peer-local flag `buddy.ctrl.multiOk`
|
|
|
|
## is set `false` which is the default mode. This flag is updated by the
|
|
|
|
## worker when deemed appropriate.
|
|
|
|
## * For all workers, there can be only one `runSingle()` function active
|
|
|
|
## simultaneously for all worker peers.
|
|
|
|
## * There will be no `runMulti()` function active for the same worker peer
|
|
|
|
## simultaneously
|
|
|
|
## * There will be no `runPool()` iterator active simultaneously.
|
|
|
|
##
|
|
|
|
## Note that this function runs in `async` mode.
|
|
|
|
##
|
2022-08-04 08:04:30 +00:00
|
|
|
let
|
|
|
|
ctx = buddy.ctx
|
|
|
|
peer = buddy.peer
|
2022-09-30 08:22:14 +00:00
|
|
|
bq = buddy.data.bQueue
|
2022-07-21 12:14:41 +00:00
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
if bq.blockQueueBacktrackOk:
|
|
|
|
let rc = await bq.blockQueueBacktrackWorker()
|
|
|
|
if rc.isOk:
|
2022-07-21 12:14:41 +00:00
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
# Update persistent database (may reset `multiOk`)
|
2022-07-21 12:14:41 +00:00
|
|
|
buddy.ctrl.multiOk = true
|
2022-09-30 08:22:14 +00:00
|
|
|
while buddy.processStaged() and not buddy.ctrl.stopped:
|
|
|
|
# Allow thread switch as `persistBlocks()` might be slow
|
|
|
|
await sleepAsync(10.milliseconds)
|
|
|
|
return
|
|
|
|
|
|
|
|
buddy.ctrl.zombie = true
|
|
|
|
|
|
|
|
# Initialise/re-initialise this worker
|
2022-10-08 17:20:50 +00:00
|
|
|
elif await buddy.data.pivot.pivotNegotiate(buddy.data.bestNumber):
|
2022-09-30 08:22:14 +00:00
|
|
|
buddy.ctrl.multiOk = true
|
|
|
|
# Update/activate `bestNumber` for local use
|
|
|
|
buddy.data.bestNumber =
|
2022-10-08 17:20:50 +00:00
|
|
|
some(buddy.data.pivot.pivotHeader.value.blockNumber)
|
2022-09-30 08:22:14 +00:00
|
|
|
|
|
|
|
elif not buddy.ctrl.stopped:
|
|
|
|
await sleepAsync(2.seconds)
|
2022-07-21 12:14:41 +00:00
|
|
|
|
|
|
|
|
2022-08-24 13:44:18 +00:00
|
|
|
proc runPool*(buddy: FullBuddyRef; last: bool) =
|
2022-07-21 12:14:41 +00:00
|
|
|
## Ocne started, the function `runPool()` is called for all worker peers in
|
|
|
|
## a row (as the body of an iteration.) There will be no other worker peer
|
|
|
|
## functions activated simultaneously.
|
|
|
|
##
|
|
|
|
## This procedure is started if the global flag `buddy.ctx.poolMode` is set
|
|
|
|
## `true` (default is `false`.) It is the responsibility of the `runPool()`
|
|
|
|
## instance to reset the flag `buddy.ctx.poolMode`, typically at the first
|
2022-08-24 13:44:18 +00:00
|
|
|
## peer instance.
|
|
|
|
##
|
|
|
|
## The argument `last` is set `true` if the last entry is reached.
|
2022-07-21 12:14:41 +00:00
|
|
|
##
|
|
|
|
## Note that this function does not run in `async` mode.
|
|
|
|
##
|
2022-09-30 08:22:14 +00:00
|
|
|
let
|
|
|
|
ctx = buddy.ctx
|
|
|
|
bq = buddy.data.bQueue
|
2022-08-04 08:04:30 +00:00
|
|
|
if ctx.poolMode:
|
2022-07-21 12:14:41 +00:00
|
|
|
# Mind the gap, fill in if necessary
|
2022-09-30 08:22:14 +00:00
|
|
|
bq.blockQueueGrout()
|
2022-08-04 08:04:30 +00:00
|
|
|
ctx.poolMode = false
|
2022-07-21 12:14:41 +00:00
|
|
|
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc runMulti*(buddy: FullBuddyRef) {.async.} =
|
2022-07-21 12:14:41 +00:00
|
|
|
## This peer worker is invoked if the `buddy.ctrl.multiOk` flag is set
|
|
|
|
## `true` which is typically done after finishing `runSingle()`. This
|
|
|
|
## instance can be simultaneously active for all peer workers.
|
|
|
|
##
|
|
|
|
# Fetch work item
|
2022-08-04 08:04:30 +00:00
|
|
|
let
|
|
|
|
ctx = buddy.ctx
|
2022-09-30 08:22:14 +00:00
|
|
|
bq = buddy.data.bQueue
|
|
|
|
rc = await bq.blockQueueWorker()
|
2022-07-21 12:14:41 +00:00
|
|
|
if rc.isErr:
|
2022-09-30 08:22:14 +00:00
|
|
|
if rc.error == StagedQueueOverflow:
|
|
|
|
# Mind the gap: Turn on pool mode if there are too may staged items.
|
|
|
|
buddy.ctx.poolMode = true
|
|
|
|
else:
|
2022-07-21 12:14:41 +00:00
|
|
|
return
|
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
# Update persistent database
|
|
|
|
while buddy.processStaged() and not buddy.ctrl.stopped:
|
|
|
|
# Allow thread switch as `persistBlocks()` might be slow
|
|
|
|
await sleepAsync(10.milliseconds)
|
2022-07-21 12:14:41 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|