2022-08-15 15:51:50 +00:00
|
|
|
# nimbus-eth1
|
|
|
|
# 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
|
2023-01-30 22:10:23 +00:00
|
|
|
std/[sets, tables],
|
2022-10-20 16:59:54 +00:00
|
|
|
eth/[common, trie/nibbles],
|
2022-09-16 07:24:12 +00:00
|
|
|
../../range_desc,
|
2022-09-30 08:22:14 +00:00
|
|
|
"."/[hexary_desc, hexary_error]
|
2022-08-15 15:51:50 +00:00
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
{.push raises: [].}
|
2022-08-15 15:51:50 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private debugging helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
#proc pp(q: openArray[byte]): string =
|
|
|
|
# q.toSeq.mapIt(it.toHex(2)).join.toLowerAscii.pp(hex = true)
|
2022-08-15 15:51:50 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc hexaryImport*(
|
2022-09-16 07:24:12 +00:00
|
|
|
db: HexaryTreeDbRef; ## Contains node table
|
2022-09-02 18:16:09 +00:00
|
|
|
recData: Blob; ## Node to add
|
|
|
|
unrefNodes: var HashSet[RepairKey]; ## Keep track of freestanding nodes
|
|
|
|
nodeRefs: var HashSet[RepairKey]; ## Ditto
|
2022-10-14 16:40:32 +00:00
|
|
|
): HexaryNodeReport
|
2023-01-30 22:10:23 +00:00
|
|
|
{.gcsafe, raises: [RlpError, KeyError].} =
|
2022-08-15 15:51:50 +00:00
|
|
|
## Decode a single trie item for adding to the table and add it to the
|
|
|
|
## database. Branch and exrension record links are collected.
|
2022-10-14 16:40:32 +00:00
|
|
|
if recData.len == 0:
|
|
|
|
return HexaryNodeReport(error: RlpNonEmptyBlobExpected)
|
2022-08-15 15:51:50 +00:00
|
|
|
let
|
|
|
|
nodeKey = recData.digestTo(NodeKey)
|
|
|
|
repairKey = nodeKey.to(RepairKey) # for repair table
|
|
|
|
var
|
|
|
|
rlp = recData.rlpFromBytes
|
|
|
|
blobs = newSeq[Blob](2) # temporary, cache
|
|
|
|
links: array[16,RepairKey] # reconstruct branch node
|
|
|
|
blob16: Blob # reconstruct branch node
|
|
|
|
top = 0 # count entries
|
|
|
|
rNode: RNodeRef # repair tree node
|
|
|
|
|
|
|
|
# Collect lists of either 2 or 17 blob entries.
|
|
|
|
for w in rlp.items:
|
|
|
|
case top
|
|
|
|
of 0, 1:
|
|
|
|
if not w.isBlob:
|
2022-10-14 16:40:32 +00:00
|
|
|
return HexaryNodeReport(error: RlpBlobExpected)
|
2022-08-15 15:51:50 +00:00
|
|
|
blobs[top] = rlp.read(Blob)
|
|
|
|
of 2 .. 15:
|
|
|
|
var key: NodeKey
|
|
|
|
if not key.init(rlp.read(Blob)):
|
2022-10-14 16:40:32 +00:00
|
|
|
return HexaryNodeReport(error: RlpBranchLinkExpected)
|
2022-08-15 15:51:50 +00:00
|
|
|
# Update ref pool
|
|
|
|
links[top] = key.to(RepairKey)
|
2022-09-02 18:16:09 +00:00
|
|
|
unrefNodes.excl links[top] # is referenced, now (if any)
|
|
|
|
nodeRefs.incl links[top]
|
2022-08-15 15:51:50 +00:00
|
|
|
of 16:
|
|
|
|
if not w.isBlob:
|
2022-10-14 16:40:32 +00:00
|
|
|
return HexaryNodeReport(error: RlpBlobExpected)
|
2022-08-15 15:51:50 +00:00
|
|
|
blob16 = rlp.read(Blob)
|
|
|
|
else:
|
2022-10-14 16:40:32 +00:00
|
|
|
return HexaryNodeReport(error: Rlp2Or17ListEntries)
|
2022-08-15 15:51:50 +00:00
|
|
|
top.inc
|
|
|
|
|
|
|
|
# Verify extension data
|
|
|
|
case top
|
|
|
|
of 2:
|
|
|
|
if blobs[0].len == 0:
|
2022-10-14 16:40:32 +00:00
|
|
|
return HexaryNodeReport(error: RlpNonEmptyBlobExpected)
|
2022-08-15 15:51:50 +00:00
|
|
|
let (isLeaf, pathSegment) = hexPrefixDecode blobs[0]
|
|
|
|
if isLeaf:
|
|
|
|
rNode = RNodeRef(
|
|
|
|
kind: Leaf,
|
|
|
|
lPfx: pathSegment,
|
|
|
|
lData: blobs[1])
|
|
|
|
else:
|
|
|
|
var key: NodeKey
|
|
|
|
if not key.init(blobs[1]):
|
2022-10-14 16:40:32 +00:00
|
|
|
return HexaryNodeReport(error: RlpExtPathEncoding)
|
2022-08-15 15:51:50 +00:00
|
|
|
# Update ref pool
|
|
|
|
rNode = RNodeRef(
|
|
|
|
kind: Extension,
|
|
|
|
ePfx: pathSegment,
|
|
|
|
eLink: key.to(RepairKey))
|
2022-09-02 18:16:09 +00:00
|
|
|
unrefNodes.excl rNode.eLink # is referenced, now (if any)
|
|
|
|
nodeRefs.incl rNode.eLink
|
2022-08-15 15:51:50 +00:00
|
|
|
of 17:
|
|
|
|
for n in [0,1]:
|
|
|
|
var key: NodeKey
|
|
|
|
if not key.init(blobs[n]):
|
2022-10-14 16:40:32 +00:00
|
|
|
return HexaryNodeReport(error: RlpBranchLinkExpected)
|
2022-08-15 15:51:50 +00:00
|
|
|
# Update ref pool
|
|
|
|
links[n] = key.to(RepairKey)
|
2022-09-02 18:16:09 +00:00
|
|
|
unrefNodes.excl links[n] # is referenced, now (if any)
|
|
|
|
nodeRefs.incl links[n]
|
2022-08-15 15:51:50 +00:00
|
|
|
rNode = RNodeRef(
|
|
|
|
kind: Branch,
|
|
|
|
bLink: links,
|
|
|
|
bData: blob16)
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
# Add to database
|
|
|
|
if not db.tab.hasKey(repairKey):
|
|
|
|
db.tab[repairKey] = rNode
|
2022-08-15 15:51:50 +00:00
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
# Update unreferenced nodes list
|
|
|
|
if repairKey notin nodeRefs:
|
|
|
|
unrefNodes.incl repairKey # keep track of stray nodes
|
2022-08-15 15:51:50 +00:00
|
|
|
|
2022-09-02 18:16:09 +00:00
|
|
|
elif db.tab[repairKey].convertTo(Blob) != recData:
|
2022-10-14 16:40:32 +00:00
|
|
|
return HexaryNodeReport(error: DifferentNodeValueExists)
|
2022-08-15 15:51:50 +00:00
|
|
|
|
2022-10-14 16:40:32 +00:00
|
|
|
HexaryNodeReport(kind: some(rNode.kind))
|
2022-08-15 15:51:50 +00:00
|
|
|
|
2022-09-16 07:24:12 +00:00
|
|
|
|
|
|
|
proc hexaryImport*(
|
|
|
|
db: HexaryTreeDbRef; ## Contains node table
|
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
|
|
|
rec: NodeSpecs; ## Expected key and value data pair
|
2022-10-14 16:40:32 +00:00
|
|
|
): HexaryNodeReport
|
2023-01-30 22:10:23 +00:00
|
|
|
{.gcsafe, raises: [RlpError, KeyError].} =
|
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
|
|
|
## Ditto without referece checks but expected node key argument.
|
|
|
|
if rec.data.len == 0:
|
2022-10-14 16:40:32 +00:00
|
|
|
return HexaryNodeReport(error: RlpNonEmptyBlobExpected)
|
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 rec.nodeKey != rec.data.digestTo(NodeKey):
|
|
|
|
return HexaryNodeReport(error: ExpectedNodeKeyDiffers)
|
|
|
|
|
2022-09-16 07:24:12 +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
|
|
|
repairKey = rec.nodeKey.to(RepairKey) # for repair table
|
2022-09-16 07:24:12 +00:00
|
|
|
var
|
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
|
|
|
rlp = rec.data.rlpFromBytes
|
2022-09-16 07:24:12 +00:00
|
|
|
blobs = newSeq[Blob](2) # temporary, cache
|
|
|
|
links: array[16,RepairKey] # reconstruct branch node
|
|
|
|
blob16: Blob # reconstruct branch node
|
|
|
|
top = 0 # count entries
|
|
|
|
rNode: RNodeRef # repair tree node
|
|
|
|
|
|
|
|
# Collect lists of either 2 or 17 blob entries.
|
|
|
|
for w in rlp.items:
|
|
|
|
case top
|
|
|
|
of 0, 1:
|
|
|
|
if not w.isBlob:
|
2022-10-14 16:40:32 +00:00
|
|
|
return HexaryNodeReport(error: RlpBlobExpected)
|
2022-09-16 07:24:12 +00:00
|
|
|
blobs[top] = rlp.read(Blob)
|
|
|
|
of 2 .. 15:
|
|
|
|
var key: NodeKey
|
|
|
|
if not key.init(rlp.read(Blob)):
|
2022-10-14 16:40:32 +00:00
|
|
|
return HexaryNodeReport(error: RlpBranchLinkExpected)
|
2022-09-16 07:24:12 +00:00
|
|
|
# Update ref pool
|
|
|
|
links[top] = key.to(RepairKey)
|
|
|
|
of 16:
|
|
|
|
if not w.isBlob:
|
2022-10-14 16:40:32 +00:00
|
|
|
return HexaryNodeReport(error: RlpBlobExpected)
|
2022-09-16 07:24:12 +00:00
|
|
|
blob16 = rlp.read(Blob)
|
|
|
|
else:
|
2022-10-14 16:40:32 +00:00
|
|
|
return HexaryNodeReport(error: Rlp2Or17ListEntries)
|
2022-09-16 07:24:12 +00:00
|
|
|
top.inc
|
|
|
|
|
|
|
|
# Verify extension data
|
|
|
|
case top
|
|
|
|
of 2:
|
|
|
|
if blobs[0].len == 0:
|
2022-10-14 16:40:32 +00:00
|
|
|
return HexaryNodeReport(error: RlpNonEmptyBlobExpected)
|
2022-09-16 07:24:12 +00:00
|
|
|
let (isLeaf, pathSegment) = hexPrefixDecode blobs[0]
|
|
|
|
if isLeaf:
|
|
|
|
rNode = RNodeRef(
|
|
|
|
kind: Leaf,
|
|
|
|
lPfx: pathSegment,
|
|
|
|
lData: blobs[1])
|
|
|
|
else:
|
|
|
|
var key: NodeKey
|
|
|
|
if not key.init(blobs[1]):
|
2022-10-14 16:40:32 +00:00
|
|
|
return HexaryNodeReport(error: RlpExtPathEncoding)
|
2022-09-16 07:24:12 +00:00
|
|
|
# Update ref pool
|
|
|
|
rNode = RNodeRef(
|
|
|
|
kind: Extension,
|
|
|
|
ePfx: pathSegment,
|
|
|
|
eLink: key.to(RepairKey))
|
|
|
|
of 17:
|
|
|
|
for n in [0,1]:
|
|
|
|
var key: NodeKey
|
|
|
|
if not key.init(blobs[n]):
|
2022-10-14 16:40:32 +00:00
|
|
|
return HexaryNodeReport(error: RlpBranchLinkExpected)
|
2022-09-16 07:24:12 +00:00
|
|
|
# Update ref pool
|
|
|
|
links[n] = key.to(RepairKey)
|
|
|
|
rNode = RNodeRef(
|
|
|
|
kind: Branch,
|
|
|
|
bLink: links,
|
|
|
|
bData: blob16)
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
|
|
|
# Add to database
|
|
|
|
if not db.tab.hasKey(repairKey):
|
|
|
|
db.tab[repairKey] = rNode
|
|
|
|
|
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 db.tab[repairKey].convertTo(Blob) != rec.data:
|
2022-10-14 16:40:32 +00:00
|
|
|
return HexaryNodeReport(error: DifferentNodeValueExists)
|
2022-09-16 07:24:12 +00:00
|
|
|
|
2022-10-14 16:40:32 +00:00
|
|
|
HexaryNodeReport(kind: some(rNode.kind))
|
2022-09-16 07:24:12 +00:00
|
|
|
|
2022-08-15 15:51:50 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|