|
| 1 | +//! Filter re-checking infrastructure |
| 2 | +//! |
| 3 | +//! When gap limits change during block processing, we need to re-check compact filters |
| 4 | +//! with the new set of addresses. This module provides the infrastructure to track |
| 5 | +//! which filters need re-checking and manage the re-check iterations. |
| 6 | +
|
| 7 | +use std::collections::VecDeque; |
| 8 | + |
| 9 | +/// Configuration for filter re-checking behavior |
| 10 | +#[derive(Debug, Clone)] |
| 11 | +pub struct FilterRecheckConfig { |
| 12 | + /// Whether filter re-checking is enabled |
| 13 | + pub enabled: bool, |
| 14 | + /// Maximum number of re-check iterations to prevent infinite loops |
| 15 | + pub max_iterations: u32, |
| 16 | +} |
| 17 | + |
| 18 | +impl Default for FilterRecheckConfig { |
| 19 | + fn default() -> Self { |
| 20 | + Self { |
| 21 | + enabled: true, |
| 22 | + max_iterations: 10, |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/// Represents a range of block heights that need filter re-checking |
| 28 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 29 | +pub struct RecheckRange { |
| 30 | + /// Starting height (inclusive) |
| 31 | + pub start: u32, |
| 32 | + /// Ending height (inclusive) |
| 33 | + pub end: u32, |
| 34 | + /// Which iteration this is (for loop detection) |
| 35 | + pub iteration: u32, |
| 36 | +} |
| 37 | + |
| 38 | +impl RecheckRange { |
| 39 | + /// Create a new recheck range |
| 40 | + pub fn new(start: u32, end: u32, iteration: u32) -> Self { |
| 41 | + Self { |
| 42 | + start, |
| 43 | + end, |
| 44 | + iteration, |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// Check if this range contains a height |
| 49 | + pub fn contains(&self, height: u32) -> bool { |
| 50 | + height >= self.start && height <= self.end |
| 51 | + } |
| 52 | + |
| 53 | + /// Get the number of blocks in this range |
| 54 | + pub fn len(&self) -> u32 { |
| 55 | + self.end.saturating_sub(self.start).saturating_add(1) |
| 56 | + } |
| 57 | + |
| 58 | + /// Check if the range is empty |
| 59 | + pub fn is_empty(&self) -> bool { |
| 60 | + self.end < self.start |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// Queue for managing filter re-check operations |
| 65 | +#[derive(Debug)] |
| 66 | +pub struct FilterRecheckQueue { |
| 67 | + /// Queue of ranges that need re-checking |
| 68 | + pending_ranges: VecDeque<RecheckRange>, |
| 69 | + /// Configuration |
| 70 | + config: FilterRecheckConfig, |
| 71 | + /// Total number of ranges added (for statistics) |
| 72 | + total_ranges_added: u64, |
| 73 | + /// Total number of ranges completed (for statistics) |
| 74 | + total_ranges_completed: u64, |
| 75 | +} |
| 76 | + |
| 77 | +impl FilterRecheckQueue { |
| 78 | + /// Create a new filter recheck queue |
| 79 | + pub fn new(config: FilterRecheckConfig) -> Self { |
| 80 | + Self { |
| 81 | + pending_ranges: VecDeque::new(), |
| 82 | + config, |
| 83 | + total_ranges_added: 0, |
| 84 | + total_ranges_completed: 0, |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /// Add a range to be re-checked |
| 89 | + /// |
| 90 | + /// Returns Ok(()) if the range was added, or Err with a message if it was rejected |
| 91 | + /// (e.g., due to exceeding max iterations) |
| 92 | + pub fn add_range(&mut self, start: u32, end: u32, iteration: u32) -> Result<(), String> { |
| 93 | + if !self.config.enabled { |
| 94 | + return Err("Filter re-checking is disabled".to_string()); |
| 95 | + } |
| 96 | + |
| 97 | + if iteration >= self.config.max_iterations { |
| 98 | + return Err(format!( |
| 99 | + "Maximum re-check iterations ({}) exceeded for range {}-{}", |
| 100 | + self.config.max_iterations, start, end |
| 101 | + )); |
| 102 | + } |
| 103 | + |
| 104 | + let range = RecheckRange::new(start, end, iteration); |
| 105 | + |
| 106 | + // Check if we already have this range queued |
| 107 | + if self.pending_ranges.iter().any(|r| r.start == start && r.end == end) { |
| 108 | + tracing::debug!("Range {}-{} already queued for re-check, skipping", start, end); |
| 109 | + return Ok(()); |
| 110 | + } |
| 111 | + |
| 112 | + tracing::info!( |
| 113 | + "📋 Queuing filter re-check for heights {}-{} (iteration {}/{})", |
| 114 | + start, |
| 115 | + end, |
| 116 | + iteration + 1, |
| 117 | + self.config.max_iterations |
| 118 | + ); |
| 119 | + |
| 120 | + self.pending_ranges.push_back(range); |
| 121 | + self.total_ranges_added += 1; |
| 122 | + Ok(()) |
| 123 | + } |
| 124 | + |
| 125 | + /// Get the next range to re-check |
| 126 | + pub fn next_range(&mut self) -> Option<RecheckRange> { |
| 127 | + self.pending_ranges.pop_front() |
| 128 | + } |
| 129 | + |
| 130 | + /// Mark a range as completed |
| 131 | + pub fn mark_completed(&mut self, _range: &RecheckRange) { |
| 132 | + self.total_ranges_completed += 1; |
| 133 | + } |
| 134 | + |
| 135 | + /// Check if there are any pending re-checks |
| 136 | + pub fn has_pending(&self) -> bool { |
| 137 | + !self.pending_ranges.is_empty() |
| 138 | + } |
| 139 | + |
| 140 | + /// Get the number of pending ranges |
| 141 | + pub fn pending_count(&self) -> usize { |
| 142 | + self.pending_ranges.len() |
| 143 | + } |
| 144 | + |
| 145 | + /// Clear all pending ranges |
| 146 | + pub fn clear(&mut self) { |
| 147 | + self.pending_ranges.clear(); |
| 148 | + } |
| 149 | + |
| 150 | + /// Get statistics about re-check operations |
| 151 | + pub fn stats(&self) -> RecheckStats { |
| 152 | + RecheckStats { |
| 153 | + pending_ranges: self.pending_ranges.len(), |
| 154 | + total_added: self.total_ranges_added, |
| 155 | + total_completed: self.total_ranges_completed, |
| 156 | + config: self.config.clone(), |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /// Check if re-checking is enabled |
| 161 | + pub fn is_enabled(&self) -> bool { |
| 162 | + self.config.enabled |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +/// Statistics about filter re-check operations |
| 167 | +#[derive(Debug, Clone)] |
| 168 | +pub struct RecheckStats { |
| 169 | + /// Number of ranges currently pending |
| 170 | + pub pending_ranges: usize, |
| 171 | + /// Total ranges added since creation |
| 172 | + pub total_added: u64, |
| 173 | + /// Total ranges completed |
| 174 | + pub total_completed: u64, |
| 175 | + /// Configuration |
| 176 | + pub config: FilterRecheckConfig, |
| 177 | +} |
| 178 | + |
| 179 | +#[cfg(test)] |
| 180 | +mod tests { |
| 181 | + use super::*; |
| 182 | + |
| 183 | + #[test] |
| 184 | + fn test_recheck_range_basic() { |
| 185 | + let range = RecheckRange::new(100, 200, 0); |
| 186 | + assert_eq!(range.start, 100); |
| 187 | + assert_eq!(range.end, 200); |
| 188 | + assert_eq!(range.iteration, 0); |
| 189 | + assert_eq!(range.len(), 101); |
| 190 | + assert!(!range.is_empty()); |
| 191 | + } |
| 192 | + |
| 193 | + #[test] |
| 194 | + fn test_recheck_range_contains() { |
| 195 | + let range = RecheckRange::new(100, 200, 0); |
| 196 | + assert!(!range.contains(99)); |
| 197 | + assert!(range.contains(100)); |
| 198 | + assert!(range.contains(150)); |
| 199 | + assert!(range.contains(200)); |
| 200 | + assert!(!range.contains(201)); |
| 201 | + } |
| 202 | + |
| 203 | + #[test] |
| 204 | + fn test_recheck_queue_add_and_retrieve() { |
| 205 | + let mut queue = FilterRecheckQueue::new(FilterRecheckConfig::default()); |
| 206 | + |
| 207 | + // Add a range |
| 208 | + assert!(queue.add_range(100, 200, 0).is_ok()); |
| 209 | + assert_eq!(queue.pending_count(), 1); |
| 210 | + assert!(queue.has_pending()); |
| 211 | + |
| 212 | + // Retrieve it |
| 213 | + let range = queue.next_range().unwrap(); |
| 214 | + assert_eq!(range.start, 100); |
| 215 | + assert_eq!(range.end, 200); |
| 216 | + assert_eq!(queue.pending_count(), 0); |
| 217 | + assert!(!queue.has_pending()); |
| 218 | + } |
| 219 | + |
| 220 | + #[test] |
| 221 | + fn test_recheck_queue_max_iterations() { |
| 222 | + let config = FilterRecheckConfig { |
| 223 | + enabled: true, |
| 224 | + max_iterations: 3, |
| 225 | + }; |
| 226 | + let mut queue = FilterRecheckQueue::new(config); |
| 227 | + |
| 228 | + // These should succeed |
| 229 | + assert!(queue.add_range(100, 200, 0).is_ok()); |
| 230 | + assert!(queue.add_range(100, 200, 1).is_ok()); |
| 231 | + assert!(queue.add_range(100, 200, 2).is_ok()); |
| 232 | + |
| 233 | + // This should fail (iteration 3 >= max_iterations 3) |
| 234 | + assert!(queue.add_range(100, 200, 3).is_err()); |
| 235 | + } |
| 236 | + |
| 237 | + #[test] |
| 238 | + fn test_recheck_queue_disabled() { |
| 239 | + let config = FilterRecheckConfig { |
| 240 | + enabled: false, |
| 241 | + max_iterations: 10, |
| 242 | + }; |
| 243 | + let mut queue = FilterRecheckQueue::new(config); |
| 244 | + |
| 245 | + // Should fail when disabled |
| 246 | + assert!(queue.add_range(100, 200, 0).is_err()); |
| 247 | + } |
| 248 | + |
| 249 | + #[test] |
| 250 | + fn test_recheck_queue_duplicate_detection() { |
| 251 | + let mut queue = FilterRecheckQueue::new(FilterRecheckConfig::default()); |
| 252 | + |
| 253 | + // Add the same range twice |
| 254 | + assert!(queue.add_range(100, 200, 0).is_ok()); |
| 255 | + assert!(queue.add_range(100, 200, 0).is_ok()); // Should succeed but not add |
| 256 | + |
| 257 | + // Should only have one range |
| 258 | + assert_eq!(queue.pending_count(), 1); |
| 259 | + } |
| 260 | + |
| 261 | + #[test] |
| 262 | + fn test_recheck_queue_stats() { |
| 263 | + let mut queue = FilterRecheckQueue::new(FilterRecheckConfig::default()); |
| 264 | + |
| 265 | + queue.add_range(100, 200, 0).unwrap(); |
| 266 | + queue.add_range(201, 300, 0).unwrap(); |
| 267 | + |
| 268 | + let stats = queue.stats(); |
| 269 | + assert_eq!(stats.pending_ranges, 2); |
| 270 | + assert_eq!(stats.total_added, 2); |
| 271 | + assert_eq!(stats.total_completed, 0); |
| 272 | + |
| 273 | + // Complete one |
| 274 | + let range = queue.next_range().unwrap(); |
| 275 | + queue.mark_completed(&range); |
| 276 | + |
| 277 | + let stats = queue.stats(); |
| 278 | + assert_eq!(stats.pending_ranges, 1); |
| 279 | + assert_eq!(stats.total_completed, 1); |
| 280 | + } |
| 281 | + |
| 282 | + #[test] |
| 283 | + fn test_recheck_queue_clear() { |
| 284 | + let mut queue = FilterRecheckQueue::new(FilterRecheckConfig::default()); |
| 285 | + |
| 286 | + queue.add_range(100, 200, 0).unwrap(); |
| 287 | + queue.add_range(201, 300, 0).unwrap(); |
| 288 | + assert_eq!(queue.pending_count(), 2); |
| 289 | + |
| 290 | + queue.clear(); |
| 291 | + assert_eq!(queue.pending_count(), 0); |
| 292 | + assert!(!queue.has_pending()); |
| 293 | + } |
| 294 | +} |
0 commit comments