UTXO
Unspent transaction output
A UTXO is an unspent transaction output.
Every bitcoin transaction creates outputs that can be consumed as inputs in future transactions. UTXOs are simply the transaction outputs that have not been consumed yet and can still be used for spending.
So if you think bitcoin as being one big graph of transactions, the UTXOs are the ends of it.
The collection of all the UTXOs is referred to as the UTXO set.
Usage
How are UTXOs used in Bitcoin?
Keeping track of UTXOs is useful for two reasons:
1. Validating transactions
When your node receives a new transaction from the network, it needs to validate that all of its inputs are referencing outputs that have not already been spent.
If the transaction's inputs are all unspent outputs (UTXOs), then the transaction is valid:
However, if the transaction is trying to spend an output that has already been spent in a previous transaction, then the transaction is invalid and will be rejected:
2. Calculating address balance
The "balance" of an address is the sum of all the UTXOs locked to that address:
You can see the balance of addresses on blockchain explorers like mempool.space and bitcoinexplorer.org.
It's important to note that bitcoins don't "live" inside addresses. Bitcoins are held inside outputs, and an address is essentially a lock that can be placed on top of an output. Therefore, the balance of an address is just the sum of all the UTXOs that have been locked to that address.
Location
Where are UTXOs stored in Bitcoin?
In Bitcoin Core, all of the UTXOs are stored in the chainstate database:
~/.bitcoin/chainstate
This is a separate database that gets stored in memory (RAM), which makes it faster to access than having to trawl through the raw blockchain files to check if an output has been spent or not.
The chainstate database is a simple LevelDB key:value store that contains the following information:
- Key - This is made up of the TXID:VOUT for each output. This is known as an "outpoint", and every output in the blockchain has its own unique outpoint, which means it can be used as a reference for looking up each individual output directly.
- Value - The value for each UTXO in the database contains the following fields:
- Height - The height of the block containing the UTXO.
- Coinbase - Whether the UTXO is from a coinbase transaction or not. This is important because outputs from coinbase transactions cannot be spent until the transaction is 100 blocks deep in the blockchain.
- Amount - The value of the output in satoshis.
- Locking Code - This is the locking code that was placed on the output. This is important because every output needs to be unlocked when its being spent in a transaction, so this allows you to quickly check if the unlocking code on the input satisfies the conditions of the locking code on the output.
The chainstate database gets updated with each new transaction that gets mined in to the blockchain; UTXOs that get spent in a transaction are removed from the database, and the new outputs are added to the database.
You can find out some basic information about the UTXO set from your local node by running the bitcoin-cli gettxoutsetinfo
:
$ bitcoin-cli gettxoutsetinfo
{
"height": 796565,
"bestblock": "00000000000000000002f63578950b747bfaa88dcd0bc0d8730827176d01b1f9",
"txouts": 106662924,
"bogosize": 8071055609,
"hash_serialized_2": "596d06c8a5052e1d3e492ee26a811606f5fe30b20bbbd9db2fe64e44e411c17a",
"total_amount": 19415818.12246298,
"transactions": 68046641,
"disk_size": 6827932282
}
The above is just an example of the output from bitcoin-cli gettxoutsetinfo
. When you run this command it can take a few seconds to return the results.
Tools
- bitcoin-utxo-dump - This tool reads the chainstate database from your local node and saves all of the UTXOs to a CSV file.
- Statoshi.info (Unspent Transaction Output Set) - Real-time statistics on the state of the UTXO set from a running node.
Summary
A UTXO is just a fancy name for an output of a transaction that hasn't been spent yet.
Therefore, the UTXO set refers to the circulating supply of bitcoins.
There are lots of acronyms in Bitcoin, but ultimately they're almost always complex-sounding names for things that are pretty straightforward. Don't let it put you off.