Skip to content

Commit 0cccc49

Browse files
authored
Merge pull request #2846 from opentensor/feat/roman/selective-metagraph
Add `SelectiveMetagraph` interface into SDK
2 parents 46d3b3a + a00e87d commit 0cccc49

File tree

8 files changed

+770
-108
lines changed

8 files changed

+770
-108
lines changed

bittensor/core/async_subtensor.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@
1616

1717
from bittensor.core.chain_data import (
1818
DelegateInfo,
19-
StakeInfo,
19+
DynamicInfo,
2020
MetagraphInfo,
2121
NeuronInfoLite,
2222
NeuronInfo,
2323
ProposalVoteData,
24+
StakeInfo,
25+
SelectiveMetagraphIndex,
2426
SubnetHyperparameters,
2527
SubnetIdentity,
2628
SubnetInfo,
2729
WeightCommitInfo,
2830
decode_account_id,
29-
DynamicInfo,
3031
)
3132
from bittensor.core.chain_data.chain_identity import ChainIdentity
3233
from bittensor.core.chain_data.delegate_info import DelegatedInfo
@@ -1470,32 +1471,56 @@ async def get_minimum_required_stake(self):
14701471
async def get_metagraph_info(
14711472
self,
14721473
netuid: int,
1474+
field_indices: Optional[list["SelectiveMetagraphIndex"]] = None,
14731475
block: Optional[int] = None,
14741476
block_hash: Optional[str] = None,
14751477
reuse_block: bool = False,
14761478
) -> Optional[MetagraphInfo]:
14771479
"""
1478-
Retrieves the MetagraphInfo dataclass from the node for a single subnet (netuid)
1480+
Retrieves full or partial metagraph information for the specified subnet (netuid).
14791481
14801482
Arguments:
1481-
netuid: The NetUID of the subnet.
1483+
netuid (int): The NetUID of the subnet to query.
1484+
field_indices (Optional[list[SelectiveMetagraphIndex]]): An optional list of SelectiveMetagraphIndex values
1485+
specifying which fields to retrieve. If not provided, all available fields will be returned.
14821486
block: the block number at which to retrieve the hyperparameter. Do not specify if using block_hash or
14831487
reuse_block
14841488
block_hash: The hash of blockchain block number for the query. Do not specify if using
14851489
block or reuse_block
14861490
reuse_block: Whether to reuse the last-used block hash. Do not set if using block_hash or block.
14871491
14881492
Returns:
1489-
MetagraphInfo dataclass
1493+
Optional[MetagraphInfo]: A MetagraphInfo object containing the requested subnet data, or None if the subnet
1494+
with the given netuid does not exist.
1495+
1496+
Example:
1497+
meta_info = await subtensor.get_metagraph_info(netuid=2)
1498+
1499+
partial_meta_info = await subtensor.get_metagraph_info(
1500+
netuid=2,
1501+
field_indices=[SelectiveMetagraphIndex.Name, SelectiveMetagraphIndex.OwnerHotkeys]
1502+
)
14901503
"""
1504+
indexes = SelectiveMetagraphIndex.all_indices()
1505+
1506+
if field_indices:
1507+
if isinstance(field_indices, list) and all(
1508+
isinstance(f, SelectiveMetagraphIndex) for f in field_indices
1509+
):
1510+
indexes = [f.value for f in field_indices]
1511+
else:
1512+
raise ValueError(
1513+
"`field_indices` must be a list of SelectiveMetagraphIndex items."
1514+
)
1515+
14911516
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)
14921517
if not block_hash and reuse_block:
14931518
block_hash = self.substrate.last_block_hash
14941519

14951520
query = await self.substrate.runtime_call(
14961521
"SubnetInfoRuntimeApi",
1497-
"get_metagraph",
1498-
params=[netuid],
1522+
"get_selective_metagraph",
1523+
params=[netuid, indexes if 0 in indexes else [0] + indexes],
14991524
block_hash=block_hash,
15001525
)
15011526
if query.value is None:

bittensor/core/chain_data/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
MetagraphInfoEmissions,
1717
MetagraphInfoPool,
1818
MetagraphInfoParams,
19+
SelectiveMetagraphIndex,
1920
)
2021
from .neuron_info import NeuronInfo
2122
from .neuron_info_lite import NeuronInfoLite
@@ -50,6 +51,7 @@
5051
ProposalCallData,
5152
ProposalVoteData,
5253
ScheduledColdkeySwapInfo,
54+
SelectiveMetagraphIndex,
5355
StakeInfo,
5456
SubnetHyperparameters,
5557
SubnetIdentity,

0 commit comments

Comments
 (0)