2023-04-25 13:34:57 +00:00
|
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
else:
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
|
|
import
|
|
|
|
|
std/[options, strutils, sequtils],
|
|
|
|
|
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-05-13 15:45:48 +00:00
|
|
|
|
../../waku/common/logging,
|
2023-08-09 17:11:50 +00:00
|
|
|
|
../../waku/waku_core,
|
|
|
|
|
../../waku/waku_node,
|
|
|
|
|
../../waku/node/waku_metrics,
|
|
|
|
|
../../waku/node/peer_manager,
|
2024-04-23 16:53:18 +00:00
|
|
|
|
../../waku/node/health_monitor,
|
2023-09-22 13:36:46 +00:00
|
|
|
|
../../waku/waku_api/message_cache,
|
2023-10-27 19:43:54 +00:00
|
|
|
|
../../waku/waku_api/handlers,
|
2023-09-22 13:36:46 +00:00
|
|
|
|
../../waku/waku_api/rest/server,
|
|
|
|
|
../../waku/waku_api/rest/debug/handlers as rest_debug_api,
|
|
|
|
|
../../waku/waku_api/rest/relay/handlers as rest_relay_api,
|
|
|
|
|
../../waku/waku_api/rest/filter/handlers as rest_filter_api,
|
|
|
|
|
../../waku/waku_api/rest/lightpush/handlers as rest_lightpush_api,
|
|
|
|
|
../../waku/waku_api/rest/store/handlers as rest_store_api,
|
2024-04-25 13:09:52 +00:00
|
|
|
|
../../waku/waku_api/rest/legacy_store/handlers as rest_legacy_store_api,
|
2023-09-22 13:36:46 +00:00
|
|
|
|
../../waku/waku_api/rest/health/handlers as rest_health_api,
|
2023-10-05 12:00:09 +00:00
|
|
|
|
../../waku/waku_api/rest/admin/handlers as rest_admin_api,
|
2023-09-26 11:33:52 +00:00
|
|
|
|
../../waku/waku_archive,
|
2024-04-17 19:48:20 +00:00
|
|
|
|
../../waku/discovery/waku_dnsdisc,
|
|
|
|
|
../../waku/discovery/waku_discv5,
|
2024-01-30 12:15:23 +00:00
|
|
|
|
../../waku/waku_enr/sharding,
|
2023-09-26 11:33:52 +00:00
|
|
|
|
../../waku/waku_rln_relay,
|
|
|
|
|
../../waku/waku_store,
|
|
|
|
|
../../waku/waku_filter_v2,
|
2024-05-13 15:45:48 +00:00
|
|
|
|
../../waku/factory/networks_config,
|
2024-03-03 00:59:53 +00:00
|
|
|
|
../../waku/factory/node_factory,
|
|
|
|
|
../../waku/factory/internal_config,
|
|
|
|
|
../../waku/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-13 15:45:48 +00:00
|
|
|
|
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)
|
|
|
|
|
|
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()
|