Pass initialization options when starting a peer (#328)

* Pass initialization options when starting a peer

* Update config used for relay and peer nodes in watcher

* Rename types

* Refactor mobymask libp2p message parsing

* Enable laconic debug logs in server command
This commit is contained in:
prathamesh0
2023-02-21 17:57:25 +05:30
committed by GitHub
parent 6fa3ee28b5
commit 6a8b9a2385
13 changed files with 206 additions and 104 deletions
+1 -1
View File
@@ -20,7 +20,7 @@ async function main (): Promise<void> {
// 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();
await peer.init({});
peer.subscribeMessage((peerId: PeerId, message: string) => {
console.log(`> ${peerId.toString()} > ${message}`);
+31 -7
View File
@@ -28,8 +28,12 @@ import {
P2PConfig
} from '@cerc-io/util';
import { TypeSource } from '@graphql-tools/utils';
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1319854183
import { RelayNodeInit, PeerIdObj } from '@cerc-io/peer';
import {
RelayNodeInitConfig,
PeerInitConfig,
PeerIdObj
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1319854183
} from '@cerc-io/peer';
import { BaseCmd } from './base';
import { readPeerId } from './utils/index';
@@ -145,7 +149,13 @@ export class ServerCmd {
parseLibp2pMessage?: (peerId: string, data: any) => void
): Promise<void> {
const { createRelayNode, Peer } = await import('@cerc-io/peer');
const { RELAY_DEFAULT_HOST, RELAY_DEFAULT_PORT, RELAY_DEFAULT_MAX_DIAL_RETRY } = await import('@cerc-io/peer');
const {
RELAY_DEFAULT_HOST,
RELAY_DEFAULT_PORT,
RELAY_DEFAULT_MAX_DIAL_RETRY,
RELAY_REDIAL_INTERVAL,
PING_INTERVAL
} = await import('@cerc-io/peer');
// Run the relay node if enabled
if (p2pConfig.enableRelay) {
@@ -157,11 +167,13 @@ export class ServerCmd {
peerIdObj = readPeerId(relayConfig.peerIdFile);
}
const relayNodeInit: RelayNodeInit = {
const relayNodeInit: RelayNodeInitConfig = {
host: relayConfig.host ?? RELAY_DEFAULT_HOST,
port: relayConfig.port ?? RELAY_DEFAULT_PORT,
announceDomain: relayConfig.announce,
relayPeers: relayConfig.relayPeers ?? [],
pingInterval: relayConfig.pingInterval ?? PING_INTERVAL,
redialInterval: relayConfig.redialInterval ?? RELAY_REDIAL_INTERVAL,
maxDialRetry: relayConfig.maxDialRetry ?? RELAY_DEFAULT_MAX_DIAL_RETRY,
peerIdObj
};
@@ -170,10 +182,22 @@ export class ServerCmd {
// Run a peer node if enabled
if (p2pConfig.enablePeer) {
const peer = new Peer(p2pConfig.relayMultiaddr, true);
await peer.init();
const peerConfig = p2pConfig.peer;
assert(peerConfig, 'Peer config not set');
peer.subscribeTopic(p2pConfig.pubSubTopic, (peerId, data) => {
const peer = new Peer(peerConfig.relayMultiaddr, true);
const peerNodeInit: PeerInitConfig = {
pingInterval: peerConfig.pingInterval,
pingTimeout: peerConfig.pingTimeout,
maxRelayConnections: peerConfig.maxRelayConnections,
relayRedialInterval: peerConfig.relayRedialInterval,
maxConnections: peerConfig.maxConnections,
dialTimeout: peerConfig.dialTimeout
};
await peer.init(peerNodeInit);
peer.subscribeTopic(peerConfig.pubSubTopic, (peerId, data) => {
if (parseLibp2pMessage) {
parseLibp2pMessage(peerId.toString(), data);
}