Hyperledger Fabric2.x fabcar查詢、插入和更改擁有者實踐


一、概述

上一篇文章,已經介紹了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包

 

下載完成后,得到文件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]# ls
fabcar.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:7050
2020-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:7050
2020-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:7051
2020-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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM