[区块链从原理到实现] - 区块链中的各种共识算法

[区块链从原理到实现] - 引用

• POW
• BFT
• PBFT
• IBFT
• POS
• POS
• DPOS
• RAFT
• PAXOS

IBFT

• 摩根大通的区块链源代码: https://github.com/jpmorganchase/quorum
• IBFT共识算法介绍: https://github.com/ethereum/EIPs/issues/650
IBFT共识中的基本流程也就是下面这个图了:

如果要分析IBFT的具体过程,那么参考上述的摩根区块链中的共识代码.
如果需要具体了解IBFT的运行机制参考上述的IBFT介绍的链接.
后续也会写一个文章专门来分析IBFT的具体执行流程,目前还没有完成.待完成后会把链接添加在此处.

PBFT

1. Request：请求端C发送请求到任意一节点，这里是0
2. Pre-Prepare：服务端0收到C的请求后进行广播，扩散至123
3. Prepare：123,收到后记录并再次广播，1->023，2->013，3因为宕机无法广播
4. Commit：0123节点在Prepare阶段，若收到超过一定数量的相同请求，则进入Commit阶段，广播Commit请求

DPOS

DPOS Consensus Algorithm  - The Missing White Paper
DPOS共识算法 - 丢失的白皮书（此处说明在开发DPOS的时候是没有这个白皮书的，这个白皮书是后补充的，姑且算作白皮书。）

This is the missing white paper and analysis of delegated proof of stake (DPOS).  The goal of this paper is to provide an analysis of why DPOS works and what makes it robust. An early description of DPOS can be found at bitshares.org; however, that description also includes many aspects that are not part of the actual consensus process.

All blockchains are fundamentally a deterministic state machine acted upon by transactions. Consensus is the process of agreeing on a deterministic order of transactions and filtering invalid transactions. There are many different consensus algorithms that could produce equivalent ordering of transactions, but DPOS has proven robust, secure, and efficient by years of reliable operation on multiple blockchains.

Like all consensus algorithms, the most harm the block producers can cause is censorship. All blocks must be valid according to the deterministic open source state machine logic.

Summary of DPOS Algorithm
DPOS算法摘要。
The DPOS algorithm is divided into two parts: electing a group of block producers and scheduling production. The election process makes sure that stakeholders are ultimately in control because stakeholders lose the most when the network does not operate smoothly. How people are elected has little impact on how consensus is achieved on a minute by minute basis. Therefore, this document will focus on how consensus is reached after the block producers have been chosen.
DPOS算法分为两部分：1.选举区块生产者；2.调度并产生区块。

To help explain this algorithm I want to assume 3 block producers, A, B, and C. Because consensus requires 2⁄3 + 1 to resolve all cases, this simplified model will assume that producer C is deemed the tie breaker. In the real world there would be 21 or more block producers. Like proof of work, the general rule is that longest chain wins. Any time an honest peer sees a valid strictly longer chain it will switch from its current fork to the longer one.
I will to show by example how DPOS operates under most conceivable network conditions. These examples should help you understand why DPOS is robust and hard to break.

Normal Operation

Under normal operation block producers take turns producing a block every 3 seconds.
Assuming no one misses their turn then this will produce the longest possible chain. It is invalid for a block producer to produce a block at any other time slot than the one they are scheduled for.

Minority Fork
Up to 1⁄3 of the nodes can be malicious or malfunction and create a minority fork. In this case the minority fork will only produce one block every 9 seconds while the majority fork will produce 2 blocks every 9 seconds. Once again, the honest 2⁄3 majority will always be longer than the minority.

Double Production by Disconnected Minority 断开的少数节点产生的同时出多个块的问题.
The minority can attempt to produce an unlimited number of forks, but all of their forks will be shorter than the majority chain because the minority is limited to growing the chain slower than the majority. 少数派可以试图产生无数的分叉,但是所有这些分叉都比主链要短,因为少数派的数量限制了分叉的链要比主链增长的慢.

Network Fragmentation 网络原因导致的分片.
It is entirely possible for the network to fragment in which case no fork has a majority of the block producers. In this case the longest chain will fall to the largest minority. When network connectivity is restored the smaller minorities will naturally switch to the longest chain and unambiguous consensus will be restored. 网络问题也可能导致分片,这种情况是没有分叉包含大多数块的生产者(如文章中三个出块节点A,B,C各自为一个分叉进行出块.这种情况下包含最多少数派节点的链就是最长的链.当网络连接恢复之后叫小的少数派就会自然切换到最长的链上.在此种情况下,共识能够正常运行,不存在冲突.)

It is possible for there to be 3 forks where the two longest forks are the same length.  In this case the producers on the 3rd (smaller fork) will break the tie when they rejoin the network.  There is an odd number of producers so it is impossible to maintain a tie for long. Later we will cover producer shuffling which will randomize order of production to ensure that even if two forks have the same number of producers, the forks will grow in different length bursts causing one fork to take over the other.

Double Production by Connected Minority	已连接的少数派在自己的出块时间产生了2个或者多个有效块
Under this scenario minority B produced two or more alternative blocks on their time slot. The next scheduled producer ( C ), may choose to build off of any one of the alternatives produced by B. When this happens it will become the longest chain and all nodes that selected B1 will switch forks. It does not matter how many alternative blocks a minority of bad producers attempt to propagate, they will never be part of the longest chain for more than a round. 假设B在自己的出块时间产生了2个或者多个有效块,那么到C出块的时候C会选择其中的一个,在其基础上出块,然后被C的选择的分支的长度就变成最长的了,然后A再出块的时候也是在这个分支上进行出块.这样B之前同时产生的块中其余没有被选中你的块就成了无效块就被丢弃了.


PoS机制虽然考虑了PoW的不足，但也有缺点：依据权益结余来选择，会导致首富账户的权力更大，有可能支配记账权。股份授权证明机制（Delegated Proof of Stake，DPoS），是对PoW、PoS不足的提出的。下面以比特股为例，理解DPoS的思想。

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客