<p>Yes, it is possible.</p>

<p>According to <a href="https://bitcoin.stackexchange.com/questions/23035/order-of-transactions-within-a-block">Order of transactions within a block</a>, transactions within a block must be listed in the "correct" order: a transaction may only spend outputs of transactions listed earlier in the same block, or in previous blocks.  </p>

<p>However, blocks do not necessarily appear in the blk*.dat files in their proper order, but rather in the order in which they were downloaded.  If A,B are two blocks, and the <code>prevBlockHash</code> of B is the hash of A, it's still possible that B appears before A in the file.  In that case, a valid transaction may reference outputs from a transaction that appears later in the file, though earlier in the block chain.</p>

<p>It's also worth noting that blk*.dat can contain orphaned blocks.  So you may find two different transactions in the blk*.dat files that spend the same input.  You'll then have to determine which of them is in a block that is part of the longest chain (measured by total work), and which is on an orphan side chain.</p>

CC BY-SA 3.0
2016-09-24T17:14:37.487Z
2016-09-24T17:14:37.487Z
2017-04-13T12:47:33.583Z
Nate Eldredge
<p>I'm reading through each blk*.dat file in order.</p>

<p>As I'm reading through each transaction, is it possible that I will read a transaction that is referencing an output (as an input) that I haven't encountered yet?</p>

<p>For example:</p>

<pre><code>|--------|
| header |
|--------|  
| tx1    | 
| tx2    | &lt;- This transaction is referencing inputs I haven't encountered...
| tx3    |
| tx4    | &lt;- ...because the outputs are created in this transaction.
| tx5    |
|--------|
</code></pre>

<p>Furthermore, if this is true, it possible that the required inputs will be found in a transaction <em>in a future block</em> (and not just in a transaction further down the current block)?</p>

<transactions>
Reading transactions from blk*.dat files
inersha
# Reading transactions from blk*.dat files

(Asked by: username_0 on 2016-09-24)

I'm reading through each blk*.dat file in order.
As I'm reading through each transaction, is it possible that I will read a transaction that is referencing an output (as an input) that I haven't encountered yet?
For example:
|--------|
| header |
|--------|  
| tx1    | 
| tx2    | <- This transaction is referencing inputs I haven't encountered...
| tx3    |
| tx4    | <- ...because the outputs are created in this transaction.
| tx5    |
|--------|

Furthermore, if this is true, it possible that the required inputs will be found in a transaction in a future block (and not just in a transaction further down the current block)?

**Tags:** transactions


---

## 1 answers

### Answer by username_1 on 2016-09-24. Score: 3 | Accepted answer

Yes, it is possible.

According to [Order of transactions within a block](https://bitcoin.stackexchange.com/questions/23035/order-of-transactions-within-a-block), transactions within a block must be listed in the "correct" order: a transaction may only spend outputs of transactions listed earlier in the same block, or in previous blocks.

However, blocks do not necessarily appear in the blk\*.dat files in their proper order, but rather in the order in which they were downloaded. If A,B are two blocks, and the `prevBlockHash` of B is the hash of A, it's still possible that B appears before A in the file. In that case, a valid transaction may reference outputs from a transaction that appears later in the file, though earlier in the block chain.

It's also worth noting that blk\*.dat can contain orphaned blocks. So you may find two different transactions in the blk\*.dat files that spend the same input. You'll then have to determine which of them is in a block that is part of the longest chain (measured by total work), and which is on an orphan side chain.


---

