Fabric開発(三)ubuntuの下でFabcar 2.0ネットワークを起動し、Fabcarのdemoをテストします.


前の章の内容はfabric 1.4.3ネットワーク構築、fabric 2.2.0ローカルコンパイルについて詳しく説明しました.今日はfabria 2.2.0バージョンで、ネットワークを起動し、SDKでfabcarのdemoを呼び出して、fabricの新しいバージョンを体験してみます.今日はNode.jsでSDKを作成します.なので、node.jsをインストールして、fabric 2.2.0はnodeに対してバージョン要求があります.今回は最新のnodejsをインストールします.v 12.18 xです.
1.最新バージョンのnodejsをインストールする
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install -y nodejs
バージョンを表示
node -v
v12.18.2
必ずnpmの最新バージョンを更新してください.でないと、SDKは成功しません.これらのピットは全部私達が踏んだ更新npmです.
npm install -g npm
2.fabricのソースコード、fabric-samplesのソースコード、fabricのミラーをダウンロードして、私達はGit命令を使ってソースコードをダウンロードすることができます.まず対応するカタログを作成して、それからディレクトリに入ります.Gitダウンロードのソースコード:
mkdir -p ~/go/src/github.com/hyperledger 
cd ~/go/src/github.com/hyperledger 
git clone https://github.com/hyperledger/fabric.git
Fabricは更新されているので、最新のソースコードは必要ありません.v 2.2.0バージョンのソースコードに切り替える必要があります.
cd ~/go/src/github.com/hyperledger/fabric
git checkout v2.2.0
3.Fabric Dockerミラーのダウンロード
cd ~/go/src/github.com/hyperledger/fabric/script
./bootstrap.sh
ダウンロードが完了したら、ダウンロードしたdockerのミラーを一覧表示します.具体的には下記の通りです.
REPOSITORY                   TAG                            IMAGE ID            CREATED             SIZE
hyperledger/fabric-ccenv     2.2                            a7ae534189c8        33 minutes ago      586MB
hyperledger/fabric-ccenv     2.2.0                          a7ae534189c8        33 minutes ago      586MB
hyperledger/fabric-ccenv     amd64-2.2.0-snapshot-5ea85bc   a7ae534189c8        33 minutes ago      586MB
hyperledger/fabric-ccenv     latest                         a7ae534189c8        33 minutes ago      586MB
hyperledger/fabric-baseos    2.2                            0cd12fe77474        2 hours ago         6.85MB
hyperledger/fabric-baseos    2.2.0                          0cd12fe77474        2 hours ago         6.85MB
hyperledger/fabric-baseos    amd64-2.2.0-snapshot-5ea85bc   0cd12fe77474        2 hours ago         6.85MB
hyperledger/fabric-baseos    latest                         0cd12fe77474        2 hours ago         6.85MB
hyperledger/fabric-tools     2.2                            5eb2356665e7        2 weeks ago         519MB
hyperledger/fabric-tools     2.2.0                          5eb2356665e7        2 weeks ago         519MB
hyperledger/fabric-tools     latest                         5eb2356665e7        2 weeks ago         519MB
hyperledger/fabric-peer      2.2                            760f304a3282        2 weeks ago         54.9MB
hyperledger/fabric-peer      2.2.0                          760f304a3282        2 weeks ago         54.9MB
hyperledger/fabric-peer      latest                         760f304a3282        2 weeks ago         54.9MB
hyperledger/fabric-orderer   2.2                            5fb8e97da88d        2 weeks ago         38.4MB
hyperledger/fabric-orderer   2.2.0                          5fb8e97da88d        2 weeks ago         38.4MB
hyperledger/fabric-orderer   latest                         5fb8e97da88d        2 weeks ago         38.4MB
hyperledger/fabric-ccenv     <none>                         aac435a5d3f1        2 weeks ago         586MB
hyperledger/fabric-baseos    <none>                         aa2bdf8013af        2 weeks ago         6.85MB
hyperledger/fabric-nodeenv   2.2                            ab88fe4d29dd        3 weeks ago         293MB
hyperledger/fabric-nodeenv   2.2.0                          ab88fe4d29dd        3 weeks ago         293MB
hyperledger/fabric-nodeenv   latest                         ab88fe4d29dd        3 weeks ago         293MB
hyperledger/fabric-javaenv   2.2                            56c30f316b23        3 weeks ago         504MB
hyperledger/fabric-javaenv   2.2.0                          56c30f316b23        3 weeks ago         504MB
hyperledger/fabric-javaenv   latest                         56c30f316b23        3 weeks ago         504MB
couchdb                      3.1                            7962d7fc8ed7        6 weeks ago         189MB
golang                       1.14.4-alpine3.12              3289bf11c284        7 weeks ago         370MB
alpine                       3.12                           a24bb4013296        7 weeks ago         5.57MB
hyperledger/fabric-ca        1.4                            743a758fae29        2 months ago        154MB
hyperledger/fabric-ca        1.4.7                          743a758fae29        2 months ago        154MB
hyperledger/fabric-ca        latest                         743a758fae29        2 months ago        154MB
ここでfabricソース、fabric-samplesソース、fabricのミラーリングの仕事はすでにls命令を実行しました.現在のディレクトリの下でfabric-samplesディレクトリが多くなりました.4.Fabricネットワークを起動してtest-networkフォルダに入り、テストネットワークを開始します.
./network.sh up
次のように表示します
Starting nodes with CLI timeout of '5' tries and CLI delay of '3' seconds and using database 'leveldb'

