2022-06-16 08:58:50 +00:00
|
|
|
# Nimbus - Fetch account and storage states from peers efficiently
|
|
|
|
#
|
|
|
|
# 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-04-14 22:28:57 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2022-06-16 08:58:50 +00:00
|
|
|
import
|
2023-04-24 20:24:07 +00:00
|
|
|
std/[strformat, strutils],
|
2022-06-16 08:58:50 +00:00
|
|
|
chronos,
|
|
|
|
chronicles,
|
2022-10-20 16:59:54 +00:00
|
|
|
eth/[common, p2p],
|
2022-06-16 08:58:50 +00:00
|
|
|
stint,
|
2023-04-24 20:24:07 +00:00
|
|
|
../../utils/prettify,
|
|
|
|
../types,
|
|
|
|
./timer_helper
|
2022-06-16 08:58:50 +00:00
|
|
|
|
|
|
|
logScope:
|
2023-04-24 20:24:07 +00:00
|
|
|
topics = "tick"
|
2022-06-16 08:58:50 +00:00
|
|
|
|
|
|
|
type
|
2023-04-06 19:42:07 +00:00
|
|
|
TickerSnapStatsUpdater* = proc: TickerSnapStats {.gcsafe, raises: [].}
|
|
|
|
## Snap sync state update function
|
|
|
|
|
|
|
|
TickerFullStatsUpdater* = proc: TickerFullStats {.gcsafe, raises: [].}
|
|
|
|
## Full sync state update function
|
|
|
|
|
|
|
|
SnapDescDetails = object
|
|
|
|
## Private state descriptor
|
|
|
|
snapCb: TickerSnapStatsUpdater
|
|
|
|
recovery: bool
|
|
|
|
lastRecov: bool
|
|
|
|
lastStats: TickerSnapStats
|
|
|
|
|
|
|
|
FullDescDetails = object
|
|
|
|
## Private state descriptor
|
|
|
|
fullCb: TickerFullStatsUpdater
|
|
|
|
lastStats: TickerFullStats
|
|
|
|
|
|
|
|
TickerSnapStats* = object
|
|
|
|
## Snap sync state (see `TickerSnapStatsUpdater`)
|
2023-01-17 09:28:14 +00:00
|
|
|
beaconBlock*: Option[BlockNumber]
|
2022-08-04 08:04:30 +00:00
|
|
|
pivotBlock*: Option[BlockNumber]
|
2023-01-17 09:28:14 +00:00
|
|
|
nAccounts*: (float,float) ## Mean and standard deviation
|
|
|
|
accountsFill*: (float,float,float) ## Mean, standard deviation, merged total
|
2022-12-19 21:22:09 +00:00
|
|
|
nAccountStats*: int ## #chunks
|
2023-01-17 09:28:14 +00:00
|
|
|
nSlotLists*: (float,float) ## Mean and standard deviation
|
2023-04-21 21:11:04 +00:00
|
|
|
nContracts*: (float,float) ## Mean and standard deviation
|
2022-11-16 23:51:06 +00:00
|
|
|
nStorageQueue*: Option[int]
|
2023-04-21 21:11:04 +00:00
|
|
|
nContractQueue*: Option[int]
|
2022-10-08 17:20:50 +00:00
|
|
|
nQueues*: int
|
2022-06-16 08:58:50 +00:00
|
|
|
|
2023-04-06 19:42:07 +00:00
|
|
|
TickerFullStats* = object
|
|
|
|
## Full sync state (see `TickerFullStatsUpdater`)
|
2023-04-14 22:28:57 +00:00
|
|
|
pivotBlock*: Option[BlockNumber]
|
2023-04-06 19:42:07 +00:00
|
|
|
topPersistent*: BlockNumber
|
|
|
|
nextUnprocessed*: Option[BlockNumber]
|
|
|
|
nextStaged*: Option[BlockNumber]
|
|
|
|
nStagedQueue*: int
|
|
|
|
suspended*: bool
|
|
|
|
reOrg*: bool
|
2022-06-16 08:58:50 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
TickerRef* = ref object
|
2023-04-06 19:42:07 +00:00
|
|
|
## Ticker descriptor object
|
|
|
|
nBuddies: int
|
2022-08-04 08:04:30 +00:00
|
|
|
logTicker: TimerCallback
|
2023-04-06 19:42:07 +00:00
|
|
|
started: Moment
|
|
|
|
visited: Moment
|
|
|
|
prettyPrint: proc(t: TickerRef) {.gcsafe, raises: [].}
|
|
|
|
case fullMode: bool
|
|
|
|
of false:
|
|
|
|
snap: SnapDescDetails
|
|
|
|
of true:
|
|
|
|
full: FullDescDetails
|
2022-06-16 08:58:50 +00:00
|
|
|
|
|
|
|
const
|
2023-04-21 21:11:04 +00:00
|
|
|
extraTraceMessages = false # or true
|
|
|
|
## Enabled additional logging noise
|
|
|
|
|
2022-08-12 15:42:07 +00:00
|
|
|
tickerStartDelay = chronos.milliseconds(100)
|
|
|
|
tickerLogInterval = chronos.seconds(1)
|
2022-11-14 14:13:00 +00:00
|
|
|
tickerLogSuppressMax = chronos.seconds(100)
|
2022-06-16 08:58:50 +00:00
|
|
|
|
2022-08-12 15:42:07 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions: pretty printing
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-11-08 18:56:04 +00:00
|
|
|
proc pc99(val: float): string =
|
|
|
|
if 0.99 <= val and val < 1.0: "99%"
|
|
|
|
elif 0.0 < val and val <= 0.01: "1%"
|
|
|
|
else: val.toPC(0)
|
2022-08-12 15:42:07 +00:00
|
|
|
|
2023-04-21 21:11:04 +00:00
|
|
|
proc toStr(a: Option[int]): string =
|
|
|
|
if a.isNone: "n/a"
|
|
|
|
else: $a.unsafeGet
|
|
|
|
|
2022-06-16 08:58:50 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2023-04-06 19:42:07 +00:00
|
|
|
# Private functions: printing ticker messages
|
2022-06-16 08:58:50 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-04-21 21:11:04 +00:00
|
|
|
template logTxt(info: static[string]): static[string] =
|
|
|
|
"Ticker " & info
|
|
|
|
|
2022-07-01 11:42:17 +00:00
|
|
|
template noFmtError(info: static[string]; code: untyped) =
|
|
|
|
try:
|
|
|
|
code
|
|
|
|
except ValueError as e:
|
|
|
|
raiseAssert "Inconveivable (" & info & "): " & e.msg
|
|
|
|
|
2023-04-06 19:42:07 +00:00
|
|
|
proc snapTicker(t: TickerRef) {.gcsafe.} =
|
2022-11-01 15:07:44 +00:00
|
|
|
let
|
2023-04-06 19:42:07 +00:00
|
|
|
data = t.snap.snapCb()
|
2022-11-14 14:13:00 +00:00
|
|
|
now = Moment.now()
|
2022-07-01 11:42:17 +00:00
|
|
|
|
2023-04-06 19:42:07 +00:00
|
|
|
if data != t.snap.lastStats or
|
|
|
|
t.snap.recovery != t.snap.lastRecov or
|
2022-12-19 21:22:09 +00:00
|
|
|
tickerLogSuppressMax < (now - t.visited):
|
2022-08-04 08:04:30 +00:00
|
|
|
var
|
2023-04-21 21:11:04 +00:00
|
|
|
nAcc, nSto, nCon: string
|
2023-01-18 08:31:57 +00:00
|
|
|
pv = "n/a"
|
2022-08-04 08:04:30 +00:00
|
|
|
let
|
2023-04-21 21:11:04 +00:00
|
|
|
nStoQ = data.nStorageQueue.toStr
|
|
|
|
nConQ = data.nContractQueue.toStr
|
2023-04-14 22:28:57 +00:00
|
|
|
bc = data.beaconBlock.toStr
|
2023-04-06 19:42:07 +00:00
|
|
|
recoveryDone = t.snap.lastRecov
|
2022-11-08 18:56:04 +00:00
|
|
|
accCov = data.accountsFill[0].pc99 &
|
|
|
|
"(" & data.accountsFill[1].pc99 & ")" &
|
2022-11-16 23:51:06 +00:00
|
|
|
"/" & data.accountsFill[2].pc99 &
|
2022-12-19 21:22:09 +00:00
|
|
|
"~" & data.nAccountStats.uint.toSI
|
2023-01-18 08:31:57 +00:00
|
|
|
nInst = t.nBuddies
|
2022-11-01 15:07:44 +00:00
|
|
|
|
|
|
|
# With `int64`, there are more than 29*10^10 years range for seconds
|
2022-11-14 14:13:00 +00:00
|
|
|
up = (now - t.started).seconds.uint64.toSI
|
2022-08-04 08:04:30 +00:00
|
|
|
mem = getTotalMem().uint.toSI
|
2022-07-01 11:42:17 +00:00
|
|
|
|
2023-04-06 19:42:07 +00:00
|
|
|
t.snap.lastStats = data
|
2022-12-19 21:22:09 +00:00
|
|
|
t.visited = now
|
2023-04-06 19:42:07 +00:00
|
|
|
t.snap.lastRecov = t.snap.recovery
|
2022-12-19 21:22:09 +00:00
|
|
|
|
2023-04-14 22:28:57 +00:00
|
|
|
if data.pivotBlock.isSome:
|
|
|
|
pv = data.pivotBlock.toStr & "/" & $data.nQueues
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
noFmtError("runLogTicker"):
|
2022-10-21 19:29:42 +00:00
|
|
|
nAcc = (&"{(data.nAccounts[0]+0.5).int64}" &
|
|
|
|
&"({(data.nAccounts[1]+0.5).int64})")
|
|
|
|
nSto = (&"{(data.nSlotLists[0]+0.5).int64}" &
|
|
|
|
&"({(data.nSlotLists[1]+0.5).int64})")
|
2023-04-21 21:11:04 +00:00
|
|
|
nCon = (&"{(data.nContracts[0]+0.5).int64}" &
|
|
|
|
&"({(data.nContracts[1]+0.5).int64})")
|
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
|
|
|
|
2023-04-06 19:42:07 +00:00
|
|
|
if t.snap.recovery:
|
2023-04-25 12:24:32 +00:00
|
|
|
info "Snap sync ticker (recovery)",
|
2023-04-21 21:11:04 +00:00
|
|
|
up, nInst, bc, pv, nAcc, accCov, nSto, nStoQ, nCon, nConQ, mem
|
2022-12-19 21:22:09 +00:00
|
|
|
elif recoveryDone:
|
2023-04-25 12:24:32 +00:00
|
|
|
info "Snap sync ticker (recovery done)",
|
2023-04-21 21:11:04 +00:00
|
|
|
up, nInst, bc, pv, nAcc, accCov, nSto, nStoQ, nCon, nConQ, mem
|
2022-11-25 14:56:42 +00:00
|
|
|
else:
|
2023-04-25 12:24:32 +00:00
|
|
|
info "Snap sync ticker",
|
2023-04-21 21:11:04 +00:00
|
|
|
up, nInst, bc, pv, nAcc, accCov, nSto, nStoQ, nCon, nConQ, mem
|
2022-06-16 08:58:50 +00:00
|
|
|
|
|
|
|
|
2023-04-06 19:42:07 +00:00
|
|
|
proc fullTicker(t: TickerRef) {.gcsafe.} =
|
|
|
|
let
|
|
|
|
data = t.full.fullCb()
|
|
|
|
now = Moment.now()
|
|
|
|
|
|
|
|
if data != t.full.lastStats or
|
|
|
|
tickerLogSuppressMax < (now - t.visited):
|
|
|
|
let
|
2023-04-14 22:28:57 +00:00
|
|
|
persistent = data.topPersistent.toStr
|
|
|
|
staged = data.nextStaged.toStr
|
|
|
|
unprocessed = data.nextUnprocessed.toStr
|
2023-04-06 19:42:07 +00:00
|
|
|
queued = data.nStagedQueue
|
|
|
|
reOrg = if data.reOrg: "t" else: "f"
|
2023-04-14 22:28:57 +00:00
|
|
|
pv = data.pivotBlock.toStr
|
2023-04-06 19:42:07 +00:00
|
|
|
|
2023-04-14 22:28:57 +00:00
|
|
|
nInst = t.nBuddies
|
2023-04-06 19:42:07 +00:00
|
|
|
|
|
|
|
# With `int64`, there are more than 29*10^10 years range for seconds
|
|
|
|
up = (now - t.started).seconds.uint64.toSI
|
|
|
|
mem = getTotalMem().uint.toSI
|
|
|
|
|
|
|
|
t.full.lastStats = data
|
|
|
|
t.visited = now
|
|
|
|
|
|
|
|
if data.suspended:
|
2023-04-25 12:24:32 +00:00
|
|
|
info "Full sync ticker (suspended)", up, nInst, pv,
|
2023-04-06 19:42:07 +00:00
|
|
|
persistent, unprocessed, staged, queued, reOrg, mem
|
|
|
|
else:
|
2023-04-25 12:24:32 +00:00
|
|
|
info "Full sync ticker", up, nInst, pv,
|
2023-04-06 19:42:07 +00:00
|
|
|
persistent, unprocessed, staged, queued, reOrg, mem
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions: ticking log messages
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc setLogTicker(t: TickerRef; at: Moment) {.gcsafe.}
|
|
|
|
|
|
|
|
proc runLogTicker(t: TickerRef) {.gcsafe.} =
|
|
|
|
t.prettyPrint(t)
|
|
|
|
t.setLogTicker(Moment.fromNow(tickerLogInterval))
|
2022-06-16 08:58:50 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc setLogTicker(t: TickerRef; at: Moment) =
|
2023-04-21 21:11:04 +00:00
|
|
|
if t.logTicker.isNil:
|
|
|
|
when extraTraceMessages:
|
|
|
|
debug logTxt "was stopped", fullMode=t.fullMode, nBuddies=t.nBuddies
|
|
|
|
else:
|
2022-08-04 08:04:30 +00:00
|
|
|
t.logTicker = safeSetTimer(at, runLogTicker, t)
|
2022-06-16 08:58:50 +00:00
|
|
|
|
2023-04-06 19:42:07 +00:00
|
|
|
proc initImpl(t: TickerRef; cb: TickerSnapStatsUpdater) =
|
|
|
|
t.fullMode = false
|
|
|
|
t.prettyPrint = snapTicker
|
|
|
|
t.snap = SnapDescDetails(snapCb: cb)
|
|
|
|
|
|
|
|
proc initImpl(t: TickerRef; cb: TickerFullStatsUpdater) =
|
|
|
|
t.fullMode = true
|
|
|
|
t.prettyPrint = fullTicker
|
|
|
|
t.full = FullDescDetails(fullCb: cb)
|
|
|
|
|
2023-04-21 21:11:04 +00:00
|
|
|
proc startImpl(t: TickerRef) =
|
|
|
|
t.logTicker = safeSetTimer(Moment.fromNow(tickerStartDelay),runLogTicker,t)
|
|
|
|
if t.started == Moment.default:
|
|
|
|
t.started = Moment.now()
|
|
|
|
|
|
|
|
proc stopImpl(t: TickerRef) =
|
|
|
|
## Stop ticker unconditionally
|
|
|
|
t.logTicker = nil
|
|
|
|
|
2022-06-16 08:58:50 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2022-08-04 08:04:30 +00:00
|
|
|
# Public constructor and start/stop functions
|
2022-06-16 08:58:50 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-04-24 20:24:07 +00:00
|
|
|
proc init*(
|
|
|
|
T: type TickerRef;
|
|
|
|
cb: TickerSnapStatsUpdater|TickerFullStatsUpdater;
|
|
|
|
): T =
|
2023-04-14 22:28:57 +00:00
|
|
|
## Constructor
|
|
|
|
new result
|
|
|
|
result.initImpl(cb)
|
|
|
|
|
2023-04-06 19:42:07 +00:00
|
|
|
proc init*(t: TickerRef; cb: TickerSnapStatsUpdater) =
|
|
|
|
## Re-initialise ticket
|
2023-04-14 22:28:57 +00:00
|
|
|
if not t.isNil:
|
|
|
|
t.visited.reset
|
|
|
|
if t.fullMode:
|
|
|
|
t.prettyPrint(t) # print final state for full sync
|
|
|
|
t.initImpl(cb)
|
2023-04-06 19:42:07 +00:00
|
|
|
|
|
|
|
proc init*(t: TickerRef; cb: TickerFullStatsUpdater) =
|
|
|
|
## Re-initialise ticket
|
2023-04-14 22:28:57 +00:00
|
|
|
if not t.isNil:
|
|
|
|
t.visited.reset
|
|
|
|
if not t.fullMode:
|
|
|
|
t.prettyPrint(t) # print final state for snap sync
|
|
|
|
t.initImpl(cb)
|
2022-06-16 08:58:50 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc start*(t: TickerRef) =
|
2022-06-16 08:58:50 +00:00
|
|
|
## Re/start ticker unconditionally
|
2023-04-14 22:28:57 +00:00
|
|
|
if not t.isNil:
|
2023-04-21 21:11:04 +00:00
|
|
|
when extraTraceMessages:
|
|
|
|
debug logTxt "start", fullMode=t.fullMode, nBuddies=t.nBuddies
|
|
|
|
t.startImpl()
|
2022-06-16 08:58:50 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc stop*(t: TickerRef) =
|
2022-06-16 08:58:50 +00:00
|
|
|
## Stop ticker unconditionally
|
2023-04-14 22:28:57 +00:00
|
|
|
if not t.isNil:
|
2023-04-21 21:11:04 +00:00
|
|
|
t.stopImpl()
|
|
|
|
when extraTraceMessages:
|
|
|
|
debug logTxt "stop", fullMode=t.fullMode, nBuddies=t.nBuddies
|
2022-06-16 08:58:50 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc startBuddy*(t: TickerRef) =
|
|
|
|
## Increment buddies counter and start ticker unless running.
|
2023-04-14 22:28:57 +00:00
|
|
|
if not t.isNil:
|
|
|
|
if t.nBuddies <= 0:
|
|
|
|
t.nBuddies = 1
|
2023-04-24 20:24:07 +00:00
|
|
|
if t.fullMode or not t.snap.recovery:
|
2023-04-21 21:11:04 +00:00
|
|
|
t.startImpl()
|
|
|
|
when extraTraceMessages:
|
|
|
|
debug logTxt "start buddy", fullMode=t.fullMode, nBuddies=t.nBuddies
|
2023-04-14 22:28:57 +00:00
|
|
|
else:
|
|
|
|
t.nBuddies.inc
|
2022-08-04 08:04:30 +00:00
|
|
|
|
2022-11-25 14:56:42 +00:00
|
|
|
proc startRecovery*(t: TickerRef) =
|
|
|
|
## Ditto for recovery mode
|
2023-04-21 21:11:04 +00:00
|
|
|
if not t.isNil and not t.fullMode and not t.snap.recovery:
|
|
|
|
t.snap.recovery = true
|
|
|
|
if t.nBuddies <= 0:
|
|
|
|
t.startImpl()
|
|
|
|
when extraTraceMessages:
|
|
|
|
debug logTxt "start recovery", fullMode=t.fullMode, nBuddies=t.nBuddies
|
2022-11-25 14:56:42 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc stopBuddy*(t: TickerRef) =
|
|
|
|
## Decrement buddies counter and stop ticker if there are no more registered
|
|
|
|
## buddies.
|
2023-04-14 22:28:57 +00:00
|
|
|
if not t.isNil:
|
|
|
|
t.nBuddies.dec
|
2023-04-21 21:11:04 +00:00
|
|
|
if t.nBuddies <= 0 and not t.fullMode and not t.snap.recovery:
|
2023-05-16 13:52:44 +00:00
|
|
|
t.nBuddies = 0
|
2023-04-21 21:11:04 +00:00
|
|
|
t.stopImpl()
|
|
|
|
when extraTraceMessages:
|
|
|
|
debug logTxt "stop (buddy)", fullMode=t.fullMode, nBuddies=t.nBuddies
|
2022-11-25 14:56:42 +00:00
|
|
|
|
|
|
|
proc stopRecovery*(t: TickerRef) =
|
|
|
|
## Ditto for recovery mode
|
2023-04-21 21:11:04 +00:00
|
|
|
if not t.isNil and not t.fullMode and t.snap.recovery:
|
|
|
|
t.snap.recovery = false
|
|
|
|
t.stopImpl()
|
|
|
|
when extraTraceMessages:
|
|
|
|
debug logTxt "stop (recovery)", fullMode=t.fullMode, nBuddies=t.nBuddies
|
2022-06-16 08:58:50 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|