Hyperledger Fabric Multi Host 対応(2)

# Hyperledger Multi Host 対応(2)

Hyperledger Fabric のマルチホスト構成について、実装方法の検討とFabcar を使用した実装例を考えてみる。

今回は、構築のための事前準備を実施。  
すでにHypeledger Fabricと、fabric-sampleの環境を構築済みの場合は、本章はスキップしてください。

---
## 準備

Ubuntu 18.04 LTS がインストールされた環境を2台準備する。  
※セキュリティ設定で互いに通信ができるようにしておく。  

* 事前準備

Hyperledger Fabricに必要な dockerなどを以下を参考にしてインストールする。  
※2台とも同じ環境を作成する。VM環境で構築する場合は、1台作成したところでclone してもいい。
<https://hyperledger-fabric.readthedocs.io/en/release-1.4/prereqs.html>

* Hyperledger Fabric 1.4.6 とfabric-samples をインストール
以下のコマンドで、Hyperledger FabricのDockerイメージと、Fabric-samplesをインストール。  

```
$ curl -sSL http://bit.ly/2ysbOFE | bash -s -- 1.4.6 1.4.6 0.4.18
```

以下のようにコンテナイメージの一覧が表示されたら、インストール完了

```
===> List out hyperledger docker images
hyperledger/fabric-ca          1.4                 3b96a893c1e4        4 days ago          150MB
hyperledger/fabric-ca          1.4.6               3b96a893c1e4        4 days ago          150MB
hyperledger/fabric-ca          latest              3b96a893c1e4        4 days ago          150MB
hyperledger/fabric-tools       1.4                 0f9743ac0662        4 days ago          1.49GB
hyperledger/fabric-tools       1.4.6               0f9743ac0662        4 days ago          1.49GB
hyperledger/fabric-tools       latest              0f9743ac0662        4 days ago          1.49GB
hyperledger/fabric-ccenv       1.4                 191911f4454f        4 days ago          1.36GB
hyperledger/fabric-ccenv       1.4.6               191911f4454f        4 days ago          1.36GB
hyperledger/fabric-ccenv       latest              191911f4454f        4 days ago          1.36GB
hyperledger/fabric-orderer     1.4                 84eaba5388e7        4 days ago          120MB
hyperledger/fabric-orderer     1.4.6               84eaba5388e7        4 days ago          120MB
hyperledger/fabric-orderer     latest              84eaba5388e7        4 days ago          120MB
hyperledger/fabric-peer        1.4                 5a52faa5d8c2        4 days ago          128MB
hyperledger/fabric-peer        1.4.6               5a52faa5d8c2        4 days ago          128MB
hyperledger/fabric-peer        latest              5a52faa5d8c2        4 days ago          128MB
hyperledger/fabric-zookeeper   0.4                 ede9389347db        3 months ago        276MB
hyperledger/fabric-zookeeper   0.4.18              ede9389347db        3 months ago        276MB
hyperledger/fabric-zookeeper   latest              ede9389347db        3 months ago        276MB
hyperledger/fabric-kafka       0.4                 caaae0474ef2        3 months ago        270MB
hyperledger/fabric-kafka       0.4.18              caaae0474ef2        3 months ago        270MB
hyperledger/fabric-kafka       latest              caaae0474ef2        3 months ago        270MB
hyperledger/fabric-couchdb     0.4                 d369d4eaa0fd        3 months ago        261MB
hyperledger/fabric-couchdb     0.4.18              d369d4eaa0fd        3 months ago        261MB
hyperledger/fabric-couchdb     latest              d369d4eaa0fd        3 months ago        261MB
```

* Fabric-sample の動作確認
fabcar のフォルダに移動して実行。

```
$ cd fabric-samples/fabcar/
$ ./startFabric.sh golang
```
以下のように表示されたら起動完了。

```
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

TypeScript:

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

  Next, install all required packages:
    npm install

  Next, compile the TypeScript code into JavaScript:
    npm run build

  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 dist/enrollAdmin
    node dist/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 dist/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 dist/query

Java:

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

  Then, install dependencies and run the test using:
    mvn test

  The test will invoke the sample client app which perform the following:
    - Enroll admin and user1 and import them into the wallet (if they don't already exist there)
    - Submit a transaction to create a new car
    - Evaluate a transaction (query) to return details of this car
    - Submit a transaction to change the owner of this car
    - Evaluate a transaction (query) to return the updated details of this car
```

起動したら、手順にしたがって動作確認する。

```
$ cd javascript
$ npm install
```

以下のようなセキュリティ警告が出たら、次のURLを参考に解消する。
<https://qiita.com/hibikikudo/items/0af352acac85fce28ec2>

```
added 504 packages from 1081 contributors and audited 1723 packages in 74.447s

1 package is looking for funding
  run `npm fund` for details

found 9 vulnerabilities (2 low, 1 moderate, 5 high, 1 critical)
  run `npm audit fix` to fix them, or `npm audit` for details
```

Admin証明書取得、user１登録

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

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

query / invoke の実行

```
$ node query.js 
Wallet path: /home/fukata/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":"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"}}]

$ node invoke.js 
Wallet path: /home/fukata/fabric-samples/fabcar/javascript/wallet
Transaction has been submitted

$ node query.js 
Wallet path: /home/fukata/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"}}]
 
```

きちんと動作していることが確認できたら動作確認完了です。

---

## 2台のPC間でSSH接続ができるようにする。

今回は、証明書などをコピーするため、SSHで接続できるようにする。  
以後、org1 側を ホスト1、org2側をホスト2と呼ぶ


### SSHのインストール
SSHを2台ともインストール  
org1 側

```
$ sudo apt install ssh
```

org２側

```
$ sudo apt install ssh
```

### 公開鍵の作成と配布
ホスト１側で、証明書を作成して、ホスト2に公開鍵を渡す。   

#### ホスト１の証明書作成
※以下の例では、passphrase は無しとしていますが、11文字以上としておくことをお勧めします。  

```
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/fukata/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/fukata/.ssh/id_rsa.
Your public key has been saved in /home/fukata/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:0nkDTDeLA/goorQMtnaDr7kPdTw7UFza8jf9FDom+mA fukata@fukata-VirtualBox
The key's randomart image is:
+---[RSA 2048]----+
|     .... o      |
|    o ++ o o     |
|     B .= .  .   |
|oo. + +. +. . .  |
|*.++ +..S+o= .   |
|.*.oo o.o.+.+    |
|..o .o E     .   |
|  o.  o o        |
| ++.     .       |
+----[SHA256]-----+
```

#### 公開鍵のコピー
公開鍵を、ホスト2にコピーする

```
$ scp -P 22 ~/.ssh/id_rsa.pub <ユーザ名>@<ホスト2_IPアドレス>:~/.ssh/authorized_keys
```

#### パーミッションの変更

ホスト1側

```
$ chmod 700 ~/.ssh
```

ホスト2側

```
$ chmod 600 ~/.ssh/authorized_keys
```

#### 接続確認

ホスト１からホスト２に接続する。  
パスワードを入力することなく接続できれば完了。

```
$ ssh <ユーザ名>@<ホスト2_IPアドレス>
```

次は、マルチホスト環境を構築。