The program code for Halving (fourth in 2024) will look like this, that is using C programming language.
Let’s make the use of The GetBlockSubsidy() function.
CAmount GetBlockSubsidy(int nHeight, const Concensus::Params& consensusParams)
{ int halvings = nHeight / consensusParams.nSubsidyHalvingInterval; // Force block reward to zero when right shiftis undefined.
if (halvings >= 64) return 0; cAmount nSubsidy = 50 * COIN; //Subsidy is cut in half every after 210, 000 blocks which will occur approximately every four years
nSubsidy >>= halvings; return nSubsidy;
}
Hints:
  1. consensusParams.nSubsidyHalvingInterval means consensus rules everyone on the bitcoin network needs to honour.
  2. consensus.nSubsidyHalvingInterval = 210, 000; i.e every 210, 000 blocks a halving occurs
  3. nHeight is the current block height or the number of blocks that have already happen
  4. Soon, we shall reach block 840,000 i.e 210,000*4, At this point (Year 2024) the equation becomes Int halvings = 840,000/210,000; which will give halvings the value of 4 (the fourth time the network shall halved)
  5. if (halvings >= 64) return 0; i.e Force block reward to zero when right shift is undefined and once there have been 64 halvings, there should be no further nSubsidy released
cAmount nSubsidy = 50 * COIN; nSubsidy >>= halvings; return nSubsidy; this means that: cAmount nSubsidy = 50 * 100000000; each coin is divided into 100, 000, 000 units nSubsidy >>=4; return nSubsidy i.e At the very start, 50 Bitcoin were rewarded for every block found on the network. Then after 210, 000 blocks (first halving) 25 Bitcoin was rewarded. 12.5 Bitcoin reward for the second halving. Third halving which is where we are today, the reward is 6.25 Bitcoin. Therefore, for the fourth halving which is in the year 2024 the reward shall be 3.125 Bitcoin.
Constructive additions and subtraction are highly welcome 🤗