2022-08-04 08:04:30 +00:00
|
|
|
# Nimbus
|
2022-05-09 14:04:48 +00:00
|
|
|
# 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.
|
|
|
|
|
2023-02-14 23:38:33 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2022-05-09 14:04:48 +00:00
|
|
|
import
|
2022-10-20 16:59:54 +00:00
|
|
|
eth/[common, p2p],
|
2022-05-09 14:04:48 +00:00
|
|
|
chronicles,
|
|
|
|
chronos,
|
2022-08-12 15:42:07 +00:00
|
|
|
../db/select_backend,
|
2022-12-02 04:39:12 +00:00
|
|
|
../core/chain,
|
2022-08-04 08:04:30 +00:00
|
|
|
./snap/[worker, worker_desc],
|
2022-10-19 10:04:06 +00:00
|
|
|
"."/[protocol, sync_desc, sync_sched]
|
2022-05-13 16:30:10 +00:00
|
|
|
|
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-08-04 08:04:30 +00:00
|
|
|
SnapSyncRef* = RunnerSyncRef[CtxData,BuddyData]
|
2022-05-23 16:53:19 +00:00
|
|
|
|
2022-12-09 13:43:55 +00:00
|
|
|
const
|
|
|
|
extraTraceMessages = false # or true
|
|
|
|
## Enable additional logging noise
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private logging helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template traceMsg(f, info: static[string]; args: varargs[untyped]) =
|
|
|
|
trace "Snap scheduler " & f & "() " & info, args
|
|
|
|
|
|
|
|
template traceMsgCtx(f, info: static[string]; c: SnapCtxRef) =
|
|
|
|
when extraTraceMessages:
|
|
|
|
block:
|
|
|
|
let
|
|
|
|
poolMode {.inject.} = c.poolMode
|
|
|
|
daemon {.inject.} = c.daemon
|
|
|
|
f.traceMsg info, poolMode, daemon
|
|
|
|
|
|
|
|
template traceMsgBuddy(f, info: static[string]; b: SnapBuddyRef) =
|
|
|
|
when extraTraceMessages:
|
|
|
|
block:
|
|
|
|
let
|
|
|
|
peer {.inject.} = b.peer
|
|
|
|
runState {.inject.} = b.ctrl.state
|
|
|
|
multiOk {.inject.} = b.ctrl.multiOk
|
|
|
|
poolMode {.inject.} = b.ctx.poolMode
|
|
|
|
daemon {.inject.} = b.ctx.daemon
|
|
|
|
f.traceMsg info, peer, runState, multiOk, poolMode, daemon
|
|
|
|
|
|
|
|
|
|
|
|
template tracerFrameCtx(f: static[string]; c: SnapCtxRef; code: untyped) =
|
|
|
|
f.traceMsgCtx "begin", c
|
|
|
|
code
|
|
|
|
f.traceMsgCtx "end", c
|
|
|
|
|
|
|
|
template tracerFrameBuddy(f: static[string]; b: SnapBuddyRef; code: untyped) =
|
|
|
|
f.traceMsgBuddy "begin", b
|
|
|
|
code
|
|
|
|
f.traceMsgBuddy "end", b
|
|
|
|
|
2022-05-17 11:09:49 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2022-08-04 08:04:30 +00:00
|
|
|
# Virtual methods/interface, `mixin` functions
|
2022-05-17 11:09:49 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc runSetup(ctx: SnapCtxRef; ticker: bool): bool =
|
2022-12-09 13:43:55 +00:00
|
|
|
tracerFrameCtx("runSetup", ctx):
|
|
|
|
result = worker.setup(ctx,ticker)
|
2022-05-17 11:09:49 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc runRelease(ctx: SnapCtxRef) =
|
2022-12-09 13:43:55 +00:00
|
|
|
tracerFrameCtx("runRelease", ctx):
|
|
|
|
worker.release(ctx)
|
2022-05-23 16:53:19 +00:00
|
|
|
|
2022-11-14 14:13:00 +00:00
|
|
|
proc runDaemon(ctx: SnapCtxRef) {.async.} =
|
2022-12-09 13:43:55 +00:00
|
|
|
tracerFrameCtx("runDaemon", ctx):
|
|
|
|
await worker.runDaemon(ctx)
|
2022-11-14 14:13:00 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc runStart(buddy: SnapBuddyRef): bool =
|
2022-12-09 13:43:55 +00:00
|
|
|
tracerFrameBuddy("runStart", buddy):
|
|
|
|
result = worker.start(buddy)
|
2022-08-04 08:04:30 +00:00
|
|
|
|
|
|
|
proc runStop(buddy: SnapBuddyRef) =
|
2022-12-09 13:43:55 +00:00
|
|
|
tracerFrameBuddy("runStop", buddy):
|
|
|
|
worker.stop(buddy)
|
2022-08-04 08:04:30 +00:00
|
|
|
|
2022-11-25 14:56:42 +00:00
|
|
|
proc runPool(buddy: SnapBuddyRef; last: bool): bool =
|
2022-12-09 13:43:55 +00:00
|
|
|
tracerFrameBuddy("runPool", buddy):
|
|
|
|
result = worker.runPool(buddy, last)
|
2022-08-04 08:04:30 +00:00
|
|
|
|
|
|
|
proc runSingle(buddy: SnapBuddyRef) {.async.} =
|
2022-12-09 13:43:55 +00:00
|
|
|
tracerFrameBuddy("runSingle", buddy):
|
|
|
|
await worker.runSingle(buddy)
|
2022-05-23 16:53:19 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc runMulti(buddy: SnapBuddyRef) {.async.} =
|
2022-12-09 13:43:55 +00:00
|
|
|
tracerFrameBuddy("runMulti", buddy):
|
|
|
|
await worker.runMulti(buddy)
|
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-08-04 08:04:30 +00:00
|
|
|
proc init*(
|
|
|
|
T: type SnapSyncRef;
|
|
|
|
ethNode: EthereumNode;
|
2022-12-02 04:39:12 +00:00
|
|
|
chain: ChainRef;
|
2022-08-04 08:04:30 +00:00
|
|
|
rng: ref HmacDrbgContext;
|
|
|
|
maxPeers: int;
|
2022-11-25 14:56:42 +00:00
|
|
|
dbBackend: ChainDb;
|
|
|
|
enableTicker = false;
|
|
|
|
noRecovery = false;
|
|
|
|
): T =
|
2022-05-13 16:30:10 +00:00
|
|
|
new result
|
2022-10-10 02:31:28 +00:00
|
|
|
result.initSync(ethNode, chain, maxPeers, enableTicker)
|
2022-08-04 08:04:30 +00:00
|
|
|
result.ctx.chain = chain # explicitely override
|
2023-02-23 13:13:02 +00:00
|
|
|
result.ctx.pool.rng = rng
|
|
|
|
result.ctx.pool.dbBackend = dbBackend
|
|
|
|
result.ctx.pool.noRecovery = noRecovery
|
2023-01-20 15:01:29 +00:00
|
|
|
# Required to have been initialised via `addEthHandlerCapability()`
|
2022-10-19 10:04:06 +00:00
|
|
|
doAssert not result.ctx.ethWireCtx.isNil
|
2022-05-13 16:30:10 +00:00
|
|
|
|
2022-07-21 12:14:41 +00:00
|
|
|
proc start*(ctx: SnapSyncRef) =
|
2022-11-25 14:56:42 +00:00
|
|
|
doAssert ctx.startSync()
|
2022-05-13 16:30:10 +00:00
|
|
|
|
2022-07-21 12:14:41 +00:00
|
|
|
proc stop*(ctx: SnapSyncRef) =
|
2022-08-04 08:04:30 +00:00
|
|
|
ctx.stopSync()
|
2022-06-06 13:42:08 +00:00
|
|
|
|
2022-05-13 16:30:10 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|