LOCAL_VERSION=2.2.0
DOCKER_IMAGE_VERSION=2.2.0
Creating network "net_test" with the default driver
Creating volume "net_peer0.org1.example.com" with default driver
Creating volume "net_peer0.org2.example.com" with default driver
Creating volume "net_orderer.example.com" with default driver
Creating peer0.org2.example.com ...
Creating peer0.org1.example.com ...
Creating orderer.example.com ...
Creating peer0.org2.example.com
Creating orderer.example.com
Creating peer0.org2.example.com ... done
CONTAINER ID        IMAGE                               COMMAND                  CREATED             STATUS                     PORTS                              NAMES
f16b792b2b82        hyperledger/fabric-peer:latest      "peer node start"        3 seconds ago       Up 1 second                0.0.0.0:7051->7051/tcp             peer0.org1.example.com
55253429c867        hyperledger/fabric-orderer:latest   "orderer"                3 seconds ago       Up Less than a second      0.0.0.0:7050->7050/tcp             orderer.example.com
b6ba2d2de649        hyperledger/fabric-peer:latest      "peer node start"        3 seconds ago       Up Less than a second      7051/tcp, 0.0.0.0:9051->9051/tcp   peer0.org2.example.com
cf8d0e038f5d        hyperledger/fabric-nodeenv:2.2      "docker-entrypoint.s…"   7 hours ago         Exited (137) 7 hours ago                                      priceless_hamilton

ネットワークの起動に成功しました.4.1チャンネルを確立し、network.shスクリプトを使用してOrg 1とOrg 2の間でチャネルを作成し、そのピアをチャンネルに追加します.次のコマンドを実行して、デフォルトの名前でチャンネルmychanelを作成します.
./network.sh createChannel
作成に成功しました.4.2チャネル上でチェーンコードを起動する
./network.sh deployCC
アクセスでダウンロードできない場合、解決方法:goエージェントを設定します.https://www.cnblogs.com/zhangmingcheng/p/12294156.html
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
4.3ネットワークとの対話命令
./network.sh up
./network.sh createChannel
./network.sh deployCC
./network.sh down
4.3.1 test-networkディレクトリで動作し、これらのバイナリファイルをCLIパスに追加する
export PATH=${PWD}/../bin:${PWD}:$PATH
4.3.2 FABRIC_を設置するCFG_PATHは格納庫の中のcore.yamlファイルfabric-samplesを指す:
export FABRIC_CFG_PATH=$PWD/../config/
4.3.3環境変数を設定して、peer Org 1の形で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
4.3.4チャンネル分類帳に追加された自動車のリストを取得する.
peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'

次のように表示します
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"}}]

4.3.5ネットワークを閉じる:
./network.sh down
Stopping network

Stopping peer0.org1.example.com ... done
Stopping orderer.example.com    ... done
Stopping peer0.org2.example.com ... done
Removing peer0.org1.example.com ... done
Removing orderer.example.com    ... done
Removing peer0.org2.example.com ... done
Removing network net_test
Removing volume net_peer0.org1.example.com
Removing volume net_peer0.org2.example.com
Removing volume net_orderer.example.com
Removing network net_test
WARNING: Network net_test not found.
Removing volume net_peer0.org3.example.com
WARNING: Volume net_peer0.org3.example.com not found.
私達は今fabcarのデモを起動して、体験してみます.フォルダに入ります.
cd ~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/fabcar

5.fabcarテストネットワークを起動する
cd ~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/fabcar
./startFabric.sh
以下の画面を表示して起動成功を説明します.
Committed chaincode definition for chaincode 'fabcar' on channel 'mychannel':
Version: 1, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
===================== Query chaincode definition successful on peer0.org1 on channel 'mychannel' =====================

Using organization 2
===================== Querying chaincode definition on peer0.org2 on channel 'mychannel'... =====================
Attempting to Query committed status on peer0.org2, Retry after 3 seconds.
++ peer lifecycle chaincode querycommitted --channelID mychannel --name fabcar
++ res=0
++ set +x

Committed chaincode definition for chaincode 'fabcar' on channel 'mychannel':
Version: 1, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
===================== Query chaincode definition successful on peer0.org2 on channel 'mychannel' =====================

