Commit Graph

683 Commits

Author SHA1 Message Date
Rusty Russell
11df13e779 channeld: fix memleak where tx gets leaked.
We steal it onto "peer" where we should steal it onto the inflight:

```
label=struct bitcoin_tx
backtrace:
  ccan/ccan/tal/tal.c:477 (tal_alloc_)
  bitcoin/tx.c:612 (clone_bitcoin_tx)
  channeld/channeld.c:2163 (handle_peer_commit_sig)
  channeld/channeld.c:2191 (handle_peer_commit_sig)
  channeld/channeld.c:2831 (interactive_send_commitments)
  channeld/channeld.c:3814 (splice_initiator_user_finalized)
  channeld/channeld.c:3882 (splice_initiator_user_update)
  channeld/channeld.c:5651 (req_in)
  channeld/channeld.c:6044 (main)
  ../sysdeps/nptl/libc_start_call_main.h:58 (__libc_start_call_main)
  ../csu/libc-start.c:360 (__libc_start_main_impl)
parents:
  struct peer
```

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-10-04 07:45:57 +10:30
Rusty Russell
e21bcbfbb9 channeld: fix memleak when inflights is NULL.
In this case, we were allocating off NULL, which meant a leak:

```
 MEMLEAK: 0x565086722e98
   label=channeld/channeld.c:3433:struct inflight
   backtrace:
     ccan/ccan/tal/tal.c:477 (tal_alloc_)
     channeld/channeld.c:3433 (inflights_new)
     channeld/channeld.c:3573 (splice_accepter)
     channeld/channeld.c:4145 (peer_in)
     channeld/channeld.c:6051 (main)
   parents:
```

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-10-04 07:45:57 +10:30
Dusty Daemon
181d6cc908 splice: minor code cleanups
A few little stylistic things were bugging me when reading through the splice code so I cleaned them up.

ChangeLog-None
2023-10-04 07:45:57 +10:30
Rusty Russell
e11b35cb3a common/memleak: implement callback arg for dump_memleak.
This makes it easier to use outside simple subds, and now lightningd can
simply dump to log rather than returning JSON.

JSON formatting was a lot of work, and we only did it for lightningd, not for
subdaemons.  Easier to use the logs in all cases.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-10-03 10:05:55 +02:00
Rusty Russell
f2d8f37f1f channeld: remove #if DEVELOPER in favor of runtime flag.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-09-21 20:08:24 +09:30
Rusty Russell
91b5a3b323 bitcoin: remove DEVELOPER options.
Mainly removing the PSBT re-marshalling which hasn't had any issues in
recent libwally, and making dev_no_grind into the clearer
dev_no_signature_grind.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-09-21 20:08:24 +09:30
Rusty Russell
a9f26b7d07 common/daemon.c: remove #ifdef DEVELOPER in favor of runtime flag.
Also requires us to expose memleak when !DEVELOPER, however we only
ever used the memleak tracking when the LIGHTNINGD_DEV_MEMLEAK
environment variable was set, so keep that.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-09-21 20:08:24 +09:30
Dusty Daemon
0a5ef7f2e6 splice: Fixes from splice-out test
Added a test for splicing out that exposed some behavior and code glitches that are addressed in this commit.

Added test for splice gossip.

Also added documentation for how to do a splice out.

ChangeLog-Fixed: Added docs, testing, and some fixes related to splicing out, insufficent balance handling, and restarting during a splice.
2023-09-21 13:41:45 +09:30
Rusty Russell
48522f3e9e common: don't hang up when we receive a warning.
We were allowed to, but the spec removed that.  So we handle warnings
differently from errors now.

