# 什么是哈希值？

SHA256(
\$previousBlockHash,
\$newTransactionsToBeIncluded,
\$magicNumber
);

00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048

# GetBlockSubsidy()函数

CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
{
int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;
// Force block reward to zero when right shift is undefined.

if (halvings >= 64)
plain
return 0;

CAmount nSubsidy = 50 * COIN;

// Subsidy is cut in half every 210,000 blocks which will occur approximately every 4 years.
nSubsidy >>= halvings;
return nSubsidy;
}

## 已经发生过多少次比特币减半了？

int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;

consensus.nSubsidyHalvingInterval = 210000;

int halvings = 630000 / 210000;

int halvings = 620000 / 210000;

## 区块奖励结束

// Force block reward to zero when right shift is undefined.

if (halvings >= 64)

return 0;

## 你能得到多少比特币作为奖励？

“减半”一词其实指的是对矿工可以获得的比特币数量进行限制。每采出210,000个区块，这个奖励金额就会减为一半

CAmount nSubsidy = 50 * COIN;

nSubsidy >>= halvings; return nSubsidy;

nSubsidy >>= halvings;

CAmount nSubsidy = 50 * 100000000;

nSubsidy >>= 3; return nSubsidy;

100101010000001011111001000000000

nSubsidy >>= 3表示将位右移3位。这就得到了：

100101010000001011111001000000

# 结论

• 如何让所有节点达成共识？
• 如何防止结果发生偏差？
• 如何防止网络连接中断造成的影响？
• 如何防止数据损失造成的影响？
• 如果一个节点在执行写操作的过程中崩溃了怎么办？

Mattias Geniar，独立开发人员，Linux系统管理员和通用问题解决者。

Dissecting the code responsible for the Bitcoin halving

• 发表于:
• 本文为 InfoQ 中文站特供稿件
• 首发地址https://www.infoq.cn/article/ItUzqarOb4AUyxmIvPJh
• 如有侵权，请联系 yunjia_community@tencent.com 删除。

2020-05-18

2020-05-18

2020-05-15

2020-05-15

2020-05-15

2020-05-15