在Substrate中,Babe,Aura和Grandpa之间有什么区别

Jos*_*rff 1 consensus blockchain parity-io substrate

Substrate支持“可插入共识”,因此开发人员可以从几种共识算法中进行选择。它标配有四种算法:

Some of these (eg babe and grandpa) can even be used together in a single node. What are the differences between each consensus algorithm, and which ones can or should be used together?

Jos*_*rff 5

For a blockchain to be live (continue growing and adding new transactions), two things must happen to solve the problem of distributed consensus. Typically, these jobs are performed by full nodes as is the case with the default Substrate node.

  1. Block Authoring. Nodes create new blocks. Each new block contains a reference to a parent block.

  2. Block Finalization. When forks appear in the chain, Nodes must choose which side of the fork to consider the real or "canonical" one. Once a block is Finalized, the canonical chain will always contain it.

Let's look at each of the algorithms mentioned individually, and see how they accomplish those tasks.

Block Authoring

Aura

Aura primarily provides block authoring. In aura a known set of authorities are allowed to produce blocks. The authorities must be chosen before block production begins and all authorities must know the entire authority set. Time is divided up into "slots" of a fixed length. During each slot one block is produced, and the authorities take turns producing blocks in order forever.

In Aura, forks only happen when it takes longer than the slot duration for a block to traverse the network. Thus forks are uncommon in good network conditions.

Babe

Babe also primarily provides block authoring. Like, Aura, it is a slot-based consensus algorithm with a known set of validators. In addition, each validator is assigned a weight which must be assigned before block production begins. Unlike Aura, the authorities don't take turns in order. Instead, during each round, each authority generates a pseudorandom number using a VRF. If the random number is lower than their weight, they are allowed to produce a block.

Because multiple validators may be able to produce a block during the same slot, forks are more common in Babe than they are in Aura, and are common even in good network conditions.

Substrate's implementation of Babe also has a fallback mechanism for when no authorities are chosen in a given slot.

Proof of Work

Proof of Work also provides block authoring. Unlike Babe and Aura, it is not slot-based, and does not have a known authority set. In proof of Work, anyone can produce a block at any time, so long as they can solve a computationally challenging problem (typically a hash preimage search). The difficulty of this problem can be tuned to provide a statistical target block time.

Block Finalization

Probabilistic methods

Each of the block authoring mechanisms we've discussed previously needs to know where on the chain is should build the next block. Methods such as the "longest chain rule" "heaviest observed subtree" often work in practice and provide probabilistic finality. That is, with each new block that is added to a chain, the probability that it will be reverted decreases, approaching zero. When true certainty that a block is final is desired, a more sophisticated game can be used.

Grandpa

Grandpa provides block finalization. It has a known weighted authority set like Babe. However, Grandpa does not author blocks; it just listens to gossip about blocks that have been produced by some authoring engine like the three discussed above. Each authority participates in two rounds of voting on blocks. The details of the voting are beyond the scope of this post. Once 2/3 of the grandpa authorities have voted for a particular block, it is considered finalized.

Hybrid Consensus

In general a block authoring engine and a finality gadget can be used together in a single chain, as Babe and Grandpa are in the code linked in the question. When such a system is used, block authoring engines must be made aware of blocks that are finalized so that they don't waste time building on top of blocks that will never be in the canonical chain.

关于权重的注意事项:与Substrate捆绑在一起的Babe,Grandpa和许多其他算法都依赖于权重。共识算法本身通常不规定权重的分配方式,而是假设它们以某种方式分配,而将分配留给外部机制。在公共网络中,通常基于放样多少令牌来分配权重。在默认的“底物”节点中,所有权重都设置为,1因为phragmen算法可使所有验证器保持接近相等的赌注。