electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 7a5016ec4285eeea063db3afbc48b610c75894fb
parent f93bc5951cc6bd30ca3cfdbdf32ef848a14a3ff8
Author: ThomasV <thomasv@gitorious>
Date:   Thu,  3 Oct 2013 10:05:01 +0200

move synchronous_get to network.py, fix get_balance script

Diffstat:
Mlib/commands.py | 2+-
Mlib/interface.py | 17-----------------
Mlib/network.py | 19++++++++++++++++++-
Mlib/wallet.py | 2+-
Mscripts/get_balance | 21+++++++++++----------
5 files changed, 31 insertions(+), 30 deletions(-)

diff --git a/lib/commands.py b/lib/commands.py @@ -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): diff --git a/lib/interface.py b/lib/interface.py @@ -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 diff --git a/lib/network.py b/lib/network.py @@ -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) diff --git a/lib/wallet.py b/lib/wallet.py @@ -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: diff --git a/scripts/get_balance b/scripts/get_balance @@ -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.))