This also means the LND "internal error" workaround is done in
lightningd (we still disconnect, but we don't want to close channel).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: Protocol: we no longer disconnect every time we receive a warning message.
2023-09-20 13:56:46 +09:30
Dusty Daemon
5531c9d460 splice: quieter in more cases
We also shouldn’t be announcing channels or commitments when we have requested for `stfu` mode but are waiting for our apeer.

Changelog-None
2023-08-15 06:25:30 +09:30
Rusty Russell
028dcb875b channeld: fix gcc-12.3.0 -O3 warning.
```
In function ‘peer_reconnect’,
    inlined from ‘init_channel’ at channeld/channeld.c:5890:3,
    inlined from ‘main’ at channeld/channeld.c:5951:2:
channeld/channeld.c:5028:21: error: ‘next_matches_inflight’ may be used uninitialized [-Werror=maybe-uninitialized]
 5027 |                 if (remote_next_funding && !next_matches_current
      |                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 5028 |                     && !next_matches_inflight) {
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~~
channeld/channeld.c: In function ‘main’:
channeld/channeld.c:4595:36: note: ‘next_matches_inflight’ was declared here
 4595 |         bool next_matches_current, next_matches_inflight;
      |                                    ^~~~~~~~~~~~~~~~~~~~~
channeld/channeld.c:5042:57: error: ‘inflight’ may be used uninitialized [-Werror=maybe-uninitialized]
 5042 |                                                         &inflight->outpoint.txid),
      |                                                         ^
channeld/channeld.c:4594:26: note: ‘inflight’ was declared here
 4594 |         struct inflight *inflight;
      |                          ^~~~~~~~
cc1: all warnings being treated as errors
make: *** [Makefile:300: channeld/channeld.o] Error 1
```

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-08-15 06:21:23 +09:30
Dusty Daemon
1f929114d9 splice: STFU -> disable announce and commit timers
There is a small chance these can fire during a splice or other STFU mode activity which shouldn’t be allowed to happen.

Changelog-None
2023-08-13 12:59:56 +09:30
Dusty Daemon
c50e93d9fb splice: Move splice to experimental feature bit
This was recommended by @t-bast: if the final spec commits to something
compatible, we can simply advertize and accept both features, but if it
does change in incompatible ways we won't cause problems for nodes
who implement the official spec.

(I split this, so first, we remove the OPT_SPLICE entirely, to make
sure we caught them all. --RR)

Suggested-by: @t-bast
Changelog-None
2023-08-10 15:52:53 +09:30
Rusty Russell
91a58a0bdc channeld: don't send splice TLV fields unless negotiated.
This make ACINQ seize up, and not send revoke_and_ack.  Eventually,
this can cause a bad signature error, should payments go in both
directions, which is a separate bug, but this is the trigger.

See: #6500
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-08-07 15:22:38 +09:30
Rusty Russell
84fe3dc940 build: more 32-bit printf fixes.
Reported-by: Shahana
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-08-07 11:52:40 +09:30
Rusty Russell
568f277835 channeld: replace struct splice with struct splicing for OpenBSD.
Since it's only for transitory splicing info, the new name makes sense.

```
cc channeld/channeld.c
In file included from channeld/channeld.c:23:
./channeld/splice.h:37:8: error: redefinition of 'splice'
struct splice {
       ^
/usr/include/sys/socket.h:140:8: note: previous definition is here
struct  splice {
        ^
```

Reported-by: @grubles
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Fixes: #6486
2023-08-03 13:43:35 +09:30
niftynei
dd871d9e26 inflights: use ctx for making new ones
Also convert everything to an array thingy
2023-07-31 21:00:22 +09:30
Dusty Daemon
4628e3ace8 channeld: Code to implement splicing
Update the lightningd <-> channeld interface with lots of new commands to needed to facilitate spicing.

Implement the channeld splicing protocol leveraging the interactivetx protocol.

Implement lightningd’s channel_control to support channeld in its splicing efforts.

Changelog-Added: Added the features to enable splicing & resizing of active channels.
2023-07-31 21:00:22 +09:30
Rusty Russell
38ee765922 channeld: don't backtrace if master closes during sync read.
STATUS_FAIL_MASTER_IO is correct here (and what we use elsewhere):
STATUS_FAIL_INTERNAL_ERR prints a worrying backtrace:

```
0x5606a2992bdf send_backtrace
        common/daemon.c:33
0x5606a299ccab status_failed
        common/status.c:221
0x5606a297f4f1 master_wait_sync_reply
        channeld/channeld.c:978
0x5606a29826ef send_commit
        channeld/channeld.c:1379
0x5606a299d06a timer_expired
        common/timeout.c:62
0x5606a2984fcd main
        channeld/channeld.c:4046
0x7fd97dd9ed8f __libc_start_call_main
        ../sysdeps/nptl/libc_start_call_main.h:58
0x7fd97dd9ee3f __libc_start_main_impl
        ../csu/libc-start.c:392
0x5606a297e724 ???
        ???:0
0xffffffffffffffff ???
        ???:0
```

Fixes: #6221
Reported-by: https://github.com/joelklabo
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-07-31 20:51:43 +09:30
Vincenzo Palazzo
f4fd12cc15 channeld: Verify the signature sent by the counterparty
This commit addresses an issue to enhance the resilience of core
lightning when receiving node announcements.

According to BOLT 7 (The announcement_signatures Message),
if the node_signature OR the bitcoin_signature is NOT correct,
it is recommended to either send a warning and close the connection or send an error and fail the channel.

In this commit, we take a strict approach. If any error is detected, we
send an error and fail the open channel operation.
This is because the announcement_signatures operation is optional,
and we assume that it must be correct.

lnprototest at commit dea47c29b5541dbfe7fe53cc2598330e897fa4f4 report
the following error now.

```
2023-07-06T21:03:20.930Z DEBUG   hsmd: Shutting down

ERROR    root:helpers.py:170 Traceback (most recent call last):
  File "/home/vincent/Github/lightning/external/lnprototest/tests/helpers.py", line 167, in run_runner
    runner.run(test)
  File "/home/vincent/Github/lightning/external/lnprototest/lnprototest/runner.py", line 99, in run
    all_done = sequence.action(self)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/home/vincent/Github/lightning/external/lnprototest/lnprototest/structure.py", line 55, in action
    all_done &= e.action(runner)
                ^^^^^^^^^^^^^^^^
  File "/home/vincent/Github/lightning/external/lnprototest/lnprototest/event.py", line 365, in action
    raise EventError(self, "{}: message was {}".format(err, msg.to_str()))
lnprototest.errors.EventError: `Expected msgtype-warning, got msgtype-error: message was error channel_id=a37362839b13f61cfe82d35bd397b1264c389b245847cfb6111b38892546dc77 data=4661696c656420746f20766572696679206e6f64655f7369676e61747572652e` on event [{"event": "ExpectMsg", "file": "test_bolt2-01-close_channel.py", "pos": "157"},]
============================================================================================================================================================== short test summary info ===============================================================================================================================================================
FAILED tests/test_bolt2-01-close_channel.py::test_close_channel_shutdown_msg_normal_case_receiver_side - AssertionError: `Expected msgtype-shutdown, got msgtype-error: message was error channel_id=a37362839b13f61cfe82d35bd397b1264c389b245847cfb6111b38892546dc77 data=4661696c656420746f20766572696679206e6f64655f7369676e61747572652e` on event [{"event": "ExpectMsg", "file": "test_bolt2-01-close_channel.py", "pos": "75"},]
FAILED tests/test_bolt2-01-close_channel.py::test_close_channel_shutdown_msg_wrong_script_pubkey_receiver_side - AssertionError: `Expected msgtype-warning, got msgtype-error: message was error channel_id=a37362839b13f61cfe82d35bd397b1264c389b245847cfb6111b38892546dc77 data=4661696c656420746f20766572696679206e6f64655f7369676e61747572652e` on event [{"event": "ExpectMsg", "file": "test_bolt2-01-close_channel.py", "pos": "157"},]

```

Changelog-Fixes: channeld: Verify the signature sent in announcement_signatures by the counterparty
Reported-by: lnprototest
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
2023-07-26 06:57:15 +09:30
Rusty Russell
b6f2eb949a channeld: don't close connection on invalid badonion code (LND bug?)
```
channeld WARNING: Bad update_fail_malformed_htlc failure code 4103
```

Warren Togami reports this happening with Bitrefill on every reconnect,
so it's clearly something LND does.

(4103 is TEMPORARY_CHANNEL_FAILURE, which does not belong in update_fail_malformed_htlc).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: Protocol: We allow update_fail_malformed_htlc with invalid error codes (LND?)
2023-07-24 13:58:32 +09:30
niftynei
0cd7fe5089 spec: update to latest dual-funding wire changes
the witnesses are maddeningly weird now (you concat everything together)

we also changed some things to be s64's (it's a teeny tiny change)
2023-07-13 19:34:47 +09:30
Greg Sanders
4b70736d13 Support p2tr deposit addresses
Changelog-Added: JSON-RPC: newaddr: p2tr option to create taproot addresses.
Changelog-Changed: Wallet: we now use taproot change addresses.
2023-07-11 11:41:15 +09:30
Rusty Russell
af6d7c0779 global: thread zero fee option everywhere.
In most cases, it's the same as option_anchor_outputs, but for
fees it's different.  This transformation is the simplest:
pass it as a pair, and test it explicitly.

