Ethereum中节点之间区块的同步过程(The Process of Synchronization between Nodes in Ethereum)
* 产生同步的条件
在区块的同步过程中,一个节点每次只会与另外其他一个进行同步,并且只有出现以下三种情况之一的时候节点之间才会开启一次同步:
- 节点N connect一个新节点P,P有更大的总难度值(P节点的区块链长度更长)时,N和P之间开启一次同步
- 与N已经connect的节点P通告它的total difficult值比N要大,N和P之间开启一次同步
- N收到一个比当前其区块链上的头结点的total difficulty值更大的区块,并且在N的链中找不到这个接收到的区块的祖先节点
文献原文:
A node will only synchronise with one other node at a time. A node N starts a block synchronisation in the following cases:
1. N starts a connection to a new peer with higher advertised total difficulty(e.g. after joining or rejoining the network).
2. A node advertising a higher total difficulty than N connects to N.
3. N receives a block with higher total difficulty than the head of its current blockchain and is missing some of the block’s ancestors.
* 同步的详细过程
Node N with lower total diffficulty,P with highter total difficulty
- 节点N获取P上最新区块的header。
- 若N的链上不存在这个最新的区块,那么N通过以下方法进行同步:
- N从P处获取MaxHeaderFetch个最新的区块的头(从P的链上从顶至尾的MaxHeaderFetch个区块),MaxHeaderFetch的默认值为256,但P发送的区块数量可以小于MaxHeaderFetch的值。
- 如果N接收到的区块都没有在N的区块链中,那么N每次从P处请求一个BlockHerder,并在N的区块链中进行二分搜索来查找N和P的链的最新的公共祖先区块。
- 在N找到公共祖先后,N每次请求MaxHeaderFetch个区块(包括Block Header和Block Body)。
文献原文:
1. The node with lower total difficulty (node N) sends a GetBlockHeaders request to the node with higher total difficulty (node P), requesting the header of the latest block of P.
2. P responds with a BlockHeaders message containing the block header of the block specified in the received GetBlockHeaders message.
3. N requests the MaxHeaderFetch (= 256) blocks starting at MaxHeaderFetch blocks below the height of its own blockchain.
4. P sends up to MaxHeaderFetch of the requested blocks (but may send fewer).
5. If none of the blocks received from P are in N’s blockchain, N starts a binary search over its own blockchain to find a common ancestor,requesting one block from P per step in the search.
6. As soon as N finds a common ancestor, N requests block headers and bodies from P starting from the common ancestor. N asks for MaxHeaderFetch blocks per request, but P may send fewer.
参考资料:
[1] Karl, Security of Blockchain Technologies (Ph.D. thesis), Swiss Federal Institute of Technology, 2016,p.3.
[2]X Li, P Jiang, T Chen, X Luo, Q Wen,A survey on the security of blockchain systems,Future Generation Computer Systems, 2017,pp. 32–33.