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-06-16 08:58:50 +00:00
|
|
|
std/hashes,
|
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:
|
2022-06-16 08:58:50 +00:00
|
|
|
topics = "snap-sync"
|
2022-05-23 16:53:19 +00:00
|
|
|
|
2022-05-13 16:30:10 +00:00
|
|
|
type
|
2022-05-24 08:07:39 +00:00
|
|
|
SnapSyncCtx* = ref object of Worker
|
2022-06-16 08:58:50 +00:00
|
|
|
buddies: KeyedQueue[Peer,WorkerBuddy] ## LRU cache with worker descriptors
|
|
|
|
pool: PeerPool ## for starting the system
|
2022-05-23 16:53:19 +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 =
|
2022-06-16 08:58:50 +00:00
|
|
|
## Needed for `buddies` table key comparison
|
2022-05-23 16:53:19 +00:00
|
|
|
hash(peer.remote.id)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-06-16 08:58:50 +00:00
|
|
|
# Private functions
|
2022-05-23 16:53:19 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-06-16 08:58:50 +00:00
|
|
|
proc workerLoop(sp: WorkerBuddy) {.async.} =
|
|
|
|
let ns = sp.nsCtx
|
|
|
|
trace "Starting peer worker", peer=sp,
|
|
|
|
peers=ns.pool.len, workers=ns.buddies.len, maxWorkers=ns.buddiesMax
|
2022-05-17 11:09:49 +00:00
|
|
|
|
2022-06-16 08:58:50 +00:00
|
|
|
# Do something, work a bit
|
|
|
|
await sp.workerExec
|
2022-05-09 14:04:48 +00:00
|
|
|
|
2022-06-16 08:58:50 +00:00
|
|
|
# Continue until stopped
|
2022-06-06 13:42:08 +00:00
|
|
|
while not sp.ctrl.stopped:
|
2022-06-16 08:58:50 +00:00
|
|
|
# Rotate connection table so the most used entry is at the end
|
|
|
|
discard sp.nsCtx.buddies.lruFetch(sp.peer)
|
|
|
|
|
|
|
|
let delayMs = if sp.workerLockedOk: 1000 else: 50
|
|
|
|
await sleepAsync(chronos.milliseconds(delayMs))
|
2022-05-23 16:53:19 +00:00
|
|
|
|
|
|
|
# Do something, work a bit
|
2022-05-24 08:07:39 +00:00
|
|
|
await sp.workerExec
|
2022-05-23 16:53:19 +00:00
|
|
|
|
2022-06-16 08:58:50 +00:00
|
|
|
trace "Peer worker done", peer=sp,
|
|
|
|
peers=ns.pool.len, workers=ns.buddies.len, maxWorkers=ns.buddiesMax
|
2022-05-23 16:53:19 +00:00
|
|
|
|
2022-05-09 14:04:48 +00:00
|
|
|
|
2022-05-17 11:09:49 +00:00
|
|
|
proc onPeerConnected(ns: SnapSyncCtx, peer: Peer) =
|
2022-06-06 13:42:08 +00:00
|
|
|
let sp = WorkerBuddy.new(ns, peer)
|
2022-05-23 16:53:19 +00:00
|
|
|
|
2022-06-16 08:58:50 +00:00
|
|
|
# Check for known entry (which should not exist.)
|
|
|
|
if ns.buddies.hasKey(peer):
|
|
|
|
trace "Ignoring already registered peer!", peer,
|
|
|
|
peers=ns.pool.len, workers=ns.buddies.len, maxWorkers=ns.buddiesMax
|
2022-05-23 16:53:19 +00:00
|
|
|
return
|
|
|
|
|
2022-06-16 08:58:50 +00:00
|
|
|
# Initialise worker for this peer
|
2022-06-06 13:42:08 +00:00
|
|
|
if not sp.workerStart():
|
2022-06-16 08:58:50 +00:00
|
|
|
trace "Ignoring useless peer", peer,
|
|
|
|
peers=ns.pool.len, workers=ns.buddies.len, maxWorkers=ns.buddiesMax
|
|
|
|
asyncSpawn peer.disconnect(UselessPeer)
|
2022-06-06 13:42:08 +00:00
|
|
|
return
|
2022-05-24 08:07:39 +00:00
|
|
|
|
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.
|
2022-06-16 08:58:50 +00:00
|
|
|
if ns.buddiesMax <= ns.buddies.len:
|
|
|
|
let leastPeer = ns.buddies.shift.value.data
|
|
|
|
trace "Peer overflow! Deleting least used entry", leastPeer,
|
|
|
|
peers=ns.pool.len, workers=ns.buddies.len, maxWorkers=ns.buddiesMax
|
2022-06-06 13:42:08 +00:00
|
|
|
leastPeer.workerStop()
|
2022-06-16 08:58:50 +00:00
|
|
|
asyncSpawn leastPeer.peer.disconnect(UselessPeer)
|
2022-05-23 16:53:19 +00:00
|
|
|
|
|
|
|
# Add peer entry
|
2022-06-16 08:58:50 +00:00
|
|
|
discard ns.buddies.lruAppend(sp.peer, sp, ns.buddiesMax)
|
2022-05-23 16:53:19 +00:00
|
|
|
|
2022-06-16 08:58:50 +00:00
|
|
|
# Run worker
|
|
|
|
asyncSpawn sp.workerLoop()
|
2022-05-09 14:04:48 +00:00
|
|
|
|
|
|
|
|
2022-06-16 08:58:50 +00:00
|
|
|
proc onPeerDisconnected(ns: SnapSyncCtx, peer: Peer) =
|
|
|
|
let rc = ns.buddies.delete(peer)
|
2022-05-23 16:53:19 +00:00
|
|
|
if rc.isOk:
|
2022-06-06 13:42:08 +00:00
|
|
|
rc.value.data.workerStop()
|
2022-06-16 08:58:50 +00:00
|
|
|
trace "Disconnected peer", peer,
|
|
|
|
peers=ns.pool.len, workers=ns.buddies.len, maxWorkers=ns.buddiesMax
|
2022-05-17 11:09:49 +00:00
|
|
|
else:
|
2022-06-16 08:58:50 +00:00
|
|
|
debug "Disconnected from unregistered peer", peer,
|
|
|
|
peers=ns.pool.len, workers=ns.buddies.len, maxWorkers=ns.buddiesMax
|
|
|
|
discard
|
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-06-16 08:58:50 +00:00
|
|
|
let size = max(1,maxPeers)
|
|
|
|
result.buddies.init(size)
|
|
|
|
result.buddiesMax = size
|
2022-05-23 16:53:19 +00:00
|
|
|
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))
|
2022-06-06 13:42:08 +00:00
|
|
|
|
|
|
|
# Initialise sub-systems
|
|
|
|
ctx.workerSetup()
|
2022-05-13 16:30:10 +00:00
|
|
|
po.setProtocol eth
|
2022-05-23 16:53:19 +00:00
|
|
|
ctx.pool.addObserver(ctx, po)
|
2022-05-13 16:30:10 +00:00
|
|
|
|
2022-06-06 13:42:08 +00:00
|
|
|
proc stop*(ctx: SnapSyncCtx) =
|
|
|
|
## Stop syncing
|
|
|
|
ctx.pool.delObserver(ctx)
|
|
|
|
ctx.workerRelease()
|
|
|
|
|
2022-05-13 16:30:10 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|