Fabric2.0版本基础环境搭建与测试参考:Fabric hyperledger 2.0 first-network 手动部署执行
提交链码定义后,将使用升级的链码二进制文件中的代码启动新的链码容器。 如果您要求在链码定义中执行Init函数,则需要在成功提交新定义后再次调用Init函数来初始化升级的链码。 如果在不更改链码版本的情况下更新了链码定义(如仅修改背书策略的情况),则链码容器将保持不变,并且无需调用Init函数。
Fabric链码生命周期使用链码定义中的sequence来跟踪升级。每次升级时所有通道成员都需要将sequence加一,并批准新的定义以升级链码。 版本参数用于跟踪链码二进制文件,仅在升级链码二进制文件时才需要更改。
为了方便测试,我们修改fabric-samples/chaincode/abstore/go/abstore.go,添加一个新的方法newMethod,调用时返回"这是升级后才有的方法"。
func (t *ABstore) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
fmt.Println("ABstore Invoke")
function, args := stub.GetFunctionAndParameters()
if function == "invoke" {
// Make payment of X units from A to B
return t.invoke(stub, args)
} else if function == "delete" {
// Deletes an entity from its state
return t.delete(stub, args)
} else if function == "query" {
// the old "Query" is now implemtned in invoke
return t.query(stub, args)
} else if function == "newMethod"{
return t.newMethod(stub, args)
}
return shim.Error("Invalid invoke function name. Expecting \"invoke\" \"delete\" \"query\"")
}
func (t *ABstore) newMethod(stub shim.ChaincodeStubInterface, args []string) pb.Response {
return shim.Success([]byte("这是升级后才有的方法"))
}
重新打包链码(此处我们修改生成的包名为mycc_new.tar.gz):
peer lifecycle chaincode package mycc_new.tar.gz --path github.com/hyperledger/fabric-samples/chaincode/abstore/go/ --lang golang --label mycc_1
节点peer0.org1.example.com安装新的链码:
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
CORE_PEER_LOCALMSPID="Org1MSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
peer lifecycle chaincode install mycc_new.tar.gz
节点peer0.org1.example.com查询已安装链码
peer lifecycle chaincode queryinstalled
返回结果:
Installed chaincodes on peer:
Package ID: mycc_1:c62d60b38372d28e3e08032d10712d769a533da3f705421f52ece3f7dc788aa6, Label: mycc_1
Package ID: mycc_1:85d70866ac889af5a83f4f1b022b56165039a9db06debf1a61bc780b2c02b1e3, Label: mycc_1
其中mycc_1:85d70866ac889af5a83f4f1b022b56165039a9db06debf1a61bc780b2c02b1e3为新的packageID
节点peer0.org2.example.com安装链码:
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org2.example.com:9051
CORE_PEER_LOCALMSPID="Org2MSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
peer lifecycle chaincode install mycc_new.tar.gz
查询已安装链码:
peer lifecycle chaincode queryinstalled
重新同意链码定义(这里sequence为2,version我们定义为2.0):
export CHANNEL_NAME=mychannel #通道名
CC_PACKAGE_ID=mycc_1:85d70866ac889af5a83f4f1b022b56165039a9db06debf1a61bc780b2c02b1e3 #传入新得到package ID
#当前身份:peer0.org2.example.com
peer lifecycle chaincode approveformyorg --channelID $CHANNEL_NAME --name mycc --version 2.0 --package-id $CC_PACKAGE_ID --sequence 2 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
#查询当前链码定义已同意的组织
peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name mycc --version 2.0 --sequence 2 --output json
切换节点为peer0.org1.example.com:
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
CORE_PEER_LOCALMSPID="Org1MSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
peer lifecycle chaincode approveformyorg --channelID $CHANNEL_NAME --name mycc --version 2.0 --package-id $CC_PACKAGE_ID --sequence 2 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
#查询当前链码定义已同意的组织
peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name mycc --version 2.0 --sequence 2 --output json
重新提交链码定义(sequence为2,version为2.0):
peer lifecycle chaincode commit -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --channelID mychannel --name mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt --version 2.0 --sequence 2
由于新的链码定义没有通过–init-required指定必须执行Init函数,此处无需init
#查询
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
这里我们尝试调用智能合约里面新增的方法:
peer chaincode query -C mychannel -n mycc -c '{"Args":["newMethod"]}'
返回结果:
这是升级后才有的方法
测试完成