In what step/process does Bitcoin prevent double-spending?

Pls answer without searching anywhere or consulting the code.

Reply to this note

Please Login to reply.

Discussion

Probably in mining a block. They gotta ensure all inputs havent been spent yet right? Ive submitted transactions spending from the same utxo and the miner goes with the highest fee rate no matter which one I broadcast first.

They decide how to resolve a potential double spend. But a double spend is really when conflicting txs append to the blockchain. So validating blocks is the preventative measure.

Having up-to-date knowledge of the blockchain means each client knows which unspent transactions are available. When the next block arrives, each transaction's inputs can be compared to available outputs (unspent txns). Each unspent transaction is processed as a whole, so each is registered as spent exactly once, and a new transaction may redirect some "change output" back to the same owner if not everything is transferred.

Each bitcoin client will itself qualify each block to see if it checks out completely and onlu then is it accepted as part of the blockchain. That way, the blockchain can only be extended with valid blocks, i.e. no cheating/fraud permitted, verified by each bitcoin client. (Referring here to full clients that themselves process the blockchain.)

When a new block comes in each node validates that the transactions in that block spend UTXOs that exist in the UTXO set. If a tx input does not exist in the UTXO set the block is invalid. But you'd have a hard time even constructing such a block template under normal circumstances because nodes also validate each tx input when they first see a transaction come across the p2p network. If the input does not exist in the UTXO set or in the mempool (or I suppose in the same package now that packages are a thing) that tx will get dropped and won't propagate through the network. So really it happens at the block level but practically speaking it happens at the mempool level.

During the validation process.

The successful miner will have validated transactions and in the case of a double spend where all conflicting transactions are still unconfirmed, the miner will have chosen one of them based on their own criteria such as fees, other associated unconfirmed transactions, and their fees. That is how a double spend is RESOLVED.

A double spend is PREVENTED when nodes validate blocks and ensure that there are no double spends present within the blockchain. If a double spend is detected, then the block is not appended to the user's blockchain and the double spend is prevented. This happens the same on all nodes across the network.

Your node defines the rules of what constitutes an unspent coin, a coin is initially created in a valid blocks first transaction, and spent by a transaction with a valid witness to a new coin with a locking script and amount.

For any transaction, your node checks that the inputs are valid previous transaction outputs that have not been spent, simply by looking if the coin ID (prev TX hash + output index) is in the utxo list, if not: invalid double spending transaction, if yes: valid transaction in block, and remove coin ID from utxo set.