mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-01-06 19:38:05 +00:00
Add heartbeat checks in relay node to hangup disconnected peers (#314)
* Close connections from relay node to disconnected peers * Review changes and update libp2p version * Update package version
This commit is contained in:
parent
f5ff7cc96e
commit
40fd5f8ab8
@ -2,7 +2,7 @@
|
||||
"packages": [
|
||||
"packages/*"
|
||||
],
|
||||
"version": "0.2.26",
|
||||
"version": "0.2.27",
|
||||
"npmClient": "yarn",
|
||||
"useWorkspaces": true,
|
||||
"command": {
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/address-watcher",
|
||||
"version": "0.2.26",
|
||||
"version": "0.2.27",
|
||||
"description": "Address Watcher",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
@ -26,11 +26,11 @@
|
||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||
"dependencies": {
|
||||
"@apollo/client": "^3.3.19",
|
||||
"@cerc-io/cache": "^0.2.26",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.26",
|
||||
"@cerc-io/solidity-mapper": "^0.2.26",
|
||||
"@cerc-io/tracing-client": "^0.2.26",
|
||||
"@cerc-io/util": "^0.2.26",
|
||||
"@cerc-io/cache": "^0.2.27",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.27",
|
||||
"@cerc-io/solidity-mapper": "^0.2.27",
|
||||
"@cerc-io/tracing-client": "^0.2.27",
|
||||
"@cerc-io/util": "^0.2.27",
|
||||
"@types/lodash": "^4.14.168",
|
||||
"debug": "^4.3.1",
|
||||
"ethers": "^5.4.4",
|
||||
|
2
packages/cache/package.json
vendored
2
packages/cache/package.json
vendored
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/cache",
|
||||
"version": "0.2.26",
|
||||
"version": "0.2.27",
|
||||
"description": "Generic object cache",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/cli",
|
||||
"version": "0.2.26",
|
||||
"version": "0.2.27",
|
||||
"main": "dist/index.js",
|
||||
"license": "AGPL-3.0",
|
||||
"scripts": {
|
||||
@ -11,8 +11,8 @@
|
||||
"chat": "node dist/chat.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@cerc-io/peer": "^0.2.26",
|
||||
"@cerc-io/util": "^0.2.26",
|
||||
"@cerc-io/peer": "^0.2.27",
|
||||
"@cerc-io/util": "^0.2.27",
|
||||
"@ethersproject/providers": "^5.4.4",
|
||||
"@graphql-tools/utils": "^9.1.1",
|
||||
"@ipld/dag-cbor": "^8.0.0",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/codegen",
|
||||
"version": "0.2.26",
|
||||
"version": "0.2.27",
|
||||
"description": "Code generator",
|
||||
"private": true,
|
||||
"main": "index.js",
|
||||
@ -20,7 +20,7 @@
|
||||
},
|
||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||
"dependencies": {
|
||||
"@cerc-io/util": "^0.2.26",
|
||||
"@cerc-io/util": "^0.2.27",
|
||||
"@graphql-tools/load-files": "^6.5.2",
|
||||
"@poanet/solidity-flattener": "https://github.com/vulcanize/solidity-flattener.git",
|
||||
"@solidity-parser/parser": "^0.13.2",
|
||||
|
@ -41,12 +41,12 @@
|
||||
"dependencies": {
|
||||
"@apollo/client": "^3.3.19",
|
||||
"@ethersproject/providers": "^5.4.4",
|
||||
"@cerc-io/cli": "^v0.2.26",
|
||||
"@cerc-io/ipld-eth-client": "^v0.2.26",
|
||||
"@cerc-io/solidity-mapper": "^v0.2.26",
|
||||
"@cerc-io/util": "^v0.2.26",
|
||||
"@cerc-io/cli": "^0.2.27",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.27",
|
||||
"@cerc-io/solidity-mapper": "^0.2.27",
|
||||
"@cerc-io/util": "^0.2.27",
|
||||
{{#if (subgraphPath)}}
|
||||
"@cerc-io/graph-node": "^v0.2.26",
|
||||
"@cerc-io/graph-node": "^0.2.27",
|
||||
{{/if}}
|
||||
"apollo-type-bigint": "^0.1.3",
|
||||
"debug": "^4.3.1",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/eden-watcher",
|
||||
"version": "0.2.26",
|
||||
"version": "0.2.27",
|
||||
"description": "eden-watcher",
|
||||
"private": true,
|
||||
"main": "dist/index.js",
|
||||
@ -38,11 +38,11 @@
|
||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||
"dependencies": {
|
||||
"@apollo/client": "^3.3.19",
|
||||
"@cerc-io/cli": "^0.2.26",
|
||||
"@cerc-io/graph-node": "^0.2.26",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.26",
|
||||
"@cerc-io/solidity-mapper": "^0.2.26",
|
||||
"@cerc-io/util": "^0.2.26",
|
||||
"@cerc-io/cli": "^0.2.27",
|
||||
"@cerc-io/graph-node": "^0.2.27",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.27",
|
||||
"@cerc-io/solidity-mapper": "^0.2.27",
|
||||
"@cerc-io/util": "^0.2.27",
|
||||
"@ethersproject/providers": "^5.4.4",
|
||||
"apollo-type-bigint": "^0.1.3",
|
||||
"debug": "^4.3.1",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/erc20-watcher",
|
||||
"version": "0.2.26",
|
||||
"version": "0.2.27",
|
||||
"description": "ERC20 Watcher",
|
||||
"private": true,
|
||||
"main": "dist/index.js",
|
||||
@ -42,10 +42,10 @@
|
||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||
"dependencies": {
|
||||
"@apollo/client": "^3.3.19",
|
||||
"@cerc-io/cli": "^0.2.26",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.26",
|
||||
"@cerc-io/solidity-mapper": "^0.2.26",
|
||||
"@cerc-io/util": "^0.2.26",
|
||||
"@cerc-io/cli": "^0.2.27",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.27",
|
||||
"@cerc-io/solidity-mapper": "^0.2.27",
|
||||
"@cerc-io/util": "^0.2.27",
|
||||
"@ethersproject/providers": "^5.4.4",
|
||||
"apollo-type-bigint": "^0.1.3",
|
||||
"debug": "^4.3.1",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/erc721-watcher",
|
||||
"version": "0.2.26",
|
||||
"version": "0.2.27",
|
||||
"description": "erc721-watcher",
|
||||
"private": true,
|
||||
"main": "dist/index.js",
|
||||
@ -47,10 +47,10 @@
|
||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||
"dependencies": {
|
||||
"@apollo/client": "^3.3.19",
|
||||
"@cerc-io/cli": "^0.2.26",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.26",
|
||||
"@cerc-io/solidity-mapper": "^0.2.26",
|
||||
"@cerc-io/util": "^0.2.26",
|
||||
"@cerc-io/cli": "^0.2.27",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.27",
|
||||
"@cerc-io/solidity-mapper": "^0.2.27",
|
||||
"@cerc-io/util": "^0.2.27",
|
||||
"@ethersproject/providers": "^5.4.4",
|
||||
"apollo-type-bigint": "^0.1.3",
|
||||
"debug": "^4.3.1",
|
||||
|
@ -1,10 +1,10 @@
|
||||
{
|
||||
"name": "@cerc-io/graph-node",
|
||||
"version": "0.2.26",
|
||||
"version": "0.2.27",
|
||||
"main": "dist/index.js",
|
||||
"license": "AGPL-3.0",
|
||||
"devDependencies": {
|
||||
"@cerc-io/solidity-mapper": "^0.2.26",
|
||||
"@cerc-io/solidity-mapper": "^0.2.27",
|
||||
"@ethersproject/providers": "^5.4.4",
|
||||
"@graphprotocol/graph-ts": "^0.22.0",
|
||||
"@nomiclabs/hardhat-ethers": "^2.0.2",
|
||||
@ -50,9 +50,9 @@
|
||||
"dependencies": {
|
||||
"@apollo/client": "^3.3.19",
|
||||
"@cerc-io/assemblyscript": "0.19.10-watcher-ts-0.1.2",
|
||||
"@cerc-io/cache": "^0.2.26",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.26",
|
||||
"@cerc-io/util": "^0.2.26",
|
||||
"@cerc-io/cache": "^0.2.27",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.27",
|
||||
"@cerc-io/util": "^0.2.27",
|
||||
"@types/json-diff": "^0.5.2",
|
||||
"bn.js": "^4.11.9",
|
||||
"debug": "^4.3.1",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/graph-test-watcher",
|
||||
"version": "0.2.26",
|
||||
"version": "0.2.27",
|
||||
"description": "graph-test-watcher",
|
||||
"private": true,
|
||||
"main": "dist/index.js",
|
||||
@ -38,11 +38,11 @@
|
||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||
"dependencies": {
|
||||
"@apollo/client": "^3.3.19",
|
||||
"@cerc-io/cli": "^0.2.26",
|
||||
"@cerc-io/graph-node": "^0.2.26",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.26",
|
||||
"@cerc-io/solidity-mapper": "^0.2.26",
|
||||
"@cerc-io/util": "^0.2.26",
|
||||
"@cerc-io/cli": "^0.2.27",
|
||||
"@cerc-io/graph-node": "^0.2.27",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.27",
|
||||
"@cerc-io/solidity-mapper": "^0.2.27",
|
||||
"@cerc-io/util": "^0.2.27",
|
||||
"@ethersproject/providers": "^5.4.4",
|
||||
"apollo-type-bigint": "^0.1.3",
|
||||
"debug": "^4.3.1",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/ipld-eth-client",
|
||||
"version": "0.2.26",
|
||||
"version": "0.2.27",
|
||||
"description": "IPLD ETH Client",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
@ -20,7 +20,7 @@
|
||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||
"dependencies": {
|
||||
"@apollo/client": "^3.7.1",
|
||||
"@cerc-io/cache": "^0.2.26",
|
||||
"@cerc-io/cache": "^0.2.27",
|
||||
"cross-fetch": "^3.1.4",
|
||||
"debug": "^4.3.1",
|
||||
"ethers": "^5.4.4",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/mobymask-watcher",
|
||||
"version": "0.2.26",
|
||||
"version": "0.2.27",
|
||||
"description": "mobymask-watcher",
|
||||
"private": true,
|
||||
"main": "dist/index.js",
|
||||
@ -37,10 +37,10 @@
|
||||
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
|
||||
"dependencies": {
|
||||
"@apollo/client": "^3.3.19",
|
||||
"@cerc-io/cli": "^0.2.26",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.26",
|
||||
"@cerc-io/solidity-mapper": "^0.2.26",
|
||||
"@cerc-io/util": "^0.2.26",
|
||||
"@cerc-io/cli": "^0.2.27",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.27",
|
||||
"@cerc-io/solidity-mapper": "^0.2.27",
|
||||
"@cerc-io/util": "^0.2.27",
|
||||
"@ethersproject/providers": "^5.4.4",
|
||||
"apollo-type-bigint": "^0.1.3",
|
||||
"debug": "^4.3.1",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/peer",
|
||||
"version": "0.2.26",
|
||||
"version": "0.2.27",
|
||||
"description": "libp2p module",
|
||||
"main": "dist/index.js",
|
||||
"exports": "./dist/index.js",
|
||||
@ -26,7 +26,7 @@
|
||||
"relay-node": "node dist/relay.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@cerc-io/libp2p": "0.42.2-laconic-0.1.0",
|
||||
"@cerc-io/libp2p": "0.42.2-laconic-0.1.1",
|
||||
"@cerc-io/webrtc-direct": "^5.0.0-laconic-0.1.1",
|
||||
"@chainsafe/libp2p-noise": "^11.0.0",
|
||||
"@libp2p/floodsub": "^6.0.0",
|
||||
|
@ -25,7 +25,8 @@ import { multiaddr, Multiaddr } from '@multiformats/multiaddr';
|
||||
import { floodsub } from '@libp2p/floodsub';
|
||||
import { pubsubPeerDiscovery } from '@libp2p/pubsub-peer-discovery';
|
||||
|
||||
import { MAX_CONCURRENT_DIALS_PER_PEER, MAX_CONNECTIONS, MIN_CONNECTIONS, PUBSUB_DISCOVERY_INTERVAL, PUBSUB_SIGNATURE_POLICY, RELAY_TAG, RELAY_REDIAL_DELAY, CONN_CHECK_INTERVAL, PING_TIMEOUT } from './constants.js';
|
||||
import { MAX_CONCURRENT_DIALS_PER_PEER, MAX_CONNECTIONS, MIN_CONNECTIONS, PUBSUB_DISCOVERY_INTERVAL, PUBSUB_SIGNATURE_POLICY, RELAY_TAG, RELAY_REDIAL_DELAY, PING_TIMEOUT } from './constants.js';
|
||||
import { PeerHearbeatChecker } from './peer-heartbeat-checker.js';
|
||||
|
||||
export const CHAT_PROTOCOL = '/chat/1.0.0';
|
||||
|
||||
@ -33,13 +34,13 @@ export const ERR_PROTOCOL_SELECTION = 'protocol selection failed';
|
||||
|
||||
export class Peer {
|
||||
_node?: Libp2p
|
||||
_peerHeartbeatChecker?: PeerHearbeatChecker
|
||||
_wrtcTransport: (components: WebRTCDirectComponents) => Transport
|
||||
_relayNodeMultiaddr: Multiaddr
|
||||
|
||||
_peerStreamMap: Map<string, Pushable<any>> = new Map()
|
||||
_messageHandlers: Array<(peerId: PeerId, message: any) => void> = []
|
||||
_topicHandlers: Map<string, Array<(peerId: PeerId, data: any) => void>> = new Map()
|
||||
_peerHeartbeatIntervalIdsMap: Map<string, NodeJS.Timer> = new Map();
|
||||
|
||||
constructor (relayNodeURL: string, nodejs?: boolean) {
|
||||
this._relayNodeMultiaddr = multiaddr(relayNodeURL);
|
||||
@ -105,6 +106,7 @@ export class Peer {
|
||||
}
|
||||
|
||||
console.log('libp2p node created', this._node);
|
||||
this._peerHeartbeatChecker = new PeerHearbeatChecker(this._node);
|
||||
|
||||
// Dial to the HOP enabled relay node
|
||||
await this._dialRelay();
|
||||
@ -171,7 +173,7 @@ export class Peer {
|
||||
|
||||
await this._node.unhandle(CHAT_PROTOCOL);
|
||||
const remotePeerIds = this._node.getPeers();
|
||||
remotePeerIds.forEach(remotePeerId => this._stopHeartbeatChecks(remotePeerId));
|
||||
remotePeerIds.forEach(remotePeerId => this._peerHeartbeatChecker?.stop(remotePeerId));
|
||||
const hangUpPromises = remotePeerIds.map(async peerId => this._node?.hangUp(peerId));
|
||||
await Promise.all(hangUpPromises);
|
||||
}
|
||||
@ -334,8 +336,8 @@ export class Peer {
|
||||
|
||||
console.log(`Current number of peers connected: ${this._node.getPeers().length}`);
|
||||
|
||||
// Start heartbeat check peer
|
||||
await this._startHeartbeatChecks(
|
||||
// Start heartbeat check for peer
|
||||
await this._peerHeartbeatChecker?.start(
|
||||
remotePeerId,
|
||||
async () => this._handleDeadConnections(remotePeerId)
|
||||
);
|
||||
@ -348,46 +350,6 @@ export class Peer {
|
||||
console.log('Closed');
|
||||
}
|
||||
|
||||
async _startHeartbeatChecks (peerId: PeerId, handleDisconnect: () => Promise<void>): Promise<void> {
|
||||
if (this._peerHeartbeatIntervalIdsMap.has(peerId.toString())) {
|
||||
// Do not start connection check interval if already present
|
||||
return;
|
||||
}
|
||||
|
||||
const intervalId = setInterval(async () => {
|
||||
await this._validatePing(
|
||||
peerId,
|
||||
async () => {
|
||||
// Check if connection check interval for peer is already cleared
|
||||
if (!this._peerHeartbeatIntervalIdsMap.has(peerId.toString())) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Clear and remove check interval for remote peer if not connected
|
||||
this._stopHeartbeatChecks(peerId);
|
||||
|
||||
await handleDisconnect();
|
||||
}
|
||||
);
|
||||
}, CONN_CHECK_INTERVAL);
|
||||
|
||||
this._peerHeartbeatIntervalIdsMap.set(peerId.toString(), intervalId);
|
||||
}
|
||||
|
||||
async _validatePing (peerId: PeerId, handleDisconnect: () => Promise<void>): Promise<void> {
|
||||
assert(this._node);
|
||||
|
||||
try {
|
||||
// Ping remote peer
|
||||
await this._node.ping(peerId);
|
||||
} catch (err) {
|
||||
// On error i.e. no pong
|
||||
console.log(`Not connected to peer ${peerId.toString()}`);
|
||||
|
||||
await handleDisconnect();
|
||||
}
|
||||
}
|
||||
|
||||
async _handleDisconnect (connection: Connection): Promise<void> {
|
||||
assert(this._node);
|
||||
const disconnectedPeerId = connection.remotePeer;
|
||||
@ -400,7 +362,7 @@ export class Peer {
|
||||
|
||||
if (!peerConnections.length) {
|
||||
// Stop connection check for disconnected peer
|
||||
this._stopHeartbeatChecks(disconnectedPeerId);
|
||||
this._peerHeartbeatChecker?.stop(disconnectedPeerId);
|
||||
|
||||
if (disconnectedPeerId.toString() === this._relayNodeMultiaddr?.getPeerId()) {
|
||||
// Reconnect to relay node if disconnected
|
||||
@ -409,17 +371,6 @@ export class Peer {
|
||||
}
|
||||
}
|
||||
|
||||
_stopHeartbeatChecks (peerId: PeerId): void {
|
||||
// Clear check interval for disconnected peer
|
||||
const intervalId = this._peerHeartbeatIntervalIdsMap.get(peerId.toString());
|
||||
|
||||
if (intervalId) {
|
||||
clearInterval(intervalId);
|
||||
}
|
||||
|
||||
this._peerHeartbeatIntervalIdsMap.delete(peerId.toString());
|
||||
}
|
||||
|
||||
async _connectPeer (peer: PeerInfo): Promise<void> {
|
||||
assert(this._node);
|
||||
|
||||
|
83
packages/peer/src/peer-heartbeat-checker.ts
Normal file
83
packages/peer/src/peer-heartbeat-checker.ts
Normal file
@ -0,0 +1,83 @@
|
||||
//
|
||||
// Copyright 2023 Vulcanize, Inc.
|
||||
//
|
||||
|
||||
import { Libp2p } from '@cerc-io/libp2p';
|
||||
import type { PeerId } from '@libp2p/interface-peer-id';
|
||||
|
||||
import { CONN_CHECK_INTERVAL } from './constants.js';
|
||||
|
||||
/**
|
||||
* Used for tracking heartbeat of connected remote peers
|
||||
*/
|
||||
export class PeerHearbeatChecker {
|
||||
_node: Libp2p;
|
||||
_peerHeartbeatIntervalIdsMap: Map<string, NodeJS.Timer> = new Map();
|
||||
|
||||
constructor (node: Libp2p) {
|
||||
this._node = node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to start heartbeat checks for a peer
|
||||
* @param peerId
|
||||
* @param handleDisconnect
|
||||
*/
|
||||
async start (peerId: PeerId, handleDisconnect: () => Promise<void>): Promise<void> {
|
||||
if (this._peerHeartbeatIntervalIdsMap.has(peerId.toString())) {
|
||||
// Do not start connection check interval if already present
|
||||
return;
|
||||
}
|
||||
|
||||
const intervalId = setInterval(async () => {
|
||||
await this._validatePing(
|
||||
peerId,
|
||||
async () => {
|
||||
// Check if connection check interval for peer is already cleared
|
||||
if (!this._peerHeartbeatIntervalIdsMap.has(peerId.toString())) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Clear and remove check interval for remote peer if not connected
|
||||
this.stop(peerId);
|
||||
|
||||
await handleDisconnect();
|
||||
}
|
||||
);
|
||||
}, CONN_CHECK_INTERVAL);
|
||||
|
||||
this._peerHeartbeatIntervalIdsMap.set(peerId.toString(), intervalId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to check connection using ping request
|
||||
* @param peerId
|
||||
* @param handleDisconnect
|
||||
*/
|
||||
async _validatePing (peerId: PeerId, handleDisconnect: () => Promise<void>): Promise<void> {
|
||||
try {
|
||||
// Ping remote peer
|
||||
await this._node.ping(peerId);
|
||||
} catch (err) {
|
||||
// On error i.e. no pong
|
||||
console.log(`Not connected to peer ${peerId.toString()}`);
|
||||
|
||||
await handleDisconnect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to stop heartbeat checks for a peer
|
||||
* @param peerId
|
||||
*/
|
||||
stop (peerId: PeerId): void {
|
||||
// Clear check interval for disconnected peer
|
||||
const intervalId = this._peerHeartbeatIntervalIdsMap.get(peerId.toString());
|
||||
|
||||
if (intervalId) {
|
||||
clearInterval(intervalId);
|
||||
}
|
||||
|
||||
this._peerHeartbeatIntervalIdsMap.delete(peerId.toString());
|
||||
}
|
||||
}
|
@ -18,8 +18,10 @@ import { pubsubPeerDiscovery } from '@libp2p/pubsub-peer-discovery';
|
||||
import { createFromJSON } from '@libp2p/peer-id-factory';
|
||||
import type { Connection } from '@libp2p/interface-connection';
|
||||
import { multiaddr } from '@multiformats/multiaddr';
|
||||
import type { PeerId } from '@libp2p/interface-peer-id';
|
||||
|
||||
import { HOP_TIMEOUT, PUBSUB_DISCOVERY_INTERVAL, PUBSUB_SIGNATURE_POLICY } from './constants.js';
|
||||
import { PeerHearbeatChecker } from './peer-heartbeat-checker.js';
|
||||
|
||||
const log = debug('laconic:relay');
|
||||
|
||||
@ -79,17 +81,24 @@ async function main (): Promise<void> {
|
||||
}
|
||||
});
|
||||
|
||||
const peerHeartbeatChecker = new PeerHearbeatChecker(node);
|
||||
|
||||
console.log(`Relay node started with id ${node.peerId.toString()}`);
|
||||
console.log('Listening on:');
|
||||
node.getMultiaddrs().forEach((ma) => console.log(ma.toString()));
|
||||
console.log();
|
||||
|
||||
// Listen for peers connection
|
||||
node.addEventListener('peer:connect', (evt) => {
|
||||
node.addEventListener('peer:connect', async (evt) => {
|
||||
// console.log('event peer:connect', evt);
|
||||
// Log connected peer
|
||||
const connection: Connection = evt.detail;
|
||||
log(`Connected to ${connection.remotePeer.toString()} using multiaddr ${connection.remoteAddr.toString()}`);
|
||||
|
||||
// Start heartbeat check for peer
|
||||
await peerHeartbeatChecker.start(
|
||||
connection.remotePeer,
|
||||
async () => _handleDeadConnections(node, connection.remotePeer)
|
||||
);
|
||||
});
|
||||
|
||||
// Listen for peers disconnecting
|
||||
@ -149,6 +158,13 @@ async function _dialRelayPeers (node: Libp2p, relayPeersList: string[]): Promise
|
||||
});
|
||||
}
|
||||
|
||||
async function _handleDeadConnections (node: Libp2p, remotePeerId: PeerId): Promise<void> {
|
||||
// Close existing connections of remote peer
|
||||
log(`Closing connections for ${remotePeerId}`);
|
||||
await node.hangUp(remotePeerId);
|
||||
log('Closed');
|
||||
}
|
||||
|
||||
main().catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
|
@ -1,10 +1,10 @@
|
||||
{
|
||||
"name": "@cerc-io/solidity-mapper",
|
||||
"version": "0.2.26",
|
||||
"version": "0.2.27",
|
||||
"main": "dist/index.js",
|
||||
"license": "AGPL-3.0",
|
||||
"devDependencies": {
|
||||
"@cerc-io/ipld-eth-client": "^0.2.26",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.27",
|
||||
"@ethersproject/abi": "^5.3.0",
|
||||
"@nomiclabs/hardhat-ethers": "^2.0.2",
|
||||
"@nomiclabs/hardhat-waffle": "^2.0.1",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/test",
|
||||
"version": "0.2.26",
|
||||
"version": "0.2.27",
|
||||
"main": "dist/index.js",
|
||||
"license": "AGPL-3.0",
|
||||
"private": true,
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@cerc-io/tracing-client",
|
||||
"version": "0.2.26",
|
||||
"version": "0.2.27",
|
||||
"description": "ETH VM tracing client",
|
||||
"private": true,
|
||||
"main": "dist/index.js",
|
||||
|
@ -1,11 +1,11 @@
|
||||
{
|
||||
"name": "@cerc-io/util",
|
||||
"version": "0.2.26",
|
||||
"version": "0.2.27",
|
||||
"main": "dist/index.js",
|
||||
"license": "AGPL-3.0",
|
||||
"dependencies": {
|
||||
"@apollo/utils.keyvaluecache": "^1.0.1",
|
||||
"@cerc-io/solidity-mapper": "^0.2.26",
|
||||
"@cerc-io/solidity-mapper": "^0.2.27",
|
||||
"@ethersproject/providers": "^5.4.4",
|
||||
"@graphql-tools/schema": "^9.0.10",
|
||||
"@graphql-tools/utils": "^9.1.1",
|
||||
@ -37,8 +37,8 @@
|
||||
"yargs": "^17.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@cerc-io/cache": "^0.2.26",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.26",
|
||||
"@cerc-io/cache": "^0.2.27",
|
||||
"@cerc-io/ipld-eth-client": "^0.2.27",
|
||||
"@nomiclabs/hardhat-waffle": "^2.0.1",
|
||||
"@types/express": "^4.17.14",
|
||||
"@types/fs-extra": "^9.0.11",
|
||||
|
25
yarn.lock
25
yarn.lock
@ -299,10 +299,10 @@
|
||||
binaryen "101.0.0-nightly.20210723"
|
||||
long "^4.0.0"
|
||||
|
||||
"@cerc-io/libp2p@0.42.2-laconic-0.1.0":
|
||||
version "0.42.2-laconic-0.1.0"
|
||||
resolved "https://git.vdb.to/api/packages/cerc-io/npm/%40cerc-io%2Flibp2p/-/0.42.2-laconic-0.1.0/libp2p-0.42.2-laconic-0.1.0.tgz#42284e291426d6aeef7fb9942785277f682e6977"
|
||||
integrity sha512-sXMFZawBOI4lbTnajI7/0Wo/G3+uVEV2FLFqK72QR13V1CkNpCHzOrT87SBnhbG/MMUWeiggUrLULoXtc7+gHA==
|
||||
"@cerc-io/libp2p@0.42.2-laconic-0.1.1":
|
||||
version "0.42.2-laconic-0.1.1"
|
||||
resolved "https://git.vdb.to/api/packages/cerc-io/npm/%40cerc-io%2Flibp2p/-/0.42.2-laconic-0.1.1/libp2p-0.42.2-laconic-0.1.1.tgz#428272102b0fff29816e387557f43d7b31aa6b68"
|
||||
integrity sha512-uoXiyLMhO4vPvNK9Vmpj+p9GHZJk4+ON3qdMj22ui+8sjf/CNOfhOG9mQcUa2u2FMDo5rUAXp2wMehKphCVDzg==
|
||||
dependencies:
|
||||
"@achingbrain/nat-port-mapper" "^1.0.3"
|
||||
"@libp2p/crypto" "^1.0.4"
|
||||
@ -2472,7 +2472,7 @@
|
||||
dependencies:
|
||||
"@libp2p/interface-connection" "^3.0.0"
|
||||
|
||||
"@libp2p/interface-peer-discovery@^1.0.0":
|
||||
"@libp2p/interface-peer-discovery@^1.0.0", "@libp2p/interface-peer-discovery@^1.0.1":
|
||||
version "1.0.5"
|
||||
resolved "https://registry.yarnpkg.com/@libp2p/interface-peer-discovery/-/interface-peer-discovery-1.0.5.tgz#0fb935d55221e0ff58b4dad93646111a4fc7dcdf"
|
||||
integrity sha512-R0TN/vDaCJLvRhop0y4qoPqapHxX4AEQDEtqmpayAA1BuPgbBq4fS4mepR3FAMcNva/szeqVCDuI4gDejtCaVg==
|
||||
@ -2480,14 +2480,6 @@
|
||||
"@libp2p/interface-peer-info" "^1.0.0"
|
||||
"@libp2p/interfaces" "^3.0.0"
|
||||
|
||||
"@libp2p/interface-peer-discovery@^1.0.1":
|
||||
version "1.0.4"
|
||||
resolved "https://registry.npmjs.org/@libp2p/interface-peer-discovery/-/interface-peer-discovery-1.0.4.tgz"
|
||||
integrity sha512-VPLi7onA+WOjYFYH79Qq2hqR+b+OLqTRom5WJaAXv6pclFb1gUetBv4W1MEHY8Hb7l1MidANO/kSySHZ5A3yPg==
|
||||
dependencies:
|
||||
"@libp2p/interface-peer-info" "^1.0.0"
|
||||
"@libp2p/interfaces" "^3.0.0"
|
||||
|
||||
"@libp2p/interface-peer-id@^1.0.0", "@libp2p/interface-peer-id@^1.0.2":
|
||||
version "1.1.2"
|
||||
resolved "https://registry.yarnpkg.com/@libp2p/interface-peer-id/-/interface-peer-id-1.1.2.tgz#22cbfb4707949cd49c3271a871172221d6920049"
|
||||
@ -2604,12 +2596,7 @@
|
||||
"@multiformats/multiaddr" "^11.0.0"
|
||||
it-stream-types "^1.0.4"
|
||||
|
||||
"@libp2p/interfaces@^3.0.0", "@libp2p/interfaces@^3.0.2":
|
||||
version "3.1.0"
|
||||
resolved "https://registry.npmjs.org/@libp2p/interfaces/-/interfaces-3.1.0.tgz"
|
||||
integrity sha512-WxzUVaeOpMcNPyjruVbjNvVnjFzay8udqiaW0+rDltlYHSA1LXvZIzk1s5+/m+PKFM+UK7KmY1FZ70HiJzlZ9Q==
|
||||
|
||||
"@libp2p/interfaces@^3.0.3":
|
||||
"@libp2p/interfaces@^3.0.0", "@libp2p/interfaces@^3.0.2", "@libp2p/interfaces@^3.0.3":
|
||||
version "3.3.0"
|
||||
resolved "https://registry.yarnpkg.com/@libp2p/interfaces/-/interfaces-3.3.0.tgz#19299d0a627d8513170506261e3a1a572df58154"
|
||||
integrity sha512-X02bdMBl3tFoQfQWIioSnupOpA9lbBkmTx920idtfmes02kHcGWI+vEtMs1Prm+HtlIPQLQiQe5ZEr1WKwhPRQ==
|
||||
|
Loading…
Reference in New Issue
Block a user