Skip to content

Commit

Permalink
capture erros, store connected peers accross runs, dont fail hard if …
Browse files Browse the repository at this point in the history
…1 promise fails
  • Loading branch information
roks0n committed Jul 23, 2019
1 parent d859a5f commit 3501ee4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
28 changes: 17 additions & 11 deletions src/crawler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

/**
Expand All @@ -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)
}
Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions src/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down

0 comments on commit 3501ee4

Please sign in to comment.