Implement reception of messages over waku-relay

This commit is contained in:
Franck Royer 2021-03-16 13:39:34 +11:00
parent 57fa974812
commit 953aeea053
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 45 additions and 2 deletions

View File

@ -2,6 +2,7 @@ import test from 'ava';
import Libp2p from 'libp2p'; import Libp2p from 'libp2p';
import Pubsub from 'libp2p-interfaces/src/pubsub'; import Pubsub from 'libp2p-interfaces/src/pubsub';
import { delay } from '../test_utils/delay';
import { NimWaku } from '../test_utils/nim_waku'; import { NimWaku } from '../test_utils/nim_waku';
import { createNode } from './node'; import { createNode } from './node';
@ -126,6 +127,41 @@ test('Nim-interop: js node sends message to nim node', async (t) => {
t.is(Buffer.compare(payload, message.payload), 0); t.is(Buffer.compare(payload, message.payload), 0);
}); });
test('Nim-interop: nim node sends message to js node', async (t) => {
const message = Message.fromUtf8String('Here is another message.');
const node = await createNode();
const wakuRelayNode = new WakuRelay(node.pubsub);
const peerId = node.peerId.toB58String();
const localMultiaddr = node.multiaddrs.find((addr) =>
addr.toString().match(/127\.0\.0\.1/)
);
const multiAddrWithId = localMultiaddr + '/p2p/' + peerId;
const nimWaku = new NimWaku(t.title);
await nimWaku.start({ staticnode: multiAddrWithId });
await patchPeerStore(nimWaku, node);
await wakuRelayNode.subscribe();
await delay(3000);
const receivedPromise = waitForNextData(node.pubsub);
await nimWaku.sendMessage(message);
await delay(3000);
const receivedMsg = await receivedPromise;
t.is(receivedMsg.contentTopic, message.contentTopic);
t.is(receivedMsg.version, message.version);
const payload = Buffer.from(receivedMsg.payload);
t.is(Buffer.compare(payload, message.payload), 0);
});
function waitForNextData(pubsub: Pubsub): Promise<Message> { function waitForNextData(pubsub: Pubsub): Promise<Message> {
return new Promise((resolve) => { return new Promise((resolve) => {
pubsub.once(TOPIC, resolve); pubsub.once(TOPIC, resolve);

View File

@ -26,6 +26,8 @@ export async function createNode() {
emitSelf: true, emitSelf: true,
signMessages: false, signMessages: false,
strictSigning: false, strictSigning: false,
// Ensure that no signature is expected in the messages.
globalSignaturePolicy: 'StrictNoSign',
}, },
}, },
}); });

View File

@ -11,8 +11,13 @@ export const TOPIC = '/waku/2/default-waku/proto';
// This is the class to pass to libp2p as pubsub protocol // This is the class to pass to libp2p as pubsub protocol
export class WakuRelayPubsub extends Gossipsub { export class WakuRelayPubsub extends Gossipsub {
constructor(libp2p: Libp2p) { /**
super(libp2p); *
* @param libp2p: Libp2p
* @param options: Partial<GossipInputOptions>
*/
constructor(libp2p: Libp2p, options?: any) {
super(libp2p, options);
const multicodecs = [CODEC]; const multicodecs = [CODEC];