840 Commits

Author SHA1 Message Date
Rusty Russell
f284489c96 common: don't abort() if wally_psbt_output_taproot_keypath_add() fails.
It fails on duplicates.  It would ideally succeed, but bug reported:

	https://github.com/ElementsProject/libwally-core/issues/509

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Fixed: JSON-RPC: `signpsbt` no longer crashes if asked to sign an already-signed PSBT with taproot paths.
2025-11-19 07:23:39 +10:30
Rusty Russell
35f65c5d91 common: add amount_msat_deduct / amount_msat_deduct_sub.
I added amount_msat_accumulate for the "a+=b" case, but I was struggling
with a name for the subtractive equivalent.  After some prompting, ChatGPT
suggested deduct.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-11-17 10:56:18 +10:30
Rusty Russell
f8fd97fb5d global: replace randombytes_buf() with randbytes() wrapper.
This allows us to override it for deterministic results.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-11-13 21:21:29 +10:30
Rusty Russell
2086699b70 common: add randbytes() wrapper to override cryptographic entropy: $CLN_DEV_ENTROPY_SEED
Only in developer mode, ofc.

Notes:
1. We have to move the initialization before the lightningd main trace_start,
   since that uses pseudorand().
2. To make the results stable, we need to use per-caller values to randbytes().
   Otherwise external timing changes the call order.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-11-13 21:21:29 +10:30
Dusty Daemon
a45189cc70 splice: Update to current spec
Updating splice related reestablish code to
https://github.com/lightning/bolts/pull/1289
and
https://github.com/lightning/bolts/pull/1160

Changelog-Changed: Breaking change -- if you have splicing enabled on a channel both nodes must upgrade in unison due to updating `channel_reestablish` for to new splice specifications
2025-11-13 15:17:19 +10:30
Dusty Daemon
4d15581401 splice: Add Bolt references and conform to them
Adding Bolt references around `commitment_signed` logic and conforming to them.

This allows us to remove the `await_commitment_succcess` logic which was never elegant anyway, nice!

While we’re there we remove a parameter from `handle_peer_commit_sig_batch` that shouldn’t have been there anyway.

