diff --git a/bittensor/axon.py b/bittensor/axon.py index 8dbba96e93..3110c087f2 100644 --- a/bittensor/axon.py +++ b/bittensor/axon.py @@ -559,6 +559,12 @@ def check_config(cls, config: "bittensor.config"): config.axon.external_port > 1024 and config.axon.external_port < 65535 ), "External port must be in range [1024, 65535]" + def to_string(self): + """ + Provides a human-readable representation of the AxonInfo for this Axon. + """ + return self.info().to_string() + def __str__(self) -> str: """ Provides a human-readable representation of the Axon instance. diff --git a/bittensor/chain_data.py b/bittensor/chain_data.py index 33d465d915..3f44a66a01 100644 --- a/bittensor/chain_data.py +++ b/bittensor/chain_data.py @@ -14,12 +14,12 @@ # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. - import torch import bittensor +import json from enum import Enum -from dataclasses import dataclass +from dataclasses import dataclass, asdict from scalecodec.types import GenericCall from typing import List, Tuple, Dict, Optional, Any, TypedDict, Union from scalecodec.base import RuntimeConfiguration, ScaleBytes @@ -227,6 +227,28 @@ def __str__(self): def __repr__(self): return self.__str__() + def to_string(self) -> str: + """Converts the AxonInfo object to a string representation using JSON.""" + try: + return json.dumps(asdict(self)) + except (TypeError, ValueError) as e: + bittensor.logging.error(f"Error converting AxonInfo to string: {e}") + return AxonInfo(0, "", 0, 0, "", "").to_string() + + @classmethod + def from_string(cls, s: str) -> "AxonInfo": + """Creates an AxonInfo object from its string representation using JSON.""" + try: + data = json.loads(s) + return cls(**data) + except json.JSONDecodeError as e: + bittensor.logging.error(f"Error decoding JSON: {e}") + except TypeError as e: + bittensor.logging.error(f"Type error: {e}") + except ValueError as e: + bittensor.logging.error(f"Value error: {e}") + return AxonInfo(0, "", 0, 0, "", "") + @classmethod def from_neuron_info(cls, neuron_info: dict) -> "AxonInfo": """Converts a dictionary to an axon_info object."""