forked from hyperledger-iroha/iroha-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquery_transactions.py
More file actions
228 lines (199 loc) · 7.46 KB
/
query_transactions.py
File metadata and controls
228 lines (199 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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')