fabric
Blockchain
Hyperledger

動画コース 「Zero to Blockchain」(Hyperledger Fabric) に、ローカル開発環境を簡単に作れるシェルがあったので試してみた

はじめに

IBMのRedbookサイトYouTubeZero to Blockchainという無料の動画コースが公開中です。 (両者は同じものです) 英語ですが、最新版のHyperledger Fabric V1を文字通り「ゼロ=何もしらん状態」から学べるコースです。
image

  • 全15章で現在12章まで公開されています
  • 動画で使ったプレゼン資料やコード一式はGithub上に公開されていますので実際に手を動かして学べます
  • (ちと怪しげに翻訳された:sweat_smile:日本語版のpdf資料もありました)

image

このコースの第三章「Set up local Hyperledger Fabric V1 development environment」にローカル環境にHyperledger Fabric V1 の開発環境をメチャ簡単に構築できるスグレモノのシェルがあったのでご紹介します。

  • Ubuntuの場合、OS導入後に基本的には2つシェル実行するだけでDockerからNode.jsからサンプルからComposerまで、全部入れてくれます
  • Hyperledger Fabricの本家のDockerイメージからpullしていますので、「本物」です。(お勉強用のプロプラエタリなDockerイメージというわけではありません)
  • (筆者はやってませんが)Windows/Macでも同様の手順が用意されています
  • ちなみにHyperledger Fabricの本家サイトのGetting Startedにも環境構築の手順があれこれ書いてありますが、要はこれをすごく簡単に実行するもの、と思っていいと思います

環境構築の流れ

以下のように、非常に簡単です。Dockerイメージをpullするので所要時間はネットワーク環境次第かと思いますが、筆者の環境では30-40分くらいで終わりました。

  1. Zero to BlockchainのGithubからリソース一式をゲットする
  2. OS(今回はUbuntu 16.04)を素から導入する
  3. 前提となるパッケージを4つ apt-get installする
  4. 一番目のシェルを実行する(Docker/nodeなど基盤部分のインストール)
  5. リブートする
  6. 二番目のシェルを実行する(FabricのDockerイメージのインストール)
  7. インストールの確認

やってみた

では、やってみましょう。

1.Githubからリソースをゲットする

Githubの当コースのリポジトリー一式をgit cloneかzipでダウンロードします。
Chapter 03のDocumentationに細かい説明が書いてあるPDFがあります。Ubuntuの場合は当記事のコマンドのコピペでも十分かと思いますが、WindowsやMACの場合はpdfを見て事前作業やコマンドをチェックください。

なお、別環境でダウンロードした場合は、リソース一式を導入対象マシンにコピーしておいてください。(最後のテストで使います)

2. OS(今回はUbuntu 16.04)を素から導入する

筆者は以下の環境を準備しました

  • Windows 7上のVMWare WorkstationにUbuntu 16.04 Server版を一から導入
  • Ubuntu上の操作は管理者特権を持ったID(筆者の場合はishida)で実施
  • :warning: もしIDをrootで導入したい場合は追加の操作が必要です。当記事末尾をご参照ください

3. 前提となるパッケージを4つ apt-get installする

