Browse Source

Move address_is_old to AddressSynchronizer.

Cache local_height at that level instead of wallet.synchronize
master
ThomasV 4 years ago
parent
commit
6e7ffa29ae
  1. 22
      electrum/address_synchronizer.py
  2. 2
      electrum/lnwatcher.py
  3. 27
      electrum/wallet.py

22
electrum/address_synchronizer.py

@ -65,8 +65,9 @@ class AddressSynchronizer(Logger):
synchronizer: Optional['Synchronizer'] synchronizer: Optional['Synchronizer']
verifier: Optional['SPV'] verifier: Optional['SPV']
def __init__(self, db: 'WalletDB'): def __init__(self, db: 'WalletDB', config):
self.db = db self.db = db
self.config = config
self.network = None self.network = None
Logger.__init__(self) Logger.__init__(self)
# verifier (SPV) and synchronizer are started in start_network # verifier (SPV) and synchronizer are started in start_network
@ -917,3 +918,22 @@ class AddressSynchronizer(Logger):
def is_empty(self, address: str) -> bool: def is_empty(self, address: str) -> bool:
coins = self.get_addr_utxo(address) coins = self.get_addr_utxo(address)
return not bool(coins) 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

2
electrum/lnwatcher.py

@ -325,7 +325,7 @@ class WatchTower(LNWatcher):
LOGGING_SHORTCUT = 'W' LOGGING_SHORTCUT = 'W'
def __init__(self, network): def __init__(self, network):
adb = AddressSynchronizer(WalletDB({}, manual_upgrades=False)) adb = AddressSynchronizer(WalletDB({}, manual_upgrades=False), network.config)
adb.start_network(network) adb.start_network(network)
LNWatcher.__init__(self, adb, network) LNWatcher.__init__(self, adb, network)
self.network = network self.network = network

27
electrum/wallet.py

@ -294,7 +294,7 @@ class Abstract_Wallet(ABC):
self.keystore = None # type: Optional[KeyStore] # will be set by load_keystore self.keystore = None # type: Optional[KeyStore] # will be set by load_keystore
self.network = None self.network = None
self.adb = AddressSynchronizer(db) self.adb = AddressSynchronizer(db, config)
for addr in self.get_addresses(): for addr in self.get_addresses():
self.adb.add_address(addr) self.adb.add_address(addr)
self.lock = self.adb.lock self.lock = self.adb.lock
@ -511,7 +511,7 @@ class Abstract_Wallet(ABC):
if not hasattr(self, '_not_old_change_addresses'): if not hasattr(self, '_not_old_change_addresses'):
self._not_old_change_addresses = self.get_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 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 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)] if not self.adb.is_used(addr) and not self.is_address_reserved(addr)]
return unused_addrs return unused_addrs
@ -1666,24 +1666,6 @@ class Abstract_Wallet(ABC):
def can_export(self): def can_export(self):
return not self.is_watching_only() and hasattr(self.keystore, 'get_private_key') 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( def bump_fee(
self, self,
*, *,
@ -3106,7 +3088,7 @@ class Deterministic_Wallet(Abstract_Wallet):
addresses = self.get_receiving_addresses() addresses = self.get_receiving_addresses()
k = self.num_unused_trailing_addresses(addresses) k = self.num_unused_trailing_addresses(addresses)
for addr in addresses[0:-k]: for addr in addresses[0:-k]:
if self.address_is_old(addr): if self.adb.address_is_old(addr):
n = 0 n = 0
else: else:
n += 1 n += 1
@ -3174,14 +3156,13 @@ class Deterministic_Wallet(Abstract_Wallet):
last_few_addresses = self.get_change_addresses(slice_start=-limit) last_few_addresses = self.get_change_addresses(slice_start=-limit)
else: else:
last_few_addresses = self.get_receiving_addresses(slice_start=-limit) 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 count += 1
self.create_new_address(for_change) self.create_new_address(for_change)
else: else:
break break
return count return count
#@AddressSynchronizer.with_local_height_cached FIXME
def synchronize(self): def synchronize(self):
count = 0 count = 0
with self.lock: with self.lock:

Loading…
Cancel
Save