Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions bittensor/core/async_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,19 +616,31 @@ async def all_subnets(
return subnets

async def blocks_since_last_step(
self, netuid: int, block: Optional[int] = None
self,
netuid: int,
block: Optional[int] = None,
block_hash: Optional[str] = None,
reuse_block: bool = False,
) -> Optional[int]:
"""Returns number of blocks since the last epoch of the subnet.

Arguments:
netuid (int): The unique identifier of the subnetwork.
block: the block number for this query.
block_hash: The hash of the blockchain block number for the query. Do not specify if using reuse_block or
block.
reuse_block: Whether to reuse the last-used blockchain block hash. Do not set if using block_hash or block.


Returns:
block number of the last step in the subnet.
"""
query = await self.query_subtensor(
name="BlocksSinceLastStep", block=block, params=[netuid]
name="BlocksSinceLastStep",
block=block,
block_hash=block_hash,
reuse_block=reuse_block,
params=[netuid],
)
return query.value if query is not None and hasattr(query, "value") else query

Expand Down Expand Up @@ -1803,11 +1815,16 @@ async def get_next_epoch_start_block(
int: The block number at which the next epoch will start.
"""
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)
if not block_hash and reuse_block:
block_hash = self.substrate.last_block_hash
block = await self.substrate.get_block_number(block_hash=block_hash)
tempo = await self.tempo(netuid=netuid, block_hash=block_hash)
return (((block // tempo) + 1) * tempo) + 1 if tempo else None
blocks_since_last_step = await self.blocks_since_last_step(
netuid=netuid, block=block, block_hash=block_hash, reuse_block=reuse_block
)
tempo = await self.tempo(
netuid=netuid, block=block, block_hash=block_hash, reuse_block=reuse_block
)

if block and blocks_since_last_step and tempo:
return block - blocks_since_last_step + tempo + 1
return None

async def get_owned_hotkeys(
self,
Expand Down
6 changes: 5 additions & 1 deletion bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1410,8 +1410,12 @@ def get_next_epoch_start_block(
int: The block number at which the next epoch will start.
"""
block = block or self.block
blocks_since_last_step = self.blocks_since_last_step(netuid=netuid, block=block)
tempo = self.tempo(netuid=netuid, block=block)
return (((block // tempo) + 1) * tempo) + 1 if tempo else None

if block and blocks_since_last_step and tempo:
return block - blocks_since_last_step + tempo + 1
return None

def get_owned_hotkeys(
self,
Expand Down
4 changes: 4 additions & 0 deletions tests/unit_tests/test_async_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3192,6 +3192,8 @@ async def test_blocks_since_last_step_with_value(subtensor, mocker):
mocked_query_subtensor.assert_awaited_once_with(
name="BlocksSinceLastStep",
block=block,
block_hash=None,
reuse_block=False,
params=[netuid],
)

Expand All @@ -3214,6 +3216,8 @@ async def test_blocks_since_last_step_is_none(subtensor, mocker):
mocked_query_subtensor.assert_awaited_once_with(
name="BlocksSinceLastStep",
block=block,
block_hash=None,
reuse_block=False,
params=[netuid],
)

Expand Down
Loading