Skip to content

fuzz: cover deferred writing in chanmon_consistency#4465

Draft
joostjager wants to merge 7 commits intolightningdevkit:mainfrom
joostjager:chain-mon-internal-deferred-writes-with-fuzz
Draft

fuzz: cover deferred writing in chanmon_consistency#4465
joostjager wants to merge 7 commits intolightningdevkit:mainfrom
joostjager:chain-mon-internal-deferred-writes-with-fuzz

Conversation

@joostjager
Copy link
Contributor

@joostjager joostjager commented Mar 6, 2026

Based on #4351

joostjager and others added 7 commits March 6, 2026 11:30
The previous wording implied that persisting a full ChannelMonitor
would automatically resolve all pending updates. Reword to make clear
that each update ID still needs to be individually marked complete via
channel_monitor_updated, even after a full monitor persistence.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Extract the ChannelMonitor construction boilerplate that was
duplicated across channelmonitor test functions into a reusable
#[cfg(test)] pub(super) dummy_monitor helper, generic over the
signer type.

AI tools were used in preparing this commit.
Pure refactor: move the bodies of Watch::watch_channel and
Watch::update_channel into methods on ChainMonitor, and have
the Watch trait methods delegate to them. This prepares for adding
deferred mode where the Watch methods will conditionally queue
operations instead of executing them immediately.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add a `deferred` parameter to `ChainMonitor::new` and
`ChainMonitor::new_async_beta`. When set to true, the Watch trait
methods (watch_channel and update_channel) will unimplemented!() for
now. All existing callers pass false to preserve current behavior.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace the unimplemented!() stubs with a full deferred write
implementation. When ChainMonitor has deferred=true, Watch trait
operations queue PendingMonitorOp entries instead of executing
immediately. A new flush() method drains the queue and forwards
operations to the internal watch/update methods, calling
channel_monitor_updated on Completed status.

The BackgroundProcessor is updated to capture pending_operation_count
before persisting the ChannelManager, then flush that many writes
afterward - ensuring monitor writes happen in the correct order
relative to manager persistence.

Key changes:
- Add PendingMonitorOp enum and pending_ops queue to ChainMonitor
- Implement flush() and pending_operation_count() public methods
- Integrate flush calls in BackgroundProcessor (both sync and async)
- Add TestChainMonitor::new_deferred, flush helpers, and auto-flush
  in release_pending_monitor_events for test compatibility
- Add create_node_cfgs_deferred for deferred-mode test networks
- Add unit tests for queue/flush mechanics and full payment flow

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add a `deferred` flag to `TestChainMonitor` that controls whether the
underlying `ChainMonitor` queues operations instead of executing them
immediately. The flag is derived from the fuzz input alongside the
existing monitor style bits, so each of the three nodes can
independently run in deferred or immediate mode.

In deferred mode, `watch_channel` and `update_channel` always return
`InProgress`. A new `flush_and_update_latest_monitors` method drains
the queued operations and, when the persister reports `Completed`,
promotes the pending shadow monitor snapshots to persisted state.
This method is called before `release_pending_monitor_events` and
at each point where the fuzzer completes pending monitor updates.

On node reload, deferred monitors are flushed immediately after
`watch_channel` so the node starts with a consistent state.

AI tools were used in preparing this commit.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ldk-reviews-bot
Copy link

👋 Hi! I see this is a draft PR.
I'll wait to assign reviewers until you mark it as ready for review.
Just convert it out of draft status when you're ready for review!

@codecov
Copy link

codecov bot commented Mar 6, 2026

Codecov Report

❌ Patch coverage is 92.94404% with 29 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.10%. Comparing base (90b79e4) to head (51afc25).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
lightning/src/chain/chainmonitor.rs 90.22% 22 Missing and 4 partials ⚠️
lightning/src/util/test_utils.rs 92.85% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4465      +/-   ##
==========================================
+ Coverage   85.97%   86.10%   +0.13%     
==========================================
  Files         159      159              
  Lines      104722   105410     +688     
  Branches   104722   105410     +688     
==========================================
+ Hits        90030    90765     +735     
+ Misses      12191    12129      -62     
- Partials     2501     2516      +15     
Flag Coverage Δ
tests 86.10% <92.94%> (+0.13%) ⬆️

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.

Sign up for free to 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