Skip to content

Commit e0a4f9b

Browse files
baziorekPawlak00Grzegorz BaziorstepanLavturbopape
authored
cherry-pick new features from develop -> main (#97)
* 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 <[email protected]> Signed-off-by: G.Bazior <[email protected]> * 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 <[email protected]> * Generated python files from protobuf files with script `compile-proto.py` Signed-off-by: G.Bazior <[email protected]> Co-authored-by: Grzegorz Bazior <[email protected]> Signed-off-by: G.Bazior <[email protected]> * Corrected merge - rerun scripts: download-schema.py and compile-proto.py Signed-off-by: G.Bazior <[email protected]> * Tab -> spaces Signed-off-by: G.Bazior <[email protected]> * Add ability to provide custom TLS cert (#63) Signed-off-by: Stepan Lavrentev <[email protected]> Signed-off-by: G.Bazior <[email protected]> * 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 <[email protected]> * Add :param ordering_sequence: Signed-off-by: Rafik Naccache <[email protected]> * Fix wrong example for :param ordering_sequence: Signed-off-by: Rafik Naccache <[email protected]> * 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 <[email protected]> * remove redundant OR clause to manage ordering_sequence Signed-off-by: Rafik Naccache <[email protected]> Signed-off-by: G.Bazior <[email protected]> * 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 <[email protected]> Signed-off-by: G.Bazior <[email protected]> * Added missing changes from Pawlak00@b6d7f42 corrected in #77 Signed-off-by: G.Bazior <[email protected]> Co-authored-by: Piotr Pawłowski <[email protected]> Co-authored-by: Grzegorz Bazior <[email protected]> Co-authored-by: Stepan Lavrentev <[email protected]> Co-authored-by: Rafik NACCACHE <[email protected]>
1 parent 41a724b commit e0a4f9b

16 files changed

+935
-4509
lines changed

examples/query_transactions.py

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
8+
# Here are Iroha dependencies.
9+
# Python library generally consists of 3 parts:
10+
# Iroha, IrohaCrypto and IrohaGrpc which we need to import:
11+
import os
12+
import binascii
13+
from iroha import IrohaCrypto
14+
from iroha import Iroha, IrohaGrpc
15+
from google.protobuf.timestamp_pb2 import Timestamp
16+
from iroha.primitive_pb2 import can_set_my_account_detail
17+
import sys
18+
19+
if sys.version_info[0] < 3:
20+
raise Exception('Python 3 or a more recent version is required.')
21+
22+
# Here is the information about the environment and admin account information:
23+
IROHA_HOST_ADDR = os.getenv('IROHA_HOST_ADDR', '127.0.0.1')
24+
IROHA_PORT = os.getenv('IROHA_PORT', '50051')
25+
ADMIN_ACCOUNT_ID = os.getenv('ADMIN_ACCOUNT_ID', 'admin@test')
26+
ADMIN_PRIVATE_KEY = os.getenv(
27+
'ADMIN_PRIVATE_KEY', 'f101537e319568c765b2cc89698325604991dca57b9716b58016b253506cab70')
28+
29+
# Here we will create user keys
30+
user_private_key = IrohaCrypto.private_key()
31+
user_public_key = IrohaCrypto.derive_public_key(user_private_key)
32+
iroha = Iroha(ADMIN_ACCOUNT_ID)
33+
net = IrohaGrpc('{}:{}'.format(IROHA_HOST_ADDR, IROHA_PORT))
34+
35+
36+
def trace(func):
37+
"""
38+
A decorator for tracing methods' begin/end execution points
39+
"""
40+
41+
def tracer(*args, **kwargs):
42+
name = func.__name__
43+
print('\tEntering "{}"'.format(name))
44+
result = func(*args, **kwargs)
45+
print('\tLeaving "{}"'.format(name))
46+
return result
47+
48+
return tracer
49+
50+
# Let's start defining the commands:
51+
@trace
52+
def send_transaction_and_print_status(transaction):
53+
hex_hash = binascii.hexlify(IrohaCrypto.hash(transaction))
54+
print('Transaction hash = {}, creator = {}'.format(
55+
hex_hash, transaction.payload.reduced_payload.creator_account_id))
56+
net.send_tx(transaction)
57+
for status in net.tx_status_stream(transaction):
58+
print(status)
59+
60+
# For example, below we define a transaction made of 2 commands:
61+
# CreateDomain and CreateAsset.
62+
# Each of Iroha commands has its own set of parameters and there are many commands.
63+
# You can check out all of them here:
64+
# https://iroha.readthedocs.io/en/main/develop/api/commands.html
65+
@trace
66+
def create_domain_and_asset():
67+
"""
68+
Create domain 'domain' and asset 'coin#domain' with precision 2
69+
"""
70+
commands = [
71+
iroha.command('CreateDomain', domain_id='domain', default_role='user'),
72+
iroha.command('CreateAsset', asset_name='coin',
73+
domain_id='domain', precision=2)
74+
]
75+
# And sign the transaction using the keys from earlier:
76+
tx = IrohaCrypto.sign_transaction(
77+
iroha.transaction(commands), ADMIN_PRIVATE_KEY)
78+
send_transaction_and_print_status(tx)
79+
# You can define queries
80+
# (https://iroha.readthedocs.io/en/main/develop/api/queries.html)
81+
# the same way.
82+
83+
@trace
84+
def add_coin_to_admin():
85+
"""
86+
Add 1000.00 units of 'coin#domain' to 'admin@test'
87+
"""
88+
tx = iroha.transaction([
89+
iroha.command('AddAssetQuantity',
90+
asset_id='coin#domain', amount='1000.00')
91+
])
92+
IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY)
93+
send_transaction_and_print_status(tx)
94+
tx_tms = tx.payload.reduced_payload.created_time
95+
print(tx_tms)
96+
first_time, last_time = tx_tms - 1, tx_tms + 1
97+
return first_time, last_time
98+
99+
@trace
100+
def create_account_userone():
101+
"""
102+
Create account 'userone@domain'
103+
"""
104+
tx = iroha.transaction([
105+
iroha.command('CreateAccount', account_name='userone', domain_id='domain',
106+
public_key=user_public_key)
107+
])
108+
IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY)
109+
send_transaction_and_print_status(tx)
110+
111+
112+
@trace
113+
def transfer_coin_from_admin_to_userone():
114+
"""
115+
Transfer 2.00 'coin#domain' from 'admin@test' to 'userone@domain'
116+
"""
117+
tx = iroha.transaction([
118+
iroha.command('TransferAsset', src_account_id='admin@test', dest_account_id='userone@domain',
119+
asset_id='coin#domain', description='init top up', amount='2.00')
120+
])
121+
IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY)
122+
send_transaction_and_print_status(tx)
123+
124+
125+
@trace
126+
def userone_grants_to_admin_set_account_detail_permission():
127+
"""
128+
Make 'admin@test' able to set detail to 'userone@domain'
129+
"""
130+
tx = iroha.transaction([
131+
iroha.command('GrantPermission', account_id='admin@test',
132+
permission=can_set_my_account_detail)
133+
], creator_account='userone@domain')
134+
IrohaCrypto.sign_transaction(tx, user_private_key)
135+
send_transaction_and_print_status(tx)
136+
137+
138+
@trace
139+
def set_age_to_userone():
140+
"""
141+
Set age to 'userone@domain' by 'admin@test'
142+
"""
143+
tx = iroha.transaction([
144+
iroha.command('SetAccountDetail',
145+
account_id='userone@domain', key='age', value='18')
146+
])
147+
IrohaCrypto.sign_transaction(tx, ADMIN_PRIVATE_KEY)
148+
send_transaction_and_print_status(tx)
149+
150+
151+
@trace
152+
def get_coin_info():
153+
"""
154+
Get asset info for 'coin#domain'
155+
:return:
156+
"""
157+
query = iroha.query('GetAssetInfo', asset_id='coin#domain')
158+
IrohaCrypto.sign_query(query, ADMIN_PRIVATE_KEY)
159+
160+
response = net.send_query(query)
161+
data = response.asset_response.asset
162+
print('Asset id = {}, precision = {}'.format(data.asset_id, data.precision))
163+
164+
165+
@trace
166+
def get_account_assets():
167+
"""
168+
List all the assets of 'userone@domain'
169+
"""
170+
query = iroha.query('GetAccountAssets', account_id='userone@domain')
171+
IrohaCrypto.sign_query(query, ADMIN_PRIVATE_KEY)
172+
173+
response = net.send_query(query)
174+
data = response.account_assets_response.account_assets
175+
for asset in data:
176+
print('Asset id = {}, balance = {}'.format(
177+
asset.asset_id, asset.balance))
178+
179+
@trace
180+
def query_transactions(first_time = None, last_time = None,
181+
first_height = None, last_height = None):
182+
query = iroha.query('GetAccountTransactions', account_id = ADMIN_ACCOUNT_ID,
183+
first_tx_time = first_time,
184+
last_tx_time = last_time,
185+
first_tx_height = first_height,
186+
last_tx_height = last_height,
187+
page_size = 3)
188+
IrohaCrypto.sign_query(query, ADMIN_PRIVATE_KEY)
189+
response = net.send_query(query)
190+
data = response
191+
print(data)
192+
193+
@trace
194+
def get_userone_details():
195+
"""
196+
Get all the kv-storage entries for 'userone@domain'
197+
"""
198+
query = iroha.query('GetAccountDetail', account_id='userone@domain')
199+
IrohaCrypto.sign_query(query, ADMIN_PRIVATE_KEY)
200+
201+
response = net.send_query(query)
202+
data = response.account_detail_response
203+
print('Account id = {}, details = {}'.format('userone@domain', data.detail))
204+
205+
# Let's run the commands defined previously:
206+
create_domain_and_asset()
207+
first_time, last_time = add_coin_to_admin()
208+
create_account_userone()
209+
transfer_coin_from_admin_to_userone()
210+
userone_grants_to_admin_set_account_detail_permission()
211+
set_age_to_userone()
212+
get_coin_info()
213+
get_account_assets()
214+
get_userone_details()
215+
# set timestamp to correct value
216+
# for more protobuf timestamp api info see:
217+
# https://googleapis.dev/python/protobuf/latest/google/protobuf/timestamp_pb2.html
218+
first_tx_time = Timestamp()
219+
first_tx_time.FromMilliseconds(first_time)
220+
last_tx_time = Timestamp()
221+
last_tx_time.FromMilliseconds(last_time)
222+
# query for txs in measured time
223+
print('transactions from time interval query: ')
224+
query_transactions(first_tx_time, last_tx_time)
225+
# query for txs in given height range
226+
print('transactions from height range query: ')
227+
query_transactions(first_height = 2, last_height = 3)
228+
print('done')

0 commit comments

Comments
 (0)