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.
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
## Worker peers scheduler template
|
|
|
|
## ===============================
|
|
|
|
##
|
|
|
|
## Public descriptors
|
|
|
|
|
2022-07-21 12:14:41 +00:00
|
|
|
import
|
2023-03-02 09:57:58 +00:00
|
|
|
#std/options,
|
2022-10-10 02:31:28 +00:00
|
|
|
eth/[common, p2p],
|
2022-12-02 04:39:12 +00:00
|
|
|
../core/chain,
|
2022-10-19 10:04:06 +00:00
|
|
|
../db/db_chain,
|
|
|
|
./handlers
|
2022-10-10 02:31:28 +00:00
|
|
|
|
|
|
|
export
|
|
|
|
chain,
|
|
|
|
db_chain
|
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
|
|
|
|
|
|
|
type
|
2022-08-04 08:04:30 +00:00
|
|
|
BuddyRunState* = enum
|
|
|
|
Running = 0 ## Running, default state
|
|
|
|
Stopped ## Stopped or about stopping
|
|
|
|
ZombieStop ## Abandon/ignore (wait for pushed out of LRU table)
|
|
|
|
ZombieRun ## Extra zombie state to potentially recover from
|
2022-07-21 12:14:41 +00:00
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
BuddyCtrlRef* = ref object
|
2022-07-21 12:14:41 +00:00
|
|
|
## Control and state settings
|
2023-03-02 09:57:58 +00:00
|
|
|
runState: BuddyRunState ## Access with getters
|
|
|
|
multiOk*: bool ## Triggers `runSingle()` mode unless `true`
|
2022-07-21 12:14:41 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
BuddyRef*[S,W] = ref object
|
|
|
|
## Worker peer state descriptor.
|
2023-03-02 09:57:58 +00:00
|
|
|
ctx*: CtxRef[S] ## Shared data descriptor back reference
|
|
|
|
peer*: Peer ## Reference to eth p2pProtocol entry
|
|
|
|
ctrl*: BuddyCtrlRef ## Control and state settings
|
|
|
|
only*: W ## Worker peer specific data
|
2022-07-21 12:14:41 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
CtxRef*[S] = ref object
|
|
|
|
## Shared state among all syncing peer workers (aka buddies.)
|
2023-03-02 09:57:58 +00:00
|
|
|
buddiesMax*: int ## Max number of buddies
|
|
|
|
ethWireCtx*: EthWireRef ## Eth protocol wire context (if available)
|
|
|
|
chain*: ChainRef ## Block chain database (no need for `Peer`)
|
|
|
|
poolMode*: bool ## Activate `runPool()` workers if set `true`
|
|
|
|
daemon*: bool ## Enable global background job
|
|
|
|
exCtrlFile*: Option[string] ## Extra instructions file (if any)
|
|
|
|
pool*: S ## Shared context for all worker peers
|
2022-07-21 12:14:41 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc `$`*[S,W](worker: BuddyRef[S,W]): string =
|
|
|
|
$worker.peer & "$" & $worker.ctrl.runState
|
2022-07-21 12:14:41 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public getters, `BuddyRunState` execution control functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
proc state*(ctrl: BuddyCtrlRef): BuddyRunState =
|
2022-07-21 12:14:41 +00:00
|
|
|
## Getter (logging only, details of `BuddyCtrl` are private)
|
|
|
|
ctrl.runState
|
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
proc running*(ctrl: BuddyCtrlRef): bool =
|
2022-07-21 12:14:41 +00:00
|
|
|
## Getter, if `true` if `ctrl.state()` is `Running`
|
|
|
|
ctrl.runState == Running
|
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
proc stopped*(ctrl: BuddyCtrlRef): bool =
|
2022-07-21 12:14:41 +00:00
|
|
|
## Getter, if `true`, if `ctrl.state()` is not `Running`
|
2022-11-08 18:56:04 +00:00
|
|
|
ctrl.runState != Running
|
2022-07-21 12:14:41 +00:00
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
proc zombie*(ctrl: BuddyCtrlRef): bool =
|
2022-07-21 12:14:41 +00:00
|
|
|
## Getter, `true` if `ctrl.state()` is `Zombie` (i.e. not `running()` and
|
|
|
|
## not `stopped()`)
|
|
|
|
ctrl.runState in {ZombieStop, ZombieRun}
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public setters, `BuddyRunState` execution control functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
proc `zombie=`*(ctrl: BuddyCtrlRef; value: bool) =
|
2022-07-21 12:14:41 +00:00
|
|
|
## Setter
|
|
|
|
if value:
|
|
|
|
case ctrl.runState:
|
|
|
|
of Running:
|
|
|
|
ctrl.runState = ZombieRun
|
|
|
|
of Stopped:
|
|
|
|
ctrl.runState = ZombieStop
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
else:
|
|
|
|
case ctrl.runState:
|
|
|
|
of ZombieRun:
|
|
|
|
ctrl.runState = Running
|
|
|
|
of ZombieStop:
|
|
|
|
ctrl.runState = Stopped
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
proc `stopped=`*(ctrl: BuddyCtrlRef; value: bool) =
|
2022-07-21 12:14:41 +00:00
|
|
|
## Setter
|
|
|
|
if value:
|
|
|
|
case ctrl.runState:
|
|
|
|
of Running:
|
|
|
|
ctrl.runState = Stopped
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
else:
|
|
|
|
case ctrl.runState:
|
|
|
|
of Stopped:
|
|
|
|
ctrl.runState = Running
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|