# How does Bitcoin protect against doublespends?

I am trying to understand the blockchain and I have read a bunch of these explanations, but they all don't address some of the questions I am still wondering about. So first I'll explain what I know, and then list out some questions.

There is a ledger with everyone's transactions on it. The ledger is duplicated on each node in the network. Some nodes are `miners`. Nodes just store them and have other things read and update it. Miners create blocks and send to all nodes.

So say Alice has only \$10 and gives \$10 to Joe and \$10 to Bob.

Two entries are sent to the network which is `Alice->\$10->Joe` and `Alice->\$10->Bob`.

Now every node will have pending transactions of either `Alice->\$10->Joe` or `Alice->\$10->Bob`.

A consensus happens to decide which of the two should be added first. (is it to figure out which happens first and second or which to accept and which to reject like not add to chain at all?)

The miners will keep hashing the `Alice->\$10->Joe` or `Alice->\$10->Bob` depending on which one the miner has. They keep doing it until a miner has a hash that is considered valid.

For the first valid hash of a miner, that miner sends that transactions to every node as a valid block, and all nodes just add it to their chain if it looks valid (they check too). Lets say `Alice->\$10->Bob` got mined first, and added to all nodes.

Now the blockchain knows Alice has no money left.

Then `Alice->\$10->Joe` gets mined, and sent to everyone, but they all reject it because Alice has no money left (this part I'm not sure about).

Or is it that both transactions get mined, but which ever the majority of miners are for a certain transaction after 10 minutes, that transaction is added and the other is rejected?

I'm still confused about how the consensus part works. How the majority part works and how things get mined. How does it work, if only 1 unique transaction goes in the network like `Alice->\$10->Joe` and not `Alice->\$10->Bob`?

• Hello omega, I've edited the title and tags of your question to capture what I perceive to be the main topic in your question. I hope I got that right, but please feel free to further edit or amend your question if I didn't.
– Murch
Jul 17 '17 at 3:34

No one on the network has a 'balance', they just have the keys to the outputs of previous transactions which they haven't spent yet. At the start, Alice holds the keys to some Unspent Transaction Outputs (UTXOs), totalling a value of \$10 bitcoin. That's what you mean when you say she has \$10.

When she makes a transaction to Bob or Joe, she uses those UTXOs as input for the new transaction. But because she only has \$10 worth of UTXOs, if she uses the same UTXOs in both transactions, only one of them can be valid because you can't use the same UTXOs twice. So only one of the two transactions can be added to the chain, correct. The one which is added will just be the one which the miner of the block saw first usually. Some miners might see the transaction to Joe first, and try to mine that into a block, while others may see the one to Bob and try to mine that into a block.

The consensus is just whichever miner finds a valid block first, then the transaction that miner included (let's say Bob's) will be part of the longest valid blockchain and all the other nodes will accept it. Nodes trust the longest of the valid blockchains (note that it has to be valid, no honest node will accept invalid blocks or mine on top of an invalid chain to extend it), so if that longest chain contains the transaction to Bob, Alice's previously unspent outputs will now be deemed spent, and instead Bob will have the keys to the new outputs of the transaction (because Alice used his public key/bitcoin address). The nodes will then drop the transaction to Joe because it is now invalid, and Bob has his \$10.

For an attacker to change which transaction was accepted after it has been mined into the block chain, he would have to mine an alternative chain containing the other transaction, and race against the rest of the network to make his chain the longest. He will be the only miner to extend the chain if it is shorter, other miners will focus on the longest chain, so he would have to have at least 51% of the mining power of the network to extend his chain faster than the honest miners extend the accepted chain.

It's the same behaviour as if only one unique transaction was sent to the network, in that case all the miners will be trying to include that one instead of some trying to include the different one, so there won't be any outcome uncertainty.

• Jul 13 '17 at 2:06
• @omega: If you have another question, please create a second topic. :) — omega, MeshCollider: If all of these were just to ask for more clarification, please add the important parts to the question and answer respectively and then remove obsolete comments. Thank you.
– Murch
Jul 17 '17 at 3:37
• @MeshCollider this is an awesome explanation - I'm personally curious about what you meant regarding the last paragraph. How is it the same behavior as if only one unique transaction was sent? Jul 17 '17 at 6:34
• That last paragraph was just an answer to the last part of his question, "How does it work, if only 1 unique transaction goes in the network like Alice->\$10->Joe and not Alice->\$10->Bob?" Jul 17 '17 at 10:29