Refactor peer CLI to be run from watchers (#331)

* Refactor peer CLI to be run from watchers

* Participate in chat protocol only through chat CLI
This commit is contained in:
prathamesh0
2023-02-27 14:15:26 +05:30
committed by GitHub
parent 90d60f54a6
commit 0400546996
10 changed files with 209 additions and 106 deletions
+7 -3
View File
@@ -43,9 +43,13 @@ A basic CLI to pass messages between peers using `stdin`/`stdout`
```bash
# In packages/cli
yarn chat --relay-node <RELAY_NODE_URL>
yarn chat --relay-multiaddr <RELAY_MULTIADDR> --max-connections [MAX_CONNECTIONS] --dial-timeout [DIAL_TIMEOUT] --max-relay-connections [MAX_RELAY_CONNECTIONS] --peer-id-file [PEER_ID_FILE_PATH]
```
* `relay-node`: multiaddr of a primary hop enabled relay node
* `relay-multiaddr (r)`: multiaddr of a primary hop enabled relay node
* `max-connections`: max number of connections for this peer
* `dial-timeout`: timeout for dial to peers (ms)
* `max-relay-connections`: max number of relay node connections for this peer
* `peer-id-file (f)`: file path for peer id to be used (json)
* The process starts reading from `stdin` and outputs messages from others peers to `stdout`.
* The process starts reading from `stdin` and outputs messages from others peers over the `/chat/1.0.0` protocol to `stdout`.
+1 -1
View File
@@ -8,7 +8,7 @@
"build": "yarn clean && tsc && yarn copy-assets",
"clean": "rm -rf ./dist",
"copy-assets": "copyfiles -u 1 src/**/*.gql dist/",
"chat": "node dist/chat.js"
"chat": "DEBUG='vulcanize:*, laconic:*' node dist/chat.js"
},
"dependencies": {
"@cerc-io/peer": "^0.2.29",
+9 -31
View File
@@ -3,35 +3,25 @@
//
import * as readline from 'readline';
import { hideBin } from 'yargs/helpers';
import yargs from 'yargs';
import debug from 'debug';
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1319854183
import { PeerId } from '@libp2p/interface-peer-id';
import { PeerCmd } from './peer';
const log = debug('vulcanize:chat');
const TEST_TOPIC = 'test';
interface Arguments {
relayNode: string;
}
async function main (): Promise<void> {
const argv: Arguments = _getArgv();
// https://adamcoster.com/blog/commonjs-and-esm-importexport-compatibility-examples#importing-esm-into-commonjs-cjs
const { Peer } = await import('@cerc-io/peer');
const peer = new Peer(argv.relayNode, true);
await peer.init({});
const peerCmd = new PeerCmd();
const peer = await peerCmd.exec(TEST_TOPIC);
peer.subscribeMessage((peerId: PeerId, message: string) => {
console.log(`> ${peerId.toString()} > ${message}`);
log(`> ${peerId.toString()} > ${message}`);
});
peer.subscribeTopic(TEST_TOPIC, (peerId, data) => {
console.log(`> ${peerId.toString()} > ${data}`);
});
console.log(`Peer ID: ${peer.peerId?.toString()}`);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
@@ -41,19 +31,7 @@ async function main (): Promise<void> {
peer.broadcastMessage(input);
});
console.log('Reading input...');
}
function _getArgv (): any {
return yargs(hideBin(process.argv)).parserConfiguration({
'parse-numbers': false
}).options({
relayNode: {
type: 'string',
describe: 'Relay node URL',
demandOption: true
}
}).argv;
log('Reading input...');
}
main().catch(err => {
+1
View File
@@ -14,3 +14,4 @@ export * from './server';
export * from './job-runner';
export * from './index-block';
export * from './fill';
export * from './peer';
+92
View File
@@ -0,0 +1,92 @@
//
// Copyright 2023 Vulcanize, Inc.
//
import { hideBin } from 'yargs/helpers';
import yargs from 'yargs';
import debug from 'debug';
import {
PeerInitConfig,
PeerIdObj
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1319854183
} from '@cerc-io/peer';
import { readPeerId } from './utils';
const log = debug('vulcanize:peer');
interface Arguments {
relayMultiaddr: string;
maxConnections: number;
dialTimeout: number;
maxRelayConnections: number;
peerIdFile: string;
}
export class PeerCmd {
async exec (pubSubTopic?: string, parseLibp2pMessage?: (log: debug.Debugger, peerId: string, data: any) => void): Promise<any> {
const argv: Arguments = _getArgv();
const { Peer } = await import('@cerc-io/peer');
const peer = new Peer(argv.relayMultiaddr, true);
let peerIdObj: PeerIdObj | undefined;
if (argv.peerIdFile) {
peerIdObj = readPeerId(argv.peerIdFile);
}
const peerNodeInit: PeerInitConfig = {
maxConnections: argv.maxConnections,
dialTimeout: argv.dialTimeout,
maxRelayConnections: argv.maxRelayConnections
};
await peer.init(peerNodeInit, peerIdObj);
log(`Peer ID: ${peer.peerId?.toString()}`);
// Subscribe this peer to a pubsub topic if provided
if (pubSubTopic) {
peer.subscribeTopic(pubSubTopic, (peerId, data) => {
if (parseLibp2pMessage) {
parseLibp2pMessage(log, peerId.toString(), data);
} else {
log(`> ${peerId.toString()} > ${data}`);
}
});
}
return peer;
}
}
function _getArgv (): any {
return yargs(hideBin(process.argv)).parserConfiguration({
'parse-numbers': false
}).options({
relayMultiaddr: {
type: 'string',
alias: 'r',
describe: 'Multiaddr of the primary relay node for this peer',
demandOption: true
},
maxConnections: {
type: 'number',
describe: 'Max number of connections for a peer'
},
dialTimeout: {
type: 'number',
describe: 'Timeout for dial to peers (ms)'
},
maxRelayConnections: {
type: 'number',
describe: 'Max number of relay node connections for a peer'
},
peerIdFile: {
type: 'string',
alias: 'f',
describe: 'Peer id file path (json)'
}
}).argv;
}
+3 -3
View File
@@ -105,7 +105,7 @@ export class ServerCmd {
async exec (
createResolvers: (indexer: IndexerInterface, eventWatcher: EventWatcher) => Promise<any>,
typeDefs: TypeSource,
parseLibp2pMessage?: (peerId: string, data: any) => void
parseLibp2pMessage?: (log: debug.Debugger, peerId: string, data: any) => void
): Promise<{
app: Application,
server: ApolloServer
@@ -146,7 +146,7 @@ export class ServerCmd {
async _startP2PNodes (
p2pConfig: P2PConfig,
parseLibp2pMessage?: (peerId: string, data: any) => void
parseLibp2pMessage?: (log: debug.Debugger, peerId: string, data: any) => void
): Promise<void> {
const { createRelayNode, Peer } = await import('@cerc-io/peer');
const {
@@ -206,7 +206,7 @@ export class ServerCmd {
peer.subscribeTopic(peerConfig.pubSubTopic, (peerId, data) => {
if (parseLibp2pMessage) {
parseLibp2pMessage(peerId.toString(), data);
parseLibp2pMessage(log, peerId.toString(), data);
}
});