Fabric智能合约编写,部署,调用
Fabric智能合约编写,部署,调用
开启网络
cd /root/go/src/github.com/hyperledger/fabric-samples/test-network
./network.sh down
./network.sh up createChannel
打包智能合约
- 进入fabric-samples文件夹下的chaincode/fabcar/go目录下
- 执行
GO111MODULE=on go mod vendor
下载依赖(文件夹下已经有go.mod,不需要使用go mod init生成该module文件) - 进入到
/root/go/src/github.com/hyperledger/fabric-samples/test-network
文件下 - 使用以下命令将二进制文件添加到cli路径:
export PATH=${PWD}/../bin:$PATH
- 还需要设置FABRIC_CFG_PATH路径指向fabric-samples中的core.yaml文件,命令如下:
export FABRIC_CFG_PATH=$PWD/../config/
- 检查peer命令是否可以使用:
peer version
- 打包智能合约:
peer lifecycle chaincode package fabcar.tar.gz --path ../chaincode/fabcar/go/ --lang golang --label fabcar_1.0
(../chaincode/fabcar/go/路径保证准确,fabcar_1.0自定义命名)
安装,批准,打包好的链码
切换到组织1
- 设置允许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
输出结果:
2021-10-12 15:48:55.040 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nKfabcar_1.0:dfb655dc2702090da647bcbafc8e0a06f6f9d15e62ec97bb8653d60671e84bc9\022\nfabcar_1.0" >
2021-10-12 15:48:55.162 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: fabcar_1.0:dfb655dc2702090da647bcbafc8e0a06f6f9d15e62ec97bb8653d60671e84bc9
- 批准同意合约定义
- 查询已经安装的链码
peer lifecycle chaincode queryinstalled
输出:
installed
Installed chaincodes on peer:
Package ID: fabcar_1.0:dfb655dc2702090da647bcbafc8e0a06f6f9d15e62ec97bb8653d60671e84bc9, Label: fabcar_1.0
- 当前组织批准同意链码定义:
export CC_PACKAGE_ID=fabcar_1.0:dfb655dc2702090da647bcbafc8e0a06f6f9d15e62ec97bb8653d60671e84bc9
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
输出:
2021-10-12 15:57:41.577 CST [chaincodeCmd] ClientWait -> INFO 001 txid [dadbfad51d025a0710de313e34cb27d6c0651ebfc4d65fb4e36f8cc93b5f0a09] committed with status (VALID) at localhost:7051
- 此时检查合约定义是否满足策略
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
输出:
{
"approvals": {
"Org1MSP": true,
"Org2MSP": false
}
}
Org2MSP这里显示未同意合约定义
那么需要进入Org2中的环境配置中重复合约安装并同意合约定义
切换到组织2
- 设置允许org1操作peer cli的环境变量:
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
输出结果:
2021-10-12 16:03:40.266 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nKfabcar_1.0:dfb655dc2702090da647bcbafc8e0a06f6f9d15e62ec97bb8653d60671e84bc9\022\nfabcar_1.0" >
2021-10-12 16:03:40.266 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: fabcar_1.0:dfb655dc2702090da647bcbafc8e0a06f6f9d15e62ec97bb8653d60671e84bc9
- 批准同意合约定义
- 查询已经安装的链码
peer lifecycle chaincode queryinstalled
输出:
installed
Installed chaincodes on peer:
Package ID: fabcar_1.0:dfb655dc2702090da647bcbafc8e0a06f6f9d15e62ec97bb8653d60671e84bc9, Label: fabcar_1.0
- 当前组织批准同意链码定义:
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
输出:
2021-10-12 16:06:34.017 CST [chaincodeCmd] ClientWait -> INFO 001 txid [6e3a141a65e406bf3ea6b7bbdab8b0fceb7113c51b84a2b551eb59fa6395828e] committed with status (VALID) at localhost:9051
- 此时检查合约定义是否满足策略
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
输出:
{
"approvals": {
"Org1MSP": true,
"Org2MSP": true
}
}
过半数组织同意了智能合约定义
那么可以提交智能合约
提交智能合约到通道
- 提交
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
输出:
2021-10-12 16:09:51.514 CST [chaincodeCmd] ClientWait -> INFO 001 txid [48d65fdb7e4afc6cbe9be27e3625f22aaec47883b96ef2c1f32ab8cd18ad4e4c] committed with status (VALID) at localhost:9051
2021-10-12 16:09:51.582 CST [chaincodeCmd] ClientWait -> INFO 002 txid [48d65fdb7e4afc6cbe9be27e3625f22aaec47883b96ef2c1f32ab8cd18ad4e4c] committed with status (VALID) at localhost:7051
- 检查合约是否成功提交到通道
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
输出:
Committed chaincode definition for chaincode 'fabcar' on channel 'mychannel':
Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
初始化智能合约
- 设置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 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":[]}'
输出:
2021-10-12 16:20:51.917 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
调用智能合约
- 查询所有汽车
peer chaincode query -C mychannel -n fabcar -c '{"Args":["QueryAllCars"]}'
输出:
[{"Key":"CAR0","Record":{"make":"Toyota","model":"Prius","colour":"blue","owner":"Tomoko"}},{"Key":"CAR1","Record":{"make":"Ford","model":"Mustang","colour":"red","owner":"Brad"}},{"Key":"CAR2","Record":{"make":"Hyundai","model":"Tucson","colour":"green","owner":"Jin Soo"}},{"Key":"CAR3","Record":{"make":"Volkswagen","model":"Passat","colour":"yellow","owner":"Max"}},{"Key":"CAR4","Record":{"make":"Tesla","model":"S","colour":"black","owner":"Adriana"}},{"Key":"CAR5","Record":{"make":"Peugeot","model":"205","colour":"purple","owner":"Michel"}},{"Key":"CAR6","Record":{"make":"Chery","model":"S22L","colour":"white","owner":"Aarav"}},{"Key":"CAR7","Record":{"make":"Fiat","model":"Punto","colour":"violet","owner":"Pari"}},{"Key":"CAR8","Record":{"make":"Tata","model":"Nano","colour":"indigo","owner":"Valeria"}},{"Key":"CAR9","Record":{"make":"Holden","model":"Barina","colour":"brown","owner":"Shotaro"}}]
-
查询单部汽车
peer chaincode query -C mychannel -n fabcar -c '{"Args":["QueryCar","CAR0"]}'
输出:
{"make":"Toyota","model":"Prius","colour":"blue","owner":"Tomoko"}
-
新增一辆汽车
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","Yujialing"]}'
输出:
2021-10-12 16:32:55.044 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
-
查询刚刚新增的车辆
peer chaincode query -C mychannel -n fabcar -c '{"Args":["QueryCar","CAR10"]}'
输出:
{"make":"GEELY","model":"Borui","colour":"Blue","owner":"Yujialing"}
-
更改刚刚新增车辆CAR10的车主为"广东靓仔"
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","广东靓仔"]}'
-
再次查看CAR10信息看是否修改车主成功
peer chaincode query -C mychannel -n fabcar -c '{"Args":["QueryCar","CAR10"]}'
输出:
{"make":"GEELY","model":"Borui","colour":"Blue","owner":"广东靓仔"}