Browse Source

peer script

283
ThomasV 11 years ago
parent
commit
e355c290f8
  1. 82
      scripts/peers
  2. 4
      scripts/servers

82
scripts/peers

@ -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"

4
scripts/servers

@ -22,7 +22,7 @@ while servers:
i = q.get(timeout=1000)
servers.remove(i.server)
if i.is_connected:
i.send([('blockchain.numblocks.subscribe',[])], lambda i,x: results_queue.put((i,x)))
i.send([('blockchain.headers.subscribe',[])], lambda i,x: results_queue.put((i,x)))
reached_servers.append(i.server)
i.status = "ok"
else:
@ -32,7 +32,7 @@ d = defaultdict(int)
while reached_servers:
i, r = results_queue.get(timeout=1000)
i.blocks = r.get('result')
i.blocks = r.get('result').get('block_height')
d[i.blocks] += 1
reached_servers.remove(i.server)

Loading…
Cancel
Save