Skip to content

Store and check channel data in channel monitors - #4218

Closed
joostjager wants to merge 4 commits into
lightningdevkit:mainfrom
joostjager:chan-monitor-consistency-check
Closed

Store and check channel data in channel monitors#4218
joostjager wants to merge 4 commits into
lightningdevkit:mainfrom
joostjager:chan-monitor-consistency-check

Conversation

@joostjager

@joostjagerjoostjager commented Nov 11, 2025

Copy link
Copy Markdown
Contributor

Add a new safe_channels feature flag that gates in-development work toward persisting channel monitors and channels atomically, preventing them from desynchronizing and causing force closures.

This commit begins that transition by storing both pieces together and adding consistency checks during writes. These checks mirror what the channel manager currently validates only on reload, but performing them earlier increases coverage and surfaces inconsistencies sooner.

@ldk-reviews-bot

ldk-reviews-bot commented Nov 11, 2025

Copy link
Copy Markdown

👋 I see @valentinewallace was un-assigned.
If you'd like another reviewer assignment, please click here.

@joostjager
joostjagerforce-pushed the chan-monitor-consistency-check branch 3 times, most recently from dda4c5c to b00ff03CompareNovember 14, 2025 10:56
@joostjagerjoostjager changed the title add channel <-> channel monitor consistency checkChannel <-> channel monitor consistency checkNov 14, 2025
@joostjager
joostjagerforce-pushed the chan-monitor-consistency-check branch 2 times, most recently from c3771b6 to f387f90CompareNovember 14, 2025 11:01
@codecov

codecovBot commented Nov 14, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 65.54054% with 51 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.34%. Comparing base (62c5849) to head (4e068e2).
⚠️ Report is 507 commits behind head on main.

Files with missing linesPatch %Lines
lightning/src/ln/channel.rs38.66%46 Missing ⚠️
lightning/src/ln/channelmanager.rs91.80%5 Missing ⚠️
Additional details and impacted files
@@ Coverage Diff @@## main #4218 +/- ##
==========================================
- Coverage 89.38% 89.34% -0.04% 
==========================================
Files 180 180 Lines 139834 139921 +87 Branches 139834 139921 +87 ==========================================
+ Hits 124985 125008 +23 - Misses 12262 12324 +62 - Partials 2587 2589 +2 
FlagCoverage Δ
fuzzing35.21% <44.59%> (-0.01%)⬇️
tests88.67% <64.18%> (-0.05%)⬇️

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.

@joostjager
joostjagerforce-pushed the chan-monitor-consistency-check branch 3 times, most recently from 00a95f1 to a82719bCompareNovember 14, 2025 19:36
@joostjagerjoostjager changed the title Channel <-> channel monitor consistency checkStore and check channel data in channel monitors Add a new safe_channels feature flag that gates in-development work toward persisting channel monitors and channels atomically, preventing them from desynchronizing and causing force closures. This commit begins that transition by storing both pieces together and adding consistency checks during writes. These checks mirror what the channel manager currently validates only on reload, but performing them earlier increases coverage and surfaces inconsistencies sooner.Nov 14, 2025
@joostjagerjoostjager changed the title Store and check channel data in channel monitors Add a new safe_channels feature flag that gates in-development work toward persisting channel monitors and channels atomically, preventing them from desynchronizing and causing force closures. This commit begins that transition by storing both pieces together and adding consistency checks during writes. These checks mirror what the channel manager currently validates only on reload, but performing them earlier increases coverage and surfaces inconsistencies sooner.Store and check channel data in channel monitorsNov 14, 2025
@joostjager
joostjagerforce-pushed the chan-monitor-consistency-check branch from a82719b to d4d0879CompareNovember 18, 2025 14:13
@joostjager
joostjager removed the request for review from valentinewallaceNovember 19, 2025 14:50

/// The encoded channel data associated with this ChannelMonitor, if any.
#[cfg(feature = "safe_channels")]
pub encoded_channel: Option<Vec<u8>>,

@joostjagerjoostjagerNov 20, 2025

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Continuing discussion here @valentinewallace#4151 (comment)

Did you explore encoding the FundedChannel itself rather than a bag of bytes? I guess it would require propagating the signer trait parameter, which is kind of annoying?

I think it is possible, but with a few workarounds. The advantage would be that we have a clear target struct to strip all the data off that is redundant for channel monitor.

https://github.com/joostjager/rust-lightning/compare/chan-monitor-unencoded-chan%7E1...joostjager:rust-lightning:chan-monitor-unencoded-chan?expand=1 (wip)

We do need to allow cloning of the channel to get a copy to store inside the monitor and monitor update. Also equality needs to be implemented. And it is the case that a channel contains a list of blocked monitor updates, that can again contain a channel copy. Not sure if we getting into something undesirable with that form of nesting.

The pending_splice field also turned out to be difficult to clone. For now, I just took the serialized version of it.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Problems above have mostly been solved now

@joostjager

joostjager commented Nov 24, 2025

Copy link
Copy Markdown
ContributorAuthor

Tests now pass with the unencoded channel as part of ChannelMonitorUpdate. Spent considerable time figuring out how to fix the infinite type recursion problem caused by blocked_monitor_updates (which again contain FundedChannelState instances), but didn't succeed. For now, the serialized versions are stored in blocked_monitor_updates to break the recursion.

Isolated repro: https://github.com/lightningdevkit/rust-lightning/compare/main...joostjager:repro-recursive-ser-issue?expand=1

@joostjager
joostjagerforce-pushed the chan-monitor-consistency-check branch 2 times, most recently from 2dc7ca2 to 17f71b0CompareNovember 25, 2025 07:33
@joostjager
joostjagerforce-pushed the chan-monitor-consistency-check branch 5 times, most recently from 44538f9 to 4c035abCompareNovember 27, 2025 08:40
@joostjager
joostjagerforce-pushed the chan-monitor-consistency-check branch 2 times, most recently from ab4f775 to 4c69502CompareDecember 4, 2025 09:10
Add a new `safe_channels` feature flag that gates in-development work
toward persisting channel monitors and channels atomically, preventing
them from desynchronizing and causing force closures.
This commit begins that transition by storing both pieces together and
adding consistency checks during writes. These checks mirror what the
channel manager currently validates only on reload, but performing them
earlier increases coverage and surfaces inconsistencies sooner.
@joostjager
joostjagerforce-pushed the chan-monitor-consistency-check branch from 4c69502 to 9114c1dCompareDecember 19, 2025 08:01
@joostjager

Copy link
Copy Markdown
ContributorAuthor

Implemented safe channels via #4351

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@joostjager@ldk-reviews-bot