Take the 2-minute tour ×
Bitcoin Stack Exchange is a question and answer site for Bitcoin crypto-currency enthusiasts. It's 100% free, no registration required.

When the Bitcoin client looks at the input of a transaction, it know the hash of the previous transaction from which the funds are coming, however then, how does it know that this output has not been spent already? Does it need to scan the whole blocklist for another transaction spending that same output? Isn't that very inefficient?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Bitcoin-Qt marks outputs as spent in the database once they are spent. To check whether an input's prev-out is spent, Bitcoin just needs to look up the transaction and check whether the output is marked spent.

Other ways of doing it are to have a set of all unspent outputs and check whether the prev-out is in this set (future versions of Bitcoin will do something like this), or to index inputs by their prev-outs.

share|improve this answer
    
Thanks for clarifying. So the sum of all unspent outputs is the balance for an address? –  bananer Jan 25 '13 at 0:16
    
@bananer Yes, that's right. –  theymos Jan 25 '13 at 0:19
    
Where does this appear in the code? –  Kinnard Hockenhull Jan 25 '13 at 2:41
    
@KinnardHockenhull For 0.7.x and below, search for vSpent. –  theymos Jan 25 '13 at 4:52
    
So I imagine there is some sort of hashtable indexed on the transaction hash, with some flag indicating if it was spent. –  Flavien Jan 25 '13 at 9:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.