In blockchain, there are two account models: the account model and the UTXO model used in Bitcoin. The account model is easy to understand. Our bank accounts are all account models. We maintain a data in the database, and the increase or decrease of our balance is based on this data.

Bitcoin’s UTXO model is also known as transaction-based model, which means that all data is recorded in a Transaction. If you want to know how much money is in a person’s account, you can only infer it from the Transaction. In this article, let’s talk about bitcoin UTXO.

1. Types of transactions in Bitcoin

In Bitcoin, there are three common types of transactions, one is to pay a part to others, and then give yourself part of the change, this is to split a UTXO into two, about the concept of change, we will talk about next.

The other is to pay someone a sum of Bitcoin and then create a new UTXO by combining multiple UTXOs and giving them to other users, which is like exchanging a lot of change for a high-denomination bill.

Another way is to exchange a large UTXO for several small UTXOs in a transaction that involves sending bitcoins to multiple people. This is like exchanging a large bill for several small bills.

The total value of utXOs in a single transaction is generally slightly greater than the value of UTXOs out, because some of the bitcoins are taken away by miners as transaction fees.

2. UTXO model

Bitcoin is based on transaction ledgers, which means all the data is stored in the blockchain ledgers. There is no place to keep track of how many coins are in each account. How many coins an account has needs to be calculated according to transactions.

A data structure like UTXO, which stands for Unspent Transaction Output, needs to be maintained on all bitcoin nodes. Full nodes are network nodes that run the full bitcoin software and are typically involved in mining. These UTXOs are extracted by full nodes after traversing bitcoin’s ledger and then maintained in memory.

If UTXO needs to be queried on the chain at the time of transaction initiation, it will definitely take a long time. In order to make UTXO query faster, the whole node will scan the whole chain and then generate the mapping between the address and UTXO. Convenient trading time quick inquiry. The following structure is formed:

When a transaction is made, the full node will go to the utXOs in the utXOs collection to find the matching UTXOs, and then sign the transaction with the owner’s private key.

Each transaction costs some UTXOs and generates some new UTXOs. If you now have a UTXO of 2 BTC and need to pay a person 0.5 BTC, the transaction will split the UTXO into two UTXOs of 0.5 and 1.5 BTC (actually slightly less than 1.5 due to the handling fee). A 0.5 BTC UTXO is sent to someone else’s address, and a 1.5 BTC UTXO is returned to your address. This method is also called change.

The split UTXO is readded to the UTXO collection for future transactions, which then forms the following transaction chain:

Each output in UTXO gives the hash of the transaction that produced the output and the number of outputs in that transaction.

These coins in circulation, all from the original bitcoin block output. Each block is mined to create a special transaction, known as a coinbase-transaction. The trade would send a set number of bitcoins to the address from which the block was mined, starting with 50, and then halving the number of coins produced by minting every four years or so.

There are only a few nodes running the whole node, and most people still use mobile phone wallet. For mobile phone wallet, it is naturally impossible to maintain the data on the mobile phone, so mobile phone wallet usually requests a server and obtains the results from the server.

The API can be used to query unspent bitcoins in an address:

curl --location --request GET http://www.tokenview.com:8088/unspent/btc/1JctmVUHHfchRQHR1K1v9E9rs3mSWLGEmt/1/50
Copy the code

Then you can get the UTXO of the address:

{
    "code": 1."msg": "Success"."data": [{"block_no": 707638."output_no": 0."index": "23"."txid": "e167d10b492465e5a1b4d15176fa580af21cae314da337184f82089daf9bc740"."hex": "76a914c143e8f3f235ca9e82df0a017f01cd3f91ff3d4388ac"."confirmations": 3629."value": "0.00231716"
        },
        {
            "block_no": 628354."output_no": 0."index": "16"."txid": "8f8153d676adfb1ceb27154b96f025a6898d6ee49522cf38b847a63f93527372"."hex": "76a914c143e8f3f235ca9e82df0a017f01cd3f91ff3d4388ac"."confirmations": 82913."value": "0.00231716"}}]Copy the code

Confirmations indicates the number of confirmations that follow the block. The larger the number of confirmations, the more secure the current UTXO is and the less likely it will be tampered with. Value is the number of bitcoins contained in the UTXO, txId is the transaction Id that outputs the changed UTXO. Output_no indicates the number of outputs from the previous transaction.

3. Solve the double flower problem

In the digital currency system, the double spending problem must be solved. Double spending problem refers to the situation where the same penny is spent twice. Bitcoin uses the UTXO model to solve this problem.

As each UTXO is spent, it needs to be signed by the owner’s private key. Those utXOs that are spent are deleted from the collection. If someone tried to spend UTXO twice, it would be immediately detected and one of the two deals would fail.

4. Why doesn’t Ethereum use UTXO

V god thinks UTXO does not meet ethereum’s requirements very well. Ethereum is built around smart contracts, and UTXO’s model would complicate how smart contracts handle money. Each transfer requires a careful combination of UTXO. Also, ethereum has a lot of money transfers, and UTXO takes up much more space than the account model.

But on the other hand, Bitcoin is definitely not doing functions like smart contract at the chain layer. It is positioning itself as the lowest level of application. The recent upgrade of Taproot makes Bitcoin look more vibrant. It’s not entirely certain yet, but it’s certainly something to look forward to.

Ethereum, on the other hand, positioned itself as general-purpose computing from the beginning, and the initial ecosystem did take off with the explosion of smart contracts, but now the user implementation experience is a bit poor, with high gas charges and slow transaction speeds, and Ethereum is now referred to as the rich chain. So now ethereum is rushing to roll out Layer2, and only if the system can support more high-frequency, smaller transactions can it attract enough users.

5. Bitcoin in an exchange

It is also important to note that there are many centralized exchanges that offer bitcoin trading, but the bitcoin on the exchange is not utXO-BASED.

When you charge coins at the exchange, the exchange will set up an account for you, and all subsequent transactions will be based on this account, without any interaction with the chain.

This has two advantages. The first transaction is faster. It takes a long time for a transaction to be confirmed on the Bitcoin network, and it slows down when there are many transactions. Second, it saves a lot of fees. Because there is no interaction with the chain, there is no transaction fee. Of course, the exchange itself will still charge you for every transaction you make.

In other words, trading on the exchange is not really trading on the chain. Coins are only sent back to the address on the chain when they are withdrawn from the exchange.

The text/Rayjun