Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thewhaleking committed Dec 17, 2024
1 parent 0e7a9de commit c4c301d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
4 changes: 2 additions & 2 deletions bittensor/core/async_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Optional, Any, Union, TypedDict, Iterable, TYPE_CHECKING

import aiohttp
from async_lru import alru_cache
import asyncstdlib as a
import numpy as np
import scalecodec
from bittensor_wallet import Wallet
Expand Down Expand Up @@ -284,7 +284,7 @@ async def get_current_block(self) -> int:
"""
return await self.substrate.get_block_number(None)

@alru_cache(maxsize=128)
@a.lru_cache(maxsize=128)
async def _get_block_hash(self, block_id: int):
return await self.substrate.get_block_hash(block_id)

Expand Down
3 changes: 3 additions & 0 deletions bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ def __init__(
self._event_loop = asyncio.get_event_loop()
self._event_loop.run_until_complete(self._subtensor.__aenter__())
self.substrate = SubstrateWrapper(self._subtensor.substrate, self._event_loop)
self._init_subtensor()

def _init_subtensor(self):
for attr_name in dir(AsyncSubtensor):
attr = getattr(AsyncSubtensor, attr_name)
if asyncio.iscoroutinefunction(attr):
Expand Down
28 changes: 23 additions & 5 deletions tests/unit_tests/test_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,9 @@ def test_determine_chain_endpoint_and_network(
@pytest.fixture
def subtensor(mocker):
fake_substrate = mocker.AsyncMock()
mocker.patch.object(
subtensor_module, "AsyncSubstrateInterface", return_value=fake_substrate
mocker.patch(
"bittensor.utils.substrate_interface.AsyncSubstrateInterface",
return_value=fake_substrate,
)
return Subtensor()

Expand Down Expand Up @@ -1995,19 +1996,36 @@ def test_recycle_none(subtensor, mocker):
"""Tests recycle method with None result."""
# Preps
mocked_get_hyperparameter = mocker.patch.object(
subtensor, "_get_hyperparameter", return_value=None
subtensor_module.AsyncSubtensor, "get_hyperparameter", return_value=None
)

fake_netuid = 1
fake_block = 2
fake_block_hash = "0x123456789"
fake_reuse_block = True

mocked_determine_block_hash = mocker.patch.object(
subtensor_module.AsyncSubtensor, "_determine_block_hash"
)

# Call
result = subtensor.recycle(fake_netuid, fake_block)
result = subtensor.recycle(
netuid=fake_netuid,
block=fake_block,
block_hash=fake_block_hash,
reuse_block=fake_reuse_block,
)

# Asserts
mocked_determine_block_hash.asseert_awaited_once_with(
fake_block, fake_block_hash, fake_reuse_block
)

mocked_get_hyperparameter.assert_called_once_with(
param_name="Burn",
netuid=fake_netuid,
block=fake_block,
block_hash=mocked_determine_block_hash.return_value,
reuse_block=fake_reuse_block,
)

assert result is None
Expand Down

0 comments on commit c4c301d

Please sign in to comment.