How does the halving work? The code behind the hype

How does bitcoin halving work on a technical level? We explain it with the help of Andreas Antonopoulos.

In a recent video Antonopoulos explained in detail the core Bitcoin code that controls the halving of the Bitcoin Block Reward (BTC)..

The Bitcoin halving code. Source: Bitcoin Core Software.

Explanation line by line

The GetBlockSubsidy function is called every time a Bitcoin block is evaluated or a new block is dismantled. Its purpose is to calculate the appropriate block reward size. Line 1240 evaluates the split cycle in half and divides the current height of the block by 210,000; the interval between halving. At the time of this writing, the height of block 629369 is 2.99 if we divide it by 210,000. Since the variable in which this value is stored is of the type Integer, the decimal places are discarded. Based on this information, the code “knows” that we had two halves. The next half will therefore take place in block 630000.

Satoshi remains corrected

Lines 1242 and 1243 correct an error that Satoshi made in the original code. This error originally held the block reward in half over the last 64 halves; The total number of halves embedded in the Bitcoin protocol.

Line 1245 multiplies the reward of the original block of 50 by a constant coin equal to 100,000,000 Satoshis. Ironically The Bitcoin code does not contain any bitcoins, only Satoshis.

Line 1247 calculates the corresponding block reward for the height of the current block. A bitwise operation is used, which is more efficient than normal mathematical operations. The block reward calculated in line 1245 is adjusted according to the current block reward. This would currently look like this:

50 * 100,000,000 / 22nd= 1,250,000,000 satoshis or 12.5 bitcoins.

Finally Line 1248 returns the corresponding block reward when called by another function or procedure in the Bitcoin code.

If the halving is done in about 4 days or 631 blocks, we can all breathe a sigh of relief and appreciate the beauty of the Bitcoin code.