fabric2.0链码操作
1、打包链码
链码在安装前需要打包为tar文件。我们可以使用peer命令,node sdk,或者第三方工具。
第三方的打包工具需要满足以下要求:
链码以tar.gz结尾;
tar文件需要包含2个文件(不是目录),元文件Chaincode-Package-Metadata.json和chaincode文件。
Chaincode-Package-Metadata.json文件长成下面这样。
{"Path":"fabric-samples/chaincode/fabcar/go","Type":"golang","Label":"fabcarv1"}
peer命令打包示例:
首先我们处理依赖问题:
cd fabric-sample/chaincode/fabcar/go
GO111MODULE=on go mod vendor
# peer0.org2
docker exec \
-e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp \
-e CORE_PEER_ADDRESS=peer0.org2.example.com:7051 \
-e CORE_PEER_LOCALMSPID="Org2MSP" \
-e 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 \
cli peer lifecycle chaincode install fabcar2.tar.gz
# peer0.org2
docker exec \
-e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp \
-e CORE_PEER_ADDRESS=peer0.org2.example.com:7051 \
-e CORE_PEER_LOCALMSPID="Org2MSP" \
-e 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 \
cli peer lifecycle chaincode queryinstalled
如果我们现在看下peer节点的日志,可以看到出了新块#3
两个机构都已经批准了链码,现在可以提交了。(这里暂时没想明白为什么为false)
4、向通道提交链码
链码提交可以在一个peer节点上完成:
docker exec cli peer lifecycle chaincode commit -o orderer.example.com:7050 \
--tls \
--cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
--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:7051 \
--tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt \
--channelID mychannel --name fabcar2 --version 1 --sequence 1 --init-required
可以看到出了新块#5:
注意我们在commit命令中已经包含了init所需的参数。
同样,我们可以使用querycommited命令来查看链码的提交状态:
docker exec cli peer lifecycle chaincode querycommitted --channelID mychannel --name fabcar2
5、调用链码的Init方法
fabcar链码的Init方法需要首先调用。
docker exec cli peer chaincode invoke -o orderer.example.com:7050 \
--tls \
--cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
--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:7051 \
--tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt \
-C mychannel -n fabcar2 \
--isInit \
-c '{"function":"initLedger","Args":[]}'
6、调用链码方法并从另一个peer节点查询
和之前一样,我们在peer0.org1上调用链码的changeCarOwner()
方法,在peer0.org2上进行查询:
# peer0.org1
docker exec cli peer chaincode invoke \
> -o orderer.example.com:7050 \
> --tls \
> --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem \
> --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:7051 \
> --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt \
> -C mychannel -n fabcar2 -c '{"function":"changeCarOwner","Args":["CAR9","zmcheng"]}'
# peer0.org2
docker exec \
-e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp \
-e CORE_PEER_ADDRESS=peer0.org2.example.com:7051 \
-e CORE_PEER_LOCALMSPID="Org2MSP" \
-e 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 \
cli peer chaincode query -C mychannel -n fabcar2 -c '{"Args":["queryAllCars"]}'