What is UTXO?
UTXO stands for Unspent Transaction Output, which means "unspent transaction output". It should be said that the core concept of Bitcoin transaction and the core knowledge point of transaction is UTXO, so let's talk about UTXO in this article.
Transaction components
There is no concept of accounts in Bitcoin. The so-called balance of an address is actually calculated by counting all transactions related to this address. So let's adjust the focus of the microscope and take a look at what elements are included in a transaction.
The first element is input. First of all, not all transactions have this item. For example, each block has a special transaction that is autonomously managed by miners, which is the so-called coinbase transaction. It is a transaction without input. In this transaction, a certain amount of bitcoins is directly transferred to the miner who made this block. Except for this special case, other transactions have input. For example, Xiao Ming wants to transfer 1 bitcoin to Xiao Gang. What should be the input of this transaction? It is some transactions related to Xiao Ming's address. To be more precise, it is the unspent transaction outputs belonging to Xiao Ming's address contained in these transactions, that is, UTXO.
So we naturally move on to the second element, which is that the transaction should include output. "Output" is a term, and in English it is called Output. In the transaction where Xiao Ming transfers one bitcoin to Xiao Gang, the output of the transaction is the bitcoin pointing to Xiao Zhang's address. In this way, we understand the last three letters TXO in UTXO, where TX stands for transaction and O stands for output. The last U refers to Unspent, which means unspent. If Xiao Zhang has never used this output as input for other transactions, then this output has not been consumed, which is UTXO.
The last element is the transaction fee. In a non-coinbase transaction, the input must be equal to the output plus the transaction fee, which is transferred to the miner. For simplicity in this discussion, we will ignore the transaction fee.
Understanding the basic components of Bitcoin transactions actually means understanding UTXO. Go to http://blockchain.com and click on a specific transaction, and you can see the details of the elements we just talked about.
Input and change
A characteristic of UTXO is that, like a coin, it cannot be broken up for use. So how do you make up the input amount during a transaction, and how do you get the change?
Xiao Ming transfers 1 bitcoin to Xiao Gang. The whole process is like this. Xiao Ming needs to collect enough inputs. For example, in the previous transaction corresponding to Xiao Ming's address, a UTXO with a face value of 0.9 is found, which is not enough for 1 bitcoin. Fortunately, multiple inputs are allowed in the transaction, so Xiao Ming finds another UTXO with a face value of 0.2. In this way, there will be two inputs in this transfer transaction. At the same time, there will be two outputs, one pointing to Xiao Gang's address with a face value of 1 bitcoin. The other pointing to Xiao Ming's address with a face value of 0.1 bitcoin, this output is the change.
To summarize, the whole process is as follows: Xiao Ming first needs to collect enough Input** of sufficient denomination. Here he finds two Inputs, and these two Inputs are the Outputs of previous transactions. These two Outputs are UTXOs before they are consumed, but once the current transaction takes effect, they will be consumed, and two new UTXOs will be generated in this transaction, one pointing to Xiao Ming and the other pointing to Xiao Gang. It is equivalent to Xiao Ming and Xiao Gang receiving a coin each, which can be consumed in other transactions in the future. The balance of Xiao Ming and Xiao Gang's respective addresses is actually the sum of all their corresponding UTXOs.
In this way, we know how to collect the inputs, generate the outputs, and make the change in the transaction.
Why use the UTXO model?
At this point, we have a question: Isn’t Bitcoin just a big ledger? Why not use the account model instead of the UTXO model?
The account model is the basic model of traditional banks or services like Alipay. Under this model, if I have an account corresponding to me and it is recorded that I have 13 yuan, then the number 13 is clearly recorded in the system. The account model is indeed very simple and very flexible. Ethereum and some other blockchain projects use the account model. Let's look at Bitcoin. Xiao Ming has 13 coins. In fact, there is no number 13 on the blockchain because there are only transactions on the blockchain. However, when we open a blockchain browser such as http://blockchain.com, we can see the balance corresponding to an address. This is calculated by the blockchain browser itself, not originally on the blockchain.
However, many experts would say that UTXO is a very good model, mainly because UTXO is very suitable for parallel computing, which is very clever in distributed computer networks. The specific details are not what this article is concerned with. I recommend the article "Comparison of UTXO and Account Models" on the WeChat public account NervosNetwork, which has a more detailed discussion.
Summarize
Let's stop here and summarize a few words about UTXO. UTXO is a key concept for understanding Bitcoin transactions. For those who want to understand the underlying principles of Bitcoin, this is a pit that cannot be avoided. Each transaction can contain multiple inputs and usually two outputs. The total output plus the handling fee is exactly equal to the total input. Each output has its own face value like a coin and belongs to a specific address. Outputs that have not been used as inputs for other transactions are "unspent transaction outputs", or UTXOs. There is no concept of accounts in the UTXO model, so it seems a little more complicated than the account model, but it actually has huge advantages. For example, it is very conducive to parallel computing in distributed systems.