-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiroha_functions.py
More file actions
345 lines (296 loc) · 12.6 KB
/
iroha_functions.py
File metadata and controls
345 lines (296 loc) · 12.6 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env python3
from iroha.primitive_pb2 import can_set_my_account_detail
from iroha import IrohaCrypto
import binascii
import iroha_config as iroha_config
import sys
if sys.version_info[0] < 3:
raise Exception('Python 3 or a more recent version is required.')
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
@trace
def send_transaction_and_print_status(transaction):
"""
Send a transaction to the Blockchain (BSMD)
:param transaction: Transaction we are sending to the BSMD
:return: null:
"""
# print(transaction)
hex_hash = binascii.hexlify(IrohaCrypto.hash(transaction))
# print('Transaction hash = {}, creator = {}'.format(
# hex_hash, transaction.payload.reduced_payload.creator_account_id))
iroha_config.network.send_tx(transaction)
# for status in iroha_config.network.tx_status_stream(transaction):
# print(status)
@trace
def create_domain_and_asset():
"""
Create a domain, default user and define asset
:return: null
"""
# print(iroha_config.domain_id, iroha_config.default_role, iroha_config.asset_name, iroha_config.domain_id, iroha_config.asset_precision, iroha_config.admin_private_key)
commands = [iroha_config.iroha.command('CreateDomain', domain_id=iroha_config.domain_id,
default_role=iroha_config.default_role),
iroha_config.iroha.command('CreateAsset', asset_name=iroha_config.asset_name,
domain_id=iroha_config.domain_id, precision=iroha_config.asset_precision)]
tx = IrohaCrypto.sign_transaction(iroha_config.iroha.transaction(commands), iroha_config.admin_private_key)
send_transaction_and_print_status(tx)
@trace
def create_account_user(name, public_key, domain_id, asset_qty, asset_id):
"""
Create a personal account. This function works in three steps
1. Create an account with a name, in a domain and a public key
2. The admin create credit (assets) for the account (credit is created only if the user
buy it)
3. The admin transfer the credit to the user
:param name: (str) Name of the node we are creating
:param public_key: (str) public key of the node
:param domain_id: (str) Name of the domain the user wants to join
:param asset_qty: (float) Quantity of assets the node buy
:param asset_id: (name#domain) Name of asset the node buy
:return: null:
Usage example:
create_account_user('Tommy', 'key', 'federated', '5', fedcoin#federated)
"""
# 1. Create account
tx = iroha_config.iroha.transaction(
[iroha_config.iroha.command('CreateAccount',
account_name=name,
domain_id=domain_id,
public_key=public_key)])
IrohaCrypto.sign_transaction(tx, iroha_config.admin_private_key)
send_transaction_and_print_status(tx)
# 2. Create credit for the user
tx = iroha_config.iroha.transaction([iroha_config.iroha.command('AddAssetQuantity',
asset_id=asset_id,
amount=asset_qty)])
IrohaCrypto.sign_transaction(tx, iroha_config.admin_private_key)
send_transaction_and_print_status(tx)
# 3. Transfer credit to the user
dest_account_id = name + '@' + domain_id
tx = iroha_config.iroha.transaction([
iroha_config.iroha.command('TransferAsset',
src_account_id='admin@test',
dest_account_id=dest_account_id,
asset_id=asset_id,
description='initial credit',
amount=asset_qty)])
IrohaCrypto.sign_transaction(tx, iroha_config.admin_private_key)
send_transaction_and_print_status(tx)
@trace
def get_balance(iroha, account_id, private_key):
"""
Get the balance of the account
:param iroha: (Iroha('name@domain')) Address for connecting to a domain
:param account_id: (name@domain) Id of the user in the domain
:param private_key: (str) Private key of the user
:return: data: (array) asset id and assets quantity
Usage example:
get_balance(Iroha('david@federated'), IrohaGrpc('127.0.0.1'), 'david@federated', 'key')
Return example:
[asset_id: "fedcoin#federated"
account_id: "generator@federated"
balance: "1000"
]
"""
query = iroha.query('GetAccountAssets',
account_id=account_id)
IrohaCrypto.sign_query(query, private_key)
response = iroha_config.network.send_query(query)
data = response.account_assets_response.account_assets
for asset in data:
print('Asset id = {}, balance = {}'.format(asset.asset_id, asset.balance))
return data
@trace
def grants_access_to_set_details(iroha, my_id_account, private_key, grant_account_id):
"""
Grant access to write details in own identity
:param iroha: (Iroha('name@domain')) Address for connecting to a domain
:param my_id_account: (name@domain) Id of the user granting the access
:param private_key: (str) Private key of the user granting the access
:param grant_account_id: (name@domain) Id of the user we want to grant the access
:return: null:
Usage example:
grants_access_to_set_details(Iroha('david@federated'),IrohaGrpc('127.0.0.1'), 'david@federated','key',
'juan@federated')
"""
tx = iroha.transaction([
iroha.command('GrantPermission',
account_id=grant_account_id,
permission=can_set_my_account_detail)
],
creator_account=my_id_account)
IrohaCrypto.sign_transaction(tx, private_key)
send_transaction_and_print_status(tx)
@trace
def set_detail_to_node(iroha, account_id, private_key, detail_key, detail_value):
"""
Set the details of a node. In federated learning the details are in JSON format and
contains the address (location) where the weight is stored (if the weight is small enough it can be
embedded to the block if needed)
:param iroha: (Iroha('name@domain')) Address for connecting to a domain
:param account_id: (name@domain) Id of the user in the domain
:param private_key: (str) Private key of the user
:param detail_key: (str) Name of the detail we want to set
:param detail_value: (str) Value of the detail
:return: null:
Usage example:
set_detail_to_node(Iroha('david@federated'),IrohaGrpc('127.0.0.1'), 'david@federated', 'key', 'age', '33')
"""
tx = iroha.transaction([
iroha.command('SetAccountDetail',
account_id=account_id,
key=detail_key,
value=detail_value)
])
IrohaCrypto.sign_transaction(tx, private_key)
send_transaction_and_print_status(tx)
@trace
def transfer_assets(iroha, account_id, private_key, destination_account, asset_id, quantity, description):
"""
Transfer assets from one account to another
:param iroha: (Iroha(name@domain)) Address for connecting to a domain
:param account_id: (name@domain) Id of the user in the domain
:param private_key: (str) Private key of the user
:param destination_account: (name@domain) Id of the destination account
:param asset_id: (name#domain) Id of the asset we want to transfer
:param quantity: (float) Number of assets we want to transfer
:param description: (str) Small message to the receiver of assets
:return: null:
Usage example:
transfer_assets(Iroha('david@federated'),IrohaGrpc('127.0.0.1'), 'david@federated', 'key',
'toro@federated', 'fedcoin#federated', '2', 'Shut up and take my money')
"""
tx = iroha.transaction([
iroha.command('TransferAsset',
src_account_id=account_id,
dest_account_id=destination_account,
asset_id=asset_id,
description=description,
amount=quantity)
])
IrohaCrypto.sign_transaction(tx, private_key)
send_transaction_and_print_status(tx)
@trace
def get_detail_from_generator(iroha, account_id, private_key, generator_id, detail_id):
"""
Consult a single detail writen by some generator
:param iroha: (Iroha(name@domain)) Address for connecting to a domain
:param account_id: (name@domain) Id of the user in the domain
:param private_key: (str) Private key of the user
:param generator_id: (name@domain) Id of the user who create de detail
:param detail_id: (string) Name of the detail
:return: data: (json) solicited details of the user
Usage example:
get_detail_from_generator(Iroha('david@federated'),IrohaGrpc('127.0.0.1'), 'david@federated', 'key',
'david@federated', 'Address')
Return example:
{
"nodeA@domain":{
"Age":"35"
}
}
"""
query = iroha.query('GetAccountDetail',
account_id=account_id,
writer=generator_id,
key=detail_id)
IrohaCrypto.sign_query(query, private_key)
response = iroha_config.network.send_query(query)
data = response.account_detail_response
print('Account id = {}, details = {}'.format(account_id, data.detail))
return data.detail
@trace
def get_all_details_from_generator(iroha, account_id, private_key, generator_id):
"""
Consult all the details generated by some node
:param iroha: (Iroha(name@domain)) Address for connecting to a domain
:param account_id: (name@domain) Id of the user in the domain
:param private_key: (str) Private key of the user
:param generator_id: (name@domain) Id of the user who create de detail
:return: data: (json) solicited details of the user
Usage example:
get_detail_from_generator(Iroha('david@federated'),IrohaGrpc('127.0.0.1'), 'david@federated', 'key',
'david@federated')
Return example:
{
"nodeA@domain":{
"Age":"35",
"Name":"Quetzacolatl"
}
}
"""
query = iroha.query('GetAccountDetail',
account_id=account_id,
writer=generator_id)
IrohaCrypto.sign_query(query, private_key)
response = iroha_config.network.send_query(query)
data = response.account_detail_response
print('Account id = {}, details = {}'.format(account_id, data.detail))
return data.detail
@trace
def get_all_details(iroha, account_id, private_key):
"""
Consult all details of the node
:param iroha: (Iroha(name@domain)) Address for connecting to a domain
:param account_id: (name@domain) Id of the user in the domain
:param private_key: (str) Private key of the user
:return: data: (json) solicited details of the user
Usage example:
get_detail_from_generator(Iroha('david@federated'),IrohaGrpc('127.0.0.1'), 'david@federated', 'key')
Return example:
{
"nodeA@domain":{
"Age":"35",
"Name":"Quetzacoatl"
},
"nodeB@domain":{
"Location":"35.3333535,-45.2141556464",
"Status":"valid"
},
"nodeA@domainB":{
"FederatingParam":"35.242553",
"Loop":"3"
}
}
"""
query = iroha.query('GetAccountDetail',
account_id=account_id)
IrohaCrypto.sign_query(query, private_key)
response = iroha_config.network.send_query(query)
data = response.account_detail_response
print('Account id = {}, details = {}'.format(account_id, data.detail))
return data.detail
@trace
def get_block(height):
"""
Get the balance of the account
:param iroha: (Iroha('name@domain')) Address for connecting to a domain
:param network: (IrohaGrpc('IP address')) Physical address of one node running the BSMD
:param account_id: (name@domain) Id of the user in the domain
:param private_key: (str) Private key of the user
:return: data: (array) asset id and assets quantity
Usage example:
get_balance(Iroha('david@federated'), IrohaGrpc('127.0.0.1'), 'david@federated', 'key')
Return example:
[asset_id: "fedcoin#federated"
account_id: "generator@federated"
balance: "1000"
]
"""
iroha_config.iroha.blocks_query()
query = iroha_config.iroha.query('GetBlock',
height=height)
IrohaCrypto.sign_query(query, iroha_config.admin_private_key)
block = iroha_config.network.send_query(query)
print(block)
return block