From 12d65f5e522a0edff0acc38b0e44d679d8716975 Mon Sep 17 00:00:00 2001 From: ecdsa Date: Tue, 12 Mar 2013 23:10:43 +0100 Subject: [PATCH] better synchronize method --- lib/wallet.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/wallet.py b/lib/wallet.py index 5c3a7b87..b315c208 100644 --- a/lib/wallet.py +++ b/lib/wallet.py @@ -566,6 +566,7 @@ class Wallet: if h == ['*']: continue for tx_hash, tx_height in h: tx = self.transactions.get(tx_hash) + if tx is None: raise BaseException("Wallet not synchronized") for output in tx.d.get('outputs'): if output.get('address') != addr: continue key = tx_hash + ":%d" % output.get('index') @@ -1157,22 +1158,6 @@ class WalletSynchronizer(threading.Thread): def is_running(self): with self.lock: return self.running - def synchronize_wallet(self): - new_addresses = self.wallet.synchronize() - if new_addresses: - self.subscribe_to_addresses(new_addresses) - self.wallet.up_to_date = False - return - - if not self.interface.is_up_to_date('synchronizer'): - if self.wallet.is_up_to_date(): - self.wallet.set_up_to_date(False) - self.was_updated = True - return - - self.wallet.set_up_to_date(True) - self.was_updated = True - def subscribe_to_addresses(self, addresses): messages = [] @@ -1204,15 +1189,30 @@ class WalletSynchronizer(threading.Thread): self.subscribe_to_addresses(self.wallet.addresses(True)) while self.is_running(): - # 1. send new requests - self.synchronize_wallet() + # 1. create new addresses + new_addresses = self.wallet.synchronize() + # request missing addresses + if new_addresses: + self.subscribe_to_addresses(new_addresses) + + # request missing transactions for tx_hash, tx_height in missing_tx: if (tx_hash, tx_height) not in requested_tx: self.interface.send([ ('blockchain.transaction.get',[tx_hash, tx_height]) ], 'synchronizer') requested_tx.append( (tx_hash, tx_height) ) missing_tx = [] + # detect if situation has changed + if not self.interface.is_up_to_date('synchronizer'): + if self.wallet.is_up_to_date(): + self.wallet.set_up_to_date(False) + self.was_updated = True + else: + if not self.wallet.is_up_to_date(): + self.wallet.set_up_to_date(True) + self.was_updated = True + if self.was_updated: self.interface.trigger_callback('updated') self.was_updated = False