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

Introduce FundingScope abstraction to ChannelMonitor #3663

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

Conversation

wpaulino
Copy link
Contributor

When establishing a channel, the funding transaction may be replaced either:

  • after the funding transaction has confirmed using splicing,
  • before the funding transaction has confirmed for v2 channel establishment using tx_init_rbf, or
  • before the splice's funding transaction has confirmed using tx_init_rbf.

In each of these cases, fields in the ChannelMonitor will need to be updated once the new funding transaction confirms. This commit introduces a FundingScope to hold the aforementioned fields, allowing to swap in another FundingScope when necessary.

This is the same abstraction as in channel.rs representing a different set of data.

@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Mar 12, 2025

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

@wpaulino wpaulino requested a review from jkczyz March 12, 2025 00:22
@wpaulino wpaulino added the weekly goal Someone wants to land this this week label Mar 12, 2025
Copy link

codecov bot commented Mar 12, 2025

Codecov Report

Attention: Patch coverage is 92.85714% with 14 lines in your changes missing coverage. Please review.

Project coverage is 89.20%. Comparing base (00ee0ef) to head (6097917).
Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
lightning/src/chain/channelmonitor.rs 92.51% 3 Missing and 11 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3663      +/-   ##
==========================================
- Coverage   89.23%   89.20%   -0.04%     
==========================================
  Files         155      155              
  Lines      119327   119357      +30     
  Branches   119327   119357      +30     
==========================================
- Hits       106482   106469      -13     
- Misses      10242    10284      +42     
- Partials     2603     2604       +1     

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

/// The set of outpoints in each counterparty commitment transaction. We always need at least
/// the payment hash from `HTLCOutputInCommitment` to claim even a revoked commitment
/// transaction broadcast as we need to be able to construct the witness script in all cases.
counterparty_claimable_outpoints: HashMap<Txid, Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)>>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, is there a way to avoid duplicating these for all state updates while a splice is pending? In theory it should be short but it seems like a DoS issue if someone can get 100 splices in-flight at once and then never confirm any and our memory usage goes up 100x.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call, we're definitely gonna need a new way of tracking these to avoid that. We could keep them at the ChannelMonitorImpl level and index them by the counterparty commitment number (since all scopes will be at the same commitment number). Then, we add a new map in FundingScope that maps txid -> commitment number.

Is there a reason for us using the txid over the commitment number in current main?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, doing this will actually break the transaction_output_index, they are not guaranteed to remain consistent across all commitments/scopes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It does seem like "HTLCOutputInCommiemnt's output_index is not like the other fields" keeps coming up....maybe time to finally refactor the type so that output_index is separate (cc @tankyleo)

Copy link
Contributor

Choose a reason for hiding this comment

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

yes I can certainly take a look. Hopefully this allows us to delete the brute force search we talked about last week, Matt, to populate the output indices.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a TODO(splicing) to make sure we don't miss this.

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.

IIUC, a follow-up PR will have ChannelMonitor hold a FundingScope for each pending transaction?

channel_value_satoshis, channel_keys_id, destination_script.into(), keys,
channel_parameters.clone(), initial_holder_commitment_tx, secp_ctx
channel_parameters.channel_value_satoshis, channel_keys_id, destination_script.into(),
keys, channel_parameters.clone(), initial_holder_commitment_tx, secp_ctx
Copy link
Contributor

Choose a reason for hiding this comment

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

Will OnchainTxHandler hold parameters for each funding scope?

Unrelated to this PR, but I wonder if we need to write channel_parameters twice if we can pass it to OnchainTxHandler on read.

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, I plan to move all channel-specific data from OnchainTxHandler to ChannelMonitor, and provide it as needed to OnchainTxHandler via the outputs API.

When establishing a channel, the funding transaction may be replaced
either:

- after the funding transaction has confirmed using splicing,
- before the funding transaction has confirmed for v2 channel
  establishment using `tx_init_rbf`, or
- before the splice's funding transaction has confirmed using
  `tx_init_rbf`.

In each of these cases, fields in the `ChannelMonitor` will need to be
updated once the new funding transaction confirms. This commit
introduces a `FundingScope` to hold the aforementioned fields, allowing
to swap in another `FundingScope` when necessary.

This is the same abstraction as in `channel.rs` representing a different
set of data.
The `ChannelTransactionParameters` will change across `FundingScope`s,
so we make sure to track it to ensure it is updated accordingly when a
new `FundingScope` is applied/considered.

Along the way, we also clean up some unnecessary function arguments to
`ChannelMonitor::new` that we can just obtain from the
`ChannelTransactionParameters` already provided.
@wpaulino
Copy link
Contributor Author

IIUC, a follow-up PR will have ChannelMonitor hold a FundingScope for each pending transaction?

Yeah it will happen in the PR introducing splice monitor updates.

@wpaulino wpaulino force-pushed the channel-monitor-funding-scope branch from 8b47754 to 6097917 Compare March 12, 2025 22:31
@wpaulino wpaulino requested review from jkczyz and TheBlueMatt March 12, 2025 22:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
weekly goal Someone wants to land this this week
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants