The Bitcoin Transaction Model - What's an UTXO?
Transactions are at the core of how the Bitcoin system operates. The structure and mechanisms of Bitcoin-transactions will be examined in this article.
Bitcoin works different than most people first think. There are no coins and no accounts. In your wallet application you might see a number of your current holdings (your spendable BTC), but this number is not stored as such in the blockchain. Bitcoin-Addresses do not have a “deposit” or a “balance” attached. This information is calculated and presented by your wallet.
All the data regarding funds is stored just in plain transactions. This is why Transactions in Bitcoin are so essential, because they themself contain all the information concerning the spent and spendable funds in the system. The are self-contained and everybody who has access to the blockchain can validate them. Bitcoin is a chain of transactions, stored in blocks.
A transaction can be described via double-entry bookkeeping. Below you can see an image with the current transaction TD in the center, three transactions TA, TB, TC on the left and a virtual transaction TE, that could happen in the future, on the right. The goal of this transaction TD is simply to send 0.12BTC to another address. The specific addresses do not matter in this example, but each of the inputs and outputs are attached to addresses.
Inside the transaction TD we can find Inputs on the left side and Outputs on the right side. Those do usually not add up, the outputs must be a little less than the inputs. The difference, here 0.002BTC (3), is a transaction fee that gets collected by the miner that finds the block of which the transaction is part of. The fee does not need a specific output to get collected.
TD has two outputs O1 and O2. The intended 0.12BTC (1) are “sent” via O1 to the target-address. In reality though nothing is sent on the blockchain, the target-address being attached to O1 is enough for the receiver (who has the private key of the target-address) to control the funds from now on. The output O2 with 0.005BTC (2) goes back to ourselves (more on O2 later). We need to pay a fee to the miners, just enough that they have an incentive to include our transaction in a block. So the wallet calculates a reasonable fee based on current market parameters (for the sake of this simple example 0.002BTC (3), which would be way too high for a real transaction in the year 2020). The output O2 is therefore calculated as O2=SUM(I1,I2,I3)-SUM(O1,Fee).
It would be possible to omit O2 and create just one output O1, which would automatically increase the difference between inputs and outputs to 0.007BTC, which means the fee would be 0.007BTC. But we do want to keep the fee low, so we take the remaining 0.005BTC (2) back as Change. This change of 0.005BTC goes usually back to our own sender-address (could also be another address we control) and is spendable by us in the future.
Inputs, Outputs, UTXOs
In our example transaction TD we have three inputs. These inputs come from past transactions TA-TC, more specific from outputs of those transactions. Even more specific, these outputs O1-O3 must be spendable, which means that they have not been included in another already mined block as part of a transaction. This is all validated during the mining process and the network consensus. For this reason waiting for one or more confirmation blocks is recommended, because a rouge miner could construct a invalid block which spends an output multiple times (the double spend problem a bit simplified).
It’s important to understand that outputs are atomic and non-divisible and inputs are linked to previous outputs. The Bitcoin blockchain consists of transactions that are chained together via inputs and outputs. More specifically the outputs are linked to the inputs by the transaction-id and the output-index found in the previous transaction, but let’s not get too technical for this article. Because outputs are non-divisible (like a bank note or a physical coin) the desired value of summed up inputs and outputs with fee do hardly ever match exactly and transactions have change that goes back to the sender. This is very similar to paying with cash in the physical world.
In our example we need 0.122BTC (amout to send + fee) for our transaction, which means we must have at least 0.122BTC of unspent transaction outputs (UTXOs) available. Our wallet looks for such UTXOs by checking the addresses we control (this is called UTXO-Set, all unspent outputs we have in our wallet) and constructs the transaction with the best fitting UTXOs as new inputs to meet the target amount. In our example the closest we could get with the available inputs was 0.127BTC from I1+I2+I3.
As shown in the image above, a transaction TE could use the output O1 of TD in the future. The output O2 of TD goes back to our wallet as UTXO.
There is a special type of transaction which does not use inputs from previous outputs, the so called Coinbase-transaction, included by the miner of a new block. This transaction creates UTXOs for an address the miner specifies ‘out of thin air’. The amount of the Coinbase-UTXO is the sum of all transaction fees in the mined block and a block reward (currently 6.25BTC in Dec. 2020).
Wallets and Transaction Construction
Transactions are the only information carrier in the Bitcoin blockchain regarding funds and they are constructed by referencing previous transactions. As mentioned above this means that there are no accounts, coins or balances stored by themselves in the blockchain. All this readable and essential information to us humans is calculated and presented by the Wallet application. Wallets must therefore analyze the blockchain (full node wallets via a local copy of the blockchain) or use APIs from online-services that operate full nodes to ask for the information they need (mobile/light client wallets).
Because transactions have all essential information in them, they can be validated by everyone who has access to the blockchain. Therefore it makes sense for the Bitcoin system to have many full-nodes running, even if they are not mining. By validating transactions full nodes strengthen the system and ensure that correct transactions are propagated in the network.
Wallets are fully responsible to construct correct transactions, so if there is a bug in the wallet-application this might lead to a loss of funds. Think of not adding an output O2 for the change in our example-transaction TD. All funds would go to the miner-which is already bad and would be terrible if our inputs would add up to a higher amount, let’s say 5BTC.
Another interesting aspect is, that transactions can be constructed offline, if the wallet has all information it needs to build a valid transaction (it must have the UTXO-Set for addresses we control). This means transactions are portable without a network and a transaction could be stored on an offline-medium (e.g. Disk-Storage) and be imported into another wallet. That other wallet then later connects to the network to let the transaction propagate.
Scripts & More
This is a very high level and rather non-technical overview of how Bitcoin transactions work, we discuss only one usecase: send an amount from one address to another. There are more capabilities in the Bitcoin network, like multi-signature & time-locks, which even enable other network layers to be built on top of Bitcoin (e.g. Lightning), bridged by special transactions.
This is a total random transaction on the blockchain. We can see information regarding Input Scripts and Output Scripts, which define the rules how a transaction gets checked. These scripts are written in a little programming language that makes Bitcoin to a certain extent flexible and more versatile than just to be used to transfer BTC between two addresses.
As with most technical topics on Bitcoin, I can recommend the Book Mastering Bitcoin by Andreas M. Antonopoulos for a deeper dive.