electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 37e7add7765dc4ec48dbb5fdeb05e79e9f97eac0
parent fb76fcc886b7c999387a6676f479678df742fdaa
Author: ThomasV <thomasv@electrum.org>
Date:   Wed,  3 Jul 2019 08:46:00 +0200

Do not pass storage to address_synchronizer

Diffstat:
Melectrum/address_synchronizer.py | 25+++++--------------------
Melectrum/wallet.py | 14+++++++++++---
2 files changed, 16 insertions(+), 23 deletions(-)

diff --git a/electrum/address_synchronizer.py b/electrum/address_synchronizer.py @@ -38,7 +38,6 @@ from .i18n import _ from .logging import Logger if TYPE_CHECKING: - from .storage import WalletStorage from .network import Network @@ -60,12 +59,8 @@ class AddressSynchronizer(Logger): inherited by wallet """ - def __init__(self, storage: 'WalletStorage'): - if not storage.is_ready_to_be_used_by_wallet(): - raise Exception("storage not ready to be used by AddressSynchronizer") - - self.storage = storage - self.db = self.storage.db + def __init__(self, db): + self.db = db self.network = None # type: Network Logger.__init__(self) # verifier (SPV) and synchronizer are started in start_network @@ -158,7 +153,7 @@ class AddressSynchronizer(Logger): def on_blockchain_updated(self, event, *args): self._get_addr_balance_cache = {} # invalidate cache - def stop_threads(self, write_to_disk=True): + def stop_threads(self): if self.network: if self.synchronizer: asyncio.run_coroutine_threadsafe(self.synchronizer.stop(), self.network.asyncio_loop) @@ -167,9 +162,7 @@ class AddressSynchronizer(Logger): asyncio.run_coroutine_threadsafe(self.verifier.stop(), self.network.asyncio_loop) self.verifier = None self.network.unregister_callback(self.on_blockchain_updated) - self.storage.put('stored_height', self.get_local_height()) - if write_to_disk: - self.storage.write() + self.db.put('stored_height', self.get_local_height()) def add_address(self, address): if not self.db.get_addr_history(address): @@ -370,12 +363,10 @@ class AddressSynchronizer(Logger): @profiler def check_history(self): - save = False hist_addrs_mine = list(filter(lambda k: self.is_mine(k), self.db.get_history())) hist_addrs_not_mine = list(filter(lambda k: not self.is_mine(k), self.db.get_history())) for addr in hist_addrs_not_mine: self.db.remove_addr_history(addr) - save = True for addr in hist_addrs_mine: hist = self.db.get_addr_history(addr) for tx_hash, tx_height in hist: @@ -384,9 +375,6 @@ class AddressSynchronizer(Logger): tx = self.db.get_transaction(tx_hash) if tx is not None: self.add_transaction(tx_hash, tx, allow_unrelated=True) - save = True - if save: - self.storage.write() def remove_local_transactions_we_dont_have(self): for txid in itertools.chain(self.db.list_txi(), self.db.list_txo()): @@ -398,7 +386,6 @@ class AddressSynchronizer(Logger): with self.lock: with self.transaction_lock: self.db.clear_history() - self.storage.write() def get_txpos(self, tx_hash): """Returns (height, txpos) tuple, even if the tx is unverified.""" @@ -560,7 +547,7 @@ class AddressSynchronizer(Logger): cached_local_height = getattr(self.threadlocal_cache, 'local_height', None) if cached_local_height is not None: return cached_local_height - return self.network.get_local_height() if self.network else self.storage.get('stored_height', 0) + return self.network.get_local_height() if self.network else self.db.get('stored_height', 0) def get_tx_height(self, tx_hash: str) -> TxMinedInfo: with self.lock: @@ -580,8 +567,6 @@ class AddressSynchronizer(Logger): self.up_to_date = up_to_date if self.network: self.network.notify('status') - if up_to_date: - self.storage.write() def is_up_to_date(self): with self.lock: return self.up_to_date diff --git a/electrum/wallet.py b/electrum/wallet.py @@ -209,10 +209,10 @@ class Abstract_Wallet(AddressSynchronizer): if not storage.is_ready_to_be_used_by_wallet(): raise Exception("storage not ready to be used by Abstract_Wallet") + self.storage = storage # load addresses needs to be called before constructor for sanity checks - storage.db.load_addresses(self.wallet_type) - - AddressSynchronizer.__init__(self, storage) + self.storage.db.load_addresses(self.wallet_type) + AddressSynchronizer.__init__(self, storage.db) # saved fields self.use_change = storage.get('use_change', True) @@ -235,6 +235,14 @@ class Abstract_Wallet(AddressSynchronizer): self._coin_price_cache = {} + def stop_threads(self): + super().stop_threads() + self.storage.write() + + def set_up_to_date(self, b): + super().set_up_to_date(b) + if b: self.storage.write() + def load_and_cleanup(self): self.load_keystore() self.test_addresses_sanity()