<p>Having sufficient proof of work is a <strong>necessary</strong> criteria for a block to be valid, but it is not a <strong>sufficient</strong> criteria for a block to be valid. The proof of work is used to limit the cadence of updates to the shared global state and as a mechanism to select the author of the next block in a distributed manner. Additionally, the block has to pass all the <a href="https://en.bitcoin.it/wiki/Protocol_rules#.22block.22_messages" rel="nofollow noreferrer">~40ish rules for blocks</a> to be valid in the first place.</p>
<p>A block that contains two conflicting transactions is invalid. A full node validating it will mark the UTXO as spent when it validates the first transaction, and when the second transaction refers to the same UTXO as its input, the UTXO is no longer available and the block gets rejected.</p>

CC BY-SA 4.0
2022-07-11T21:00:02.507Z
2022-07-11T22:41:09.723Z
2022-07-11T22:41:09.723Z
Murch
<p>If a block contains two conflicting transactions (a double spend) then the block is invalid, despite the proof of work being correct. Thus, the miner has wasted their work, and the block will be rejected by all validating nodes.</p>

CC BY-SA 4.0
2022-07-11T20:24:35.447Z
Can you elaborate how is block will get rejected by all validating nodes technically? Because I know how it rejects block when 2nd transaction is double spent after 1st transaction is already broadcasted to bitcoin ledger. But How the block will get rejected when 2 transaction which are double spent are included in same block and broadcasted (Which means those 2 transaction are technically broadcasted simultaneously )
Arjun Reddy
CC BY-SA 4.0
2022-07-11T20:36:53.893Z
The validating node marks the UTXO as spent when validating the first transaction, and when it tries to validate the second transaction, the UTXO referred to in the input is spent already and it throws the invalid block out.
Murch
CC BY-SA 4.0
2022-07-11T20:21:55.893Z
2022-07-11T20:21:55.893Z
meshcollider
<p>The complete block is transmitted as a whole to the fully validating node.
The validator starts checking the transactions one by one.</p>
<p><a href="https://github.com/bitcoin/bitcoin/blob/master/src/consensus/tx_check.cpp" rel="nofollow noreferrer">https://github.com/bitcoin/bitcoin/blob/master/src/consensus/tx_check.cpp</a></p>
<p>The relevant code that rejects block with double spends is:</p>
<pre><code>    // Check for duplicate inputs (see CVE-2018-17144)
    // While Consensus::CheckTxInputs does check if all inputs of a tx are available, and UpdateCoins marks all inputs
    // of a tx as spent, it does not check if the tx has duplicate inputs.
    // Failure to run this check will result in either a crash or an inflation bug, depending on the implementation of
    // the underlying coins database.
    std::set&lt;COutPoint&gt; vInOutPoints;
    for (const auto&amp; txin : tx.vin) {
        if (!vInOutPoints.insert(txin.prevout).second)
            return state.Invalid(TxValidationResult::TX_CONSENSUS, &quot;bad-txns-inputs-duplicate&quot;);
    }
</code></pre>

CC BY-SA 4.0
2022-07-18T19:23:12.913Z
This code doesn't check for doublespends per se, but rather that a UTXO was not spent twice within a single transaction.
Murch
CC BY-SA 4.0
2022-07-14T23:21:10.723Z
2022-07-14T23:21:10.723Z
Hugo Franklin
<p>Let's say Alice sends 5 BTC to Bob and Alice tries to make double spending by again sending those same 5 BTC inputs to carl and <strong>lets say that Alice is also a one of the miner node.</strong></p>
<ol>
<li><strong>Here Alice tries to take those 2 double spent transactions into a same single block</strong></li>
<li><strong>lets say Alice solved hash puzzle and broadcasted those 2 transactions in the same block</strong></li>
</ol>
<p>since in first transaction, inputs UTXO and output UTXO are valid and in second transaction which is of same block, even have input UTXO which is unspent, but it is double spent. when block is broadcasted those 2 transactions are broadcasted simultaneously,</p>
<p>Questions:</p>
<ul>
<li>how other nodes are going to validate those 2 simultaneously broadcasted transaction technically by bitcoin code(Where those 2 transaction in same block)?</li>
<li>How is this double spending is solved technically by bitcoin code, when 2 transactions are included in same block (When block is broadcasted then those 2 transactions are broadcasted simultaneously )?</li>
</ul>

