self-synchronizing wallet

This commit is contained in:
thomasv 2011-12-16 12:22:04 +01:00
parent 3333db56c1
commit 5af496ceb5
2 changed files with 40 additions and 36 deletions

View File

@ -341,40 +341,39 @@ 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
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
else:
is_found = True
num_gap = 0
if not is_found: return False
# remove limit-1 addresses.
n = self.gap_limit
self.addresses = self.addresses[:-n]
while True:
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:
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
# history and addressbook
self.update_tx_history()
@ -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()

View File

@ -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...