In future we could rationalize some paths, but this was nice
and mechanical.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-06-29 11:28:47 -04:00
Rusty Russell
ccf084156d channeld: use explicit --experimental-upgrade flag, not #ifdef EXPERIMENTAL_FEATURES
And no longer insist on opt_quiesce.

Changelog-EXPERIMENTAL: Config: `--experimental-upgrade-protocol` enables simple channel upgrades.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-05-23 09:34:08 +09:30
Rusty Russell
6c23349c72 channeld: allow stfu based on peer features, not EXPERIMENTAL_FEATURES.
Changelog-EXPERIMENTAL: Config: `--experimental-quiesce` enables queiescence, for testing.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-05-23 09:34:08 +09:30
Rusty Russell
e51460be28 wire: remove our own blinded payments patch since it's in spec.
Since this was merged, `make extract-peer-csv` was broken!

But the field names changed:
1. `tlv_update_add_tlvs` -> `tlv_update_add_htlc_tlvs`
2. `blinding` -> `blinding_point`.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-05-23 09:34:08 +09:30
Rusty Russell
2fb942d21c bitcoin: rename confusing functions.
1. anchor_to_remote_redeem => bitcoin_wscript_to_remote_anchored,
   which matches other witness script producing functions and makes
   it clear that it's a to_remote variant.
