Skip to content

Commit 529d204

Browse files
authored
Merge pull request #2877 from opentensor/feat/roman/add-is_subnet_active
Add subtensor.is_subnet_active method
2 parents ca79ebc + 176af07 commit 529d204

File tree

6 files changed

+90
-1
lines changed

6 files changed

+90
-1
lines changed

bittensor/core/async_subtensor.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,6 +2582,35 @@ async def is_hotkey_registered_on_subnet(
25822582
is not None
25832583
)
25842584

2585+
async def is_subnet_active(
2586+
self,
2587+
netuid: int,
2588+
block: Optional[int] = None,
2589+
block_hash: Optional[str] = None,
2590+
reuse_block: bool = False,
2591+
) -> bool:
2592+
"""Verify if subnet with provided netuid is active.
2593+
2594+
Args:
2595+
netuid (int): The unique identifier of the subnet.
2596+
block (Optional[int]): The blockchain block number for the query.
2597+
block_hash (Optional[str]): The blockchain block_hash representation of block id.
2598+
reuse_block (bool): Whether to reuse the last-used block hash.
2599+
2600+
Returns:
2601+
True if subnet is active, False otherwise.
2602+
2603+
This means whether the `start_call` was initiated or not.
2604+
"""
2605+
query = await self.query_subtensor(
2606+
name="FirstEmissionBlockNumber",
2607+
block=block,
2608+
block_hash=block_hash,
2609+
reuse_block=reuse_block,
2610+
params=[netuid],
2611+
)
2612+
return True if query and query.value > 0 else False
2613+
25852614
async def last_drand_round(self) -> Optional[int]:
25862615
"""
25872616
Retrieves the last drand round emitted in bittensor. This corresponds when committed weights will be revealed.

bittensor/core/subtensor.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,6 +2023,25 @@ def is_hotkey_registered_on_subnet(
20232023
is not None
20242024
)
20252025

2026+
def is_subnet_active(self, netuid: int, block: Optional[int] = None) -> bool:
2027+
"""Verify if subnet with provided netuid is active.
2028+
2029+
Args:
2030+
netuid (int): The unique identifier of the subnet.
2031+
block (Optional[int]): The blockchain block number for the query.
2032+
2033+
Returns:
2034+
True if subnet is active, False otherwise.
2035+
2036+
This means whether the `start_call` was initiated or not.
2037+
"""
2038+
query = self.query_subtensor(
2039+
name="FirstEmissionBlockNumber",
2040+
block=block,
2041+
params=[netuid],
2042+
)
2043+
return True if query and query.value > 0 else False
2044+
20262045
def last_drand_round(self) -> Optional[int]:
20272046
"""
20282047
Retrieves the last drand round emitted in bittensor. This corresponds when committed weights will be revealed.

bittensor/core/subtensor_api/subnets.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def __init__(self, subtensor: Union["_Subtensor", "_AsyncSubtensor"]):
3232
self.get_uid_for_hotkey_on_subnet = subtensor.get_uid_for_hotkey_on_subnet
3333
self.immunity_period = subtensor.immunity_period
3434
self.is_hotkey_registered_on_subnet = subtensor.is_hotkey_registered_on_subnet
35+
self.is_subnet_active = subtensor.is_subnet_active
3536
self.max_weight_limit = subtensor.max_weight_limit
3637
self.min_allowed_weights = subtensor.min_allowed_weights
3738
self.recycle = subtensor.recycle

bittensor/core/subtensor_api/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def add_legacy_methods(subtensor: "SubtensorApi"):
108108
subtensor.is_hotkey_registered_on_subnet = (
109109
subtensor._subtensor.is_hotkey_registered_on_subnet
110110
)
111+
subtensor.is_subnet_active = subtensor._subtensor.is_subnet_active
111112
subtensor.last_drand_round = subtensor._subtensor.last_drand_round
112113
subtensor.log_verbose = subtensor._subtensor.log_verbose
113114
subtensor.max_weight_limit = subtensor._subtensor.max_weight_limit

tests/e2e_tests/utils/e2e_test_utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def validator(self, wallet, netuid):
229229

230230

231231
def wait_to_start_call(
232-
subtensor: "bittensor.Subtensor",
232+
subtensor: "bittensor.SubtensorApi",
233233
subnet_owner_wallet: "bittensor.Wallet",
234234
netuid: int,
235235
in_blocks: int = 10,
@@ -242,6 +242,11 @@ def wait_to_start_call(
242242
f"Current block: [blue]{subtensor.block}[/blue]."
243243
)
244244

245+
# make sure subnet isn't active
246+
assert subtensor.subnets.is_subnet_active(netuid) is False, (
247+
"Subnet is already active."
248+
)
249+
245250
# make sure we passed start_call limit
246251
subtensor.wait_for_block(subtensor.block + in_blocks + 1)
247252
status, message = subtensor.start_call(
@@ -251,6 +256,11 @@ def wait_to_start_call(
251256
wait_for_finalization=True,
252257
)
253258
assert status, message
259+
# make sure subnet is active
260+
assert subtensor.subnets.is_subnet_active(netuid), (
261+
"Subnet did not activated after start call."
262+
)
263+
254264
return True
255265

256266

tests/unit_tests/test_subtensor.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3633,3 +3633,32 @@ def test_get_subnet_validator_permits_is_none(subtensor, mocker):
36333633
)
36343634

36353635
assert result is None
3636+
3637+
3638+
@pytest.mark.parametrize(
3639+
"query_return, expected",
3640+
[
3641+
[111, True],
3642+
[0, False],
3643+
],
3644+
)
3645+
def test_is_subnet_active(subtensor, mocker, query_return, expected):
3646+
# preps
3647+
netuid = mocker.Mock()
3648+
block = mocker.Mock()
3649+
mocked_query_subtensor = mocker.MagicMock(
3650+
return_value=mocker.Mock(value=query_return)
3651+
)
3652+
subtensor.query_subtensor = mocked_query_subtensor
3653+
3654+
# call
3655+
result = subtensor.is_subnet_active(netuid=netuid, block=block)
3656+
3657+
# Asserts
3658+
mocked_query_subtensor.assert_called_once_with(
3659+
name="FirstEmissionBlockNumber",
3660+
block=block,
3661+
params=[netuid],
3662+
)
3663+
3664+
assert result == expected

0 commit comments

Comments
 (0)