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.
|
|
|
|
|
|
|
|
import
|
2022-09-02 18:16:09 +00:00
|
|
|
std/[strformat, strutils, times],
|
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,
|
2022-07-21 18:16:28 +00:00
|
|
|
../../../utils/prettify,
|
2022-09-30 08:22:14 +00:00
|
|
|
../../misc/timer_helper
|
2022-06-16 08:58:50 +00:00
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "snap-ticker"
|
|
|
|
|
|
|
|
type
|
|
|
|
TickerStats* = object
|
2022-08-04 08:04:30 +00:00
|
|
|
pivotBlock*: Option[BlockNumber]
|
2022-09-02 18:16:09 +00:00
|
|
|
nAccounts*: (float,float) ## mean and standard deviation
|
|
|
|
accountsFill*: (float,float,float) ## mean, standard deviation, merged total
|
2022-10-21 19:29:42 +00:00
|
|
|
nSlotLists*: (float,float) ## mean and standard deviation
|
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
|
|
|
nStorageQueue*: Option[uint64]
|
2022-10-08 17:20:50 +00:00
|
|
|
nQueues*: int
|
2022-06-16 08:58:50 +00:00
|
|
|
|
|
|
|
TickerStatsUpdater* =
|
2022-08-04 08:04:30 +00:00
|
|
|
proc: TickerStats {.gcsafe, raises: [Defect].}
|
2022-06-16 08:58:50 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
TickerRef* = ref object
|
2022-06-16 08:58:50 +00:00
|
|
|
## Account fetching state that is shared among all peers.
|
2022-08-04 08:04:30 +00:00
|
|
|
nBuddies: int
|
|
|
|
lastStats: TickerStats
|
|
|
|
lastTick: uint64
|
|
|
|
statsCb: TickerStatsUpdater
|
|
|
|
logTicker: TimerCallback
|
|
|
|
tick: uint64 # more than 5*10^11y before wrap when ticking every sec
|
2022-06-16 08:58:50 +00:00
|
|
|
|
|
|
|
const
|
2022-08-12 15:42:07 +00:00
|
|
|
tickerStartDelay = chronos.milliseconds(100)
|
|
|
|
tickerLogInterval = chronos.seconds(1)
|
2022-08-04 08:04:30 +00:00
|
|
|
tickerLogSuppressMax = 100
|
2022-06-16 08:58:50 +00:00
|
|
|
|
2022-08-12 15:42:07 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions: pretty printing
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc ppMs*(elapsed: times.Duration): string
|
|
|
|
{.gcsafe, raises: [Defect, ValueError]} =
|
|
|
|
result = $elapsed.inMilliseconds
|
|
|
|
let ns = elapsed.inNanoseconds mod 1_000_000 # fraction of a milli second
|
|
|
|
if ns != 0:
|
|
|
|
# to rounded deca milli seconds
|
|
|
|
let dm = (ns + 5_000i64) div 10_000i64
|
|
|
|
result &= &".{dm:02}"
|
|
|
|
result &= "ms"
|
|
|
|
|
|
|
|
proc ppSecs*(elapsed: times.Duration): string
|
|
|
|
{.gcsafe, raises: [Defect, ValueError]} =
|
|
|
|
result = $elapsed.inSeconds
|
|
|
|
let ns = elapsed.inNanoseconds mod 1_000_000_000 # fraction of a second
|
|
|
|
if ns != 0:
|
|
|
|
# round up
|
|
|
|
let ds = (ns + 5_000_000i64) div 10_000_000i64
|
|
|
|
result &= &".{ds:02}"
|
|
|
|
result &= "s"
|
|
|
|
|
|
|
|
proc ppMins*(elapsed: times.Duration): string
|
|
|
|
{.gcsafe, raises: [Defect, ValueError]} =
|
|
|
|
result = $elapsed.inMinutes
|
|
|
|
let ns = elapsed.inNanoseconds mod 60_000_000_000 # fraction of a minute
|
|
|
|
if ns != 0:
|
|
|
|
# round up
|
|
|
|
let dm = (ns + 500_000_000i64) div 1_000_000_000i64
|
|
|
|
result &= &":{dm:02}"
|
|
|
|
result &= "m"
|
|
|
|
|
|
|
|
proc pp(d: times.Duration): string
|
|
|
|
{.gcsafe, raises: [Defect, ValueError]} =
|
|
|
|
if 40 < d.inSeconds:
|
|
|
|
d.ppMins
|
|
|
|
elif 200 < d.inMilliseconds:
|
|
|
|
d.ppSecs
|
|
|
|
else:
|
|
|
|
d.ppMs
|
|
|
|
|
2022-06-16 08:58:50 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions: ticking log messages
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
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
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc setLogTicker(t: TickerRef; at: Moment) {.gcsafe.}
|
2022-06-16 08:58:50 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc runLogTicker(t: TickerRef) {.gcsafe.} =
|
|
|
|
let data = t.statsCb()
|
2022-07-01 11:42:17 +00:00
|
|
|
|
2022-10-19 10:04:06 +00:00
|
|
|
if data != t.lastStats or t.lastTick + tickerLogSuppressMax < t.tick:
|
2022-08-04 08:04:30 +00:00
|
|
|
t.lastStats = data
|
|
|
|
t.lastTick = t.tick
|
|
|
|
var
|
2022-10-19 10:04:06 +00:00
|
|
|
nAcc, nSto, bulk: string
|
2022-08-04 08:04:30 +00:00
|
|
|
pivot = "n/a"
|
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
|
|
|
nStoQue = "n/a"
|
2022-08-04 08:04:30 +00:00
|
|
|
let
|
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
|
|
|
accCov = data.accountsFill[0].toPC(0) &
|
|
|
|
"(" & data.accountsFill[1].toPC(0) & ")" &
|
2022-09-02 18:16:09 +00:00
|
|
|
"/" & data.accountsFill[2].toPC(0)
|
2022-08-04 08:04:30 +00:00
|
|
|
buddies = t.nBuddies
|
|
|
|
tick = t.tick.toSI
|
|
|
|
mem = getTotalMem().uint.toSI
|
2022-07-01 11:42:17 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
noFmtError("runLogTicker"):
|
|
|
|
if data.pivotBlock.isSome:
|
2022-10-08 17:20:50 +00:00
|
|
|
pivot = &"#{data.pivotBlock.get}/{data.nQueues}"
|
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})")
|
2022-06-16 08:58:50 +00:00
|
|
|
|
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
|
|
|
if data.nStorageQueue.isSome:
|
|
|
|
nStoQue = $data.nStorageQueue.unsafeGet
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
info "Snap sync statistics",
|
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
|
|
|
tick, buddies, pivot, nAcc, accCov, nSto, nStoQue, mem
|
2022-06-16 08:58:50 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
t.tick.inc
|
|
|
|
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) =
|
|
|
|
if not t.logTicker.isNil:
|
|
|
|
t.logTicker = safeSetTimer(at, runLogTicker, t)
|
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
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc init*(T: type TickerRef; cb: TickerStatsUpdater): T =
|
|
|
|
## Constructor
|
|
|
|
T(statsCb: 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
|
2022-08-04 08:04:30 +00:00
|
|
|
#debug "Started ticker"
|
|
|
|
t.logTicker = safeSetTimer(Moment.fromNow(tickerStartDelay), runLogTicker, t)
|
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
|
2022-08-04 08:04:30 +00:00
|
|
|
t.logTicker = nil
|
|
|
|
#debug "Stopped ticker"
|
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.
|
|
|
|
if t.nBuddies <= 0:
|
|
|
|
t.nBuddies = 1
|
|
|
|
t.start()
|
2022-06-16 08:58:50 +00:00
|
|
|
else:
|
2022-08-04 08:04:30 +00:00
|
|
|
t.nBuddies.inc
|
|
|
|
|
|
|
|
proc stopBuddy*(t: TickerRef) =
|
|
|
|
## Decrement buddies counter and stop ticker if there are no more registered
|
|
|
|
## buddies.
|
|
|
|
t.nBuddies.dec
|
|
|
|
if t.nBuddies <= 0:
|
|
|
|
t.stop()
|
2022-06-16 08:58:50 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|