@@ -1275,7 +1275,11 @@ enum BackgroundEvent {
12751275 /// Some [`ChannelMonitorUpdate`] (s) completed before we were serialized but we still have
12761276 /// them marked pending, thus we need to run any [`MonitorUpdateCompletionAction`] (s) pending
12771277 /// on a channel.
1278- MonitorUpdatesComplete { counterparty_node_id: PublicKey, channel_id: ChannelId },
1278+ MonitorUpdatesComplete {
1279+ counterparty_node_id: PublicKey,
1280+ channel_id: ChannelId,
1281+ highest_update_id_completed: u64,
1282+ },
12791283}
12801284
12811285/// A pointer to a channel that is unblocked when an event is surfaced
@@ -8025,9 +8029,11 @@ where
80258029 /// Free the background events, generally called from [`PersistenceNotifierGuard`] constructors.
80268030 ///
80278031 /// Expects the caller to have a total_consistency_lock read lock.
8028- #[rustfmt::skip]
80298032 fn process_background_events(&self) -> NotifyOption {
8030- debug_assert_ne!(self.total_consistency_lock.held_by_thread(), LockHeldState::NotHeldByThread);
8033+ debug_assert_ne!(
8034+ self.total_consistency_lock.held_by_thread(),
8035+ LockHeldState::NotHeldByThread
8036+ );
80318037
80328038 self.background_events_processed_since_startup.store(true, Ordering::Release);
80338039
@@ -8039,11 +8045,34 @@ where
80398045
80408046 for event in background_events.drain(..) {
80418047 match event {
8042- BackgroundEvent::MonitorUpdateRegeneratedOnStartup { counterparty_node_id, funding_txo, channel_id, update } => {
8043- self.apply_post_close_monitor_update(counterparty_node_id, channel_id, funding_txo, update);
8048+ BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
8049+ counterparty_node_id,
8050+ funding_txo,
8051+ channel_id,
8052+ update,
8053+ } => {
8054+ self.apply_post_close_monitor_update(
8055+ counterparty_node_id,
8056+ channel_id,
8057+ funding_txo,
8058+ update,
8059+ );
80448060 },
8045- BackgroundEvent::MonitorUpdatesComplete { counterparty_node_id, channel_id } => {
8046- self.channel_monitor_updated(&channel_id, None, &counterparty_node_id);
8061+ BackgroundEvent::MonitorUpdatesComplete {
8062+ counterparty_node_id,
8063+ channel_id,
8064+ highest_update_id_completed,
8065+ } => {
8066+ // Now that we can finally handle the background event, remove all in-flight
8067+ // monitor updates for this channel that we've known to complete, as they have
8068+ // already been persisted to the monitor and can be applied to our internal
8069+ // state such that the channel resumes operation if no new updates have been
8070+ // made since.
8071+ self.channel_monitor_updated(
8072+ &channel_id,
8073+ Some(highest_update_id_completed),
8074+ &counterparty_node_id,
8075+ );
80478076 },
80488077 }
80498078 }
@@ -17224,39 +17253,58 @@ where
1722417253 ($counterparty_node_id: expr, $chan_in_flight_upds: expr, $monitor: expr,
1722517254 $peer_state: expr, $logger: expr, $channel_info_log: expr
1722617255 ) => { {
17256+ // When all in-flight updates have completed after we were last serialized, we
17257+ // need to remove them. However, we can't guarantee that the next serialization
17258+ // will have happened after processing the
17259+ // `BackgroundEvent::MonitorUpdatesComplete`, so removing them now could lead to the
17260+ // channel never being resumed as the event would not be regenerated after another
17261+ // reload. At the same time, we don't want to resume the channel now because there
17262+ // may be post-update actions to handle. Therefore, we're forced to keep tracking
17263+ // the completed in-flight updates (but only when they have all completed) until we
17264+ // are processing the `BackgroundEvent::MonitorUpdatesComplete`.
1722717265 let mut max_in_flight_update_id = 0;
17228- let starting_len = $chan_in_flight_upds.len();
17229- $chan_in_flight_upds.retain(|upd| upd.update_id > $monitor.get_latest_update_id());
17230- if $chan_in_flight_upds.len() < starting_len {
17266+ let num_updates_completed = $chan_in_flight_upds
17267+ .iter()
17268+ .filter(|update| {
17269+ max_in_flight_update_id = cmp::max(max_in_flight_update_id, update.update_id);
17270+ update.update_id <= $monitor.get_latest_update_id()
17271+ })
17272+ .count();
17273+ if num_updates_completed > 0 {
1723117274 log_debug!(
1723217275 $logger,
1723317276 "{} ChannelMonitorUpdates completed after ChannelManager was last serialized",
17234- starting_len - $chan_in_flight_upds.len()
17277+ num_updates_completed,
1723517278 );
1723617279 }
17280+ let all_updates_completed = num_updates_completed == $chan_in_flight_upds.len();
17281+
1723717282 let funding_txo = $monitor.get_funding_txo();
17238- for update in $chan_in_flight_upds.iter() {
17239- log_debug!($logger, "Replaying ChannelMonitorUpdate {} for {}channel {}",
17240- update.update_id, $channel_info_log, &$monitor.channel_id());
17241- max_in_flight_update_id = cmp::max(max_in_flight_update_id, update.update_id);
17242- pending_background_events.push(
17243- BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
17244- counterparty_node_id: $counterparty_node_id,
17245- funding_txo: funding_txo,
17246- channel_id: $monitor.channel_id(),
17247- update: update.clone(),
17248- });
17249- }
17250- if $chan_in_flight_upds.is_empty() {
17251- // We had some updates to apply, but it turns out they had completed before we
17252- // were serialized, we just weren't notified of that. Thus, we may have to run
17253- // the completion actions for any monitor updates, but otherwise are done.
17283+ if all_updates_completed {
17284+ log_debug!($logger, "All monitor updates completed since the ChannelManager was last serialized");
1725417285 pending_background_events.push(
1725517286 BackgroundEvent::MonitorUpdatesComplete {
1725617287 counterparty_node_id: $counterparty_node_id,
1725717288 channel_id: $monitor.channel_id(),
17289+ highest_update_id_completed: max_in_flight_update_id,
1725817290 });
1725917291 } else {
17292+ $chan_in_flight_upds.retain(|update| {
17293+ let replay = update.update_id > $monitor.get_latest_update_id();
17294+ if replay {
17295+ log_debug!($logger, "Replaying ChannelMonitorUpdate {} for {}channel {}",
17296+ update.update_id, $channel_info_log, &$monitor.channel_id());
17297+ pending_background_events.push(
17298+ BackgroundEvent::MonitorUpdateRegeneratedOnStartup {
17299+ counterparty_node_id: $counterparty_node_id,
17300+ funding_txo: funding_txo,
17301+ channel_id: $monitor.channel_id(),
17302+ update: update.clone(),
17303+ }
17304+ );
17305+ }
17306+ replay
17307+ });
1726017308 $peer_state.closed_channel_monitor_update_ids.entry($monitor.channel_id())
1726117309 .and_modify(|v| *v = cmp::max(max_in_flight_update_id, *v))
1726217310 .or_insert(max_in_flight_update_id);
0 commit comments