Fabric2.0版本基礎環境搭建與測試參考:Fabric hyperledger 2.0 first-network 手動部署執行
升級步驟
- 重新打包鏈碼(升級鏈碼文件情形)
- 重新安裝鏈碼(升級鏈碼文件情形)
- 組織同意新的鏈碼定義:
(1) 升級鏈碼文件:需要更新鏈碼定義中的鏈碼版本和package ID
(2)僅僅更新背書策略:無需執行1,2
無論哪種情況,每次升級sequence都需要加1 - 提交鏈碼定義:當足夠數量的通道成員批准了新的鏈碼定義時,一個組織可以提交新定義以將鏈碼定義升級到通道。 作爲生命週期過程的一部分,沒有單獨的升級命令
提交鏈碼定義後,將使用升級的鏈碼二進制文件中的代碼啓動新的鏈碼容器。 如果您要求在鏈碼定義中執行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"]}'
返回結果:
這是升級後纔有的方法
測試完成