Ch13: Something on Mining Node

Khor Aik Cheow, PhD
6 min readJun 30, 2020

--

A mining node is a full node that keeps a full copy of the blockchain. Mining node is also called ‘miner’. Mining node performs 2 important tasks:

· Validation of new transactions

· Mining (adding new blocks to the blockchain)

When a light wallet sends a transaction to the nearest full node, the full node will validate this new transaction. Some articles use ‘validation’ and ‘verification’ interchangeably. This article will use ‘validation’ on all new transaction received by mining node.

Full node validates new transaction according to network rules. You can see some of the validation rules here:

https://en.bitcoin.it/wiki/Protocol_rules#.22tx.22_messages

Among all the rules, signature validation is commonly cited in most articles. This is basically to ensure that the unlocking script (scriptSig) can unlock the locking script (scriptPubKey) to prove ownership of the receiver. However, this may not be an accurate description, since only P2PK & P2MS use ‘signature’ in their unlocking script, and P2PKH & P2SH use the signature together with the public key or script. It is therefore more accurate to describe this as a ‘signature/PubKey/script validation’.

The second most cited transaction validation is ‘fund validation’, where the full node checks the UTXO set (database) to make sure that the sender has enough fund (UTXOs) to send to the receiver.

Upon successful validation on the new transaction, the full node will store this transaction in full node’s memory pool (mempool/transaction pool). The full node will also broadcast this transaction to other full/mining nodes, where the other full/mining nodes will validate, store it and broadcast again until this transaction is present in every full/mining node’s memory pool. Take note that until this stage, the transaction is still ‘unconfirmed,’ i.e. not yet in blockchain.

Next, the mining node will construct a new block (candidate block) filled with transactions selected from the memory pool. Note that it’s up to the mining node to decide how many transactions to put in a new block. Each block has a different number of transactions. The chart below shows an average of 2k to 2.3k transactions per block in 2020.

The mining node will attempt to add this new block by using a ‘consensus algorithm’. Some articles describe this as consensus rules, protocol, mechanism, method or mathematical (cryptographic) equation, puzzle, problem. The term ‘consensus algorithm’ will be used in this article. There are many types of consensus algorithm, as shown in the diagram below. This article will focus on PoW (Proof of Work) consensus algorithm only.

Source: https://101blockchains.com/wp-content/uploads/2018/08/Different_Consensus_Algorithms.png

The Bitcoin network is using PoW. It is a probabilistic-finality type of consensus (as opposed to absolute finality), meaning that the probability of a particular transaction (just added to the blockchain) being reverted (returned back to the memory pool) decreases as the block containing the transaction sinks deep into the chain (more blocks are added on top of that block).

How is it possible that a transaction that has just been added to the blockchain may have to be returned back to the memory pool? Consider 2 mining nodes adding a new block at the same time. When this happens, the blockchain splits into two: yellow-blue and yellow-red.

If mining node #558 manages to add another new block faster than the rest of the miners, its blockchain will be longer than the yellow-blue blockchain. The Bitcoin network will consider the longest chain as the legitimate blockchain, which in this case is yellow-red-green blockchain.

Mining node #963 has no choice but to update its blockchain by adding the red & green block. The blue block will become an ‘orphan block’. All the transactions of the blue block (except those already inside red & green block) will be returned to the memory pool of mining node #963, and will be ready to be included in the next new block constructed by mining node #963.

This is why the Bitcoin network needs to wait (about one hour) until the block containing the transaction is ‘6 blocks deep’ in the blockchain before confirming that particular transaction.

The PoW consensus algorithm attempts to generate a matching block header hash that is less than or equal to the defined target. In the previous article, we explained about target. We also know that blocks are ‘chained’ together when each block header contains ‘hash of previous block header’. Some articles use ‘hash of previous block’ but I find it more accurate to describe it as ‘hash of previous block header’.

All fields (except nonce) in the block header are fixed in value. Therefore, we can only adjust the nonce iteratively so that we can have different block header hash. If after all the iterations, the nonce is exhausted and we still have no solution, we can adjust the ‘micro nonce’ in coinbase, which in turn will change the value of Merkle Root in the block header. This option will allow us to generate more block header hash.

To mine (or to add) a new block to the blockchain, we need to create a block with a block header hash less than or equal to the defined target. The diagram below will give you an idea of how to get a block header hash that matches the target.

Upon finding a matching block header hash, the mining node will add the new block to its own blockchain copy. Note that the moment the block is added to the blockchain, the transactions in that block are ‘confirmed’. The mining node will then broadcast this new block to the rest of the full/mining nodes for validation & check. The rest of full/mining nodes will do 2 things:

· Validate the transactions in that new block again.

· Check if block header hash of new block is less than or equal to the defined target.

If the validation & check is successful, the full/mining nodes will add the new block to their own blockchain copies, and continue to broadcast this new block to the rest.

A summary of a new transaction flow is shown in the diagram below:

Remember

· Mining node validates new transaction & mine (add) new block to the blockchain

--

--