Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tests): peer-exchange interop #1773

Merged
merged 7 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
39 changes: 39 additions & 0 deletions packages/tests/src/waitForRemotePeerWithCodec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { IdentifyResult } from "@libp2p/interface";
import type { PeerId } from "@libp2p/interface/peer-id";
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,
nodePeerId: PeerId
): Promise<void> {
const connectedPeers = waku.libp2p
.getConnections()
.map((conn) => conn.remotePeer);
if (
connectedPeers.find((connectedPeer) => connectedPeer.equals(nodePeerId))
) {
return;
}

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;
}
33 changes: 27 additions & 6 deletions packages/tests/tests/peer_exchange.node.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { PeerId } from "@libp2p/interface/peer-id";
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 +11,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 Down Expand Up @@ -48,11 +50,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, nwaku2PeerId);

const components = waku.libp2p.components as unknown as Libp2pComponents;
const peerExchange = new WakuPeerExchange(components);
Expand All @@ -62,6 +66,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 +75,32 @@ 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[] = [];
let foundNodePeerId: PeerId | undefined = undefined;
const doesPeerIdExistInResponse =
peerInfos.find(
({ ENR }) => ENR?.peerInfo?.id.toString() === nwaku1PeerId.toString()
) !== undefined;
peerInfos.find(({ ENR }) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not use .find in that case and would use .forEach since side effects important so that presence of foundNodePeerId is the result.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed it to use some which seems more appropriate & efficient

foundNodeMas = ENR?.peerInfo?.multiaddrs ?? [];
foundNodePeerId = ENR?.peerInfo?.id;
return ENR?.peerInfo?.id.toString() === nwaku1PeerId.toString();
}) !== undefined;

if (!foundNodePeerId) {
throw new Error("Peer ID not found");
}

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,
foundNodePeerId
);

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

Expand Down
Loading