Consensus algorithm for blockchain


Consensus algorithm for blockchain


I wrote it in the last articleDistributed consistency protocolRelated theories and algorithms, those algorithms can not be used in the blockchain system, can not prevent evil situations, can only tolerate node downtime, network partition and other situations.

Let’s have a look at this sectionBlockchainThe consensus algorithm is commonly used in. Let’s see why distributed networks need consensus?

two-army problem

Consensus algorithm for blockchain

As shown in the picture, the White army is powerful and lives in an important place. The blue army is separated by the White army and becomes two armies. Only when the two blue armies reach an agreement (when to attack the White Army), can they defeat the White army. However, in order to reach an agreement, blue 1 and blue 2 must use messengers to communicate with each other through the territory of the White army in order to attack and win at the same time. However, as the White army may arrest the messenger, the two blue armies can not reach a consensus.Therefore, the two armed forces problem expresses that the channel is not trusted (message loss, timeout, etc.), and if there is a trusted channel, the two armed forces problem can be solved.

Communication between the two armies, like TCP’s three handshakes, requires both sides to send and receive feedback to confirm each other and reach a consensus by receiving the correct message.

Byzantine Generals Problem

The question of Byzantine Generals is a matter of consensus: first, byLeslie LamportByzantine failure was proposed in 1982.The core description is that there may be traitors in the army, but the attack must be consistent. Compared with the distributed system, there are malicious nodes in the nodes, which may also be attacked by hackers.

![](data:image/svg+xml;utf8,<?xml version=”1.0″?>)

The picture is a bit ugly. We’ll make do with it.

The Byzantine Empire wanted to attack a powerful city, so it sent 10 troops to encircle the enemy. Although this enemy was no better than the Byzantine Empire, it was also able to resist the simultaneous attacks of five conventional Byzantine armies. For some reasons, these 10 armies can’t gather together to make a single breakthrough, and must attack at the same time under separate encirclement. There is no chance that any of their troops will win if they attack alone, unless at least six troops attack at the same time. They are scattered around the enemy country and rely on the communication between the signalmen to negotiate the intention and time of attack.The problem that bothers these generals is that they are not sure whether there are traitors among them, and the traitors may arbitrarily change their attack intention or attack time. In this state, can the Byzantine Generals find a distributed protocol that allows them to negotiate remotely and win the battle? This is the famous Byzantine general problem.

It should be clear that,Byzantine general problem does not consider whether the signalmen will be intercepted or unable to convey information, that is, the message transmission channel is reliable and safe. Lamport has proved that it is impossible to achieve consistency through message delivery on unreliable channels where messages may be lost. It is assumed that the channel is safe and reliable

In the final analysis, this problem is an algorithm problem about consistency and correctness. This algorithm is aimed at loyal generals, because traitors can not pass, or pass messages to make trouble. We just want to find a Byzantine fault tolerance algorithm BFT (Byzantine fault tolerance) under the interference of traitors.

It can be seen that the two armies problem is a special case of Byzantine general problem.

What can be called Byzantine error?

The node that does evil or is attacked by hackers. Node downtime, network partition, timeout and so on are not Byzantine errors.

How to solve the problem of Byzantine Generals?

Adjutant commander model

In the question of Byzantine Generals: each general needs to communicate with all generals, and has learned about the attack arrangements of other generals, so as to reach a consensus. So the Byzantine general problem can be simplified as the commander – adjutant model. A commander, multiple adjutants, need consistency agreement to ensure that the commander issued orders, multiple adjutants can get consistent results.

A commander passes his command to n-1 adjutants, so that:

uniformity: all loyal adjutants obey one command (most N / 2 + 1 in the result set).

CorrectnessIf the commander is loyal, every loyal adjutant obeys his orders. (if the general does evil, he only needs to abide by the first rule.)

Why does BFT need node number n > = 3F + 1?

F is the number of Byzantine error nodes, which is also the number of traitors that BFT can tolerate.

To the contrary, if n < 3F + 1, that is, when f = 1, n = 3.

  1. If the commander is loyal, an adjutant is loyal, and an adjutant is a traitor. When the commander sends an offensive order to two adjutants, the two adjutants will ask each other what the commander’s order is. At this time, the traitor’s adjutant will forge a false order, saying that the command given by the commander is to retreat, and the loyal adjutant will be confused, because what he receives is an offensive order. At this time, the loyal adjutant does not know whether the commander is a traitor or another adjutant is a traitor.

  2. If the commander is a traitor, the two adjutants are loyal. The commander gave each of the two adjutants an attack and a retreat; when the two adjutants communicated, they found that the orders they received were inconsistent and they could not reach a consensus.

So when n < 3F + 1, we can’t achieve Byzantine fault tolerance, we need n > = 3F + 1. The specific derivation can be seen in the paper in the reference, or the video of Teacher Li Yongle.

PBFT(Practical Byzantine Fault Tolerance)

BFT assumes that there is no problem in the channel, that is, it does not consider the unreachable message, message loss, disorder, repetition, network partition and so on. Miguel Castro (Castro) and Barbara Liskov (Liskov) proposed pbft algorithm for the first time in their paper practical Byzantine fault tolerance published in 1999. The number of fault tolerance of this algorithm also satisfies 3F + 1 < = n.

The basic flow of pbft algorithm includes the following four steps

This work adoptsCC agreementReprint must indicate the author and the link of this article