2022-11-04 10:52:27 +01:00
|
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
else:
|
|
|
|
|
{.push raises: [].}
|
2021-07-14 19:58:46 +02:00
|
|
|
|
|
2020-04-29 12:49:27 +08:00
|
|
|
|
import
|
2024-05-17 16:28:54 +02:00
|
|
|
|
std/[options, strutils, sequtils, net],
|
2022-11-23 10:08:00 +01:00
|
|
|
|
chronicles,
|
2022-10-18 12:35:26 -05:00
|
|
|
|
chronos,
|
|
|
|
|
metrics,
|
2022-11-30 19:41:19 +01:00
|
|
|
|
libbacktrace,
|
2022-10-18 12:35:26 -05:00
|
|
|
|
system/ansi_c,
|
2023-04-25 15:34:57 +02:00
|
|
|
|
libp2p/crypto/crypto
|
2022-07-25 13:01:37 +02:00
|
|
|
|
import
|
2023-11-09 15:18:39 +05:30
|
|
|
|
../../tools/rln_keystore_generator/rln_keystore_generator,
|
2024-03-06 19:38:43 +05:30
|
|
|
|
../../tools/rln_db_inspector/rln_db_inspector,
|
2022-12-07 12:30:32 +01:00
|
|
|
|
../../waku/common/logging,
|
2024-03-03 02:59:53 +02:00
|
|
|
|
../../waku/factory/external_config,
|
2024-05-03 14:07:15 +02:00
|
|
|
|
../../waku/factory/waku,
|
2024-04-24 15:59:50 +02:00
|
|
|
|
../../waku/node/health_monitor,
|
2024-04-30 15:07:17 +02:00
|
|
|
|
../../waku/node/waku_metrics,
|
2024-04-24 15:59:50 +02:00
|
|
|
|
../../waku/waku_api/rest/builder as rest_server_builder
|
2022-10-18 12:35:26 -05:00
|
|
|
|
|
|
|
|
|
logScope:
|
2023-04-25 15:34:57 +02:00
|
|
|
|
topics = "wakunode main"
|
2021-07-22 11:46:54 +02:00
|
|
|
|
|
2024-05-13 17:45:48 +02:00
|
|
|
|
const git_version* {.strdefine.} = "n/a"
|
2024-02-07 12:42:20 +01:00
|
|
|
|
|
|
|
|
|
{.pop.}
|
|
|
|
|
# @TODO confutils.nim(775, 17) Error: can raise an unlisted exception: ref IOError
|
2022-10-21 15:01:01 +02: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 10:08:00 +01:00
|
|
|
|
|
2024-05-03 14:07:15 +02:00
|
|
|
|
const versionString = "version / git commit hash: " & waku.git_version
|
2022-11-03 10:45:06 +01:00
|
|
|
|
|
2024-05-13 17:45:48 +02:00
|
|
|
|
var conf = WakuNodeConf.load(version = versionString).valueOr:
|
|
|
|
|
error "failure while loading the configuration", error = error
|
2022-11-03 10:45:06 +01:00
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
2024-05-17 16:28:54 +02:00
|
|
|
|
## Also called within Waku.init. The call to startRestServerEsentials needs the following line
|
|
|
|
|
logging.setupLog(conf.logLevel, conf.logFormat)
|
2022-11-23 10:08:00 +01:00
|
|
|
|
|
2024-02-07 12:42:20 +01:00
|
|
|
|
case conf.cmd
|
2023-11-09 15:18:39 +05:30
|
|
|
|
of generateRlnKeystore:
|
|
|
|
|
doRlnKeystoreGenerator(conf)
|
2024-03-06 19:38:43 +05:30
|
|
|
|
of inspectRlnDb:
|
|
|
|
|
doInspectRlnDb(conf)
|
2023-11-09 15:18:39 +05:30
|
|
|
|
of noCommand:
|
2024-04-23 18:53:18 +02:00
|
|
|
|
# NOTE: {.threadvar.} is used to make the global variable GC safe for the closure uses it
|
|
|
|
|
# It will always be called from main thread anyway.
|
|
|
|
|
# Ref: https://nim-lang.org/docs/manual.html#threads-gc-safety
|
|
|
|
|
var nodeHealthMonitor {.threadvar.}: WakuNodeHealthMonitor
|
|
|
|
|
nodeHealthMonitor = WakuNodeHealthMonitor()
|
|
|
|
|
nodeHealthMonitor.setOverallHealth(HealthStatus.INITIALIZING)
|
|
|
|
|
|
2024-04-24 15:59:50 +02:00
|
|
|
|
let restServer = rest_server_builder.startRestServerEsentials(
|
|
|
|
|
nodeHealthMonitor, conf
|
|
|
|
|
).valueOr:
|
|
|
|
|
error "Starting esential REST server failed.", error = $error
|
2024-04-23 18:53:18 +02:00
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
2024-05-03 14:07:15 +02:00
|
|
|
|
var waku = Waku.init(conf).valueOr:
|
|
|
|
|
error "Waku initialization failed", error = error
|
2023-11-09 15:18:39 +05:30
|
|
|
|
quit(QuitFailure)
|
2020-09-01 04:09:54 +02:00
|
|
|
|
|
2024-05-03 14:07:15 +02:00
|
|
|
|
waku.restServer = restServer
|
2024-04-24 15:59:50 +02:00
|
|
|
|
|
2024-05-03 14:07:15 +02:00
|
|
|
|
nodeHealthMonitor.setNode(waku.node)
|
2024-04-23 18:53:18 +02:00
|
|
|
|
|
2024-05-03 14:07:15 +02:00
|
|
|
|
(waitFor startWaku(addr waku)).isOkOr:
|
|
|
|
|
error "Starting waku failed", error = error
|
2023-11-09 15:18:39 +05:30
|
|
|
|
quit(QuitFailure)
|
2022-10-28 00:05:02 +02:00
|
|
|
|
|
2024-04-24 15:59:50 +02:00
|
|
|
|
rest_server_builder.startRestServerProtocolSupport(
|
2024-05-03 14:07:15 +02:00
|
|
|
|
restServer, waku.node, waku.wakuDiscv5, conf
|
2024-04-24 15:59:50 +02:00
|
|
|
|
).isOkOr:
|
|
|
|
|
error "Starting protocols support REST server failed.", error = $error
|
|
|
|
|
quit(QuitFailure)
|
2024-04-23 18:53:18 +02:00
|
|
|
|
|
2024-05-03 14:07:15 +02:00
|
|
|
|
waku.metricsServer = waku_metrics.startMetricsServerAndLogging(conf).valueOr:
|
2024-03-08 16:46:42 -06:00
|
|
|
|
error "Starting monitoring and external interfaces failed", error = error
|
2024-03-16 00:08:47 +01:00
|
|
|
|
quit(QuitFailure)
|
2022-10-28 00:05:02 +02:00
|
|
|
|
|
2024-04-23 18:53:18 +02:00
|
|
|
|
nodeHealthMonitor.setOverallHealth(HealthStatus.READY)
|
|
|
|
|
|
2024-03-08 16:46:42 -06:00
|
|
|
|
debug "Setting up shutdown hooks"
|
2023-11-09 15:18:39 +05:30
|
|
|
|
## Setup shutdown hooks for this process.
|
|
|
|
|
## Stop node gracefully on shutdown.
|
2022-11-23 10:08:00 +01:00
|
|
|
|
|
2024-05-03 14:07:15 +02:00
|
|
|
|
proc asyncStopper(node: Waku) {.async: (raises: [Exception]).} =
|
2024-04-23 18:53:18 +02:00
|
|
|
|
nodeHealthMonitor.setOverallHealth(HealthStatus.SHUTTING_DOWN)
|
2023-11-09 15:18:39 +05:30
|
|
|
|
await node.stop()
|
|
|
|
|
quit(QuitSuccess)
|
2022-12-06 11:51:33 +01:00
|
|
|
|
|
2023-11-09 15:18:39 +05:30
|
|
|
|
# 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"
|
2024-05-03 14:07:15 +02:00
|
|
|
|
asyncSpawn asyncStopper(waku)
|
2022-11-23 10:08:00 +01:00
|
|
|
|
|
2023-11-09 15:18:39 +05:30
|
|
|
|
setControlCHook(handleCtrlC)
|
2021-04-15 10:18:14 +02:00
|
|
|
|
|
2023-11-09 15:18:39 +05:30
|
|
|
|
# Handle SIGTERM
|
|
|
|
|
when defined(posix):
|
|
|
|
|
proc handleSigterm(signal: cint) {.noconv.} =
|
|
|
|
|
notice "Shutting down after receiving SIGTERM"
|
2024-05-03 14:07:15 +02:00
|
|
|
|
asyncSpawn asyncStopper(waku)
|
2022-11-23 10:08:00 +01:00
|
|
|
|
|
2023-11-09 15:18:39 +05:30
|
|
|
|
c_signal(ansi_c.SIGTERM, handleSigterm)
|
2022-11-23 10:08:00 +01:00
|
|
|
|
|
2023-11-09 15:18:39 +05:30
|
|
|
|
# Handle SIGSEGV
|
|
|
|
|
when defined(posix):
|
|
|
|
|
proc handleSigsegv(signal: cint) {.noconv.} =
|
|
|
|
|
# Require --debugger:native
|
2024-02-07 12:42:20 +01:00
|
|
|
|
fatal "Shutting down after receiving SIGSEGV", stacktrace = getBacktrace()
|
2022-11-29 16:02:18 +01:00
|
|
|
|
|
2023-11-09 15:18:39 +05:30
|
|
|
|
# Not available in -d:release mode
|
|
|
|
|
writeStackTrace()
|
2022-11-29 16:02:18 +01:00
|
|
|
|
|
2024-05-03 14:07:15 +02:00
|
|
|
|
waitFor waku.stop()
|
2023-11-09 15:18:39 +05:30
|
|
|
|
quit(QuitFailure)
|
2022-11-29 16:02:18 +01:00
|
|
|
|
|
2023-11-09 15:18:39 +05:30
|
|
|
|
c_signal(ansi_c.SIGSEGV, handleSigsegv)
|
2022-11-29 16:02:18 +01:00
|
|
|
|
|
2023-11-09 15:18:39 +05:30
|
|
|
|
info "Node setup complete"
|
2020-09-01 04:09:54 +02:00
|
|
|
|
|
2023-11-09 15:18:39 +05:30
|
|
|
|
runForever()
|