Skip to content

Commit

Permalink
optimize waitFor utility
Browse files Browse the repository at this point in the history
  • Loading branch information
danisharora099 committed Jan 9, 2024
1 parent 1ab3d58 commit dca2f6e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
13 changes: 12 additions & 1 deletion packages/tests/src/waitForRemotePeerWithCodec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { IdentifyResult } from "@libp2p/interface";
import type { PeerId } from "@libp2p/interface/peer-id";
import type { LightNode } from "@waku/interfaces";

/**
Expand All @@ -12,8 +13,18 @@ import type { LightNode } from "@waku/interfaces";
*/
export async function waitForRemotePeerWithCodec(
waku: LightNode,
codec: string
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)) {
Expand Down
15 changes: 13 additions & 2 deletions packages/tests/tests/peer_exchange.node.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
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";
Expand Down Expand Up @@ -55,7 +56,7 @@ describe("Peer Exchange", () => {
waku = await createLightNode();
await waku.start();
await waku.libp2p.dialProtocol(nwaku2Ma, PeerExchangeCodec);
await waitForRemotePeerWithCodec(waku, PeerExchangeCodec);
await waitForRemotePeerWithCodec(waku, PeerExchangeCodec, nwaku2PeerId);

const components = waku.libp2p.components as unknown as Libp2pComponents;
const peerExchange = new WakuPeerExchange(components);
Expand All @@ -77,16 +78,26 @@ describe("Peer Exchange", () => {
expect(peerInfos[0].ENR?.peerInfo?.multiaddrs).to.not.be.null;

let foundNodeMas: Multiaddr[] = [];
let foundNodePeerId: PeerId | undefined = undefined;
const doesPeerIdExistInResponse =
peerInfos.find(({ ENR }) => {
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);

await waku.libp2p.dialProtocol(foundNodeMas, PeerExchangeCodec);
await waitForRemotePeerWithCodec(waku, 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

0 comments on commit dca2f6e

Please sign in to comment.