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