【Hyperledger Fabric v1.4.11】動作環境の構築②

# 目的
仮想ソフトウェア（VirtualBox）を使用して、Linux ゲスト OS（Ubuntu）を導入し、その上に Hyperledger Fabric v1.4.11 の動作環境を構築します。
本ページでは、Hyperledger Fabric v1.4.11 のインストールと動作確認を記載しています。


# インストールするソフトウェア

* Hyperledger Fabric v1.4.11
* Hyperledger Fabric CA v1.4.9


# 作業ステップ
本ページでは、太字のステップのみ記載しています。

* ステップ①：Ubuntu Linux ゲストOSのインストールと初期設定
* ステップ②：必要なツールおよびソフトウェアのインストール
* **ステップ③：Hyperledger Fabric のインストールと動作確認**

# インストール前の準備（ステップ①②）
以下を参照してください。

https://qiita.com/kbys7367/items/b836579eed5c6ae3234f

# ステップ③：Hyperledger Fabric のインストールと動作確認

公式サイトの手順（ https://hyperledger-fabric.readthedocs.io/en/release-1.4/install.html# ）に従って、インストール用のスクリプトをダウンロードして、実行します。
デフォルトでは、Fabricの最新バージョンがインスト―スされるため、ドキュメントに記載に従ってバージョンを指定します。

```
curl -sSL http://bit.ly/2ysbOFE | bash -s -- <fabric_version> <fabric-ca_version> <thirdparty_version>
```
```
~$ mkdir fabric
~$ cd fabric/
~/fabric$ curl -sSL http://bit.ly/2ysbOFE | bash -s -- 1.4.11 1.4.9 0.4.22

Clone hyperledger/fabric-samples repo
（中略）
hyperledger/fabric-ca        1.4.9     dbbc768aec79   6 months ago   158MB
hyperledger/fabric-ca        latest    dbbc768aec79   6 months ago   158MB
```

### コマンドの実行パスの設定
```
~/fabric$ ls fabric-samples/bin/
configtxgen    cryptogen  fabric-ca-client  idemixgen  peer
configtxlator  discover   fabric-ca-server  orderer
~/fabric$ echo 'export PATH=$PATH:$HOME/fabric/fabric-samples/bin' >> ~/.profile
~/fabric$ . ~/.profile
```

### Docker コンテナの登録確認
「docker images」コマンドで、インストールした Docker コンテナを確認します。

```
~/fabric$ docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
REPOSITORY                   TAG       SIZE
hyperledger/fabric-tools     1.4       1.55GB
hyperledger/fabric-tools     1.4.11    1.55GB
hyperledger/fabric-tools     latest    1.55GB
hyperledger/fabric-ccenv     1.4       1.42GB
hyperledger/fabric-ccenv     1.4.11    1.42GB
hyperledger/fabric-ccenv     latest    1.42GB
hyperledger/fabric-orderer   1.4       133MB
hyperledger/fabric-orderer   1.4.11    133MB
hyperledger/fabric-orderer   latest    133MB
hyperledger/fabric-peer      1.4       141MB
hyperledger/fabric-peer      1.4.11    141MB
hyperledger/fabric-peer      latest    141MB
hyperledger/fabric-ca        1.4       158MB
hyperledger/fabric-ca        1.4.9     158MB
hyperledger/fabric-ca        latest    158MB
```

## サンプルによる動作確認

### fabcar を用いた動作確認
セットアップスクリプト（ startFabric.sh ）を用いて、サンプルのセットアップを行います。
セットアップ後、Hyperledger Fabric ネットワークが立ち上がります。

```
~/fabric/fabric-samples$ cd fabcar/
~/fabric/fabric-samples/fabcar$ npm install
npm WARN saveError ENOENT: no such file or directory, open '/home/vagrant/fabric/fabric-samples/fabric-samples/fabcar/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
（中略）
up to date in 10.349s

~/fabric/fabric-samples/fabcar$ ./startFabric.sh
Stopping for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
proceeding ...
（中略）
Starting for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds and using database 'couchdb'
proceeding ...
LOCAL_VERSION=1.4.11
DOCKER_IMAGE_VERSION=1.4.11
（中略）
Creating ca_peerOrg2            ... done
Creating ca_peerOrg1         ... done
Creating orderer.example.com    ... done
Creating couchdb1               ... done
Creating couchdb0            ... done
Creating couchdb3               ... done
Creating couchdb2               ... done
Creating peer0.org1.example.com ... done
Creating peer1.org1.example.com ... done
Creating peer1.org2.example.com ... done
Creating peer0.org2.example.com ... done
Creating cli                    ... done
（中略）
 ____    _____      _      ____    _____
/ ___|  |_   _|    / \    |  _ \  |_   _|
\___ \    | |     / _ \   | |_) |   | |
 ___) |   | |    / ___ \  |  _ <    | |
|____/    |_|   /_/   \_\ |_| \_\   |_|

Build your first network (BYFN) end-to-end test
（中略）
========= All GOOD, BYFN execution completed ===========


 _____   _   _   ____
| ____| | \ | | |  _ \
|  _|   |  \| | | | | |
| |___  | |\  | | |_| |
|_____| |_| \_| |____/

+ echo 'Installing smart contract on peer0.org1.example.com'
Installing smart contract on peer0.org1.example.com
（中略）
Total setup execution time : 124 secs ...

Next, use the FabCar applications to interact with the deployed FabCar contract.
The FabCar applications are available in multiple programming languages.
Follow the instructions for the programming language of your choice:

JavaScript:

  Start by changing into the "javascript" directory:
    cd javascript

  Next, install all required packages:
    npm install

  Then run the following applications to enroll the admin user, and register a new user
  called user1 which will be used by the other applications to interact with the deployed
  FabCar contract:
    node enrollAdmin
    node registerUser

  You can run the invoke application as follows. By default, the invoke application will
  create a new car, but you can update the application to submit other transactions:
    node invoke

  You can run the query application as follows. By default, the query application will
  return all cars, but you can update the application to evaluate other transactions:
    node query
（以降略）
```

