2022-05-09 14:04:48 +00:00
|
|
|
# Nimbus - Ethereum Snap Protocol (SNAP), version 1
|
|
|
|
#
|
2024-02-19 11:46:48 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2022-05-09 14:04:48 +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-03-10 17:10:30 +00:00
|
|
|
## This module implements Ethereum Snapshot Protocol version 1, `snap/1`.
|
|
|
|
## Specification:
|
|
|
|
## `snap/1 <https://github.com/ethereum/devp2p/blob/master/caps/snap.md>`_
|
2024-08-30 11:18:36 +00:00
|
|
|
##
|
|
|
|
## Use NIM command line optipn `-d:p2pProtocolDebug` for dumping the
|
|
|
|
## generated driver code (just to have it stored somewhere lest one forgets.)
|
2022-05-09 14:04:48 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
std/options,
|
|
|
|
chronicles,
|
|
|
|
chronos,
|
2022-10-10 02:31:28 +00:00
|
|
|
eth/[common, p2p, p2p/private/p2p_types],
|
2023-01-20 15:01:29 +00:00
|
|
|
./snap/snap_types,
|
|
|
|
../../constants
|
2022-05-23 16:53:19 +00:00
|
|
|
|
2023-01-20 15:01:29 +00:00
|
|
|
export
|
|
|
|
snap_types
|
2022-05-09 14:04:48 +00:00
|
|
|
|
2023-01-20 15:01:29 +00:00
|
|
|
logScope:
|
|
|
|
topics = "snap1"
|
2022-05-09 14:04:48 +00:00
|
|
|
|
2022-05-10 08:02:34 +00:00
|
|
|
const
|
|
|
|
snapVersion* = 1
|
|
|
|
prettySnapProtoName* = "[snap/" & $snapVersion & "]"
|
|
|
|
|
2022-05-23 16:53:19 +00:00
|
|
|
# Pickeled tracer texts
|
|
|
|
trSnapRecvReceived* =
|
|
|
|
"<< " & prettySnapProtoName & " Received "
|
|
|
|
trSnapRecvProtocolViolation* =
|
|
|
|
"<< " & prettySnapProtoName & " Protocol violation, "
|
|
|
|
trSnapRecvError* =
|
|
|
|
"<< " & prettySnapProtoName & " Error "
|
|
|
|
trSnapRecvTimeoutWaiting* =
|
|
|
|
"<< " & prettySnapProtoName & " Timeout waiting "
|
2022-06-06 13:42:08 +00:00
|
|
|
|
2022-05-23 16:53:19 +00:00
|
|
|
trSnapSendSending* =
|
|
|
|
">> " & prettySnapProtoName & " Sending "
|
|
|
|
trSnapSendReplying* =
|
|
|
|
">> " & prettySnapProtoName & " Replying "
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
|
2022-07-01 11:42:17 +00:00
|
|
|
proc read(rlp: var Rlp, t: var SnapAccount, T: type Account): T =
|
2023-02-15 10:14:40 +00:00
|
|
|
## RLP mixin, decoding
|
|
|
|
rlp.snapRead T
|
2022-05-09 14:04:48 +00:00
|
|
|
|
2023-03-03 20:01:59 +00:00
|
|
|
proc read(rlp: var Rlp; T: type SnapProofNodes): T =
|
2023-02-15 10:14:40 +00:00
|
|
|
## RLP mixin, decoding
|
|
|
|
rlp.snapRead T
|
|
|
|
|
2023-03-22 20:11:49 +00:00
|
|
|
proc read(rlp: var Rlp; T: type SnapTriePaths): T =
|
|
|
|
## RLP mixin, decoding
|
|
|
|
rlp.snapRead T
|
|
|
|
|
2023-02-15 10:14:40 +00:00
|
|
|
proc append(writer: var RlpWriter, t: SnapAccount, account: Account) =
|
|
|
|
## RLP mixin, encoding
|
|
|
|
writer.snapAppend account
|
|
|
|
|
2023-03-03 20:01:59 +00:00
|
|
|
proc append(writer: var RlpWriter; spn: SnapProofNodes) =
|
2023-02-15 10:14:40 +00:00
|
|
|
## RLP mixin, encoding
|
2023-03-03 20:01:59 +00:00
|
|
|
writer.snapAppend spn
|
2022-05-09 14:04:48 +00:00
|
|
|
|
2023-03-22 20:11:49 +00:00
|
|
|
proc append(writer: var RlpWriter; stn: SnapTriePaths) =
|
|
|
|
## RLP mixin, encoding
|
|
|
|
writer.snapAppend stn
|
|
|
|
|
2024-02-19 11:46:48 +00:00
|
|
|
template handleHandlerError(x: untyped) =
|
|
|
|
if x.isErr:
|
|
|
|
raise newException(EthP2PError, x.error)
|
2022-05-09 14:04:48 +00:00
|
|
|
|
2022-12-09 13:43:55 +00:00
|
|
|
p2pProtocol snap1(version = snapVersion,
|
2022-05-09 14:04:48 +00:00
|
|
|
rlpxName = "snap",
|
2023-01-20 15:01:29 +00:00
|
|
|
peerState = SnapPeerState,
|
|
|
|
networkState = SnapWireBase,
|
2022-05-09 14:04:48 +00:00
|
|
|
useRequestIds = true):
|
|
|
|
|
|
|
|
requestResponse:
|
|
|
|
# User message 0x00: GetAccountRange.
|
2023-01-20 15:01:29 +00:00
|
|
|
proc getAccountRange(
|
|
|
|
peer: Peer;
|
2024-10-01 09:19:29 +00:00
|
|
|
root: Hash32;
|
2023-03-10 17:10:30 +00:00
|
|
|
origin: openArray[byte];
|
|
|
|
limit: openArray[byte];
|
2023-01-20 15:01:29 +00:00
|
|
|
replySizeMax: uint64;
|
|
|
|
) =
|
|
|
|
trace trSnapRecvReceived & "GetAccountRange (0x00)", peer, root,
|
2023-03-10 17:10:30 +00:00
|
|
|
nOrigin=origin.len, nLimit=limit.len, replySizeMax
|
2023-01-20 15:01:29 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
ctx = peer.networkState()
|
2024-02-19 11:46:48 +00:00
|
|
|
res = ctx.getAccountRange(
|
2023-01-20 15:01:29 +00:00
|
|
|
root, origin, limit, replySizeMax)
|
2024-02-19 11:46:48 +00:00
|
|
|
handleHandlerError(res)
|
2022-05-09 14:04:48 +00:00
|
|
|
|
2024-02-19 11:46:48 +00:00
|
|
|
let
|
|
|
|
(accounts, proof) = res.get
|
2023-01-20 15:01:29 +00:00
|
|
|
# For logging only
|
|
|
|
nAccounts = accounts.len
|
2023-03-03 20:01:59 +00:00
|
|
|
nProof = proof.nodes.len
|
2023-01-20 15:01:29 +00:00
|
|
|
|
|
|
|
if nAccounts == 0 and nProof == 0:
|
|
|
|
trace trSnapSendReplying & "EMPTY AccountRange (0x01)", peer
|
|
|
|
else:
|
|
|
|
trace trSnapSendReplying & "AccountRange (0x01)", peer,
|
|
|
|
nAccounts, nProof
|
|
|
|
|
|
|
|
await response.send(accounts, proof)
|
2022-05-09 14:04:48 +00:00
|
|
|
|
|
|
|
# User message 0x01: AccountRange.
|
2023-01-20 15:01:29 +00:00
|
|
|
proc accountRange(
|
|
|
|
peer: Peer;
|
2023-02-15 10:14:40 +00:00
|
|
|
accounts: openArray[SnapAccount];
|
2023-03-03 20:01:59 +00:00
|
|
|
proof: SnapProofNodes)
|
2023-01-20 15:01:29 +00:00
|
|
|
|
2022-05-09 14:04:48 +00:00
|
|
|
|
|
|
|
requestResponse:
|
|
|
|
# User message 0x02: GetStorageRanges.
|
2023-01-20 15:01:29 +00:00
|
|
|
proc getStorageRanges(
|
|
|
|
peer: Peer;
|
2024-10-01 09:19:29 +00:00
|
|
|
root: Hash32;
|
|
|
|
accounts: openArray[Hash32];
|
2023-01-20 15:01:29 +00:00
|
|
|
origin: openArray[byte];
|
|
|
|
limit: openArray[byte];
|
|
|
|
replySizeMax: uint64;
|
|
|
|
) =
|
|
|
|
trace trSnapRecvReceived & "GetStorageRanges (0x02)", peer, root,
|
|
|
|
nAccounts=accounts.len, nOrigin=origin.len, nLimit=limit.len,
|
|
|
|
replySizeMax
|
|
|
|
|
|
|
|
let
|
|
|
|
ctx = peer.networkState()
|
2024-02-19 11:46:48 +00:00
|
|
|
res = ctx.getStorageRanges(
|
2023-01-20 15:01:29 +00:00
|
|
|
root, accounts, origin, limit, replySizeMax)
|
2024-02-19 11:46:48 +00:00
|
|
|
handleHandlerError(res)
|
2023-01-20 15:01:29 +00:00
|
|
|
|
2024-02-19 11:46:48 +00:00
|
|
|
let
|
|
|
|
(slots, proof) = res.get
|
2023-01-20 15:01:29 +00:00
|
|
|
# For logging only
|
|
|
|
nSlots = slots.len
|
2023-03-03 20:01:59 +00:00
|
|
|
nProof = proof.nodes.len
|
2023-01-20 15:01:29 +00:00
|
|
|
|
|
|
|
if nSlots == 0 and nProof == 0:
|
|
|
|
trace trSnapSendReplying & "EMPTY StorageRanges (0x03)", peer
|
|
|
|
else:
|
|
|
|
trace trSnapSendReplying & "StorageRanges (0x03)", peer,
|
|
|
|
nSlots, nProof
|
|
|
|
|
|
|
|
await response.send(slots, proof)
|
2022-05-09 14:04:48 +00:00
|
|
|
|
|
|
|
# User message 0x03: StorageRanges.
|
|
|
|
# Note: See comments in this file for a list of Geth quirks to expect.
|
2023-01-20 15:01:29 +00:00
|
|
|
proc storageRanges(
|
|
|
|
peer: Peer;
|
|
|
|
slotLists: openArray[seq[SnapStorage]];
|
2023-03-03 20:01:59 +00:00
|
|
|
proof: SnapProofNodes)
|
2023-01-20 15:01:29 +00:00
|
|
|
|
2022-05-09 14:04:48 +00:00
|
|
|
|
|
|
|
requestResponse:
|
2023-01-20 15:01:29 +00:00
|
|
|
# User message 0x04: GetByteCodes.
|
|
|
|
proc getByteCodes(
|
|
|
|
peer: Peer;
|
2024-10-01 09:19:29 +00:00
|
|
|
nodes: openArray[Hash32];
|
2023-01-20 15:01:29 +00:00
|
|
|
replySizeMax: uint64;
|
|
|
|
) =
|
2022-05-23 16:53:19 +00:00
|
|
|
trace trSnapRecvReceived & "GetByteCodes (0x04)", peer,
|
2023-01-20 15:01:29 +00:00
|
|
|
nNodes=nodes.len, replySizeMax
|
2022-05-09 14:04:48 +00:00
|
|
|
|
2023-01-20 15:01:29 +00:00
|
|
|
let
|
|
|
|
ctx = peer.networkState()
|
|
|
|
codes = ctx.getByteCodes(nodes, replySizeMax)
|
|
|
|
|
2024-02-19 11:46:48 +00:00
|
|
|
handleHandlerError(codes)
|
|
|
|
|
|
|
|
let
|
2023-01-20 15:01:29 +00:00
|
|
|
# For logging only
|
2024-02-19 11:46:48 +00:00
|
|
|
nCodes = codes.get.len
|
2023-01-20 15:01:29 +00:00
|
|
|
|
|
|
|
if nCodes == 0:
|
|
|
|
trace trSnapSendReplying & "EMPTY ByteCodes (0x05)", peer
|
|
|
|
else:
|
|
|
|
trace trSnapSendReplying & "ByteCodes (0x05)", peer, nCodes
|
2024-02-19 11:46:48 +00:00
|
|
|
|
|
|
|
await response.send(codes.get)
|
2022-05-09 14:04:48 +00:00
|
|
|
|
|
|
|
# User message 0x05: ByteCodes.
|
2023-01-20 15:01:29 +00:00
|
|
|
proc byteCodes(
|
|
|
|
peer: Peer;
|
2024-10-01 09:19:29 +00:00
|
|
|
codes: openArray[seq[byte]])
|
2023-01-20 15:01:29 +00:00
|
|
|
|
2022-05-09 14:04:48 +00:00
|
|
|
|
|
|
|
requestResponse:
|
2023-01-20 15:01:29 +00:00
|
|
|
# User message 0x06: GetTrieNodes.
|
|
|
|
proc getTrieNodes(
|
|
|
|
peer: Peer;
|
2024-10-01 09:19:29 +00:00
|
|
|
root: Hash32;
|
2023-03-22 20:11:49 +00:00
|
|
|
pathGroups: openArray[SnapTriePaths];
|
2023-01-20 15:01:29 +00:00
|
|
|
replySizeMax: uint64;
|
|
|
|
) =
|
|
|
|
trace trSnapRecvReceived & "GetTrieNodes (0x06)", peer, root,
|
2023-03-22 20:11:49 +00:00
|
|
|
nPathGroups=pathGroups.len, replySizeMax
|
2022-05-09 14:04:48 +00:00
|
|
|
|
2023-01-20 15:01:29 +00:00
|
|
|
let
|
|
|
|
ctx = peer.networkState()
|
2023-03-22 20:11:49 +00:00
|
|
|
nodes = ctx.getTrieNodes(root, pathGroups, replySizeMax)
|
2023-01-20 15:01:29 +00:00
|
|
|
|
2024-02-19 11:46:48 +00:00
|
|
|
handleHandlerError(nodes)
|
|
|
|
|
|
|
|
let
|
2023-01-20 15:01:29 +00:00
|
|
|
# For logging only
|
2024-02-19 11:46:48 +00:00
|
|
|
nNodes = nodes.get.len
|
2023-01-20 15:01:29 +00:00
|
|
|
|
|
|
|
if nNodes == 0:
|
|
|
|
trace trSnapSendReplying & "EMPTY TrieNodes (0x07)", peer
|
|
|
|
else:
|
|
|
|
trace trSnapSendReplying & "TrieNodes (0x07)", peer, nNodes
|
|
|
|
|
2024-02-19 11:46:48 +00:00
|
|
|
await response.send(nodes.get)
|
2022-05-09 14:04:48 +00:00
|
|
|
|
|
|
|
# User message 0x07: TrieNodes.
|
2023-01-20 15:01:29 +00:00
|
|
|
proc trieNodes(
|
|
|
|
peer: Peer;
|
2024-10-01 09:19:29 +00:00
|
|
|
nodes: openArray[seq[byte]])
|
2023-01-20 15:01:29 +00:00
|
|
|
|
|
|
|
# End
|