Skip to content

Commit

Permalink
chore: update & fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
danisharora099 committed Jun 6, 2024
1 parent 5a74b1a commit a5bd2cd
Showing 1 changed file with 45 additions and 37 deletions.
82 changes: 45 additions & 37 deletions packages/tests/tests/peer-exchange/pe.optional.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { bootstrap } from "@libp2p/bootstrap";
import {
Fleet,
getPredefinedBootstrapNodes
} from "@waku/core/lib/predefined_bootstrap_nodes";
import { wakuPeerExchangeDiscovery } from "@waku/discovery";
DnsNodeDiscovery,
enrTree,
wakuPeerExchangeDiscovery
} from "@waku/discovery";
import type { LightNode } from "@waku/interfaces";
import { createLightNode } from "@waku/sdk";
import {
Expand All @@ -17,50 +17,58 @@ import { afterEachCustom, tearDownNodes } from "../../src";
describe("Peer Exchange", () => {
describe("Auto Discovery", function () {
let waku: LightNode;
const predefinedNodes: string[] = [];

afterEachCustom(this, async () => {
await tearDownNodes([], waku);
});

const testCases: [Fleet, number][] = [
[Fleet.Test, 2], // on test fleet there are only 3 peers
[Fleet.Sandbox, 3]
];
it(`should discover peers other than used for bootstrapping`, async function () {
this.timeout(50_000);

testCases.map(([name, nodes]) => {
it(`should discover peers other than used for bootstrapping on ${name} fleet`, async function () {
this.timeout(50_000);
const predefinedNodes = getPredefinedBootstrapNodes(name, nodes);
const dns = await DnsNodeDiscovery.dnsOverHttp();
const dnsEnrs = await dns.getPeers(
[enrTree["SANDBOX"], enrTree["TEST"]],
{
lightPush: 1
}
);
const dnsPeerMultiaddrs = dnsEnrs
.flatMap(
(enr) => enr.peerInfo?.multiaddrs.map((ma) => ma.toString()) ?? []
)
.filter((ma) => ma.includes("wss"));

const singleShardInfo = { clusterId: 1, shard: 1 };
const shardInfo = singleShardInfosToShardInfo([singleShardInfo]);
const pubsubTopic = singleShardInfoToPubsubTopic(singleShardInfo);
waku = await createLightNode({
libp2p: {
peerDiscovery: [
bootstrap({ list: predefinedNodes }),
wakuPeerExchangeDiscovery([pubsubTopic])
]
},
shardInfo: shardInfo
});
const singleShardInfo = { clusterId: 1, shard: 1 };
const shardInfo = singleShardInfosToShardInfo([singleShardInfo]);
const pubsubTopic = singleShardInfoToPubsubTopic(singleShardInfo);
waku = await createLightNode({
libp2p: {
peerDiscovery: [
bootstrap({ list: dnsPeerMultiaddrs }),
wakuPeerExchangeDiscovery([pubsubTopic])
]
},
shardInfo: shardInfo
});

await waku.start();
await waku.start();

const foundPxPeer = await new Promise<boolean>((resolve) => {
waku.libp2p.addEventListener("peer:discovery", (evt) => {
const peerId = evt.detail.id.toString();
const isBootstrapNode = predefinedNodes.find((n) =>
n.includes(peerId)
);
if (!isBootstrapNode) {
resolve(true);
}
});
const foundPxPeer = await new Promise<boolean>((resolve) => {
waku.libp2p.addEventListener("peer:discovery", (evt) => {
const peerId = evt.detail.id.toString();
console.log({ peerId });
const isBootstrapNode = predefinedNodes.find((n) =>
n.includes(peerId)
);
console.log({ isBootstrapNode });
if (!isBootstrapNode) {
resolve(true);
}
});

expect(foundPxPeer).to.be.true;
});

expect(foundPxPeer).to.be.true;
});
});
});

0 comments on commit a5bd2cd

Please sign in to comment.