separate synchronize and update_tx_history

This commit is contained in:
ThomasV 2011-12-19 13:18:19 +01:00
parent a7aa613b58
commit fd5f581128
2 changed files with 28 additions and 13 deletions

View File

@ -355,13 +355,16 @@ class Wallet:
def synchronize(self): def synchronize(self):
is_new = False
while True: while True:
if self.change_addresses == []: if self.change_addresses == []:
self.create_new_address2(True) self.create_new_address2(True)
is_new = True
continue continue
a = self.change_addresses[-1] a = self.change_addresses[-1]
if self.history.get(a): if self.history.get(a):
self.create_new_address2(True) self.create_new_address2(True)
is_new = True
else: else:
break break
@ -369,17 +372,19 @@ class Wallet:
while True: while True:
if len(self.addresses) < n: if len(self.addresses) < n:
self.create_new_address2(False) self.create_new_address2(False)
is_new = True
continue continue
if map( lambda a: self.history.get(a), self.addresses[-n:] ) == n*[[]]: if map( lambda a: self.history.get(a), self.addresses[-n:] ) == n*[[]]:
break break
else: else:
self.create_new_address2(False) self.create_new_address2(False)
is_new = True
is_found = (len(self.change_addresses) > 1 ) or ( len(self.addresses) > self.gap_limit )
if not is_found: return False
# history and addressbook def is_found(self):
self.update_tx_history() return (len(self.change_addresses) > 1 ) or ( len(self.addresses) > self.gap_limit )
def fill_addressbook(self):
for tx in self.tx_history.values(): for tx in self.tx_history.values():
if tx['value']<0: if tx['value']<0:
for i in tx['outputs']: for i in tx['outputs']:
@ -387,7 +392,7 @@ class Wallet:
self.addressbook.append(i) self.addressbook.append(i)
# redo labels # redo labels
self.update_tx_labels() self.update_tx_labels()
return True
def save(self): def save(self):
s = { s = {
@ -528,6 +533,7 @@ class Wallet:
self.servers = map( lambda x:x[1], ast.literal_eval( self.request( repr ( ('peers', '' )))) ) self.servers = map( lambda x:x[1], ast.literal_eval( self.request( repr ( ('peers', '' )))) )
def update(self): def update(self):
is_new = False
blocks, changed_addresses = self.poll() blocks, changed_addresses = self.poll()
if blocks == -1: raise BaseException("session not found") if blocks == -1: raise BaseException("session not found")
self.blocks = int(blocks) self.blocks = int(blocks)
@ -536,9 +542,11 @@ class Wallet:
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
is_new = True
if changed_addresses: if is_new:
self.synchronize() self.synchronize()
self.update_tx_history()
self.save() self.save()
return True return True
else: else:
@ -756,10 +764,11 @@ 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.synchronize() wallet.synchronize()
if r: if wallet.is_found():
print "recovery successful" wallet.fill_addressbook()
wallet.save() wallet.save()
print "recovery successful"
else: else:
print "no wallet found" print "no wallet found"
else: else:

View File

@ -134,9 +134,15 @@ def init_wallet(wallet):
def recover_thread( wallet, dialog ): def recover_thread( wallet, dialog ):
wallet.init_mpk( wallet.seed ) # not encrypted at this point wallet.init_mpk( wallet.seed ) # not encrypted at this point
wallet.is_found = wallet.synchronize() wallet.synchronize()
if wallet.is_found:
if wallet.is_found():
# history and addressbook
wallet.update_tx_history()
wallet.fill_addressbook()
print "recovery successful"
wallet.save() wallet.save()
gobject.idle_add( dialog.destroy ) gobject.idle_add( dialog.destroy )
thread.start_new_thread( recover_thread, ( wallet, dialog ) ) thread.start_new_thread( recover_thread, ( wallet, dialog ) )
@ -504,8 +510,8 @@ class BitcoinGUI:
get_servers_time = time.time() get_servers_time = time.time()
self.period = 15 if self.wallet.use_http() else 5 self.period = 15 if self.wallet.use_http() else 5
u = self.wallet.update() if self.wallet.update():
if u: self.wallet.update_session()
gobject.idle_add( self.update_history_tab ) gobject.idle_add( self.update_history_tab )
gobject.idle_add( self.update_receiving_tab ) gobject.idle_add( self.update_receiving_tab )
# addressbook too... # addressbook too...