In the previous blogs, we looked at
- Complexity & cost of the current financial systems and what constitutes a blockchain
- How hashing ensures data integrity & trust-less verification of blocks
- How p2p network and immutable ledger helps decentralization & integrity of the network
In this blog, we will understand how mining works and how does a miner “win” the right to write to the blockchain
Miner’s motivation to mine
Mining is the term used for those who have powerful computers which creates a block, generates a SHA256 hash and cracks the cryptographic puzzle, so as to be able to write their block permanently to the blockchain
We have thousands and thousands of miners i.e. ASIC computers owned by individuals or groups of individuals across the globe, in the hope of solving the cryptographic puzzle, writing their block onto the Bitcoin blockchain and winning the reward. The reward a miner gets is a set number of Bitcoins, which varies every 210,000 blocks or roughly every 4 years. The reward is deterministic and is coded in such a way that every 4 years, the reward decreases, which is called halving. The miner can generate money by selling their newly minted Bitcoins in the Spot market or they can pledge their BTC and loan fiat currency to run their operations
Miner’s income when they get to write a block = Newly minted Bitcoins + Transaction fee’s paid by users (you & me)
When Bitcoin mining first started in 2009, the miners got 50 Bitcoins. After the first halving i.e. roughly after 4 years in 2013, the miners got 25 Bitcoins. As I write this article, the current reward is 6.25 Bitcoins per block. The next halving will approximately be around 2024, until then for every block that is added, the miners would get 6.25 BTC
The mining reward is the mechanism of introducing new Bitcoin (minting) into circulation. Unlike a government backed fiat currency where the central bank can print as many notes as they like, thus increasing the money supply and driving inflation; in Bitcoin blockchain, this is hard coded and there is a schedule of this release through the miners. The last Bitcoin that would be minted would be in 2140, post that the miners income would be solely from transaction fees which you & I pay when we use the Bitcoin network
Another way of looking at this is that, every four years the new Bitcoins getting added to the system halves from the previous four years, therefore making it “more precious” which drives the Bull run for BTC which always touches new all time highs
Importance of Nonce
In the previous blogs, we saw that a blockchain consists of data which are nothing but numerous transactions which the miner picked to include in the block based on the transaction fees, you then have the previous hash and then the nonce
Since SHA256 hashing algorithm outputs the same hash no matter who runs the algo and since all the above fields are constant for a block, the miner needs some modifiable attribute which he can change each time, to be able to generate a new hash for that block – this attribute in the Nonce. However why does the miner want to keep generating multiple hashes for the same block?
Hash pool
The Bitcoin algorithm provides a Target and any miner who finds a hash which is below the target value will get to write to the blockchain. Let us visualize the hash pool as a list of all possible hashes arranged sequentially from bottom to top
In the above diagram, the miner generated an hash with Nonce 59 and checked if he/she cracked the puzzle, since the hash was above the target, the miner changed the nonce and regenerated the hash. The miner kept changing the nonce until at Nonce 67 (golden Nonce) he/she got the hash which was below the target, thus cracking the puzzle and being able to write to the blockchain and win the reward
Because SHA256 has the avalanche effect, the miner cannot just lower the nonce and expect to find the hash below the target. For every change in nonce value, the output hash would be from anywhere in the hashpool, therefore the only way to crack this is through sheer brute force
More the computing power, the more iterations you can run in the hope of finding the hash which would be below the target
The beauty of the Target is that it is variable. The algorithm is built such that on an average one block should be added every 10 minutes, so if there are very few miners trying to crack the puzzle and if the average time taken to add blocks is > 10 minutes, the target value will automatically adjust to calibrate such that it becomes easier for the miners to crack the puzzle within 10 minutes. If there are many miners online and if the blocks are being added in < 10 minutes, the target goes lower, so it becomes that much more difficult for the miners to crack the puzzle
Therefore irrespective of the number of miners, the blockchain will automatically self calibrate itself and ensure that the heartbeat of the blockchain is maintained at around 10 minutes
What next
Now that you understand how mining works, the other question that you may have is what happens if multiple miners get to solve the puzzle at the same time. This is probable as there are many miners simultaneously trying to crack the puzzle. In the next blog, we shall look at how this scenario is handled