基于Fabric2.0.1安装部署智能合约
在Fabric1.4的版本中我们对智能合约的操作是基于 peer chaincode 命令。可以参考 https://hyperledger-fabric.readthedocs.io/en/release-1.4/commands/peerchaincode.html ,但是在2.0 在可以继续使用 peer chaincode 基础上增加了 Fabric chaincode lifecycle 这个命令 翻译过来是 fabric智能合于生命周期。下面介绍一下在2.0版本中如何使用 这个命令来安装部署智能合约 可以参考官网: https://hyperledger-fabric.readthedocs.io/en/latest/deploy_chaincode.html#package-the-smart-contract
1、打包智能合约
我们需要打包智能合约,然后才能安装到我们的peer节点。如果您想安装一个用Go、JavaScript或Typescript编写的智能合约,打包方式是不同的 我们以Go为例 ,其他形式请参考官方文档。
export CC_SRC_PATH = /opt/gopath/src/github.com/hyperledger/fabric-samples/chaincode/fabcar/go
peer lifecycle chaincode package fabcar.tar.gz --path ${CC_SRC_PATH} --lang golang --label fabcar_1
# 执行成功之后可以看到当目录下生成一个 fabcar.tar.gz 文件
2、安装智能合约
我们需要将智能合约分别安装到Org1 和 Org 2 因为接下来我们在实例化的时候设置的背书策略是这2个
#1、Org1MSP 切换环境
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051
#2、在Org1MSP 安装智能合约
peer lifecycle chaincode install fabcar.tar.gz
#3、切换到 Org2MSP
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
export CORE_PEER_ADDRESS=localhost:9051
#4、安装智能合约
peer lifecycle chaincode install fabcar.tar.gz
3、配置智能合约
智能合约安装好之后需要对他进行配置,包括 名称 、版本、背书策略
#1、首先查询出我们刚才安装好的 packageid
peer lifecycle chaincode queryinstalled
#2、packageid (由链码标签和二进制编码组成):
Installed chaincodes on peer:
Package ID: fabcar_1:5a95aa7a65c07699afa0c4452a50f92e7c3289589e3fea7fd6856706f3de6845, Label: fabcar_1
#3、将Package ID 设置到环境变量中,然后安装
export CC_PACKAGE_ID=fabcar_1:5a95aa7a65c07699afa0c4452a50f92e7c3289589e3fea7fd6856706f3de6845
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 1.0 --package-id $CC_PACKAGE_ID --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
#4、 Org1MSP 切换到环境 配置
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 1.0 --package-id $CC_PACKAGE_ID --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
4、将链码实例化到channel
#1、检查 所有组织是否都配置好了
peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name fabcar --version 1.0 --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --output json
#2、提交
peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 1.0 --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
#3、查询安装结果
peer lifecycle chaincode querycommitted --channelID mychannel --name fabcar --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
5、调用智能合约
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n fabcar --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"initLedger","Args":[]}'
6、查询智能合约
peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'