In this article, we learn about coinbase transactions, data, rewards, fees, and how transactions are aggregated into the Bitcoin blockchain.
Table of contents.
- The Coinbase Transaction.
- Coinbase Rewards and Transaction Fees.
- Coinbase Data.
In the prerequisite article "Bitcoin Transaction Pool", we learned about transaction pools, we know that once transactions have been validated, the bitcoin node adds them into a mempool/transaction pool where they wait to be included in a block. A full node collects, validates, and relays new transactions and finally aggregates the transaction into a candidate block.
The node maintains a local copy of the entire blockchain and listens for new transactions, trying to mine them and at the same time listening for blocks discovered by other nodes. During the time when the node was looking for a solution to a block, it also collected transactions in preparation for the next block. Once the block is received and validated, it compares it against transactions in the memory pool and removes transactions that were included in the previous block. The remaining unconfirmed transactions wait to be reordered in a new block.
A candidate block is a temporary block created using transactions selected from the memory pool. Nodes select transactions from their memory pool to form a candidate block. This block is not yet validated since it does not have a valid PoW(Proof of Work). It is only valid if the miner successfully solves the cryptographic puzzle specified by bitcoin.
The Coinbase Transaction.
This is the first transaction in a block, it is a unique transaction created by a miner. It is used to collect block rewards for work done and other transaction fees collected by a miner. It is unique since it has no inputs yet it produces outputs.
The following image demonstrates the above explanation;
A miner's node creates this transaction as payment to his/her own wallet for the validation and mining of transactions. The reward is the sum of the coinbase transaction and the transaction fees from all transactions included in the block.
This transaction does not spend any UTXOs instead has a single input referred to as the coinbase that creates bitcoins from nothing. This is the way new coins are added into circulation.
Structure of the coinbase transaction
It is obvious that a coinbase transaction will not be similar to normal transactions.
The coinbase transactions have additional fields such as the coinbase data size and coinbase data. The former is the length of the data in the coinbase transaction, it ranges from 2 - 100 bytes, and the latter is arbitrary data used for the extra nonce and mining tags. It begins with the block height.
Other fields include the transaction hash, output index, and the sequence number. The first two are set to values that don't represent a UTXO reference. Instead of a transaction hash, the first field is set to zero, the output index is filled with 4 bytes set to 0xFF.
The coinbase data is used to replace an unlocking script.
Coinbase Rewards and Transaction Fees.
In order to come up with the coinbase transaction, a full node will first calculate the total amount of transaction fees by adding all inputs and outputs of all transactions in a block.
Total fees = Sum(inputs) - Sum(outputs)
The next step involves calculating the correct reward for the new block. This is calculated based on the block height that starts from 50 bitcoin(reward for the genesis block - 2009) and is reduced by half after every 210000 blocks. At the time of this writing, the reward is 6.25 bitcoins.
The coinbase reward is then added to the transaction fees and the sum is returned. To prevent miners from rewarding themselves more coins, a block with an incorrect reward is considered invalid by all other nodes rendering the miner's PoW invalid. This means that the miner wasted time, electricity, and computational power since the node is invalid. In general, a miner can only spend a reward that has been accepted by every node in the bitcoin blockchain.
As mentioned, coinbase transactions don't have the unlocking scripts fields, they have coinbase data fields. A coinbase data field is between 2 to 100 bytes of data long.
This data field can have arbitrary data except for the first few bytes. For example, in the genesis block, the coinbase data included the text "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks". It marked the date and conveyed a message. Miners use the extra data fields to include nonce values and strings that identify the mining pool.
BIP-34 demands that the first few bytes be used for the block height index as a script push operation.
For example, let's consider the following coinbase data from block Block 740188 already mined block. We have the transction hash e8a44fe682d4e97515440b8ed71b1f19054b2ee8807cb272daca45e9b92d4114
"asm": "OP_DUP OP_HASH160 a8a3f4fbd6a9a7b8c668730694a1e54df3e07b9f OP_EQUALVERIFY OP_CHECKSIG",
The above data is just a snapshot of the whole thing, we can obtain it by executing the following command;
$ bitcoin-cli getrawtransaction e8a44fe682d4e97515440b8ed71b1f19054b2ee8807cb272daca45e9b92d4114 1
We have our coinbase data which is - 035c4b0b04951fa3622f706f6f6c696e2e636f6d2ffabe6d6d37e7bfbe30ffd2409c4507f0f5fa88b4bba9c76ba1f6badb778a75eecbc1b54d01000000000000009465e8c1827b92e37717b2bdd3b6cb241200c566000000000000*
We decode it as follows;
*C 03 instructs the script execution engine to push the next three bytes onto the script stack.
0x5c4b0b is the encoded block height encoded in little-endian format. We reverse the order and we have 0x0b4b5c which translates to 740188 which is the actual block height in decimal. We can confirm this here.
04951fa3622f706f6f6c696e2e636f6d2ffabe6d6d37e7bfbe30ffd2409c4507f0f5fa88b4bba9c76ba1f6badb778a75eecbc1b54d01000000000000009465e8c1827b92e37717b2bdd3b6cb241200c566000000000000 is shared between the encoded nonce that is used to find the solution for a proof of work consensus algorithm and other data from the mining node that mined the block.
A candidate block is a temporary block created using transactions selected from the memory pool.
A coinbase transaction is the first transaction in a block, It is used to collect block rewards for work done and other transaction fees collected by a miner.
Coinbase transactions don't have the unlocking scripts fields, instead, they have coinbase data fields that range between 2 to 100 bytes of data long.