3
3
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)
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 = aD1+bD2+c*D3 and (a,b,c) is not unique. Does one specify (a,b,c) while transacting for X BTC? – kasa – 2017-05-10T09:39:39.537
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 – 2017-05-10T11:17:06.700
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/
– kasa – 2017-05-11T03:07:21.790Yes, 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 – 2017-05-11T04:50:04.263
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 – 2017-05-11T05:02:06.203
Edited the answer. Please comment if anything is incorrect/missing. – sanket1729 – 2017-05-11T06:10:16.163
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 – 2017-05-11T09:35:27.603
@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. – kasa – 2017-05-11T09:55:27.607
Hello, I already mentioned in the answer that it cannot check double spend on it's own but relies on connected full nodes. – sanket1729 – 2017-05-11T10:14:33.377
@Pieter, can you please help me understand what part of the answer is wrong? – sanket1729 – 2017-05-11T10:37:43.133
I apologize, and misread. – Pieter Wuille – 2017-05-11T15:35:41.957
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 – 2017-05-12T14:31:54.190