From 3501ee4527e714f312a6f6cb46d975e9f1db48c5 Mon Sep 17 00:00:00 2001 From: roks0n Date: Tue, 23 Jul 2019 23:21:11 +0200 Subject: [PATCH] capture erros, store connected peers accross runs, dont fail hard if 1 promise fails --- package.json | 2 +- src/crawler.js | 28 +++++++++++++++++----------- src/peer.js | 8 ++++++-- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index f3d9453..18325e6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@deadlock-delegate/crawler", - "version": "1.2.4", + "version": "1.2.5", "description": "Crawler scans the ARK network to get information about the peers in the network.", "main": "src/crawler.js", "scripts": { diff --git a/src/crawler.js b/src/crawler.js index 58fbc02..5389444 100644 --- a/src/crawler.js +++ b/src/crawler.js @@ -10,16 +10,20 @@ class Crawler { * Initializes the internal request reactor. * @method constructor */ - constructor (timeout = 3000) { + constructor (timeout = 3000, disconnect = true) { this.timeout = timeout this.headers = {} this.socket = undefined + this.disconnect = disconnect this.request = { headers: { nethash: 'no-nethash', version: 'no-version' } } + + this.nodes = {} + this.peers = new Peers() } /** @@ -30,22 +34,24 @@ class Crawler { */ async run (peer) { this.heights = [] - this.nodes = {} this.samplePeers = {} this.startTime = new Date() - this.peers = new Peers() NETWORK_P2P_PORT = peer.port - this.peers.add(peer.ip, NETWORK_P2P_PORT) + if (!this.peers.get(peer.ip)) { + this.peers.add(peer.ip, NETWORK_P2P_PORT) + } try { console.log(`... discovering network peers`) await this.discoverPeers(peer) console.log(`... scanning network`) await this.scanNetwork() - console.log(`... disconnecting from all peers`) - this.peers.disconnect() + if (this.disconnect) { + console.log(`... disconnecting from all peers`) + this.peers.disconnectAll() + } } catch (err) { console.error(err) } @@ -64,8 +70,8 @@ class Crawler { this.request, (err, response) => { if (err) { - reject(new Error(err)) - return + console.error(`Error when calling p2p.peer.getPeers on ${peer.ip}`) + return resolve() } if (peer.ip in this.samplePeers) { @@ -83,8 +89,7 @@ class Crawler { }) if (this.samplePeers[peer.ip] === VISITED) { - resolve() - return + return resolve() } // note: this is not very efficient on large arrays @@ -118,7 +123,8 @@ class Crawler { this.request, (err, response) => { if (err) { - return reject(new Error(err)) + console.error(`Error when calling p2p.peer.getStatus on ${peer.ip}`) + return resolve() } this.heights.push({ height: response.data.state.header.height, diff --git a/src/peer.js b/src/peer.js index 50c0019..e9d7b21 100644 --- a/src/peer.js +++ b/src/peer.js @@ -11,7 +11,11 @@ class Peers { return connection } connection = SocketClient.create({ hostname: ip, port }) - connection.on('error', (err) => console.error(err)) + connection.on('error', (err) => { + console.error(err) + connection.destroy() + this.connections.delete(ip) + }) this.connections.set(ip, connection) return connection } @@ -24,7 +28,7 @@ class Peers { return this.connections } - disconnect () { + disconnectAll () { for (const [ip, connection] of this.connections.entries()) { connection.destroy() this.connections.delete(ip)