2

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

inersha
  • 3,063
  • 1
  • 17
  • 41

1 Answers1

3

Yes, it is possible.

According to 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.

Nate Eldredge
  • 23,040
  • 3
  • 40
  • 80