If there are 32 halvings, why is the if block in the GetBlockSubsidy function checking if there are equal or more than 64 halvings?
Asked
Active
Viewed 233 times
1 Answers
3
Variable nsubsidy
is of type CAmount
which is an int64
.
As the comments say in the code you quoted, a 64-bit integer value can have no more than 64 bitwise right-shift operations meaningfully performed. So this is the limit applied.
It just happens that the initial subsidy 50 * COIN
is a much smaller value that falls to zero only around 32 bitwise right shifts.
So the limit is based on the data type not the initial value. Regardless of the initial value, we know that the result must be zero after a 64-bit variable is right shifted 64 times.
A bitwise rightshift is a pretty cheap operation in CPU cycles so there's probably not much to be gained by optimising further here.

RedGrittyBrick
- 26,841
- 3
- 25
- 51
-
See also BIP42 for background (while it's written in a humorous way - check the publication date - it does describe an actual big that existed before this check was introduced). Note that Bitcoin was always intended to have a 21M BTC limit; this BIP did not actually make the supply finite. – Pieter Wuille Jul 30 '23 at 15:22
-
1LOL – RedGrittyBrick Jul 30 '23 at 15:39
-
Ok so would bitcoin work just fine after 2140 if the code was like this? if (halvings > 33) return 0; – Efe Jul 30 '23 at 16:55
-
1@Efe Yes, that would behave exactly the same way. – Pieter Wuille Jul 30 '23 at 17:02