refactor: more optimizations to hashrate types with metaclass

This commit is contained in:
Upstream Data
2024-11-21 15:30:01 -07:00
parent 82552390c8
commit 01b96227e0
24 changed files with 129 additions and 457 deletions

View File

@@ -12,7 +12,7 @@ class DeviceInfo(BaseModel):
make: MinerMake | None = None make: MinerMake | None = None
model: MinerModelType | None = None model: MinerModelType | None = None
firmware: MinerFirmware | None = None firmware: MinerFirmware | None = None
algo: MinerAlgoType | None = None algo: type[MinerAlgoType] | None = None
@field_serializer("make") @field_serializer("make")
def serialize_make(self, make: MinerMake, _info): def serialize_make(self, make: MinerMake, _info):

View File

@@ -4,6 +4,13 @@ from .hashrate.base import AlgoHashRateType
from .hashrate.unit.base import AlgoHashRateUnitType from .hashrate.unit.base import AlgoHashRateUnitType
class MinerAlgoType(str): class MinerAlgoMeta(type):
name: str
def __str__(cls):
return cls.name
class MinerAlgoType(metaclass=MinerAlgoMeta):
hashrate: type[AlgoHashRateType] hashrate: type[AlgoHashRateType]
unit: type[AlgoHashRateUnitType] unit: type[AlgoHashRateUnitType]

View File

@@ -6,12 +6,8 @@ from .hashrate.unit import Blake256Unit
# make this json serializable # make this json serializable
class _Blake256Algo(MinerAlgoType): class Blake256Algo(MinerAlgoType):
hashrate = Blake256HashRate hashrate: type[Blake256HashRate] = Blake256HashRate
unit = Blake256Unit unit: type[Blake256Unit] = Blake256Unit
def __repr__(self): name = "Blake256"
return "Blake256Algo"
Blake256Algo = _Blake256Algo("Blake256")

View File

@@ -6,12 +6,8 @@ from .hashrate.unit import EaglesongUnit
# make this json serializable # make this json serializable
class _EaglesongAlgo(MinerAlgoType): class EaglesongAlgo(MinerAlgoType):
hashrate = EaglesongHashRate hashrate: type[EaglesongHashRate] = EaglesongHashRate
unit = EaglesongUnit unit: type[EaglesongUnit] = EaglesongUnit
def __repr__(self): name = "Eaglesong"
return "EaglesongAlgo"
EaglesongAlgo = _EaglesongAlgo("Eaglesong")

View File

@@ -6,12 +6,8 @@ from .hashrate.unit import EquihashUnit
# make this json serializable # make this json serializable
class _EquihashAlgo(MinerAlgoType): class EquihashAlgo(MinerAlgoType):
hashrate = EquihashHashRate hashrate: type[EquihashHashRate] = EquihashHashRate
unit = EquihashUnit unit: type[EquihashUnit] = EquihashUnit
def __repr__(self): name = "Equihash"
return "EquihashAlgo"
EquihashAlgo = _EquihashAlgo("Equihash")

View File

@@ -5,13 +5,8 @@ from .hashrate import EtHashHashRate
from .hashrate.unit import EtHashUnit from .hashrate.unit import EtHashUnit
# make this json serializable class EtHashAlgo(MinerAlgoType):
class _EtHashAlgo(MinerAlgoType): hashrate: type[EtHashHashRate] = EtHashHashRate
hashrate = EtHashHashRate unit: type[EtHashUnit] = EtHashUnit
unit = EtHashUnit
def __repr__(self): name = "EtHash"
return "EtHashAlgo"
EtHashAlgo = _EtHashAlgo("EtHash")

View File

@@ -6,12 +6,8 @@ from .hashrate.unit import HandshakeUnit
# make this json serializable # make this json serializable
class _HandshakeAlgo(MinerAlgoType): class HandshakeAlgo(MinerAlgoType):
hashrate = HandshakeHashRate hashrate: type[HandshakeHashRate] = HandshakeHashRate
unit = HandshakeUnit unit: type[HandshakeUnit] = HandshakeUnit
def __repr__(self): name = "Handshake"
return "HandshakeAlgo"
HandshakeAlgo = _HandshakeAlgo("Handshake")

View File

