diff --git a/electrum/address_synchronizer.py b/electrum/address_synchronizer.py index 922372014..bc2c0067c 100644 --- a/electrum/address_synchronizer.py +++ b/electrum/address_synchronizer.py @@ -86,7 +86,7 @@ class AddressSynchronizer(Logger): # locks: if you need to take multiple ones, acquire them in the order they are defined here! self.lock = threading.RLock() self.transaction_lock = threading.RLock() - self.future_tx = {} # type: Dict[str, int] # txid -> blocks remaining + self.future_tx = {} # type: Dict[str, int] # txid -> wanted height # Transactions pending verification. txid -> tx_height. Access with self.lock. self.unverified_tx = defaultdict(int) # true when synchronized @@ -626,12 +626,11 @@ class AddressSynchronizer(Logger): return cached_local_height return self.network.get_local_height() if self.network else self.db.get('stored_height', 0) - def add_future_tx(self, tx: Transaction, num_blocks: int) -> bool: - assert num_blocks > 0, num_blocks + def add_future_tx(self, tx: Transaction, wanted_height: int) -> bool: with self.lock: tx_was_added = self.add_transaction(tx) if tx_was_added: - self.future_tx[tx.txid()] = num_blocks + self.future_tx[tx.txid()] = wanted_height return tx_was_added def get_tx_height(self, tx_hash: str) -> TxMinedInfo: @@ -646,9 +645,11 @@ class AddressSynchronizer(Logger): height = self.unverified_tx[tx_hash] return TxMinedInfo(height=height, conf=0) elif tx_hash in self.future_tx: - num_blocks_remainining = self.future_tx[tx_hash] - assert num_blocks_remainining > 0, num_blocks_remainining - return TxMinedInfo(height=TX_HEIGHT_FUTURE, conf=-num_blocks_remainining) + num_blocks_remainining = self.future_tx[tx_hash] - self.get_local_height() + if num_blocks_remainining > 0: + return TxMinedInfo(height=TX_HEIGHT_FUTURE, conf=-num_blocks_remainining) + else: + return TxMinedInfo(height=TX_HEIGHT_LOCAL, conf=0) else: # local transaction return TxMinedInfo(height=TX_HEIGHT_LOCAL, conf=0) diff --git a/electrum/lnwatcher.py b/electrum/lnwatcher.py index 6d8a2ec3a..aebc8468a 100644 --- a/electrum/lnwatcher.py +++ b/electrum/lnwatcher.py @@ -411,16 +411,16 @@ class LNWalletWatcher(LNWatcher): async def try_redeem(self, prevout: str, sweep_info: 'SweepInfo', chan_id_for_log: str, name: str) -> None: prev_txid, prev_index = prevout.split(':') broadcast = True + local_height = self.network.get_local_height() if sweep_info.cltv_expiry: - local_height = self.network.get_local_height() - remaining = sweep_info.cltv_expiry - local_height - if remaining > 0: + wanted_height = sweep_info.cltv_expiry - local_height + if wanted_height - local_height > 0: broadcast = False reason = 'waiting for {}: CLTV ({} > {}), prevout {}'.format(name, local_height, sweep_info.cltv_expiry, prevout) if sweep_info.csv_delay: prev_height = self.get_tx_height(prev_txid) - remaining = sweep_info.csv_delay - prev_height.conf - if remaining > 0: + wanted_height = sweep_info.csv_delay + prev_height.height - 1 + if wanted_height - local_height > 0: broadcast = False reason = 'waiting for {}: CSV ({} >= {}), prevout: {}'.format(name, prev_height.conf, sweep_info.csv_delay, prevout) tx = sweep_info.gen_tx() @@ -432,13 +432,13 @@ class LNWalletWatcher(LNWatcher): if broadcast: await self.network.try_broadcasting(tx, name) else: - if self.lnworker.wallet.db.get_transaction(txid): + if txid in self.lnworker.wallet.future_tx: return self.logger.debug(f'(chan {chan_id_for_log}) trying to redeem {name}: {prevout}') self.logger.info(reason) # it's OK to add local transaction, the fee will be recomputed try: - tx_was_added = self.lnworker.wallet.add_future_tx(tx, remaining) + tx_was_added = self.lnworker.wallet.add_future_tx(tx, wanted_height) except Exception as e: self.logger.info(f'could not add future tx: {name}. prevout: {prevout} {str(e)}') tx_was_added = False