2022-11-04 09:52:27 +00:00
|
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
else:
|
|
|
|
|
{.push raises: [].}
|
2021-07-14 17:58:46 +00:00
|
|
|
|
|
2020-04-29 04:49:27 +00:00
|
|
|
|
import
|
2024-02-07 11:42:20 +00:00
|
|
|
|
std/[options, strutils, os, sequtils],
|
2021-06-09 14:37:08 +00:00
|
|
|
|
stew/shims/net as stewNet,
|
2022-11-23 09:08:00 +00:00
|
|
|
|
chronicles,
|
2022-10-18 17:35:26 +00:00
|
|
|
|
chronos,
|
|
|
|
|
metrics,
|
2022-11-30 18:41:19 +00:00
|
|
|
|
libbacktrace,
|
2022-10-18 17:35:26 +00:00
|
|
|
|
system/ansi_c,
|
2023-04-25 13:34:57 +00:00
|
|
|
|
libp2p/crypto/crypto
|
2022-07-25 11:01:37 +00:00
|
|
|
|
import
|
2023-11-09 09:48:39 +00:00
|
|
|
|
../../tools/rln_keystore_generator/rln_keystore_generator,
|
2022-12-07 11:30:32 +00:00
|
|
|
|
../../waku/common/logging,
|
2023-06-22 20:58:14 +00:00
|
|
|
|
./external_config,
|
2024-02-07 11:42:20 +00:00
|
|
|
|
./networks_config,
|
2023-04-25 13:34:57 +00:00
|
|
|
|
./app
|
2022-10-18 17:35:26 +00:00
|
|
|
|
|
|
|
|
|
logScope:
|
2023-04-25 13:34:57 +00:00
|
|
|
|
topics = "wakunode main"
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
2024-02-07 11:42:20 +00:00
|
|
|
|
proc logConfig(conf: WakuNodeConf) =
|
|
|
|
|
info "Configuration: Enabled protocols",
|
|
|
|
|
relay = conf.relay,
|
|
|
|
|
rlnRelay = conf.rlnRelay,
|
|
|
|
|
store = conf.store,
|
|
|
|
|
filter = conf.filter,
|
|
|
|
|
lightpush = conf.lightpush,
|
|
|
|
|
peerExchange = conf.peerExchange
|
|
|
|
|
|
|
|
|
|
info "Configuration. Network",
|
|
|
|
|
cluster = conf.clusterId,
|
|
|
|
|
pubsubTopics = conf.pubsubTopics,
|
|
|
|
|
maxPeers = conf.maxRelayPeers
|
|
|
|
|
|
|
|
|
|
for i in conf.discv5BootstrapNodes:
|
|
|
|
|
info "Configuration. Bootstrap nodes", node = i
|
|
|
|
|
|
|
|
|
|
if conf.rlnRelay and conf.rlnRelayDynamic:
|
|
|
|
|
info "Configuration. Validation",
|
|
|
|
|
mechanism = "onchain rln",
|
|
|
|
|
contract = conf.rlnRelayEthContractAddress,
|
|
|
|
|
maxMessageSize = conf.maxMessageSize
|
|
|
|
|
|
|
|
|
|
{.pop.}
|
|
|
|
|
# @TODO confutils.nim(775, 17) Error: can raise an unlisted exception: ref IOError
|
2022-10-21 13:01:01 +00:00
|
|
|
|
when isMainModule:
|
|
|
|
|
## Node setup happens in 6 phases:
|
|
|
|
|
## 1. Set up storage
|
|
|
|
|
## 2. Initialize node
|
|
|
|
|
## 3. Mount and initialize configured protocols
|
|
|
|
|
## 4. Start node and mounted protocols
|
|
|
|
|
## 5. Start monitoring tools and external interfaces
|
|
|
|
|
## 6. Setup graceful shutdown hooks
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2023-04-25 13:34:57 +00:00
|
|
|
|
const versionString = "version / git commit hash: " & app.git_version
|
2023-02-06 11:53:05 +00:00
|
|
|
|
let rng = crypto.newRng()
|
2022-11-03 09:45:06 +00:00
|
|
|
|
|
2024-02-07 11:42:20 +00:00
|
|
|
|
let confRes = WakuNodeConf.load(version = versionString)
|
2022-11-03 09:45:06 +00:00
|
|
|
|
if confRes.isErr():
|
2024-02-07 11:42:20 +00:00
|
|
|
|
error "failure while loading the configuration", error = confRes.error
|
2022-11-03 09:45:06 +00:00
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
2024-02-07 11:42:20 +00:00
|
|
|
|
var conf = confRes.get()
|
|
|
|
|
|
2022-12-07 11:30:32 +00:00
|
|
|
|
## Logging setup
|
|
|
|
|
|
|
|
|
|
# Adhere to NO_COLOR initiative: https://no-color.org/
|
2024-02-07 11:42:20 +00:00
|
|
|
|
let color =
|
|
|
|
|
try:
|
|
|
|
|
not parseBool(os.getEnv("NO_COLOR", "false"))
|
|
|
|
|
except CatchableError:
|
|
|
|
|
true
|
2022-12-07 11:30:32 +00:00
|
|
|
|
|
|
|
|
|
logging.setupLogLevel(conf.logLevel)
|
|
|
|
|
logging.setupLogFormat(conf.logFormat, color)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2024-02-07 11:42:20 +00:00
|
|
|
|
case conf.cmd
|
2023-11-09 09:48:39 +00:00
|
|
|
|
of generateRlnKeystore:
|
|
|
|
|
doRlnKeystoreGenerator(conf)
|
|
|
|
|
of noCommand:
|
2024-02-13 10:10:45 +00:00
|
|
|
|
# The Waku Network config (cluster-id=1)
|
|
|
|
|
if conf.clusterId == 1:
|
|
|
|
|
let twnClusterConf = ClusterConf.TheWakuNetworkConf()
|
|
|
|
|
if len(conf.shards) != 0:
|
|
|
|
|
conf.pubsubTopics = conf.shards.mapIt(twnClusterConf.pubsubTopics[it.uint16])
|
|
|
|
|
else:
|
|
|
|
|
conf.pubsubTopics = twnClusterConf.pubsubTopics
|
|
|
|
|
|
|
|
|
|
# Override configuration
|
|
|
|
|
conf.maxMessageSize = twnClusterConf.maxMessageSize
|
|
|
|
|
conf.clusterId = twnClusterConf.clusterId
|
|
|
|
|
conf.rlnRelay = twnClusterConf.rlnRelay
|
|
|
|
|
conf.rlnRelayEthContractAddress = twnClusterConf.rlnRelayEthContractAddress
|
|
|
|
|
conf.rlnRelayDynamic = twnClusterConf.rlnRelayDynamic
|
|
|
|
|
conf.rlnRelayBandwidthThreshold = twnClusterConf.rlnRelayBandwidthThreshold
|
|
|
|
|
conf.discv5Discovery = twnClusterConf.discv5Discovery
|
|
|
|
|
conf.discv5BootstrapNodes =
|
|
|
|
|
conf.discv5BootstrapNodes & twnClusterConf.discv5BootstrapNodes
|
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
var wakunode2 = App.init(rng, conf)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2024-02-13 10:10:45 +00:00
|
|
|
|
info "Running nwaku node", version = app.git_version
|
|
|
|
|
logConfig(conf)
|
|
|
|
|
|
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
##############
|
|
|
|
|
# Node setup #
|
|
|
|
|
##############
|
2023-04-25 13:34:57 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
debug "1/7 Setting up storage"
|
2022-10-27 22:05:02 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
## Peer persistence
|
|
|
|
|
let res1 = wakunode2.setupPeerPersistence()
|
|
|
|
|
if res1.isErr():
|
2024-02-07 11:42:20 +00:00
|
|
|
|
error "1/7 Setting up storage failed", error = res1.error
|
2023-11-09 09:48:39 +00:00
|
|
|
|
quit(QuitFailure)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
debug "2/7 Retrieve dynamic bootstrap nodes"
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
let res3 = wakunode2.setupDyamicBootstrapNodes()
|
|
|
|
|
if res3.isErr():
|
2024-02-07 11:42:20 +00:00
|
|
|
|
error "2/7 Retrieving dynamic bootstrap nodes failed", error = res3.error
|
2023-11-09 09:48:39 +00:00
|
|
|
|
quit(QuitFailure)
|
2022-03-17 16:33:17 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
debug "3/7 Initializing node"
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
let res4 = wakunode2.setupWakuApp()
|
|
|
|
|
if res4.isErr():
|
2024-02-07 11:42:20 +00:00
|
|
|
|
error "3/7 Initializing node failed", error = res4.error
|
2023-11-09 09:48:39 +00:00
|
|
|
|
quit(QuitFailure)
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
debug "4/7 Mounting protocols"
|
2021-04-24 04:56:37 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
let res5 = waitFor wakunode2.setupAndMountProtocols()
|
|
|
|
|
if res5.isErr():
|
2024-02-07 11:42:20 +00:00
|
|
|
|
error "4/7 Mounting protocols failed", error = res5.error
|
2023-11-09 09:48:39 +00:00
|
|
|
|
quit(QuitFailure)
|
2020-09-01 02:09:54 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
debug "5/7 Starting node and mounted protocols"
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
let res6 = wakunode2.startApp()
|
|
|
|
|
if res6.isErr():
|
2024-02-07 11:42:20 +00:00
|
|
|
|
error "5/7 Starting node and protocols failed", error = res6.error
|
2023-11-09 09:48:39 +00:00
|
|
|
|
quit(QuitFailure)
|
2022-10-27 22:05:02 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
debug "6/7 Starting monitoring and external interfaces"
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
let res7 = wakunode2.setupMonitoringAndExternalInterfaces()
|
|
|
|
|
if res7.isErr():
|
2024-02-07 11:42:20 +00:00
|
|
|
|
error "6/7 Starting monitoring and external interfaces failed", error = res7.error
|
2023-11-09 09:48:39 +00:00
|
|
|
|
quit(QuitFailure)
|
2022-10-27 22:05:02 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
debug "7/7 Setting up shutdown hooks"
|
|
|
|
|
## Setup shutdown hooks for this process.
|
|
|
|
|
## Stop node gracefully on shutdown.
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2023-12-14 06:16:39 +00:00
|
|
|
|
proc asyncStopper(node: App) {.async: (raises: [Exception]).} =
|
2023-11-09 09:48:39 +00:00
|
|
|
|
await node.stop()
|
|
|
|
|
quit(QuitSuccess)
|
2022-12-06 10:51:33 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
# Handle Ctrl-C SIGINT
|
|
|
|
|
proc handleCtrlC() {.noconv.} =
|
|
|
|
|
when defined(windows):
|
|
|
|
|
# workaround for https://github.com/nim-lang/Nim/issues/4057
|
|
|
|
|
setupForeignThreadGc()
|
|
|
|
|
notice "Shutting down after receiving SIGINT"
|
|
|
|
|
asyncSpawn asyncStopper(wakunode2)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
setControlCHook(handleCtrlC)
|
2021-04-15 08:18:14 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
# Handle SIGTERM
|
|
|
|
|
when defined(posix):
|
|
|
|
|
proc handleSigterm(signal: cint) {.noconv.} =
|
|
|
|
|
notice "Shutting down after receiving SIGTERM"
|
|
|
|
|
asyncSpawn asyncStopper(wakunode2)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
c_signal(ansi_c.SIGTERM, handleSigterm)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
# Handle SIGSEGV
|
|
|
|
|
when defined(posix):
|
|
|
|
|
proc handleSigsegv(signal: cint) {.noconv.} =
|
|
|
|
|
# Require --debugger:native
|
2024-02-07 11:42:20 +00:00
|
|
|
|
fatal "Shutting down after receiving SIGSEGV", stacktrace = getBacktrace()
|
2022-11-29 15:02:18 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
# Not available in -d:release mode
|
|
|
|
|
writeStackTrace()
|
2022-11-29 15:02:18 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
waitFor wakunode2.stop()
|
|
|
|
|
quit(QuitFailure)
|
2022-11-29 15:02:18 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
c_signal(ansi_c.SIGSEGV, handleSigsegv)
|
2022-11-29 15:02:18 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
info "Node setup complete"
|
2020-09-01 02:09:54 +00:00
|
|
|
|
|
2023-11-09 09:48:39 +00:00
|
|
|
|
runForever()
|