2024-06-28 10:34:57 +00:00
|
|
|
|
{.push raises: [].}
|
2023-04-25 13:34:57 +00:00
|
|
|
|
|
|
|
|
|
import
|
2024-05-16 20:29:11 +00:00
|
|
|
|
std/[options, sequtils],
|
2023-04-25 13:34:57 +00:00
|
|
|
|
stew/results,
|
|
|
|
|
chronicles,
|
|
|
|
|
chronos,
|
2023-10-27 07:11:47 +00:00
|
|
|
|
libp2p/wire,
|
|
|
|
|
libp2p/multicodec,
|
2023-04-25 13:34:57 +00:00
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
|
libp2p/protocols/pubsub/gossipsub,
|
|
|
|
|
libp2p/peerid,
|
|
|
|
|
eth/keys,
|
2023-04-26 17:25:18 +00:00
|
|
|
|
presto,
|
|
|
|
|
metrics,
|
|
|
|
|
metrics/chronos_httpserver
|
2023-04-25 13:34:57 +00:00
|
|
|
|
import
|
2024-07-05 22:03:38 +00:00
|
|
|
|
../common/logging,
|
|
|
|
|
../waku_core,
|
|
|
|
|
../waku_node,
|
|
|
|
|
../node/peer_manager,
|
|
|
|
|
../node/health_monitor,
|
|
|
|
|
../waku_api/message_cache,
|
|
|
|
|
../waku_api/rest/server,
|
|
|
|
|
../waku_archive,
|
|
|
|
|
../discovery/waku_dnsdisc,
|
|
|
|
|
../discovery/waku_discv5,
|
|
|
|
|
../waku_enr/sharding,
|
|
|
|
|
../waku_rln_relay,
|
|
|
|
|
../waku_store,
|
|
|
|
|
../waku_filter_v2,
|
|
|
|
|
../factory/networks_config,
|
|
|
|
|
../factory/node_factory,
|
|
|
|
|
../factory/internal_config,
|
|
|
|
|
../factory/external_config
|
2023-04-25 13:34:57 +00:00
|
|
|
|
|
|
|
|
|
logScope:
|
2024-05-03 12:07:15 +00:00
|
|
|
|
topics = "wakunode waku"
|
2023-04-25 13:34:57 +00:00
|
|
|
|
|
|
|
|
|
# Git version in git describe format (defined at compile time)
|
|
|
|
|
const git_version* {.strdefine.} = "n/a"
|
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
type Waku* = object
|
|
|
|
|
version: string
|
|
|
|
|
conf: WakuNodeConf
|
|
|
|
|
rng: ref HmacDrbgContext
|
|
|
|
|
key: crypto.PrivateKey
|
2023-06-22 20:58:14 +00:00
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
wakuDiscv5*: WakuDiscoveryV5
|
|
|
|
|
dynamicBootstrapNodes: seq[RemotePeerInfo]
|
2023-04-25 13:34:57 +00:00
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
node*: WakuNode
|
2023-04-25 13:34:57 +00:00
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
restServer*: WakuRestServerRef
|
|
|
|
|
metricsServer*: MetricsHttpServerRef
|
2023-04-25 13:34:57 +00:00
|
|
|
|
|
2024-05-13 15:45:48 +00:00
|
|
|
|
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)
|
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
func version*(waku: Waku): string =
|
|
|
|
|
waku.version
|
2023-04-25 13:34:57 +00:00
|
|
|
|
|
|
|
|
|
## Initialisation
|
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
proc init*(T: type Waku, conf: WakuNodeConf): Result[Waku, string] =
|
2024-03-08 22:46:42 +00:00
|
|
|
|
var confCopy = conf
|
|
|
|
|
let rng = crypto.newRng()
|
|
|
|
|
|
2024-05-17 14:28:54 +00:00
|
|
|
|
logging.setupLog(conf.logLevel, conf.logFormat)
|
2024-05-13 15:45:48 +00:00
|
|
|
|
|
|
|
|
|
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
|
2024-06-28 09:19:16 +00:00
|
|
|
|
confCopy.rlnRelayChainId = twnClusterConf.rlnRelayChainId
|
2024-05-13 15:45:48 +00:00
|
|
|
|
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)
|
|
|
|
|
|
2024-03-08 22:46:42 +00:00
|
|
|
|
if not confCopy.nodekey.isSome():
|
|
|
|
|
let keyRes = crypto.PrivateKey.random(Secp256k1, rng[])
|
2024-03-15 23:08:47 +00:00
|
|
|
|
if keyRes.isErr():
|
2024-03-08 22:46:42 +00:00
|
|
|
|
error "Failed to generate key", error = $keyRes.error
|
|
|
|
|
return err("Failed to generate key: " & $keyRes.error)
|
|
|
|
|
confCopy.nodekey = some(keyRes.get())
|
|
|
|
|
|
|
|
|
|
debug "Retrieve dynamic bootstrap nodes"
|
2024-05-01 19:13:08 +00:00
|
|
|
|
let dynamicBootstrapNodesRes = waku_dnsdisc.retrieveDynamicBootstrapNodes(
|
2024-03-15 23:08:47 +00:00
|
|
|
|
confCopy.dnsDiscovery, confCopy.dnsDiscoveryUrl, confCopy.dnsDiscoveryNameServers
|
|
|
|
|
)
|
2024-03-08 22:46:42 +00:00
|
|
|
|
if dynamicBootstrapNodesRes.isErr():
|
2024-03-15 23:08:47 +00:00
|
|
|
|
error "Retrieving dynamic bootstrap nodes failed",
|
|
|
|
|
error = dynamicBootstrapNodesRes.error
|
|
|
|
|
return err(
|
|
|
|
|
"Retrieving dynamic bootstrap nodes failed: " & dynamicBootstrapNodesRes.error
|
|
|
|
|
)
|
2024-03-08 22:46:42 +00:00
|
|
|
|
|
|
|
|
|
let nodeRes = setupNode(confCopy, some(rng))
|
2024-03-15 23:08:47 +00:00
|
|
|
|
if nodeRes.isErr():
|
|
|
|
|
error "Failed setting up node", error = nodeRes.error
|
2024-03-08 22:46:42 +00:00
|
|
|
|
return err("Failed setting up node: " & nodeRes.error)
|
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
var waku = Waku(
|
2024-03-15 23:08:47 +00:00
|
|
|
|
version: git_version,
|
|
|
|
|
conf: confCopy,
|
|
|
|
|
rng: rng,
|
|
|
|
|
key: confCopy.nodekey.get(),
|
|
|
|
|
node: nodeRes.get(),
|
|
|
|
|
dynamicBootstrapNodes: dynamicBootstrapNodesRes.get(),
|
|
|
|
|
)
|
2024-03-08 22:46:42 +00:00
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
ok(waku)
|
2023-04-25 13:34:57 +00:00
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
|
proc getPorts(
|
|
|
|
|
listenAddrs: seq[MultiAddress]
|
2024-05-03 12:07:15 +00:00
|
|
|
|
): Result[tuple[tcpPort, websocketPort: Option[Port]], string] =
|
2023-10-27 07:11:47 +00:00
|
|
|
|
var tcpPort, websocketPort = none(Port)
|
|
|
|
|
|
|
|
|
|
for a in listenAddrs:
|
|
|
|
|
if a.isWsAddress():
|
|
|
|
|
if websocketPort.isNone():
|
|
|
|
|
let wsAddress = initTAddress(a).valueOr:
|
|
|
|
|
return err("getPorts wsAddr error:" & $error)
|
|
|
|
|
websocketPort = some(wsAddress.port)
|
|
|
|
|
elif tcpPort.isNone():
|
|
|
|
|
let tcpAddress = initTAddress(a).valueOr:
|
|
|
|
|
return err("getPorts tcpAddr error:" & $error)
|
|
|
|
|
tcpPort = some(tcpAddress.port)
|
|
|
|
|
|
|
|
|
|
return ok((tcpPort: tcpPort, websocketPort: websocketPort))
|
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
proc getRunningNetConfig(waku: ptr Waku): Result[NetConfig, string] =
|
|
|
|
|
var conf = waku[].conf
|
|
|
|
|
let (tcpPort, websocketPort) = getPorts(waku[].node.switch.peerInfo.listenAddrs).valueOr:
|
2023-10-27 07:11:47 +00:00
|
|
|
|
return err("Could not retrieve ports " & error)
|
|
|
|
|
|
|
|
|
|
if tcpPort.isSome():
|
|
|
|
|
conf.tcpPort = tcpPort.get()
|
|
|
|
|
|
|
|
|
|
if websocketPort.isSome():
|
|
|
|
|
conf.websocketPort = websocketPort.get()
|
|
|
|
|
|
|
|
|
|
# Rebuild NetConfig with bound port values
|
|
|
|
|
let netConf = networkConfiguration(conf, clientId).valueOr:
|
|
|
|
|
return err("Could not update NetConfig: " & error)
|
|
|
|
|
|
2024-03-08 22:46:42 +00:00
|
|
|
|
return ok(netConf)
|
2023-10-27 07:11:47 +00:00
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
proc updateEnr(waku: ptr Waku, netConf: NetConfig): Result[void, string] =
|
|
|
|
|
let record = enrConfiguration(waku[].conf, netConf, waku[].key).valueOr:
|
2023-11-21 20:15:39 +00:00
|
|
|
|
return err("ENR setup failed: " & error)
|
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
if isClusterMismatched(record, waku[].conf.clusterId):
|
2024-01-30 12:15:23 +00:00
|
|
|
|
return err("cluster id mismatch configured shards")
|
2023-10-27 07:11:47 +00:00
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
waku[].node.enr = record
|
2023-10-27 07:11:47 +00:00
|
|
|
|
|
|
|
|
|
return ok()
|
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
proc updateWaku(waku: ptr Waku): Result[void, string] =
|
|
|
|
|
if waku[].conf.tcpPort == Port(0) or waku[].conf.websocketPort == Port(0):
|
|
|
|
|
let netConf = getRunningNetConfig(waku).valueOr:
|
2023-10-27 07:11:47 +00:00
|
|
|
|
return err("error calling updateNetConfig: " & $error)
|
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
updateEnr(waku, netConf).isOkOr:
|
2023-10-27 07:11:47 +00:00
|
|
|
|
return err("error calling updateEnr: " & $error)
|
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
waku[].node.announcedAddresses = netConf.announcedAddresses
|
2023-10-27 07:11:47 +00:00
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
printNodeNetworkInfo(waku[].node)
|
2023-10-27 07:11:47 +00:00
|
|
|
|
|
|
|
|
|
return ok()
|
2023-04-25 13:34:57 +00:00
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
proc startWaku*(waku: ptr Waku): Future[Result[void, string]] {.async: (raises: []).} =
|
|
|
|
|
(await startNode(waku.node, waku.conf, waku.dynamicBootstrapNodes)).isOkOr:
|
|
|
|
|
return err("error while calling startNode: " & $error)
|
2023-10-27 07:11:47 +00:00
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
# Update waku data that is set dynamically on node start
|
|
|
|
|
updateWaku(waku).isOkOr:
|
2023-10-27 07:11:47 +00:00
|
|
|
|
return err("Error in updateApp: " & $error)
|
|
|
|
|
|
2024-03-08 22:46:42 +00:00
|
|
|
|
## Discv5
|
2024-05-03 12:07:15 +00:00
|
|
|
|
if waku[].conf.discv5Discovery:
|
|
|
|
|
waku[].wakuDiscV5 = waku_discv5.setupDiscoveryV5(
|
|
|
|
|
waku.node.enr, waku.node.peerManager, waku.node.topicSubscriptionQueue, waku.conf,
|
|
|
|
|
waku.dynamicBootstrapNodes, waku.rng, waku.key,
|
2024-05-01 19:13:08 +00:00
|
|
|
|
)
|
2023-06-27 13:50:11 +00:00
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
(await waku.wakuDiscV5.start()).isOkOr:
|
2024-05-01 19:13:08 +00:00
|
|
|
|
return err("failed to start waku discovery v5: " & $error)
|
2023-06-27 13:50:11 +00:00
|
|
|
|
|
2023-10-27 07:11:47 +00:00
|
|
|
|
return ok()
|
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
# Waku shutdown
|
2023-04-25 13:34:57 +00:00
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
proc stop*(waku: Waku): Future[void] {.async: (raises: [Exception]).} =
|
|
|
|
|
if not waku.restServer.isNil():
|
|
|
|
|
await waku.restServer.stop()
|
2023-04-25 13:34:57 +00:00
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
if not waku.metricsServer.isNil():
|
|
|
|
|
await waku.metricsServer.stop()
|
2023-04-26 17:25:18 +00:00
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
if not waku.wakuDiscv5.isNil():
|
|
|
|
|
await waku.wakuDiscv5.stop()
|
2023-06-27 13:50:11 +00:00
|
|
|
|
|
2024-05-03 12:07:15 +00:00
|
|
|
|
if not waku.node.isNil():
|
|
|
|
|
await waku.node.stop()
|