chore: Simplify configuration for the waku network (#2404)

This commit is contained in:
Alvaro Revuelta 2024-02-07 12:42:20 +01:00 committed by GitHub
parent 1b4026673a
commit 985d092fbc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 133 additions and 15 deletions

View File

@ -34,6 +34,8 @@ type ProtectedTopic* = object
topic*: string topic*: string
key*: secp256k1.SkPublicKey key*: secp256k1.SkPublicKey
type ShardIdx = distinct uint16
type StartUpCommand* = enum type StartUpCommand* = enum
noCommand # default, runs waku noCommand # default, runs waku
generateRlnKeystore # generates a new RLN keystore generateRlnKeystore # generates a new RLN keystore
@ -256,6 +258,11 @@ type
desc: "Default pubsub topic to subscribe to. Argument may be repeated." desc: "Default pubsub topic to subscribe to. Argument may be repeated."
name: "pubsub-topic" .}: seq[string] name: "pubsub-topic" .}: seq[string]
shards* {.
desc: "Shards index to subscribe to [0..MAX_SHARDS-1]. Argument may be repeated."
defaultValue: @[]
name: "shard" .}: seq[ShardIdx]
contentTopics* {. contentTopics* {.
desc: "Default content topic to subscribe to. Argument may be repeated." desc: "Default content topic to subscribe to. Argument may be repeated."
name: "content-topic" .}: seq[string] name: "content-topic" .}: seq[string]
@ -576,6 +583,15 @@ proc parseCmdArg*(T: type Option[int], p: string): T =
except CatchableError: except CatchableError:
raise newException(ValueError, "Invalid number") raise newException(ValueError, "Invalid number")
proc completeCmdArg*(T: type ShardIdx, val: string): seq[ShardIdx] =
return @[]
proc parseCmdArg*(T: type ShardIdx, p: string): T =
try:
ShardIdx(parseInt(p))
except CatchableError:
raise newException(ValueError, "Invalid shard index")
proc parseCmdArg*(T: type Option[uint], p: string): T = proc parseCmdArg*(T: type Option[uint], p: string): T =
try: try:
some(parseUint(p)) some(parseUint(p))
@ -608,6 +624,18 @@ proc readValue*(r: var EnvvarReader, value: var ProtectedTopic) {.raises: [Seria
except CatchableError: except CatchableError:
raise newException(SerializationError, getCurrentExceptionMsg()) raise newException(SerializationError, getCurrentExceptionMsg())
proc readValue*(r: var TomlReader, value: var ShardIdx) {.raises: [SerializationError].} =
try:
value = parseCmdArg(ShardIdx, r.readValue(string))
except CatchableError:
raise newException(SerializationError, getCurrentExceptionMsg())
proc readValue*(r: var EnvvarReader, value: var ShardIdx) {.raises: [SerializationError].} =
try:
value = parseCmdArg(ShardIdx, r.readValue(string))
except CatchableError:
raise newException(SerializationError, getCurrentExceptionMsg())
{.push warning[ProveInit]: off.} {.push warning[ProveInit]: off.}
proc load*(T: type WakuNodeConf, version=""): ConfResult[T] = proc load*(T: type WakuNodeConf, version=""): ConfResult[T] =

View File

@ -0,0 +1,40 @@
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}
type ClusterConf* = object
maxMessageSize*: string
clusterId*: uint32
rlnRelay*: bool
rlnRelayEthContractAddress*: string
rlnRelayDynamic*: bool
rlnRelayBandwidthThreshold*: int
pubsubTopics*: seq[string]
discv5Discovery*: bool
discv5BootstrapNodes*: seq[string]
# cluster-id=1
# Cluster configuration corresponding to The Waku Network. Note that it
# overrides existing cli configuration
proc TheWakuNetworkConf*(T: type ClusterConf): ClusterConf =
return ClusterConf(
maxMessageSize: "150KiB",
clusterId: 1.uint32,
rlnRelay: true,
rlnRelayEthContractAddress: "0xF471d71E9b1455bBF4b85d475afb9BB0954A29c4",
rlnRelayDynamic: true,
rlnRelayBandwidthThreshold: 0,
pubsubTopics:
@[
"/waku/2/rs/1/0", "/waku/2/rs/1/1", "/waku/2/rs/1/2", "/waku/2/rs/1/3",
"/waku/2/rs/1/4", "/waku/2/rs/1/5", "/waku/2/rs/1/6", "/waku/2/rs/1/7"
],
discv5Discovery: true,
discv5BootstrapNodes:
@[
"enr:-QESuEC1p_s3xJzAC_XlOuuNrhVUETmfhbm1wxRGis0f7DlqGSw2FM-p2Ugl_r25UHQJ3f1rIRrpzxJXSMaJe4yk1XFSAYJpZIJ2NIJpcISygI2rim11bHRpYWRkcnO4XAArNiZub2RlLTAxLmRvLWFtczMud2FrdS50ZXN0LnN0Y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
std/[options, strutils, os], std/[options, strutils, os, sequtils],
stew/shims/net as stewNet, stew/shims/net as stewNet,
chronicles, chronicles,
chronos, chronos,
@ -16,12 +16,37 @@ import
../../tools/rln_keystore_generator/rln_keystore_generator, ../../tools/rln_keystore_generator/rln_keystore_generator,
../../waku/common/logging, ../../waku/common/logging,
./external_config, ./external_config,
./networks_config,
./app ./app
logScope: logScope:
topics = "wakunode main" topics = "wakunode main"
{.pop.} # @TODO confutils.nim(775, 17) Error: can raise an unlisted exception: ref IOError 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
when isMainModule: when isMainModule:
## Node setup happens in 6 phases: ## Node setup happens in 6 phases:
## 1. Set up storage ## 1. Set up storage
@ -34,23 +59,48 @@ when isMainModule:
const versionString = "version / git commit hash: " & app.git_version const versionString = "version / git commit hash: " & app.git_version
let rng = crypto.newRng() let rng = crypto.newRng()
let confRes = WakuNodeConf.load(version=versionString) let confRes = WakuNodeConf.load(version = versionString)
if confRes.isErr(): if confRes.isErr():
error "failure while loading the configuration", error=confRes.error error "failure while loading the configuration", error = confRes.error
quit(QuitFailure) quit(QuitFailure)
let conf = confRes.get() var conf = confRes.get()
# 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
## Logging setup ## Logging setup
# Adhere to NO_COLOR initiative: https://no-color.org/ # Adhere to NO_COLOR initiative: https://no-color.org/
let color = try: not parseBool(os.getEnv("NO_COLOR", "false")) let color =
except CatchableError: true try:
not parseBool(os.getEnv("NO_COLOR", "false"))
except CatchableError:
true
logging.setupLogLevel(conf.logLevel) logging.setupLogLevel(conf.logLevel)
logging.setupLogFormat(conf.logFormat, color) logging.setupLogFormat(conf.logFormat, color)
case conf.cmd: info "Running nwaku node", version = app.git_version
logConfig(conf)
case conf.cmd
of generateRlnKeystore: of generateRlnKeystore:
doRlnKeystoreGenerator(conf) doRlnKeystoreGenerator(conf)
of noCommand: of noCommand:
@ -65,42 +115,42 @@ when isMainModule:
## Peer persistence ## Peer persistence
let res1 = wakunode2.setupPeerPersistence() let res1 = wakunode2.setupPeerPersistence()
if res1.isErr(): if res1.isErr():
error "1/7 Setting up storage failed", error=res1.error error "1/7 Setting up storage failed", error = res1.error
quit(QuitFailure) quit(QuitFailure)
debug "2/7 Retrieve dynamic bootstrap nodes" debug "2/7 Retrieve dynamic bootstrap nodes"
let res3 = wakunode2.setupDyamicBootstrapNodes() let res3 = wakunode2.setupDyamicBootstrapNodes()
if res3.isErr(): if res3.isErr():
error "2/7 Retrieving dynamic bootstrap nodes failed", error=res3.error error "2/7 Retrieving dynamic bootstrap nodes failed", error = res3.error
quit(QuitFailure) quit(QuitFailure)
debug "3/7 Initializing node" debug "3/7 Initializing node"
let res4 = wakunode2.setupWakuApp() let res4 = wakunode2.setupWakuApp()
if res4.isErr(): if res4.isErr():
error "3/7 Initializing node failed", error=res4.error error "3/7 Initializing node failed", error = res4.error
quit(QuitFailure) quit(QuitFailure)
debug "4/7 Mounting protocols" debug "4/7 Mounting protocols"
let res5 = waitFor wakunode2.setupAndMountProtocols() let res5 = waitFor wakunode2.setupAndMountProtocols()
if res5.isErr(): if res5.isErr():
error "4/7 Mounting protocols failed", error=res5.error error "4/7 Mounting protocols failed", error = res5.error
quit(QuitFailure) quit(QuitFailure)
debug "5/7 Starting node and mounted protocols" debug "5/7 Starting node and mounted protocols"
let res6 = wakunode2.startApp() let res6 = wakunode2.startApp()
if res6.isErr(): if res6.isErr():
error "5/7 Starting node and protocols failed", error=res6.error error "5/7 Starting node and protocols failed", error = res6.error
quit(QuitFailure) quit(QuitFailure)
debug "6/7 Starting monitoring and external interfaces" debug "6/7 Starting monitoring and external interfaces"
let res7 = wakunode2.setupMonitoringAndExternalInterfaces() let res7 = wakunode2.setupMonitoringAndExternalInterfaces()
if res7.isErr(): if res7.isErr():
error "6/7 Starting monitoring and external interfaces failed", error=res7.error error "6/7 Starting monitoring and external interfaces failed", error = res7.error
quit(QuitFailure) quit(QuitFailure)
debug "7/7 Setting up shutdown hooks" debug "7/7 Setting up shutdown hooks"
@ -133,7 +183,7 @@ when isMainModule:
when defined(posix): when defined(posix):
proc handleSigsegv(signal: cint) {.noconv.} = proc handleSigsegv(signal: cint) {.noconv.} =
# Require --debugger:native # Require --debugger:native
fatal "Shutting down after receiving SIGSEGV", stacktrace=getBacktrace() fatal "Shutting down after receiving SIGSEGV", stacktrace = getBacktrace()
# Not available in -d:release mode # Not available in -d:release mode
writeStackTrace() writeStackTrace()