From cdf1ba2ad52a878873b1eb6477f490f4d911bd1b Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Mon, 14 Aug 2023 17:14:52 +0530 Subject: [PATCH 1/2] use `peer:identify` instead of `peer:update` --- .../src/waku_peer_exchange_discovery.ts | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/packages/peer-exchange/src/waku_peer_exchange_discovery.ts b/packages/peer-exchange/src/waku_peer_exchange_discovery.ts index 06a22d0c0f..e8322d49c0 100644 --- a/packages/peer-exchange/src/waku_peer_exchange_discovery.ts +++ b/packages/peer-exchange/src/waku_peer_exchange_discovery.ts @@ -1,4 +1,4 @@ -import type { PeerUpdate } from "@libp2p/interface-libp2p"; +import type { IdentifyResult } from "@libp2p/interface-libp2p"; import type { PeerDiscovery, PeerDiscoveryEvents, @@ -61,11 +61,10 @@ export class PeerExchangeDiscovery private queryAttempts: Map = new Map(); private readonly handleDiscoveredPeer = ( - event: CustomEvent + event: CustomEvent, ): void => { - const { - peer: { protocols, id: peerId }, - } = event.detail; + const { protocols, peerId } = event.detail; + if ( !protocols.includes(PeerExchangeCodec) || this.queryingPeers.has(peerId.toString()) @@ -74,7 +73,7 @@ export class PeerExchangeDiscovery this.queryingPeers.add(peerId.toString()); this.startRecurringQueries(peerId).catch((error) => - log(`Error querying peer ${error}`) + log(`Error querying peer ${error}`), ); }; @@ -98,8 +97,8 @@ export class PeerExchangeDiscovery // might be better to use "peer:identify" or "peer:update" this.components.events.addEventListener( - "peer:update", - this.handleDiscoveredPeer + "peer:identify", + this.handleDiscoveredPeer, ); } @@ -112,8 +111,8 @@ export class PeerExchangeDiscovery this.isStarted = false; this.queryingPeers.clear(); this.components.events.removeEventListener( - "peer:update", - this.handleDiscoveredPeer + "peer:identify", + this.handleDiscoveredPeer, ); } @@ -126,7 +125,7 @@ export class PeerExchangeDiscovery } private readonly startRecurringQueries = async ( - peerId: PeerId + peerId: PeerId, ): Promise => { const peerIdStr = peerId.toString(); const { @@ -137,7 +136,7 @@ export class PeerExchangeDiscovery log( `Querying peer: ${peerIdStr} (attempt ${ this.queryAttempts.get(peerIdStr) ?? 1 - })` + })`, ); await this.query(peerId); @@ -204,7 +203,7 @@ export class PeerExchangeDiscovery protocols: [], multiaddrs: peerInfo.multiaddrs, }, - }) + }), ); } } @@ -217,7 +216,7 @@ export class PeerExchangeDiscovery } export function wakuPeerExchangeDiscovery(): ( - components: Libp2pComponents + components: Libp2pComponents, ) => PeerExchangeDiscovery { return (components: Libp2pComponents) => new PeerExchangeDiscovery(components); From f3fda7279ee80a3aa95b6a39632204395c917109 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Mon, 14 Aug 2023 17:15:11 +0530 Subject: [PATCH 2/2] dial the peer after initialising peer-exchange --- packages/tests/tests/peer_exchange.node.spec.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/tests/tests/peer_exchange.node.spec.ts b/packages/tests/tests/peer_exchange.node.spec.ts index 19a06709c1..fb7439f351 100644 --- a/packages/tests/tests/peer_exchange.node.spec.ts +++ b/packages/tests/tests/peer_exchange.node.spec.ts @@ -117,7 +117,11 @@ describe("Peer Exchange", () => { await waku.start(); const nwaku2Ma = await nwaku2.getMultiaddrWithId(); - await waku.libp2p.dialProtocol(nwaku2Ma, PeerExchangeCodec); + + // we do this because we want peer-exchange discovery to get initialised before we dial the peer which contains info about the other peer + setTimeout(() => { + void waku.libp2p.dialProtocol(nwaku2Ma, PeerExchangeCodec); + }, 1000); return new PeerExchangeDiscovery(waku.libp2p.components); },