<p>In a bitcoin transaction, A's address is not scanned for spendable Unspent Transaction Outputs (UTXOs). Full nodes simply check if the inputs of the transaction are really unspent. There is no scanning of addresses involved. </p>

<p>UTXOs are stored as the transaction id (txid) and the output index of the transaction that created them. You cannot reference the outputs to be spent by address, the address merely specifies which private key must sign the input script in order to claim the UTXO. </p>

<p>Suppose you advertise <code>addr1</code> for donations, and you receive 3 donations D1,D2 and D3 on that address. You have 3 UTXOs and not 1. The only thing is that they can be claimed by same private key. Hence, there is no looking around for previous transactions. Wallet has to specify which UTXOs it is consuming.</p>

<p>SPV allows a lightweight client to verify that a transaction is included in the Bitcoin blockchain, without downloading the entire blockchain. To verify that a transaction is in a block, a SPV client requests a proof of inclusion, in the form of a Merkle tree branch. It does not check for double-spend on it's own. Rather it relies on the full node servers it is connected to do so. To prevent from being cheated from single server, they connect to multiple servers to get the block headers.
From Bitcoin Wiki:</p>

<blockquote>
  <p>The level of difficulty required to obtain confidence the remote node is not feeding you fictional transactions depends on your threat model. If you are connecting to a node that is known to be reliable, the difficulty doesn't matter. If you want to pick a random node, the cost for an attacker to mine a block sequence containing a bogus transaction should be higher than the value to be obtained by defrauding you. By changing how deeply buried the block must be, you can trade off confirmation time vs cost of an attack. </p>
</blockquote>

<p>You might also want to look into Fraud Proofs which would allow full nodes to prove that information propagated on the network is invalid, e.g. allow full nodes to point out double-spends to thin clients. </p>

<p>Another Reference: <a href="https://en.bitcoin.it/wiki/Thin_Client_Security" rel="nofollow noreferrer">Thin Client Security</a></p>

CC BY-SA 3.0
2017-05-10T09:39:39.537Z
one clarification: Is it possible to specify the how much fraction of each donation I can spend? For eg: I want to spend X BTC in total. It can be fetched in many ways from D1, D2 and D3.   X = a*D1+b*D2+c*D3 and (a,b,c) is not unique. Does one specify (a,b,c) while transacting for X BTC?
honeybadger
CC BY-SA 3.0
2017-05-10T11:17:06.700Z
All the UTXO in the input to transactions have to be consumed. 
The transaction would look something like. 
`Inputs: D1,D2 and D3.`
`Output: D4 and X. `

with condition(D4<= D1 + D2 + D3 - X)
Note that D1, D2 and D3 are no longer UTXO. All inputs to transactions must be completely consumed. Instead we have 2 new UTXO, namely X and D4.
sanket1729
CC BY-SA 3.0
2017-05-11T03:07:21.790Z
The donation amounts D1,D2 and D3 are not mixed. If X <= D1,D2,D3, then  X BTC are transacted only from one of {D1,D2,D3}. Essentially after the BTC are spent, the contents of the wallet would look like {D1-X,D2,D3}, assuming X is consumed from D1. 
Please refer this link: 
https://www.cryptocoinsnews.com/bitcoin-transaction-really-works/
honeybadger
CC BY-SA 3.0
2017-05-11T04:50:04.263Z
Yes, there are many ways to spend inputs. In your example, it would not make sense to spend D2 and D3 since X<=D1. In a bitcoin transaction, all input UTXO's must be completely consumed. If you have UTXO's {D1,D2,D3,D4 ..} and you can choose any subset of it on condition that sum is greater than X. Say, you choose D2 and D4. The new UXTO set would be {D1,D3,O1 and O2} where O1 and O2 are outputs. Here, D1 and D3 are consumed
All UTXO's which are input to Transaction must be completely spent. The article you linked explains the same.
sanket1729
CC BY-SA 3.0
2017-05-11T05:02:06.203Z
This is not an answer but a clarification. It does not address the problem of how SPV clients are secure against double spends.
Pieter Wuille
CC BY-SA 3.0
2017-05-11T06:10:16.163Z
Edited the answer. Please comment if anything is incorrect/missing.
sanket1729
CC BY-SA 3.0
2017-05-11T09:35:27.603Z
Now it is just wrong. The merkle path proves that a transaction was included in a block. It cannot prove that it is not a double spend.
Pieter Wuille
CC BY-SA 3.0
2017-05-11T09:55:27.607Z
@Pieter: You are right. It does not prove that it is not a double-spend. Ideally, one has to wait for at least 6 confirmations. However the rest of  SanketKanjalkar 's explanation is correct.
honeybadger
CC BY-SA 3.0
2017-05-11T10:14:33.377Z
Hello, I already mentioned in the answer that it cannot check double spend on it's own but relies on connected full nodes.
sanket1729
CC BY-SA 3.0
2017-05-11T10:37:43.133Z
@Pieter, can you please help me understand what part of the answer is wrong?
sanket1729
CC BY-SA 3.0
2017-05-11T15:35:41.957Z
I apologize, and misread.
Pieter Wuille
CC BY-SA 3.0
2017-05-12T14:31:54.190Z
Hi Sanket,
Nice answer! I've made a few small edits to your post to fix terminology, or make it a bit clearer. Please review my edit and check if it fits what you wanted your answer to say, otherwise please feel free to roll back or adapt further.
Welcome to Bitcoin.Stackexchange.com.  :)
Murch
CC BY-SA 3.0
2017-05-10T07:41:49.230Z
2017-11-25T01:54:38.843Z
2017-11-25T01:54:38.843Z
sanket1729
<p>I am new to Bitcoins. I have read about how double spending is prevented in Bitcoins. </p>

