Skip to content

Add splice-out support #3979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

jkczyz
Copy link
Contributor

@jkczyz jkczyz commented Jul 31, 2025

Splice-in support was added in #3736. This PR expands ChannelManager::splice_channel to support splice-out (i.e., removing funds from a channel). This is accomplished by adding a FundingTxContributions enum to cover both use cases.

Depends on #3736.

@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Jul 31, 2025

👋 Thanks for assigning @tankyleo as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@jkczyz jkczyz requested a review from wpaulino July 31, 2025 16:11
@jkczyz jkczyz marked this pull request as draft July 31, 2025 16:11
@jkczyz jkczyz self-assigned this Jul 31, 2025
Copy link

codecov bot commented Jul 31, 2025

Codecov Report

❌ Patch coverage is 74.86339% with 46 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.77%. Comparing base (6d47f0a) to head (8a596fb).
⚠️ Report is 79 commits behind head on main.

Files with missing lines Patch % Lines
lightning/src/ln/msgs.rs 29.41% 15 Missing and 9 partials ⚠️
lightning/src/ln/funding.rs 51.42% 16 Missing and 1 partial ⚠️
lightning/src/ln/channel.rs 92.72% 2 Missing and 2 partials ⚠️
lightning/src/ln/interactivetxs.rs 97.91% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3979      +/-   ##
==========================================
+ Coverage   88.74%   88.77%   +0.02%     
==========================================
  Files         173      176       +3     
  Lines      124899   127934    +3035     
  Branches   124899   127934    +3035     
==========================================
+ Hits       110841   113571    +2730     
- Misses      11631    11796     +165     
- Partials     2427     2567     +140     
Flag Coverage Δ
fuzzing 22.09% <2.38%> (?)
tests 88.60% <73.77%> (-0.14%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jkczyz jkczyz force-pushed the 2025-07-splice-out branch 3 times, most recently from 2d39059 to 381fba6 Compare August 1, 2025 00:23
@jkczyz jkczyz marked this pull request as ready for review August 1, 2025 00:23
@ldk-reviews-bot
Copy link

🔔 1st Reminder

Hey @wpaulino! This PR has been waiting for your review.
Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

@@ -6079,7 +6081,7 @@ pub enum FundingTxContributions {
InputsOnly {
/// The inputs used to meet the contributed amount. Any excess amount will be sent to a
/// change output.
inputs: Vec<(TxIn, Transaction)>,
inputs: Vec<(TxIn, Transaction, Weight)>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this a struct now so we can properly document each field. The weight here should be the witness_weight for the input to be spent. We may even be able to reuse bump_transaction::Utxo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may even be able to reuse bump_transaction::Utxo.

Hmm... that doesn't have a TxIn. We also need the full previous transaction in InteractiveTxConstructor to use as prevtx in tx_add_input.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would just need to track TxIn::sequence and prevtx separately if we choose to go with it

our_funding_contribution_satoshis,
);
// FIXME: Should we check value_to_self instead? Do HTLCs need to be accounted for?
// FIXME: Check that we can pay for the outputs from the channel value?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably don't even need our_funding_contribution for a splice out, we can just assume it from adding all the output values

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point. For paying for the outputs, seems we should actually use check_v2_funding_inputs_sufficient for both splice-in and splice-out and account for outputs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, actually that only looks at the input from the previous funding transaction for weight, not amount.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-wrote this to ensure fees for the funding outputs are accounted for. One thing of note is that our_funding_contribution for a splice-out needs to be adjusted by the fees to be paid (i.e., fees need to be subtracted from the negative our_funding_contribution to increase the amount being removed from the channel).

Also, a bit of an oversight, but we don't yet support accepting a splice-out. Technically, it could contain both inputs and outputs even if the net amount is negative. But I think we shouldn't care as long as we check everything is sane upon exchanging tx_complete.

@ldk-reviews-bot
Copy link

👋 The first review has been submitted!

Do you think this PR is ready for a second reviewer? If so, click here to assign a second reviewer.

@jkczyz jkczyz force-pushed the 2025-07-splice-out branch 2 times, most recently from 396948e to 59fbd7e Compare August 7, 2025 23:38
@jkczyz jkczyz requested a review from wpaulino August 8, 2025 13:42
@ldk-reviews-bot
Copy link

🔔 1st Reminder

Hey @wpaulino! This PR has been waiting for your review.
Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

@jkczyz jkczyz force-pushed the 2025-07-splice-out branch from fde2f61 to f12c993 Compare August 11, 2025 16:39
@jkczyz
Copy link
Contributor Author

jkczyz commented Aug 11, 2025

Rebased to resolve merge conflicts.

@jkczyz jkczyz force-pushed the 2025-07-splice-out branch from f12c993 to 806220e Compare August 11, 2025 22:22
@jkczyz
Copy link
Contributor Author

jkczyz commented Aug 11, 2025

Squashed as requested.

funding_feerate.to_sat_per_kwu() as u32,
));

