fabric-sample 2.2.0 test-network實戰

2020-10-15 15:00:23

相比於fabricv2.0.0v2.2.0的部分指令作出了小更改

1.下載fabric-sample及fabric2.2.0二進位制檔案和映象

cd /home/yujialing/go/src/github.com/hyperledger
git clone https://github.com/hyperledger/fabric-samples.git
cd fabric-samples
git checkout 22393b629bcac7f7807cc6998aa44e06ecc77426
curl -sSL https://bit.ly/2ysbOFE | bash -s -- 2.2.0 1.4.8 -s

設定環境變數

vim ~/.bashrc

將以下內容複製到bashrc檔案中(路徑中使用者名稱需要改成你們的使用者名稱),按「I」插入,插入完成後按ESC退出插入,輸出:wq!儲存退出,如下圖所示(pwdfabric-sample的路徑)

export PATH=$PATH:/home/yujialing/go/src/github.com/hyperledger/fabric-samples/bin
export FABRIC_CFG_PATH=/home/yujialing/go/src/github.com/hyperledger/fabric-samples/config

退出後在命令列輸入以下命令重新整理組態檔

source ~/.bashrc

2.建立並初始化Fabric網路

cd test-network
./network.sh up -ca
./network.sh createChannel
./network.sh deployCC

./network.sh up -ca執行完畢後如下圖所示

./network.sh createChannel執行完畢後如下圖所示

./network.sh deployCC執行完畢後如下圖所示


3.賬本互動

Org1Peer0的身份進行賬本互動,在命令列中輸入以下指令

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 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 basic --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":"CreateAsset","Args":["asset6","black","20","Jack","630"]}'

在這裡插入圖片描述

查詢剛剛建立的資產

peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'

在這裡插入圖片描述
更新資產,將顏色改為blue藍色,價值改為680

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 basic --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":"UpdateAsset","Args":["asset6","blue","20","Jack","680"]}'

查詢資產是否更新成功

peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'

在這裡插入圖片描述
查詢資產是否存在

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 basic --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":"AssetExists","Args":["asset6"]}'

在這裡插入圖片描述
資產轉移,將asset6資產由Jack轉給Michel

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 basic --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":"TransferAsset","Args":["asset6","Michel"]}'

在這裡插入圖片描述
刪除資產,將asset6資產刪除

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 basic --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":"DeleteAsset","Args":["asset6"]}'

在這裡插入圖片描述
歡迎小夥伴的討論,若有問題請在評論區評論或私信,謝謝你。