refactor: fix hashrate return typing.

This commit is contained in:
Brett Rowan
2024-06-28 08:35:27 -06:00
parent 30f385c2d9
commit 7a3c9a3460
16 changed files with 73 additions and 51 deletions

View File

@@ -19,6 +19,8 @@ from typing import List, Optional, Union
from pyasic.config import MinerConfig, MiningModeConfig from pyasic.config import MinerConfig, MiningModeConfig
from pyasic.data import AlgoHashRate, Fan, HashBoard, HashUnit from pyasic.data import AlgoHashRate, Fan, HashBoard, HashUnit
from pyasic.data.error_codes import MinerErrorData, X19Error from pyasic.data.error_codes import MinerErrorData, X19Error
from pyasic.data.pools import PoolMetrics
from pyasic.errors import APIError
from pyasic.miners.backends.bmminer import BMMiner from pyasic.miners.backends.bmminer import BMMiner
from pyasic.miners.backends.cgminer import CGMiner from pyasic.miners.backends.cgminer import CGMiner
from pyasic.miners.data import ( from pyasic.miners.data import (
@@ -31,8 +33,6 @@ from pyasic.miners.data import (
from pyasic.rpc.antminer import AntminerRPCAPI from pyasic.rpc.antminer import AntminerRPCAPI
from pyasic.ssh.antminer import AntminerModernSSH from pyasic.ssh.antminer import AntminerModernSSH
from pyasic.web.antminer import AntminerModernWebAPI, AntminerOldWebAPI from pyasic.web.antminer import AntminerModernWebAPI, AntminerOldWebAPI
from pyasic.data.pools import PoolMetrics
from pyasic.errors import APIError
ANTMINER_MODERN_DATA_LOC = DataLocations( ANTMINER_MODERN_DATA_LOC = DataLocations(
**{ **{
@@ -95,7 +95,7 @@ class AntminerModern(BMMiner):
web: AntminerModernWebAPI web: AntminerModernWebAPI
_rpc_cls = AntminerRPCAPI _rpc_cls = AntminerRPCAPI
web: AntminerRPCAPI rpc: AntminerRPCAPI
_ssh_cls = AntminerModernSSH _ssh_cls = AntminerModernSSH
ssh: AntminerModernSSH ssh: AntminerModernSSH
@@ -156,7 +156,7 @@ class AntminerModern(BMMiner):
await self.send_config(cfg) await self.send_config(cfg)
return True return True
async def _get_hostname(self, web_get_system_info: dict = None) -> Union[str, None]: async def _get_hostname(self, web_get_system_info: dict = None) -> Optional[str]:
if web_get_system_info is None: if web_get_system_info is None:
try: try:
web_get_system_info = await self.web.get_system_info() web_get_system_info = await self.web.get_system_info()
@@ -169,7 +169,7 @@ class AntminerModern(BMMiner):
except KeyError: except KeyError:
pass pass
async def _get_mac(self, web_get_system_info: dict = None) -> Union[str, None]: async def _get_mac(self, web_get_system_info: dict = None) -> Optional[str]:
if web_get_system_info is None: if web_get_system_info is None:
try: try:
web_get_system_info = await self.web.get_system_info() web_get_system_info = await self.web.get_system_info()
@@ -264,7 +264,9 @@ class AntminerModern(BMMiner):
pass pass
return self.light return self.light
async def _get_expected_hashrate(self, rpc_stats: dict = None) -> Optional[float]: async def _get_expected_hashrate(
self, rpc_stats: dict = None
) -> Optional[AlgoHashRate]:
if rpc_stats is None: if rpc_stats is None:
try: try:
rpc_stats = await self.rpc.stats() rpc_stats = await self.rpc.stats()
@@ -377,8 +379,7 @@ class AntminerModern(BMMiner):
alive=pool_info.get("Status") == "Alive", alive=pool_info.get("Status") == "Alive",
url=pool_info.get("URL"), url=pool_info.get("URL"),
user=pool_info.get("User"), user=pool_info.get("User"),
index=pool_info.get("POOL") index=pool_info.get("POOL"),
) )
pools_data.append(pool_data) pools_data.append(pool_data)
except LookupError: except LookupError:
@@ -446,7 +447,7 @@ class AntminerOld(CGMiner):
self.config = config self.config = config
await self.web.set_miner_conf(config.as_am_old(user_suffix=user_suffix)) await self.web.set_miner_conf(config.as_am_old(user_suffix=user_suffix))
async def _get_mac(self) -> Union[str, None]: async def _get_mac(self) -> Optional[str]:
try: try:
data = await self.web.get_system_info() data = await self.web.get_system_info()
if data: if data:

View File

@@ -236,7 +236,7 @@ class Auradine(StockFirmware):
except LookupError: except LookupError:
pass pass
async def _get_hashrate(self, rpc_summary: dict = None) -> Optional[float]: async def _get_hashrate(self, rpc_summary: dict = None) -> Optional[AlgoHashRate]:
if rpc_summary is None: if rpc_summary is None:
try: try:
rpc_summary = await self.rpc.summary() rpc_summary = await self.rpc.summary()

View File

@@ -173,7 +173,7 @@ class AvalonMiner(CGMiner):
except (KeyError, ValueError): except (KeyError, ValueError):
pass pass
async def _get_hashrate(self, rpc_devs: dict = None) -> Optional[float]: async def _get_hashrate(self, rpc_devs: dict = None) -> Optional[AlgoHashRate]:
if rpc_devs is None: if rpc_devs is None:
try: try:
rpc_devs = await self.rpc.devs() rpc_devs = await self.rpc.devs()
@@ -238,7 +238,9 @@ class AvalonMiner(CGMiner):
return hashboards return hashboards
async def _get_expected_hashrate(self, rpc_stats: dict = None) -> Optional[float]: async def _get_expected_hashrate(
self, rpc_stats: dict = None
) -> Optional[AlgoHashRate]:
if rpc_stats is None: if rpc_stats is None:
try: try:
rpc_stats = await self.rpc.stats() rpc_stats = await self.rpc.stats()

View File

@@ -105,7 +105,7 @@ class BFGMiner(StockFirmware):
return self.fw_ver return self.fw_ver
async def _get_hashrate(self, rpc_summary: dict = None) -> Optional[float]: async def _get_hashrate(self, rpc_summary: dict = None) -> Optional[AlgoHashRate]:
# get hr from API # get hr from API
if rpc_summary is None: if rpc_summary is None:
try: try:
@@ -207,7 +207,9 @@ class BFGMiner(StockFirmware):
return fans return fans
async def _get_expected_hashrate(self, rpc_stats: dict = None) -> Optional[float]: async def _get_expected_hashrate(
self, rpc_stats: dict = None
) -> Optional[AlgoHashRate]:
# X19 method, not sure compatibility # X19 method, not sure compatibility
if rpc_stats is None: if rpc_stats is None:
try: try:

View File

@@ -109,7 +109,7 @@ class BMMiner(StockFirmware):
return self.fw_ver return self.fw_ver
async def _get_hashrate(self, rpc_summary: dict = None) -> Optional[float]: async def _get_hashrate(self, rpc_summary: dict = None) -> Optional[AlgoHashRate]:
# get hr from API # get hr from API
if rpc_summary is None: if rpc_summary is None:
try: try:
@@ -223,7 +223,9 @@ class BMMiner(StockFirmware):
return fans return fans
async def _get_expected_hashrate(self, rpc_stats: dict = None) -> Optional[float]: async def _get_expected_hashrate(
self, rpc_stats: dict = None
) -> Optional[AlgoHashRate]:
# X19 method, not sure compatibility # X19 method, not sure compatibility
if rpc_stats is None: if rpc_stats is None:
try: try:

View File

@@ -26,6 +26,7 @@ from pyasic.config import MinerConfig
from pyasic.config.mining import MiningModePowerTune from pyasic.config.mining import MiningModePowerTune
from pyasic.data import AlgoHashRate, Fan, HashBoard, HashUnit from pyasic.data import AlgoHashRate, Fan, HashBoard, HashUnit
from pyasic.data.error_codes import BraiinsOSError, MinerErrorData from pyasic.data.error_codes import BraiinsOSError, MinerErrorData
from pyasic.data.pools import PoolMetrics
from pyasic.errors import APIError from pyasic.errors import APIError
from pyasic.miners.data import ( from pyasic.miners.data import (
DataFunction, DataFunction,
@@ -39,7 +40,6 @@ from pyasic.rpc.bosminer import BOSMinerRPCAPI
from pyasic.ssh.braiins_os import BOSMinerSSH from pyasic.ssh.braiins_os import BOSMinerSSH
from pyasic.web.braiins_os import BOSerWebAPI, BOSMinerWebAPI from pyasic.web.braiins_os import BOSerWebAPI, BOSMinerWebAPI
from pyasic.web.braiins_os.proto.braiins.bos.v1 import SaveAction from pyasic.web.braiins_os.proto.braiins.bos.v1 import SaveAction
from pyasic.data.pools import PoolMetrics
BOSMINER_DATA_LOC = DataLocations( BOSMINER_DATA_LOC = DataLocations(
**{ **{
@@ -349,7 +349,7 @@ class BOSMiner(BraiinsOSFirmware):
return None return None
return hostname return hostname
async def _get_hashrate(self, rpc_summary: dict = None) -> Optional[float]: async def _get_hashrate(self, rpc_summary: dict = None) -> Optional[AlgoHashRate]:
if rpc_summary is None: if rpc_summary is None:
try: try:
rpc_summary = await self.rpc.summary() rpc_summary = await self.rpc.summary()
@@ -525,7 +525,9 @@ class BOSMiner(BraiinsOSFirmware):
except (TypeError, AttributeError): except (TypeError, AttributeError):
return self.light return self.light
async def _get_expected_hashrate(self, rpc_devs: dict = None) -> Optional[float]: async def _get_expected_hashrate(
self, rpc_devs: dict = None
) -> Optional[AlgoHashRate]:
if rpc_devs is None: if rpc_devs is None:
try: try:
rpc_devs = await self.rpc.devs() rpc_devs = await self.rpc.devs()
@@ -600,14 +602,12 @@ class BOSMiner(BraiinsOSFirmware):
url=pool_info.get("URL"), url=pool_info.get("URL"),
user=pool_info.get("User"), user=pool_info.get("User"),
index=pool_info.get("POOL"), index=pool_info.get("POOL"),
) )
pools_data.append(pool_data) pools_data.append(pool_data)
except LookupError: except LookupError:
pass pass
return pools_data return pools_data
async def upgrade_firmware(self, file: Path): async def upgrade_firmware(self, file: Path):
""" """
Upgrade the firmware of the BOSMiner device. Upgrade the firmware of the BOSMiner device.
@@ -866,7 +866,7 @@ class BOSer(BraiinsOSFirmware):
except LookupError: except LookupError:
pass pass
async def _get_hashrate(self, rpc_summary: dict = None) -> Optional[float]: async def _get_hashrate(self, rpc_summary: dict = None) -> Optional[AlgoHashRate]:
if rpc_summary is None: if rpc_summary is None:
try: try:
rpc_summary = await self.rpc.summary() rpc_summary = await self.rpc.summary()
@@ -883,7 +883,7 @@ class BOSer(BraiinsOSFirmware):
async def _get_expected_hashrate( async def _get_expected_hashrate(
self, grpc_miner_details: dict = None self, grpc_miner_details: dict = None
) -> Optional[float]: ) -> Optional[AlgoHashRate]:
if grpc_miner_details is None: if grpc_miner_details is None:
try: try:
grpc_miner_details = await self.web.get_miner_details() grpc_miner_details = await self.web.get_miner_details()

View File

@@ -15,9 +15,10 @@
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
import logging import logging
from typing import List, Optional
import aiofiles
from pathlib import Path from pathlib import Path
from typing import List, Optional
import aiofiles
from pyasic.config import MinerConfig, MiningModeConfig from pyasic.config import MinerConfig, MiningModeConfig
from pyasic.data import AlgoHashRate, Fan, HashBoard, HashUnit from pyasic.data import AlgoHashRate, Fan, HashBoard, HashUnit
@@ -388,7 +389,7 @@ class BTMiner(StockFirmware):
return hostname return hostname
async def _get_hashrate(self, rpc_summary: dict = None) -> Optional[float]: async def _get_hashrate(self, rpc_summary: dict = None) -> Optional[AlgoHashRate]:
if rpc_summary is None: if rpc_summary is None:
try: try:
rpc_summary = await self.rpc.summary() rpc_summary = await self.rpc.summary()
@@ -564,7 +565,9 @@ class BTMiner(StockFirmware):
pass pass
return errors return errors
async def _get_expected_hashrate(self, rpc_summary: dict = None) -> Optional[float]: async def _get_expected_hashrate(
self, rpc_summary: dict = None
) -> Optional[AlgoHashRate]:
if rpc_summary is None: if rpc_summary is None:
try: try:
rpc_summary = await self.rpc.summary() rpc_summary = await self.rpc.summary()
@@ -675,17 +678,24 @@ class BTMiner(StockFirmware):
result = await self.rpc.update_firmware(upgrade_contents) result = await self.rpc.update_firmware(upgrade_contents)
logging.info("Firmware upgrade process completed successfully for Whatsminer.") logging.info(
"Firmware upgrade process completed successfully for Whatsminer."
)
return result return result
except FileNotFoundError as e: except FileNotFoundError as e:
logging.error(f"File not found during the firmware upgrade process: {e}") logging.error(f"File not found during the firmware upgrade process: {e}")
raise raise
except ValueError as e: except ValueError as e:
logging.error(f"Validation error occurred during the firmware upgrade process: {e}") logging.error(
f"Validation error occurred during the firmware upgrade process: {e}"
)
raise raise
except OSError as e: except OSError as e:
logging.error(f"OS error occurred during the firmware upgrade process: {e}") logging.error(f"OS error occurred during the firmware upgrade process: {e}")
raise raise
except Exception as e: except Exception as e:
logging.error(f"An unexpected error occurred during the firmware upgrade process: {e}", exc_info=True) logging.error(
f"An unexpected error occurred during the firmware upgrade process: {e}",
exc_info=True,
)
raise raise

View File

@@ -109,7 +109,7 @@ class CGMiner(StockFirmware):
return self.fw_ver return self.fw_ver
async def _get_hashrate(self, rpc_summary: dict = None) -> Optional[float]: async def _get_hashrate(self, rpc_summary: dict = None) -> Optional[AlgoHashRate]:
if rpc_summary is None: if rpc_summary is None:
try: try:
rpc_summary = await self.rpc.summary() rpc_summary = await self.rpc.summary()

View File

@@ -19,9 +19,9 @@ from typing import List, Optional
from pyasic.config import MinerConfig from pyasic.config import MinerConfig
from pyasic.data import AlgoHashRate, Fan, HashBoard, HashUnit from pyasic.data import AlgoHashRate, Fan, HashBoard, HashUnit
from pyasic.data.error_codes import MinerErrorData, X19Error from pyasic.data.error_codes import MinerErrorData, X19Error
from pyasic.data.pools import PoolMetrics
from pyasic.errors import APIError from pyasic.errors import APIError
from pyasic.logger import logger from pyasic.logger import logger
from pyasic.data.pools import PoolMetrics
from pyasic.miners.data import DataFunction, DataLocations, DataOptions, WebAPICommand from pyasic.miners.data import DataFunction, DataLocations, DataOptions, WebAPICommand
from pyasic.miners.device.firmware import ePICFirmware from pyasic.miners.device.firmware import ePICFirmware
from pyasic.web.epic import ePICWebAPI from pyasic.web.epic import ePICWebAPI
@@ -220,7 +220,7 @@ class ePIC(ePICFirmware):
except KeyError: except KeyError:
pass pass
async def _get_hashrate(self, web_summary: dict = None) -> Optional[float]: async def _get_hashrate(self, web_summary: dict = None) -> Optional[AlgoHashRate]:
if web_summary is None: if web_summary is None:
try: try:
web_summary = await self.web.summary() web_summary = await self.web.summary()
@@ -239,7 +239,9 @@ class ePIC(ePICFirmware):
except (LookupError, ValueError, TypeError): except (LookupError, ValueError, TypeError):
pass pass
async def _get_expected_hashrate(self, web_summary: dict = None) -> Optional[float]: async def _get_expected_hashrate(
self, web_summary: dict = None
) -> Optional[AlgoHashRate]:
if web_summary is None: if web_summary is None:
try: try:
web_summary = await self.web.summary() web_summary = await self.web.summary()

View File

@@ -169,7 +169,7 @@ class Innosilicon(CGMiner):
async def _get_hashrate( async def _get_hashrate(
self, rpc_summary: dict = None, web_get_all: dict = None self, rpc_summary: dict = None, web_get_all: dict = None
) -> Optional[float]: ) -> Optional[AlgoHashRate]:
if web_get_all: if web_get_all:
web_get_all = web_get_all["all"] web_get_all = web_get_all["all"]

View File

@@ -162,7 +162,7 @@ class LUXMiner(LuxOSFirmware):
return mac return mac
async def _get_hashrate(self, rpc_summary: dict = None) -> Optional[float]: async def _get_hashrate(self, rpc_summary: dict = None) -> Optional[AlgoHashRate]:
if rpc_summary is None: if rpc_summary is None:
try: try:
rpc_summary = await self.rpc.summary() rpc_summary = await self.rpc.summary()
@@ -263,7 +263,9 @@ class LUXMiner(LuxOSFirmware):
fans.append(Fan()) fans.append(Fan())
return fans return fans
async def _get_expected_hashrate(self, rpc_stats: dict = None) -> Optional[float]: async def _get_expected_hashrate(
self, rpc_stats: dict = None
) -> Optional[AlgoHashRate]:
if rpc_stats is None: if rpc_stats is None:
try: try:
rpc_stats = await self.rpc.stats() rpc_stats = await self.rpc.stats()

View File

@@ -225,7 +225,7 @@ class MaraMiner(MaraFirmware):
except LookupError: except LookupError:
pass pass
async def _get_hashrate(self, web_brief: dict = None) -> Optional[float]: async def _get_hashrate(self, web_brief: dict = None) -> Optional[AlgoHashRate]:
if web_brief is None: if web_brief is None:
try: try:
web_brief = await self.web.brief() web_brief = await self.web.brief()
@@ -271,7 +271,9 @@ class MaraMiner(MaraFirmware):
pass pass
return False return False
async def _get_expected_hashrate(self, web_brief: dict = None) -> Optional[float]: async def _get_expected_hashrate(
self, web_brief: dict = None
) -> Optional[AlgoHashRate]:
if web_brief is None: if web_brief is None:
try: try:
web_brief = await self.web.brief() web_brief = await self.web.brief()
@@ -288,7 +290,7 @@ class MaraMiner(MaraFirmware):
async def _get_wattage_limit( async def _get_wattage_limit(
self, web_miner_config: dict = None self, web_miner_config: dict = None
) -> Optional[float]: ) -> Optional[AlgoHashRate]:
if web_miner_config is None: if web_miner_config is None:
try: try:
web_miner_config = await self.web.get_miner_config() web_miner_config = await self.web.get_miner_config()

View File

@@ -15,7 +15,7 @@
from typing import List, Optional, Tuple from typing import List, Optional, Tuple
from pyasic.config import MinerConfig from pyasic.config import MinerConfig
from pyasic.data import Fan, HashBoard from pyasic.data import AlgoHashRate, Fan, HashBoard
from pyasic.data.error_codes import MinerErrorData from pyasic.data.error_codes import MinerErrorData
from pyasic.miners.base import BaseMiner from pyasic.miners.base import BaseMiner
from pyasic.rpc.unknown import UnknownRPCAPI from pyasic.rpc.unknown import UnknownRPCAPI
@@ -80,7 +80,7 @@ class UnknownMiner(BaseMiner):
async def _get_hostname(self) -> Optional[str]: async def _get_hostname(self) -> Optional[str]:
return None return None
async def _get_hashrate(self) -> Optional[float]: async def _get_hashrate(self) -> Optional[AlgoHashRate]:
return None return None
async def _get_hashboards(self) -> List[HashBoard]: async def _get_hashboards(self) -> List[HashBoard]:
@@ -113,7 +113,7 @@ class UnknownMiner(BaseMiner):
async def _get_fault_light(self) -> bool: async def _get_fault_light(self) -> bool:
return False return False
async def _get_expected_hashrate(self) -> Optional[float]: async def _get_expected_hashrate(self) -> Optional[AlgoHashRate]:
return None return None
async def _is_mining(self, *args, **kwargs) -> Optional[bool]: async def _is_mining(self, *args, **kwargs) -> Optional[bool]:

View File

@@ -193,7 +193,7 @@ class VNish(VNishFirmware, BMMiner):
except KeyError: except KeyError:
pass pass
async def _get_hashrate(self, rpc_summary: dict = None) -> Optional[float]: async def _get_hashrate(self, rpc_summary: dict = None) -> Optional[AlgoHashRate]:
# get hr from API # get hr from API
if rpc_summary is None: if rpc_summary is None:
try: try:

View File

@@ -19,7 +19,7 @@ import warnings
from typing import List, Optional, Protocol, Tuple, Type, TypeVar, Union from typing import List, Optional, Protocol, Tuple, Type, TypeVar, Union
from pyasic.config import MinerConfig from pyasic.config import MinerConfig
from pyasic.data import Fan, HashBoard, MinerData from pyasic.data import AlgoHashRate, Fan, HashBoard, MinerData
from pyasic.data.device import DeviceInfo from pyasic.data.device import DeviceInfo
from pyasic.data.error_codes import MinerErrorData from pyasic.data.error_codes import MinerErrorData
from pyasic.data.pools import PoolMetrics from pyasic.data.pools import PoolMetrics
@@ -238,7 +238,7 @@ class MinerProtocol(Protocol):
""" """
return await self._get_hostname() return await self._get_hostname()
async def get_hashrate(self) -> Optional[float]: async def get_hashrate(self) -> Optional[AlgoHashRate]:
"""Get the hashrate of the miner and return it as a float in TH/s. """Get the hashrate of the miner and return it as a float in TH/s.
Returns: Returns:
@@ -318,7 +318,7 @@ class MinerProtocol(Protocol):
""" """
return await self._get_fault_light() return await self._get_fault_light()
async def get_expected_hashrate(self) -> Optional[float]: async def get_expected_hashrate(self) -> Optional[AlgoHashRate]:
"""Get the nominal hashrate from factory if available. """Get the nominal hashrate from factory if available.
Returns: Returns:
@@ -362,7 +362,7 @@ class MinerProtocol(Protocol):
async def _get_hostname(self) -> Optional[str]: async def _get_hostname(self) -> Optional[str]:
pass pass
async def _get_hashrate(self) -> Optional[float]: async def _get_hashrate(self) -> Optional[AlgoHashRate]:
pass pass
async def _get_hashboards(self) -> List[HashBoard]: async def _get_hashboards(self) -> List[HashBoard]:
@@ -392,7 +392,7 @@ class MinerProtocol(Protocol):
async def _get_fault_light(self) -> Optional[bool]: async def _get_fault_light(self) -> Optional[bool]:
pass pass
async def _get_expected_hashrate(self) -> Optional[float]: async def _get_expected_hashrate(self) -> Optional[AlgoHashRate]:
pass pass
async def _is_mining(self) -> Optional[bool]: async def _is_mining(self) -> Optional[bool]:
@@ -561,4 +561,4 @@ class BaseMiner(MinerProtocol):
self.ssh = self._ssh_cls(ip) self.ssh = self._ssh_cls(ip)
AnyMiner = TypeVar("AnyMiner", bound=BaseMiner) AnyMiner = TypeVar("AnyMiner", bound=BaseMiner)

View File

@@ -55,7 +55,6 @@ class MinersTest(unittest.TestCase):
"expected_hashrate", "expected_hashrate",
"uptime", "uptime",
"wattage", "wattage",
"voltage",
"wattage_limit", "wattage_limit",
"pools", "pools",
] ]