tnew interface - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 9e316b7d2ba72a89e8bf4253aae02cb3ac4a77f7 DIR parent 3e49cfdbd47ea06c9bc8a8ba35444668378bb5f7 HTML Author: ThomasV <thomasv@gitorious> Date: Tue, 13 Mar 2012 16:43:56 +0100 new interface Diffstat: M client/gui_qt.py | 2 +- M client/interface.py | 75 +++++++++++++++++++++++--------- M client/wallet.py | 7 +++++-- 3 files changed, 61 insertions(+), 23 deletions(-) --- DIR diff --git a/client/gui_qt.py b/client/gui_qt.py t@@ -190,7 +190,7 @@ class ElectrumWindow(QMainWindow): if self.wallet.interface.blocks == 0: text = "Server not ready" icon = QIcon(":icons/status_disconnected.png") - elif not self.wallet.interface.was_polled: + elif not self.wallet.interface.is_up_to_date: text = "Synchronizing..." icon = QIcon(":icons/status_waiting.svg") else: DIR diff --git a/client/interface.py b/client/interface.py t@@ -34,7 +34,7 @@ class Interface: self.message = '' self.is_connected = False self.was_updated = True # fixme: use a semaphore - self.was_polled = False # True after the first poll + self.is_up_to_date = False # True after the first poll def send_tx(self, data): out = self.handler('blockchain.transaction.broadcast', data ) t@@ -66,7 +66,7 @@ class NativeInterface(Interface): self.port = port def new_session(self, addresses, version): - self.was_polled = False + self.is_up_to_date = False out = self.handler('session.new', [ version, addresses ] ) self.session_id, self.message = ast.literal_eval( out ) t@@ -130,7 +130,7 @@ class NativeInterface(Interface): if blocks == -1: raise BaseException("session not found") self.blocks = int(blocks) if changed_addr: self.was_updated = True - self.was_polled = True + self.is_up_to_date = True return changed_addr def update_wallet_thread(self, wallet): t@@ -197,7 +197,7 @@ class NativeInterface(Interface): class TCPInterface(Interface): - """json-rpc over TCP""" + """json-rpc over persistent TCP connection""" def __init__(self, host=None, port=50001): Interface.__init__(self) t@@ -210,29 +210,64 @@ class TCPInterface(Interface): request = json.dumps( { 'method':cmd, 'params':params } ) self.s.send( request + '\n' ) + def send_tx(self, data): + out = self.send('transaction.broadcast', data ) + return out + def listen_thread(self, wallet): - out = '' - while True: - msg = self.s.recv(1024) - out += msg - s = out.find('\n') - if s!=-1: - c = out[0:s] - out = out[s+1:] - c = json.loads(c) - cmd = c.get('method') - if cmd == 'server.banner': - self.message = c.get('result') + try: + self.is_connected = True + out = '' + while True: + msg = self.s.recv(1024) + out += msg + while True: + s = out.find('\n') + if s==-1: break + c = out[0:s] + out = out[s+1:] + c = json.loads(c) + cmd = c.get('method') + if cmd == 'server.banner': + self.message = c.get('result') + if cmd == 'numblocks.subscribe': + self.blocks = c.get('result') + print "received numblocks",self.blocks + elif cmd =='address.subscribe': + addr = c.get('address') + status = c.get('status') + if wallet.status.get(addr) != status: + self.send('address.get_history', addr) + wallet.status[addr] = status + self.is_up_to_date = False + else: + self.is_up_to_date = True + elif cmd == 'address.get_history': + addr = c.get('address') + print "updating history for", addr + wallet.history[addr] = c.get('result') + wallet.synchronize() + wallet.update_tx_history() + wallet.save() + self.was_updated = True else: print "received message:", c - + except: + traceback.print_exc(file=sys.stdout) + self.is_connected = False + + + def subscribe(self,address): + self.send('address.subscribe', address) + + def start(self, wallet): thread.start_new_thread(self.listen_thread, (wallet,)) self.send('client.version', wallet.electrum_version) - self.send('server.banner', None) + self.send('server.banner') + self.send('numblocks.subscribe') for address in wallet.all_addresses(): - self.send('blockchain.address.subscribe', address) - + self.subscribe(address) class HttpInterface(Interface): DIR diff --git a/client/wallet.py b/client/wallet.py t@@ -449,8 +449,11 @@ class Wallet: def create_new_address(self, bool): address = self.create_new_address_without_history(bool) - self.history[address] = h = self.interface.retrieve_history(address) - self.status[address] = h[-1]['blk_hash'] if h else None + if self.interface.port == 50001: + self.interface.subscribe(address) + else: + self.history[address] = h = self.interface.retrieve_history(address) + self.status[address] = h[-1]['blk_hash'] if h else None return address