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

<p>Sorting by timestamp isn't safe either.  See <a href="https://bitcoin.stackexchange.com/questions/21511/why-are-the-blocks-out-of-order?rq=1">Why are the blocks out of order?</a>  The protocol does not enforce accuracy of the timestamps, and a later block can legally have an earlier timestamp.  <a href="https://bitcoin.stackexchange.com/a/77964/5757">At last check</a> there were almost 14000 examples of this in the blockchain.</p>

<p>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.</p>

CC BY-SA 4.0
2018-09-17T14:30:14.910Z
2018-09-17T14:30:14.910Z
Nate Eldredge
<p>I parsed the block00000.dat using python code:</p>

<pre><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
      else:
        break
</code></pre>

<p>The full code is<br>
<a href="https://github.com/keviny/bitcoin-block-parser/blob/master/block.py" rel="nofollow noreferrer">https://github.com/keviny/bitcoin-block-parser/blob/master/block.py</a></p>

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

<p>For example:<br>
....<br>
258 00000000613da6433c0f6c61d8bcb17c39603dd4a8595142359905b18b84edcd
259 000000007964a0e8b97bbea93e937c63d4097c42ccb4db464b1ab3047c62869c
260 000000001a748e1c97226c59b9ddf79a54e0857116808a1b5610a52aef4ed5b5
261 0000000025f2bb2ee58597083e13079899ecc6f2bb7e8bfd57e66b324b05d654
262 00000000d80530efa37319e4b4508b0759fdab8ea8a81272cbe8986881489507<br>
....</p>

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

<p>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?</p>

<blockchain><blockchain.info><block>
Why the block's order in blockxxxxx.dat is different from blockchain.info
Carpemer
# Why the block's order in blockxxxxx.dat is different from blockchain.info

(Asked by: username_0 on 2018-09-17)

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
      else:
        break

```

The full code is  
<https://github.com/keviny/bitcoin-block-parser/blob/master/block.py>

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?

**Tags:** blockchain, blockchain.info, block


---

## 1 answers

### Answer by username_1 on 2018-09-17. Score: 2 | Accepted answer

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?](https://bitcoin.stackexchange.com/questions/21511/why-are-the-blocks-out-of-order?rq=1) The protocol does not enforce accuracy of the timestamps, and a later block can legally have an earlier timestamp. [At last check](https://bitcoin.stackexchange.com/a/77964/5757) 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.


---

