Commit Graph

184 Commits

Author SHA1 Message Date
f321x
19e32d6054 lnwatcher/txbatcher: more logging
log more clearly if an input is considered dust, this makes the logs
more helpful when debugging sweeping of lightning utxos.
2025-10-31 14:50:25 +01:00
SomberNight
b944371ffd adb: change API of util.TxMinedInfo: height() is now always SPV-ed 2025-09-24 13:46:24 +00:00
SomberNight
b036eaf3eb lnwatcher: add some type hints 2025-09-08 15:01:50 +00:00
f321x
23f158291c lnwatcher: catch callback exceptions
Catch exceptions happening to callbacks to continue calling the
remaining callbacks. Otherwise if the first callback throws an exception
the remaining callbacks aren't going to be called.
2025-09-08 13:37:27 +02:00
ThomasV
6ffaa55813 lnwatcher: early return in sweep_commitment_transaction if chan.need_to_subscribe returns False 2025-08-19 14:06:22 +02:00
ThomasV
102132fa43 lnwatcher: add 'subscribe' parameter to add_callback
when a channel is redeemed, we still need to call the callback
in order to set labels, accounting_addresses.

Fixes #9987
2025-07-09 10:32:05 +02:00
SomberNight
f337b4782d 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-07-08 14:02:52 +00:00
SomberNight
a1a55db39c lnwatcher: keep_watching should wait at least until closing_tx is deep
Even if it decides there is nothing to sweep from the ctx/etc, it still needs to
keep watching until a reorg-safe depth.
2025-07-08 13:36:04 +00:00
SomberNight
93738e7159 lnwatcher: maybe_add_pending_forceclose: also check SRK channels
ref https://github.com/spesmilo/electrum/pull/9798#discussion_r2172090792

and clarify "was_added" parameter
2025-07-08 13:31:15 +00:00
SomberNight
2b92e8a97a txbatcher: add type-hints 2025-06-26 14:10:18 +00:00
ThomasV
cd2f52e2d9 follow-up 5610a94537 2025-06-06 12:35:41 +02:00
ThomasV
5610a94537 fix #9856 2025-06-06 12:17:21 +02:00
ThomasV
853b793bef rm verbosity_shortcuts option (unused, redundant) 2025-05-29 16:20:41 +02:00
ThomasV
58480a69db TxBatcher: store fee policy names instead of fee descriptors
This allows to change the fee policy of batches dynamically.
Config.fee_policy is a mapping between policy names and descriptors.
2025-05-26 13:30:25 +02:00
SomberNight
586cf33c05 lnchannel: rm sweep_info cache
- was added when functions in lnsweep returned already signed tx, and signing is expensive
- get_ctx_sweep_info does not presign anymore
- cache invalidation is difficult here
  - e.g. not only on new blocks, but we should e.g. also invalidate the cache when learning new preimages
2025-05-15 19:41:44 +00:00
ThomasV
0607a406ce Qt: add closing warning if we have an unconfirmed local commitment tx with htlcs
add htlc direction (offered, received) to the htlc sweep_info name
regtest: add test_reedeem_received_htlcs
2025-05-12 15:48:20 +02:00
ThomasV
5be646dfd2 call lnwatcher callbacks in asyncio thread
This partially reverts fbebe7de1a
lnwatcher.trigger_callbacks is called manually in commands.py
2025-05-06 12:37:00 +02:00
ThomasV
855aff7c44 lnwatcher: cleanup unused code 2025-05-06 12:37:00 +02:00
ThomasV
e41cefae1a lnwatcher: call maybe_redeem even for already spent inputs
fixes #9789
2025-05-05 17:30:55 +02:00
ThomasV
a511ab8e78 lnwatcher.maybe_extract_preimage: early return if tx is unsigned 2025-04-25 13:31:42 +02:00
ThomasV
3463e68306 add accounting addresses 2025-03-17 10:47:19 +01:00
ThomasV
8e6be0a36a Remove inheritance between LNWatcher and Watchtower
As LNWatcher is no longer async, there is not enough overlap
between these classes to deserve inheritance
2025-03-14 11:59:56 +01:00
ThomasV
fbebe7de1a Make lnwatcher not async
This fixes offline history not having the proper labels
2025-03-14 11:09:11 +01:00
ThomasV
bdb7a82220 batch payment manager:
The class TxBatcher handles the creation, broadcast and replacement
of replaceable transactions. Callers (LNWatcher, SwapManager) use
methods add_payment_output and add_sweep_info. Transactions
created by TxBatcher may combine sweeps and outgoing payments.

Transactions created by TxBatcher will have their fee bumped
automatically (this was only the case for sweeps before).

