-
Notifications
You must be signed in to change notification settings - Fork 449
Persistent MonitorEvents
#4491
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
valentinewallace
wants to merge
20
commits into
lightningdevkit:main
Choose a base branch
from
valentinewallace:2026-03-persistent-mon-evs
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
Persistent MonitorEvents
#4491
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
2a55a26
Split up some tests that have many variants
valentinewallace 0430d7e
Remove unnecessary pending_monitor_events clone
valentinewallace e3b1672
Add persistent_monitor_events flag to monitors/manager
valentinewallace 76a984b
Add helper to push monitor events
valentinewallace 5259907
Rename pending_monitor_events to _legacy
valentinewallace 48438df
Add chain::Watch ack_monitor_event API
valentinewallace 868fd0c
Add monitor event ids
valentinewallace ac5b59a
Add MonitorUpdateCompletionAction::AckMonitorEvents
valentinewallace 1229514
Ack all monitor events besides HTLCUpdate
valentinewallace 4af1b4c
Fix replay of monitor events for outbounds
valentinewallace 26522cc
Fix replay of preimage monitor events for fwds
valentinewallace 001fb53
Track monitor event id in HTLCForwardInfo::Fail*
valentinewallace d2a9e09
Add monitor_event_source to holding cell HTLC fails
valentinewallace b8bb43b
Pipe monitor event source HTLCForwardInfo -> holding cell htlcs
valentinewallace 9a9716d
Ack monitor events on check_free_peer_holding_cells
valentinewallace 0f1a4a0
Ack monitor events on revoke_and_ack
valentinewallace 1896811
Ack monitor event when inbound edge is closed
valentinewallace 147388e
Ack monitor events when failing-to-fail-backwards
valentinewallace a34f379
Ack monitor events from blocked updates in Channel
valentinewallace 28357d8
Support persistent monitor events
valentinewallace 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
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 |
|---|---|---|
|
|
@@ -67,6 +67,21 @@ use core::iter::Cycle; | |
| use core::ops::Deref; | ||
| use core::sync::atomic::{AtomicUsize, Ordering}; | ||
|
|
||
| /// Identifies the source of a [`MonitorEvent`] for acknowledgment via | ||
| /// [`chain::Watch::ack_monitor_event`] once the event has been processed. | ||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| pub struct MonitorEventSource { | ||
| /// The event ID assigned by the [`ChannelMonitor`]. | ||
| pub event_id: u64, | ||
| /// The channel from which the [`MonitorEvent`] originated. | ||
| pub channel_id: ChannelId, | ||
| } | ||
|
|
||
| impl_writeable_tlv_based!(MonitorEventSource, { | ||
| (1, event_id, required), | ||
| (3, channel_id, required), | ||
| }); | ||
|
|
||
| /// A pending operation queued for later execution when `ChainMonitor` is in deferred mode. | ||
| enum PendingMonitorOp<ChannelSigner: EcdsaChannelSigner> { | ||
| /// A new monitor to insert and persist. | ||
|
|
@@ -367,9 +382,6 @@ pub struct ChainMonitor< | |
| fee_estimator: F, | ||
| persister: P, | ||
| _entropy_source: ES, | ||
| /// "User-provided" (ie persistence-completion/-failed) [`MonitorEvent`]s. These came directly | ||
| /// from the user and not from a [`ChannelMonitor`]. | ||
| pending_monitor_events: Mutex<Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, PublicKey)>>, | ||
| /// The best block height seen, used as a proxy for the passage of time. | ||
| highest_chain_height: AtomicUsize, | ||
|
|
||
|
|
@@ -437,7 +449,6 @@ where | |
| logger, | ||
| fee_estimator: feeest, | ||
| _entropy_source, | ||
| pending_monitor_events: Mutex::new(Vec::new()), | ||
| highest_chain_height: AtomicUsize::new(0), | ||
| event_notifier: Arc::clone(&event_notifier), | ||
| persister: AsyncPersister { persister, event_notifier }, | ||
|
|
@@ -656,7 +667,6 @@ where | |
| fee_estimator: feeest, | ||
| persister, | ||
| _entropy_source, | ||
| pending_monitor_events: Mutex::new(Vec::new()), | ||
| highest_chain_height: AtomicUsize::new(0), | ||
| event_notifier: Arc::new(Notifier::new()), | ||
| pending_send_only_events: Mutex::new(Vec::new()), | ||
|
|
@@ -801,16 +811,11 @@ where | |
| return Ok(()); | ||
| } | ||
| let funding_txo = monitor_data.monitor.get_funding_txo(); | ||
| self.pending_monitor_events.lock().unwrap().push(( | ||
| monitor_data.monitor.push_monitor_event(MonitorEvent::Completed { | ||
| funding_txo, | ||
| channel_id, | ||
| vec![MonitorEvent::Completed { | ||
| funding_txo, | ||
| channel_id, | ||
| monitor_update_id: monitor_data.monitor.get_latest_update_id(), | ||
| }], | ||
| monitor_data.monitor.get_counterparty_node_id(), | ||
| )); | ||
| monitor_update_id: monitor_data.monitor.get_latest_update_id(), | ||
| }); | ||
|
|
||
| self.event_notifier.notify(); | ||
| Ok(()) | ||
|
|
@@ -823,14 +828,11 @@ where | |
| pub fn force_channel_monitor_updated(&self, channel_id: ChannelId, monitor_update_id: u64) { | ||
| let monitors = self.monitors.read().unwrap(); | ||
| let monitor = &monitors.get(&channel_id).unwrap().monitor; | ||
| let counterparty_node_id = monitor.get_counterparty_node_id(); | ||
| let funding_txo = monitor.get_funding_txo(); | ||
| self.pending_monitor_events.lock().unwrap().push(( | ||
| funding_txo, | ||
| monitor.push_monitor_event(MonitorEvent::Completed { | ||
| funding_txo: monitor.get_funding_txo(), | ||
| channel_id, | ||
| vec![MonitorEvent::Completed { funding_txo, channel_id, monitor_update_id }], | ||
| counterparty_node_id, | ||
| )); | ||
| monitor_update_id, | ||
| }); | ||
| self.event_notifier.notify(); | ||
| } | ||
|
|
||
|
|
@@ -1265,21 +1267,13 @@ where | |
| // The channel is post-close (funding spend seen, lockdown, or | ||
| // holder tx signed). Return InProgress so ChannelManager freezes | ||
| // the channel until the force-close MonitorEvents are processed. | ||
| // Push a Completed event into pending_monitor_events so it gets | ||
| // picked up after the per-monitor events in the next | ||
| // release_pending_monitor_events call. | ||
| let funding_txo = monitor.get_funding_txo(); | ||
| let channel_id = monitor.channel_id(); | ||
| self.pending_monitor_events.lock().unwrap().push(( | ||
| funding_txo, | ||
| channel_id, | ||
| vec![MonitorEvent::Completed { | ||
| funding_txo, | ||
| channel_id, | ||
| monitor_update_id: monitor.get_latest_update_id(), | ||
| }], | ||
| monitor.get_counterparty_node_id(), | ||
| )); | ||
| // Push a Completed event into the monitor so it gets picked up | ||
| // in the next release_pending_monitor_events call. | ||
| monitor.push_monitor_event(MonitorEvent::Completed { | ||
| funding_txo: monitor.get_funding_txo(), | ||
| channel_id: monitor.channel_id(), | ||
| monitor_update_id: monitor.get_latest_update_id(), | ||
| }); | ||
| log_debug!( | ||
| logger, | ||
| "Deferring completion of ChannelMonitorUpdate id {:?} (channel is post-close)", | ||
|
|
@@ -1644,7 +1638,7 @@ where | |
|
|
||
| fn release_pending_monitor_events( | ||
| &self, | ||
| ) -> Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, PublicKey)> { | ||
| ) -> Vec<(OutPoint, ChannelId, Vec<(u64, MonitorEvent)>, PublicKey)> { | ||
| for (channel_id, update_id) in self.persister.get_and_clear_completed_updates() { | ||
| let _ = self.channel_monitor_updated(channel_id, update_id); | ||
| } | ||
|
|
@@ -1664,12 +1658,15 @@ where | |
| )); | ||
| } | ||
| } | ||
| // Drain pending_monitor_events (which includes deferred post-close | ||
| // completions) after per-monitor events so that force-close | ||
| // MonitorEvents are processed by ChannelManager first. | ||
| pending_monitor_events.extend(self.pending_monitor_events.lock().unwrap().split_off(0)); | ||
| pending_monitor_events | ||
| } | ||
|
|
||
| fn ack_monitor_event(&self, source: MonitorEventSource) { | ||
| let monitors = self.monitors.read().unwrap(); | ||
| if let Some(monitor_state) = monitors.get(&source.channel_id) { | ||
|
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. Do we expect to hit the case where we can't find the monitor for the |
||
| monitor_state.monitor.ack_monitor_event(source.event_id); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl< | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.