From 6e7ffa29ae3938172c2bfd20fbc5ad16172b5b05 Mon Sep 17 00:00:00 2001 From: ThomasV Date: Tue, 7 Jun 2022 16:52:09 +0200 Subject: [PATCH] Move address_is_old to AddressSynchronizer. Cache local_height at that level instead of wallet.synchronize --- electrum/address_synchronizer.py | 22 +++++++++++++++++++++- electrum/lnwatcher.py | 2 +- electrum/wallet.py | 27 ++++----------------------- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/electrum/address_synchronizer.py b/electrum/address_synchronizer.py index e06ef7952..1bc933166 100644 --- a/electrum/address_synchronizer.py +++ b/electrum/address_synchronizer.py @@ -65,8 +65,9 @@ class AddressSynchronizer(Logger): synchronizer: Optional['Synchronizer'] verifier: Optional['SPV'] - def __init__(self, db: 'WalletDB'): + def __init__(self, db: 'WalletDB', config): self.db = db + self.config = config self.network = None Logger.__init__(self) # verifier (SPV) and synchronizer are started in start_network @@ -917,3 +918,22 @@ class AddressSynchronizer(Logger): def is_empty(self, address: str) -> bool: coins = self.get_addr_utxo(address) return not bool(coins) + + @with_local_height_cached + def address_is_old(self, address: str, *, req_conf: int = 3) -> bool: + """Returns whether address has any history that is deeply confirmed. + Used for reorg-safe(ish) gap limit roll-forward. + """ + max_conf = -1 + h = self.db.get_addr_history(address) + needs_spv_check = not self.config.get("skipmerklecheck", False) + for tx_hash, tx_height in h: + if needs_spv_check: + tx_age = self.get_tx_height(tx_hash).conf + else: + if tx_height <= 0: + tx_age = 0 + else: + tx_age = self.get_local_height() - tx_height + 1 + max_conf = max(max_conf, tx_age) + return max_conf >= req_conf diff --git a/electrum/lnwatcher.py b/electrum/lnwatcher.py index 0a1897f43..c3ade499a 100644 --- a/electrum/lnwatcher.py +++ b/electrum/lnwatcher.py @@ -325,7 +325,7 @@ class WatchTower(LNWatcher): LOGGING_SHORTCUT = 'W' def __init__(self, network): - adb = AddressSynchronizer(WalletDB({}, manual_upgrades=False)) + adb = AddressSynchronizer(WalletDB({}, manual_upgrades=False), network.config) adb.start_network(network) LNWatcher.__init__(self, adb, network) self.network = network diff --git a/electrum/wallet.py b/electrum/wallet.py index 5d006c94c..637d3ac34 100644 --- a/electrum/wallet.py +++ b/electrum/wallet.py @@ -294,7 +294,7 @@ class Abstract_Wallet(ABC): self.keystore = None # type: Optional[KeyStore] # will be set by load_keystore self.network = None - self.adb = AddressSynchronizer(db) + self.adb = AddressSynchronizer(db, config) for addr in self.get_addresses(): self.adb.add_address(addr) self.lock = self.adb.lock @@ -511,7 +511,7 @@ class Abstract_Wallet(ABC): if not hasattr(self, '_not_old_change_addresses'): self._not_old_change_addresses = self.get_change_addresses() self._not_old_change_addresses = [addr for addr in self._not_old_change_addresses - if not self.address_is_old(addr)] + if not self.adb.address_is_old(addr)] unused_addrs = [addr for addr in self._not_old_change_addresses if not self.adb.is_used(addr) and not self.is_address_reserved(addr)] return unused_addrs @@ -1666,24 +1666,6 @@ class Abstract_Wallet(ABC): def can_export(self): return not self.is_watching_only() and hasattr(self.keystore, 'get_private_key') - def address_is_old(self, address: str, *, req_conf: int = 3) -> bool: - """Returns whether address has any history that is deeply confirmed. - Used for reorg-safe(ish) gap limit roll-forward. - """ - max_conf = -1 - h = self.db.get_addr_history(address) - needs_spv_check = not self.config.get("skipmerklecheck", False) - for tx_hash, tx_height in h: - if needs_spv_check: - tx_age = self.adb.get_tx_height(tx_hash).conf - else: - if tx_height <= 0: - tx_age = 0 - else: - tx_age = self.adb.get_local_height() - tx_height + 1 - max_conf = max(max_conf, tx_age) - return max_conf >= req_conf - def bump_fee( self, *, @@ -3106,7 +3088,7 @@ class Deterministic_Wallet(Abstract_Wallet): addresses = self.get_receiving_addresses() k = self.num_unused_trailing_addresses(addresses) for addr in addresses[0:-k]: - if self.address_is_old(addr): + if self.adb.address_is_old(addr): n = 0 else: n += 1 @@ -3174,14 +3156,13 @@ class Deterministic_Wallet(Abstract_Wallet): last_few_addresses = self.get_change_addresses(slice_start=-limit) else: last_few_addresses = self.get_receiving_addresses(slice_start=-limit) - if any(map(self.address_is_old, last_few_addresses)): + if any(map(self.adb.address_is_old, last_few_addresses)): count += 1 self.create_new_address(for_change) else: break return count - #@AddressSynchronizer.with_local_height_cached FIXME def synchronize(self): count = 0 with self.lock: