Skip to content

Commit f6aa766

Browse files
authored
Merge pull request #2865 from opentensor/fix/roman/get_next_epoch_start_block
Fix `get_next_epoch_start_block`
2 parents 0e55d3a + 29736b0 commit f6aa766

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

bittensor/core/async_subtensor.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -616,19 +616,31 @@ async def all_subnets(
616616
return subnets
617617

618618
async def blocks_since_last_step(
619-
self, netuid: int, block: Optional[int] = None
619+
self,
620+
netuid: int,
621+
block: Optional[int] = None,
622+
block_hash: Optional[str] = None,
623+
reuse_block: bool = False,
620624
) -> Optional[int]:
621625
"""Returns number of blocks since the last epoch of the subnet.
622626
623627
Arguments:
624628
netuid (int): The unique identifier of the subnetwork.
625629
block: the block number for this query.
630+
block_hash: The hash of the blockchain block number for the query. Do not specify if using reuse_block or
631+
block.
632+
reuse_block: Whether to reuse the last-used blockchain block hash. Do not set if using block_hash or block.
633+
626634
627635
Returns:
628636
block number of the last step in the subnet.
629637
"""
630638
query = await self.query_subtensor(
631-
name="BlocksSinceLastStep", block=block, params=[netuid]
639+
name="BlocksSinceLastStep",
640+
block=block,
641+
block_hash=block_hash,
642+
reuse_block=reuse_block,
643+
params=[netuid],
632644
)
633645
return query.value if query is not None and hasattr(query, "value") else query
634646

@@ -1803,11 +1815,16 @@ async def get_next_epoch_start_block(
18031815
int: The block number at which the next epoch will start.
18041816
"""
18051817
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)
1806-
if not block_hash and reuse_block:
1807-
block_hash = self.substrate.last_block_hash
1808-
block = await self.substrate.get_block_number(block_hash=block_hash)
1809-
tempo = await self.tempo(netuid=netuid, block_hash=block_hash)
1810-
return (((block // tempo) + 1) * tempo) + 1 if tempo else None
1818+
blocks_since_last_step = await self.blocks_since_last_step(
1819+
netuid=netuid, block=block, block_hash=block_hash, reuse_block=reuse_block
1820+
)
1821+
tempo = await self.tempo(
1822+
netuid=netuid, block=block, block_hash=block_hash, reuse_block=reuse_block
1823+
)
1824+
1825+
if block and blocks_since_last_step and tempo:
1826+
return block - blocks_since_last_step + tempo + 1
1827+
return None
18111828

18121829
async def get_owned_hotkeys(
18131830
self,

bittensor/core/subtensor.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1410,8 +1410,12 @@ def get_next_epoch_start_block(
14101410
int: The block number at which the next epoch will start.
14111411
"""
14121412
block = block or self.block
1413+
blocks_since_last_step = self.blocks_since_last_step(netuid=netuid, block=block)
14131414
tempo = self.tempo(netuid=netuid, block=block)
1414-
return (((block // tempo) + 1) * tempo) + 1 if tempo else None
1415+
1416+
if block and blocks_since_last_step and tempo:
1417+
return block - blocks_since_last_step + tempo + 1
1418+
return None
14151419

14161420
def get_owned_hotkeys(
14171421
self,

tests/unit_tests/test_async_subtensor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3192,6 +3192,8 @@ async def test_blocks_since_last_step_with_value(subtensor, mocker):
31923192
mocked_query_subtensor.assert_awaited_once_with(
31933193
name="BlocksSinceLastStep",
31943194
block=block,
3195+
block_hash=None,
3196+
reuse_block=False,
31953197
params=[netuid],
31963198
)
31973199

@@ -3214,6 +3216,8 @@ async def test_blocks_since_last_step_is_none(subtensor, mocker):
32143216
mocked_query_subtensor.assert_awaited_once_with(
32153217
name="BlocksSinceLastStep",
32163218
block=block,
3219+
block_hash=None,
3220+
reuse_block=False,
32173221
params=[netuid],
32183222
)
32193223

0 commit comments

Comments
 (0)