2021-07-14 17:58:46 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2020-04-29 04:49:27 +00:00
|
|
|
import
|
2022-10-18 14:05:53 +00:00
|
|
|
std/[options, tables, strutils, sequtils, os],
|
2021-06-09 14:37:08 +00:00
|
|
|
chronos, chronicles, metrics,
|
|
|
|
stew/shims/net as stewNet,
|
2020-09-01 02:09:54 +00:00
|
|
|
eth/keys,
|
2021-08-12 08:51:38 +00:00
|
|
|
eth/p2p/discoveryv5/enr,
|
2020-05-15 04:11:14 +00:00
|
|
|
libp2p/crypto/crypto,
|
2021-06-15 08:55:47 +00:00
|
|
|
libp2p/protocols/ping,
|
2022-01-10 14:07:01 +00:00
|
|
|
libp2p/protocols/pubsub/[gossipsub, rpc/messages],
|
|
|
|
libp2p/[builders, multihash],
|
2022-10-18 14:05:53 +00:00
|
|
|
libp2p/transports/[transport, wstransport]
|
2022-07-25 11:01:37 +00:00
|
|
|
import
|
|
|
|
../protocol/waku_store,
|
2022-08-12 10:15:51 +00:00
|
|
|
../protocol/waku_filter,
|
2022-09-20 11:03:34 +00:00
|
|
|
../protocol/waku_rln_relay/waku_rln_relay_types,
|
|
|
|
../protocol/waku_peer_exchange,
|
2022-10-18 14:05:53 +00:00
|
|
|
../utils/[peers, wakuenr],
|
2021-08-12 08:51:38 +00:00
|
|
|
./peer_manager/peer_manager,
|
2022-09-08 09:02:50 +00:00
|
|
|
./storage/message/waku_store_queue,
|
2022-09-16 10:55:22 +00:00
|
|
|
./storage/message/message_retention_policy_capacity,
|
|
|
|
./storage/message/message_retention_policy_time,
|
2021-11-01 18:02:39 +00:00
|
|
|
./dnsdisc/waku_dnsdisc,
|
2022-06-27 19:35:26 +00:00
|
|
|
./discv5/waku_discv5,
|
2022-09-08 09:02:50 +00:00
|
|
|
./wakuswitch,
|
2022-10-18 14:05:53 +00:00
|
|
|
./waku_node
|
2020-10-21 09:54:29 +00:00
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
|
2021-07-14 17:58:46 +00:00
|
|
|
{.pop.} # @TODO confutils.nim(775, 17) Error: can raise an unlisted exception: ref IOError
|
2020-10-21 09:54:29 +00:00
|
|
|
when isMainModule:
|
2021-07-22 09:46:54 +00:00
|
|
|
## 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
|
|
|
|
|
2020-10-21 09:54:29 +00:00
|
|
|
import
|
2022-05-17 15:48:08 +00:00
|
|
|
confutils, toml_serialization,
|
2021-04-15 08:18:14 +00:00
|
|
|
system/ansi_c,
|
2022-02-16 16:12:09 +00:00
|
|
|
libp2p/nameresolving/dnsresolver,
|
2021-07-22 09:46:54 +00:00
|
|
|
../../common/utils/nat,
|
|
|
|
./config,
|
2022-07-19 11:11:46 +00:00
|
|
|
./wakunode2_setup,
|
2022-06-28 10:22:59 +00:00
|
|
|
./wakunode2_setup_rest,
|
2022-07-17 15:16:57 +00:00
|
|
|
./wakunode2_setup_metrics,
|
2022-07-17 14:25:21 +00:00
|
|
|
./wakunode2_setup_rpc,
|
2022-07-19 09:19:18 +00:00
|
|
|
./wakunode2_setup_sql_migrations,
|
2022-09-14 16:09:08 +00:00
|
|
|
./storage/sqlite,
|
2022-09-20 09:39:52 +00:00
|
|
|
./storage/message/dual_message_store,
|
2022-09-13 11:36:04 +00:00
|
|
|
./storage/message/sqlite_store,
|
2021-07-22 09:46:54 +00:00
|
|
|
./storage/peer/waku_peer_storage
|
2022-10-18 14:05:53 +00:00
|
|
|
|
|
|
|
when defined(rln) or defined(rlnzerokit):
|
|
|
|
import ../protocol/waku_rln_relay/waku_rln_relay_utils
|
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "wakunode.setup"
|
|
|
|
|
|
|
|
###################
|
|
|
|
# Setup functions #
|
|
|
|
###################
|
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
# 1/7 Setup storage
|
2021-07-22 09:46:54 +00:00
|
|
|
proc setupStorage(conf: WakuNodeConf):
|
2022-09-20 09:39:52 +00:00
|
|
|
SetupResult[tuple[pStorage: WakuPeerStorage, mStorage: MessageStore]] =
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
|
|
## Setup a SQLite Database for a wakunode based on a supplied
|
|
|
|
## configuration file and perform all necessary migration.
|
|
|
|
##
|
|
|
|
## If config allows, return peer storage and message store
|
|
|
|
## for use elsewhere.
|
|
|
|
|
|
|
|
var
|
|
|
|
sqliteDatabase: SqliteDatabase
|
2022-09-20 09:39:52 +00:00
|
|
|
storeTuple: tuple[pStorage: WakuPeerStorage, mStorage: MessageStore]
|
2021-07-22 09:46:54 +00:00
|
|
|
|
2022-09-20 09:39:52 +00:00
|
|
|
# Setup database connection
|
2021-07-22 09:46:54 +00:00
|
|
|
if conf.dbPath != "":
|
|
|
|
let dbRes = SqliteDatabase.init(conf.dbPath)
|
2022-09-20 09:39:52 +00:00
|
|
|
if dbRes.isErr():
|
|
|
|
warn "failed to init database connection", err = dbRes.error
|
2021-07-22 09:46:54 +00:00
|
|
|
waku_node_errors.inc(labelValues = ["init_db_failure"])
|
2022-09-20 09:39:52 +00:00
|
|
|
return err("failed to init database connection")
|
2021-07-22 09:46:54 +00:00
|
|
|
else:
|
|
|
|
sqliteDatabase = dbRes.value
|
|
|
|
|
2022-09-20 09:39:52 +00:00
|
|
|
|
|
|
|
if not sqliteDatabase.isNil():
|
2022-09-14 16:09:08 +00:00
|
|
|
## Database vacuuming
|
|
|
|
# TODO: Wrap and move this logic to the appropriate module
|
|
|
|
let
|
|
|
|
pageSize = ?sqliteDatabase.getPageSize()
|
|
|
|
pageCount = ?sqliteDatabase.getPageCount()
|
|
|
|
freelistCount = ?sqliteDatabase.getFreelistCount()
|
|
|
|
|
|
|
|
debug "sqlite database page stats", pageSize=pageSize, pages=pageCount, freePages=freelistCount
|
|
|
|
|
|
|
|
# TODO: Run vacuuming conditionally based on database page stats
|
|
|
|
if conf.dbVacuum and (pageCount > 0 and freelistCount > 0):
|
|
|
|
debug "starting sqlite database vacuuming"
|
|
|
|
|
|
|
|
let resVacuum = sqliteDatabase.vacuum()
|
|
|
|
if resVacuum.isErr():
|
|
|
|
return err("failed to execute vacuum: " & resVacuum.error())
|
|
|
|
|
|
|
|
debug "finished sqlite database vacuuming"
|
|
|
|
|
2022-09-20 09:39:52 +00:00
|
|
|
sqliteDatabase.runMigrations(conf)
|
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
|
2022-09-20 09:39:52 +00:00
|
|
|
if conf.persistPeers:
|
|
|
|
let res = WakuPeerStorage.new(sqliteDatabase)
|
|
|
|
if res.isErr():
|
|
|
|
warn "failed to init peer store", err = res.error
|
|
|
|
waku_node_errors.inc(labelValues = ["init_store_failure"])
|
|
|
|
else:
|
|
|
|
storeTuple.pStorage = res.value
|
2021-07-22 09:46:54 +00:00
|
|
|
|
2022-09-20 09:39:52 +00:00
|
|
|
if conf.persistMessages:
|
2022-10-04 13:34:05 +00:00
|
|
|
if conf.sqliteStore:
|
|
|
|
debug "setting up sqlite-only store"
|
2022-09-20 09:39:52 +00:00
|
|
|
let res = SqliteStore.init(sqliteDatabase)
|
|
|
|
if res.isErr():
|
|
|
|
warn "failed to init message store", err = res.error
|
2021-07-22 09:46:54 +00:00
|
|
|
waku_node_errors.inc(labelValues = ["init_store_failure"])
|
|
|
|
else:
|
2022-09-20 09:39:52 +00:00
|
|
|
storeTuple.mStorage = res.value
|
2022-10-04 13:34:05 +00:00
|
|
|
elif not sqliteDatabase.isNil():
|
|
|
|
debug "setting up dual message store"
|
2022-09-20 09:39:52 +00:00
|
|
|
let res = DualMessageStore.init(sqliteDatabase, conf.storeCapacity)
|
2022-09-13 11:36:04 +00:00
|
|
|
if res.isErr():
|
2022-09-20 09:39:52 +00:00
|
|
|
warn "failed to init message store", err = res.error
|
2021-07-22 09:46:54 +00:00
|
|
|
waku_node_errors.inc(labelValues = ["init_store_failure"])
|
|
|
|
else:
|
|
|
|
storeTuple.mStorage = res.value
|
2022-10-04 13:34:05 +00:00
|
|
|
else:
|
|
|
|
debug "setting up in-memory store"
|
|
|
|
storeTuple.mStorage = StoreQueueRef.new(conf.storeCapacity)
|
2022-09-20 09:39:52 +00:00
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
ok(storeTuple)
|
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
# 2/7 Retrieve dynamic bootstrap nodes
|
|
|
|
proc retrieveDynamicBootstrapNodes(conf: WakuNodeConf): SetupResult[seq[RemotePeerInfo]] =
|
2022-06-08 09:20:18 +00:00
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
if conf.dnsDiscovery and conf.dnsDiscoveryUrl != "":
|
2022-06-08 09:20:18 +00:00
|
|
|
# DNS discovery
|
2022-03-17 16:33:17 +00:00
|
|
|
debug "Discovering nodes using Waku DNS discovery", url=conf.dnsDiscoveryUrl
|
|
|
|
|
|
|
|
var nameServers: seq[TransportAddress]
|
|
|
|
for ip in conf.dnsDiscoveryNameServers:
|
|
|
|
nameServers.add(initTAddress(ip, Port(53))) # Assume all servers use port 53
|
|
|
|
|
|
|
|
let dnsResolver = DnsResolver.new(nameServers)
|
|
|
|
|
|
|
|
proc resolver(domain: string): Future[string] {.async, gcsafe.} =
|
|
|
|
trace "resolving", domain=domain
|
|
|
|
let resolved = await dnsResolver.resolveTxt(domain)
|
|
|
|
return resolved[0] # Use only first answer
|
2022-09-20 11:03:34 +00:00
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
var wakuDnsDiscovery = WakuDnsDiscovery.init(conf.dnsDiscoveryUrl,
|
|
|
|
resolver)
|
|
|
|
if wakuDnsDiscovery.isOk:
|
|
|
|
return wakuDnsDiscovery.get().findPeers()
|
|
|
|
.mapErr(proc (e: cstring): string = $e)
|
|
|
|
else:
|
|
|
|
warn "Failed to init Waku DNS discovery"
|
2022-06-08 20:23:13 +00:00
|
|
|
|
|
|
|
debug "No method for retrieving dynamic bootstrap nodes specified."
|
2022-06-08 09:20:18 +00:00
|
|
|
ok(newSeq[RemotePeerInfo]()) # Return an empty seq by default
|
2022-03-17 16:33:17 +00:00
|
|
|
|
|
|
|
# 3/7 Initialize node
|
2021-07-22 09:46:54 +00:00
|
|
|
proc initNode(conf: WakuNodeConf,
|
2022-03-17 16:33:17 +00:00
|
|
|
pStorage: WakuPeerStorage = nil,
|
|
|
|
dynamicBootstrapNodes: openArray[RemotePeerInfo] = @[]): SetupResult[WakuNode] =
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
|
|
## Setup a basic Waku v2 node based on a supplied configuration
|
|
|
|
## file. Optionally include persistent peer storage.
|
|
|
|
## No protocols are mounted yet.
|
2020-09-11 04:16:45 +00:00
|
|
|
|
2022-02-16 16:12:09 +00:00
|
|
|
var dnsResolver: DnsResolver
|
|
|
|
if conf.dnsAddrs:
|
|
|
|
# Support for DNS multiaddrs
|
|
|
|
var nameServers: seq[TransportAddress]
|
|
|
|
for ip in conf.dnsAddrsNameServers:
|
|
|
|
nameServers.add(initTAddress(ip, Port(53))) # Assume all servers use port 53
|
|
|
|
|
|
|
|
dnsResolver = DnsResolver.new(nameServers)
|
|
|
|
|
2021-11-01 18:02:39 +00:00
|
|
|
let
|
|
|
|
## `udpPort` is only supplied to satisfy underlying APIs but is not
|
|
|
|
## actually a supported transport for libp2p traffic.
|
|
|
|
udpPort = conf.tcpPort
|
2021-07-22 09:46:54 +00:00
|
|
|
(extIp, extTcpPort, extUdpPort) = setupNat(conf.nat,
|
|
|
|
clientId,
|
|
|
|
Port(uint16(conf.tcpPort) + conf.portsShift),
|
2021-10-12 11:43:01 +00:00
|
|
|
Port(uint16(udpPort) + conf.portsShift))
|
2022-05-18 08:15:03 +00:00
|
|
|
|
|
|
|
dns4DomainName = if conf.dns4DomainName != "": some(conf.dns4DomainName)
|
|
|
|
else: none(string)
|
2022-06-09 15:46:21 +00:00
|
|
|
|
|
|
|
discv5UdpPort = if conf.discv5Discovery: some(Port(uint16(conf.discv5UdpPort) + conf.portsShift))
|
|
|
|
else: none(Port)
|
2022-05-18 08:15:03 +00:00
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
## @TODO: the NAT setup assumes a manual port mapping configuration if extIp config is set. This probably
|
|
|
|
## implies adding manual config item for extPort as well. The following heuristic assumes that, in absence of manual
|
|
|
|
## config, the external port is the same as the bind port.
|
2022-05-18 08:15:03 +00:00
|
|
|
extPort = if (extIp.isSome() or dns4DomainName.isSome()) and extTcpPort.isNone():
|
2021-07-22 09:46:54 +00:00
|
|
|
some(Port(uint16(conf.tcpPort) + conf.portsShift))
|
|
|
|
else:
|
|
|
|
extTcpPort
|
2021-12-06 19:51:37 +00:00
|
|
|
|
|
|
|
wakuFlags = initWakuFlags(conf.lightpush,
|
|
|
|
conf.filter,
|
|
|
|
conf.store,
|
|
|
|
conf.relay)
|
2021-11-01 18:02:39 +00:00
|
|
|
|
2021-11-10 12:05:36 +00:00
|
|
|
node = WakuNode.new(conf.nodekey,
|
2022-02-16 16:12:09 +00:00
|
|
|
conf.listenAddress, Port(uint16(conf.tcpPort) + conf.portsShift),
|
|
|
|
extIp, extPort,
|
|
|
|
pStorage,
|
|
|
|
conf.maxConnections.int,
|
|
|
|
Port(uint16(conf.websocketPort) + conf.portsShift),
|
|
|
|
conf.websocketSupport,
|
|
|
|
conf.websocketSecureSupport,
|
|
|
|
conf.websocketSecureKeyPath,
|
|
|
|
conf.websocketSecureCertPath,
|
|
|
|
some(wakuFlags),
|
2022-02-18 11:10:38 +00:00
|
|
|
dnsResolver,
|
2022-03-29 08:09:48 +00:00
|
|
|
conf.relayPeerExchange, # We send our own signed peer record when peer exchange enabled
|
2022-06-09 15:46:21 +00:00
|
|
|
dns4DomainName,
|
|
|
|
discv5UdpPort
|
2022-02-16 16:12:09 +00:00
|
|
|
)
|
2021-11-01 18:02:39 +00:00
|
|
|
|
|
|
|
if conf.discv5Discovery:
|
2022-03-01 14:11:56 +00:00
|
|
|
let
|
|
|
|
discoveryConfig = DiscoveryConfig.init(
|
|
|
|
conf.discv5TableIpLimit, conf.discv5BucketIpLimit, conf.discv5BitsPerHop)
|
2021-11-01 18:02:39 +00:00
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
# select dynamic bootstrap nodes that have an ENR containing a udp port.
|
|
|
|
# Discv5 only supports UDP https://github.com/ethereum/devp2p/blob/master/discv5/discv5-theory.md)
|
|
|
|
var discv5BootstrapEnrs: seq[enr.Record]
|
|
|
|
for n in dynamicBootstrapNodes:
|
|
|
|
if n.enr.isSome():
|
|
|
|
let
|
|
|
|
enr = n.enr.get()
|
|
|
|
tenrRes = enr.toTypedRecord()
|
|
|
|
if tenrRes.isOk() and (tenrRes.get().udp.isSome() or tenrRes.get().udp6.isSome()):
|
|
|
|
discv5BootstrapEnrs.add(enr)
|
|
|
|
|
|
|
|
# parse enrURIs from the configuration and add the resulting ENRs to the discv5BootstrapEnrs seq
|
|
|
|
for enrUri in conf.discv5BootstrapNodes:
|
|
|
|
addBootstrapNode(enrUri, discv5BootstrapEnrs)
|
|
|
|
|
2021-11-01 18:02:39 +00:00
|
|
|
node.wakuDiscv5 = WakuDiscoveryV5.new(
|
2022-06-09 15:46:21 +00:00
|
|
|
extIP, extPort, discv5UdpPort,
|
2021-11-01 18:02:39 +00:00
|
|
|
conf.listenAddress,
|
2022-06-09 15:46:21 +00:00
|
|
|
discv5UdpPort.get(),
|
2022-03-17 16:33:17 +00:00
|
|
|
discv5BootstrapEnrs,
|
2021-11-01 18:02:39 +00:00
|
|
|
conf.discv5EnrAutoUpdate,
|
|
|
|
keys.PrivateKey(conf.nodekey.skkey),
|
2021-12-06 19:51:37 +00:00
|
|
|
wakuFlags,
|
2021-11-01 18:02:39 +00:00
|
|
|
[], # Empty enr fields, for now
|
2022-03-01 14:11:56 +00:00
|
|
|
node.rng,
|
|
|
|
discoveryConfig
|
2021-11-01 18:02:39 +00:00
|
|
|
)
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
|
|
ok(node)
|
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
# 4/7 Mount and initialize configured protocols
|
2022-09-20 09:39:52 +00:00
|
|
|
proc setupProtocols(node: WakuNode, conf: WakuNodeConf, mStorage: MessageStore): SetupResult[bool] =
|
2021-07-22 09:46:54 +00:00
|
|
|
## Setup configured protocols on an existing Waku v2 node.
|
|
|
|
## Optionally include persistent message storage.
|
|
|
|
## No protocols are started yet.
|
2020-12-24 08:02:30 +00:00
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
# Mount relay on all nodes
|
2022-03-29 08:09:48 +00:00
|
|
|
var peerExchangeHandler = none(RoutingRecordsHandler)
|
|
|
|
if conf.relayPeerExchange:
|
|
|
|
proc handlePeerExchange(peer: PeerId, topic: string,
|
|
|
|
peers: seq[RoutingRecordsPair]) {.gcsafe, raises: [Defect].} =
|
|
|
|
## Handle peers received via gossipsub peer exchange
|
|
|
|
# TODO: Only consider peers on pubsub topics we subscribe to
|
|
|
|
let exchangedPeers = peers.filterIt(it.record.isSome()) # only peers with populated records
|
|
|
|
.mapIt(toRemotePeerInfo(it.record.get()))
|
|
|
|
|
|
|
|
debug "connecting to exchanged peers", src=peer, topic=topic, numPeers=exchangedPeers.len
|
|
|
|
|
|
|
|
# asyncSpawn, as we don't want to block here
|
|
|
|
asyncSpawn node.connectToNodes(exchangedPeers, "peer exchange")
|
|
|
|
|
|
|
|
peerExchangeHandler = some(handlePeerExchange)
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
if conf.relay:
|
|
|
|
waitFor mountRelay(node,
|
|
|
|
conf.topics.split(" "),
|
|
|
|
peerExchangeHandler = peerExchangeHandler)
|
2020-12-24 08:02:30 +00:00
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
# Keepalive mounted on all nodes
|
2022-09-07 15:31:27 +00:00
|
|
|
waitFor mountLibp2pPing(node)
|
2020-12-24 08:02:30 +00:00
|
|
|
|
2022-10-04 20:20:44 +00:00
|
|
|
when defined(rln) or defined(rlnzerokit):
|
2021-09-24 17:32:45 +00:00
|
|
|
if conf.rlnRelay:
|
2022-08-30 17:59:02 +00:00
|
|
|
let res = node.mountRlnRelay(conf)
|
|
|
|
if res.isErr():
|
|
|
|
debug "could not mount WakuRlnRelay"
|
2022-06-27 19:35:26 +00:00
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
if conf.swap:
|
2022-09-07 15:31:27 +00:00
|
|
|
waitFor mountSwap(node)
|
2021-07-22 09:46:54 +00:00
|
|
|
# TODO Set swap peer, for now should be same as store peer
|
2020-09-11 04:16:45 +00:00
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
# Store setup
|
|
|
|
if (conf.storenode != "") or (conf.store):
|
2022-09-20 09:39:52 +00:00
|
|
|
let retentionPolicy = if conf.sqliteStore: TimeRetentionPolicy.init(conf.sqliteRetentionTime)
|
|
|
|
else: CapacityRetentionPolicy.init(conf.storeCapacity)
|
|
|
|
waitFor mountStore(node, mStorage, retentionPolicy=some(retentionPolicy))
|
2021-03-26 08:49:51 +00:00
|
|
|
|
2022-09-21 09:32:59 +00:00
|
|
|
executeMessageRetentionPolicy(node)
|
|
|
|
startMessageRetentionPolicyPeriodicTask(node, interval=MessageStoreDefaultRetentionPolicyInterval)
|
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
if conf.storenode != "":
|
|
|
|
setStorePeer(node, conf.storenode)
|
2021-03-26 08:49:51 +00:00
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
# NOTE Must be mounted after relay
|
|
|
|
if (conf.lightpushnode != "") or (conf.lightpush):
|
2022-09-07 15:31:27 +00:00
|
|
|
waitFor mountLightPush(node)
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
|
|
if conf.lightpushnode != "":
|
|
|
|
setLightPushPeer(node, conf.lightpushnode)
|
|
|
|
|
|
|
|
# Filter setup. NOTE Must be mounted after relay
|
|
|
|
if (conf.filternode != "") or (conf.filter):
|
2022-09-07 15:31:27 +00:00
|
|
|
waitFor mountFilter(node, filterTimeout = chronos.seconds(conf.filterTimeout))
|
2021-06-25 21:06:56 +00:00
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
if conf.filternode != "":
|
|
|
|
setFilterPeer(node, conf.filternode)
|
|
|
|
|
2022-09-20 11:03:34 +00:00
|
|
|
# waku peer exchange setup
|
|
|
|
if (conf.peerExchangeNode != "") or (conf.peerExchange):
|
|
|
|
waitFor mountWakuPeerExchange(node)
|
|
|
|
|
|
|
|
if conf.peerExchangeNode != "":
|
|
|
|
setPeerExchangePeer(node, conf.peerExchangeNode)
|
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
ok(true) # Success
|
2021-03-26 08:49:51 +00:00
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
# 5/7 Start node and mounted protocols
|
|
|
|
proc startNode(node: WakuNode, conf: WakuNodeConf,
|
|
|
|
dynamicBootstrapNodes: seq[RemotePeerInfo] = @[]): SetupResult[bool] =
|
2021-07-22 09:46:54 +00:00
|
|
|
## Start a configured node and all mounted protocols.
|
|
|
|
## Resume history, connect to static nodes and start
|
|
|
|
## keep-alive, if configured.
|
2022-03-01 14:11:56 +00:00
|
|
|
|
|
|
|
# Start Waku v2 node
|
2021-07-22 09:46:54 +00:00
|
|
|
waitFor node.start()
|
2022-03-01 14:11:56 +00:00
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
# Start discv5 and connect to discovered nodes
|
2022-03-01 14:11:56 +00:00
|
|
|
if conf.discv5Discovery:
|
|
|
|
if not waitFor node.startDiscv5():
|
|
|
|
error "could not start Discovery v5"
|
2021-11-15 13:29:18 +00:00
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
# Resume historical messages, this has to be called after the node has been started
|
|
|
|
if conf.store and conf.persistMessages:
|
|
|
|
waitFor node.resume()
|
|
|
|
|
|
|
|
# Connect to configured static nodes
|
|
|
|
if conf.staticnodes.len > 0:
|
2022-01-26 11:02:57 +00:00
|
|
|
waitFor connectToNodes(node, conf.staticnodes, "static")
|
2021-08-12 08:51:38 +00:00
|
|
|
|
2022-08-24 15:44:43 +00:00
|
|
|
if dynamicBootstrapNodes.len > 0:
|
|
|
|
info "Connecting to dynamic bootstrap peers"
|
|
|
|
waitFor connectToNodes(node, dynamicBootstrapNodes, "dynamic bootstrap")
|
2022-09-20 11:03:34 +00:00
|
|
|
|
|
|
|
# retrieve and connect to peer exchange peers
|
|
|
|
if conf.peerExchangeNode != "":
|
|
|
|
info "Retrieving peer info via peer exchange protocol"
|
|
|
|
let desiredOutDegree = node.wakuRelay.parameters.d.uint64()
|
|
|
|
discard waitFor node.wakuPeerExchange.request(desiredOutDegree)
|
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
# Start keepalive, if enabled
|
|
|
|
if conf.keepAlive:
|
|
|
|
node.startKeepalive()
|
|
|
|
|
|
|
|
ok(true) # Success
|
2020-11-24 04:53:42 +00:00
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
# 6/7 Start monitoring tools and external interfaces
|
2021-07-22 09:46:54 +00:00
|
|
|
proc startExternal(node: WakuNode, conf: WakuNodeConf): SetupResult[bool] =
|
|
|
|
## Start configured external interfaces and monitoring tools
|
2022-06-28 10:22:59 +00:00
|
|
|
## on a Waku v2 node, including the RPC API, REST API and metrics
|
2021-07-22 09:46:54 +00:00
|
|
|
## monitoring ports.
|
2021-05-13 21:21:46 +00:00
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
if conf.rpc:
|
2022-07-17 14:25:21 +00:00
|
|
|
startRpcServer(node, conf.rpcAddress, Port(conf.rpcPort + conf.portsShift), conf)
|
2022-06-28 10:22:59 +00:00
|
|
|
|
|
|
|
if conf.rest:
|
|
|
|
startRestServer(node, conf.restAddress, Port(conf.restPort + conf.portsShift), conf)
|
2021-05-13 21:21:46 +00:00
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
if conf.metricsLogging:
|
|
|
|
startMetricsLog()
|
|
|
|
|
|
|
|
if conf.metricsServer:
|
|
|
|
startMetricsServer(conf.metricsServerAddress,
|
|
|
|
Port(conf.metricsServerPort + conf.portsShift))
|
|
|
|
|
|
|
|
ok(true) # Success
|
2022-05-17 15:48:08 +00:00
|
|
|
|
|
|
|
{.push warning[ProveInit]: off.}
|
|
|
|
let conf = try:
|
|
|
|
WakuNodeConf.load(
|
|
|
|
secondarySources = proc (conf: WakuNodeConf, sources: auto) =
|
|
|
|
if conf.configFile.isSome:
|
|
|
|
sources.addConfigFile(Toml, conf.configFile.get)
|
|
|
|
)
|
|
|
|
except CatchableError as err:
|
|
|
|
error "Failure while loading the configuration: \n", err_msg=err.msg
|
|
|
|
quit 1 # if we don't leave here, the initialization of conf does not work in the success case
|
|
|
|
{.pop.}
|
|
|
|
|
2022-05-17 20:11:07 +00:00
|
|
|
# if called with --version, print the version and quit
|
|
|
|
if conf.version:
|
|
|
|
echo "version / git commit hash: ", git_version
|
|
|
|
quit(QuitSuccess)
|
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
var
|
|
|
|
node: WakuNode # This is the node we're going to setup using the conf
|
2021-05-04 12:11:41 +00:00
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
##############
|
|
|
|
# Node setup #
|
|
|
|
##############
|
2022-03-01 14:11:56 +00:00
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
debug "1/7 Setting up storage"
|
2022-03-01 14:11:56 +00:00
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
var
|
|
|
|
pStorage: WakuPeerStorage
|
2022-09-20 09:39:52 +00:00
|
|
|
mStorage: MessageStore
|
2021-04-29 04:54:31 +00:00
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
let setupStorageRes = setupStorage(conf)
|
|
|
|
|
|
|
|
if setupStorageRes.isErr:
|
2022-03-17 16:33:17 +00:00
|
|
|
error "1/7 Setting up storage failed. Continuing without storage."
|
2021-07-22 09:46:54 +00:00
|
|
|
else:
|
|
|
|
(pStorage, mStorage) = setupStorageRes.get()
|
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
debug "2/7 Retrieve dynamic bootstrap nodes"
|
|
|
|
|
|
|
|
var dynamicBootstrapNodes: seq[RemotePeerInfo]
|
|
|
|
let dynamicBootstrapNodesRes = retrieveDynamicBootstrapNodes(conf)
|
|
|
|
if dynamicBootstrapNodesRes.isErr:
|
2022-06-08 20:23:13 +00:00
|
|
|
warn "2/7 Retrieving dynamic bootstrap nodes failed. Continuing without dynamic bootstrap nodes."
|
2022-03-17 16:33:17 +00:00
|
|
|
else:
|
|
|
|
dynamicBootstrapNodes = dynamicBootstrapNodesRes.get()
|
|
|
|
|
|
|
|
debug "3/7 Initializing node"
|
2021-07-22 09:46:54 +00:00
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
let initNodeRes = initNode(conf, pStorage, dynamicBootstrapNodes)
|
2021-04-29 04:54:31 +00:00
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
if initNodeRes.isErr:
|
2022-03-17 16:33:17 +00:00
|
|
|
error "3/7 Initializing node failed. Quitting."
|
2021-07-22 09:46:54 +00:00
|
|
|
quit(QuitFailure)
|
|
|
|
else:
|
|
|
|
node = initNodeRes.get()
|
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
debug "4/7 Mounting protocols"
|
2021-04-24 04:56:37 +00:00
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
let setupProtocolsRes = setupProtocols(node, conf, mStorage)
|
2020-09-01 02:09:54 +00:00
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
if setupProtocolsRes.isErr:
|
2022-03-17 16:33:17 +00:00
|
|
|
error "4/7 Mounting protocols failed. Continuing in current state."
|
2020-09-01 02:09:54 +00:00
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
debug "5/7 Starting node and mounted protocols"
|
2021-04-15 08:18:14 +00:00
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
let startNodeRes = startNode(node, conf, dynamicBootstrapNodes)
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
|
|
if startNodeRes.isErr:
|
2022-03-17 16:33:17 +00:00
|
|
|
error "5/7 Starting node and mounted protocols failed. Continuing in current state."
|
2021-07-22 09:46:54 +00:00
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
debug "6/7 Starting monitoring and external interfaces"
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
|
|
let startExternalRes = startExternal(node, conf)
|
|
|
|
|
|
|
|
if startExternalRes.isErr:
|
2022-03-17 16:33:17 +00:00
|
|
|
error "6/7 Starting monitoring and external interfaces failed. Continuing in current state."
|
2021-07-22 09:46:54 +00:00
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
debug "7/7 Setting up shutdown hooks"
|
2021-07-22 09:46:54 +00:00
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
# 7/7 Setup graceful shutdown hooks
|
2021-07-22 09:46:54 +00:00
|
|
|
## Setup shutdown hooks for this process.
|
|
|
|
## Stop node gracefully on shutdown.
|
2021-04-15 08:18:14 +00:00
|
|
|
|
|
|
|
# Handle Ctrl-C SIGINT
|
|
|
|
proc handleCtrlC() {.noconv.} =
|
|
|
|
when defined(windows):
|
|
|
|
# workaround for https://github.com/nim-lang/Nim/issues/4057
|
|
|
|
setupForeignThreadGc()
|
|
|
|
info "Shutting down after receiving SIGINT"
|
|
|
|
waitFor node.stop()
|
|
|
|
quit(QuitSuccess)
|
|
|
|
|
|
|
|
setControlCHook(handleCtrlC)
|
|
|
|
|
|
|
|
# Handle SIGTERM
|
|
|
|
when defined(posix):
|
|
|
|
proc handleSigterm(signal: cint) {.noconv.} =
|
|
|
|
info "Shutting down after receiving SIGTERM"
|
|
|
|
waitFor node.stop()
|
|
|
|
quit(QuitSuccess)
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
c_signal(ansi_c.SIGTERM, handleSigterm)
|
2021-06-02 07:53:34 +00:00
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
debug "Node setup complete"
|
2020-09-01 02:09:54 +00:00
|
|
|
|
2021-05-28 18:41:29 +00:00
|
|
|
runForever()
|