Skip to content

Commit

Permalink
Collecting all info about the node
Browse files Browse the repository at this point in the history
  • Loading branch information
wigy-opensource-developer committed Apr 8, 2020
1 parent 3ea7761 commit 4b51c9c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
30 changes: 16 additions & 14 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
31 changes: 19 additions & 12 deletions src/crawler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) => {
Expand All @@ -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)
}
)
})
Expand All @@ -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()
}
)
Expand Down

0 comments on commit 4b51c9c

Please sign in to comment.