Section2.3.5 stack
Stack
是consensus
里一个主要的接口, 包含了consensus.go
里定义的除自身和Consenter
之外的所有接口.
在pbft
内部, 也定义了一个与此对应的接口innerStack
, 用于内部使用.
对于Stack
里一些方法的介绍在前面各部分已经提到了一部分, 这里综合地描述下Stack
的各个部分.
Stack is the set of stack-facing methods available to the consensus plugin
Vars
虽然在注释中地提到了在consensus/helper/handler.go
中定义的ConsensusHandler
也实现了Stack
, 但是查找代码会发现, 其实只有consensus/helper/helper.go
中定义的Helper
实现了Stack
. 所以下面只会讨论Helper
.
Helper contains the reference to the peer's MessageHandlerCoordinator
consenter
consenter
consenter
成员变量的类型为Consenter
, 即consensus
里定义的Consenter
接口. 注意到, Stack
并不包括Consenter
.
在Engine
的初始化过程中, 会先创建Helper
, 然后用Helper
作为参数创建obcBatch
(即Consenter
的实现), 然后再把obcBatch
赋给Helper
, 整个过程下来, Helper
和Consenter
两者会互相包含对方作为成员变量.
Helper
中仅使用consenter
来实现ExecutionConsumer
接口. 在ExecutionConsumer
已经提到. 这些方法将会在executor
中被使用.
coordinator
coordinator
coordinator
类型为peer.MessageHandlerCoordinator
, 定义在peer
中, 是初始化engine
时的唯一参数, 也可以说是和节点联系的途径. engine
使用它获得PeerEndpoint
(本节点), 然后将它传给Helper
用于初始化, 自己并不保存这个变量. Helper
会保存该变量, 并用它来实现了NetworkStack
里的功能, 包括获取节点信息, 广播, 单播, 另外还包括获取区块链长度.
ConsensusHandler
中也保存了该类型变量, 但并未使用.
该变量也会被传给executor
, 将在后面提到.
secOn
secOn
一个类型为bool
的简单变量, 判断是否对消息进行加密(Sign&Verify), 由配置信息决定取值, 且在程序运行时不再改变.
valid
valid
类型为bool
. 用于标记是否当前状态是否为最新.
Whether we believe the state is up to date
用于实现LedgerManager
接口, 在LedgerManager
中已经提到了使用情况.
在engine
执行func ProcessTransactionMsg()
和Helper
执行func UpdateState()
时会检查valid
的取值.
secHelper
secHelper
用于实现SecurityUtils
接口, 类型为crypto.Peer
, 通过调用coordinator.GetSecHelper()
生成. 在消息传递前会进行加密, 接收后进行解密.
实际上, 在pbft
中并没有直接使用SecurityUtils
接口, 而是进行了封装, 但主要还是SecurityUtils
接口部分.
curBatch
& curBatchErrs
curBatch
& curBatchErrs
类型分别为[]*pb.Transaction
和[]*pb.TransactionResult
, 储存Tx和Tx的执行结果, 注释中提到, 这两个变量将被删去.
这两个变量将会在记录产生的Tx, 然后通过func CommitTxBatch()
提交.
persist.Helper
persist.Helper
Help
继承了persist.Helper
, 用于实现StatePersistor
接口. 暂不关注.
executor
executor
类型为consensus.Executor
, 用于实现Executor
接口. 通过executor.NewImpl(h, h, mhc)
初始化, 这里, h
是指Helper
自身, mhc
则是前面提到的peer.MessageHandlerCoordinator
. 这里mhc
是作为statetransfer.PartialStack
使用的, statetransfer
定义在core/peer/statetransfer/statetransfer.go
, 用来转换状态, 具体地, 在executor
里除了调用外func Start()
, func Halt()
只是在收到stateUpdateEvent
后调用func SyncToTarget()
来将区块链更新到目标状态.
前面已经提到过executor
的执行模式, 即把所有消息都发送给event.Manager
后经event.Manager
调用func ProcessEvent()
来执行. executor
也不具体规定执行动作, 而只是把Helper
里的方法(即接口ExecutionConsumer
和LegacyExecutor
)封装起来, 实际动作是在后面那两个接口里定义的.
func
前面围绕着各个变量已经提到了一部分函数, 除此之外还定义了一些函数用于实现接口.
Executor
Executor
针对这个接口, 主要函数是通过executor
实现的, 特别的, Executor
定义了func Start()
, func Halt()
, 而这两个函数对于Helper
来说是无意义的, 所以, 仅仅为了实现Executor
, Helper
中定义了两个空函数, 这样做只是为了满足编程语言规范. 另外, executor
实际实现了Executor
接口, 所以也定义了这两个函数, 在这两个函数中分别启动和停止event.Manager
和statetransfer
. 而executor
本身的启动, 是在func setConsenter()
里进行的. (由于Helper
的初始化后紧接着就执行了func setConsenter()
, 所以也可以理解成Helper
初始化时边启动了executor
.
ReadOnlyLedger
ReadOnlyLedger
这些部分是由Helper
和Ledger
等交互实现的, 在ReadOnlyLedger
里已经提到, 不再重复.
Last updated
Was this helpful?