Browse Source

self-synchronizing wallet

283
thomasv 13 years ago
parent
commit
5af496ceb5
  1. 51
      client/electrum.py
  2. 7
      client/gui.py

51
client/electrum.py

@ -341,41 +341,40 @@ class Wallet:
self.change_addresses.append(address) self.change_addresses.append(address)
else: else:
self.addresses.append(address) 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() self.save()
return address return address
def recover(self):
# todo: recover receiving addresses from tx def synchronize(self):
is_found = False
while True: while True:
addr = self.create_new_address2(True) if self.change_addresses == []:
self.history[addr] = h = self.retrieve_history(addr) self.create_new_address2(True)
self.status[addr] = h[-1]['blk_hash'] if h else None continue
print "recovering", addr a = self.change_addresses[-1]
if self.status[addr] is not None: if self.history.get(a):
is_found = True self.create_new_address2(True)
else: else:
break break
num_gap = 0 n = self.gap_limit
while True: while True:
addr = self.create_new_address2(False) if len(self.addresses) < n:
self.history[addr] = h = self.retrieve_history(addr) self.create_new_address2(False)
self.status[addr] = h[-1]['blk_hash'] if h else None continue
print "recovering", addr if map( lambda a: self.history.get(a), self.addresses[-n:] ) == n*[[]]:
if self.status[addr] is None: break
num_gap += 1
if num_gap == self.gap_limit: break
else: else:
is_found = True self.create_new_address2(False)
num_gap = 0
is_found = (len(self.change_addresses) > 1 ) or ( len(self.addresses) > self.gap_limit )
if not is_found: return False if not is_found: return False
# remove limit-1 addresses.
n = self.gap_limit
self.addresses = self.addresses[:-n]
# history and addressbook # history and addressbook
self.update_tx_history() self.update_tx_history()
for tx in self.tx_history.values(): for tx in self.tx_history.values():
@ -536,8 +535,10 @@ See the release notes for more information.""",1)
print "updating history for", addr print "updating history for", addr
self.history[addr] = self.retrieve_history(addr) self.history[addr] = self.retrieve_history(addr)
self.status[addr] = blk_hash self.status[addr] = blk_hash
self.update_tx_history()
if changed_addresses: if changed_addresses:
self.synchronize()
self.save()
return True return True
else: else:
return False return False
@ -755,7 +756,7 @@ if __name__ == '__main__':
gap = raw_input("gap limit (default 5):") gap = raw_input("gap limit (default 5):")
if gap: wallet.gap_limit = int(gap) if gap: wallet.gap_limit = int(gap)
print "recovering wallet..." print "recovering wallet..."
r = wallet.recover() r = wallet.synchronize()
if r: if r:
print "recovery successful" print "recovery successful"
wallet.save() wallet.save()

7
client/gui.py

@ -514,8 +514,10 @@ class BitcoinGUI:
self.period = 15 if self.wallet.use_http() else 5 self.period = 15 if self.wallet.use_http() else 5
u = self.wallet.update() u = self.wallet.update()
if u: if u:
self.wallet.save()
gobject.idle_add( self.update_history_tab ) gobject.idle_add( self.update_history_tab )
gobject.idle_add( self.update_receiving_tab )
# addressbook too...
time.sleep(self.period) time.sleep(self.period)
except BaseException: except BaseException:
print "starting new session" print "starting new session"
@ -823,6 +825,7 @@ class BitcoinGUI:
scroll.add(treeview) scroll.add(treeview)
hbox = gtk.HBox() hbox = gtk.HBox()
if not is_recv:
button = gtk.Button("New address") button = gtk.Button("New address")
button.connect("clicked", self.newaddress_dialog, is_recv) button.connect("clicked", self.newaddress_dialog, is_recv)
button.show() button.show()
@ -922,7 +925,7 @@ class BitcoinGUI:
for item in h: for item in h:
if not item['is_in'] : n=n+1 if not item['is_in'] : n=n+1
tx = "None" if n==0 else "%d"%n 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): def update_sending_tab(self):
# detect addresses that are not mine in history, add them here... # detect addresses that are not mine in history, add them here...

Loading…
Cancel
Save