use std::{collections::HashMap, net::IpAddr, time::Duration}; use macaddr::MacAddr; use measurements::{Frequency, Power, Temperature, Voltage}; #[cfg(feature = "python")] use pyo3::prelude::*; use serde::{Deserialize, Serialize}; use ts_rs::TS; use super::{ board::{BoardData, MinerControlBoard}, capabilities::TuningCapabilities, device::DeviceInfo, fan::FanData, hashrate::{HashRate, HashrateAverages}, message::MinerMessage, operating_state::OperatingState, pool::PoolGroupData, psu::PsuInfo, share::ShareSummary, }; use crate::data::{ deserialize::deserialize_macaddr, serialize::{serialize_macaddr, serialize_power, serialize_temperature}, }; /// Manual set points by board ID: (frequency, voltage). Missing measurements /// remain `None`; an empty map means no board set points were reported. pub type ManualTuningTargets = HashMap, Option)>; /// Scalar manual set points by board ID: (megahertz, volts). pub type ManualTuningValues = HashMap, Option)>; pub(crate) fn manual_targets_to_values(targets: &ManualTuningTargets) -> ManualTuningValues { targets .iter() .map(|(&id, &(frequency, voltage))| { ( id, ( frequency.map(|f| f.as_megahertz()), voltage.map(|v| v.as_volts()), ), ) }) .collect() } pub(crate) fn manual_targets_from_values(values: ManualTuningValues) -> ManualTuningTargets { values .into_iter() .map(|(id, (frequency, voltage))| { ( id, ( frequency.map(Frequency::from_megahertz), voltage.map(Voltage::from_volts), ), ) }) .collect() } mod manual_targets_serde { use super::*; pub fn serialize( targets: &ManualTuningTargets, serializer: S, ) -> Result { manual_targets_to_values(targets).serialize(serializer) } pub fn deserialize<'de, D: serde::Deserializer<'de>>( deserializer: D, ) -> Result { ManualTuningValues::deserialize(deserializer).map(manual_targets_from_values) } } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)] /// Firmware tuning target reported by a miner or requested by configuration. /// /// [`TuningTarget::Manual`] means perpetual/autotuning is disabled. Every other /// variant represents a firmware-managed tuning target and implies that tuning /// is enabled. pub enum TuningTarget { /// Use manual tuning with perpetual tuning disabled. /// /// Set points are optional because some firmware responses report that /// tuning is disabled without reporting one or both configured values. Manual { /// Board IDs mapped to (frequency, voltage), serialized as (MHz, volts). #[serde(default, with = "manual_targets_serde")] #[ts(type = "Record")] boards: ManualTuningTargets, }, /// Target a power limit. Power(#[ts(type = "{ watts: number }")] Power), /// Target a hashrate. HashRate(HashRate), /// Target a named mining mode. MiningMode(MiningMode), /// Target a named firmware preset (e.g. VNish autotune presets). Preset(String), } impl TuningTarget { /// Create a power tuning target from watts. pub fn from_watts(watts: f64) -> Self { TuningTarget::Power(Power::from_watts(watts)) } } #[cfg_attr(feature = "python", pyclass(from_py_object, str, module = "asic_rs"))] #[cfg_attr(feature = "python", derive(asic_rs_pydantic::PyPydanticEnum))] #[derive( Debug, Clone, Copy, PartialEq, Serialize, Deserialize, strum::Display, strum::EnumString, TS, )] /// Firmware-defined mining performance mode. pub enum MiningMode { /// Lower-power or quiet mining mode. #[cfg_attr(feature = "python", pydantic(value = "Low"))] Low, /// Normal mining mode. #[cfg_attr(feature = "python", pydantic(value = "Normal"))] Normal, /// High-performance mining mode. #[cfg_attr(feature = "python", pydantic(value = "High"))] High, } #[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, TS)] /// Standardized telemetry snapshot for one miner. pub struct MinerData { /// The schema version of this MinerData object, for use in external APIs pub schema_version: String, /// The time this data was gathered and constructed pub timestamp: u64, /// The IP address of the miner this data is for pub ip: IpAddr, /// The MAC address of the miner this data is for #[serde( serialize_with = "serialize_macaddr", deserialize_with = "deserialize_macaddr" )] #[ts(type = "string | null")] pub mac: Option, /// Hardware information about this miner pub device_info: DeviceInfo, /// The serial number of the miner, also known as the control board serial pub serial_number: Option, /// The network hostname of the miner pub hostname: Option, /// The API version of the miner pub api_version: Option, /// The firmware version of the miner pub firmware_version: Option, /// The type of control board on the miner pub control_board_version: Option, /// The expected number of boards in the miner. pub expected_hashboards: Option, /// Per-hashboard data for this miner pub hashboards: Vec, /// The current hashrate of the miner pub hashrate: Option, /// Rolling average hashrate windows (1m/5m/15m/24h), when reported pub hashrate_averages: Option, /// The expected hashrate of the miner pub expected_hashrate: Option, /// The total expected number of chips across all boards on this miner pub expected_chips: Option, /// The total number of working chips across all boards on this miner pub total_chips: Option, /// The expected number of fans on the miner pub expected_fans: Option, /// The current fan information for the miner pub fans: Vec, /// Miner-level fan duty cycle as a percentage (0-100), when reported. Firmware /// exposes no maximum RPM, so this duty cycle is the normalized fan indicator. pub fan_speed_percent: Option, /// Board target temperature threshold in Celsius, when reported (round 19 #10). pub temp_target: Option, /// Board hot temperature threshold in Celsius, when reported (round 19 #10). pub temp_hot: Option, /// Board dangerous temperature threshold in Celsius, when reported (round 19 #10). pub temp_dangerous: Option, /// The current PDU fan information for the miner pub psu_fans: Vec, /// The average temperature across all chips in the miner #[serde(serialize_with = "serialize_temperature")] #[ts(type = "number | null")] pub average_temperature: Option, /// The environment temperature of the miner, such as air temperature or immersion fluid temperature #[serde(serialize_with = "serialize_temperature")] #[ts(type = "number | null")] pub fluid_temperature: Option, /// The coolant exhaust temperature, only for water-cooled miners with dedicated sensors #[serde(serialize_with = "serialize_temperature")] #[ts(type = "number | null")] pub outlet_fluid_temperature: Option, /// The current power consumption of the miner #[serde(serialize_with = "serialize_power")] #[ts(type = "number | null")] pub wattage: Option, /// Power-supply info (identity, rated voltage envelope, live output V/A), /// when the firmware reports it. pub psu: Option, /// The current manual tuning percent of full power (100 = unthrottled), where supported pub tuning_percent: Option, /// The current tuning target of the miner, such as power target or hashrate target pub tuning_target: Option, /// The current tuning target adjusted by scaling settings, when available. pub scaled_tuning_target: Option, /// The factory tuning envelope the firmware exposes (default / min / max /// power and hashrate, or selectable presets), when reported. pub tuning_capabilities: Option, /// The current efficiency in W/TH/s (J/TH) of the miner pub efficiency: Option, /// The state of the fault/alert light on the miner pub light_flashing: Option, /// Any message on the miner, including errors pub messages: Vec, /// The total uptime of the miner's system #[ts(type = "{ secs: number, nanos: number } | null")] pub uptime: Option, /// Whether the hashing process is currently running, /// false if paused, true if running, even if the hashrate is 0 /// or the firmware is tuning. This legacy flag may default when data is /// missing; use `operating_state` for explicit detailed state telemetry. pub is_mining: bool, /// Detailed state explicitly reported by firmware, independent of `is_mining`. /// Unavailable, invalid, and boolean-only status responses leave this `None`. #[serde(default)] #[cfg_attr(feature = "python", pydantic(default = None))] pub operating_state: Option, /// Auto-tuner status as a human-readable label (round 19 #10), when reported: /// e.g. "Stable"/"Tuning" (Braiins), "Optimized"/"Tuning"/"Off" (ePIC). pub tuner_status: Option, /// The current pools configured on the miner pub pools: Vec, /// Difficulty of the best share found over the miner's lifetime, when reported. /// /// This is a dimensionless share difficulty (not hashrate). Firmware that /// only expose a single best-share value should populate this field and /// leave [`Self::session_best_share`] as `None`. pub best_share: Option, /// Difficulty of the best share found since the last boot or hashing start, /// when the firmware distinguishes session from all-time. pub session_best_share: Option, /// Cumulative share statistics (difficulty accepted/rejected, stale, blocks), /// when reported. Miner-level aggregate from the cgminer/btminer summary. pub share_summary: Option, } #[cfg(feature = "python")] pub use python_tuning_target::PyTuningTarget; #[cfg(feature = "python")] mod python_tuning_target { use asic_rs_pydantic::{ PyPydanticType, PydanticSchemaMode, get_required_field, literal_schema, pydantic_typed_dict_schema, tagged_union_schema, }; use measurements::Power; use pyo3::{exceptions::PyValueError, prelude::*, types::PyAnyMethods}; use super::{ HashRate, ManualTuningValues, MiningMode, TuningTarget, manual_targets_from_values, manual_targets_to_values, }; #[pyclass(name = "TuningTarget", skip_from_py_object, module = "asic_rs")] #[derive(Debug, Clone, PartialEq)] pub enum PyTuningTarget { Manual { boards: ManualTuningValues }, Power { watts: f64 }, HashRate { target_hashrate: HashRate }, Mode { target_mode: MiningMode }, Preset { name: String }, } #[pymethods] impl PyTuningTarget { #[staticmethod] #[pyo3(signature = (boards = None))] fn manual(boards: Option) -> Self { Self::Manual { boards: boards.unwrap_or_default(), } } #[staticmethod] fn power(watts: f64) -> Self { Self::Power { watts } } #[staticmethod] fn hashrate(hashrate: HashRate) -> Self { Self::HashRate { target_hashrate: hashrate, } } #[staticmethod] fn mode(mode: MiningMode) -> Self { Self::Mode { target_mode: mode } } #[staticmethod] fn preset(name: String) -> Self { Self::Preset { name } } #[getter] fn variant(&self) -> &'static str { match self { Self::Manual { .. } => "manual", Self::Power { .. } => "power", Self::HashRate { .. } => "hashrate", Self::Mode { .. } => "mode", Self::Preset { .. } => "preset", } } /// Board IDs mapped to (frequency in MHz, voltage in volts). #[getter] fn boards(&self) -> Option { match self { Self::Manual { boards } => Some(boards.clone()), _ => None, } } #[getter] fn watts(&self) -> Option { match self { Self::Power { watts } => Some(*watts), _ => None, } } #[getter] #[pyo3(name = "target_hashrate")] fn py_target_hashrate(&self) -> Option { match self { Self::HashRate { target_hashrate } => Some(target_hashrate.clone()), _ => None, } } #[getter] #[pyo3(name = "target_mode")] fn py_target_mode(&self) -> Option { match self { Self::Mode { target_mode } => Some(*target_mode), _ => None, } } #[getter] fn preset_name(&self) -> Option { match self { Self::Preset { name } => Some(name.clone()), _ => None, } } fn __repr__(&self) -> String { match self { Self::Manual { boards } => { let mut entries: Vec<_> = boards.iter().collect(); entries.sort_by_key(|(id, _)| **id); let entries = entries .into_iter() .map(|(id, (frequency, voltage))| { let frequency = frequency .map(|v| format!("{v:?}")) .unwrap_or_else(|| "None".to_owned()); let voltage = voltage .map(|v| format!("{v:?}")) .unwrap_or_else(|| "None".to_owned()); format!("{id}: ({frequency}, {voltage})") }) .collect::>() .join(", "); format!("TuningTarget.manual(boards={{{entries}}})") } Self::Power { watts } => format!("TuningTarget.power(watts={watts:?})"), Self::HashRate { target_hashrate } => { format!("TuningTarget.hashrate(hashrate={target_hashrate})") } Self::Mode { target_mode } => format!("TuningTarget.mode(mode={target_mode})"), Self::Preset { name } => format!("TuningTarget.preset(name={name:?})"), } } fn __str__(&self) -> String { self.__repr__() } } impl From for PyTuningTarget { fn from(value: TuningTarget) -> Self { match value { TuningTarget::Manual { boards } => Self::Manual { boards: manual_targets_to_values(&boards), }, TuningTarget::Power(power) => Self::Power { watts: power.as_watts(), }, TuningTarget::HashRate(hashrate) => Self::HashRate { target_hashrate: hashrate, }, TuningTarget::MiningMode(mode) => Self::Mode { target_mode: mode }, TuningTarget::Preset(name) => Self::Preset { name }, } } } impl From for TuningTarget { fn from(value: PyTuningTarget) -> Self { match value { PyTuningTarget::Manual { boards } => TuningTarget::Manual { boards: manual_targets_from_values(boards), }, PyTuningTarget::Power { watts } => TuningTarget::Power(Power::from_watts(watts)), PyTuningTarget::HashRate { target_hashrate } => { TuningTarget::HashRate(target_hashrate) } PyTuningTarget::Mode { target_mode } => TuningTarget::MiningMode(target_mode), PyTuningTarget::Preset { name } => TuningTarget::Preset(name), } } } impl<'py> pyo3::IntoPyObject<'py> for TuningTarget { type Target = pyo3::PyAny; type Output = pyo3::Bound<'py, pyo3::PyAny>; type Error = pyo3::PyErr; const OUTPUT_TYPE: pyo3::inspect::PyStaticExpr = { ::TYPE_HINT }; fn into_pyobject(self, py: pyo3::Python<'py>) -> Result { PyTuningTarget::from(self) .into_pyobject(py) .map(pyo3::Bound::into_any) } } impl PyPydanticType for TuningTarget { fn pydantic_schema<'py>( core_schema: &Bound<'py, PyAny>, mode: PydanticSchemaMode, ) -> PyResult> { let number = as PyPydanticType>::pydantic_schema(core_schema, mode)?; let pair = core_schema.call_method1("tuple_schema", (vec![number.clone(), number],))?; let key_options = pyo3::types::PyDict::new(core_schema.py()); use pyo3::types::PyDictMethods; key_options.set_item("ge", 0)?; key_options.set_item("le", u8::MAX)?; let key = core_schema.call_method("int_schema", (), Some(&key_options))?; let manual_value_schema = core_schema.call_method1("dict_schema", (key, pair))?; let manual_schema = pydantic_typed_dict_schema!(core_schema, "asic_rs.TuningTargetManual", { "type" => required(literal_schema(core_schema, &["manual"])?), "value" => required(manual_value_schema), })?; let power_schema = pydantic_typed_dict_schema!(core_schema, "asic_rs.TuningTargetPower", { "type" => required(literal_schema(core_schema, &["power"])?), "value" => required(::pydantic_schema(core_schema, mode)?), })?; let hashrate_schema = pydantic_typed_dict_schema!(core_schema, "asic_rs.TuningTargetHashRate", { "type" => required(literal_schema(core_schema, &["hashrate"])?), "value" => required(::pydantic_schema(core_schema, mode)?), })?; let mode_schema = pydantic_typed_dict_schema!(core_schema, "asic_rs.TuningTargetMode", { "type" => required(literal_schema(core_schema, &["mode"])?), "value" => required(::pydantic_schema(core_schema, mode)?), })?; let preset_schema = pydantic_typed_dict_schema!(core_schema, "asic_rs.TuningTargetPreset", { "type" => required(literal_schema(core_schema, &["preset"])?), "value" => required(::pydantic_schema(core_schema, mode)?), })?; let tagged_union = tagged_union_schema( core_schema, [ ("manual", manual_schema), ("power", power_schema), ("hashrate", hashrate_schema), ("mode", mode_schema), ("preset", preset_schema), ], "type", Some("asic_rs.TuningTarget"), )?; if mode == PydanticSchemaMode::Serialization { return Ok(tagged_union); } let target_instance = core_schema.call_method1( "is_instance_schema", (core_schema.py().get_type::(),), )?; asic_rs_pydantic::union_schema(core_schema, [target_instance, tagged_union]) } fn from_pydantic(value: &Bound<'_, PyAny>) -> PyResult { if let Ok(target) = value.extract::>() { return Ok(target.clone().into()); } let type_str: String = get_required_field(value, "type")?.extract()?; let v = get_required_field(value, "value")?; match type_str.as_str() { "manual" => Ok(TuningTarget::Manual { boards: manual_targets_from_values(v.extract()?), }), "power" => Ok(TuningTarget::Power( ::from_pydantic(&v)?, )), "hashrate" => Ok(TuningTarget::HashRate( ::from_pydantic(&v)?, )), "mode" => Ok(TuningTarget::MiningMode( ::from_pydantic(&v)?, )), "preset" => Ok(TuningTarget::Preset( ::from_pydantic(&v)?, )), _ => Err(PyValueError::new_err(format!( "Unknown TuningTarget type '{type_str}', expected 'manual', 'power', 'hashrate', 'mode', or 'preset'" ))), } } fn to_pydantic_data(&self, py: Python<'_>) -> PyResult> { use pyo3::types::{PyDict, PyDictMethods}; let dict = PyDict::new(py); match self { TuningTarget::Manual { boards } => { dict.set_item("type", "manual")?; dict.set_item("value", manual_targets_to_values(boards))?; } TuningTarget::Power(p) => { dict.set_item("type", "power")?; dict.set_item("value", ::to_pydantic_data(p, py)?)?; } TuningTarget::HashRate(hr) => { dict.set_item("type", "hashrate")?; dict.set_item( "value", ::to_pydantic_data(hr, py)?, )?; } TuningTarget::MiningMode(m) => { dict.set_item("type", "mode")?; dict.set_item( "value", ::to_pydantic_data(m, py)?, )?; } TuningTarget::Preset(name) => { dict.set_item("type", "preset")?; dict.set_item( "value", ::to_pydantic_data(name, py)?, )?; } } Ok(dict.into_any().unbind()) } fn to_pydantic_repr_value(&self, py: Python<'_>) -> PyResult> { use pyo3::IntoPyObject as _; PyTuningTarget::from(self.clone()) .into_pyobject(py) .map(|b| b.into_any().unbind()) } } } #[cfg(test)] mod tests { use super::*; #[test] fn manual_tuning_target_serialization_round_trips() -> anyhow::Result<()> { let target = TuningTarget::Manual { boards: HashMap::from([ ( 0, ( Some(Frequency::from_megahertz(480.0)), Some(Voltage::from_volts(12.6)), ), ), ( 3, ( Some(Frequency::from_megahertz(490.0)), Some(Voltage::from_volts(12.7)), ), ), (7, (None, Some(Voltage::from_volts(12.5)))), (8, (Some(Frequency::from_megahertz(500.0)), None)), (9, (None, None)), ]), }; let encoded = serde_json::to_value(&target)?; assert_eq!( encoded, serde_json::json!({"Manual": {"boards": { "0": [480.0, 12.6], "3": [490.0, 12.7], "7": [null, 12.5], "8": [500.0, null], "9": [null, null], }}}) ); assert_eq!(target, serde_json::from_value(encoded)?); let empty = TuningTarget::Manual { boards: HashMap::new(), }; assert_eq!( empty, serde_json::from_value(serde_json::to_value(&empty)?)? ); Ok(()) } }