はじめに
今回の目的は、ビジネスネットワーク定義後の次のステップとしてクエリチュートリアルのビジネスネットワーク更新までを試してみます。
環境構築
事前準備
こちらを参照し、ビジネスネットワークの定義まで行ってください。
英語チュートリアルを読みたくないという方は下記のブログを参照してビジネスネットワークの定義まで行ってください。(※2018/03/21(水)現在で動作確認済)
環境
– Operating Systems: Ubuntu 16.04 LTS
– Docker: Version 1.13.1
– Docker-Compose: Version 1.8.0
– Node: v8.1.0
– npm: v5.6.0
– Python: 3.5.0
– VSCode: 1.21.1(Hyperleger Composerの拡張: 0.16.2)
チュートリアル
下記URLを参考にチュートリアルを行います。
https://hyperledger.github.io/composer/latest/tutorials/queries
ステップ1:モデルファイルを更新する
- modelディレクトリ下にある「acme.biznet.cto」ファイルに以下のイベントを追加します。この際ビジネスネットワーク定義時に設定したものは残しておいても構いません。
1 2 3 4 5 6 7 8 9 10 |
event TradeNotification { --> Commodity commodity } transaction RemoveHighQuantityCommodities { } event RemoveNotification { --> Commodity commodity } |
- 変更後、保存します。
ステップ2:トランザクションロジックを更新する
- libディレクトリ下にある「logic.js」を書き換えます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
/** * Track the trade of a commodity from one trader to another * @param {org.acme.biznet.Trade} trade - the trade to be processed * @transaction */ function tradeCommodity(trade) { // set the new owner of the commodity trade.commodity.owner = trade.newOwner; return getAssetRegistry('org.acme.biznet.Commodity') .then(function (assetRegistry) { // emit a notification that a trade has occurred var tradeNotification = getFactory().newEvent('org.acme.biznet', 'TradeNotification'); tradeNotification.commodity = trade.commodity; emit(tradeNotification); // persist the state of the commodity return assetRegistry.update(trade.commodity); }); } /** * Remove all high volume commodities * @param {org.acme.biznet.RemoveHighQuantityCommodities} remove - the remove to be processed * @transaction */ function removeHighQuantityCommodities(remove) { return getAssetRegistry('org.acme.biznet.Commodity') .then(function (assetRegistry) { return query('selectCommoditiesWithHighQuantity') .then(function (results) { var promises = []; for (var n = 0; n < results.length; n++) { var trade = results[n]; // emit a notification that a trade was removed var removeNotification = getFactory().newEvent('org.acme.biznet', 'RemoveNotification'); removeNotification.commodity = trade; emit(removeNotification); // remove the commodity promises.push(assetRegistry.remove(trade)); } // we have to return all the promises return Promise.all(promises); }); }); } |
参考ページ
Emitting Events
https://hyperledger.github.io/composer/next/business-network/publishing-events.html
ステップ3:クエリ定義ファイルを作成する
- tutorial-networkディレクトリ下に「queries.qry」を作成します。
- 以下のコードを貼り付けます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/** Sample queries for Commodity Trading business network */ query selectCommodities { description: "Select all commodities" statement: SELECT org.acme.biznet.Commodity } query selectCommoditiesByExchange { description: "Select all commodities based on their main exchange" statement: SELECT org.acme.biznet.Commodity WHERE (mainExchange==_$exchange) } query selectCommoditiesByOwner { description: "Select all commodities based on their owner" statement: SELECT org.acme.biznet.Commodity WHERE (owner == _$owner) } query selectCommoditiesWithHighQuantity { description: "Select commodities based on quantity" statement: SELECT org.acme.biznet.Commodity WHERE (quantity > 60) } |
3. 変更後、保存します。
ステップ4:ビジネスネットワークアーカイブを生成する
- 「tutorial-network」ディレクトリにある「package.json」を開きます。
- versionを0.0.1から0.0.2へ変更します。
- 「tutorial-network」ディレクトリで以下のコマンドを実行すると「tutorial-network@0.0.2.bna」が生成されます。
1 |
composer archive create --sourceType dir --sourceName . -a tutorial-network@0.0.2.bna |
ステップ5:更新されたビジネスネットワーク定義をデプロイする
- 「tutorial-network@0.0.2.bna」があるディレクトリに移動します。
- 以下のコマンドを実行して、更新したビジネスネットワークをインストールします。
1 |
composer network install --card PeerAdmin@hlfv1 --archiveFile tutorial-network@0.0.2.bna |
3. 以下のコマンドを実行して、ネットワークを新しいバージョンにアップグレードします。
1 |
composer network upgrade -c PeerAdmin@hlfv1 -n tutorial-network -V 0.0.2 |
4. 以下のコマンドを実行して、ビジネスネットワークの現在のバージョンを確認してください。
1 |
composer network ping -c admin@tutorial-network | grep Business |
全て成功したら「Hyperledger Composerクエリチュートリアルをやってみる その1」は終了です。
その2へ続きます。
- Hyperledger Composerクエリチュートリアルをやってみる その2 - 2018年4月11日
- Hyperledger Composer クエリチュートリアルをやってみる その1 - 2018年4月10日
本記事に対するコメントはまだありません