Last active
February 6, 2019 01:43
-
-
Save braydonf/528a51785f0f5641464d172696cd2112 to your computer and use it in GitHub Desktop.
Read bitcoin blocks from disk with bcoin
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
// 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