<p>Addition to Bolton's answer: Transactions in a block can be in any order, given that if a transaction spends another's output, the parent transaction must come before the child tx and the coinbase transaction (collecting the block reward) comes first.</p>

CC BY-SA 4.0
2019-06-06T16:04:17.317Z
I would not mention the coinbase transaction, as its ordering is quite irrelevant to the question. tx3 can spend tx2's outputs but no tx can spend coinbase's outputs for another 100 blocks, so its position is kind of besides the point.
Giszmo
CC BY-SA 4.0
2019-06-06T07:21:40.877Z
2019-06-06T07:21:40.877Z
MCCCS
<p>Bitcoin and other blockchain protocols enforce the constraint that any valid chain cannot have double spend UTXOs. If a miner were to include two transactions in a block both spending the same UTXO, the <em>entire block</em> would be invalid, and no other nodes in the network would accept it. If you were to create two transactions from the same UTXO and send them both out to the network, it would be up to the miners to choose which one to include. Some miners will prefer transactions that pay more fees, so those will tend to be included over the competing spend but most miners still adhere to "first seen safe", which means that unless a transaction signals being replaceable (replace by fee), it will get included over a later, higher fee version.</p>

CC BY-SA 4.0
2019-06-06T12:42:43.650Z
A lot of nodes and miners still manage their mempool by adhering to the "first seen" rule, so it's not necessarily the transaction with the highest fee rate but might just be the first version that gets included.
Murch
CC BY-SA 4.0
2019-06-06T06:02:41.800Z
2019-06-07T04:04:37.523Z
2019-06-07T04:04:37.523Z
Bolton Bailey
<p>If you create two transactions that use some of the same UTXO as inputs, every node will perceive only one of them as valid. By default, this will be the one that nodes first see: once a node learns about a transaction, it considers the UTXO used in the inputs to be spent from the perspective of the mempool. When the node sees the second transaction, it considers the second transaction invalid for attempting to claim inputs that are already marked as used.</p>

<p>When replace-by-fee (rbf) was signaled in advance, the second may replace the first, but then the first is evicted from the node's mempool.</p>

<p>In any case, there will never be more than one transaction spending the same input considered for block inclusion at the same time.</p>

CC BY-SA 4.0
2019-06-07T08:59:46.203Z
2019-06-07T08:59:46.203Z
Murch
<p>Let's say, I am trying to do a double spend by creating two transactions almost immediately, is it possible that both the transactions are added in one block ? If yes, then which transaction is valid and which is invalid ? Also is there any specific order in which miners gather transactions in a block before starting to find proof of work ?</p>

<blockchain><utxo>
What is the order in which UTXOs are sorted in a block?
Taral
# What is the order in which UTXOs are sorted in a block?

(Asked by: username_0 on 2019-06-06)

Let's say, I am trying to do a double spend by creating two transactions almost immediately, is it possible that both the transactions are added in one block ? If yes, then which transaction is valid and which is invalid ? Also is there any specific order in which miners gather transactions in a block before starting to find proof of work ?

**Tags:** blockchain, utxo


---

## 3 answers

### Answer by username_1 on 2019-06-06. Score: 2

Addition to username_3's answer: Transactions in a block can be in any order, given that if a transaction spends another's output, the parent transaction must come before the child tx and the coinbase transaction (collecting the block reward) comes first.

> **Comments:**
> **username_2**: I would not mention the coinbase transaction, as its ordering is quite irrelevant to the question. tx3 can spend tx2's outputs but no tx can spend coinbase's outputs for another 100 blocks, so its position is kind of besides the point.  

---

### Answer by username_3 on 2019-06-06. Score: 1

Bitcoin and other blockchain protocols enforce the constraint that any valid chain cannot have double spend UTXOs. If a miner were to include two transactions in a block both spending the same UTXO, the *entire block* would be invalid, and no other nodes in the network would accept it. If you were to create two transactions from the same UTXO and send them both out to the network, it would be up to the miners to choose which one to include. Some miners will prefer transactions that pay more fees, so those will tend to be included over the competing spend but most miners still adhere to "first seen safe", which means that unless a transaction signals being replaceable (replace by fee), it will get included over a later, higher fee version.

> **Comments:**
> **username_4**: A lot of nodes and miners still manage their mempool by adhering to the "first seen" rule, so it's not necessarily the transaction with the highest fee rate but might just be the first version that gets included.  

---

### Answer by username_4 on 2019-06-07. Score: 1

If you create two transactions that use some of the same UTXO as inputs, every node will perceive only one of them as valid. By default, this will be the one that nodes first see: once a node learns about a transaction, it considers the UTXO used in the inputs to be spent from the perspective of the mempool. When the node sees the second transaction, it considers the second transaction invalid for attempting to claim inputs that are already marked as used.

When replace-by-fee (rbf) was signaled in advance, the second may replace the first, but then the first is evicted from the node's mempool.

In any case, there will never be more than one transaction spending the same input considered for block inclusion at the same time.


---

