|
| 1 | +use std::sync::atomic::{AtomicU64, Ordering}; |
| 2 | +use tracing::{error, warn}; |
| 3 | + |
| 4 | +/// Emergency circuit breaker that halts the node if a rollback is detected. |
| 5 | +/// This is the last line of defense against state corruption. |
| 6 | +pub struct CircuitBreaker { |
| 7 | + /// Last finalized round seen |
| 8 | + last_finalized: AtomicU64, |
| 9 | + /// Whether circuit breaker is enabled |
| 10 | + enabled: bool, |
| 11 | +} |
| 12 | + |
| 13 | +impl CircuitBreaker { |
| 14 | + /// Create a new circuit breaker |
| 15 | + pub fn new(enabled: bool) -> Self { |
| 16 | + Self { |
| 17 | + last_finalized: AtomicU64::new(0), |
| 18 | + enabled, |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + /// Check if round is moving forward |
| 23 | + /// HALTS THE PROCESS if rollback detected |
| 24 | + pub fn check_finality(&self, current_round: u64) { |
| 25 | + if !self.enabled { |
| 26 | + // When disabled, still track but don't enforce |
| 27 | + self.last_finalized.store(current_round, Ordering::SeqCst); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + let last = self.last_finalized.load(Ordering::SeqCst); |
| 32 | + |
| 33 | + if current_round < last { |
| 34 | + // CRITICAL: ROLLBACK DETECTED |
| 35 | + error!("╔═══════════════════════════════════════════════════════╗"); |
| 36 | + error!("║ 🚨 EMERGENCY CIRCUIT BREAKER 🚨 ║"); |
| 37 | + error!("║ ROLLBACK DETECTED - HALTING NODE ║"); |
| 38 | + error!("╚═══════════════════════════════════════════════════════╝"); |
| 39 | + error!(""); |
| 40 | + error!("Last finalized round: {}", last); |
| 41 | + error!("Current round: {}", current_round); |
| 42 | + error!("Rollback amount: {} rounds", last - current_round); |
| 43 | + error!(""); |
| 44 | + error!("This indicates a critical consensus failure."); |
| 45 | + error!("The node is halting to prevent state corruption."); |
| 46 | + error!(""); |
| 47 | + error!("MANUAL INTERVENTION REQUIRED:"); |
| 48 | + error!("1. Check all validator logs"); |
| 49 | + error!("2. Verify network state with other operators"); |
| 50 | + error!("3. Determine root cause"); |
| 51 | + error!("4. Coordinate recovery plan"); |
| 52 | + error!(""); |
| 53 | + error!("DO NOT RESTART without understanding the cause."); |
| 54 | + error!(""); |
| 55 | + error!("Exit code 100 = circuit breaker triggered"); |
| 56 | + |
| 57 | + // HALT THE PROCESS |
| 58 | + std::process::exit(100); |
| 59 | + } |
| 60 | + |
| 61 | + // Update last finalized |
| 62 | + self.last_finalized.store(current_round, Ordering::SeqCst); |
| 63 | + } |
| 64 | + |
| 65 | + /// Check if round is advancing too slowly (possible stall) |
| 66 | + pub fn check_liveness(&self, current_round: u64, max_lag: u64) { |
| 67 | + if !self.enabled { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + let last = self.last_finalized.load(Ordering::SeqCst); |
| 72 | + |
| 73 | + if last > 0 && current_round == last { |
| 74 | + // No progress - this is checked elsewhere |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // Check for large gaps (possible network partition) |
| 79 | + if current_round > last + max_lag { |
| 80 | + warn!("⚠️ Large finality gap detected: {} rounds", current_round - last); |
| 81 | + warn!("Possible network partition or synchronization issue"); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /// Get the last finalized round |
| 86 | + pub fn last_finalized(&self) -> u64 { |
| 87 | + self.last_finalized.load(Ordering::SeqCst) |
| 88 | + } |
| 89 | + |
| 90 | + /// Check if enabled |
| 91 | + pub fn is_enabled(&self) -> bool { |
| 92 | + self.enabled |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl Default for CircuitBreaker { |
| 97 | + fn default() -> Self { |
| 98 | + Self::new(true) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#[cfg(test)] |
| 103 | +mod tests { |
| 104 | + use super::*; |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn test_circuit_breaker_allows_forward() { |
| 108 | + let cb = CircuitBreaker::new(true); |
| 109 | + |
| 110 | + cb.check_finality(1); |
| 111 | + cb.check_finality(2); |
| 112 | + cb.check_finality(3); |
| 113 | + |
| 114 | + assert_eq!(cb.last_finalized(), 3); |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn test_circuit_breaker_allows_same() { |
| 119 | + let cb = CircuitBreaker::new(true); |
| 120 | + |
| 121 | + cb.check_finality(5); |
| 122 | + cb.check_finality(5); |
| 123 | + |
| 124 | + assert_eq!(cb.last_finalized(), 5); |
| 125 | + } |
| 126 | + |
| 127 | + #[test] |
| 128 | + fn test_circuit_breaker_disabled_allows_backward() { |
| 129 | + // When disabled, circuit breaker allows backward movement |
| 130 | + let cb = CircuitBreaker::new(false); |
| 131 | + |
| 132 | + cb.check_finality(10); |
| 133 | + cb.check_finality(5); // Would halt if enabled, but disabled so OK |
| 134 | + |
| 135 | + assert_eq!(cb.last_finalized(), 5); |
| 136 | + } |
| 137 | + |
| 138 | + #[test] |
| 139 | + fn test_liveness_check() { |
| 140 | + let cb = CircuitBreaker::new(true); |
| 141 | + |
| 142 | + cb.check_finality(100); |
| 143 | + cb.check_liveness(1100, 100); // 1000 round gap - should warn but not halt |
| 144 | + |
| 145 | + // Test passes if no panic |
| 146 | + } |
| 147 | + |
| 148 | + #[test] |
| 149 | + fn test_is_enabled() { |
| 150 | + let cb_enabled = CircuitBreaker::new(true); |
| 151 | + let cb_disabled = CircuitBreaker::new(false); |
| 152 | + |
| 153 | + assert!(cb_enabled.is_enabled()); |
| 154 | + assert!(!cb_disabled.is_enabled()); |
| 155 | + } |
| 156 | + |
| 157 | + // Note: Cannot test actual rollback halt in unit tests as it calls std::process::exit(100) |
| 158 | + // This must be tested in integration tests or manually |
| 159 | +} |
0 commit comments