被回滚区块是否还保存在bitcoin全节点
2019-06-14 本文已影响0人
qishuai
之前有同事询问,过去被回滚的区块是否还能在全节点找到? 当时没有具体了解bitcoin在forked block上的处理,给他的结论是全节点不会保存被forked block信息,如果保留也是短暂时间内的。这个结论的主要依据是bitcoin源码中定义了一个BlockIndex
的链表数据,而这个结构只保留主链上的block,所以想当然的认为全节点是不会保留forked block信息的。
另外一种情况是: 历史上发生了区块回滚的事件,一个新建的bitcoin全节点,通过区块同步的方式下载所有区块链数据,那么可以肯定的是,在下载到最高度的区间内的所有forked block信息,将会全部丢失。因为区块同步下载的过程是不会涉及到forked block的。
获取forked block数据
bitcon给出的API getchaintips
中能够获得相关信息,具体使用方法如下:
> bitcoin-cli getchaintips
[
{
"height": 580618,
"hash": "00000000000000000021873a6237b6bf299c334925062609b5e91d39d46ca044",
"branchlen": 0,
"status": "active"
},
{
"height": 578141,
"hash": "0000000000000000001253a5f37d3763dbe928d21f7d72a708f05268c044179c",
"branchlen": 1,
"status": "valid-fork"
},
{
"height": 573313,
"hash": "0000000000000000000883f9f6eb9a459426f715ee3895a4dbb6e51e9237eceb",
"branchlen": 1,
"status": "valid-fork"
},
{
"height": 570069,
"hash": "000000000000000000214b7d19ea458da8f8e80bc437587a02eb418b5fd8b3aa",
"branchlen": 1,
"status": "valid-headers"
},
{
"height": 478576,
"hash": "000000000000000001416af072f8989829f4c60a1a9658e1cec08411798e4ffa",
"branchlen": 18,
"status": "headers-only"
}
]
该API返回的是一个json数组,各个字段的含义如下:
- height: 发生fork事件的高度
- hash: forked block hash值
- branchlen: 发生fork的深度
- status: forked block的验证状态
对于status
字段,其可能的值,以及具体含义可以查看如下代码:
std::string status;
if (chainActive.Contains(block)) {
// This block is part of the currently active chain.
status = "active";
} else if (block->nStatus & BLOCK_FAILED_MASK) {
// This block or one of its ancestors is invalid.
status = "invalid";
} else if (!block->HaveTxsDownloaded()) {
// This block cannot be connected because full block data for it or one of its parents is missing.
status = "headers-only";
} else if (block->IsValid(BLOCK_VALID_SCRIPTS)) {
// This block is fully validated, but no longer part of the active chain. It was probably the active block once, but was reorganized.
status = "valid-fork";
} else if (block->IsValid(BLOCK_VALID_TREE)) {
// The headers for this block are valid, but it has not been validated. It was probably never part of the most-work chain.
status = "valid-headers";
} else {
// No clue.
status = "unknown";
}
obj.pushKV("status", status);
分析
之所以bitcoin全节点保存了这些数据,是因为有一个全局的block容器mapBlockIndex
,来处理该节点遇到的所有block,以及header数据。上面提到只保留主链的容器是:
/** The currently-connected chain of blocks (protected by cs_main). */
extern CChain& chainActive;