-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathactions.py
More file actions
465 lines (376 loc) · 14.3 KB
/
Copy pathactions.py
File metadata and controls
465 lines (376 loc) · 14.3 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import requests
import json
import time
import os
from genesis_blockchain_tools.contract import Contract
from libs import api, db, tools, loger, check
wait = tools.read_config('test')['wait_tx_status']
log = loger.create_loger(__name__)
def login(url, pr_key, role=0, ecosystem=1):
token, uid, network_id = api.getuid(url)
result = api.login(
url=url,
token=token,
uid=uid,
network_id=network_id,
private_key=pr_key,
role_id=role,
ecosystem=ecosystem
)
return result
def call_contract(url, prKey, name, data, jvtToken, ecosystem=1):
schema = api.contract(url, jvtToken, name)
contract = Contract(schema=schema,
private_key=prKey,
ecosystem_id=ecosystem,
params=data)
tx_bin_data = {'call1': contract.concat()}
result = api.send_tx(url, jvtToken, tx_bin_data)
log.info(result)
if 'hashes' in result:
return result['hashes']['call1']
else:
return result
def call_multi_contract(url, prKey, name, data, jvtToken, withData=True):
full_bindata = {}
i = 1
for inf in data:
if withData and inf['params']['Data'] == '[]':
continue
schema = api.contract(url, jvtToken, inf['contract'])
contract = Contract(
schema=schema,
private_key=prKey,
params=inf['params']
)
tx_bin_data = contract.concat()
full_bindata.update({'call' + str(i): tx_bin_data})
i += 1
result = api.send_tx(url, jvtToken, full_bindata)
return result
def tx_status(url, sleep_time, hsh, jvt_token):
sec = 0
while sec < sleep_time:
time.sleep(1)
resp = api.tx_status(url, jvt_token, hsh)
jresp = resp['results'][hsh]
if (len(jresp['blockid']) > 0 and 'errmsg' not in json.dumps(jresp)) or ('errmsg' in json.dumps(jresp)):
break
else:
sec = sec + 1
if 'errmsg' not in jresp and jresp['blockid'] == '':
return {'blockid': None, 'result': None, 'error': None}
if 'errmsg' not in jresp and int(jresp['blockid']) > 0:
return {'blockid': int(jresp['blockid']), 'result': jresp['result'], 'error': None}
else:
return {'blockid': 0, 'error': jresp['errmsg']['error'], 'result': None}
def tx_status_multi(url, sleep_time, hshs, jvt_token):
url_end = url + '/txstatus'
allTxInBlocks = False
sec = 0
list = []
for hash in hshs:
list.append(hshs[hash])
while sec < sleep_time:
time.sleep(1)
resp = requests.post(url_end, params={'data': json.dumps({'hashes': list})},
headers={'Authorization': jvt_token})
jresp = resp.json()['results']
for status in jresp.values():
if (len(status['blockid']) > 0 and 'errmsg' not in json.dumps(status)):
allTxInBlocks = True
else:
allTxInBlocks = False
if allTxInBlocks:
return jresp
else:
sec = sec + 1
return jresp
def call_get_api(url, data, token):
resp = requests.get(url, params=data, headers={'Authorization': token})
return resp.json()
def call_get_api_with_full_response(url, data, token):
resp = requests.get(url, params=data, headers={'Authorization': token})
return resp
def call_post_api(url, data, token):
resp = requests.post(url, data=data, headers={'Authorization': token})
return resp.json()
def get_count(url, name, token):
res = api.list(url, token, name, limit=1)
return res['count']
def get_list(url, type, token, app_id=0):
count = get_count(url, type, token)
res = api.list(url, token, type, limit=count)
res_list = []
i = 0
if app_id > 0:
for el in res['list']:
if int(el['app_id']) == app_id:
res_list.append(el)
i += 1
return {'count': i, 'list': res_list}
else:
return res
def get_contract_id(url, name, token):
res = api.contract(url, token, name)
return res['tableid']
def get_object_id(url, name, object, token, ecosystem=1):
id = None
param_name = 'name'
if object == 'members':
param_name = 'member_name'
res = get_list(url, object, token)
for object in res['list']:
if object[param_name] == name:
if ecosystem > 0:
if int(object['ecosystem']) == ecosystem:
id = object['id']
break
else:
id = object['id']
break
return id
def get_object_name(url, id, object, token):
name = None
res = get_list(url, object, token)
for object in res['list']:
if object['id'] == id:
name = object['name']
break
return name
def is_contract_activated(url, name, token):
res = api.contract(url, token, name)
if res['walletid'] == '0':
return False
else:
return True
def get_activated_wallet(url, name, token):
res = api.contract(url, token, name)
return res['walletid']
def get_parameter_id(url, name, token):
res = api.ecosystemparam(url, token, name)
return res['id']
def get_parameter_value(url, name, token):
res = api.ecosystemparam(url, token, name)
return res['value']
def get_sysparams_value(url, token, name):
list = api.systemparams(url, token, name)
return list['list'][0]['value']
def get_content(url, type, name, lang, app_id, token):
res = api.content(url, token, type, name, lang, app_id)
return res
def get_max_block_id(url, token):
res = api.maxblockid(url, token)
return res['max_block_id']
# limits
def is_count_tx_in_block(url, token, max_block_id, count_tx):
block = max_block_id - 3
while block < max_block_id:
info = api.block(url, token, block)
block += 1
if int(info['tx_count']) > count_tx:
log.error('Error in count_tx. Block ' + str(block) + ' has ' +
str(info['count_tx']) + ' transactions')
return False
return True
# api
def get_ecosys_tables(url, token):
count = get_count(url, 'tables', token)
tables = api.tables(url, token, limit=count)['list']
list = []
for table in tables:
list.append(table['name'])
return list
# system_contracts
def get_export_app_data(url, token, app_id, member_id):
result = api.list(url, token, 'binaries')
for item in result['list']:
if item['name'] == 'export' \
and item['app_id'] == str(app_id) \
and item['member_id'] == str(member_id):
return str(item['data'])
return None
# system_contracts
def get_import_app_data(url, token, member_id):
result = api.list(url, token, 'buffer_data')
for item in result['list']:
if item['key'] == 'import' \
and item['member_id'] == str(member_id):
return item['value']
return None
# block_chain compare_nodes
def get_count_DB_objects(url, token):
tables = {}
list = api.tables(url, token, limit=1000)['list']
for table in list:
tables[table['name']] = table['count']
return tables
def get_table_hashes(url, token, db_inf, ecos='1'):
tables = {}
list = api.tables(url, token, limit=1000)['list']
for table in list:
tables[table['name']] = db.get_table_hash(
db_inf, ecos + '_' + table['name'])
return tables
# done
def get_user_token_amounts(url, token):
count = get_count(url, 'keys', token)
keys = api.list(url, token, 'keys', limit=count)
amounts = []
for item in keys['list']:
amounts.append(int(item['amount']))
amounts.sort()
return amounts
# cost
def get_balance_by_id(url, token, key_id, ecos=1):
count = get_count(url, 'keys', token)
keys = api.list(url, token, 'keys', limit=count)
for item in keys['list']:
if item['id'] == str(key_id) \
and item['ecosystem'] == str(ecos):
return item['amount']
return None
# API
def is_wallet_created(url, token, id):
count = get_count(url, 'keys', token)
keys = api.list(url, token, 'keys', limit=count)
for item in keys['list']:
if item['id'] == str(id) \
and int(item['amount']) == 1000000000000000000000:
return True
return False
# cost
def is_commission_in_history(url, token, id_from, id_to, summ):
count = get_count(url, 'history', token)
table = api.list(url, token, 'history', limit=count)
for item in table['list']:
if item['sender_id'] == str(id_from) \
and item['recipient_id'] == str(id_to):
if item['amount'] == str(summ):
return True
return False
def get_commission_from_history(url, token, id_from, id_to, block_id):
count = get_count(url, 'history', token)
table = api.list(url, token, 'history', offset=int(count)-5, limit=5)
amounts = []
for item in table['list']:
if item['sender_id'] == str(id_from) \
and item['recipient_id'] == str(id_to) \
and item['block_id'] == str(block_id):
amounts.append(int(item['amount']))
return amounts
def is_contract_present(url, token, name):
ans = api.contract(url, token, name)
if 'error' not in ans:
return True
if ans['error'] == 'E_CONTRACT':
return False
else:
return True
def imp_app(app_name, url, pr_key, token):
log.info('Start install "{app_name}"'.format(app_name=app_name))
path = os.path.join(os.getcwd(), 'fixtures', 'basic', app_name + '.json')
resp = call_contract(url, pr_key, 'ImportUpload',
{'input_file': {'Path': path}}, token)
res_import_upload = tx_status(url, wait, resp, token)
if int(res_import_upload['blockid']) > 0:
founder_id = get_parameter_value(url, 'founder_account', token)
bufer_data_list = get_list(url, 'buffer_data', token)['list']
for item in bufer_data_list:
if item['key'] == 'import' \
and item['member_id'] == founder_id:
import_app_data = json.loads(item['value'])['data']
break
contract_name = 'Import'
data = [{'contract': contract_name,
'params': import_app_data[i]} for i in range(len(import_app_data))]
resp = call_multi_contract(url, pr_key, contract_name, data, token)
time.sleep(wait)
if 'hashes' in resp:
hashes = resp['hashes']
result = tx_status_multi(url, wait, hashes, token)
for status in result.values():
log.debug(str(status))
if int(status['blockid']) < 1:
log.error('Import "{app_name}" is failed'.format(app_name=app_name))
exit(1)
log.info('"{app_name}" successfully installed'.format(app_name=app_name))
def get_load_blocks_time(url, token, max_block, wait_upating):
sec = 1
while sec < wait_upating:
max_block_id1 = get_max_block_id(url, token)
if max_block_id1 == max_block:
return {'time': sec, 'blocks': max_block_id1}
else:
sec = sec + 1
return {'time': wait_upating + 1, 'blocks': max_block_id1}
def roles_install(url, pr_key, token, wait):
data = {}
log.info('RolesInstall started')
call = call_contract(url, pr_key, 'RolesInstall',
data, token)
if check.is_tx_in_block(url, wait, {'hash': call}, token) < 1:
log.error('RolesInstall is failed')
exit(1)
def voting_templates_install(url, pr_key, token, wait):
data = {}
log.info('VotingTemplatesInstall started')
call = call_contract(url, pr_key, 'VotingTemplatesInstall',
data, token)
if check.is_tx_in_block(url, wait, {'hash': call}, token) < 1:
log.error('VotingTemplatesInstall is failed')
exit(1)
def edit_app_param(name, val, url, pr_key, token, wait):
log.info('EditAppParam started')
id = get_object_id(url, name, 'app_params', token)
data = {'Id': id, 'Value': val, 'Conditions': 'true'}
call = call_contract(url, pr_key, 'EditAppParam',
data, token)
if check.is_tx_in_block(url, wait, {'hash': call}, token) < 1:
log.error('EditAppParam ' + name + ' is failed')
exit(1)
def update_profile(name, url, pr_key, token, wait):
log.info('UpdateProfile started')
time.sleep(5)
data = {'member_name': name}
resp = call_contract(url, pr_key, 'ProfileEdit',
data, token)
if check.is_tx_in_block(url, wait, {'hash': resp}, token) < 1:
log.error('UpdateProfile ' + name + ' is failed')
exit(1)
def set_apla_consensus(id, url, pr_key, token, wait):
log.info('setAplaconsensus started')
data = {'member_id': id, 'rid': 3}
call = call_contract(url, pr_key, 'RolesAssign',
data, token)
if check.is_tx_in_block(url, wait, {'hash': call}, token) < 1:
log.error('RolesAssign ' + id + ' is failed')
exit(1)
def create_voiting(tcp_address, api_address, key_id, pub_key, url, pr_key, token, wait):
log.info('VotingNodeAdd started')
data = {'TcpAddress': tcp_address, 'ApiAddress': api_address,
'KeyId': key_id, 'PubKey': pub_key, 'Duration': 1}
call = call_contract(url, pr_key, 'VotingNodeAdd',
data, token)
if check.is_tx_in_block(url, wait, {'hash': call}, token) < 1:
log.error('VotingNodeAdd is failed')
exit(1)
def voting_status_update(url, pr_key, token, wait):
log.info('VotingStatusUpdate started')
data = {}
call = call_contract(url, pr_key, 'VotingStatusUpdate',
data, token)
if check.is_tx_in_block(url, wait, {'hash': call}, token) < 1:
log.error('VoitingStatusUpdate is failed')
exit(1)
def voiting(id, url, pr_key, token, wait):
log.info('VotingDecisionAccept started')
data = {'votingID': id,
'RoleId': 3}
call = call_contract(url, pr_key, 'VotingDecisionAccept',
data, token)
if check.is_tx_in_block(url, wait, {'hash': call}, token) < 1:
log.error('VotingDecisionAccept ' + id + ' is failed')
exit(1)
return False
return True