TxBatcher manages several TxBatches. TxBatches are created
dynamically when needed.

The GUI does not touch txbatcher transactions:
  - wallet.get_candidates_for_batching excludes txbatcher
    transactions
  - RBF dialogs do not work with txbatcher transactions

wallet:
  - instead of reading config variables, make_unsigned_transaction
    takes new parameters: base_tx, send_change_to_lighting

tests:
  - unit tests in test_txbatcher.py (replaces test_sswaps.py)
  - force all regtests to use MPP, so that we sweep transactions
    with several HTLCs. This forces the payment manager to aggregate
    first-stage HTLC tx inputs. second-stage are not batched for now.
2025-03-13 10:17:10 +01:00
ThomasV
798df671ea If we have proposed htlcs in a channel that was force-closed,
call lnworker.htlc_failed once the htlc_tx is deeply mined.

In the case of a forwarding, this will fail incoming htlcs.
(fixes #8547)
2025-03-12 20:11:11 +01:00
ThomasV
c7dcfab3fc lnwatcher: remove unused class ListenerItem 2025-03-11 13:31:49 +01:00
ThomasV
c42201ccac remove lnworker.enable_htlc_settle_onchain: not used
I think this has only been used for manual testing
2025-03-11 12:12:43 +01:00
ThomasV
4641c913c7 lnwatcher: remove unneeded for ... else construct
(backport from batch_payment_manager branch)
2025-03-07 10:44:58 +01:00
ThomasV
840243e029 separate fee policy from config
- Wallet.make_unsigned_transaction takes a FeePolicy parameter
 - fee sliders act on a FeePolicy instead of config
 - different fee policies may be used for different purposes
 - do not detect dust outputs in lnsweep, delegate that to lnwatcher
2025-03-05 10:29:26 +01:00
SomberNight
cba073dfd1 lightning: change derivation of funding_pubkey
Ideally, given an on-chain backup, after the remote force-closes, we should be able to spend our anchor output,
to CPFP the remote commitment tx (assuming the channel used OPTION_ANCHORS).
To spend the anchor output, we need to be able to sign with the local funding_privkey.

Previously we derived the funding_key from the channel_seed (which comes from os.urandom).
Prior to anchors, there was no use case for signing with the funding_key given a channel backup.
Now with anchors, we should make its derivation deterministic somehow, in a way so that it can
be derived given just an on-chain backup.
- one way would be to put some more data into the existing OP_RETURN
  - uses block space
  - the OP_RETURNs can be disabled via "use_recoverable_channels"
  - only the initiator can use OP_RETURNs (so what if channel is in incoming dir?)
- instead, new scheme for our funding_key:
  - we derive the funding_privkey from the lnworker root secret (derived from our bip32 seed)
  - for outgoing channels:
    - lnworker_root_secret + remote_node_id + funding_tx_nlocktime
  - for incoming channels:
    - lnworker_root_secret + remote_node_id + remote_funding_pubkey
  - a check is added to avoid reusing the same key between channels:
      not letting to user open more than one channel with the same peer in a single block
  - only the first 16 bytes of the remote_node_id are used, as the onchain backup OP_RETURNs only contain that
- as the funding_privkey cannot be derived from the channel_seed anymore, it is included in the
imported channel backups, which in turn need a new version defined
  - a wallet db upgrade is used to update already stored imported cbs
  - alternatively we could keep the imported cbs as-is, so no new version, no new funding_privkey field, as it is clearly somewhat redundant given on-chain backups can reconstruct it
    - however adding the field seems easier
      - otherwise the existing code would try to derive the funding_privkey from the channel_seed
      - also note: atm there is no field in the imported backups to distinguish anchor channels vs static-remotekey channels
2025-01-14 17:56:48 +00:00
ThomasV
33d0e6dbec Attach labels to outpoints instead of txids.
Move labels logic from lnworker to wallet.

Due to batching, a single transaction may have several labels attached to it.
2025-01-03 10:54:11 +01:00
ThomasV
29a8c41025 move watchtower to a plugin.
remove watchtower dialog in qt
2024-12-20 15:34:26 +01:00
ThomasV
05266da707 lnwatcher: special-case non-anchor first stage htlcs 2024-12-12 10:52:21 +01:00
ThomasV
3b1dc194e4 Refactor lnsweep:
- txins have an optional make_witness method
 - instead of gen_tx, SweepInfo has a txin and
   an optional txout, for 1st stage HTLCs
 - sweep transactions are created by lnwatcher

The purpose of this change is to allow combining several
inputs in the same sweep transaction.
2024-12-11 17:14:14 +01:00
ThomasV
6598507d3c lnwatcher: replace inspect_tx_candidate with get_spender.
inspect_tx_candidate assumes that htlc transactions have
only one input, which is not true for anchor channels.

inspect_tx_candidate is still used by the watchtower, because
it does not have access to channel information.
2024-12-11 09:33:47 +01:00
bitromortac
d535821516 htlctx: deal with possible peer htlctx batching
Due to anchor channel's sighash.SINGLE and sighash.ANYONECANPAY,
several HTLC-transactions can be combined. This means we must watch for
revoked outputs in the HTLC transaction not only at index 0 but at any
index.
2024-11-26 09:28:00 +01:00
ThomasV
cdc746713e lnwatcher: if broadcast is successful, add tx immediately, so that second-stage htlc can be added as well 2024-11-25 11:39:12 +01:00
bitromortac
b6e224c864 lnwatcher: add field for onchain htlc settlement control 2024-11-21 10:58:41 +01:00
bitromortac
18f81d0080 lnwatcher: renaming and comments for clarity 2024-11-21 10:43:09 +01:00
ThomasV
fef6fc54c9 lnwatcher: do not keep watching channel because of non-existing outputs. fixes #9299 2024-11-14 10:18:07 +01:00
ThomasV
6fb9207a44 lnwatcher: do not listen to 'fee' and 'network_updated' events.
Only blockchain changes are relevant for breach remedy.
2024-10-24 15:25:50 +02:00
ThomasV
f0d0c23869 Invalidate cache of sweep_info on each new block
Add regtest test for swapserver_forceclose
2024-10-24 11:20:12 +02:00
SomberNight
22a8348303 renames: use consistent naming of cltv delta vs cltv abs
to avoid confusing relative vs absolute cltvs
(see b0401a6386)
2023-10-19 16:40:05 +00:00
ThomasV
68159b3ef6 walletDB: replace 'manual_upgrades' parameter with 'upgrade', with opposite semantics 2023-09-22 15:02:07 +02:00
ThomasV
b5bc5ff9ed Separate WalletDB from storage upgrades.
Make sure that WalletDB.data is always a StoredDict.
Perform db upgrades in a separate class, since they
operate on a dict object.
2023-09-22 15:02:06 +02:00
ThomasV
b96cc82333 Make storage a field of db
This comes from the jsonpatch_new branch.
I rather have in master now, because it touches a lot of filese.
2023-08-18 08:08:31 +02:00
SomberNight
a6c36b8588 regtests: test_watchtower started failing due to newly exposed bug
local_watchtower.adb.start_network was getting called twice.

follow-up 6ac3f84095

```
20230418T014725.636141Z |    ERROR | __main__ |
Traceback (most recent call last):
  File "/home/user/wspace/electrum/./run_electrum", line 435, in main
    handle_cmd(
  File "/home/user/wspace/electrum/./run_electrum", line 469, in handle_cmd
    d = daemon.Daemon(config, fd)
  File "/home/user/wspace/electrum/electrum/util.py", line 462, in <lambda>
    return lambda *args, **kw_args: do_profile(args, kw_args)
  File "/home/user/wspace/electrum/electrum/util.py", line 458, in do_profile
    o = func(*args, **kw_args)
  File "/home/user/wspace/electrum/electrum/daemon.py", line 404, in __init__
    self.network = Network(config, daemon=self)
  File "/home/user/wspace/electrum/electrum/network.py", line 348, in __init__
    self.local_watchtower.adb.start_network(self)
  File "/home/user/wspace/electrum/electrum/address_synchronizer.py", line 185, in start_network
    assert self.network is None, "already started"
AssertionError: already started
```
2023-04-18 02:02:07 +00:00
SomberNight
446879ade0 lnwatcher.maybe_redeem: wanted_height should always be absolute
previously, if prev_height.height was <= 0, lnwatcher was calling adb.set_future_tx()
with weird wanted_height values (with ~sweep_info.csv_delay)
2023-04-04 13:37:10 +00:00
SomberNight
1530668960 qt/qml: delay starting network until after first-start-network-setup
The qt, qml, and kivy GUIs have a first-start network-setup screen
that allows the user customising the network settings before creating a wallet.
Previously the daemon used to create the network and start it, before this screen,
before the GUI even starts. If the user changed network settings, those would
be set on the already running network, potentially including restarting the network.

Now it becomes the responsibility of the GUI to start the network, allowing this
first-start customisation to take place before starting the network at all.
The qt and the qml GUIs are adapted to make use of this. Kivy, and the other
prototype GUIs are not adapted and just start the network right away, as before.
2023-03-30 00:59:02 +00:00
SomberNight
62ab6d9702 (trivial) reduce log spam during ln-channel-open 2023-03-09 15:18:09 +00:00