How does Bitcoin knows it can use the 'Coins' you want to 'Send'?
The accounting model used by Bitcoin is called the 'UTXO Model', which comes from the abbreviation of 'Unspent Transaction Output', and is widely known as UTXO.
This accounting model aims to establish the way in which the entire accounting record will be carried out, within Bitcoin network. That is, how all value-transfer transactions within Bitcoin network will be recorded.
By design, every Bitcoin transaction has an input - which is where the bitcoins comes from, and an output - that is where the bitcoin of a transaction goes.
So a UTXO, is always the result of a previous transaction, and the entry or start of a next transaction. It is because of this form of registration that Bitcoin is highly traceable, since all UTXO's comes from a transaction that is recorded and known.
Another objective of this model is to facilitate the verification and validity of each transaction, a UTXO can never be greater in balance than the original input (i.e. amount/number of coins in a UTXO can never be bigger than the initial input provided or the coins received).
And what is a Transaction?
Transactions are cryptographically certified statements that instruct network participants to update their copy of the 'independently maintained copy (of the Bitcoin network ledger)' of the balance ledger (UTXOSET).
So when you want to spend bitcoin, you create a transaction, which explicitly states which UTXO's are being spent and in addition, the transaction defines how those funds should be allocated to the new owner in the form of new UTXO's.
Considerations related to UTXO's:
The UTXO's are named in 'Satoshis', the native unit of the Bitcoin protocol. A Satoshi is the smallest unit of Bitcoin. 100,000,000 Satoshis is equal to 1 bitcoin.
They cannot be partially spent, they are created once and then spent in full by another transaction.
Let's consider an example:
- You have 5000 Satoshis in a UTXO (0.00005000 bitcoin) and you send 1000 Satoshis,
- A transaction will be created that will completely spend those 5000 Satoshis, generating one input for the 5000 Satoshis and two outputs (2 UTXO's),
- One for 1000 Satoshis, which are the ones that are sent to another address,
- And the other UTXO for 4000 Satoshis, which is the change that corresponds to '5000–1000 = 4000' Satoshis. Which are returned to the change address corresponding to your own keys.
We are not going to dive into the technical details of how the transactions are made, but just know that for this you can use any wallet and that it is those pieces of software (that form these wallets) that are responsible for doing it for you. Then a 'Transaction' is the transfer of bitcoin from one wallet to another (simplified by the transaction handling mechanism of the wallet).
Here begins the main process, once you send the transaction through a wallet, it must be validated that each entry of the transaction corresponds to a valid UTXO that exists on the network, as an example we will say that each UTXO is a coin that you are going to send.
Only while they are available to be spent, is that we refer to them as "Unspent" transaction outputs, but the chain (ledger) or blockchain, keeps a record of all transactions made.
Each UTXO is uniquely identifiable by the combination of the transaction that created it (saves the transaction ID) and the transaction output index (how many UTXO's were generated in that output and its position).
Now, we take a dive to understand about the 'Nodes', to be able to understand what is done inside them (what purpose they fulfill). It becomes important to understand, why it is important that you know that each 'Complete Node' maintains a totally autonomous database of all valid UTXO's in the entire network and it is against that database that the transactions that arrive at the node are collated.
So to validate a transaction, a full node (a node that maintains a whole copy of the ledger and Bitcoin time-chain) will only search the UTXO Database for the coin you are trying to spend (it will validate UTXO to UTXO, even if the transaction has more than one "coin"), making sure that it exists and is being used validly, that is, they come from the correct transaction ID.
This is done at each and every node and for each and every transaction.
This is how the node determines that the transaction that is arriving or entering the node, is valid in terms of the expenditure of the coins, that is, it validates that those currencies exist and come from the appropriate transaction ID, other things are validated but for the purposes of this article it is enough to know this.
Subsequently, when a transaction is confirmed within a block, the coins of that transaction are deleted from the UTXO database, it is important that you understand that they are deleted as part of that transaction. For example, those coins are removed from your wallet and the new outputs of that transaction (the coins that have reached their new owner) are added to the database, now as new coins that are related to your new ID.
Finally, to elaborate that this Database is known as UTXO Set (set of unspent outputs).
It is thanks to this UTXO Set (along with other lower level considerations) that you can have an approximation to know how many Bitcoins there are in circulation (the supply). The estimate is approximate and if you are interested in knowing more about the subject I invite you to read the following twitter thread.
Where is the UTXO stored?
Speaking of Bitcoin terminology, TIP would be the last block of the active chain, a database is maintained with the set of outputs of unspent transactions (UTXOs), and is stored in $DATADIR/chainstate, and in memory.
And when a new block is received, the UTXOs spent by the transactions in the block are removed from that Database and the new outputs of those transactions are added.
If there is a reorganization of the chain (reorg), the data stored in $DATADIR/blocks/rev*.dat are used to undo the changes in the UTXO Database and leave the UTXO Database according to the new chain (the reorganized).
What happens to transactions after they are included in a newly mined block? Are they removed from the Database?
The mempool is a set of transactions that are valid but are not yet in a block, that is, they have not yet been confirmed, so they are not treated as part of the UTXO Database. Rather, it is just a group of transactions that are internally consistent and consistent with the active chain. This means that any mempool transaction only spends the outputs that are in the UTXO Set, or from other mempool transactions.
Each time a new block enters, its transactions are removed from the mempool (assuming they were there), and any transactions from the mempool and their dependencies that conflict with the new UTXO Set are removed.
This article was originally published in Spanish on Dinero Sin Reglas