2022-08-04 08:04:30 +00:00
|
|
|
# Nimbus
|
2022-07-21 12:14:41 +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.
|
|
|
|
|
|
|
|
import
|
2022-10-20 16:59:54 +00:00
|
|
|
eth/[common, p2p],
|
2022-07-21 12:14:41 +00:00
|
|
|
chronicles,
|
|
|
|
chronos,
|
2022-08-04 08:04:30 +00:00
|
|
|
stew/[interval_set, sorted_set],
|
|
|
|
"."/[full/worker, sync_desc, sync_sched, protocol]
|
2022-07-21 12:14:41 +00:00
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
{.push raises: [].}
|
2022-07-21 12:14:41 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "full-sync"
|
|
|
|
|
|
|
|
type
|
2022-08-04 08:04:30 +00:00
|
|
|
FullSyncRef* = RunnerSyncRef[CtxData,BuddyData]
|
2022-07-21 12:14:41 +00:00
|
|
|
|
2022-12-18 16:06:43 +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: FullCtxRef) =
|
|
|
|
when extraTraceMessages:
|
|
|
|
block:
|
|
|
|
let
|
|
|
|
poolMode {.inject.} = c.poolMode
|
|
|
|
daemon {.inject.} = c.daemon
|
|
|
|
f.traceMsg info, poolMode, daemon
|
|
|
|
|
|
|
|
template traceMsgBuddy(f, info: static[string]; b: FullBuddyRef) =
|
|
|
|
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: FullCtxRef; code: untyped) =
|
|
|
|
f.traceMsgCtx "begin", c
|
|
|
|
code
|
|
|
|
f.traceMsgCtx "end", c
|
|
|
|
|
|
|
|
template tracerFrameBuddy(f: static[string]; b: FullBuddyRef; code: untyped) =
|
|
|
|
f.traceMsgBuddy "begin", b
|
|
|
|
code
|
|
|
|
f.traceMsgBuddy "end", b
|
|
|
|
|
2022-07-21 12:14:41 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2022-08-04 08:04:30 +00:00
|
|
|
# Virtual methods/interface, `mixin` functions
|
2022-07-21 12:14:41 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc runSetup(ctx: FullCtxRef; ticker: bool): bool =
|
2022-12-18 16:06:43 +00:00
|
|
|
tracerFrameCtx("runSetup", ctx):
|
|
|
|
result = worker.setup(ctx,ticker)
|
2022-07-21 12:14:41 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc runRelease(ctx: FullCtxRef) =
|
2022-12-18 16:06:43 +00:00
|
|
|
tracerFrameCtx("runRelease", ctx):
|
|
|
|
worker.release(ctx)
|
2022-07-21 12:14:41 +00:00
|
|
|
|
2022-11-14 14:13:00 +00:00
|
|
|
proc runDaemon(ctx: FullCtxRef) {.async.} =
|
2022-12-18 16:06:43 +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: FullBuddyRef): bool =
|
2022-12-18 16:06:43 +00:00
|
|
|
tracerFrameBuddy("runStart", buddy):
|
|
|
|
result = worker.start(buddy)
|
2022-08-04 08:04:30 +00:00
|
|
|
|
|
|
|
proc runStop(buddy: FullBuddyRef) =
|
2022-12-18 16:06:43 +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: FullBuddyRef; last: bool): bool =
|
2022-12-18 16:06:43 +00:00
|
|
|
tracerFrameBuddy("runPool", buddy):
|
|
|
|
result = worker.runPool(buddy, last)
|
2022-08-04 08:04:30 +00:00
|
|
|
|
|
|
|
proc runSingle(buddy: FullBuddyRef) {.async.} =
|
2022-12-18 16:06:43 +00:00
|
|
|
tracerFrameBuddy("runSingle", buddy):
|
|
|
|
await worker.runSingle(buddy)
|
2022-07-21 12:14:41 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc runMulti(buddy: FullBuddyRef) {.async.} =
|
2022-12-18 16:06:43 +00:00
|
|
|
tracerFrameBuddy("runMulti", buddy):
|
|
|
|
await worker.runMulti(buddy)
|
2022-07-21 12:14:41 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc init*(
|
|
|
|
T: type FullSyncRef;
|
|
|
|
ethNode: EthereumNode;
|
2022-12-02 04:39:12 +00:00
|
|
|
chain: ChainRef;
|
2022-09-30 08:22:14 +00:00
|
|
|
rng: ref HmacDrbgContext;
|
2022-07-21 12:14:41 +00:00
|
|
|
maxPeers: int;
|
2022-08-04 08:04:30 +00:00
|
|
|
enableTicker = false): T =
|
|
|
|
new result
|
2022-10-10 02:31:28 +00:00
|
|
|
result.initSync(ethNode, chain, maxPeers, enableTicker)
|
2022-09-30 08:22:14 +00:00
|
|
|
result.ctx.data.rng = rng
|
2022-07-21 12:14:41 +00:00
|
|
|
|
|
|
|
proc start*(ctx: FullSyncRef) =
|
2022-08-04 08:04:30 +00:00
|
|
|
doAssert ctx.startSync()
|
2022-07-21 12:14:41 +00:00
|
|
|
|
|
|
|
proc stop*(ctx: FullSyncRef) =
|
2022-08-04 08:04:30 +00:00
|
|
|
ctx.stopSync()
|
2022-07-21 12:14:41 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|