in this article, we will explain the Bitcoin Halving Code where you will know about how Bitcoin Halving actually happened into the code.
What is Bitcoin Halving?
Bitcoin Halving is an event that occurred after every 210,000 blocks on the bitcoin blockchain and it is the event that responsible for fixed issuance of Bitcoins which is capped to maximum of 21 millions Bitcoins that can ever be generated.
Bitcoin Halving is responsible for the continuous reduction in the issuance of bitcoins in every four years means it makes bitcoin disinflationary by which the rate of newly bitcoins creation goes down to half after every 4 years due to bitcoin halving event.
Basically, Bitcoin Halving is not measured by years as normally people say that Bitcoin Halving will occur after 4 years. The Bitcoin Halving is counted in blocks where it actually occurred after every 210,000 blocks and every block generated in 10 minutes at average that means 210,000 blocks generated approximately in 4 years of time. That’s why we saw Bitcoin Halving to be occurred in approximately in 4 years. The 3rd Bitcoin Halving will be occurred at 630,000 block where Bitcoin block reward will reduce from 12.5 Bitcoins to 6.25 Bitcoins.
Bitcoin Halving Code explained!
For every block generated by miners need to put the block subsidy (block reward) for that block in coibase transaction.
So this block subsidy is calculated for any block generated through a function defined in 1238 line of validation.cpp file in bitcoin source code.
file link- https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp
This function named GetBlosckSubsity() is returned the block subsidy value (block reward) for the recently mined block.
int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;
In this function, it first count the number of halvings already occurred until that recently mined block by dividing the total number of blocks (nHeight) by 210,000 blocks (consensusParams.nSubsidyHalvingInterval😉 that returned only integer value. For example if the recently mined block is 230,000 then it will return 1 (integer value) to the number of halvings counter variable (halvings) that determines it as only 1 halving occurred at that block.
if (halvings >= 64) return 0;
After counting the halvings, it checks for halvings value upto 64 where it returns block subsidy value (block reward) zero if halvings value more than or equal to 64.
CAmount nSubsidy = 50 * COIN;
Now, nSubsidy object is created by initialising it with a value of 5,000,000,000 (in satoshis) where COIN is equal to 100,000,000 (number of Satoshis in one Bitcoin), nSubsidy referred to the block subsidy value (block reward) for first time when no halving occurred means block reward is 5 billions satoshis from genesis block to 209,999 block.
nSubsidy >>= halvings; return nSubsidy;
Now, nSubsidy or block subsidy value get bitwise shifts by halvings value that means nSubsidy get divided by 2 in every bitwise shifts. For example when halvings value is zero then it doesn’t do any bitwise shifts and nSubsidy value remain 5 billions satoshis as block reward for miners. If halvings value is 1 then nSubsidy value get bitwise shifts once by dividing it by 2 one time that changes nSubsidy value to 2.5 billions satoshis as block reward for miners. Similarly, if halvings value is 2 then nSubsidy value get bitwise shifts twice by dividing nSubsidy value by 2 two times that changes nSubsidy value to 1.25 billions satoshis as block reward for miners and so on.
After bitwise shifts, it returns the nSubsidy value to the function.
What happened at 3rd Bitcoin Halving?
The 3rd Bitcoin Halving is going to occur at 630,000 block that seems to be generated on 12 May 2020.
Basically, the 629,999 block will be the last block that will generate the 12.5 Bitcoins as miners block reward and after it, when the next block 630,000 mined then the block subsidy or block reward will be reduced to 6.25 bitcoins where every block generated after 629,999 must contain no more than 6.25 bitcoins as block subsidy or block reward as per the consensus rules.
check bitcoin halving counter – click here