Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: unsigned_long_long on December 26, 2020, 11:27:59 PM



Title: Tracking unspent outputs
Post by: unsigned_long_long on December 26, 2020, 11:27:59 PM
I have a question for anybody familiar with the Bitcoin codebase. I'm interested in how spent/unspent outputs are tracked to prevent double spends, specifically I just want to know what data structures are used for checking if a output is spent or not (i.e. to prevent double spends). I know this could be done with a hash table or bloom filter.

I took a quick look through the codebase, I would have expected it to be https://github.com/bitcoin/bitcoin/blob/master/src/txdb.h#L59 (https://github.com/bitcoin/bitcoin/blob/master/src/txdb.h#L59) although this HaveCoin seems to check the existence of the coin - not if it's spent or not.

It would take me a long time to read through the code and figure this out so I'm hoping somebody who already knows the answer can help me.

Thanks in advance.  :)


Title: Re: Tracking unspent outputs
Post by: NotATether on December 27, 2020, 09:48:17 AM
The HaveCoin method really does check if a coin is unspent. If you look in the base class CCoinsView https://github.com/bitcoin/bitcoin/blob/c7ad94428ab6f54661d7a5441e1fdd0ebf034903/src/coins.h#L189-L190 , that's where it's mentioned. The implementation is defined in coins.cpp and it just calls the IsSpent() method of each Coin class. And that method checks if the output contained in it has been set to null.

IsSpent code is located at https://github.com/bitcoin/bitcoin/blob/c7ad94428ab6f54661d7a5441e1fdd0ebf034903/src/coins.h#L76-L78