2022-10-08 17:20:50 +00:00
|
|
|
# Nimbus
|
2024-02-15 02:57:05 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2022-08-04 08:04:30 +00:00
|
|
|
# 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-02-14 23:38:33 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
import
|
2022-11-08 18:56:04 +00:00
|
|
|
std/[math, sequtils, strutils, hashes],
|
2022-11-28 09:03:23 +00:00
|
|
|
eth/common,
|
2023-04-04 13:36:18 +00:00
|
|
|
stew/interval_set,
|
2022-08-04 08:04:30 +00:00
|
|
|
stint,
|
|
|
|
../../constants,
|
2023-04-04 13:36:18 +00:00
|
|
|
../../utils/prettify,
|
2022-08-24 13:44:18 +00:00
|
|
|
../protocol,
|
2022-08-04 08:04:30 +00:00
|
|
|
../types
|
|
|
|
|
2023-04-24 20:24:07 +00:00
|
|
|
export
|
|
|
|
types
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
type
|
2022-09-16 07:24:12 +00:00
|
|
|
ByteArray32* = array[32,byte]
|
|
|
|
## Used for 32 byte database keys
|
|
|
|
|
|
|
|
NodeKey* = distinct ByteArray32
|
|
|
|
## Hash key without the hash wrapper (as opposed to `NodeTag` which is a
|
2022-12-12 22:00:24 +00:00
|
|
|
## number.)
|
2022-09-16 07:24:12 +00:00
|
|
|
|
2022-10-19 10:04:06 +00:00
|
|
|
NodeTag* = distinct UInt256
|
2022-12-12 22:00:24 +00:00
|
|
|
## Trie leaf item, account hash etc. This data type is a representation
|
|
|
|
## for a `NodeKey` geared up for arithmetic and comparing keys.
|
2022-10-19 10:04:06 +00:00
|
|
|
|
2022-10-14 16:40:32 +00:00
|
|
|
NodeTagRange* = Interval[NodeTag,UInt256]
|
2022-08-04 08:04:30 +00:00
|
|
|
## Interval `[minPt,maxPt]` of` NodeTag` elements, can be managed in an
|
|
|
|
## `IntervalSet` data type.
|
|
|
|
|
2022-10-14 16:40:32 +00:00
|
|
|
NodeTagRangeSet* = IntervalSetRef[NodeTag,UInt256]
|
|
|
|
## Managed structure to handle non-adjacent `NodeTagRange` intervals
|
2022-08-04 08:04:30 +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
|
|
|
NodeSpecs* = object
|
|
|
|
## Multi purpose descriptor for a hexary trie node:
|
|
|
|
## * Missing node specs. If the `data` argument is empty, the `partialPath`
|
|
|
|
## refers to a missoing node entry. The `nodeKey` is another way of
|
|
|
|
## writing the node hash and used to verify that a potential data `Blob`
|
|
|
|
## is acceptable as node data.
|
|
|
|
## * Node data. If the `data` argument is non-empty, the `partialPath`
|
|
|
|
## fields can/will be used as function argument for various functions
|
|
|
|
## when healing.
|
|
|
|
partialPath*: Blob ## Compact encoded partial path nibbles
|
|
|
|
nodeKey*: NodeKey ## Derived from node hash
|
|
|
|
data*: Blob ## Node data (might not be present)
|
|
|
|
|
2022-08-24 13:44:18 +00:00
|
|
|
PackedAccountRange* = object
|
|
|
|
## Re-packed version of `SnapAccountRange`. The reason why repacking is
|
|
|
|
## needed is that the `snap/1` protocol uses another RLP encoding than is
|
|
|
|
## used for storing in the database. So the `PackedAccount` is `BaseDB`
|
|
|
|
## trie compatible.
|
|
|
|
accounts*: seq[PackedAccount] ## List of re-packed accounts data
|
2023-02-15 10:14:40 +00:00
|
|
|
proof*: seq[SnapProof] ## Boundary proofs
|
2022-08-24 13:44:18 +00:00
|
|
|
|
|
|
|
PackedAccount* = object
|
|
|
|
## In fact, the `snap/1` driver returns the `Account` structure which is
|
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
|
|
|
## unwanted overhead, here.
|
|
|
|
accKey*: NodeKey
|
2022-08-24 13:44:18 +00:00
|
|
|
accBlob*: Blob
|
|
|
|
|
2023-04-21 21:11:04 +00:00
|
|
|
AccountCodeHeader* = object
|
|
|
|
## Contract code header
|
|
|
|
accKey*: NodeKey ## Owner account
|
|
|
|
codeHash*: Hash256 ## Contarct code hash
|
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
AccountSlotsHeader* = object
|
|
|
|
## Storage root header
|
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
|
|
|
accKey*: NodeKey ## Owner account, maybe unnecessary
|
2022-10-14 16:40:32 +00:00
|
|
|
storageRoot*: Hash256 ## Start of storage tree
|
|
|
|
subRange*: Option[NodeTagRange] ## Sub-range of slot range covered
|
2022-09-02 18:16:09 +00:00
|
|
|
|
2023-04-04 13:36:18 +00:00
|
|
|
AccountSlotsChanged* = object
|
|
|
|
## Variant of `AccountSlotsHeader` representing some transition
|
|
|
|
account*: AccountSlotsHeader ## Account header
|
|
|
|
newRange*: Option[NodeTagRange] ## New sub-range (if-any)
|
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
AccountStorageRange* = object
|
|
|
|
## List of storage descriptors, the last `AccountSlots` storage data might
|
2022-11-08 18:56:04 +00:00
|
|
|
## be incomplete and the `proof` is needed for proving validity.
|
2022-10-14 16:40:32 +00:00
|
|
|
storages*: seq[AccountSlots] ## List of accounts and storage data
|
2023-02-15 10:14:40 +00:00
|
|
|
proof*: seq[SnapProof] ## Boundary proofs for last entry
|
2022-11-08 18:56:04 +00:00
|
|
|
base*: NodeTag ## Lower limit for last entry w/proof
|
2022-09-02 18:16:09 +00:00
|
|
|
|
|
|
|
AccountSlots* = object
|
|
|
|
## Account storage descriptor
|
|
|
|
account*: AccountSlotsHeader
|
|
|
|
data*: seq[SnapStorage]
|
|
|
|
|
2023-04-04 13:36:18 +00:00
|
|
|
# See below for definition of constant `FullNodeTagRange`
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-09-16 07:24:12 +00:00
|
|
|
proc to*(tag: NodeTag; T: type Hash256): T =
|
2022-08-04 08:04:30 +00:00
|
|
|
## Convert to serialised equivalent
|
2022-09-16 07:24:12 +00:00
|
|
|
result.data = tag.UInt256.toBytesBE
|
|
|
|
|
|
|
|
proc to*(key: NodeKey; T: type NodeTag): T =
|
|
|
|
## Convert from serialised equivalent
|
|
|
|
UInt256.fromBytesBE(key.ByteArray32).T
|
2022-08-04 08:04:30 +00:00
|
|
|
|
2022-09-16 07:24:12 +00:00
|
|
|
proc to*(key: Hash256; T: type NodeTag): T =
|
2022-08-04 08:04:30 +00:00
|
|
|
## Syntactic sugar
|
2022-09-16 07:24:12 +00:00
|
|
|
key.data.NodeKey.to(T)
|
2022-08-04 08:04:30 +00:00
|
|
|
|
2022-09-16 07:24:12 +00:00
|
|
|
proc to*(tag: NodeTag; T: type NodeKey): T =
|
|
|
|
## Syntactic sugar
|
|
|
|
tag.UInt256.toBytesBE.T
|
|
|
|
|
|
|
|
proc to*(hash: Hash256; T: type NodeKey): T =
|
|
|
|
## Syntactic sugar
|
|
|
|
hash.data.NodeKey
|
2022-08-04 08:04:30 +00:00
|
|
|
|
2022-10-19 10:04:06 +00:00
|
|
|
proc to*(key: NodeKey; T: type Hash256): T =
|
|
|
|
## Syntactic sugar
|
|
|
|
T(data: key.ByteArray32)
|
|
|
|
|
2022-09-16 07:24:12 +00:00
|
|
|
proc to*(key: NodeKey; T: type Blob): T =
|
2022-08-04 08:04:30 +00:00
|
|
|
## Syntactic sugar
|
2022-09-16 07:24:12 +00:00
|
|
|
key.ByteArray32.toSeq
|
2022-08-04 08:04:30 +00:00
|
|
|
|
|
|
|
proc to*(n: SomeUnsignedInt|UInt256; T: type NodeTag): T =
|
|
|
|
## Syntactic sugar
|
|
|
|
n.u256.T
|
|
|
|
|
2022-10-28 07:26:17 +00:00
|
|
|
proc digestTo*(data: Blob; T: type NodeKey): T =
|
|
|
|
keccakHash(data).data.T
|
|
|
|
|
|
|
|
|
2022-10-19 10:04:06 +00:00
|
|
|
proc hash*(a: NodeKey): Hash =
|
|
|
|
## Table/KeyedQueue mixin
|
|
|
|
a.ByteArray32.hash
|
|
|
|
|
|
|
|
proc `==`*(a, b: NodeKey): bool =
|
|
|
|
## Table/KeyedQueue mixin
|
|
|
|
a.ByteArray32 == b.ByteArray32
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public constructors
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-09-16 07:24:12 +00:00
|
|
|
proc init*(key: var NodeKey; data: openArray[byte]): bool =
|
2022-10-14 16:40:32 +00:00
|
|
|
## Import argument `data` into `key` which must have length either `32`, or
|
2022-09-16 07:24:12 +00:00
|
|
|
## `0`. The latter case is equivalent to an all zero byte array of size `32`.
|
2022-08-04 08:04:30 +00:00
|
|
|
if data.len == 32:
|
2022-09-16 07:24:12 +00:00
|
|
|
(addr key.ByteArray32[0]).copyMem(unsafeAddr data[0], data.len)
|
2022-08-04 08:04:30 +00:00
|
|
|
return true
|
|
|
|
elif data.len == 0:
|
2022-09-16 07:24:12 +00:00
|
|
|
key.reset
|
2022-08-04 08:04:30 +00:00
|
|
|
return true
|
|
|
|
|
2022-09-16 07:24:12 +00:00
|
|
|
proc init*(tag: var NodeTag; data: openArray[byte]): bool =
|
|
|
|
## Similar to `init(key: var NodeHash; .)`.
|
|
|
|
var key: NodeKey
|
|
|
|
if key.init(data):
|
|
|
|
tag = key.to(NodeTag)
|
2022-08-04 08:04:30 +00:00
|
|
|
return true
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public rlp support
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-11-16 23:51:06 +00:00
|
|
|
proc read*[T: NodeTag|NodeKey](rlp: var Rlp, W: type T): T
|
2023-01-30 22:10:23 +00:00
|
|
|
{.gcsafe, raises: [RlpError].} =
|
2022-08-04 08:04:30 +00:00
|
|
|
rlp.read(Hash256).to(T)
|
|
|
|
|
2022-11-16 23:51:06 +00:00
|
|
|
proc append*(writer: var RlpWriter, val: NodeTag|NodeKey) =
|
|
|
|
writer.append(val.to(Hash256))
|
2022-08-04 08:04:30 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-10-14 16:40:32 +00:00
|
|
|
# Public `NodeTag` and `NodeTagRange` functions
|
2022-08-04 08:04:30 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc u256*(lp: NodeTag): UInt256 = lp.UInt256
|
|
|
|
proc low*(T: type NodeTag): T = low(UInt256).T
|
|
|
|
proc high*(T: type NodeTag): T = high(UInt256).T
|
|
|
|
|
|
|
|
proc `+`*(a: NodeTag; b: UInt256): NodeTag = (a.u256+b).NodeTag
|
|
|
|
proc `-`*(a: NodeTag; b: UInt256): NodeTag = (a.u256-b).NodeTag
|
|
|
|
proc `-`*(a, b: NodeTag): UInt256 = (a.u256 - b.u256)
|
|
|
|
|
|
|
|
proc `==`*(a, b: NodeTag): bool = a.u256 == b.u256
|
|
|
|
proc `<=`*(a, b: NodeTag): bool = a.u256 <= b.u256
|
|
|
|
proc `<`*(a, b: NodeTag): bool = a.u256 < b.u256
|
|
|
|
|
2022-08-12 15:42:07 +00:00
|
|
|
proc cmp*(x, y: NodeTag): int = cmp(x.UInt256, y.UInt256)
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc hash*(a: NodeTag): Hash =
|
|
|
|
## Mixin for `Table` or `keyedQueue`
|
|
|
|
a.to(Hash256).data.hash
|
|
|
|
|
|
|
|
proc digestTo*(data: Blob; T: type NodeTag): T =
|
|
|
|
## Hash the `data` argument
|
2022-09-03 18:15:35 +00:00
|
|
|
keccakHash(data).to(T)
|
2022-08-04 08:04:30 +00:00
|
|
|
|
2023-04-04 13:36:18 +00:00
|
|
|
const
|
|
|
|
# Cannot be defined earlier: `NodeTag` operations needed
|
|
|
|
FullNodeTagRange* = NodeTagRange.new(low(NodeTag),high(NodeTag))
|
|
|
|
|
2022-10-14 16:40:32 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions: `NodeTagRange` helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc isEmpty*(lrs: NodeTagRangeSet): bool =
|
|
|
|
## Returns `true` if the argument set `lrs` of intervals is empty
|
2022-11-25 14:56:42 +00:00
|
|
|
lrs.chunks == 0
|
2022-10-14 16:40:32 +00:00
|
|
|
|
|
|
|
proc isEmpty*(lrs: openArray[NodeTagRangeSet]): bool =
|
|
|
|
## Variant of `isEmpty()` where intervals are distributed across several
|
|
|
|
## sets.
|
|
|
|
for ivSet in lrs:
|
2022-11-25 14:56:42 +00:00
|
|
|
if 0 < ivSet.chunks:
|
2022-10-14 16:40:32 +00:00
|
|
|
return false
|
2022-10-20 16:59:54 +00:00
|
|
|
true
|
2022-10-08 17:20:50 +00:00
|
|
|
|
2023-04-04 13:36:18 +00:00
|
|
|
proc isEmpty*(iv: NodeTagRange): bool =
|
|
|
|
## Ditto for an interval range.
|
|
|
|
false # trivially by definition
|
|
|
|
|
|
|
|
|
2022-10-14 16:40:32 +00:00
|
|
|
proc isFull*(lrs: NodeTagRangeSet): bool =
|
|
|
|
## Returns `true` if the argument set `lrs` contains of the single
|
|
|
|
## interval [low(NodeTag),high(NodeTag)].
|
2024-02-16 09:08:07 +00:00
|
|
|
lrs.total.isZero and 0 < lrs.chunks
|
2022-10-14 16:40:32 +00:00
|
|
|
|
2023-04-04 13:36:18 +00:00
|
|
|
proc isFull*(lrs: openArray[NodeTagRangeSet]): bool =
|
|
|
|
## Variant of `isFull()` where intervals are distributed across several
|
|
|
|
## sets. This function makes sense only if the interval sets are mutually
|
|
|
|
## disjunct.
|
|
|
|
var accu: NodeTag
|
|
|
|
for ivSet in lrs:
|
|
|
|
if 0 < ivSet.total:
|
|
|
|
if high(NodeTag) - ivSet.total < accu:
|
|
|
|
return true
|
|
|
|
accu = accu + ivSet.total
|
|
|
|
elif 0 < ivSet.chunks:
|
|
|
|
# number of points in `ivSet` is `2^256 + 1`
|
|
|
|
return true
|
|
|
|
|
|
|
|
proc isFull*(iv: NodeTagRange): bool =
|
|
|
|
## Ditto for an interval range.
|
|
|
|
iv == FullNodeTagRange
|
|
|
|
|
2022-10-14 16:40:32 +00:00
|
|
|
|
|
|
|
proc emptyFactor*(lrs: NodeTagRangeSet): float =
|
2022-10-08 17:20:50 +00:00
|
|
|
## Relative uncovered total, i.e. `#points-not-covered / 2^256` to be used
|
|
|
|
## in statistics or triggers.
|
2022-08-04 08:04:30 +00:00
|
|
|
if 0 < lrs.total:
|
|
|
|
((high(NodeTag) - lrs.total).u256 + 1).to(float) / (2.0^256)
|
|
|
|
elif lrs.chunks == 0:
|
2022-08-17 07:30:11 +00:00
|
|
|
1.0 # `total` represents the residue class `mod 2^256` from `0`..`(2^256-1)`
|
2022-08-04 08:04:30 +00:00
|
|
|
else:
|
2022-10-08 17:20:50 +00:00
|
|
|
0.0 # number of points in `lrs` is `2^256 + 1`
|
|
|
|
|
2022-10-14 16:40:32 +00:00
|
|
|
proc emptyFactor*(lrs: openArray[NodeTagRangeSet]): float =
|
2022-10-08 17:20:50 +00:00
|
|
|
## Variant of `emptyFactor()` where intervals are distributed across several
|
|
|
|
## sets. This function makes sense only if the interval sets are mutually
|
|
|
|
## disjunct.
|
2022-10-19 10:04:06 +00:00
|
|
|
var accu: NodeTag
|
2022-10-08 17:20:50 +00:00
|
|
|
for ivSet in lrs:
|
|
|
|
if 0 < ivSet.total:
|
|
|
|
if high(NodeTag) - ivSet.total < accu:
|
|
|
|
return 0.0
|
|
|
|
accu = accu + ivSet.total
|
|
|
|
elif ivSet.chunks == 0:
|
|
|
|
discard
|
|
|
|
else: # number of points in `ivSet` is `2^256 + 1`
|
|
|
|
return 0.0
|
2023-04-04 13:36:18 +00:00
|
|
|
# Calculate: (2^256 - accu) / 2^256
|
2022-10-14 16:40:32 +00:00
|
|
|
if accu == 0.to(NodeTag):
|
2023-04-04 13:36:18 +00:00
|
|
|
1.0
|
|
|
|
else:
|
|
|
|
((high(NodeTag) - accu).u256 + 1).to(float) / (2.0^256)
|
2022-08-04 08:04:30 +00:00
|
|
|
|
2022-10-19 10:04:06 +00:00
|
|
|
|
2022-10-14 16:40:32 +00:00
|
|
|
proc fullFactor*(lrs: NodeTagRangeSet): float =
|
2022-10-08 17:20:50 +00:00
|
|
|
## Relative covered total, i.e. `#points-covered / 2^256` to be used
|
|
|
|
## in statistics or triggers
|
2022-08-17 07:30:11 +00:00
|
|
|
if 0 < lrs.total:
|
|
|
|
lrs.total.u256.to(float) / (2.0^256)
|
|
|
|
elif lrs.chunks == 0:
|
2022-10-08 17:20:50 +00:00
|
|
|
0.0 # `total` represents the residue class `mod 2^256` from `0`..`(2^256-1)`
|
2022-08-17 07:30:11 +00:00
|
|
|
else:
|
2022-10-08 17:20:50 +00:00
|
|
|
1.0 # number of points in `lrs` is `2^256 + 1`
|
|
|
|
|
2023-04-04 13:36:18 +00:00
|
|
|
proc fullFactor*(lrs: openArray[NodeTagRangeSet]): float =
|
|
|
|
## Variant of `fullFactor()` where intervals are distributed across several
|
|
|
|
## sets. This function makes sense only if the interval sets are mutually
|
|
|
|
## disjunct.
|
|
|
|
var accu: NodeTag
|
|
|
|
for ivSet in lrs:
|
|
|
|
if 0 < ivSet.total:
|
|
|
|
if high(NodeTag) - ivSet.total < accu:
|
|
|
|
return 1.0
|
|
|
|
accu = accu + ivSet.total
|
|
|
|
elif ivSet.chunks == 0:
|
|
|
|
discard
|
|
|
|
else: # number of points in `ivSet` is `2^256 + 1`
|
|
|
|
return 1.0
|
|
|
|
accu.u256.to(float) / (2.0^256)
|
|
|
|
|
2022-12-12 22:00:24 +00:00
|
|
|
proc fullFactor*(iv: NodeTagRange): float =
|
|
|
|
## Relative covered length of an inetrval, i.e. `#points-covered / 2^256`
|
|
|
|
if 0 < iv.len:
|
|
|
|
iv.len.u256.to(float) / (2.0^256)
|
|
|
|
else:
|
|
|
|
1.0 # number of points in `iv` is `2^256 + 1`
|
|
|
|
|
2022-10-14 16:40:32 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions: printing & pretty printing
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-10-08 17:20: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
|
|
|
proc `$`*(nodeTag: NodeTag): string =
|
|
|
|
if nodeTag == high(NodeTag):
|
2022-11-08 18:56:04 +00:00
|
|
|
"2^256-1"
|
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
|
|
|
elif nodeTag == 0.u256.NodeTag:
|
2022-08-04 08:04:30 +00:00
|
|
|
"0"
|
2023-04-04 13:36:18 +00:00
|
|
|
elif nodeTag == 2.u256.pow(255).NodeTag:
|
|
|
|
"2^255" # 800...
|
|
|
|
elif nodeTag == 2.u256.pow(254).NodeTag:
|
|
|
|
"2^254" # 400..
|
|
|
|
elif nodeTag == 2.u256.pow(253).NodeTag:
|
|
|
|
"2^253" # 200...
|
|
|
|
elif nodeTag == 2.u256.pow(251).NodeTag:
|
|
|
|
"2^252" # 100...
|
2022-08-04 08:04:30 +00:00
|
|
|
else:
|
2023-04-04 13:36:18 +00:00
|
|
|
nodeTag.UInt256.toHex
|
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
|
|
|
|
|
|
|
proc `$`*(nodeKey: NodeKey): string =
|
|
|
|
$nodeKey.to(NodeTag)
|
2022-08-04 08:04:30 +00:00
|
|
|
|
|
|
|
proc leafRangePp*(a, b: NodeTag): string =
|
|
|
|
## Needed for macro generated DSL files like `snap.nim` because the
|
|
|
|
## `distinct` flavour of `NodeTag` is discarded there.
|
|
|
|
result = "[" & $a
|
|
|
|
if a != b:
|
|
|
|
result &= ',' & $b
|
|
|
|
result &= "]"
|
|
|
|
|
2022-12-19 21:22:09 +00:00
|
|
|
proc leafRangePp*(iv: NodeTagRange): string =
|
|
|
|
## Variant of `leafRangePp()`
|
|
|
|
leafRangePp(iv.minPt, iv.maxPt)
|
|
|
|
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc `$`*(a, b: NodeTag): string =
|
|
|
|
## Prettyfied prototype
|
|
|
|
leafRangePp(a,b)
|
|
|
|
|
2022-10-14 16:40:32 +00:00
|
|
|
proc `$`*(iv: NodeTagRange): string =
|
2022-12-19 21:22:09 +00:00
|
|
|
leafRangePp iv
|
2022-08-04 08:04:30 +00:00
|
|
|
|
2022-10-28 07:26:17 +00:00
|
|
|
|
2023-04-04 13:36:18 +00:00
|
|
|
proc fullPC3*(w: NodeTagRangeSet|NodeTagRange): string =
|
|
|
|
## Pretty print fill state of range sets.
|
|
|
|
if w.isEmpty:
|
|
|
|
"0%"
|
|
|
|
elif w.isFull:
|
|
|
|
"100%"
|
|
|
|
else:
|
|
|
|
let ff = w.fullFactor
|
|
|
|
if ff <= 0.99999:
|
|
|
|
ff.toPC(3)
|
|
|
|
else:
|
|
|
|
"99.999"
|
|
|
|
|
|
|
|
proc fullPC3*(w: openArray[NodeTagRangeSet]): string =
|
|
|
|
## Variant of `fullPC3()` where intervals are distributed across several
|
|
|
|
## sets. This function makes sense only if the interval sets are mutually
|
|
|
|
## disjunct.
|
|
|
|
if w.isEmpty:
|
|
|
|
"0%"
|
|
|
|
else:
|
|
|
|
let partition = "~" & $w.mapIt(it.chunks).foldl(a+b)
|
|
|
|
if w.isFull:
|
|
|
|
"100%" & partition
|
|
|
|
else:
|
|
|
|
let ff = w.fullFactor
|
|
|
|
if ff <= 0.99999:
|
|
|
|
ff.toPC(3) & partition
|
|
|
|
else:
|
|
|
|
"99.999" & partition
|
|
|
|
|
|
|
|
|
2022-11-08 18:56:04 +00:00
|
|
|
proc dump*(
|
|
|
|
ranges: openArray[NodeTagRangeSet];
|
2024-02-15 02:57:05 +00:00
|
|
|
moan: proc(overlap: UInt256; iv: NodeTagRange) {.gcsafe, raises: [].};
|
2022-11-08 18:56:04 +00:00
|
|
|
printRangesMax = high(int);
|
|
|
|
): string =
|
|
|
|
## Dump/anlalyse range sets
|
|
|
|
var
|
|
|
|
cache: NodeTagRangeSet
|
|
|
|
ivTotal = 0.u256
|
|
|
|
ivCarry = false
|
|
|
|
|
|
|
|
if ranges.len == 1:
|
|
|
|
cache = ranges[0]
|
|
|
|
ivTotal = cache.total
|
|
|
|
if ivTotal == 0.u256 and 0 < cache.chunks:
|
|
|
|
ivCarry = true
|
|
|
|
else:
|
|
|
|
cache = NodeTagRangeSet.init()
|
|
|
|
for ivSet in ranges:
|
|
|
|
if ivSet.total == 0.u256 and 0 < ivSet.chunks:
|
|
|
|
ivCarry = true
|
|
|
|
elif ivTotal <= high(UInt256) - ivSet.total:
|
|
|
|
ivTotal += ivSet.total
|
|
|
|
else:
|
|
|
|
ivCarry = true
|
|
|
|
for iv in ivSet.increasing():
|
|
|
|
let n = cache.merge(iv)
|
|
|
|
if n != iv.len and not moan.isNil:
|
|
|
|
moan(iv.len - n, iv)
|
|
|
|
|
2024-02-16 09:08:07 +00:00
|
|
|
if cache.total.isZero and 0 < cache.chunks:
|
2022-11-08 18:56:04 +00:00
|
|
|
result = "2^256"
|
|
|
|
if not ivCarry:
|
|
|
|
result &= ":" & $ivTotal
|
|
|
|
else:
|
|
|
|
result = $cache.total
|
|
|
|
if ivCarry:
|
|
|
|
result &= ":2^256"
|
|
|
|
elif ivTotal != cache.total:
|
|
|
|
result &= ":" & $ivTotal
|
|
|
|
|
|
|
|
result &= ":"
|
|
|
|
if cache.chunks <= printRangesMax:
|
|
|
|
result &= toSeq(cache.increasing).mapIt($it).join(",")
|
|
|
|
else:
|
|
|
|
result &= toSeq(cache.increasing).mapIt($it)[0 ..< printRangesMax].join(",")
|
|
|
|
result &= " " & $(cache.chunks - printRangesMax) & " more .."
|
|
|
|
|
|
|
|
proc dump*(
|
|
|
|
range: NodeTagRangeSet;
|
|
|
|
printRangesMax = high(int);
|
|
|
|
): string =
|
|
|
|
## Ditto
|
|
|
|
[range].dump(nil, printRangesMax)
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|