From 5b0c3c3cac3ddb5687d8f59457d6056527a8666c Mon Sep 17 00:00:00 2001 From: Danish Arora <35004822+danisharora099@users.noreply.github.com> Date: Thu, 23 Mar 2023 16:06:51 +0530 Subject: [PATCH] feat: compliance test for peer-exchange discovery (#1186) * skip fleet test for CI * add: compliance test * fix: check CI * fix: nwaku node name * remove: setTimeout * force typecasting instead of ts-ignore * rm: only for the test * increase readability --- .../src/waku_peer_exchange_discovery.ts | 2 +- .../tests/tests/peer_exchange.node.spec.ts | 84 ++++++++++++++++--- 2 files changed, 75 insertions(+), 11 deletions(-) diff --git a/packages/peer-exchange/src/waku_peer_exchange_discovery.ts b/packages/peer-exchange/src/waku_peer_exchange_discovery.ts index 82f88a3e24..0ce33d74d1 100644 --- a/packages/peer-exchange/src/waku_peer_exchange_discovery.ts +++ b/packages/peer-exchange/src/waku_peer_exchange_discovery.ts @@ -6,7 +6,7 @@ import { symbol } from "@libp2p/interface-peer-discovery"; import type { PeerId } from "@libp2p/interface-peer-id"; import type { PeerInfo } from "@libp2p/interface-peer-info"; import type { PeerProtocolsChangeData } from "@libp2p/interface-peer-store"; -import { EventEmitter } from "@libp2p/interfaces/events"; +import { CustomEvent, EventEmitter } from "@libp2p/interfaces/events"; import debug from "debug"; import { diff --git a/packages/tests/tests/peer_exchange.node.spec.ts b/packages/tests/tests/peer_exchange.node.spec.ts index 8aba5eb1ee..975a00e0dd 100644 --- a/packages/tests/tests/peer_exchange.node.spec.ts +++ b/packages/tests/tests/peer_exchange.node.spec.ts @@ -1,13 +1,14 @@ import { bootstrap } from "@libp2p/bootstrap"; +import tests from "@libp2p/interface-peer-discovery-compliance-tests"; import { Fleet, getPredefinedBootstrapNodes, } from "@waku/core/lib/predefined_bootstrap_nodes"; -import { createLightNode } from "@waku/create"; -import { PeerInfo } from "@waku/interfaces"; -import type { LightNode } from "@waku/interfaces"; +import { createLightNode, Libp2pComponents } from "@waku/create"; +import type { LightNode, PeerInfo } from "@waku/interfaces"; import { PeerExchangeCodec, + PeerExchangeDiscovery, WakuPeerExchange, wakuPeerExchangeDiscovery, } from "@waku/peer-exchange"; @@ -108,13 +109,16 @@ describe("Peer Exchange", () => { await nwaku2.waitForLog("Discovered px peers via discv5", 10); - // the ts-ignores are added ref: https://github.com/libp2p/js-libp2p-interfaces/issues/338#issuecomment-1431643645 - const peerExchange = new WakuPeerExchange({ - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - connectionManager: waku.libp2p.connectionManager, - peerStore: waku.libp2p.peerStore, - }); + // the forced type casting is done in ref to https://github.com/libp2p/js-libp2p-interfaces/issues/338#issuecomment-1431643645 + const { connectionManager, registrar, peerStore } = + waku.libp2p as unknown as Libp2pComponents; + const components = { + connectionManager: connectionManager, + registrar: registrar, + peerStore: peerStore, + }; + + const peerExchange = new WakuPeerExchange(components); const numPeersToRequest = 1; @@ -138,4 +142,64 @@ describe("Peer Exchange", () => { expect(waku.libp2p.peerStore.has(await nwaku2.getPeerId())).to.be.true; }); }); + + describe("compliance test", async function () { + this.timeout(25_000); + + let waku: LightNode; + let nwaku1: Nwaku; + let nwaku2: Nwaku; + + beforeEach(async function () { + nwaku1 = new Nwaku(makeLogFileName(this) + "1"); + nwaku2 = new Nwaku(makeLogFileName(this) + "2"); + }); + + tests({ + async setup() { + await nwaku1.start({ + discv5Discovery: true, + peerExchange: true, + }); + + const enr = (await nwaku1.info()).enrUri; + + await nwaku2.start({ + discv5Discovery: true, + peerExchange: true, + discv5BootstrapNode: enr, + }); + + waku = await createLightNode(); + + await waku.start(); + const nwaku2Ma = await nwaku2.getMultiaddrWithId(); + + await waku.libp2p.dialProtocol(nwaku2Ma, PeerExchangeCodec); + await new Promise((resolve) => { + waku.libp2p.peerStore.addEventListener("change:protocols", (evt) => { + if (evt.detail.protocols.includes(PeerExchangeCodec)) { + resolve(); + } + }); + }); + + // the forced type casting is done in ref to https://github.com/libp2p/js-libp2p-interfaces/issues/338#issuecomment-1431643645 + const { connectionManager, registrar, peerStore } = + waku.libp2p as unknown as Libp2pComponents; + const components = { + connectionManager: connectionManager, + registrar: registrar, + peerStore: peerStore, + }; + + return new PeerExchangeDiscovery(components); + }, + teardown: async () => { + !!nwaku1 && nwaku1.stop(); + !!nwaku2 && nwaku2.stop(); + !!waku && (await waku.stop()); + }, + }); + }); });