From a739ada33a1584726181a51ff6fd51cd6ff4a135 Mon Sep 17 00:00:00 2001 From: Danish Arora <35004822+danisharora099@users.noreply.github.com> Date: Mon, 22 Jul 2024 18:01:15 +0530 Subject: [PATCH] chore(peer-exchange): use an event listener to gauge if the service is mounted (#2071) * chore: use service mounted event listener * chore: update event name * chore: change event name to isStarted * chore: address comments --- .../waku_peer_exchange_discovery.ts | 10 ++++- .../tests/peer-exchange/compliance.spec.ts | 45 ++++++++++--------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/packages/discovery/src/peer-exchange/waku_peer_exchange_discovery.ts b/packages/discovery/src/peer-exchange/waku_peer_exchange_discovery.ts index 17bcd50168..516115ee5b 100644 --- a/packages/discovery/src/peer-exchange/waku_peer_exchange_discovery.ts +++ b/packages/discovery/src/peer-exchange/waku_peer_exchange_discovery.ts @@ -50,12 +50,16 @@ export interface Options { maxRetries?: number; } +interface CustomDiscoveryEvent extends PeerDiscoveryEvents { + "waku:peer-exchange:started": CustomEvent; +} + export const DEFAULT_PEER_EXCHANGE_TAG_NAME = Tags.PEER_EXCHANGE; const DEFAULT_PEER_EXCHANGE_TAG_VALUE = 50; const DEFAULT_PEER_EXCHANGE_TAG_TTL = 100_000_000; export class PeerExchangeDiscovery - extends TypedEventEmitter + extends TypedEventEmitter implements PeerDiscovery { private readonly components: Libp2pComponents; @@ -102,6 +106,10 @@ export class PeerExchangeDiscovery return; } + this.dispatchEvent( + new CustomEvent("waku:peer-exchange:started", { detail: true }) + ); + log.info("Starting peer exchange node discovery, discovering peers"); // might be better to use "peer:identify" or "peer:update" diff --git a/packages/tests/tests/peer-exchange/compliance.spec.ts b/packages/tests/tests/peer-exchange/compliance.spec.ts index 681c5cf0c3..68e276b8d2 100644 --- a/packages/tests/tests/peer-exchange/compliance.spec.ts +++ b/packages/tests/tests/peer-exchange/compliance.spec.ts @@ -24,36 +24,41 @@ describe("Peer Exchange", function () { beforeEachCustom(this, async () => { nwaku1 = new ServiceNode(makeLogFileName(this.ctx) + "1"); nwaku2 = new ServiceNode(makeLogFileName(this.ctx) + "2"); + + await nwaku1.start({ + relay: true, + discv5Discovery: true, + peerExchange: true + }); + const enr = (await nwaku1.info()).enrUri; + + await nwaku2.start({ + relay: true, + discv5Discovery: true, + peerExchange: true, + discv5BootstrapNode: enr + }); }); tests({ async setup() { - await nwaku1.start({ - relay: true, - discv5Discovery: true, - peerExchange: true - }); - - const enr = (await nwaku1.info()).enrUri; - - await nwaku2.start({ - relay: true, - discv5Discovery: true, - peerExchange: true, - discv5BootstrapNode: enr - }); - waku = await createLightNode(); await waku.start(); const nwaku2Ma = await nwaku2.getMultiaddrWithId(); - // 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); + const peerExchange = new PeerExchangeDiscovery( + waku.libp2p.components, + pubsubTopic + ); + + peerExchange.addEventListener("waku:peer-exchange:started", (event) => { + if (event.detail === true) { + void waku.libp2p.dialProtocol(nwaku2Ma, PeerExchangeCodec); + } + }); - return new PeerExchangeDiscovery(waku.libp2p.components, pubsubTopic); + return peerExchange; }, teardown: async () => { this.timeout(15000);