2022-11-04 09:52:27 +00:00
|
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
else:
|
|
|
|
|
{.push raises: [].}
|
2021-07-14 17:58:46 +00:00
|
|
|
|
|
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
|
|
|
|
stew/shims/net as stewNet,
|
2022-11-23 09:08:00 +00:00
|
|
|
|
chronicles,
|
2022-10-18 17:35:26 +00:00
|
|
|
|
chronos,
|
|
|
|
|
metrics,
|
2022-11-30 18:41:19 +00:00
|
|
|
|
libbacktrace,
|
2022-10-18 17:35:26 +00:00
|
|
|
|
system/ansi_c,
|
2020-09-01 02:09:54 +00:00
|
|
|
|
eth/keys,
|
2021-08-12 08:51:38 +00:00
|
|
|
|
eth/p2p/discoveryv5/enr,
|
2022-11-04 09:52:08 +00:00
|
|
|
|
libp2p/builders,
|
|
|
|
|
libp2p/multihash,
|
2020-05-15 04:11:14 +00:00
|
|
|
|
libp2p/crypto/crypto,
|
2021-06-15 08:55:47 +00:00
|
|
|
|
libp2p/protocols/ping,
|
2022-11-04 09:52:08 +00:00
|
|
|
|
libp2p/protocols/pubsub/gossipsub,
|
|
|
|
|
libp2p/protocols/pubsub/rpc/messages,
|
|
|
|
|
libp2p/transports/wstransport,
|
2022-10-18 17:35:26 +00:00
|
|
|
|
libp2p/nameresolving/dnsresolver
|
2022-07-25 11:01:37 +00:00
|
|
|
|
import
|
2022-10-28 18:13:41 +00:00
|
|
|
|
../../waku/common/sqlite,
|
|
|
|
|
../../waku/common/utils/nat,
|
2022-10-18 17:35:26 +00:00
|
|
|
|
../../waku/v2/node/peer_manager/peer_manager,
|
2022-11-04 08:40:13 +00:00
|
|
|
|
../../waku/v2/node/peer_manager/peer_store/waku_peer_storage,
|
|
|
|
|
../../waku/v2/node/peer_manager/peer_store/migrations as peer_store_sqlite_migrations,
|
2022-10-18 17:35:26 +00:00
|
|
|
|
../../waku/v2/node/dnsdisc/waku_dnsdisc,
|
|
|
|
|
../../waku/v2/node/discv5/waku_discv5,
|
2022-11-23 09:08:00 +00:00
|
|
|
|
../../waku/v2/node/wakuswitch,
|
2022-10-27 22:05:02 +00:00
|
|
|
|
../../waku/v2/node/waku_node,
|
|
|
|
|
../../waku/v2/node/waku_metrics,
|
2022-11-23 09:08:00 +00:00
|
|
|
|
../../waku/v2/protocol/waku_archive,
|
|
|
|
|
../../waku/v2/protocol/waku_archive/driver/queue_driver,
|
|
|
|
|
../../waku/v2/protocol/waku_archive/driver/sqlite_driver,
|
|
|
|
|
../../waku/v2/protocol/waku_archive/driver/sqlite_driver/migrations as archive_driver_sqlite_migrations,
|
|
|
|
|
../../waku/v2/protocol/waku_archive/retention_policy,
|
|
|
|
|
../../waku/v2/protocol/waku_archive/retention_policy/retention_policy_capacity,
|
|
|
|
|
../../waku/v2/protocol/waku_archive/retention_policy/retention_policy_time,
|
2022-11-04 09:52:08 +00:00
|
|
|
|
../../waku/v2/protocol/waku_peer_exchange,
|
2022-10-27 22:05:02 +00:00
|
|
|
|
../../waku/v2/utils/peers,
|
|
|
|
|
../../waku/v2/utils/wakuenr,
|
2022-10-18 17:35:26 +00:00
|
|
|
|
./wakunode2_setup_rest,
|
|
|
|
|
./wakunode2_setup_rpc,
|
|
|
|
|
./config
|
|
|
|
|
|
2022-11-09 18:45:04 +00:00
|
|
|
|
when defined(rln):
|
2022-10-18 17:35:26 +00:00
|
|
|
|
import
|
2022-11-21 14:15:03 +00:00
|
|
|
|
../../waku/v2/protocol/waku_rln_relay
|
2022-10-18 17:35:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logScope:
|
2022-10-27 22:05:02 +00:00
|
|
|
|
topics = "wakunode"
|
2020-10-21 09:54:29 +00:00
|
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
type SetupResult[T] = Result[T, string]
|
2022-10-18 17:35:26 +00:00
|
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
proc setupDatabaseConnection(dbUrl: string): SetupResult[Option[SqliteDatabase]] =
|
|
|
|
|
## dbUrl mimics SQLAlchemy Database URL schema
|
|
|
|
|
## See: https://docs.sqlalchemy.org/en/14/core/engines.html#database-urls
|
2022-11-23 09:08:00 +00:00
|
|
|
|
if dbUrl == "" or dbUrl == "none":
|
2022-10-27 22:05:02 +00:00
|
|
|
|
return ok(none(SqliteDatabase))
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
let dbUrlParts = dbUrl.split("://", 1)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
let
|
2022-10-27 22:05:02 +00:00
|
|
|
|
engine = dbUrlParts[0]
|
|
|
|
|
path = dbUrlParts[1]
|
|
|
|
|
|
|
|
|
|
let connRes = case engine
|
|
|
|
|
of "sqlite":
|
|
|
|
|
# SQLite engine
|
|
|
|
|
# See: https://docs.sqlalchemy.org/en/14/core/engines.html#sqlite
|
|
|
|
|
SqliteDatabase.new(path)
|
2022-10-21 13:01:01 +00:00
|
|
|
|
|
|
|
|
|
else:
|
2022-10-27 22:05:02 +00:00
|
|
|
|
return err("unknown database engine")
|
2022-10-21 13:01:01 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
if connRes.isErr():
|
|
|
|
|
return err("failed to init database connection: " & connRes.error)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
ok(some(connRes.value))
|
2022-10-21 13:01:01 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
proc gatherSqlitePageStats(db: SqliteDatabase): SetupResult[(int64, int64, int64)] =
|
2022-11-23 09:08:00 +00:00
|
|
|
|
let
|
2022-10-27 22:05:02 +00:00
|
|
|
|
pageSize = ?db.getPageSize()
|
|
|
|
|
pageCount = ?db.getPageCount()
|
|
|
|
|
freelistCount = ?db.getFreelistCount()
|
2022-09-20 09:39:52 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
ok((pageSize, pageCount, freelistCount))
|
2022-09-14 16:09:08 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
proc performSqliteVacuum(db: SqliteDatabase): SetupResult[void] =
|
|
|
|
|
## SQLite database vacuuming
|
|
|
|
|
# TODO: Run vacuuming conditionally based on database page stats
|
|
|
|
|
# if (pageCount > 0 and freelistCount > 0):
|
2022-09-14 16:09:08 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
debug "starting sqlite database vacuuming"
|
2022-09-14 16:09:08 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
let resVacuum = db.vacuum()
|
|
|
|
|
if resVacuum.isErr():
|
|
|
|
|
return err("failed to execute vacuum: " & resVacuum.error)
|
2022-09-14 16:09:08 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
debug "finished sqlite database vacuuming"
|
2022-09-14 16:09:08 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
|
|
|
|
|
const PeerPersistenceDbUrl = "sqlite://peers.db"
|
|
|
|
|
|
|
|
|
|
proc setupPeerStorage(): SetupResult[Option[WakuPeerStorage]] =
|
|
|
|
|
let db = ?setupDatabaseConnection(PeerPersistenceDbUrl)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-11-04 08:40:13 +00:00
|
|
|
|
?peer_store_sqlite_migrations.migrate(db.get())
|
2022-09-20 09:39:52 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
let res = WakuPeerStorage.new(db.get())
|
|
|
|
|
if res.isErr():
|
|
|
|
|
return err("failed to init peer store" & res.error)
|
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
ok(some(res.value))
|
2022-10-27 22:05:02 +00:00
|
|
|
|
|
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
proc setupWakuArchiveRetentionPolicy(retentionPolicy: string): SetupResult[Option[RetentionPolicy]] =
|
|
|
|
|
if retentionPolicy == "" or retentionPolicy == "none":
|
|
|
|
|
return ok(none(RetentionPolicy))
|
2022-09-20 09:39:52 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
let rententionPolicyParts = retentionPolicy.split(":", 1)
|
|
|
|
|
let
|
|
|
|
|
policy = rententionPolicyParts[0]
|
|
|
|
|
policyArgs = rententionPolicyParts[1]
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
if policy == "time":
|
|
|
|
|
var retentionTimeSeconds: int64
|
2022-11-23 09:08:00 +00:00
|
|
|
|
try:
|
2022-10-27 22:05:02 +00:00
|
|
|
|
retentionTimeSeconds = parseInt(policyArgs)
|
|
|
|
|
except ValueError:
|
|
|
|
|
return err("invalid time retention policy argument")
|
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
let retPolicy: RetentionPolicy = TimeRetentionPolicy.init(retentionTimeSeconds)
|
2022-10-27 22:05:02 +00:00
|
|
|
|
return ok(some(retPolicy))
|
|
|
|
|
|
|
|
|
|
elif policy == "capacity":
|
|
|
|
|
var retentionCapacity: int
|
2022-11-23 09:08:00 +00:00
|
|
|
|
try:
|
2022-10-27 22:05:02 +00:00
|
|
|
|
retentionCapacity = parseInt(policyArgs)
|
|
|
|
|
except ValueError:
|
|
|
|
|
return err("invalid capacity retention policy argument")
|
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
let retPolicy: RetentionPolicy = CapacityRetentionPolicy.init(retentionCapacity)
|
2022-10-27 22:05:02 +00:00
|
|
|
|
return ok(some(retPolicy))
|
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
else:
|
2022-10-27 22:05:02 +00:00
|
|
|
|
return err("unknown retention policy")
|
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
proc setupWakuArchiveDriver(dbUrl: string, vacuum: bool, migrate: bool): SetupResult[ArchiveDriver] =
|
2022-10-27 22:05:02 +00:00
|
|
|
|
let db = ?setupDatabaseConnection(dbUrl)
|
|
|
|
|
|
|
|
|
|
if db.isSome():
|
|
|
|
|
# SQLite vacuum
|
|
|
|
|
# TODO: Run this only if the database engine is SQLite
|
|
|
|
|
let (pageSize, pageCount, freelistCount) = ?gatherSqlitePageStats(db.get())
|
|
|
|
|
debug "sqlite database page stats", pageSize=pageSize, pages=pageCount, freePages=freelistCount
|
|
|
|
|
|
|
|
|
|
if vacuum and (pageCount > 0 and freelistCount > 0):
|
|
|
|
|
?performSqliteVacuum(db.get())
|
|
|
|
|
|
|
|
|
|
# Database migration
|
|
|
|
|
if migrate:
|
2022-11-23 09:08:00 +00:00
|
|
|
|
?archive_driver_sqlite_migrations.migrate(db.get())
|
|
|
|
|
|
|
|
|
|
if db.isSome():
|
|
|
|
|
debug "setting up sqlite waku archive driver"
|
|
|
|
|
let res = SqliteDriver.new(db.get())
|
|
|
|
|
if res.isErr():
|
|
|
|
|
return err("failed to init sqlite archive driver: " & res.error)
|
2022-10-27 22:05:02 +00:00
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
ok(res.value)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
debug "setting up in-memory waku archive driver"
|
|
|
|
|
let driver = QueueDriver.new() # Defaults to a capacity of 25.000 messages
|
|
|
|
|
ok(driver)
|
2022-10-27 22:05:02 +00:00
|
|
|
|
|
|
|
|
|
|
2022-12-05 19:02:21 +00:00
|
|
|
|
proc retrieveDynamicBootstrapNodes*(dnsDiscovery: bool, dnsDiscoveryUrl: string, dnsDiscoveryNameServers: seq[ValidIpAddress]): SetupResult[seq[RemotePeerInfo]] =
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
if dnsDiscovery and dnsDiscoveryUrl != "":
|
2022-10-21 13:01:01 +00:00
|
|
|
|
# DNS discovery
|
2022-10-27 22:05:02 +00:00
|
|
|
|
debug "Discovering nodes using Waku DNS discovery", url=dnsDiscoveryUrl
|
2022-10-21 13:01:01 +00:00
|
|
|
|
|
|
|
|
|
var nameServers: seq[TransportAddress]
|
2022-10-27 22:05:02 +00:00
|
|
|
|
for ip in dnsDiscoveryNameServers:
|
2022-10-21 13:01:01 +00:00
|
|
|
|
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-10-27 22:05:02 +00:00
|
|
|
|
var wakuDnsDiscovery = WakuDnsDiscovery.init(dnsDiscoveryUrl, resolver)
|
|
|
|
|
if wakuDnsDiscovery.isOk():
|
2022-10-21 13:01:01 +00:00
|
|
|
|
return wakuDnsDiscovery.get().findPeers()
|
|
|
|
|
.mapErr(proc (e: cstring): string = $e)
|
|
|
|
|
else:
|
|
|
|
|
warn "Failed to init Waku DNS discovery"
|
|
|
|
|
|
|
|
|
|
debug "No method for retrieving dynamic bootstrap nodes specified."
|
|
|
|
|
ok(newSeq[RemotePeerInfo]()) # Return an empty seq by default
|
|
|
|
|
|
|
|
|
|
proc initNode(conf: WakuNodeConf,
|
2022-10-27 22:05:02 +00:00
|
|
|
|
peerStore: Option[WakuPeerStorage],
|
2022-10-21 13:01:01 +00:00
|
|
|
|
dynamicBootstrapNodes: openArray[RemotePeerInfo] = @[]): SetupResult[WakuNode] =
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
## Setup a basic Waku v2 node based on a supplied configuration
|
|
|
|
|
## file. Optionally include persistent peer storage.
|
|
|
|
|
## No protocols are mounted yet.
|
|
|
|
|
|
|
|
|
|
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
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
dnsResolver = DnsResolver.new(nameServers)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
|
|
|
|
let
|
2022-10-21 13:01:01 +00:00
|
|
|
|
## `udpPort` is only supplied to satisfy underlying APIs but is not
|
|
|
|
|
## actually a supported transport for libp2p traffic.
|
|
|
|
|
udpPort = conf.tcpPort
|
|
|
|
|
(extIp, extTcpPort, extUdpPort) = setupNat(conf.nat,
|
|
|
|
|
clientId,
|
|
|
|
|
Port(uint16(conf.tcpPort) + conf.portsShift),
|
|
|
|
|
Port(uint16(udpPort) + conf.portsShift))
|
|
|
|
|
|
|
|
|
|
dns4DomainName = if conf.dns4DomainName != "": some(conf.dns4DomainName)
|
|
|
|
|
else: none(string)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
discv5UdpPort = if conf.discv5Discovery: some(Port(uint16(conf.discv5UdpPort) + conf.portsShift))
|
|
|
|
|
else: none(Port)
|
|
|
|
|
|
|
|
|
|
## @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.
|
|
|
|
|
extPort = if (extIp.isSome() or dns4DomainName.isSome()) and extTcpPort.isNone():
|
|
|
|
|
some(Port(uint16(conf.tcpPort) + conf.portsShift))
|
|
|
|
|
else:
|
|
|
|
|
extTcpPort
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
wakuFlags = initWakuFlags(conf.lightpush,
|
|
|
|
|
conf.filter,
|
|
|
|
|
conf.store,
|
|
|
|
|
conf.relay)
|
|
|
|
|
|
|
|
|
|
var node: WakuNode
|
2022-10-27 22:05:02 +00:00
|
|
|
|
|
|
|
|
|
let pStorage = if peerStore.isNone(): nil
|
|
|
|
|
else: peerStore.get()
|
2022-10-21 13:01:01 +00:00
|
|
|
|
try:
|
|
|
|
|
node = WakuNode.new(conf.nodekey,
|
2022-11-23 09:08:00 +00:00
|
|
|
|
conf.listenAddress, Port(uint16(conf.tcpPort) + conf.portsShift),
|
2022-10-21 13:01:01 +00:00
|
|
|
|
extIp, extPort,
|
|
|
|
|
pStorage,
|
|
|
|
|
conf.maxConnections.int,
|
|
|
|
|
Port(uint16(conf.websocketPort) + conf.portsShift),
|
|
|
|
|
conf.websocketSupport,
|
|
|
|
|
conf.websocketSecureSupport,
|
|
|
|
|
conf.websocketSecureKeyPath,
|
|
|
|
|
conf.websocketSecureCertPath,
|
|
|
|
|
some(wakuFlags),
|
|
|
|
|
dnsResolver,
|
|
|
|
|
conf.relayPeerExchange, # We send our own signed peer record when peer exchange enabled
|
|
|
|
|
dns4DomainName,
|
2022-10-28 13:12:06 +00:00
|
|
|
|
discv5UdpPort,
|
2022-11-24 13:11:23 +00:00
|
|
|
|
some(conf.agentString),
|
|
|
|
|
some(conf.peerStoreCapacity),
|
2022-10-21 13:01:01 +00:00
|
|
|
|
)
|
|
|
|
|
except:
|
|
|
|
|
return err("failed to create waku node instance: " & getCurrentExceptionMsg())
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
if conf.discv5Discovery:
|
|
|
|
|
let
|
|
|
|
|
discoveryConfig = DiscoveryConfig.init(
|
|
|
|
|
conf.discv5TableIpLimit, conf.discv5BucketIpLimit, conf.discv5BitsPerHop)
|
|
|
|
|
|
|
|
|
|
# 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)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
# parse enrURIs from the configuration and add the resulting ENRs to the discv5BootstrapEnrs seq
|
|
|
|
|
for enrUri in conf.discv5BootstrapNodes:
|
|
|
|
|
addBootstrapNode(enrUri, discv5BootstrapEnrs)
|
|
|
|
|
|
|
|
|
|
node.wakuDiscv5 = WakuDiscoveryV5.new(
|
|
|
|
|
extIP, extPort, discv5UdpPort,
|
|
|
|
|
conf.listenAddress,
|
|
|
|
|
discv5UdpPort.get(),
|
|
|
|
|
discv5BootstrapEnrs,
|
|
|
|
|
conf.discv5EnrAutoUpdate,
|
|
|
|
|
keys.PrivateKey(conf.nodekey.skkey),
|
|
|
|
|
wakuFlags,
|
|
|
|
|
[], # Empty enr fields, for now
|
|
|
|
|
node.rng,
|
|
|
|
|
discoveryConfig
|
|
|
|
|
)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
ok(node)
|
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
proc setupProtocols(node: WakuNode, conf: WakuNodeConf,
|
|
|
|
|
archiveDriver: Option[ArchiveDriver],
|
|
|
|
|
archiveRetentionPolicy: Option[RetentionPolicy]): Future[SetupResult[void]] {.async.} =
|
2022-10-21 13:01:01 +00:00
|
|
|
|
## Setup configured protocols on an existing Waku v2 node.
|
|
|
|
|
## Optionally include persistent message storage.
|
|
|
|
|
## No protocols are started yet.
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
# Mount relay on all nodes
|
|
|
|
|
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()))
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
debug "connecting to exchanged peers", src=peer, topic=topic, numPeers=exchangedPeers.len
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
# asyncSpawn, as we don't want to block here
|
|
|
|
|
asyncSpawn node.connectToNodes(exchangedPeers, "peer exchange")
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
peerExchangeHandler = some(handlePeerExchange)
|
2022-03-29 08:09:48 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
if conf.relay:
|
|
|
|
|
try:
|
|
|
|
|
let pubsubTopics = conf.topics.split(" ")
|
2022-11-23 09:08:00 +00:00
|
|
|
|
await mountRelay(node, pubsubTopics, peerExchangeHandler = peerExchangeHandler)
|
2022-10-21 13:01:01 +00:00
|
|
|
|
except:
|
|
|
|
|
return err("failed to mount waku relay protocol: " & getCurrentExceptionMsg())
|
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
# Keepalive mounted on all nodes
|
|
|
|
|
try:
|
|
|
|
|
await mountLibp2pPing(node)
|
|
|
|
|
except:
|
|
|
|
|
return err("failed to mount libp2p ping protocol: " & getCurrentExceptionMsg())
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
|
|
|
|
when defined(rln):
|
2022-10-21 13:01:01 +00:00
|
|
|
|
if conf.rlnRelay:
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-11-08 11:53:47 +00:00
|
|
|
|
let rlnConf = WakuRlnConfig(
|
|
|
|
|
rlnRelayDynamic: conf.rlnRelayDynamic,
|
|
|
|
|
rlnRelayPubsubTopic: conf.rlnRelayPubsubTopic,
|
|
|
|
|
rlnRelayContentTopic: conf.rlnRelayContentTopic,
|
|
|
|
|
rlnRelayMembershipIndex: conf.rlnRelayMembershipIndex,
|
|
|
|
|
rlnRelayEthContractAddress: conf.rlnRelayEthContractAddress,
|
|
|
|
|
rlnRelayEthClientAddress: conf.rlnRelayEthClientAddress,
|
|
|
|
|
rlnRelayEthAccountPrivateKey: conf.rlnRelayEthAccountPrivateKey,
|
|
|
|
|
rlnRelayEthAccountAddress: conf.rlnRelayEthAccountAddress,
|
|
|
|
|
rlnRelayCredPath: conf.rlnRelayCredPath,
|
|
|
|
|
rlnRelayCredentialsPassword: conf.rlnRelayCredentialsPassword
|
|
|
|
|
)
|
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
try:
|
2022-11-08 11:53:47 +00:00
|
|
|
|
let res = await node.mountRlnRelay(rlnConf)
|
2022-08-30 17:59:02 +00:00
|
|
|
|
if res.isErr():
|
2022-10-21 13:01:01 +00:00
|
|
|
|
return err("failed to mount waku RLN relay protocol: " & res.error)
|
|
|
|
|
except:
|
|
|
|
|
return err("failed to mount waku RLN relay protocol: " & getCurrentExceptionMsg())
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
if conf.swap:
|
|
|
|
|
try:
|
|
|
|
|
await mountSwap(node)
|
|
|
|
|
# TODO: Set swap peer, for now should be same as store peer
|
|
|
|
|
except:
|
|
|
|
|
return err("failed to mount waku swap protocol: " & getCurrentExceptionMsg())
|
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
if conf.store:
|
2022-11-23 09:08:00 +00:00
|
|
|
|
# Archive setup
|
|
|
|
|
let messageValidator: MessageValidator = DefaultMessageValidator()
|
|
|
|
|
mountArchive(node, archiveDriver, messageValidator=some(messageValidator), retentionPolicy=archiveRetentionPolicy)
|
|
|
|
|
|
|
|
|
|
# Store setup
|
2022-10-21 13:01:01 +00:00
|
|
|
|
try:
|
2022-11-23 09:08:00 +00:00
|
|
|
|
await mountStore(node)
|
2022-10-21 13:01:01 +00:00
|
|
|
|
except:
|
|
|
|
|
return err("failed to mount waku store protocol: " & getCurrentExceptionMsg())
|
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
# TODO: Move this to storage setup phase
|
2022-11-23 09:08:00 +00:00
|
|
|
|
if archiveRetentionPolicy.isSome():
|
2022-10-27 22:05:02 +00:00
|
|
|
|
executeMessageRetentionPolicy(node)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
startMessageRetentionPolicyPeriodicTask(node, interval=WakuArchiveDefaultRetentionPolicyInterval)
|
2022-10-21 13:01:01 +00:00
|
|
|
|
|
2022-10-28 18:11:28 +00:00
|
|
|
|
if conf.storenode != "":
|
|
|
|
|
try:
|
2022-11-23 09:08:00 +00:00
|
|
|
|
mountStoreClient(node)
|
2022-10-28 18:11:28 +00:00
|
|
|
|
setStorePeer(node, conf.storenode)
|
|
|
|
|
except:
|
|
|
|
|
return err("failed to set node waku store peer: " & getCurrentExceptionMsg())
|
2022-10-21 13:01:01 +00:00
|
|
|
|
|
|
|
|
|
# NOTE Must be mounted after relay
|
2022-10-28 14:30:01 +00:00
|
|
|
|
if conf.lightpush:
|
2022-10-21 13:01:01 +00:00
|
|
|
|
try:
|
|
|
|
|
await mountLightPush(node)
|
|
|
|
|
except:
|
|
|
|
|
return err("failed to mount waku lightpush protocol: " & getCurrentExceptionMsg())
|
|
|
|
|
|
2022-10-28 14:30:01 +00:00
|
|
|
|
if conf.lightpushnode != "":
|
|
|
|
|
try:
|
|
|
|
|
mountLightPushClient(node)
|
|
|
|
|
setLightPushPeer(node, conf.lightpushnode)
|
|
|
|
|
except:
|
|
|
|
|
return err("failed to set node waku lightpush peer: " & getCurrentExceptionMsg())
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
# Filter setup. NOTE Must be mounted after relay
|
2022-11-02 10:59:58 +00:00
|
|
|
|
if conf.filter:
|
2022-10-21 13:01:01 +00:00
|
|
|
|
try:
|
|
|
|
|
await mountFilter(node, filterTimeout = chronos.seconds(conf.filterTimeout))
|
|
|
|
|
except:
|
|
|
|
|
return err("failed to mount waku filter protocol: " & getCurrentExceptionMsg())
|
|
|
|
|
|
2022-11-02 10:59:58 +00:00
|
|
|
|
if conf.filternode != "":
|
|
|
|
|
try:
|
|
|
|
|
await mountFilterClient(node)
|
|
|
|
|
setFilterPeer(node, conf.filternode)
|
|
|
|
|
except:
|
|
|
|
|
return err("failed to set node waku filter peer: " & getCurrentExceptionMsg())
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
# waku peer exchange setup
|
|
|
|
|
if (conf.peerExchangeNode != "") or (conf.peerExchange):
|
|
|
|
|
try:
|
2022-11-02 08:45:21 +00:00
|
|
|
|
await mountPeerExchange(node)
|
2022-10-21 13:01:01 +00:00
|
|
|
|
except:
|
|
|
|
|
return err("failed to mount waku peer-exchange protocol: " & getCurrentExceptionMsg())
|
2022-09-20 11:03:34 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
if conf.peerExchangeNode != "":
|
|
|
|
|
try:
|
2022-09-20 11:03:34 +00:00
|
|
|
|
setPeerExchangePeer(node, conf.peerExchangeNode)
|
2022-10-21 13:01:01 +00:00
|
|
|
|
except:
|
2022-10-26 10:50:19 +00:00
|
|
|
|
return err("failed to set node waku peer-exchange peer: " & getCurrentExceptionMsg())
|
2022-09-20 11:03:34 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
return ok()
|
2022-03-01 14:11:56 +00:00
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
proc startNode(node: WakuNode, conf: WakuNodeConf,
|
2022-10-27 22:05:02 +00:00
|
|
|
|
dynamicBootstrapNodes: seq[RemotePeerInfo] = @[]): Future[SetupResult[void]] {.async.} =
|
2022-10-21 13:01:01 +00:00
|
|
|
|
## Start a configured node and all mounted protocols.
|
2022-10-27 22:05:02 +00:00
|
|
|
|
## Connect to static nodes and start
|
2022-10-21 13:01:01 +00:00
|
|
|
|
## keep-alive, if configured.
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
# Start Waku v2 node
|
|
|
|
|
try:
|
|
|
|
|
await node.start()
|
|
|
|
|
except:
|
|
|
|
|
return err("failed to start waku node: " & getCurrentExceptionMsg())
|
|
|
|
|
|
|
|
|
|
# Start discv5 and connect to discovered nodes
|
|
|
|
|
if conf.discv5Discovery:
|
|
|
|
|
try:
|
|
|
|
|
if not await node.startDiscv5():
|
|
|
|
|
error "could not start Discovery v5"
|
|
|
|
|
except:
|
|
|
|
|
return err("failed to start waku discovery v5: " & getCurrentExceptionMsg())
|
2022-09-20 11:03:34 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
# Connect to configured static nodes
|
|
|
|
|
if conf.staticnodes.len > 0:
|
|
|
|
|
try:
|
|
|
|
|
await connectToNodes(node, conf.staticnodes, "static")
|
|
|
|
|
except:
|
|
|
|
|
return err("failed to connect to static nodes: " & getCurrentExceptionMsg())
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
if dynamicBootstrapNodes.len > 0:
|
|
|
|
|
info "Connecting to dynamic bootstrap peers"
|
|
|
|
|
try:
|
|
|
|
|
await connectToNodes(node, dynamicBootstrapNodes, "dynamic bootstrap")
|
|
|
|
|
except:
|
|
|
|
|
return err("failed to connect to dynamic bootstrap nodes: " & getCurrentExceptionMsg())
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-11-25 13:15:39 +00:00
|
|
|
|
if conf.peerExchange:
|
|
|
|
|
asyncSpawn runPeerExchangeDiscv5Loop(node.wakuPeerExchange)
|
|
|
|
|
|
2022-10-21 13:01:01 +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()
|
|
|
|
|
try:
|
|
|
|
|
discard await node.wakuPeerExchange.request(desiredOutDegree)
|
|
|
|
|
except:
|
|
|
|
|
return err("failed to retrieve peer info via peer exchange protocol: " & getCurrentExceptionMsg())
|
|
|
|
|
|
|
|
|
|
# Start keepalive, if enabled
|
|
|
|
|
if conf.keepAlive:
|
|
|
|
|
node.startKeepalive()
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
return ok()
|
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
when defined(waku_exp_store_resume):
|
|
|
|
|
proc resumeMessageStore(node: WakuNode, address: string): Future[SetupResult[void]] {.async.} =
|
|
|
|
|
# Resume historical messages, this has to be called after the node has been started
|
|
|
|
|
if address != "":
|
|
|
|
|
return err("empty peer multiaddres")
|
|
|
|
|
|
|
|
|
|
var remotePeer: RemotePeerInfo
|
|
|
|
|
try:
|
|
|
|
|
remotePeer = parseRemotePeerInfo(address)
|
|
|
|
|
except:
|
|
|
|
|
return err("invalid peer multiaddress: " & getCurrentExceptionMsg())
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
await node.resume(some(@[remotePeer]))
|
|
|
|
|
except:
|
|
|
|
|
return err("failed to resume messages history: " & getCurrentExceptionMsg())
|
2021-05-13 21:21:46 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
proc startRpcServer(node: WakuNode, address: ValidIpAddress, port: uint16, portsShift: uint16, conf: WakuNodeConf): SetupResult[void] =
|
|
|
|
|
try:
|
|
|
|
|
startRpcServer(node, address, Port(port + portsShift), conf)
|
|
|
|
|
except:
|
|
|
|
|
return err("failed to start the json-rpc server: " & getCurrentExceptionMsg())
|
|
|
|
|
|
|
|
|
|
ok()
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
proc startRestServer(node: WakuNode, address: ValidIpAddress, port: uint16, portsShift: uint16, conf: WakuNodeConf): SetupResult[void] =
|
|
|
|
|
startRestServer(node, address, Port(port + portsShift), conf)
|
|
|
|
|
ok()
|
|
|
|
|
|
|
|
|
|
proc startMetricsServer(node: WakuNode, address: ValidIpAddress, port: uint16, portsShift: uint16): SetupResult[void] =
|
|
|
|
|
startMetricsServer(address, Port(port + portsShift))
|
|
|
|
|
ok()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc startMetricsLogging(): SetupResult[void] =
|
|
|
|
|
startMetricsLog()
|
2022-10-21 13:01:01 +00:00
|
|
|
|
ok()
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
2022-05-17 15:48:08 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
{.pop.} # @TODO confutils.nim(775, 17) Error: can raise an unlisted exception: ref IOError
|
|
|
|
|
when isMainModule:
|
|
|
|
|
## 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
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-11-03 09:45:06 +00:00
|
|
|
|
const versionString = "version / git commit hash: " & git_version
|
|
|
|
|
|
|
|
|
|
let confRes = WakuNodeConf.load(version=versionString)
|
|
|
|
|
if confRes.isErr():
|
|
|
|
|
error "failure while loading the configuration", error=confRes.error
|
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
|
|
let conf = confRes.get()
|
2022-10-18 23:03:43 +00:00
|
|
|
|
|
|
|
|
|
# set log level
|
|
|
|
|
if conf.logLevel != LogLevel.NONE:
|
|
|
|
|
setLogLevel(conf.logLevel)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
|
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
|
##############
|
|
|
|
|
# Node setup #
|
|
|
|
|
##############
|
2022-10-27 22:05:02 +00:00
|
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
|
debug "1/7 Setting up storage"
|
2022-10-27 22:05:02 +00:00
|
|
|
|
|
|
|
|
|
## Peer persistence
|
|
|
|
|
var peerStore = none(WakuPeerStorage)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
if conf.peerPersistence:
|
|
|
|
|
let peerStoreRes = setupPeerStorage();
|
|
|
|
|
if peerStoreRes.isOk():
|
|
|
|
|
peerStore = peerStoreRes.get()
|
|
|
|
|
else:
|
|
|
|
|
error "failed to setup peer store", error=peerStoreRes.error
|
|
|
|
|
waku_node_errors.inc(labelValues = ["init_store_failure"])
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
|
|
|
|
## Waku archive
|
|
|
|
|
var archiveDriver = none(ArchiveDriver)
|
|
|
|
|
var archiveRetentionPolicy = none(RetentionPolicy)
|
2022-10-27 22:05:02 +00:00
|
|
|
|
|
|
|
|
|
if conf.store:
|
|
|
|
|
# Message storage
|
|
|
|
|
let dbUrlValidationRes = validateDbUrl(conf.storeMessageDbUrl)
|
|
|
|
|
if dbUrlValidationRes.isErr():
|
|
|
|
|
error "failed to configure the message store database connection", error=dbUrlValidationRes.error
|
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
let archiveDriverRes = setupWakuArchiveDriver(dbUrlValidationRes.get(), vacuum=conf.storeMessageDbVacuum, migrate=conf.storeMessageDbMigration)
|
|
|
|
|
if archiveDriverRes.isOk():
|
|
|
|
|
archiveDriver = some(archiveDriverRes.get())
|
2022-10-27 22:05:02 +00:00
|
|
|
|
else:
|
2022-11-23 09:08:00 +00:00
|
|
|
|
error "failed to configure archive driver", error=archiveDriverRes.error
|
2022-10-27 22:05:02 +00:00
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
|
|
# Message store retention policy
|
|
|
|
|
let storeMessageRetentionPolicyRes = validateStoreMessageRetentionPolicy(conf.storeMessageRetentionPolicy)
|
|
|
|
|
if storeMessageRetentionPolicyRes.isErr():
|
|
|
|
|
error "invalid store message retention policy configuration", error=storeMessageRetentionPolicyRes.error
|
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
let archiveRetentionPolicyRes = setupWakuArchiveRetentionPolicy(storeMessageRetentionPolicyRes.get())
|
|
|
|
|
if archiveRetentionPolicyRes.isOk():
|
|
|
|
|
archiveRetentionPolicy = archiveRetentionPolicyRes.get()
|
2022-10-27 22:05:02 +00:00
|
|
|
|
else:
|
2022-11-23 09:08:00 +00:00
|
|
|
|
error "failed to configure the message retention policy", error=archiveRetentionPolicyRes.error
|
2022-10-27 22:05:02 +00:00
|
|
|
|
quit(QuitFailure)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
# TODO: Move retention policy execution here
|
2022-11-23 09:08:00 +00:00
|
|
|
|
# if archiveRetentionPolicy.isSome():
|
2022-10-27 22:05:02 +00:00
|
|
|
|
# executeMessageRetentionPolicy(node)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
# startMessageRetentionPolicyPeriodicTask(node, interval=WakuArchiveDefaultRetentionPolicyInterval)
|
2022-10-27 22:05:02 +00:00
|
|
|
|
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
|
debug "2/7 Retrieve dynamic bootstrap nodes"
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
|
var dynamicBootstrapNodes: seq[RemotePeerInfo]
|
2022-10-27 22:05:02 +00:00
|
|
|
|
let dynamicBootstrapNodesRes = retrieveDynamicBootstrapNodes(conf.dnsDiscovery, conf.dnsDiscoveryUrl, conf.dnsDiscoveryNameServers)
|
|
|
|
|
if dynamicBootstrapNodesRes.isOk():
|
2022-03-17 16:33:17 +00:00
|
|
|
|
dynamicBootstrapNodes = dynamicBootstrapNodesRes.get()
|
2022-10-27 22:05:02 +00:00
|
|
|
|
else:
|
|
|
|
|
warn "2/7 Retrieving dynamic bootstrap nodes failed. Continuing without dynamic bootstrap nodes.", error=dynamicBootstrapNodesRes.error
|
2022-03-17 16:33:17 +00:00
|
|
|
|
|
|
|
|
|
debug "3/7 Initializing node"
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
var node: WakuNode # This is the node we're going to setup using the conf
|
|
|
|
|
|
|
|
|
|
let initNodeRes = initNode(conf, peerStore, dynamicBootstrapNodes)
|
|
|
|
|
if initNodeRes.isok():
|
|
|
|
|
node = initNodeRes.get()
|
|
|
|
|
else:
|
2022-10-21 13:01:01 +00:00
|
|
|
|
error "3/7 Initializing node failed. Quitting.", error=initNodeRes.error
|
2021-07-22 09:46:54 +00:00
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
|
debug "4/7 Mounting protocols"
|
2021-04-24 04:56:37 +00:00
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
let setupProtocolsRes = waitFor setupProtocols(node, conf, archiveDriver, archiveRetentionPolicy)
|
2022-10-21 13:01:01 +00:00
|
|
|
|
if setupProtocolsRes.isErr():
|
|
|
|
|
error "4/7 Mounting protocols failed. Continuing in current state.", error=setupProtocolsRes.error
|
2020-09-01 02:09:54 +00:00
|
|
|
|
|
2022-03-17 16:33:17 +00:00
|
|
|
|
debug "5/7 Starting node and mounted protocols"
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
let startNodeRes = waitFor startNode(node, conf, dynamicBootstrapNodes)
|
|
|
|
|
if startNodeRes.isErr():
|
|
|
|
|
error "5/7 Starting node and mounted protocols failed. Continuing in current state.", error=startNodeRes.error
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
|
|
|
|
when defined(waku_exp_store_resume):
|
|
|
|
|
# Resume message store on boot
|
|
|
|
|
if conf.storeResumePeer != "":
|
|
|
|
|
let resumeMessageStoreRes = waitFor resumeMessageStore(node, conf.storeResumePeer)
|
|
|
|
|
if resumeMessageStoreRes.isErr():
|
|
|
|
|
error "failed to resume message store from peer node. Continuing in current state", error=resumeMessageStoreRes.error
|
2022-10-27 22:05:02 +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
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
if conf.rpc:
|
|
|
|
|
let startRpcServerRes = startRpcServer(node, conf.rpcAddress, conf.rpcPort, conf.portsShift, conf)
|
|
|
|
|
if startRpcServerRes.isErr():
|
|
|
|
|
error "6/7 Starting JSON-RPC server failed. Continuing in current state.", error=startRpcServerRes.error
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
if conf.rest:
|
|
|
|
|
let startRestServerRes = startRestServer(node, conf.restAddress, conf.restPort, conf.portsShift, conf)
|
|
|
|
|
if startRestServerRes.isErr():
|
|
|
|
|
error "6/7 Starting REST server failed. Continuing in current state.", error=startRestServerRes.error
|
2021-07-22 09:46:54 +00:00
|
|
|
|
|
2022-10-27 22:05:02 +00:00
|
|
|
|
if conf.metricsServer:
|
|
|
|
|
let startMetricsServerRes = startMetricsServer(node, conf.metricsServerAddress, conf.metricsServerPort, conf.portsShift)
|
|
|
|
|
if startMetricsServerRes.isErr():
|
|
|
|
|
error "6/7 Starting metrics server failed. Continuing in current state.", error=startMetricsServerRes.error
|
|
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
|
if conf.metricsLogging:
|
2022-10-27 22:05:02 +00:00
|
|
|
|
let startMetricsLoggingRes = startMetricsLogging()
|
|
|
|
|
if startMetricsLoggingRes.isErr():
|
|
|
|
|
error "6/7 Starting metrics console logging failed. Continuing in current state.", error=startMetricsLoggingRes.error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
debug "7/7 Setting up shutdown hooks"
|
2021-07-22 09:46:54 +00:00
|
|
|
|
## Setup shutdown hooks for this process.
|
|
|
|
|
## Stop node gracefully on shutdown.
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
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()
|
2022-11-29 15:02:18 +00:00
|
|
|
|
notice "Shutting down after receiving SIGINT"
|
2021-04-15 08:18:14 +00:00
|
|
|
|
waitFor node.stop()
|
|
|
|
|
quit(QuitSuccess)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2021-04-15 08:18:14 +00:00
|
|
|
|
setControlCHook(handleCtrlC)
|
|
|
|
|
|
|
|
|
|
# Handle SIGTERM
|
|
|
|
|
when defined(posix):
|
|
|
|
|
proc handleSigterm(signal: cint) {.noconv.} =
|
2022-11-29 15:02:18 +00:00
|
|
|
|
notice "Shutting down after receiving SIGTERM"
|
2021-04-15 08:18:14 +00:00
|
|
|
|
waitFor node.stop()
|
|
|
|
|
quit(QuitSuccess)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-09-07 15:31:27 +00:00
|
|
|
|
c_signal(ansi_c.SIGTERM, handleSigterm)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
|
2022-11-29 15:02:18 +00:00
|
|
|
|
# Handle SIGSEGV
|
|
|
|
|
when defined(posix):
|
|
|
|
|
proc handleSigsegv(signal: cint) {.noconv.} =
|
2022-11-30 18:41:19 +00:00
|
|
|
|
# Require --debugger:native
|
|
|
|
|
fatal "Shutting down after receiving SIGSEGV", stacktrace=getBacktrace()
|
2022-11-29 15:02:18 +00:00
|
|
|
|
|
2022-11-30 18:41:19 +00:00
|
|
|
|
# Not available in -d:release mode
|
2022-11-29 15:02:18 +00:00
|
|
|
|
writeStackTrace()
|
|
|
|
|
|
2022-11-30 18:41:19 +00:00
|
|
|
|
waitFor node.stop()
|
2022-11-29 15:02:18 +00:00
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
|
|
|
|
|
c_signal(ansi_c.SIGSEGV, handleSigsegv)
|
|
|
|
|
|
2022-10-21 13:01:01 +00:00
|
|
|
|
info "Node setup complete"
|
2020-09-01 02:09:54 +00:00
|
|
|
|
|
2021-05-28 18:41:29 +00:00
|
|
|
|
runForever()
|