主页 > imtoken里的ht > 比特币源码阅读——货币供应
比特币源码阅读——货币供应
imtoken里的ht 2023-04-13 06:17:41
1. 说明
比特币的初始货币供应是对问题解决者成功计算的 50 亿聪的奖励。每 210,000 个区块(10 分钟/区块大约需要 4 年)后,奖励呈几何级数减少:
求和公式计算总供应量(为什么 n=32比特币的源代码在哪里,因为 32 的奖励是 1 satoshi,然后 /2 奖励就没有了):
源码实现:
在比特币源代码中,总量(2100万亿聪)不是直接设定的,而是从每210,000个区块减半奖励中推断出来的
相关源代码:
class CMainParams : public CChainParams:
# 创世区块的50亿聪比特币的奖励 (50 * COIN)
genesis = CreateGenesisBlock(1231006505, 2083236893, 0x1d00ffff, 1, 50 * COIN);
# 减半周期是210000个块, 210000.0 * 10 / 60 / 24 /365 = 4 (大约4年减半一次)
consensus.nSubsidyHalvingInterval = 210000;
# 减半的逻辑
GetBlockSubsidy
总计算:
from decimal import *
COIN = 10 ** 8
base_reward = 50 * COIN
adjust_section = 210000
all_sectio_num = 33
def get_decimal_str(num):
return "{0:.8f}".format(((Decimal(num) / COIN).quantize(Decimal('0.00000000'))))
sum = 0
for i in range (0, all_sectio_num):
nSubsidy = base_reward
nSubsidy = nSubsidy >> i
sum += adjust_section * nSubsidy
print("sum: %s" % sum)
print("sum decimal: %s" % get_decimal_str(sum))
分阶段打印:
from decimal import *
sum = 2099999997690000
COIN = 10 ** 8
base_reward = 50 * COIN
adjust_section = 210000
all_sectio_num = 33 + 1
def get_decimal_str(num):
return "{0:.8f}".format(((Decimal(num) / COIN).quantize(Decimal('0.00000000'))))
def percentage(a, b):
if (abs(1.0 * b - 0.0) < 0.000001):
return "infinite"
else:
return ("%.8f%%" % (1.0 * a / b * 100))
summined = 0
print("s s s s s s s s" % ("Block", "Reward Era", "BTC/block", "Start BTC", "BTC Added", "End BTC", "BTC Increase", "End BTC % of Limit"))
for i in range(0 , all_sectio_num):
block = i * adjust_section
nSubsidy = base_reward
nSubsidy = nSubsidy >> i
currentmined = adjust_section * nSubsidy
endedbtc = summined + currentmined
increase = percentage(currentmined, summined)
endedbtcoflimit = percentage(endedbtc, sum)
print("d d s s s s s s" %(block, i+1, get_decimal_str(nSubsidy), get_decimal_str(summined), get_decimal_str(currentmined), get_decimal_str(endedbtc), increase, endedbtcoflimit))
summined = summined + currentmined
理论时间计算(以实际为准):
比特币减半是由明确的 210,000 个区块决定的。而每 4 年减半的奖励也只是从这个前提推导出来的。根据以下脚本比特币的源代码在哪里,如果挖矿算力不变,大约有 2140 个比特币将被挖出
from decimal import *
import datetime
sum = 2099999997690000
COIN = 10 ** 8
base_reward = 50 * COIN
adjust_section = 210000
all_sectio_num = 33 + 1
beginepoch = 1231006505 # GMT+08:00: 2009年1月4日星期日凌晨2点15分
oneyear = 60 * 60 * 24 * 365
def get_decimal_str(num):
return "{0:.8f}".format(((Decimal(num) / COIN).quantize(Decimal('0.00000000'))))
def percentage(a, b):
if (abs(1.0 * b - 0.0) < 0.000001):
return "infinite"
else:
return ("%.8f%%" % (1.0 * a / b * 100))
# GMT+08:00
def getepochstr(epoch):
return datetime.datetime.fromtimestamp(epoch).strftime('%Y-%m-%d')
current_epoch = beginepoch
summined = 0
print("s s s s s s s s s s" % ("Date reached", "Block","Reward Era", "BTC/block", "Year (estimate)", "Start BTC", "BTC Added", "End BTC", "BTC Increase", "End BTC % of Limit"))
for i in range (0, all_sectio_num):
# 每一轮打印4年
for j in range(0, 4):
block = i * adjust_section + adjust_section/4 * j
nSubsidy = base_reward
nSubsidy = nSubsidy >> i
currentmined = adjust_section / 4 * nSubsidy
endedbtc = summined + currentmined
increase = percentage(currentmined, summined)
endedbtcoflimit = percentage(endedbtc, sum)
estimate = current_epoch + oneyear
print("s d d s s s s s s s" % (getepochstr(current_epoch), block, i+1, get_decimal_str(nSubsidy), getepochstr(estimate), get_decimal_str(summined), get_decimal_str(currentmined), get_decimal_str(endedbtc),increase, endedbtcoflimit))
current_epoch = current_epoch + oneyear
summined = summined + currentmined
2. 参考资料