Skip to content

Commit

Permalink
Merge branch 'master' of github.com:waku-org/js-waku into chore/renam…
Browse files Browse the repository at this point in the history
…e-local-peer-cache
  • Loading branch information
danisharora099 committed Feb 27, 2024
2 parents e7993ea + df8c0d7 commit 97f8262
Show file tree
Hide file tree
Showing 8 changed files with 481 additions and 188 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"size-limit": "^11.0.1",
"ts-loader": "^9.4.2",
"ts-node": "^10.9.2",
"typedoc": "^0.25.7",
"typedoc": "^0.25.9",
"typescript": "^5.3.2"
},
"lint-staged": {
Expand Down
16 changes: 9 additions & 7 deletions packages/tests/src/utils/custom_mocha_hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const log = new Logger("test:mocha-hook");
function withGracefulTimeout(
asyncOperation: () => Promise<void>,
doneCallback: (error?: unknown) => void,
timeoutDuration: number = MOCHA_HOOK_MAX_TIMEOUT
timeoutDuration = MOCHA_HOOK_MAX_TIMEOUT
): void {
let operationCompleted = false;

Expand Down Expand Up @@ -45,24 +45,26 @@ function withGracefulTimeout(

export const beforeEachCustom = function (
suite: Suite,
cb: () => Promise<void>
cb: () => Promise<void>,
timeout = MOCHA_HOOK_MAX_TIMEOUT
): void {
const timeoutBefore = suite.timeout();
suite.timeout(MOCHA_HOOK_MAX_TIMEOUT);
suite.timeout(timeout);
suite.beforeEach((done) => {
withGracefulTimeout(cb, done);
withGracefulTimeout(cb, done, timeout);
});
suite.timeout(timeoutBefore); // restore timeout to the original value
};

export const afterEachCustom = function (
suite: Suite,
cb: () => Promise<void>
cb: () => Promise<void>,
timeout = MOCHA_HOOK_MAX_TIMEOUT
): void {
const timeoutBefore = suite.timeout();
suite.timeout(MOCHA_HOOK_MAX_TIMEOUT);
suite.timeout(timeout);
suite.afterEach((done) => {
withGracefulTimeout(cb, done);
withGracefulTimeout(cb, done, timeout);
});
suite.timeout(timeoutBefore); // restore timeout to the original value
};
64 changes: 64 additions & 0 deletions packages/tests/tests/peer-exchange/compliance.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import tests from "@libp2p/interface-compliance-tests/peer-discovery";
import type { LightNode } from "@waku/interfaces";
import { PeerExchangeCodec, PeerExchangeDiscovery } from "@waku/peer-exchange";
import { createLightNode } from "@waku/sdk";
import { singleShardInfoToPubsubTopic } from "@waku/utils";

import {
beforeEachCustom,
makeLogFileName,
ServiceNode,
tearDownNodes
} from "../../src/index.js";

const pubsubTopic = [singleShardInfoToPubsubTopic({ clusterId: 0, shard: 2 })];

describe("Peer Exchange", function () {
describe("Compliance Test", function () {
this.timeout(100_000);

let waku: LightNode;
let nwaku1: ServiceNode;
let nwaku2: ServiceNode;

beforeEachCustom(this, async () => {
nwaku1 = new ServiceNode(makeLogFileName(this.ctx) + "1");
nwaku2 = new ServiceNode(makeLogFileName(this.ctx) + "2");
});

tests({
async setup() {
await nwaku1.start({
relay: true,
discv5Discovery: true,
peerExchange: true
});

const enr = (await nwaku1.info()).enrUri;

await nwaku2.start({
relay: true,
discv5Discovery: true,
peerExchange: true,
discv5BootstrapNode: enr
});

waku = await createLightNode();
await waku.start();

const nwaku2Ma = await nwaku2.getMultiaddrWithId();

// we do this because we want peer-exchange discovery to get initialised before we dial the peer which contains info about the other peer
setTimeout(() => {
void waku.libp2p.dialProtocol(nwaku2Ma, PeerExchangeCodec);
}, 1000);

return new PeerExchangeDiscovery(waku.libp2p.components, pubsubTopic);
},
teardown: async () => {
this.timeout(15000);
await tearDownNodes([nwaku1, nwaku2], waku);
}
});
});
});
236 changes: 236 additions & 0 deletions packages/tests/tests/peer-exchange/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
import { bootstrap } from "@libp2p/bootstrap";
import type { PeerId } from "@libp2p/interface";
import type { LightNode, PeersByDiscoveryResult } from "@waku/interfaces";
import { wakuPeerExchangeDiscovery } from "@waku/peer-exchange";
import { createLightNode, Tags } from "@waku/sdk";
import { Logger, singleShardInfoToPubsubTopic } from "@waku/utils";
import { expect } from "chai";
import Sinon, { SinonSpy } from "sinon";

import {
afterEachCustom,
beforeEachCustom,
makeLogFileName,
ServiceNode,
tearDownNodes
} from "../../src/index.js";

export const log = new Logger("test:pe");
const pubsubTopic = [singleShardInfoToPubsubTopic({ clusterId: 0, shard: 2 })];

describe("Peer Exchange", function () {
this.timeout(150_000);
let waku: LightNode;
let nwaku1: ServiceNode;
let nwaku2: ServiceNode;
let nwaku3: ServiceNode;
let dialPeerSpy: SinonSpy;
let nwaku1PeerId: PeerId;

beforeEachCustom(this, async () => {
nwaku1 = new ServiceNode(makeLogFileName(this.ctx) + "1");
nwaku2 = new ServiceNode(makeLogFileName(this.ctx) + "2");
await nwaku1.start({
pubsubTopic: pubsubTopic,
discv5Discovery: true,
peerExchange: true,
relay: true
});
await nwaku2.start({
pubsubTopic: pubsubTopic,
discv5Discovery: true,
peerExchange: true,
discv5BootstrapNode: (await nwaku1.info()).enrUri,
relay: true
});
nwaku1PeerId = await nwaku1.getPeerId();
});

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

it("getPeersByDiscovery", async function () {
waku = await createLightNode({
libp2p: {
peerDiscovery: [
bootstrap({ list: [(await nwaku2.getMultiaddrWithId()).toString()] }),
wakuPeerExchangeDiscovery(pubsubTopic)
]
}
});
await waku.start();
dialPeerSpy = Sinon.spy((waku as any).connectionManager, "dialPeer");
const pxPeersDiscovered = new Set<PeerId>();
await new Promise<void>((resolve) => {
waku.libp2p.addEventListener("peer:discovery", (evt) => {
return void (async () => {
const peerId = evt.detail.id;
const peer = await waku.libp2p.peerStore.get(peerId);
const tags = Array.from(peer.tags.keys());
if (tags.includes(Tags.PEER_EXCHANGE)) {
pxPeersDiscovered.add(peerId);
if (pxPeersDiscovered.size === 1) {
resolve();
}
}
})();
});
});
expect(dialPeerSpy.callCount).to.equal(1);

const peers_after = <PeersByDiscoveryResult>(
await waku.connectionManager.getPeersByDiscovery()
);
const discovered_peer_exchange = peers_after.DISCOVERED[Tags.PEER_EXCHANGE];
const discovered_bootstram = peers_after.DISCOVERED[Tags.BOOTSTRAP];
const connected_peer_exchange = peers_after.CONNECTED[Tags.PEER_EXCHANGE];
const connected_bootstram = peers_after.CONNECTED[Tags.BOOTSTRAP];
expect(discovered_peer_exchange.length).to.eq(1);
expect(discovered_peer_exchange[0].id.toString()).to.eq(
nwaku1PeerId.toString()
);
expect(discovered_peer_exchange[0].tags.has("peer-exchange")).to.be.true;
expect(discovered_bootstram.length).to.eq(1);
expect(connected_peer_exchange.length).to.eq(0);
expect(connected_bootstram.length).to.eq(1);
});

// will be skipped until https://github.com/waku-org/js-waku/issues/1860 is fixed
it.skip("new peer added after a peer was already found", async function () {
waku = await createLightNode({
libp2p: {
peerDiscovery: [
bootstrap({ list: [(await nwaku2.getMultiaddrWithId()).toString()] }),
wakuPeerExchangeDiscovery(pubsubTopic)
]
}
});
await waku.start();

dialPeerSpy = Sinon.spy((waku as any).connectionManager, "dialPeer");
const pxPeersDiscovered = new Set<PeerId>();
await new Promise<void>((resolve) => {
waku.libp2p.addEventListener("peer:discovery", (evt) => {
return void (async () => {
const peerId = evt.detail.id;
const peer = await waku.libp2p.peerStore.get(peerId);
const tags = Array.from(peer.tags.keys());
if (tags.includes(Tags.PEER_EXCHANGE)) {
pxPeersDiscovered.add(peerId);
if (pxPeersDiscovered.size === 1) {
resolve();
}
}
})();
});
});

nwaku3 = new ServiceNode(makeLogFileName(this) + "3");
await nwaku3.start({
pubsubTopic: pubsubTopic,
discv5Discovery: true,
peerExchange: true,
discv5BootstrapNode: (await nwaku1.info()).enrUri,
relay: true,
lightpush: true,
filter: true
});

await new Promise<void>((resolve) => {
waku.libp2p.addEventListener("peer:discovery", (evt) => {
return void (async () => {
const peerId = evt.detail.id;
const peer = await waku.libp2p.peerStore.get(peerId);
const tags = Array.from(peer.tags.keys());
if (tags.includes(Tags.PEER_EXCHANGE)) {
pxPeersDiscovered.add(peerId);
if (pxPeersDiscovered.size === 2) {
resolve();
}
}
})();
});
});
});

// will be skipped until https://github.com/waku-org/js-waku/issues/1858 is fixed
it.skip("wrong wakuPeerExchangeDiscovery pubsub topic", async function () {
waku = await createLightNode({
libp2p: {
peerDiscovery: [
bootstrap({ list: [(await nwaku2.getMultiaddrWithId()).toString()] }),
wakuPeerExchangeDiscovery(["wrong"])
]
}
});
await waku.start();
dialPeerSpy = Sinon.spy((waku as any).connectionManager, "dialPeer");

const pxPeersDiscovered = new Set<PeerId>();
await new Promise<void>((resolve) => {
const timeoutId = setTimeout(() => {
resolve();
}, 40000);

waku.libp2p.addEventListener("peer:discovery", (evt) => {
return void (async () => {
const peerId = evt.detail.id;
const peer = await waku.libp2p.peerStore.get(peerId);
const tags = Array.from(peer.tags.keys());
if (tags.includes(Tags.PEER_EXCHANGE)) {
pxPeersDiscovered.add(peerId);
if (pxPeersDiscovered.size === 1) {
clearTimeout(timeoutId);
resolve();
}
}
})();
});
});

expect(
pxPeersDiscovered.size,
"No peer should have been discovered"
).to.equal(0);
});

it("peerDiscovery without wakuPeerExchangeDiscovery", async function () {
waku = await createLightNode({
libp2p: {
peerDiscovery: [
bootstrap({ list: [(await nwaku2.getMultiaddrWithId()).toString()] })
]
}
});
await waku.start();
dialPeerSpy = Sinon.spy((waku as any).connectionManager, "dialPeer");

const pxPeersDiscovered = new Set<PeerId>();
await new Promise<void>((resolve) => {
const timeoutId = setTimeout(() => {
resolve();
}, 40000);

waku.libp2p.addEventListener("peer:discovery", (evt) => {
return void (async () => {
const peerId = evt.detail.id;
const peer = await waku.libp2p.peerStore.get(peerId);
const tags = Array.from(peer.tags.keys());
if (tags.includes(Tags.PEER_EXCHANGE)) {
pxPeersDiscovered.add(peerId);
if (pxPeersDiscovered.size === 1) {
clearTimeout(timeoutId);
resolve();
}
}
})();
});
});

expect(
pxPeersDiscovered.size,
"No peer should have been discovered"
).to.equal(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { wakuPeerExchangeDiscovery } from "@waku/peer-exchange";
import { createLightNode, DefaultPubsubTopic } from "@waku/sdk";
import { expect } from "chai";

import { afterEachCustom, tearDownNodes } from "../src";
import { afterEachCustom, tearDownNodes } from "../../src";

describe("Peer Exchange", () => {
describe("Auto Discovery", function () {
Expand Down
Loading

0 comments on commit 97f8262

Please sign in to comment.