commit 43c8fd43c4f7c5c2797126fddfc05dcafd6c68e0
parent b782a1bb3fa06e14637b22a8d3deefb2a2bac9b3
Author: ThomasV <thomasv@gitorious>
Date: Mon, 15 Sep 2014 14:52:28 +0200
update peers and txradar scripts
Diffstat:
M | scripts/peers | | | 77 | +++-------------------------------------------------------------------------- |
M | scripts/txradar | | | 69 | +++++++-------------------------------------------------------------- |
A | scripts/util.py | | | 71 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 81 insertions(+), 136 deletions(-)
diff --git a/scripts/peers b/scripts/peers
@@ -1,56 +1,8 @@
#!/usr/bin/env python
-import time, electrum, Queue
-from electrum import Interface, SimpleConfig
-from electrum.network import filter_protocol, parse_servers
+import util, json
from collections import defaultdict
-# electrum.util.set_verbosity(1)
-
-# 1. start interface and wait for connection
-interface = electrum.Interface('ecdsa.net:110:s')
-q = Queue.Queue()
-interface.start(q)
-
-i, r = q.get()
-if not interface.is_connected:
- print "not connected"
- exit(1)
-
-# 2. get list of peers
-interface.send_request({'id':0, 'method':'server.peers.subscribe','params':[]})
-i, r = q.get(timeout=10000)
-peers = parse_servers(r.get('result'))
-peers = filter_protocol(peers,'s')
-i.stop()
-
-print "Contacting %d servers"%len(peers)
-# start interfaces
-q2 = Queue.Queue()
-config = SimpleConfig()
-interfaces = map ( lambda server: Interface(server, config), peers )
-reached_servers = []
-for i in interfaces:
- i.start(q2)
-
-t0 = time.time()
-while peers:
- try:
- i, r = q2.get(timeout=1)
- except:
- if time.time() - t0 > 10:
- print "timeout"
- break
- else:
- continue
- if i.server in peers:
- peers.remove(i.server)
- if i.is_connected:
- reached_servers.append(i)
- else:
- print "Connection failed:", i.server
-
-print "%d servers could be reached"%len(reached_servers)
def analyze(results):
out = {}
@@ -62,47 +14,24 @@ def analyze(results):
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_queue = Queue.Queue()
-for i in reached_servers:
- i.send_request({'method':'blockchain.headers.subscribe','params':[]}, results_queue)
-
-results = {}
-t0 = time.time()
-while reached_servers:
- try:
- i, r = results_queue.get(timeout=1)
- except:
- if time.time() - t0 > 10:
- break
- else:
- continue
- results[i.server] = r.get('result')
- reached_servers.remove(i)
- i.stop()
-
-for i in reached_servers:
- print i.server, "did not answer"
-print "%d answers"%len(results)
+peers = util.get_peers()
+results = util.send_request(peers, {'method':'blockchain.headers.subscribe','params':[]})
errors = analyze(results).keys()
diff --git a/scripts/txradar b/scripts/txradar
@@ -1,75 +1,20 @@
#!/usr/bin/env python
-
-import time, sys, electrum, Queue
-from electrum import Interface, SimpleConfig
-from electrum.network import filter_protocol, parse_servers
-from collections import defaultdict
-
+import util, sys
try:
tx = sys.argv[1]
except:
print "usage: txradar txid"
sys.exit(1)
-# 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()
-
-# 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 "Contacting %d servers"%len(peers)
-# 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)
-
-t0 = time.time()
-while peers:
- try:
- i = q.get(timeout=1)
- except:
- if time.time() - t0 > 10:
- print "timeout"
- break
- else:
- continue
- peers.remove(i.server)
- if i.is_connected:
- i.send([('blockchain.transaction.get',[tx])], lambda i,x: results_queue.put((i,x)))
- reached_servers.append(i.server)
- else:
- print "Connection failed:", i.server
-
-print "%d servers could be reached"%len(reached_servers)
-
+peers = util.get_peers()
+results = util.send_request(peers, {'method':'blockchain.transaction.get','params':[tx]})
r1 = []
r2 = []
-while reached_servers:
- i, r = results_queue.get(timeout=10000)
- reached_servers.remove(i.server)
-
- if r.get('result'):
- r1.append(i.server)
- else:
- r2.append(i.server)
+for k, v in results.items():
+ (r1 if v else r2).append(k)
+print "Received %d answers"%len(results)
+print "Propagation rate: %.1f percent" % (len(r1) *100./(len(r1)+ len(r2)))
-if r2:
- print "----------------"
- print "servers that have not seen this transaction:\n", '\n'.join(r2)
- print "----------------"
- print "propagation rate: %.1f percent" % (len(r1) *100./(len(r1)+ len(r2)))
-else:
- print "transaction propagated everywhere"
diff --git a/scripts/util.py b/scripts/util.py
@@ -0,0 +1,71 @@
+import time, electrum, Queue
+from electrum import Interface, SimpleConfig
+from electrum.network import filter_protocol, parse_servers
+
+# electrum.util.set_verbosity(1)
+
+def get_peers():
+ # 1. start interface and wait for connection
+ interface = electrum.Interface('ecdsa.net:110:s')
+ q = Queue.Queue()
+ interface.start(q)
+ i, r = q.get()
+ if not interface.is_connected:
+ raise BaseException("not connected")
+ # 2. get list of peers
+ interface.send_request({'id':0, 'method':'server.peers.subscribe','params':[]})
+ i, r = q.get(timeout=10000)
+ peers = parse_servers(r.get('result'))
+ peers = filter_protocol(peers,'s')
+ i.stop()
+ return peers
+
+def send_request(peers, request):
+ print "Contacting %d servers"%len(peers)
+ # start interfaces
+ q2 = Queue.Queue()
+ config = SimpleConfig()
+ interfaces = map ( lambda server: Interface(server, config), peers )
+ reached_servers = []
+ for i in interfaces:
+ i.start(q2)
+ t0 = time.time()
+ while peers:
+ try:
+ i, r = q2.get(timeout=1)
+ except:
+ if time.time() - t0 > 10:
+ print "timeout"
+ break
+ else:
+ continue
+ if i.server in peers:
+ peers.remove(i.server)
+ if i.is_connected:
+ reached_servers.append(i)
+ else:
+ print "Connection failed:", i.server
+
+ print "%d servers could be reached"%len(reached_servers)
+
+ results_queue = Queue.Queue()
+ for i in reached_servers:
+ i.send_request(request, results_queue)
+ results = {}
+ t0 = time.time()
+ while reached_servers:
+ try:
+ i, r = results_queue.get(timeout=1)
+ except:
+ if time.time() - t0 > 10:
+ break
+ else:
+ continue
+ results[i.server] = r.get('result')
+ reached_servers.remove(i)
+ i.stop()
+
+ for i in reached_servers:
+ print i.server, "did not answer"
+ print "%d answers"%len(results)
+ return results