Section2.3.4 blockchain
ReadOnlyLedger
ReadOnlyLedger
ReadOnlyLedger is used for interrogating the blockchain.
在fabric中, 储存交易的最高层的数据结构称作Ledger
, blockchain
是定义在Ledger
之内的, 同时也是Ledger
私有的, 不暴露给外部使用. 因此, 对于blockchain
的操作, 都是经过Ledger
.
Usage
和前面介绍的接口一样, ReadOnlyLedger
也只作为Stack
的一部分出现.
Implement
在Helper
中, 每次对ReadOnlyLedger
内几个函数的实现, 都是先通过调用func ledger.GetLedger()
获得ledger
, 然后执行对应的方法.
查看func GetLedger()
其实可以发现, 它也并没有每次都生成一个Ledger
, 而是只生成一次, 以后的都会返回同一个.
Ledger
内部自己是通过调用blockchain
的方法来实现的.
blockchain
内则通过操作数据库来实现.
Summary
在fabric中, blockchain
被封装在了Ledger
内, ReadOnlyLedger
定义了对Blockchain的读操作, Helper
使用Ledger
对其进行了实现, 而Ledger
内部则是调用了blockchain
的对应方法, blockchain
则是通过使用db
进行了实现.
LegacyExecutor
LegacyExecutor
LegacyExecutor is used to invoke transactions, potentially modifying the backing ledger
和ReadOnlyLedger
类似, 只不过LegacyExecutor
内定义的是写操作.
Usage
作为
Stack
的一部分. 此处和ReadOnlyLedger
类似.作为
consensus/executor.PartialStack
接口定义的一部分.PartialStack
只在executor
内部被作为type coordinatorImpl struct
的成员变量来使用.executor
是作为Helper
的一部分来使用, 用来处理实际Tx的执行动作. 前面提到obcBatch
使用了event.Manager
模块进行消息接收处理, 其实executor
也注册了一个event.Manager
用来接收执行后的消息. 就是说, 当obcEvent
接收到来自pbft
的消息后(比如executedEvent
),obcEvent
会执行相应动作(比如Commit
), 这个动作是在Helper
里定义的, 但是是通过executor
来实现的,executor
会把对应时间发给自己的EventManager
,EventManager
再将消息传回executor
,executor
收到消息后进行执行. 执行动作就会用到我们刚才提到的接口.
Implement
虽然这里也有两类使用, 但是Usage#2中, PartialStack
接口也是用Helper
来实现的, 所以也就只有在Helper
处的一处实现.
在Helper
中的实现, 一部分也是直接通过与Ledger
交互来实现的. 如:
而对于func ExecTxs()
, 则是通过chaincode
.
Summary
LegacyExecutor
用来对blockchain
进行写操作, 实际实现只有一处, 在Helper
中, 另一处在executor
里虽作为一部分定义了一个新的接口, 但该接口仍是通过Helper
来初始化的. 在Helper
中, 除func ExecTxs()
是和chaincode
交互实现的, 其他仍是和Ledger
交互. executor
的存在, 主要是为了方便收集来自obcBatch
关于对blockchain
写操作的消息, 集中调用Helper
进行执行.
Executor
Executor
Executor is intended to eventually supplant the old Executor interface. The problem with invoking the calls directly above, is that they must be coordinated with state transfer, to eliminate possible races and ledger corruption.
Executor
是用来执行tx的, 其中也会包含执行后的动作(调用func Commited()
等).
Usage
Executor
也被定义为Stack
的一部分, 在Helper
内进行了实现, 实际上是Helper
用executor
来实现的, Helper
只是进行了封装, Helper
这些函数, 在func obcBatch.ProcessEvent()
中被调用(并非全部使用).
Implement
executor
对这些方法只是简单地将数据封装成一个新的时间, 交给event.Manager
, 然后在func ProcessEvent()
中集中处理.
注意到rawExecutor
, 它其实就是实现了LegacyExecutor
的接口(实际上是PartialStack
), 后面又调用了consumer
, 即Event中介绍的ExecutionConsumer
, 从这里就可以看出, Executor
其实就是把两个接口内的方法又进行了封装.
Summary
Executor
作为Stack
的一部分以Helper
实现, 本质上是由executor.coordinatorImpl
来实现的. 在内容上, 就是将LegacyExecutor
和ExecutionConsumer
接口封装到一块.
LedgerManager
LedgerManager
LedgerManager is used to manipulate the state of the ledger
LedgerManager
内只定义了两个方法, 而且还都没有参数, 就是设置当前的Ledger
是否是最新的.
Usage
只作为Stack
的一部分使用. 两个函数被pbft
进一步封装成了innerStack
的函数, 会在需要临时禁用Ledger
的时候(func stateTransfer()
等)处或检测到状态过期的时候调用func InvalidateState()
, 会在收到stateUpdatedEvent
消息后调用func ValidateState()
重新启用Ledger
.
Implement
实现很简单, 在Helper
中保存了一个bool
变量, 分别置为false, true.
Summary
LedgerManager
也是Stack
的一部分, 用于设置Ledger
的状态是否最新/合法, 会在状态转换前或检测到失效后置false, 收到stateUpdatedEvent
后重新置true.
Last updated
Was this helpful?