Skip to content

Commit ad3b24c

Browse files
committed
[WIP] Add get_parents method to subtensor
1 parent 6bf7d42 commit ad3b24c

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

bittensor/core/async_subtensor.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,51 @@ async def get_block_hash(self, block: Optional[int] = None) -> str:
973973
else:
974974
return await self.substrate.get_chain_head()
975975

976+
async def get_parents(
977+
self,
978+
hotkey: str,
979+
netuid: int,
980+
block: Optional[int] = None,
981+
block_hash: Optional[str] = None,
982+
reuse_block: bool = False,
983+
) -> tuple[bool, list[tuple[float, str]], str]:
984+
"""
985+
This method retrieves the parent of a given hotkey and netuid. It queries the SubtensorModule's ParentKeys
986+
storage function to get the children and formats them before returning as a tuple.
987+
988+
Arguments:
989+
hotkey (str): The child hotkey SS58.
990+
netuid (int): The netuid value.
991+
block (Optional[int]): The block number for which the children are to be retrieved.
992+
block_hash (Optional[str]): The hash of the block to retrieve the subnet unique identifiers from.
993+
reuse_block (bool): Whether to reuse the last-used block hash.
994+
995+
Returns:
996+
A tuple containing a boolean indicating success or failure, a list of formatted
997+
parents [(proportion, parent)], and an error message (if applicable)
998+
"""
999+
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)
1000+
try:
1001+
parents = await self.substrate.query(
1002+
module="SubtensorModule",
1003+
storage_function="ParentKeys",
1004+
params=[hotkey, netuid],
1005+
block_hash=block_hash,
1006+
reuse_block_hash=reuse_block,
1007+
)
1008+
if parents:
1009+
formatted_parents = []
1010+
for proportion, parent in parents.value:
1011+
# Convert U64 to int
1012+
formatted_child = decode_account_id(parent[0])
1013+
normalized_proportion = u64_normalized_float(proportion)
1014+
formatted_parents.append((normalized_proportion, formatted_child))
1015+
return True, formatted_parents, ""
1016+
else:
1017+
return True, [], ""
1018+
except SubstrateRequestException as e:
1019+
return False, [], format_error_message(e)
1020+
9761021
async def get_children(
9771022
self,
9781023
hotkey: str,

bittensor/core/subtensor.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,42 @@ def get_hyperparameter(
751751

752752
return getattr(result, "value", result)
753753

754+
def get_parents(
755+
self, hotkey: str, netuid: int, block: Optional[int] = None
756+
) -> tuple[bool, list[tuple[float, str]], str]:
757+
"""
758+
This method retrieves the parent of a given hotkey and netuid. It queries the SubtensorModule's ParentKeys
759+
storage function to get the children and formats them before returning as a tuple.
760+
761+
Arguments:
762+
hotkey (str): The child hotkey SS58.
763+
netuid (int): The netuid.
764+
block (Optional[int]): The block number for which the children are to be retrieved.
765+
766+
Returns:
767+
A tuple containing a boolean indicating success or failure, a list of formatted
768+
parents [(proportion, parent)], and an error message (if applicable)
769+
"""
770+
try:
771+
parents = self.substrate.query(
772+
module="SubtensorModule",
773+
storage_function="ParentKeys",
774+
params=[hotkey, netuid],
775+
block_hash=self.determine_block_hash(block),
776+
)
777+
if parents:
778+
formatted_parents = []
779+
for proportion, parent in parents.value:
780+
# Convert U64 to int
781+
formatted_child = decode_account_id(parent[0])
782+
normalized_proportion = u64_normalized_float(proportion)
783+
formatted_parents.append((normalized_proportion, formatted_child))
784+
return True, formatted_parents, ""
785+
else:
786+
return True, [], ""
787+
except SubstrateRequestException as e:
788+
return False, [], format_error_message(e)
789+
754790
def get_children(
755791
self, hotkey: str, netuid: int, block: Optional[int] = None
756792
) -> tuple[bool, list[tuple[float, str]], str]:

bittensor/core/subtensor_api/subnets.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def __init__(self, subtensor: Union["_Subtensor", "_AsyncSubtensor"]):
1414
self.bonds = subtensor.bonds
1515
self.difficulty = subtensor.difficulty
1616
self.get_all_subnets_info = subtensor.get_all_subnets_info
17+
self.get_parents = subtensor.get_parents
1718
self.get_children = subtensor.get_children
1819
self.get_children_pending = subtensor.get_children_pending
1920
self.get_current_weight_commit_info = subtensor.get_current_weight_commit_info

0 commit comments

Comments
 (0)