pull down to refresh

Just published online on Stackchain Magazine: https://stackchainmagazine.net/camount-getblocksubsidy/
Written by @realBitcoinDog
CAmount GetBlockSubsidy() By: Will Schoellkopf
Will Schoellkopf is author of The Bitcoin Dog: Following the Scent to the Bitcoin C++ Source Code, and this article contains excerpts from the chapter, “SCENT 3: 21 MILLION BITCOIN LIMIT.”
bitcoin/src/validation.cpp
1667 CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
1668 {
1669 int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;
1670 // Force block reward to zero when right shift is undefined.
1671 if (halvings >= 64)
1672 return 0;
1673
1674 CAmount nSubsidy = 50 * COIN;
1675 // Subsidy is cut in half every 210,000 blocks which will occur
    approximately every 4 years.
1676 nSubsidy >>= halvings;
1677 return nSubsidy;
1678 }
Halvings nSubsidy (satoshis) Binary value 0 5,000,000,000 0b100101010000001011111001000000000 1 2,500,000,000 0b10010101000000101111100100000000 2 1,250,000,000 0b1001010100000010111110010000000 3 625,000,000 0b100101010000001011111001000000 4 312,500,000 0b10010101000000101111100100000 5 156,250,000 0b1001010100000010111110010000 … … … 30 9 0b1001 31 4 0b100 32 2 0b10 33 1 0b1 Who says there will only ever be 21 million Bitcoin? There is no hard-coded limit defined above. You say you don’t trust, but do you verify? Have you even read the code running on your node? Steady lads, because you’re going to learn today. Hold my hand, as I lead you on a journey down the Bitcoin rabbit hole that’s deeper than you’ve ever gone before. So deep that we will reach the raw bits of Bitcoin — literal 1’s and 0’s — that make up the Bitcoin C++ Source Code.
Instead of specifying a hard limit to the number of bitcoin that will ever be mined, the bitcoin reward for each mined block gets reduced, programmatically. This brilliant innovation helps bootstrap the network. In the beginning, when the world didn’t fully appreciate Bitcoin’s potential value, the mining reward was 50 bitcoin per block to try to entice miners. However, instead of keeping the reward at 50 forever, after four years (or 210,000 blocks), the reward was cut in half to 25 bitcoin. It’s important to gradually lower the bitcoin reward rather than immediately set it to zero so that miners continue to want to participate in the network. Additionally, as the years pass, and the bitcoin reward per block decreases and decreases, the value of that bitcoin should go up as seen in the eyes of the world even though the amount of bitcoin will go down.
1667 CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
When Satoshi Nakamoto envisioned the role of the miners, it had nothing to do with rewarding them with bitcoin for finding a valid proof-of-work. Rather, it was about subsidizing their electricity and computational costs for helping secure the network. Bitcoin is susceptible to a 51% Attack: If a single bad actor accumulates over half of all the hashing power in the world, they become the majority and thereby the consensus. With this power, they can mine blocks to spend bitcoin and confirm transactions, but then go back and generate different blocks to form a different version of history, and re-spend the same bitcoin. This is called the Double Spending problem. While the nodes are programmed to believe that the longest chain of blocks is the real chain because it has the most hashing power or proof-of-work behind it, the bad actor could have already double-spent their bitcoin on goods and services without having actually paid for it.
By giving miners this subsidy when they find a valid block, they are rewarded for contributing hashing power to the network. The more miners acting in good faith, the harder it is for any bad actors to get the majority of the hashing power. Satoshi’s master plan worked! By slowly lowering the bitcoin rewarded to the miners, he was able to bootstrap the network, and now there is so much hashing power behind Bitcoin, so much incentive for the good faith actors to keep the network healthy and strong, it is inconceivable that anyone could ever try to take majority control.
One of the things the nodes check for in a valid block is that the correct amount of bitcoin reward is being passed on to the miner for producing this block. GetBlockSubsidy is called to determine what that reward should be, and it will return the correct (typedef int64_t) CAmount of bitcoin. nHeight is the current block’s number, or height in the blockchain. consensusParams is a global variable that includes important const or constant information to be used throughout the Bitcoin source code that is immutable.
1669 int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;
Satoshi Nakamoto determined that every time the Bitcoin block reward or subsidy was adjusted, it should be cut in half. So, while the Bitcoin blockchain was launched with 50 bitcoin rewarded per block, the nodes must determine which halving era we’re in to know what should be the proper reward for the block they are validating. halvings is a local integer variable to store which halving era we are on. consensusParams.nSubsidyHalvingInterval is 210,000. One block gets mined on average every 10 minutes due to the difficulty adjustment, so 10 minutes per block for 210,000 blocks leads to a halving once every four years.
1670 // Force block reward to zero when right shift is undefined.
1671 if (halvings >= 64)
1672 return 0;
There will only ever be 33 halving eras for the Bitcoin blockchain, but the C++ code doesn’t necessarily know that! We will see shortly how a right shift operator is applied to the local variable nSubsidy which is the return value of this function. nSubsidy is defined as a CAmount variable which has a type definition or typedef of int64_t meaning it is a 64-bit integer. Since it is only 64 bits, it can only be right shifted 64 times; otherwise, the code will have undefined behavior. Thankfully, Bitcoin is well-tested! While this if statement shouldn’t evaluate to true until about the year 2,265, the developers have accounted for this case with BIP-42 to solve the “infinite inflation bug” (restart block rewards every 64 halvings) which may have resulted from such undefined behavior.
1674 CAmount nSubsidy = 50 * COIN;
COIN is a global variable set to 100,000,000. In truth, the Bitcoin network doesn’t actually think in terms of bitcoin, but rather satoshis. The reason a bitcoin is represented as having eight decimal places is because in the code it’s really represented as 100 million satoshis. To simplify things, we may think of bitcoin in terms of satoshis to the power of 10^8, but one day bitcoin will be so valuable, items will have their costs denominated in terms of satoshis, not bitcoin. So, to start, as our base case, before we apply any halvings, we set the nSubsidy to 5,000,000,000 satoshis, or 50 bitcoin.
1675 // Subsidy is cut in half every 210,000 blocks which will occur
    approximately every 4 years.
