From 367f07d956113071f5fa8ab7e8a28cd06ca9bd9c Mon Sep 17 00:00:00 2001 From: Noah <2+noah@noreply.gitea.bluefla.re> Date: Thu, 22 Oct 2026 10:11:52 -0600 Subject: [PATCH] Add hardware detection plan model Introduce the detection core data model: chain observations, board profile and PSU envelopes, envelope intersection, and a conservative operating point when the hardware is not confirmed by an EEPROM. Refusal is explicit for unknown chips, missing PSUs and empty envelopes, and degraded chains are reported rather than fatal. --- src/detect.rs | 345 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 2 files changed, 346 insertions(+) create mode 100644 src/detect.rs diff --git a/src/detect.rs b/src/detect.rs new file mode 100644 index 0000000..05e66c1 --- /dev/null +++ b/src/detect.rs @@ -0,0 +1,345 @@ +// SPDX-License-Identifier: MIT +//! Hardware detection model. +//! +//! The detection core decides what is physically attached to a control board and whether that +//! combination is safe to run. It never talks to a chain directly; backends report what they +//! probed, and this module turns that into a `HardwarePlan` the miner can execute. +//! +//! Identification deliberately does not require a hashboard EEPROM. The EEPROM, when present and +//! valid, confirms a board revision; a chain probe alone is enough to select a chip profile. + +/// Chip families the detection core can name. `Unknown` is a real answer, not a placeholder. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum ChipModel { + Bm1362, + Bm1366, + Bm1368, + Bm1370, + Bm1398, + Unknown, +} + +/// How a chain reports temperature. PIC boards proxy telemetry through the power MCU. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum SensorTopology { + Pic, + NoPic, + Unknown, +} + +/// State of a hashboard EEPROM. Absence is normal and must never block detection. +#[derive(Clone, Debug, Eq, PartialEq)] +pub enum EepromState { + Present { model: String }, + Absent, + Invalid, +} + +/// What a backend observed on one physical chain. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct ChainReport { + pub index: u8, + pub chip: ChipModel, + pub chips_found: u16, + pub eeprom: EepromState, + /// Nonce responses per mille of dispatched work, when the backend could measure it. + pub response_per_mille: Option, +} + +impl ChainReport { + pub fn is_identified(&self) -> bool { + self.chip != ChipModel::Unknown && self.chips_found > 0 + } +} + +/// Safe operating envelope for one hashboard/chip profile, from the profile database. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct Envelope { + pub v_min_mv: u16, + pub v_max_mv: u16, + pub f_min_mhz: u16, + pub f_max_mhz: u16, +} + +impl Envelope { + pub fn intersect(self, other: Envelope) -> Option { + let v_min_mv = self.v_min_mv.max(other.v_min_mv); + let v_max_mv = self.v_max_mv.min(other.v_max_mv); + let f_min_mhz = self.f_min_mhz.max(other.f_min_mhz); + let f_max_mhz = self.f_max_mhz.min(other.f_max_mhz); + if v_min_mv > v_max_mv || f_min_mhz > f_max_mhz { + return None; + } + Some(Envelope { + v_min_mv, + v_max_mv, + f_min_mhz, + f_max_mhz, + }) + } +} + +/// A hashboard profile from the database. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct BoardProfile { + pub name: String, + pub chip: ChipModel, + pub sensor: SensorTopology, + pub chains: u8, + pub envelope: Envelope, +} + +/// What the PSU reports about itself and what it can deliver. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct PsuCapability { + pub name: String, + pub envelope: Envelope, + pub max_dc_watts: u16, +} + +/// Which fans cool which thermal zones, learned by active probing. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct FanZone { + pub fan: u8, + pub zones: Vec, +} + +/// Why detection refused a combination. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum DetectionError { + NoChains, + UnknownChip, + ChipProfileMismatch, + PsuUnidentified, + EnvelopeEmpty, +} + +/// Operating point chosen for the machine. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct OperatingPoint { + pub voltage_mv: u16, + pub frequency_mhz: u16, + /// True when the profile was confirmed by an EEPROM or a full chain identification. + pub confirmed: bool, +} + +/// The result of detection: everything the miner needs and nothing it has to guess. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct HardwarePlan { + pub profile: String, + pub chip: ChipModel, + pub chains: Vec, + pub envelope: Envelope, + pub operating_point: OperatingPoint, + pub psu: String, + pub fan_zones: Vec, +} + +impl HardwarePlan { + /// Chains that identified but reported no nonces, or fewer than expected chips. + pub fn degraded_chains(&self, expected: u16) -> Vec { + self.chains + .iter() + .filter(|chain| { + chain.chips_found < expected + || chain.response_per_mille.is_some_and(|ratio| ratio == 0) + }) + .map(|chain| chain.index) + .collect() + } +} + +/// Turn observations into a plan, or refuse. +/// +/// Refusal is a feature: an unverified or out-of-envelope combination must not mine silently. +pub fn plan( + chains: &[ChainReport], + profile: &BoardProfile, + psu: Option<&PsuCapability>, + fan_zones: Vec, +) -> Result { + if chains.is_empty() { + return Err(DetectionError::NoChains); + } + if chains.iter().any(|chain| chain.chip == ChipModel::Unknown) { + return Err(DetectionError::UnknownChip); + } + if chains.iter().any(|chain| chain.chip != profile.chip) { + return Err(DetectionError::ChipProfileMismatch); + } + let psu = psu.ok_or(DetectionError::PsuUnidentified)?; + let envelope = profile + .envelope + .intersect(psu.envelope) + .ok_or(DetectionError::EnvelopeEmpty)?; + let confirmed = chains + .iter() + .any(|chain| matches!(chain.eeprom, EepromState::Present { .. })) + && chains.iter().all(ChainReport::is_identified); + let operating_point = if confirmed { + OperatingPoint { + voltage_mv: envelope.v_max_mv.min(profile.envelope.v_max_mv), + frequency_mhz: envelope.f_max_mhz.min(profile.envelope.f_max_mhz), + confirmed: true, + } + } else { + // Unverified hardware runs at the conservative floor of the usable envelope. + OperatingPoint { + voltage_mv: envelope.v_min_mv, + frequency_mhz: envelope.f_min_mhz, + confirmed: false, + } + }; + Ok(HardwarePlan { + profile: profile.name.clone(), + chip: profile.chip, + chains: chains.to_vec(), + envelope, + operating_point, + psu: psu.name.clone(), + fan_zones, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn profile() -> BoardProfile { + BoardProfile { + name: "s19jpro-bm1362".into(), + chip: ChipModel::Bm1362, + sensor: SensorTopology::Pic, + chains: 3, + envelope: Envelope { + v_min_mv: 1100, + v_max_mv: 1500, + f_min_mhz: 300, + f_max_mhz: 700, + }, + } + } + + fn psu() -> PsuCapability { + PsuCapability { + name: "APW12".into(), + envelope: Envelope { + v_min_mv: 1200, + v_max_mv: 1600, + f_min_mhz: 0, + f_max_mhz: 1000, + }, + max_dc_watts: 3600, + } + } + + fn chain(index: u8, eeprom: EepromState) -> ChainReport { + ChainReport { + index, + chip: ChipModel::Bm1362, + chips_found: 126, + eeprom, + response_per_mille: Some(980), + } + } + + #[test] + fn confirmed_plan_uses_verified_ceiling() { + let chains = [chain( + 0, + EepromState::Present { + model: "BHB42601".into(), + }, + )]; + let plan = plan(&chains, &profile(), Some(&psu()), Vec::new()).expect("plan"); + assert!(plan.operating_point.confirmed); + assert_eq!(plan.operating_point.voltage_mv, 1500); + assert_eq!(plan.operating_point.frequency_mhz, 700); + assert_eq!(plan.envelope.v_min_mv, 1200); + } + + #[test] + fn absent_eeprom_still_yields_a_conservative_plan() { + let chains = [chain(0, EepromState::Absent)]; + let plan = plan(&chains, &profile(), Some(&psu()), Vec::new()).expect("plan"); + assert!(!plan.operating_point.confirmed); + assert_eq!(plan.operating_point.voltage_mv, plan.envelope.v_min_mv); + assert_eq!(plan.operating_point.frequency_mhz, plan.envelope.f_min_mhz); + } + + #[test] + fn unknown_chip_refuses() { + let mut chains = [chain(0, EepromState::Absent)]; + chains[0].chip = ChipModel::Unknown; + assert_eq!( + plan(&chains, &profile(), Some(&psu()), Vec::new()), + Err(DetectionError::UnknownChip) + ); + } + + #[test] + fn wrong_chip_refuses() { + let mut chains = [chain(0, EepromState::Absent)]; + chains[0].chip = ChipModel::Bm1366; + assert_eq!( + plan(&chains, &profile(), Some(&psu()), Vec::new()), + Err(DetectionError::ChipProfileMismatch) + ); + } + + #[test] + fn missing_psu_refuses() { + let chains = [chain(0, EepromState::Absent)]; + assert_eq!( + plan(&chains, &profile(), None, Vec::new()), + Err(DetectionError::PsuUnidentified) + ); + } + + #[test] + fn incompatible_psu_refuses() { + let chains = [chain(0, EepromState::Absent)]; + let weak = PsuCapability { + name: "APW3".into(), + envelope: Envelope { + v_min_mv: 700, + v_max_mv: 1090, + f_min_mhz: 0, + f_max_mhz: 1000, + }, + max_dc_watts: 1600, + }; + assert_eq!( + plan(&chains, &profile(), Some(&weak), Vec::new()), + Err(DetectionError::EnvelopeEmpty) + ); + } + + #[test] + fn empty_chain_set_refuses() { + assert_eq!( + plan(&[], &profile(), Some(&psu()), Vec::new()), + Err(DetectionError::NoChains) + ); + } + + #[test] + fn degraded_chains_are_reported_not_fatal() { + let mut silent = chain(1, EepromState::Absent); + silent.response_per_mille = Some(0); + let chains = [chain(0, EepromState::Absent), silent]; + let plan = plan(&chains, &profile(), Some(&psu()), Vec::new()).expect("plan"); + assert_eq!(plan.degraded_chains(126), vec![1]); + } + + #[test] + fn fan_zones_are_carried_into_the_plan() { + let chains = [chain(0, EepromState::Absent)]; + let zones = vec![FanZone { + fan: 0, + zones: vec![0, 1], + }]; + let plan = plan(&chains, &profile(), Some(&psu()), zones.clone()).expect("plan"); + assert_eq!(plan.fan_zones, zones); + } +} diff --git a/src/lib.rs b/src/lib.rs index 9675636..aa016ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,7 @@ pub mod bm1362; pub mod bm1366; pub mod bm1370; pub mod bm13xx; +pub mod detect; pub mod difficulty; pub mod hal_wire; pub mod management;