Skip to content

Commit

Permalink
Use DiscoveryManager
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrsantos committed Sep 22, 2023
1 parent f02ca75 commit f64d12b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
30 changes: 14 additions & 16 deletions apps/wakunode2/app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -81,6 +82,9 @@ type
restServer: Option[RestServerRef]
metricsServer: Option[MetricsHttpServerRef]

topics: seq[string]
dm: DiscoveryManager

AppResult*[T] = Result[T, string]


Expand Down Expand Up @@ -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,
)


Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion apps/wakunode2/wakunode2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions waku/node/waku_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit f64d12b

Please sign in to comment.