2022-05-09 14:04:48 +00:00
|
|
|
# Nimbus - New sync approach - A fusion of snap, trie, beam and other methods
|
|
|
|
#
|
|
|
|
# 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
|
2022-05-23 16:53:19 +00:00
|
|
|
std/[hashes, strutils],
|
2022-05-09 14:04:48 +00:00
|
|
|
chronicles,
|
|
|
|
chronos,
|
2022-05-23 16:53:19 +00:00
|
|
|
eth/[common/eth_types, p2p, p2p/peer_pool, p2p/private/p2p_types],
|
|
|
|
stew/keyed_queue,
|
|
|
|
"."/[protocol, types],
|
2022-05-24 08:07:39 +00:00
|
|
|
./snap/worker
|
2022-05-13 16:30:10 +00:00
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2022-05-23 16:53:19 +00:00
|
|
|
logScope:
|
|
|
|
topics = "snap sync"
|
|
|
|
|
2022-05-13 16:30:10 +00:00
|
|
|
type
|
2022-05-24 08:07:39 +00:00
|
|
|
SnapSyncCtx* = ref object of Worker
|
|
|
|
peerTab: KeyedQueue[Peer,WorkerBuddy] ## LRU cache
|
|
|
|
tabSize: int ## maximal number of entries
|
|
|
|
pool: PeerPool ## for starting the system, debugging
|
2022-05-23 16:53:19 +00:00
|
|
|
|
|
|
|
# debugging
|
|
|
|
lastDump: seq[string]
|
|
|
|
lastlen: int
|
2022-05-13 16:30:10 +00:00
|
|
|
|
2022-05-17 11:09:49 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-05-24 08:07:39 +00:00
|
|
|
proc nsCtx(sp: WorkerBuddy): SnapSyncCtx =
|
2022-05-23 16:53:19 +00:00
|
|
|
sp.ns.SnapSyncCtx
|
2022-05-17 11:09:49 +00:00
|
|
|
|
2022-05-23 16:53:19 +00:00
|
|
|
proc hash(peer: Peer): Hash =
|
|
|
|
## Needed for `peerTab` table key comparison
|
|
|
|
hash(peer.remote.id)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private debugging helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc dumpPeers(sn: SnapSyncCtx; force = false) =
|
|
|
|
if sn.lastLen != sn.peerTab.len or force:
|
|
|
|
sn.lastLen = sn.peerTab.len
|
|
|
|
|
|
|
|
let poolSize = sn.pool.len
|
|
|
|
if sn.peerTab.len == 0:
|
|
|
|
trace "*** Empty peer list", poolSize
|
|
|
|
else:
|
|
|
|
var n = sn.peerTab.len - 1
|
|
|
|
for sp in sn.peerTab.prevValues:
|
|
|
|
trace "*** Peer list entry",
|
2022-05-24 08:07:39 +00:00
|
|
|
n, poolSize, peer=sp, worker=sp.huntPp
|
2022-05-23 16:53:19 +00:00
|
|
|
n.dec
|
2022-05-17 11:09:49 +00:00
|
|
|
|
2022-05-13 16:30:10 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-05-09 14:04:48 +00:00
|
|
|
|
2022-05-24 08:07:39 +00:00
|
|
|
proc syncPeerLoop(sp: WorkerBuddy) {.async.} =
|
2022-05-09 14:04:48 +00:00
|
|
|
# This basic loop just runs the head-hunter for each peer.
|
2022-05-23 16:53:19 +00:00
|
|
|
var cache = ""
|
2022-05-24 08:07:39 +00:00
|
|
|
while sp.ctrl.runState != BuddyStopped:
|
2022-05-23 16:53:19 +00:00
|
|
|
|
|
|
|
# Do something, work a bit
|
2022-05-24 08:07:39 +00:00
|
|
|
await sp.workerExec
|
|
|
|
if sp.ctrl.runState == BuddyStopped:
|
2022-05-23 16:53:19 +00:00
|
|
|
trace "Ignoring stopped peer", peer=sp
|
2022-05-09 14:04:48 +00:00
|
|
|
return
|
2022-05-23 16:53:19 +00:00
|
|
|
|
|
|
|
# Rotate LRU connection table so the most used entry is at the list end
|
|
|
|
# TODO: Update implementation of lruFetch() using re-link, only
|
|
|
|
discard sp.nsCtx.peerTab.lruFetch(sp.peer)
|
|
|
|
|
2022-05-24 08:07:39 +00:00
|
|
|
let delayMs = if sp.workerLockedOk: 1000 else: 50
|
2022-05-09 14:04:48 +00:00
|
|
|
await sleepAsync(chronos.milliseconds(delayMs))
|
|
|
|
|
2022-05-17 11:09:49 +00:00
|
|
|
|
2022-05-24 08:07:39 +00:00
|
|
|
proc syncPeerStart(sp: WorkerBuddy) =
|
2022-05-09 14:04:48 +00:00
|
|
|
asyncSpawn sp.syncPeerLoop()
|
|
|
|
|
2022-05-24 08:07:39 +00:00
|
|
|
proc syncPeerStop(sp: WorkerBuddy) =
|
|
|
|
sp.ctrl.runState = BuddyStopped
|
|
|
|
# TODO: Cancel running `WorkerBuddy` instances. We need clean cancellation
|
2022-05-17 11:09:49 +00:00
|
|
|
# for this. Doing so reliably will be addressed at a later time.
|
2022-05-09 14:04:48 +00:00
|
|
|
|
|
|
|
|
2022-05-17 11:09:49 +00:00
|
|
|
proc onPeerConnected(ns: SnapSyncCtx, peer: Peer) =
|
2022-05-23 16:53:19 +00:00
|
|
|
trace "Peer connected", peer
|
2022-05-17 11:09:49 +00:00
|
|
|
|
2022-05-24 08:07:39 +00:00
|
|
|
let sp = WorkerBuddy.new(ns, peer, BuddyRunningOk)
|
2022-05-23 16:53:19 +00:00
|
|
|
|
|
|
|
# Manage connection table, check for existing entry
|
|
|
|
if ns.peerTab.hasKey(peer):
|
2022-05-24 08:07:39 +00:00
|
|
|
trace "Peer exists already!", peer # can this happen, at all?
|
2022-05-23 16:53:19 +00:00
|
|
|
return
|
|
|
|
|
2022-05-24 08:07:39 +00:00
|
|
|
# Initialise snap sync for this peer
|
|
|
|
discard sp.workerStart
|
|
|
|
|
2022-05-23 16:53:19 +00:00
|
|
|
# Check for table overflow. An overflow should not happen if the table is
|
|
|
|
# as large as the peer connection table.
|
|
|
|
if ns.tabSize <= ns.peerTab.len:
|
|
|
|
let leastPeer = ns.peerTab.shift.value.data
|
|
|
|
leastPeer.syncPeerStop
|
|
|
|
trace "Peer table full, deleted least used",
|
|
|
|
leastPeer, poolSize=ns.pool.len, tabLen=ns.peerTab.len, tabMax=ns.tabSize
|
|
|
|
|
|
|
|
# Add peer entry
|
|
|
|
discard ns.peerTab.append(sp.peer,sp)
|
|
|
|
trace "Starting peer",
|
|
|
|
peer, poolSize=ns.pool.len, tabLen=ns.peerTab.len, tabMax=ns.tabSize
|
|
|
|
|
|
|
|
# Debugging, peer table dump after adding gentry
|
|
|
|
#ns.dumpPeers(true)
|
2022-05-09 14:04:48 +00:00
|
|
|
sp.syncPeerStart()
|
|
|
|
|
2022-05-17 11:09:49 +00:00
|
|
|
proc onPeerDisconnected(ns: SnapSyncCtx, peer: Peer) =
|
2022-05-23 16:53:19 +00:00
|
|
|
trace "Peer disconnected", peer
|
|
|
|
|
|
|
|
# Debugging, peer table dump before removing entry
|
|
|
|
#ns.dumpPeers(true)
|
2022-05-09 14:04:48 +00:00
|
|
|
|
2022-05-23 16:53:19 +00:00
|
|
|
let rc = ns.peerTab.delete(peer)
|
|
|
|
if rc.isOk:
|
|
|
|
rc.value.data.syncPeerStop()
|
2022-05-17 11:09:49 +00:00
|
|
|
else:
|
2022-05-23 16:53:19 +00:00
|
|
|
debug "Disconnected from unregistered peer", peer
|
2022-05-09 14:04:48 +00:00
|
|
|
|
2022-05-13 16:30:10 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-05-09 14:04:48 +00:00
|
|
|
|
2022-05-23 16:53:19 +00:00
|
|
|
proc new*(T: type SnapSyncCtx; ethNode: EthereumNode; maxPeers: int): T =
|
2022-05-13 16:30:10 +00:00
|
|
|
## Constructor
|
|
|
|
new result
|
2022-05-23 16:53:19 +00:00
|
|
|
let size = max(1,2*maxPeers) # allow double argument size
|
|
|
|
result.peerTab.init(size)
|
|
|
|
result.tabSize = size
|
|
|
|
result.pool = ethNode.peerPool
|
2022-05-13 16:30:10 +00:00
|
|
|
|
|
|
|
proc start*(ctx: SnapSyncCtx) =
|
|
|
|
## Set up syncing. This call should come early.
|
2022-05-09 14:04:48 +00:00
|
|
|
var po = PeerObserver(
|
|
|
|
onPeerConnected:
|
2022-05-13 16:30:10 +00:00
|
|
|
proc(p: Peer) {.gcsafe.} =
|
|
|
|
ctx.onPeerConnected(p),
|
2022-05-09 14:04:48 +00:00
|
|
|
onPeerDisconnected:
|
2022-05-13 16:30:10 +00:00
|
|
|
proc(p: Peer) {.gcsafe.} =
|
|
|
|
ctx.onPeerDisconnected(p))
|
|
|
|
po.setProtocol eth
|
2022-05-23 16:53:19 +00:00
|
|
|
ctx.pool.addObserver(ctx, po)
|
2022-05-13 16:30:10 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|