<p>The other two answers explain what has to be done to discover doublespends. I'll try to explain how it works.</p>

<p><strong>There are no "balances", there are "coins"</strong><br>
A common misunderstanding is that there is a register of balances for addresses, and when you spend bitcoins, some of your balance gets deducted from your address. Actually, that's not a good description of what's happening. Under the hood, it's more akin to receiving a "coin" whenever you get paid, and drawing upon your available "coins" when spending.  </p>

<p><strong>Spending "coins"</strong><br>
When someone sends you a transaction, they create an "unspent transaction output (UTXO)". This UTXO is uniquely identifiable e.g. <em>the first output of the transaction with the id <code>X</code></em>. UTXOs are like coins: They have a specific value and can only be spent completely. To spend part of one, you have to give up the complete coin, get back change.</p>

<p><strong>Example:</strong><br>
You have two Unspent Transaction Outputs ("coins") worth 0.03 BTC and 0.09 BTC. They were created as "first output of transaction <code>X</code>" and "third output of transaction <code>Y</code>" respectively. You want to send 0.10 BTC to Bob.  </p>

<p>You cite these specific two UTXOs as inputs to your transaction <code>Z</code>. I.e. you order "I want to spend the first output of transaction X, and the third output of transaction Y. I'm converting them to first and second output of transaction Z. – Here are the signatures for <code>X.first</code> and <code>Y.third</code>.".</p>

