diff --git a/lib/synchronizer.py b/lib/synchronizer.py index 1a866275b..4d4435b73 100644 --- a/lib/synchronizer.py +++ b/lib/synchronizer.py @@ -35,6 +35,7 @@ class WalletSynchronizer(threading.Thread): self.running = False self.lock = threading.Lock() self.queue = Queue.Queue() + self.address_queue = Queue.Queue() def stop(self): with self.lock: self.running = False @@ -42,28 +43,24 @@ class WalletSynchronizer(threading.Thread): def is_running(self): with self.lock: return self.running - + def add(self, address): + self.address_queue.put(address) + def subscribe_to_addresses(self, addresses): messages = [] for addr in addresses: messages.append(('blockchain.address.subscribe', [addr])) self.network.subscribe( messages, lambda i,r: self.queue.put(r)) - def run(self): with self.lock: self.running = True - while self.is_running(): - if not self.network.is_connected(): self.network.wait_until_connected() - self.run_interface() - def run_interface(self): - print_error("synchronizer: connected to", self.network.main_server()) requested_tx = [] @@ -84,10 +81,18 @@ class WalletSynchronizer(threading.Thread): self.subscribe_to_addresses(self.wallet.addresses(True)) while self.is_running(): + # 1. create new addresses - new_addresses = self.wallet.synchronize() + self.wallet.synchronize() # request missing addresses + new_addresses = [] + while True: + try: + addr = self.address_queue.get(block=False) + except Queue.Empty: + break + new_addresses.append(addr) if new_addresses: self.subscribe_to_addresses(new_addresses) diff --git a/lib/wallet.py b/lib/wallet.py index 67676b5c8..53f0d4103 100644 --- a/lib/wallet.py +++ b/lib/wallet.py @@ -1139,25 +1139,23 @@ class Deterministic_Wallet(Abstract_Wallet): if n > nmax: nmax = n return nmax + 1 + def create_new_address(self, account, for_change): + address = account.create_new_address(for_change) + self.history[address] = [] + self.synchronizer.add(address) + self.save_accounts() + def synchronize_sequence(self, account, for_change): limit = self.gap_limit_for_change if for_change else self.gap_limit - new_addresses = [] while True: addresses = account.get_addresses(for_change) if len(addresses) < limit: - address = account.create_new_address(for_change) - self.history[address] = [] - new_addresses.append( address ) + self.create_new_address(account, for_change) continue - if map( lambda a: self.address_is_old(a), addresses[-limit:] ) == limit*[False]: break else: - address = account.create_new_address(for_change) - self.history[address] = [] - new_addresses.append( address ) - - return new_addresses + self.create_new_address(account, for_change) def check_pending_accounts(self): for account_id, addr in self.next_addresses.items(): @@ -1169,22 +1167,15 @@ class Deterministic_Wallet(Abstract_Wallet): self.next_addresses.pop(account_id) def synchronize_account(self, account): - new = [] - new += self.synchronize_sequence(account, 0) - new += self.synchronize_sequence(account, 1) - return new + self.synchronize_sequence(account, 0) + self.synchronize_sequence(account, 1) def synchronize(self): self.check_pending_accounts() - new = [] for account in self.accounts.values(): if type(account) in [ImportedAccount, PendingAccount]: continue - new += self.synchronize_account(account) - if new: - self.save_accounts() - self.storage.put('addr_history', self.history, True) - return new + self.synchronize_account(account) def restore(self, callback): from i18n import _