Skip to content

Commit

Permalink
handle SubstrateRequestException error
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-opentensor committed Dec 21, 2024
1 parent 49ea149 commit 0090e9d
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions bittensor/core/subtensor_with_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@

import numpy as np
from numpy.typing import NDArray
from substrateinterface.exceptions import SubstrateRequestException

from bittensor.core.metagraph import Metagraph
from bittensor.core.settings import version_as_int
from bittensor.core.settings import version_as_int, ARCHIVE_ENTRYPOINT
from bittensor.core.subtensor import Subtensor
from bittensor.utils.btlogging import logging

Expand Down Expand Up @@ -78,9 +79,15 @@ def wrapper(*args, **kwargs):
not self._subtensor
or self._subtensor.chain_endpoint != endpoint
):
self._subtensor = self._get_subtensor(endpoint=endpoint)
self._get_subtensor(endpoint=endpoint)
result = method(*args, **kwargs)
return result

except SubstrateRequestException as error:
if "UnknownBlock: State already discarded for" in str(error):
self._get_subtensor(endpoint=ARCHIVE_ENTRYPOINT)
return method(*args, **kwargs)

except Exception as error:
logging.error(
f"Attempt [blue]{retries}[/blue] for method [blue]{method.__name__}[/blue] failed. Error: {error}"
Expand Down Expand Up @@ -168,7 +175,6 @@ def _get_subtensor(self, endpoint: Optional[str] = None):
logging.debug(
f"[magenta]Subtensor initialized with endpoint:[/magenta] [blue]{endpoint}[/blue]."
)
return self._subtensor

def get_retry_seconds(self, netuid: Optional[int] = None) -> int:
"""Returns the number of seconds to wait before retrying a request based on `retry_second` or `_retry_epoch`.
Expand Down

0 comments on commit 0090e9d

Please sign in to comment.