<p>Suppose  A, a peer, receives 'B' BTCs from various transactions: B1,B2,B3, etc. Also, A had spent 'S' BTCs in various transactions S1,S2,S3, etc.  Now, whenever a new transaction is initiated from A to B (eg: A wants to transfer X bitcoins to B), the transaction has to be verified as follows. </p>

<p>Fullnodes have the Unspent Transactions Output (UTXO) database. A's address is scanned in this database and checked if A has any unspent bitcoins. If there are at least X unspent bitcoins against A's address, then the transaction is legitimate.</p>

<p>Q 1) Where does SPV and Merkel trees come into picture during the above-mentioned verification process? I have read elsewhere on the internet that SPV nodes will query for the previous transactions B1,B2,B3, .. and S1,S2,S3 &amp; check if whether A's claim is valid or not.</p>

<p>Q 2) How many previous transactions do usually SPVs look into? My understanding is that SPVs have to look at all the transactions associated with A's address. What if A does not input all its previous transactions? (Eg: A hides its some of its spending transactions, say, S1) </p>

<doublespend><spv>
How does SPV prevent double spending of UTXO?
honeybadger
# How does SPV prevent double spending of UTXO?

(Asked by: username_0 on 2017-05-08)

I am new to Bitcoins. I have read about how double spending is prevented in Bitcoins.

Suppose A, a peer, receives 'B' BTCs from various transactions: B1,B2,B3, etc. Also, A had spent 'S' BTCs in various transactions S1,S2,S3, etc. Now, whenever a new transaction is initiated from A to B (eg: A wants to transfer X bitcoins to B), the transaction has to be verified as follows.

Fullnodes have the Unspent Transactions Output (UTXO) database. A's address is scanned in this database and checked if A has any unspent bitcoins. If there are at least X unspent bitcoins against A's address, then the transaction is legitimate.

Q 1) Where does SPV and Merkel trees come into picture during the above-mentioned verification process? I have read elsewhere on the internet that SPV nodes will query for the previous transactions B1,B2,B3, .. and S1,S2,S3 & check if whether A's claim is valid or not.

Q 2) How many previous transactions do usually SPVs look into? My understanding is that SPVs have to look at all the transactions associated with A's address. What if A does not input all its previous transactions? (Eg: A hides its some of its spending transactions, say, S1)

**Tags:** doublespend, spv


---

## 1 answers

### Answer by username_1 on 2017-05-10. Score: 6 | Accepted answer

In a bitcoin transaction, A's address is not scanned for spendable Unspent Transaction Outputs (UTXOs). Full nodes simply check if the inputs of the transaction are really unspent. There is no scanning of addresses involved.

