mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-01-05 23:43:07 +00:00
* Separate new lightpush protocol New RPC defined Rename al occurence of old lightpush to legacy lightpush, fix rest tests of lightpush New lightpush protocol added back Setup new lightpush protocol, mounting and rest api for it modified: apps/chat2/chat2.nim modified: tests/node/test_wakunode_lightpush.nim modified: tests/node/test_wakunode_sharding.nim modified: tests/test_peer_manager.nim modified: tests/test_wakunode_lightpush.nim renamed: tests/waku_lightpush/lightpush_utils.nim -> tests/waku_lightpush_legacy/lightpush_utils.nim renamed: tests/waku_lightpush/test_all.nim -> tests/waku_lightpush_legacy/test_all.nim renamed: tests/waku_lightpush/test_client.nim -> tests/waku_lightpush_legacy/test_client.nim renamed: tests/waku_lightpush/test_ratelimit.nim -> tests/waku_lightpush_legacy/test_ratelimit.nim modified: tests/wakunode_rest/test_all.nim renamed: tests/wakunode_rest/test_rest_lightpush.nim -> tests/wakunode_rest/test_rest_lightpush_legacy.nim modified: waku/factory/node_factory.nim modified: waku/node/waku_node.nim modified: waku/waku_api/rest/admin/handlers.nim modified: waku/waku_api/rest/builder.nim new file: waku/waku_api/rest/legacy_lightpush/client.nim new file: waku/waku_api/rest/legacy_lightpush/handlers.nim new file: waku/waku_api/rest/legacy_lightpush/types.nim modified: waku/waku_api/rest/lightpush/client.nim modified: waku/waku_api/rest/lightpush/handlers.nim modified: waku/waku_api/rest/lightpush/types.nim modified: waku/waku_core/codecs.nim modified: waku/waku_lightpush.nim modified: waku/waku_lightpush/callbacks.nim modified: waku/waku_lightpush/client.nim modified: waku/waku_lightpush/common.nim modified: waku/waku_lightpush/protocol.nim modified: waku/waku_lightpush/rpc.nim modified: waku/waku_lightpush/rpc_codec.nim modified: waku/waku_lightpush/self_req_handler.nim new file: waku/waku_lightpush_legacy.nim renamed: waku/waku_lightpush/README.md -> waku/waku_lightpush_legacy/README.md new file: waku/waku_lightpush_legacy/callbacks.nim new file: waku/waku_lightpush_legacy/client.nim new file: waku/waku_lightpush_legacy/common.nim new file: waku/waku_lightpush_legacy/protocol.nim new file: waku/waku_lightpush_legacy/protocol_metrics.nim new file: waku/waku_lightpush_legacy/rpc.nim new file: waku/waku_lightpush_legacy/rpc_codec.nim new file: waku/waku_lightpush_legacy/self_req_handler.nim Adapt to non-invasive libp2p observers cherry pick latest lightpush (v1) changes into legacy lightpush code after rebase to latest master Fix vendor dependencies from origin/master after failed rebase of them Adjust examples, test to new lightpush - keep using of legacy Fixup error code mappings Fix REST admin interface with distinct legacy and new lightpush Fix lightpush v2 tests * Utilize new publishEx interface of pubsub libp2p * Adapt to latest libp2p pubslih design changes. publish returns an outcome as Result error. * Fix review findings * Fix tests, re-added lost one * Fix rebase * Apply suggestions from code review Co-authored-by: Ivan FB <128452529+Ivansete-status@users.noreply.github.com> * Addressing review comments * Fix incentivization tests * Fix build failed on libwaku * Change new lightpush endpoint version to 3 instead of 2. Noticed that old and new lightpush metrics can cause trouble in monitoring dashboards so decided to give new name as v3 for the new lightpush metrics and change legacy ones back - temporarly till old lightpush will be decommissioned * Fixing flaky test with rate limit timing * Fixing logscope of lightpush and legacy lightpush --------- Co-authored-by: Ivan FB <128452529+Ivansete-status@users.noreply.github.com>
249 lines
7.6 KiB
Nim
249 lines
7.6 KiB
Nim
{.push raises: [].}
|
||
|
||
import
|
||
std/[options, strutils, os, sequtils, net],
|
||
chronicles,
|
||
chronos,
|
||
metrics,
|
||
libbacktrace,
|
||
system/ansi_c,
|
||
libp2p/crypto/crypto,
|
||
confutils
|
||
|
||
import
|
||
waku/[
|
||
common/enr,
|
||
common/logging,
|
||
factory/waku,
|
||
factory/external_config,
|
||
waku_node,
|
||
node/health_monitor,
|
||
node/waku_metrics,
|
||
node/peer_manager,
|
||
waku_api/rest/builder as rest_server_builder,
|
||
waku_lightpush/common,
|
||
waku_filter_v2,
|
||
waku_peer_exchange/protocol,
|
||
waku_core/peers,
|
||
waku_core/multiaddrstr,
|
||
],
|
||
./tester_config,
|
||
./lightpush_publisher,
|
||
./filter_subscriber,
|
||
./diagnose_connections,
|
||
./service_peer_management
|
||
|
||
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"
|
||
sources.addConfigFile(Toml, configFile),
|
||
)
|
||
except CatchableError:
|
||
error "Loading Waku configuration failed", error = getCurrentExceptionMsg()
|
||
quit(QuitFailure)
|
||
|
||
wakuConf.logLevel = conf.logLevel
|
||
wakuConf.logFormat = conf.logFormat
|
||
wakuConf.nat = conf.nat
|
||
wakuConf.maxConnections = 500
|
||
wakuConf.restAddress = conf.restAddress
|
||
wakuConf.restPort = conf.restPort
|
||
wakuConf.restAllowOrigin = conf.restAllowOrigin
|
||
|
||
wakuConf.dnsAddrs = true
|
||
wakuConf.dnsAddrsNameServers = @[parseIpAddress("8.8.8.8"), parseIpAddress("1.1.1.1")]
|
||
|
||
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...
|
||
|
||
wakuConf.metricsServer = true
|
||
wakuConf.metricsServerAddress = parseIpAddress("0.0.0.0")
|
||
wakuConf.metricsServerPort = conf.metricsPort
|
||
|
||
# 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
|
||
wakuConf.relay = false
|
||
wakuConf.filter = false
|
||
wakuConf.lightpush = false
|
||
wakuConf.store = false
|
||
|
||
wakuConf.rest = false
|
||
|
||
# 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)
|
||
|
||
var wakuApp = Waku.new(wakuConf).valueOr:
|
||
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"
|
||
|
||
var codec = WakuLightPushCodec
|
||
# mounting relevant client, for PX filter client must be mounted ahead
|
||
if conf.testFunc == TesterFunctionality.SENDER:
|
||
wakuApp.node.mountLegacyLightPushClient()
|
||
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)
|
||
|
||
if conf.testFunc == TesterFunctionality.SENDER:
|
||
setupAndPublish(wakuApp.node, conf, serviceNodePeerInfo)
|
||
else:
|
||
setupAndSubscribe(wakuApp.node, conf, serviceNodePeerInfo)
|
||
|
||
runForever()
|