共识算法-PBFT简释

算法整体流程

算法流程大致如下:

  1. 客户端请求主节点执行一些操作(发送请求)
  2. 主节点将请求多播到所有备份节点
  3. 各节点执请求,响应客户端
  4. 客户端等待f+1个不同节点响应的相同结果

    The algorithm works roughly as follows:

    1. A client sends a request to invoke a service operation to the primary
    2. The primary multicasts the request to the backups
    3. Replicas execute the request and send a reply to the client
    4. The client waits for f+1 replies from different replicas with the same result; this is the result of the operation.

    Three Phase 共识的三个阶段

  • σi表示i节点对消息m进行签名

    pre-prepare 预准备阶段

    主节点接收到客户端请求后,为请求分配一个序列号 n ,将预准备消息(pre-prepare message)多播给所有备份节点,并将其追加到日志中。

预准备消息:<σp,m>

  • v:视图号
  • d:m的摘要
  • n:视图中的消息序列号
  • m:客户端的请求消息
In the pre-prepare phase, the primary assigns a sequence number, , to the request, multicasts a pre-prepare message with piggybacked to all the backups,and appends the message to its log. The message has the form <σp,m>, where indicates the view in which the message is being sent, is theclient’s request message, and is ’s digest.

prepare 准备阶段

如果节点i接收到经校验无误的预准备消息,就将准备消息多播给所有节点进入准备阶段,并将预准备消息和准备消息追加到日志中。否则不作任何处理。

准备消息:σi

If backup accepts the <σp,m> message, it enters the prepare phase by multicasting a σi message to all other replicas and adds both messages to its log. Otherwise, it does nothing.

谓词(predicate) prepared(m,v,n,i)为真,当且仅当节点i在日志中插入了:

  • 请求消息m
  • m的预准备消息 <σp,m>
  • 与预准备消息匹配的2f个节点(包括自己)的准备消息
We define the predicate prepared to be true if and only if replica has inserted in its log: the request m, a pre-prepare for in view with sequence number n, and 2f prepares from different backups that match the pre-prepare. The replicas verify whether the prepares match the pre-prepare by checking that they have the same view, sequence number, and digest.

预准备与准备阶段保证了所有正常节点在特定视图中请求的排序一致

The pre-prepare and prepare phases of the algorithm guarantee that non-faulty replicas agree on a total order for the requests within a view.

commit 提交阶段

当谓词 prepared 为真时,节点将提交消息多播给其他节点

提交消息:σp

Replica multicasts a COMMIT to theother replicas when prepared becomes true.

谓词 committed(m,v,n) 为真,当且仅当某f+1个节点中的谓词 prepared 为真

committed(m,v,n) is true if and only if prepared(m,v,n,i) is true for all in some set of f+1 non-faulty replicas

谓词 committed-local(m,v,n,i) 为真,当且仅当谓词prepared为真,且收到了来自不同节点(通常包括自己)2f+1个与m的预准备消息匹配的提交消息。即 v, n, d相同

committed-local(m,v,n,i) is true if and only if prepared is true and has accepted 2f+1 commits (possibly including its own) from different replicas that match the pre-prepare for m. a commit matches a pre-prepare if they have the same view, sequence number, and digest.

在谓词 committed-local(m,v,n,i) 为真之后,节点执行m请求,i的状态反映了较小序列号请求线性执行的结果。

Each replica executes the operation requested by m after committed-local is true and i’s state reflects the sequential execution of all requests with lower sequence numbers.

算法流程示例

3个正常节点,1个离线节点

  1. A节点接收到请求后,将<σp,m>(省略为[P-P])多播给所有备份节点
  2. 各备份节点校验正确后,进入准备阶段,将σi(省略为[P])多播给所有节点
  3. 各节点谓词 prepared 为真后,将σp(省略为[C])多播给所有节点。谓词 prepared 的意义在于节点可借此判断自己是否收与到了正确的信息,与网络中的信息一致。但并不能判断其他节点是否也正确收到了消息(因为可能存在拜占庭节点向不同的节点发送不同的消息)。
  4. 各节点谓词 committed-local 为真后,执行请求,并向客户端响应执行结果。谓词 committed-local 的意义在于,可以确认 2f+1个(即n-f,除拜占庭节点外的所有节点)已经收到了正确的消息,因此可以执行请求内容。

共识算法-PBFT简释_第1张图片
需要注意的是

  • 第一阶段只有主节点发送消息,第二阶段主节点不发送消息
  • 节点会持有自己向其他节点多播的消息,例如第二阶段后B节点只收到了一条(2f-1)[P],加上自己持有的[P]即为两条(2f)

    3个正常节点,主节点为拜占庭节点

    主节点A向BC发送了<σp,m>(省略为[P-P],相应的PREPARE消息为[P]),向D发送了<σp,m'>(省略为[P-P]',相应的PREPARE消息为[P]')

在prepare阶段后,正常节点的谓词prepared为假,不再进入commit阶段
共识算法-PBFT简释_第2张图片

文献

Miguel Castro and Barbara Liskov. “Practical Byzantine fault tolerance” Operating Systems Design and Implementation (1999).

你可能感兴趣的:(区块链分布式)