2222 TYPE_CHECKING ,
2323)
2424
25- import asyncstdlib as a
2625from bt_decode import MetadataV15 , PortableRegistry , decode as decode_by_type_string
2726from scalecodec .base import ScaleBytes , ScaleType , RuntimeConfigurationObject
2827from scalecodec .types import (
5857 get_next_id ,
5958 rng as random ,
6059)
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
6261from async_substrate_interface .utils .decoding import (
6362 _determine_if_old_runtime_call ,
6463 _bt_decode_to_dict_or_list ,
@@ -748,6 +747,12 @@ def __init__(
748747 self .registry_type_map = {}
749748 self .type_id_to_name = {}
750749 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+ )
751756
752757 async def __aenter__ (self ):
753758 if not self ._mock :
@@ -1869,9 +1874,8 @@ async def get_metadata(self, block_hash=None) -> MetadataV15:
18691874
18701875 return runtime .metadata_v15
18711876
1872- @a .lru_cache (maxsize = 512 )
18731877 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 )
18751879
18761880 async def _get_parent_block_hash (self , block_hash ):
18771881 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:
19161920 "Unknown error occurred during retrieval of events"
19171921 )
19181922
1919- @a .lru_cache (maxsize = 16 )
19201923 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 )
19221925
19231926 get_block_runtime_version = get_block_runtime_info
19241927
@@ -1929,9 +1932,8 @@ async def _get_block_runtime_info(self, block_hash: str) -> dict:
19291932 response = await self .rpc_request ("state_getRuntimeVersion" , [block_hash ])
19301933 return response .get ("result" )
19311934
1932- @a .lru_cache (maxsize = 512 )
19331935 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 )
19351937
19361938 async def _get_block_runtime_version_for (self , block_hash : str ):
19371939 """
@@ -2240,9 +2242,8 @@ async def rpc_request(
22402242 else :
22412243 raise SubstrateRequestException (result [payload_id ][0 ])
22422244
2243- @a .lru_cache (maxsize = 512 )
22442245 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 )
22462247
22472248 async def _get_block_hash (self , block_id : int ) -> str :
22482249 return (await self .rpc_request ("chain_getBlockHash" , [block_id ]))["result" ]
0 commit comments