Skip to content

Commit 8aefb55

Browse files
authored
[Tests] AsyncSubtensor (Part 4) (#2410)
* added tests from `AsyncSubtensor.get_netuids_for_hotkey` until `AsyncSubtensor.neurons_lite` * ruff * move out `_decode_hex_identity_dict` from inner function * added tests until `AsyncSubtensor.query_identity` * added tests from `AsyncSubtensor.get_netuids_for_hotkey` until `AsyncSubtensor.neurons_lite` * fix * ruff
1 parent 247d92a commit 8aefb55

File tree

2 files changed

+337
-26
lines changed

2 files changed

+337
-26
lines changed

bittensor/core/async_subtensor.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,32 @@ def decode_ss58_tuples(line: tuple):
8282
return [decode_account_id(line[x][0]) for x in range(len(line))]
8383

8484

85+
def _decode_hex_identity_dict(info_dictionary: dict[str, Any]) -> dict[str, Any]:
86+
"""Decodes a dictionary of hexadecimal identities."""
87+
for k, v in info_dictionary.items():
88+
if isinstance(v, dict):
89+
item = next(iter(v.values()))
90+
else:
91+
item = v
92+
if isinstance(item, tuple) and item:
93+
if len(item) > 1:
94+
try:
95+
info_dictionary[k] = (
96+
bytes(item).hex(sep=" ", bytes_per_sep=2).upper()
97+
)
98+
except UnicodeDecodeError:
99+
logging.error(f"Could not decode: {k}: {item}.")
100+
else:
101+
try:
102+
info_dictionary[k] = bytes(item[0]).decode("utf-8")
103+
except UnicodeDecodeError:
104+
logging.error(f"Could not decode: {k}: {item}.")
105+
else:
106+
info_dictionary[k] = item
107+
108+
return info_dictionary
109+
110+
85111
class AsyncSubtensor:
86112
"""Thin layer for interacting with Substrate Interface. Mostly a collection of frequently-used calls."""
87113

@@ -858,7 +884,6 @@ async def neuron_for_uid(
858884
method="neuronInfo_getNeuron",
859885
params=params, # custom rpc method
860886
)
861-
862887
if not (result := json_body.get("result", None)):
863888
return NeuronInfo.get_null_neuron()
864889

@@ -924,30 +949,6 @@ async def query_identity(
924949
See the `Bittensor CLI documentation <https://docs.bittensor.com/reference/btcli>`_ for supported identity parameters.
925950
"""
926951

927-
def decode_hex_identity_dict_(info_dictionary):
928-
for k, v in info_dictionary.items():
929-
if isinstance(v, dict):
930-
item = next(iter(v.values()))
931-
else:
932-
item = v
933-
if isinstance(item, tuple) and item:
934-
if len(item) > 1:
935-
try:
936-
info_dictionary[k] = (
937-
bytes(item).hex(sep=" ", bytes_per_sep=2).upper()
938-
)
939-
except UnicodeDecodeError:
940-
print(f"Could not decode: {k}: {item}")
941-
else:
942-
try:
943-
info_dictionary[k] = bytes(item[0]).decode("utf-8")
944-
except UnicodeDecodeError:
945-
print(f"Could not decode: {k}: {item}")
946-
else:
947-
info_dictionary[k] = item
948-
949-
return info_dictionary
950-
951952
identity_info = await self.substrate.query(
952953
module="Registry",
953954
storage_function="IdentityOf",
@@ -956,7 +957,7 @@ def decode_hex_identity_dict_(info_dictionary):
956957
reuse_block_hash=reuse_block,
957958
)
958959
try:
959-
return decode_hex_identity_dict_(identity_info["info"])
960+
return _decode_hex_identity_dict(identity_info["info"])
960961
except TypeError:
961962
return {}
962963

0 commit comments

Comments
 (0)