Skip to content

Commit 9722bc4

Browse files
committed
Remove sql_lru_cache from sync substrate and async substrate, and instead only use it in an experimental new class for just async substrate.
1 parent 98ea830 commit 9722bc4

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
TYPE_CHECKING,
2222
)
2323

24+
import asyncstdlib as a
2425
from bittensor_wallet.keypair import Keypair
2526
from bittensor_wallet.utils import SS58_FORMAT
2627
from bt_decode import MetadataV15, PortableRegistry, decode as decode_by_type_string
@@ -1659,8 +1660,11 @@ def convert_event_data(data):
16591660
events.append(convert_event_data(item))
16601661
return events
16611662

1662-
@async_sql_lru_cache(max_size=512)
1663+
@a.lru_cache(maxsize=512)
16631664
async def get_parent_block_hash(self, block_hash):
1665+
return await self._get_parent_block_hash(block_hash)
1666+
1667+
async def _get_parent_block_hash(self, block_hash):
16641668
block_header = await self.rpc_request("chain_getHeader", [block_hash])
16651669

16661670
if block_header["result"] is None:
@@ -1672,16 +1676,22 @@ async def get_parent_block_hash(self, block_hash):
16721676
return block_hash
16731677
return parent_block_hash
16741678

1675-
@async_sql_lru_cache(max_size=16)
1679+
@a.lru_cache(maxsize=16)
16761680
async def get_block_runtime_info(self, block_hash: str) -> dict:
1681+
return await self._get_block_runtime_info(block_hash)
1682+
1683+
async def _get_block_runtime_info(self, block_hash: str) -> dict:
16771684
"""
16781685
Retrieve the runtime info of given block_hash
16791686
"""
16801687
response = await self.rpc_request("state_getRuntimeVersion", [block_hash])
16811688
return response.get("result")
16821689

