Hyperledger Fabric 2.0 Endorser

文章內容基於 Fabric v2.0.1 整理

Fabric 中所有與鏈相關的操作都通過 Chaincode 來實現,所有 Chaincode 的執行都是向 EndorserServer 提交 SignedProposal 來觸發。

1. System Chaincode


Fabric 對於一些特殊功能(如 Chaincode 本身的管理,Channel 相關操作等)提供內置的 System Chaincode 來實現。有如下 System Chaincode :

  • cscc :the configuration handler for the peer,對 Channel 的管理
  • qscc :the ledger query functions,一些通用的查詢功能
  • lscc :chaincode lifecycle and policies around it,1.x 版 Chaincode 管理
  • _lifecycle :2.0 新增用於替代 lscc ,實現更完善的 Chaincode lifecycle 管理(根據命令等的組織方式,個人臆測今後可能還會用於其他內容的 lifecycle 管理

另外還有兩個 System Chaincode 通過插件的方式提供:

  • escc :endorsement system chaincode,部署 Chaincode 時指定,對 Chaincode 執行結果進行背書
  • vscc :Validate system chaincode,validates the given envelope corresponding to a transaction with an endorsement policy as given in its serialized form.

2. Process Proposal


Proposal 處理流程主要分爲三部分:

  • Pre Process : 校驗 Proposal 有效性,簽名是否匹配等,對於非 System Chaincode 此階段進行 ACL 檢查
  • Simulate :此階段會以當前 World State 爲基礎,使用提供的輸入執行 Chaincode ,並生成對 World State 操作的讀寫集(RWSet
  • Endorse :當前 Peer 對此次 Proposal 正確的執行結果進行簽名背書,有些 System Chaincode 不進行背書如 _lifecycle InstallChaincode

大體流程如下:

Created with Raphaël 2.2.0StartRecv SignedProposalUnpackProposalUnpack ErrorFailure ResponseEndPre ProcessPre Process ErrorSimulateProposalSimulate ErrorSimulate Status ErrorChannel is EmptySuccess ResponseEndorseEndorse erroryesnoyesnoyesnoyesnoyesnoyesno

3. Simulate Proposal


Simulate Proposal 過程負責 Chaincode 的模擬執行,所謂模擬執行指的是以當前的 World State 爲基礎來,傳遞輸入參數執行 Chaincode 相關邏輯,ChaincodeWorld State 的插入、刪除更新操作以及刪除更新操作引用哪些值(基於當前狀態值修改)由模擬器收集並記錄,最終的結果即爲讀寫集RWSet)。需要注意的是私有數據的讀寫集不直接返回到 Response 中,而是返回私有數據對應的 Hash 值,私有數據讀寫集本身通過 Gossip 網絡進行分發。

Chaincode 調用過程中可能需要 build And / Or launch Chaincode ,自 2.0 起 Chaincode (1.x 的 instantiate 過的,2.0 approve & commit 的)會預加載並啓動,所以大部分情況下 Chaincode 都是出於 Ready 狀態,這裏只關注 Chaincode 執行部分。

Peer 與 Chaincode 之間的交互由一對 Handler 進行處理,Application Chaincode 通過 gRPC 連接、System Chaincode 通過 兩個 golang channel 模擬連接。2.0 之前只能由 Chaincode 主動連接 Peer,2.0 新增了 Chaincode as Service 由 Peer 主動連接 Chaincode,交互過程沒有太多區別。兩個成對的 Handler 代碼見:Peer 側 HandlerChaincode 側 Handler

EndorserTXSimulatorGossipServiceEndorserSupportDecoratorChaincodeSupportHandlerRegistrychaincode.HandlergRPC / Channelshim.HandlerSend MessageRecv MessagehandleMessagehandleMessageRecv MessagehandleMessagehandleMessageSend Messageloop[ handle message ]callChaincodeExecuteDecorateChaincodeInputloop[ for decorators ]ExecuteInvokeCheckInvocation(ccid, MessageType, error)LaunchHandler(Handler)(Handler, error)executeExecuteserialSendAsyncSend MessageRecv Messageloop[ handle message ]Wait(ChaincodeMessage, error)(ChaincodeMessage, error)(ChaincodeMessage, error)(Response, ChaincodeEvent, error)(Response, ChaincodeEvent, error)ExecuteLegacyIniterroropt[ is Legacy lscc deploy/upgrade ](Response, ChaincodeEvent, error)GetTxSimulationResults(TxSimulationResults, error)DistributePrivateDataopt[ Has Private Simulation ]opt[ no error and TXSimulator not nil ]EndorserTXSimulatorGossipServiceEndorserSupportDecoratorChaincodeSupportHandlerRegistrychaincode.HandlergRPC / Channelshim.Handler
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章