I parsed the block00000.dat using python code:

class BlockFile:
  def __init__(self, block_filename):
    self.block_filename = block_filename
    self.blockchain = open(block_filename, 'rb', buffering=16*1024*1024)

  def get_next_block(self):
    while True:
      block = Block(self.blockchain)
      if block.is_ready:
        yield block

The full code is

It is very strange that the order of block in block00000 in different from the number in blockchain.info.

For example:
258 00000000613da6433c0f6c61d8bcb17c39603dd4a8595142359905b18b84edcd 259 000000007964a0e8b97bbea93e937c63d4097c42ccb4db464b1ab3047c62869c 260 000000001a748e1c97226c59b9ddf79a54e0857116808a1b5610a52aef4ed5b5 261 0000000025f2bb2ee58597083e13079899ecc6f2bb7e8bfd57e66b324b05d654 262 00000000d80530efa37319e4b4508b0759fdab8ea8a81272cbe8986881489507

The # in blockchain.info is:
258->block #257 (it is ok, because we use different start)
259->block #281
260->block #258
261->block #282
262->block #297

Is this by design, and I need to iterate all the blocks in block0000.dat, then sort by the time in blockheader to get the right block order?

1 Answer 1


The blkxxxx.dat files contain the blocks in the order that they were downloaded from peers. Since the client requests blocks from many different peers in parallel, and different peers may respond at different speeds, it is quite possible for blocks to be downloaded out of order. (Headers are downloaded first, as a separate step, so the client knows what hashes to request before it gets the full blocks.)

Sorting by timestamp isn't safe either. See Why are the blocks out of order? The protocol does not enforce accuracy of the timestamps, and a later block can legally have an earlier timestamp. At last check there were almost 14000 examples of this in the blockchain.

The only correct way to sort them is to actually reconstruct the chain, using the "previous" field of each block's header to identify the block that should come before it. This will also help you identify and remove orphan branches and disconnected blocks.

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.