1

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 ?

2

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.

  • 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 Jun 6 at 16:04
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.

  • 1
    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 Jun 6 at 12:42
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.