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_",  "namespaces/metadata/#, 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/", cd)

4. init


2.0 为 chaincode 增加了 init-required 选项,在 approvecommit 阶段设置,设置了 init-requiredchaincode 必须先执行 Init 才能执行其他的操作。详见 chaincode_support.go CheckInvocation

错误示例如下:

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

Start lifecycle.Cache ChaincodeStore ChaincodeCustodian RuntimeLauncher ContainerRouter InitializeLocalChaincodes ListInstalledChaincodes ([]InstalledChaincode, error) Load (installPkg, error) handleChaincodeInstalledWhileLocked NotifyInstalled loop [ installed chaincodes ] error Work Launch error Stop error alt [ chore.runnable ] [ if chore.stoppable ] Build error opt [ chaincode not built ] loop [ for choreQueue ] Start lifecycle.Cache ChaincodeStore ChaincodeCustodian RuntimeLauncher ContainerRouter

2. Initialize


初始化本地账本时,

Initialize 在创建本地 kvLedger 时调用,加载由 ExternalFunctions.CommitChaincodeDefinition 写入的 ChaincodeDefinition metadata 状态数据

kvLedger lifecycle.Cache Resources ChaincodeCustodian Initialize update ChaincodeDefinitionIfDefined (bool, ChaincodeDefinition, error) NotifyInstalledAndRunnable opt [ is local chaincode ] loop [ for dirty chaincodes ] error error kvLedger lifecycle.Cache Resources ChaincodeCustodian

3. InitializeMetadata


InitializeMetadataPeer 初始化过程中调用,将当前 channel 上的 chaincodes 提交到 MetadataManager

Peer lifecycle.Cache MetadataManager InitializeMetadata retrieveChaincodesMetadataSetWhileLocked getLifecycleSCCChaincodeInfo (LocalChaincodeInfo, error) (MetadataSet, error) InitializeMetadata Peer lifecycle.Cache MetadataManager

4. HandleChaincodeInstalled


HandleChaincodeInstalled 安装链码时调用

Invocation ExternalFunctions lifecycle.Cache ChaincodeCustodian EventBroker MetadataManager Gossip Node InstallChaincode HandleChaincodeInstalled handleChaincodeInstalledWhileLocked NotifyInstalled ProcessInstallEvent handleMetadataUpdates handleMetadataUpdatesForChannel retrieveChaincodesMetadataSetWhileLocked UpdateMetadata fireListenersForChannel UpdateChaincodes loop [ for localChaincode.References ] (InstalledChaincode, error) Invocation ExternalFunctions lifecycle.Cache ChaincodeCustodian EventBroker MetadataManager Gossip Node

5. State


HandleStateUpdatespeer 收到块并验证通过还未写入状态数据库时调用,StateCommitDoneCommit Block 时调用

LockBasedTxMgr lifecycle.Cache EventBroker InterestedInNamespaces _lifecycle HandleStateUpdates update not init error error StateCommitDone ApproveOrDefineCommitted LockBasedTxMgr lifecycle.Cache EventBroker

你可能感兴趣的:(Fabric)