diff --git a/apps/wakunode2/app.nim b/apps/wakunode2/app.nim index c976e7de44..ab7ba889cc 100644 --- a/apps/wakunode2/app.nim +++ b/apps/wakunode2/app.nim @@ -11,6 +11,7 @@ import libp2p/crypto/crypto, libp2p/nameresolving/dnsresolver, libp2p/protocols/pubsub/gossipsub, + libp2p/discovery/discoverymngr, libp2p/peerid, eth/keys, json_rpc/rpcserver, @@ -81,6 +82,9 @@ type restServer: Option[RestServerRef] metricsServer: Option[MetricsHttpServerRef] + topics: seq[string] + dm: DiscoveryManager + AppResult*[T] = Result[T, string] @@ -160,7 +164,8 @@ proc init*(T: type App, rng: ref HmacDrbgContext, conf: WakuNodeConf): T = rng: rng, key: key, record: record, - node: nil + node: nil, + topics: topics, ) @@ -329,14 +334,16 @@ proc setupWakuApp*(app: var App): AppResult[void] = ## Mount protocols -proc setupProtocols(node: WakuNode, - conf: WakuNodeConf, - nodeKey: crypto.PrivateKey): - Future[AppResult[void]] {.async.} = +proc setupProtocols(app: App): Future[AppResult[void]] {.async.} = ## Setup configured protocols on an existing Waku v2 node. ## Optionally include persistent message storage. ## No protocols are started yet. + let + node = app.node + conf = app.conf + key = app.key + # Mount relay on all nodes var peerExchangeHandler = none(RoutingRecordsHandler) if conf.relayPeerExchange: @@ -355,14 +362,7 @@ proc setupProtocols(node: WakuNode, peerExchangeHandler = some(handlePeerExchange) if conf.relay: - let pubsubTopics = - if conf.pubsubTopics.len > 0 or conf.contentTopics.len > 0: - # TODO autoshard content topics only once. - # Already checked for errors in app.init - let shards = conf.contentTopics.mapIt(getShard(it).expect("Valid Shard")) - conf.pubsubTopics & shards - else: - conf.topics + let pubsubTopics = app.topics try: await mountRelay(node, pubsubTopics, peerExchangeHandler = peerExchangeHandler) @@ -493,9 +493,7 @@ proc setupProtocols(node: WakuNode, proc setupAndMountProtocols*(app: App): Future[AppResult[void]] {.async.} = return await setupProtocols( - app.node, - app.conf, - app.key + app ) ## Start node diff --git a/apps/wakunode2/wakunode2.nim b/apps/wakunode2/wakunode2.nim index 30a281f332..e44ec023cd 100644 --- a/apps/wakunode2/wakunode2.nim +++ b/apps/wakunode2/wakunode2.nim @@ -11,7 +11,8 @@ import metrics, libbacktrace, system/ansi_c, - libp2p/crypto/crypto + libp2p/crypto/crypto, + libp2p/discovery/[rendezvousinterface, discoverymngr] import ../../waku/common/logging, ./external_config, @@ -92,6 +93,11 @@ when isMainModule: error "5/7 Starting node and protocols failed", error=res6.error quit(QuitFailure) + wakunode2.dm = DiscoveryManager() + wakunode2.dm.add(RendezVousInterface.new(rdv = wakunode2.node.rendezvous, tta = 1.minutes)) + for topic in wakunode2.topics: + wakunode2.dm.advertise(RdvNamespace(topic)) + debug "6/7 Starting monitoring and external interfaces" let res7 = wakunode2.setupMonitoringAndExternalInterfaces() diff --git a/waku/common/utils/nat.nim b/waku/common/utils/nat.nim index de36ffcd87..81fbdfeebc 100644 --- a/waku/common/utils/nat.nim +++ b/waku/common/utils/nat.nim @@ -54,6 +54,9 @@ proc setupNat*(natConf, clientId: string, endpoint.udpPort = some(extUdpPort) else: # NatNone + if natConf == "none": + return ok((none(ValidIpAddress), none(Port), none(Port))) + if not natConf.startsWith("extip:"): return err("not a valid NAT mechanism: " & $natConf) @@ -64,4 +67,3 @@ proc setupNat*(natConf, clientId: string, return err("not a valid IP address: " & $natConf[6..^1]) return ok(endpoint) - diff --git a/waku/node/builder.nim b/waku/node/builder.nim index 7dea3ef0a9..1488ab909b 100644 --- a/waku/node/builder.nim +++ b/waku/node/builder.nim @@ -11,7 +11,9 @@ import libp2p/crypto/crypto, libp2p/builders, libp2p/nameresolving/nameresolver, - libp2p/transports/wstransport + libp2p/transports/wstransport, + libp2p/protocols/connectivity/relay/client, + libp2p/services/[autorelayservice, hpservice] import ../waku_enr, ../waku_discv5, @@ -145,6 +147,10 @@ proc build*(builder: WakuNodeBuilder): Result[WakuNode, string] = if builder.record.isNone(): return err("node record is required") + let relayClient = RelayClient.new() + let autoRelayService = AutoRelayService.new(1, relayClient, nil, rng) + let autonatService = getAutonatService(rng) + let hpservice = HPService.new(autonatService, autoRelayService) var switch: Switch try: switch = newWakuSwitch( @@ -161,7 +167,8 @@ proc build*(builder: WakuNodeBuilder): Result[WakuNode, string] = sendSignedPeerRecord = builder.switchSendSignedPeerRecord.get(false), agentString = builder.switchAgentString, peerStoreCapacity = builder.peerStorageCapacity, - services = @[Service(getAutonatService(rng))], + services = @[Service(hpservice)], + relay = relayClient, ) except CatchableError: return err("failed to create switch: " & getCurrentExceptionMsg()) diff --git a/waku/node/waku_node.nim b/waku/node/waku_node.nim index 5d9aa54e93..8ba52e4285 100644 --- a/waku/node/waku_node.nim +++ b/waku/node/waku_node.nim @@ -20,6 +20,7 @@ import libp2p/protocols/connectivity/autonat/client, libp2p/protocols/connectivity/autonat/service, libp2p/protocols/rendezvous, + libp2p/discovery/discoverymngr, libp2p/nameresolving/nameresolver, libp2p/builders, libp2p/transports/tcptransport, @@ -98,6 +99,7 @@ type libp2pPing*: Ping rng*: ref rand.HmacDrbgContext rendezvous*: RendezVous + dm*: DiscoveryManager announcedAddresses* : seq[MultiAddress] started*: bool # Indicates that node has started listening topicSubscriptionQueue*: AsyncEventQueue[SubscriptionEvent] diff --git a/waku/node/waku_switch.nim b/waku/node/waku_switch.nim index f81e4a6efc..9466cf3667 100644 --- a/waku/node/waku_switch.nim +++ b/waku/node/waku_switch.nim @@ -8,6 +8,7 @@ import std/[options, math], chronos, chronicles, eth/keys, + libp2p/protocols/connectivity/relay/relay, libp2p/crypto/crypto, libp2p/protocols/pubsub/gossipsub, libp2p/protocols/rendezvous, @@ -80,7 +81,7 @@ proc newWakuSwitch*( peerStoreCapacity = none(int), # defaults to 1.25 maxConnections services: seq[switch.Service] = @[], rendezvous: RendezVous = nil, - ): Switch + relay: Relay = nil): Switch {.raises: [Defect, IOError, LPError].} = var b = SwitchBuilder @@ -95,9 +96,13 @@ proc newWakuSwitch*( .withTcpTransport(transportFlags) .withNameResolver(nameResolver) .withSignedPeerRecord(sendSignedPeerRecord) - .withCircuitRelay() .withAutonat() + if relay != nil: + b = b.withCircuitRelay(relay) + else: + b = b.withCircuitRelay() + if peerStoreCapacity.isSome(): b = b.withPeerStore(peerStoreCapacity.get()) else: