Hyperledger Fabric2.x fabcar查询、插入和更改拥有者实践

发布时间:2020-11-19 13:15:25编辑:admin阅读(2738)

    一、概述

    上一篇文章,已经介绍了Hyperledger Fabric 2.x 环境搭建,参考链接:https://www.cnblogs.com/xiao987334176/p/13969885.html

    接下来介绍如何使用它。

     

    二、Hyperledger Fabric Samples

    github地址:https://github.com/hyperledger/fabric-samples

    您可以使用Fabric示例开始使用Hyperledger结构,探索重要的结构功能,并学习如何使用Fabric SDK构建与区块链网络交互的应用程序。

     

    接下来介绍,如何将这个智能合约导入进去。

    下载智能合约依赖

    下载zip包

    1.png

     

    下载完成后,得到文件fabric-samples-master.zip

    将文件上传到/opt目录

     

    解压文件,并执行go命令

    cd /opt/
    unzip fabric-samples-master.zip
    cd /opt/fabric-samples-master/chaincode/fabcar/go
    go env -w GO111MODULE=on
    go env -w GOPROXY=https://goproxy.cn,direct
    go mod init
    go mod vendor

    执行完毕后,当前文件夹下多出vendor,这个就是放智能合约依赖包的文件夹

     

    与网络互动

    网络启动之后,可以使用peer cli客户端去操作网络,可以通过cli客户端去调用部署智能合约,更新通道,或者安装和部署新的智能合约。

    首先确保操作目录为test-network目录,比如我的目录是:

    /opt/fabric-2.2.1/scripts/fabric-samples/test-network

    使用以下命令将二进制文件添加到cli路径:

    export PATH=${PWD}/../bin:${PWD}:$PATH

     

    还需要设置FABRIC_CFG_PATH路径指向fabric-samples中的core.yaml文件,命令如下:

    export FABRIC_CFG_PATH=$PWD/../config/

     

    组织1(Org1)部署智能合约

    设置允许org1操作peer cli的环境变量:

    # Environment variables for Org1
    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


    添加hosts记录

    vi /etc/hosts

    内容如需:

    127.0.0.1 orderer.example.com
    127.0.0.1 peer0.org1.example.com
    127.0.0.1 peer0.org2.example.com

    注意:由于在本机运行,所以ip地址是127.0.0.1

     

    打包智能合约

    cd /opt/fabric-samples-master/chaincode/fabcar/go
    peer lifecycle chaincode package fabcar.tar.gz --path /opt/fabric-samples-master/chaincode/fabcar/go/ --lang golang --label fabcar_1

    ls查看当前目录,发现有了一个fabcar.tar.gz文件

    [root@centos7_02 go]# lsfabcar.go  fabcar.tar.gz  go.mod  go.sum  vendor

     

    安装智能合约(时间会比较久,耐心稍等)

    peer lifecycle chaincode install fabcar.tar.gz

     输出:

    2020-11-16 13:34:09.269 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nIfabcar_1:762e0fe3dbeee0f7b08fb6200adeb4a3a20f649a00f168c0b3c2257e53b6e506\022\010fabcar_1" > 
    2020-11-16 13:34:09.269 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: fabcar_1:762e0fe3dbeee0f7b08fb6200adeb4a3a20f649a00f168c0b3c2257e53b6e506

    成功后返回status:200等信息

     

    当前组织同意合约定义(需要用到上面一部返回的Package id

    export cafile=/opt/fabric-2.2.1/scripts/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
    
    peer lifecycle chaincode approveformyorg --tls true --cafile $cafile --channelID mychannel --name fabcar --version 1 --init-required --package-id fabcar_1:762e0fe3dbeee0f7b08fb6200adeb4a3a20f649a00f168c0b3c2257e53b6e506 --sequence 1 –waitForEvent

    输出:

    2020-11-16 13:42:33.922 CST [cli.lifecycle.chaincode] setOrdererClient -> INFO 001 Retrieved channel (mychannel) orderer endpoint: orderer.example.com:70502020-11-16 13:42:35.964 CST [chaincodeCmd] ClientWait -> INFO 002 txid [7ac6f8b7673fefdaf87d14b3c31d99d507192bf416e23d740e514dd05de010f1] committed with status (VALID) at localhost:7051

     

    此时检查合约定义是否满足策略

    peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name fabcar --version 1 --sequence 1 --output json --init-required

    输出:

    {
        "approvals": {
            "Org1MSP": true,
            "Org2MSP": false
        }
    }

     

    Org2MSP这里显示未同意合约定义
    那么需要进入Org2中的环境配置中重复合约安装并同意合约定义

     

    切换环境到Org2

     切换环境变量为peer0.org2.example.com的配置

    export CORE_PEER_LOCALMSPID="Org2MSP"
    export CORE_PEER_TLS_ROOTCERT_FILE=/opt/fabric-2.2.1/scripts/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
    export CORE_PEER_MSPCONFIGPATH=/opt/fabric-2.2.1/scripts/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
    export CORE_PEER_ADDRESS=peer0.org2.example.com:9051

     

    组织2(Org2)部署智能合约

    由于Org1已经打包了智能合约,Org2直接安装就可

    cd /opt/fabric-samples-master/chaincode/fabcar/go
    peer lifecycle chaincode install fabcar.tar.gz

    输出:

    2020-11-16 13:47:37.740 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nIfabcar_1:762e0fe3dbeee0f7b08fb6200adeb4a3a20f649a00f168c0b3c2257e53b6e506\022\010fabcar_1" > 
    2020-11-16 13:47:37.740 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: fabcar_1:762e0fe3dbeee0f7b08fb6200adeb4a3a20f649a00f168c0b3c2257e53b6e506

     

    当前组织同意合约定义(需要用到上面一部返回的Package id

    export cafile=/opt/fabric-2.2.1/scripts/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
    
    peer lifecycle chaincode approveformyorg --tls true --cafile $cafile --channelID mychannel --name fabcar --version 1 --init-required --package-id fabcar_1:762e0fe3dbeee0f7b08fb6200adeb4a3a20f649a00f168c0b3c2257e53b6e506 --sequence 1 –waitForEvent

     

    输出:

    2020-11-16 13:49:31.806 CST [cli.lifecycle.chaincode] setOrdererClient -> INFO 001 Retrieved channel (mychannel) orderer endpoint: orderer.example.com:70502020-11-16 13:49:33.844 CST [chaincodeCmd] ClientWait -> INFO 002 txid [4b74a503343ca2f99a5a1b5827b8ba818eebd4fc059f9a6d62ed508a30fc98ec] committed with status (VALID) at peer0.org2.example.com:9051

     

    再次检查合约定义是否满足策略

    peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name fabcar --version 1 --sequence 1 --output json --init-required

    输出:

    {
        "approvals": {
            "Org1MSP": true,
            "Org2MSP": true
        }
    }

     

    过半数组织同意了智能合约定义
    那么可以提交智能合约

    export org1_CertFiles=/opt/fabric-2.2.1/scripts/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
    export org2_CertFiles=/opt/fabric-2.2.1/scripts/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
    
    peer lifecycle chaincode commit -o orderer.example.com:7050 --tls true --cafile $cafile --channelID mychannel --name fabcar --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles  $org1_CertFiles --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles  $org2_CertFiles --version 1 --sequence 1 --init-required

    输出:

    2020-11-16 13:55:48.068 CST [chaincodeCmd] ClientWait -> INFO 001 txid [356869cdf5affcc19974fbe3f4367c570b0676d2ac5d35038382f216112b2f8f] committed with status (VALID) at peer0.org1.example.com:70512020-11-16 13:55:48.074 CST [chaincodeCmd] ClientWait -> INFO 002 txid [356869cdf5affcc19974fbe3f4367c570b0676d2ac5d35038382f216112b2f8f] committed with status (VALID) at peer0.org2.example.com:9051

     

    成功后初始化智能合约

    peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile $cafile -C mychannel -n fabcar --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles $org1_CertFiles --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles $org2_CertFiles --isInit -c '{"Args":["InitLedger"]}'

    输出:

    2020-11-16 13:58:13.631 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 orderer.example.com:7050 --tls true --cafile $cafile -C mychannel -n fabcar --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles $org1_CertFiles --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles $org2_CertFiles -c '{"Args":["CreateCar","CAR10","GEELY","Borui","Blue","Yujialing"]}'

    输出:

    2020-11-16 14:01:00.005 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 orderer.example.com:7050 --tls true --cafile $cafile -C mychannel -n fabcar --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles $org1_CertFiles --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles $org2_CertFiles -c '{"Args":["ChangeCarOwner","CAR10","广东靓仔"]}'

    输出:

    2020-11-16 14:03:20.053 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200

     

    再次查看CAR10信息看是否修改车主成功

    peer chaincode query -C mychannel -n fabcar -c '{"Args":["QueryCar","CAR10"]}'

    输出:

    {"make":"GEELY","model":"Borui","colour":"Blue","owner":"广东靓仔"}

    其他用go语言编写的智能合约也是相同的操作
    Hyperledger Fabric 2.0的fabcar实践至此结束

     

     

    本文参考链接:

    https://blog.csdn.net/bean_business/article/details/108015490


关键字