Changelog-Changed: Adding stricter conformance to Bolt spec for splice commitments.
2025-11-13 10:56:54 +10:30
Dusty Daemon
4525243b88 splice: Clean up some spammy debug messages 2025-11-13 10:56:54 +10:30
Rusty Russell
815ac7e309 fuzz: fix compilation.
Header changes in master broke this upon rebase :(

```
In file included from ./channeld/full_channel.h:5,
                 from tests/fuzz/fuzz-full_channel.c:7:
./channeld/channeld_htlc.h:13:28: error: field ‘amount’ has incomplete type
   13 |         struct amount_msat amount;
      |                            ^~~~~~
./channeld/channeld_htlc.h:17:23: error: field ‘rhash’ has incomplete type
   17 |         struct sha256 rhash;
      |                       ^~~~~
```

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-11-10 17:09:08 +10:30
Rusty Russell
75616f6b77 common: add new_htable() macro to allocate, initialize and setup memleak coverage for any typed hash table.
You can now simply add per-tal-object helpers for memleak, but our older pattern required
calling memleak functions explicitly during memleak handling.  Hash tables in particular need
to be dynamically allocated (we override the allocators using htable_set_allocator and assume
this), so it makes sense to have a helper macro that does all three.

This eliminates a huge amount of code.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-10-24 11:30:17 +10:30
Rusty Russell
6e5cb299dd global: remove unnecessary includes from C files.
Basically, `devtools/reduce-includes.sh */*.c`.

Build time from make clean (RUST=0) (includes building external libs):

Before:
	real    0m38.944000-40.416000(40.1131+/-0.4)s
	user    3m6.790000-17.159000(15.0571+/-2.8)s
	sys     0m35.304000-37.336000(36.8942+/-0.57)s
After:
	real    0m37.872000-39.974000(39.5466+/-0.59)s
	user    3m1.211000-14.968000(12.4556+/-3.9)s
	sys     0m35.008000-36.830000(36.4143+/-0.5)s

Build time after touch config.vars (RUST=0):

Before:
	real    0m19.831000-21.862000(21.5528+/-0.58)s
	user    2m15.361000-30.731000(28.4798+/-4.4)s
	sys     0m21.056000-22.339000(22.0346+/-0.35)s

After:
	real    0m18.384000-21.307000(20.8605+/-0.92)s
	user    2m5.585000-26.843000(23.6017+/-6.7)s
	sys     0m19.650000-22.003000(21.4943+/-0.69)s

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-10-23 06:44:04 +10:30
Rusty Russell
f6a4e79420 global: remove unnecessary includes from headers.
Each header should only include the other headers it needs to compile;
`devtools/reduce-includes.sh */*.h` does this.  The C files then need
additional includes if they don't compile.

And remove the entirely useless wire/onion_wire.h, which only serves to include wire/onion_wiregen.h.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-10-23 06:44:04 +10:30
Rusty Russell
e120f87083 Makefile: create a library containing common, wire and bitcoin objects.
This means we don't have to manually choose what to link against,
which is much of the complexity of our Makefiles: the compiler will
automatically use any object files it needs to link.

We already do this for ccan as libccan.a, now we have libcommon.a.

We don't link against it for *everything*, as some tests require their own
versions.

Notes:
1. I get rid of the weird plugins/test/Makefile2 (accidental commit?)
2. Many tests change due to update-mocks.
3. In some places I added the missing dependency on the Makefile itself, though most are in the next
   patch.

Before:
	Total program size:     221366528
	Total tests size:       364243856

After:
	Total program size:     190733656
	Total tests size:       337880888

Build time from make clean (RUST=0) (includes building external libs):

Before:
	real    0m38.227000-44.245000(41.8222+/-1.6)s
	user    3m2.105000-33.696000(23.1442+/-8.4)s
	sys     0m35.054000-42.269000(39.7231+/-2)s
After:
	real    0m38.944000-40.416000(40.1131+/-0.4)s
	user    3m6.790000-17.159000(15.0571+/-2.8)s
	sys     0m35.304000-37.336000(36.8942+/-0.57)s

Build time after touch config.vars (RUST=0):

Before:
	real    0m18.928000-22.776000(21.5084+/-1.1)s
	user    2m8.613000-36.567000(27.7281+/-7.7)s
	sys     0m20.458000-23.436000(22.3963+/-0.77)s

After:
	real    0m19.831000-21.862000(21.5528+/-0.58)s
	user    2m15.361000-30.731000(28.4798+/-4.4)s
	sys     0m21.056000-22.339000(22.0346+/-0.35)s

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>

rusty@rusty-Framework:~/devel/cvs/lightni
2025-10-23 06:44:04 +10:30
Lagrang3
7e5cf41b4e htlc_wire: fix crash when adding an HTLC
In line channeld/channeld_wiregen.c:832 `*added+i` is not a tal object hence
the instruction in common/htlc_wire.c:200 `tal_arr(ctx, struct tlv_field, 0);` crashes CLN.
This is fixed by stating that added_htlc is a a varsize_type.

Logs:

2025-08-16T02:25:28.640Z **BROKEN** lightningd: FATAL SIGNAL 6 (version v25.05-200-g79b959b)V
...
2025-08-16T02:25:28.640Z **BROKEN** lightningd: backtrace: ccan/ccan/tal/tal.c:95 (call_error) 0x54f6bc
2025-08-16T02:25:28.640Z **BROKEN** lightningd: backtrace: ccan/ccan/tal/tal.c:169 (check_bounds) 0x54f75a
2025-08-16T02:25:28.640Z **BROKEN** lightningd: backtrace: ccan/ccan/tal/tal.c:178 (to_tal_hdr) 0x54f782
2025-08-16T02:25:28.640Z **BROKEN** lightningd: backtrace: ccan/ccan/tal/tal.c:193 (to_tal_hdr_or_null) 0x54f7c7
2025-08-16T02:25:28.640Z **BROKEN** lightningd: backtrace: ccan/ccan/tal/tal.c:471 (tal_alloc_) 0x54ffe4
2025-08-16T02:25:28.640Z **BROKEN** lightningd: backtrace: ccan/ccan/tal/tal.c:517 (tal_alloc_arr_) 0x5500c4
2025-08-16T02:25:28.640Z **BROKEN** lightningd: backtrace: common/htlc_wire.c:200 (fromwire_len_and_tlvstream) 0x48d63d
2025-08-16T02:25:28.640Z **BROKEN** lightningd: backtrace: common/htlc_wire.c:234 (fromwire_added_htlc) 0x48dd23
2025-08-16T02:25:28.640Z **BROKEN** lightningd: backtrace: channeld/channeld_wiregen.c:832 (fromwire_channeld_got_commitsig) 0x4c61fa
2025-08-16T02:25:28.640Z **BROKEN** lightningd: backtrace: lightningd/peer_htlcs.c:2377 (peer_got_commitsig) 0x4549cb
2025-08-16T02:25:28.640Z **BROKEN** lightningd: backtrace: lightningd/channel_control.c:1552 (channel_msg) 0x4140fe
2025-08-16T02:25:28.640Z **BROKEN** lightningd: backtrace: lightningd/subd.c:560 (sd_msg_read) 0x461513
2025-08-16T02:25:28.640Z **BROKEN** lightningd: backtrace: ccan/ccan/io/io.c:60 (next_plan) 0x544885
2025-08-16T02:25:28.640Z **BROKEN** lightningd: backtrace: ccan/ccan/io/io.c:422 (do_plan) 0x544cea
2025-08-16T02:25:28.640Z **BROKEN** lightningd: backtrace: ccan/ccan/io/io.c:439 (io_ready) 0x544d9d
2025-08-16T02:25:28.640Z **BROKEN** lightningd: backtrace: ccan/ccan/io/poll.c:455 (io_loop) 0x54665d
2025-08-16T02:25:28.640Z **BROKEN** lightningd: backtrace: lightningd/io_loop_with_timers.c:22 (io_loop_with_timers) 0x42d220
2025-08-16T02:25:28.640Z **BROKEN** lightningd: backtrace: lightningd/lightningd.c:1487 (main) 0x43280f

gdb inspection:
830             *added = num_added ? tal_arr(ctx, struct added_htlc, num_added) : NULL;
831             for (size_t i = 0; i < num_added; i++)
832                     fromwire_added_htlc(&cursor, &plen, *added + i);
(gdb) p i
$3 = 1

Changelog-None: crash introduced this release.
Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
[ Added test, removed Changelog --RR ]
2025-08-27 14:14:31 +09:30
Peter Neuroth
ef52de88aa channeld: Add extra_tlvs to incomming wire msg
This appends the extra_tlvs to the internal channeld_offer_htlc wire
msg. We also recombine the extra_tlvs with the blinded path key for
forwarding htlcs.

Signed-off-by: Peter Neuroth <pet.v.ne@gmail.com>
2025-08-14 18:57:05 +09:30
Peter Neuroth
631a1d950f channeld: Add extra_tlvs to wire htlcs
This appends the extra_tlvs to the internal wire htlcs "added" and
"existing" for the extra tlvs to be handed to lightningd.

Signed-off-by: Peter Neuroth <pet.v.ne@gmail.com>
2025-08-14 18:57:05 +09:30
Peter Neuroth
f4cc633c6f channeld: add extra_tlvs from update_add_htlc msg
We currently only consider known tlv types in the internal
representation of a htlc. This commit adds the remaining unknown tlv
fields to the htlc as well. This is in prepareation to forward these to
the htlc_accepted_hook.

Signed-off-by: Peter Neuroth <pet.v.ne@gmail.com>
2025-08-14 18:57:05 +09:30
Dusty Daemon
052f36cf2e connectd: Implement sending of start_batch
Implement the sending of `start_batch` and `protocol_batch_element` from `channeld` to `connectd`.

Each real peer wire message is prefixed with `protocol_batch_element` so connectd can know the size of the message that were batched together.

`connectd` intercepts `protocol_batch_element` messages and eats them (doesn’t forward them to peer) to get individual messages out of the batch.

It needs this to be able to encrypt them individiaully. Afterwards it recombines the now encrypted messages into a single message to send over the wire to the peer.

`channeld` remains responsible for making `start_batch` the first message of the message bundle.
2025-08-14 16:40:04 +09:30
Dusty Daemon
3841737d5c channeld: Cleaner error messages
Since handling commit sig batches is coming for multiple locations now, add more explicity error handling so log messages are more useful.
2025-08-14 16:40:04 +09:30
Dusty Daemon
7eb2add23c channeld: Implement receiving of start_batch
Since `batch_size` has moved into this new message, we can’t ignore it anymore and have to process it
2025-08-14 16:40:04 +09:30
Dusty Daemon
e755be0c4c splice: Remove batch_size from commitment_signed
The new spec sends `batch_size` in `start_batch` and removes it from `commitment_signed` so we need to stop processing it in `commitment_signed`.

Since the tlv is now reduced to one element and that automagically turns it into a direct use TLV so we have to update the code everywhere it is referenced.
2025-08-14 16:40:04 +09:30
Dusty Daemon
07f4bc39b1 splice: Add start_batch and an internal wire type
We add `start_batch` to match t-bast’s splicing spec and we add a new internal wire type `WIRE_PROTOCOL_BATCH_ELEMENT` using the type number 0

Changelog-Added: support for `start_batch`
2025-08-14 16:40:04 +09:30
Dusty Daemon
c602a8a54c channeld: Move counting code to its own function
Counting remote inputs needing sigs can be in its own method
2025-08-14 04:12:17 +09:30
Dusty Daemon
122dae1076 splice: Even if the psbt’s “match” the sig data may vary
Since the signature data may vary, we must copy the new psbt into splicing->current_psbt.

This never occured during normal operation, but when doing a cross splice there may be vital signature(s) in the psbt that came from another splice that get dropped without this step.
2025-08-14 04:12:17 +09:30
Dusty Daemon
8783f3c035 splice: Add details to log message
Add more information to funding issue failure messages when splicing
2025-08-14 04:12:17 +09:30
Dusty Daemon
e3e0813552 splice: Sign shared output early
When doing a multi channel splice, we need to break the deadlock by signing the shared output early (even though it is not sent to the peer until later).
2025-08-14 04:12:17 +09:30
Dusty Daemon
dd1e183b8a splice: Change abort rules to sent sigs
Previous behavior was to fail on abort when we have signatures in the inflight — change this behavior to fail on abort if we have sent our peer our signatures.
2025-08-14 04:12:17 +09:30
Dusty Daemon
d84d0f70d5 splice: Track if splice sigs are sent
Use the new i_sent_sigs field to track if we’ve sent our peer our user sigs.
2025-08-14 04:12:17 +09:30
Dusty Daemon
a8ff8ce717 splice: Add field to DB to track if we’ve sent sigs
We used to use a check on the active psbt to see if our splice signature was in it — but now we need to generate the signature early.

So we have to add a field tracking if we’ve sent it and add it to the database, wire protocols, and inflight objects.
2025-08-14 04:12:17 +09:30
Dusty Daemon
87ae35597d splice: Add details to log message
Make the failure reason more clear by adding more information to the signature failure message.
2025-08-14 04:12:17 +09:30
Rusty Russell
d86b88d409 channeld: remove experimental-upgrade-protocol.
It was already disabled by Dusty due to a number conflict with splicing, and
the proposal probably needs updating to use quiescence now that is merged.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-EXPERIMENTAL: The non-functional `experimental-upgrade-protocol` config option.
2025-07-07 11:18:37 +09:30
Dusty Daemon
10708e3168 splice: Adjust batch_size TLV to match Eclair’s
Eclair only sets `batch_size` and `funding_txid` when its a batch larger than 1.

Adjust how we send and what we expect to receive to match this.

Changelog-None
2025-05-15 16:58:00 -05:00
Rusty Russell
e11c2f6c04 channeld: allow announcement_signatures during splice.
It can actually happen, at least in theory.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-05-15 16:40:33 +09:30
Dusty Daemon
9d773910c9 splice: Eclair Interop — reestablish corner case
Handle a specific corner case where Eclair expects us to re-send splice_locked.

Changelog-None
2025-05-15 12:47:12 +09:30
Dusty Daemon
b76c0c1d37 splice: Test fix for test crash splice
Fix a typo where the commit sig message ordering was not handled correctly for the first element.

We need to use msg_batch[0] to get the first post-sorted result instead of the original msg.

Changelog-None
2025-05-15 10:47:30 +09:30
Dusty Daemon
8d247fa9e6 splice: Add more explicit error messages
To make failure casee more clear
2025-05-13 14:52:15 +09:30
Dusty Daemon
44e338daa0 splice: Message sorting should be using txid parsing
Update from old channel_id non-spec version
2025-05-13 14:52:15 +09:30
Dusty Daemon
3f626371c3 splice: Verbose logging for Eclair interop 2025-05-13 14:52:15 +09:30
Dusty Daemon
4466286167 splice: Changing encoding of TLV funding_txid
Using the wrong encoding flips the bytes. We have to use sha256 as the spec says to prevent them from flipping.
2025-05-13 14:52:15 +09:30
Dusty Daemon
9a3c3c0a3e splice: Improve log message for handle_peer_commit_sig 2025-05-13 14:52:15 +09:30
Dusty Daemon
7bf544db02 splice: Add support for tx_abort during RBF
We do this by adding a specific txid the tx_abort applies to and performing checks based on that.

If the txid is NULL or unrecognized than no inflights are dropped from DB. If we recognize it than we do the check to see if we signed it and, if not, we let lightningd remove it from DB.
2025-05-13 14:52:15 +09:30
Dusty Daemon
0f109d0155 splice: Add verbose log messages for new reestablish TLV 2025-05-13 14:52:15 +09:30
Dusty Daemon
357b4b503d splice: Enable user splice RBF
Allow user’s to RBF existing splices. For now this is done by simple executing an additional splice command, in the future this will can also be done with dedicated RPCs.

Changelog-Added: Enabled the ability to RBF splices
2025-05-13 14:52:15 +09:30
Dusty Daemon
398bd1cb8d splice: Enable the receiving of splice RBF
Turn on the receiving of splice RBF attempts.
2025-05-13 14:52:15 +09:30
Dusty Daemon
b231db3f65 splice: Only send or recv commit sig if needed
Be more conservative about when we request of send commit sig for splice to match the Eclair behavior.
2025-05-13 14:52:15 +09:30
Dusty Daemon
c02f89baa8 splice: Decrement next_commitment_number for Eclair
Eclair requires `next_commitment_number` to be decremented to resend the individual splice commitment_signed message.
2025-05-13 14:52:15 +09:30
Dusty Daemon
e928817fa7 splice: Update to Eclair style of reestablish
Update to use Eclair’s spec’d version of reestablish.

Changelog-None
2025-05-13 14:52:15 +09:30
Dusty Daemon
dd962225ef splice: Allow commit_sig batch in any order
Other implementations are sending commit_sig batches in different orders. We add support for them being in any order by ordering the batch of messages after receiving them.

Changelog-Changed: Increase interop compatability by loosening requirement that commitment signed messages be received in a particular order and sorting them internally.
2025-05-13 14:52:15 +09:30
Dusty Daemon
c9c56e340d PSBT: Clean up PSBT chunk allocations
Cleaning up the memory hierarchy of PSBT usage in splicing and `psbt_finalize_input`
2025-05-13 14:52:15 +09:30
Dusty Daemon
1621b67d34 splice: Prevent user from signing an unfinal splice
An extra check to ensure the user doesn’t try to sign a splice that wasn’t finalized.
2025-05-13 14:52:15 +09:30
Dusty Daemon
5e5ed774fc PSBT: Add audi_psbt routine
A routine that audit’s and asserts PSBT memory to confirm it has a sane memory allocation hierarchy.

Changelog-None
2025-05-13 14:52:15 +09:30