- // Assess cleanup. Typically, we'll clean up only between the last two known full
- // monitors.
- if let Ok((_, old_monitor)) = maybe_old_monitor {
- let start = old_monitor.get_latest_update_id();
- let end = if monitor.get_latest_update_id() == CLOSED_CHANNEL_UPDATE_ID {
- // We don't want to clean the rest of u64, so just do possible pending
- // updates. Note that we never write updates at
- // `CLOSED_CHANNEL_UPDATE_ID`.
- cmp::min(
- start.saturating_add(self.maximum_pending_updates),
- CLOSED_CHANNEL_UPDATE_ID - 1,
- )
- } else {
- monitor.get_latest_update_id().saturating_sub(1)
- };
- // We should bother cleaning up only if there's at least one update
- // expected.
- for update_id in start..=end {
- let update_name = UpdateName::from(update_id);
- #[cfg(debug_assertions)]
- {
- if let Ok(update) =
- self.read_monitor_update(&monitor_name, &update_name)
- {
- // Assert that we are reading what we think we are.
- debug_assert_eq!(update.update_id, update_name.0);
- } else if update_id != start && monitor.get_latest_update_id() != CLOSED_CHANNEL_UPDATE_ID
- {
- // We're deleting something we should know doesn't exist.
- panic!(
- "failed to read monitor update {}",
- update_name.as_str()
- );
- }
- // On closed channels, we will unavoidably try to read
- // non-existent updates since we have to guess at the range of
- // stale updates, so do nothing.
- }
- if let Err(e) = self.kv_store.remove(
- CHANNEL_MONITOR_UPDATE_PERSISTENCE_PRIMARY_NAMESPACE,
- monitor_name.as_str(),
- update_name.as_str(),
- true,
- ) {
- log_error!(
- self.logger,
- "error cleaning up channel monitor updates for monitor {}, reason: {}",
- monitor_name.as_str(),
- e
- );
- };
- }
- };