关于区块链:Hyperledger-Fabric-2x-动态更新智能合约

2次阅读

共计 2113 个字符,预计需要花费 6 分钟才能阅读完成。

一、阐明

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

 

二、环境筹备

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

 

执行以下命令,能够看到已装置的合约信息:

peer lifecycle chaincode queryinstalled

 

三、从新打包代码

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

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 不一样的记录:

 

五、从新审批

再次别离为 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
    }
}

 

扫码关注有惊喜!

正文完
 0