The mining is considered as the most important as well as an interesting concept when bitcoin is taken into consideration.
Mining is the process by which new transactions are validated and added to the so-called ‘blockchain’. The mining process is not only confined to bitcoins, but also for all other blockchains which rely on Proof-Of-Work(POW) consensus. The process demands a dedicated mining hardware and thus, not all nodes are involved in mining. Those who are participating in mining process are known as ‘miners’.
Before digging deeper into the concept, let's have an overview. Being a decentralized system, transaction validation is often questioned in both Bitcoin as well as Ethereum. In POW, the one with highest computational power is allowed to approve the transaction. But how do the protocol distinguish which node is with highest computational power? It is difficult or in other words, it requires a central entity for this decision, which is impossible in the case of P2P applications like Bitcoin. So, what the protocol does is, to allow all nodes to compete with each other to solve a cryptographic puzzle. Those who possess higher computational power has a higher chance to solve first. Those who solve first proposes their block to others in the hope that others also accept it. If so, the miner is credited with a block reward. Therefore, the protocol is actually selecting the node with the highest computational power implicitly. The process repeats for every new block.
When someone makes a transaction in bitcoin blockchain, they broadcast it to the network. The miners listen to this and engage in transaction verification. Miners verify that the transactions aren't performed already. For this, miners always maintain the latest version of the blockchain. They assemble a new block and add the verified transactions to it. Next, they should solve a cryptographic puzzle. So what do miners actually do? Here, the mission is to find a hash value for the header of the newly assembled block.
- A block header consist of the following details:
1. Hash of the previous block
2. Merkle root (The transactions in a block are stored in a Merkle tree and its Merkle root is stored in block header)
- Finding hash value using a hashing algorithm(Bitcoin uses SHA256 hashing algorithm) is not a big deal. Every node can do that. Therefore, a difficulty level is associated with it, to make the nodes compete with each other. The difficulty level is a measure of how difficult is to find the hash value within a given target set. Difficulty level shrinks the set of hash values that a block can have. Without difficulty level, a block can have any of the hash value within the super gigantic set of 2256 possibilities !!!(since bitcoin uses SHA256 and it has 256 bits). By associating a difficulty level, the target set is reduced considerably.
- The difficulty level is specified in terms of a number of zeroes, which means the miner has to find a hash value which starts with a specified number of zeroes. This can be stated in another way. The discovered hash must be less than the difficulty level.
- The nodes keep finding different hash values and checks whether it satisfies the required difficulty. Since the data of a block remains same, the hash algorithm always returns same output. Therefore, the only possibility to try out different hash values is by associating a nonce with the block header. The nonce is an arbitrary string of 32-bit length. So, the entire mining process can be summarized as a process of finding a nonce which results a hash value less than the required difficulty level.
Target space is extremely small compared to the output space of the hash algorithm. Being a small target set, the probability of finding success is reduced. The miners keep changing the nonce in a brute force manner and the corresponding hash is computed each time. This is the real game and the computational power of nodes really matters here because the miners have to try out large combinations of ‘Nonce’. Most of the time, the entire 232 combinations were tried, but none results succeed. In that case, there is a parameter, called 'coinbase parameter', which is considered. A single change in coinbase parameter changes the entire Merkle tree structure and changes the Merkle root in the block header. So, the same process can be repeated to produce a different set of hash values.
The node which equipped with dedicated hardware and higher computational power has a greater chance to win this game and get the block reward. Those who find hash first will broadcast the block along with that particular nonce which resulted in success, by hoping that everyone else in the network will accept it. On receiving this, others stop mining and verifies that the received hash satisfies the current difficulty level. If yes, the nodes show their acceptance by adding it to their version of the blockchain.
- The difficulty level is adjusted every 2016 blocks, to sustain a block time of 10 minutes on average. If more and more nodes involve in mining and the new blocks are mined more frequently than 10 minutes, then the protocol is designed to increase the mining difficulty for the next 2016 blocks.
- Block rewards are offered to promote the validator for being trusty. How does the validator get a reward upon validating? or who gives this reward? Well, the answer is very simple. The miners add a special transaction at the time of creation of block. This transaction is very special and it allows miners to generate new coins and credit it to themselves. The protocol allows this transaction and it is known as ‘coinbase transaction’. This transaction is special because it is the only transaction in which input is blank( because the coins are freshly minted). This will be the first transaction in every block. You can see this in block explorer.
- Every miner adds this transaction to the block they assemble, irrespective of their success on finding the hash. If they find out the hash, the block is proposed to others. The proposed block along with “our coinbase transaction”(ie. The transaction paying to ourselves) is valid only after getting approved by others. It is like, I've minted a 10$ and paid it to myself, which is illegal at that moment. Then I work hard for finding something useful for society. if I succeed in that, the entire society considers my coins are legal.
This is the only way by which new coins are added to the bitcoin economy. In the case of bitcoin, it is 12.5 BTC. The reward is halved every 210,000 blocks or roughly every 4 years(It was 50 BTC at its beginning !!!).