From e07f517043934c2a1348a3433943acc43b153290 Mon Sep 17 00:00:00 2001 From: ifrit98 Date: Sat, 28 Oct 2023 05:26:27 +0000 Subject: [PATCH 1/2] add AxonInfo._string() --- bittensor/axon.py | 6 ++++++ bittensor/chain_data.py | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) 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..2edb27f086 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,16 @@ def __str__(self): def __repr__(self): return self.__str__() + def to_string(self) -> str: + """Converts the AxonInfo object to a string representation using JSON.""" + return json.dumps(asdict(self)) + + @classmethod + def from_string(cls, s: str) -> "AxonInfo": + """Creates an AxonInfo object from its string representation using JSON.""" + data = json.loads(s) + return cls(**data) + @classmethod def from_neuron_info(cls, neuron_info: dict) -> "AxonInfo": """Converts a dictionary to an axon_info object.""" From 3e6648abb427c4a25e671c76445bf68804c34cf0 Mon Sep 17 00:00:00 2001 From: ifrit98 Date: Thu, 2 Nov 2023 18:39:47 +0000 Subject: [PATCH 2/2] add error handling --- bittensor/chain_data.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/bittensor/chain_data.py b/bittensor/chain_data.py index 2edb27f086..3f44a66a01 100644 --- a/bittensor/chain_data.py +++ b/bittensor/chain_data.py @@ -229,13 +229,25 @@ def __repr__(self): def to_string(self) -> str: """Converts the AxonInfo object to a string representation using JSON.""" - return json.dumps(asdict(self)) + 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.""" - data = json.loads(s) - return cls(**data) + 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":