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

<p>When she makes a transaction to Bob or Joe, she uses those UTXOs as <em>input</em> 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.</p>

<p>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 <em>spent</em>, 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.</p>

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

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

CC BY-SA 3.0
2017-07-13T02:06:45.853Z
Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/62113/discussion-between-meshcollider-and-omega).
meshcollider
CC BY-SA 3.0
2017-07-17T03:37:08.083Z
@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
CC BY-SA 3.0
2017-07-17T06:34:36.747Z
@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?
rb612
CC BY-SA 3.0
2017-07-17T10:29:55.327Z
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?"
meshcollider
CC BY-SA 3.0
2017-07-13T00:13:30.703Z
2017-07-17T03:48:13.260Z
2017-07-17T03:48:13.260Z
meshcollider
<p>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.</p>

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

<p>So say Alice has only $10 and gives $10 to Joe and $10 to Bob.</p>

<p>Two entries are sent to the network which is <code>Alice-&gt;$10-&gt;Joe</code> and <code>Alice-&gt;$10-&gt;Bob</code>.</p>

<p>Now every node will have pending transactions of either <code>Alice-&gt;$10-&gt;Joe</code> or <code>Alice-&gt;$10-&gt;Bob</code>. </p>

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

<p>The miners will keep hashing the <code>Alice-&gt;$10-&gt;Joe</code> or <code>Alice-&gt;$10-&gt;Bob</code> depending on which one the miner has. They keep doing it until a miner has a hash that is considered valid.</p>

<p>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 <code>Alice-&gt;$10-&gt;Bob</code> got mined first, and added to all nodes.</p>

<p>Now the blockchain knows Alice has no money left.</p>

<p>Then <code>Alice-&gt;$10-&gt;Joe</code> gets mined, and sent to everyone, but they all reject it because Alice has no money left (this part I'm not sure about).</p>

<p>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?</p>

<p>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 <code>Alice-&gt;$10-&gt;Joe</code> and not <code>Alice-&gt;$10-&gt;Bob</code>?</p>

CC BY-SA 3.0
2017-07-17T03:34:42.180Z
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
<doublespend><utxo>
How does Bitcoin protect against doublespends?
omega
# How does Bitcoin protect against doublespends?

(Asked by: username_0 on 2017-07-12)

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

**Tags:** doublespend, utxo

> **Comments:**
> **username_1**: Hello username_0, 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.  

---

## 1 answers

### Answer by username_2 on 2017-07-13. Score: 4 | Accepted answer

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.

> **Comments:**
> **username_2**: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/62113/discussion-between-username_2-and-username_0).  
> **username_1**: @username_0: If you have another question, please create a second topic. :) —  username_0, 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.  
> **username_3**: @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?  
> **username_2**: 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?"  

---

