From a6c1fae5a65d147fdb03e1c71f01f15608cc79f7 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 11 Mar 2021 10:54:35 +1100 Subject: [PATCH] test: Nim-waku connects to JS (wip) --- src/lib/node.spec.ts | 17 ++++--- src/test_utils/nim_waku.spec.ts | 38 ++++++++++++++++ src/test_utils/nim_waku.ts | 78 +++++++++++++++++++++++++++------ 3 files changed, 112 insertions(+), 21 deletions(-) create mode 100644 src/test_utils/nim_waku.spec.ts diff --git a/src/lib/node.spec.ts b/src/lib/node.spec.ts index 3dde03ca11..a231aba856 100644 --- a/src/lib/node.spec.ts +++ b/src/lib/node.spec.ts @@ -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(); diff --git a/src/test_utils/nim_waku.spec.ts b/src/test_utils/nim_waku.spec.ts new file mode 100644 index 0000000000..87167a7204 --- /dev/null +++ b/src/test_utils/nim_waku.spec.ts @@ -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); +}); diff --git a/src/test_utils/nim_waku.ts b/src/test_utils/nim_waku.ts index 8b77fc88e6..f7ea1f34f6 100644 --- a/src/test_utils/nim_waku.ts +++ b/src/test_utils/nim_waku.ts @@ -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 { + 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; +}