//! Display-layer device-naming cleanup (round-9 item #11). //! //! asic-rs hands us compact Rust-identifier model strings and bare control-board //! names, not human-facing labels: //! - `device_info.make` = "Antminer" / "Whatsminer" (note the casing) //! - `device_info.model` = the model enum's variant identifier verbatim, e.g. //! "S19KPro", "S19jPro", "M30SPlusVE40", "M30SPlusPlusVE40" -- no spaces, //! "+" rendered "Plus", "++" rendered "PlusPlus", the WhatsMiner version //! block ("VE40") baked in. //! - `control_board_version` = a bare board name ("Xilinx", "CVITek", //! "AMLogic", "BeagleBoneBlack"; WhatsMiner "H3"/"H6"/"H6OS"/"H616"). //! //! This module turns those into the Brand / Model / Submodel / control-board //! short code the table shows. It is a pure string transform -- no asic-rs edit //! -- so it stays a build-on-top layer over the vendored crate. //! //! Known limitations (asic-rs does not expose the data): //! - Antminer has no hashboard-type/submodel over the network, so Submodel is //! blank for Antminer (only WhatsMiner's version block is recoverable). //! - asic-rs reports only "AMLogic" (not the 120mm C76 vs 140mm CBE board) and //! only "Xilinx" (not C49 vs C55); we disambiguate by model family as a //! best-effort heuristic documented at `control_board_code`. /// Human brand from asic-rs `device_info.make`. pub fn brand(make: &str) -> String { match make.trim().to_ascii_lowercase().as_str() { "antminer" => "Antminer".to_string(), "whatsminer" => "WhatsMiner".to_string(), "" => String::new(), other => { // Title-case an unknown make so the Brand column still reads well. let mut c = other.chars(); match c.next() { Some(f) => f.to_ascii_uppercase().to_string() + c.as_str(), None => String::new(), } } } } fn is_whatsminer(make: &str) -> bool { make.trim().eq_ignore_ascii_case("whatsminer") } fn is_antminer(make: &str) -> bool { make.trim().eq_ignore_ascii_case("antminer") } /// Split asic-rs's firmware family string into the family shown in the Firmware /// column and an optional vendor prefix that moves into the FW Version column /// (round 11 #6). asic-rs reports stock firmware as "AntMiner Stock" / /// "WhatsMiner Stock"; the Firmware column should read just "Stock" (so it /// colours as the neutral/default family), and the vendor word belongs with the /// version. Aftermarket families ("VNish", "Braiins", ...) carry no vendor /// prefix and pass through unchanged. pub fn firmware_family(raw: &str) -> (String, Option) { let trimmed = raw.trim(); // Vendor prefixes asic-rs prepends on stock-firmware family strings, matched // case-insensitively. for vendor in ["AntMiner", "WhatsMiner"] { if trimmed.len() > vendor.len() && trimmed[..vendor.len()].eq_ignore_ascii_case(vendor) { let rest = trimmed[vendor.len()..].trim(); if !rest.is_empty() { return (rest.to_string(), Some(vendor.to_string())); } } } (trimmed.to_string(), None) } /// Whether a miner is liquid-cooled (hydro / water / immersion) rather than /// air-cooled, inferred from the model name (round 11 #17). asic-rs exposes no /// cooling-type field, so this is a best-effort model-string heuristic: it /// decides whether a reported "fluid" temperature is a real coolant reading or /// (for air-cooled units) the ambient/environment temperature. pub fn is_liquid_cooled(model: &str) -> bool { let m = model.to_ascii_lowercase(); m.contains("hydro") || m.contains("immers") || m.contains("water") } /// The human model name for the Model column: brand dropped (it has its own /// column), family-letter suffix lowercased, word suffixes spaced, "+"/"++" for /// Plus/PlusPlus, and (WhatsMiner) the trailing version block moved to Submodel. pub fn clean_model(make: &str, model_id: &str) -> String { let id = model_id.trim(); if id.is_empty() { return String::new(); } if is_whatsminer(make) { clean_whatsminer(id).0 } else if is_antminer(make) { clean_antminer(id) } else { id.to_string() } } /// Best-effort ASIC chip model for a miner, used ONLY as a fallback when the /// firmware did not report a chip type on the wire (round 21 #3). Most firmwares /// on Antminer hardware (stock Antminer, VNish, LuxOS, Braiins, UMC) never put the /// chip string in their API, but the chip is a fixed per-model hardware fact, so /// it can be derived from the detected model. Only high-confidence, widely /// documented Antminer mappings are returned; everything else (and WhatsMiner, /// whose chips are not cleanly named) returns None so the column stays blank /// rather than show a guess. `model_id` is the raw asic-rs model id. pub fn chip_type_for_model(make: &str, model_id: &str) -> Option<&'static str> { if !is_antminer(make) { return None; } // Normalize: drop a leading "ANTMINER ", cut any trailing parenthetical // ("S19 (88)" -> "S19"), uppercase, and strip every non-alphanumeric char. let base = model_id.trim(); let base = base .strip_prefix("ANTMINER ") .or_else(|| base.strip_prefix("Antminer ")) .unwrap_or(base); let base = base.split('(').next().unwrap_or(base); let key: String = base .chars() .filter(|c| c.is_ascii_alphanumeric()) .map(|c| c.to_ascii_uppercase()) .collect(); // Bitmain chip per model (SHA-256 air/hydro). Sources: Bitmain shop specs + // repair-guide chip markings. Keep conservative -- omit uncertain variants. let chip = match key.as_str() { "S19" | "S19PRO" | "S19A" | "S19APRO" | "T19" => "BM1398", "S19PROPLUSHYD" | "S19PROPLUSHYDRO" | "S19PROHYD" | "S19PROHYDRO" => "BM1398", "S19J" | "S19JPRO" | "S19JPROPLUS" | "S19JPRONOPIC" => "BM1362", "S19XP" | "S19XPHYD" | "S19XPHYDRO" | "S19KPRO" | "S19KPRONOPIC" => "BM1366", "S21" | "S21PRO" | "S21PLUS" | "S21HYD" | "S21HYDRO" | "T21" => "BM1370", _ => return None, }; Some(chip) } /// The Submodel column value. WhatsMiner: the version block ("VE40"). Antminer: /// empty -- the hashboard type is not reported over the network by asic-rs. pub fn submodel(make: &str, model_id: &str) -> String { if is_whatsminer(make) { clean_whatsminer(model_id.trim()).1 } else { String::new() } } /// Split a WhatsMiner identifier into (clean model, version submodel). The /// version block is the trailing run beginning at the first 'V' (WhatsMiner /// tiers never contain a 'V', so the first 'V' always starts the version). The /// prefix's Plus/PlusPlus become +/++. fn clean_whatsminer(id: &str) -> (String, String) { let (prefix, version) = match id.find('V') { Some(i) => (&id[..i], id[i..].to_string()), None => (id, String::new()), }; let model = prefix.replace("PlusPlus", "++").replace("Plus", "+"); (model, version) } /// Known Antminer word-suffixes, longest-first so "PlusPlus" wins over "Plus" /// and "NoPIC" is matched whole. Each maps to its spaced display form. const ANT_SUFFIXES: &[(&str, &str)] = &[ ("PlusPlus", "++"), ("Plus", "+"), ("NoPIC", " NoPIC"), ("Hydro", " Hydro"), ("Pro", " Pro"), ("XP", " XP"), ]; /// Clean an Antminer identifier, e.g. "S19KPro" -> "S19k Pro", "S21eXPHydro" -> /// "S21e XP Hydro", "L3PlusPlus" -> "L3++". Unknown/odd inputs pass through. fn clean_antminer(id: &str) -> String { // asic-rs "Unknown" models pass the raw vendor string through with the brand // still in it (e.g. "ANTMINER S19 (88)", a NoPIC S19); drop a redundant // leading brand word so the Model column never repeats the Brand column. // `get(..9)` returns None (rather than panicking) when byte 9 is not a UTF-8 // char boundary, so a multibyte miner-supplied model string can't crash here // (round 18 #12). The prefix is 9 ASCII bytes, so a real match implies a // boundary at 9 and the following `id[9..]` is safe. let id = match id.get(..9) { Some(p) if p.eq_ignore_ascii_case("ANTMINER ") => id[9..].trim_start(), _ => id, }; let bytes = id.as_bytes(); // Base = leading letters then digits ("S19", "KS5", "L3", "Z15"). Without a // letter+digit base (e.g. an Unknown passthrough), return the id unchanged. let mut i = 0; while i < bytes.len() && bytes[i].is_ascii_alphabetic() { i += 1; } let letters_end = i; while i < bytes.len() && bytes[i].is_ascii_digit() { i += 1; } if letters_end == 0 || i == letters_end { return id.to_string(); } let mut out = String::from(&id[..i]); let mut rest = &id[i..]; // An optional single family letter directly after the digits (j/i/a/e/K/L), // recognised as "not the start of a known suffix". Lowercase it, keep it // attached to the base (no space): "S19K" -> "S19k". if let Some(first) = rest.chars().next() { let starts_suffix = ANT_SUFFIXES.iter().any(|(s, _)| rest.starts_with(s)); if first.is_ascii_alphabetic() && !starts_suffix { out.push(first.to_ascii_lowercase()); rest = &rest[first.len_utf8()..]; } } // Remaining suffix words, matched greedily against the known set. while !rest.is_empty() { match ANT_SUFFIXES.iter().find(|(s, _)| rest.starts_with(s)) { Some((s, disp)) => { out.push_str(disp); rest = &rest[s.len()..]; } None => { // Unknown trailing token: emit it space-separated and stop. out.push(' '); out.push_str(rest.trim_start()); break; } } } out } /// Normalise a raw control-board name for matching: drop spaces/punctuation and /// uppercase, so "BeagleBone Black", "BeagleBoneBlack" and "BBB" all collapse. fn compact_upper(s: &str) -> String { s.chars().filter(|c| c.is_ascii_alphanumeric()).flat_map(|c| c.to_uppercase()).collect() } /// The control-board short code for the Control-board column, e.g. /// "BeagleBoneBlack" -> "BBB (C77)", "Xilinx" on an S17 -> "XIL (C49)". /// /// asic-rs reports only the board FAMILY ("AMLogic"/"Xilinx"), never the exact /// Bitmain board number, so where a family spans two boards we disambiguate by /// model era/class (best-effort, since the network data can't distinguish them): /// - Xilinx: S17/T17-era -> C49, otherwise (S19-era) -> C55. /// - AMLogic: XP-class / S19k (140mm fans) -> CBE, otherwise (120mm) -> C76. /// /// `clean_model` is the already-cleaned model name (used for the era/class test). pub fn control_board_code(make: &str, board_name: &str, clean_model: &str) -> String { let (cpu, code) = control_board_split(make, board_name, clean_model); if code.is_empty() { cpu } else { format!("{cpu} ({code})") } } /// The control-board CPU family short code alone ("AML", "CVI", "BBB", "XIL", /// WhatsMiner "H3"/"H6"/"H6os"/"H616", or the raw name if unrecognised). pub fn control_board_cpu(make: &str, board_name: &str, clean_model: &str) -> String { control_board_split(make, board_name, clean_model).0 } /// The Bitmain board code alone ("C77"/"C88"/"C49"/"C55"/"C76"/"CBE"; /// WhatsMiner "CB2"/"CB4"/"CB6"), or "" when unknown. pub fn control_board_board(make: &str, board_name: &str, clean_model: &str) -> String { control_board_split(make, board_name, clean_model).1 } /// Split a raw control-board name into (CPU family short code, Bitmain board /// code). asic-rs reports only the board FAMILY ("AMLogic"/"Xilinx"), never the /// exact board number, so where a family spans two boards we disambiguate by /// model era/class (best-effort): /// - Xilinx: S17/T17-era -> C49, otherwise (S19-era) -> C55. /// - AMLogic: XP-class / S19k / S21 (140mm fans) -> CBE, otherwise 120mm C76. fn control_board_split(make: &str, board_name: &str, clean_model: &str) -> (String, String) { let raw = board_name.trim(); if raw.is_empty() { return (String::new(), String::new()); } // asic-rs renders an unrecognised board as "Unknown: "; surface it in // the CPU part with no code. if raw.starts_with("Unknown") { return (raw.to_string(), String::new()); } let key = compact_upper(raw); let pair = |cpu: &str, code: &str| (cpu.to_string(), code.to_string()); if is_whatsminer(make) { return match key.as_str() { "H3" => pair("H3", "CB2"), "H6" => pair("H6", "CB4"), "H6OS" => pair("H6os", "CB4"), "H616" => pair("H616", "CB6"), _ => (raw.to_string(), String::new()), }; } // Antminer (and any make that reports these families). match key.as_str() { "BEAGLEBONEBLACK" | "BBB" | "BB" | "BEAGLEBONE" => pair("BBB", "C77"), "CVITEK" | "CVI" => pair("CVI", "C88"), "XILINX" | "XIL" => { if is_s17_era(clean_model) { pair("XIL", "C49") } else { pair("XIL", "C55") } } "AMLOGIC" | "AML" => { if is_aml_140mm(clean_model) { pair("AML", "CBE") } else { pair("AML", "C76") } } _ => (raw.to_string(), String::new()), } } /// S17/T17-era models use the Xilinx C49 control board. fn is_s17_era(clean_model: &str) -> bool { let m = clean_model.trim_start(); m.starts_with("S17") || m.starts_with("T17") } /// Best-effort: the 140mm-fan AMLogic board (CBE) is used on the XP-class, the /// S19k Pro, and the S21 Pro; everything else AMLogic is the 120mm C76. asic-rs /// cannot tell these apart, so this is a model-family heuristic, not a read. fn is_aml_140mm(clean_model: &str) -> bool { // Per the doc above: XP-class, the S19k Pro, and the S21 Pro -- NOT every S21 // (plain S21 / S21+ / S21e are the 120mm C76 board) (round 18 #12). let m = clean_model.trim_start(); m.contains("XP") || m.starts_with("S19k") || m.contains("S21 Pro") } /// Nameplate expected hashrate in Sol/s for an Equihash (Zcash) miner model, when /// the firmware itself does not report an expected hashrate (round 18 #6). Plain /// Antminer Z15 firmware omits the cgminer `total_rateideal` field (the Z15j does /// report it), so its actual-vs-expected column and colour would otherwise be /// blank. The Z15 nameplate is 420 KSol/s. Returns `None` for models with no known /// nameplate (the caller then leaves expected unknown). pub fn equihash_expected_sols(clean_model: &str) -> Option { let m = clean_model.to_ascii_lowercase(); // Match the Z15 family (z15, z15j); both share the 420 KSol/s nameplate. if m.contains("z15") { Some(420_000.0) } else { None } } #[cfg(test)] mod tests { use super::*; #[test] fn chip_type_derivation() { // Fleet models (round 21 #3): derived from the detected model for Antminer // hardware where the firmware does not report a chip string on the wire. assert_eq!(chip_type_for_model("Antminer", "S21Pro"), Some("BM1370")); assert_eq!(chip_type_for_model("Antminer", "S19jPro"), Some("BM1362")); assert_eq!(chip_type_for_model("Antminer", "S19KPro"), Some("BM1366")); assert_eq!(chip_type_for_model("Antminer", "S19"), Some("BM1398")); assert_eq!(chip_type_for_model("Antminer", "T19"), Some("BM1398")); // Raw "Unknown" passthrough with brand + parenthetical normalizes to S19. assert_eq!(chip_type_for_model("Antminer", "ANTMINER S19 (88)"), Some("BM1398")); // WhatsMiner chips are not cleanly named -> never derived. assert_eq!(chip_type_for_model("Whatsminer", "M30S+VE40"), None); // Unknown Antminer model -> None (no guess). assert_eq!(chip_type_for_model("Antminer", "L7"), None); } #[test] fn brand_normalises_case() { assert_eq!(brand("Antminer"), "Antminer"); assert_eq!(brand("Whatsminer"), "WhatsMiner"); assert_eq!(brand("whatsminer"), "WhatsMiner"); assert_eq!(brand(""), ""); assert_eq!(brand("avalonminer"), "Avalonminer"); } #[test] fn antminer_model_cleanup() { // The headline example from the spec. assert_eq!(clean_model("Antminer", "S19KPro"), "S19k Pro"); assert_eq!(clean_model("Antminer", "S19jPro"), "S19j Pro"); assert_eq!(clean_model("Antminer", "S21Pro"), "S21 Pro"); assert_eq!(clean_model("Antminer", "S19"), "S19"); assert_eq!(clean_model("Antminer", "S19XP"), "S19 XP"); assert_eq!(clean_model("Antminer", "S19jXP"), "S19j XP"); assert_eq!(clean_model("Antminer", "S19Plus"), "S19+"); assert_eq!(clean_model("Antminer", "S19ProPlus"), "S19 Pro+"); assert_eq!(clean_model("Antminer", "S19jProPlus"), "S19j Pro+"); assert_eq!(clean_model("Antminer", "S19ProPlusHydro"), "S19 Pro+ Hydro"); assert_eq!(clean_model("Antminer", "S19jNoPIC"), "S19j NoPIC"); assert_eq!(clean_model("Antminer", "S21PlusPlus"), "S21++"); assert_eq!(clean_model("Antminer", "S21eXPHydro"), "S21e XP Hydro"); assert_eq!(clean_model("Antminer", "L3PlusPlus"), "L3++"); assert_eq!(clean_model("Antminer", "KS5Pro"), "KS5 Pro"); assert_eq!(clean_model("Antminer", "Z15Pro"), "Z15 Pro"); assert_eq!(clean_model("Antminer", "S9i"), "S9i"); assert_eq!(clean_model("Antminer", "S17e"), "S17e"); assert_eq!(clean_model("Antminer", "S19a"), "S19a"); assert_eq!(clean_model("Antminer", "S19aPro"), "S19a Pro"); // asic-rs Unknown passthrough carrying the raw vendor string (a NoPIC // S19 seen live at 10.4.x): drop the redundant brand, keep the rest. assert_eq!(clean_model("Antminer", "ANTMINER S19 (88)"), "S19 (88)"); } #[test] fn whatsminer_model_and_submodel() { assert_eq!(clean_model("Whatsminer", "M30SPlusVE40"), "M30S+"); assert_eq!(submodel("Whatsminer", "M30SPlusVE40"), "VE40"); assert_eq!(clean_model("Whatsminer", "M30SPlusPlusVE40"), "M30S++"); assert_eq!(submodel("Whatsminer", "M30SPlusPlusVE40"), "VE40"); assert_eq!(clean_model("Whatsminer", "M30SVE40"), "M30S"); assert_eq!(submodel("Whatsminer", "M30SVE40"), "VE40"); assert_eq!(clean_model("Whatsminer", "M20S"), "M20S"); assert_eq!(submodel("Whatsminer", "M20S"), ""); assert_eq!(clean_model("Whatsminer", "M50S++VK30"), "M50S++"); assert_eq!(submodel("Whatsminer", "M50S++VK30"), "VK30"); } #[test] fn antminer_has_no_submodel() { assert_eq!(submodel("Antminer", "S19KPro"), ""); } #[test] fn control_board_short_codes() { // Antminer families (board name as asic-rs renders the enum variant). assert_eq!(control_board_code("Antminer", "BeagleBoneBlack", "S19"), "BBB (C77)"); // Also accept the spaced form some layers produce. assert_eq!(control_board_code("Antminer", "BeagleBone Black", "S19"), "BBB (C77)"); assert_eq!(control_board_code("Antminer", "CVITek", "S21 Pro"), "CVI (C88)"); assert_eq!(control_board_code("Antminer", "Xilinx", "S17 Pro"), "XIL (C49)"); assert_eq!(control_board_code("Antminer", "Xilinx", "T17"), "XIL (C49)"); assert_eq!(control_board_code("Antminer", "Xilinx", "S19 Pro"), "XIL (C55)"); assert_eq!(control_board_code("Antminer", "AMLogic", "S19j Pro"), "AML (C76)"); assert_eq!(control_board_code("Antminer", "AMLogic", "S19k Pro"), "AML (CBE)"); assert_eq!(control_board_code("Antminer", "AMLogic", "S19 XP"), "AML (CBE)"); // WhatsMiner families. assert_eq!(control_board_code("Whatsminer", "H3", "M30S"), "H3 (CB2)"); assert_eq!(control_board_code("Whatsminer", "H6", "M50"), "H6 (CB4)"); assert_eq!(control_board_code("Whatsminer", "H6OS", "M50"), "H6os (CB4)"); assert_eq!(control_board_code("Whatsminer", "H616", "M30S+"), "H616 (CB6)"); // Unknown / blank pass through. assert_eq!(control_board_code("Antminer", "Unknown: FOO", "S19"), "Unknown: FOO"); assert_eq!(control_board_code("Antminer", "", "S19"), ""); } #[test] fn control_board_cpu_and_board_split() { // CPU column vs board-code column. assert_eq!(control_board_cpu("Antminer", "AMLogic", "S19j Pro"), "AML"); assert_eq!(control_board_board("Antminer", "AMLogic", "S19j Pro"), "C76"); assert_eq!(control_board_cpu("Antminer", "CVITek", "S21 Pro"), "CVI"); assert_eq!(control_board_board("Antminer", "CVITek", "S21 Pro"), "C88"); assert_eq!(control_board_cpu("Whatsminer", "H616", "M30S+"), "H616"); assert_eq!(control_board_board("Whatsminer", "H616", "M30S+"), "CB6"); // S21 Pro on an AMLogic board is the 140mm CBE, not C76. assert_eq!(control_board_board("Antminer", "AMLogic", "S21 Pro"), "CBE"); assert_eq!(control_board_code("Antminer", "AMLogic", "S21 Pro"), "AML (CBE)"); // Unknown -> cpu holds the raw string, no board code. assert_eq!(control_board_cpu("Antminer", "Unknown: FOO", "S19"), "Unknown: FOO"); assert_eq!(control_board_board("Antminer", "Unknown: FOO", "S19"), ""); } #[test] fn firmware_family_splits_vendor_prefix() { // Stock firmware: vendor word moves out of the family word (round 11 #6). assert_eq!(firmware_family("AntMiner Stock"), ("Stock".to_string(), Some("AntMiner".to_string()))); assert_eq!( firmware_family("WhatsMiner Stock"), ("Stock".to_string(), Some("WhatsMiner".to_string())) ); // Aftermarket families pass through with no vendor prefix. assert_eq!(firmware_family("VNish"), ("VNish".to_string(), None)); assert_eq!(firmware_family("Braiins"), ("Braiins".to_string(), None)); // A bare vendor word with nothing after it is left as-is (not emptied). assert_eq!(firmware_family("AntMiner"), ("AntMiner".to_string(), None)); } #[test] fn liquid_cooling_detection() { assert!(is_liquid_cooled("S21 XP Hydro")); assert!(is_liquid_cooled("Antminer S19 Hydro")); assert!(is_liquid_cooled("M53 immersion")); assert!(!is_liquid_cooled("S21 Pro")); assert!(!is_liquid_cooled("M30S++")); } }