move synchronous_get to network.py, fix get_balance script
authorThomasV <thomasv@gitorious>
Thu, 3 Oct 2013 08:05:01 +0000 (10:05 +0200)
committerThomasV <thomasv@gitorious>
Thu, 3 Oct 2013 08:05:01 +0000 (10:05 +0200)
lib/commands.py
lib/interface.py
lib/network.py
lib/wallet.py
scripts/get_balance

index 9caac36..c08b590 100644 (file)
@@ -101,7 +101,7 @@ class Commands:
 
     def getaddresshistory(self, addr):
         h = self.wallet.get_history(addr)
-        if h is None: h = self.wallet.interface.synchronous_get([ ('blockchain.address.get_history',[addr]) ])[0]
+        if h is None: h = self.network.synchronous_get([ ('blockchain.address.get_history',[addr]) ])[0]
         return h
 
     def listunspent(self):
index e0ec526..272e129 100644 (file)
@@ -532,23 +532,6 @@ class Interface(threading.Thread):
         return self.unanswered_requests == {}
 
 
-    def synchronous_get(self, requests, timeout=100000000):
-        # todo: use generators, unanswered_requests should be a list of arrays...
-        queue = Queue.Queue()
-        ids = self.send(requests, lambda i,r: queue.put(r))
-        id2 = ids[:]
-        res = {}
-        while ids:
-            r = queue.get(True, timeout)
-            _id = r.get('id')
-            if _id in ids:
-                ids.remove(_id)
-                res[_id] = r.get('result')
-        out = []
-        for _id in id2:
-            out.append(res[_id])
-        return out
-
 
     def start(self, queue):
         self.queue = queue
index e01e2db..02cf727 100644 (file)
@@ -226,9 +226,26 @@ class Network(threading.Thread):
         with self.lock: return self.running
 
     
+    def synchronous_get(self, requests, timeout=100000000):
+        queue = Queue.Queue()
+        ids = self.interface.send(requests, lambda i,r: queue.put(r))
+        id2 = ids[:]
+        res = {}
+        while ids:
+            r = queue.get(True, timeout)
+            _id = r.get('id')
+            if _id in ids:
+                ids.remove(_id)
+                res[_id] = r.get('result')
+        out = []
+        for _id in id2:
+            out.append(res[_id])
+        return out
+
+
     def retrieve_transaction(self, tx_hash, tx_height=0):
         import transaction
-        r = self.interface.synchronous_get([ ('blockchain.transaction.get',[tx_hash, tx_height]) ])[0]
+        r = self.synchronous_get([ ('blockchain.transaction.get',[tx_hash, tx_height]) ])[0]
         if r:
             return transaction.Transaction(r)
 
index e95e257..7f45a67 100644 (file)
@@ -1374,7 +1374,7 @@ class Wallet:
                     # assert not self.is_mine(_addr)
                     ext_requests.append( ('blockchain.address.get_history', [_addr]) )
 
-                ext_h = self.network.interface.synchronous_get(ext_requests)
+                ext_h = self.network.synchronous_get(ext_requests)
                 print_error("sync:", ext_requests, ext_h)
                 height = None
                 for h in ext_h:
index 496931b..489228f 100755 (executable)
@@ -3,25 +3,26 @@
 import sys
 from electrum import Interface
 from electrum import bitcoin, Transaction
+from electrum import Network, SimpleConfig
 
 
-def get_transaction(interface, tx_hash, tx_height):
-    raw_tx = interface.synchronous_get([(
+def get_transaction(network, tx_hash, tx_height):
+    raw_tx = network.synchronous_get([(
         'blockchain.transaction.get', [tx_hash, tx_height])])[0]
     tx = Transaction(raw_tx)
     return tx
 
 
-def get_history(interface, addr):
-    transactions = interface.synchronous_get([(
+def get_history(network, addr):
+    transactions = network.synchronous_get([(
         'blockchain.address.get_history', [addr])])[0]
     transactions.sort(key=lambda x: x["height"])
     return [(i["tx_hash"], i["height"]) for i in transactions]
 
 
-def get_addr_balance(interface, address):
+def get_addr_balance(network, address):
     prevout_values = {}
-    h = get_history(interface, address)
+    h = get_history(network, address)
     if h == ['*']:
         return 0, 0
     c = u = 0
@@ -31,7 +32,7 @@ def get_addr_balance(interface, address):
     # fetch transactions
     for tx_hash, tx_height in h:
         transactions[(tx_hash, tx_height)] = get_transaction(
-            interface, tx_hash, tx_height)
+            network, tx_hash, tx_height)
 
     for tx_hash, tx_height in h:
         tx = transactions[(tx_hash, tx_height)]
@@ -74,9 +75,9 @@ def update_tx_outputs(tx, prevout_values):
 
 
 def main(address):
-    interface = Interface()
-    interface.start()
-    c, u = get_addr_balance(interface, address)
+    network = Network(SimpleConfig({'server':'btc.it-zone.org:110:s','verbose':False}))
+    network.start(wait=True)
+    c, u = get_addr_balance(network, address)
 
     print("Balance - confirmed: %d (%.8f BTC), unconfirmed: %d (%.8f BTC)" %
           (c, c / 100000000., u, u / 100000000.))