Hello I'm using a python script to actually retrieve information from a blk file and to read data.

import datetime

# Read block !
file = open("REDACTED\blocks\\blk00000.dat", 'rb')

numberOfBlocks = 0
i = 0
toAnalyse = 5000

while i < toAnalyse:  

    block_magic_bytes = file.read(4)
    if b''==block_magic_bytes :
    block_size = file.read(4)
    block_size_int = int.from_bytes(block_size,'little')
    block_version = file.read(4)
    block_lastblock = file.read(32)
    block_lastblock_little_endian = int.from_bytes(block_lastblock,'little') 

    # merkle root

    block_time = int.from_bytes(file.read(4),'little')


    block_nonce = int.from_bytes(file.read(4),'little')

    # Prints some information
    print("Block number   : " + str(numberOfBlocks))

    print("Magic bytes    : " + block_magic_bytes.hex(" ").upper())
    print("Block size hex : " + block_size.hex(" ").upper())
    print("Block size int : " + str(block_size_int))
    print("Block version  : " + block_version.hex(" "))
    print( f'Block preBlock : {block_lastblock_little_endian:064x}' )
    print("Block time     : " + str(datetime.datetime.fromtimestamp(block_time)))
    print("Block nonce    : " + str(block_nonce))

    # Jumps to next magic_bytes

print("Last block read")

But when I read the block 486 in the blk file I get this :

Block number   : 486
Magic bytes    : F9 BE B4 D9
Block size hex : D8 00 00 00
Block size int : 216
Block version  : 01 00 00 00
Block preBlock : 00000000806df68baab17e49e567d4211177fef4849ffd8242d095c6a1169f45
Block time     : 2009-01-14 22:27:29
Block nonce    : 2063568441

As I understand the BTC blockchain the previous hash should be the hash of the block with height 485, but when I look using an explorer this is the hash of block 499 : https://www.blockchain.com/btc/block/00000000806df68baab17e49e567d4211177fef4849ffd8242d095c6a1169f45

Is it that blocks aren't in order in blk files ? Are my blk files wrong ? I downloaded them using bitcoin-core. Are some blocks actually not included in the overall blockchain ?

I would appreciate any pointers,tips or help of course ^^

1 Answer 1


Is it that blocks aren't in order in blk files ?

This is true. For example, during the initial-block-loading phase, your node may be concurrently collecting different ranges of blocks from multiple peers. So they can arrive out of order. You also have dead forks.

As Murch noted: "the .blk files store block data in the order that it is received by the node, rather than in the order it was requested or the order of their heights."

So nodes, or other programs processing the block data files probably all need to create indexes of blocks by block-ID and height as well as indexes of transactions by transaction-ID etc. Some indexes might be of all blocks and others might be only of blocks in the currently active chain.

I can't tell whether the above accounts for the whole of your issue.

  • Thanks for the explanation, so theoritically I have to parse and hash every block. And then looking into another block if the hash matches starting from block with height 0 ? But dead forks aren't on the main net are they ? And thanks for the python tip :D ^^ Sep 2, 2022 at 11:03
  • Yes, you need to do something like build an index of blocks with the block hash as key. Since occasionally two miners produce blocks at the same height, your BLK files will contain both even after one is selected as constituting part of the main chain and the other marked stale. Sep 2, 2022 at 11:59
  • It's in there, but I feel it could be perhaps stated a bit more clearly that the .blk files store block data in the order that it is received by the node, rather than in the order it was requested or the order of their heights.
    – Murch
    Sep 7, 2022 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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