Skip to content

Instantly share code, notes, and snippets.

@braydonf
Last active February 6, 2019 01:43
Show Gist options
  • Save braydonf/528a51785f0f5641464d172696cd2112 to your computer and use it in GitHub Desktop.
Save braydonf/528a51785f0f5641464d172696cd2112 to your computer and use it in GitHub Desktop.
Read bitcoin blocks from disk with bcoin
// original: https://gist.github.com/braydonf/e9572bb40c084691fa20
'use strict';
const path = require('path');
const {Block, Network} = require('bcoin');
const fs = require('bfile');
const bufio = require('bufio');
(async function() {
const network = Network.get('main');
const location = '/home/user/.bitcoin/blocks'
const regexp = /^blk\d{5}\.dat$/;
const files = (await fs.readdir(location)).filter((f) => regexp.test(f))
for (const file of files) {
const data = await fs.readFile(path.resolve(location, file));
const reader = bufio.read(data);
let magic = null;
let size = 0;
while (reader.left() >= 4) {
magic = reader.readU32();
if (magic !== network.magic) {
reader.seek(4);
continue;
}
size = reader.readU32();
const block = Block.fromReader(reader);
const hash = block.rhash('hex');
console.log(`File: ${file} Block: ${hash} Size: ${size}`);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment