LoginSignup
0
0

More than 3 years have passed since last update.

[HyperledgerIroha]Pythonライブラリでクエリを実行する

Posted at

記事の内容

Hyperledger Iroha Pythonライブラリを使ったクエリの実行方法です。

やってみること

指定したアカウントが送信したトランザクションの一覧を取得する「GetAccountTransactions」を実行してみます。

実装

get_account_transactions.py
from iroha import Iroha, IrohaCrypto, IrohaGrpc
import iroha_config

net = IrohaGrpc(iroha_config.IROHA_HOST)

iroha = Iroha(iroha_config.ADMIN_ACCOUNT)
admin_priv_key = iroha_config.ADMIN_PRIV_KEY

# Queryの作成
get_block_query = iroha.query(
    'GetAccountTransactions',
    account_id = 'admin@test',
    page_size = 10,
)

# Queryへ署名
IrohaCrypto.sign_query(get_block_query, iroha_config.ADMIN_PRIV_KEY)

# Queryの送信
response = net.send_query(get_block_query)
print(response)

iroha_configにはIrohaのホスト名や秘密鍵などを設定しています。

ポイントは以下3つです。
・クエリの作成は「iroha.query」を使用する。(更新系は「iroha.command」を使用する)
・署名は「sign_query」を使用する。(更新系は「sign_transaction」を使用する)
・クエリの送信は「send_query」を使用する。(更新系は「send_tx」を使用する)

実行結果

指定したアカウントが送信したトランザクションの情報を取得できました。
page_sizeに10を設定していますが、あまりブロックチェーンにデータを載せてないので、3件だけデータを取得できました。

query_hash: "a45f8b59aa211688836bb6567c10baf10e24dfdc9db6c62d3fc8a779200ee136"
transactions_page_response {
  transactions {
    payload {
      reduced_payload {
        commands {
          create_asset {
            asset_name: "samplecoin"
            domain_id: "test"
            precision: 10
          }
        }
        commands {
          add_asset_quantity {
            asset_id: "samplecoin#test"
            amount: "100000000"
          }
        }
        creator_account_id: "admin@test"
        created_time: 1594000825126
        quorum: 1
      }
    }
    signatures {
      public_key: "313a07e6384776ed95447710d15e59148473ccfc052a681317a72a69f2a49910"
      signature: "042b25c8096ca4bfcaca54d05c3d862498f38ddc612b0255138cc74044005d0475467176ef84672fd79b93714e8263e22aa867e7f8e22cf7eba06bb0e63e0b02"
    }
  }
  transactions {
    payload {
      reduced_payload {
        commands {
          transfer_asset {
            src_account_id: "admin@test"
            dest_account_id: "test@test"
            asset_id: "samplecoin#test"
            description: "test"
            amount: "10000"
          }
        }
        creator_account_id: "admin@test"
        created_time: 1594002435597
        quorum: 1
      }
    }
    signatures {
      public_key: "313a07e6384776ed95447710d15e59148473ccfc052a681317a72a69f2a49910"
      signature: "f147a7e1c1604a3172efa91e4604fd4bfff728fe0d8d4c644e7d3a0d65ee4f3d2436ad9a26bc9777b0de514f120ed6b4d83a5410cbc097a281a5a588702d4d06"
    }
  }
  transactions {
    payload {
      reduced_payload {
        commands {
          create_account {
            account_name: "iroha"
            domain_id: "test"
            public_key: "efdc215eab6dd2c4435d370da73e4b88350e1fed9d39afed503fcbee985fce1f"
          }
        }
        creator_account_id: "admin@test"
        created_time: 1594016319157
        quorum: 1
      }
    }
    signatures {
      public_key: "313a07e6384776ed95447710d15e59148473ccfc052a681317a72a69f2a49910"
      signature: "429bf3ae70b60d31ab3fc3c15fa11cb8155b6a824f831652c1c46068feaeb866846b8048e0959cbcbeed532423f050e77d2fe192ea90d0827a6f1489bf67ec0a"
    }
  }
  all_transactions_size: 3
}

関連記事

[Hyperledger Iroha]Python SDKの使い方メモ
[Hyperledger Iroha]Pythonのlibraryを使ってアカウントを作成する

感想

一通りIrohaライブラリを使ってみました。
他のクエリは実行するクエリ名称を変更したり、パラメーターを変更すれば動きます。

Irohaクエリを見ている限りだと、「誰が」、「何を」したのかは簡単に取得することができますが、
「どの資産に対して」、「誰が」、「何を」したのかが分かるクエリは用意されていないようです。

0
0
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
0
0