2. is_anchor_witness_script => is_to_remote_anchored_witness_script
   makes it clear that it's about a to_remote output (as altered
   when anchors are enabled) not an anchor output!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-10 17:26:47 +09:30
Rusty Russell
faae44713b lightningd: clarify uses of dynamic (mempool) feerate floor, and static.
We have the FEERATE_FLOOR constant if you don't care, but usually you want
to use the current bitcoind lower limit, so call get_feerate_floor()
(which is currently the same, but coming!).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-10 07:31:12 +09:30
Rusty Russell
458a85042b Makefile: update to BOLT 20066dc2aba906f37f3be5a810ae67040f265377
"BOLT 03: fix static-remote same amt and pre-image test vector"

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-07 21:22:56 +09:30
Rusty Russell
538854fdce bitcoin: add tx_feerate() to reverse-calculate feerate a tx paid.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-06 09:01:48 +09:30
Rusty Russell
b6a3f93b75 channeld: don't asort(NULL).
It's defined to be nonull:

```
channeld/channeld.c:2381:2: runtime error: null pointer passed as argument 1, which is declared to never be null
/usr/include/stdlib.h:856:3: note: nonnull attribute specified here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior channeld/channeld.c:2381:2 in 
```

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-04-05 06:12:24 +09:30
Rusty Russell
03c153ac0b channeld: don't spin trying to send commitment while waiting.
We would sleep for 10msec (default) and try again, spamming the logs
every second.  But we're waiting for revoke_and_ack, and that handler
already sets off the timer, so there's no need to spin at all!

Fixes: #6077
Changelog-Fixed: `channeld`: no longer spin and spam logs when waiting for revoke_and_ack.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-03-22 10:30:48 +10:30
adi2011
5f481aaf96 wire: Add patch file for peer storage bkp
Add msg type peer_storage and your_peer_storage
2023-02-08 08:37:59 -06:00
Greg Sanders
b67fde8106 Fix 'extreme cases' logging of many commit timer failures 2023-02-07 15:08:13 -06:00
Michael Schmoock
ad249607d6 dual-fund: update extracted CSVs to latest bolt draft
Changelog-None
2023-02-04 15:31:16 +10:30
Matt Whitlock
acfb63e4bf channeld: remove dead HTLCs from htable and free them (eventually)
The channel->htlcs map was exhibiting unbounded growth, as elements were
never removed from it. This was causing lightning_channeld processes to
consume ever-increasing amounts of memory, and iterating over the map
was causing ever-increasing CPU utilization. There were FIXME comments
suggesting that the intention was to remove HTLCs from the map upon
their deaths. This commit implements that intention.

