mirror of
https://github.com/waku-org/js-waku.git
synced 2025-02-18 15:26:50 +00:00
Merge #6
6: Add more tests r=D4nte a=D4nte Co-authored-by: Franck Royer <franck@royer.one>
This commit is contained in:
commit
46869e998d
14
.github/workflows/ci.yml
vendored
14
.github/workflows/ci.yml
vendored
@ -35,9 +35,12 @@ jobs:
|
|||||||
# This would have been done part of npm pretest but it gives better
|
# This would have been done part of npm pretest but it gives better
|
||||||
# visibility in the CI if done as a separate step
|
# visibility in the CI if done as a separate step
|
||||||
- name: Build wakunode2
|
- name: Build wakunode2
|
||||||
if: steps.cache-nim-waku.outputs.cache-hit != 'true'
|
|
||||||
shell: bash
|
shell: bash
|
||||||
run: cd nim-waku && make wakunode2
|
run: cd nim-waku && ./build/wakunode2 --help || make wakunode2
|
||||||
|
|
||||||
|
- name: Ensure wakunode2 is ready
|
||||||
|
shell: bash
|
||||||
|
run: cd nim-waku && ./build/wakunode2 --help
|
||||||
|
|
||||||
- name: Install bufbuild
|
- name: Install bufbuild
|
||||||
uses: mu-io/setup-buf@v1beta
|
uses: mu-io/setup-buf@v1beta
|
||||||
@ -68,3 +71,10 @@ jobs:
|
|||||||
|
|
||||||
- name: test
|
- name: test
|
||||||
run: npm run test
|
run: npm run test
|
||||||
|
|
||||||
|
- name: Upload logs on failure
|
||||||
|
uses: actions/upload-artifact@v2
|
||||||
|
if: failure()
|
||||||
|
with:
|
||||||
|
name: nim-waku-logs
|
||||||
|
path: log/
|
||||||
|
@ -3,8 +3,10 @@ import Mplex from 'libp2p-mplex';
|
|||||||
import { bytes } from 'libp2p-noise/dist/src/@types/basic';
|
import { bytes } from 'libp2p-noise/dist/src/@types/basic';
|
||||||
import { Noise } from 'libp2p-noise/dist/src/noise';
|
import { Noise } from 'libp2p-noise/dist/src/noise';
|
||||||
import TCP from 'libp2p-tcp';
|
import TCP from 'libp2p-tcp';
|
||||||
|
import Multiaddr from 'multiaddr';
|
||||||
|
import PeerId from 'peer-id';
|
||||||
|
|
||||||
import { WakuRelay, WakuRelayPubsub } from './waku_relay';
|
import { CODEC, WakuRelay, WakuRelayPubsub } from './waku_relay';
|
||||||
|
|
||||||
export default class Waku {
|
export default class Waku {
|
||||||
private constructor(public libp2p: Libp2p, public relay: WakuRelay) {}
|
private constructor(public libp2p: Libp2p, public relay: WakuRelay) {}
|
||||||
@ -35,6 +37,11 @@ export default class Waku {
|
|||||||
return new Waku(libp2p, new WakuRelay(libp2p.pubsub));
|
return new Waku(libp2p, new WakuRelay(libp2p.pubsub));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async dialWithMultiAddr(peerId: PeerId, multiaddr: Multiaddr[]) {
|
||||||
|
this.libp2p.peerStore.addressBook.set(peerId, multiaddr);
|
||||||
|
await this.libp2p.dialProtocol(peerId, CODEC);
|
||||||
|
}
|
||||||
|
|
||||||
async stop() {
|
async stop() {
|
||||||
await this.libp2p.stop();
|
await this.libp2p.stop();
|
||||||
}
|
}
|
||||||
|
@ -10,134 +10,244 @@ import { Message } from './waku_message';
|
|||||||
import { CODEC, TOPIC } from './waku_relay';
|
import { CODEC, TOPIC } from './waku_relay';
|
||||||
|
|
||||||
describe('Waku Relay', () => {
|
describe('Waku Relay', () => {
|
||||||
// TODO: Fix this, see https://github.com/ChainSafe/js-libp2p-gossipsub/issues/151
|
afterEach(function () {
|
||||||
it.skip('Publish', async () => {
|
if (this.currentTest!.state === 'failed') {
|
||||||
const message = Message.fromUtf8String('Bird bird bird, bird is the word!');
|
console.log(`Test failed, log file name is ${makeLogFileName(this)}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const [waku1, waku2] = await Promise.all([
|
let waku1: Waku;
|
||||||
|
let waku2: Waku;
|
||||||
|
beforeEach(async function () {
|
||||||
|
[waku1, waku2] = await Promise.all([
|
||||||
Waku.create(NOISE_KEY_1),
|
Waku.create(NOISE_KEY_1),
|
||||||
Waku.create(NOISE_KEY_2),
|
Waku.create(NOISE_KEY_2),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Add node's 2 data to the PeerStore
|
await waku1.dialWithMultiAddr(waku2.libp2p.peerId, waku2.libp2p.multiaddrs);
|
||||||
waku1.libp2p.peerStore.addressBook.set(
|
|
||||||
waku2.libp2p.peerId,
|
|
||||||
waku2.libp2p.multiaddrs
|
|
||||||
);
|
|
||||||
await waku1.libp2p.dial(waku2.libp2p.peerId);
|
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
new Promise((resolve) =>
|
||||||
|
waku1.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
|
||||||
|
),
|
||||||
|
new Promise((resolve) =>
|
||||||
|
waku2.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
|
||||||
|
await waku1.relay.subscribe();
|
||||||
await waku2.relay.subscribe();
|
await waku2.relay.subscribe();
|
||||||
await new Promise((resolve) =>
|
|
||||||
waku2.libp2p.pubsub.once('pubsub:subscription-change', (...args) =>
|
|
||||||
resolve(args)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Setup the promise before publishing to ensure the event is not missed
|
await Promise.all([
|
||||||
const promise = waitForNextData(waku1.libp2p.pubsub);
|
new Promise((resolve) =>
|
||||||
|
waku1.libp2p.pubsub.once('pubsub:subscription-change', (...args) =>
|
||||||
await waku2.relay.publish(message);
|
resolve(args)
|
||||||
|
)
|
||||||
const node1Received = await promise;
|
),
|
||||||
|
new Promise((resolve) =>
|
||||||
expect(node1Received.isEqualTo(message)).to.be.true;
|
waku2.libp2p.pubsub.once('pubsub:subscription-change', (...args) =>
|
||||||
|
resolve(args)
|
||||||
await Promise.all([waku1.stop(), waku2.stop()]);
|
)
|
||||||
|
),
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Registers waku relay protocol', async function () {
|
afterEach(async function () {
|
||||||
const waku = await Waku.create(NOISE_KEY_1);
|
await waku1.stop();
|
||||||
|
await waku2.stop();
|
||||||
|
});
|
||||||
|
|
||||||
const protocols = Array.from(waku.libp2p.upgrader.protocols.keys());
|
it('Subscribe', async function () {
|
||||||
|
const subscribers1 = waku1.libp2p.pubsub.getSubscribers(TOPIC);
|
||||||
|
const subscribers2 = waku2.libp2p.pubsub.getSubscribers(TOPIC);
|
||||||
|
|
||||||
|
expect(subscribers1).to.contain(waku2.libp2p.peerId.toB58String());
|
||||||
|
expect(subscribers2).to.contain(waku1.libp2p.peerId.toB58String());
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Register correct protocols', async function () {
|
||||||
|
const protocols = Array.from(waku1.libp2p.upgrader.protocols.keys());
|
||||||
|
|
||||||
expect(protocols).to.contain(CODEC);
|
expect(protocols).to.contain(CODEC);
|
||||||
|
expect(protocols.findIndex((value) => value.match(/sub/))).to.eq(-1);
|
||||||
await waku.stop();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Does not register any sub protocol', async function () {
|
// TODO: Fix this
|
||||||
const waku = await Waku.create(NOISE_KEY_1);
|
it.skip('Publish', async function () {
|
||||||
|
this.timeout(10000);
|
||||||
|
|
||||||
const protocols = Array.from(waku.libp2p.upgrader.protocols.keys());
|
const message = Message.fromUtf8String('JS to JS communication works');
|
||||||
expect(protocols.findIndex((value) => value.match(/sub/))).to.eq(-1);
|
// waku.libp2p.pubsub.globalSignaturePolicy = 'StrictSign';
|
||||||
|
|
||||||
await waku.stop();
|
const receivedPromise = waitForNextData(waku2.libp2p.pubsub);
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
new Promise((resolve) =>
|
||||||
|
waku1.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
|
||||||
|
),
|
||||||
|
new Promise((resolve) =>
|
||||||
|
waku2.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
|
||||||
|
await waku1.relay.publish(message);
|
||||||
|
|
||||||
|
const receivedMsg = await receivedPromise;
|
||||||
|
|
||||||
|
expect(receivedMsg.contentTopic).to.eq(message.contentTopic);
|
||||||
|
expect(receivedMsg.version).to.eq(message.version);
|
||||||
|
|
||||||
|
const payload = Buffer.from(receivedMsg.payload!);
|
||||||
|
expect(Buffer.compare(payload, message.payload!)).to.eq(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Interop: Nim', function () {
|
describe('Interop: Nim', function () {
|
||||||
let waku: Waku;
|
describe('Nim connects to js', function () {
|
||||||
let nimWaku: NimWaku;
|
let waku: Waku;
|
||||||
|
let nimWaku: NimWaku;
|
||||||
|
|
||||||
beforeEach(async function () {
|
beforeEach(async function () {
|
||||||
this.timeout(10_000);
|
this.timeout(10_000);
|
||||||
waku = await Waku.create(NOISE_KEY_1);
|
waku = await Waku.create(NOISE_KEY_1);
|
||||||
|
|
||||||
const peerId = waku.libp2p.peerId.toB58String();
|
const peerId = waku.libp2p.peerId.toB58String();
|
||||||
const localMultiaddr = waku.libp2p.multiaddrs.find((addr) =>
|
const localMultiaddr = waku.libp2p.multiaddrs.find((addr) =>
|
||||||
addr.toString().match(/127\.0\.0\.1/)
|
addr.toString().match(/127\.0\.0\.1/)
|
||||||
);
|
);
|
||||||
const multiAddrWithId = localMultiaddr + '/p2p/' + peerId;
|
const multiAddrWithId = localMultiaddr + '/p2p/' + peerId;
|
||||||
|
|
||||||
nimWaku = new NimWaku(makeLogFileName(this));
|
nimWaku = new NimWaku(makeLogFileName(this));
|
||||||
await nimWaku.start({ staticnode: multiAddrWithId });
|
await nimWaku.start({ staticnode: multiAddrWithId });
|
||||||
|
|
||||||
await waku.relay.subscribe();
|
await waku.relay.subscribe();
|
||||||
await new Promise((resolve) =>
|
await new Promise((resolve) =>
|
||||||
waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
|
waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
|
||||||
);
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async function () {
|
||||||
|
nimWaku ? nimWaku.stop() : null;
|
||||||
|
waku ? await waku.stop() : null;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('nim subscribes to js', async function () {
|
||||||
|
const nimPeerId = await nimWaku.getPeerId();
|
||||||
|
const subscribers = waku.libp2p.pubsub.getSubscribers(TOPIC);
|
||||||
|
|
||||||
|
expect(subscribers).to.contain(nimPeerId.toB58String());
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Js publishes to nim', async function () {
|
||||||
|
this.timeout(5000);
|
||||||
|
|
||||||
|
const message = Message.fromUtf8String('This is a message');
|
||||||
|
|
||||||
|
await waku.relay.publish(message);
|
||||||
|
|
||||||
|
await nimWaku.waitForLog('WakuMessage received');
|
||||||
|
|
||||||
|
const msgs = await nimWaku.messages();
|
||||||
|
|
||||||
|
expect(msgs[0].contentTopic).to.equal(message.contentTopic);
|
||||||
|
expect(msgs[0].version).to.equal(message.version);
|
||||||
|
|
||||||
|
const payload = Buffer.from(msgs[0].payload);
|
||||||
|
expect(Buffer.compare(payload, message.payload!)).to.equal(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Nim publishes to js', async function () {
|
||||||
|
this.timeout(5000);
|
||||||
|
const message = Message.fromUtf8String('Here is another message.');
|
||||||
|
|
||||||
|
await waku.relay.subscribe();
|
||||||
|
|
||||||
|
await new Promise((resolve) =>
|
||||||
|
waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
|
||||||
|
);
|
||||||
|
|
||||||
|
const receivedPromise = waitForNextData(waku.libp2p.pubsub);
|
||||||
|
|
||||||
|
await nimWaku.sendMessage(message);
|
||||||
|
|
||||||
|
const receivedMsg = await receivedPromise;
|
||||||
|
|
||||||
|
expect(receivedMsg.contentTopic).to.eq(message.contentTopic);
|
||||||
|
expect(receivedMsg.version).to.eq(message.version);
|
||||||
|
|
||||||
|
const payload = Buffer.from(receivedMsg.payload!);
|
||||||
|
expect(Buffer.compare(payload, message.payload!)).to.eq(0);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(async function () {
|
describe('Js connects to nim', function () {
|
||||||
nimWaku ? nimWaku.stop() : null;
|
let waku: Waku;
|
||||||
waku ? await waku.stop() : null;
|
let nimWaku: NimWaku;
|
||||||
});
|
|
||||||
|
|
||||||
it('nim subscribes to js', async function () {
|
beforeEach(async function () {
|
||||||
const nimPeerId = await nimWaku.getPeerId();
|
this.timeout(10_000);
|
||||||
const subscribers = waku.libp2p.pubsub.getSubscribers(TOPIC);
|
waku = await Waku.create(NOISE_KEY_1);
|
||||||
|
|
||||||
expect(subscribers).to.contain(nimPeerId.toB58String());
|
nimWaku = new NimWaku(makeLogFileName(this));
|
||||||
});
|
await nimWaku.start();
|
||||||
|
|
||||||
it('Js publishes to nim', async function () {
|
const nimPeerId = await nimWaku.getPeerId();
|
||||||
this.timeout(5000);
|
|
||||||
|
|
||||||
const message = Message.fromUtf8String('This is a message');
|
await waku.dialWithMultiAddr(nimPeerId, [nimWaku.multiaddr]);
|
||||||
|
|
||||||
await waku.relay.publish(message);
|
await new Promise((resolve) =>
|
||||||
|
waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
|
||||||
|
);
|
||||||
|
|
||||||
await nimWaku.waitForLog('WakuMessage received');
|
await waku.relay.subscribe();
|
||||||
|
|
||||||
const msgs = await nimWaku.messages();
|
await new Promise((resolve) =>
|
||||||
|
waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
expect(msgs[0].contentTopic).to.equal(message.contentTopic);
|
afterEach(async function () {
|
||||||
expect(msgs[0].version).to.equal(message.version);
|
nimWaku ? nimWaku.stop() : null;
|
||||||
|
waku ? await waku.stop() : null;
|
||||||
|
});
|
||||||
|
|
||||||
const payload = Buffer.from(msgs[0].payload);
|
it('nim subscribes to js', async function () {
|
||||||
expect(Buffer.compare(payload, message.payload!)).to.equal(0);
|
const subscribers = waku.libp2p.pubsub.getSubscribers(TOPIC);
|
||||||
});
|
|
||||||
|
|
||||||
it('Nim publishes to js', async function () {
|
const nimPeerId = await nimWaku.getPeerId();
|
||||||
this.timeout(5000);
|
expect(subscribers).to.contain(nimPeerId.toB58String());
|
||||||
const message = Message.fromUtf8String('Here is another message.');
|
});
|
||||||
|
|
||||||
await waku.relay.subscribe();
|
it('Js publishes to nim', async function () {
|
||||||
|
const message = Message.fromUtf8String('This is a message');
|
||||||
|
|
||||||
await new Promise((resolve) =>
|
await waku.relay.publish(message);
|
||||||
waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
|
|
||||||
);
|
|
||||||
|
|
||||||
const receivedPromise = waitForNextData(waku.libp2p.pubsub);
|
await nimWaku.waitForLog('WakuMessage received');
|
||||||
|
|
||||||
await nimWaku.sendMessage(message);
|
const msgs = await nimWaku.messages();
|
||||||
|
|
||||||
const receivedMsg = await receivedPromise;
|
expect(msgs[0].contentTopic).to.equal(message.contentTopic);
|
||||||
|
expect(msgs[0].version).to.equal(message.version);
|
||||||
|
|
||||||
expect(receivedMsg.contentTopic).to.eq(message.contentTopic);
|
const payload = Buffer.from(msgs[0].payload);
|
||||||
expect(receivedMsg.version).to.eq(message.version);
|
expect(Buffer.compare(payload, message.payload!)).to.equal(0);
|
||||||
|
});
|
||||||
|
|
||||||
const payload = Buffer.from(receivedMsg.payload!);
|
it('Nim publishes to js', async function () {
|
||||||
expect(Buffer.compare(payload, message.payload!)).to.eq(0);
|
const message = Message.fromUtf8String('Here is another message.');
|
||||||
|
|
||||||
|
const receivedPromise = waitForNextData(waku.libp2p.pubsub);
|
||||||
|
|
||||||
|
await nimWaku.sendMessage(message);
|
||||||
|
|
||||||
|
const receivedMsg = await receivedPromise;
|
||||||
|
|
||||||
|
expect(receivedMsg.contentTopic).to.eq(message.contentTopic);
|
||||||
|
expect(receivedMsg.version).to.eq(message.version);
|
||||||
|
|
||||||
|
const payload = Buffer.from(receivedMsg.payload!);
|
||||||
|
expect(Buffer.compare(payload, message.payload!)).to.eq(0);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -33,6 +33,7 @@ export interface Args {
|
|||||||
|
|
||||||
export class NimWaku {
|
export class NimWaku {
|
||||||
private process?: ChildProcess;
|
private process?: ChildProcess;
|
||||||
|
private pid?: number;
|
||||||
private portsShift: number;
|
private portsShift: number;
|
||||||
private peerId?: PeerId;
|
private peerId?: PeerId;
|
||||||
private logPath: string;
|
private logPath: string;
|
||||||
@ -42,7 +43,7 @@ export class NimWaku {
|
|||||||
this.logPath = `${LOG_DIR}/nim-waku_${logName}.log`;
|
this.logPath = `${LOG_DIR}/nim-waku_${logName}.log`;
|
||||||
}
|
}
|
||||||
|
|
||||||
async start(args: Args) {
|
async start(args?: Args) {
|
||||||
try {
|
try {
|
||||||
await existsAsync(LOG_DIR);
|
await existsAsync(LOG_DIR);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -70,21 +71,38 @@ export class NimWaku {
|
|||||||
logFile, // stderr
|
logFile, // stderr
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
this.pid = this.process.pid;
|
||||||
|
console.log(
|
||||||
|
`nim-waku ${
|
||||||
|
this.process.pid
|
||||||
|
} started at ${new Date().toLocaleTimeString()}`
|
||||||
|
);
|
||||||
|
|
||||||
this.process.on('exit', (signal) => {
|
this.process.on('exit', (signal) => {
|
||||||
if (signal != 0) {
|
console.log(
|
||||||
console.log(`nim-waku process exited with ${signal}`);
|
`nim-waku ${
|
||||||
}
|
this.process ? this.process.pid : this.pid
|
||||||
|
} process exited with ${signal} at ${new Date().toLocaleTimeString()}`
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.process.on('error', (err) => {
|
this.process.on('error', (err) => {
|
||||||
console.log(`nim-waku process encountered an error: ${err}`);
|
console.log(
|
||||||
|
`nim-waku ${
|
||||||
|
this.process ? this.process.pid : this.pid
|
||||||
|
} process encountered an error: ${err} at ${new Date().toLocaleTimeString()}`
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.waitForLog('RPC Server started');
|
await this.waitForLog('RPC Server started');
|
||||||
}
|
}
|
||||||
|
|
||||||
public stop() {
|
public stop() {
|
||||||
|
console.log(
|
||||||
|
`nim-waku ${
|
||||||
|
this.process ? this.process.pid : this.pid
|
||||||
|
} getting SIGINT at ${new Date().toLocaleTimeString()}`
|
||||||
|
);
|
||||||
this.process ? this.process.kill('SIGINT') : null;
|
this.process ? this.process.kill('SIGINT') : null;
|
||||||
this.process = undefined;
|
this.process = undefined;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user