-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
nodelist.py
383 lines (369 loc) · 12.8 KB
/
nodelist.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
# This Python file uses the following encoding: utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from builtins import next
import re
import time
import math
import json
from beem.instance import shared_steem_instance
from beem.account import Account
import logging
log = logging.getLogger(__name__)
class NodeList(list):
""" Returns a node list
.. code-block:: python
from beem.nodelist import NodeList
n = NodeList()
nodes_urls = n.get_nodes()
"""
def __init__(self):
nodes = [
{
"url": "https://api.steemit.com",
"version": "0.20.2",
"type": "appbase",
"owner": "steemit",
"score": 100
},
{
"url": "https://steemd-appbase.steemit.com",
"version": "0.20.2",
"type": "appbase",
"owner": "steemit",
"score": 100
},
{
"url": "wss://steemd-appbase.steemit.com",
"version": "0.20.2",
"type": "appbase",
"owner": "steemit",
"score": 100
},
{
"url": "wss://appbasetest.timcliff.com",
"version": "0.19.11",
"type": "appbase",
"owner": "timcliff",
"score": -10
},
{
"url": "https://appbasetest.timcliff.com",
"version": "0.20.2",
"type": "appbase",
"owner": "timcliff",
"score": 10
},
{
"url": "https://api.steem.house",
"version": "0.20.2",
"type": "appbase",
"owner": "gtg",
"score": 90
},
{
"url": "https://api.steemitdev.com",
"version": "0.19.11",
"type": "appbase-dev",
"owner": "steemit",
"score": 100
},
{
"url": "https://api.steemitstage.com",
"version": "0.19.11",
"type": "appbase-dev",
"owner": "steemit",
"score": 110
},
{
"url": "wss://rpc.steemviz.com",
"version": "0.19.12",
"type": "appbase",
"owner": "ausbitbank",
"score": 10
},
{
"url": "https://rpc.steemviz.com",
"version": "0.19.12",
"type": "appbase",
"owner": "ausbitbank",
"score": 100
},
{
"url": "wss://steemd.privex.io",
"version": "0.20.2",
"type": "appbase",
"owner": "privex",
"score": 50
},
{
"url": "https://steemd.privex.io",
"version": "0.20.2",
"type": "appbase",
"owner": "privex",
"score": 50
},
{
"url": "wss://rpc.buildteam.io",
"version": "0.20.2",
"type": "appbase",
"owner": "themarkymark",
"score": -10
},
{
"url": "https://rpc.buildteam.io",
"version": "0.20.2",
"type": "appbase",
"owner": "themarkymark",
"score": 20
},
{
"url": "wss://gtg.steem.house:8090",
"version": "0.19.12",
"type": "appbase",
"owner": "gtg",
"score": 75
},
{
"url": "https://gtg.steem.house:8090",
"version": "0.19.12",
"type": "appbase",
"owner": "gtg",
"score": 80
},
{
"url": "wss://steemd.pevo.science",
"version": "0.19.2",
"type": "normal",
"owner": "pharesim",
"score": -10
},
{
"url": "https://steemd.pevo.science",
"version": "0.19.6",
"type": "normal",
"owner": "pharesim",
"score": -10
},
{
"url": "wss://rpc.steemliberator.com",
"version": "0.19.12",
"type": "appbase",
"owner": "netuoso",
"score": -10
},
{
"url": "https://rpc.steemliberator.com",
"version": "0.19.12",
"type": "appbase",
"owner": "netuoso",
"score": 20
},
{
"url": "wss://seed.bitcoiner.me",
"version": "0.19.6",
"type": "normal",
"owner": "bitcoiner",
"score": -10
},
{
"url": "https://seed.bitcoiner.me",
"version": "0.19.6",
"type": "normal",
"owner": "bitcoiner",
"score": -10
},
{
"url": "wss://steemd.steemgigs.org",
"version": "0.19.6",
"type": "normal",
"owner": "steemgigs",
"score": -10
},
{
"url": "https://steemd.steemgigs.org",
"version": "0.19.6",
"type": "normal",
"owner": "steemgigs",
"score": -10
},
{
"url": "wss://steemd.minnowsupportproject.org",
"version": "0.19.11",
"type": "appbase",
"owner": "followbtcnews",
"score": -10
},
{
"url": "https://steemd.minnowsupportproject.org",
"version": "0.19.12",
"type": "appbase",
"owner": "followbtcnews",
"score": 10
},
{
"url": "https://rpc.curiesteem.com",
"version": "0.20.2",
"type": "appbase",
"owner": "curie",
"score": -10
},
{
"url": "wss://rpc.curiesteem.com",
"version": "0.20.2",
"type": "appbase",
"owner": "curie",
"score": 50
},
{
"url": "wss://testnet.steem.vc",
"version": "0.19.2",
"type": "testnet",
"owner": "almost-digital",
"score": 20
},
{
"url": "ws://testnet.steem.vc",
"version": "0.19.2",
"type": "testnet",
"owner": "almost-digital",
"score": 5
},
{
"url": "https://testnet.steem.vc",
"version": "0.19.2",
"type": "testnet",
"owner": "almost-digital",
"score": 10
},
{
"url": "http://testnet.steem.vc",
"version": "0.19.2",
"type": "testnet",
"owner": "almost-digital",
"score": 5
},
{
"url": "https://testnet.steemitdev.com",
"version": "0.21.0",
"type": "testnet-dev",
"owner": "steemit",
"score": 5
}]
super(NodeList, self).__init__(nodes)
def update_nodes(self, weights=None, steem_instance=None):
""" Reads metadata from fullnodeupdate and recalculates the nodes score
:params list/dict weight: can be used to weight the different benchmarks
.. code-block:: python
from beem.nodelist import NodeList
nl = NodeList()
weights = [0, 0.1, 0.2, 1]
nl.update_nodes(weights)
weights = {'block': 0.1, 'history': 0.1, 'apicall': 1, 'config': 1}
nl.update_nodes(weights)
"""
steem = steem_instance or shared_steem_instance()
metadata = None
account = None
cnt = 0
while metadata is None and cnt < 5:
cnt += 1
try:
account = Account("fullnodeupdate", steem_instance=steem)
metadata = json.loads(account["json_metadata"])
except:
steem.rpc.next()
account = None
metadata = None
if metadata is None:
return
report = metadata["report"]
failing_nodes = metadata["failing_nodes"]
parameter = metadata["parameter"]
benchmarks = parameter["benchmarks"]
if weights is None:
weights_dict = {}
for benchmark in benchmarks:
weights_dict[benchmark] = (1. / len(benchmarks))
elif isinstance(weights, list):
weights_dict = {}
i = 0
weight_sum = 0
for w in weights:
weight_sum += w
for benchmark in benchmarks:
if i < len(weights):
weights_dict[benchmark] = weights[i] / weight_sum
else:
weights_dict[benchmark] = 0.
i += 1
elif isinstance(weights, dict):
weights_dict = {}
i = 0
weight_sum = 0
for w in weights:
weight_sum += weights[w]
for benchmark in benchmarks:
if benchmark in weights:
weights_dict[benchmark] = weights[benchmark] / weight_sum
else:
weights_dict[benchmark] = 0.
max_score = len(report) + 1
new_nodes = []
for node in self:
new_node = node.copy()
for report_node in report:
if node["url"] == report_node["node"]:
new_node["version"] = report_node["version"]
scores = []
for benchmark in benchmarks:
result = report_node[benchmark]
rank = result["rank"]
if not result["ok"]:
rank = max_score + 1
score = (max_score - rank) / (max_score - 1) * 100
weighted_score = score * weights_dict[benchmark]
scores.append(weighted_score)
sum_score = 0
for score in scores:
sum_score += score
new_node["score"] = sum_score
for node_failing in failing_nodes:
if node["url"] == node_failing:
new_node["score"] = -1
new_nodes.append(new_node)
super(NodeList, self).__init__(new_nodes)
def get_nodes(self, normal=True, appbase=True, dev=False, testnet=False, testnetdev=False, wss=True, https=True, not_working=False):
""" Returns nodes as list
:param bool normal: when True, nodes with version 0.19.5 are included
:param bool appbase: when True, nodes with version 0.19.11 are included
:param bool dev: when True, dev nodes with version 0.19.11 are included
:param bool testnet: when True, testnet nodes are included
:param bool testnetdev: When True, testnet-dev nodes are included
:param bool not_working: When True, all nodes including not working ones will be returned
"""
node_list = []
node_type_list = []
if normal:
node_type_list.append("normal")
if appbase:
node_type_list.append("appbase")
if dev:
node_type_list.append("appbase-dev")
if testnet:
node_type_list.append("testnet")
if testnetdev:
node_type_list.append("testnet-dev")
for node in self:
if node["type"] in node_type_list and (node["score"] >= 0 or not_working):
if not https and node["url"][:5] == 'https':
continue
if not wss and node["url"][:3] == 'wss':
continue
node_list.append(node)
return [node["url"] for node in sorted(node_list, key=lambda self: self['score'], reverse=True)]
def get_testnet(self, testnet=True, testnetdev=False):
"""Returns testnet nodes"""
return self.get_nodes(normal=False, appbase=False, testnet=testnet, testnetdev=testnetdev)