做地址监控还碰到几个不太明确的问题,需要找出解决方案。
getBlock取到区块后,这个链有可能不是最长的,会不会被另一个更长的链替换掉?那样取到的信息哪些会变动?交易是否还有效?
以太坊的叔块是否需要判断?前一版本是没有对这个做判断的
交易错误的处理,以太坊中有因为gas不够而交易出错,但交易列表中是正常的,可能需要检测交易收据。
先看看bitcoin的最长链判断,基于以下代码:
struct CBlockIndexWorkComparator
{
bool operator()(CBlockIndex *pa, CBlockIndex *pb) const {
// First sort by most total work, ...
if (pa->nChainWork > pb->nChainWork) return false;
if (pa->nChainWork < pb->nChainWork) return true;
// ... then by earliest time received, ...
if (pa->nSequenceId < pb->nSequenceId) return false;
if (pa->nSequenceId > pb->nSequenceId) return true;
// Use pointer address as tie breaker (should only happen with blocks
// loaded from disk, as those all have id 0).
if (pa < pb) return false;
if (pa > pb) return true;
// Identical blocks.
return false;
}
};
用三个规则进行判断,从1到3如下:
第3条规则与抛硬币差不多,判断指针大小,这条规则的目的是为了总能比较出个结果,客户端能选一条链进行工作。
网络在同一时间找到两个不同的块是非常罕见的,如果出现后就可能基于不同链工作,只到下一个区块产生使另一分支变得更长,就会选择更长的分支工作。
真正起作用的还是第1条规则,看哪个条链最长!
比特币白皮书中有一段对此的描述
Nodes always consider the longest chain to be the correct one and will keep working on extending it. If two nodes broadcast different versions of the next block simultaneously, some nodes may receive one or the other first. In that case, they work on the first one they received, but save the other branch in case it becomes longer. The tie will be broken when the next proof-of-work is found and one branch becomes longer; the nodes that were working on the other branch will then switch to the longer one.
一旦有矿工在两条链上工作就会出现分叉,有两个并行链进行,在其中一条链变成最长时另外一条链会被拒绝。
被拒绝链中的交易会根据规则进行核对,重复的或者冲突交易(如双花)会简单的丢弃,其余的交易被放到接收链将来的块中。
被拒绝的块的矿工不会获得奖励。
在这可以查到比特币上孤块的一些信息
https://blockchain.info/orphaned-blocks
https://bitcoin.stackexchange.com/questions/37273/how-is-a-blockchain-split-resolved
http://cointext.com/2013/10/28/bitcoin-blockchain-the-longest-chain-wins/
https://blog.ethereum.org/2014/07/11/toward-a-12-second-block-time/