It is similar to the question here
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 C (eg: A wants to transfer X bitcoins to C), the transaction has to be verified as follows.
- The full nodes have the UTXO (Unspent Transaction) details. The full nodes go through this UTXO database and verify if S1,S2,S3 are unspent or not. UTXO's size is around 512 MB. So, not difficult to go through all of the UTXO.
- Verification can also be done via SPV nodes, a lightweight client. These SPV nodes do not have the UTXO details. However, they have the header information of the entire blockchain. So, a new transaction is initiated, SPV client requests the full node to give information of the S1,S2,S3 transactions. Full node gives this information in the form of Merkel Trees. SPV node verifies the validity of these Merkel trees using the blockchain header info. The blockchain header would not change for all the transactions which are 'old enough'. As a conservative rule, people usually wait for at least 6 more blocks to be built on top of this present block, before delivering their goods or service.
Please refer to this link as well- Good article on how individual transactions are processed.
Thanks for clarifying. So the sum of all unspent outputs is the balance for an address? – Philip Frank – 2013-01-25T00:16:32.733
@bananer Yes, that's right. – theymos – 2013-01-25T00:19:02.063
Where does this appear in the code? – Kinnard Hockenhull – 2013-01-25T02:41:44.297
@KinnardHockenhull For 0.7.x and below, search for vSpent. – theymos – 2013-01-25T04:52:16.580
So I imagine there is some sort of hashtable indexed on the transaction hash, with some flag indicating if it was spent. – Flavien – 2013-01-25T09:59:08.707