<doublespend><block-validity>
Can miners double-spend with two transactions in one block?
Arjun Reddy
# Can miners double-spend with two transactions in one block?

(Asked by: username_0 on 2022-07-11)

Let's say Alice sends 5 BTC to Bob and Alice tries to make double spending by again sending those same 5 BTC inputs to carl and **lets say that Alice is also a one of the miner node.**

1. **Here Alice tries to take those 2 double spent transactions into a same single block**
2. **lets say Alice solved hash puzzle and broadcasted those 2 transactions in the same block**

since in first transaction, inputs UTXO and output UTXO are valid and in second transaction which is of same block, even have input UTXO which is unspent, but it is double spent. when block is broadcasted those 2 transactions are broadcasted simultaneously,

Questions:

* how other nodes are going to validate those 2 simultaneously broadcasted transaction technically by bitcoin code(Where those 2 transaction in same block)?
* How is this double spending is solved technically by bitcoin code, when 2 transactions are included in same block (When block is broadcasted then those 2 transactions are broadcasted simultaneously )?

**Tags:** doublespend, block-validity


---

## 3 answers

### Answer by username_1 on 2022-07-11. Score: 4

Having sufficient proof of work is a **necessary** criteria for a block to be valid, but it is not a **sufficient** criteria for a block to be valid. The proof of work is used to limit the cadence of updates to the shared global state and as a mechanism to select the author of the next block in a distributed manner. Additionally, the block has to pass all the [~40ish rules for blocks](https://en.bitcoin.it/wiki/Protocol_rules#.22block.22_messages) to be valid in the first place.

A block that contains two conflicting transactions is invalid. A full node validating it will mark the UTXO as spent when it validates the first transaction, and when the second transaction refers to the same UTXO as its input, the UTXO is no longer available and the block gets rejected.


---

### Answer by username_2 on 2022-07-11. Score: 3

If a block contains two conflicting transactions (a double spend) then the block is invalid, despite the proof of work being correct. Thus, the miner has wasted their work, and the block will be rejected by all validating nodes.

> **Comments:**
> **username_0**: Can you elaborate how is block will get rejected by all validating nodes technically? Because I know how it rejects block when 2nd transaction is double spent after 1st transaction is already broadcasted to bitcoin ledger. But How the block will get rejected when 2 transaction which are double spent are included in same block and broadcasted (Which means those 2 transaction are technically broadcasted simultaneously )  
> **username_1**: The validating node marks the UTXO as spent when validating the first transaction, and when it tries to validate the second transaction, the UTXO referred to in the input is spent already and it throws the invalid block out.  

---

### Answer by username_3 on 2022-07-14. Score: -1

The complete block is transmitted as a whole to the fully validating node.
The validator starts checking the transactions one by one.

<https://github.com/bitcoin/bitcoin/blob/master/src/consensus/tx_check.cpp>

The relevant code that rejects block with double spends is:

```
    // Check for duplicate inputs (see CVE-2018-17144)
    // While Consensus::CheckTxInputs does check if all inputs of a tx are available, and UpdateCoins marks all inputs
    // of a tx as spent, it does not check if the tx has duplicate inputs.
    // Failure to run this check will result in either a crash or an inflation bug, depending on the implementation of
    // the underlying coins database.
    std::set<COutPoint> vInOutPoints;
    for (const auto& txin : tx.vin) {
        if (!vInOutPoints.insert(txin.prevout).second)
            return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputs-duplicate");
    }

```

> **Comments:**
> **username_1**: This code doesn't check for doublespends per se, but rather that a UTXO was not spent twice within a single transaction.  

---

