The process of mining explained here is applicable to every miner on the network and every miner keeps executing the tasks mentioned here regularly.
Miners are always looking forward to mining new blocks, and are also listening actively to receive new blocks from other miners. They are also listening for new transactions to store in the transaction pool. Miners also spread the incoming transactions to other connected nodes after validation. As mentioned before, at some point, the miner collects all transactions from the transaction pool. This activity is done by all miners.
The miner constructs a new block and adds all transactions to it. Before adding these transactions, it will check if any of the transactions are not already written in a block that it might receive from other miners. If so, it will discard those transactions.
The miner will add their own coinbase transaction for getting rewards for mining the block.
The next task for a miner is to generate the block header and perform the following tasks:
- The miner takes hashes of two transactions at a time to generate a new hash till he gets a single hash from all transactions. The hash is referred to as a root transaction hash or Merkle root transaction hash. This hash is added to the block header.
- The miner also identifies the hash of the previous block. The previous block will become parent to the current block and its hash will also be added to the block header.
- The miner calculates the state and receipts of transaction root hashes and adds them to the block header.
- A nonce and timestamp is also added to the block header.
- A block hash consisting of both block header and body is generated.
- The mining process starts where the miner keeps changing the nonce value and tries to find a hash that will satisfy as an answer to the given puzzle. It is to be kept in mind that everything mentioned here is executed by every miner in the network.
- Eventually, one of the miners will be able to solve the puzzle and advertise the same to other miners in the network. The other miners will verify the answer and, if found correct, will further verify every transaction, accept the block, and append the same to their ledger instance.
This entire process is also known as Proof of Work (PoW) wherein a miner provides proof that it is has worked on computing the final answer that could satisfy as solution to the puzzle. There are other algorithms such as Proof of Stake (PoS) and Proof of Authority (PoA), but they are not used or discussed in this book.
The header block and its content is shown in the following diagram: