Hyperledger Fabric 2.0 chaincode lifecycle

Chaincode Lifecycle


環境準備參見 Fabric 2.0 debug 環境準備 腳本部分

2.0 新增 _lifecycle 系統 chaincode 管理 Chaincode 生命週期。與 1.x 不同 install 之後不會直接 instantiate ,而是需要通過 approvecommit 兩個階段

1. install


chaincode 安裝到指定的 peers,只進行存盤、構建。

peer lifecycle chaincode install --peerAddresses 127.0.0.1:7051 ./chaincode.tgz

會觸發 Cache.HandleChaincodeInstalled 填充 Cache.localChaincodes[PackageID]

2. approveformyorg


爲我所屬的 Org 批准 chaincode

bin/peer lifecycle chaincode approveformyorg --channelID wagaga --name mycc --version 1 --init-required --package-id "golang-external:14893d10c6c01ce892588e83ecc0fb911a0de131d51a4d23547056775840f495" --sequence 1 --waitForEvent
2020-03-13 16:17:03.243 CST [cli.lifecycle.chaincode] setOrdererClient -> INFO 001 Retrieved channel (wagaga) orderer endpoint: 127.0.0.1:7050
2020-03-13 16:17:05.361 CST [chaincodeCmd] ClientWait -> INFO 002 txid [23763a3b0a14ab0baa081bcb6fcdcec6029a1b31e00381871bec9a85078a91b3] committed with status (VALID) at
PutPrivateData("_implicit_org_<ORG>",  "namespaces/metadata/<CHAINCODE_NAME>#<SEQUENCE>, cd.Paramters())

PutPrivateData("_implicit_org_<ORG>",  "chaincode-sources/metadata/<CHAINCODE_NAME>#<SEQUENCE>, PackageID)

完成共識後,觸發 Cache.HandleStateUpdates ,填充 Cache.definedChaincodes[channelID][CHAINCODE_NAME]

Block Commit 時觸發 StateCommitDone 只是通知註冊了 EventBroker 的事件.

3. commit


bin/peer lifecycle chaincode commit -o 127.0.0.1:7050 --channelID wagaga -n mycc --version 1 --init-required --sequence 1
2020-03-13 16:34:43.456 CST [chaincodeCmd] ClientWait -> INFO 001 txid [86c6813ce18f912c591519313711780c635670f8aace7cf5750e7656ba845fae] committed with status (VALID) at
PutState("namespaces/metadata/<CHAINCODE_NAME>", cd)

4. init


2.0 爲 chaincode 增加了 init-required 選項,在 approvecommit 階段設置,設置了 init-requiredchaincode 必須先執行 Init 才能執行其他的操作。

錯誤示例如下:

bin/peer chaincode query -C wagaga -n mycc -c '{"Args":["query","a"]}'
Error: endorsement failure during query. response: status:500 message:"error in simulation: failed to execute transaction 23cbb805a07e9fe23da930f017039a9edd49be150ea3e6c9744be124cf65c7d4: invalid invocation: chaincode 'mycc' has not been initialized for this version, must call as init first"

初始化 chaincode :

bin/peer chaincode invoke -o 127.0.0.1:7050 --peerAddresses 127.0.0.1:7051 -C wagaga -n mycc -I -c '{"Args":["Init", "a","1000","b", "1000"]}'

bin/peer chaincode query -C wagaga -n mycc -c '{"Args":["query","a"]}'

Cache 詳解

在 2.0 版的 lifecycle chaincode 中,lifecycle.Cache 是一個很重要到的組件。
ChaincodeInfoListInstalledChaincodesGetInstalledChaincode 僅獲取信息這裏忽略不管,RegisterListener 只有使用的是 couch versiondb 時纔會註冊,同樣這裏也不關注。


1. InitializeLocalChaincodes


peer node start 過程中調用,詳見代碼 node/start.go
install 到此 peer 加載到 localChaincodes

Startlifecycle.CacheChaincodeStoreChaincodeCustodianRuntimeLauncherContainerRouterInitializeLocalChaincodesListInstalledChaincodes([]InstalledChaincode, error)Load(installPkg, error)handleChaincodeInstalledWhileLockedNotifyInstalledloop[ installed chaincodes ]errorWorkLauncherrorStoperroralt[ chore.runnable ][ if chore.stoppable ]Builderroropt[ chaincode not built ]loop[ for choreQueue ]Startlifecycle.CacheChaincodeStoreChaincodeCustodianRuntimeLauncherContainerRouter

2. Initialize


初始化本地賬本時,

Initialize 在創建本地 kvLedger 時調用,加載由 ExternalFunctions.CommitChaincodeDefinition 寫入的 ChaincodeDefinition metadata 狀態數據

kvLedgerlifecycle.CacheResourcesChaincodeCustodianInitializeupdateChaincodeDefinitionIfDefined(bool, ChaincodeDefinition, error)NotifyInstalledAndRunnableopt[ is local chaincode ]loop[ for dirty chaincodes ]errorerrorkvLedgerlifecycle.CacheResourcesChaincodeCustodian

3. InitializeMetadata


InitializeMetadataPeer 初始化過程中調用,將當前 channel 上的 chaincodes 提交到 MetadataManager

Peerlifecycle.CacheMetadataManagerInitializeMetadataretrieveChaincodesMetadataSetWhileLockedgetLifecycleSCCChaincodeInfo(LocalChaincodeInfo, error)(MetadataSet, error)InitializeMetadataPeerlifecycle.CacheMetadataManager

4. HandleChaincodeInstalled


HandleChaincodeInstalled 安裝鏈碼時調用

InvocationExternalFunctionslifecycle.CacheChaincodeCustodianEventBrokerMetadataManagerGossip NodeInstallChaincodeHandleChaincodeInstalledhandleChaincodeInstalledWhileLockedNotifyInstalledProcessInstallEventhandleMetadataUpdateshandleMetadataUpdatesForChannelretrieveChaincodesMetadataSetWhileLockedUpdateMetadatafireListenersForChannelUpdateChaincodesloop[ for localChaincode.References ](InstalledChaincode, error)InvocationExternalFunctionslifecycle.CacheChaincodeCustodianEventBrokerMetadataManagerGossip Node

5. State


HandleStateUpdatespeer 收到塊並驗證通過還未寫入狀態數據庫時調用,StateCommitDoneCommit Block 時調用

LockBasedTxMgrlifecycle.CacheEventBrokerInterestedInNamespaces_lifecycleHandleStateUpdatesupdate not initerrorerrorStateCommitDoneApproveOrDefineCommittedLockBasedTxMgrlifecycle.CacheEventBroker
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章