• Home
  • Hyperledger fabricのサンプルアプリを触ってみる

Hyperledger fabricのサンプルアプリを触ってみる

Hyperledger Fabric のサンプルである”fabcar”を使った チュートリアルをもとに、Hyperledger Fabric のアプリがどのように機能するかを確認する。

※ fabcar は Hyperledger Fabric のサンプル群である fabric-samples のサンプルの1つで、Fabric ネットワーク上のスマートコントラクト (チェーンコード) を Node.js から呼び出すもの。

サンプルアプリケーションネットワークの起動

0. Hyperledger Fabric導入

未導入の場合はこちらを参照して導入する。

1. ネットワークの起動

※不要なdocker が起動している場合は事前に停止しておく。
$ cd fabric-samples/fabcar
$ ./startFabric.sh javascript

Total setup execution time : が表示されネットワークが構築される。

2. ネットワークの起動確認

$ docker ps

3. アプリケーションをインストールする

Fabric に依存関係のあるアプリケーションをインストールする。

$ cd fabric-samples/fabcar/javascript
$ npm install

サンプルアプリケーションでの動作検証

1. 管理者ユーザーの登録

$ cd fabric-samples/fabcar/javascript
$ node enrollAdmin.js
$ docker logs -f ca_peerOrg1

Successfully enrolled admin user が表示される

Wallet path: /home/ubuntu/fabric/fabric-samples/fabcar/javascript/wallet
Successfully enrolled admin user "admin" and imported it into the wallet

Successfully incremented state for identity admin が表示される

2019/08/26 03:44:54 [DEBUG] Received request for /api/v1/enroll
2019/08/26 03:44:54 [DEBUG] ca.Config: &{Version:1.4.2 Cfg:{Identities:{PasswordAttempts:10 AllowRemove:false} Affiliations:{AllowRemove:false}} CA:{Name:ca-org1 Keyfile:/etc/hyperledger/fabric-ca-server-config/87ba5451b2e7eb0c6907e45f6ea4e214e4a7a7714fcc4e3f2d833835c75aabd8_sk Certfile:/etc/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem Chainfile:/etc/hyperledger/fabric-ca-server/ca-chain.pem} Signing:0xc0004a25e0 CSR:{CN:ca.org1.example.com Names:[{C:US ST:North Carolina L: O:Hyperledger OU:Fabric SerialNumber:}] Hosts:[5d657b66dd1c localhost] KeyRequest:0xc000438660 CA:0xc0004386e0 SerialNumber:} Registry:{MaxEnrollments:-1 Identities:[{ Name:**** Pass:**** Type:client Affiliation: MaxEnrollments:0 Attrs:map[hf.Registrar.Attributes:* hf.AffiliationMgr:1 hf.Registrar.Roles:* hf.Registrar.DelegateRoles:* hf.Revoker:1 hf.IntermediateCA:1 hf.GenCRL:1]  }]} Affiliations:map[org2:[department1] org1:[department1 department2]] LDAP:{ Enabled:false URL:ldap://****:****@:/ UserFilter:(uid=%s) GroupFilter:(memberUid=%s) Attribute:{[uid member] [{ }] map[groups:[{ }]]} TLS:{false [] { }}  } DB:{ Type:sqlite3 Datasource:/etc/hyperledger/fabric-ca-server/fabric-ca-server.db TLS:{false [] { }}  } CSP:0xc000438a00 Client: Intermediate:{ParentServer:{ URL: CAName:  } TLS:{Enabled:false CertFiles:[] Client:{KeyFile: CertFile:}} Enrollment:{ Name: Secret:**** CAName: AttrReqs:[] Profile: Label: CSR: Type:x509  }} CRL:{Expiry:24h0m0s} Idemix:{IssuerPublicKeyfile:/etc/hyperledger/fabric-ca-server/IssuerPublicKey IssuerSecretKeyfile:/etc/hyperledger/fabric-ca-server/msp/keystore/IssuerSecretKey RevocationPublicKeyfile:/etc/hyperledger/fabric-ca-server/IssuerRevocationPublicKey RevocationPrivateKeyfile:/etc/hyperledger/fabric-ca-server/msp/keystore/IssuerRevocationPrivateKey RHPoolSize:1000 NonceExpiration:15s NonceSweepInterval:15m}}
2019/08/26 03:44:54 [DEBUG] DB: Getting identity admin
2019/08/26 03:44:54 [DEBUG] DB: Login user admin with max enrollments of -1 and state of 0
2019/08/26 03:44:55 [DEBUG] DB: identity admin successfully logged in
2019/08/26 03:44:55 [DEBUG] DB: Getting identity admin
2019/08/26 03:44:55 [DEBUG] Processing sign request: id=admin, CommonName=admin, Subject=
2019/08/26 03:44:55 [DEBUG] Request is not for a CA signing certificate
2019/08/26 03:44:55 [DEBUG] Checking CSR fields to make sure that they do not exceed maximum character limits
2019/08/26 03:44:55 [DEBUG] Finished processing sign request
2019/08/26 03:44:55 [DEBUG] DB: Getting identity admin
2019/08/26 03:44:55 [INFO] signed certificate with serial number 537553457662216865910118675845150147467436290976
2019/08/26 03:44:55 [DEBUG] DB: Insert Certificate
2019/08/26 03:44:55 [DEBUG] Saved serial number as hex 5e28bb0fde07f7954d43ee05bf1092e8276af3a0
2019/08/26 03:44:55 [DEBUG] saved certificate with serial number 537553457662216865910118675845150147467436290976
2019/08/26 03:44:55 [DEBUG] Successfully incremented state for identity admin to 1
2019/08/26 03:44:55 [INFO] 172.22.0.1:52396 POST /api/v1/enroll 201 0 "OK"

2. 新規ユーザーの登録

$ node registerUser.js
$ docker logs -f ca_peerOrg1

Successfully registered and enrolled admin user “user1” が表示される

Wallet path: /home/ubuntu/fabric/fabric-samples/fabcar/javascript/wallet
Successfully registered and enrolled admin user "user1" and imported it into the wallet

3. 元帳リストの取得(初期状態の確認)

$ node query.js

CAR0 ~ CAR9 が表示される

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

4. 元帳リストへの追加(CAR12の追加)

$ node node invoke.js

Transaction has been submitted が表示される

Wallet path: /home/ubuntu/fabric/fabric-samples/fabcar/javascript/wallet
Transaction has been submitted

5. 元帳リストの取得(追加後の確認)

$ node query.js

CAR12 が表示される

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

6. 元帳リストの更新(CAR12の変更)

事前にinvoke.jsの中身を変更する

//↓ コメントアウト
//await contract.submitTransaction('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom');
//↓ 追加
await contract.submitTransaction('changeCarOwner', 'CAR12', 'Dave');
$ node invoke.js

Transaction has been submittedが表示される

Wallet path: /home/ubuntu/fabric/fabric-samples/fabcar/javascript/wallet
Transaction has been submitted

7. 元帳リストの取得(CAR12の所有者変更確認)

$ node query.js

CAR12のownerがDaveとなっている

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

ご相談・お見積もり

03-5207-2689