Skip to content

Clean up and split up is_pre_funded_state#4021

Merged
TheBlueMatt merged 3 commits intolightningdevkit:mainfrom
TheBlueMatt:2025-08-pre-funded-state-checks
Aug 28, 2025
Merged

Clean up and split up is_pre_funded_state#4021
TheBlueMatt merged 3 commits intolightningdevkit:mainfrom
TheBlueMatt:2025-08-pre-funded-state-checks

Conversation

@TheBlueMatt
Copy link
Collaborator

This turns out to have been kinda buggy, and impossible to read the code, so clean it up a bunch.

@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Aug 18, 2025

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

@codecov
Copy link

codecov bot commented Aug 18, 2025

Codecov Report

❌ Patch coverage is 83.33333% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.75%. Comparing base (1c36624) to head (4cd52cb).
⚠️ Report is 29 commits behind head on main.

Files with missing lines Patch % Lines
lightning/src/ln/channel.rs 82.14% 4 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4021      +/-   ##
==========================================
- Coverage   88.78%   88.75%   -0.03%     
==========================================
  Files         176      176              
  Lines      128139   128833     +694     
  Branches   128139   128833     +694     
==========================================
+ Hits       113768   114348     +580     
- Misses      11803    11894      +91     
- Partials     2568     2591      +23     
Flag Coverage Δ
fuzzing 21.93% <23.33%> (+0.02%) ⬆️
tests 88.58% <83.33%> (-0.03%) ⬇️

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.

Copy link
Contributor

@wpaulino wpaulino left a comment

Choose a reason for hiding this comment

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

I think we could make UnfundedV2 channels be resumable as well, currently we don't keep them around after a disconnect

@wpaulino wpaulino requested review from jkczyz and removed request for joostjager August 18, 2025 22:56
@TheBlueMatt TheBlueMatt added this to the 0.2 milestone Aug 19, 2025
@ldk-reviews-bot
Copy link

🔔 1st Reminder

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

@TheBlueMatt
Copy link
Collaborator Author

Discussed this offline yesterday and I believe we concluded this is probably the right way to go (keeping the ChannelState as Funded during splicing) but @wpaulino was gonna look into it a bit more. In the mean time, I kinda assume this PR can go ahead, and we can change the checks here when splicing actually starts updating ChannelState (as with all the other places that will need to change).

@TheBlueMatt TheBlueMatt requested a review from wpaulino August 21, 2025 14:51
@TheBlueMatt TheBlueMatt self-assigned this Aug 21, 2025
@TheBlueMatt TheBlueMatt added the weekly goal Someone wants to land this this week label Aug 21, 2025
@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

🔔 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

🔔 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

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

@wpaulino
Copy link
Contributor

I think we could make UnfundedV2 channels be resumable as well, currently we don't keep them around after a disconnect

Do you plan to address this here as well @TheBlueMatt? Feel free to squash

@TheBlueMatt
Copy link
Collaborator Author

Oops, sorry, I missed that comment. I don't really want to change the set of resumable channels in this PR, its already complicated enough as it is :/

@TheBlueMatt TheBlueMatt force-pushed the 2025-08-pre-funded-state-checks branch from 9e89ac9 to 9a24ecb Compare August 26, 2025 17:39
@TheBlueMatt
Copy link
Collaborator Author

Went ahead and squashed.

@TheBlueMatt TheBlueMatt force-pushed the 2025-08-pre-funded-state-checks branch from 9a24ecb to ad3ff19 Compare August 26, 2025 18:02
@TheBlueMatt
Copy link
Collaborator Author

Grr, also had to rebase.

`Channel::is_pre_funded_state` is used to mean several different
things. In this case, its used to skip all the `shutdown` logic as
the funding transaction can't possibly have been broadcasted so
there's really no ned to try to sign a transaction spending it.

Here, we really want to capture any channel in `NegotiatingFunding`
or any V1 channel in `FundingNegotiated` or, finally, any V2
channel in `FundingNegotiated` where we haven't yet sent our
signatures (which is not captured in `is_pre_funded_state`).

Instead of a new helper, we just check the states directly in
`shutdown` handling.
`Channel::is_pre_funded_state` is used to mean several different
things. In this case its used to decide if we should provide a
`ChannelMonitorUpdate` marking a channel as closed when we go to
force-close it.

Here, we want to capture exactly when the original `ChannelMonitor`
is first created, but were doing so indirectly by looking at the
channel's state. Worse, `is_pre_funded_state` got updated to be
false whenever there is an interctive signing session, which isn't
correct for this use - we may have an interactive signing session
but have already persisted the original `ChannelMonitor` when we
received the first `commitment_signed`.

Instead, we just move to examining
`counterparty_next_commitment_transaction_number` which is
decrementing for the first time at exactly the time we create the
original `ChannelMonitor`, so it provides a much simpler test.

Fixes lightningdevkit#3880
`Channel::is_pre_funded_state` is used to mean several different
things. In the past few commits we stopped using it for a few
conflicting uses, but here we break out the remaining uses and
rename the remnants for clarity.

`is_funding_broadcast` was using `is_pre_funded_state` and was then
later used to decide if the `Channel` could be written to disk
(because it can be resumed on restart), if we should broadcast a
force-close transaction, and when to emit a `ChannelPending` event.

These were also somewhat divergent - we shouldn't generate a
`ChannelReady` event or broadcast a force-closing transaction until
we've actually broadcasted but want to write the `Channel` to disk
once we enter funding signature exchange for dual-funded open.

Thus, the ability to write a `Channel` to disk is provided by a new
`can_resume_on_restart` method. Then, `is_funding_broadcast`  is
updated to only consider funding broadcasted after we provide our
funding signatures (i.e. the funding *could* have been
broadcasted). This is still a bit early to generate a
`ChannelPending` event (as the funding may not actually have been
broadcasted yet), but its better than it was.

Finally, the remaining `is_pre_funded_state` is renamed
`can_resume_on_reconnect`, which has slightly different semantics
than on-restart channels in batch opens.
@TheBlueMatt TheBlueMatt force-pushed the 2025-08-pre-funded-state-checks branch from ad3ff19 to 4cd52cb Compare August 28, 2025 14:52
@TheBlueMatt
Copy link
Collaborator Author

Fixed commit messages and squash-pushed a method rename (which is only inaccurate for the manual-broadcast case, but it is in that case):

$ git diff-tree -U1 ad3ff19d1 4cd52cb9c
diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs
index d97c5a346f..db8a36b4ad 100644
--- a/lightning/src/ln/channel.rs
+++ b/lightning/src/ln/channel.rs
@@ -4003,3 +4003,3 @@ where
 	pub(crate) fn should_emit_channel_pending_event(&mut self) -> bool {
-		self.is_funding_broadcast() && !self.channel_pending_event_emitted
+		self.is_funding_broadcastable() && !self.channel_pending_event_emitted
 	}
@@ -4099,3 +4099,3 @@ where
 	/// funding transaction has been broadcast if necessary.
-	fn is_funding_broadcast(&self) -> bool {
+	fn is_funding_broadcastable(&self) -> bool {
 		match self.channel_state {
@@ -5451,3 +5451,3 @@ where

-		let broadcast = self.is_funding_broadcast();
+		let broadcast = self.is_funding_broadcastable();

@TheBlueMatt TheBlueMatt requested review from jkczyz and wpaulino August 28, 2025 14:55
@TheBlueMatt TheBlueMatt merged commit a9bbb24 into lightningdevkit:main Aug 28, 2025
25 checks passed
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

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants