2022-04-20 10:06:48 +10:00
|
|
|
import { expect } from "chai";
|
|
|
|
import { describe } from "mocha";
|
|
|
|
import { Multiaddr } from "multiaddr";
|
2022-04-20 11:15:11 +10:00
|
|
|
import { Protocols, Waku, WakuMessage } from "js-waku";
|
2022-04-20 12:07:40 +10:00
|
|
|
import { v4 as uuidv4 } from "uuid";
|
|
|
|
import delay from "./delay.js";
|
|
|
|
|
2022-04-20 12:14:08 +10:00
|
|
|
export default function runAll(nodes) {
|
|
|
|
describe("Run Waku Test Suite", () => {
|
2022-04-20 11:15:11 +10:00
|
|
|
const wakus = [];
|
2022-04-20 10:06:48 +10:00
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
wakus.forEach((waku) => {
|
|
|
|
waku.stop().catch((e) => console.log("Waku failed to stop", e));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Connect", async function () {
|
2022-04-20 11:15:11 +10:00
|
|
|
this.timeout(20000);
|
2022-04-20 10:06:48 +10:00
|
|
|
|
|
|
|
const peerIds = nodes.map((a) => {
|
|
|
|
const ma = new Multiaddr(a);
|
2022-04-20 11:15:11 +10:00
|
|
|
return ma.getPeerId();
|
2022-04-20 10:06:48 +10:00
|
|
|
});
|
2022-04-20 14:14:41 +10:00
|
|
|
const hostnames = nodes.map((a) => {
|
|
|
|
const ma = new Multiaddr(a);
|
|
|
|
return ma.nodeAddress().address;
|
|
|
|
});
|
2022-04-20 10:06:48 +10:00
|
|
|
|
|
|
|
const promises = nodes.map(async (node, i) => {
|
|
|
|
wakus[i] = await Waku.create({
|
|
|
|
bootstrap: { peers: [node] },
|
|
|
|
});
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
wakus[i].libp2p.connectionManager.on("peer:connect", (connection) => {
|
|
|
|
resolve(connection.remotePeer);
|
|
|
|
});
|
2022-04-20 12:14:08 +10:00
|
|
|
}).then((peerId) => {
|
2022-04-20 10:06:48 +10:00
|
|
|
console.log("connected", peerId.toB58String());
|
2022-04-20 14:14:41 +10:00
|
|
|
expect(peerId.toB58String()).to.eq(
|
|
|
|
peerIds[i],
|
|
|
|
`Could not connect to ${hostnames[i]}`
|
|
|
|
);
|
2022-04-20 10:06:48 +10:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Relay", async function () {
|
|
|
|
this.timeout(60000);
|
|
|
|
|
2022-04-20 12:07:40 +10:00
|
|
|
const id = uuidv4();
|
2022-04-20 10:06:48 +10:00
|
|
|
|
|
|
|
const promises = nodes.map(async (node, i) => {
|
|
|
|
wakus[i] = await Waku.create({
|
|
|
|
bootstrap: { peers: [node] },
|
|
|
|
});
|
|
|
|
|
|
|
|
await wakus[i].waitForRemotePeer([Protocols.Relay]);
|
|
|
|
console.log(node + ": ready");
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
// All connected and relay ready
|
|
|
|
|
2022-04-20 13:47:28 +10:00
|
|
|
const contentTopic = `/waku-tests/1/relay-test-${id}/utf8`;
|
2022-04-20 10:06:48 +10:00
|
|
|
|
2022-04-20 11:15:11 +10:00
|
|
|
const messages = [];
|
2022-04-20 14:14:41 +10:00
|
|
|
const hostnames = nodes.map((a) => {
|
|
|
|
const ma = new Multiaddr(a);
|
|
|
|
return ma.nodeAddress().address;
|
|
|
|
});
|
2022-04-20 10:06:48 +10:00
|
|
|
|
2022-04-20 14:14:41 +10:00
|
|
|
wakus.forEach((waku, i) => {
|
|
|
|
messages[i] = [];
|
2022-04-20 10:06:48 +10:00
|
|
|
waku.relay.addObserver(
|
|
|
|
(message) => {
|
2022-04-20 14:14:41 +10:00
|
|
|
messages[i].push({
|
2022-04-20 10:06:48 +10:00
|
|
|
msg: message.payloadAsUtf8,
|
2022-04-20 11:15:11 +10:00
|
|
|
timestamp: message.timestamp,
|
2022-04-20 10:06:48 +10:00
|
|
|
rcvd: new Date(),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[contentTopic]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const relayPromises = wakus.map(async (waku, i) => {
|
|
|
|
const msg = await WakuMessage.fromUtf8String(
|
2022-04-20 14:14:41 +10:00
|
|
|
`sent via ${hostnames[i]} - ${id}`,
|
2022-04-20 10:06:48 +10:00
|
|
|
contentTopic
|
|
|
|
);
|
2022-05-04 11:18:31 +10:00
|
|
|
return waku.relay.send(msg);
|
2022-04-20 10:06:48 +10:00
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(relayPromises);
|
|
|
|
await delay(30000);
|
|
|
|
|
|
|
|
console.log(messages);
|
|
|
|
|
2022-04-20 14:14:41 +10:00
|
|
|
messages.forEach((msgs) => {
|
|
|
|
msgs.forEach((msg) => {
|
|
|
|
const diff = msg.rcvd.getTime() - msg.timestamp.getTime();
|
|
|
|
console.log(msg.timestamp, msg.rcvd, diff + "ms");
|
|
|
|
});
|
2022-04-20 10:06:48 +10:00
|
|
|
});
|
|
|
|
|
2022-04-20 14:14:41 +10:00
|
|
|
// Checking that message sent by waku[i]
|
2022-04-20 10:06:48 +10:00
|
|
|
for (let i = 0; i < wakus.length; i++) {
|
2022-04-20 14:14:41 +10:00
|
|
|
// is received by waku[j]
|
|
|
|
for (let j = 0; j < wakus.length; j++) {
|
|
|
|
if (i === j) continue;
|
|
|
|
|
|
|
|
expect(messages[j].map((m) => m.msg)).to.contain(
|
|
|
|
`sent via ${hostnames[i]} - ${id}`,
|
|
|
|
`Node connected to ${hostnames[j]} did not receive message sent via ${hostnames[i]}`
|
|
|
|
);
|
|
|
|
}
|
2022-04-20 10:06:48 +10:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-04-20 14:22:21 +10:00
|
|
|
it("Send via Light Push, receive via Relay", async function () {
|
2022-04-20 10:06:48 +10:00
|
|
|
this.timeout(60000);
|
|
|
|
|
2022-04-20 12:07:40 +10:00
|
|
|
const id = uuidv4();
|
2022-04-20 10:06:48 +10:00
|
|
|
|
|
|
|
const promises = nodes.map(async (node, i) => {
|
|
|
|
wakus[i] = await Waku.create({
|
|
|
|
bootstrap: { peers: [node] },
|
|
|
|
});
|
|
|
|
|
2022-04-20 14:22:21 +10:00
|
|
|
await wakus[i].waitForRemotePeer([
|
|
|
|
Protocols.LightPush,
|
|
|
|
Protocols.Relay,
|
|
|
|
]);
|
2022-04-20 10:06:48 +10:00
|
|
|
console.log(node + ": ready");
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
// All connected and relay ready
|
|
|
|
|
2022-04-20 13:47:28 +10:00
|
|
|
const contentTopic = `/waku-tests/1/light-push-${id}/utf8`;
|
2022-04-20 10:06:48 +10:00
|
|
|
|
2022-04-20 11:15:11 +10:00
|
|
|
const messages = [];
|
2022-04-20 14:22:21 +10:00
|
|
|
const hostnames = nodes.map((a) => {
|
|
|
|
const ma = new Multiaddr(a);
|
|
|
|
return ma.nodeAddress().address;
|
|
|
|
});
|
2022-04-20 10:06:48 +10:00
|
|
|
|
2022-04-20 14:22:21 +10:00
|
|
|
wakus.forEach((waku, i) => {
|
|
|
|
messages[i] = [];
|
2022-04-20 10:06:48 +10:00
|
|
|
waku.relay.addObserver(
|
|
|
|
(message) => {
|
2022-04-20 14:22:21 +10:00
|
|
|
messages[i].push({
|
2022-04-20 10:06:48 +10:00
|
|
|
msg: message.payloadAsUtf8,
|
2022-04-20 11:15:11 +10:00
|
|
|
timestamp: message.timestamp,
|
2022-04-20 10:06:48 +10:00
|
|
|
rcvd: new Date(),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[contentTopic]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const relayPromises = wakus.map(async (waku, i) => {
|
|
|
|
const msg = await WakuMessage.fromUtf8String(
|
2022-04-20 14:22:21 +10:00
|
|
|
`sent via ${hostnames[i]} - ${id}`,
|
2022-04-20 10:06:48 +10:00
|
|
|
contentTopic
|
|
|
|
);
|
2022-05-04 11:18:31 +10:00
|
|
|
return waku.lightPush.push(msg);
|
2022-04-20 10:06:48 +10:00
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(relayPromises);
|
2022-04-20 14:22:21 +10:00
|
|
|
await delay(30000);
|
2022-04-20 10:06:48 +10:00
|
|
|
|
|
|
|
console.log(messages);
|
|
|
|
|
2022-04-20 14:22:21 +10:00
|
|
|
messages.forEach((msgs) => {
|
|
|
|
msgs.forEach((msg) => {
|
|
|
|
const diff = msg.rcvd.getTime() - msg.timestamp.getTime();
|
|
|
|
console.log(msg.timestamp, msg.rcvd, diff + "ms");
|
|
|
|
});
|
2022-04-20 10:06:48 +10:00
|
|
|
});
|
|
|
|
|
2022-04-20 14:22:21 +10:00
|
|
|
// Checking that message sent by waku[i]
|
2022-04-20 10:06:48 +10:00
|
|
|
for (let i = 0; i < wakus.length; i++) {
|
2022-04-20 14:22:21 +10:00
|
|
|
// is received by waku[j]
|
|
|
|
for (let j = 0; j < wakus.length; j++) {
|
|
|
|
if (i === j) continue;
|
|
|
|
|
|
|
|
expect(messages[j].map((m) => m.msg)).to.contain(
|
|
|
|
`sent via ${hostnames[i]} - ${id}`,
|
|
|
|
`Node connected to ${hostnames[j]} did not receive message sent via ${hostnames[i]}`
|
|
|
|
);
|
|
|
|
}
|
2022-04-20 10:06:48 +10:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-04-20 14:22:21 +10:00
|
|
|
it("Retrieve from Store, sent via Light push", async function () {
|
2022-04-20 10:06:48 +10:00
|
|
|
this.timeout(30000);
|
|
|
|
|
2022-04-20 12:07:40 +10:00
|
|
|
const id = uuidv4();
|
2022-04-20 10:06:48 +10:00
|
|
|
|
|
|
|
const promises = nodes.map(async (node, i) => {
|
|
|
|
wakus[i] = await Waku.create({
|
|
|
|
bootstrap: { peers: [node] },
|
|
|
|
});
|
|
|
|
|
2022-04-20 14:22:21 +10:00
|
|
|
await wakus[i].waitForRemotePeer([Protocols.Relay, Protocols.Store]);
|
2022-04-20 10:06:48 +10:00
|
|
|
console.log(node + ": ready");
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
// All connected and relay ready
|
|
|
|
|
2022-04-20 14:22:21 +10:00
|
|
|
const hostnames = nodes.map((a) => {
|
|
|
|
const ma = new Multiaddr(a);
|
|
|
|
return ma.nodeAddress().address;
|
|
|
|
});
|
|
|
|
|
2022-04-20 13:47:28 +10:00
|
|
|
const contentTopic = `/waku-tests/1/store-test-${id}/utf8`;
|
2022-04-20 10:06:48 +10:00
|
|
|
|
2022-04-20 14:22:21 +10:00
|
|
|
const pushPromises = wakus.map(async (waku, i) => {
|
2022-04-20 10:06:48 +10:00
|
|
|
const msg = await WakuMessage.fromUtf8String(
|
2022-04-20 13:47:28 +10:00
|
|
|
`sent via ${nodes[i]} - ${id}`,
|
2022-04-20 10:06:48 +10:00
|
|
|
contentTopic
|
|
|
|
);
|
2022-05-04 11:18:31 +10:00
|
|
|
return waku.lightPush.push(msg);
|
2022-04-20 10:06:48 +10:00
|
|
|
});
|
|
|
|
|
2022-04-20 14:22:21 +10:00
|
|
|
await Promise.all(pushPromises);
|
2022-04-20 10:06:48 +10:00
|
|
|
await delay(5000);
|
|
|
|
|
|
|
|
const storePromises = wakus.map(async (waku, index) => {
|
|
|
|
const messages = await waku.store.queryHistory([contentTopic]);
|
|
|
|
const payloads = messages.map((msg) => msg.payloadAsUtf8);
|
|
|
|
console.log(index, payloads);
|
|
|
|
|
|
|
|
for (let i = 0; i < wakus.length; i++) {
|
2022-04-20 14:22:21 +10:00
|
|
|
expect(payloads).to.contain(
|
|
|
|
`sent via ${nodes[i]} - ${id}`,
|
|
|
|
`Store ${hostnames[index]} did not contain message sent via ${hostnames[i]}`
|
|
|
|
);
|
2022-04-20 10:06:48 +10:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(storePromises);
|
|
|
|
});
|
|
|
|
});
|
2022-04-20 12:14:08 +10:00
|
|
|
}
|