OSを素で導入したら、以下を事前に導入しておきます。( Ubuntuに入ってなくて何度か失敗したんで、、、そこからの学び:smirk:

プログラム 理由
openssh-server 単にputty使いたかったから
unzip 途中で「無い」と怒られた
python(2.7) 途中で「無い」と怒られた
curl 作業の前提として文書に記載あり
事前準備
ishida@ubuntu:~$ sudo apt-get install -y openssh-server

ishida@ubuntu:~$ which unzip
ishida@ubuntu:~$ sudo apt-get install -y unzip

ishida@ubuntu:~$ which python
ishida@ubuntu:~$ sudo apt-get install -y python
ishida@ubuntu:~$ python -V
Python 2.7.12

ishida@ubuntu:~$ which curl
ishida@ubuntu:~$ sudo apt-get install -y curl

4. 一番目のシェルを実行する

putty/sshでログインして以下のコマンドを入力。10分くらい待ちます。(※コマンドはpdf資料に細かく書いてありますが、ubuntuの方で読むのが面倒なら以下のコピペでOKです)

Part_1.sh
ishida@ubuntu:~$ curl -H 'Accept: application/vnd.github.v3.raw' https://raw.githubusercontent.com/rddill-IBM/ZeroToBlockchain/master/setup_Ubuntu_Part_1.sh | bash

~~~(途中省略)~~~
       =====================================================
       -----> installation Part 1 complete
       =====================================================
       =====================================================
       ----->  Reboot is required prior to executing step 2
       =====================================================

5. リブートする

普通にリブートします

6. 二番目のシェルを実行する

以下のコマンドを入力して、10-15分くらい待ちます。
(基本、一番目のコマンドのPart_1.shがPart2.shになっただけ)

Part_2.sh
ishida@ubuntu:~$ curl -H 'Accept: application/vnd.github.v3.raw' https://raw.githubusercontent.com/rddill-IBM/ZeroToBlockchain/master/setup_Ubuntu_Part_2.sh | bash

~~~(途中省略)~~~

Hyperledger Composer PeerAdmin card has been imported
The following Business Network Cards are available:

Connection Profile: hlfv1
┌─────────────────┬───────────┬──────────────────┐
│ Card Name       │ UserId    │ Business Network │
├─────────────────┼───────────┼──────────────────┤
│ PeerAdmin@hlfv1 │ PeerAdmin │                  │
└─────────────────┴───────────┴──────────────────┘


Issue composer card list --name <Card Name> to get details a specific card

Command succeeded

[sudo] password for ishida:
       =====================================================
       -----> Copying PeerAdmin Credentials
       =====================================================
'/home/ishida/fabric-tools/fabric-scripts/hlfv1/composer/creds/114aab0e76bf0c78308f89efc4b8c9423e31568da0c340ca187a9b17aa9a4457-priv' -> '/home/ishida/.hfc-key-store/114aab0e76bf0c78308f89efc4b8c9423e31568da0c340ca187a9b17aa9a4457-priv'
'/home/ishida/fabric-tools/fabric-scripts/hlfv1/composer/creds/114aab0e76bf0c78308f89efc4b8c9423e31568da0c340ca187a9b17aa9a4457-pub' -> '/home/ishida/.hfc-key-store/114aab0e76bf0c78308f89efc4b8c9423e31568da0c340ca187a9b17aa9a4457-pub'
'/home/ishida/fabric-tools/fabric-scripts/hlfv1/composer/creds/PeerAdmin' -> '/home/ishida/.hfc-key-store/PeerAdmin'
'/home/ishida/fabric-tools/fabric-scripts/hlfv1/composer/creds/PeerAdmin.cert' -> '/home/ishida/.hfc-key-store/PeerAdmin.cert'
       =====================================================
       -----> installation complete
       =====================================================
ishida@ubuntu:~$

これでHyperledger Fabricの開発環境が出来ました。簡単すぎ!

7. インストールの確認

これだけでは簡単すぎて拍子抜けですので、インストールがうまくいったのか、確認しましょう。Chapter03ディレクトリにインストール確認用のnode.jsアプリケーションがあります。npm installで必要なモジュールをインストールして、実行権限付与後、シェル buildAndDeployを実行します。

:warning: 記事執筆時点(2017/12/11)では最後のcomposer-rest-serverの起動が失敗しますので、実行前にstart_rest_server.shの末尾一行を下記に書き替えてください。

ネットワークがfabricが起動する様子が見られます。(とはいえ、これだけでは「なんか動いたけど、なんのことやらよくわからん」と思います。より深いご理解のために同コースの第四章以降をご視聴ください)

start_rest_server.sh
# 変更前-> -nパラメータが無効、と言われる
#composer-rest-server -p hlfv1 -n zerotoblockchain-network -i admin -s adminPW
# 変更後
composer-rest-server -c admin@zerotoblockchain-network -n always -w true

ちと長いですが実行結果を掲載します。

buildAndDeploy
ishida@ubuntu:~$ cd ZeroToBlockchain/Chapter03
ishida@ubuntu:~/ZeroToBlockchain/Chapter03$ npm install
ishida@ubuntu:~/ZeroToBlockchain/Chapter03$ chmod 775 *.sh
buildAndDeploy
ishida@ubuntu:~/ZeroToBlockchain/Chapter03$
ishida@ubuntu:~/ZeroToBlockchain/Chapter03$ ./buildAndDeploy

       network archive, start and deploy script for the Zero To Blockchain Series
       This has been tested on Mac OSX thru High Sierra and Ubuntu V16 LTS
       This script will create your Composer archive

Parameters:
       Network Name is:  zerotoblockchain-network
       =====================================================
       -----> creating archive
       =====================================================
       =====================================================
       -----> option passed for network name is: 'zerotoblockchain-network'
       =====================================================

       archive creation script for the Zero To Blockchain Series
       This has been tested on Mac OSX thru High Sierra and Ubuntu V16 LTS
       This script will create your Composer archive

Parameters:
       Network Name is:  zerotoblockchain-network
       =====================================================
       -----> creating archive
       =====================================================
Creating Business Network Archive


Looking for package.json of Business Network Definition
        Input directory: /home/ishida/ZeroToBlockchain/Chapter03/network

Found:
        Description: Zero to Blockchain tutorial network
        Name: zerotoblockchain-network
        Identifier: zerotoblockchain-network@0.1.5

Written Business Network Definition Archive file to
        Output file: ./dist/zerotoblockchain-network.bna

Command succeeded

       =====================================================
       -----> starting network
       =====================================================
       =====================================================
       -----> using execs from previous installation, stored in /home/ishida/fabric-tools
       =====================================================
       =====================================================
       -----> starting fabric
       =====================================================
Development only script for Hyperledger Fabric control
Running 'startFabric.sh'
FABRIC_VERSION is set to 'hlfv1'
FABRIC_START_TIMEOUT is unset, assuming 15 (seconds)

#Detect architecture
ARCH=`uname -m`
uname -m

# Grab the current directory
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
 cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd
 dirname "${BASH_SOURCE[0]}"

#

ARCH=$ARCH docker-compose -f "${DIR}"/composer/docker-compose.yml down
Removing peer0.org1.example.com ... done
Removing ca.org1.example.com    ... done
Removing couchdb                ... done
Removing orderer.example.com    ... done
Removing network composer_default
ARCH=$ARCH docker-compose -f "${DIR}"/composer/docker-compose.yml up -d
Creating network "composer_default" with the default driver
Creating orderer.example.com ...
Creating ca.org1.example.com ...
Creating couchdb ...
Creating ca.org1.example.com
Creating orderer.example.com
Creating couchdb ... done
Creating peer0.org1.example.com ...
Creating peer0.org1.example.com ... done

# wait for Hyperledger Fabric to start
# incase of errors when running later commands, issue export FABRIC_START_TIMEOUT=<larger number>
echo ${FABRIC_START_TIMEOUT}
15
sleep ${FABRIC_START_TIMEOUT}

# Create the channel
docker exec peer0.org1.example.com peer channel create -o orderer.example.com:7050 -c composerchannel -f /etc/hyperledger/configtx/composer-channel.tx
2017-12-11 06:13:09.075 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2017-12-11 06:13:09.075 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2017-12-11 06:13:09.079 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized
2017-12-11 06:13:09.081 UTC [msp] GetLocalMSP -> DEBU 004 Returning existing local MSP
2017-12-11 06:13:09.081 UTC [msp] GetDefaultSigningIdentity -> DEBU 005 Obtaining default signing identity
2017-12-11 06:13:09.081 UTC [msp] GetLocalMSP -> DEBU 006 Returning existing local MSP
2017-12-11 06:13:09.081 UTC [msp] GetDefaultSigningIdentity -> DEBU 007 Obtaining default signing identity
2017-12-11 06:13:09.081 UTC [msp/identity] Sign -> DEBU 008 Sign: plaintext: 0A8C060A074F7267314D53501280062D...6D706F736572436F6E736F727469756D
2017-12-11 06:13:09.081 UTC [msp/identity] Sign -> DEBU 009 Sign: digest: 3F5C851F08CF3243BF3D21A8AEA907F8A2355EE49BA9577442A255699CB89BA4
2017-12-11 06:13:09.081 UTC [msp] GetLocalMSP -> DEBU 00a Returning existing local MSP
2017-12-11 06:13:09.081 UTC [msp] GetDefaultSigningIdentity -> DEBU 00b Obtaining default signing identity
2017-12-11 06:13:09.081 UTC [msp] GetLocalMSP -> DEBU 00c Returning existing local MSP
2017-12-11 06:13:09.082 UTC [msp] GetDefaultSigningIdentity -> DEBU 00d Obtaining default signing identity
2017-12-11 06:13:09.082 UTC [msp/identity] Sign -> DEBU 00e Sign: plaintext: 0AC9060A1B08021A0608F5C3B8D10522...0C1B31B7452B2BF24D990155A556FBB7
2017-12-11 06:13:09.082 UTC [msp/identity] Sign -> DEBU 00f Sign: digest: 6739BBB606A911B35064361483CCBB22110290714083B7577EF07A0541333A13
2017-12-11 06:13:09.152 UTC [msp] GetLocalMSP -> DEBU 010 Returning existing local MSP
2017-12-11 06:13:09.152 UTC [msp] GetDefaultSigningIdentity -> DEBU 011 Obtaining default signing identity
2017-12-11 06:13:09.152 UTC [msp] GetLocalMSP -> DEBU 012 Returning existing local MSP
2017-12-11 06:13:09.152 UTC [msp] GetDefaultSigningIdentity -> DEBU 013 Obtaining default signing identity
2017-12-11 06:13:09.152 UTC [msp/identity] Sign -> DEBU 014 Sign: plaintext: 0AC9060A1B08021A0608F5C3B8D10522...ABDF5AADFB4C12080A021A0012021A00
2017-12-11 06:13:09.152 UTC [msp/identity] Sign -> DEBU 015 Sign: digest: AE5A58F1062C6D3DDA0017873D561BABE0DEE1226FCC080983976E12BBCE069E
2017-12-11 06:13:09.154 UTC [channelCmd] readBlock -> DEBU 016 Got status: &{NOT_FOUND}
2017-12-11 06:13:09.154 UTC [msp] GetLocalMSP -> DEBU 017 Returning existing local MSP
2017-12-11 06:13:09.154 UTC [msp] GetDefaultSigningIdentity -> DEBU 018 Obtaining default signing identity
2017-12-11 06:13:09.157 UTC [channelCmd] InitCmdFactory -> INFO 019 Endorser and orderer connections initialized
2017-12-11 06:13:09.358 UTC [msp] GetLocalMSP -> DEBU 01a Returning existing local MSP
2017-12-11 06:13:09.358 UTC [msp] GetDefaultSigningIdentity -> DEBU 01b Obtaining default signing identity
2017-12-11 06:13:09.358 UTC [msp] GetLocalMSP -> DEBU 01c Returning existing local MSP
2017-12-11 06:13:09.358 UTC [msp] GetDefaultSigningIdentity -> DEBU 01d Obtaining default signing identity
2017-12-11 06:13:09.358 UTC [msp/identity] Sign -> DEBU 01e Sign: plaintext: 0AC9060A1B08021A0608F5C3B8D10522...8B0D0E4B69D112080A021A0012021A00
2017-12-11 06:13:09.358 UTC [msp/identity] Sign -> DEBU 01f Sign: digest: 720046FF437CA8EEF1646ACC85AE39CFEC0C5EF7E22D9A957BE3DA0A2257F278
2017-12-11 06:13:09.362 UTC [channelCmd] readBlock -> DEBU 020 Received block: 0
2017-12-11 06:13:09.363 UTC [main] main -> INFO 021 Exiting.....

# Join peer0.org1.example.com to the channel.
docker exec -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.example.com/msp" peer0.org1.example.com peer channel join -b composerchannel.block
2017-12-11 06:13:09.561 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2017-12-11 06:13:09.561 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2017-12-11 06:13:09.572 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized
2017-12-11 06:13:09.573 UTC [msp/identity] Sign -> DEBU 004 Sign: plaintext: 0A8A070A5C08011A0C08F5C3B8D10510...789F78C6D34F1A080A000A000A000A00
2017-12-11 06:13:09.574 UTC [msp/identity] Sign -> DEBU 005 Sign: digest: 1DF5A1EDFE3133C9C50D24B41B779D12A0DAA0A5F32FA3611D9FD28373981018
2017-12-11 06:13:09.689 UTC [channelCmd] executeJoin -> INFO 006 Peer joined the channel!
2017-12-11 06:13:09.689 UTC [main] main -> INFO 007 Exiting.....
       =====================================================
       -----> creating new PeerAdmin card (required with each restart)
       =====================================================
Development only script for Hyperledger Fabric control
Running 'createPeerAdminCard.sh'
FABRIC_VERSION is set to 'hlfv1'
FABRIC_START_TIMEOUT is unset, assuming 15 (seconds)

Using composer-cli at v0.16.0
Deleted Business Network Card: PeerAdmin@hlfv1

Command succeeded


Successfully created business network card file to
        Output file: /tmp/PeerAdmin@hlfv1.card

Command succeeded


Successfully imported business network card
        Card file: /tmp/PeerAdmin@hlfv1.card
        Card name: PeerAdmin@hlfv1

Command succeeded

Hyperledger Composer PeerAdmin card has been imported
The following Business Network Cards are available:

Connection Profile: hlfv1
┌────────────────────────────────┬───────────┬──────────────────────────┐
│ Card Name                      │ UserId    │ Business Network         │
├────────────────────────────────┼───────────┼──────────────────────────┤
│ PeerAdmin@hlfv1                │ PeerAdmin │                          │
├────────────────────────────────┼───────────┼──────────────────────────┤
│ admin@zerotoblockchain-network │ admin     │ zerotoblockchain-network │
└────────────────────────────────┴───────────┴──────────────────────────┘


Issue composer card list --name <Card Name> to get details a specific card

Command succeeded

userName:            PeerAdmin
description:
businessNetworkName:
roles:
  - PeerAdmin
  - ChannelAdmin
connectionProfile:
  name:    hlfv1
  type:    hlfv1
  channel: composerchannel
secretSet:           No secret set
credentialsSet:      Credentials set

Command succeeded

       =====================================================
       -----> start up complete
       =====================================================
       =====================================================
       -----> deploying network
       =====================================================
       =====================================================
       -----> option passed for network name is: 'zerotoblockchain-network'
       =====================================================

       network deploy script for the Zero To Blockchain Series
       This has been successfully tested on OSX Sierra and Ubuntu 16.04
       This script will create your Composer archive

Parameters:
       Network Name is:  zerotoblockchain-network
       =====================================================
       -----> deploying network
       =====================================================
       =====================================================
       -----> installing PeerAdmin card
       =====================================================
✔ Installing runtime for business network zerotoblockchain-network. This may take a minute...

Command succeeded

       =====================================================
       -----> starting network
       =====================================================
Starting business network from archive: zerotoblockchain-network.bna
Business network definition:
        Identifier: zerotoblockchain-network@0.1.5
        Description: Zero to Blockchain tutorial network

Processing these Network Admins:
        userName: admin

✔ Starting business network definition. This may take a minute...
Successfully created business network card:
        Filename: networkadmin.card

Command succeeded

       =====================================================
       -----> importing networkadmin card
       =====================================================
Deleted Business Network Card: admin@zerotoblockchain-network

Command succeeded


Successfully imported business network card
        Card file: networkadmin.card
        Card name: admin@zerotoblockchain-network

Command succeeded

       =====================================================
       -----> pinging admin@zerotoblockchain-network card
       =====================================================
The connection to the network was successfully tested: zerotoblockchain-network
        version: 0.16.0
        participant: org.hyperledger.composer.system.NetworkAdmin#admin

Command succeeded

       =====================================================
       -----> starting REST server
       =====================================================

       network archive, start and deploy script for the Zero To Blockchain Series
       This has been tested on Mac OSX thru High Sierra and Ubuntu V16 LTS
       This script will create your Composer archive

Parameters:
       Network Name is:  zerotoblockchain-network
       =====================================================
       -----> testing rest server
        when this completes,
         go to your favorite browser
        and enter localhost:3000/explorer
       =====================================================
Discovering types from business network definition ...
Discovered types from business network definition
Generating schemas for all types in business network definition ...
Generated schemas for all types in business network definition
Adding schemas for all types to Loopback ...
Added schemas for all types to Loopback
Web server listening at: http://localhost:3000
Browse your REST API at http://localhost:3000/explorer

3000番ポートを叩くとAPI Explorerが表示されます。
image

dockerも色々動いてますね。

docker_ps
ishida@ubuntu:~$ docker ps
CONTAINER ID        IMAGE                                                                                                                         COMMAND                  CREATED             STATUS              PORTS                                            NAMES
df519c21ceda        dev-peer0.org1.example.com-zerotoblockchain-network-0.16.0-d38b9c0aced7122f4e412a504feda921dfa0c092dbe1b0d057585fed66d11577   "chaincode -peer.a..."   16 minutes ago      Up 16 minutes                                                        dev-peer0.org1.example.com-zerotoblockchain-network-0.16.0
3afe30f429fb        hyperledger/fabric-peer:x86_64-1.0.4                                                                                          "peer node start -..."   16 minutes ago      Up 16 minutes       0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp   peer0.org1.example.com
aa48bb091b39        hyperledger/fabric-couchdb:x86_64-1.0.4                                                                                       "tini -- /docker-e..."   16 minutes ago      Up 16 minutes       4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp       couchdb
d6bfe28ad82c        hyperledger/fabric-orderer:x86_64-1.0.4                                                                                       "orderer"                16 minutes ago      Up 16 minutes       0.0.0.0:7050->7050/tcp                           orderer.example.com
3041bd9d5e5c        hyperledger/fabric-ca:x86_64-1.0.4                                                                                            "sh -c 'fabric-ca-..."   16 minutes ago      Up 16 minutes       0.0.0.0:7054->7054/tcp                           ca.org1.example.com

最後に

このように、簡単にHyperledger Fabricの開発環境が手に入りました!
この後のコースではFabric composerの使いかたなど、本格的に動画で解説してくれていますので、続きもぜひご視聴くださいませ :smiley:

:warning: 導入前: もしIDをrootで導入したい場合は

事前にnpmを自分でインストールし、オプション unsafe-permをtrueに設定(※)しておきます。 これをしないとStep1の hyperledger composerの導入近辺で以下のようにpermission deniedで怒られます。

※unsafe-permのデフォルトは非rootではtrue、rootではfalse、と違いがあります。trueでないといけないようです。

image

以下のようにセットします。

root@ubuntu:~# apt-get install -y npm
root@ubuntu:~# npm -v
3.5.2
root@ubuntu:~# npm config get unsafe-perm
false
root@ubuntu:~# npm config set unsafe-perm true
root@ubuntu:~# npm config get unsafe-perm
true

以上です。