2022-11-16 23:51:06 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed
|
|
|
|
# except according to those terms.
|
|
|
|
|
2023-03-02 09:57:58 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2022-11-16 23:51:06 +00:00
|
|
|
import
|
2023-01-30 17:50:58 +00:00
|
|
|
std/[math, sets, sequtils],
|
2023-01-17 09:28:14 +00:00
|
|
|
chronicles,
|
2022-11-16 23:51:06 +00:00
|
|
|
chronos,
|
2023-01-17 09:28:14 +00:00
|
|
|
eth/[common, p2p, trie/trie_defs],
|
2022-12-12 22:00:24 +00:00
|
|
|
stew/[interval_set, keyed_queue, sorted_set],
|
2022-11-16 23:51:06 +00:00
|
|
|
../../sync_desc,
|
|
|
|
".."/[constants, range_desc, worker_desc],
|
2022-11-25 14:56:42 +00:00
|
|
|
./db/[hexary_error, snapdb_accounts, snapdb_pivot],
|
2022-12-19 21:22:09 +00:00
|
|
|
./pivot/[heal_accounts, heal_storage_slots,
|
2022-12-24 09:54:18 +00:00
|
|
|
range_fetch_accounts, range_fetch_storage_slots,
|
|
|
|
storage_queue_helper],
|
2022-12-12 22:00:24 +00:00
|
|
|
./ticker
|
2022-11-16 23:51:06 +00:00
|
|
|
|
2023-01-17 09:28:14 +00:00
|
|
|
logScope:
|
|
|
|
topics = "snap-pivot"
|
|
|
|
|
2022-11-16 23:51:06 +00:00
|
|
|
const
|
2023-01-17 09:28:14 +00:00
|
|
|
extraTraceMessages = false or true
|
|
|
|
## Enabled additional logging noise
|
2022-11-16 23:51:06 +00:00
|
|
|
|
2022-12-12 22:00:24 +00:00
|
|
|
proc pivotMothball*(env: SnapPivotRef) {.gcsafe.}
|
|
|
|
|
2022-11-16 23:51:06 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-12-24 09:54:18 +00:00
|
|
|
proc accountsHealingOk(
|
|
|
|
env: SnapPivotRef; # Current pivot environment
|
|
|
|
ctx: SnapCtxRef; # Some global context
|
|
|
|
): bool =
|
|
|
|
## Returns `true` if accounts healing is enabled for this pivot.
|
|
|
|
not env.fetchAccounts.processed.isEmpty and
|
|
|
|
healAccountsCoverageTrigger <= ctx.pivotAccountsCoverage()
|
|
|
|
|
|
|
|
|
2022-12-09 13:43:55 +00:00
|
|
|
proc init(
|
2023-01-17 09:28:14 +00:00
|
|
|
T: type SnapRangeBatchRef; # Collection of sets of account ranges
|
|
|
|
ctx: SnapCtxRef; # Some global context
|
|
|
|
): T =
|
|
|
|
## Account ranges constructor
|
|
|
|
new result
|
|
|
|
result.unprocessed.init() # full range on the first set of the pair
|
|
|
|
result.processed = NodeTagRangeSet.init()
|
2022-11-16 23:51:06 +00:00
|
|
|
|
2023-01-17 09:28:14 +00:00
|
|
|
# Update coverage level roll over
|
|
|
|
ctx.pivotAccountsCoverage100PcRollOver()
|
2022-12-19 21:22:09 +00:00
|
|
|
|
2023-01-17 09:28:14 +00:00
|
|
|
# Initialise accounts range fetch batch, the pair of `fetchAccounts[]` range
|
|
|
|
# sets. Deprioritise already processed ranges by moving it to the second set.
|
2023-02-23 13:13:02 +00:00
|
|
|
for iv in ctx.pool.coveredAccounts.increasing:
|
2023-01-17 09:28:14 +00:00
|
|
|
discard result.unprocessed[0].reduce iv
|
|
|
|
discard result.unprocessed[1].merge iv
|
2022-11-16 23:51:06 +00:00
|
|
|
|
2023-01-17 09:28:14 +00:00
|
|
|
proc init(
|
|
|
|
T: type SnapPivotRef; # Privot descriptor type
|
|
|
|
ctx: SnapCtxRef; # Some global context
|
|
|
|
header: BlockHeader; # Header to generate new pivot from
|
|
|
|
): T =
|
|
|
|
## Pivot constructor.
|
|
|
|
result = T(
|
|
|
|
stateHeader: header,
|
|
|
|
fetchAccounts: SnapRangeBatchRef.init(ctx))
|
|
|
|
result.storageAccounts.init()
|
2022-11-16 23:51:06 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions: pivot table related
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc beforeTopMostlyClean*(pivotTable: var SnapPivotTable) =
|
|
|
|
## Clean up pivot queues of the entry before the top one. The queues are
|
|
|
|
## the pivot data that need most of the memory. This cleaned pivot is not
|
|
|
|
## usable any more after cleaning but might be useful as historic record.
|
|
|
|
let rc = pivotTable.beforeLastValue
|
|
|
|
if rc.isOk:
|
2022-12-12 22:00:24 +00:00
|
|
|
rc.value.pivotMothball
|
2022-11-16 23:51:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc topNumber*(pivotTable: var SnapPivotTable): BlockNumber =
|
2022-11-25 14:56:42 +00:00
|
|
|
## Return the block number of the top pivot entry, or zero if there is none.
|
2022-11-16 23:51:06 +00:00
|
|
|
let rc = pivotTable.lastValue
|
|
|
|
if rc.isOk:
|
|
|
|
return rc.value.stateHeader.blockNumber
|
|
|
|
|
|
|
|
|
2023-01-17 09:28:14 +00:00
|
|
|
proc reverseUpdate*(
|
2022-12-12 22:00:24 +00:00
|
|
|
pivotTable: var SnapPivotTable; # Pivot table
|
|
|
|
header: BlockHeader; # Header to generate new pivot from
|
|
|
|
ctx: SnapCtxRef; # Some global context
|
2022-11-16 23:51:06 +00:00
|
|
|
) =
|
2023-01-17 09:28:14 +00:00
|
|
|
## Activate environment for earlier state root implied by `header` argument.
|
2022-11-16 23:51:06 +00:00
|
|
|
##
|
|
|
|
## Note that the pivot table is assumed to be sorted by the block numbers of
|
|
|
|
## the pivot header.
|
|
|
|
##
|
2023-01-17 09:28:14 +00:00
|
|
|
# Append per-state root environment to LRU queue
|
|
|
|
discard pivotTable.prepend(
|
|
|
|
header.stateRoot, SnapPivotRef.init(ctx, header))
|
|
|
|
|
|
|
|
# Make sure that the LRU table does not grow too big.
|
|
|
|
if max(3, ctx.buddiesMax) < pivotTable.len:
|
|
|
|
# Delete second entry rather than the first which might currently
|
|
|
|
# be needed.
|
|
|
|
let rc = pivotTable.secondKey
|
|
|
|
if rc.isOk:
|
|
|
|
pivotTable.del rc.value
|
2022-11-16 23:51:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc tickerStats*(
|
2022-12-12 22:00:24 +00:00
|
|
|
pivotTable: var SnapPivotTable; # Pivot table
|
|
|
|
ctx: SnapCtxRef; # Some global context
|
2022-11-16 23:51:06 +00:00
|
|
|
): TickerStatsUpdater =
|
|
|
|
## This function returns a function of type `TickerStatsUpdater` that prints
|
|
|
|
## out pivot table statitics. The returned fuction is supposed to drive
|
|
|
|
## ticker` module.
|
|
|
|
proc meanStdDev(sum, sqSum: float; length: int): (float,float) =
|
|
|
|
if 0 < length:
|
|
|
|
result[0] = sum / length.float
|
|
|
|
result[1] = sqrt(sqSum / length.float - result[0] * result[0])
|
|
|
|
|
2023-01-26 12:37:19 +00:00
|
|
|
result = proc: SnapTickerStats =
|
2022-11-16 23:51:06 +00:00
|
|
|
var
|
|
|
|
aSum, aSqSum, uSum, uSqSum, sSum, sSqSum: float
|
|
|
|
count = 0
|
2023-02-23 13:13:02 +00:00
|
|
|
for kvp in ctx.pool.pivotTable.nextPairs:
|
2022-11-16 23:51:06 +00:00
|
|
|
|
|
|
|
# Accounts mean & variance
|
|
|
|
let aLen = kvp.data.nAccounts.float
|
|
|
|
if 0 < aLen:
|
|
|
|
count.inc
|
|
|
|
aSum += aLen
|
|
|
|
aSqSum += aLen * aLen
|
|
|
|
|
|
|
|
# Fill utilisation mean & variance
|
2022-11-25 14:56:42 +00:00
|
|
|
let fill = kvp.data.fetchAccounts.processed.fullFactor
|
2022-11-16 23:51:06 +00:00
|
|
|
uSum += fill
|
|
|
|
uSqSum += fill * fill
|
|
|
|
|
|
|
|
let sLen = kvp.data.nSlotLists.float
|
|
|
|
sSum += sLen
|
|
|
|
sSqSum += sLen * sLen
|
|
|
|
let
|
2023-02-23 13:13:02 +00:00
|
|
|
env = ctx.pool.pivotTable.lastValue.get(otherwise = nil)
|
|
|
|
accCoverage = (ctx.pool.coveredAccounts.fullFactor +
|
|
|
|
ctx.pool.covAccTimesFull.float)
|
2022-11-16 23:51:06 +00:00
|
|
|
accFill = meanStdDev(uSum, uSqSum, count)
|
|
|
|
var
|
2023-01-17 09:28:14 +00:00
|
|
|
beaconBlock = none(BlockNumber)
|
2022-11-16 23:51:06 +00:00
|
|
|
pivotBlock = none(BlockNumber)
|
|
|
|
stoQuLen = none(int)
|
2022-12-20 15:38:57 +00:00
|
|
|
procChunks = 0
|
2022-11-16 23:51:06 +00:00
|
|
|
if not env.isNil:
|
|
|
|
pivotBlock = some(env.stateHeader.blockNumber)
|
2022-12-20 15:38:57 +00:00
|
|
|
procChunks = env.fetchAccounts.processed.chunks
|
2022-12-24 09:54:18 +00:00
|
|
|
stoQuLen = some(env.storageQueueTotal())
|
2023-02-23 13:13:02 +00:00
|
|
|
if 0 < ctx.pool.beaconHeader.blockNumber:
|
|
|
|
beaconBlock = some(ctx.pool.beaconHeader.blockNumber)
|
2022-11-16 23:51:06 +00:00
|
|
|
|
2023-01-26 12:37:19 +00:00
|
|
|
SnapTickerStats(
|
2023-01-17 09:28:14 +00:00
|
|
|
beaconBlock: beaconBlock,
|
2022-11-16 23:51:06 +00:00
|
|
|
pivotBlock: pivotBlock,
|
2023-02-23 13:13:02 +00:00
|
|
|
nQueues: ctx.pool.pivotTable.len,
|
2022-11-16 23:51:06 +00:00
|
|
|
nAccounts: meanStdDev(aSum, aSqSum, count),
|
|
|
|
nSlotLists: meanStdDev(sSum, sSqSum, count),
|
|
|
|
accountsFill: (accFill[0], accFill[1], accCoverage),
|
2022-12-20 15:38:57 +00:00
|
|
|
nAccountStats: procChunks,
|
2022-11-16 23:51:06 +00:00
|
|
|
nStorageQueue: stoQuLen)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions: particular pivot
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-12-12 22:00:24 +00:00
|
|
|
proc pivotMothball*(env: SnapPivotRef) =
|
|
|
|
## Clean up most of this argument `env` pivot record and mark it `archived`.
|
|
|
|
## Note that archived pivots will be checked for swapping in already known
|
|
|
|
## accounts and storage slots.
|
|
|
|
env.fetchAccounts.unprocessed.init()
|
|
|
|
|
|
|
|
# Simplify storage slots queues by resolving partial slots into full list
|
|
|
|
for kvp in env.fetchStoragePart.nextPairs:
|
|
|
|
discard env.fetchStorageFull.append(
|
|
|
|
kvp.key, SnapSlotsQueueItemRef(acckey: kvp.data.accKey))
|
|
|
|
env.fetchStoragePart.clear()
|
|
|
|
|
|
|
|
# Provide index into `fetchStorageFull`
|
|
|
|
env.storageAccounts.clear()
|
|
|
|
for kvp in env.fetchStorageFull.nextPairs:
|
|
|
|
let rc = env.storageAccounts.insert(kvp.data.accKey.to(NodeTag))
|
|
|
|
# Note that `rc.isErr` should not exist as accKey => storageRoot
|
|
|
|
if rc.isOk:
|
|
|
|
rc.value.data = kvp.key
|
|
|
|
|
|
|
|
# Finally, mark that node `archived`
|
|
|
|
env.archived = true
|
|
|
|
|
|
|
|
|
2022-12-19 21:22:09 +00:00
|
|
|
proc execSnapSyncAction*(
|
|
|
|
env: SnapPivotRef; # Current pivot environment
|
|
|
|
buddy: SnapBuddyRef; # Worker peer
|
|
|
|
) {.async.} =
|
|
|
|
## Execute a synchronisation run.
|
|
|
|
let
|
|
|
|
ctx = buddy.ctx
|
|
|
|
|
|
|
|
block:
|
|
|
|
# Clean up storage slots queue first it it becomes too large
|
|
|
|
let nStoQu = env.fetchStorageFull.len + env.fetchStoragePart.len
|
2022-12-24 09:54:18 +00:00
|
|
|
if storageSlotsQuPrioThresh < nStoQu:
|
2022-12-19 21:22:09 +00:00
|
|
|
await buddy.rangeFetchStorageSlots(env)
|
|
|
|
if buddy.ctrl.stopped or env.archived:
|
|
|
|
return
|
|
|
|
|
|
|
|
if not env.fetchAccounts.processed.isFull:
|
|
|
|
await buddy.rangeFetchAccounts(env)
|
|
|
|
|
2022-12-24 09:54:18 +00:00
|
|
|
# Update 100% accounting
|
2023-01-17 09:28:14 +00:00
|
|
|
ctx.pivotAccountsCoverage100PcRollOver()
|
2022-12-24 09:54:18 +00:00
|
|
|
|
2022-12-19 21:22:09 +00:00
|
|
|
# Run at least one round fetching storage slosts even if the `archived`
|
|
|
|
# flag is set in order to keep the batch queue small.
|
|
|
|
if not buddy.ctrl.stopped:
|
|
|
|
await buddy.rangeFetchStorageSlots(env)
|
|
|
|
|
|
|
|
if buddy.ctrl.stopped or env.archived:
|
|
|
|
return
|
|
|
|
|
2022-12-24 09:54:18 +00:00
|
|
|
if env.accountsHealingOk(ctx):
|
2022-12-19 21:22:09 +00:00
|
|
|
await buddy.healAccounts(env)
|
|
|
|
if buddy.ctrl.stopped or env.archived:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Some additional storage slots might have been popped up
|
|
|
|
await buddy.rangeFetchStorageSlots(env)
|
|
|
|
if buddy.ctrl.stopped or env.archived:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Don't bother with storage slots healing before accounts healing takes
|
|
|
|
# place. This saves communication bandwidth. The pivot might change soon,
|
|
|
|
# anyway.
|
2022-12-24 09:54:18 +00:00
|
|
|
if env.accountsHealingOk(ctx):
|
2022-12-19 21:22:09 +00:00
|
|
|
await buddy.healStorageSlots(env)
|
2022-11-28 09:03:23 +00:00
|
|
|
|
|
|
|
|
2022-11-25 14:56:42 +00:00
|
|
|
proc saveCheckpoint*(
|
2022-12-12 22:00:24 +00:00
|
|
|
env: SnapPivotRef; # Current pivot environment
|
|
|
|
ctx: SnapCtxRef; # Some global context
|
2022-11-28 09:03:23 +00:00
|
|
|
): Result[int,HexaryError] =
|
2022-11-16 23:51:06 +00:00
|
|
|
## Save current sync admin data. On success, the size of the data record
|
|
|
|
## saved is returned (e.g. for logging.)
|
2022-11-25 14:56:42 +00:00
|
|
|
##
|
|
|
|
let
|
|
|
|
fa = env.fetchAccounts
|
2022-12-24 09:54:18 +00:00
|
|
|
nStoQu = env.storageQueueTotal()
|
2022-11-25 14:56:42 +00:00
|
|
|
|
2023-01-17 09:28:14 +00:00
|
|
|
if fa.processed.isEmpty:
|
|
|
|
return err(NoAccountsYet)
|
|
|
|
|
2022-12-24 09:54:18 +00:00
|
|
|
if accountsSaveProcessedChunksMax < fa.processed.chunks:
|
2022-11-25 14:56:42 +00:00
|
|
|
return err(TooManyProcessedChunks)
|
2022-11-16 23:51:06 +00:00
|
|
|
|
2022-12-24 09:54:18 +00:00
|
|
|
if accountsSaveStorageSlotsMax < nStoQu:
|
2022-11-16 23:51:06 +00:00
|
|
|
return err(TooManySlotAccounts)
|
|
|
|
|
2023-02-23 13:13:02 +00:00
|
|
|
ctx.pool.snapDb.savePivot SnapDbPivotRegistry(
|
2022-11-25 14:56:42 +00:00
|
|
|
header: env.stateHeader,
|
|
|
|
nAccounts: env.nAccounts,
|
|
|
|
nSlotLists: env.nSlotLists,
|
|
|
|
processed: toSeq(env.fetchAccounts.processed.increasing)
|
|
|
|
.mapIt((it.minPt,it.maxPt)),
|
|
|
|
slotAccounts: (toSeq(env.fetchStorageFull.nextKeys) &
|
2022-12-24 09:54:18 +00:00
|
|
|
toSeq(env.fetchStoragePart.nextKeys)).mapIt(it.to(NodeKey)) &
|
|
|
|
toSeq(env.parkedStorage.items))
|
2022-11-16 23:51:06 +00:00
|
|
|
|
|
|
|
|
2022-11-25 14:56:42 +00:00
|
|
|
proc recoverPivotFromCheckpoint*(
|
2022-12-12 22:00:24 +00:00
|
|
|
env: SnapPivotRef; # Current pivot environment
|
|
|
|
ctx: SnapCtxRef; # Global context (containing save state)
|
|
|
|
topLevel: bool; # Full data set on top level only
|
2022-11-25 14:56:42 +00:00
|
|
|
) =
|
|
|
|
## Recover some pivot variables and global list `coveredAccounts` from
|
|
|
|
## checkpoint data. If the argument `toplevel` is set `true`, also the
|
|
|
|
## `processed`, `unprocessed`, and the `fetchStorageFull` lists are
|
|
|
|
## initialised.
|
|
|
|
##
|
2023-02-23 13:13:02 +00:00
|
|
|
let recov = ctx.pool.recovery
|
2022-11-25 14:56:42 +00:00
|
|
|
if recov.isNil:
|
|
|
|
return
|
|
|
|
|
|
|
|
env.nAccounts = recov.state.nAccounts
|
|
|
|
env.nSlotLists = recov.state.nSlotLists
|
|
|
|
|
|
|
|
# Import processed interval
|
|
|
|
for (minPt,maxPt) in recov.state.processed:
|
|
|
|
if topLevel:
|
|
|
|
env.fetchAccounts.unprocessed.reduce(minPt, maxPt)
|
2022-12-12 22:00:24 +00:00
|
|
|
discard env.fetchAccounts.processed.merge(minPt, maxPt)
|
2023-02-23 13:13:02 +00:00
|
|
|
discard ctx.pool.coveredAccounts.merge(minPt, maxPt)
|
2023-01-17 09:28:14 +00:00
|
|
|
ctx.pivotAccountsCoverage100PcRollOver() # update coverage level roll over
|
2022-11-25 14:56:42 +00:00
|
|
|
|
|
|
|
# Handle storage slots
|
2022-12-12 22:00:24 +00:00
|
|
|
let stateRoot = recov.state.header.stateRoot
|
|
|
|
for w in recov.state.slotAccounts:
|
2022-12-19 21:22:09 +00:00
|
|
|
let pt = NodeTagRange.new(w.to(NodeTag),w.to(NodeTag)) # => `pt.len == 1`
|
2022-12-12 22:00:24 +00:00
|
|
|
|
|
|
|
if 0 < env.fetchAccounts.processed.covered(pt):
|
|
|
|
# Ignoring slots that have accounts to be downloaded, anyway
|
2023-02-23 13:13:02 +00:00
|
|
|
let rc = ctx.pool.snapDb.getAccountsData(stateRoot, w)
|
2022-12-12 22:00:24 +00:00
|
|
|
if rc.isErr:
|
|
|
|
# Oops, how did that account get lost?
|
|
|
|
discard env.fetchAccounts.processed.reduce pt
|
|
|
|
env.fetchAccounts.unprocessed.merge pt
|
|
|
|
elif rc.value.storageRoot != emptyRlpHash:
|
2022-12-24 09:54:18 +00:00
|
|
|
env.storageQueueAppendFull(rc.value.storageRoot, w)
|
2022-12-12 22:00:24 +00:00
|
|
|
|
|
|
|
# Handle mothballed pivots for swapping in (see `pivotMothball()`)
|
|
|
|
if not topLevel:
|
|
|
|
for kvp in env.fetchStorageFull.nextPairs:
|
|
|
|
let rc = env.storageAccounts.insert(kvp.data.accKey.to(NodeTag))
|
|
|
|
if rc.isOk:
|
|
|
|
rc.value.data = kvp.key
|
|
|
|
env.archived = true
|
2022-11-16 23:51:06 +00:00
|
|
|
|
2023-01-17 09:28:14 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public function, manage new peer and pivot update
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc pivotApprovePeer*(buddy: SnapBuddyRef) {.async.} =
|
|
|
|
## Approve peer and update pivot. On failure, the `buddy` will be stopped so
|
|
|
|
## it will not proceed to the next scheduler task.
|
|
|
|
let
|
|
|
|
ctx = buddy.ctx
|
2023-01-30 22:10:23 +00:00
|
|
|
peer {.used.} = buddy.peer
|
2023-02-23 13:13:02 +00:00
|
|
|
beaconHeader = ctx.pool.beaconHeader
|
2023-01-17 09:28:14 +00:00
|
|
|
var
|
|
|
|
pivotHeader: BlockHeader
|
|
|
|
|
|
|
|
block:
|
2023-02-23 13:13:02 +00:00
|
|
|
let rc = ctx.pool.pivotTable.lastValue
|
2023-01-17 09:28:14 +00:00
|
|
|
if rc.isOk:
|
|
|
|
pivotHeader = rc.value.stateHeader
|
|
|
|
|
|
|
|
# Check whether the pivot needs to be updated
|
2023-01-18 08:31:57 +00:00
|
|
|
if pivotHeader.blockNumber + pivotBlockDistanceMin < beaconHeader.blockNumber:
|
|
|
|
# If the entry before the previous entry is unused, then run a pool mode
|
|
|
|
# based session (which should enable a pivot table purge).
|
|
|
|
block:
|
2023-02-23 13:13:02 +00:00
|
|
|
let rc = ctx.pool.pivotTable.beforeLast
|
2023-01-18 08:31:57 +00:00
|
|
|
if rc.isOk and rc.value.data.fetchAccounts.processed.isEmpty:
|
|
|
|
ctx.poolMode = true
|
2023-01-17 09:28:14 +00:00
|
|
|
|
|
|
|
when extraTraceMessages:
|
2023-01-18 08:31:57 +00:00
|
|
|
trace "New pivot from beacon chain", peer,
|
|
|
|
pivot=("#" & $pivotHeader.blockNumber),
|
|
|
|
beacon=("#" & $beaconHeader.blockNumber), poolMode=ctx.poolMode
|
2023-01-17 09:28:14 +00:00
|
|
|
|
2023-02-23 13:13:02 +00:00
|
|
|
discard ctx.pool.pivotTable.lruAppend(
|
2023-01-18 08:31:57 +00:00
|
|
|
beaconHeader.stateRoot, SnapPivotRef.init(ctx, beaconHeader),
|
|
|
|
pivotTableLruEntriesMax)
|
2023-01-17 09:28:14 +00:00
|
|
|
|
2023-01-18 08:31:57 +00:00
|
|
|
pivotHeader = beaconHeader
|
2023-01-17 09:28:14 +00:00
|
|
|
|
2023-01-18 08:31:57 +00:00
|
|
|
# Not ready yet?
|
|
|
|
if pivotHeader.blockNumber == 0:
|
|
|
|
buddy.ctrl.stopped = true
|
2023-01-17 09:28:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc pivotUpdateBeaconHeaderCB*(ctx: SnapCtxRef): SyncReqNewHeadCB =
|
|
|
|
## Update beacon header. This function is intended as a call back function
|
|
|
|
## for the RPC module.
|
2023-01-18 08:31:57 +00:00
|
|
|
result = proc(h: BlockHeader) {.gcsafe.} =
|
2023-02-23 13:13:02 +00:00
|
|
|
if ctx.pool.beaconHeader.blockNumber < h.blockNumber:
|
2023-01-18 08:31:57 +00:00
|
|
|
# when extraTraceMessages:
|
|
|
|
# trace "External beacon info update", header=("#" & $h.blockNumber)
|
2023-02-23 13:13:02 +00:00
|
|
|
ctx.pool.beaconHeader = h
|
2023-01-17 09:28:14 +00:00
|
|
|
|
2022-11-16 23:51:06 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|