hyperledger/fabric-samplesのbalance-transferをUbuntu16.04で動かしてみた

in #hyperledger6 years ago (edited)

0_4MfnPLogMm19IVbf.png

■Premise

こちらの環境ができていることが必要です。

https://steemit.com/hyperledger/@daidai/hyperledger-fabric1-0-ubuntu16-04

■Overview

今回はHyperledger Fabricで使用できるSamplesを利用していきます。Fabric-client&fabric-ca-clientの Node.js SDK APIのデモをできるbalance-transferを動かしてみましょう。

hyperledger/fabric-samplesRead-only mirror of https://gerrit.hyperledger.org/r/#/admin/github.com

■Install

・ubuntu以下にclone
cd /home/ubuntu
git clone https://github.com/hyperledger/fabric-samples.git

・今回は安定しているfablic-sample1.0を利用していきます。
git checkout release-1.0

・release-1.0に切り替わっているか確認
git branch

■Test

dockerコンテナを起動してみましょう。

・balance-transferに移動
cd /home/ubuntu/fabric-samples/balance-transfer

・dockerコンテナを起動
./runApp.sh

Stopping peer0.org2.example.com ... done

Stopping peer1.org1.example.com ... done
Stopping peer0.org1.example.com ... done
Stopping peer1.org2.example.com ... done
Stopping orderer.example.com ... done
Stopping ca_peerOrg1 ... done
Stopping ca_peerOrg2 ... done
Removing peer0.org2.example.com ... done
Removing peer1.org1.example.com ... done
Removing peer0.org1.example.com ... done
Removing peer1.org2.example.com ... done
Removing orderer.example.com ... done
Removing ca_peerOrg1 ... done
Removing ca_peerOrg2 ... done
Removing network artifacts_default
5fdebe000332
c81d7de17ea4
Untagged: dev-peer0.org1.example.com-mycc-v0:latest
Deleted: sha256:263eded73f20bf6a61a465ec78892736057aee9e324a12bb03048c03bff24aea
Deleted: sha256:1aafcced47e3975f5720a6d727376713fed700a4dc4c24cbc666b67dd413e0b8
Deleted: sha256:dc33bef3d7b7953294d322a0a46dd823a82737e7000858115ccbfa7403a5869b
Deleted: sha256:6f690e23338df33759e289750d11c899c87c1b73bd430c5052c81d8e7fc292b3
Deleted: sha256:60cbb1ba70185f11e3304bd038fcb5ef5eee021d77d460e1c95fb0f63213d1ba
Untagged: dev-peer1.org1.example.com-mycc-v0:latest
Deleted: sha256:be0d3fbea4d4c5b0bc4774940cf33a19d756b35b46d5304284ad76283e91d2c2
Deleted: sha256:61cbf1e7af38d46e58e7b04d1040e147b49f157eba59d919ea2aeca60b6428a8
Deleted: sha256:695cec95a57609d496f0f33618dd6ed424cb2855289696fda207537a5979907b
Deleted: sha256:a509e47fece5f10a5649b0c2c5783e4cba118d3cec1f4da30083351bb4b30bd5
Deleted: sha256:76e0362584126e401e0f75a40e44c077c791c45c1b83013bfd116b502020455f
Deleted: sha256:331306418302dd16d7bfd9c64b4b0469f5a40f695b2c56393b3df664bdc0486d
Deleted: sha256:465bd365fc600c0c74acabf00903ab0c64f788277d2dfa03e8efa040e39978f9
Creating network "artifacts_default" with the default driver
Creating ca_peerOrg2
Creating ca_peerOrg1
Creating orderer.example.com
Creating peer1.org1.example.com
Creating peer1.org2.example.com
Creating peer0.org2.example.com
Creating peer0.org1.example.com
/home/ubuntu/fabric-samples_good/balance-transfer
============== node modules installed already =============
[2018-04-25 08:38:27.499] [DEBUG] Helper - [crypto_ecdsa_aes]: constructor, keySize: 256
[2018-04-25 08:38:27.510] [DEBUG] Helper - [crypto_ecdsa_aes]: Hash algorithm: SHA2, hash output size: 256
[2018-04-25 08:38:27.599] [DEBUG] Helper - [utils.CryptoKeyStore]: CryptoKeyStore, constructor - start
[2018-04-25 08:38:27.601] [DEBUG] Helper - [utils.CryptoKeyStore]: constructor, no super class specified, using config:
[2018-04-25 08:38:27.610] [DEBUG] Helper - [crypto_ecdsa_aes]: constructor, keySize: 256
[2018-04-25 08:38:27.610] [DEBUG] Helper - [crypto_ecdsa_aes]: Hash algorithm: SHA2, hash output size: 256
[2018-04-25 08:38:27.611] [DEBUG] Helper - [utils.CryptoKeyStore]: CryptoKeyStore, constructor - start
[2018-04-25 08:38:27.611] [DEBUG] Helper - [utils.CryptoKeyStore]: constructor, no super class specified, using config:
[2018-04-25 08:38:27.629] [INFO] SampleWebApp - ****************** SERVER STARTED ************************
[2018-04-25 08:38:27.630] [INFO] SampleWebApp - ************** http://localhost:4000 ******************

