Skip to content

[Splicing] Tx negotiation during splicing #3736

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

Conversation

optout21
Copy link
Contributor

@optout21 optout21 commented Apr 15, 2025

Implementation of transaction negotiation during splicing.
Builds on 3407 and 3443.

  • No new phase, Funded(FundedChannel) is used throughout splicing
  • Both FundedChannel and PendingV2Channel can act as a transaction constructor
  • PendingV2Channel logic is put behind a trait -- FundingTxConstructorV2
  • A RenegotiatingScope is used to store extra state during splicing
  • FundingChannel can act as a FundingTxConstructorV2, using the state from RenegotiatingScope (if present)
  • Since both FundedChannel and FundingTxConstructor has context(), context accessors are extracted into a common base trait, ChannelContextProvider (it is also shared by InitialRemoteCommitmentReceiver).

(Also relevant: #3444)

@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Apr 15, 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.

@ldk-reviews-bot
Copy link

🔔 1st Reminder

Hey @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

🔔 1st Reminder

Hey @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

🔔 2nd Reminder

Hey @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

🔔 2nd Reminder

Hey @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

🔔 3rd Reminder

Hey @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 @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

🔔 4th Reminder

Hey @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

🔔 4th Reminder

Hey @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

🔔 5th Reminder

Hey @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

🔔 5th Reminder

Hey @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

🔔 6th Reminder

Hey @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

🔔 6th Reminder

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

/// Data needed during splicing --
/// when the funding transaction is being renegotiated in a funded channel.
#[cfg(splicing)]
struct RefundingScope {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we introducing yet another structure as opposed to tracking all the fields here in PendingSplice?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

RefundingScope does not live through the whole lifetime of splicing. Not before splice_ack received, and not after tx_complete. PendingSplice has a longer lifetime. Also, the fields are belonging to each other. I could 'flatten' the structure, and just move the fields to PendingSplice, but I think it's clearer if they are in a struct, and can be set to None at once.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, I did remove the PendingSpliceInit sub-struct, and included the few fields in PendingSplice directly (See 0f8acd3)

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm still not convinced this is helpful, FundedChannel and RefundingScope are almost the same, why not just implement the trait directly on FundedChannel?

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 have clarified as follows:

  • RefundingScope does not implement the trait, but the FundedChannelRefundingWrapper does
  • The wrapper makes sense to be kept
  • RefundingScope could be dropped, and its 4 fields included directly in PendingSplice
  • FundedChannelRefundingWrapper could be returned by PendingV2Channel as well, and then the trait can be dropped, as only one struct would need 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.

RefundingScope has been dropped, its 4 fields are included directly in PendingSplice.

@@ -2414,6 +2414,7 @@ pub(super) trait FundingTxConstructorV2<SP: Deref>: ChannelContextProvider<SP> w
fn begin_interactive_funding_tx_construction<ES: Deref>(
&mut self, signer_provider: &SP, entropy_source: &ES, holder_node_id: PublicKey,
change_destination_opt: Option<ScriptBuf>,
_is_splice: bool, prev_funding_input: Option<(TxIn, TransactionU16LenLimited)>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need is_splice if prev_funding_input being set implies we are splicing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the prev. funding input is set only by the initiator, and this method is used on both side (initiator and acceptor).

Copy link
Contributor

Choose a reason for hiding this comment

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

The funding input still needs to be passed through to the interactive constructor though so we can make sure the counterparty adds it when we're not the initiator and we can check it's the same as we expect.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is, everything will go through it after begin_interactive_funding_tx_construction

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Clarified: the previous funding as an input is a shared input, and the splice-acceptor side also has to be aware of it, it has to check that it is being added by the initiator. So it has to provided as a special shared input, which is added during negotiation by the initiator, and checked by the acceptor.

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've tried to do it, but ran into a problem with the tests. Apparently on the acceptor side (v1 channel) the field funding.funding_transaction is never set.

Copy link
Contributor

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

Sorry about the late review. We were traveling to an off site last week. Just a high-level pass on the first four commits. Will need to take a closer look at the last one.

Copy link

codecov bot commented Apr 30, 2025

Codecov Report

Attention: Patch coverage is 56.30252% with 52 lines in your changes missing coverage. Please review.

Project coverage is 90.28%. Comparing base (8aae34e) to head (f1c280d).
Report is 15 commits behind head on main.

Files with missing lines Patch % Lines
lightning/src/ln/channel.rs 55.23% 46 Missing and 1 partial ⚠️
lightning/src/ln/channelmanager.rs 64.28% 4 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3736      +/-   ##
==========================================
+ Coverage   89.73%   90.28%   +0.55%     
==========================================
  Files         159      160       +1     
  Lines      128910   132032    +3122     
  Branches   128910   132032    +3122     
==========================================
+ Hits       115676   119207    +3531     
+ Misses      10536    10172     -364     
+ Partials     2698     2653      -45     

☔ 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 optout21 force-pushed the splice-dual-tx4 branch 3 times, most recently from 88d2e83 to 866368d Compare May 5, 2025 11:59
@optout21
Copy link
Contributor Author

optout21 commented May 6, 2025

Ready for a new round of review. I have addressed the comments, applied most of them. There is still one to-do (update channel reserve values), that I will do, but the rest is ready for review.
I did the changes in separate 'fix' commits.

@optout21
Copy link
Contributor Author

Ready for a new round of review. All pending and newly raised comments addressed.

Comment on lines 8493 to 8522
try_channel_entry!(self, peer_state, Err(ChannelError::Close((
err.into(),
ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
))), chan_entry)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is closing the channel here a spec requirement? Seems like we could just drop the message or send a warning.

Comment on lines 8454 to +8479
self.internal_tx_msg(&counterparty_node_id, msg.channel_id, |channel: &mut Channel<SP>| {
match channel.as_unfunded_v2_mut() {
Some(unfunded_channel) => {
Ok(unfunded_channel.tx_add_input(msg).into_msg_send_event(counterparty_node_id))
},
None => Err("tx_add_input"),
}
Ok(channel.tx_add_input(msg)?.into_msg_send_event(counterparty_node_id))
Copy link
Contributor

Choose a reason for hiding this comment

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

Both tx_add_input and internal_tx_msg have their own error strings that when combined don't make sense. We should probably drop the one from internal_tx_msg and have tx_add_input, etc. return a ChannelError.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, I've changed, also for tx_complete.
I did remove a Channel::Close from internal_tx_complete(), and I think that makes sense, since we don't want to close on a funded splicing channel in case of a tx_complete error.

Comment on lines 9534 to 9562
// TODO try_channel_entry()
let splice_ack_msg = chan_entry.get_mut().splice_init(msg, our_funding_contribution, &self.signer_provider,
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason this is a TODO?

} else {
return Err(MsgHandleErrInternal::send_err_msg_no_close("Channel is not funded, cannot splice".to_owned(), msg.channel_id));
// Handle inside channel
let tx_msg_opt = chan_entry.get_mut().splice_ack(msg, &self.signer_provider, &self.entropy_source, &self.get_our_node_id(), &self.logger)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we not using try_channel_entry anymore?

@@ -8649,9 +8980,20 @@ impl<SP: Deref> FundedChannel<SP> where
"Insufficient inputs for splicing; channel ID {}, err {}",
self.context.channel_id(), err,
)})?;
// Convert inputs
let mut funding_inputs = Vec::new();
for (tx_in, tx, _w) in our_funding_inputs.into_iter() {
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 just make our_funding_inputs an owned value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, though an extra clone() is added in channelmanager.rs, due to optionally_notify closure.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm... that's unfortunate. It seems to stem form the fact that PersistenceNotifierGuard has an FnMut rather than an FnOnce. Might be able to fix that but probably better to save for a separate PR.

our_funding_inputs: Vec::new(), // inputs go directly to [`FundingNegotiationContext`] above
awaiting_splice_ack: false, // we don't need any additional message for the handshake
refunding_scope,
});
// TODO(splicing): Store msg.funding_pubkey
Copy link
Contributor

Choose a reason for hiding this comment

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

Can be removed now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, this is about storing the funding pubkey of the other party, as it may change. It's done nowhere as of yet.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, we just have to overwrite it in the new ChannelTransactionParameters

/// Data needed during splicing --
/// when the funding transaction is being renegotiated in a funded channel.
#[cfg(splicing)]
struct RefundingScope {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm still not convinced this is helpful, FundedChannel and RefundingScope are almost the same, why not just implement the trait directly on FundedChannel?

@optout21 optout21 force-pushed the splice-dual-tx4 branch 2 times, most recently from 0333069 to 54ddcbe Compare May 15, 2025 23:14
@optout21
Copy link
Contributor Author

I've done some of the bigger changes, but there are still several outstanding.

@@ -5293,7 +5293,7 @@ fn check_v2_funding_inputs_sufficient(
}

/// Context for dual-funded channels.
Copy link
Contributor

Choose a reason for hiding this comment

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

Please update docs.


// TODO(splicing): Continue with commitment flow, new tx confirmation

/*
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of commenting this out, could you condition the error on whether we are in a test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It turned out the proper close works not, so I could uncomment this part B)

#[cfg(splicing)]
struct FundedChannelRefundingWrapper<'a, SP: Deref> where SP::Target: SignerProvider {
struct NegotiatingV2ChannelView<'a, SP: Deref> where SP::Target: SignerProvider {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe drop V2 since a V1 channel can be spliced?

pub our_funding_inputs: Vec<(TxIn, TransactionU16LenLimited)>,
/// Set when splice_ack has been processed (on the initiator side),
/// used to prevent processing of multiple splice_ack's.
awaiting_splice_ack: bool,
Copy link
Contributor

Choose a reason for hiding this comment

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

IIUC, @wpaulino was saying we can check if those are Some?

@@ -8649,9 +8980,20 @@ impl<SP: Deref> FundedChannel<SP> where
"Insufficient inputs for splicing; channel ID {}, err {}",
self.context.channel_id(), err,
)})?;
// Convert inputs
let mut funding_inputs = Vec::new();
for (tx_in, tx, _w) in our_funding_inputs.into_iter() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm... that's unfortunate. It seems to stem form the fact that PersistenceNotifierGuard has an FnMut rather than an FnOnce. Might be able to fix that but probably better to save for a separate PR.

@optout21
Copy link
Contributor Author

Rebased to current main (there were some conflicts)

@optout21
Copy link
Contributor Author

Comments addressed, I will clean/reorg up the commits.

optout21 added 2 commits June 3, 2025 11:21
This is a simple rename, DualFundingContext to FundingNegotiationContext,
to suggest that this is use not only in dual-funded channel open.
Also rename the field dual_funding_context to funding_negotiation_context.
The begin_interactive_funding_tx_construction() method is extended with the
prev_funding_input optional parameter, containing the previous funding
transaction, which will be added to the negotiation as an input by the
initiator.
optout21 added 2 commits June 3, 2025 12:30
Introduce struct NegotiatingV2ChannelView to perform transaction negotiation
logic, on top of both PendingV2Channel (dual-funded channel open) and
FundedChannel (splicing).
Fill the logic for including transaction negotiation during splicing,
implement the functions:
splice_channel, splice_init, splice_ack, funding_tx_constructed.
Also extend the test case test_v1_splice_in with the steps for
funding negotiation during splicing.
@optout21
Copy link
Contributor Author

optout21 commented Jun 3, 2025

I have rearranged the commits in this PR (there were some reverted changes, many fixes, etc.), into these 4:

  • preparation: Rename DualFundingContext
  • partial: Extend begin_interactive_...() with splicing-specific parameters
  • partial: Introduce NegotiatingV2ChannelView to bridge Funded and PendingV2
  • main logic: Implement transaction negotiation during splicing

Otherwise, no change applied this time.
I'd appreciate a new round of review.

(Note: the old commits are available on branch splice-dual-tx4-save)

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.

4 participants