2018-10-25 19:34:31 +02:00
|
|
|
# Copyright (C) 2018 The Electrum developers
|
|
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
|
# file LICENCE or http://www.opensource.org/licenses/mit-license.php
|
|
|
|
|
|
2025-09-08 15:01:50 +00:00
|
|
|
from typing import TYPE_CHECKING, Optional, Dict, Callable, Awaitable
|
2018-07-16 17:01:18 +02:00
|
|
|
|
2025-06-06 12:17:21 +02:00
|
|
|
from . import util
|
lnwatcher: keep watching sweep TXOs that are dust due to high fees
- if fee estimates are high atm, some outputs are not worth to sweep
- however, fee estimates might be only-temporarily very high
- previously in such a case lnwatcher would just discard outputs as dust,
and mark the channel REDEEMED (and hence never watch it or try again)
- now, instead, if the outputs would not be dust if fee estimates were lower,
lnwatcher will keep watching the channel
- and if estimates go down, lnwatcher will sweep them then
- relatedly, previously txbatcher.is_dust() used allow_fallback_to_static_rates=True,
and it erroneously almost always fell back to the static rates (150 s/b) during
startup (race: lnwatcher was faster than the network managed to get estimates)
- now, instead, txbatcher.is_dust() does not fallback to static rates,
and the callers are supposed to handle NoDynamicFeeEstimates.
- I think this makes much more sense. The previous meaning of "is_dust"
with the fallback was weird. Now it means: "is dust at current feerates".
fixes https://github.com/spesmilo/electrum/issues/9980
2025-06-27 14:00:27 +00:00
|
|
|
from .util import TxMinedInfo, BelowDustLimit, NoDynamicFeeEstimates
|
2025-05-06 11:59:53 +02:00
|
|
|
from .util import EventListener, event_listener, log_exceptions, ignore_exceptions
|
2025-02-14 14:12:12 +01:00
|
|
|
from .transaction import Transaction, TxOutpoint
|
2022-06-01 23:03:35 +02:00
|
|
|
from .logging import Logger
|
2025-05-08 19:28:05 +02:00
|
|
|
from .address_synchronizer import TX_HEIGHT_LOCAL
|
lnwatcher: keep watching sweep TXOs that are dust due to high fees
- if fee estimates are high atm, some outputs are not worth to sweep
- however, fee estimates might be only-temporarily very high
- previously in such a case lnwatcher would just discard outputs as dust,
and mark the channel REDEEMED (and hence never watch it or try again)
- now, instead, if the outputs would not be dust if fee estimates were lower,
lnwatcher will keep watching the channel
- and if estimates go down, lnwatcher will sweep them then
- relatedly, previously txbatcher.is_dust() used allow_fallback_to_static_rates=True,
and it erroneously almost always fell back to the static rates (150 s/b) during
startup (race: lnwatcher was faster than the network managed to get estimates)
- now, instead, txbatcher.is_dust() does not fallback to static rates,
and the callers are supposed to handle NoDynamicFeeEstimates.
- I think this makes much more sense. The previous meaning of "is_dust"
with the fallback was weird. Now it means: "is dust at current feerates".
fixes https://github.com/spesmilo/electrum/issues/9980
2025-06-27 14:00:27 +00:00
|
|
|
from .lnutil import REDEEM_AFTER_DOUBLE_SPENT_DELAY
|
2022-04-28 10:21:47 +02:00
|
|
|
|
2018-07-16 17:01:18 +02:00
|
|
|
|
2018-10-22 15:35:57 +02:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from .network import Network
|
2020-02-16 14:45:04 +01:00
|
|
|
from .lnsweep import SweepInfo
|
2020-03-30 03:49:50 +02:00
|
|
|
from .lnworker import LNWallet
|
2025-03-11 18:13:32 +01:00
|
|
|
from .lnchannel import AbstractChannel
|
2018-06-22 10:57:11 +02:00
|
|
|
|
|
|
|
|
|
2022-06-16 12:05:05 +02:00
|
|
|
class LNWatcher(Logger, EventListener):
|
2022-06-01 23:03:35 +02:00
|
|
|
|
2025-03-14 11:43:03 +01:00
|
|
|
def __init__(self, lnworker: 'LNWallet'):
|
|
|
|
|
self.lnworker = lnworker
|
2022-06-01 23:03:35 +02:00
|
|
|
Logger.__init__(self)
|
2025-03-14 11:43:03 +01:00
|
|
|
self.adb = lnworker.wallet.adb
|
|
|
|
|
self.config = lnworker.config
|
2025-09-08 15:01:50 +00:00
|
|
|
self.callbacks = {} # type: Dict[str, Callable[[], Awaitable[None]]] # address -> lambda function
|
2025-02-07 09:52:03 +01:00
|
|
|
self.network = None
|
2022-06-16 12:05:05 +02:00
|
|
|
self.register_callbacks()
|
2025-05-08 19:28:05 +02:00
|
|
|
self._pending_force_closes = set()
|
2019-02-28 06:08:58 +01:00
|
|
|
|
2025-02-07 09:52:03 +01:00
|
|
|
def start_network(self, network: 'Network'):
|
|
|
|
|
self.network = network
|
|
|
|
|
|
2025-03-14 11:43:03 +01:00
|
|
|
def stop(self):
|
2022-06-16 12:05:05 +02:00
|
|
|
self.unregister_callbacks()
|
2020-05-01 04:39:49 +02:00
|
|
|
|
2025-09-08 15:01:50 +00:00
|
|
|
def remove_callback(self, address: str) -> None:
|
2020-05-20 13:49:44 +02:00
|
|
|
self.callbacks.pop(address, None)
|
|
|
|
|
|
2025-09-08 15:01:50 +00:00
|
|
|
def add_callback(
|
|
|
|
|
self,
|
|
|
|
|
address: str,
|
|
|
|
|
callback: Callable[[], Awaitable[None]],
|
|
|
|
|
*,
|
|
|
|
|
subscribe: bool = True,
|
|
|
|
|
) -> None:
|
2025-06-28 16:51:39 +02:00
|
|
|
if subscribe:
|
|
|
|
|
self.adb.add_address(address)
|
2020-05-20 13:49:44 +02:00
|
|
|
self.callbacks[address] = callback
|
2018-12-04 20:50:24 +01:00
|
|
|
|
2025-09-08 15:01:50 +00:00
|
|
|
async def trigger_callbacks(self, *, requires_synchronizer: bool = True):
|
2025-05-06 11:59:53 +02:00
|
|
|
if requires_synchronizer and not self.adb.synchronizer:
|
2025-03-14 11:43:03 +01:00
|
|
|
self.logger.info("synchronizer not set yet")
|
|
|
|
|
return
|
|
|
|
|
for address, callback in list(self.callbacks.items()):
|
2025-09-08 13:37:27 +02:00
|
|
|
try:
|
|
|
|
|
await callback()
|
|
|
|
|
except Exception:
|
|
|
|
|
self.logger.exception(f"LNWatcher callback failed {address=}")
|
2025-06-06 12:17:21 +02:00
|
|
|
# send callback to GUI
|
|
|
|
|
util.trigger_callback('wallet_updated', self.lnworker.wallet)
|
2025-03-14 11:43:03 +01:00
|
|
|
|
2022-06-16 12:05:05 +02:00
|
|
|
@event_listener
|
|
|
|
|
async def on_event_blockchain_updated(self, *args):
|
2025-05-06 11:59:53 +02:00
|
|
|
await self.trigger_callbacks()
|
2022-06-01 23:03:35 +02:00
|
|
|
|
2025-02-14 14:12:12 +01:00
|
|
|
@event_listener
|
2025-06-06 12:35:41 +02:00
|
|
|
async def on_event_adb_added_tx(self, adb, tx_hash, tx):
|
2025-02-14 14:12:12 +01:00
|
|
|
# called if we add local tx
|
2025-06-06 12:35:41 +02:00
|
|
|
if adb != self.adb:
|
2025-02-14 14:12:12 +01:00
|
|
|
return
|
2025-05-06 11:59:53 +02:00
|
|
|
await self.trigger_callbacks()
|
2025-02-14 14:12:12 +01:00
|
|
|
|
2022-06-16 12:05:05 +02:00
|
|
|
@event_listener
|
2025-05-06 11:59:53 +02:00
|
|
|
async def on_event_adb_added_verified_tx(self, adb, tx_hash):
|
2022-06-01 23:03:35 +02:00
|
|
|
if adb != self.adb:
|
|
|
|
|
return
|
2025-05-06 11:59:53 +02:00
|
|
|
await self.trigger_callbacks()
|
2022-06-01 23:03:35 +02:00
|
|
|
|
2022-06-16 12:05:05 +02:00
|
|
|
@event_listener
|
2025-05-06 11:59:53 +02:00
|
|
|
async def on_event_adb_set_up_to_date(self, adb):
|
2022-06-01 23:03:35 +02:00
|
|
|
if adb != self.adb:
|
|
|
|
|
return
|
2025-05-06 11:59:53 +02:00
|
|
|
await self.trigger_callbacks()
|
2025-02-07 09:52:03 +01:00
|
|
|
|
|
|
|
|
def add_channel(self, chan: 'AbstractChannel') -> None:
|
|
|
|
|
outpoint = chan.funding_outpoint.to_str()
|
|
|
|
|
address = chan.get_funding_address()
|
|
|
|
|
callback = lambda: self.check_onchain_situation(address, outpoint)
|
2025-06-28 16:51:39 +02:00
|
|
|
self.add_callback(address, callback, subscribe=chan.need_to_subscribe())
|
2025-03-14 11:43:03 +01:00
|
|
|
|
2025-05-06 11:59:53 +02:00
|
|
|
@ignore_exceptions
|
|
|
|
|
@log_exceptions
|
2025-06-26 13:52:07 +00:00
|
|
|
async def check_onchain_situation(self, address: str, funding_outpoint: str) -> None:
|
2025-02-07 09:52:03 +01:00
|
|
|
# early return if address has not been added yet
|
|
|
|
|
if not self.adb.is_mine(address):
|
|
|
|
|
return
|
|
|
|
|
# inspect_tx_candidate might have added new addresses, in which case we return early
|
2025-06-27 11:37:23 +00:00
|
|
|
# note: maybe we should wait until adb.is_up_to_date... (?)
|
2025-02-07 09:52:03 +01:00
|
|
|
funding_txid = funding_outpoint.split(':')[0]
|
|
|
|
|
funding_height = self.adb.get_tx_height(funding_txid)
|
2025-03-14 11:43:03 +01:00
|
|
|
closing_txid = self.adb.get_spender(funding_outpoint)
|
2025-02-07 09:52:03 +01:00
|
|
|
closing_height = self.adb.get_tx_height(closing_txid)
|
|
|
|
|
if closing_txid:
|
|
|
|
|
closing_tx = self.adb.get_transaction(closing_txid)
|
|
|
|
|
if closing_tx:
|
2025-05-06 11:59:53 +02:00
|
|
|
keep_watching = await self.sweep_commitment_transaction(funding_outpoint, closing_tx)
|
2025-02-07 09:52:03 +01:00
|
|
|
else:
|
|
|
|
|
self.logger.info(f"channel {funding_outpoint} closed by {closing_txid}. still waiting for tx itself...")
|
|
|
|
|
keep_watching = True
|
|
|
|
|
else:
|
|
|
|
|
keep_watching = True
|
2025-05-06 11:59:53 +02:00
|
|
|
await self.update_channel_state(
|
2025-02-07 09:52:03 +01:00
|
|
|
funding_outpoint=funding_outpoint,
|
|
|
|
|
funding_txid=funding_txid,
|
|
|
|
|
funding_height=funding_height,
|
|
|
|
|
closing_txid=closing_txid,
|
|
|
|
|
closing_height=closing_height,
|
|
|
|
|
keep_watching=keep_watching)
|
2020-09-18 20:54:09 +02:00
|
|
|
|
|
|
|
|
def diagnostic_name(self):
|
|
|
|
|
return f"{self.lnworker.wallet.diagnostic_name()}-LNW"
|
2020-02-16 14:26:07 +01:00
|
|
|
|
2025-05-06 11:59:53 +02:00
|
|
|
async def update_channel_state(
|
|
|
|
|
self, *, funding_outpoint: str, funding_txid: str,
|
|
|
|
|
funding_height: TxMinedInfo, closing_txid: str,
|
|
|
|
|
closing_height: TxMinedInfo, keep_watching: bool) -> None:
|
2020-02-16 14:26:07 +01:00
|
|
|
chan = self.lnworker.channel_by_txo(funding_outpoint)
|
|
|
|
|
if not chan:
|
|
|
|
|
return
|
2022-07-12 10:13:19 +02:00
|
|
|
chan.update_onchain_state(
|
|
|
|
|
funding_txid=funding_txid,
|
|
|
|
|
funding_height=funding_height,
|
|
|
|
|
closing_txid=closing_txid,
|
|
|
|
|
closing_height=closing_height,
|
|
|
|
|
keep_watching=keep_watching)
|
2025-05-08 19:28:05 +02:00
|
|
|
if closing_height.conf > 0:
|
|
|
|
|
self._pending_force_closes.discard(chan)
|
2025-05-06 11:59:53 +02:00
|
|
|
await self.lnworker.handle_onchain_state(chan)
|
2020-02-16 14:26:07 +01:00
|
|
|
|
2025-06-26 13:52:07 +00:00
|
|
|
async def sweep_commitment_transaction(self, funding_outpoint: str, closing_tx: Transaction) -> bool:
|
2021-09-13 14:46:29 +02:00
|
|
|
"""This function is called when a channel was closed. In this case
|
|
|
|
|
we need to check for redeemable outputs of the commitment transaction
|
|
|
|
|
or spenders down the line (HTLC-timeout/success transactions).
|
|
|
|
|
|
2025-02-07 09:52:03 +01:00
|
|
|
Returns whether we should continue to monitor.
|
|
|
|
|
|
2025-06-26 13:52:07 +00:00
|
|
|
Side-effects:
|
2025-02-07 09:52:03 +01:00
|
|
|
- sets defaults labels
|
2025-02-07 09:52:03 +01:00
|
|
|
- populates wallet._accounting_addresses
|
2025-02-07 09:52:03 +01:00
|
|
|
"""
|
2025-06-26 13:52:07 +00:00
|
|
|
assert closing_tx
|
2020-02-16 14:26:07 +01:00
|
|
|
chan = self.lnworker.channel_by_txo(funding_outpoint)
|
|
|
|
|
if not chan:
|
2020-04-13 15:57:53 +02:00
|
|
|
return False
|
2025-08-19 13:46:16 +02:00
|
|
|
if not chan.need_to_subscribe():
|
|
|
|
|
return False
|
|
|
|
|
self.logger.info(f'sweep_commitment_transaction {funding_outpoint}')
|
2021-09-13 14:46:29 +02:00
|
|
|
# detect who closed and get information about how to claim outputs
|
2025-05-08 19:28:05 +02:00
|
|
|
is_local_ctx, sweep_info_dict = chan.get_ctx_sweep_info(closing_tx)
|
2025-06-27 11:37:23 +00:00
|
|
|
# note: we need to keep watching *at least* until the closing tx is deeply mined,
|
|
|
|
|
# possibly longer if there are TXOs to sweep
|
|
|
|
|
keep_watching = not self.adb.is_deeply_mined(closing_tx.txid())
|
2021-09-13 14:46:29 +02:00
|
|
|
# create and broadcast transactions
|
2020-02-16 14:26:07 +01:00
|
|
|
for prevout, sweep_info in sweep_info_dict.items():
|
2024-11-14 10:18:07 +01:00
|
|
|
prev_txid, prev_index = prevout.split(':')
|
2024-11-25 11:39:12 +01:00
|
|
|
name = sweep_info.name + ' ' + chan.get_id_for_log()
|
2024-12-12 12:25:07 +01:00
|
|
|
self.lnworker.wallet.set_default_label(prevout, name)
|
2024-11-14 10:18:07 +01:00
|
|
|
if not self.adb.get_transaction(prev_txid):
|
|
|
|
|
# do not keep watching if prevout does not exist
|
2025-02-14 14:12:12 +01:00
|
|
|
self.logger.info(f'prevout does not exist for {name}: {prevout}')
|
2024-11-14 10:18:07 +01:00
|
|
|
continue
|
lnwatcher: keep watching sweep TXOs that are dust due to high fees
- if fee estimates are high atm, some outputs are not worth to sweep
- however, fee estimates might be only-temporarily very high
- previously in such a case lnwatcher would just discard outputs as dust,
and mark the channel REDEEMED (and hence never watch it or try again)
- now, instead, if the outputs would not be dust if fee estimates were lower,
lnwatcher will keep watching the channel
- and if estimates go down, lnwatcher will sweep them then
- relatedly, previously txbatcher.is_dust() used allow_fallback_to_static_rates=True,
and it erroneously almost always fell back to the static rates (150 s/b) during
startup (race: lnwatcher was faster than the network managed to get estimates)
- now, instead, txbatcher.is_dust() does not fallback to static rates,
and the callers are supposed to handle NoDynamicFeeEstimates.
- I think this makes much more sense. The previous meaning of "is_dust"
with the fallback was weird. Now it means: "is dust at current feerates".
fixes https://github.com/spesmilo/electrum/issues/9980
2025-06-27 14:00:27 +00:00
|
|
|
watch_sweep_info = self.maybe_redeem(sweep_info)
|
2025-06-27 11:37:23 +00:00
|
|
|
spender_txid = self.adb.get_spender(prevout) # note: LOCAL spenders don't count
|
2022-06-10 16:00:30 +02:00
|
|
|
spender_tx = self.adb.get_transaction(spender_txid) if spender_txid else None
|
|
|
|
|
if spender_tx:
|
|
|
|
|
# the spender might be the remote, revoked or not
|
2024-12-11 11:51:03 +01:00
|
|
|
htlc_sweepinfo = chan.maybe_sweep_htlcs(closing_tx, spender_tx)
|
|
|
|
|
for prevout2, htlc_sweep_info in htlc_sweepinfo.items():
|
lnwatcher: keep watching sweep TXOs that are dust due to high fees
- if fee estimates are high atm, some outputs are not worth to sweep
- however, fee estimates might be only-temporarily very high
- previously in such a case lnwatcher would just discard outputs as dust,
and mark the channel REDEEMED (and hence never watch it or try again)
- now, instead, if the outputs would not be dust if fee estimates were lower,
lnwatcher will keep watching the channel
- and if estimates go down, lnwatcher will sweep them then
- relatedly, previously txbatcher.is_dust() used allow_fallback_to_static_rates=True,
and it erroneously almost always fell back to the static rates (150 s/b) during
startup (race: lnwatcher was faster than the network managed to get estimates)
- now, instead, txbatcher.is_dust() does not fallback to static rates,
and the callers are supposed to handle NoDynamicFeeEstimates.
- I think this makes much more sense. The previous meaning of "is_dust"
with the fallback was weird. Now it means: "is dust at current feerates".
fixes https://github.com/spesmilo/electrum/issues/9980
2025-06-27 14:00:27 +00:00
|
|
|
watch_htlc_sweep_info = self.maybe_redeem(htlc_sweep_info)
|
2025-03-14 11:43:03 +01:00
|
|
|
htlc_tx_spender = self.adb.get_spender(prevout2)
|
2024-12-12 12:25:07 +01:00
|
|
|
self.lnworker.wallet.set_default_label(prevout2, htlc_sweep_info.name)
|
2021-11-15 14:23:33 +01:00
|
|
|
if htlc_tx_spender:
|
2025-03-14 11:43:03 +01:00
|
|
|
keep_watching |= not self.adb.is_deeply_mined(htlc_tx_spender)
|
2025-02-07 09:52:03 +01:00
|
|
|
self.maybe_add_accounting_address(htlc_tx_spender, htlc_sweep_info)
|
2020-02-16 14:26:07 +01:00
|
|
|
else:
|
lnwatcher: keep watching sweep TXOs that are dust due to high fees
- if fee estimates are high atm, some outputs are not worth to sweep
- however, fee estimates might be only-temporarily very high
- previously in such a case lnwatcher would just discard outputs as dust,
and mark the channel REDEEMED (and hence never watch it or try again)
- now, instead, if the outputs would not be dust if fee estimates were lower,
lnwatcher will keep watching the channel
- and if estimates go down, lnwatcher will sweep them then
- relatedly, previously txbatcher.is_dust() used allow_fallback_to_static_rates=True,
and it erroneously almost always fell back to the static rates (150 s/b) during
startup (race: lnwatcher was faster than the network managed to get estimates)
- now, instead, txbatcher.is_dust() does not fallback to static rates,
and the callers are supposed to handle NoDynamicFeeEstimates.
- I think this makes much more sense. The previous meaning of "is_dust"
with the fallback was weird. Now it means: "is dust at current feerates".
fixes https://github.com/spesmilo/electrum/issues/9980
2025-06-27 14:00:27 +00:00
|
|
|
keep_watching |= watch_htlc_sweep_info
|
2025-03-14 11:43:03 +01:00
|
|
|
keep_watching |= not self.adb.is_deeply_mined(spender_txid)
|
2025-03-11 18:13:32 +01:00
|
|
|
self.maybe_extract_preimage(chan, spender_tx, prevout)
|
2025-02-07 09:52:03 +01:00
|
|
|
self.maybe_add_accounting_address(spender_txid, sweep_info)
|
2020-02-16 14:26:07 +01:00
|
|
|
else:
|
lnwatcher: keep watching sweep TXOs that are dust due to high fees
- if fee estimates are high atm, some outputs are not worth to sweep
- however, fee estimates might be only-temporarily very high
- previously in such a case lnwatcher would just discard outputs as dust,
and mark the channel REDEEMED (and hence never watch it or try again)
- now, instead, if the outputs would not be dust if fee estimates were lower,
lnwatcher will keep watching the channel
- and if estimates go down, lnwatcher will sweep them then
- relatedly, previously txbatcher.is_dust() used allow_fallback_to_static_rates=True,
and it erroneously almost always fell back to the static rates (150 s/b) during
startup (race: lnwatcher was faster than the network managed to get estimates)
- now, instead, txbatcher.is_dust() does not fallback to static rates,
and the callers are supposed to handle NoDynamicFeeEstimates.
- I think this makes much more sense. The previous meaning of "is_dust"
with the fallback was weird. Now it means: "is dust at current feerates".
fixes https://github.com/spesmilo/electrum/issues/9980
2025-06-27 14:00:27 +00:00
|
|
|
keep_watching |= watch_sweep_info
|
2025-06-26 13:52:07 +00:00
|
|
|
self.maybe_add_pending_forceclose(
|
2025-07-08 13:31:15 +00:00
|
|
|
chan=chan,
|
|
|
|
|
spender_txid=spender_txid,
|
|
|
|
|
is_local_ctx=is_local_ctx,
|
|
|
|
|
sweep_info=sweep_info,
|
|
|
|
|
)
|
2020-02-16 18:54:27 +01:00
|
|
|
return keep_watching
|
2020-02-16 14:26:07 +01:00
|
|
|
|
2025-05-08 19:28:05 +02:00
|
|
|
def get_pending_force_closes(self):
|
|
|
|
|
return self._pending_force_closes
|
|
|
|
|
|
2025-02-14 14:12:12 +01:00
|
|
|
def maybe_redeem(self, sweep_info: 'SweepInfo') -> bool:
|
lnwatcher: keep watching sweep TXOs that are dust due to high fees
- if fee estimates are high atm, some outputs are not worth to sweep
- however, fee estimates might be only-temporarily very high
- previously in such a case lnwatcher would just discard outputs as dust,
and mark the channel REDEEMED (and hence never watch it or try again)
- now, instead, if the outputs would not be dust if fee estimates were lower,
lnwatcher will keep watching the channel
- and if estimates go down, lnwatcher will sweep them then
- relatedly, previously txbatcher.is_dust() used allow_fallback_to_static_rates=True,
and it erroneously almost always fell back to the static rates (150 s/b) during
startup (race: lnwatcher was faster than the network managed to get estimates)
- now, instead, txbatcher.is_dust() does not fallback to static rates,
and the callers are supposed to handle NoDynamicFeeEstimates.
- I think this makes much more sense. The previous meaning of "is_dust"
with the fallback was weird. Now it means: "is dust at current feerates".
fixes https://github.com/spesmilo/electrum/issues/9980
2025-06-27 14:00:27 +00:00
|
|
|
""" returns 'keep_watching' """
|
2025-02-14 14:12:12 +01:00
|
|
|
try:
|
2025-05-25 14:44:42 +02:00
|
|
|
self.lnworker.wallet.txbatcher.add_sweep_input('lnwatcher', sweep_info)
|
2025-02-14 14:12:12 +01:00
|
|
|
except BelowDustLimit:
|
2025-10-31 12:48:36 +01:00
|
|
|
self.logger.debug(f"maybe_redeem: BelowDustLimit: {sweep_info.name}")
|
lnwatcher: keep watching sweep TXOs that are dust due to high fees
- if fee estimates are high atm, some outputs are not worth to sweep
- however, fee estimates might be only-temporarily very high
- previously in such a case lnwatcher would just discard outputs as dust,
and mark the channel REDEEMED (and hence never watch it or try again)
- now, instead, if the outputs would not be dust if fee estimates were lower,
lnwatcher will keep watching the channel
- and if estimates go down, lnwatcher will sweep them then
- relatedly, previously txbatcher.is_dust() used allow_fallback_to_static_rates=True,
and it erroneously almost always fell back to the static rates (150 s/b) during
startup (race: lnwatcher was faster than the network managed to get estimates)
- now, instead, txbatcher.is_dust() does not fallback to static rates,
and the callers are supposed to handle NoDynamicFeeEstimates.
- I think this makes much more sense. The previous meaning of "is_dust"
with the fallback was weird. Now it means: "is dust at current feerates".
fixes https://github.com/spesmilo/electrum/issues/9980
2025-06-27 14:00:27 +00:00
|
|
|
# utxo is considered dust at *current* fee estimates.
|
|
|
|
|
# but maybe the fees atm are very high? We will retry later.
|
|
|
|
|
pass
|
|
|
|
|
except NoDynamicFeeEstimates:
|
2025-10-31 12:48:36 +01:00
|
|
|
self.logger.debug(f"maybe_redeem: NoDynamicFeeEstimates: {sweep_info.name}")
|
lnwatcher: keep watching sweep TXOs that are dust due to high fees
- if fee estimates are high atm, some outputs are not worth to sweep
- however, fee estimates might be only-temporarily very high
- previously in such a case lnwatcher would just discard outputs as dust,
and mark the channel REDEEMED (and hence never watch it or try again)
- now, instead, if the outputs would not be dust if fee estimates were lower,
lnwatcher will keep watching the channel
- and if estimates go down, lnwatcher will sweep them then
- relatedly, previously txbatcher.is_dust() used allow_fallback_to_static_rates=True,
and it erroneously almost always fell back to the static rates (150 s/b) during
startup (race: lnwatcher was faster than the network managed to get estimates)
- now, instead, txbatcher.is_dust() does not fallback to static rates,
and the callers are supposed to handle NoDynamicFeeEstimates.
- I think this makes much more sense. The previous meaning of "is_dust"
with the fallback was weird. Now it means: "is dust at current feerates".
fixes https://github.com/spesmilo/electrum/issues/9980
2025-06-27 14:00:27 +00:00
|
|
|
pass # will retry later
|
|
|
|
|
if sweep_info.is_anchor():
|
2025-02-14 14:12:12 +01:00
|
|
|
return False
|
|
|
|
|
return True
|
2025-03-11 18:13:32 +01:00
|
|
|
|
|
|
|
|
def maybe_extract_preimage(self, chan: 'AbstractChannel', spender_tx: Transaction, prevout: str):
|
2025-04-25 12:40:18 +02:00
|
|
|
if not spender_tx.is_complete():
|
|
|
|
|
self.logger.info('spender tx is unsigned')
|
|
|
|
|
return
|
2025-03-11 18:13:32 +01:00
|
|
|
txin_idx = spender_tx.get_input_idx_that_spent_prevout(TxOutpoint.from_str(prevout))
|
|
|
|
|
assert txin_idx is not None
|
|
|
|
|
spender_txin = spender_tx.inputs()[txin_idx]
|
|
|
|
|
chan.extract_preimage_from_htlc_txin(
|
|
|
|
|
spender_txin,
|
2025-03-14 11:43:03 +01:00
|
|
|
is_deeply_mined=self.adb.is_deeply_mined(spender_tx.txid()),
|
2025-03-11 18:13:32 +01:00
|
|
|
)
|
2025-02-07 09:52:03 +01:00
|
|
|
|
|
|
|
|
def maybe_add_accounting_address(self, spender_txid: str, sweep_info: 'SweepInfo'):
|
|
|
|
|
spender_tx = self.adb.get_transaction(spender_txid) if spender_txid else None
|
|
|
|
|
if not spender_tx:
|
|
|
|
|
return
|
|
|
|
|
for i, txin in enumerate(spender_tx.inputs()):
|
|
|
|
|
if txin.prevout == sweep_info.txin.prevout:
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
return
|
2025-05-08 19:28:05 +02:00
|
|
|
if sweep_info.name in ['offered-htlc', 'received-htlc']:
|
2025-02-07 09:52:03 +01:00
|
|
|
# always consider ours
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
witness = txin.witness_elements()
|
|
|
|
|
for sig in witness:
|
|
|
|
|
# fixme: verify sig is ours
|
|
|
|
|
witness2 = sweep_info.txin.make_witness(sig)
|
|
|
|
|
if txin.witness == witness2:
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
self.logger.info(f"signature not found {sweep_info.name}, {txin.prevout.to_str()}")
|
|
|
|
|
return
|
|
|
|
|
self.logger.info(f'adding txin address {sweep_info.name}, {txin.prevout.to_str()}')
|
|
|
|
|
prev_txid, prev_index = txin.prevout.to_str().split(':')
|
|
|
|
|
prev_tx = self.adb.get_transaction(prev_txid)
|
|
|
|
|
txout = prev_tx.outputs()[int(prev_index)]
|
|
|
|
|
self.lnworker.wallet._accounting_addresses.add(txout.address)
|
2025-05-08 19:28:05 +02:00
|
|
|
|
2025-06-26 13:52:07 +00:00
|
|
|
def maybe_add_pending_forceclose(
|
|
|
|
|
self,
|
|
|
|
|
*,
|
|
|
|
|
chan: 'AbstractChannel',
|
|
|
|
|
spender_txid: Optional[str],
|
|
|
|
|
is_local_ctx: bool,
|
|
|
|
|
sweep_info: 'SweepInfo',
|
lnwatcher: keep watching sweep TXOs that are dust due to high fees
- if fee estimates are high atm, some outputs are not worth to sweep
- however, fee estimates might be only-temporarily very high
- previously in such a case lnwatcher would just discard outputs as dust,
and mark the channel REDEEMED (and hence never watch it or try again)
- now, instead, if the outputs would not be dust if fee estimates were lower,
lnwatcher will keep watching the channel
- and if estimates go down, lnwatcher will sweep them then
- relatedly, previously txbatcher.is_dust() used allow_fallback_to_static_rates=True,
and it erroneously almost always fell back to the static rates (150 s/b) during
startup (race: lnwatcher was faster than the network managed to get estimates)
- now, instead, txbatcher.is_dust() does not fallback to static rates,
and the callers are supposed to handle NoDynamicFeeEstimates.
- I think this makes much more sense. The previous meaning of "is_dust"
with the fallback was weird. Now it means: "is dust at current feerates".
fixes https://github.com/spesmilo/electrum/issues/9980
2025-06-27 14:00:27 +00:00
|
|
|
) -> None:
|
|
|
|
|
"""Adds chan into set of ongoing force-closures if the user should keep the wallet open, waiting for it.
|
|
|
|
|
(we are waiting for ctx to be confirmed and there are received htlcs)
|
|
|
|
|
"""
|
|
|
|
|
if is_local_ctx and sweep_info.name == 'received-htlc':
|
|
|
|
|
cltv = sweep_info.cltv_abs
|
|
|
|
|
assert cltv is not None, f"missing cltv for {sweep_info}"
|
|
|
|
|
if self.adb.get_local_height() > cltv + REDEEM_AFTER_DOUBLE_SPENT_DELAY:
|
|
|
|
|
# We had plenty of time to sweep. The remote also had time to time out the htlc.
|
|
|
|
|
# Maybe its value has been ~dust at current and past fee levels (every time we checked).
|
|
|
|
|
# We should not keep warning the user forever.
|
|
|
|
|
return
|
2025-05-08 19:28:05 +02:00
|
|
|
tx_mined_status = self.adb.get_tx_height(spender_txid)
|
2025-09-10 15:24:28 +00:00
|
|
|
if tx_mined_status.height() == TX_HEIGHT_LOCAL:
|
2025-05-08 19:28:05 +02:00
|
|
|
self._pending_force_closes.add(chan)
|