8

Does the Bitcoin protocol guarantee that within a valid block, if transaction B spends the outputs of transaction A, A will always precede B?

Or is it possible to have B before A?

Flavien
  • 1,017
  • 1
  • 9
  • 17

1 Answers1

7

The Bitcoin protocol requires transactions to appear in topological order in the blockchain. Given two transactions A and B where B spends an output of A:

  • Both A and B may be included in the same block.
  • A must precede B in the transaction list.

Colloquially, a miner will never include a transaction in a block template until the transaction's requirements are met. Since B spends an output of A, B is not valid until A has created said output. To satisfy this dependency, B can only be included after A was included.

Also see: Can multiple transactions transferring the same bitcoin be done in one block?

Murch
  • 71,155
  • 33
  • 180
  • 600
  • I cannot understand this answer it seems to contradict itself. It says 1) all transactions in a block happen at the same time, 2) A & B can be in same block but they will stand in chronological order. 2) means that 1) is not true, 2) means that transactions in a block are considered to happen in the order they are listed, not at the same time. Fix!?!? – mwengler Aug 08 '14 at 22:41
  • They don't have to be in different blocks, i.e. they will all be timestamped at the same time. It doesn't even matter if a miner receives knowledge of B first. However, if a miner were trying to first determine the validity of B, he would find it invalid, as he doesn't know about the transaction output created by A yet. Thus, even though the transactions can happen at the in the same block, the protocol enforces that the transactions will be written out in the block in the correct order. – Murch Aug 08 '14 at 23:03
  • I am curious about unrelated transactions - how are they ordered? Surely a timestamp is insufficient? If it is a UUID, how is it ordered? – Angad Oct 11 '19 at 07:45
  • They don't need to be in a specific order, but some miners seem to keep them in the same order they selected them into the block, i.e. ordered by the transactions' fee rates paid. – Murch Oct 11 '19 at 14:52