1683-
@async_sql_lru_cache(max_size=512)
1690+
@a.lru_cache(maxsize=512)
16841691
async def get_block_runtime_version_for(self, block_hash: str):
1692+
return await self._get_block_runtime_version_for(block_hash)
1693+
1694+
async def _get_block_runtime_version_for(self, block_hash: str):
16851695
"""
16861696
Retrieve the runtime version of the parent of a given block_hash
16871697
"""
@@ -1914,7 +1924,6 @@ async def _make_rpc_request(
19141924

19151925
return request_manager.get_results()
19161926

1917-
@async_sql_lru_cache(max_size=512)
19181927
async def supports_rpc_method(self, name: str) -> bool:
19191928
"""
19201929
Check if substrate RPC supports given method
@@ -1985,8 +1994,11 @@ async def rpc_request(
19851994
else:
19861995
raise SubstrateRequestException(result[payload_id][0])
19871996

1988-
@async_sql_lru_cache(max_size=512)
1997+
@a.lru_cache(maxsize=512)
19891998
async def get_block_hash(self, block_id: int) -> str:
1999+
return await self._get_block_hash(block_id)
2000+
2001+
async def _get_block_hash(self, block_id: int) -> str:
19902002
return (await self.rpc_request("chain_getBlockHash", [block_id]))["result"]
19912003

19922004
async def get_chain_head(self) -> str:
@@ -3230,6 +3242,28 @@ async def _handler(block_data: dict[str, Any]):
32303242
return await co
32313243

32323244

3245+
class DiskCachedAsyncSubstrateInterface(AsyncSubstrateInterface):
3246+
"""
3247+
Experimental new class that uses disk-caching in addition to memory-caching for the cached methods
3248+
"""
3249+
3250+
@async_sql_lru_cache(maxsize=512)
3251+
async def get_parent_block_hash(self, block_hash):
3252+
return await self._get_parent_block_hash(block_hash)
3253+
3254+
@async_sql_lru_cache(maxsize=16)
3255+
async def get_block_runtime_info(self, block_hash: str) -> dict:
3256+
return await self._get_block_runtime_info(block_hash)
3257+
3258+
@async_sql_lru_cache(maxsize=512)
3259+
async def get_block_runtime_version_for(self, block_hash: str):
3260+
return await self._get_block_runtime_version_for(block_hash)
3261+
3262+
@async_sql_lru_cache(maxsize=512)
3263+
async def get_block_hash(self, block_id: int) -> str:
3264+
return await self._get_block_hash(block_id)
3265+
3266+
32333267
async def get_async_substrate_interface(
32343268
url: str,
32353269
use_remote_preset: bool = False,

async_substrate_interface/sync_substrate.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import functools
12
import logging
23
import random
34
from hashlib import blake2b
@@ -30,7 +31,6 @@
3031
ScaleObj,
3132
)
3233
from async_substrate_interface.utils import hex_to_bytes, json, get_next_id
33-
from async_substrate_interface.utils.cache import sql_lru_cache
3434
from async_substrate_interface.utils.decoding import (
3535
_determine_if_old_runtime_call,
3636
_bt_decode_to_dict_or_list,
@@ -1406,7 +1406,7 @@ def convert_event_data(data):
14061406
events.append(convert_event_data(item))
14071407
return events
14081408

1409-
@sql_lru_cache(max_size=512)
1409+
@functools.lru_cache(maxsize=512)
14101410
def get_parent_block_hash(self, block_hash):
14111411
block_header = self.rpc_request("chain_getHeader", [block_hash])
14121412

@@ -1419,15 +1419,15 @@ def get_parent_block_hash(self, block_hash):
14191419
return block_hash
14201420
return parent_block_hash
14211421

1422-
@sql_lru_cache(max_size=16)
1422+
@functools.lru_cache(maxsize=16)
14231423
def get_block_runtime_info(self, block_hash: str) -> dict:
14241424
"""
14251425
Retrieve the runtime info of given block_hash
14261426
"""
14271427
response = self.rpc_request("state_getRuntimeVersion", [block_hash])
14281428
return response.get("result")
14291429

1430-
@sql_lru_cache(max_size=512)
1430+
@functools.lru_cache(maxsize=512)
14311431
def get_block_runtime_version_for(self, block_hash: str):
14321432
"""
14331433
Retrieve the runtime version of the parent of a given block_hash
@@ -1655,8 +1655,7 @@ def _make_rpc_request(
16551655

16561656
return request_manager.get_results()
16571657

1658-
# TODO change this logic
1659-
@sql_lru_cache(max_size=512)
1658+
@functools.lru_cache(maxsize=512)
16601659
def supports_rpc_method(self, name: str) -> bool:
16611660
"""
16621661
Check if substrate RPC supports given method
@@ -1727,7 +1726,7 @@ def rpc_request(
17271726
else:
17281727
raise SubstrateRequestException(result[payload_id][0])
17291728

1730-
@sql_lru_cache(max_size=512)
1729+
@functools.lru_cache(maxsize=512)
17311730
def get_block_hash(self, block_id: int) -> str:
17321731
return self.rpc_request("chain_getBlockHash", [block_id])["result"]
17331732

async_substrate_interface/utils/cache.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ def _insert_into_cache(c, conn, table_name, key, result, chain):
5353
pass
5454

5555

56-
def sql_lru_cache(max_size=None):
56+
def sql_lru_cache(maxsize=None):
5757
def decorator(func):
5858
conn = sqlite3.connect(CACHE_LOCATION)
5959
c = conn.cursor()
6060
table_name = _get_table_name(func)
6161
_create_table(c, conn, table_name)
6262

63-
@functools.lru_cache(maxsize=max_size)
63+
@functools.lru_cache(maxsize=maxsize)
6464
def inner(self, *args, **kwargs):
6565
c = conn.cursor()
6666
key = pickle.dumps((args, kwargs))
@@ -83,14 +83,14 @@ def inner(self, *args, **kwargs):
8383
return decorator
8484

8585

86-
def async_sql_lru_cache(max_size=None):
86+
def async_sql_lru_cache(maxsize=None):
8787
def decorator(func):
8888
conn = sqlite3.connect(CACHE_LOCATION)
8989
c = conn.cursor()
9090
table_name = _get_table_name(func)
9191
_create_table(c, conn, table_name)
9292

93-
@a.lru_cache(maxsize=max_size)
93+
@a.lru_cache(maxsize=maxsize)
9494
async def inner(self, *args, **kwargs):
9595
c = conn.cursor()
9696
key = pickle.dumps((args, kwargs))

0 commit comments

Comments
 (0)