Hyperledger Fabric 2.x 动态更新智能合约

249 阅读1分钟

一、说明

在上一篇文章中分享了智能合约的安装与使用,如果业务有变更代码需要修改怎么办呢?本文分享如何对已安装的合约进行版本更新。

 

二、环境准备

区块链网络安装:《Hyperledger Fabric 2.x 环境搭建》 智能合约安装:《Hyperledger Fabric 2.x 自定义智能合约》

 

执行以下命令,可以看到已安装的合约信息:

peer lifecycle chaincode queryinstalled

file

 

三、重新打包代码

重新把最新的合约源代码打包:

peer lifecycle chaincode package mycc.tar.gz --path /opt/app/my-fabric-chaincode-java --lang java --label mycc

 

四、重新安装合约

再次分别为 peer0.org1peer0.org2 两个机构安装合约:

peer lifecycle chaincode install mycc.tar.gz

执行以下命令,重新查看已安装的合约信息:

peer lifecycle chaincode queryinstalled

可以发现新增加了一条 Label 名称相同 Package ID 不一样的记录:

file

 

五、重新审批

再次分别为 peer0.org1peer0.org2 两个机构审批合约:

peer lifecycle chaincode approveformyorg \
  -o localhost:7050 \
  --ordererTLSHostnameOverride orderer.example.com \
  --tls \
  --cafile ${MSP_PATH}/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
  --channelID mychannel \
  --name mycc \
  --version 1.1 \
  --package-id mycc:ecd2abc60ea098508aeefc135d8838787e9c1e3b8e411386a23ca56b7dfed758 \
  --sequence 2

package-id:需填入新安装的 Package ID sequence:因为是审批第二个合约,所以需要填 2 version:只是标识符,可改可不改

 

执行以下命令,检查节点审批状态:

peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name mycc --version 1.1 --sequence 2 --output json

返回:

{
	"approvals": {
		"Org1MSP": true,
		"Org2MSP": true
	}
}

 

六、重新提交

执行以下命令,向通道提交合约:

peer lifecycle chaincode commit \
  -o localhost:7050 \
  --ordererTLSHostnameOverride orderer.example.com \
  --tls \
  --cafile ${MSP_PATH}/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
  --channelID mychannel \
  --name mycc \
  --peerAddresses localhost:7051 \
  --tlsRootCertFiles ${MSP_PATH}/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt \
  --peerAddresses localhost:9051 \
  --tlsRootCertFiles ${MSP_PATH}/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt \
  --version 1.1 \
  --sequence 2

需要把 sequenceversion 改为审批时的值

 

 

七、查看已提交合约

执行一下命令:

peer lifecycle chaincode querycommitted --channelID mychannel --name mycc --output json

可以看到现在通道 mychannel 名字为 mycc 的合约已经更新为 1.1 版本:

{
	"sequence": 2,
	"version": "1.1",
	"endorsement_plugin": "escc",
	"validation_plugin": "vscc",
	"validation_parameter": "EiAvQ2hhbm5lbC9BcHBsaWNhdGlvbi9FbmRvcnNlbWVudA==",
	"collections": {},
	"approvals": {
		"Org1MSP": true,
		"Org2MSP": true
	}
}

 

扫码关注有惊喜!

file