-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
549 lines (433 loc) · 20 KB
/
utilities.py
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
import pytz
import argparse
import os
import json
import toml
import datetime
import psutil
from datetime import datetime
import matplotlib.pyplot as plt
import networkx as nx
import subprocess
import hashlib
import constants as c
# Functions
DT_FRMT = '%Y-%m-%dT%H:%M:%SZ'
def send_cmd_over_ssh(host, user, command, print_flag):
result = subprocess.check_output('ssh %s@%s %s' % (user, host, command), shell=True)
decoded_ret = result.decode('UTF-8')
if print_flag:
print(decoded_ret)
return decoded_ret.split('\n')
def scp_get(host, user, remote_file, local_folder):
scp_cmd = 'scp %s@%s:%s %s%s%s' % (user, host, remote_file, local_folder, os.sep, remote_file.split(os.sep)[-1])
print(scp_cmd)
os.system(scp_cmd)
def scp_send(host, user, local_file, remote_file):
scp_cmd = 'scp %s %s@%s:%s' % (local_file, user, host, remote_file)
print(scp_cmd)
os.system(scp_cmd)
def exec_real_cmd(host, user, real_cmd, print_flag):
try:
if print_flag:
print('Remote command: %s' % real_cmd)
print('Result:')
res = send_cmd_over_ssh(host=host, user=user, command=real_cmd, print_flag=print_flag)
if print_flag:
print('****')
return res
except Exception as e:
print('EXCEPTION: %s' % str(e))
return None
# Get the first MAC address
def get_eth_mac(host, user, eth_interface):
# I apologize for the following code, if you try something better please send a PR
# Run the remote command
if user == 'pi':
ret = exec_real_cmd(host, user, 'sudo ifconfig %s' % eth_interface, False)
else:
ret = exec_real_cmd(host, user, 'ifconfig %s' % eth_interface, False)
for idx in range(0, len(ret)):
if 'Ethernet' in ret[idx]:
res = ret[idx].replace(' ', ' ').replace(' ', ';').replace(';;', '').split(';')
break
return res[1]
def get_real_account(host, user, account):
if 'hashed_mac' in account:
tmp = account.split(';')
if len(tmp) == 2:
eth_mac = get_eth_mac(host, user, tmp[1])
else:
eth_mac = get_eth_mac(host, user, 'eth0')
# Encode the MAC address
h = hashlib.new('sha512')
h.update(str.encode(eth_mac))
real_account = h.hexdigest()
else:
real_account = account
return real_account
def init_chain(node, host, user, remote_goroot, cfg, account, tokens_string, tokens_to_stake):
chain_name = cfg['tendermint']['chainName']
app = '%sd' % cfg['tendermint']['app']
# Setup the main configuration
exec_real_cmd(host, user, '%s/bin/%s init %s --chain-id %s' % (remote_goroot, app, node, chain_name), False)
exec_real_cmd(host, user, '%s/bin/%s config chain-id %s' % (remote_goroot, app, chain_name), False)
exec_real_cmd(host, user, '%s/bin/%s config keyring-backend test' % (remote_goroot, app), False)
# Get the real account
real_account = get_real_account(host, user, account)
# Setup the part related to the node key and address
exec_real_cmd(host, user, '%s/bin/%s keys add %s' % (remote_goroot, app, real_account), False)
address = exec_real_cmd(host, user, '%s/bin/%s keys show %s -a' % (remote_goroot, app, real_account), True)
exec_real_cmd(host, user, '%s/bin/%s add-genesis-account %s %s' % (remote_goroot, app, address[0], tokens_string), False)
if node in cfg['validators']:
exec_real_cmd(host, user, '%s/bin/%s gentx %s %s --chain-id %s' % (remote_goroot, app, real_account, tokens_to_stake, chain_name), False)
exec_real_cmd(host, user, '%s/bin/%s collect-gentxs' % (remote_goroot, app), False)
exec_real_cmd(host, user, '%s/bin/%s validate-genesis' % (remote_goroot, app), False)
def help_args():
print('\n*** Input arguments\n')
print('\t $ python handle_sidechain.py -f $F -n $N -c $C -p $P')
print('')
for help_arg in c.HELP_ARGS:
print(' * %s' % help_arg)
print()
def help_commands():
print('*** Available commands\n')
for help_cmd in c.HELP_CMDS:
print(' * %s: %s' % (help_cmd['cmd'], help_cmd['desc']))
print('\tExample: %s' % help_cmd['example'])
print()
def exec_cmd(cfg, node, cmd, param):
# Define main variables
host = cfg['nodes'][node][2]
user = cfg['nodes'][node][3]
remote_goroot = cfg['nodes'][node][4]
account = cfg['nodes'][node][5]
tokens_string = cfg['nodes'][node][6]
tokens_to_stake = cfg['nodes'][node][7]
# Get the node status
print('**** NODE: %s@%s; COMMAND: \'%s\'' % (user, host, cmd))
# Start the node
if cmd == 'start':
# ATTENTION: After a restart you have to wait the first empty block before performing transactions
real_cmd = 'nohup %s/bin/%sd start --log_format json >> /home/%s/log/%s.log 2>&1 &' % (remote_goroot, cfg['tendermint']['app'], user, cfg['tendermint']['app'])
print('Remote command: %s' % real_cmd)
# This command does not work with ParallelSSHClient class
subprocess.Popen(['ssh', '%s@%s' % (user, host), real_cmd], shell=False,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print('****')
else:
# Apply the setup on the sideschain
if cmd == 'apply_setup':
# Send the configuration TOML files
local_file = '%s/to_sidechain/toml_config/%s.toml' % (cfg['dataFolder'], node)
remote_file = '/home/%s/.%s/config/config.toml' % (user, cfg['tendermint']['app'])
scp_send(host, user, local_file, remote_file)
local_file = '%s/to_sidechain/toml_app/%s.toml' % (cfg['dataFolder'], node)
remote_file = '/home/%s/.%s/config/app.toml' % (user, cfg['tendermint']['app'])
scp_send(host, user, local_file, remote_file)
# Send the genesis file
local_file = '%s/to_sidechain/%s.json' % (cfg['dataFolder'], cfg['genesisFileGeneratorNode'])
remote_file = '/home/%s/.%s/config/genesis.json' % (user, cfg['tendermint']['app'])
scp_send(host, user, local_file, remote_file)
# Get the node disk occupancy
if cmd == 'check_disk_size':
real_cmd = 'df -h | grep /dev'
exec_real_cmd(host, user, real_cmd, True)
# Get the sidechain disk occupancy
if cmd == 'check_sidechain_size':
real_cmd = 'du -sh /home/%s/.%s' % (user, cfg['tendermint']['app'])
exec_real_cmd(host, user, real_cmd, True)
# Delete the sidechain
if cmd == 'delete':
real_cmd = 'rm -rf /home/%s/.%s' % (user, cfg['tendermint']['app'])
exec_real_cmd(host, user, real_cmd, False)
# Get the genesis file
if cmd == 'get_genesis_file':
local_folder = '%s/from_sidechain/genesis_files' % cfg['dataFolder']
scp_get(host, user, '/home/%s/.%s/config/genesis.json' % (user, cfg['tendermint']['app']), local_folder)
print('Rename %s/genesis.json to %s/%s.json' % (local_folder, local_folder, node))
os.rename('%s/genesis.json' % local_folder, '%s/%s.json' % (local_folder, node))
if cmd == 'get_toml_reference_file':
if node == cfg['tomlFilesReferenceNode']:
scp_get(host, user, '/home/%s/.%s/config/app.toml' % (user, cfg['tendermint']['app']),
'%s/from_sidechain/toml_app' % cfg['dataFolder'])
scp_get(host, user, '/home/%s/.%s/config/config.toml' % (user, cfg['tendermint']['app']),
'%s/from_sidechain/toml_config' % cfg['dataFolder'])
# Get the node ID
if cmd == 'get_node_id':
real_cmd = '%s/bin/%sd tendermint show-node-id' % (remote_goroot, cfg['tendermint']['app'])
exec_real_cmd(host, user, real_cmd, True)
# Initialize the sidechain
if cmd == 'init':
init_chain(node, host, user, remote_goroot, cfg, account, tokens_string, tokens_to_stake)
# Get the node log
if cmd == 'log':
real_cmd = 'tail -%s /home/%s/log/%s.log' % (param, user, cfg['tendermint']['app'])
exec_real_cmd(host, user, real_cmd, True)
# Get the node ID
if cmd == 'save_node_id':
real_cmd = '%s/bin/%sd tendermint show-node-id' % (remote_goroot, cfg['tendermint']['app'])
node_id_info = exec_real_cmd(host, user, real_cmd, False)
fw = open('%s/from_sidechain/nodes_ids.txt' % cfg['dataFolder'], 'a')
fw.write('%s=%s@%s:%i\n' % (node, node_id_info[0], host, cfg['tendermint']['peerPort']))
fw.close()
# Show account info
if cmd == 'show_account_info':
real_cmd = '%s/bin/%sd keys show %s -a' % (remote_goroot, cfg['tendermint']['app'], get_real_account(host, user, account))
account_address = exec_real_cmd(host, user, real_cmd, False)
real_cmd = '%s/bin/%sd query account %s' % (remote_goroot, cfg['tendermint']['app'], account_address[0])
exec_real_cmd(host, user, real_cmd, True)
# Show key info
if cmd == 'show_key_info':
real_cmd = '%s/bin/%sd keys show %s' % (remote_goroot, cfg['tendermint']['app'], get_real_account(host, user, account))
exec_real_cmd(host, user, real_cmd, True)
# Get the node status
if cmd == 'status':
real_cmd = 'ps aux | grep -v grep | grep %s/bin/%s' % (remote_goroot, cfg['tendermint']['app'])
exec_real_cmd(host, user, real_cmd, True)
# Stop the node
if cmd == 'stop':
real_cmd = 'killall %sd' % cfg['tendermint']['app']
exec_real_cmd(host, user, real_cmd, False)
def clear(cfg):
if os.path.isfile('%s/from_sidechain/nodes_ids.txt' % cfg['dataFolder']):
os.unlink('%s/from_sidechain/nodes_ids.txt' % cfg['dataFolder'])
def check_cmd_availability(cmd):
for available_cmd in c.HELP_CMDS:
if available_cmd['cmd'] == cmd:
return True
return False
def run_cmd(node, cmd, cfg, param):
# Clear folder and files, which can eventually be used
if cmd == 'save_node_id':
clear(cfg)
if node == 'all':
# cycle over the nodes
for single_node in cfg['nodes'].keys():
# exec the command
exec_cmd(cfg, single_node, cmd, param)
else:
# exec the command
exec_cmd(cfg, node, cmd, param)
def check_data_folder(data_folder):
if os.path.isdir(data_folder) is False:
os.mkdir(data_folder)
os.mkdir('%s/from_sidechain' % data_folder)
os.mkdir('%s/from_sidechain/genesis_files' % data_folder)
os.mkdir('%s/from_sidechain/toml_app' % data_folder)
os.mkdir('%s/from_sidechain/toml_config' % data_folder)
os.mkdir('%s/to_sidechain/' % data_folder)
os.mkdir('%s/to_sidechain/toml_app' % data_folder)
os.mkdir('%s/to_sidechain/toml_config' % data_folder)
def clean_folder(folder):
print('Clean data folder %s' % folder)
# Cycle recursively in the given folder
for root, dirs, files in os.walk(folder):
for file in files:
print('Delete %s/%s' % (root, file))
os.unlink(os.path.join(root, file))
def draw_sidechain(cfg):
nodes_info = cfg['nodes']
edges = cfg['edges']
nodes = nodes_info.keys()
validators_pos = dict()
for validator in cfg['validators']:
validators_pos[validator] = [nodes_info[validator][0], nodes_info[validator][1]]
edges_with_validators = []
edges_without_validators = []
for edge in edges:
if edge[0] in cfg['validators'] and edge[1] in cfg['validators']:
edges_with_validators.append(edge)
else:
edges_without_validators.append(edge)
# Get the positions from the nodes info
nodes_pos = dict()
for node in nodes_info:
nodes_pos[node] = [nodes_info[node][0], nodes_info[node][1]]
G = nx.DiGraph()
G.add_nodes_from(nodes)
G.add_edges_from(edges)
nx.draw_networkx(G, with_labels=True, pos=nodes_pos, node_color='#FFFFFF', edge_color='#FFFFFF',
font_weight='bold', font_size=24, font_color='#000000')
nx.draw_networkx_nodes(G, nodes_pos, nodelist=nodes, node_color='#628CBD', node_size=600, alpha=1.0)
nx.draw_networkx_nodes(G, validators_pos, nodelist=list(validators_pos.keys()), node_color='#FF8000', node_size=600, alpha=1.0)
nx.draw_networkx_edges(G, nodes_pos, edgelist=edges_without_validators, width=4, alpha=1.0, edge_color='#228B22')
nx.draw_networkx_edges(G, nodes_pos, edgelist=edges_with_validators, width=4, alpha=1.0, edge_color='#FF4000')
plt.show()
# plt.savefig('chain.pdf')
def get_dt(str_dt, tz_local, flag_set_minute=True):
if str_dt == 'now':
dt = datetime.datetime.now()
elif str_dt == 'now_s00':
dt = datetime.datetime.now()
dt = dt.replace(second=0, microsecond=0)
else:
dt = datetime.datetime.strptime(str_dt, DT_FRMT)
dt = tz_local.localize(dt)
dt_utc = dt.astimezone(pytz.utc)
if flag_set_minute is True:
# Set the correct minute (0,15,30,45)
return set_start_minute(dt_utc)
else:
return dt_utc
def set_start_minute(dt_utc):
if 0 <= dt_utc.minute < 15:
return dt_utc.replace(minute=0, second=0, microsecond=0)
elif 15 <= dt_utc.minute < 30:
return dt_utc.replace(minute=15, second=0, microsecond=0)
elif 30 <= dt_utc.minute < 45:
return dt_utc.replace(minute=30, second=0, microsecond=0)
else:
return dt_utc.replace(minute=45, second=0, microsecond=0)
def set_end_minute(dt_utc):
if 0 <= dt_utc.minute < 15:
return dt_utc.replace(minute=14, second=59, microsecond=0)
elif 15 <= dt_utc.minute < 30:
return dt_utc.replace(minute=29, second=59, microsecond=0)
elif 30 <= dt_utc.minute < 45:
return dt_utc.replace(minute=44, second=59, microsecond=0)
else:
return dt_utc.replace(minute=59, second=59, microsecond=0)
def get_macs(family):
macs = dict()
for interface, snics in psutil.net_if_addrs().items():
for snic in snics:
if snic.family == family:
macs[interface] = snic.address
return macs
def check_configuration(cfg):
ids = get_nodes_ids_from_file(cfg)
print(ids)
nodes_conns = dict()
res = True
for node in cfg['nodes'].keys():
for edge in cfg['edges']:
if node == edge[0]:
if node not in nodes_conns.keys():
nodes_conns[node] = [ids[edge[1]]]
else:
if ids[edge[1]] in nodes_conns[node]:
print('ERROR: Edge %s -> %s already saved in the conf file' % (node, ids[edge[1]]))
res = False
nodes_conns[node].append(ids[edge[1]])
if res is True:
print('RESULT: Configuration OK')
else:
print('RESULT: Found errors in conf file %s' % args.c)
def create_setup(cfg):
ids = get_nodes_ids_from_file(cfg)
nodes_conns = dict()
for node in cfg['nodes'].keys():
for edge in cfg['edges']:
if node == edge[0]:
if node not in nodes_conns.keys():
nodes_conns[node] = [ids[edge[1]]]
else:
try:
nodes_conns[node].append(ids[edge[1]])
except Exception as e:
print('ERROR! Probably wrong configuration')
# Get file from
create_toml_files(nodes_conns, cfg)
genesis_data = dict()
validators_data = []
genesis_folder = '%s/from_sidechain/genesis_files' % cfg['dataFolder']
for node in cfg['nodes'].keys():
tmp_genesis_data = json.loads(open(os.path.expanduser('%s/%s.json' % (genesis_folder, node))).read())
if node != cfg['genesisFileGeneratorNode']:
genesis_data[node] = tmp_genesis_data
else:
genesis_result = tmp_genesis_data
# Get the configured validators
for validator in tmp_genesis_data['app_state']['genutil']['gen_txs']:
validators_data.append(validator)
# Append accounts and balances of nodes that will not generate the genesis file
for node in genesis_data.keys():
for account in genesis_data[node]['app_state']['auth']['accounts']:
genesis_result['app_state']['auth']['accounts'].append(account)
for balance in genesis_data[node]['app_state']['bank']['balances']:
genesis_result['app_state']['bank']['balances'].append(balance)
# Append the validators
genesis_result['app_state']['genutil']['gen_txs'] = []
for validator in validators_data:
genesis_result['app_state']['genutil']['gen_txs'].append(validator)
# Create the final genesis file
final_genesis_file = '%s/to_sidechain/%s.json' % (cfg['dataFolder'], cfg['genesisFileGeneratorNode'])
print('Create genesis file %s' % final_genesis_file)
with open(final_genesis_file, 'w') as outfile:
json.dump(genesis_result, outfile, indent=2)
def get_perspeers_string(node, nodes_conns):
str_peers = ''
if node in nodes_conns.keys():
for peer in nodes_conns[node]:
str_peers = '%s%s,' % (str_peers, peer)
return str_peers[:-1]
else:
return str_peers
def create_toml_files(nodes_conns, cfg):
for k in cfg['nodes'].keys():
# Config file
toml_config = toml.load('%s/from_sidechain/toml_config/config.toml' % cfg['dataFolder'])
toml_config['moniker'] = k
toml_config['p2p']['external_address'] = '%s:%i' % (cfg['nodes'][k][2], cfg['tendermint']['peerPort'])
toml_config['p2p']['persistent_peers'] = get_perspeers_string(k, nodes_conns)
file_path = '%s/to_sidechain/toml_config/%s.toml' % (cfg['dataFolder'], k)
with open(file_path, 'w') as f:
toml.dump(toml_config, f)
# App file
toml_app = toml.load('%s/from_sidechain/toml_app/app.toml' % cfg['dataFolder'])
toml_app['api']['enable'] = True
toml_app['api']['address'] = 'tcp://localhost:%i' % cfg['tendermint']['apiPort']
file_path = '%s/to_sidechain/toml_app/%s.toml' % (cfg['dataFolder'], k)
with open(file_path, 'w') as f:
toml.dump(toml_app, f)
def get_nodes_ids_from_file(cfg):
f = open(os.path.expanduser('%s/from_sidechain/nodes_ids.txt' % cfg['dataFolder']), 'r')
ids = dict()
for id_line in f:
(node, data) = id_line.split('=')
ids[node] = data[:-1]
return ids
def create_nodes_file(cfg):
of = '/tmp/nodes.txt'
fw = open(of, 'w')
for node in cfg['nodes'].keys():
fw.write('%s%s\n' % (cfg['nodes'][node][2], node))
fw.close()
print('File %s created' % of)
def add_meter(node, cfg, app_cli):
host = cfg['nodes'][node][2]
user = cfg['nodes'][node][3]
remote_goroot = cfg['nodes'][node][4]
account = cfg['nodes'][node][5]
real_cmd = '%s/bin/%s keys show %s' % (remote_goroot, app_cli, get_real_account(host, user, account))
key_str = exec_real_cmd(host, user, real_cmd, False)
# Build the dataset
res = ''
for elem in key_str:
res = '%s%s' % (res, elem)
data_key = json.loads(res)
# Do the transaction
transaction_params = {
"meter": data_key["name"],
"account": data_key["address"]
}
return transaction_params
def get_tokens_amount(ci, cfg, app_cli, logger):
# pscli query account $(pscli keys show $NEW -a) | jq ".value.coins[0]"
data = ci.get_account_info()
remote_goroot = cfg['cosmos']['goPath']
cmd = '%s/bin/%s query account %s' % (remote_goroot, app_cli, data['address'])
# real_cmd = '%s/bin/%s keys show %s' % (remote_goroot, app_cli, u.get_real_account(host, user, account))
raw_data = subprocess.check_output(cmd, shell=True)
data = json.loads(raw_data.decode('utf-8'))
logger.info('Tokens balance for of %s' % data['value']['address'])
for token in data['value']['coins']:
logger.info('BALANCE[%s] = %s' % (token['denom'], token['amount']))
return data
# Main
if __name__ == "__main__":
# Input args
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('-c', help='comand')
args = arg_parser.parse_args()