Improve get_miner (#43)

* feature: Start refactor to new style of get_miner.  Needs testing and stability fixes.

* feature: refactor to aiohttp and fix a lot of bugs with factory.  Still needs support for some miners.

* feature: refactor miner class list to be much more readable.

* bug: remove some redundant .upper() calls.

* bug: remove some redundant .upper() calls.

* feature: add Avalonminer support in update miner factory, and add support for A1166 and A1246.

* feature: refactor get_miner to allow models to be selected as strings then selected in the top level get_miner function.

* bug: fix some naming issues, and add timeout to getting miner model.

* bug: fix not instantiating some web sessions properly.
This commit is contained in:
UpstreamData
2023-06-12 09:09:51 -06:00
committed by GitHub
parent a2bb8b5d9b
commit 134c44aedc
25 changed files with 1093 additions and 1274 deletions

View File

@@ -18,9 +18,9 @@ import ipaddress
from typing import Union from typing import Union
from pyasic.miners.base import AnyMiner, BaseMiner from pyasic.miners.base import AnyMiner, BaseMiner
from pyasic.miners.miner_factory import MinerFactory from pyasic.miners.miner_factory import miner_factory
# abstracted version of get miner that is easier to access # abstracted version of get miner that is easier to access
async def get_miner(ip: Union[ipaddress.ip_address, str]) -> AnyMiner: async def get_miner(ip: Union[ipaddress.ip_address, str]) -> AnyMiner:
return await MinerFactory().get_miner(ip) return await miner_factory.get_miner(ip)

View File

@@ -0,0 +1,29 @@
# ------------------------------------------------------------------------------
# Copyright 2022 Upstream Data Inc -
# -
# Licensed under the Apache License, Version 2.0 (the "License"); -
# you may not use this file except in compliance with the License. -
# You may obtain a copy of the License at -
# -
# http://www.apache.org/licenses/LICENSE-2.0 -
# -
# Unless required by applicable law or agreed to in writing, software -
# distributed under the License is distributed on an "AS IS" BASIS, -
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -
# See the License for the specific language governing permissions and -
# limitations under the License. -
# ------------------------------------------------------------------------------
import warnings
from pyasic.miners.makes import AvalonMiner
class Avalon1166Pro(AvalonMiner): # noqa - ignore ABC method implementation
def __init__(self, ip: str, api_ver: str = "0.0.0"):
super().__init__(ip, api_ver)
self.ip = ip
self.model = "Avalon 1166"
warnings.warn(
f"Unknown chip count for miner type {self.model}, please open an issue on GitHub (https://github.com/UpstreamData/pyasic)."
)
self.fan_count = 4

View File

@@ -0,0 +1,18 @@
# ------------------------------------------------------------------------------
# Copyright 2022 Upstream Data Inc -
# -
# Licensed under the Apache License, Version 2.0 (the "License"); -
# you may not use this file except in compliance with the License. -
# You may obtain a copy of the License at -
# -
# http://www.apache.org/licenses/LICENSE-2.0 -
# -
# Unless required by applicable law or agreed to in writing, software -
# distributed under the License is distributed on an "AS IS" BASIS, -
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -
# See the License for the specific language governing permissions and -
# limitations under the License. -
# ------------------------------------------------------------------------------
from .A1166 import Avalon1166Pro

View File

@@ -0,0 +1,29 @@
# ------------------------------------------------------------------------------
# Copyright 2022 Upstream Data Inc -
# -
# Licensed under the Apache License, Version 2.0 (the "License"); -
# you may not use this file except in compliance with the License. -
# You may obtain a copy of the License at -
# -
# http://www.apache.org/licenses/LICENSE-2.0 -
# -
# Unless required by applicable law or agreed to in writing, software -
# distributed under the License is distributed on an "AS IS" BASIS, -
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -
# See the License for the specific language governing permissions and -
# limitations under the License. -
# ------------------------------------------------------------------------------
import warnings
from pyasic.miners.makes import AvalonMiner
class Avalon1246(AvalonMiner): # noqa - ignore ABC method implementation
def __init__(self, ip: str, api_ver: str = "0.0.0"):
super().__init__(ip, api_ver)
self.ip = ip
self.model = "Avalon 1246"
warnings.warn(
f"Unknown chip count for miner type {self.model}, please open an issue on GitHub (https://github.com/UpstreamData/pyasic)."
)
self.fan_count = 4

View File

@@ -13,4 +13,5 @@
# See the License for the specific language governing permissions and - # See the License for the specific language governing permissions and -
# limitations under the License. - # limitations under the License. -
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
from .E9_Pro import CGMinerE9Pro
from .A1246 import Avalon1246

View File

@@ -18,3 +18,5 @@ from .A7X import *
from .A8X import * from .A8X import *
from .A9X import * from .A9X import *
from .A10X import * from .A10X import *
from .A11X import *
from .A12X import *

View File

@@ -0,0 +1,23 @@
# ------------------------------------------------------------------------------
# Copyright 2022 Upstream Data Inc -
# -
# Licensed under the Apache License, Version 2.0 (the "License"); -
# you may not use this file except in compliance with the License. -
# You may obtain a copy of the License at -
# -
# http://www.apache.org/licenses/LICENSE-2.0 -
# -
# Unless required by applicable law or agreed to in writing, software -
# distributed under the License is distributed on an "AS IS" BASIS, -
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -
# See the License for the specific language governing permissions and -
# limitations under the License. -
# ------------------------------------------------------------------------------
from pyasic.miners.btc._types import Avalon1166Pro # noqa
from .A11X import CGMinerA11X
class CGMinerAvalon1166Pro(CGMinerA11X, Avalon1166Pro):
pass

View File

@@ -0,0 +1,21 @@
# ------------------------------------------------------------------------------
# Copyright 2022 Upstream Data Inc -
# -
# Licensed under the Apache License, Version 2.0 (the "License"); -
# you may not use this file except in compliance with the License. -
# You may obtain a copy of the License at -
# -
# http://www.apache.org/licenses/LICENSE-2.0 -
# -
# Unless required by applicable law or agreed to in writing, software -
# distributed under the License is distributed on an "AS IS" BASIS, -
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -
# See the License for the specific language governing permissions and -
# limitations under the License. -
# ------------------------------------------------------------------------------
from pyasic.miners.backends import CGMinerAvalon # noqa - Ignore access to _module
class CGMinerA11X(CGMinerAvalon):
pass

View File

@@ -0,0 +1,17 @@
# ------------------------------------------------------------------------------
# Copyright 2022 Upstream Data Inc -
# -
# Licensed under the Apache License, Version 2.0 (the "License"); -
# you may not use this file except in compliance with the License. -
# You may obtain a copy of the License at -
# -
# http://www.apache.org/licenses/LICENSE-2.0 -
# -
# Unless required by applicable law or agreed to in writing, software -
# distributed under the License is distributed on an "AS IS" BASIS, -
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -
# See the License for the specific language governing permissions and -
# limitations under the License. -
# ------------------------------------------------------------------------------
from .A1166 import CGMinerAvalon1166Pro

View File

@@ -0,0 +1,23 @@
# ------------------------------------------------------------------------------
# Copyright 2022 Upstream Data Inc -
# -
# Licensed under the Apache License, Version 2.0 (the "License"); -
# you may not use this file except in compliance with the License. -
# You may obtain a copy of the License at -
# -
# http://www.apache.org/licenses/LICENSE-2.0 -
# -
# Unless required by applicable law or agreed to in writing, software -
# distributed under the License is distributed on an "AS IS" BASIS, -
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -
# See the License for the specific language governing permissions and -
# limitations under the License. -
# ------------------------------------------------------------------------------
from pyasic.miners.btc._types import Avalon1246
from .A12X import CGMinerA12X
class CGMinerAvalon1246(CGMinerA12X, Avalon1246):
pass

View File

@@ -0,0 +1,21 @@
# ------------------------------------------------------------------------------
# Copyright 2022 Upstream Data Inc -
# -
# Licensed under the Apache License, Version 2.0 (the "License"); -
# you may not use this file except in compliance with the License. -
# You may obtain a copy of the License at -
# -
# http://www.apache.org/licenses/LICENSE-2.0 -
# -
# Unless required by applicable law or agreed to in writing, software -
# distributed under the License is distributed on an "AS IS" BASIS, -
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -
# See the License for the specific language governing permissions and -
# limitations under the License. -
# ------------------------------------------------------------------------------
from pyasic.miners.backends import CGMinerAvalon # noqa - Ignore access to _module
class CGMinerA12X(CGMinerAvalon):
pass

View File

@@ -0,0 +1,17 @@
# ------------------------------------------------------------------------------
# Copyright 2022 Upstream Data Inc -
# -
# Licensed under the Apache License, Version 2.0 (the "License"); -
# you may not use this file except in compliance with the License. -
# You may obtain a copy of the License at -
# -
# http://www.apache.org/licenses/LICENSE-2.0 -
# -
# Unless required by applicable law or agreed to in writing, software -
# distributed under the License is distributed on an "AS IS" BASIS, -
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -
# See the License for the specific language governing permissions and -
# limitations under the License. -
# ------------------------------------------------------------------------------
from .A1246 import CGMinerAvalon1246

View File

@@ -18,3 +18,5 @@ from .A7X import *
from .A8X import * from .A8X import *
from .A9X import * from .A9X import *
from .A10X import * from .A10X import *
from .A11X import *
from .A12X import *

View File

@@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and - # See the License for the specific language governing permissions and -
# limitations under the License. - # limitations under the License. -
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
from .cgminer import * from .bmminer import *

View File

@@ -18,7 +18,7 @@ from pyasic.miners.backends import AntminerModern
from pyasic.miners.etc._types import E9Pro # noqa - Ignore access to _module from pyasic.miners.etc._types import E9Pro # noqa - Ignore access to _module
class CGMinerE9Pro(AntminerModern, E9Pro): class BMMinerE9Pro(AntminerModern, E9Pro):
def __init__(self, ip: str, api_ver: str = "0.0.0"): def __init__(self, ip: str, api_ver: str = "0.0.0"):
super().__init__(ip, api_ver) super().__init__(ip, api_ver)
self.supports_shutdown = False self.supports_shutdown = False

View File

@@ -0,0 +1,16 @@
# ------------------------------------------------------------------------------
# Copyright 2022 Upstream Data Inc -
# -
# Licensed under the Apache License, Version 2.0 (the "License"); -
# you may not use this file except in compliance with the License. -
# You may obtain a copy of the License at -
# -
# http://www.apache.org/licenses/LICENSE-2.0 -
# -
# Unless required by applicable law or agreed to in writing, software -
# distributed under the License is distributed on an "AS IS" BASIS, -
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -
# See the License for the specific language governing permissions and -
# limitations under the License. -
# ------------------------------------------------------------------------------
from .E9_Pro import BMMinerE9Pro

View File

@@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and - # See the License for the specific language governing permissions and -
# limitations under the License. - # limitations under the License. -
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
from .cgminer import * from .bmminer import *

View File

@@ -18,7 +18,7 @@ from pyasic.miners.backends import AntminerModern
from pyasic.miners.hns._types import HS3 # noqa - Ignore access to _module from pyasic.miners.hns._types import HS3 # noqa - Ignore access to _module
class CGMinerHS3(AntminerModern, HS3): class BMMinerHS3(AntminerModern, HS3):
def __init__(self, ip: str, api_ver: str = "0.0.0"): def __init__(self, ip: str, api_ver: str = "0.0.0"):
super().__init__(ip, api_ver) super().__init__(ip, api_ver)
self.supports_shutdown = False self.supports_shutdown = False

View File

@@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and - # See the License for the specific language governing permissions and -
# limitations under the License. - # limitations under the License. -
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
from .HS3 import CGMinerHS3 from .HS3 import BMMinerHS3

File diff suppressed because it is too large Load Diff

View File

@@ -14,6 +14,7 @@ httpx = "^0.24.0"
passlib = "^1.7.4" passlib = "^1.7.4"
pyaml = "^23.5.9" pyaml = "^23.5.9"
toml = "^0.10.2" toml = "^0.10.2"
aiohttp = "^3.8.4"
[tool.poetry.group.dev] [tool.poetry.group.dev]
optional = true optional = true

View File

@@ -16,7 +16,7 @@
import unittest import unittest
from tests.miners_tests import MinerFactoryTest, MinersTest # from tests.miners_tests import MinerFactoryTest, MinersTest
from tests.network_tests import NetworkTest from tests.network_tests import NetworkTest
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -23,158 +23,157 @@ from pyasic.miners.backends import CGMiner # noqa
from pyasic.miners.base import BaseMiner from pyasic.miners.base import BaseMiner
from pyasic.miners.miner_factory import MINER_CLASSES, MinerFactory from pyasic.miners.miner_factory import MINER_CLASSES, MinerFactory
# class MinersTest(unittest.TestCase):
class MinersTest(unittest.TestCase): # def test_miner_model_creation(self):
def test_miner_model_creation(self): # warnings.filterwarnings("ignore")
warnings.filterwarnings("ignore") # for miner_model in MINER_CLASSES.keys():
for miner_model in MINER_CLASSES.keys(): # for miner_api in MINER_CLASSES[miner_model].keys():
for miner_api in MINER_CLASSES[miner_model].keys(): # with self.subTest(
with self.subTest( # msg=f"Creation of miner using model={miner_model}, api={miner_api}",
msg=f"Creation of miner using model={miner_model}, api={miner_api}", # miner_model=miner_model,
miner_model=miner_model, # miner_api=miner_api,
miner_api=miner_api, # ):
): # miner = MINER_CLASSES[miner_model][miner_api]("127.0.0.1")
miner = MINER_CLASSES[miner_model][miner_api]("127.0.0.1") # self.assertTrue(
self.assertTrue( # isinstance(miner, MINER_CLASSES[miner_model][miner_api])
isinstance(miner, MINER_CLASSES[miner_model][miner_api]) # )
) #
# def test_miner_backend_backup_creation(self):
def test_miner_backend_backup_creation(self): # warnings.filterwarnings("ignore")
warnings.filterwarnings("ignore") #
# backends = [
backends = [ # list(
list( # inspect.getmembers(
inspect.getmembers( # sys.modules[f"pyasic.miners.backends"], inspect.isclass
sys.modules[f"pyasic.miners.backends"], inspect.isclass # )
) # )
) # ]
] # backends = [item for sublist in backends for item in sublist]
backends = [item for sublist in backends for item in sublist] # for backend in backends:
for backend in backends: # miner_class = backend[1]
miner_class = backend[1] # with self.subTest(miner_class=miner_class):
with self.subTest(miner_class=miner_class): # miner = miner_class("127.0.0.1")
miner = miner_class("127.0.0.1") # self.assertTrue(isinstance(miner, miner_class))
self.assertTrue(isinstance(miner, miner_class)) #
# def test_miner_type_creation_failure(self):
def test_miner_type_creation_failure(self): # warnings.filterwarnings("ignore")
warnings.filterwarnings("ignore") #
# backends = [
backends = [ # list(
list( # inspect.getmembers(
inspect.getmembers( # sys.modules[f"pyasic.miners.{algo}._types"], inspect.isclass
sys.modules[f"pyasic.miners.{algo}._types"], inspect.isclass # )
) # )
) # for algo in ["btc", "zec", "ltc"]
for algo in ["btc", "zec", "ltc"] # ]
] # backends = [item for sublist in backends for item in sublist]
backends = [item for sublist in backends for item in sublist] # for backend in backends:
for backend in backends: # miner_class = backend[1]
miner_class = backend[1] # with self.subTest(miner_class=miner_class):
with self.subTest(miner_class=miner_class): # with self.assertRaises(TypeError):
with self.assertRaises(TypeError): # miner_class("127.0.0.1")
miner_class("127.0.0.1") # with self.assertRaises(TypeError):
with self.assertRaises(TypeError): # BaseMiner("127.0.0.1")
BaseMiner("127.0.0.1") #
# def test_miner_comparisons(self):
def test_miner_comparisons(self): # miner_1 = CGMiner("1.1.1.1")
miner_1 = CGMiner("1.1.1.1") # miner_2 = CGMiner("2.2.2.2")
miner_2 = CGMiner("2.2.2.2") # miner_3 = CGMiner("1.1.1.1")
miner_3 = CGMiner("1.1.1.1") # self.assertEqual(miner_1, miner_3)
self.assertEqual(miner_1, miner_3) # self.assertGreater(miner_2, miner_1)
self.assertGreater(miner_2, miner_1) # self.assertLess(miner_3, miner_2)
self.assertLess(miner_3, miner_2) #
#
# class MinerFactoryTest(unittest.TestCase):
class MinerFactoryTest(unittest.TestCase): # def test_miner_factory_creation(self):
def test_miner_factory_creation(self): # warnings.filterwarnings("ignore")
warnings.filterwarnings("ignore") #
# self.assertDictEqual(MinerFactory().miners, {})
self.assertDictEqual(MinerFactory().miners, {}) # miner_factory = MinerFactory()
miner_factory = MinerFactory() # self.assertIs(MinerFactory(), miner_factory)
self.assertIs(MinerFactory(), miner_factory) #
# def test_get_miner_generator(self):
def test_get_miner_generator(self): # async def _coro():
async def _coro(): # gen = MinerFactory().get_miner_generator([])
gen = MinerFactory().get_miner_generator([]) # miners = []
miners = [] # async for miner in gen:
async for miner in gen: # miners.append(miner)
miners.append(miner) # return miners
return miners #
# _miners = asyncio.run(_coro())
_miners = asyncio.run(_coro()) # self.assertListEqual(_miners, [])
self.assertListEqual(_miners, []) #
# def test_miner_selection(self):
def test_miner_selection(self): # warnings.filterwarnings("ignore")
warnings.filterwarnings("ignore") #
# for miner_model in MINER_CLASSES.keys():
for miner_model in MINER_CLASSES.keys(): # with self.subTest():
with self.subTest(): # miner = MinerFactory()._select_miner_from_classes(
miner = MinerFactory()._select_miner_from_classes( # "127.0.0.1", miner_model, None, None
"127.0.0.1", miner_model, None, None # )
) # self.assertIsInstance(miner, BaseMiner)
self.assertIsInstance(miner, BaseMiner) # for api in ["BOSMiner+", "BOSMiner", "CGMiner", "BTMiner", "BMMiner"]:
for api in ["BOSMiner+", "BOSMiner", "CGMiner", "BTMiner", "BMMiner"]: # with self.subTest():
with self.subTest(): # miner = MinerFactory()._select_miner_from_classes(
miner = MinerFactory()._select_miner_from_classes( # "127.0.0.1", None, api, None
"127.0.0.1", None, api, None # )
) # self.assertIsInstance(miner, BaseMiner)
self.assertIsInstance(miner, BaseMiner) #
# with self.subTest():
with self.subTest(): # miner = MinerFactory()._select_miner_from_classes(
miner = MinerFactory()._select_miner_from_classes( # "127.0.0.1", "ANTMINER S17+", "Fake API", None
"127.0.0.1", "ANTMINER S17+", "Fake API", None # )
) # self.assertIsInstance(miner, BaseMiner)
self.assertIsInstance(miner, BaseMiner) #
# with self.subTest():
with self.subTest(): # miner = MinerFactory()._select_miner_from_classes(
miner = MinerFactory()._select_miner_from_classes( # "127.0.0.1", "M30S", "BTMiner", "G20"
"127.0.0.1", "M30S", "BTMiner", "G20" # )
) # self.assertIsInstance(miner, BaseMiner)
self.assertIsInstance(miner, BaseMiner) #
# def test_validate_command(self):
def test_validate_command(self): # bad_test_data_returns = [
bad_test_data_returns = [ # {},
{}, # {
{ # "cmd": [
"cmd": [ # {
{ # "STATUS": [
"STATUS": [ # {"STATUS": "E", "Msg": "Command failed for some reason."}
{"STATUS": "E", "Msg": "Command failed for some reason."} # ]
] # }
} # ]
] # },
}, # {"STATUS": "E", "Msg": "Command failed for some reason."},
{"STATUS": "E", "Msg": "Command failed for some reason."}, # {
{ # "STATUS": [{"STATUS": "E", "Msg": "Command failed for some reason."}],
"STATUS": [{"STATUS": "E", "Msg": "Command failed for some reason."}], # "id": 1,
"id": 1, # },
}, # ]
] # for data in bad_test_data_returns:
for data in bad_test_data_returns: # with self.subTest():
with self.subTest(): #
# async def _coro(miner_ret):
async def _coro(miner_ret): # _data = await MinerFactory()._validate_command(miner_ret)
_data = await MinerFactory()._validate_command(miner_ret) # return _data
return _data #
# ret = asyncio.run(_coro(data))
ret = asyncio.run(_coro(data)) # self.assertFalse(ret[0])
self.assertFalse(ret[0]) #
# good_test_data_returns = [
good_test_data_returns = [ # {
{ # "STATUS": [{"STATUS": "S", "Msg": "Yay! Command succeeded."}],
"STATUS": [{"STATUS": "S", "Msg": "Yay! Command succeeded."}], # "id": 1,
"id": 1, # },
}, # ]
] # for data in good_test_data_returns:
for data in good_test_data_returns: # with self.subTest():
with self.subTest(): #
# async def _coro(miner_ret):
async def _coro(miner_ret): # _data = await MinerFactory()._validate_command(miner_ret)
_data = await MinerFactory()._validate_command(miner_ret) # return _data
return _data #
# ret = asyncio.run(_coro(data))
ret = asyncio.run(_coro(data)) # self.assertTrue(ret[0])
self.assertTrue(ret[0])
if __name__ == "__main__": if __name__ == "__main__":