CentOSでHyperledgerFabricのサンプルシステム「fabcar」を動かすまでのコマンド

# はじめに
ブロックチェーン基盤の一つHyperledgerFabricのサンプルシステムfabcarをCentOS上で動かしてみた。
サンプルで提供されるSDKを動かすまでのコマンドをまとめたので記録しておく。

---
## CentOSのバージョン確認

``` :CentOSコンソール
$ cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
```

---
## まずcentosを日本語化
[参考](https://www.server-world.info/query?os=CentOS_7&p=japanese)  

``` :CentOSコンソール
$ sudo yum -y install ibus-kkc vlgothic-*
$ vi ~/.bash_profile
```

.bash_profileを編集する。

``` :.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH

# 追加ここから
LANG=ja_JP.UTF-8
 export LANG
# 追加ここまで
```
``` :CentOSコンソール
$ source ~/.bash_profile
$ a
-bash: a: コマンドが見つかりません     ←日本語になってることを確認
```

---
## HyperledgerFabricの前提ソフトインストール
Prerequisites 
[公式](https://hyperledger-fabric.readthedocs.io/en/release-1.4/prereqs.html)

### Install cURL
  既に済みだった  

### Docker and Docker Compose

- docker インストール  
[参考](https://qiita.com/inakadegaebal/items/be9fecce813cebec5986)

``` :CentOSコンソール
$ sudo yum install -y yum-utils
$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
$ sudo yum makecache fast

$ yum list docker-ce.x86_64 --showduplicates | sort -r
$ sudo yum install docker-ce-18.09.6-3.el7

$ docker -v
Docker version 19.03.3, build a872fc2f86

$ groups
centos adm wheel systemd-journal

$ sudo usermod -aG docker centos

#再度ログイン

$ groups
centos adm wheel systemd-journal docker
```
<br>
dockerコマンドをsudoを書かなくてもようにする

``` :CentOSコンソール
sudo groupadd docker
sudo usermod -aG docker centos
```

docker起動

``` :CentOSコンソール
sudo systemctl start docker
sudo docker run hello-world
```

OS起動時にDockerを自動起動させる

``` :CentOSコンソール
sudo systemctl enable docker
```  
<br>  

- docker compose インストール  
[参考](https://qiita.com/uhooi/items/fb14d99d3323bd2eee9d)

``` :CentOSコンソール
$ sudo curl -L https://github.com/docker/compose/releases/download/1.14.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose

$ docker-compose --version
docker-compose version 1.14.0, build c7bdf9e

```

### go言語インストール

[参考](https://stupiddog.jp/note/archives/1148)

``` :CentOSコンソール
$ curl -L -O https://dl.google.com/go/go1.11.10.linux-amd64.tar.gz
$ sudo tar -C /usr/local -xf go1.11.10.linux-amd64.tar.gz

$ echo 'export GOPATH=$HOME/go' >> ~/.bash_profile
$ echo 'export PATH=$PATH:$GOPATH/bin' >> ~/.bash_profile
$ source ~/.bash_profile

$ go version
go version go1.11.10 linux/amd64
```

### nodejsインストール

[参考](http://neos21.hatenablog.com/entry/2019/03/30/080000)
[参考](http://yuji-ueda.hatenadiary.jp/entry/2017/03/14/104727)

``` :CentOSコンソール
$ curl -sL https://rpm.nodesource.com/setup_8.x | bash -
$ sudo yum install nodejs-8.9.4

$ node -v
v8.9.4
$ npm -v
5.6.0
```

### gitをインストール

[参考](https://www.task-notes.com/entry/20150622/1434942000)

``` :CentOSコンソール
$ sudo yum -y install git
$ git --version
git version 1.8.3.1
```

### g++をインストール

[参考](https://sioyaki.com/entry/2017/04/10/101610)

``` :CentOSコンソール
$ sudo yum install gcc-c++
$ g++ --version
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
```

---

## Install Samples, Binaries and Docker Images

[公式](https://hyperledger-fabric.readthedocs.io/en/release-1.4/install.html)

- サンプルのレポジトリのダウンロード
- dockerimageの取得
- 
``` :CentOSコンソール
$ cd <任意の場所> 
$ curl -sSL http://bit.ly/2ysbOFE | bash -s -- 1.4.3
$ ll
drwxrwxr-x.  5 centos centos   53 10月 10 03:30 ./
drwx------.  8 centos centos  201 10月 10 03:26 ../
drwxrwxr-x.  2 centos centos  147  8月 26 21:30 bin/
drwxrwxr-x.  2 centos centos   64  8月 26 21:35 config/
drwxrwxr-x. 18 centos centos 4096 10月 10 03:30 fabric-samples/     ←追加されていることを確認

$ docker images -a     ←dockerイメージが追加されていることを確認
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
hyperledger/fabric-tools       1.4.3               18ed4db0cd57        6 weeks ago         1.55GB
hyperledger/fabric-tools       latest              18ed4db0cd57        6 weeks ago         1.55GB
hyperledger/fabric-ca          1.4.3               c18a0d3cc958        6 weeks ago         253MB
hyperledger/fabric-ca          latest              c18a0d3cc958        6 weeks ago         253MB
hyperledger/fabric-ccenv       1.4.3               3d31661a812a        6 weeks ago         1.45GB
hyperledger/fabric-ccenv       latest              3d31661a812a        6 weeks ago         1.45GB
hyperledger/fabric-orderer     1.4.3               b666a6ebbe09        6 weeks ago         173MB
hyperledger/fabric-orderer     latest              b666a6ebbe09        6 weeks ago         173MB
hyperledger/fabric-peer        1.4.3               fa87ccaed0ef        6 weeks ago         179MB
hyperledger/fabric-peer        latest              fa87ccaed0ef        6 weeks ago         179MB
hyperledger/fabric-javaenv     1.4.3               5ba5ba09db8f        2 months ago        1.76GB
hyperledger/fabric-javaenv     latest              5ba5ba09db8f        2 months ago        1.76GB
hyperledger/fabric-zookeeper   0.4.15              20c6045930c8        6 months ago        1.43GB
hyperledger/fabric-zookeeper   latest              20c6045930c8        6 months ago        1.43GB
hyperledger/fabric-kafka       0.4.15              b4ab82bbaf2f        6 months ago        1.44GB
hyperledger/fabric-kafka       latest              b4ab82bbaf2f        6 months ago        1.44GB
hyperledger/fabric-couchdb     0.4.15              8de128a55539        6 months ago        1.5GB
hyperledger/fabric-couchdb     latest              8de128a55539        6 months ago        1.5GB
hello-world                    latest              fce289e99eb9        9 months ago        1.84kB
```

## Building Your First Network
[公式](https://hyperledger-fabric.readthedocs.io/en/release-1.4/build_network.html)

とりあえず動くことを確認

``` :CentOSコンソール
$ cd fabric-samples/first-network
$ ./byfn.sh generate
$ ./byfn.sh up

...
...
 ____    _____      _      ____    _____
/ ___|  |_   _|    / \    |  _ \  |_   _|
\___ \    | |     / _ \   | |_) |   | |
 ___) |   | |    / ___ \  |  _ <    | |
|____/    |_|   /_/   \_\ |_| \_\   |_|
...
...
 _____   _   _   ____
| ____| | \ | | |  _ \
|  _|   |  \| | | | | |
| |___  | |\  | | |_| |
|_____| |_| \_| |____/


$ ./byfn.sh down

```

## fabcarを動かしてみる

### 立ち上げ
[参考](https://qiita.com/yoshihiro-nnnn/items/a9db142f59f606977b11)

``` :CentOSコンソール
$ cd fabric-samples/fabcar
$ ll
drwxrwxr-x.  6 centos centos  122 10月 10 03:30 ./
drwxrwxr-x. 18 centos centos 4096 10月 10 03:30 ../
-rw-rw-r--.  1 centos centos   50 10月 10 03:30 .gitignore
drwxrwxr-x.  4 centos centos   64 10月 10 03:30 java/
drwxrwxr-x.  3 centos centos  198 10月 10 03:30 javascript/
drwxrwxr-x.  2 centos centos  122 10月 10 03:30 javascript-low-level/
-rwxrwxr-x.  1 centos centos 7777 10月 10 03:30 startFabric.sh*
drwxrwxr-x.  4 centos centos  130 10月 10 03:30 typescript/

$ ./startFabric.sh

...
...
Total setup execution time : 99 secs ...
...
...


$ cd javascript
$ npm install
```


### fabcarのSDKを動かす
[参考](https://qiita.com/yoshihiro-nnnn/items/a9db142f59f606977b11)

``` :CentOSコンソール
$ pwd
<...>/fabric-samples/fabcar/javascript

$ ll
drwxrwxr-x.   4 centos centos    243 10月 10 04:50 ./
drwxrwxr-x.   6 centos centos    147 10月 10 04:40 ../
-rwxrwxr-x.   1 centos centos    231 10月 10 03:30 .editorconfig*
-rw-rw-r--.   1 centos centos     52 10月 10 03:30 .eslintignore
-rw-rw-r--.   1 centos centos    945 10月 10 03:30 .eslintrc.js
-rw-rw-r--.   1 centos centos   1145 10月 10 03:30 .gitignore
-rw-rw-r--.   1 centos centos   1867 10月 10 03:30 enrollAdmin.js      ←これから使う
-rw-rw-r--.   1 centos centos   1959 10月 10 03:30 invoke.js           ←これから使う
drwxrwxr-x. 260 centos centos   8192 10月 10 04:50 node_modules/
-rw-rw-r--.   1 centos centos 188752 10月 10 04:50 package-lock.json
-rw-rw-r--.   1 centos centos   1027 10月 10 03:30 package.json
-rw-rw-r--.   1 centos centos   1841 10月 10 03:30 query.js            ←これから使う
-rw-rw-r--.   1 centos centos   2348 10月 10 03:30 registerUser.js     ←これから使う
drwxrwxr-x.   2 centos centos     22 10月 10 03:30 wallet/

$ npm rebuild  (いらないかも？)

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

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

$ ll wallet
drwxrwxr-x. 4 centos centos  48 10月 10 04:56 ./
drwxrwxr-x. 4 centos centos 243 10月 10 04:50 ../
-rw-rw-r--. 1 centos centos   0 10月 10 03:30 .gitkeep
drwxrwxr-x. 2 centos centos 249 10月 10 04:56 admin/     ←存在することを確認
drwxrwxr-x. 2 centos centos 172 10月 10 04:56 user1/     ←存在することを確認

$ node query.js
Wallet path: /home/centos/fabricarea/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/centos/fabricarea/fabric-samples/fabcar/javascript/wallet
Transaction has been submitted

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

```

ここまでで、fabcarシステムを構築、invoke,queryのSDK実行が完了。

ちなみにqueryの結果を整形すると以下
- 1レコードはこんな感じ  
{
&ensp;"Key":"CAR0",
&ensp;"Record":&ensp;{
&ensp;&ensp;&ensp;"colour":"blue",
&ensp;&ensp;&ensp;"make":"Toyota",
&ensp;&ensp;&ensp;"model":"Prius",
&ensp;&ensp;&ensp;"owner":"Tomoko"
&ensp;}
}

- 表にまとめるとこんな感じ  

| Key  | colour | make       |model    |owner    |
|:-----|:-------|------------|:--------|:--------|
| CAR0 | blue   | Toyota     | Prius   | Tomoko  |
| CAR1 | red    | Ford       | Mustang | Brad    |
| __CAR12__| Black  | Honda      | Accord  | Tom     |
| CAR2 | green  | Hyundai    | Tucson  | Jin Soo |
| CAR3 | yellow | Volkswagen | Passat  | Max     |
| CAR4 | black  | Tesla      | S       | Adriana |
| CAR5 | purple | Peugeot    | 205     | Michel  |
| CAR6 | white  | Chery      | S22L    | Aarav   |
| CAR7 | violet | Fiat       | Punto   | Pari    |
| CAR8 | indigo | Tata       | Nano    | Valeria |
| CAR9 | brown  | Holden     | Barina  | Shotaro |

※CAR12がinvokeで追加された車


### fabcarシステムを落とす
``` :CentOSコンソール
$ cd first-network
$ ./byfn.sh down

$ cd basic-network
$ ./teardown.sh

$ docker ps -a    ←コンテナが削除されていることを確認
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
```

<br>
以上