@@ -24,12 +24,14 @@ import
2424 stream/ connection, # create and close stream read / write connections
2525 multiaddress,
2626 # encode different addressing schemes. For example, /ip4/7.7.7.7/tcp/6543 means it is using IPv4 protocol and TCP
27+ multicodec,
2728 peerinfo,
2829 # manage the information of a peer, such as peer ID and public / private key
2930 peerid, # Implement how peers interact
3031 protobuf/ minprotobuf, # message serialisation/deserialisation from and to protobufs
3132 nameresolving/ dnsresolver,
3233 protocols/ mix/ curve25519,
34+ protocols/ mix/ mix_protocol,
3335 ] # define DNS resolution
3436import
3537 waku/ [
3840 waku_lightpush/ rpc,
3941 waku_enr,
4042 discovery/ waku_dnsdisc,
43+ discovery/ waku_ext_kademlia,
4144 waku_node,
4245 node/ waku_metrics,
4346 node/ peer_manager,
8487
8588const MinMixNodePoolSize = 4
8689
90+ proc parseKadBootstrapNode (
91+ multiAddrStr: string
92+ ): Result [(PeerId , seq [MultiAddress ]), string ] =
93+ # # Parse a multiaddr string that includes /p2p/<peerID> into (PeerId, seq[MultiAddress])
94+ let multiAddr = MultiAddress .init (multiAddrStr).valueOr:
95+ return err (" Invalid multiaddress: " & multiAddrStr)
96+
97+ let peerIdPart = multiAddr.getPart (multiCodec (" p2p" )).valueOr:
98+ return err (" Multiaddress must include /p2p/<peerID>: " & multiAddrStr)
99+
100+ let peerIdBytes = peerIdPart.protoArgument ().valueOr:
101+ return err (" Failed to extract peer ID from multiaddress: " & multiAddrStr)
102+
103+ let peerId = PeerId .init (peerIdBytes).valueOr:
104+ return err (" Invalid peer ID in multiaddress: " & multiAddrStr)
105+
106+ ok ((peerId, @ [multiAddr]))
107+
87108# ####################
88109# # chat2 protobufs ##
89110# ####################
@@ -453,14 +474,39 @@ proc processInput(rfd: AsyncFD, rng: ref HmacDrbgContext) {.async.} =
453474 (await node.mountMix (conf.clusterId, mixPrivKey, conf.mixnodes)).isOkOr:
454475 error " failed to mount waku mix protocol: " , error = $ error
455476 quit (QuitFailure )
456- await node.mountRendezvousClient (conf.clusterId)
477+
478+ # Setup extended kademlia discovery if bootstrap nodes are provided
479+ if conf.kadBootstrapNodes.len > 0 :
480+ var kadBootstrapPeers: seq [(PeerId , seq [MultiAddress ])]
481+ for nodeStr in conf.kadBootstrapNodes:
482+ let parsed = parseKadBootstrapNode (nodeStr).valueOr:
483+ error " Failed to parse kademlia bootstrap node" , node = nodeStr, error = error
484+ continue
485+ kadBootstrapPeers.add (parsed)
486+
487+ if kadBootstrapPeers.len > 0 :
488+ (
489+ await setupExtendedKademliaDiscovery (
490+ node,
491+ ExtendedKademliaDiscoveryParams (
492+ bootstrapNodes: kadBootstrapPeers,
493+ mixPubKey: some (mixPubKey),
494+ advertiseMix: false ,
495+ ),
496+ )
497+ ).isOkOr:
498+ error " failed to setup kademlia discovery" , error = error
499+ quit (QuitFailure )
500+
501+ # await node.mountRendezvousClient(conf.clusterId)
457502
458503 await node.start ()
459504
460505 node.peerManager.start ()
506+ node.startExtendedKademliaDiscoveryLoop (minMixPeers = MinMixNodePoolSize )
461507
462508 await node.mountLibp2pPing ()
463- await node.mountPeerExchangeClient ()
509+ # await node.mountPeerExchangeClient()
464510 let pubsubTopic = conf.getPubsubTopic (node, conf.contentTopic)
465511 echo " pubsub topic is: " & pubsubTopic
466512 let nick = await readNick (transp)
@@ -601,11 +647,6 @@ proc processInput(rfd: AsyncFD, rng: ref HmacDrbgContext) {.async.} =
601647 node, pubsubTopic, conf.contentTopic, servicePeerInfo, false
602648 )
603649 echo " waiting for mix nodes to be discovered..."
604- while true :
605- if node.getMixNodePoolSize () >= MinMixNodePoolSize :
606- break
607- discard await node.fetchPeerExchangePeers ()
608- await sleepAsync (1000 )
609650
610651 while node.getMixNodePoolSize () < MinMixNodePoolSize :
611652 info " waiting for mix nodes to be discovered" ,
0 commit comments