超級賬本test-network測試工作流程

2020-09-29 11:01:14

一、啟動測試網路

1.進入test-network目錄

cd fabric-samples/test-network

2.在test-network目錄中,執行以下命令刪除先前執行的所有容器或工程

./network.sh down

3.過執行以下命令來啟動網路

./network.sh up

此命令建立一個由兩個peer節點和一個order節點組成的Fabric網路

二、測試網路的組成部分

執行以下命令以列出所有正在您的計算機上執行的Docker容器

docker ps -a

三、建立一個通道

  1. 執行以下命令以建立一個預設名稱為「 mychannel」的通道
./network.sh createChannel

四、在channel啟動一個鏈碼

1.使用以下命令在通道上啟動鏈碼

./network.sh deployCC

五、與網路互動

1.使用以下命令將這些二進位制檔案新增到您的CLI路徑

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

2.還需要將fabric-samples程式碼庫中的FABRIC_CFG_PATH設定為指向其中的core.yaml檔案

export FABRIC_CFG_PATH=$PWD/../config/

3.設定環境變數,以允許您作為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

4.執行以下命令以獲取已新增到通道賬本中的汽車列表

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

執行上面👆指令前需要執行如下:
新開一個命令視窗
執行啟動指令:cd fabric-samples/fabcar後,
執行./startFabric.sh,等待完成即可。
最後再繼續執行之前的指令

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"}}]

5.使用以下命令呼叫fabcar鏈碼來更改賬本上汽車的所有者

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 ${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":"changeCarOwner","Args":["CAR9","Dave"]}'

如果命令成功,應該看到以下響應:

2020-9-04 17:38:21.048 EST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200

6.設定以下環境變數以作為Org2進行操作

export CORE_PEER_TLS_ENABLED=true
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

7.現在可以查詢執行在peer0.org2.example.com的fabcar鏈碼

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

結果將顯示"CAR9"已轉移到Dave:

{"make":"Holden","model":"Barina","colour":"brown","owner":"Dave"}

六、關停網路

使用完測試網路後,您可以使用以下命令關閉網路

./network.sh down