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-16 23:51:06 +00:00
|
|
|
".."/[misc/best_pivot, protocol, sync_desc],
|
|
|
|
./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-01 15:07:44 +00:00
|
|
|
./worker/db/[snapdb_check, snapdb_desc],
|
|
|
|
"."/[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-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-08-24 13:44:18 +00:00
|
|
|
result = 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-14 14:13:00 +00:00
|
|
|
let nPivots = ctx.data.pivotTable.len
|
|
|
|
trace "I am the mighty recovery daemon ... stopped for now", nPivots
|
|
|
|
# To be populated ...
|
|
|
|
ctx.daemon = false
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
# This pivot finder one harmonises assigned difficulties of at least two
|
|
|
|
# peers. There can only be one `pivot2Exec()` instance active/unfinished
|
|
|
|
# (which is wrapped into the helper function `updateSinglePivot()`.)
|
|
|
|
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-08-24 13:44:18 +00:00
|
|
|
proc runPool*(buddy: SnapBuddyRef, last: 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
|
|
|
|
if ctx.poolMode:
|
|
|
|
ctx.poolMode = false
|
2022-10-08 17:20:50 +00:00
|
|
|
|
|
|
|
let rc = ctx.data.pivotTable.lastValue
|
|
|
|
if rc.isOk:
|
2022-11-01 15:07:44 +00:00
|
|
|
|
|
|
|
# Check whether last pivot accounts and storage are complete.
|
|
|
|
let
|
|
|
|
env = rc.value
|
|
|
|
peer = buddy.peer
|
|
|
|
pivot = "#" & $env.stateHeader.blockNumber # for logging
|
|
|
|
|
|
|
|
if not env.storageDone:
|
|
|
|
|
2022-10-08 17:20:50 +00:00
|
|
|
# Check whether accounts download is complete
|
2022-11-01 15:07:44 +00:00
|
|
|
if env.fetchAccounts.unprocessed.isEmpty():
|
|
|
|
|
|
|
|
# FIXME: This check might not be needed. It will visit *every* node
|
|
|
|
# in the hexary trie for checking the account leaves.
|
2022-11-09 19:16:25 +00:00
|
|
|
#
|
|
|
|
# Note: This is insane on main net
|
2022-11-01 15:07:44 +00:00
|
|
|
if buddy.checkAccountsTrieIsComplete(env):
|
|
|
|
env.accountsState = HealerDone
|
|
|
|
|
|
|
|
# Check whether storage slots are complete
|
2022-11-08 18:56:04 +00:00
|
|
|
if env.fetchStorageFull.len == 0 and
|
|
|
|
env.fetchStoragePart.len == 0:
|
2022-11-01 15:07:44 +00:00
|
|
|
env.storageDone = true
|
2022-05-09 14:04:48 +00:00
|
|
|
|
2022-11-16 23:51:06 +00:00
|
|
|
when extraTraceMessages:
|
2022-11-01 15:07:44 +00:00
|
|
|
trace "Checked for pivot DB completeness", peer, pivot,
|
|
|
|
nAccounts=env.nAccounts, accountsState=env.accountsState,
|
|
|
|
nSlotLists=env.nSlotLists, storageDone=env.storageDone
|
2022-10-21 19:29:42 +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:
|
|
|
|
if not buddy.checkAccountsTrieIsComplete(env):
|
|
|
|
error "Ooops, all accounts fetched but DvnB still incomplete", peer, pivot
|
2022-10-08 17:20:50 +00:00
|
|
|
|
2022-11-01 15:07:44 +00:00
|
|
|
if not buddy.checkStorageSlotsTrieIsComplete(env):
|
|
|
|
error "Ooops, all storages fetched but DB still incomplete", peer, pivot
|
2022-10-08 17:20:50 +00:00
|
|
|
|
2022-11-01 15:07:44 +00:00
|
|
|
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
|
|
|
|
# ------------------------
|
|
|
|
|
|
|
|
# If this is a new pivot, the previous one can be partially 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()
|
|
|
|
|
|
|
|
# This one is the syncing work horse
|
|
|
|
let syncActionContinue = await env.execSnapSyncAction(buddy)
|
|
|
|
|
|
|
|
# Save state so sync can be partially resumed at next start up
|
2022-11-01 15:07:44 +00:00
|
|
|
block:
|
2022-11-16 23:51:06 +00:00
|
|
|
let
|
|
|
|
nMissingNodes = env.fetchAccounts.missingNodes.len
|
|
|
|
nStoQu = env.fetchStorageFull.len + env.fetchStoragePart.len
|
|
|
|
|
|
|
|
rc = env.saveSnapState(ctx)
|
|
|
|
if rc.isErr:
|
|
|
|
error "Failed to save recovery checkpoint", peer, pivot,
|
|
|
|
nAccounts=env.nAccounts, nSlotLists=env.nSlotLists,
|
|
|
|
nMissingNodes, nStoQu, error=rc.error
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
when extraTraceMessages:
|
|
|
|
trace "Saved recovery checkpoint", peer, pivot,
|
|
|
|
nAccounts=env.nAccounts, nSlotLists=env.nSlotLists,
|
|
|
|
nMissingNodes, nStoQu, blobSize=rc.value
|
|
|
|
|
|
|
|
if not syncActionContinue:
|
|
|
|
return
|
2022-11-01 15:07:44 +00:00
|
|
|
|
|
|
|
# Check whether there are more accounts to fetch.
|
|
|
|
#
|
|
|
|
# Note that some other process might have temporarily borrowed from the
|
|
|
|
# `fetchAccounts.unprocessed` list. Whether we are done can only be decided
|
|
|
|
# if only a single buddy is active. S be it.
|
|
|
|
if env.fetchAccounts.unprocessed.isEmpty():
|
|
|
|
|
2022-11-08 18:56:04 +00:00
|
|
|
# Debugging log: analyse pivot against database
|
|
|
|
warn "Analysing accounts database -- might be slow", peer, pivot
|
|
|
|
discard buddy.checkAccountsListOk(env)
|
|
|
|
|
2022-11-01 15:07:44 +00:00
|
|
|
# Check whether pivot download is complete.
|
2022-11-08 18:56:04 +00:00
|
|
|
if env.fetchStorageFull.len == 0 and
|
|
|
|
env.fetchStoragePart.len == 0:
|
2022-11-01 15:07:44 +00:00
|
|
|
trace "Running pool mode for verifying completeness", peer, pivot
|
|
|
|
buddy.ctx.poolMode = true
|
2022-05-23 16:53:19 +00:00
|
|
|
|
2022-11-08 18:56:04 +00:00
|
|
|
# Debugging log: analyse pivot against database
|
|
|
|
warn "Analysing storage slots database -- might be slow", peer, pivot
|
|
|
|
discard buddy.checkStorageSlotsTrieIsComplete(env)
|
|
|
|
|
2022-05-17 11:09:49 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|