Test connection triggered from js and add API

This commit is contained in:
Franck Royer 2021-03-23 11:14:51 +11:00
parent fdff7c43b8
commit 46c41dc50f
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 102 additions and 9 deletions

View File

@ -37,6 +37,14 @@ export default class Waku {
return new Waku(libp2p, new WakuRelay(libp2p.pubsub));
}
/**
* Dials to the provided peer. If successful, the known metadata of the peer will be added to the nodes peerStore, and the Connection will be returned
* @param peer The peer to dial
*/
async dial(peer: PeerId | Multiaddr | string) {
return this.libp2p.dialProtocol(peer, CODEC);
}
async dialWithMultiAddr(peerId: PeerId, multiaddr: Multiaddr[]) {
this.libp2p.peerStore.addressBook.set(peerId, multiaddr);
await this.libp2p.dialProtocol(peerId, CODEC);

View File

@ -2,6 +2,7 @@ import { expect } from 'chai';
import Pubsub from 'libp2p-interfaces/src/pubsub';
import { NOISE_KEY_1, NOISE_KEY_2 } from '../test_utils/constants';
import { delay } from '../test_utils/delay';
import { makeLogFileName } from '../test_utils/log_file';
import { NimWaku } from '../test_utils/nim_waku';
@ -179,6 +180,80 @@ describe('Waku Relay', () => {
});
});
describe('Js connects to nim', function () {
let waku: Waku;
let nimWaku: NimWaku;
beforeEach(async function () {
this.timeout(10_000);
waku = await Waku.create(NOISE_KEY_1);
nimWaku = new NimWaku(this.test!.ctx!.currentTest!.title);
await nimWaku.start();
await waku.dial(await nimWaku.getMultiaddrWithId());
await delay(100);
await new Promise((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 subscribers = waku.libp2p.pubsub.getSubscribers(TOPIC);
const nimPeerId = await nimWaku.getPeerId();
expect(subscribers).to.contain(nimPeerId.toB58String());
});
it('Js publishes to nim', async function () {
const message = Message.fromUtf8String('This is a message');
// TODO: nim-waku does not really follow the `StrictNoSign` policy hence we need to change
// it for nim-waku to process our messages. Can be removed once
// https://github.com/status-im/nim-waku/issues/422 is fixed
waku.libp2p.pubsub.globalSignaturePolicy = 'StrictSign';
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 () {
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);
});
});
describe('Js connects to nim', function () {
let waku: Waku;
let nimWaku: NimWaku;
@ -190,9 +265,7 @@ describe('Waku Relay', () => {
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start();
const nimPeerId = await nimWaku.getPeerId();
await waku.dialWithMultiAddr(nimPeerId, [nimWaku.multiaddr]);
await waku.dial(await nimWaku.getMultiaddrWithId());
await new Promise((resolve) =>
waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve)

View File

@ -36,6 +36,7 @@ export class NimWaku {
private pid?: number;
private portsShift: number;
private peerId?: PeerId;
private multiaddrWithId?: Multiaddr;
private logPath: string;
constructor(logName: string) {
@ -160,14 +161,25 @@ export class NimWaku {
}
async getPeerId(): Promise<PeerId> {
if (this.peerId) {
return this.peerId;
return await this.setPeerId().then((res) => res.peerId);
}
async getMultiaddrWithId(): Promise<Multiaddr> {
return await this.setPeerId().then((res) => res.multiaddrWithId);
}
private async setPeerId(): Promise<{
peerId: PeerId;
multiaddrWithId: Multiaddr;
}> {
if (this.peerId && this.multiaddrWithId) {
return { peerId: this.peerId, multiaddrWithId: this.multiaddrWithId };
}
const res = await this.info();
const strPeerId = multiaddr(res.listenStr).getPeerId();
return PeerId.createFromB58String(strPeerId);
this.multiaddrWithId = multiaddr(res.listenStr);
const peerIdStr = this.multiaddrWithId.getPeerId();
this.peerId = PeerId.createFromB58String(peerIdStr);
return { peerId: this.peerId, multiaddrWithId: this.multiaddrWithId };
}
get multiaddr(): Multiaddr {