nimbus-eth1/nimbus/sync/handlers/snap.nim

146 lines
4.2 KiB
Nim
Raw Normal View History

# Nimbus
# Copyright (c) 2018-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.
{.push raises: [].}
import
chronicles,
eth/p2p,
../snap/worker/db/hexary_range,
../protocol,
../protocol/snap/snap_types,
../../core/chain
logScope:
topics = "wire-protocol"
type
SnapWireRef* = ref object of SnapWireBase
chain: ChainRef
peerPool: PeerPool
const
proofNodeSizeMax = 532
## Branch node with all branches `high(UInt256)` within RLP list
# ------------------------------------------------------------------------------
# Private functions: helper functions
# ------------------------------------------------------------------------------
proc notImplemented(name: string) =
debug "snapWire: hHandler method not implemented", meth=name
proc append(writer: var RlpWriter; t: SnapProof; node: Blob) =
## RLP mixin, encoding
writer.snapAppend node
# ------------------------------------------------------------------------------
# Private functions: peer observer
# ------------------------------------------------------------------------------
#proc onPeerConnected(ctx: SnapWireRef, peer: Peer) =
# debug "snapWire: add peer", peer
# discard
#
#proc onPeerDisconnected(ctx: SnapWireRef, peer: Peer) =
# debug "snapWire: remove peer", peer
# discard
#
#proc setupPeerObserver(ctx: SnapWireRef) =
# var po = PeerObserver(
# onPeerConnected:
# proc(p: Peer) {.gcsafe.} =
# ctx.onPeerConnected(p),
# onPeerDisconnected:
# proc(p: Peer) {.gcsafe.} =
# ctx.onPeerDisconnected(p))
# po.setProtocol protocol.snap
# ctx.peerPool.addObserver(ctx, po)
# ------------------------------------------------------------------------------
# Public constructor/destructor
# ------------------------------------------------------------------------------
proc init*(
T: type SnapWireRef;
chain: ChainRef;
peerPool: PeerPool;
): T =
## Constructor (uses `init()` as suggested in style guide.)
let ctx = T(
chain: chain,
peerPool: peerPool)
#ctx.setupPeerObserver()
ctx
# ------------------------------------------------------------------------------
# Public functions: helpers
# ------------------------------------------------------------------------------
proc proofNodesSizeMax*(n: int): int =
## Max number of bytes needed to store a list of `n` RLP encoded hexary
## nodes which is a `Branch` node where every link reference is set to
## `high(UInt256)`.
const nMax = high(int) div proofNodeSizeMax
if n <= nMax:
hexaryRangeRlpSize(n * proofNodeSizeMax)
else:
high(int)
proc proofEncode*(proof: seq[SnapProof]): Blob =
rlp.encode proof
# ------------------------------------------------------------------------------
# Public functions: snap wire protocol handlers
# ------------------------------------------------------------------------------
method getAccountRange*(
ctx: SnapWireRef;
root: Hash256;
origin: Hash256;
limit: Hash256;
replySizeMax: uint64;
): (seq[SnapAccount], seq[SnapProof])
{.gcsafe.} =
notImplemented("getAccountRange")
method getStorageRanges*(
ctx: SnapWireRef;
root: Hash256;
accounts: openArray[Hash256];
origin: openArray[byte];
limit: openArray[byte];
replySizeMax: uint64;
): (seq[seq[SnapStorage]], seq[SnapProof])
{.gcsafe.} =
notImplemented("getStorageRanges")
method getByteCodes*(
ctx: SnapWireRef;
nodes: openArray[Hash256];
replySizeMax: uint64;
): seq[Blob]
{.gcsafe.} =
notImplemented("getByteCodes")
method getTrieNodes*(
ctx: SnapWireRef;
root: Hash256;
paths: openArray[seq[Blob]];
replySizeMax: uint64;
): seq[Blob]
{.gcsafe.} =
notImplemented("getTrieNodes")
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------