docker コマンドを使用して、Hyperledger Fabric 状態を確認できます。

```
~/fabric/fabric-samples/fabcar$ docker ps --format "table {{.Names}}\t{{.Command}}" --no-trunc
NAMES                                   COMMAND
dev-peer0.org2.example.com-fabcar-1.0   "chaincode -peer.address=peer0.org2.example.com:9052"
dev-peer0.org1.example.com-fabcar-1.0   "chaincode -peer.address=peer0.org1.example.com:7052"
cli                                     "/bin/bash"
peer1.org2.example.com                  "peer node start"
peer0.org1.example.com                  "peer node start"
peer0.org2.example.com                  "peer node start"
peer1.org1.example.com                  "peer node start"
ca_peerOrg1                             "sh -c 'fabric-ca-server start --ca.certfile /etc/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem --ca.keyfile /etc/hyperledger/fabric-ca-server-config/e65789c13499f408a4b1442ee9951ccb2fac327f96c0dc038742b40a8054bf2d_sk -b admin:adminpw -d'"
orderer.example.com                     "orderer"
couchdb3                                "tini -- /docker-entrypoint.sh /opt/couchdb/bin/couchdb"
couchdb0                                "tini -- /docker-entrypoint.sh /opt/couchdb/bin/couchdb"
ca_peerOrg2                             "sh -c 'fabric-ca-server start --ca.certfile /etc/hyperledger/fabric-ca-server-config/ca.org2.example.com-cert.pem --ca.keyfile /etc/hyperledger/fabric-ca-server-config/207832b57c12b365fd56c117ea4adcbe7a1b18e8006260f6a15a560831f3eb3e_sk -b admin:adminpw -d'"
couchdb1                                "tini -- /docker-entrypoint.sh /opt/couchdb/bin/couchdb"
couchdb2                                "tini -- /docker-entrypoint.sh /opt/couchdb/bin/couchdb"

```

セットアップスクリプトの実行結果として出力された手順に従って、JavaScript 版の fabcar サンプルを実行します。

```
~/fabric/fabric-samples/fabcar$ cd javascript
~/fabric/fabric-samples/fabcar/javascript$ ls
enrollAdmin.js  node_modules  package-lock.json  registerUser.js
invoke.js       package.json  query.js           wallet

~/fabric/fabric-samples/fabcar/javascript$ npm install
npm WARN deprecated mkdirp@0.5.1: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
（中略）
found 14 vulnerabilities (6 low, 1 moderate, 6 high, 1 critical)
  run `npm audit fix` to fix them, or `npm audit` for details

~/fabric/fabric-samples/fabcar/javascript$ node enrollAdmin
Wallet path: /home/vagrant/fabric/fabric-samples/fabcar/javascript/wallet
Successfully enrolled admin user "admin" and imported it into the wallet

~/fabric/fabric-samples/fabcar/javascript$ node registerUser
Wallet path: /home/vagrant/fabric/fabric-samples/fabcar/javascript/wallet
Successfully registered and enrolled admin user "user1" and imported it into the wallet

~/fabric/fabric-samples/fabcar/javascript$ node invoke
Wallet path: /home/vagrant/fabric/fabric-samples/fabcar/javascript/wallet
Transaction has been submitted

~/fabric/fabric-samples/fabcar/javascript$ node query
Wallet path: /home/vagrant/fabric/fabric-samples/fabcar/javascript/wallet
Transaction has been evaluated, result is: [{"Key":"CAR0", "Record":{"colour":"blue","make":"Toyota","model":"Prius","owner":"Tomoko"}},{"Key":"CAR1", "Record":{"colour":"red","make":"Ford","model":"Mustang","owner":"Brad"}},{"Key":"CAR12", "Record":{"colour":"Black","make":"Honda","model":"Accord","owner":"Tom"}},{"Key":"CAR2", "Record":{"colour":"green","make":"Hyundai","model":"Tucson","owner":"Jin Soo"}},{"Key":"CAR3", "Record":{"colour":"yellow","make":"Volkswagen","model":"Passat","owner":"Max"}},{"Key":"CAR4", "Record":{"colour":"black","make":"Tesla","model":"S","owner":"Adriana"}},{"Key":"CAR5", "Record":{"colour":"purple","make":"Peugeot","model":"205","owner":"Michel"}},{"Key":"CAR6", "Record":{"colour":"white","make":"Chery","model":"S22L","owner":"Aarav"}},{"Key":"CAR7", "Record":{"colour":"violet","make":"Fiat","model":"Punto","owner":"Pari"}},{"Key":"CAR8", "Record":{"colour":"indigo","make":"Tata","model":"Nano","owner":"Valeria"}},{"Key":"CAR9", "Record":{"colour":"brown","make":"Holden","model":"Barina","owner":"Shotaro"}}]
```

query の結果を取得できれば、Hyperledger Fabric が正しく動作していることが確認できます。


## サンプルのアンインストール

アンセットアップスクリプト（ stopFabric.sh ）を用いて、サンプルのアンインストールを行います。

```
~/fabric/fabric-samples/fabcar$ ./stopFabric.sh
Stopping for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
proceeding ...
（中略）
---- No containers available for deletion ----
---- No images available for deletion ----

~/fabric/fabric-samples/fabcar$ docker ps --format "table {{.Names}}\t{{.Command}}" --no-trunc
NAMES     COMMAND
```


# 参考文献

https://hyperledger-fabric.readthedocs.io/en/release-1.4/whatsnew.html