Last active
June 16, 2020 11:06
-
-
Save jacobheun/932f44750e46d0dde872c287bb731882 to your computer and use it in GitHub Desktop.
IPFS dht stats dump comparison script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Compares two routing table dumps from IPFS (`ipfs stats dht`). | |
* Run with node by specifying the path to the old and new dump files. | |
* | |
* 1. Get two routing table dumps | |
* | |
* ipfs stats dht > ./old.txt | |
* ipfs stats dht > ./new.txt | |
* | |
* 2. Run the comparison. This assumes same folder, but as long as the | |
* path from this file is specified, it should work fine. | |
* | |
* node index.js old.txt new.txt | |
*/ | |
'use strict' | |
const fs = require('fs') | |
const readline = require('readline') | |
const path = require('path') | |
const pDefer = () => { | |
const deferred = {}; | |
deferred.promise = new Promise((resolve, reject) => { | |
deferred.resolve = resolve; | |
deferred.reject = reject; | |
}); | |
return deferred; | |
} | |
function rtCompare (newPeers, oldPeers) { | |
let buckets = {} | |
newPeers.forEach((_, key) => { | |
const oldPeer = oldPeers.get(key) | |
if (oldPeer) { | |
buckets[oldPeer.bucketId] = buckets[oldPeer.bucketId] || new Set() | |
buckets[oldPeer.bucketId].add(key) | |
} | |
}) | |
return buckets | |
} | |
function processRT (fileStream) { | |
const deferred = pDefer() | |
const rl = readline.createInterface({ | |
input: fileStream | |
}) | |
let dhtType | |
let bucketId | |
let peers = new Map() | |
rl.on('line', (line) => { | |
if (line.match(/^DHT\s(wan|lan)/)) { | |
dhtType = line.match(/^DHT\s(wan|lan)/)[1] | |
} else if (line.match(/^\s?.Bucket\s+(\d+)/)) { | |
bucketId = line.match(/^\s?.Bucket\s+(\d+)/)[1] | |
} else if (line.match(/\b(\w*(Qm|12D)\w*)\b/g)) { | |
const peerId = line.match(/\b(\w*(Qm|12D)\w*)\b/g)[0] | |
peers.set(peerId, { | |
bucketId, | |
dhtType | |
}) | |
} | |
}) | |
rl.on('close', () => { | |
console.log('RT has %s peers', peers.size) | |
deferred.resolve(peers) | |
}) | |
return deferred.promise | |
} | |
async function main() { | |
let newFile = path.resolve(process.argv.pop()) | |
let oldFile = path.resolve(process.argv.pop()) | |
let oldFileStream = fs.createReadStream(oldFile) | |
let newFileStream = fs.createReadStream(newFile) | |
const oldRT = await processRT(oldFileStream) | |
const newRT = await processRT(newFileStream) | |
const bucketChange = rtCompare(newRT, oldRT) | |
console.log("bucket(#common peers): <common peers>") | |
Object.keys(bucketChange).forEach(key => { | |
const peers = bucketChange[key] | |
console.log("%s(%s):", key, peers.size, Array.from(peers)) | |
}) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment