2021-03-17 15:34:58 +11:00
|
|
|
import 'jest';
|
2021-03-15 17:29:48 +11:00
|
|
|
import Libp2p from 'libp2p';
|
2021-03-10 14:55:16 +11:00
|
|
|
import Pubsub from 'libp2p-interfaces/src/pubsub';
|
2021-03-10 14:24:23 +11:00
|
|
|
|
2021-03-10 17:39:53 +11:00
|
|
|
import { NimWaku } from '../test_utils/nim_waku';
|
|
|
|
|
2021-03-19 14:40:16 +11:00
|
|
|
import Waku from './waku';
|
2021-03-10 16:22:49 +11:00
|
|
|
import { Message } from './waku_message';
|
2021-03-19 14:40:16 +11:00
|
|
|
import { CODEC, TOPIC } from './waku_relay';
|
2021-03-10 14:24:23 +11:00
|
|
|
|
2021-03-19 10:35:27 +11:00
|
|
|
// TODO: Fix this, see https://github.com/ChainSafe/js-libp2p-gossipsub/issues/151
|
|
|
|
test.skip('Publishes message', async () => {
|
2021-03-12 14:23:21 +11:00
|
|
|
const message = Message.fromUtf8String('Bird bird bird, bird is the word!');
|
2021-03-10 14:24:23 +11:00
|
|
|
|
2021-03-19 14:40:16 +11:00
|
|
|
const [waku1, waku2] = await Promise.all([Waku.create(), Waku.create()]);
|
2021-03-10 14:24:23 +11:00
|
|
|
|
|
|
|
// Add node's 2 data to the PeerStore
|
2021-03-19 14:40:16 +11:00
|
|
|
waku1.libp2p.peerStore.addressBook.set(
|
|
|
|
waku2.libp2p.peerId,
|
|
|
|
waku2.libp2p.multiaddrs
|
|
|
|
);
|
|
|
|
await waku1.libp2p.dial(waku2.libp2p.peerId);
|
2021-03-10 15:15:36 +11:00
|
|
|
|
2021-03-19 14:40:16 +11:00
|
|
|
await waku2.relay.subscribe();
|
2021-03-15 11:35:30 +11:00
|
|
|
await new Promise((resolve) =>
|
2021-03-19 14:40:16 +11:00
|
|
|
waku2.libp2p.pubsub.once('pubsub:subscription-change', (...args) =>
|
|
|
|
resolve(args)
|
|
|
|
)
|
2021-03-15 11:35:30 +11:00
|
|
|
);
|
2021-03-10 14:24:23 +11:00
|
|
|
|
2021-03-10 14:55:16 +11:00
|
|
|
// Setup the promise before publishing to ensure the event is not missed
|
2021-03-19 14:40:16 +11:00
|
|
|
const promise = waitForNextData(waku1.libp2p.pubsub);
|
2021-03-10 14:24:23 +11:00
|
|
|
|
2021-03-19 14:40:16 +11:00
|
|
|
await waku2.relay.publish(message);
|
2021-03-10 14:24:23 +11:00
|
|
|
|
2021-03-10 14:55:16 +11:00
|
|
|
const node1Received = await promise;
|
2021-03-10 14:24:23 +11:00
|
|
|
|
2021-03-17 15:34:58 +11:00
|
|
|
expect(node1Received.isEqualTo(message)).toBeTruthy();
|
2021-03-10 14:24:23 +11:00
|
|
|
});
|
2021-03-10 14:55:16 +11:00
|
|
|
|
2021-03-17 15:34:58 +11:00
|
|
|
test('Registers waku relay protocol', async () => {
|
2021-03-19 14:40:16 +11:00
|
|
|
const waku = await Waku.create();
|
2021-03-10 15:08:27 +11:00
|
|
|
|
2021-03-19 14:40:16 +11:00
|
|
|
const protocols = Array.from(waku.libp2p.upgrader.protocols.keys());
|
2021-03-10 15:08:27 +11:00
|
|
|
|
2021-03-17 15:34:58 +11:00
|
|
|
expect(protocols.findIndex((value) => value == CODEC)).toBeTruthy();
|
2021-03-10 15:08:27 +11:00
|
|
|
});
|
|
|
|
|
2021-03-17 15:34:58 +11:00
|
|
|
test('Does not register any sub protocol', async () => {
|
2021-03-19 14:40:16 +11:00
|
|
|
const waku = await Waku.create();
|
2021-03-10 16:25:54 +11:00
|
|
|
|
2021-03-19 14:40:16 +11:00
|
|
|
const protocols = Array.from(waku.libp2p.upgrader.protocols.keys());
|
2021-03-17 15:34:58 +11:00
|
|
|
expect(protocols.findIndex((value) => value.match(/sub/))).toBeTruthy();
|
2021-03-10 16:25:54 +11:00
|
|
|
});
|
|
|
|
|
2021-03-17 15:34:58 +11:00
|
|
|
test('Nim-interop: nim-waku node connects to js node', async () => {
|
2021-03-19 14:40:16 +11:00
|
|
|
const waku = await Waku.create();
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2021-03-19 14:40:16 +11:00
|
|
|
const peerId = waku.libp2p.peerId.toB58String();
|
2021-03-11 10:54:35 +11:00
|
|
|
|
2021-03-19 14:40:16 +11:00
|
|
|
const localMultiaddr = waku.libp2p.multiaddrs.find((addr) =>
|
2021-03-11 10:54:35 +11:00
|
|
|
addr.toString().match(/127\.0\.0\.1/)
|
|
|
|
);
|
|
|
|
const multiAddrWithId = localMultiaddr + '/p2p/' + peerId;
|
|
|
|
|
2021-03-17 15:34:58 +11:00
|
|
|
const nimWaku = new NimWaku(expect.getState().currentTestName);
|
2021-03-15 13:25:14 +11:00
|
|
|
await nimWaku.start({ staticnode: multiAddrWithId });
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2021-03-11 15:02:29 +11:00
|
|
|
const nimPeers = await nimWaku.peers();
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2021-03-17 15:34:58 +11:00
|
|
|
expect(nimPeers).toEqual([
|
2021-03-11 15:02:29 +11:00
|
|
|
{
|
|
|
|
multiaddr: multiAddrWithId,
|
|
|
|
protocol: CODEC,
|
|
|
|
connected: true,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
2021-03-12 17:08:42 +11:00
|
|
|
const nimPeerId = await nimWaku.getPeerId();
|
2021-03-19 14:40:16 +11:00
|
|
|
const jsPeers = waku.libp2p.peerStore.peers;
|
2021-03-11 15:02:29 +11:00
|
|
|
|
2021-03-17 15:34:58 +11:00
|
|
|
expect(jsPeers.has(nimPeerId.toB58String())).toBeTruthy();
|
2021-03-10 17:39:53 +11:00
|
|
|
});
|
|
|
|
|
2021-03-17 15:34:58 +11:00
|
|
|
test('Nim-interop: js node receives default subscription from nim node', async () => {
|
2021-03-19 14:40:16 +11:00
|
|
|
const waku = await Waku.create();
|
2021-03-12 09:46:47 +11:00
|
|
|
|
2021-03-19 14:40:16 +11:00
|
|
|
const peerId = waku.libp2p.peerId.toB58String();
|
2021-03-12 09:46:47 +11:00
|
|
|
|
2021-03-19 14:40:16 +11:00
|
|
|
const localMultiaddr = waku.libp2p.multiaddrs.find((addr) =>
|
2021-03-12 09:46:47 +11:00
|
|
|
addr.toString().match(/127\.0\.0\.1/)
|
|
|
|
);
|
|
|
|
const multiAddrWithId = localMultiaddr + '/p2p/' + peerId;
|
|
|
|
|
2021-03-17 15:34:58 +11:00
|
|
|
const nimWaku = new NimWaku(expect.getState().currentTestName);
|
2021-03-15 13:25:14 +11:00
|
|
|
await nimWaku.start({ staticnode: multiAddrWithId });
|
2021-03-12 09:46:47 +11:00
|
|
|
|
2021-03-12 17:08:42 +11:00
|
|
|
const nimPeerId = await nimWaku.getPeerId();
|
2021-03-19 14:40:16 +11:00
|
|
|
const subscribers = waku.libp2p.pubsub.getSubscribers(TOPIC);
|
2021-03-12 09:46:47 +11:00
|
|
|
|
2021-03-17 15:34:58 +11:00
|
|
|
expect(subscribers).toContain(nimPeerId.toB58String());
|
2021-03-12 09:46:47 +11:00
|
|
|
});
|
|
|
|
|
2021-03-17 15:34:58 +11:00
|
|
|
test('Nim-interop: js node sends message to nim node', async () => {
|
2021-03-15 13:27:50 +11:00
|
|
|
const message = Message.fromUtf8String('This is a message');
|
2021-03-19 14:40:16 +11:00
|
|
|
const waku = await Waku.create();
|
2021-03-12 10:35:50 +11:00
|
|
|
|
2021-03-16 15:41:44 +11:00
|
|
|
// TODO: nim-waku does 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
|
2021-03-19 14:40:16 +11:00
|
|
|
waku.libp2p.pubsub.globalSignaturePolicy = 'StrictSign';
|
2021-03-16 15:41:44 +11:00
|
|
|
|
2021-03-19 14:40:16 +11:00
|
|
|
const peerId = waku.libp2p.peerId.toB58String();
|
|
|
|
const localMultiaddr = waku.libp2p.multiaddrs.find((addr) =>
|
2021-03-12 10:35:50 +11:00
|
|
|
addr.toString().match(/127\.0\.0\.1/)
|
|
|
|
);
|
|
|
|
const multiAddrWithId = localMultiaddr + '/p2p/' + peerId;
|
|
|
|
|
2021-03-17 15:34:58 +11:00
|
|
|
const nimWaku = new NimWaku(expect.getState().currentTestName);
|
2021-03-15 13:27:50 +11:00
|
|
|
await nimWaku.start({ staticnode: multiAddrWithId });
|
2021-03-12 10:35:50 +11:00
|
|
|
|
2021-03-19 14:40:16 +11:00
|
|
|
await patchPeerStore(nimWaku, waku.libp2p);
|
2021-03-15 15:34:56 +11:00
|
|
|
|
2021-03-19 14:40:16 +11:00
|
|
|
await waku.relay.publish(message);
|
2021-03-12 10:35:50 +11:00
|
|
|
|
2021-03-15 13:27:50 +11:00
|
|
|
await nimWaku.waitForLog('WakuMessage received');
|
2021-03-12 10:35:50 +11:00
|
|
|
|
2021-03-15 13:27:50 +11:00
|
|
|
const msgs = await nimWaku.messages();
|
2021-03-12 10:35:50 +11:00
|
|
|
|
2021-03-17 15:34:58 +11:00
|
|
|
expect(msgs[0].contentTopic).toEqual(message.contentTopic);
|
|
|
|
expect(msgs[0].version).toEqual(message.version);
|
2021-03-12 10:35:50 +11:00
|
|
|
|
2021-03-15 13:27:50 +11:00
|
|
|
const payload = Buffer.from(msgs[0].payload);
|
2021-03-17 15:34:58 +11:00
|
|
|
expect(Buffer.compare(payload, message.payload)).toBe(0);
|
2021-03-12 10:35:50 +11:00
|
|
|
});
|
|
|
|
|
2021-03-17 15:34:58 +11:00
|
|
|
test('Nim-interop: nim node sends message to js node', async () => {
|
2021-03-16 13:39:34 +11:00
|
|
|
const message = Message.fromUtf8String('Here is another message.');
|
2021-03-19 14:40:16 +11:00
|
|
|
const waku = await Waku.create();
|
2021-03-16 13:39:34 +11:00
|
|
|
|
2021-03-19 14:40:16 +11:00
|
|
|
const peerId = waku.libp2p.peerId.toB58String();
|
|
|
|
const localMultiaddr = waku.libp2p.multiaddrs.find((addr) =>
|
2021-03-16 13:39:34 +11:00
|
|
|
addr.toString().match(/127\.0\.0\.1/)
|
|
|
|
);
|
|
|
|
const multiAddrWithId = localMultiaddr + '/p2p/' + peerId;
|
|
|
|
|
2021-03-17 15:34:58 +11:00
|
|
|
const nimWaku = new NimWaku(expect.getState().currentTestName);
|
2021-03-16 13:39:34 +11:00
|
|
|
await nimWaku.start({ staticnode: multiAddrWithId });
|
|
|
|
|
2021-03-19 14:40:16 +11:00
|
|
|
await patchPeerStore(nimWaku, waku.libp2p);
|
2021-03-16 13:39:34 +11:00
|
|
|
|
2021-03-19 14:40:16 +11:00
|
|
|
await waku.relay.subscribe();
|
2021-03-16 13:39:34 +11:00
|
|
|
|
2021-03-16 13:45:18 +11:00
|
|
|
await new Promise((resolve) =>
|
2021-03-19 14:40:16 +11:00
|
|
|
waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
|
2021-03-16 13:45:18 +11:00
|
|
|
);
|
2021-03-16 13:39:34 +11:00
|
|
|
|
2021-03-19 14:40:16 +11:00
|
|
|
const receivedPromise = waitForNextData(waku.libp2p.pubsub);
|
2021-03-16 13:39:34 +11:00
|
|
|
|
|
|
|
await nimWaku.sendMessage(message);
|
|
|
|
|
|
|
|
const receivedMsg = await receivedPromise;
|
|
|
|
|
2021-03-17 15:34:58 +11:00
|
|
|
expect(receivedMsg.contentTopic).toBe(message.contentTopic);
|
|
|
|
expect(receivedMsg.version).toBe(message.version);
|
2021-03-16 13:39:34 +11:00
|
|
|
|
|
|
|
const payload = Buffer.from(receivedMsg.payload);
|
2021-03-17 15:34:58 +11:00
|
|
|
expect(Buffer.compare(payload, message.payload)).toBe(0);
|
2021-03-16 13:39:34 +11:00
|
|
|
});
|
|
|
|
|
2021-03-10 16:22:49 +11:00
|
|
|
function waitForNextData(pubsub: Pubsub): Promise<Message> {
|
2021-03-10 14:55:16 +11:00
|
|
|
return new Promise((resolve) => {
|
2021-03-10 15:15:36 +11:00
|
|
|
pubsub.once(TOPIC, resolve);
|
2021-03-10 14:56:12 +11:00
|
|
|
}).then((msg: any) => {
|
2021-03-10 16:22:49 +11:00
|
|
|
return Message.fromBinary(msg.data);
|
2021-03-10 14:55:16 +11:00
|
|
|
});
|
|
|
|
}
|
2021-03-15 17:29:48 +11:00
|
|
|
|
|
|
|
// TODO: Remove this hack, tracked with https://github.com/status-im/nim-waku/issues/419
|
|
|
|
async function patchPeerStore(nimWaku: NimWaku, node: Libp2p) {
|
|
|
|
const nimPeerId = await nimWaku.getPeerId();
|
|
|
|
node.identifyService!.peerStore.protoBook.set(nimPeerId, [CODEC]);
|
|
|
|
const peer = node.peerStore.peers.get(nimPeerId.toB58String());
|
|
|
|
if (!peer) {
|
|
|
|
throw 'Did not find nim-waku node in peers';
|
|
|
|
}
|
|
|
|
peer.protocols = [CODEC];
|
|
|
|
node.peerStore.peers.set(nimPeerId.toB58String(), peer);
|
|
|
|
|
|
|
|
await new Promise((resolve) =>
|
|
|
|
node.pubsub.once('gossipsub:heartbeat', resolve)
|
|
|
|
);
|
|
|
|
}
|