2024-06-28 10:34:57 +00:00
|
|
|
|
{.push raises: [].}
|
2021-07-14 17:58:46 +00:00
|
|
|
|
|
2020-04-29 04:49:27 +00:00
|
|
|
|
import
|
2024-05-17 14:28:54 +00:00
|
|
|
|
std/[options, strutils, sequtils, net],
|
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,
|
2024-03-06 14:08:43 +00:00
|
|
|
|
../../tools/rln_db_inspector/rln_db_inspector,
|
2024-07-05 22:03:38 +00:00
|
|
|
|
waku/[
|
|
|
|
|
common/logging,
|
|
|
|
|
factory/external_config,
|
|
|
|
|
factory/waku,
|
|
|
|
|
node/health_monitor,
|
|
|
|
|
node/waku_metrics,
|
|
|
|
|
waku_api/rest/builder as rest_server_builder,
|
|
|
|
|
]
|
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-05-13 15:45:48 +00:00
|
|
|
|
const git_version* {.strdefine.} = "n/a"
|
2024-02-07 11:42:20 +00:00
|
|
|
|
|
|
|
|
|
{.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
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
const versionString = "version / git commit hash: " & waku.git_version
|
2022-11-03 09:45:06 +00:00
|
|
|
|
|
2024-05-13 15:45:48 +00:00
|
|
|
|
var conf = WakuNodeConf.load(version = versionString).valueOr:
|
|
|
|
|
error "failure while loading the configuration", error = error
|
2022-11-03 09:45:06 +00:00
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
2024-05-17 14:28:54 +00:00
|
|
|
|
## Also called within Waku.init. The call to startRestServerEsentials needs the following line
|
|
|
|
|
logging.setupLog(conf.logLevel, conf.logFormat)
|
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)
|
2024-03-06 14:08:43 +00:00
|
|
|
|
of inspectRlnDb:
|
|
|
|
|
doInspectRlnDb(conf)
|
2023-11-09 09:48:39 +00:00
|
|
|
|
of noCommand:
|
2024-04-23 16:53:18 +00: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-09-16 13:30:38 +00:00
|
|
|
|
var confCopy = conf
|
|
|
|
|
|
2024-04-24 13:59:50 +00:00
|
|
|
|
let restServer = rest_server_builder.startRestServerEsentials(
|
2024-09-16 13:30:38 +00:00
|
|
|
|
nodeHealthMonitor, confCopy
|
2024-04-24 13:59:50 +00:00
|
|
|
|
).valueOr:
|
|
|
|
|
error "Starting esential REST server failed.", error = $error
|
2024-04-23 16:53:18 +00:00
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
2024-09-16 13:30:38 +00:00
|
|
|
|
var waku = Waku.init(confCopy).valueOr:
|
2024-05-03 12:07:15 +00:00
|
|
|
|
error "Waku initialization failed", error = error
|
2023-11-09 09:48:39 +00:00
|
|
|
|
quit(QuitFailure)
|
2020-09-01 02:09:54 +00:00
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
waku.restServer = restServer
|
2024-04-24 13:59:50 +00:00
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
nodeHealthMonitor.setNode(waku.node)
|
2024-04-23 16:53:18 +00:00
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
(waitFor startWaku(addr waku)).isOkOr:
|
|
|
|
|
error "Starting waku failed", error = error
|
2023-11-09 09:48:39 +00:00
|
|
|
|
quit(QuitFailure)
|
2022-10-27 22:05:02 +00:00
|
|
|
|
|
2024-04-24 13:59:50 +00:00
|
|
|
|
rest_server_builder.startRestServerProtocolSupport(
|
2024-09-16 13:30:38 +00:00
|
|
|
|
restServer, waku.node, waku.wakuDiscv5, confCopy
|
2024-04-24 13:59:50 +00:00
|
|
|
|
).isOkOr:
|
|
|
|
|
error "Starting protocols support REST server failed.", error = $error
|
|
|
|
|
quit(QuitFailure)
|
2024-04-23 16:53:18 +00:00
|
|
|
|
|
2024-09-16 13:30:38 +00:00
|
|
|
|
waku.metricsServer = waku_metrics.startMetricsServerAndLogging(confCopy).valueOr:
|
2024-03-08 22:46:42 +00:00
|
|
|
|
error "Starting monitoring and external interfaces failed", error = error
|
2024-03-15 23:08:47 +00:00
|
|
|
|
quit(QuitFailure)
|
2022-10-27 22:05:02 +00:00
|
|
|
|
|
2024-04-23 16:53:18 +00:00
|
|
|
|
nodeHealthMonitor.setOverallHealth(HealthStatus.READY)
|
|
|
|
|
|
2024-03-08 22:46:42 +00:00
|
|
|
|
debug "Setting up shutdown hooks"
|
2023-11-09 09:48:39 +00:00
|
|
|
|
## Setup shutdown hooks for this process.
|
|
|
|
|
## Stop node gracefully on shutdown.
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
proc asyncStopper(node: Waku) {.async: (raises: [Exception]).} =
|
2024-04-23 16:53:18 +00:00
|
|
|
|
nodeHealthMonitor.setOverallHealth(HealthStatus.SHUTTING_DOWN)
|
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"
|
2024-05-03 12:07:15 +00:00
|
|
|
|
asyncSpawn asyncStopper(waku)
|
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"
|
2024-05-03 12:07:15 +00:00
|
|
|
|
asyncSpawn asyncStopper(waku)
|
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
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
waitFor waku.stop()
|
2023-11-09 09:48:39 +00:00
|
|
|
|
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()
|