これで必要なコンテナが立ち上がり、Nodeの起動もされたようですね。

peerが4台、orderが1台、caが2台起動しているか確認します。

docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3cb42658a129 hyperledger/fabric-peer "peer node start" 35 seconds ago Up 33 seconds 0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp peer0.org2.example.com
9ea3e72865b0 hyperledger/fabric-peer "peer node start" 35 seconds ago Up 33 seconds 0.0.0.0:8056->7051/tcp, 0.0.0.0:8058->7053/tcp peer1.org2.example.com
0be86fa272b4 hyperledger/fabric-peer "peer node start" 35 seconds ago Up 32 seconds 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp peer0.org1.example.com
6dc8d06e8857 hyperledger/fabric-peer "peer node start" 35 seconds ago Up 33 seconds 0.0.0.0:7056->7051/tcp, 0.0.0.0:7058->7053/tcp peer1.org1.example.com
da020b22301b hyperledger/fabric-orderer "orderer" 35 seconds ago Up 34 seconds 0.0.0.0:7050->7050/tcp orderer.example.com
9960c169bf7d hyperledger/fabric-ca "sh -c 'fabric-ca-se…" 35 seconds ago Up 32 seconds 0.0.0.0:8054->7054/tcp ca_peerOrg2
9d02cc8923c5 hyperledger/fabric-ca "sh -c 'fabric-ca-se…" 35 seconds ago Up 34 seconds 0.0.0.0:7054->7054/tcp ca_peerOrg1

では実際に新しいユーザーを登録していきましょう。

curl -s -X POST http://localhost:4000/users -H "content-type: application/x-www-form-urlencoded" -d 'username=Jim&orgName=Org1'

これでtokenが生成ができました。このtokenはchannelの作成やchaincodeのインストール等に利用するのでコピーしておく必要があります。

{
"success": true,
"secret": "RaxhMgevgJcm",
"message": "Jim enrolled Successfully",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MjQ2NDk1NjEsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE1MjQ2MTM1NjF9.q0oSwEkFdx8KkWk4XR-oYkXxTdqPpjtUorTfvrDu1ME"
}

この後、以下のことをしていきます。

・channelの作成、参加

・chaincodeのインストール、インスタンス化

一々コマンドを流すのはめんどくさいですよね。

すべてで実行してくれるシェルが入っているのでそちらを実行していきましょう。

・シェルを実行
./testApis.sh

{"success":true,"secret":"","message":"Jim enrolled Successfully","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MjQ2NTA0NzIsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE1MjQ2MTQ0NzJ9._ZEYFpASwnnrd0NLcbvz5-b78bThwDXKcVgXjbS6000"}
ORG1 token is eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MjQ2NTA0NzIsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE1MjQ2MTQ0NzJ9._ZEYFpASwnnrd0NLcbvz5-b78bThwDXKcVgXjbS6000
POST request Enroll on Org2 ...
{"success":true,"secret":"","message":"Barry enrolled Successfully","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MjQ2NTA0NzIsInVzZXJuYW1lIjoiQmFycnkiLCJvcmdOYW1lIjoib3JnMiIsImlhdCI6MTUyNDYxNDQ3Mn0.3W04WOjFalYV_XSEYq4hrGjgtv7-O8mMdW0BEqty4Rk"}
ORG2 token is eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MjQ2NTA0NzIsInVzZXJuYW1lIjoiQmFycnkiLCJvcmdOYW1lIjoib3JnMiIsImlhdCI6MTUyNDYxNDQ3Mn0.3W04WOjFalYV_XSEYq4hrGjgtv7-O8mMdW0BEqty4Rk
POST request Create channel ...
{"success":true,"message":"Channel 'mychannel' created Successfully"}
POST request Join channel on Org1
{"success":true,"message":"Successfully joined peers in organization org1 to the channel 'mychannel'"}
POST request Join channel on Org2
{"success":true,"message":"Successfully joined peers in organization org2 to the channel 'mychannel'"}
POST Install chaincode on Org1
Successfully Installed chaincode on organization org1
POST Install chaincode on Org2
Successfully Installed chaincode on organization org2
POST instantiate chaincode on peer1 of Org1
Chaincode Instantiation is SUCCESS
POST invoke chaincode on peers of Org1 and Org2
Transacton ID is d27299046a71d869ac8efb8d73f44641eac875143680f0b629ceb13129f3d8c6
GET query chaincode on peer1 of Org1
a now has 90 after the move

■Trouble shooting

うまく動作しない場合は以下のコマンドを実行してみてください。

docker、Nodeの内容をすべてリセットできます。

sudo systemctl restart docker
docker rm $(docker ps -aq)
docker network prune
lsof -i
kill 00000(lsof -iで表示されたポートを消す)

Coin Marketplace

STEEM 0.20
TRX 0.26
JST 0.039
BTC 100331.97
ETH 3646.26
USDT 1.00
SBD 3.05