Using organization 1
Using organization 2
++ fcn_call='{"function":"initLedger","Args":[]}'
++ echo invoke fcn 'call:{"function":"initLedger","Args":[]}'
invoke fcn call:{"function":"initLedger","Args":[]}
++ peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile /home/cuijb/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n fabcar --peerAddresses localhost:7051 --tlsRootCertFiles /home/cuijb/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles /home/cuijb/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt --isInit -c '{"function":"initLedger","Args":[]}'
++ res=0
++ set +x
2020-07-23 21:40:27.083 +05 [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
===================== Invoke transaction successful on peer0.org1 peer0.org2 on channel 'mychannel' =====================

6.SDKを利用してfabricネットワークとインタラクションして、javascript、typescript、java、go言語とネットワークを選択します.今日はnodejsを選択します.
cd ~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/fabcar
cd javascript
npm install
私たちは契約の流れに従って順番にスタートします.
node enrollAdmin.js
Wallet path: /home/cuijb/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/fabcar/javascript/wallet
Successfully enrolled admin user "admin" and imported it into the wallet

node registerUser.js
Wallet path: /home/cuijb/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/fabcar/javascript/wallet
Successfully registered and enrolled admin user "appUser" and imported it into the wallet
これから、取引を行います.
node invoke.js
Wallet path: /home/cuijb/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/fabcar/javascript/wallet
2020-07-23T17:02:53.715Z - error: [ServiceEndpoint]: Error: Failed to connect before the deadline on Endorser- name: peer0.org1.example.com, url:grpcs://localhost:7051, connected:false, connectAttempted:true
2020-07-23T17:02:53.716Z - error: [ServiceEndpoint]: waitForReady - Failed to connect to remote gRPC server peer0.org1.example.com url:grpcs://localhost:7051 timeout:3000
2020-07-23T17:02:53.718Z - error: [NetworkConfig]: buildPeer - Unable to connect to the endorser peer0.org1.example.com due to Error: Failed to connect before the deadline on Endorser- name: peer0.org1.example.com, url:grpcs://localhost:7051, connected:false, connectAttempted:true
    at checkState (/home/cuijb/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/fabcar/javascript/node_modules/@grpc/grpc-js/build/src/client.js:69:26)
    at Timeout._onTimeout (/home/cuijb/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/fabcar/javascript/node_modules/@grpc/grpc-js/build/src/channel.js:292:17)
    at listOnTimeout (internal/timers.js:549:17)
    at processTimers (internal/timers.js:492:7) {
  connectFailed: true
}
2020-07-23T17:02:56.725Z - error: [ServiceEndpoint]: Error: Failed to connect before the deadline on Discoverer- name: peer0.org1.example.com, url:grpcs://localhost:7051, connected:false, connectAttempted:true
2020-07-23T17:02:56.725Z - error: [ServiceEndpoint]: waitForReady - Failed to connect to remote gRPC server peer0.org1.example.com url:grpcs://localhost:7051 timeout:3000
Failed to submit transaction: Error: Failed to connect before the deadline on Discoverer- name: peer0.org1.example.com, url:grpcs://localhost:7051, connected:false, connectAttempted:true

多くの間違いを発見しました.ネットワークを起動し、証明書を更新したため、きれいに削除されていない環境で多重化前のvolumeが起動され、証明書の認証に失敗しました.ソリューション:以下のコマンドを実行して、ボリュームを削除し、ネットワークを閉じます.
./networkDown.sh
以下のコマンドを実行してボリュームを削除します.
docker rm  $(docker ps -a | grep "hyperledger/*" | awk "{print \$1}") 
docker-compose down --volumes --remove-orphans 
docker volume prune
docker network prune 
ネットワークを再起動
./startFabric.sh
SDK呼び出し環境に入る
node enrollAdmin.js
Wallet path: /home/cuijb/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/fabcar/javascript/wallet
Successfully enrolled admin user "admin" and imported it into the wallet
実行します
 node registerUser.js
Wallet path: /home/cuijb/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/fabcar/javascript/wallet
Successfully registered and enrolled admin user "appUser" and imported it into the wallet

取引を実行する
node invoke.js
Wallet path: /home/cuijb/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/fabcar/javascript/wallet
Transaction has been submitted
私たちははっきりと取引が完了したのを見ることができます.これから取引を調べます.
node query.js
Wallet path: /home/cuijb/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/fabcar/javascript/wallet
Transaction has been evaluated, result is: [{"Key":"CAR0","Record":{"make":"Toyota","model":"Prius","colour":"blue","owner":"Tomoko"}},{"Key":"CAR1","Record":{"make":"Ford","model":"Mustang","colour":"red","owner":"Brad"}},{"Key":"CAR13","Record":{"make":"Honda","model":"Accord123","colour":"Black","owner":"jake"}},{"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コマンドでチェーンコードを実行します.
peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'
戻りの結果は以下の通りです.
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"}}]
SDKを通じて照会を呼び出す取引と、命令で照会する取引が一致しているかどうか確認できます.
ありがとうございます2020/7月24日に2020/7/26を修正します.