//! Share difficulty helpers. //! //! Firmwares report best-share difficulty as a JSON number, a decimal string, //! or an AxeOS-style suffix string (`"483k"`, `"1.2M"`). #[cfg(feature = "python")] use pyo3::pyclass; use serde::{Deserialize, Serialize}; use serde_json::Value; use ts_rs::TS; /// Cumulative share statistics reported by a miner (round 19 #10, T2). /// /// These are miner-level aggregates from the cgminer/btminer summary. Difficulty /// values are difficulty-weighted sums (not raw share counts, which live on /// [`PoolData`][`super::pool::PoolData`]). Every field is optional; a backend /// fills in only what its firmware exposes. #[cfg_attr(feature = "python", pyclass(from_py_object, module = "asic_rs"))] #[cfg_attr(feature = "python", asic_rs_pydantic::py_pydantic_model(getters))] #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default, TS)] pub struct ShareSummary { /// Difficulty-weighted accepted work, when reported. pub difficulty_accepted: Option, /// Difficulty-weighted rejected work, when reported. pub difficulty_rejected: Option, /// Raw stale share count, when reported. pub stale_shares: Option, /// Number of blocks found by this miner, when reported. pub found_blocks: Option, } impl ShareSummary { /// True when no field is populated. pub fn is_empty(&self) -> bool { self.difficulty_accepted.is_none() && self.difficulty_rejected.is_none() && self.stale_shares.is_none() && self.found_blocks.is_none() } } /// Parse a [`ShareSummary`] from a cgminer/btminer `SUMMARY[0]` object (the value /// extracted at `/SUMMARY/0`). Reads "Difficulty Accepted", "Difficulty Rejected", /// "Stale", and "Found Blocks". Returns `None` when none are present. pub fn parse_cgminer_share_summary(summary: &Value) -> Option { let f = |k: &str| { summary .get(k) .and_then(|v| v.as_f64()) .filter(|v| v.is_finite() && *v >= 0.0) }; let u = |k: &str| summary.get(k).and_then(|v| v.as_u64()); let s = ShareSummary { difficulty_accepted: f("Difficulty Accepted"), difficulty_rejected: f("Difficulty Rejected"), stale_shares: u("Stale"), found_blocks: u("Found Blocks"), }; (!s.is_empty()).then_some(s) } /// Parse a share difficulty from a JSON value. /// /// Accepts numbers and strings. Suffixes `k`, `M`, `G`, `T`, `P`, and `E` /// are treated as SI powers of 1000. Returns `None` for negative, non-finite, /// or unparsable values. pub fn parse_share_difficulty(value: &Value) -> Option { match value { Value::Number(n) => n.as_f64().filter(|v| v.is_finite() && *v >= 0.0), Value::String(s) => parse_share_difficulty_str(s), _ => None, } } /// Parse a share difficulty from a decimal or suffix string. pub fn parse_share_difficulty_str(s: &str) -> Option { let s = s.trim(); if s.is_empty() { return None; } let (num, mul) = match s.as_bytes().last().copied() { Some( b @ (b'k' | b'K' | b'm' | b'M' | b'g' | b'G' | b't' | b'T' | b'p' | b'P' | b'e' | b'E'), ) => { let mul = match b { b'k' | b'K' => 1e3, b'm' | b'M' => 1e6, b'g' | b'G' => 1e9, b't' | b'T' => 1e12, b'p' | b'P' => 1e15, b'e' | b'E' => 1e18, _ => unreachable!(), }; (&s[..s.len() - 1], mul) } _ => (s, 1.0), }; let v: f64 = num.trim().parse().ok()?; let v = v * mul; (v.is_finite() && v >= 0.0).then_some(v) } #[cfg(test)] mod tests { use super::*; use serde_json::json; #[test] fn parses_numbers_and_suffix_strings() { assert_eq!(parse_share_difficulty(&json!(12)), Some(12.0)); assert_eq!(parse_share_difficulty(&json!(0)), Some(0.0)); assert_eq!(parse_share_difficulty(&json!("483k")), Some(483_000.0)); assert_eq!(parse_share_difficulty(&json!("1.2M")), Some(1.2e6)); assert_eq!( parse_share_difficulty(&json!("499065300")), Some(499_065_300.0) ); assert_eq!(parse_share_difficulty(&json!(" 2.0k ")), Some(2_000.0)); assert_eq!(parse_share_difficulty(&json!(-1)), None); assert_eq!(parse_share_difficulty(&json!("nope")), None); assert_eq!(parse_share_difficulty(&json!(null)), None); } }