Skip to content

Commit 9d6f2bc

Browse files
wip
1 parent 0c52ea8 commit 9d6f2bc

File tree

2 files changed

+189
-95
lines changed

2 files changed

+189
-95
lines changed

bittensor/core/async_subtensor.py

Lines changed: 101 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,11 @@ def _get_substrate(
406406

407407
@property
408408
async def block(self):
409-
"""Provides an asynchronous getter to retrieve the current block."""
409+
"""Provides an asynchronous getter to retrieve the current block number.
410+
411+
Returns:
412+
The current blockchain block number.
413+
"""
410414
return await self.get_current_block()
411415

412416
async def determine_block_hash(
@@ -797,7 +801,7 @@ async def query_map(
797801
) -> "AsyncQueryMapResult":
798802
"""Queries map storage from any module on the Bittensor blockchain.
799803
800-
Use this function for nonstandard queries to constants defined within the Bittensor blockchain, if these cannot
804+
Use this function for nonstandard queries to map storage defined within the Bittensor blockchain, if these cannot
801805
be accessed through other, standard getter methods.
802806
803807
Parameters:
@@ -832,7 +836,7 @@ async def query_map_subtensor(
832836
) -> "AsyncQueryMapResult":
833837
"""Queries map storage from the Subtensor module on the Bittensor blockchain.
834838
835-
Use this function for nonstandard queries to constants defined within the Bittensor blockchain, if these cannot
839+
Use this function for nonstandard queries to map storage defined within the Bittensor blockchain, if these cannot
836840
be accessed through other, standard getter methods.
837841
838842
Parameters:
@@ -866,7 +870,7 @@ async def query_module(
866870
) -> Optional[Union["ScaleObj", Any]]:
867871
"""Queries any module storage on the Bittensor blockchain with the specified parameters and block number.
868872
This function is a generic query interface that allows for flexible and diverse data retrieval from various
869-
blockchain modules. Use this function for nonstandard queries to constants defined within the Bittensor
873+
blockchain modules. Use this function for nonstandard queries to storage defined within the Bittensor
870874
blockchain, if these cannot be accessed through other, standard getter methods.
871875
872876
Parameters:
@@ -935,7 +939,7 @@ async def query_subtensor(
935939
) -> Optional[Union["ScaleObj", Any]]:
936940
"""Queries named storage from the Subtensor module on the Bittensor blockchain.
937941
938-
Use this function for nonstandard queries to constants defined within the Bittensor blockchain, if these cannot
942+
Use this function for nonstandard queries to storage defined within the Bittensor blockchain, if these cannot
939943
be accessed through other, standard getter methods.
940944
941945
Parameters:
@@ -969,9 +973,13 @@ async def state_call(
969973
"""Makes a state call to the Bittensor blockchain, allowing for direct queries of the blockchain's state.
970974
This function is typically used for advanced, nonstandard queries not provided by other getter methods.
971975
976+
Use this method when you need to query runtime APIs or storage functions that don't have dedicated
977+
wrapper methods in the SDK. For standard queries, prefer the specific getter methods (e.g., ``get_balance``,
978+
``get_stake``) which provide better type safety and error handling.
979+
972980
Parameters:
973-
method: The method name for the state call.
974-
data: The data to be passed to the method.
981+
method: The runtime API method name (e.g., "SubnetInfoRuntimeApi", "get_metagraph").
982+
data: Hex-encoded string of the SCALE-encoded parameters to pass to the method.
975983
block: The block number to query. Do not specify if using ``block_hash`` or ``reuse_block``.
976984
block_hash: The block hash at which to check the parameter. Do not set if using ``block`` or
977985
``reuse_block``.
@@ -2001,7 +2009,7 @@ async def get_commitment_metadata(
20012009
block_hash: Optional[str] = None,
20022010
reuse_block: bool = False,
20032011
) -> Union[str, dict]:
2004-
# TODO: how to handle return data? need good example
2012+
# TODO: how to handle return data? need good example @roman
20052013
"""Fetches raw commitment metadata from specific subnet for given hotkey.
20062014
20072015
Parameters:
@@ -2012,7 +2020,11 @@ async def get_commitment_metadata(
20122020
reuse_block: Whether to reuse the last-used blockchain hash.
20132021
20142022
Returns:
2015-
The raw commitment metadata from specific subnet for given hotkey.
2023+
The raw commitment metadata. Returns a dict when commitment data exists,
2024+
or an empty string when no commitment is found for the given hotkey on the subnet.
2025+
2026+
Notes:
2027+
- <https://docs.learnbittensor.org/glossary#commit-reveal>
20162028
"""
20172029
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)
20182030
commit_data = await self.substrate.query(
@@ -2132,8 +2144,6 @@ async def get_crowdloan_by_id(
21322144
) -> Optional["CrowdloanInfo"]:
21332145
"""Retrieves detailed information about a specific crowdloan campaign.
21342146
2135-
2136-
21372147
Parameters:
21382148
crowdloan_id: Unique identifier of the crowdloan (auto-incremented starting from 0).
21392149
block: The blockchain block number for the query.
@@ -2563,9 +2573,8 @@ async def get_last_bonds_reset(
25632573
block: Optional[int] = None,
25642574
block_hash: Optional[str] = None,
25652575
reuse_block: bool = False,
2566-
) -> bytes:
2567-
"""
2568-
Retrieves the last bonds reset triggered at commitment from given subnet for a specific hotkey.
2576+
):
2577+
"""Retrieves the block number when bonds were last reset for a specific hotkey on a subnet.
25692578
25702579
Parameters:
25712580
netuid: The network uid to fetch from.
@@ -2575,7 +2584,11 @@ async def get_last_bonds_reset(
25752584
reuse_block: Whether to use the last-used block. Do not set if using ``block_hash`` or ``block``.
25762585
25772586
Returns:
2578-
bytes: The last bonds reset data for the specified hotkey and netuid.
2587+
The block number when bonds were last reset, or ``None`` if no bonds reset has occurred.
2588+
2589+
Notes:
2590+
- <https://docs.learnbittensor.org/resources/glossary#validator-miner-bonds>
2591+
- <https://docs.learnbittensor.org/resources/glossary#commit-reveal>
25792592
"""
25802593
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)
25812594
block = await self.substrate.query(
@@ -2982,11 +2995,21 @@ async def get_metagraph_info(
29822995
return MetagraphInfo.from_dict(query.value)
29832996

29842997
async def get_minimum_required_stake(self):
2985-
"""
2986-
Returns the minimum required stake for nominators in the Subtensor network.
2998+
"""Returns the minimum required stake threshold for nominator cleanup operations.
2999+
3000+
This threshold is used ONLY for cleanup after unstaking operations. If a nominator's remaining stake
3001+
falls below this minimum after an unstake, the remaining stake is forcefully cleared and returned
3002+
to the coldkey to prevent dust accounts.
3003+
3004+
This is NOT the minimum checked during staking operations. The actual minimum for staking is determined
3005+
by DefaultMinStake (typically 0.001 TAO plus fees).
29873006
29883007
Returns:
2989-
Balance: The minimum required stake as a Balance object.
3008+
The minimum stake threshold as a Balance object. Nominator stakes below this amount
3009+
are automatically cleared after unstake operations.
3010+
3011+
Notes:
3012+
- <https://docs.learnbittensor.org/staking-and-delegation/delegation>
29903013
"""
29913014
result = await self.substrate.query(
29923015
module="SubtensorModule", storage_function="NominatorMinRequiredStake"
@@ -4584,7 +4607,7 @@ async def filter_netuids_by_registered_hotkeys(
45844607
) -> list[int]:
45854608
"""
45864609
Filters a given list of all netuids for certain specified netuids and hotkeys
4587-
4610+
# TODO @roman I find this confusing, what is the difference between all_netuids and filter_for_netuids? what is the intent for this method's
45884611
Parameters:
45894612
all_netuids: A list of netuids to filter.
45904613
filter_for_netuids: A subset of all_netuids to filter from the main list.
@@ -4912,18 +4935,22 @@ async def max_weight_limit(
49124935
block_hash: Optional[str] = None,
49134936
reuse_block: bool = False,
49144937
) -> Optional[float]:
4915-
"""
4916-
Returns network MaxWeightsLimit hyperparameter.
4938+
"""Returns the MaxWeightsLimit hyperparameter for a subnet.
49174939
49184940
Parameters:
49194941
netuid: The unique identifier of the subnetwork.
49204942
block: The blockchain block number for the query.
4921-
block_hash: The blockchain block_hash representation of block id.
4922-
reuse_block: Whether to reuse the last-used block hash.
4943+
block_hash: The hash of the block at which to query. Do not set if using ``block`` or ``reuse_block``.
4944+
reuse_block: Whether to reuse the last-used block hash. Do not set if using ``block_hash`` or ``block``.
49234945
49244946
Returns:
4925-
The value of the MaxWeightsLimit hyperparameter, or ``None`` if the subnetwork does not exist or the
4926-
parameter is not found.
4947+
The stored maximum weight limit as a normalized float in [0, 1], or ``None`` if the subnetwork
4948+
does not exist. Note: this value is not actually enforced - the weight validation code uses
4949+
a hardcoded u16::MAX instead.
4950+
4951+
Notes:
4952+
- This hyperparameter is now a constant rather than a settable variable.
4953+
- <https://docs.learnbittensor.org/subnets/subnet-hyperparameters>
49274954
"""
49284955
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)
49294956
call = await self.get_hyperparameter(
@@ -4976,18 +5003,25 @@ async def min_allowed_weights(
49765003
block_hash: Optional[str] = None,
49775004
reuse_block: bool = False,
49785005
) -> Optional[int]:
4979-
"""
4980-
Returns network MinAllowedWeights hyperparameter.
5006+
"""Returns the MinAllowedWeights hyperparameter for a subnet.
5007+
5008+
This hyperparameter sets the minimum length of the weights vector that a validator must submit.
5009+
It checks ``weights.len() >= MinAllowedWeights``. For example, a validator could submit ``[1000, 0, 0, 0]``
5010+
to satisfy ``MinAllowedWeights=4``, but this would fail if ``MinAllowedWeights`` were set to 5.
5011+
This ensures validators distribute attention across the subnet.
49815012
49825013
Parameters:
49835014
netuid: The unique identifier of the subnetwork.
49845015
block: The blockchain block number for the query.
4985-
block_hash: The blockchain block_hash representation of block id.
4986-
reuse_block: Whether to reuse the last-used block hash.
5016+
block_hash: The hash of the block at which to query. Do not set if using ``block`` or ``reuse_block``.
5017+
reuse_block: Whether to reuse the last-used block hash. Do not set if using ``block_hash`` or ``block``.
49875018
49885019
Returns:
4989-
The value of the MinAllowedWeights hyperparameter, or ``None`` if the subnetwork does not exist or the
4990-
parameter is not found.
5020+
The minimum number of required weight connections, or ``None`` if the subnetwork does not
5021+
exist or the parameter is not found.
5022+
5023+
Notes:
5024+
- <https://docs.learnbittensor.org/subnets/subnet-hyperparameters>
49915025
"""
49925026
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)
49935027
call = await self.get_hyperparameter(
@@ -5174,18 +5208,23 @@ async def recycle(
51745208
block_hash: Optional[str] = None,
51755209
reuse_block: bool = False,
51765210
) -> Optional[Balance]:
5177-
"""
5178-
Retrieves the 'Burn' hyperparameter for a specified subnet. The 'Burn' parameter represents the amount of Tao
5179-
that is effectively recycled within the Bittensor network.
5211+
"""Retrieves the 'Burn' hyperparameter for a specified subnet.
5212+
5213+
The 'Burn' parameter represents the amount of TAO that is recycled when registering a neuron
5214+
on this subnet. Recycled tokens are removed from circulation but can be re-emitted, unlike
5215+
burned tokens which are permanently removed.
51805216
51815217
Parameters:
51825218
netuid: The unique identifier of the subnet.
51835219
block: The blockchain block number for the query.
5184-
block_hash: The hash of the blockchain block number for the query.
5185-
reuse_block: Whether to reuse the last-used blockchain block hash.
5220+
block_hash: The hash of the block at which to query. Do not set if using ``block`` or ``reuse_block``.
5221+
reuse_block: Whether to reuse the last-used block hash. Do not set if using ``block_hash`` or ``block``.
51865222
51875223
Returns:
5188-
The value of the 'Burn' hyperparameter if the subnet exists, ``None`` otherwise.
5224+
The amount of TAO recycled per neuron registration, or ``None`` if the subnet does not exist.
5225+
5226+
Notes:
5227+
- <https://docs.learnbittensor.org/resources/glossary#recycling-and-burning>
51895228
"""
51905229
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)
51915230
call = await self.get_hyperparameter(
@@ -5280,18 +5319,17 @@ async def subnetwork_n(
52805319
block_hash: Optional[str] = None,
52815320
reuse_block: bool = False,
52825321
) -> Optional[int]:
5283-
"""
5284-
Returns network SubnetworkN hyperparameter.
5322+
"""Returns the current number of registered neurons (UIDs) in a subnet.
52855323
52865324
Parameters:
52875325
netuid: The unique identifier of the subnetwork.
52885326
block: The blockchain block number for the query.
5289-
block_hash: The hash of the blockchain block number at which to check the subnet existence.
5290-
reuse_block: Whether to reuse the last-used block hash.
5327+
block_hash: The hash of the block at which to query. Do not set if using ``block`` or ``reuse_block``.
5328+
reuse_block: Whether to reuse the last-used block hash. Do not set if using ``block_hash`` or ``block``.
52915329
52925330
Returns:
5293-
The value of the SubnetworkN hyperparameter, or ``None`` if the subnetwork does not exist or the parameter
5294-
is not found.
5331+
The current number of registered neurons in the subnet, or ``None`` if the subnetwork does not exist.
5332+
52955333
"""
52965334
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)
52975335
call = await self.get_hyperparameter(
@@ -5309,18 +5347,24 @@ async def tempo(
53095347
block_hash: Optional[str] = None,
53105348
reuse_block: bool = False,
53115349
) -> Optional[int]:
5312-
"""
5313-
Returns network Tempo hyperparameter.
5350+
"""Returns the Tempo hyperparameter for a subnet.
5351+
5352+
Tempo determines the length of an epoch in blocks. It defines how frequently the subnet's consensus mechanism
5353+
runs, calculating emissions and updating rankings. A tempo of 360 blocks equals approximately 72 minutes
5354+
(360 blocks × 12 seconds per block).
53145355
53155356
Parameters:
53165357
netuid: The unique identifier of the subnetwork.
53175358
block: The blockchain block number for the query.
5318-
block_hash: The hash of the blockchain block number at which to check the subnet existence.
5319-
reuse_block: Whether to reuse the last-used block hash.
5359+
block_hash: The hash of the block at which to query. Do not set if using ``block`` or ``reuse_block``.
5360+
reuse_block: Whether to reuse the last-used block hash. Do not set if using ``block_hash`` or ``block``.
53205361
53215362
Returns:
5322-
The value of the Tempo hyperparameter, or ``None`` if the subnetwork does not exist or the parameter is not
5323-
found.
5363+
The tempo value in blocks, or ``None`` if the subnetwork does not exist.
5364+
5365+
Notes:
5366+
- <https://docs.learnbittensor.org/resources/glossary#tempo>
5367+
- <https://docs.learnbittensor.org/resources/glossary#epoch>
53245368
"""
53255369
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)
53265370
call = await self.get_hyperparameter(
@@ -5444,17 +5488,20 @@ async def weights_rate_limit(
54445488
block_hash: Optional[str] = None,
54455489
reuse_block: bool = False,
54465490
) -> Optional[int]:
5447-
"""
5448-
Returns network WeightsSetRateLimit hyperparameter.
5491+
"""Returns the WeightsSetRateLimit hyperparameter for a subnet.
5492+
5493+
This hyperparameter limits how many times a validator can set weights per epoch. It prevents validators
5494+
from spamming weight updates and ensures stable consensus calculations. Once the limit is reached, validators
5495+
must wait until the next epoch to set weights again.
54495496
54505497
Parameters:
54515498
netuid: The unique identifier of the subnetwork.
54525499
block: The blockchain block number for the query.
5453-
block_hash: The blockchain block_hash representation of the block id.
5454-
reuse_block: Whether to reuse the last-used blockchain block hash.
5500+
block_hash: The hash of the block at which to query. Do not set if using ``block`` or ``reuse_block``.
5501+
reuse_block: Whether to reuse the last-used block hash. Do not set if using ``block_hash`` or ``block``.
54555502
54565503
Returns:
5457-
Optional[int]: The value of the WeightsSetRateLimit hyperparameter, or ``None`` if the subnetwork does not
5504+
The maximum number of weight set operations allowed per epoch, or ``None`` if the subnetwork does not
54585505
exist or the parameter is not found.
54595506
"""
54605507
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)

0 commit comments

Comments
 (0)