if channel_balance > contribution_amount.unsigned_abs() + estimated_fee {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't enough, we need to make sure the balance is still above the reserve and has enough to pay for fees on the commitment transaction if they're the channel initiator

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, the reserve check will be at the call site as a separate check. I hadn't considered the fees for the commitment transaction. For the splice-in case, is it assumed it is sufficient because it was sufficient with the previous funding contributions?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the splice-in case, is it assumed it is sufficient because it was sufficient with the previous funding contributions?

Most likely yeah, but it's better to be safe than sorry. We already need to always check it for the counterparty and for our splice-outs, so might as well always do it for splice-ins.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving this for @tankyleo.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See other comments.

@jkczyz jkczyz force-pushed the 2025-07-splice-out branch from 806220e to 9c2f266 Compare August 13, 2025 23:58
@jkczyz jkczyz requested a review from wpaulino August 13, 2025 23:58
@jkczyz jkczyz force-pushed the 2025-07-splice-out branch from 9c2f266 to ef1aee7 Compare August 14, 2025 22:19
@jkczyz
Copy link
Contributor Author

jkczyz commented Aug 14, 2025

Squashed as requested.

Copy link
Contributor

@wpaulino wpaulino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also make sure a FundingTransactionReadyForSigning event gets pushed when doing splice outs

/// When funds are added to a channel.
SpliceIn {
/// The amount to contribute to the splice.
value: Amount,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another variant could be to consume the full input set, but we can revisit that later

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking we could have a constructor that took inputs and gave back a SpliceContribution::SpliceIn with amount. However, we'd need to provide information about the balance in order to compute that. So maybe best to use a dedicated variant as you said.

@wpaulino wpaulino requested a review from tankyleo August 14, 2025 23:24
@ldk-reviews-bot
Copy link

🔔 1st Reminder

Hey @wpaulino @tankyleo! This PR has been waiting for your review.
Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

1 similar comment
@ldk-reviews-bot
Copy link

🔔 1st Reminder

Hey @wpaulino @tankyleo! This PR has been waiting for your review.
Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

funding_feerate.to_sat_per_kwu() as u32,
));

