From 4b51c9cb83da3f102f0baeee1480a3aeecc38fa4 Mon Sep 17 00:00:00 2001 From: wigy Date: Wed, 8 Apr 2020 20:40:27 +0200 Subject: [PATCH] Collecting all info about the node --- app.js | 30 ++++++++++++++++-------------- src/crawler.js | 31 +++++++++++++++++++------------ 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/app.js b/app.js index 4ae5381..adae908 100644 --- a/app.js +++ b/app.js @@ -11,29 +11,31 @@ const report = (crawler) => { const nodes = Object.values(crawler.nodes) - for (const item of nodes) { - if (item.height === undefined || item.id === undefined) { + for (const node of nodes) { + if (node.height === undefined || node.id === undefined) { continue } - if (blockStats[item.height]) { - blockStats[item.height].count += 1 - blockStats[item.height].ids[item.id] += 1 + console.log(JSON.stringify(node, undefined, 2)) + + if (blockStats[node.height]) { + blockStats[node.height].count += 1 + blockStats[node.height].ids[node.id] += 1 } else { - blockStats[item.height] = {} - blockStats[item.height].count = 1 - blockStats[item.height].height = item.height + blockStats[node.height] = {} + blockStats[node.height].count = 1 + blockStats[node.height].height = node.height // todo block ids - blockStats[item.height].ids = {} - blockStats[item.height].ids[item.id] = 1 + blockStats[node.height].ids = {} + blockStats[node.height].ids[node.id] = 1 } - if (versionStats[item.version]) { - versionStats[item.version].count += 1 + if (versionStats[node.version]) { + versionStats[node.version].count += 1 } else { - versionStats[item.version] = { + versionStats[node.version] = { count: 1, - version: item.version + version: node.version } } } diff --git a/src/crawler.js b/src/crawler.js index a9d0498..9c9deeb 100644 --- a/src/crawler.js +++ b/src/crawler.js @@ -31,7 +31,7 @@ class Crawler { async run (peer) { this.nodes = {} this.heights = [] - this.samplePeers = {} + this.traversalState = {} this.startTime = new Date() NETWORK_P2P_PORT = peer.port @@ -71,8 +71,8 @@ class Crawler { return resolve() } - if (currentNode.ip in this.samplePeers) { - this.samplePeers[currentNode.ip] = VISITED + if (currentNode.ip in this.traversalState) { + this.traversalState[currentNode.ip] = VISITED } response.data.map((peer) => { @@ -85,23 +85,24 @@ class Crawler { } }) - if (this.samplePeers[currentNode.ip] === VISITED) { + if (this.traversalState[currentNode.ip] === VISITED) { return resolve() } // note: this is not very efficient on large arrays const samplePeers = response.data - .filter(p => this.samplePeers[p.ip] !== VISITED) + .filter(p => this.traversalState[p.ip] !== VISITED) + .filter(a => a.ip !== currentNode.ip) .map(x => ({ x, r: Math.random() })) .sort((a, b) => a.r - b.r) .map(a => a.x) .slice(0, this.sampleSize) - .filter(a => a.ip !== currentNode.ip) + const discoverPeers = samplePeers .map((peer) => { - this.samplePeers[peer.ip] = NOT_VISITED + this.traversalState[peer.ip] = NOT_VISITED return this.discoverPeers(peer) }) - Promise.all(samplePeers).then(resolve) + Promise.all(discoverPeers).then(resolve) } ) }) @@ -122,12 +123,18 @@ class Crawler { console.error(`Error when calling p2p.peer.getStatus on ${peer.ip}: ${err}`) return resolve() } - this.heights.push({ + const block = { height: response.data.state.header.height, id: response.data.state.header.id - }) - peer.height = response.data.state.header.height - peer.id = response.data.state.header.id + } + this.heights.push(block) + if (peer.height !== block.height) { + console.log(peer.ip + ' heights: ' + peer.height + '<>' + block.height) + } + Object.assign(peer, response.data.config); + Object.assign(peer, block); + // peer.height = block.height + // peer.id = block.id return resolve() } )