<pre><code>X.first   0.03 BTC                       Z.first    0.10 BTC (spendable with Bob's signature)
                       ---- tx Z  ---&gt; 
Y.third   0.09 BTC                       Z.second   0.02 BTC (spendable with your own signature)
</code></pre>

<p>In this example, you use up your two "coins", to give one "coin" to Bob to pay 0.10 BTC, and get back the change as another new "coin" which goes to yourself. Your transaction <code>Z</code> spent <code>X.first</code> and <code>Y.third</code>, and converted them to two new "coins" <code>Z.first</code> and <code>Z.second</code>.</p>

<p>Now, as described by <a href="https://bitcoin.stackexchange.com/a/43561/5406">Greg</a> and <a href="https://bitcoin.stackexchange.com/a/43578/5406">Jestin</a>, every full node has worked through the whole blockchain. Every single transaction references one or more "coins" that get spent, and creates new "coins" as in the example.</p>

<p>While a node works through the blockchain, it keeps track of the "coins" that exist, and updates this database with every transaction that happens. The database is referred to as the Bitcoin network's "Unspent Transaction Output Set". So, when you're up to date, the node has a list of every "coin" that is available for spending.</p>

<p><strong>Back to the question</strong><br>
So, you sent 1 BTC to Bob one month ago. Now, you're trying to use the same "coins", <code>X.first</code> and <code>Y.third</code> a month later to send money to Carol. The nodes look at their "coin" database, and see that they are not in it. They can immediately say "Those coins have already been spent.".</p>

CC BY-SA 3.0
2016-10-11T09:20:13.570Z
Great answer, do you have more sources on the "balance" vs. "coin" misunderstanding?
Fabian Braun
CC BY-SA 3.0
2016-10-11T15:18:25.187Z
@GerdMarvin: Check out the following: [How is a wallet's balance computed?](http://bitcoin.stackexchange.com/q/22997/5406), [How does the network know my current balance?](http://bitcoin.stackexchange.com/q/13069/5406), [What is an “unspent output”?](http://bitcoin.stackexchange.com/q/4301/5406), [Can someone explain how the Bitcoin Blockchain works?](http://bitcoin.stackexchange.com/q/12427/5406) (second answer)
Murch
CC BY-SA 3.0
2016-04-02T08:24:11.283Z
2016-10-11T15:26:23.060Z
2017-04-13T12:47:34.187Z
Murch
<p>Yes, they do. Mining software indexes the blockchain database so that lookups like this are relatively quick (that is, they don't have to laboriously search through gigabytes of transactions).</p>

CC BY-SA 3.0
2016-03-31T22:14:01.347Z
2016-03-31T22:14:01.347Z
Greg Hewgill
<p>Yes, miners check the blockchain to ensure all new transactions are valid before including them into a block.  However, it also depends on your definition of "miner".  If you mean the people running ASIC miners as part of a mining pool, then no, they do not generally check for the validity of transactions or blocks.  They simply hash block headers to calculate nonces.  If you mean mining pool operators, then yes, they need to check the validity of transactions and blocks, otherwise the blocks their pool produces will be rejected by the rest of the network.</p>

<p>Also, miners are not the only nodes on the Bitcoin network that validate transactions.  Full nodes, which relay transactions through the network, also perform checks to ensure transactions are valid before relaying them to other nodes.  You would have a very hard time even getting your invalid transaction to a mining node, let alone having it included in a block.</p>

CC BY-SA 3.0
2016-04-01T22:14:15.867Z
2016-04-01T22:14:15.867Z
Jestin
<p>Assuming I spent 1BTC, then waited a few months and then spent it again. How is this detected? Do the miners go through the entire blockchain to assure the coin was never spent before?</p>

<doublespend>
How do miners detect double-spending?
frmsaul
# How do miners detect double-spending?

(Asked by: username_0 on 2016-03-31)

Assuming I spent 1BTC, then waited a few months and then spent it again. How is this detected? Do the miners go through the entire blockchain to assure the coin was never spent before?

**Tags:** doublespend


---

## 3 answers

### Answer by username_1 on 2016-04-02. Score: 7

The other two answers explain what has to be done to discover doublespends. I'll try to explain how it works.

**There are no "balances", there are "coins"**  
A common misunderstanding is that there is a register of balances for addresses, and when you spend bitcoins, some of your balance gets deducted from your address. Actually, that's not a good description of what's happening. Under the hood, it's more akin to receiving a "coin" whenever you get paid, and drawing upon your available "coins" when spending.

**Spending "coins"**  
When someone sends you a transaction, they create an "unspent transaction output (UTXO)". This UTXO is uniquely identifiable e.g. *the first output of the transaction with the id `X`*. UTXOs are like coins: They have a specific value and can only be spent completely. To spend part of one, you have to give up the complete coin, get back change.

**Example:**  
You have two Unspent Transaction Outputs ("coins") worth 0.03 BTC and 0.09 BTC. They were created as "first output of transaction `X`" and "third output of transaction `Y`" respectively. You want to send 0.10 BTC to Bob.

You cite these specific two UTXOs as inputs to your transaction `Z`. I.e. you order "I want to spend the first output of transaction X, and the third output of transaction Y. I'm converting them to first and second output of transaction Z. – Here are the signatures for `X.first` and `Y.third`.".

```
X.first   0.03 BTC                       Z.first    0.10 BTC (spendable with Bob's signature)
                       ---- tx Z  ---> 
Y.third   0.09 BTC                       Z.second   0.02 BTC (spendable with your own signature)

```

In this example, you use up your two "coins", to give one "coin" to Bob to pay 0.10 BTC, and get back the change as another new "coin" which goes to yourself. Your transaction `Z` spent `X.first` and `Y.third`, and converted them to two new "coins" `Z.first` and `Z.second`.

Now, as described by [username_3](https://bitcoin.stackexchange.com/a/43561/5406) and [username_4](https://bitcoin.stackexchange.com/a/43578/5406), every full node has worked through the whole blockchain. Every single transaction references one or more "coins" that get spent, and creates new "coins" as in the example.

While a node works through the blockchain, it keeps track of the "coins" that exist, and updates this database with every transaction that happens. The database is referred to as the Bitcoin network's "Unspent Transaction Output Set". So, when you're up to date, the node has a list of every "coin" that is available for spending.

**Back to the question**  
So, you sent 1 BTC to Bob one month ago. Now, you're trying to use the same "coins", `X.first` and `Y.third` a month later to send money to Carol. The nodes look at their "coin" database, and see that they are not in it. They can immediately say "Those coins have already been spent.".

> **Comments:**
> **username_2**: Great answer, do you have more sources on the "balance" vs. "coin" misunderstanding?  
> **username_1**: @GerdMarvin: Check out the following: [How is a wallet's balance computed?](http://bitcoin.stackexchange.com/q/22997/5406), [How does the network know my current balance?](http://bitcoin.stackexchange.com/q/13069/5406), [What is an “unspent output”?](http://bitcoin.stackexchange.com/q/4301/5406), [Can someone explain how the Bitcoin Blockchain works?](http://bitcoin.stackexchange.com/q/12427/5406) (second answer)  

---

### Answer by username_3 on 2016-03-31. Score: 5

Yes, they do. Mining software indexes the blockchain database so that lookups like this are relatively quick (that is, they don't have to laboriously search through gigabytes of transactions).


---

### Answer by username_4 on 2016-04-01. Score: 5

Yes, miners check the blockchain to ensure all new transactions are valid before including them into a block. However, it also depends on your definition of "miner". If you mean the people running ASIC miners as part of a mining pool, then no, they do not generally check for the validity of transactions or blocks. They simply hash block headers to calculate nonces. If you mean mining pool operators, then yes, they need to check the validity of transactions and blocks, otherwise the blocks their pool produces will be rejected by the rest of the network.

Also, miners are not the only nodes on the Bitcoin network that validate transactions. Full nodes, which relay transactions through the network, also perform checks to ensure transactions are valid before relaying them to other nodes. You would have a very hard time even getting your invalid transaction to a mining node, let alone having it included in a block.


---