if channel_balance > contribution_amount.unsigned_abs() + estimated_fee {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See other comments.

Copy link
Contributor Author

@jkczyz jkczyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed @tankyleo's comments, leaving some for #4011. Let me know if I missed something that should have been addressed here.

/// When funds are added to a channel.
SpliceIn {
/// The amount to contribute to the splice.
value: Amount,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking we could have a constructor that took inputs and gave back a SpliceContribution::SpliceIn with amount. However, we'd need to provide information about the balance in order to compute that. So maybe best to use a dedicated variant as you said.

@jkczyz jkczyz requested a review from tankyleo August 18, 2025 22:17
Comment on lines 5927 to 5929
let mut weight = (input_count as u64) * BASE_INPUT_WEIGHT;

// Witnesses
weight = weight.saturating_add(witness_weight.to_wu());
weight = weight.saturating_add(outputs.iter().map(|txout| txout.weight().to_wu()).sum());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems we are missing the weight of the empty script sig, ie EMPTY_SCRIPT_SIG_WEIGHT ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that only for inputs? Outputs added to the transaction will have a script_pubkey that needs to be satisfied when spent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right I mean that BASE_INPUT_WEIGHT on the very first line does not include EMPTY_SCRIPT_SIG_WEIGHT and then on the second line we add the Witness weight only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that parameter should be renamed similar to #3979 (comment). But it looks like we do need to add EMPTY_SCRIPT_SIG_WEIGHT at call sites when adding FUNDING_TRANSACTION_WITNESS_WEIGHT, which AFAICT doesn't include it. (cc @wpaulino)

Updated estimate_v2_funding_transaction_fee to do all the weight calculations so as to avoid this.

@jkczyz jkczyz force-pushed the 2025-07-splice-out branch from a39a294 to 8ad0b83 Compare August 19, 2025 23:39
@jkczyz jkczyz requested a review from tankyleo August 19, 2025 23:40
@ldk-reviews-bot
Copy link

🔔 2nd Reminder

Hey @wpaulino @tankyleo! This PR has been waiting for your review.
Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

Copy link
Contributor

@tankyleo tankyleo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See discussion comments :)

@jkczyz jkczyz force-pushed the 2025-07-splice-out branch 2 times, most recently from d4c2cd8 to 7a38617 Compare August 20, 2025 16:37
jkczyz added 4 commits August 20, 2025 14:37
Splice contributions should never exceed the total bitcoin supply. This
check prevents a potential overflow when converting the contribution
from sats to msats. The commit additionally begins to store the
contribution using SignedAmount.
…text

Once the counterparty supplies their funding contribution, there is no
longer a need to store it in FundingNegotiationContext as it will have
already been used to create a FundingScope.
TransactionU16LenLimited was used to limit Transaction serialization
size to u16::MAX. This was because messages can not be longer than
u16::MAX bytes when serialized for the transport layer. However, this
limit doesn't take into account other fields in a message containing a
Transaction, including the length of the transaction itself.

Remove TransactionU16LenLimited and instead check any user supplied
transactions in the context of the enclosing message (e.g. TxAddInput).
ChannelManager::splice_channel takes witness weights with the funding
inputs. Storing these in FundingNegotiationContext allows us to use them
when calculating the change output and include them in a common struct
used for initiating a splice-in.

In preparation for having ChannelManager::splice_channel take
FundingTxContributions, add a weight to the
FundingTxContributions::InputsOnly, which supports the splice-in use
case.
@jkczyz jkczyz force-pushed the 2025-07-splice-out branch from 7a38617 to 29c61bd Compare August 20, 2025 19:37
Copy link
Contributor Author

@jkczyz jkczyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed CI and squashed commits as requested.

@jkczyz jkczyz requested a review from tankyleo August 20, 2025 19:38
@jkczyz jkczyz force-pushed the 2025-07-splice-out branch from 29c61bd to f2aa7fe Compare August 20, 2025 21:57
@jkczyz
Copy link
Contributor Author

jkczyz commented Aug 20, 2025

Pushed a fix for CI.

jkczyz added 5 commits August 21, 2025 10:42
When splicing a channel, the previous funding output is spent and fees
for it are paid by the splice initiator. However, the witness weight was
not including EMPTY_SCRIPT_SIG_WEIGHT. Fix this and update the variable
name to make clear the weight needed is the input satisfaction.
The funding inputs used for splicing and v2 channel establishment are
passed as a tuple of txin, prevtx, and witness weight. Add a struct so
that the items included can be better documented.
ChannelManager::splice_channel takes individual parameters to support
splice-in. Change these to an enum such that it can be used for
splice-out as well.
Update SpliceContribution with a variant used to support splice-out
(i.e., removing funds from a channel). The TxOut values must not exceed
the users channel balance after accounting for fees and the reserve
requirement.
When a counterparty sends splice_init with a negative contribution, they
are requesting to remove funds from a channel. Remove conditions
guarding against this and check that they have enough channel balance to
cover the removed funds.
@jkczyz jkczyz force-pushed the 2025-07-splice-out branch from f2aa7fe to 8a596fb Compare August 21, 2025 16:05
@jkczyz
Copy link
Contributor Author

jkczyz commented Aug 21, 2025

Sorry, early CI failures were masking another one. Think it should be fixed in latest push. Also, pulled out the EMPTY_SCRIPT_SIG_WEIGHT into a separate commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

4 participants