Files
alkane-toolkit/vendor/asic-rs-core/src/data/share.rs
T
noahandClaude Opus 4.8 3f9fc4a24b asic-rs telemetry T2b: cumulative share stats (difficulty acc/rej, stale, blocks)
Round 19 #10 telemetry extension, tier 2b. Adds the cumulative share statistics
from item #10 end to end: difficulty-weighted accepted/rejected work, raw stale
share count, and blocks found.

Core (asic-rs-core):
- New ShareSummary sub-struct (data/share.rs) with a parse_cgminer_share_summary
  helper reading a cgminer/btminer SUMMARY[0] object.
- New DataField::ShareSummary; MinerData gains share_summary.
- Rides the existing GetBestShare trait via a defaulted parse_share_summary method
  (no new supertrait bound, no per-backend empty impl); parse_data wires it in.

Backends (read the already-fetched SUMMARY, so no extra round-trips; these are
miner-level aggregates -> top-level MinerData fields, not per-pool PoolData):
- Antminer v2020 + v2023_07, LuxOS, WhatsMiner V1 + V2 override parse_share_summary.
- Braiins (no cumulative difficulty), ePIC (current difficulty only), WhatsMiner V3
  and VNish expose none and inherit None -- fixtures confirm.

GUI: four new columns (Diff acc / Diff rej / Stale / Blocks), right-aligned, in the
Pools category, auto-hidden when empty. MinerRow gains the four fields; to_row reads
them from share_summary. ALL_COLUMNS 63 -> 67.

Fixture tests: whatsminer v1 and antminer v2020 assert the parsed values. Patch 0026.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Djv3oncM4LxTS5tb61Ako6
2026-09-23 15:51:57 -06:00

124 lines
4.4 KiB
Rust

//! 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<f64>,
/// Difficulty-weighted rejected work, when reported.
pub difficulty_rejected: Option<f64>,
/// Raw stale share count, when reported.
pub stale_shares: Option<u64>,
/// Number of blocks found by this miner, when reported.
pub found_blocks: Option<u64>,
}
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<ShareSummary> {
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<f64> {
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<f64> {
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);
}
}