Skip to content

Consider currently confirmed FundingScope when claiming commitments #3980

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

wpaulino
Copy link
Contributor

@wpaulino wpaulino commented Jul 31, 2025

Once a commitment transaction confirms, we may have outputs to claim. To determine whom the commitment transaction belongs to, we generally compare its txid against what we know be ours and the counterparty's. This, however, relies on being able to match on the expected FundingScope, such that we can produce the necessary output claims. Since a commitment transaction confirming implies that the funding transaction it spends has also confirmed, we rely on alternative_funding_confirmed to obtain the corresponding FundingScope.

@wpaulino wpaulino requested a review from TheBlueMatt July 31, 2025 18:20
@wpaulino wpaulino self-assigned this Jul 31, 2025
@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Jul 31, 2025

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

Copy link

codecov bot commented Jul 31, 2025

Codecov Report

❌ Patch coverage is 79.87421% with 32 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.93%. Comparing base (de2005a) to head (c5c1e22).
⚠️ Report is 18 commits behind head on main.

Files with missing lines Patch % Lines
lightning/src/chain/channelmonitor.rs 79.61% 28 Missing and 4 partials ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main    #3980    +/-   ##
========================================
  Coverage   88.93%   88.93%            
========================================
  Files         174      174            
  Lines      124539   124649   +110     
  Branches   124539   124649   +110     
========================================
+ Hits       110758   110859   +101     
- Misses      11287    11290     +3     
- Partials     2494     2500     +6     
Flag Coverage Δ
fuzzing 22.21% <44.02%> (+0.03%) ⬆️
tests 88.76% <79.87%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@ldk-reviews-bot
Copy link

🔔 1st Reminder

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

👋 The first review has been submitted!

Do you think this PR is ready for a second reviewer? If so, click here to assign a second reviewer.

@wpaulino wpaulino force-pushed the splice-commitment-claims branch from 5ecc866 to 379edfc Compare August 5, 2025 18:53
@wpaulino wpaulino requested a review from TheBlueMatt August 5, 2025 18:54
@wpaulino wpaulino force-pushed the splice-commitment-claims branch from 379edfc to 4e03e45 Compare August 5, 2025 22:25
@ldk-reviews-bot
Copy link

✅ Added second reviewer: @tankyleo

@wpaulino wpaulino requested a review from TheBlueMatt August 5, 2025 22:55
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.

Generally fine, at least once the parent PR lands, but I'm still confused on #3980 (comment)

@TheBlueMatt
Copy link
Collaborator

Needs rebase, also might be worth addressing #3939 (comment) here but up to you.

@wpaulino wpaulino force-pushed the splice-commitment-claims branch from 4e03e45 to 53e1c59 Compare August 7, 2025 03:12
@wpaulino wpaulino requested a review from TheBlueMatt August 7, 2025 03:13
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.

Does load_outputs_to_watch need to look at alternative fundings?

@TheBlueMatt
Copy link
Collaborator

Oh, also we'll need to do something with build_counterparty_commitment_tx/counterparty_commitment_txs_from_update and sign_to_local_justice_tx

@ldk-reviews-bot
Copy link

🔔 1st Reminder

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

@wpaulino
Copy link
Contributor Author

wpaulino commented Aug 7, 2025

Does load_outputs_to_watch need to look at alternative fundings?

It already does for register_tx calls. For register_output calls, outputs are not directly associated with the FundingScope they originated from (though there's no reason why they shouldn't, can do if you think it's worth it), so we're tracking them all even for FundingScopes no longer relevant.

build_counterparty_commitment_tx/counterparty_commitment_txs_from_update

Given their current usage, it's not required to change them but definitely worth changing. I already have the change in a separate branch. build_counterparty_commitment_tx is currently only used when we expect to have one FundingScope, so using self.funding is always safe. For the multi-commitment counterparty updates, the commitment transactions already included the scope specific data when they were built.

sign_to_local_justice_tx

Already had this change as well, just didn't include it here.

Copy link
Contributor

@tankyleo tankyleo left a comment

Choose a reason for hiding this comment

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

LGTM on first pass, will take another pass first thing tomorrow :) Thanks for the cleanup in check_spend_holder_transaction

@wpaulino wpaulino force-pushed the splice-commitment-claims branch from 53e1c59 to e15e131 Compare August 8, 2025 17:47
if let Some((_, conf_height)) = self.alternative_funding_confirmed.as_ref() {
if *conf_height == height {
self.alternative_funding_confirmed.take();
if self.holder_tx_signed {
if self.holder_tx_signed || self.funding_spend_seen {
Copy link
Contributor

Choose a reason for hiding this comment

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

When the alternative_funding_confirmed gets reorged out, in which cases would we not broadcast the holder commitment ?

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 could be lockdown_from_offchain from a ChannelForceClosed update where should_broadcast is false, but that means we never attempted to broadcast the funding transaction.

@tankyleo
Copy link
Contributor

tankyleo commented Aug 8, 2025

Finished second pass

We generally want to hard assert on cases that could cause us to lose
money.
We can't rely waiting on another (or the same) renegotiated funding
transaction to confirm, since it may never happen. We also don't want to
rely on the counterparty to broadcast for us, or require manual
intervention from the user, so we choose to broadcast the new holder
commitment immediately. This ensures we're able to claim funds from an
already force closed channel after an alternative funding reorg.
Once a commitment transaction confirms, we may have outputs to claim. To
determine whom the commitment transaction belongs to, we generally
compare its `txid` against what we know be ours and the counterparty's.
This, however, relies on being able to match on the expected
`FundingScope`, such that we can produce the necessary output claims.
Since a commitment transaction confirming implies that the funding
transaction it spends has also confirmed, we rely on
`alternative_funding_confirmed` to obtain the corresponding
`FundingScope`.
It's best to let the caller decide what the appropriate
`ChannelTransactionParameters` are as it has the most context.
Since there may be multiple counterparty commitment transactions for the
same commitment number due to splicing, we have to locate the matching
`FundingScope::channel_parameters` to provide the signer. Since this is
intended to be called during `Persist::update_persisted_channel`, the
monitor should have already had the update applied.
@wpaulino wpaulino force-pushed the splice-commitment-claims branch from e15e131 to c5c1e22 Compare August 8, 2025 22:30
@wpaulino wpaulino requested a review from tankyleo August 8, 2025 22:30
height,
block_hash,
holder_commitment_htlcs!(self, PREV_WITH_SOURCES).unwrap(),
holder_commitment_htlcs!(self, CURRENT_WITH_SOURCES),
Copy link
Contributor

Choose a reason for hiding this comment

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

PREV_WITH_SOURCES ?

Comment on lines +2436 to +2444
let funding = inner
.alternative_funding_confirmed
.map(|(alternative_funding_txid, _)| {
inner.pending_funding
.iter()
.find(|funding| funding.funding_txid() == alternative_funding_txid)
.expect("FundingScope for confirmed alternative funding must exist")
})
.unwrap_or(&inner.funding);
Copy link
Contributor

Choose a reason for hiding this comment

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

We added the macro in the other spot that could use it, this one could use it too :)

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

Successfully merging this pull request may close these issues.

4 participants