Skip to content
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

Implement accepting dual-funded channels without contributing #3137

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

Conversation

dunxen
Copy link
Contributor

@dunxen dunxen commented Jun 20, 2024

We split this out from #2302 for easier review and to address the common non-public API parts of the V2 channel establishment implementation.

This will allow the holder to be an acceptor, but not initiator of V2 channels. We also don't expose an API for contributing to an inbound channel.

The functionality to initiate V2 channels and fund inbound channels forms part of #2302.

@dunxen dunxen force-pushed the 2024-06-non-public-API-v2-channels branch 3 times, most recently from b981cd7 to 0caf60e Compare June 20, 2024 16:14
@dunxen dunxen marked this pull request as ready for review June 20, 2024 16:27
@dunxen dunxen mentioned this pull request Jun 20, 2024
4 tasks
@dunxen dunxen force-pushed the 2024-06-non-public-API-v2-channels branch from 0caf60e to 44821af Compare June 24, 2024 16:55
@codecov-commenter
Copy link

codecov-commenter commented Jun 24, 2024

Codecov Report

Attention: Patch coverage is 62.30159% with 570 lines in your changes missing coverage. Please review.

Project coverage is 89.19%. Comparing base (0db051b) to head (67e0c12).

Files with missing lines Patch % Lines
lightning/src/ln/channelmanager.rs 58.92% 235 Missing and 18 partials ⚠️
lightning/src/ln/channel.rs 69.46% 154 Missing and 17 partials ⚠️
lightning/src/ln/interactivetxs.rs 51.81% 142 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3137      +/-   ##
==========================================
- Coverage   89.58%   89.19%   -0.40%     
==========================================
  Files         127      127              
  Lines      103534   104949    +1415     
  Branches   103534   104949    +1415     
==========================================
+ Hits        92750    93608     +858     
- Misses       8080     8600     +520     
- Partials     2704     2741      +37     

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

Copy link
Contributor

@optout21 optout21 left a comment

Choose a reason for hiding this comment

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

Looks great, mostly localized code improvements suggested.

lightning/src/ln/channel.rs Outdated Show resolved Hide resolved
lightning/src/ln/channel.rs Outdated Show resolved Hide resolved
lightning/src/ln/channel.rs Outdated Show resolved Hide resolved
lightning/src/ln/channel.rs Outdated Show resolved Hide resolved
lightning/src/ln/channel.rs Outdated Show resolved Hide resolved
lightning/src/ln/channel.rs Outdated Show resolved Hide resolved
lightning/src/ln/channel.rs Outdated Show resolved Hide resolved
lightning/src/ln/channel.rs Outdated Show resolved Hide resolved
lightning/src/ln/channelmanager.rs Outdated Show resolved Hide resolved
lightning/src/ln/mod.rs Outdated Show resolved Hide resolved
@optout21
Copy link
Contributor

As I see, the dual_funding cfg flag has been removed (not a problem)

@optout21
Copy link
Contributor

Looks to me that #2989 is in fact included in this PR (good!)

@dunxen dunxen force-pushed the 2024-06-non-public-API-v2-channels branch 4 times, most recently from 0db700b to 7dceedd Compare July 3, 2024 09:44
@dunxen dunxen requested review from optout21 and TheBlueMatt and removed request for optout21 July 3, 2024 09:51
optout21
optout21 previously approved these changes Jul 3, 2024
lightning/src/events/mod.rs Outdated Show resolved Hide resolved
@dunxen dunxen requested a review from jkczyz July 4, 2024 15:02
Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

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

Some initial comments, sorry this took so long to get back to.

lightning/src/events/mod.rs Show resolved Hide resolved
lightning/src/events/mod.rs Outdated Show resolved Hide resolved
lightning/src/ln/channel.rs Show resolved Hide resolved
lightning/src/ln/channelmanager.rs Outdated Show resolved Hide resolved
lightning/src/ln/channel.rs Outdated Show resolved Hide resolved
lightning/src/ln/channelmanager.rs Outdated Show resolved Hide resolved
lightning/src/ln/channelmanager.rs Outdated Show resolved Hide resolved

if chan.interactive_tx_signing_session.is_some() {
let monitor = try_chan_phase_entry!(self,
chan.commitment_signed_initial_v2(&msg, best_block, &self.signer_provider, &&logger), chan_phase_entry);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ugh...So the spec changed the definition of commitment_signed to now also include the initial funding commitment tx signing? Even though it implies other things inside the interactive state machine? Is it too late to change this? This seems nuts.

Does this mean that we should support adding HTLCs prior to the initial commitment transaction?

Copy link
Contributor Author

@dunxen dunxen Jul 9, 2024

Choose a reason for hiding this comment

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

The previous things it implies should really be looked at in the spec and changed there. We don't want to support adding HTLCs prior to initial commitment. So I really need to investigate what is going on with the spec and what might need to be fixed there.

if chan.interactive_tx_signing_session.is_some() {
let monitor = try_chan_phase_entry!(self,
chan.commitment_signed_initial_v2(&msg, best_block, &self.signer_provider, &&logger), chan_phase_entry);
if let Ok(persist_status) = self.chain_monitor.watch_channel(chan.context.get_funding_txo().unwrap(), monitor) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

If the persist status is pending we need to handle the later stuff in monitor_updating_restored. Really the whole contents of the block here should be in monitor_updating_restored.

lightning/src/ln/channel.rs Outdated Show resolved Hide resolved
Copy link
Contributor Author

@dunxen dunxen left a comment

Choose a reason for hiding this comment

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

Thanks @TheBlueMatt, for review and some good points raised. I'll address these ASAP ❤️

@dunxen dunxen force-pushed the 2024-06-non-public-API-v2-channels branch from 7dceedd to 64c35f4 Compare July 10, 2024 16:05
@dunxen
Copy link
Contributor Author

dunxen commented Jul 10, 2024

Still working on remaining initial feedback.

@dunxen dunxen force-pushed the 2024-06-non-public-API-v2-channels branch from 64c35f4 to a46da5a Compare July 11, 2024 14:55
@TheBlueMatt
Copy link
Collaborator

No rush, let me know when you want another pass.

@dunxen dunxen force-pushed the 2024-06-non-public-API-v2-channels branch 2 times, most recently from 0613f64 to 93896c4 Compare July 16, 2024 10:36
@dunxen dunxen force-pushed the 2024-06-non-public-API-v2-channels branch 3 times, most recently from 9af670e to df187b8 Compare September 23, 2024 07:05
@dunxen dunxen force-pushed the 2024-06-non-public-API-v2-channels branch 2 times, most recently from 86ddc27 to a932c92 Compare October 4, 2024 14:42
@dunxen
Copy link
Contributor Author

dunxen commented Oct 4, 2024

Rebased and working through drying up and continuing to address: #3137 (comment)

@TheBlueMatt
Copy link
Collaborator

LMK when you want another round of review here. I think it was mostly there so kinda waiting for you to tell me you've DRY'd things up and will take a look.

@dunxen dunxen force-pushed the 2024-06-non-public-API-v2-channels branch from a932c92 to 7e1cde2 Compare October 7, 2024 18:18
@dunxen
Copy link
Contributor Author

dunxen commented Oct 8, 2024

DRY'd up new initial commitment in latest push. Now moving over some tests from #2302 and modifying them to have manual V2 open channel messages so we can at least test them here.

fn context(&self) -> &ChannelContext<SP>;

fn initial_commitment_signed<L: Deref>(
&mut self, holder_commitment_tx: HolderCommitmentTransaction,
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should be able to shift more code here. eg assert_no_commitment_advancement() is shared across both places. The checking logic in check_funding_created_signature is shared in both sides (its broken out in the funding_created handler so that we can reset the state if it fails, but there's not really a strong reason to do that, we always FC the channel if we fail anyway, but we should still reset the funding_outpoint to None if we fail but the common code can check that case and do so). The call to .validate_holder_commitment is shared. There may be more too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok yeah I was a bit too prudent in sharing the code but there are still some obvious pieces like you pointed out. Will push them up in the existing DRY commit and a new commit with tests.

@dunxen dunxen force-pushed the 2024-06-non-public-API-v2-channels branch from 7e1cde2 to 14a5e14 Compare October 10, 2024 13:11
@dunxen
Copy link
Contributor Author

dunxen commented Oct 10, 2024

Pushed the DRYing up. Migrating the appropriate tests from 2302 is taking some time as more manual helper methods are needed. Almost there, though.

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.

Some comments based on our offline discussion. Will take a closer look at your recent changes.

Comment on lines +4114 to +4129
pub interactive_tx_constructor: Option<InteractiveTxConstructor>,
pub interactive_tx_signing_session: Option<InteractiveTxSigningSession>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these mutually exclusive? Actually, when would we have an InteractiveTxSigningSession when in ChannelPhase::Funded?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, that should have said InteractiveTxConstructor when in ChannelPhase::Funded.

Comment on lines +8868 to +8884
Ok(tx_constructor) => {
channel.set_interactive_tx_constructor(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.

Could we construct InteractiveTxConstructor before InboundV2Channel such that we can simply include the former when constructing the latter rather than needing a mutator?

ChannelPhase::Funded(chan) => &mut chan.interactive_tx_constructor,
_ => try_chan_phase_entry!(self, Err(ChannelError::Close(
(
"Got an unexpected tx_signatures message".into(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this say tx_abort message?

if self.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
self.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
self.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we're moving this, let's make it a debug_assert!(false, ...)

// If we've sent `commtiment_signed` for an interactive transaction construction,
// but have not received `tx_signatures` we MUST set `next_funding_txid` to the
// txid of that interactive transaction, else we MUST NOT set it.
next_funding_txid: Option<Txid>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you elaborate more in the comment on what issue(s) this addresses? Does this need a persistence of some kind to make it safe?

initiator_input_value_satoshis: u64,
}

// TODO(dual_funding): Use real node and API for creating V2 channels as initiator when available,
Copy link
Collaborator

Choose a reason for hiding this comment

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

bleh, if possible can we avoid adding more test code to channelmanager.rs...that file is huge (or do we need too many variables directly from ChannelManager that aren't pub here)?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd recommend the functional_tests.rs or new functional_tests_dual_funding.rs

Copy link
Collaborator

Choose a reason for hiding this comment

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

functional_tests.rs is also kinda huge. Ideally we keep our test files way smaller than most of them are :(

temporary_channel_id.clone()));
}

// Version-specific checks and logic
Copy link
Collaborator

Choose a reason for hiding this comment

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

Most of the code in both sides of this branch is the same, can we DRY it up more?

) -> Result<(Self, Option<InteractiveTxMessageSend>), AbortReason>
/// If the holder is the initiator, they need to send the first message which is a `TxAddInput`
/// message.
pub fn new<ES: Deref>(args: InteractiveTxConstructorArgs<ES>) -> Result<Self, AbortReason>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can most of the changes in here be split into separate commit(s) so they can be described in commit messages?


// TODO(dual_funding): Check all sigs are SIGHASH_ALL.

// TODO(dual_funding): I don't see how we're going to be able to ensure witness-standardness
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yea, in general I think we aren't gonna bother, at least not for some time. We should carefully document when we add local-funding logic that the counterparty can add inputs which will prevent the funding tx from confirming, and we make no attempt to prevent that. We should also document this in the open channel logic now (and in release notes added in this PR since its a major change).

msg,
});
}
Ok(())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we close the channel here?

}

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct InteractiveTxSigningSession {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here, can this go in a separate commit? Could also use some documentation.

@TheBlueMatt
Copy link
Collaborator

Git history is weird here looks like some commits from upstream got cherry-picked, can just rebase to fix it.

@dunxen
Copy link
Contributor Author

dunxen commented Oct 16, 2024

Git history is weird here looks like some commits from upstream got cherry-picked, can just rebase to fix it.

Yeah, I see now. Fixing.

Will address outstanding feedback from today.

@dunxen dunxen force-pushed the 2024-06-non-public-API-v2-channels branch from 494413c to 009512f Compare October 16, 2024 10:36
@dunxen dunxen force-pushed the 2024-06-non-public-API-v2-channels branch from 009512f to 67e0c12 Compare October 16, 2024 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants