3

In this post it is explained that the UTXO are removed after they are spent. This makes sense because they are not "unspent" anymore.

But don't all UTXOs have a reference/connection to their previous transaction? Is there a second "list" in the blockchain where all transactions from the past are stored? Because if not, where does that reference point to if the previous transaction is not in the UTXO list anymore.

The whole UTXO Model confuses me a little bit, so thank you for the clarification.

2 Answers 2

4

Once spent, the wallet removes the UTXO from its cache of unspent transactions. The transaction is not removed from the general database (the blockchain or block index). Full nodes running with txindex=1 keep every transaction forever. Some wallets though optimize the database to recover disk space - but this is a wallet-specific optimization and not part of how Bitcoin works.

For instance, Bitcoin Core has many caches and optimizations. One of them is the UTXO cache. The reason to have this cache is kind of obvious: if you had to search the entire blockchain for your own unspent coins before every transaction, that could potentially take hours. So Bitcoin Core keeps track of your UTXO's. Your UTXO's output amounts add up and become the balance you see on the wallet software.

The UTXO cache never points to previous transactions - it's just an optimization cache that makes it easy for Bitcoin Core to find TX's. The algorithm which builds the UTXO cache knows which transaction the UTXO came from, but the cache never points anywhere except to the TX index where the unspent output is located.

The pointer to a previous transaction only exists when you spend a UTXO and then a new input TX is created on that transaction. That input will point to the previously unspent transaction and that former UTXO will become spent. A new UTXO is then created after this transaction is mined. This UTXO becomes part of the Bitcoin balance of the recipient, just like the UTXO was part of your balance before you spent it.

This image (courtesy of bitcoin.org) might help illustrate how it works: enter image description here

See those UTXO's dangling without a new TX ahead of them? Until they're spent, they remain as UTXO.

The arrows in that illustration show the money flow direction, but technically the arrows point the other way around : the inputs point back at some previous UTXO output. Once an input points at an output that output is no longer a UTXO, it is a committed TX. The input links the previous TX to a new UTXO and the previous one is marked spent. Bitcoin Core then removes this previous UTXO from the cache.

1
  • 1
    The chainstate (Bitcoin Core's database with unspent transaction outputs) is not just a cache though. It is the authoritative set of all unspent outputs, and spending a missing entries from it imply a transaction is invalid. It's also not an index, as you can delete the actual block data for old blocks after applying their effects on the UTXO set. Lastly, there is a cache on top of the chainstate in memory, with lots of optimizations, but those are independent of the chainstate itself. Oct 21, 2019 at 20:06
1

I want add a clarification inside your good post by Jose Fonseca, the UTXO is a TransactionOutput, this structure has only 2 value, the amount and the scriptPubKey. The link to the previous transaction is inside the TransactionInput so, the TransactionInput and the transaction output is contained inside the Raw transaction.

So inside the cache is present only the UTXO (TransactionOutput).

I have created this schema for my document, I hope it can help you.

enter image description here

This image tries to simplify how bitcoin transactions are generated. The terms that are used are generalized, for example, you do not enter the public key in the output transaction but the address or hash160 of public key, this depend to the type of script is used.

In the image is not represented but there is a data type called Outpoint that refers to the previous raw transaction.

Still, for my project I rebuilt the data structures used in Bitcoin core (Except for the scriptWitness) if you haven't looked at the code, they can give you a general idea. you can find it here

ps: I'm developing this documentation, it can contain some error, please verify it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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