4

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!

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).

  • 3
    To 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 Apr 11 '17 at 7:10
  • @PieterWuille many thanks for the clarification. – Bobo Apr 11 '17 at 13:57
  • @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 Apr 12 '17 at 2:56
  • 3
    I 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 Apr 12 '17 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.