diff --git a/electrum/lnchannel.py b/electrum/lnchannel.py index 5458a2b0c..5329dd271 100644 --- a/electrum/lnchannel.py +++ b/electrum/lnchannel.py @@ -230,6 +230,29 @@ class AbstractChannel(Logger, ABC): def is_redeemed(self): return self.get_state() == ChannelState.REDEEMED + def need_to_subscribe(self) -> bool: + """Whether lnwatcher/synchronizer need to be watching this channel.""" + if not self.is_redeemed(): + return True + # Chan already deeply closed. Still, if some txs are missing, we should sub. + # check we have funding tx + # note: tx might not be directly related to the wallet, e.g. chan opened by remote + if (funding_item := self.get_funding_height()) is None: + return True + if self.lnworker: + funding_txid, funding_height, funding_timestamp = funding_item + if self.lnworker.wallet.adb.get_transaction(funding_txid) is None: + return True + # check we have closing tx + # note: tx might not be directly related to the wallet, e.g. local-fclose + if (closing_item := self.get_closing_height()) is None: + return True + if self.lnworker: + closing_txid, closing_height, closing_timestamp = closing_item + if self.lnworker.wallet.adb.get_transaction(closing_txid) is None: + return True + return False + @abstractmethod def get_close_options(self) -> Sequence[ChanCloseOption]: pass diff --git a/electrum/lnworker.py b/electrum/lnworker.py index 5f6a579c2..5ecaaabb7 100644 --- a/electrum/lnworker.py +++ b/electrum/lnworker.py @@ -762,10 +762,10 @@ class LNWallet(LNWorker): self.lnrater = LNRater(self, network) for chan in self.channels.values(): - if not chan.is_redeemed(): + if chan.need_to_subscribe(): self.lnwatcher.add_channel(chan.funding_outpoint.to_str(), chan.get_funding_address()) for cb in self.channel_backups.values(): - if not cb.is_redeemed(): + if cb.need_to_subscribe(): self.lnwatcher.add_channel(cb.funding_outpoint.to_str(), cb.get_funding_address()) for coro in [ diff --git a/electrum/wallet.py b/electrum/wallet.py index 0c112f551..a82d43375 100644 --- a/electrum/wallet.py +++ b/electrum/wallet.py @@ -878,6 +878,7 @@ class Abstract_Wallet(ABC, Logger, EventListener): result = {} parents = [] tx = self.adb.get_transaction(txid) + assert tx, f"cannot find {txid} in db" for i, txin in enumerate(tx.inputs()): _txid = txin.prevout.txid.hex() parents.append(_txid)