Skip to content
Merged
6 changes: 6 additions & 0 deletions bittensor/axon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 24 additions & 2 deletions bittensor/chain_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down