22
22
TYPE_CHECKING ,
23
23
)
24
24
25
- import asyncstdlib as a
26
25
from bt_decode import MetadataV15 , PortableRegistry , decode as decode_by_type_string
27
26
from scalecodec .base import ScaleBytes , ScaleType , RuntimeConfigurationObject
28
27
from scalecodec .types import (
58
57
get_next_id ,
59
58
rng as random ,
60
59
)
61
- from async_substrate_interface .utils .cache import async_sql_lru_cache
60
+ from async_substrate_interface .utils .cache import async_sql_lru_cache , CachedFetcher
62
61
from async_substrate_interface .utils .decoding import (
63
62
_determine_if_old_runtime_call ,
64
63
_bt_decode_to_dict_or_list ,
@@ -748,6 +747,12 @@ def __init__(
748
747
self .registry_type_map = {}
749
748
self .type_id_to_name = {}
750
749
self ._mock = _mock
750
+ self ._block_hash_fetcher = CachedFetcher (512 , self ._get_block_hash )
751
+ self ._parent_hash_fetcher = CachedFetcher (512 , self ._get_parent_block_hash )
752
+ self ._runtime_info_fetcher = CachedFetcher (16 , self ._get_block_runtime_info )
753
+ self ._runtime_version_for_fetcher = CachedFetcher (
754
+ 512 , self ._get_block_runtime_version_for
755
+ )
751
756
752
757
async def __aenter__ (self ):
753
758
if not self ._mock :
@@ -1869,9 +1874,8 @@ async def get_metadata(self, block_hash=None) -> MetadataV15:
1869
1874
1870
1875
return runtime .metadata_v15
1871
1876
1872
- @a .lru_cache (maxsize = 512 )
1873
1877
async def get_parent_block_hash (self , block_hash ):
1874
- return await self ._get_parent_block_hash (block_hash )
1878
+ return await self ._parent_hash_fetcher . execute (block_hash )
1875
1879
1876
1880
async def _get_parent_block_hash (self , block_hash ):
1877
1881
block_header = await self .rpc_request ("chain_getHeader" , [block_hash ])
@@ -1916,9 +1920,8 @@ async def get_storage_by_key(self, block_hash: str, storage_key: str) -> Any:
1916
1920
"Unknown error occurred during retrieval of events"
1917
1921
)
1918
1922
1919
- @a .lru_cache (maxsize = 16 )
1920
1923
async def get_block_runtime_info (self , block_hash : str ) -> dict :
1921
- return await self ._get_block_runtime_info (block_hash )
1924
+ return await self ._runtime_info_fetcher . execute (block_hash )
1922
1925
1923
1926
get_block_runtime_version = get_block_runtime_info
1924
1927
@@ -1929,9 +1932,8 @@ async def _get_block_runtime_info(self, block_hash: str) -> dict:
1929
1932
response = await self .rpc_request ("state_getRuntimeVersion" , [block_hash ])
1930
1933
return response .get ("result" )
1931
1934
1932
- @a .lru_cache (maxsize = 512 )
1933
1935
async def get_block_runtime_version_for (self , block_hash : str ):
1934
- return await self ._get_block_runtime_version_for (block_hash )
1936
+ return await self ._runtime_version_for_fetcher . execute (block_hash )
1935
1937
1936
1938
async def _get_block_runtime_version_for (self , block_hash : str ):
1937
1939
"""
@@ -2240,9 +2242,8 @@ async def rpc_request(
2240
2242
else :
2241
2243
raise SubstrateRequestException (result [payload_id ][0 ])
2242
2244
2243
- @a .lru_cache (maxsize = 512 )
2244
2245
async def get_block_hash (self , block_id : int ) -> str :
2245
- return await self ._get_block_hash (block_id )
2246
+ return await self ._block_hash_fetcher . execute (block_id )
2246
2247
2247
2248
async def _get_block_hash (self , block_id : int ) -> str :
2248
2249
return (await self .rpc_request ("chain_getBlockHash" , [block_id ]))["result" ]
0 commit comments