1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Hyperledger Fabricを使ったコミュニティ内仮想通貨構築 3日目

1
Posted at

query.jsの実行結果

サンプルにて取得するスクリプトのようです。

ubuntu@ip-172-26-10-23:/var/fabric/fabric-samples/fabcar/javascript$ node query.js

Wallet path: /var/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\"}}]"
ubuntu@ip-172-26-10-23:/var/fabric/fabric-samples/fabcar/javascript$

ご覧いただければわかると思いますが、
車の色などを取得してます。今これだけの車が、ブロックチェーン管理されているようです。

query.js メインファイルの中身

/var/fabric/fabric-samples/fabcar/javascript$ pwd
/var/fabric/fabric-samples/fabcar/javascript```

```ubuntu@ip-172-26-10-23:/var/fabric/fabric-samples/fabcar/javascript$ cat query.js```

/*

  • SPDX-License-Identifier: Apache-2.0
    */

'use strict';

const { FileSystemWallet, Gateway } = require('fabric-network');
const fs = require('fs');
const path = require('path');

const ccpPath = path.resolve(__dirname, '..', '..', 'basic-network', 'connection.json');
const ccpJSON = fs.readFileSync(ccpPath, 'utf8');
const ccp = JSON.parse(ccpJSON);

async function main() {
try {

    // Create a new file system based wallet for managing identities.
    const walletPath = path.join(process.cwd(), 'wallet');
    const wallet = new FileSystemWallet(walletPath);
    console.log(`Wallet path: ${walletPath}`);

    // Check to see if we've already enrolled the user.
    const userExists = await wallet.exists('user1');
    if (!userExists) {
        console.log('An identity for the user "user1" does not exist in the wallet');
        console.log('Run the registerUser.js application before retrying');
        return;
    }

    // Create a new gateway for connecting to our peer node.
    const gateway = new Gateway();
    await gateway.connect(ccp, { wallet, identity: 'user1', discovery: { enabled: false } });

    // Get the network (channel) our contract is deployed to.
    const network = await gateway.getNetwork('mychannel');

    // Get the contract from the network.
    const contract = network.getContract('fabcar');

    // Evaluate the specified transaction.
    // queryCar transaction - requires 1 argument, ex: ('queryCar', 'CAR4')
    // queryAllCars transaction - requires no arguments, ex: ('queryAllCars')
    const result = await contract.evaluateTransaction('queryAllCars');
    console.log(`Transaction has been evaluated, result is: ${result.toString()}`);

} catch (error) {
    console.error(`Failed to evaluate transaction: ${error}`);
    process.exit(1);
}

}

main();


## 中身
```const { FileSystemWallet, Gateway } = require('fabric-network');```

fabric-networkに接続する。(記載が必須のようです。)

```const gateway = new Gateway();
await gateway.connect(ccp, { wallet, identity: 'user1' });```
user1のウォレットを確認する。


### 接続情報を取得
```const ccpPath = path.resolve(__dirname, '..', '..', 'basic-network', 'connection.json');
const ccpJSON = fs.readFileSync(ccpPath, 'utf8');
const ccp = JSON.parse(ccpJSON);```

以下のファイルには、接続情報が記載されます。接続情報を取得
(サーバのポート番号や、オーダラー"orderers" ピアー"peers"の情報が格納されてる)
```/var/fabric/fabric-samples/basic-network/connection.json```

### スマートコントラクト(今回はfabcar)に接続
```const contract = network.getContract('fabcar');```

### トランザクション処理
上記にて使用した トランザクション(queryAllCars)にて全部の車を取得。
```const result = await contract.evaluateTransaction('queryAllCars');```


# スマートコントラクト(今回はfabcar)の確認
## 下記にスマートコントラクトがあります。
```/var/fabric/fabric-samples/chaincode/fabcar/javascript/lib/fabcar.js ```

中身をみると、下記のトランザクションの記載があるようです。
initLedger(台帳を作成)
queryCar(車情報を取得)
queryAllCars(全部情報を取得)
createCar(車を作成)
changeCarOwner(車の所有者を変更)

## トランザクション処理を変更する。
```const result = await contract.evaluateTransaction('queryAllCars');```
## 下に変更する。
```const result = await contract.evaluateTransaction('queryCar', 'CAR4');```
## 再実行
```ubuntu@ip-172-26-10-23:/var/fabric/fabric-samples/fabcar/javascript$ cat query.js```
CAR4表示されました。

queryCarを使えば、現在の情報を変更することができます。

# 次回は、元帳の更新をやってみます。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?