Skip to content

Add Shared Input support in interactive TX construction #3842

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 12 commits into
base: main
Choose a base branch
from

Conversation

optout21
Copy link
Contributor

@optout21 optout21 commented Jun 10, 2025

In interactive TX construction, add support for shared input:

  • if we expect a shared input, make sure that the remote node provides it
  • if we provide a shared input, make sure that it's accounted appropriately

Additionally, the prevtx field of the TxAddInput message is changed to Optional, as it should not be set for the shared input (it cannot, as the full funding transaction is not stored on the acceptor side) (spec discussion: lightning/bolts#1160 (comment))

To be used by splicing, see #3736 .

Note: this PR does not include splicing negotiation.

@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Jun 10, 2025

👋 Thanks for assigning @wpaulino 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.

@optout21 optout21 marked this pull request as ready for review June 11, 2025 19:03
@optout21 optout21 requested review from jkczyz, wpaulino and dunxen June 11, 2025 19:03
/// malleable.
pub prevtx: TransactionU16LenLimited,
/// malleable. Omitted for shared input.
pub prevtx: Option<TransactionU16LenLimited>,
Copy link
Contributor

Choose a reason for hiding this comment

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

We could make this an enum along with shared_input_txid, but I'm not sure what's a good name to call it

Copy link
Contributor

Choose a reason for hiding this comment

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

Probably not a good suggestion, but how about just TxInputOrigin with Txid and PrevTx variants?

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 think it's a good idea to keep the message struct field names close to the names in the spec.

Copy link
Contributor

Choose a reason for hiding this comment

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

Discussed this offline with @wpaulino. While ideally we could use an enum to represent the correct semantics, in practice we don't want to fail parsing if they are incorrect. Otherwise, we'd disconnect rather than simply abandoning the negotiation.

@ldk-reviews-bot
Copy link

🔔 1st Reminder

Hey @dunxen @jkczyz! 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 @dunxen @jkczyz! 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.

@ldk-reviews-bot
Copy link

🔔 2nd Reminder

Hey @dunxen @jkczyz! 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

🔔 2nd Reminder

Hey @dunxen @jkczyz! 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.

@optout21
Copy link
Contributor Author

Great, can you push those changes here then and rework the shared funding input work to follow a similar approach?

I managed to do it: first I refactored the shared output support as discussed, then added shared input support in a similar style. Although this PR does not include spicing, I also checked the changes with splicing (shared input is used there).

@optout21 optout21 force-pushed the shared-input branch 5 times, most recently from 166e364 to b2e0da8 Compare June 16, 2025 13:47
total_input_satoshis = total_input_satoshis.saturating_add(shared_input);
if is_initiator {
our_funding_inputs_weight =
our_funding_inputs_weight.saturating_add(P2WSH_INPUT_WEIGHT_LOWER_BOUND);
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs to be the weight of the full witness (see FUNDING_TRANSACTION_WITNESS_WEIGHT) plus the base input 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.

That's included in P2WPKH_INPUT_WEIGHT_LOWER_BOUND, I think:

/// Lower bound for P2WPKH input weight
pub(crate) const P2WPKH_INPUT_WEIGHT_LOWER_BOUND: u64 =
	BASE_INPUT_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT + P2WPKH_WITNESS_WEIGHT;

// If there is a shared input, account for it,
// and for the initiator also consider the fee
if let Some(shared_input) = shared_input {
total_input_satoshis = total_input_satoshis.saturating_add(shared_input);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this supposed to be the full channel value of the shared input, or just the locally owned portion of it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The locally owned portion, as this calculation is for ensuring that the contribution is enough to cover the (locally owned) outputs and fees.

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 have some doubts about the possible rounding errors due to the msat->sat conversion.

@optout21 optout21 requested a review from wpaulino June 17, 2025 03:10
@optout21
Copy link
Contributor Author

@wpaulino thanks for the comments. I will process them (I've re-requested review accidentally, please ignore that).

@ldk-reviews-bot
Copy link

🔔 3rd Reminder

Hey @dunxen @jkczyz @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.

1 similar comment
@ldk-reviews-bot
Copy link

🔔 3rd Reminder

Hey @dunxen @jkczyz @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.

@ldk-reviews-bot
Copy link

🔔 1st Reminder

Hey @dunxen @jkczyz @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.

This simplifies tracking separately the expected and actual shared output.
In the initiator case, we can just provide the shared output separately,
instead of including it within other outputs, and marking which one is the output.
We can use the same field for the intended shared output in the initiator
case, and the expected one in the acceptor case.
Copy link

codecov bot commented Jun 19, 2025

Codecov Report

Attention: Patch coverage is 86.65710% with 93 lines in your changes missing coverage. Please review.

Project coverage is 90.92%. Comparing base (30ab411) to head (96236df).
Report is 58 commits behind head on main.

Files with missing lines Patch % Lines
lightning/src/ln/interactivetxs.rs 91.99% 41 Missing and 8 partials ⚠️
lightning/src/ln/msgs.rs 40.00% 34 Missing and 8 partials ⚠️
lightning/src/ln/channel.rs 81.81% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3842      +/-   ##
==========================================
+ Coverage   89.95%   90.92%   +0.97%     
==========================================
  Files         164      164              
  Lines      131981   142768   +10787     
  Branches   131981   142768   +10787     
==========================================
+ Hits       118718   129812   +11094     
+ Misses      10586    10350     -236     
+ Partials     2677     2606      -71     

☔ 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.

@optout21
Copy link
Contributor Author

Review comments addressed. Keeping the fixup commits separately for now, but in the end I will just squash into a single commit.

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

Successfully merging this pull request may close these issues.

5 participants