@@ -1,6 +1,7 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Self
from pydantic import BaseModel from pydantic import BaseModel, Field
from .unit.base import AlgoHashRateUnitType from .unit.base import AlgoHashRateUnitType
@@ -24,3 +25,38 @@ class AlgoHashRateType(BaseModel, ABC):
def __round__(self, n: int = None): def __round__(self, n: int = None):
return round(self.rate, n) return round(self.rate, n)
def __add__(self, other: Self | int | float) -> Self:
if isinstance(other, self.__class__):
return self.__class__(
rate=self.rate + other.into(self.unit).rate, unit=self.unit
)
return self.__class__(rate=self.rate + other, unit=self.unit)
def __sub__(self, other: Self | int | float) -> Self:
if isinstance(other, self.__class__):
return self.__class__(
rate=self.rate - other.into(self.unit).rate, unit=self.unit
)
return self.__class__(rate=self.rate - other, unit=self.unit)
def __truediv__(self, other: Self | int | float) -> Self:
if isinstance(other, self.__class__):
return self.__class__(
rate=self.rate / other.into(self.unit).rate, unit=self.unit
)
return self.__class__(rate=self.rate / other, unit=self.unit)
def __floordiv__(self, other: Self | int | float) -> Self:
if isinstance(other, self.__class__):
return self.__class__(
rate=self.rate // other.into(self.unit).rate, unit=self.unit
)
return self.__class__(rate=self.rate // other, unit=self.unit)
def __mul__(self, other: Self | int | float) -> Self:
if isinstance(other, self.__class__):
return self.__class__(
rate=self.rate * other.into(self.unit).rate, unit=self.unit
)
return self.__class__(rate=self.rate * other, unit=self.unit)

View File

@@ -1,5 +1,7 @@
from __future__ import annotations from __future__ import annotations
from typing import Self
from pyasic.device.algorithm.hashrate.base import AlgoHashRateType from pyasic.device.algorithm.hashrate.base import AlgoHashRateType
from pyasic.device.algorithm.hashrate.unit.blake256 import Blake256Unit from pyasic.device.algorithm.hashrate.unit.blake256 import Blake256Unit
@@ -10,42 +12,7 @@ class Blake256HashRate(AlgoHashRateType):
rate: float rate: float
unit: Blake256Unit = HashUnit.BLAKE256.default unit: Blake256Unit = HashUnit.BLAKE256.default
def __add__(self, other: Blake256HashRate | int | float) -> Blake256HashRate: def into(self, other: Blake256Unit) -> Self:
if isinstance(other, Blake256HashRate): return self.__class__(
return Blake256HashRate(
rate=self.rate + other.into(self.unit).rate, unit=self.unit
)
return Blake256HashRate(rate=self.rate + other, unit=self.unit)
def __sub__(self, other: Blake256HashRate | int | float) -> Blake256HashRate:
if isinstance(other, Blake256HashRate):
return Blake256HashRate(
rate=self.rate - other.into(self.unit).rate, unit=self.unit
)
return Blake256HashRate(rate=self.rate - other, unit=self.unit)
def __truediv__(self, other: Blake256HashRate | int | float):
if isinstance(other, Blake256HashRate):
return Blake256HashRate(
rate=self.rate / other.into(self.unit).rate, unit=self.unit
)
return Blake256HashRate(rate=self.rate / other, unit=self.unit)
def __floordiv__(self, other: Blake256HashRate | int | float):
if isinstance(other, Blake256HashRate):
return Blake256HashRate(
rate=self.rate // other.into(self.unit).rate, unit=self.unit
)
return Blake256HashRate(rate=self.rate // other, unit=self.unit)
def __mul__(self, other: Blake256HashRate | int | float):
if isinstance(other, Blake256HashRate):
return Blake256HashRate(
rate=self.rate * other.into(self.unit).rate, unit=self.unit
)
return Blake256HashRate(rate=self.rate * other, unit=self.unit)
def into(self, other: Blake256Unit) -> Blake256HashRate:
return Blake256HashRate(
rate=self.rate / (other.value / self.unit.value), unit=other rate=self.rate / (other.value / self.unit.value), unit=other
) )

View File

@@ -1,5 +1,7 @@
from __future__ import annotations from __future__ import annotations
from typing import Self
from pyasic.device.algorithm.hashrate.base import AlgoHashRateType from pyasic.device.algorithm.hashrate.base import AlgoHashRateType
from pyasic.device.algorithm.hashrate.unit.eaglesong import EaglesongUnit from pyasic.device.algorithm.hashrate.unit.eaglesong import EaglesongUnit
@@ -10,42 +12,7 @@ class EaglesongHashRate(AlgoHashRateType):
rate: float rate: float
unit: EaglesongUnit = HashUnit.EAGLESONG.default unit: EaglesongUnit = HashUnit.EAGLESONG.default
def __add__(self, other: EaglesongHashRate | int | float) -> EaglesongHashRate: def into(self, other: EaglesongUnit) -> Self:
if isinstance(other, EaglesongHashRate): return self.__class__(
return EaglesongHashRate(
rate=self.rate + other.into(self.unit).rate, unit=self.unit
)
return EaglesongHashRate(rate=self.rate + other, unit=self.unit)
def __sub__(self, other: EaglesongHashRate | int | float) -> EaglesongHashRate:
if isinstance(other, EaglesongHashRate):
return EaglesongHashRate(
rate=self.rate - other.into(self.unit).rate, unit=self.unit
)
return EaglesongHashRate(rate=self.rate - other, unit=self.unit)
def __truediv__(self, other: EaglesongHashRate | int | float):
if isinstance(other, EaglesongHashRate):
return EaglesongHashRate(
rate=self.rate / other.into(self.unit).rate, unit=self.unit
)
return EaglesongHashRate(rate=self.rate / other, unit=self.unit)
def __floordiv__(self, other: EaglesongHashRate | int | float):
if isinstance(other, EaglesongHashRate):
return EaglesongHashRate(
rate=self.rate // other.into(self.unit).rate, unit=self.unit
)
return EaglesongHashRate(rate=self.rate // other, unit=self.unit)
def __mul__(self, other: EaglesongHashRate | int | float):
if isinstance(other, EaglesongHashRate):
return EaglesongHashRate(
rate=self.rate * other.into(self.unit).rate, unit=self.unit
)
return EaglesongHashRate(rate=self.rate * other, unit=self.unit)
def into(self, other: EaglesongUnit) -> EaglesongHashRate:
return EaglesongHashRate(
rate=self.rate / (other.value / self.unit.value), unit=other rate=self.rate / (other.value / self.unit.value), unit=other
) )

View File

@@ -1,5 +1,7 @@
from __future__ import annotations from __future__ import annotations
from typing import Self
from pyasic.device.algorithm.hashrate.base import AlgoHashRateType from pyasic.device.algorithm.hashrate.base import AlgoHashRateType
from pyasic.device.algorithm.hashrate.unit.equihash import EquihashUnit from pyasic.device.algorithm.hashrate.unit.equihash import EquihashUnit
@@ -10,42 +12,7 @@ class EquihashHashRate(AlgoHashRateType):
rate: float rate: float
unit: EquihashUnit = HashUnit.ETHASH.default unit: EquihashUnit = HashUnit.ETHASH.default
def __add__(self, other: EquihashHashRate | int | float) -> EquihashHashRate: def into(self, other: EquihashUnit) -> Self:
if isinstance(other, EquihashHashRate): return self.__class__(
return EquihashHashRate(
rate=self.rate + other.into(self.unit).rate, unit=self.unit
)
return EquihashHashRate(rate=self.rate + other, unit=self.unit)
def __sub__(self, other: EquihashHashRate | int | float) -> EquihashHashRate:
if isinstance(other, EquihashHashRate):
return EquihashHashRate(
rate=self.rate - other.into(self.unit).rate, unit=self.unit
)
return EquihashHashRate(rate=self.rate - other, unit=self.unit)
def __truediv__(self, other: EquihashHashRate | int | float):
if isinstance(other, EquihashHashRate):
return EquihashHashRate(
rate=self.rate / other.into(self.unit).rate, unit=self.unit
)
return EquihashHashRate(rate=self.rate / other, unit=self.unit)
def __floordiv__(self, other: EquihashHashRate | int | float):
if isinstance(other, EquihashHashRate):
return EquihashHashRate(
rate=self.rate // other.into(self.unit).rate, unit=self.unit
)
return EquihashHashRate(rate=self.rate // other, unit=self.unit)
def __mul__(self, other: EquihashHashRate | int | float):
if isinstance(other, EquihashHashRate):
return EquihashHashRate(
rate=self.rate * other.into(self.unit).rate, unit=self.unit
)
return EquihashHashRate(rate=self.rate * other, unit=self.unit)
def into(self, other: EquihashUnit) -> EquihashHashRate:
return EquihashHashRate(
rate=self.rate / (other.value / self.unit.value), unit=other rate=self.rate / (other.value / self.unit.value), unit=other
) )

View File

@@ -1,5 +1,7 @@
from __future__ import annotations from __future__ import annotations
from typing import Self
from pyasic.device.algorithm.hashrate.base import AlgoHashRateType from pyasic.device.algorithm.hashrate.base import AlgoHashRateType
from pyasic.device.algorithm.hashrate.unit.ethash import EtHashUnit from pyasic.device.algorithm.hashrate.unit.ethash import EtHashUnit
@@ -10,42 +12,7 @@ class EtHashHashRate(AlgoHashRateType):
rate: float rate: float
unit: EtHashUnit = HashUnit.ETHASH.default unit: EtHashUnit = HashUnit.ETHASH.default
def __add__(self, other: EtHashHashRate | int | float) -> EtHashHashRate: def into(self, other: EtHashUnit) -> Self:
if isinstance(other, EtHashHashRate): return self.__class__(
return EtHashHashRate(
rate=self.rate + other.into(self.unit).rate, unit=self.unit
)
return EtHashHashRate(rate=self.rate + other, unit=self.unit)
def __sub__(self, other: EtHashHashRate | int | float) -> EtHashHashRate:
if isinstance(other, EtHashHashRate):
return EtHashHashRate(
rate=self.rate - other.into(self.unit).rate, unit=self.unit
)
return EtHashHashRate(rate=self.rate - other, unit=self.unit)
def __truediv__(self, other: EtHashHashRate | int | float):
if isinstance(other, EtHashHashRate):
return EtHashHashRate(
rate=self.rate / other.into(self.unit).rate, unit=self.unit
)
return EtHashHashRate(rate=self.rate / other, unit=self.unit)
def __floordiv__(self, other: EtHashHashRate | int | float):
if isinstance(other, EtHashHashRate):
return EtHashHashRate(
rate=self.rate // other.into(self.unit).rate, unit=self.unit
)
return EtHashHashRate(rate=self.rate // other, unit=self.unit)
def __mul__(self, other: EtHashHashRate | int | float):
if isinstance(other, EtHashHashRate):
return EtHashHashRate(
rate=self.rate * other.into(self.unit).rate, unit=self.unit
)
return EtHashHashRate(rate=self.rate * other, unit=self.unit)
def into(self, other: EtHashUnit) -> EtHashHashRate:
return EtHashHashRate(
rate=self.rate / (other.value / self.unit.value), unit=other rate=self.rate / (other.value / self.unit.value), unit=other
) )

View File

@@ -1,5 +1,7 @@
from __future__ import annotations from __future__ import annotations
from typing import Self
from pyasic.device.algorithm.hashrate.base import AlgoHashRateType from pyasic.device.algorithm.hashrate.base import AlgoHashRateType
from pyasic.device.algorithm.hashrate.unit.handshake import HandshakeUnit from pyasic.device.algorithm.hashrate.unit.handshake import HandshakeUnit
@@ -10,42 +12,7 @@ class HandshakeHashRate(AlgoHashRateType):
rate: float rate: float
unit: HandshakeUnit = HashUnit.HANDSHAKE.default unit: HandshakeUnit = HashUnit.HANDSHAKE.default
def __add__(self, other: HandshakeHashRate | int | float) -> HandshakeHashRate: def into(self, other: HandshakeUnit) -> Self:
if isinstance(other, HandshakeHashRate): return self.__class__(
return HandshakeHashRate(
rate=self.rate + other.into(self.unit).rate, unit=self.unit
)
return HandshakeHashRate(rate=self.rate + other, unit=self.unit)
def __sub__(self, other: HandshakeHashRate | int | float) -> HandshakeHashRate:
if isinstance(other, HandshakeHashRate):
return HandshakeHashRate(
rate=self.rate - other.into(self.unit).rate, unit=self.unit
)
return HandshakeHashRate(rate=self.rate - other, unit=self.unit)
def __truediv__(self, other: HandshakeHashRate | int | float):
if isinstance(other, HandshakeHashRate):
return HandshakeHashRate(
rate=self.rate / other.into(self.unit).rate, unit=self.unit
)
return HandshakeHashRate(rate=self.rate / other, unit=self.unit)
def __floordiv__(self, other: HandshakeHashRate | int | float):
if isinstance(other, HandshakeHashRate):
return HandshakeHashRate(
rate=self.rate // other.into(self.unit).rate, unit=self.unit
)
return HandshakeHashRate(rate=self.rate // other, unit=self.unit)
def __mul__(self, other: HandshakeHashRate | int | float):
if isinstance(other, HandshakeHashRate):
return HandshakeHashRate(
rate=self.rate * other.into(self.unit).rate, unit=self.unit
)
return HandshakeHashRate(rate=self.rate * other, unit=self.unit)
def into(self, other: HandshakeUnit) -> HandshakeHashRate:
return HandshakeHashRate(
rate=self.rate / (other.value / self.unit.value), unit=other rate=self.rate / (other.value / self.unit.value), unit=other
) )

View File

@@ -1,5 +1,7 @@
from __future__ import annotations from __future__ import annotations
from typing import Self
from pyasic.device.algorithm.hashrate.base import AlgoHashRateType from pyasic.device.algorithm.hashrate.base import AlgoHashRateType
from pyasic.device.algorithm.hashrate.unit.kadena import KadenaUnit from pyasic.device.algorithm.hashrate.unit.kadena import KadenaUnit
@@ -10,42 +12,7 @@ class KadenaHashRate(AlgoHashRateType):
rate: float rate: float
unit: KadenaUnit = HashUnit.KADENA.default unit: KadenaUnit = HashUnit.KADENA.default
def __add__(self, other: KadenaHashRate | int | float) -> KadenaHashRate: def into(self, other: KadenaUnit) -> Self:
if isinstance(other, KadenaHashRate): return self.__class__(
return KadenaHashRate(
rate=self.rate + other.into(self.unit).rate, unit=self.unit
)
return KadenaHashRate(rate=self.rate + other, unit=self.unit)
def __sub__(self, other: KadenaHashRate | int | float) -> KadenaHashRate:
if isinstance(other, KadenaHashRate):
return KadenaHashRate(
rate=self.rate - other.into(self.unit).rate, unit=self.unit
)
return KadenaHashRate(rate=self.rate - other, unit=self.unit)
def __truediv__(self, other: KadenaHashRate | int | float):
if isinstance(other, KadenaHashRate):
return KadenaHashRate(
rate=self.rate / other.into(self.unit).rate, unit=self.unit
)
return KadenaHashRate(rate=self.rate / other, unit=self.unit)
def __floordiv__(self, other: KadenaHashRate | int | float):
if isinstance(other, KadenaHashRate):
return KadenaHashRate(
rate=self.rate // other.into(self.unit).rate, unit=self.unit
)
return KadenaHashRate(rate=self.rate // other, unit=self.unit)
def __mul__(self, other: KadenaHashRate | int | float):
if isinstance(other, KadenaHashRate):
return KadenaHashRate(
rate=self.rate * other.into(self.unit).rate, unit=self.unit
)
return KadenaHashRate(rate=self.rate * other, unit=self.unit)
def into(self, other: KadenaUnit) -> KadenaHashRate:
return KadenaHashRate(
rate=self.rate / (other.value / self.unit.value), unit=other rate=self.rate / (other.value / self.unit.value), unit=other
) )

View File

@@ -1,5 +1,7 @@
from __future__ import annotations from __future__ import annotations
from typing import Self
from pyasic.device.algorithm.hashrate.base import AlgoHashRateType from pyasic.device.algorithm.hashrate.base import AlgoHashRateType
from pyasic.device.algorithm.hashrate.unit.kheavyhash import KHeavyHashUnit from pyasic.device.algorithm.hashrate.unit.kheavyhash import KHeavyHashUnit
@@ -10,42 +12,7 @@ class KHeavyHashHashRate(AlgoHashRateType):
rate: float rate: float
unit: KHeavyHashUnit = HashUnit.KHEAVYHASH.default unit: KHeavyHashUnit = HashUnit.KHEAVYHASH.default
def __add__(self, other: KHeavyHashHashRate | int | float) -> KHeavyHashHashRate: def into(self, other: KHeavyHashUnit) -> Self:
if isinstance(other, KHeavyHashHashRate): return self.__class__(
return KHeavyHashHashRate(
rate=self.rate + other.into(self.unit).rate, unit=self.unit
)
return KHeavyHashHashRate(rate=self.rate + other, unit=self.unit)
def __sub__(self, other: KHeavyHashHashRate | int | float) -> KHeavyHashHashRate:
if isinstance(other, KHeavyHashHashRate):
return KHeavyHashHashRate(
rate=self.rate - other.into(self.unit).rate, unit=self.unit
)
return KHeavyHashHashRate(rate=self.rate - other, unit=self.unit)
def __truediv__(self, other: KHeavyHashHashRate | int | float):
if isinstance(other, KHeavyHashHashRate):
return KHeavyHashHashRate(
rate=self.rate / other.into(self.unit).rate, unit=self.unit
)
return KHeavyHashHashRate(rate=self.rate / other, unit=self.unit)
def __floordiv__(self, other: KHeavyHashHashRate | int | float):
if isinstance(other, KHeavyHashHashRate):
return KHeavyHashHashRate(
rate=self.rate // other.into(self.unit).rate, unit=self.unit
)
return KHeavyHashHashRate(rate=self.rate // other, unit=self.unit)
def __mul__(self, other: KHeavyHashHashRate | int | float):
if isinstance(other, KHeavyHashHashRate):
return KHeavyHashHashRate(
rate=self.rate * other.into(self.unit).rate, unit=self.unit
)
return KHeavyHashHashRate(rate=self.rate * other, unit=self.unit)
def into(self, other: KHeavyHashUnit) -> KHeavyHashHashRate:
return KHeavyHashHashRate(
rate=self.rate / (other.value / self.unit.value), unit=other rate=self.rate / (other.value / self.unit.value), unit=other
) )

View File

@@ -1,5 +1,7 @@
from __future__ import annotations from __future__ import annotations
from typing import Self
from pyasic.device.algorithm.hashrate.base import AlgoHashRateType from pyasic.device.algorithm.hashrate.base import AlgoHashRateType
from pyasic.device.algorithm.hashrate.unit.scrypt import ScryptUnit from pyasic.device.algorithm.hashrate.unit.scrypt import ScryptUnit
@@ -10,42 +12,7 @@ class ScryptHashRate(AlgoHashRateType):
rate: float rate: float
unit: ScryptUnit = HashUnit.SCRYPT.default unit: ScryptUnit = HashUnit.SCRYPT.default
def __add__(self, other: ScryptHashRate | int | float) -> ScryptHashRate: def into(self, other: ScryptUnit) -> Self:
if isinstance(other, ScryptHashRate): return self.__class__(
return ScryptHashRate(
rate=self.rate + other.into(self.unit).rate, unit=self.unit
)
return ScryptHashRate(rate=self.rate + other, unit=self.unit)
def __sub__(self, other: ScryptHashRate | int | float) -> ScryptHashRate:
if isinstance(other, ScryptHashRate):
return ScryptHashRate(
rate=self.rate - other.into(self.unit).rate, unit=self.unit
)
return ScryptHashRate(rate=self.rate - other, unit=self.unit)
def __truediv__(self, other: ScryptHashRate | int | float):
if isinstance(other, ScryptHashRate):
return ScryptHashRate(
rate=self.rate / other.into(self.unit).rate, unit=self.unit
)
return ScryptHashRate(rate=self.rate / other, unit=self.unit)
def __floordiv__(self, other: ScryptHashRate | int | float):
if isinstance(other, ScryptHashRate):
return ScryptHashRate(
rate=self.rate // other.into(self.unit).rate, unit=self.unit
)
return ScryptHashRate(rate=self.rate // other, unit=self.unit)
def __mul__(self, other: ScryptHashRate | int | float):
if isinstance(other, ScryptHashRate):
return ScryptHashRate(
rate=self.rate * other.into(self.unit).rate, unit=self.unit
)
return ScryptHashRate(rate=self.rate * other, unit=self.unit)
def into(self, other: ScryptUnit) -> ScryptHashRate:
return ScryptHashRate(
rate=self.rate / (other.value / self.unit.value), unit=other rate=self.rate / (other.value / self.unit.value), unit=other
) )

View File

@@ -1,5 +1,7 @@
from __future__ import annotations from __future__ import annotations
from typing import Self
from pyasic.device.algorithm.hashrate.base import AlgoHashRateType from pyasic.device.algorithm.hashrate.base import AlgoHashRateType
from pyasic.device.algorithm.hashrate.unit.sha256 import SHA256Unit from pyasic.device.algorithm.hashrate.unit.sha256 import SHA256Unit
@@ -10,42 +12,7 @@ class SHA256HashRate(AlgoHashRateType):
rate: float rate: float
unit: SHA256Unit = HashUnit.SHA256.default unit: SHA256Unit = HashUnit.SHA256.default
def __add__(self, other: SHA256HashRate | int | float) -> SHA256HashRate: def into(self, other: SHA256Unit) -> Self:
if isinstance(other, SHA256HashRate): return self.__class__(
return SHA256HashRate(
rate=self.rate + other.into(self.unit).rate, unit=self.unit
)
return SHA256HashRate(rate=self.rate + other, unit=self.unit)
def __sub__(self, other: SHA256HashRate | int | float) -> SHA256HashRate:
if isinstance(other, SHA256HashRate):
return SHA256HashRate(
rate=self.rate - other.into(self.unit).rate, unit=self.unit
)
return SHA256HashRate(rate=self.rate - other, unit=self.unit)
def __truediv__(self, other: SHA256HashRate | int | float):
if isinstance(other, SHA256HashRate):
return SHA256HashRate(
rate=self.rate / other.into(self.unit).rate, unit=self.unit
)
return SHA256HashRate(rate=self.rate / other, unit=self.unit)
def __floordiv__(self, other: SHA256HashRate | int | float):
if isinstance(other, SHA256HashRate):
return SHA256HashRate(
rate=self.rate // other.into(self.unit).rate, unit=self.unit
)
return SHA256HashRate(rate=self.rate // other, unit=self.unit)
def __mul__(self, other: SHA256HashRate | int | float):
if isinstance(other, SHA256HashRate):
return SHA256HashRate(
rate=self.rate * other.into(self.unit).rate, unit=self.unit
)
return SHA256HashRate(rate=self.rate * other, unit=self.unit)
def into(self, other: SHA256Unit) -> SHA256HashRate:
return SHA256HashRate(
rate=self.rate / (other.value / self.unit.value), unit=other rate=self.rate / (other.value / self.unit.value), unit=other
) )

View File

@@ -1,5 +1,7 @@
from __future__ import annotations from __future__ import annotations
from typing import Self
from pyasic.device.algorithm.hashrate.base import AlgoHashRateType from pyasic.device.algorithm.hashrate.base import AlgoHashRateType
from pyasic.device.algorithm.hashrate.unit.x11 import X11Unit from pyasic.device.algorithm.hashrate.unit.x11 import X11Unit
@@ -10,40 +12,7 @@ class X11HashRate(AlgoHashRateType):
rate: float rate: float
unit: X11Unit = HashUnit.X11.default unit: X11Unit = HashUnit.X11.default
def __add__(self, other: X11HashRate | int | float) -> X11HashRate: def into(self, other: X11Unit) -> Self:
if isinstance(other, X11HashRate): return self.__class__(
return X11HashRate( rate=self.rate / (other.value / self.unit.value), unit=other
rate=self.rate + other.into(self.unit).rate, unit=self.unit )
)
return X11HashRate(rate=self.rate + other, unit=self.unit)
def __sub__(self, other: X11HashRate | int | float) -> X11HashRate:
if isinstance(other, X11HashRate):
return X11HashRate(
rate=self.rate - other.into(self.unit).rate, unit=self.unit
)
return X11HashRate(rate=self.rate - other, unit=self.unit)
def __truediv__(self, other: X11HashRate | int | float):
if isinstance(other, X11HashRate):
return X11HashRate(
rate=self.rate / other.into(self.unit).rate, unit=self.unit
)
return X11HashRate(rate=self.rate / other, unit=self.unit)
def __floordiv__(self, other: X11HashRate | int | float):
if isinstance(other, X11HashRate):
return X11HashRate(
rate=self.rate // other.into(self.unit).rate, unit=self.unit
)
return X11HashRate(rate=self.rate // other, unit=self.unit)
def __mul__(self, other: X11HashRate | int | float):
if isinstance(other, X11HashRate):
return X11HashRate(
rate=self.rate * other.into(self.unit).rate, unit=self.unit
)
return X11HashRate(rate=self.rate * other, unit=self.unit)
def into(self, other: X11Unit) -> X11HashRate:
return X11HashRate(rate=self.rate / (other.value / self.unit.value), unit=other)

View File

@@ -6,12 +6,8 @@ from .hashrate.unit import KadenaUnit
# make this json serializable # make this json serializable
class _KadenaAlgo(MinerAlgoType): class KadenaAlgo(MinerAlgoType):
hashrate = KadenaHashRate hashrate: type[KadenaHashRate] = KadenaHashRate
unit = KadenaUnit unit: type[KadenaUnit] = KadenaUnit
def __repr__(self): name = "Kadena"
return "KadenaAlgo"
KadenaAlgo = _KadenaAlgo("Kadena")

View File

@@ -6,12 +6,8 @@ from .hashrate.unit import KHeavyHashUnit
# make this json serializable # make this json serializable
class _KHeavyHashAlgo(MinerAlgoType): class KHeavyHashAlgo(MinerAlgoType):
hashrate = KHeavyHashHashRate hashrate: type[KHeavyHashHashRate] = KHeavyHashHashRate
unit = KHeavyHashUnit unit: type[KHeavyHashUnit] = KHeavyHashUnit
def __repr__(self): name = "KHeavyHash"
return "KHeavyHashAlgo"
KHeavyHashAlgo = _KHeavyHashAlgo("KHeavyHash")

View File

@@ -5,13 +5,8 @@ from .hashrate import ScryptHashRate
from .hashrate.unit import ScryptUnit from .hashrate.unit import ScryptUnit
# make this json serializable class ScryptAlgo(MinerAlgoType):
class _ScryptAlgo(MinerAlgoType): hashrate: type[ScryptHashRate] = ScryptHashRate
hashrate = ScryptHashRate unit: type[ScryptUnit] = ScryptUnit
unit = ScryptUnit
def __repr__(self): name = "Scrypt"
return "ScryptAlgo"
ScryptAlgo = _ScryptAlgo("Scrypt")

View File

@@ -6,12 +6,8 @@ from .hashrate.unit import SHA256Unit
# make this json serializable # make this json serializable
class _SHA256Algo(MinerAlgoType): class SHA256Algo(MinerAlgoType):
hashrate = SHA256HashRate hashrate: type[SHA256HashRate] = SHA256HashRate
unit = SHA256Unit unit: type[SHA256Unit] = SHA256Unit
def __repr__(self): name = "SHA256"
return "SHA256Algo"
SHA256Algo = _SHA256Algo("SHA256")

View File

@@ -6,12 +6,8 @@ from .hashrate.unit import X11Unit
# make this json serializable # make this json serializable
class _X11Algo(MinerAlgoType): class X11Algo(MinerAlgoType):
hashrate = X11HashRate hashrate: type[X11HashRate] = X11HashRate
unit = X11Unit unit: type[X11Unit] = X11Unit
def __repr__(self): name = "X11"
return "X11Algo"
X11Algo = _X11Algo("X11")

View File

@@ -46,7 +46,7 @@ class MinerProtocol(Protocol):
make: MinerMake = None make: MinerMake = None
raw_model: MinerModelType = None raw_model: MinerModelType = None
firmware: MinerFirmware = None firmware: MinerFirmware = None
algo: MinerAlgoType = None algo: type[MinerAlgoType] = None
expected_hashboards: int = None expected_hashboards: int = None
expected_chips: int = None expected_chips: int = None
@@ -496,7 +496,6 @@ class MinerProtocol(Protocol):
function = getattr(self, getattr(self.data_locations, data_name).cmd) function = getattr(self, getattr(self.data_locations, data_name).cmd)
miner_data[data_name] = await function(**args_to_send) miner_data[data_name] = await function(**args_to_send)
except Exception as e: except Exception as e:
raise e
raise APIError( raise APIError(
f"Failed to call {data_name} on {self} while getting data." f"Failed to call {data_name} on {self} while getting data."
) from e ) from e