test: Connect to nim waku

This commit is contained in:
Franck Royer 2021-03-10 17:39:53 +11:00
parent b8c63355a5
commit 4f63bd5835
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
6 changed files with 145 additions and 10 deletions

29
package-lock.json generated
View File

@ -849,6 +849,15 @@
"integrity": "sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw==",
"dev": true
},
"@types/axios": {
"version": "0.14.0",
"resolved": "https://registry.npmjs.org/@types/axios/-/axios-0.14.0.tgz",
"integrity": "sha1-7CMA++fX3d1+udOr+HmZlkyvzkY=",
"dev": true,
"requires": {
"axios": "*"
}
},
"@types/bl": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/@types/bl/-/bl-2.1.0.tgz",
@ -903,11 +912,6 @@
"dev": true,
"optional": true
},
"@types/prompt-sync": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/@types/prompt-sync/-/prompt-sync-4.1.0.tgz",
"integrity": "sha1-utMynv9bQRXjTvRpgjckTUEdRHA="
},
"@types/retry": {
"version": "0.12.0",
"resolved": "https://registry.npmjs.org/@types/retry/-/retry-0.12.0.tgz",
@ -1733,6 +1737,15 @@
"resolved": "https://registry.npmjs.org/aws4/-/aws4-1.11.0.tgz",
"integrity": "sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA=="
},
"axios": {
"version": "0.21.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
"dev": true,
"requires": {
"follow-redirects": "^1.10.0"
}
},
"balanced-match": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
@ -5018,6 +5031,12 @@
"integrity": "sha512-zAoAQiudy+r5SvnSw3KJy5os/oRJYHzrzja/tBDqrZtNhUw8bt6y8OBzMWcjWr+8liV8Eb6yOhw8WZ7VFZ5ZzA==",
"dev": true
},
"follow-redirects": {
"version": "1.13.3",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.3.tgz",
"integrity": "sha512-DUgl6+HDzB0iEptNQEXLx/KhTmDb8tZUHSeLqpnjpknR70H0nC2t9N73BK6fN4hOvJ84pKlIQVQ4k5FFlBedKA==",
"dev": true
},
"for-in": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz",

View File

@ -45,7 +45,6 @@
},
"dependencies": {
"@bitauth/libauth": "^1.17.1",
"@types/prompt-sync": "^4.1.0",
"debug": "^4.3.1",
"libp2p": "^0.30.0",
"libp2p-gossipsub": "^0.7.0",
@ -60,11 +59,13 @@
"devDependencies": {
"@ava/typescript": "^1.1.1",
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@types/axios": "^0.14.0",
"@types/node": "^14.14.31",
"@typescript-eslint/eslint-plugin": "^4.0.1",
"@typescript-eslint/parser": "^4.0.1",
"ava": "^3.15.0",
"ava-fast-check": "^4.0.2",
"axios": "^0.21.1",
"codecov": "^3.5.0",
"cspell": "^4.1.0",
"cz-conventional-changelog": "^3.3.0",

View File

@ -1,14 +1,13 @@
import test from 'ava';
import Pubsub from 'libp2p-interfaces/src/pubsub';
import { delay } from '../test_utils/delay';
import { NimWaku } from '../test_utils/nim_waku';
import { createNode } from './node';
import { Message } from './waku_message';
import { CODEC, TOPIC, WakuRelay } from './waku_relay';
function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
test('Can publish message', async (t) => {
const message = Message.fromString('Bird bird bird, bird is the word!');
@ -51,6 +50,22 @@ test('Does not register any sub protocol', async (t) => {
t.truthy(protocols.findIndex((value) => value.match(/sub/)));
});
test('Nim-waku: connects', async (t) => {
const nimWaku = new NimWaku();
await nimWaku.start();
const node = await createNode();
console.log(node.peerId.toB58String());
node.peerStore.addressBook.set(nimWaku.peerId, [nimWaku.multiaddr]);
await node.dial(nimWaku.peerId);
const peers = await nimWaku.peers();
console.log(peers);
t.is(peers.length, 1);
});
function waitForNextData(pubsub: Pubsub): Promise<Message> {
return new Promise((resolve) => {
pubsub.once(TOPIC, resolve);

View File

@ -22,6 +22,7 @@ export class WakuRelayPubsub extends Gossipsub {
}
}
// TODO: Implement dial for an address with format '/ip4/127.0.0.1/tcp/60000/p2p/16Uiu2HAkyzsXzENw5XBDYEQQAeQTCYjBJpMLgBmEXuwbtcrgxBJ4'
// This class provides an interface to execute the waku relay protocol
export class WakuRelay {
constructor(private pubsub: Pubsub) {}

3
src/test_utils/delay.ts Normal file
View File

@ -0,0 +1,3 @@
export function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

View File

@ -0,0 +1,96 @@
import { ChildProcess, spawn } from 'child_process';
import axios from 'axios';
import Multiaddr from 'multiaddr';
import multiaddr from 'multiaddr';
import PeerId from 'peer-id';
import { delay } from './delay';
const NIM_WAKU_BIN = '/home/froyer/src/status-im/nim-waku/build/wakunode2';
const NIM_WAKU_RPC_URL = 'http://localhost:8545/';
const NIM_WAKU_PEER_ID = PeerId.createFromB58String(
'16Uiu2HAkyzsXzENw5XBDYEQQAeQTCYjBJpMLgBmEXuwbtcrgxBJ4'
);
const NIM_WAKKU_LISTEN_ADDR = multiaddr('/ip4/127.0.0.1/tcp/60000/');
export class NimWaku {
private process?: ChildProcess;
async start() {
// Start a local only node with the right RPC commands
// The fixed nodekey ensures the node has a fixed Peerid: 16Uiu2HAkyzsXzENw5XBDYEQQAeQTCYjBJpMLgBmEXuwbtcrgxBJ4
this.process = spawn(
NIM_WAKU_BIN,
[
'--nat=none',
'--listen-address=127.0.0.1',
'--relay=true',
'--rpc=true',
'--rpc-admin=true',
'--nodekey=B2C4E3DB22EA6EB6850689F7B3DF3DDA73F59C87EFFD902BEDCEE90A3A2341A6',
],
{ cwd: '/home/froyer/src/status-im/nim-waku/' }
);
this.process.on('exit', (signal) => {
console.log(`nim-waku stopped: ${signal}`);
});
// TODO: Wait for line "RPC Server started "
await delay(2000);
console.log('Nim waku is hopefully started');
}
/** Calls nim-waku2 JSON-RPC API `get_waku_v2_admin_v1_peers` to check
* for known peers
* @throws if nim-waku2 isn't started.
*/
async peers(): Promise<string[]> {
this.checkProcess();
const res = await this.rpcCall('get_waku_v2_admin_v1_peers', []);
return res.result;
}
async info() {
this.checkProcess();
const res = await this.rpcCall('get_waku_v2_debug_v1_info', []);
return res.result;
}
get peerId(): PeerId {
return NIM_WAKU_PEER_ID;
}
get multiaddr(): Multiaddr {
return NIM_WAKKU_LISTEN_ADDR;
}
private async rpcCall(method: string, params: any[]) {
const res = await axios.post(
NIM_WAKU_RPC_URL,
{
jsonrpc: '2.0',
id: 1,
method,
params,
},
{
headers: { 'Content-Type': 'application/json' },
}
);
return res.data;
}
private checkProcess() {
if (!this.process) {
throw "Nim Waku isn't started";
}
}
}