-
Notifications
You must be signed in to change notification settings - Fork 79
Extending TxPaginationMeta in queries #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
833667b
quries extended
Pawlak00 630b0cb
example added
Pawlak00 66738bd
docs comment added
Pawlak00 371507f
schema changed
Pawlak00 2827ba6
PR changes
Pawlak00 6626b5f
formatting fixed
Pawlak00 b4d56f9
formatting fixed
Pawlak00 1a7dee2
formatting fixed
Pawlak00 7df3aee
ordering added
Pawlak00 7909efc
edge case fix, now 0 passed as height or timestamp is passing
Pawlak00 529f0cc
whitespace
Pawlak00 dfe9f03
review fixes
Pawlak00 7fec212
review fixes
Pawlak00 2594bf9
PaginationMeta in GetPendingTransactions
Pawlak00 e45d980
review fix
Pawlak00 6df1cc9
examples/query_transactions.py
Pawlak00 ca0e28f
fix
Pawlak00 20eca32
fix
Pawlak00 f3200b4
fix
Pawlak00 2556fe8
fix
Pawlak00 e7a4ba9
fix
Pawlak00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| #!/usr/bin/env python3 | ||
| # | ||
| # Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
|
|
||
|
|
||
| # Here are Iroha dependencies. | ||
| # Python library generally consists of 3 parts: | ||
| # Iroha, IrohaCrypto and IrohaGrpc which we need to import: | ||
| import os | ||
| import binascii | ||
| from iroha import IrohaCrypto | ||
| from iroha import Iroha, IrohaGrpc | ||
| from google.protobuf.timestamp_pb2 import Timestamp | ||
| from iroha.primitive_pb2 import can_set_my_account_detail | ||
| import sys | ||
|
|
||
| if sys.version_info[0] < 3: | ||
| raise Exception('Python 3 or a more recent version is required.') | ||
|
|
||
| # Here is the information about the environment and admin account information: | ||
| IROHA_HOST_ADDR = os.getenv('IROHA_HOST_ADDR', '127.0.0.1') | ||
| IROHA_PORT = os.getenv('IROHA_PORT', '50051') | ||
| ADMIN_ACCOUNT_ID = os.getenv('ADMIN_ACCOUNT_ID', 'admin@test') | ||
| ADMIN_PRIVATE_KEY = os.getenv( | ||
| 'ADMIN_PRIVATE_KEY', 'f101537e319568c765b2cc89698325604991dca57b9716b58016b253506cab70') | ||
|
|
||
| # Here we will create user keys | ||
| user_private_key = IrohaCrypto.private_key() | ||
| user_public_key = IrohaCrypto.derive_public_key(user_private_key) | ||
| iroha = Iroha(ADMIN_ACCOUNT_ID) | ||
| net = IrohaGrpc('{}:{}'.format(IROHA_HOST_ADDR, IROHA_PORT)) | ||
|
|
||
|
|
||
| def trace(func): | ||
| """ | ||
| A decorator for tracing methods' begin/end execution points | ||
| """ | ||
|
|
||
| def tracer(*args, **kwargs): | ||
| name = func.__name__ | ||
| print('\tEntering "{}"'.format(name)) | ||
| result = func(*args, **kwargs) | ||
| print('\tLeaving "{}"'.format(name)) | ||
| return result | ||
|
|
||
| return tracer | ||
|
|
||
| # Let's start defining the commands: | ||
| @trace | ||
| def send_transaction_and_print_status(transaction): | ||
| hex_hash = binascii.hexlify(IrohaCrypto.hash(transaction)) | ||
| print('Transaction hash = {}, creator = {}'.format( | ||
| hex_hash, transaction.payload.reduced_payload.creator_account_id)) | ||
| net.send_tx(transaction) | ||
| for status in net.tx_status_stream(transaction): | ||
| print(status) | ||
|
|
||
| # For example, below we define a transaction made of 2 commands: | ||
| # CreateDomain and CreateAsset. | ||
| # Each of Iroha commands has its own set of parameters and there are many commands. | ||
| # You can check out all of them here: | ||
| # https://iroha.readthedocs.io/en/main/develop/api/commands.html | ||
| @trace | ||
| def create_domain_and_asset(): | ||
| """ | ||
| Create domain 'domain' and asset 'coin#domain' with precision 2 | ||
| """ | ||
| commands = [ | ||
| iroha.command('CreateDomain', domain_id='domain', default_role='user'), | ||
| iroha.command('CreateAsset', asset_name='coin', | ||
| domain_id='domain', precision=2) | ||
| ] | ||
| # And sign the transaction using the keys from earlier: | ||
| tx = IrohaCrypto.sign_transaction( | ||
| iroha.transaction(commands), ADMIN_PRIVATE_KEY) | ||
| send_transaction_and_print_status(tx) | ||
| # You can define queries | ||
| # (https://iroha.readthedocs.io/en/main/develop/api/queries.html) | ||
| # the same way. | ||
|
|
||
| @trace | ||
| def add_coin_to_admin(): | ||
| """ | ||
| Add 1000.00 units of 'coin#domain' to 'admin@test' | ||
| """ | ||
| tx = iroha.transaction([ | ||
| iroha.command('AddAssetQuantity', | ||
| asset_id='coin#domain', amount='1000.00') | ||
| ]) | ||
| IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY) | ||
| send_transaction_and_print_status(tx) | ||
| tx_tms = tx.payload.reduced_payload.created_time | ||
| print(tx_tms) | ||
| first_time, last_time = tx_tms - 1, tx_tms + 1 | ||
| return first_time, last_time | ||
|
|
||
| @trace | ||
| def create_account_userone(): | ||
| """ | ||
| Create account 'userone@domain' | ||
| """ | ||
| tx = iroha.transaction([ | ||
| iroha.command('CreateAccount', account_name='userone', domain_id='domain', | ||
| public_key=user_public_key) | ||
| ]) | ||
| IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY) | ||
| send_transaction_and_print_status(tx) | ||
|
|
||
|
|
||
| @trace | ||
| def transfer_coin_from_admin_to_userone(): | ||
| """ | ||
| Transfer 2.00 'coin#domain' from 'admin@test' to 'userone@domain' | ||
| """ | ||
| tx = iroha.transaction([ | ||
| iroha.command('TransferAsset', src_account_id='admin@test', dest_account_id='userone@domain', | ||
| asset_id='coin#domain', description='init top up', amount='2.00') | ||
| ]) | ||
| IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY) | ||
| send_transaction_and_print_status(tx) | ||
|
|
||
|
|
||
| @trace | ||
| def userone_grants_to_admin_set_account_detail_permission(): | ||
| """ | ||
| Make 'admin@test' able to set detail to 'userone@domain' | ||
| """ | ||
| tx = iroha.transaction([ | ||
| iroha.command('GrantPermission', account_id='admin@test', | ||
| permission=can_set_my_account_detail) | ||
| ], creator_account='userone@domain') | ||
| IrohaCrypto.sign_transaction(tx, user_private_key) | ||
| send_transaction_and_print_status(tx) | ||
|
|
||
|
|
||
| @trace | ||
| def set_age_to_userone(): | ||
| """ | ||
| Set age to 'userone@domain' by 'admin@test' | ||
| """ | ||
| tx = iroha.transaction([ | ||
| iroha.command('SetAccountDetail', | ||
| account_id='userone@domain', key='age', value='18') | ||
| ]) | ||
| IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY) | ||
| send_transaction_and_print_status(tx) | ||
|
|
||
|
|
||
| @trace | ||
| def get_coin_info(): | ||
| """ | ||
| Get asset info for 'coin#domain' | ||
| :return: | ||
| """ | ||
| query = iroha.query('GetAssetInfo', asset_id='coin#domain') | ||
| IrohaCrypto.sign_query(query, ADMIN_PRIVATE_KEY) | ||
|
|
||
| response = net.send_query(query) | ||
| data = response.asset_response.asset | ||
| print('Asset id = {}, precision = {}'.format(data.asset_id, data.precision)) | ||
|
|
||
|
|
||
| @trace | ||
| def get_account_assets(): | ||
| """ | ||
| List all the assets of 'userone@domain' | ||
| """ | ||
| query = iroha.query('GetAccountAssets', account_id='userone@domain') | ||
| IrohaCrypto.sign_query(query, ADMIN_PRIVATE_KEY) | ||
|
|
||
| response = net.send_query(query) | ||
| data = response.account_assets_response.account_assets | ||
| for asset in data: | ||
| print('Asset id = {}, balance = {}'.format( | ||
| asset.asset_id, asset.balance)) | ||
|
|
||
| @trace | ||
| def query_transactions(first_time = None, last_time = None, | ||
| first_height = None, last_height = None): | ||
| query = iroha.query('GetAccountTransactions', account_id = ADMIN_ACCOUNT_ID, | ||
| first_tx_time = first_time, | ||
| last_tx_time = last_time, | ||
| first_tx_height = first_height, | ||
| last_tx_height = last_height, | ||
| page_size = 3) | ||
| IrohaCrypto.sign_query(query, ADMIN_PRIVATE_KEY) | ||
| response = net.send_query(query) | ||
| data = response | ||
| print(data) | ||
|
|
||
| @trace | ||
| def get_userone_details(): | ||
| """ | ||
| Get all the kv-storage entries for 'userone@domain' | ||
| """ | ||
| query = iroha.query('GetAccountDetail', account_id='userone@domain') | ||
| IrohaCrypto.sign_query(query, ADMIN_PRIVATE_KEY) | ||
|
|
||
| response = net.send_query(query) | ||
| data = response.account_detail_response | ||
| print('Account id = {}, details = {}'.format('userone@domain', data.detail)) | ||
|
|
||
| # Let's run the commands defined previously: | ||
| create_domain_and_asset() | ||
| first_time, last_time = add_coin_to_admin() | ||
| create_account_userone() | ||
| transfer_coin_from_admin_to_userone() | ||
| userone_grants_to_admin_set_account_detail_permission() | ||
| set_age_to_userone() | ||
| get_coin_info() | ||
| get_account_assets() | ||
| get_userone_details() | ||
| # set timestamp to correct value | ||
| # for more protobuf timestamp api info see: | ||
| # https://googleapis.dev/python/protobuf/latest/google/protobuf/timestamp_pb2.html | ||
| first_tx_time = Timestamp() | ||
| first_tx_time.FromMilliseconds(first_time) | ||
| last_tx_time = Timestamp() | ||
| last_tx_time.FromMilliseconds(last_time) | ||
| # query for txs in measured time | ||
| print('transactions from time interval query: ') | ||
| query_transactions(first_tx_time, last_tx_time) | ||
| # query for txs in given height range | ||
| print('transactions from height range query: ') | ||
| query_transactions(first_height = 2, last_height = 3) | ||
| print('done') | ||
Pawlak00 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.