2021-03-10 17:39:53 +11:00
|
|
|
import { ChildProcess, spawn } from 'child_process';
|
2021-03-12 17:08:42 +11:00
|
|
|
import { randomInt } from 'crypto';
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2021-03-15 16:22:26 +11:00
|
|
|
import appRoot from 'app-root-path';
|
2021-03-10 17:39:53 +11:00
|
|
|
import axios from 'axios';
|
|
|
|
|
import Multiaddr from 'multiaddr';
|
|
|
|
|
import multiaddr from 'multiaddr';
|
|
|
|
|
import PeerId from 'peer-id';
|
|
|
|
|
|
2021-03-12 10:35:50 +11:00
|
|
|
import { Message } from '../lib/waku_message';
|
|
|
|
|
import { TOPIC } from '../lib/waku_relay';
|
|
|
|
|
|
2021-03-15 13:38:36 +11:00
|
|
|
import { existsAsync, mkdirAsync, openAsync } from './async_fs';
|
2021-03-11 11:11:37 +11:00
|
|
|
import waitForLine from './log_file';
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2021-03-12 17:08:42 +11:00
|
|
|
const NIM_WAKU_DEFAULT_P2P_PORT = 60000;
|
|
|
|
|
const NIM_WAKU_DEFAULT_RPC_PORT = 8545;
|
2021-03-15 16:22:26 +11:00
|
|
|
const NIM_WAKU_DIR = appRoot + '/nim-waku';
|
|
|
|
|
const NIM_WAKU_BIN = NIM_WAKU_DIR + '/build/wakunode2';
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2021-03-15 13:38:36 +11:00
|
|
|
const LOG_DIR = './log';
|
|
|
|
|
|
2021-03-11 10:54:35 +11:00
|
|
|
export interface Args {
|
|
|
|
|
staticnode?: string;
|
|
|
|
|
nat?: 'none';
|
|
|
|
|
listenAddress?: string;
|
|
|
|
|
relay?: boolean;
|
|
|
|
|
rpc?: boolean;
|
|
|
|
|
rpcAdmin?: boolean;
|
|
|
|
|
nodekey?: string;
|
2021-03-12 17:08:42 +11:00
|
|
|
portsShift?: number;
|
2021-03-11 10:54:35 +11:00
|
|
|
}
|
|
|
|
|
|
2021-03-10 17:39:53 +11:00
|
|
|
export class NimWaku {
|
|
|
|
|
private process?: ChildProcess;
|
2021-03-12 17:08:42 +11:00
|
|
|
private portsShift: number;
|
|
|
|
|
private peerId?: PeerId;
|
2021-03-15 13:25:14 +11:00
|
|
|
private logPath: string;
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2021-03-15 13:25:14 +11:00
|
|
|
constructor(testName: string) {
|
2021-03-12 17:08:42 +11:00
|
|
|
this.portsShift = randomInt(0, 5000);
|
|
|
|
|
|
|
|
|
|
const logFilePrefix = testName.replace(/ /g, '_').replace(/[':()]/g, '');
|
2021-03-10 17:39:53 +11:00
|
|
|
|
2021-03-15 13:38:36 +11:00
|
|
|
this.logPath = `${LOG_DIR}/${logFilePrefix}-nim-waku.log`;
|
2021-03-15 13:25:14 +11:00
|
|
|
}
|
2021-03-11 11:11:37 +11:00
|
|
|
|
2021-03-15 13:25:14 +11:00
|
|
|
async start(args: Args) {
|
2021-03-15 13:38:36 +11:00
|
|
|
try {
|
|
|
|
|
await existsAsync(LOG_DIR);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
try {
|
|
|
|
|
await mkdirAsync(LOG_DIR);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// Looks like 2 tests tried to create the director at the same time,
|
|
|
|
|
// it can be ignored
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-15 13:25:14 +11:00
|
|
|
const logFile = await openAsync(this.logPath, 'w');
|
2021-03-11 10:54:35 +11:00
|
|
|
|
2021-03-12 17:08:42 +11:00
|
|
|
const mergedArgs = defaultArgs();
|
|
|
|
|
|
|
|
|
|
// Object.assign overrides the properties with the source (if there are conflicts)
|
|
|
|
|
Object.assign(mergedArgs, { portsShift: this.portsShift }, args);
|
|
|
|
|
|
|
|
|
|
const argsArray = argsToArray(mergedArgs);
|
|
|
|
|
this.process = spawn(NIM_WAKU_BIN, argsArray, {
|
2021-03-15 16:22:26 +11:00
|
|
|
cwd: NIM_WAKU_DIR,
|
2021-03-11 10:54:35 +11:00
|
|
|
stdio: [
|
|
|
|
|
'ignore', // stdin
|
|
|
|
|
logFile, // stdout
|
|
|
|
|
logFile, // stderr
|
2021-03-10 17:39:53 +11:00
|
|
|
],
|
2021-03-11 10:54:35 +11:00
|
|
|
});
|
2021-03-10 17:39:53 +11:00
|
|
|
|
|
|
|
|
this.process.on('exit', (signal) => {
|
2021-03-11 10:54:35 +11:00
|
|
|
console.log(`ERROR: nim-waku node stopped: ${signal}`);
|
2021-03-10 17:39:53 +11:00
|
|
|
});
|
|
|
|
|
|
2021-03-15 13:25:14 +11:00
|
|
|
await this.waitForLog('RPC Server started');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async waitForLog(msg: string) {
|
|
|
|
|
return waitForLine(this.logPath, msg);
|
2021-03-10 17:39:53 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Calls nim-waku2 JSON-RPC API `get_waku_v2_admin_v1_peers` to check
|
|
|
|
|
* for known peers
|
|
|
|
|
* @throws if nim-waku2 isn't started.
|
|
|
|
|
*/
|
2021-03-11 15:02:29 +11:00
|
|
|
async peers() {
|
2021-03-10 17:39:53 +11:00
|
|
|
this.checkProcess();
|
|
|
|
|
|
|
|
|
|
const res = await this.rpcCall('get_waku_v2_admin_v1_peers', []);
|
|
|
|
|
|
|
|
|
|
return res.result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-12 17:08:42 +11:00
|
|
|
async info(): Promise<RpcInfoResponse> {
|
2021-03-10 17:39:53 +11:00
|
|
|
this.checkProcess();
|
|
|
|
|
|
|
|
|
|
const res = await this.rpcCall('get_waku_v2_debug_v1_info', []);
|
|
|
|
|
|
|
|
|
|
return res.result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-12 10:35:50 +11:00
|
|
|
async sendMessage(message: Message) {
|
|
|
|
|
this.checkProcess();
|
|
|
|
|
|
|
|
|
|
const rpcMessage = {
|
2021-03-15 16:26:07 +11:00
|
|
|
payload: bufToHex(message.payload),
|
2021-03-12 10:35:50 +11:00
|
|
|
contentTopic: message.contentTopic,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const res = await this.rpcCall('post_waku_v2_relay_v1_message', [
|
|
|
|
|
TOPIC,
|
|
|
|
|
rpcMessage,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return res.result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async messages() {
|
|
|
|
|
this.checkProcess();
|
|
|
|
|
|
|
|
|
|
const res = await this.rpcCall('get_waku_v2_relay_v1_messages', [TOPIC]);
|
|
|
|
|
|
|
|
|
|
return res.result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-12 17:08:42 +11:00
|
|
|
async getPeerId(): Promise<PeerId> {
|
|
|
|
|
if (this.peerId) {
|
|
|
|
|
return this.peerId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const res = await this.info();
|
|
|
|
|
const strPeerId = multiaddr(res.listenStr).getPeerId();
|
|
|
|
|
|
|
|
|
|
return PeerId.createFromB58String(strPeerId);
|
2021-03-10 17:39:53 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get multiaddr(): Multiaddr {
|
2021-03-12 17:08:42 +11:00
|
|
|
const port = NIM_WAKU_DEFAULT_P2P_PORT + this.portsShift;
|
|
|
|
|
return multiaddr(`/ip4/127.0.0.1/tcp/${port}/`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get rpcUrl(): string {
|
|
|
|
|
const port = NIM_WAKU_DEFAULT_RPC_PORT + this.portsShift;
|
|
|
|
|
return `http://localhost:${port}/`;
|
2021-03-10 17:39:53 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async rpcCall(method: string, params: any[]) {
|
|
|
|
|
const res = await axios.post(
|
2021-03-12 17:08:42 +11:00
|
|
|
this.rpcUrl,
|
2021-03-10 17:39:53 +11:00
|
|
|
{
|
|
|
|
|
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";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-11 10:54:35 +11:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-12 17:08:42 +11:00
|
|
|
export function defaultArgs(): Args {
|
2021-03-11 10:54:35 +11:00
|
|
|
return {
|
|
|
|
|
nat: 'none',
|
|
|
|
|
listenAddress: '127.0.0.1',
|
|
|
|
|
relay: true,
|
|
|
|
|
rpc: true,
|
|
|
|
|
rpcAdmin: true,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-12 10:44:47 +11:00
|
|
|
export function strToHex(str: string): string {
|
2021-03-12 10:35:50 +11:00
|
|
|
let hex: string;
|
|
|
|
|
try {
|
|
|
|
|
hex = unescape(encodeURIComponent(str))
|
|
|
|
|
.split('')
|
|
|
|
|
.map(function (v) {
|
2021-03-12 10:44:47 +11:00
|
|
|
return v.charCodeAt(0).toString(16);
|
2021-03-12 10:35:50 +11:00
|
|
|
})
|
|
|
|
|
.join('');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
hex = str;
|
|
|
|
|
console.log('invalid text input: ' + str);
|
|
|
|
|
}
|
|
|
|
|
return '0x' + hex;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-12 10:44:47 +11:00
|
|
|
export function bufToHex(buffer: Uint8Array) {
|
|
|
|
|
return (
|
|
|
|
|
'0x' +
|
|
|
|
|
Array.prototype.map
|
|
|
|
|
.call(buffer, (x) => ('00' + x.toString(16)).slice(-2))
|
|
|
|
|
.join('')
|
|
|
|
|
);
|
2021-03-12 10:35:50 +11:00
|
|
|
}
|
2021-03-12 17:08:42 +11:00
|
|
|
|
|
|
|
|
interface RpcInfoResponse {
|
|
|
|
|
// multiaddr including id.
|
|
|
|
|
listenStr: string;
|
|
|
|
|
}
|