decentralized finance education

Bitcoin Halving Explained. Code analysis.

What is Bitcoin halving?

Bitcoin halving is an event that reduces the supply of newly produced bitcoins by half. It occurs every 4 years or to be precise every 210000 blocks and it is a part of the validation logic in the Bitcoin source code.

Bitcoin halving is important to discuss as it affects miners profitability and often has an impact on the Bitcoin price. It is also one of the main reasons why Bitcoin is perceived as sound (hard) money with the inflation rate steadily trending to zero and becoming deflationary in a long run in opposite to all the other fiat currencies.

To understand it better let’s quickly recap how miners are being rewarded for validating transactions on the Bitcoin network.

Proof of Work

Transactions on the Bitcoin network are validated and confirmed by miners using a consensus mechanism called Proof of Work. In Proof of Work, miners are constantly validating transactions and grouping them in blocks. To prove that they did all the necessary work for the Proof of Work algorithm they try to solve a simple but laborious mathematical problem. The first miner who finds the correct solution submits a newly created block to the network and is rewarded for their hard work with a block reward. The block reward consists of two elements: transaction fees and block subsidy. The block subsidy is the number of bitcoins that is the exact subject to the Bitcoin halvings.

Previous Halvings

In January 2009, when the Bitcoin network was started, the first block called genesis block was mined. According to the algorithm, the block subsidy for the first block started from 50 bitcoins which was an arbitrary number chosen by the creator of the network Satoshi Nakamoto. In November 2012, Bitcoin went through its first halving at the block height of 210000 where the block reward cut in half from 50 to 25 bitcoins. At that time there was already 10.5 millions of bitcoins in existence. The second halving happened in July 2016 at the block height of 420000 with 15.75 millions of bitcoins in the circulation. The third, highly anticipated halving will be happening in May 2020 at the block height of 630000 with 18.375 millions of bitcoins in the circulation. After that Bitcoin halvings will be occurring every 4 years until roughly the year 2140 when the Bitcoin blockchain reaches the final number of 32 halvings and stops rewarding miners with block subsidy, so they will have to rely solely on the transaction fees.

Why does Bitcoin halving matter?

There are a few main reasons why Bitcoin halving matters. Let’s dive into some of them.

Sound (hard) money theory. Bitcoin halvings create controlled issuance of Bitcoin the currency with predictable inflation and eventual deflation when all bitcoins are mined. The current inflation rate is at around 3.6% and will decrease to around 1.8% after the 3rd halving. The inflation rate will be decreasing until 2140 when there will be no new supply of bitcoins. After that Bitcoin becomes deflationary as some of the already existing bitcoins will be lost.

Price impact caused by a supply shock. Assuming the demand for Bitcoin stays the same and the supply is cut in half, it should cause the price of Bitcoin to go up (at least in theory). With the current supply, the market is able to absorb 1800 new bitcoins per day (considering the most miners sell their rewards instead of hoarding). After the halving, the amount of newly created bitcoins will drop to 900 per day. If the demand stays the same it should increase the price of each coin. Bitcoin market of course is not that easily predictable and even though previous halvings had an impact on the price there is no guarantee it will happen again. This is also broadly discussed in the Bitcoin stock to flow model which can be used to try to predict Bitcoin future price based on the current stock (coins already in existence) and flow (issuance of new coins).

Rewarding early adopters. To incentivise the early adopters and help with bootstrapping the network the inflation rate was the highest at the time of starting the network. This means that early miners were able to receive the highest rewards (50 bitcoins per block) and accumulate more bitcoins. By looking at Bitcoin Money Supply we can see that at the time of creating this video 18 out of 21 million bitcoins were already minted which again incentives early adopters.

How does the Bitcoin halving actually look like in the code?

Now let’s get a bit more technical and jump into the code that is actually responsible for the Bitcoin halving. This code snippet is taken from the Bitcoin Core Github repository from the validation.cpp file that is responsible, as you may guess, for validation. I will put a link to this piece of code in the description box below.

Bitcoin halving instead of being defined as a time constant is actually defined using the number of blocks. We can see that the number of halvings is calculated using current block height (nHeight) and subsidy halving interval (nSubsidyHalvingInterval) which is a constant equal to 210000 that represents the number of blocks after which a halving should occur.

210000*10/60/24/365.25 = 3.9926990646.

Assuming that a block is mined on average every 10 minutes we can quickly calculate that it should take roughly 4 years to mine 210000 blocks. 619894 is the current block height at the time of making this video. This means that the halvings variable is currently set to 619894/210000=2.9518761905 which in integer division is just 2 although it’s getting very close to 3.

Next two steps are also quite interesting. We have an ‘if’ check that returns 0 when halving is greater or equal to 64 which is mostly used to protect the right-shift operator that comes later. This doesn’t mean there will be 64 halvings as we will quickly realise the actual subsidy will be 0 from 33rd halving onwards. The next step is to just get the first block subsidy in satoshis. COIN constant represents 1 bitcoin in satoshis (100 000 000 satoshis).

Now we’re getting to the actual calculation which is performed by using the right-shift bit operation instead of division for performance reasons. If we start with 5 000 000 000 and begin right shifting it, we can quickly see that the maximum number of times we can do it is 32. At the 32nd attempt, our reward will be 1 satoshi if we shift one more time it will give us 0 as the result. We can quickly calculate that if one halving occurs every 4 years on average, this means that the mining reward will stop around the year 2140. We can get to that number by using Bitcoin genesis block date which is the 3rd of January 2009 + 32*4 + 4 ( last halving, all 4 years before 33rd halving). This leads us to the 3rd January 2141 which is often rounded to 2140 as the final year for Bitcoin mining block subsidy rewards.

Let’s see what happens in the code in the next halving estimated for around the 5th of May 2020. When block height reaches 630000 the halvings variable starts returning 3 (630000/210000). After that we’ll be doing right bit shifting going from 50 * 100 000 000 = 5 000 000 000 >> 3 = 625 000 000 satoshis = 6.25 BTC. And that will be the new block subsidy reward for the next 4 years.

Summary

Bitcoin halving is one of the most important mechanisms when it comes to the Bitcoin network and its monetary system. It allows for predictable, decreasing inflation rate of the currency and incentives early adopters to bootstrap and secure the network.

Extras

Mastering Bitcoin (by Andreas Antonopoulos) ► https://amzn.to/2ILqylp

If you have any questions regarding Bitcoin or Bitcoin Halving comment down below.