Bitcoind block chain structure

4

0

I am trying to understand the structure of the underlying block chain stored in the core implementation (or rather just the BLK files used by a fullnode). My question is ; does the block chain store the complete tree structure , i.e all the forks which were encountered? Or does it purge the entries once a fork is resolved.

If i were to read the blk files sequentially , can i assume that every single block read is correctly ordered and no forks are present?

Thanks!

Bobo

Posted 2017-04-10T12:56:44.837

Reputation: 393

Answers

3

the BLK files are not an API. You shouldn't directly read those files. What you should do is requesting the headers/block via the JSON RPC interface (or the REST interface).

You can get the genesis block hash via:

bitcoin-cli getblockhash 0

Then you can get a block (without transactions):

bitcoin-cli getblock <hash>

Then get the next block by looking at the nextblockhash item in the block JSON response, etc.

Also, consider using the REST interface. There you can get binary responses which are much faster (no JSON encoding/decoding overhead).

Jonas Schnelli

Posted 2017-04-10T12:56:44.837

Reputation: 5 465

3To answer the question more directly: if you read the blk files directly, you would find the chain's blocks out of order, potentially with gaps in between, including forked off blocks, and the format may change in future versions.Pieter Wuille 2017-04-11T07:10:04.980

@PieterWuille many thanks for the clarification.Bobo 2017-04-11T13:57:41.120

@PieterWuille you'r wrong. While reading blk files with some math you can find order of blockchain exclude of any gaps or forks. Perhaps.D L 2017-04-12T02:56:15.177

3I wrote that code in Core. Yes, you can scan for all blocks and afterwards figure out the longest chain, and filter out garbage. But that's not what the question was about. On disk, the blocks are out of order.Pieter Wuille 2017-04-12T06:23:23.093