Skip to content

Commit

Permalink
cherry-pick new features from develop -> main (#97)
Browse files Browse the repository at this point in the history
* Extending TxPaginationMeta in queries (#77)

* quries extended
* example added
* docs comment added
* schema changed
* formatting fixed
* ordering added
* edge case fix, now 0 passed as height or timestamp is passing
* PaginationMeta in GetPendingTransactions
* examples/query_transactions.py

Signed-off-by: Piotr Pawlowski <ppiotru@gmail.com>
Signed-off-by: G.Bazior <bazior@agh.edu.pl>

* Update proto files and generated python files to support Iroha 1.4 (#96)

* Update proto files with script `download-schema.py`

Signed-off-by: Grzegorz Bazior <g.bazior@yodiss.pl>

* Generated python files from protobuf files with script `compile-proto.py`

Signed-off-by: G.Bazior <bazior@agh.edu.pl>

Co-authored-by: Grzegorz Bazior <g.bazior@yodiss.pl>
Signed-off-by: G.Bazior <bazior@agh.edu.pl>

* Corrected merge - rerun scripts: download-schema.py and compile-proto.py

Signed-off-by: G.Bazior <bazior@agh.edu.pl>

* Tab -> spaces

Signed-off-by: G.Bazior <bazior@agh.edu.pl>

* Add ability to provide custom TLS cert (#63)

Signed-off-by: Stepan Lavrentev <lawrentievsv@gmail.com>
Signed-off-by: G.Bazior <bazior@agh.edu.pl>

* Add ordering sequence to params in query (#73)

* Add ordering sequence to params

Makes possible to add ordering sequence to Query. Fixes #72

Signed-off-by: Rafik Naccache <rafik@fekr.tech>

* Add :param ordering_sequence:

Signed-off-by: Rafik Naccache <rafik@fekr.tech>

* Fix wrong example for :param ordering_sequence:

Signed-off-by: Rafik Naccache <rafik@fekr.tech>

* fix wrong ordering message construction in query

After more documentation I got to the way to construct to ordering object

Signed-off-by: Rafik Naccache <rafik@fekr.tech>

* remove redundant OR clause to manage ordering_sequence

Signed-off-by: Rafik Naccache <rafik@fekr.tech>
Signed-off-by: G.Bazior <bazior@agh.edu.pl>

* Extending TxPaginationMeta in queries (#77)

* quries extended
* example added
* docs comment added
* schema changed
* formatting fixed
* ordering added
* edge case fix, now 0 passed as height or timestamp is passing
* PaginationMeta in GetPendingTransactions
* examples/query_transactions.py

Signed-off-by: Piotr Pawlowski <ppiotru@gmail.com>
Signed-off-by: G.Bazior <bazior@agh.edu.pl>

* Added missing changes from Pawlak00@b6d7f42 corrected in #77

Signed-off-by: G.Bazior <bazior@agh.edu.pl>

Co-authored-by: Piotr Pawłowski <68233055+Pawlak00@users.noreply.github.com>
Co-authored-by: Grzegorz Bazior <g.bazior@yodiss.pl>
Co-authored-by: Stepan Lavrentev <40560660+stepanLav@users.noreply.github.com>
Co-authored-by: Rafik NACCACHE <rafik@fekr.tech>
  • Loading branch information
5 people committed Mar 23, 2022
1 parent 41a724b commit e0a4f9b
Show file tree
Hide file tree
Showing 16 changed files with 935 additions and 4,509 deletions.
228 changes: 228 additions & 0 deletions examples/query_transactions.py
@@ -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')

0 comments on commit e0a4f9b

Please sign in to comment.