Skip to content

Commit

Permalink
chore: move code from wakunode2 to a more generic place, waku (#2670)
Browse files Browse the repository at this point in the history
* testlib/wakunode.nim: not use cluster-id == 1 to avoid test rln by default
  • Loading branch information
Ivansete-status authored May 13, 2024
1 parent 6c3ad50 commit 840e012
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 65 deletions.
67 changes: 3 additions & 64 deletions apps/wakunode2/wakunode2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import
../../tools/rln_db_inspector/rln_db_inspector,
../../waku/common/logging,
../../waku/factory/external_config,
../../waku/factory/networks_config,
../../waku/factory/waku,
../../waku/node/health_monitor,
../../waku/node/waku_metrics,
Expand All @@ -26,31 +25,7 @@ import
logScope:
topics = "wakunode main"

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, maxPeers = conf.maxRelayPeers

for shard in conf.pubsubTopics:
info "Configuration. Shards", shard = shard

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,
rlnEpochSizeSec = conf.rlnEpochSizeSec,
rlnRelayUserMessageLimit = conf.rlnRelayUserMessageLimit,
rlnRelayEthClientAddress = string(conf.rlnRelayEthClientAddress)
const git_version* {.strdefine.} = "n/a"

{.pop.}
# @TODO confutils.nim(775, 17) Error: can raise an unlisted exception: ref IOError
Expand All @@ -65,15 +40,11 @@ when isMainModule:

const versionString = "version / git commit hash: " & waku.git_version

let confRes = WakuNodeConf.load(version = versionString)
if confRes.isErr():
error "failure while loading the configuration", error = confRes.error
var conf = WakuNodeConf.load(version = versionString).valueOr:
error "failure while loading the configuration", error = error
quit(QuitFailure)

var conf = confRes.get()

## Logging setup

# Adhere to NO_COLOR initiative: https://no-color.org/
let color =
try:
Expand All @@ -90,38 +61,6 @@ when isMainModule:
of inspectRlnDb:
doInspectRlnDb(conf)
of noCommand:
case conf.clusterId
# cluster-id=0
of 0:
let clusterZeroConf = ClusterConf.ClusterZeroConf()
conf.pubsubTopics = clusterZeroConf.pubsubTopics
# TODO: Write some template to "merge" the configs
# cluster-id=1 (aka The Waku Network)
of 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
conf.rlnEpochSizeSec = twnClusterConf.rlnEpochSizeSec
conf.rlnRelayUserMessageLimit = twnClusterConf.rlnRelayUserMessageLimit
else:
discard

info "Running nwaku node", version = waku.git_version
logConfig(conf)

# 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
Expand Down
4 changes: 4 additions & 0 deletions library/waku_thread/waku_thread.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type Context* = object
eventCallback*: pointer
eventUserdata*: pointer

const git_version* {.strdefine.} = "n/a"
const versionString = "version / git commit hash: " & waku.git_version

# To control when the thread is running
var running: Atomic[bool]

Expand All @@ -47,6 +50,7 @@ proc waku_init() =
proc run(ctx: ptr Context) {.thread.} =
## This is the worker thread body. This thread runs the Waku node
## and attends library user requests (stop, connect_to, etc.)
info "Starting Waku", version = versionString

var waku: Waku

Expand Down
4 changes: 3 additions & 1 deletion tests/testlib/wakunode.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import
# Waku node

proc defaultTestWakuNodeConf*(): WakuNodeConf =
## set cluster-id == 0 to not use TWN as that needs a background blockchain (e.g. anvil)
## running because RLN is mounted if TWN (cluster-id == 1) is configured.
WakuNodeConf(
cmd: noCommand,
tcpPort: Port(60000),
Expand All @@ -33,7 +35,7 @@ proc defaultTestWakuNodeConf*(): WakuNodeConf =
nat: "any",
maxConnections: 50,
maxMessageSize: "1024 KiB",
clusterId: 1.uint32,
clusterId: 0.uint32,
pubsubTopics: @["/waku/2/rs/1/0"],
relay: true,
storeMessageDbUrl: "sqlite://store.sqlite3",
Expand Down
65 changes: 65 additions & 0 deletions waku/factory/waku.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import
metrics,
metrics/chronos_httpserver
import
../../waku/common/logging,
../../waku/waku_core,
../../waku/waku_node,
../../waku/node/waku_metrics,
Expand All @@ -41,6 +42,7 @@ import
../../waku/waku_rln_relay,
../../waku/waku_store,
../../waku/waku_filter_v2,
../../waku/factory/networks_config,
../../waku/factory/node_factory,
../../waku/factory/internal_config,
../../waku/factory/external_config
Expand All @@ -65,6 +67,32 @@ type Waku* = object
restServer*: WakuRestServerRef
metricsServer*: MetricsHttpServerRef

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, maxPeers = conf.maxRelayPeers

for shard in conf.pubsubTopics:
info "Configuration. Shards", shard = shard

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,
rlnEpochSizeSec = conf.rlnEpochSizeSec,
rlnRelayUserMessageLimit = conf.rlnRelayUserMessageLimit,
rlnRelayEthClientAddress = string(conf.rlnRelayEthClientAddress)

func version*(waku: Waku): string =
waku.version

Expand All @@ -74,6 +102,43 @@ proc init*(T: type Waku, conf: WakuNodeConf): Result[Waku, string] =
var confCopy = conf
let rng = crypto.newRng()

logging.setupLogLevel(confCopy.logLevel)

case confCopy.clusterId

# cluster-id=0
of 0:
let clusterZeroConf = ClusterConf.ClusterZeroConf()
confCopy.pubsubTopics = clusterZeroConf.pubsubTopics
# TODO: Write some template to "merge" the configs

# cluster-id=1 (aka The Waku Network)
of 1:
let twnClusterConf = ClusterConf.TheWakuNetworkConf()
if len(confCopy.shards) != 0:
confCopy.pubsubTopics =
confCopy.shards.mapIt(twnClusterConf.pubsubTopics[it.uint16])
else:
confCopy.pubsubTopics = twnClusterConf.pubsubTopics

# Override configuration
confCopy.maxMessageSize = twnClusterConf.maxMessageSize
confCopy.clusterId = twnClusterConf.clusterId
confCopy.rlnRelay = twnClusterConf.rlnRelay
confCopy.rlnRelayEthContractAddress = twnClusterConf.rlnRelayEthContractAddress
confCopy.rlnRelayDynamic = twnClusterConf.rlnRelayDynamic
confCopy.rlnRelayBandwidthThreshold = twnClusterConf.rlnRelayBandwidthThreshold
confCopy.discv5Discovery = twnClusterConf.discv5Discovery
confCopy.discv5BootstrapNodes =
confCopy.discv5BootstrapNodes & twnClusterConf.discv5BootstrapNodes
confCopy.rlnEpochSizeSec = twnClusterConf.rlnEpochSizeSec
confCopy.rlnRelayUserMessageLimit = twnClusterConf.rlnRelayUserMessageLimit
else:
discard

info "Running nwaku node", version = git_version
logConfig(confCopy)

if not confCopy.nodekey.isSome():
let keyRes = crypto.PrivateKey.random(Secp256k1, rng[])
if keyRes.isErr():
Expand Down

0 comments on commit 840e012

Please sign in to comment.