1676 nSubsidy >>= halvings;
1677 return nSubsidy;
In Bitcoin, miners get rewarded every time they produce a block, but the reward corresponds to the halving era the block was produced in. There will only be 33 halvings which is why it’s estimated the last bitcoin will be mined in 2140. But why 33 halvings? Why the year 2140? Why would Satoshi pick 2140 – he won’t even be alive by then!
The original mining reward of 5,000,000,000 is represented in binary as 0b100101010000001011111001000000000. Binary numbers are only 0’s or 1’s always representing only two different values. So, to cut a binary number in half, all you have to do is right shift it by one digit! It’s all specified in the code here. The shift right operator >>= makes it so it drops a binary digit (1 or 0) every halving, cutting the value of the block reward in half. Since 5 billion is 33 digits long in binary, there can only be 33 halving eras. Finally, we return nSubsidy. Since a halving era happens about once every four years, the end of the final era should be 2140. In this issue of Stackchain Magazine, we are commemorating the 4th halving event, when sometime in April 2024 the world will mine the 840,000th block, and commence the 5th block subsidy era which will award 3.125 bitcoin per block (plus miner fees). The value of our stackchained bitcoin savings, for example, will increase overnight since the global production of bitcoin will have halved, making new bitcoin even more scarce.
There is no hard-coded 21 million number. Instead, the subsidy for a new block eventually falls to below 0.00000001 bitcoin, which is below one satoshi and no longer a valid unit of bitcoin. Therefore, if you sum up all of the bitcoin subsidies that will ever be issued, it just happens to come out to about 21 million bitcoin. A hard fork would have to update this code for the reward, and hence the limit, to increase, but that would not be accepted by the nodes, for then it would no longer be Bitcoin. The value of Bitcoin is defined by its scarcity, so the majority would never accept this decrease in the value of their investment.
This point can’t be emphasized enough: We run the nodes. We validate the blocks. Us stackchainers, WE are the majority. Just like #stackchain, we are DECENTRALIZED! There is nobody telling us what code we run. The Bitcoin code can be changed, and it has: Bitcoin Cash, BSV, and DogeCoin are all hard forks of Bitcoin that have changed the code. While attackers can copy the code, they can’t copy the network. They can’t convince us node runners to break our nodes from enforcing the Bitcoin 21 million limit. We. Give. This. Value. As is “Not your keys, not your coins,” so is “Not your node, not your rules.”

Note from Stackchain Magazine: No Bitcoin (or inferior monies) were exchanged for this article. This article was written by Will Schoellkopf. you can find him on X @realBitcoinDog, stalk him on Nostr: npub1qkl3mdr672ph7zc75qh5evr92atcqxazn23h4csww2zv20ahr33sla0cpf, or if you wanted to send him a tip you can do so at lnurl: realbitcoindog@getmash.cash
Note from Will Himself. Will Schoellkopf is the author of two books: Bitcoin Girl: Save the World and The Bitcoin Dog: Following the Scent to the Bitcoin C++ Source Code. He hosts the Bitcoin podcast It’s So Early! and publishes a weekly newsletter, featuring his favorite Bitcoin Posts of the Week, at realbitcoindog.com. His work has appeared in the anthology 21 Futures: Tales from the Timechain, in print at Bitcoin Magazine and Citadel21, and online at Satoshi’s Journal. Follow him on X @realBitcoinDog, or email will@realbitcoindog.com
Note from Stackchain Magazine: Damn… Will has pretty much written for any Bitcoin publication that you’ve ever heard of. He’s a beast. we’re pretty sure he may have written the first draft of the white paper.
The more I read about Bitcoins, the more I wonder how Satoshi could have thought of so much. This all makes me think it was definitely a group that created Bitcoin, not just one person. Thanks for the post
reply
He stood on the shoulders of giants like Adam Back and Wei Dai
In that sense it was a group. He had the altruism to not seek any control whatsoever
reply
10 sats \ 3 replies \ @k00b 15 Feb
realbitcoindog@getmash.cash
Mash has an ecash mint?
reply
It’s defunct now sadly
No it was just an lnurl address
reply
10 sats \ 1 reply \ @k00b 15 Feb
Ah. I miss those guys.
reply
Yea I was a SUPER early adopter for my books. Published out my site and how it worked with them in great detail on Satoshi’s Journal!
They disappeared in May in the great “no lightning in USA” exodus
reply