test: Nim-waku connects to JS (wip)

This commit is contained in:
Franck Royer 2021-03-11 10:54:35 +11:00
parent 4f63bd5835
commit a6c1fae5a6
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 112 additions and 21 deletions

View File

@ -50,15 +50,18 @@ 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();
test('Nim-waku: nim-waku node connects to js node', async (t) => {
const node = await createNode();
console.log(node.peerId.toB58String());
node.peerStore.addressBook.set(nimWaku.peerId, [nimWaku.multiaddr]);
await node.dial(nimWaku.peerId);
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();
await nimWaku.start({ staticnode: multiAddrWithId });
const peers = await nimWaku.peers();

View File

@ -0,0 +1,38 @@
import test from 'ava';
import { argsToArray, mergeArguments } from './nim_waku';
test('Default arguments are correct', (t) => {
const args = mergeArguments({});
const actual = argsToArray(args);
const expected = [
'--nat=none',
'--listen-address=127.0.0.1',
'--relay=true',
'--rpc=true',
'--rpc-admin=true',
'--nodekey=B2C4E3DB22EA6EB6850689F7B3DF3DDA73F59C87EFFD902BEDCEE90A3A2341A6',
];
t.deepEqual(actual, expected);
});
test('Passing staticnode argument return default + static node', (t) => {
const args = mergeArguments({
staticnode: '/ip4/1.1.1.1/tcp/1234/p2p/aabbbccdd',
});
const actual = argsToArray(args);
const expected = [
'--nat=none',
'--listen-address=127.0.0.1',
'--relay=true',
'--rpc=true',
'--rpc-admin=true',
'--nodekey=B2C4E3DB22EA6EB6850689F7B3DF3DDA73F59C87EFFD902BEDCEE90A3A2341A6',
'--staticnode=/ip4/1.1.1.1/tcp/1234/p2p/aabbbccdd',
];
t.deepEqual(actual, expected);
});

View File

@ -1,4 +1,6 @@
import { ChildProcess, spawn } from 'child_process';
import * as fs from 'fs';
import { promisify } from 'util';
import axios from 'axios';
import Multiaddr from 'multiaddr';
@ -7,6 +9,8 @@ import PeerId from 'peer-id';
import { delay } from './delay';
const openAsync = promisify(fs.open);
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(
@ -14,32 +18,43 @@ const NIM_WAKU_PEER_ID = PeerId.createFromB58String(
);
const NIM_WAKKU_LISTEN_ADDR = multiaddr('/ip4/127.0.0.1/tcp/60000/');
export interface Args {
staticnode?: string;
nat?: 'none';
listenAddress?: string;
relay?: boolean;
rpc?: boolean;
rpcAdmin?: boolean;
nodekey?: string;
}
export class NimWaku {
private process?: ChildProcess;
async start() {
async start(args: Args) {
// 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',
const logFile = await openAsync('./nim-waku.log', 'w');
const mergedArgs = argsToArray(mergeArguments(args));
console.log(mergedArgs);
this.process = spawn(NIM_WAKU_BIN, mergedArgs, {
cwd: '/home/froyer/src/status-im/nim-waku/',
stdio: [
'ignore', // stdin
logFile, // stdout
logFile, // stderr
],
{ cwd: '/home/froyer/src/status-im/nim-waku/' }
);
});
this.process.on('exit', (signal) => {
console.log(`nim-waku stopped: ${signal}`);
console.log(`ERROR: nim-waku node stopped: ${signal}`);
});
// TODO: Wait for line "RPC Server started "
await delay(2000);
await delay(5000);
console.log(await this.info());
console.log('Nim waku is hopefully started');
}
@ -94,3 +109,38 @@ export class NimWaku {
}
}
}
export function argsToArray(args: Args): Array<string> {
const array = [];
for (const [key, value] of Object.entries(args)) {
// Change the key from camelCase to kebab-case
const kebabKey = key.replace(/([A-Z])/, (_, capital) => {
return '-' + capital.toLowerCase();
});
const arg = `--${kebabKey}=${value}`;
array.push(arg);
}
return array;
}
function defaultArgs(): Args {
return {
nat: 'none',
listenAddress: '127.0.0.1',
relay: true,
rpc: true,
rpcAdmin: true,
nodekey: 'B2C4E3DB22EA6EB6850689F7B3DF3DDA73F59C87EFFD902BEDCEE90A3A2341A6',
};
}
export function mergeArguments(args: Args): Args {
const res = defaultArgs();
Object.assign(res, args);
return res;
}