// SPDX-License-Identifier: MIT //! Fault-tolerance supervisor. //! //! The goal is to keep hashing with as many dead chips or chains as possible. Faults therefore //! escalate in steps (retry, disable chip, disable chain) and only irreversible conditions stop the //! machine. This module owns that ladder; it does not talk to hardware and it does not choose //! thermal limits, which come from the profile envelope. /// A single health observation from the mining loop. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum FaultEvent { /// A chain accepted work and reported nonces for it. NoncesReported { chain: u8, per_mille: u16 }, /// A chain returned an error for a frame, reset or work dispatch. ChainError { chain: u8 }, /// A chain produced nothing for a full observation window. ChainSilent { chain: u8 }, /// One chip on a chain failed its identification or stopped responding. ChipFault { chain: u8, chip: u16 }, /// A temperature reading left the profile envelope. TemperatureFault { zone: u8 }, } /// What the caller should do next. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum FaultAction { /// Keep hashing; nothing changed. None, /// Retry the chain before treating the fault as permanent. Retry { chain: u8 }, /// Keep the machine running without this chain. DisableChain { chain: u8 }, /// Keep the machine running without this chip. DisableChip { chain: u8, chip: u16 }, /// Stop hashing entirely; the reason is carried for the API. Stop { reason: StopReason }, } /// Why the machine stopped. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum StopReason { Temperature, NoChainsLeft, } /// Thresholds for escalation. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct FaultPolicy { /// Consecutive errors tolerated before the chain is disabled (retries included). pub retries: u8, /// Nonce-response ratio below which a chain is considered stalled. pub min_response_per_mille: u16, /// Consecutive weak windows before a stalled chain is disabled. pub weak_windows: u8, } impl Default for FaultPolicy { fn default() -> Self { Self { retries: 2, min_response_per_mille: 100, weak_windows: 3, } } } #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] struct ChainHealth { chain: u8, errors: u8, weak: u8, disabled: bool, } /// Escalation state for one machine. #[derive(Clone, Debug, Eq, PartialEq)] pub struct FaultSupervisor { policy: FaultPolicy, chains: Vec, stopped: Option, } impl FaultSupervisor { pub fn new(chains: &[u8], policy: FaultPolicy) -> Self { Self { policy, chains: chains .iter() .map(|&chain| ChainHealth { chain, ..ChainHealth::default() }) .collect(), stopped: None, } } pub fn is_stopped(&self) -> bool { self.stopped.is_some() } pub fn stop_reason(&self) -> Option { self.stopped } /// Chains the miner may still dispatch work to. pub fn active_chains(&self) -> Vec { self.chains .iter() .filter(|health| !health.disabled) .map(|health| health.chain) .collect() } /// Apply one observation and return the escalation step it implies. pub fn observe(&mut self, event: FaultEvent) -> FaultAction { if self.stopped.is_some() { return FaultAction::None; } match event { FaultEvent::NoncesReported { chain, per_mille } => { let min_response = self.policy.min_response_per_mille; let weak_windows = self.policy.weak_windows; let Some(health) = self.health_mut(chain) else { return FaultAction::None; }; health.errors = 0; if per_mille >= min_response { health.weak = 0; return FaultAction::None; } health.weak = health.weak.saturating_add(1); if health.weak >= weak_windows { health.disabled = true; self.maybe_stop(chain) } else { FaultAction::None } } FaultEvent::ChainError { chain } | FaultEvent::ChainSilent { chain } => { let policy = self.policy; let Some(health) = self.health_mut(chain) else { return FaultAction::None; }; if health.disabled { return FaultAction::None; } health.errors = health.errors.saturating_add(1); if health.errors > policy.retries { health.disabled = true; self.maybe_stop(chain) } else { FaultAction::Retry { chain } } } FaultEvent::ChipFault { chain, chip } => FaultAction::DisableChip { chain, chip }, FaultEvent::TemperatureFault { .. } => { self.stopped = Some(StopReason::Temperature); FaultAction::Stop { reason: StopReason::Temperature, } } } } fn health_mut(&mut self, chain: u8) -> Option<&mut ChainHealth> { self.chains.iter_mut().find(|health| health.chain == chain) } fn maybe_stop(&mut self, chain: u8) -> FaultAction { if self.chains.iter().all(|health| health.disabled) { self.stopped = Some(StopReason::NoChainsLeft); return FaultAction::Stop { reason: StopReason::NoChainsLeft, }; } FaultAction::DisableChain { chain } } } #[cfg(test)] mod tests { use super::*; fn supervisor() -> FaultSupervisor { FaultSupervisor::new(&[0, 1, 2], FaultPolicy::default()) } #[test] fn transient_errors_retry_then_disable_the_chain() { let mut faults = supervisor(); assert_eq!( faults.observe(FaultEvent::ChainError { chain: 1 }), FaultAction::Retry { chain: 1 } ); assert_eq!( faults.observe(FaultEvent::ChainError { chain: 1 }), FaultAction::Retry { chain: 1 } ); assert_eq!( faults.observe(FaultEvent::ChainSilent { chain: 1 }), FaultAction::DisableChain { chain: 1 } ); assert_eq!(faults.active_chains(), vec![0, 2]); } #[test] fn healthy_nonces_reset_the_error_count() { let mut faults = supervisor(); faults.observe(FaultEvent::ChainError { chain: 0 }); faults.observe(FaultEvent::ChainError { chain: 0 }); assert_eq!( faults.observe(FaultEvent::NoncesReported { chain: 0, per_mille: 900 }), FaultAction::None ); assert_eq!( faults.observe(FaultEvent::ChainError { chain: 0 }), FaultAction::Retry { chain: 0 } ); } #[test] fn a_stalled_chain_is_disabled_after_repeated_weak_windows() { let mut faults = supervisor(); for _ in 0..2 { assert_eq!( faults.observe(FaultEvent::NoncesReported { chain: 2, per_mille: 10 }), FaultAction::None ); } assert_eq!( faults.observe(FaultEvent::NoncesReported { chain: 2, per_mille: 10 }), FaultAction::DisableChain { chain: 2 } ); assert_eq!(faults.active_chains(), vec![0, 1]); } #[test] fn one_good_window_clears_weak_history() { let mut faults = supervisor(); faults.observe(FaultEvent::NoncesReported { chain: 0, per_mille: 10, }); faults.observe(FaultEvent::NoncesReported { chain: 0, per_mille: 10, }); faults.observe(FaultEvent::NoncesReported { chain: 0, per_mille: 900, }); faults.observe(FaultEvent::NoncesReported { chain: 0, per_mille: 10, }); faults.observe(FaultEvent::NoncesReported { chain: 0, per_mille: 10, }); assert_eq!( faults.observe(FaultEvent::NoncesReported { chain: 0, per_mille: 10 }), FaultAction::DisableChain { chain: 0 } ); } #[test] fn a_chip_fault_disables_only_that_chip() { let mut faults = supervisor(); assert_eq!( faults.observe(FaultEvent::ChipFault { chain: 1, chip: 42 }), FaultAction::DisableChip { chain: 1, chip: 42 } ); assert_eq!(faults.active_chains(), vec![0, 1, 2]); } #[test] fn temperature_fault_stops_the_machine() { let mut faults = supervisor(); assert_eq!( faults.observe(FaultEvent::TemperatureFault { zone: 2 }), FaultAction::Stop { reason: StopReason::Temperature } ); assert!(faults.is_stopped()); assert_eq!( faults.observe(FaultEvent::ChainError { chain: 0 }), FaultAction::None ); } #[test] fn losing_every_chain_stops_instead_of_hanging() { let mut faults = FaultSupervisor::new( &[0], FaultPolicy { retries: 0, ..FaultPolicy::default() }, ); assert_eq!( faults.observe(FaultEvent::ChainError { chain: 0 }), FaultAction::Stop { reason: StopReason::NoChainsLeft } ); assert_eq!(faults.active_chains(), Vec::::new()); } #[test] fn unknown_chains_are_ignored() { let mut faults = supervisor(); assert_eq!( faults.observe(FaultEvent::ChainError { chain: 9 }), FaultAction::None ); assert_eq!(faults.active_chains(), vec![0, 1, 2]); } }