Skip to content

Commit

Permalink
Fix tests, annotations, add todo regarding subtensor_build_id and imp…
Browse files Browse the repository at this point in the history
…ort subtensor module as subtensor_module on bittensor package level
  • Loading branch information
roman-opentensor committed May 10, 2024
1 parent cae20fa commit ff9a752
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
3 changes: 3 additions & 0 deletions bittensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ def debug(on: bool = True):
ProposalCallData,
ProposalVoteData,
)

from . import subtensor as subtensor_module
from bittensor import subtensor as subtensor_module
from .subtensor import subtensor as subtensor
from .cli import cli as cli, COMMANDS as ALL_COMMANDS
from .btlogging import logging
Expand Down
12 changes: 5 additions & 7 deletions bittensor/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def __init__(
f"Connected to {self.network} network and {self.chain_endpoint}."
)

self._subtensor_errors = None
self._subtensor_errors: Dict[str, Dict[str, str]] = {}

def __str__(self) -> str:
if self.network == self.chain_endpoint:
Expand Down Expand Up @@ -4514,19 +4514,17 @@ def get_block_hash(self, block_id: int) -> str:
"""
return self.substrate.get_block_hash(block_id=block_id)

def get_error_info_by_index(self, error_index: int) -> tuple[str, str]:
def get_error_info_by_index(self, error_index: int) -> Tuple[str, str]:
"""Returns the error name and description from the Subtensor error list."""

unknown_error = "Unknown Error"
unknown_error = ("Unknown Error", "")

if not self._subtensor_errors:
self._subtensor_errors = get_subtensor_errors(self.substrate)

name, description = self._subtensor_errors.get(
str(error_index), (unknown_error, "")
)
name, description = self._subtensor_errors.get(str(error_index), unknown_error)

if name == unknown_error:
if name == unknown_error[0]:
logger.warning(
f"Subtensor returned an error with an unknown index: {error_index}"
)
Expand Down
4 changes: 3 additions & 1 deletion bittensor/utils/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def _get_errors_from_cache() -> Optional[Dict[str, Dict[str, Dict[str, str]]]]:

def get_subtensor_errors(
substrate: SubstrateInterface,
) -> Union[Dict[str, str], Dict[Any, Any]]:
) -> Union[Dict[str, Dict[str, str]], Dict[Any, Any]]:
"""Fetches or retrieves cached Subtensor error definitions using metadata.
Args:
Expand All @@ -117,6 +117,8 @@ def get_subtensor_errors(
substrate.get_metadata()

cached_errors_map = _get_errors_from_cache()
# TODO: Talk to the Nucleus team about a unique identification for each assembly (subtensor). Before that, use
# the metadata value for `subtensor_build_id`
subtensor_build_id = substrate.metadata[0].value

if not cached_errors_map or subtensor_build_id != cached_errors_map.get(
Expand Down
14 changes: 8 additions & 6 deletions tests/unit_tests/test_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
# Application
import bittensor
from bittensor.subtensor import subtensor as Subtensor, logger
from bittensor import subtensor_module


def test_serve_axon_with_external_ip_set():
Expand Down Expand Up @@ -283,14 +284,15 @@ class MockSubstrate:


@pytest.fixture
def subtensor(substrate, mocker):
mocker.patch(
"bittensor.subtensor.get_subtensor_errors",
def subtensor(substrate):
mock.patch.object(
subtensor_module,
"get_subtensor_errors",
return_value={
"1": ("ErrorOne", "Description one"),
"2": ("ErrorTwo", "Description two"),
},
)
).start()
return Subtensor()


Expand All @@ -300,8 +302,8 @@ def test_get_error_info_by_index_known_error(subtensor):
assert description == "Description one"


def test_get_error_info_by_index_unknown_error(subtensor, mocker):
mock_logger = mocker.patch.object(logger, "warning")
def test_get_error_info_by_index_unknown_error(subtensor):
mock_logger = mock.patch.object(logger, "warning").start()
fake_index = 999
name, description = subtensor.get_error_info_by_index(fake_index)
assert name == "Unknown Error"
Expand Down

0 comments on commit ff9a752

Please sign in to comment.