Skip to content

Commit 993fb22

Browse files
tests
1 parent 0057a5e commit 993fb22

File tree

6 files changed

+79
-2
lines changed

6 files changed

+79
-2
lines changed

bittensor/core/async_subtensor.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from bittensor.core.chain_data.chain_identity import ChainIdentity
3333
from bittensor.core.chain_data.delegate_info import DelegatedInfo
3434
from bittensor.core.chain_data.utils import (
35+
decode_block,
3536
decode_metadata,
3637
decode_revealed_commitment,
3738
decode_revealed_commitment_with_hotkey,
@@ -55,6 +56,7 @@
5556
root_register_extrinsic,
5657
)
5758
from bittensor.core.extrinsics.asyncex.serving import (
59+
get_last_bonds_reset,
5860
publish_metadata,
5961
get_metadata,
6062
)
@@ -1108,6 +1110,31 @@ async def get_commitment(
11081110
except TypeError:
11091111
return ""
11101112

1113+
async def get_last_commitment_bonds_reset_block(
1114+
self, netuid: int, uid: int
1115+
) -> Optional[int]:
1116+
"""
1117+
Retrieves the last block number when the bonds reset were triggered by publish_metadata for a specific neuron.
1118+
Arguments:
1119+
netuid (int): The unique identifier of the subnetwork.
1120+
uid (int): The unique identifier of the neuron.
1121+
Returns:
1122+
Optional[int]: The block number when the bonds were last reset, or None if not found.
1123+
"""
1124+
1125+
metagraph = await self.metagraph(netuid)
1126+
try:
1127+
hotkey = metagraph.hotkeys[uid] # type: ignore
1128+
except IndexError:
1129+
logging.error(
1130+
"Your uid is not in the hotkeys. Please double-check your UID."
1131+
)
1132+
return None
1133+
block = await get_last_bonds_reset(self, netuid, hotkey)
1134+
if block is None:
1135+
return None
1136+
return await decode_block(block)
1137+
11111138
async def get_all_commitments(
11121139
self,
11131140
netuid: int,

bittensor/core/chain_data/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ def decode_block(data: bytes) -> int:
148148
Returns:
149149
int: The decoded block.
150150
"""
151-
152-
return int.from_bytes(block)
151+
return int(data.value) if isinstance(data, ScaleBytes) else data
153152

154153

155154
def decode_revealed_commitment(encoded_data) -> tuple[int, str]:

bittensor/core/extrinsics/asyncex/serving.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,21 @@ async def get_metadata(
320320
reuse_block_hash=reuse_block,
321321
)
322322
return commit_data
323+
324+
325+
async def get_last_bonds_reset(
326+
subtensor: "AsyncSubtensor",
327+
netuid: int,
328+
hotkey: str,
329+
block: Optional[int] = None,
330+
) -> bytes:
331+
"""Fetches the last bonds reset triggered at commitment from the blockchain for a given hotkey and netuid."""
332+
async with subtensor.substrate:
333+
block_hash = await subtensor.determine_block_hash(block)
334+
block = subtensor.substrate.query(
335+
module="Commitments",
336+
storage_function="LastBondsReset",
337+
params=[netuid, hotkey],
338+
block_hash=block_hash,
339+
)
340+
return block

bittensor/core/subtensor_api/commitments.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ def __init__(self, subtensor: Union["_Subtensor", "_AsyncSubtensor"]):
1111
self.get_all_commitments = subtensor.get_all_commitments
1212
self.get_all_revealed_commitments = subtensor.get_all_revealed_commitments
1313
self.get_commitment = subtensor.get_commitment
14+
self.get_last_commitment_bonds_reset_block = (
15+
subtensor.get_last_commitment_bonds_reset_block
16+
)
1417
self.get_current_weight_commit_info = subtensor.get_current_weight_commit_info
1518
self.get_revealed_commitment = subtensor.get_revealed_commitment
1619
self.get_revealed_commitment_by_hotkey = (

bittensor/core/subtensor_api/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ def add_legacy_methods(subtensor: "SubtensorApi"):
3939
subtensor.get_children = subtensor._subtensor.get_children
4040
subtensor.get_children_pending = subtensor._subtensor.get_children_pending
4141
subtensor.get_commitment = subtensor._subtensor.get_commitment
42+
subtensor.get_last_commitment_bonds_reset_block = (
43+
subtensor._subtensor.get_last_commitment_bonds_reset_block
44+
)
4245
subtensor.get_current_block = subtensor._subtensor.get_current_block
4346
subtensor.get_current_weight_commit_info = (
4447
subtensor._subtensor.get_current_weight_commit_info

tests/unit_tests/test_subtensor.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,33 @@ def test_get_commitment(subtensor, mocker):
18131813
assert result == expected_result
18141814

18151815

1816+
def test_get_last_commitment_bonds_reset_block(subtensor, mocker):
1817+
"""Successful get_last_commitment_bonds_reset_block call."""
1818+
# Preps
1819+
fake_netuid = 1
1820+
fake_uid = 2
1821+
fake_hotkey = "hotkey"
1822+
expected_result = 3
1823+
1824+
mocked_get_last_bonds_reset = mocker.patch.object(
1825+
subtensor_module, "get_last_bonds_reset"
1826+
)
1827+
mocked_get_last_bonds_reset.return_value = expected_result
1828+
1829+
mocked_metagraph = mocker.MagicMock()
1830+
subtensor.metagraph = mocked_metagraph
1831+
mocked_metagraph.return_value.hotkeys = {fake_uid: fake_hotkey}
1832+
1833+
# Call
1834+
result = subtensor.get_last_commitment_bonds_reset_block(
1835+
netuid=fake_netuid, uid=fake_uid
1836+
)
1837+
1838+
# Assertions
1839+
mocked_get_last_bonds_reset.assert_called_once()
1840+
assert result == expected_result
1841+
1842+
18161843
def test_min_allowed_weights(subtensor, mocker):
18171844
"""Successful min_allowed_weights call."""
18181845
fake_netuid = 1

0 commit comments

Comments
 (0)