Last active
July 21, 2021 02:46
-
-
Save ottosch/45bedc1655154e4057e248a8eb8c2300 to your computer and use it in GitHub Desktop.
Finds the biggest block up to now
This file contains 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
#!/usr/bin/env node | |
// Create a directory with the script inside, then run npm init && npm i node-bitcoin-rpc | |
// Run it with ./bigblock.js or node bigblock.js | |
// Also change the username and password below | |
// As of 20 Jul 2021, it is block 682482 with 2,42 MB | |
const rpc = require("node-bitcoin-rpc") | |
rpc.init("127.0.0.1", 8332, "rpc_username", "rpc_pass"); | |
rpc.setTimeout(Number(process.env.TIMEOUT) || 20000); | |
errorHeights = []; | |
async function getBestBlock() { | |
return new Promise(resolve => { | |
rpc.call("getblockchaininfo", [], (e, d) => { | |
if (d.result) { | |
resolve(d.result.headers); | |
} else { | |
console.error(e); | |
} | |
}) | |
}); | |
} | |
async function getBlockSize(height) { | |
return new Promise(resolve => { | |
rpc.call("getblockhash", [height], (e, d) => { | |
if (!d) { | |
errorHeights.push(height); | |
resolve(0); | |
} else { | |
rpc.call("getblock", [d.result], (e, r) => { | |
if (r && r.result) { | |
resolve(r.result); | |
} else { | |
errorHeights.push(height); | |
resolve({height: 0, size: 0}); | |
} | |
}); | |
} | |
}); | |
}); | |
} | |
async function findBiggestBlock(cb) { | |
const segwitActivation = 477120; | |
var bestBlock = await getBestBlock(); | |
var biggestBlock = { | |
height: 0, | |
size: 0 | |
}; | |
console.log(`Best block: ${bestBlock}`); | |
console.log("=============================\n"); | |
var newBiggestFound = false; | |
for (var i = segwitActivation; i <= bestBlock; i++) { | |
var block = await getBlockSize(i); | |
if (block.size > biggestBlock.size) { | |
newBiggestFound = true; | |
biggestBlock.size = block.size; | |
biggestBlock.height = block.height; | |
} | |
if (i % 50 == 0) { | |
if (newBiggestFound) { | |
console.log(`Block ${block.height} of ${bestBlock}. New biggest: ${biggestBlock.size} bytes at height ${biggestBlock.height}`); | |
} else { | |
console.log(`Block ${block.height} of ${bestBlock}`); | |
} | |
newBiggestFound = false; | |
} | |
} | |
console.log("Biggest block:"); | |
console.log(biggestBlock); | |
console.log(formatBytes(biggestBlock.size, 1000), " MB"); | |
console.log(formatBytes(biggestBlock.size, 1024), " MiB"); | |
console.log("Problematic heights:"); | |
console.log(errorHeights); | |
} | |
function formatBytes(bytes, k) { | |
if (bytes === 0) { | |
return 0; | |
} | |
const decimals = 5; | |
var i = Math.floor(Math.log(bytes) / Math.log(k)); | |
if (i == 1) { | |
i = 2; | |
} | |
return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)); | |
} | |
findBiggestBlock(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment