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 6 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)

As multiple traits contain a context -- InitialRemoteCommitmentReceiver, FundingTxConstructor -- the context part is extracted into a separate new base trait, called ChannelContextProvider.
PendingV2Channel struct can do transaction negotiation operations, but now behind a trait, so that FundingChannel is also do that, and inherit some common logic.
FundedChannel is extended with an optional struct RefundingScope, that holds data used during splicing (re)negotiation.
It stores the same fields as PendingV2Channel, excet for the context.
FundedChannel can act as a transaction constructor (much like PendingV2Channel), when the refunding context is present.
Extend begin_interactive_funding_tx_construction() with splicing-specific parameter: extra funding input.
@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.

Handle the transaction negotiation messages during splice negotiation
(tx_add_input, tx_add_output, tx_complete).
@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.

impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
/// A channel struct implementing this trait can perform V2 transaction negotiation,
/// either at channel open or during splicing.
/// Accessors return a Result, because [`FundedChannel`] can act like a TX constructor,
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 them all return an option? I don't see the error case being useful here

fn pending_funding_mut(&mut self) -> Result<&mut FundingScope, &'static str>;
fn pending_funding_and_context_mut(&mut self) -> Result<(&FundingScope, &mut ChannelContext<SP>), &'static str>;
fn dual_funding_context(&self) -> Result<&DualFundingChannelContext, &'static str>;
fn swap_out_dual_funding_context_inputs(&mut self, funding_inputs: &mut Vec<(TxIn, TransactionU16LenLimited)>) -> Result<(), &'static str>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we just get a mutable reference to the context to avoid this? This, along with the UnfundedContext, doesn't seem to apply for a splice anyway.

Comment on lines +2757 to +2758
pending_unfunded_context: UnfundedChannelContext,
pending_dual_funding_context: DualFundingChannelContext,
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't need these for splicing

/// 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?

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

ChannelPhase::UnfundedV2(chan) => chan.tx_add_input(msg),
#[cfg(splicing)]
ChannelPhase::Funded(chan) => chan.tx_add_input(msg),
_ => panic!("Got tx_add_input in an invalid phase"),
Copy link
Contributor

Choose a reason for hiding this comment

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

We definitely don't want to panic. Peers can send any message at any point, and we shouldn't crash because of it.

#[cfg(splicing)]
impl RefundingScope {
/// Get a transaction input that is the previous funding transaction
fn get_input_of_previous_funding(pre_funding_transaction: &Option<Transaction>, pre_funding_txo: &Option<OutPoint>)
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems better suited as a method on FundedChannel

)?;

let post_value_to_self_msat = self.funding().value_to_self_msat.saturating_add(our_funding_satoshis);
let mut post_channel_transaction_parameters = self.funding().channel_transaction_parameters.clone();
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 have the funding key rotated

Comment on lines +9124 to +9125
counterparty_selected_channel_reserve_satoshis: self.funding.counterparty_selected_channel_reserve_satoshis, // TODO check
holder_selected_channel_reserve_satoshis: self.funding.holder_selected_channel_reserve_satoshis, // TODO check
Copy link
Contributor

Choose a reason for hiding this comment

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

These should be based on the new channel value, right?

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.

Comment on lines +2307 to +2308
/// Accessors return a Result, because [`FundedChannel`] can act like a TX constructor,
/// but not always (only during splicing).
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm.... we could avoid needing Result by creating a temporary RenegotiatingFunding wrapper around &FundedChannel that implements FundingTxConstructorV2. It could be obtained using a as_renegotiating_funding method on FundedChannel. Basically, push the Result / Option check to when calling as_renegotiating_funding instead of all the other methods.

fn pending_funding_mut(&mut self) -> Result<&mut FundingScope, &'static str>;
fn pending_funding_and_context_mut(&mut self) -> Result<(&FundingScope, &mut ChannelContext<SP>), &'static str>;
fn dual_funding_context(&self) -> Result<&DualFundingChannelContext, &'static str>;
fn swap_out_dual_funding_context_inputs(&mut self, funding_inputs: &mut Vec<(TxIn, TransactionU16LenLimited)>) -> Result<(), &'static str>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe just make a dual_funding_context_mut method and do the swap at the call site.

Comment on lines +1260 to +1262
chan.pending_splice.as_mut().map(|splice|
splice.refunding_scope.as_mut().map(|refunding_scope| &mut refunding_scope.pending_unfunded_context)
).flatten()
Copy link
Contributor

Choose a reason for hiding this comment

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

Use and_then instead of map to avoid needing to call flatten. Likewise throughout this commit.

Copy link

codecov bot commented Apr 30, 2025

Codecov Report

Attention: Patch coverage is 64.65969% with 135 lines in your changes missing coverage. Please review.

Project coverage is 90.26%. Comparing base (7b45811) to head (83324da).
Report is 82 commits behind head on main.

Files with missing lines Patch % Lines
lightning/src/ln/channel.rs 64.43% 122 Missing and 11 partials ⚠️
lightning/src/ln/channelmanager.rs 75.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3736      +/-   ##
==========================================
+ Coverage   89.10%   90.26%   +1.16%     
==========================================
  Files         156      158       +2     
  Lines      123431   135623   +12192     
  Branches   123431   135623   +12192     
==========================================
+ Hits       109985   122425   +12440     
+ Misses      10760    10672      -88     
+ Partials     2686     2526     -160     

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

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