From 5af496ceb55fb5ba8403cc320064e415dd6d8039 Mon Sep 17 00:00:00 2001 From: thomasv Date: Fri, 16 Dec 2011 12:22:04 +0100 Subject: [PATCH] self-synchronizing wallet --- client/electrum.py | 55 +++++++++++++++++++++++----------------------- client/gui.py | 15 ++++++++----- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/client/electrum.py b/client/electrum.py index 788bc3609..252177d9a 100755 --- a/client/electrum.py +++ b/client/electrum.py @@ -341,41 +341,40 @@ class Wallet: self.change_addresses.append(address) else: self.addresses.append(address) + + # updates + print address + self.history[address] = h = self.retrieve_history(address) + self.status[address] = h[-1]['blk_hash'] if h else None self.save() return address - - def recover(self): - # todo: recover receiving addresses from tx - is_found = False + + + def synchronize(self): + while True: - addr = self.create_new_address2(True) - self.history[addr] = h = self.retrieve_history(addr) - self.status[addr] = h[-1]['blk_hash'] if h else None - print "recovering", addr - if self.status[addr] is not None: - is_found = True + if self.change_addresses == []: + self.create_new_address2(True) + continue + a = self.change_addresses[-1] + if self.history.get(a): + self.create_new_address2(True) else: break - num_gap = 0 + n = self.gap_limit while True: - addr = self.create_new_address2(False) - self.history[addr] = h = self.retrieve_history(addr) - self.status[addr] = h[-1]['blk_hash'] if h else None - print "recovering", addr - if self.status[addr] is None: - num_gap += 1 - if num_gap == self.gap_limit: break + if len(self.addresses) < n: + self.create_new_address2(False) + continue + if map( lambda a: self.history.get(a), self.addresses[-n:] ) == n*[[]]: + break else: - is_found = True - num_gap = 0 + self.create_new_address2(False) + is_found = (len(self.change_addresses) > 1 ) or ( len(self.addresses) > self.gap_limit ) if not is_found: return False - # remove limit-1 addresses. - n = self.gap_limit - self.addresses = self.addresses[:-n] - # history and addressbook self.update_tx_history() for tx in self.tx_history.values(): @@ -526,7 +525,7 @@ See the release notes for more information.""",1) def get_servers(self): self.servers = map( lambda x:x[1], ast.literal_eval( self.request( repr ( ('peers', '' )))) ) - + def update(self): blocks, changed_addresses = self.poll() if blocks == -1: raise BaseException("session not found") @@ -536,8 +535,10 @@ See the release notes for more information.""",1) print "updating history for", addr self.history[addr] = self.retrieve_history(addr) self.status[addr] = blk_hash - self.update_tx_history() + if changed_addresses: + self.synchronize() + self.save() return True else: return False @@ -755,7 +756,7 @@ if __name__ == '__main__': gap = raw_input("gap limit (default 5):") if gap: wallet.gap_limit = int(gap) print "recovering wallet..." - r = wallet.recover() + r = wallet.synchronize() if r: print "recovery successful" wallet.save() diff --git a/client/gui.py b/client/gui.py index 0baf737f4..dd011712e 100644 --- a/client/gui.py +++ b/client/gui.py @@ -514,8 +514,10 @@ class BitcoinGUI: self.period = 15 if self.wallet.use_http() else 5 u = self.wallet.update() if u: - self.wallet.save() gobject.idle_add( self.update_history_tab ) + gobject.idle_add( self.update_receiving_tab ) + # addressbook too... + time.sleep(self.period) except BaseException: print "starting new session" @@ -823,10 +825,11 @@ class BitcoinGUI: scroll.add(treeview) hbox = gtk.HBox() - button = gtk.Button("New address") - button.connect("clicked", self.newaddress_dialog, is_recv) - button.show() - hbox.pack_start(button,False) + if not is_recv: + button = gtk.Button("New address") + button.connect("clicked", self.newaddress_dialog, is_recv) + button.show() + hbox.pack_start(button,False) def showqrcode(w, treeview, liststore): path, col = treeview.get_cursor() @@ -922,7 +925,7 @@ class BitcoinGUI: for item in h: if not item['is_in'] : n=n+1 tx = "None" if n==0 else "%d"%n - self.recv_list.prepend((address, label, tx )) + self.recv_list.append((address, label, tx )) def update_sending_tab(self): # detect addresses that are not mine in history, add them here...