ThomasV
11 years ago
2 changed files with 74 additions and 12 deletions
@ -1,17 +1,79 @@ |
|||
#!/usr/bin/env python |
|||
|
|||
import time, electrum |
|||
import time, electrum, Queue |
|||
from electrum import Interface, SimpleConfig |
|||
from electrum.network import filter_protocol, parse_servers |
|||
from collections import defaultdict |
|||
|
|||
electrum.set_verbosity(False) # default is True |
|||
network = electrum.Network({'auto_cycle':True}) |
|||
network.register_callback('peers',lambda: electrum.print_json(network.irc_servers.keys())) |
|||
# 1. start interface and wait for connection |
|||
interface = electrum.Interface('ecdsa.net:50002:s') |
|||
interface.start(wait = True) |
|||
if not interface.is_connected: |
|||
print "not connected" |
|||
exit() |
|||
|
|||
if not network.start(wait=True): |
|||
print "Not connected" |
|||
exit(1) |
|||
# 2. get list of peers |
|||
q = Queue.Queue() |
|||
interface.send([('server.peers.subscribe',[])], lambda i,x: q.put(x)) |
|||
r = q.get(timeout=10000) |
|||
peers = parse_servers(r.get('result')) |
|||
peers = filter_protocol(peers,'s') |
|||
|
|||
print "Connected to", network.interface.server |
|||
while not network.irc_servers: |
|||
time.sleep(1) |
|||
# start interfaces |
|||
config = SimpleConfig() |
|||
interfaces = map ( lambda server: Interface(server, config), peers ) |
|||
results_queue = Queue.Queue() |
|||
reached_servers = [] |
|||
for i in interfaces: i.start(q) |
|||
|
|||
while peers: |
|||
i = q.get(timeout=1) |
|||
peers.remove(i.server) |
|||
if i.is_connected: |
|||
i.send([('blockchain.headers.subscribe',[])], lambda i,x: results_queue.put((i,x))) |
|||
reached_servers.append(i.server) |
|||
|
|||
def analyze(results): |
|||
out = {} |
|||
dd = {} |
|||
for k, v in results.items(): |
|||
height = v.get('block_height') |
|||
merkle = v.get('merkle_root') |
|||
utxo = v.get('utxo_root') |
|||
d = dd.get(merkle, defaultdict(int)) |
|||
d[utxo] += 1 |
|||
dd[merkle] = d |
|||
|
|||
refs = {} |
|||
for merkle, d in dd.items(): |
|||
v = d.values() |
|||
m = max(v) |
|||
ref = d.keys()[v.index(m)] |
|||
refs[merkle] = ref, m |
|||
|
|||
for k, v in results.items(): |
|||
height = v.get('block_height') |
|||
merkle = v.get('merkle_root') |
|||
utxo = v.get('utxo_root') |
|||
ref_utxo, num = refs.get(merkle) |
|||
|
|||
if ref_utxo != utxo and num > 1: |
|||
out[k] = height, merkle, utxo |
|||
|
|||
return out |
|||
|
|||
|
|||
results = {} |
|||
while reached_servers: |
|||
i, r = results_queue.get(timeout=10000) |
|||
results[i.server] = r.get('result') |
|||
reached_servers.remove(i.server) |
|||
|
|||
electrum.print_json(results) |
|||
out = analyze(results) |
|||
if out: |
|||
print "faulty servers:" |
|||
electrum.print_json(out) |
|||
else: |
|||
print "ok" |
|||
|
|||
|
Loading…
Reference in new issue