2022-10-08 17:20:50 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
2022-09-16 07:24:12 +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.
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[options, sequtils],
|
|
|
|
chronos,
|
2022-10-20 16:59:54 +00:00
|
|
|
eth/[common, p2p],
|
2022-09-16 07:24:12 +00:00
|
|
|
"../../.."/[protocol, protocol/trace_config],
|
2022-11-01 15:07:44 +00:00
|
|
|
"../.."/[constants, range_desc, worker_desc],
|
2022-10-08 17:20:50 +00:00
|
|
|
./com_error
|
2022-09-16 07:24:12 +00:00
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "snap-fetch"
|
|
|
|
|
|
|
|
type
|
|
|
|
# SnapTrieNodes = object
|
|
|
|
# nodes*: seq[Blob]
|
|
|
|
|
|
|
|
GetTrieNodes* = object
|
|
|
|
leftOver*: seq[seq[Blob]]
|
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
|
|
|
nodes*: seq[NodeSpecs] ## `nodeKey` field unused with `NodeSpecs`
|
2022-09-16 07:24:12 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc getTrieNodesReq(
|
|
|
|
buddy: SnapBuddyRef;
|
|
|
|
stateRoot: Hash256;
|
|
|
|
paths: seq[seq[Blob]];
|
2022-11-01 15:07:44 +00:00
|
|
|
pivot: string;
|
2022-09-16 07:24:12 +00:00
|
|
|
): Future[Result[Option[SnapTrieNodes],void]]
|
|
|
|
{.async.} =
|
|
|
|
let
|
|
|
|
peer = buddy.peer
|
|
|
|
try:
|
|
|
|
let reply = await peer.getTrieNodes(stateRoot, paths, snapRequestBytesLimit)
|
|
|
|
return ok(reply)
|
|
|
|
|
|
|
|
except CatchableError as e:
|
2022-11-01 15:07:44 +00:00
|
|
|
trace trSnapRecvError & "waiting for GetByteCodes reply", peer, pivot,
|
2022-09-16 07:24:12 +00:00
|
|
|
error=e.msg
|
|
|
|
return err()
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc getTrieNodes*(
|
|
|
|
buddy: SnapBuddyRef;
|
2022-11-01 15:07:44 +00:00
|
|
|
stateRoot: Hash256; ## Current DB base (see `pivot` for logging)
|
|
|
|
paths: seq[seq[Blob]]; ## Nodes to fetch
|
|
|
|
pivot: string; ## For logging, instead of `stateRoot`
|
2022-09-16 07:24:12 +00:00
|
|
|
): Future[Result[GetTrieNodes,ComError]]
|
|
|
|
{.async.} =
|
|
|
|
## Fetch data using the `snap#` protocol, returns the trie nodes requested
|
|
|
|
## (if any.)
|
|
|
|
let
|
|
|
|
peer = buddy.peer
|
|
|
|
nPaths = paths.len
|
|
|
|
|
|
|
|
if nPaths == 0:
|
|
|
|
return err(ComEmptyRequestArguments)
|
|
|
|
|
|
|
|
let nTotal = paths.mapIt(it.len).foldl(a+b, 0)
|
|
|
|
|
|
|
|
if trSnapTracePacketsOk:
|
2022-11-01 15:07:44 +00:00
|
|
|
trace trSnapSendSending & "GetTrieNodes", peer, pivot,
|
2022-09-16 07:24:12 +00:00
|
|
|
nPaths, nTotal, bytesLimit=snapRequestBytesLimit
|
|
|
|
|
|
|
|
let trieNodes = block:
|
2022-11-01 15:07:44 +00:00
|
|
|
let rc = await buddy.getTrieNodesReq(stateRoot, paths, pivot)
|
2022-09-16 07:24:12 +00:00
|
|
|
if rc.isErr:
|
|
|
|
return err(ComNetworkProblem)
|
|
|
|
if rc.value.isNone:
|
2022-11-01 15:07:44 +00:00
|
|
|
trace trSnapRecvTimeoutWaiting & "for TrieNodes", peer, pivot, nPaths
|
2022-09-16 07:24:12 +00:00
|
|
|
return err(ComResponseTimeout)
|
|
|
|
let blobs = rc.value.get.nodes
|
|
|
|
if nTotal < blobs.len:
|
|
|
|
# Ooops, makes no sense
|
|
|
|
return err(ComTooManyTrieNodes)
|
|
|
|
blobs
|
|
|
|
|
|
|
|
let
|
|
|
|
nNodes = trieNodes.len
|
|
|
|
|
|
|
|
if nNodes == 0:
|
|
|
|
# github.com/ethereum/devp2p/blob/master/caps/snap.md#gettrienodes-0x06
|
|
|
|
#
|
|
|
|
# Notes:
|
|
|
|
# * Nodes must always respond to the query.
|
|
|
|
# * The returned nodes must be in the request order.
|
|
|
|
# * If the node does not have the state for the requested state root or for
|
|
|
|
# any requested account paths, it must return an empty reply. It is the
|
|
|
|
# responsibility of the caller to query an state not older than 128
|
|
|
|
# blocks; and the caller is expected to only ever query existing trie
|
|
|
|
# nodes.
|
|
|
|
# * The responding node is allowed to return less data than requested
|
|
|
|
# (serving QoS limits), but the node must return at least one trie node.
|
2022-11-01 15:07:44 +00:00
|
|
|
trace trSnapRecvReceived & "empty TrieNodes", peer, pivot, nPaths, nNodes
|
2022-09-16 07:24:12 +00:00
|
|
|
return err(ComNoByteCodesAvailable)
|
|
|
|
|
|
|
|
# Assemble return value
|
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
|
|
|
var dd = GetTrieNodes()
|
2022-09-16 07:24:12 +00:00
|
|
|
|
|
|
|
# For each request group/sub-sequence, analyse the results
|
|
|
|
var nInx = 0
|
|
|
|
block loop:
|
|
|
|
for n in 0 ..< nPaths:
|
|
|
|
let pathLen = paths[n].len
|
|
|
|
|
|
|
|
# Account node request
|
|
|
|
if pathLen < 2:
|
|
|
|
if trieNodes[nInx].len == 0:
|
|
|
|
dd.leftOver.add paths[n]
|
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
|
|
|
else:
|
|
|
|
dd.nodes.add NodeSpecs(
|
|
|
|
partialPath: paths[n][0],
|
|
|
|
data: trieNodes[nInx])
|
2022-09-16 07:24:12 +00:00
|
|
|
nInx.inc
|
|
|
|
if nInx < nNodes:
|
|
|
|
continue
|
|
|
|
# all the rest needs to be re-processed
|
|
|
|
dd.leftOver = dd.leftOver & paths[n+1 ..< nPaths]
|
|
|
|
break loop
|
|
|
|
|
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
|
|
|
# Storage request for account followed by storage slot paths
|
2022-09-16 07:24:12 +00:00
|
|
|
if 1 < pathLen:
|
|
|
|
var pushBack: seq[Blob]
|
|
|
|
for i in 1 ..< pathLen:
|
|
|
|
if trieNodes[nInx].len == 0:
|
|
|
|
pushBack.add paths[n][i]
|
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
|
|
|
else:
|
|
|
|
dd.nodes.add NodeSpecs(
|
|
|
|
partialPath: paths[n][i],
|
|
|
|
data: trieNodes[nInx])
|
|
|
|
nInx.inc
|
|
|
|
if nInx < nNodes:
|
|
|
|
continue
|
|
|
|
# all the rest needs to be re-processed
|
|
|
|
#
|
|
|
|
# add: account & pushBack & rest ...
|
|
|
|
dd.leftOver.add paths[n][0] & pushBack & paths[n][i+1 ..< pathLen]
|
|
|
|
dd.leftOver = dd.leftOver & paths[n+1 ..< nPaths]
|
|
|
|
break loop
|
2022-09-16 07:24:12 +00:00
|
|
|
if 0 < pushBack.len:
|
|
|
|
dd.leftOver.add paths[n][0] & pushBack
|
|
|
|
|
2022-11-01 15:07:44 +00:00
|
|
|
trace trSnapRecvReceived & "TrieNodes", peer, pivot,
|
2022-09-16 07:24:12 +00:00
|
|
|
nPaths, nNodes, nLeftOver=dd.leftOver.len
|
|
|
|
|
|
|
|
return ok(dd)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|