2024-06-28 10:34:57 +00:00
|
|
|
|
{.push raises: [].}
|
2024-05-21 21:03:33 +00:00
|
|
|
|
|
|
|
|
|
import
|
|
|
|
|
std/[options, strutils, os, sequtils, net],
|
|
|
|
|
chronicles,
|
|
|
|
|
chronos,
|
|
|
|
|
metrics,
|
|
|
|
|
libbacktrace,
|
|
|
|
|
system/ansi_c,
|
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
|
confutils
|
|
|
|
|
|
|
|
|
|
import
|
2024-07-05 22:03:38 +00:00
|
|
|
|
waku/[
|
2024-10-25 20:59:02 +00:00
|
|
|
|
common/enr,
|
2024-07-05 22:03:38 +00:00
|
|
|
|
common/logging,
|
|
|
|
|
factory/waku,
|
|
|
|
|
factory/external_config,
|
2024-10-25 20:59:02 +00:00
|
|
|
|
waku_node,
|
2024-07-05 22:03:38 +00:00
|
|
|
|
node/health_monitor,
|
|
|
|
|
node/waku_metrics,
|
2024-10-25 20:59:02 +00:00
|
|
|
|
node/peer_manager,
|
2024-07-05 22:03:38 +00:00
|
|
|
|
waku_api/rest/builder as rest_server_builder,
|
2024-08-21 12:54:18 +00:00
|
|
|
|
waku_lightpush/common,
|
|
|
|
|
waku_filter_v2,
|
2024-10-25 20:59:02 +00:00
|
|
|
|
waku_peer_exchange/protocol,
|
|
|
|
|
waku_core/peers,
|
|
|
|
|
waku_core/multiaddrstr,
|
2024-07-05 22:03:38 +00:00
|
|
|
|
],
|
2024-05-21 21:03:33 +00:00
|
|
|
|
./tester_config,
|
|
|
|
|
./lightpush_publisher,
|
2024-08-21 12:54:18 +00:00
|
|
|
|
./filter_subscriber,
|
2024-10-25 20:59:02 +00:00
|
|
|
|
./diagnose_connections,
|
|
|
|
|
./service_peer_management
|
2024-05-21 21:03:33 +00:00
|
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
|
topics = "liteprotocoltester main"
|
|
|
|
|
|
|
|
|
|
proc logConfig(conf: LiteProtocolTesterConf) =
|
|
|
|
|
info "Configuration: Lite protocol tester", conf = $conf
|
|
|
|
|
|
|
|
|
|
{.pop.}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
const versionString = "version / git commit hash: " & waku.git_version
|
|
|
|
|
|
|
|
|
|
let confRes = LiteProtocolTesterConf.load(version = versionString)
|
|
|
|
|
if confRes.isErr():
|
|
|
|
|
error "failure while loading the configuration", error = confRes.error
|
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
|
|
var conf = confRes.get()
|
|
|
|
|
|
|
|
|
|
## Logging setup
|
|
|
|
|
logging.setupLog(conf.logLevel, conf.logFormat)
|
|
|
|
|
|
|
|
|
|
info "Running Lite Protocol Tester node", version = waku.git_version
|
|
|
|
|
logConfig(conf)
|
|
|
|
|
|
|
|
|
|
##Prepare Waku configuration
|
|
|
|
|
## - load from config file
|
|
|
|
|
## - override according to tester functionality
|
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
var wakuConf: WakuNodeConf
|
|
|
|
|
|
|
|
|
|
if conf.configFile.isSome():
|
|
|
|
|
try:
|
|
|
|
|
var configFile {.threadvar.}: InputFile
|
|
|
|
|
configFile = conf.configFile.get()
|
|
|
|
|
wakuConf = WakuNodeConf.load(
|
|
|
|
|
version = versionString,
|
|
|
|
|
printUsage = false,
|
|
|
|
|
secondarySources = proc(
|
|
|
|
|
wnconf: WakuNodeConf, sources: auto
|
|
|
|
|
) {.gcsafe, raises: [ConfigurationError].} =
|
|
|
|
|
echo "Loading secondary configuration file into WakuNodeConf"
|
2024-09-11 01:51:42 +00:00
|
|
|
|
sources.addConfigFile(Toml, configFile),
|
2024-05-21 21:03:33 +00:00
|
|
|
|
)
|
|
|
|
|
except CatchableError:
|
|
|
|
|
error "Loading Waku configuration failed", error = getCurrentExceptionMsg()
|
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
|
|
wakuConf.logLevel = conf.logLevel
|
|
|
|
|
wakuConf.logFormat = conf.logFormat
|
|
|
|
|
wakuConf.nat = conf.nat
|
2024-08-21 12:54:18 +00:00
|
|
|
|
wakuConf.maxConnections = 500
|
2024-05-21 21:03:33 +00:00
|
|
|
|
wakuConf.restAddress = conf.restAddress
|
|
|
|
|
wakuConf.restPort = conf.restPort
|
|
|
|
|
wakuConf.restAllowOrigin = conf.restAllowOrigin
|
|
|
|
|
|
2024-10-25 20:59:02 +00:00
|
|
|
|
wakuConf.dnsAddrs = true
|
|
|
|
|
wakuConf.dnsAddrsNameServers = @[parseIpAddress("8.8.8.8"), parseIpAddress("1.1.1.1")]
|
|
|
|
|
|
2024-05-21 21:03:33 +00:00
|
|
|
|
wakuConf.pubsubTopics = conf.pubsubTopics
|
|
|
|
|
wakuConf.contentTopics = conf.contentTopics
|
|
|
|
|
wakuConf.clusterId = conf.clusterId
|
|
|
|
|
## TODO: Depending on the tester needs we might extend here with shards, clusterId, etc...
|
|
|
|
|
|
2024-09-04 06:35:51 +00:00
|
|
|
|
wakuConf.metricsServer = true
|
|
|
|
|
wakuConf.metricsServerAddress = parseIpAddress("0.0.0.0")
|
2024-10-25 20:59:02 +00:00
|
|
|
|
wakuConf.metricsServerPort = conf.metricsPort
|
2024-05-21 21:03:33 +00:00
|
|
|
|
|
2024-10-25 20:59:02 +00:00
|
|
|
|
# If bootstrap option is chosen we expect our clients will not mounted
|
|
|
|
|
# so we will mount PeerExchange manually to gather possible service peers,
|
|
|
|
|
# if got some we will mount the client protocols afterward.
|
|
|
|
|
wakuConf.peerExchange = false
|
2024-05-21 21:03:33 +00:00
|
|
|
|
wakuConf.relay = false
|
|
|
|
|
wakuConf.filter = false
|
|
|
|
|
wakuConf.lightpush = false
|
|
|
|
|
wakuConf.store = false
|
|
|
|
|
|
2024-08-21 12:54:18 +00:00
|
|
|
|
wakuConf.rest = false
|
|
|
|
|
|
2024-05-21 21:03:33 +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)
|
|
|
|
|
|
|
|
|
|
let restServer = rest_server_builder.startRestServerEsentials(
|
|
|
|
|
nodeHealthMonitor, wakuConf
|
|
|
|
|
).valueOr:
|
|
|
|
|
error "Starting esential REST server failed.", error = $error
|
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
2024-10-28 08:17:46 +00:00
|
|
|
|
var wakuApp = Waku.new(wakuConf).valueOr:
|
2024-05-21 21:03:33 +00:00
|
|
|
|
error "Waku initialization failed", error = error
|
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
|
|
wakuApp.restServer = restServer
|
|
|
|
|
|
|
|
|
|
nodeHealthMonitor.setNode(wakuApp.node)
|
|
|
|
|
|
|
|
|
|
(waitFor startWaku(addr wakuApp)).isOkOr:
|
|
|
|
|
error "Starting waku failed", error = error
|
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
|
|
rest_server_builder.startRestServerProtocolSupport(
|
|
|
|
|
restServer, wakuApp.node, wakuApp.wakuDiscv5, wakuConf
|
|
|
|
|
).isOkOr:
|
|
|
|
|
error "Starting protocols support REST server failed.", error = $error
|
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
|
|
wakuApp.metricsServer = waku_metrics.startMetricsServerAndLogging(wakuConf).valueOr:
|
|
|
|
|
error "Starting monitoring and external interfaces failed", error = error
|
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
|
|
nodeHealthMonitor.setOverallHealth(HealthStatus.READY)
|
|
|
|
|
|
|
|
|
|
debug "Setting up shutdown hooks"
|
|
|
|
|
## Setup shutdown hooks for this process.
|
|
|
|
|
## Stop node gracefully on shutdown.
|
|
|
|
|
|
|
|
|
|
proc asyncStopper(wakuApp: Waku) {.async: (raises: [Exception]).} =
|
|
|
|
|
nodeHealthMonitor.setOverallHealth(HealthStatus.SHUTTING_DOWN)
|
|
|
|
|
await wakuApp.stop()
|
|
|
|
|
quit(QuitSuccess)
|
|
|
|
|
|
|
|
|
|
# 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(wakuApp)
|
|
|
|
|
|
|
|
|
|
setControlCHook(handleCtrlC)
|
|
|
|
|
|
|
|
|
|
# Handle SIGTERM
|
|
|
|
|
when defined(posix):
|
|
|
|
|
proc handleSigterm(signal: cint) {.noconv.} =
|
|
|
|
|
notice "Shutting down after receiving SIGTERM"
|
|
|
|
|
asyncSpawn asyncStopper(wakuApp)
|
|
|
|
|
|
|
|
|
|
c_signal(ansi_c.SIGTERM, handleSigterm)
|
|
|
|
|
|
|
|
|
|
# Handle SIGSEGV
|
|
|
|
|
when defined(posix):
|
|
|
|
|
proc handleSigsegv(signal: cint) {.noconv.} =
|
|
|
|
|
# Require --debugger:native
|
|
|
|
|
fatal "Shutting down after receiving SIGSEGV", stacktrace = getBacktrace()
|
|
|
|
|
|
|
|
|
|
# Not available in -d:release mode
|
|
|
|
|
writeStackTrace()
|
|
|
|
|
|
|
|
|
|
waitFor wakuApp.stop()
|
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
|
|
c_signal(ansi_c.SIGSEGV, handleSigsegv)
|
|
|
|
|
|
|
|
|
|
info "Node setup complete"
|
|
|
|
|
|
2024-10-25 20:59:02 +00:00
|
|
|
|
var codec = WakuLightPushCodec
|
|
|
|
|
# mounting relevant client, for PX filter client must be mounted ahead
|
|
|
|
|
if conf.testFunc == TesterFunctionality.SENDER:
|
|
|
|
|
wakuApp.node.mountLightPushClient()
|
|
|
|
|
codec = WakuLightPushCodec
|
|
|
|
|
else:
|
|
|
|
|
waitFor wakuApp.node.mountFilterClient()
|
|
|
|
|
codec = WakuFilterSubscribeCodec
|
|
|
|
|
|
|
|
|
|
var lookForServiceNode = false
|
|
|
|
|
var serviceNodePeerInfo: RemotePeerInfo
|
|
|
|
|
if conf.serviceNode.len == 0:
|
|
|
|
|
if conf.bootstrapNode.len > 0:
|
|
|
|
|
info "Bootstrapping with PeerExchange to gather random service node"
|
|
|
|
|
let futForServiceNode = pxLookupServiceNode(wakuApp.node, conf)
|
|
|
|
|
if not (waitFor futForServiceNode.withTimeout(20.minutes)):
|
|
|
|
|
error "Service node not found in time via PX"
|
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
|
|
if futForServiceNode.read().isErr():
|
|
|
|
|
error "Service node for test not found via PX"
|
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
|
|
serviceNodePeerInfo = selectRandomServicePeer(
|
|
|
|
|
wakuApp.node.peerManager, none(RemotePeerInfo), codec
|
|
|
|
|
).valueOr:
|
|
|
|
|
error "Service node selection failed"
|
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
else:
|
|
|
|
|
error "No service or bootstrap node provided"
|
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
else:
|
|
|
|
|
# support for both ENR and URI formatted service node addresses
|
|
|
|
|
serviceNodePeerInfo = translateToRemotePeerInfo(conf.serviceNode).valueOr:
|
|
|
|
|
error "failed to parse service-node", node = conf.serviceNode
|
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
|
|
info "Service node to be used", serviceNode = $serviceNodePeerInfo
|
|
|
|
|
|
|
|
|
|
logSelfPeers(wakuApp.node.peerManager)
|
|
|
|
|
|
2024-05-21 21:03:33 +00:00
|
|
|
|
if conf.testFunc == TesterFunctionality.SENDER:
|
2024-10-25 20:59:02 +00:00
|
|
|
|
setupAndPublish(wakuApp.node, conf, serviceNodePeerInfo)
|
2024-05-21 21:03:33 +00:00
|
|
|
|
else:
|
2024-10-25 20:59:02 +00:00
|
|
|
|
setupAndSubscribe(wakuApp.node, conf, serviceNodePeerInfo)
|
2024-05-21 21:03:33 +00:00
|
|
|
|
|
|
|
|
|
runForever()
|