2022-08-04 08:04:30 +00:00
|
|
|
# Nimbus
|
2022-05-09 14:04:48 +00:00
|
|
|
# Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
2022-05-13 16:30:10 +00:00
|
|
|
# * 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.
|
2022-05-09 14:04:48 +00:00
|
|
|
|
|
|
|
import
|
2022-11-16 23:51:06 +00:00
|
|
|
std/[hashes, options, sets, strutils],
|
2022-05-17 11:09:49 +00:00
|
|
|
chronicles,
|
|
|
|
chronos,
|
2022-11-01 15:07:44 +00:00
|
|
|
eth/[common, p2p],
|
2022-08-04 08:04:30 +00:00
|
|
|
stew/[interval_set, keyed_queue],
|
2022-08-12 15:42:07 +00:00
|
|
|
../../db/select_backend,
|
2022-11-25 14:56:42 +00:00
|
|
|
../../utils/prettify,
|
|
|
|
../misc/best_pivot,
|
|
|
|
".."/[protocol, sync_desc],
|
2022-11-16 23:51:06 +00:00
|
|
|
./worker/[pivot_helper, ticker],
|
Prep for full sync after snap make 4 (#1282)
* Re-arrange fetching storage slots in batch module
why;
Previously, fetching partial slot ranges first has a chance of
terminating the worker peer 9due to network error) while there were
many inheritable storage slots on the queue.
Now, inheritance is checked first, then full slot ranges and finally
partial ranges.
* Update logging
* Bundled node information for healing into single object `NodeSpecs`
why:
Previously, partial paths and node keys were kept in separate variables.
This approach was error prone due to copying/reassembling function
argument objects.
As all partial paths, keys, and node data types are more or less handled
as `Blob`s over the network (using Eth/6x, or Snap/1) it makes sense to
hold these `Blob`s as named field in a single object (even if not all
fields are active for the current purpose.)
* For good housekeeping, using `NodeKey` type only for account keys
why:
previously, a mixture of `NodeKey` and `Hash256` was used. Now, only
state or storage root keys use the `Hash256` type.
* Always accept latest pivot (and not a slightly older one)
why;
For testing it was tried to use a slightly older pivot state root than
available. Some anecdotal tests seemed to suggest an advantage so that
more peers are willing to serve on that older pivot. But this could not
be confirmed in subsequent tests (still anecdotal, though.)
As a side note, the distance of the latest pivot to its predecessor is
at least 128 (or whatever the constant `minPivotBlockDistance` is
assigned to.)
* Reshuffle name components for some file and function names
why:
Clarifies purpose:
"storages" becomes: "storage slots"
"store" becomes: "range fetch"
* Stash away currently unused modules in sub-folder named "notused"
2022-10-27 13:49:28 +00:00
|
|
|
./worker/com/com_error,
|
2022-11-28 09:03:23 +00:00
|
|
|
./worker/db/[hexary_desc, snapdb_desc, snapdb_pivot],
|
2022-11-01 15:07:44 +00:00
|
|
|
"."/[constants, range_desc, worker_desc]
|
2022-05-24 08:07:39 +00:00
|
|
|
|
2022-09-16 07:24:12 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2022-05-23 16:53:19 +00:00
|
|
|
logScope:
|
2022-11-01 15:07:44 +00:00
|
|
|
topics = "snap-buddy"
|
2022-05-23 16:53:19 +00:00
|
|
|
|
2022-10-08 17:20:50 +00:00
|
|
|
const
|
|
|
|
extraTraceMessages = false or true
|
|
|
|
## Enabled additional logging noise
|
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2022-10-08 17:20:50 +00:00
|
|
|
# Private helpers: integration of pivot finder
|
2022-09-30 08:22:14 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-10-19 14:03:55 +00:00
|
|
|
proc pivot(ctx: SnapCtxRef): BestPivotCtxRef =
|
2022-10-08 17:20:50 +00:00
|
|
|
# Getter
|
2022-10-19 14:03:55 +00:00
|
|
|
ctx.data.pivotFinderCtx.BestPivotCtxRef
|
2022-09-30 08:22:14 +00:00
|
|
|
|
2022-10-19 14:03:55 +00:00
|
|
|
proc `pivot=`(ctx: SnapCtxRef; val: BestPivotCtxRef) =
|
2022-10-08 17:20:50 +00:00
|
|
|
# Setter
|
|
|
|
ctx.data.pivotFinderCtx = val
|
2022-09-30 08:22:14 +00:00
|
|
|
|
2022-10-19 14:03:55 +00:00
|
|
|
proc pivot(buddy: SnapBuddyRef): BestPivotWorkerRef =
|
2022-10-08 17:20:50 +00:00
|
|
|
# Getter
|
2022-10-19 14:03:55 +00:00
|
|
|
buddy.data.pivotFinder.BestPivotWorkerRef
|
2022-09-30 08:22:14 +00:00
|
|
|
|
2022-10-19 14:03:55 +00:00
|
|
|
proc `pivot=`(buddy: SnapBuddyRef; val: BestPivotWorkerRef) =
|
2022-10-08 17:20:50 +00:00
|
|
|
# Setter
|
|
|
|
buddy.data.pivotFinder = val
|
2022-09-30 08:22:14 +00:00
|
|
|
|
2022-10-08 17:20:50 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-09-30 08:22:14 +00:00
|
|
|
|
2022-11-25 14:56:42 +00:00
|
|
|
proc recoveryStepContinue(ctx: SnapCtxRef): Future[bool] {.async.} =
|
|
|
|
let recov = ctx.data.recovery
|
|
|
|
if recov.isNil:
|
|
|
|
return false
|
|
|
|
|
|
|
|
let
|
|
|
|
checkpoint =
|
|
|
|
"#" & $recov.state.header.blockNumber & "(" & $recov.level & ")"
|
|
|
|
topLevel = recov.level == 0
|
|
|
|
env = block:
|
|
|
|
let rc = ctx.data.pivotTable.eq recov.state.header.stateRoot
|
|
|
|
if rc.isErr:
|
|
|
|
error "Recovery pivot context gone", checkpoint, topLevel
|
|
|
|
return false
|
|
|
|
rc.value
|
|
|
|
|
|
|
|
# Cosmetics: allows other processes to log etc.
|
|
|
|
await sleepAsync(1300.milliseconds)
|
|
|
|
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace "Recovery continued ...", checkpoint, topLevel,
|
|
|
|
nAccounts=recov.state.nAccounts, nDangling=recov.state.dangling.len
|
|
|
|
|
|
|
|
# Update pivot data from recovery checkpoint
|
|
|
|
env.recoverPivotFromCheckpoint(ctx, topLevel)
|
|
|
|
|
|
|
|
# Fetch next recovery record if there is any
|
|
|
|
if recov.state.predecessor.isZero:
|
|
|
|
trace "Recovery done", checkpoint, topLevel
|
|
|
|
return false
|
|
|
|
let rc = ctx.data.snapDb.recoverPivot(recov.state.predecessor)
|
|
|
|
if rc.isErr:
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace "Recovery stopped at pivot stale checkpoint", checkpoint, topLevel
|
|
|
|
return false
|
|
|
|
|
|
|
|
# Set up next level pivot checkpoint
|
|
|
|
ctx.data.recovery = SnapRecoveryRef(
|
|
|
|
state: rc.value,
|
|
|
|
level: recov.level + 1)
|
|
|
|
|
|
|
|
# Push onto pivot table and continue recovery (i.e. do not stop it yet)
|
|
|
|
ctx.data.pivotTable.update(
|
|
|
|
ctx.data.recovery.state.header, ctx, reverse=true)
|
|
|
|
|
|
|
|
return true # continue recovery
|
|
|
|
|
|
|
|
|
2022-10-19 14:03:55 +00:00
|
|
|
proc updateSinglePivot(buddy: SnapBuddyRef): Future[bool] {.async.} =
|
2022-10-08 17:20:50 +00:00
|
|
|
## Helper, negotiate pivot unless present
|
|
|
|
if buddy.pivot.pivotHeader.isOk:
|
|
|
|
return true
|
2022-06-06 13:42:08 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
let
|
|
|
|
ctx = buddy.ctx
|
2022-10-08 17:20:50 +00:00
|
|
|
peer = buddy.peer
|
|
|
|
env = ctx.data.pivotTable.lastValue.get(otherwise = nil)
|
|
|
|
nMin = if env.isNil: none(BlockNumber)
|
|
|
|
else: some(env.stateHeader.blockNumber)
|
|
|
|
|
|
|
|
if await buddy.pivot.pivotNegotiate(nMin):
|
|
|
|
var header = buddy.pivot.pivotHeader.value
|
|
|
|
|
|
|
|
# Check whether there is no environment change needed
|
2022-11-01 15:07:44 +00:00
|
|
|
when pivotEnvStopChangingIfComplete:
|
2022-10-08 17:20:50 +00:00
|
|
|
let rc = ctx.data.pivotTable.lastValue
|
2022-11-01 15:07:44 +00:00
|
|
|
if rc.isOk and rc.value.storageDone:
|
2022-10-08 17:20:50 +00:00
|
|
|
# No neede to change
|
|
|
|
if extraTraceMessages:
|
|
|
|
trace "No need to change snap pivot", peer,
|
|
|
|
pivot=("#" & $rc.value.stateHeader.blockNumber),
|
2022-11-01 15:07:44 +00:00
|
|
|
stateRoot=rc.value.stateHeader.stateRoot,
|
2022-10-08 17:20:50 +00:00
|
|
|
multiOk=buddy.ctrl.multiOk, runState=buddy.ctrl.state
|
|
|
|
return true
|
|
|
|
|
2022-11-16 23:51:06 +00:00
|
|
|
buddy.ctx.data.pivotTable.update(header, buddy.ctx)
|
2022-10-08 17:20:50 +00:00
|
|
|
|
2022-11-01 15:07:44 +00:00
|
|
|
info "Snap pivot initialised", peer, pivot=("#" & $header.blockNumber),
|
2022-10-08 17:20:50 +00:00
|
|
|
multiOk=buddy.ctrl.multiOk, runState=buddy.ctrl.state
|
|
|
|
|
2022-09-16 07:24:12 +00:00
|
|
|
return true
|
2022-08-04 08:04:30 +00:00
|
|
|
|
2022-06-06 13:42:08 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public start/stop and admin functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc setup*(ctx: SnapCtxRef; tickerOK: bool): bool =
|
2022-06-06 13:42:08 +00:00
|
|
|
## Global set up
|
2022-10-14 16:40:32 +00:00
|
|
|
ctx.data.coveredAccounts = NodeTagRangeSet.init()
|
2022-10-08 17:20:50 +00:00
|
|
|
ctx.data.snapDb =
|
2022-10-10 02:31:28 +00:00
|
|
|
if ctx.data.dbBackend.isNil: SnapDbRef.init(ctx.chain.db.db)
|
2022-10-08 17:20:50 +00:00
|
|
|
else: SnapDbRef.init(ctx.data.dbBackend)
|
2022-10-19 14:03:55 +00:00
|
|
|
ctx.pivot = BestPivotCtxRef.init(ctx.data.rng)
|
2022-11-01 15:07:44 +00:00
|
|
|
ctx.pivot.pivotRelaxedMode(enable = true)
|
2022-11-16 23:51:06 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
if tickerOK:
|
2022-11-16 23:51:06 +00:00
|
|
|
ctx.data.ticker = TickerRef.init(ctx.data.pivotTable.tickerStats(ctx))
|
2022-08-04 08:04:30 +00:00
|
|
|
else:
|
|
|
|
trace "Ticker is disabled"
|
2022-11-16 23:51:06 +00:00
|
|
|
|
2022-11-25 14:56:42 +00:00
|
|
|
# Check for recovery mode
|
|
|
|
if not ctx.data.noRecovery:
|
|
|
|
let rc = ctx.data.snapDb.recoverPivot()
|
|
|
|
if rc.isOk:
|
|
|
|
ctx.data.recovery = SnapRecoveryRef(state: rc.value)
|
|
|
|
ctx.daemon = true
|
|
|
|
|
|
|
|
# Set up early initial pivot
|
|
|
|
ctx.data.pivotTable.update(ctx.data.recovery.state.header, ctx)
|
|
|
|
trace "Recovery started",
|
|
|
|
checkpoint=("#" & $ctx.data.pivotTable.topNumber() & "(0)")
|
|
|
|
if not ctx.data.ticker.isNil:
|
|
|
|
ctx.data.ticker.startRecovery()
|
|
|
|
true
|
2022-08-04 08:04:30 +00:00
|
|
|
|
|
|
|
proc release*(ctx: SnapCtxRef) =
|
2022-06-06 13:42:08 +00:00
|
|
|
## Global clean up
|
2022-10-19 14:03:55 +00:00
|
|
|
ctx.pivot = nil
|
2022-08-04 08:04:30 +00:00
|
|
|
if not ctx.data.ticker.isNil:
|
|
|
|
ctx.data.ticker.stop()
|
|
|
|
ctx.data.ticker = nil
|
2022-06-16 08:58:50 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc start*(buddy: SnapBuddyRef): bool =
|
|
|
|
## Initialise worker peer
|
|
|
|
let
|
|
|
|
ctx = buddy.ctx
|
|
|
|
peer = buddy.peer
|
|
|
|
if peer.supports(protocol.snap) and
|
|
|
|
peer.supports(protocol.eth) and
|
|
|
|
peer.state(protocol.eth).initialized:
|
2022-10-19 14:03:55 +00:00
|
|
|
buddy.pivot = BestPivotWorkerRef.init(
|
|
|
|
buddy.ctx.pivot, buddy.ctrl, buddy.peer)
|
2022-10-08 17:20:50 +00:00
|
|
|
buddy.data.errors = ComErrorStatsRef()
|
2022-08-04 08:04:30 +00:00
|
|
|
if not ctx.data.ticker.isNil:
|
|
|
|
ctx.data.ticker.startBuddy()
|
2022-06-06 13:42:08 +00:00
|
|
|
return true
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc stop*(buddy: SnapBuddyRef) =
|
2022-06-06 13:42:08 +00:00
|
|
|
## Clean up this peer
|
2022-08-04 08:04:30 +00:00
|
|
|
let
|
|
|
|
ctx = buddy.ctx
|
|
|
|
peer = buddy.peer
|
|
|
|
buddy.ctrl.stopped = true
|
2022-10-19 14:03:55 +00:00
|
|
|
buddy.pivot.clear()
|
2022-08-04 08:04:30 +00:00
|
|
|
if not ctx.data.ticker.isNil:
|
|
|
|
ctx.data.ticker.stopBuddy()
|
2022-06-06 13:42:08 +00:00
|
|
|
|
2022-05-17 11:09:49 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-11-14 14:13:00 +00:00
|
|
|
proc runDaemon*(ctx: SnapCtxRef) {.async.} =
|
|
|
|
## Enabled while `ctx.daemon` is `true`
|
2022-05-09 14:04:48 +00:00
|
|
|
##
|
2022-11-25 14:56:42 +00:00
|
|
|
if not ctx.data.recovery.isNil:
|
|
|
|
if not await ctx.recoveryStepContinue():
|
|
|
|
# Done, stop recovery
|
|
|
|
ctx.data.recovery = nil
|
|
|
|
ctx.daemon = false
|
|
|
|
|
|
|
|
# Update logging
|
|
|
|
if not ctx.data.ticker.isNil:
|
|
|
|
ctx.data.ticker.stopRecovery()
|
|
|
|
return
|
|
|
|
|
2022-11-14 14:13:00 +00:00
|
|
|
|
|
|
|
proc runSingle*(buddy: SnapBuddyRef) {.async.} =
|
|
|
|
## Enabled while
|
|
|
|
## * `buddy.ctrl.multiOk` is `false`
|
|
|
|
## * `buddy.ctrl.poolMode` is `false`
|
2022-05-09 14:04:48 +00:00
|
|
|
##
|
2022-10-19 14:03:55 +00:00
|
|
|
let peer = buddy.peer
|
2022-11-25 14:56:42 +00:00
|
|
|
# Find pivot, probably relaxed mode enabled in `setup()`
|
2022-10-19 14:03:55 +00:00
|
|
|
if not await buddy.updateSinglePivot():
|
|
|
|
# Wait if needed, then return => repeat
|
|
|
|
if not buddy.ctrl.stopped:
|
|
|
|
await sleepAsync(2.seconds)
|
|
|
|
return
|
2022-10-08 17:20:50 +00:00
|
|
|
|
|
|
|
buddy.ctrl.multiOk = true
|
2022-05-09 14:04:48 +00:00
|
|
|
|
|
|
|
|
2022-11-25 14:56:42 +00:00
|
|
|
proc runPool*(buddy: SnapBuddyRef, last: bool): bool =
|
2022-11-14 14:13:00 +00:00
|
|
|
## Enabled when `buddy.ctrl.poolMode` is `true`
|
2022-08-04 08:04:30 +00:00
|
|
|
##
|
2022-08-24 13:44:18 +00:00
|
|
|
let ctx = buddy.ctx
|
2022-11-25 14:56:42 +00:00
|
|
|
ctx.poolMode = false
|
|
|
|
result = true
|
2022-10-08 17:20:50 +00:00
|
|
|
|
2022-05-17 11:09:49 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc runMulti*(buddy: SnapBuddyRef) {.async.} =
|
2022-11-14 14:13:00 +00:00
|
|
|
## Enabled while
|
|
|
|
## * `buddy.ctrl.multiOk` is `true`
|
|
|
|
## * `buddy.ctrl.poolMode` is `false`
|
2022-08-04 08:04:30 +00:00
|
|
|
##
|
|
|
|
let
|
|
|
|
ctx = buddy.ctx
|
|
|
|
peer = buddy.peer
|
2022-08-24 13:44:18 +00:00
|
|
|
|
2022-10-08 17:20:50 +00:00
|
|
|
# Set up current state root environment for accounts snapshot
|
2022-11-01 15:07:44 +00:00
|
|
|
let
|
|
|
|
env = block:
|
|
|
|
let rc = ctx.data.pivotTable.lastValue
|
|
|
|
if rc.isErr:
|
|
|
|
return # nothing to do
|
|
|
|
rc.value
|
|
|
|
pivot = "#" & $env.stateHeader.blockNumber # for logging
|
2022-09-16 07:24:12 +00:00
|
|
|
|
2022-10-08 17:20:50 +00:00
|
|
|
buddy.data.pivotEnv = env
|
|
|
|
|
2022-11-01 15:07:44 +00:00
|
|
|
# Full sync processsing based on current snapshot
|
|
|
|
# -----------------------------------------------
|
|
|
|
if env.storageDone:
|
|
|
|
trace "Snap full sync -- not implemented yet", peer, pivot
|
|
|
|
await sleepAsync(5.seconds)
|
|
|
|
return
|
2022-10-08 17:20:50 +00:00
|
|
|
|
2022-11-01 15:07:44 +00:00
|
|
|
# Snapshot sync processing
|
|
|
|
# ------------------------
|
|
|
|
|
2022-11-25 14:56:42 +00:00
|
|
|
# If this is a new pivot, the previous one can be cleaned up. There is no
|
|
|
|
# point in keeping some older space consuming state data any longer.
|
2022-11-16 23:51:06 +00:00
|
|
|
ctx.data.pivotTable.beforeTopMostlyClean()
|
|
|
|
|
2022-11-25 14:56:42 +00:00
|
|
|
# This one is the syncing work horse which downloads the database
|
2022-11-28 09:03:23 +00:00
|
|
|
await env.execSnapSyncAction(buddy)
|
|
|
|
|
|
|
|
if env.obsolete:
|
|
|
|
return # pivot has changed
|
2022-11-16 23:51:06 +00:00
|
|
|
|
|
|
|
# Save state so sync can be partially resumed at next start up
|
2022-11-25 14:56:42 +00:00
|
|
|
let
|
|
|
|
nCheckNodes = env.fetchAccounts.checkNodes.len
|
|
|
|
nSickSubTries = env.fetchAccounts.sickSubTries.len
|
|
|
|
nStoQu = env.fetchStorageFull.len + env.fetchStoragePart.len
|
|
|
|
processed = env.fetchAccounts.processed.fullFactor.toPC(2)
|
2022-11-01 15:07:44 +00:00
|
|
|
block:
|
2022-11-25 14:56:42 +00:00
|
|
|
let rc = env.saveCheckpoint(ctx)
|
2022-11-16 23:51:06 +00:00
|
|
|
if rc.isErr:
|
|
|
|
error "Failed to save recovery checkpoint", peer, pivot,
|
|
|
|
nAccounts=env.nAccounts, nSlotLists=env.nSlotLists,
|
2022-11-25 14:56:42 +00:00
|
|
|
processed, nStoQu, error=rc.error
|
2022-11-16 23:51:06 +00:00
|
|
|
else:
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace "Saved recovery checkpoint", peer, pivot,
|
|
|
|
nAccounts=env.nAccounts, nSlotLists=env.nSlotLists,
|
2022-11-25 14:56:42 +00:00
|
|
|
processed, nStoQu, blobSize=rc.value
|
2022-11-16 23:51:06 +00:00
|
|
|
|
2022-11-28 09:03:23 +00:00
|
|
|
if buddy.ctrl.stopped:
|
|
|
|
return # peer worker has gone
|
2022-11-08 18:56:04 +00:00
|
|
|
|
2022-05-17 11:09:49 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|