-
Notifications
You must be signed in to change notification settings - Fork 432
Defer ChainMonitor updates and persistence to flush() #4345
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
base: main
Are you sure you want to change the base?
Defer ChainMonitor updates and persistence to flush() #4345
Conversation
This commit changes `ChainMonitor` to queue monitor operations instead of applying them immediately. When `watch_channel` or `update_channel` is called, the operation is added to a pending queue and `ChannelMonitorUpdateStatus::InProgress` is returned. The actual monitor insertion/update and persistence happens when `flush()` is called. This enables proper ordering of persistence: the `ChannelManager` can be persisted first, then `flush()` is called to persist the monitors. This ensures that on restart, the `ChannelManager` state is never ahead of the monitor state. The background processor is updated to: 1. Capture `pending_monitor_operation_count()` before persisting ChannelManager 2. After ChannelManager persistence completes, call `flush()` with that count 3. On shutdown, flush all remaining pending operations after final persistence Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
👋 Hi! I see this is a draft PR. |
Move the deferred persistence logic from ChainMonitor into a separate DeferredChainMonitor wrapper type. This keeps ChainMonitor focused on its core responsibilities while the wrapper handles the queuing of monitor operations and their batched persistence via flush(). The wrapper intercepts watch_channel and update_channel calls, queues them, and returns InProgress. When flush() is called, it processes the queued operations and persists them in the correct order after ChannelManager persistence. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Added a DeferredChainMonitor wrapper instead of modifying ChainMonitor directly. The wrapper intercepts watch_channel and update_channel calls, queues them, and returns InProgress. When flush is called, it processes the queued operations and persists them in the correct order after ChannelManager persistence. This approach keeps ChainMonitor unchanged so that existing tests which expect synchronous behavior continue to work without modification. Only the background processor and production code paths use the deferred wrapper while the test suite can keep using ChainMonitor directly. |
Tests that DeferredChainMonitor properly defers watch_channel and update_channel operations until flush() is called, using real ChannelManagers and a complete channel open + payment flow between two nodes both using DeferredChainMonitor. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This adds a new generic parameter M to DeferredChainMonitor that allows passing any type implementing Deref to ChainMonitor, such as Arc or references, rather than requiring ownership of the ChainMonitor. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Introduce DeferredPersistence trait that abstracts the deferred persistence methods (pending_operation_count, flush). This allows process_events_async to accept any chain monitor wrapper that implements the trait, rather than being constrained to DeferredChainMonitor<..., Arc<ChainMonitor<...>>>. The relaxed bounds enable using DeferredChainMonitor with inner monitor types other than Arc<ChainMonitor>, resolving lifetime issues that could occur with non-Arc wrapper types. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This PR changes ChainMonitor to defer monitor operations by queuing them in watch_channel and update_channel instead of immediately persisting. The actual persistence happens when the new flush() method is called. This enables correct persistence ordering where the ChannelManager is always persisted before its associated monitor updates, ensuring that on restart the manager state is never ahead of the monitor state. The background processor is updated to capture the pending operation count before persisting the ChannelManager, then flush only those operations afterward.