一 打包智能合约
①进入fabric-samples文件夹下的chaincode/fabcar/go目录下执行
GO111MODULE=on go mod vendor下载依赖(文件夹下已经有go.mod,不需要使用go mod init生成该module文件)
②进入到test-network文件下使用以下命令将二进制文件添加到cli路径:
export PATH=${PWD}/../bin:$PATH
③还需要设置FABRIC_CFG_PATH路径指向fabric-samples中的core.yaml文件,命令如下:
export FABRIC_CFG_PATH=$PWD/../config/
④检查peer命令是否可以使用:
peer version
⑤打包智能合约
#../chaincode/fabcar/go/路径保证准确,fabcar_1.0自定义命名
peer lifecycle chaincode package fabcar.tar.gz --path ../chaincode/fabcar/go/ --lang golang --label fabcar_1.0
二 安装、批准、打包好的链码
①设置允许org1操作peer cli的环境变量
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 install fabcar.tar.gz
②当前组织批准同意链码定义
export CC_PACKAGE_ID=fabcar_1.0:3cfcf67978d6b3f7c5e0375660c995b21db19c4330946079afc3925ad7306881
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
此时检查合约定义是否满足策略
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
Org2MSP这里显示未同意合约定义
那么需要进入Org2中的环境配置中重复合约安装并同意合约定义
③切换到组织二
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
#安装智能合约
peer lifecycle chaincode install fabcar.tar.gz
④当前组织同意批准同意链码定义
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
此时检查合约定义是否满足策略
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
三 部署调用智能合约
提交智能合约到通道
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
①检查合约是否提交到通道
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
②设置ca环境变量
export org1_CertFiles=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export org2_CertFiles=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
③安装智能合约
peer lifecycle chaincode install fabcar.tar.gz
④初始化智能合约
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 $org1_CertFiles --peerAddresses localhost:9051 --tlsRootCertFiles $org2_CertFiles -c '{"function":"InitLedger","Args":[]}'
⑤调用智能合约,查询所有汽车
peer chaincode query -C mychannel -n fabcar -c '{"Args":["QueryAllCars"]}'
⑥调用智能合约,查询单部汽车
peer chaincode query -C mychannel -n fabcar -c '{"Args":["QueryCar","CAR0"]}'
⑦新增一辆汽车
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 $org1_CertFiles --peerAddresses localhost:9051 --tlsRootCertFiles $org2_CertFiles -c '{"function":"CreateCar","Args":["CAR10","GEELY","Borui","Blue","LuoJiaCheng"]}'
⑧查询刚新增的车辆
peer chaincode query -C mychannel -n fabcar -c '{"Args":["QueryCar","CAR10"]}'
⑨更改刚新增车辆CAR10的车主为"LiNu"
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 $org1_CertFiles --peerAddresses localhost:9051 --tlsRootCertFiles $org2_CertFiles -c '{"function":"ChangeCarOwner","Args":["CAR10","LiNu"]}'
⑩再次查看CAR10信息看是否修改车主成功
peer chaincode query -C mychannel -n fabcar -c '{"Args":["QueryCar","CAR10"]}'