UTXOs are stored as the transaction id (txid) and the output index of the transaction that created them. You cannot reference the outputs to be spent by address, the address merely specifies which private key must sign the input script in order to claim the UTXO.

Suppose you advertise `addr1` for donations, and you receive 3 donations D1,D2 and D3 on that address. You have 3 UTXOs and not 1. The only thing is that they can be claimed by same private key. Hence, there is no looking around for previous transactions. Wallet has to specify which UTXOs it is consuming.

SPV allows a lightweight client to verify that a transaction is included in the Bitcoin blockchain, without downloading the entire blockchain. To verify that a transaction is in a block, a SPV client requests a proof of inclusion, in the form of a Merkle tree branch. It does not check for double-spend on it's own. Rather it relies on the full node servers it is connected to do so. To prevent from being cheated from single server, they connect to multiple servers to get the block headers.
From Bitcoin Wiki:

> The level of difficulty required to obtain confidence the remote node is not feeding you fictional transactions depends on your threat model. If you are connecting to a node that is known to be reliable, the difficulty doesn't matter. If you want to pick a random node, the cost for an attacker to mine a block sequence containing a bogus transaction should be higher than the value to be obtained by defrauding you. By changing how deeply buried the block must be, you can trade off confirmation time vs cost of an attack.

You might also want to look into Fraud Proofs which would allow full nodes to prove that information propagated on the network is invalid, e.g. allow full nodes to point out double-spends to thin clients.

Another Reference: [Thin Client Security](https://en.bitcoin.it/wiki/Thin_Client_Security)

> **Comments:**
> **username_0**: one clarification: Is it possible to specify the how much fraction of each donation I can spend? For eg: I want to spend X BTC in total. It can be fetched in many ways from D1, D2 and D3.   X = a*D1+b*D2+c*D3 and (a,b,c) is not unique. Does one specify (a,b,c) while transacting for X BTC?  
> **username_1**: All the UTXO in the input to transactions have to be consumed.  The transaction would look something like.  `Inputs: D1,D2 and D3.` `Output: D4 and X. `  with condition(D4<= D1 + D2 + D3 - X) Note that D1, D2 and D3 are no longer UTXO. All inputs to transactions must be completely consumed. Instead we have 2 new UTXO, namely X and D4.  
> **username_0**: The donation amounts D1,D2 and D3 are not mixed. If X <= D1,D2,D3, then  X BTC are transacted only from one of {D1,D2,D3}. Essentially after the BTC are spent, the contents of the wallet would look like {D1-X,D2,D3}, assuming X is consumed from D1.  Please refer this link:  https://www.cryptocoinsnews.com/bitcoin-transaction-really-works/  
> **username_1**: Yes, there are many ways to spend inputs. In your example, it would not make sense to spend D2 and D3 since X<=D1. In a bitcoin transaction, all input UTXO's must be completely consumed. If you have UTXO's {D1,D2,D3,D4 ..} and you can choose any subset of it on condition that sum is greater than X. Say, you choose D2 and D4. The new UXTO set would be {D1,D3,O1 and O2} where O1 and O2 are outputs. Here, D1 and D3 are consumed All UTXO's which are input to Transaction must be completely spent. The article you linked explains the same.  
> **username_2**: This is not an answer but a clarification. It does not address the problem of how SPV clients are secure against double spends.  
> **username_1**: Edited the answer. Please comment if anything is incorrect/missing.  
> **username_2**: Now it is just wrong. The merkle path proves that a transaction was included in a block. It cannot prove that it is not a double spend.  
> **username_0**: @Pieter: You are right. It does not prove that it is not a double-spend. Ideally, one has to wait for at least 6 confirmations. However the rest of  SanketKanjalkar 's explanation is correct.  
> **username_1**: Hello, I already mentioned in the answer that it cannot check double spend on it's own but relies on connected full nodes.  
> **username_1**: @Pieter, can you please help me understand what part of the answer is wrong?  
> **username_2**: I apologize, and misread.  
> **username_3**: Hi Sanket, Nice answer! I've made a few small edits to your post to fix terminology, or make it a bit clearer. Please review my edit and check if it fits what you wanted your answer to say, otherwise please feel free to roll back or adapt further. Welcome to Bitcoin.Stackexchange.com.  :)  

---

