forked from jamescoxon/Nano_Election_Visualiser
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun.py
More file actions
241 lines (192 loc) · 8.37 KB
/
run.py
File metadata and controls
241 lines (192 loc) · 8.37 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
import requests
import time
import random
import json
import os
from flask import Flask
from flask import request, render_template, send_file
from flask import jsonify
from flask_socketio import SocketIO
from flask_apscheduler import APScheduler
import logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.DEBUG)
async_mode = None
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, async_mode=async_mode)
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()
url = os.environ.get('RPC_URL', 'http://localhost:7076')
update_time = int(os.environ.get('UPDATE_TIME', '5'))
conf_active = {'action' : 'confirmation_active'}
elections_list = []
hash_dict = {}
broadcast_data = []
peer_ip = {}
rep_conf = {}
rep_cemented = {}
# read file
with open('aliases.json', 'r') as myfile:
data=myfile.read()
# parse file
rep_json = json.loads(data)
rep_dict = {}
for rep in rep_json:
print(rep)
account = rep['account']
rep_dict[account] = rep['alias']
@scheduler.task('interval', id='do_job_1', seconds=update_time, misfire_grace_time=900)
def scheduled_task():
print('SCHEDULE: Start')
with scheduler.app.app_context():
confirmation_quorum_command = {'action' : 'confirmation_quorum', 'peer_details': 'true'}
x = requests.post(url, json = confirmation_quorum_command)
confirmation_quorum_result = x.json()
for peers in confirmation_quorum_result['peers']:
ip_parts = str(peers['ip']).split(']')
ip_address = ip_parts[0][1:]
# print('{} : {}'.format(ip_address, peers['account']))
peer_ip[ip_address] = peers['account']
x = requests.post(url, json = conf_active)
result = x.json()
confs = result['confirmations']
total_confs = int(result['unconfirmed'])
while len(elections_list) < 20:
random_conf = random.randrange(total_confs)
elections_list.append(confs[random_conf])
hash_dict[confs[random_conf]] = '0'
broadcast_data.clear()
for election in elections_list:
if election not in confs:
if election in hash_dict:
block_info = {'action' : 'block_info', 'json_block': 'true', 'hash': hash_dict[election] }
x = requests.post(url, json = block_info)
result = x.json()
print(result)
if 'confirmed' in result:
confirmed = result['confirmed']
else:
confirmed = 'false'
print(confirmed)
print()
if confirmed == 'false':
print('Dropping')
broadcast_data.append({ 'root' : election, 'hash' : hash_dict[election], 'tally' : '600000000' , 'rep_num' : '0' , 'status' : 'DROPPED'})
else:
print('Success')
broadcast_data.append({ 'root' : election, 'hash' : hash_dict[election], 'tally' : '600000000' , 'rep_num' : '0' , 'status' : 'SUCCESSFUL'})
print('Removing election')
elections_list.remove(election)
if election in hash_dict:
del hash_dict[election]
else:
conf_command = {'action' : 'confirmation_info', 'json_block': 'true', 'root': election , 'representatives': 'true' }
x = requests.post(url, json = conf_command)
result = x.json()
# print(result)
total_tally = result['total_tally']
last_winner = result['last_winner']
representatives = result['blocks'][last_winner]['representatives']
hash_dict[election] = last_winner
# print('Winner: {}, Tally: {}'.format(last_winner, total_tally))
# broadcast_data[last_winner] = total_tally
broadcast_data.append({ 'root' : election, 'hash' : last_winner, 'tally' : total_tally, 'rep_num' : len(representatives), 'status' : 'IN_PROGRESS'})
broadcast_json = json.dumps(broadcast_data)
# print(broadcast_json)
# socketio.emit('update',{'data' : 'test'} , broadcast=True)
print('SCHEDULE: Done')
socketio.emit('update', broadcast_json , broadcast=True)
@socketio.on('connect')
def test_connect():
print('connected')
socketio.emit('my response', {'data': 'Connected'})
@app.route('/')
def start_page():
return send_file('./templates/index.html')
@app.route('/conf/random')
def start():
x = requests.post(url, json = conf_active)
result = x.json()
confs = result['confirmations']
total_confs = int(result['unconfirmed'])
print(total_confs)
print('Last Conf: {}'.format(confs[-1]))
random_conf = random.randrange(total_confs)
return '<html><h1>Conf Monitor</h1><br>Here is a random election to monitor:<br><a href="https://nanostatus.live/conf/{}">{}</a></html>'.format(confs[random_conf], confs[random_conf])
@app.route('/conf/all')
def all_elections():
x = requests.post(url, json = conf_active)
result = x.json()
confs = result['confirmations']
total_confs = int(result['unconfirmed'])
all =[]
for root in confs:
conf_command = {'action' : 'confirmation_info', 'json_block': 'true', 'root': root ,'representatives': 'true'}
x = requests.post(url, json = conf_command)
result = x.json()
if 'total_tally' in result:
total_tally = result['total_tally']
last_winner = result['last_winner']
all.append('{} {}<br>'.format(last_winner, total_tally))
return str(all)
@app.route('/conf/random/json')
def start_json():
x = requests.post(url, json = conf_active)
result = x.json()
confs = result['confirmations']
total_confs = int(result['unconfirmed'])
print(total_confs)
print('Last Conf: {}'.format(confs[-1]))
random_conf = random.randrange(total_confs)
return jsonify(confs[random_conf])
@app.route('/conf/<string:root>')
def conf_info(root):
print('{} {}'.format(len(peer_ip), len(rep_conf)))
rep_command = {'action' : 'telemetry', 'raw': 'true'}
x = requests.post(url, json = rep_command)
rep_result = x.json()
for nodes in rep_result['metrics']:
try:
# if nodes['address'] in peer_ip:
# print('Found address')
# else:
# print('Not present')
node_address = peer_ip[nodes['address']]
node_version = nodes['major_version']
node_cemented_count = nodes['cemented_count']
rep_conf[node_address] = node_version
rep_cemented[node_address] = node_cemented_count
except:
# print('Failed: {}'.format(nodes))
pass
conf_command = {'action' : 'confirmation_info', 'json_block': 'true', 'root': root ,'representatives': 'true'}
x = requests.post(url, json = conf_command)
result = x.json()
if 'total_tally' in result:
total_tally = result['total_tally']
else:
return '<html><head><meta http-equiv="refresh" content="5"></head>Election completed or dropped</html>'
last_winner = result['last_winner']
representatives = result['blocks'][last_winner]['representatives']
representatives_list = '<table><tr><th>Rep</th><th>Weight</th><th>Name</th><th>Node Version</th><th>Cemented</th></tr>'
balance = result['blocks'][last_winner]['contents']['balance']
account = result['blocks'][last_winner]['contents']['account']
for reps in representatives:
try:
name = rep_dict[reps]
except:
name = 'None'
try:
node_version = rep_conf[reps]
except:
node_version = 'failed'
try:
node_cemented = rep_cemented[reps]
except:
node_cemented = 'failed'
representatives_list = '{}<tr><td>{}</td><td>{} Nano</td><td>{}</td><td>{}</td><td>{}</td></tr>'.format(representatives_list, reps, int(int(representatives[reps]) / 1000000000000000000000000000000), name, node_version, node_cemented)
return '<html><head><meta http-equiv="refresh" content="5"></head>Last Winner: {}<br> Total Tally: {}<br>Account: {}<br>Block Balance: {}<br><br>{}</table></html>'.format(last_winner, total_tally, account, balance, representatives_list)
if __name__ == '__main__':
socketio.run(app, host='0.0.0.0')