Skip to content

Commit

Permalink
fix(tests): px interop
Browse files Browse the repository at this point in the history
  • Loading branch information
danisharora099 committed Jan 8, 2024
1 parent c81872a commit 12b1a1d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/tests/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * from "./node/node.js";
export * from "./teardown.js";
export * from "./message_collector.js";
export * from "./utils.js";
export * from "./waitForRemotePeerWithCodec.js";
28 changes: 28 additions & 0 deletions packages/tests/src/waitForRemotePeerWithCodec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { IdentifyResult } from "@libp2p/interface";
import type { LightNode } from "@waku/interfaces";

/**
* Wait for a remote peer to be identified with a given codec
* @param waku - Waku node
* @param codec - Codec to wait for
* @returns Promise that resolves when the peer is identified
* @internal
* This function is introduced as `core/waitForRemotePeer` only accounts for core protocols like Filter, LightPush & Store
* While this (currently) is not required by the SDK, it is required for the tests
*/
export async function waitForRemotePeerWithCodec(
waku: LightNode,
codec: string
): Promise<void> {
await new Promise<void>((resolve) => {
const cb = (evt: CustomEvent<IdentifyResult>): void => {
if (evt.detail.protocols.includes(codec)) {
waku.libp2p.removeEventListener("peer:identify", cb);
resolve();
}
};
waku.libp2p.addEventListener("peer:identify", cb);
});

return;
}
24 changes: 17 additions & 7 deletions packages/tests/tests/peer_exchange.node.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import tests from "@libp2p/interface-compliance-tests/peer-discovery";
import type { Multiaddr } from "@multiformats/multiaddr";
import type { LightNode, PeerInfo } from "@waku/interfaces";
import {
PeerExchangeCodec,
Expand All @@ -9,7 +10,7 @@ import { createLightNode, Libp2pComponents } from "@waku/sdk";
import { expect } from "chai";

import { delay } from "../src/delay.js";
import { tearDownNodes } from "../src/index.js";
import { tearDownNodes, waitForRemotePeerWithCodec } from "../src/index.js";
import { makeLogFileName } from "../src/log_file.js";
import { NimGoNode } from "../src/node/node.js";

Expand All @@ -29,7 +30,7 @@ describe("Peer Exchange", () => {
await tearDownNodes([nwaku1, nwaku2], waku);
});

it("nwaku interop", async function () {
it.only("nwaku interop", async function () {
this.timeout(55_000);

await nwaku1.start({
Expand All @@ -48,11 +49,13 @@ describe("Peer Exchange", () => {
});

const nwaku1PeerId = await nwaku1.getPeerId();
const nwaku2PeerId = await nwaku2.getPeerId();
const nwaku2Ma = await nwaku2.getMultiaddrWithId();

waku = await createLightNode();
await waku.start();
await waku.libp2p.dialProtocol(nwaku2Ma, PeerExchangeCodec);
await waitForRemotePeerWithCodec(waku, PeerExchangeCodec);

const components = waku.libp2p.components as unknown as Libp2pComponents;
const peerExchange = new WakuPeerExchange(components);
Expand All @@ -62,6 +65,7 @@ describe("Peer Exchange", () => {
let peerInfos: PeerInfo[] = [];
while (peerInfos.length <= 0) {
peerInfos = (await peerExchange.query({
peerId: nwaku2PeerId,
numPeers: numPeersToRequest
})) as PeerInfo[];
await delay(3000);
Expand All @@ -70,16 +74,22 @@ describe("Peer Exchange", () => {
expect(peerInfos.length).to.be.greaterThan(0);
expect(peerInfos.length).to.be.lessThanOrEqual(numPeersToRequest);
expect(peerInfos[0].ENR).to.not.be.null;
expect(peerInfos[0].ENR?.peerInfo?.multiaddrs).to.not.be.null;

let foundNodeMas: Multiaddr[] = [];
const doesPeerIdExistInResponse =
peerInfos.find(
({ ENR }) => ENR?.peerInfo?.id.toString() === nwaku1PeerId.toString()
) !== undefined;
peerInfos.find(({ ENR }) => {
foundNodeMas = ENR?.peerInfo?.multiaddrs ?? [];
return ENR?.peerInfo?.id.toString() === nwaku1PeerId.toString();
}) !== undefined;

expect(doesPeerIdExistInResponse).to.be.equal(true);

expect(await waku.libp2p.peerStore.has(await nwaku2.getPeerId())).to.be
.true;
await waku.libp2p.dialProtocol(foundNodeMas, PeerExchangeCodec);
await waitForRemotePeerWithCodec(waku, PeerExchangeCodec);

expect(await waku.libp2p.peerStore.has(nwaku1PeerId)).to.eq(true);
expect(waku.libp2p.getConnections()).has.length(2);
});
});

Expand Down

0 comments on commit 12b1a1d

Please sign in to comment.