Merge pull request #657 from status-im/nim-waku-v0.9

This commit is contained in:
Franck R 2022-04-06 13:39:44 +10:00 committed by GitHub
commit 249d9bf1a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 224 additions and 202 deletions

View File

@ -61,6 +61,7 @@
"muxer",
"mvps",
"nodekey",
"nwaku",
"opendns",
"peerhave",
"portfinder",

View File

@ -12,7 +12,7 @@ jobs:
build_and_test:
env:
BUF_VERSION: '0.56.0'
NIM_WAKU_VERSION: 'v0.7'
NIM_WAKU_VERSION: 'v0.9'
strategy:
matrix:
node: [16]
@ -37,7 +37,10 @@ jobs:
- name: Ensure wakunode2 is ready
shell: bash
run: cd nim-waku && ./build/wakunode2 --help
run: |
uname -a
cd nim-waku/build
./wakunode2 --help
- name: Install bufbuild
uses: mathematic-inc/setup-buf@v2beta

@ -1 +1 @@
Subproject commit dba82b6c9c9a8982608f52955356a2df8efcaf9e
Subproject commit 97f23cd5fa89220912819fc5868c5162d222e72e

View File

@ -12,7 +12,7 @@ export enum Fleet {
/**
* Return list of pre-defined (hardcoded) bootstrap nodes.
*
* Default behavior is to return nodes of the nim-waku Status Prod fleet.
* Default behavior is to return nodes of the nwaku Status Prod fleet.
*
* @param fleet The fleet to be returned. Defaults to production fleet.
* @param wantedNumber The number of connections desired. Defaults to [[DefaultWantedNumber]].

View File

@ -4,9 +4,9 @@ import PeerId from "peer-id";
import {
makeLogFileName,
NimWaku,
NOISE_KEY_1,
NOISE_KEY_2,
Nwaku,
} from "../test_utils/";
import { Protocols, Waku } from "./waku";
@ -18,20 +18,20 @@ const dbg = debug("waku:test");
const TestContentTopic = "/test/1/waku/utf8";
describe("Waku Dial [node only]", function () {
describe("Interop: Nim", function () {
describe("Interop: nwaku", function () {
let waku: Waku;
let nimWaku: NimWaku;
let nwaku: Nwaku;
afterEach(async function () {
!!nimWaku && nimWaku.stop();
!!nwaku && nwaku.stop();
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
});
it("js connects to nim", async function () {
it("connects to nwaku", async function () {
this.timeout(20_000);
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start();
const multiAddrWithId = await nimWaku.getMultiaddrWithId();
nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start();
const multiAddrWithId = await nwaku.getMultiaddrWithId();
waku = await Waku.create({
staticNoiseKey: NOISE_KEY_1,
@ -39,26 +39,26 @@ describe("Waku Dial [node only]", function () {
await waku.dial(multiAddrWithId);
await waku.waitForRemotePeer([Protocols.Relay]);
const nimPeerId = await nimWaku.getPeerId();
const nimPeerId = await nwaku.getPeerId();
expect(await waku.libp2p.peerStore.has(nimPeerId)).to.be.true;
});
});
describe("Bootstrap", function () {
let waku: Waku;
let nimWaku: NimWaku;
let nwaku: Nwaku;
afterEach(async function () {
!!nimWaku && nimWaku.stop();
!!nwaku && nwaku.stop();
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
});
it("Passing an array", async function () {
this.timeout(10_000);
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start();
const multiAddrWithId = await nimWaku.getMultiaddrWithId();
nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start();
const multiAddrWithId = await nwaku.getMultiaddrWithId();
waku = await Waku.create({
staticNoiseKey: NOISE_KEY_1,
@ -77,14 +77,14 @@ describe("Waku Dial [node only]", function () {
it("Passing a function", async function () {
this.timeout(10_000);
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start();
nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start();
waku = await Waku.create({
staticNoiseKey: NOISE_KEY_1,
bootstrap: {
getPeers: async () => {
return [await nimWaku.getMultiaddrWithId()];
return [await nwaku.getMultiaddrWithId()];
},
},
});
@ -95,7 +95,7 @@ describe("Waku Dial [node only]", function () {
});
});
const multiAddrWithId = await nimWaku.getMultiaddrWithId();
const multiAddrWithId = await nwaku.getMultiaddrWithId();
expect(connectedPeerID.toB58String()).to.eq(multiAddrWithId.getPeerId());
});
});
@ -168,18 +168,18 @@ describe("Decryption Keys", () => {
describe("Wait for remote peer / get peers", function () {
let waku: Waku;
let nimWaku: NimWaku;
let nwaku: Nwaku;
afterEach(async function () {
!!nimWaku && nimWaku.stop();
!!nwaku && nwaku.stop();
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
});
it("Relay", async function () {
this.timeout(20_000);
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start();
const multiAddrWithId = await nimWaku.getMultiaddrWithId();
nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start();
const multiAddrWithId = await nwaku.getMultiaddrWithId();
dbg("Create");
waku = await Waku.create({
@ -199,9 +199,9 @@ describe("Wait for remote peer / get peers", function () {
it("Store", async function () {
this.timeout(20_000);
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start({ persistMessages: true });
const multiAddrWithId = await nimWaku.getMultiaddrWithId();
nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ persistMessages: true });
const multiAddrWithId = await nwaku.getMultiaddrWithId();
waku = await Waku.create({
staticNoiseKey: NOISE_KEY_1,
@ -222,9 +222,9 @@ describe("Wait for remote peer / get peers", function () {
it("LightPush", async function () {
this.timeout(20_000);
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start({ lightpush: true });
const multiAddrWithId = await nimWaku.getMultiaddrWithId();
nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ lightpush: true });
const multiAddrWithId = await nwaku.getMultiaddrWithId();
waku = await Waku.create({
staticNoiseKey: NOISE_KEY_1,

View File

@ -1,7 +1,7 @@
import { expect } from "chai";
import debug from "debug";
import { makeLogFileName, NimWaku, NOISE_KEY_1 } from "../../test_utils";
import { makeLogFileName, NOISE_KEY_1, Nwaku } from "../../test_utils";
import { delay } from "../../test_utils/delay";
import { Protocols, Waku } from "../waku";
import { WakuMessage } from "../waku_message";
@ -12,23 +12,23 @@ const TestContentTopic = "/test/1/waku-light-push/utf8";
describe("Waku Light Push [node only]", () => {
let waku: Waku;
let nimWaku: NimWaku;
let nwaku: Nwaku;
afterEach(async function () {
!!nimWaku && nimWaku.stop();
!!nwaku && nwaku.stop();
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
});
it("Push successfully", async function () {
this.timeout(5_000);
this.timeout(15_000);
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start({ lightpush: true });
nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ lightpush: true });
waku = await Waku.create({
staticNoiseKey: NOISE_KEY_1,
});
await waku.dial(await nimWaku.getMultiaddrWithId());
await waku.dial(await nwaku.getMultiaddrWithId());
await waku.waitForRemotePeer([Protocols.LightPush]);
const messageText = "Light Push works!";
@ -44,7 +44,7 @@ describe("Waku Light Push [node only]", () => {
while (msgs.length === 0) {
await delay(200);
msgs = await nimWaku.messages();
msgs = await nwaku.messages();
}
expect(msgs[0].contentTopic).to.equal(message.contentTopic);
@ -53,21 +53,21 @@ describe("Waku Light Push [node only]", () => {
});
it("Push on custom pubsub topic", async function () {
this.timeout(5_000);
this.timeout(15_000);
const customPubSubTopic = "/waku/2/custom-dapp/proto";
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start({ lightpush: true, topics: customPubSubTopic });
nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ lightpush: true, topics: customPubSubTopic });
waku = await Waku.create({
pubSubTopic: customPubSubTopic,
staticNoiseKey: NOISE_KEY_1,
});
await waku.dial(await nimWaku.getMultiaddrWithId());
await waku.dial(await nwaku.getMultiaddrWithId());
await waku.waitForRemotePeer([Protocols.LightPush]);
const nimPeerId = await nimWaku.getPeerId();
const nimPeerId = await nwaku.getPeerId();
const messageText = "Light Push works!";
const message = await WakuMessage.fromUtf8String(
@ -84,10 +84,10 @@ describe("Waku Light Push [node only]", () => {
let msgs: WakuMessage[] = [];
dbg("Waiting for message to show on nim-waku side");
dbg("Waiting for message to show in nwaku");
while (msgs.length === 0) {
await delay(200);
msgs = await nimWaku.messages();
msgs = await nwaku.messages();
}
expect(msgs[0].contentTopic).to.equal(message.contentTopic);

View File

@ -3,8 +3,8 @@ import debug from "debug";
import {
makeLogFileName,
NimWaku,
NOISE_KEY_1,
Nwaku,
WakuRelayMessage,
} from "../../test_utils";
import { delay } from "../../test_utils/delay";
@ -24,9 +24,9 @@ const dbg = debug("waku:test:message");
const TestContentTopic = "/test/1/waku-message/utf8";
describe("Waku Message [node only]", function () {
describe("Interop: Nim", function () {
describe("Interop: nwaku", function () {
let waku: Waku;
let nimWaku: NimWaku;
let nwaku: Nwaku;
beforeEach(async function () {
this.timeout(30_000);
@ -34,27 +34,27 @@ describe("Waku Message [node only]", function () {
staticNoiseKey: NOISE_KEY_1,
});
nimWaku = new NimWaku(makeLogFileName(this));
dbg("Starting nim-waku node");
await nimWaku.start({ rpcPrivate: true });
nwaku = new Nwaku(makeLogFileName(this));
dbg("Starting nwaku node");
await nwaku.start({ rpcPrivate: true });
dbg("Dialing to nim-waku node");
await waku.dial(await nimWaku.getMultiaddrWithId());
dbg("Dialing to nwaku node");
await waku.dial(await nwaku.getMultiaddrWithId());
dbg("Wait for remote peer");
await waku.waitForRemotePeer([Protocols.Relay]);
dbg("Remote peer ready");
// As this test uses the nim-waku RPC API, we somehow often face
// Race conditions where the nim-waku node does not have the js-waku
// As this test uses the nwaku RPC API, we somehow often face
// Race conditions where the nwaku node does not have the js-waku
// Node in its relay mesh just yet.
await delay(500);
});
afterEach(async function () {
!!nimWaku && nimWaku.stop();
!!nwaku && nwaku.stop();
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
});
it("JS decrypts nim message [asymmetric, no signature]", async function () {
it("Decrypts nwaku message [asymmetric, no signature]", async function () {
this.timeout(5000);
const messageText = "Here is an encrypted message.";
@ -77,7 +77,7 @@ describe("Waku Message [node only]", function () {
const publicKey = getPublicKey(privateKey);
dbg("Post message");
const res = await nimWaku.postAsymmetricMessage(message, publicKey);
const res = await nwaku.postAsymmetricMessage(message, publicKey);
expect(res).to.be.true;
const receivedMsg = await receivedMsgPromise;
@ -87,11 +87,11 @@ describe("Waku Message [node only]", function () {
expect(receivedMsg.payloadAsUtf8).to.eq(messageText);
});
it("Js encrypts message for nim [asymmetric, no signature]", async function () {
it("Encrypts message for nwaku [asymmetric, no signature]", async function () {
this.timeout(5000);
dbg("Ask nim-waku to generate asymmetric key pair");
const keyPair = await nimWaku.getAsymmetricKeyPair();
dbg("Ask nwaku to generate asymmetric key pair");
const keyPair = await nwaku.getAsymmetricKeyPair();
const privateKey = hexToBytes(keyPair.privateKey);
const publicKey = hexToBytes(keyPair.publicKey);
@ -111,9 +111,9 @@ describe("Waku Message [node only]", function () {
let msgs: WakuRelayMessage[] = [];
while (msgs.length === 0) {
dbg("Wait for message to be seen by nim-waku");
dbg("Wait for message to be seen by nwaku");
await delay(200);
msgs = await nimWaku.getAsymmetricMessages(privateKey);
msgs = await nwaku.getAsymmetricMessages(privateKey);
}
dbg("Check message content");
@ -121,7 +121,7 @@ describe("Waku Message [node only]", function () {
expect(bytesToUtf8(hexToBytes(msgs[0].payload))).to.equal(messageText);
});
it("JS decrypts nim message [symmetric, no signature]", async function () {
it("Decrypts nwaku message [symmetric, no signature]", async function () {
this.timeout(5000);
const messageText = "Here is a message encrypted in a symmetric manner.";
@ -143,8 +143,8 @@ describe("Waku Message [node only]", function () {
}
);
dbg("Post message using nim-waku");
await nimWaku.postSymmetricMessage(message, symKey);
dbg("Post message using nwaku");
await nwaku.postSymmetricMessage(message, symKey);
dbg("Wait for message to be received by js-waku");
const receivedMsg = await receivedMsgPromise;
dbg("Message received by js-waku");
@ -154,11 +154,11 @@ describe("Waku Message [node only]", function () {
expect(receivedMsg.payloadAsUtf8).to.eq(messageText);
});
it("Js encrypts message for nim [symmetric, no signature]", async function () {
it("Encrypts message for nwaku [symmetric, no signature]", async function () {
this.timeout(5000);
dbg("Getting symmetric key from nim-waku");
const symKey = await nimWaku.getSymmetricKey();
dbg("Getting symmetric key from nwaku");
const symKey = await nwaku.getSymmetricKey();
dbg("Encrypting message with js-waku");
const messageText =
"This is a message I am going to encrypt with a symmetric key";
@ -176,8 +176,8 @@ describe("Waku Message [node only]", function () {
while (msgs.length === 0) {
await delay(200);
dbg("Getting messages from nim-waku");
msgs = await nimWaku.getSymmetricMessages(symKey);
dbg("Getting messages from nwaku");
msgs = await nwaku.getSymmetricMessages(symKey);
}
expect(msgs[0].contentTopic).to.equal(message.contentTopic);

View File

@ -3,9 +3,9 @@ import debug from "debug";
import {
makeLogFileName,
NimWaku,
NOISE_KEY_1,
NOISE_KEY_2,
Nwaku,
} from "../../test_utils";
import { delay } from "../../test_utils/delay";
import { DefaultPubSubTopic, Protocols, Waku } from "../waku";
@ -306,9 +306,9 @@ describe("Waku Relay [node only]", () => {
});
});
describe("Interop: Nim", function () {
describe("Interop: nwaku", function () {
let waku: Waku;
let nimWaku: NimWaku;
let nwaku: Nwaku;
beforeEach(async function () {
this.timeout(30_000);
@ -316,19 +316,19 @@ describe("Waku Relay [node only]", () => {
staticNoiseKey: NOISE_KEY_1,
});
nimWaku = new NimWaku(this.test?.ctx?.currentTest?.title + "");
await nimWaku.start();
nwaku = new Nwaku(this.test?.ctx?.currentTest?.title + "");
await nwaku.start();
await waku.dial(await nimWaku.getMultiaddrWithId());
await waku.dial(await nwaku.getMultiaddrWithId());
await waku.waitForRemotePeer([Protocols.Relay]);
});
afterEach(async function () {
!!nimWaku && nimWaku.stop();
!!nwaku && nwaku.stop();
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
});
it("nim subscribes to js", async function () {
it("nwaku subscribes", async function () {
let subscribers: string[] = [];
while (subscribers.length === 0) {
@ -336,11 +336,11 @@ describe("Waku Relay [node only]", () => {
subscribers = waku.libp2p.pubsub.getSubscribers(DefaultPubSubTopic);
}
const nimPeerId = await nimWaku.getPeerId();
const nimPeerId = await nwaku.getPeerId();
expect(subscribers).to.contain(nimPeerId.toB58String());
});
it("Js publishes to nim", async function () {
it("Publishes to nwaku", async function () {
this.timeout(30000);
const messageText = "This is a message";
@ -356,7 +356,7 @@ describe("Waku Relay [node only]", () => {
while (msgs.length === 0) {
console.log("Waiting for messages");
await delay(200);
msgs = await nimWaku.messages();
msgs = await nwaku.messages();
}
expect(msgs[0].contentTopic).to.equal(message.contentTopic);
@ -364,7 +364,7 @@ describe("Waku Relay [node only]", () => {
expect(msgs[0].payloadAsUtf8).to.equal(messageText);
});
it("Nim publishes to js", async function () {
it("Nwaku publishes", async function () {
await delay(200);
const messageText = "Here is another message.";
@ -379,7 +379,7 @@ describe("Waku Relay [node only]", () => {
}
);
await nimWaku.sendMessage(message);
await nwaku.sendMessage(Nwaku.toWakuRelayMessage(message));
const receivedMsg = await receivedMsgPromise;
@ -388,13 +388,13 @@ describe("Waku Relay [node only]", () => {
expect(receivedMsg.payloadAsUtf8).to.eq(messageText);
});
describe.skip("js to nim to js", function () {
describe.skip("Two nodes connected to nwaku", function () {
let waku1: Waku;
let waku2: Waku;
let nimWaku: NimWaku;
let nwaku: Nwaku;
afterEach(async function () {
!!nimWaku && nimWaku.stop();
!!nwaku && nwaku.stop();
!!waku1 &&
waku1.stop().catch((e) => console.log("Waku failed to stop", e));
!!waku2 &&
@ -412,13 +412,13 @@ describe("Waku Relay [node only]", () => {
}),
]);
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start();
nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start();
const nimWakuMultiaddr = await nimWaku.getMultiaddrWithId();
const nwakuMultiaddr = await nwaku.getMultiaddrWithId();
await Promise.all([
waku1.dial(nimWakuMultiaddr),
waku2.dial(nimWakuMultiaddr),
waku1.dial(nwakuMultiaddr),
waku2.dial(nwakuMultiaddr),
]);
// Wait for identify protocol to finish

View File

@ -3,9 +3,9 @@ import debug from "debug";
import {
makeLogFileName,
NimWaku,
NOISE_KEY_1,
NOISE_KEY_2,
Nwaku,
} from "../../test_utils";
import { delay } from "../../test_utils/delay";
import { Protocols, Waku } from "../waku";
@ -24,31 +24,33 @@ const TestContentTopic = "/test/1/waku-store/utf8";
describe("Waku Store", () => {
let waku: Waku;
let nimWaku: NimWaku;
let nwaku: Nwaku;
afterEach(async function () {
!!nimWaku && nimWaku.stop();
!!nwaku && nwaku.stop();
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
});
it("Retrieves history", async function () {
this.timeout(5_000);
this.timeout(15_000);
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start({ persistMessages: true });
nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ persistMessages: true });
for (let i = 0; i < 2; i++) {
expect(
await nimWaku.sendMessage(
await nwaku.sendMessage(
Nwaku.toWakuRelayMessage(
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic)
)
)
).to.be.true;
}
waku = await Waku.create({
staticNoiseKey: NOISE_KEY_1,
});
await waku.dial(await nimWaku.getMultiaddrWithId());
await waku.dial(await nwaku.getMultiaddrWithId());
await waku.waitForRemotePeer([Protocols.Store]);
const messages = await waku.store.queryHistory([]);
@ -60,25 +62,27 @@ describe("Waku Store", () => {
});
it("Retrieves history using callback", async function () {
this.timeout(10_000);
this.timeout(15_000);
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start({ persistMessages: true });
nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ persistMessages: true });
const totalMsgs = 20;
for (let i = 0; i < totalMsgs; i++) {
expect(
await nimWaku.sendMessage(
await nwaku.sendMessage(
Nwaku.toWakuRelayMessage(
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic)
)
)
).to.be.true;
}
waku = await Waku.create({
staticNoiseKey: NOISE_KEY_1,
});
await waku.dial(await nimWaku.getMultiaddrWithId());
await waku.dial(await nwaku.getMultiaddrWithId());
await waku.waitForRemotePeer([Protocols.Store]);
let messages: WakuMessage[] = [];
@ -97,25 +101,27 @@ describe("Waku Store", () => {
});
it("Retrieval aborts when callback returns true", async function () {
this.timeout(5_000);
this.timeout(15_000);
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start({ persistMessages: true });
nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ persistMessages: true });
const availMsgs = 20;
for (let i = 0; i < availMsgs; i++) {
expect(
await nimWaku.sendMessage(
await nwaku.sendMessage(
Nwaku.toWakuRelayMessage(
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic)
)
)
).to.be.true;
}
waku = await Waku.create({
staticNoiseKey: NOISE_KEY_1,
});
await waku.dial(await nimWaku.getMultiaddrWithId());
await waku.dial(await nwaku.getMultiaddrWithId());
await waku.waitForRemotePeer([Protocols.Store]);
let messages: WakuMessage[] = [];
@ -133,23 +139,25 @@ describe("Waku Store", () => {
});
it("Retrieves all historical elements in chronological order through paging", async function () {
this.timeout(5_000);
this.timeout(15_000);
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start({ persistMessages: true });
nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ persistMessages: true });
for (let i = 0; i < 15; i++) {
expect(
await nimWaku.sendMessage(
await nwaku.sendMessage(
Nwaku.toWakuRelayMessage(
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic)
)
)
).to.be.true;
}
waku = await Waku.create({
staticNoiseKey: NOISE_KEY_1,
});
await waku.dial(await nimWaku.getMultiaddrWithId());
await waku.dial(await nwaku.getMultiaddrWithId());
await waku.waitForRemotePeer([Protocols.Store]);
const messages = await waku.store.queryHistory([], {
@ -167,16 +175,18 @@ describe("Waku Store", () => {
});
it("Retrieves history using custom pubsub topic", async function () {
this.timeout(5_000);
this.timeout(15_000);
const customPubSubTopic = "/waku/2/custom-dapp/proto";
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start({ persistMessages: true, topics: customPubSubTopic });
nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ persistMessages: true, topics: customPubSubTopic });
for (let i = 0; i < 2; i++) {
expect(
await nimWaku.sendMessage(
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic),
await nwaku.sendMessage(
Nwaku.toWakuRelayMessage(
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic)
),
customPubSubTopic
)
).to.be.true;
@ -186,10 +196,10 @@ describe("Waku Store", () => {
pubSubTopic: customPubSubTopic,
staticNoiseKey: NOISE_KEY_1,
});
await waku.dial(await nimWaku.getMultiaddrWithId());
await waku.dial(await nwaku.getMultiaddrWithId());
await waku.waitForRemotePeer([Protocols.Store]);
const nimPeerId = await nimWaku.getPeerId();
const nimPeerId = await nwaku.getPeerId();
const messages = await waku.store.queryHistory([], {
peerId: nimPeerId,
@ -203,10 +213,10 @@ describe("Waku Store", () => {
});
it("Retrieves history with asymmetric & symmetric encrypted messages", async function () {
this.timeout(10_000);
this.timeout(15_000);
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start({ persistMessages: true, lightpush: true });
nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ persistMessages: true, lightpush: true });
const encryptedAsymmetricMessageText =
"This message is encrypted for me using asymmetric";
@ -256,7 +266,7 @@ describe("Waku Store", () => {
Waku.create({
staticNoiseKey: NOISE_KEY_2,
}),
nimWaku.getMultiaddrWithId(),
nwaku.getMultiaddrWithId(),
]);
dbg("Waku nodes created");
@ -266,7 +276,7 @@ describe("Waku Store", () => {
waku2.dial(nimWakuMultiaddr),
]);
dbg("Waku nodes connected to nim Waku");
dbg("Waku nodes connected to nwaku");
let lightPushPeerFound = false;
while (!lightPushPeerFound) {
@ -312,10 +322,10 @@ describe("Waku Store", () => {
});
it("Retrieves history with asymmetric & symmetric encrypted messages on different content topics", async function () {
this.timeout(10_000);
this.timeout(15_000);
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start({ persistMessages: true, lightpush: true });
nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ persistMessages: true, lightpush: true });
const encryptedAsymmetricMessageText =
"This message is encrypted for me using asymmetric";
@ -374,7 +384,7 @@ describe("Waku Store", () => {
Waku.create({
staticNoiseKey: NOISE_KEY_2,
}),
nimWaku.getMultiaddrWithId(),
nwaku.getMultiaddrWithId(),
]);
dbg("Waku nodes created");
@ -384,7 +394,7 @@ describe("Waku Store", () => {
waku2.dial(nimWakuMultiaddr),
]);
dbg("Waku nodes connected to nim Waku");
dbg("Waku nodes connected to nwaku");
let lightPushPeerFound = false;
while (!lightPushPeerFound) {
@ -433,49 +443,57 @@ describe("Waku Store", () => {
});
it("Retrieves history using start and end time", async function () {
this.timeout(5_000);
this.timeout(15_000);
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start({ persistMessages: true });
nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ persistMessages: true });
const now = new Date();
const startTime = new Date();
// Set start time 5 minutes in the past
startTime.setTime(now.getTime() - 5 * 60 * 1000);
const message1Timestamp = new Date();
message1Timestamp.setTime(startTime.getTime() + 60 * 1000);
// Set first message was 4 minutes in the past
message1Timestamp.setTime(now.getTime() - 4 * 60 * 1000);
const message2Timestamp = new Date();
message2Timestamp.setTime(startTime.getTime() + 2 * 60 * 1000);
// Set second message 2 minutes in the past
message2Timestamp.setTime(now.getTime() - 2 * 60 * 1000);
const messageTimestamps = [message1Timestamp, message2Timestamp];
const endTime = new Date();
endTime.setTime(startTime.getTime() + 3 * 60 * 1000);
// Set end time 1 minute in the past
endTime.setTime(now.getTime() - 60 * 1000);
let firstMessageTime;
for (let i = 0; i < 2; i++) {
expect(
await nimWaku.sendMessage(
await nwaku.sendMessage(
Nwaku.toWakuRelayMessage(
await WakuMessage.fromUtf8String(`Message ${i}`, TestContentTopic, {
timestamp: messageTimestamps[i],
})
)
)
).to.be.true;
if (!firstMessageTime) firstMessageTime = Date.now() / 1000;
}
waku = await Waku.create({
staticNoiseKey: NOISE_KEY_1,
});
await waku.dial(await nimWaku.getMultiaddrWithId());
await waku.dial(await nwaku.getMultiaddrWithId());
await waku.waitForRemotePeer([Protocols.Store]);
const nimPeerId = await nimWaku.getPeerId();
const nwakuPeerId = await nwaku.getPeerId();
const firstMessage = await waku.store.queryHistory([], {
peerId: nimPeerId,
peerId: nwakuPeerId,
timeFilter: { startTime, endTime: message1Timestamp },
});
const bothMessages = await waku.store.queryHistory([], {
peerId: nimPeerId,
peerId: nwakuPeerId,
timeFilter: {
startTime,
endTime,

View File

@ -8,4 +8,4 @@
export * from "./async_fs";
export * from "./constants";
export * from "./log_file";
export * from "./nim_waku";
export * from "./nwaku";

View File

@ -1,5 +1,5 @@
/**
* Utilities to make it help check nim-waku logs.
* Utilities to make it help check nwaku logs.
*
* @hidden
* @module

View File

@ -1,8 +1,8 @@
import { expect } from "chai";
import { argsToArray, bytesToHex, defaultArgs, strToHex } from "./nim_waku";
import { argsToArray, bytesToHex, defaultArgs, strToHex } from "./nwaku";
describe("nim_waku", () => {
describe("nwaku", () => {
it("Correctly serialized arguments", function () {
const args = defaultArgs();
Object.assign(args, { portsShift: 42 });

View File

@ -19,7 +19,7 @@ import * as proto from "../proto/waku/v2/message";
import { existsAsync, mkdirAsync, openAsync } from "./async_fs";
import waitForLine from "./log_file";
const dbg = debug("waku:nim-waku");
const dbg = debug("waku:nwaku");
const NIM_WAKU_DIR = appRoot + "/nim-waku";
const NIM_WAKU_BIN = NIM_WAKU_DIR + "/build/wakunode2";
@ -62,12 +62,12 @@ export interface KeyPair {
}
export interface WakuRelayMessage {
payload: string;
payload: string; // Hex encoded data string without `0x` prefix.
contentTopic?: string;
timestamp?: number; // Float in seconds
timestamp?: number; // Unix epoch time in nanoseconds as a 64-bits integer value.
}
export class NimWaku {
export class Nwaku {
private process?: ChildProcess;
private pid?: number;
private peerId?: PeerId;
@ -75,8 +75,29 @@ export class NimWaku {
private readonly logPath: string;
private rpcPort?: number;
/**
* Convert a [[WakuMessage]] to a [[WakuRelayMessage]]. The latter is used
* by the nwaku JSON-RPC API.
*/
static toWakuRelayMessage(message: WakuMessage): WakuRelayMessage {
if (!message.payload) {
throw "Attempting to convert empty message";
}
let timestamp;
if (message.proto.timestamp) {
timestamp = message.proto.timestamp.toNumber();
}
return {
payload: bytesToHex(message.payload),
contentTopic: message.contentTopic,
timestamp,
};
}
constructor(logName: string) {
this.logPath = `${LOG_DIR}/nim-waku_${logName}.log`;
this.logPath = `${LOG_DIR}/nwaku_${logName}.log`;
}
async start(args?: Args): Promise<void> {
@ -117,7 +138,7 @@ export class NimWaku {
);
const argsArray = argsToArray(mergedArgs);
dbg(`nim-waku args: ${argsArray.join(" ")}`);
dbg(`nwaku args: ${argsArray.join(" ")}`);
this.process = spawn(NIM_WAKU_BIN, argsArray, {
cwd: NIM_WAKU_DIR,
stdio: [
@ -128,14 +149,12 @@ export class NimWaku {
});
this.pid = this.process.pid;
dbg(
`nim-waku ${
this.process.pid
} started at ${new Date().toLocaleTimeString()}`
`nwaku ${this.process.pid} started at ${new Date().toLocaleTimeString()}`
);
this.process.on("exit", (signal) => {
dbg(
`nim-waku ${
`nwaku ${
this.process ? this.process.pid : this.pid
} process exited with ${signal} at ${new Date().toLocaleTimeString()}`
);
@ -143,23 +162,23 @@ export class NimWaku {
this.process.on("error", (err) => {
console.log(
`nim-waku ${
`nwaku ${
this.process ? this.process.pid : this.pid
} process encountered an error: ${err} at ${new Date().toLocaleTimeString()}`
);
});
dbg("Waiting to see 'Node setup complete' in nim-waku logs");
await this.waitForLog("Node setup complete", 9000);
dbg("nim-waku node has been started");
dbg("Waiting to see 'Node setup complete' in nwaku logs");
await this.waitForLog("Node setup complete", 15000);
dbg("nwaku node has been started");
}
public stop(): void {
const pid = this.process ? this.process.pid : this.pid;
dbg(`nim-waku ${pid} getting SIGINT at ${new Date().toLocaleTimeString()}`);
if (!this.process) throw "nim-waku process not set";
dbg(`nwaku ${pid} getting SIGINT at ${new Date().toLocaleTimeString()}`);
if (!this.process) throw "nwaku process not set";
const res = this.process.kill("SIGINT");
dbg(`nim-waku ${pid} interrupted:`, res);
dbg(`nwaku ${pid} interrupted:`, res);
this.process = undefined;
}
@ -167,9 +186,9 @@ export class NimWaku {
return waitForLine(this.logPath, msg, timeout);
}
/** Calls nim-waku2 JSON-RPC API `get_waku_v2_admin_v1_peers` to check
/** Calls nwaku JSON-RPC API `get_waku_v2_admin_v1_peers` to check
* for known peers
* @throws if nim-waku2 isn't started.
* @throws if nwaku isn't started.
*/
async peers(): Promise<string[]> {
this.checkProcess();
@ -184,33 +203,14 @@ export class NimWaku {
}
async sendMessage(
message: WakuMessage,
message: WakuRelayMessage,
pubSubTopic?: string
): Promise<boolean> {
this.checkProcess();
if (!message.payload) {
throw "Attempting to send empty message";
}
let timestamp;
if (message.timestamp) {
timestamp = message.timestamp.valueOf() / 1000;
if (Number.isInteger(timestamp)) {
// Add a millisecond to ensure it's not an integer
// Until https://github.com/status-im/nim-waku/issues/691 is done
timestamp += 0.001;
}
}
const rpcMessage = {
payload: bytesToHex(message.payload),
contentTopic: message.contentTopic,
timestamp,
};
return this.rpcCall<boolean>("post_waku_v2_relay_v1_message", [
pubSubTopic ? pubSubTopic : DefaultPubSubTopic,
rpcMessage,
message,
]);
}
@ -338,9 +338,9 @@ export class NimWaku {
this.multiaddrWithId = res.listenAddresses
.map((ma) => multiaddr(ma))
.find((ma) => ma.protoNames().includes("ws"));
if (!this.multiaddrWithId) throw "Nim-waku did not return a ws multiaddr";
if (!this.multiaddrWithId) throw "Nwaku did not return a ws multiaddr";
const peerIdStr = this.multiaddrWithId.getPeerId();
if (!peerIdStr) throw "Nim-waku multiaddr does not contain peerId";
if (!peerIdStr) throw "Nwaku multiaddr does not contain peerId";
this.peerId = PeerId.createFromB58String(peerIdStr);
return { peerId: this.peerId, multiaddrWithId: this.multiaddrWithId };
}
@ -370,7 +370,7 @@ export class NimWaku {
private checkProcess(): void {
if (!this.process) {
throw "Nim Waku isn't started";
throw "Nwaku hasn't started";
}
}
}