-
Notifications
You must be signed in to change notification settings - Fork 421
Avoid force-closing 0-conf channels when funding is reorg'd #4231
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
TheBlueMatt
wants to merge
1
commit into
lightningdevkit:main
Choose a base branch
from
TheBlueMatt:2025-11-0conf-no-reorg-close
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11344,9 +11344,13 @@ where | |
| } | ||
|
|
||
| // Check if the funding transaction was unconfirmed | ||
| let original_scid = self.funding.short_channel_id; | ||
| let was_confirmed = self.funding.funding_tx_confirmed_in.is_some(); | ||
| let funding_tx_confirmations = self.funding.get_funding_tx_confirmations(height); | ||
| if funding_tx_confirmations == 0 { | ||
| self.funding.funding_tx_confirmation_height = 0; | ||
| self.funding.short_channel_id = None; | ||
| self.funding.funding_tx_confirmed_in = None; | ||
| } | ||
|
|
||
| if let Some(channel_ready) = self.check_get_channel_ready(height, logger) { | ||
|
|
@@ -11361,18 +11365,33 @@ where | |
| self.context.channel_state.is_our_channel_ready() { | ||
|
|
||
| // If we've sent channel_ready (or have both sent and received channel_ready), and | ||
| // the funding transaction has become unconfirmed, | ||
| // close the channel and hope we can get the latest state on chain (because presumably | ||
| // the funding transaction is at least still in the mempool of most nodes). | ||
| // the funding transaction has become unconfirmed, we'll probably get a new SCID when | ||
| // it re-confirms. | ||
| // | ||
| // Note that ideally we wouldn't force-close if we see *any* reorg on a 1-conf or | ||
| // 0-conf channel, but not doing so may lead to the | ||
| // `ChannelManager::short_to_chan_info` map being inconsistent, so we currently have | ||
| // to. | ||
| if funding_tx_confirmations == 0 && self.funding.funding_tx_confirmed_in.is_some() { | ||
| let err_reason = format!("Funding transaction was un-confirmed. Locked at {} confs, now have {} confs.", | ||
| self.context.minimum_depth.unwrap(), funding_tx_confirmations); | ||
| return Err(ClosureReason::ProcessingError { err: err_reason }); | ||
| // Worse, if the funding has un-confirmed we could have accepted some HTLC(s) over it | ||
| // and are now at risk of double-spend. While its possible, even likely, that this is | ||
| // just a trivial reorg and we should wait to see the new block connected in the next | ||
| // call, its also possible we've been double-spent. To avoid further loss of funds, we | ||
| // need some kind of method to freeze the channel and avoid accepting further HTLCs, | ||
| // but absent such a method, we just force-close. | ||
| // | ||
| // The one exception we make is for 0-conf channels, which we decided to trust anyway, | ||
| // in which case we simply track the previous SCID as a `historical_scids` the same as | ||
| // after a channel is spliced. | ||
| if funding_tx_confirmations == 0 && was_confirmed { | ||
| if let Some(scid) = original_scid { | ||
| self.context.historical_scids.push(scid); | ||
| } else { | ||
| debug_assert!(false); | ||
| } | ||
| if self.context.minimum_depth(&self.funding).expect("set for a ready channel") > 1 { | ||
| // Reset the original short_channel_id so that we'll generate a closure | ||
| // `channel_update` broadcast event. | ||
| self.funding.short_channel_id = original_scid; | ||
| let err_reason = format!("Funding transaction was un-confirmed. Locked at {} confs, now have {} confs.", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: no point in logging |
||
| self.context.minimum_depth.unwrap(), funding_tx_confirmations); | ||
| return Err(ClosureReason::ProcessingError { err: err_reason }); | ||
| } | ||
| } | ||
| } else if !self.funding.is_outbound() && self.funding.funding_tx_confirmed_in.is_none() && | ||
| height >= self.context.channel_creation_height + FUNDING_CONF_DEADLINE_BLOCKS { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did we also not want to force close for 1-conf minimum channels ? Zero-conf channels have min depth 0 iiuc