From 70eca3566d19ccc5bcd09fff29d90a125c10c267 Mon Sep 17 00:00:00 2001 From: ifrit98 Date: Sun, 3 Sep 2023 04:51:52 +0000 Subject: [PATCH 1/4] let subnets define schema for required hashable body items rather than attempting to parse --- bittensor/synapse.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/bittensor/synapse.py b/bittensor/synapse.py index 22893a392a..a033d834bd 100644 --- a/bittensor/synapse.py +++ b/bittensor/synapse.py @@ -26,6 +26,7 @@ import pydantic from pydantic.schema import schema import bittensor +from abc import abstractproperty from typing import Optional, List, Any @@ -335,8 +336,26 @@ def __setattr__(self, name, value): raise AttributeError( "body_hash property is read-only and cannot be overridden." ) + if name == "required_hash_fields": + raise AttributeError( + "required_hash_fields property is read-only and cannot be overridden." + ) super().__setattr__(name, value) + @property + def required_hash_fields(self) -> List[str]: + """ + Get the list of required fields for the Synapse instance. + + This method retrieves the list of required fields for the Synapse instance + by looking up the schema definition for the current class and returning the + list of required fields. + + Returns: + List[str]: A list of required fields for the Synapse instance. + """ + return [] + def get_total_size(self) -> int: """ Get the total size of the current object. @@ -373,23 +392,8 @@ def get_body(self) -> List[Any]: # Iterating over the fields of the instance for field, value in instance_fields.items(): - # If the object is not optional and non-null, add to the list of returned body fields - required = schema([self.__class__])["definitions"][self.name].get( - "required" - ) - if ( - required - and value != None - and field - not in [ - "name", - "timeout", - "total_size", - "header_size", - "dendrite", - "axon", - ] - ): + # If the field is required in the subclass schema, add it. + if field in self.required_hash_fields: fields.append(value) return fields From 78aa5b9cde625c774b4cb77d126177ad694e81dd Mon Sep 17 00:00:00 2001 From: ifrit98 Date: Sun, 3 Sep 2023 05:25:59 +0000 Subject: [PATCH 2/4] run black --- bittensor/cli.py | 2 +- bittensor/commands/overview.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bittensor/cli.py b/bittensor/cli.py index 49c2e0ab25..5b09a035b5 100644 --- a/bittensor/cli.py +++ b/bittensor/cli.py @@ -71,7 +71,7 @@ "delegate": DelegateStakeCommand, "undelegate": DelegateUnstakeCommand, "my_delegates": MyDelegatesCommand, - } + }, }, "wallet": { "name": "wallet", diff --git a/bittensor/commands/overview.py b/bittensor/commands/overview.py index c0969168ff..3d1ce354fc 100644 --- a/bittensor/commands/overview.py +++ b/bittensor/commands/overview.py @@ -36,7 +36,7 @@ class OverviewCommand: @staticmethod - def run(cli: 'bittensor.cli'): + def run(cli: "bittensor.cli"): r"""Prints an overview for the wallet's colkey.""" console = bittensor.__console__ wallet = bittensor.wallet(config=cli.config) @@ -119,8 +119,8 @@ def run(cli: 'bittensor.cli'): # Create a copy of the config without the parser and formatter_class. ## This is needed to pass to the ProcessPoolExecutor, which cannot pickle the parser. copy_config = cli.config.copy() - copy_config['__parser'] = None - copy_config['formatter_class'] = None + copy_config["__parser"] = None + copy_config["formatter_class"] = None # Pull neuron info for all keys. ## Max len(netuids) or 5 threads. From 547de44521bc6c0be916e719146124af3d994d6f Mon Sep 17 00:00:00 2001 From: ifrit98 Date: Tue, 5 Sep 2023 20:29:50 +0000 Subject: [PATCH 3/4] add a default implementation for required_hash_fields instead of Nil --- bittensor/synapse.py | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/bittensor/synapse.py b/bittensor/synapse.py index a033d834bd..7a90acb854 100644 --- a/bittensor/synapse.py +++ b/bittensor/synapse.py @@ -345,16 +345,45 @@ def __setattr__(self, name, value): @property def required_hash_fields(self) -> List[str]: """ - Get the list of required fields for the Synapse instance. + Retrieve the list of non-optional fields of the Synapse instance. - This method retrieves the list of required fields for the Synapse instance - by looking up the schema definition for the current class and returning the - list of required fields. + This default method identifies and returns the names of non-optional attributes of the Synapse + instance that have non-null values, excluding specific attributes such as `name`, `timeout`, + `total_size`, `header_size`, `dendrite`, and `axon`. The determination of whether a field is + optional or not is based on the schema definition for the Synapse class. + + Subclasses are encouraged to override this method to provide their own implementation for + determining required fields. If not overridden, the default implementation provided by the + Synapse superclass will be used, which returns the fields based on the schema definition. Returns: - List[str]: A list of required fields for the Synapse instance. + List[str]: A list of names of the non-optional fields of the Synapse instance. """ - return [] + fields = [] + # Getting the fields of the instance + instance_fields = self.__dict__ + + # Iterating over the fields of the instance + for field, value in instance_fields.items(): + # If the object is not optional and non-null, add to the list of returned body fields + required = schema([self.__class__])["definitions"][self.name].get( + "required" + ) + if ( + required + and value != None + and field + not in [ + "name", + "timeout", + "total_size", + "header_size", + "dendrite", + "axon", + ] + ): + fields.append(field) + return fields def get_total_size(self) -> int: """ From 84fefcdb1b5b6c23c5c9752bd5e1a1369995e19c Mon Sep 17 00:00:00 2001 From: ifrit98 Date: Tue, 5 Sep 2023 20:31:12 +0000 Subject: [PATCH 4/4] remove uused import --- bittensor/synapse.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bittensor/synapse.py b/bittensor/synapse.py index 7a90acb854..d0377f12d8 100644 --- a/bittensor/synapse.py +++ b/bittensor/synapse.py @@ -26,7 +26,6 @@ import pydantic from pydantic.schema import schema import bittensor -from abc import abstractproperty from typing import Optional, List, Any