2

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

3 Answers 3

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

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.

2
  • 1
    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 ) Jul 11, 2022 at 20:24
  • 6
    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
    Jul 11, 2022 at 20:36
-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");
    }
1
  • This code doesn't check for doublespends per se, but rather that a UTXO was not spent twice within a single transaction.
    – Murch
    Jul 18, 2022 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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