PBFT algorithm java implementation (under)

java PBFT algorithm implementation (under)

In a previous blog (if you did not fancy a blog advised to see previous blog), we introduce the use of Java implementation PBFT algorithm nodes are added, such as synchronous operation of view. In this blog, I will describe the algorithm to achieve PBFT consensus process.

Project Address: GitHub

Consensus process

This is hyperchain official view showing a RBFT is a routine procedure. If you want to know more, you can go to the official website of reference hyperchain, or my previous blog .


In the process of consensus, a total of three stages: PrePrepare, prepare, commit. The above figure illustrates quite straightforward. The following simple language to introduce this process.

We envision a scenario in which we live in a village, everyone has their own little books, little books above record all expenses inside the village. One day the village spent 100 ¥, then mayor (master node) will spend this news broadcast, the broadcast message type is preprepare. After the villagers in the village received this news will certainly take a look inside the village is not really spent 100 ¥, if the news is true, then told other villagers inside the village is really spent 100 ¥ (message type is prepare ). When the name of a village received $ quorum = \ lceil \ frac { N + f +1} {2} \ rceil $ prepare message number, the message is considered true message (of course, the situation is actually more complex), then broadcast out I agree with (commit) written books of this expenditure, when a node receives quorumwhen a commit message, this will really 100 ¥ transaction information write their own small books.

The above transaction is a logical part of PBFT algorithm, but it is still fine to understand, follow the majority principle.

Next up will be in the code to understand this process.

Code

God continues to reposition itself in my pen Ma Liang.

首先我们假设一个有4个节点,其中一个主节点3个从节点。

主节点首先向所有的从节点广播pre-prepare消息(其中AC代表A_Client,AS代表A_Server)。


从节点(上面图片中的BCD节点)肯定会(实际上不一定会,因为会受到网络的因素)收到主节点发送过来的pre_prepare消息,当从节点验证此消息正确时,就会广播prepare消息。下面的一张图就是B从节点向ACD节点发送prepare消息。


在上面我们知道B节点会广播消息,其他的CD节点同样会广播消息。当节点受到一定数量(quorum)的prepare消息时,就会向外广播commit消息。


同样当节点受到一定数量(quorum)的commit消息时,节点就会将这个消息写入自己的块(block)中。


以上便是共识过程中的手绘流程图,在图中我们可以很清楚的知道当发送消息的时候,谁扮演的是客户端,谁又扮演的是服务端。至于为什么不是server广播消息而是client广播消息,在上一篇博客中已有说明,这里便不再赘述。

emm,至于怎么实现,可以去参考我的源代码,因为这个还是挺简单的,只要我们理解这个过程,其实实现起来还是比较简单的。

项目地址:GitHub,如果有任何问题,欢迎在评论区下方留言,或者使用Email私信我。

Guess you like

Origin www.cnblogs.com/xiaohuiduan/p/12359271.html