Changelog-Fixed: channeld no longer retains dead HTLCs in memory.
2023-01-20 14:04:08 -06:00
Rusty Russell
5dfcd15782 all: no longer need to call htable_clear to free htable contents.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-01-12 11:44:10 +10:30
Rusty Russell
8eee5dd7fd channeld, lightningd: allow blinded payments with !EXPERIMENTAL_FEATURES.
Gate it (where necessary) by the route-blinding feature bit.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2022-10-26 11:29:06 +10:30
Rusty Russell
85baca56c6 channeld: don't calculate blinding shared secret, let lightningd do it.
It's a premature optimization, and it make modifications more complex.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2022-10-26 11:29:06 +10:30
Rusty Russell
22c8cfc374 channeld: remove onion objects.
We don't actually process onion messages here any more (they moved to
connectd), but the flag and object files were still linked.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2022-09-29 16:10:57 +09:30
Rusty Russell
41ef85318d onionmessages: remove obsolete onion message parsing.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2022-09-29 16:10:57 +09:30
Matt Whitlock
6324980484 channeld: do not enforce max_accepted_htlcs on LOCAL in channel reinit
If a channel goes offline while the count of outstanding outgoing HTLCs
exceeds the limit that we enforce against the peer, then the channel
could never be brought online again because `add_htlc` called by
`channel_force_htlcs` in `channeld/full_channel.c` would return
`CHANNEL_ERR_TOO_MANY_HTLCS`. The protocol specification actually does
allow us to exceed the limits that we are enforcing against the peer;
we are only prohibited from exceeding the limits that the peer is
enforcing against us. `add_htlc` takes an `enforce_aggregate_limits`
parameter that appears to have been intended for `channel_force_htlcs`
to exempt the local node from obeying the limits that it is enforcing
against the peer, but this parameter was only being respected for the
total HTLC value-in-flight check but not for the HTLC count check. This
commit respects the parameter for the HTLC count check as well and
resolves the problem of "Could not restore HTLCs".

Fixes: #5636
Changelog-Fixed: channeld: Channel reinitialization no longer fails when the number of outstanding outgoing HTLCs exceeds `max_accepted_htlcs`.
2022-09-28 14:00:20 +02:00
Rusty Russell
b698a5a5ef channeld: send error, not warning, if peer has old commitment number.
This is the minimal change to meet the desired outcome of https://github.com/lightning/bolts/issues/934
which wants to give obsolete-db nodes a chance to fix things up, before we
close the channel.

We need to dance around a bit here, since we *will* close the channel if
we receive an ERROR, so we suppress that.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2022-09-26 11:36:09 +02:00
Rusty Russell
bfc21cbb55 gossipd: set no_forward bit on channel_update for private channels.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Added: Protocol: We now set the `dont_forward` bit on private channel_update's message_flags (as per latest BOLTs).
2022-09-24 15:22:27 +09:30
Rusty Russell
701dd3dcef memleak: remove exclusions from memleak_start()
Add memleak_ignore_children() so callers can do exclusions themselves.

Having two exclusions was always such a hack!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2022-09-19 11:34:42 +09:30
Rusty Russell
3380f559f9 memleak: simplify API.
Mainly renaming.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2022-09-19 11:34:42 +09:30
Rusty Russell
7fa1364645 build: allow DEVELOPER builds with -Og and gcc 9.4.0
Removes some warnings.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2022-09-19 10:18:55 +09:30
Dustin Dettmer
cc206e1f0e connectd+: Flake/race fix for new channels
1) dualopen has fd to connectd
2) channeld needs to take over
3) dualopen passes fd that leads to a connectd over for channeld to use
4) lightningd must receive the fd transfer request and process
5) dualopen shuts down and closes everything it owns

4 & 5 end up in a race. If 5 happens before 4, channeld ends up with an invalid fd for connectd — leaving it in a position to not receive messages.

Lingering for a second makes 4 win the race. Since the daemon is closing anyway, waiting for a second should be alright.

Changelog-Fixed: Fixed a condition for newly created channels that could trigger a need for reconnect.
2022-09-18 21:18:32 +09:30
Rusty Russell
3cc6d0ec2c doc: upgrade to BOLTs 341ec844f13c0c0abc4fe849059fbb98173f9766
This is a slightly looser behavior, so no change needed.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2022-09-12 09:34:52 +09:30