58
58
get_next_id ,
59
59
rng as random ,
60
60
)
61
- from async_substrate_interface .utils .cache import async_sql_lru_cache , CachedFetcher
61
+ from async_substrate_interface .utils .cache import (
62
+ async_sql_lru_cache ,
63
+ CachedFetcher ,
64
+ cached_fetcher ,
65
+ )
62
66
from async_substrate_interface .utils .decoding import (
63
67
_determine_if_old_runtime_call ,
64
68
_bt_decode_to_dict_or_list ,
@@ -794,12 +798,6 @@ def __init__(
794
798
self .registry_type_map = {}
795
799
self .type_id_to_name = {}
796
800
self ._mock = _mock
797
- self ._block_hash_fetcher = CachedFetcher (512 , self ._get_block_hash )
798
- self ._parent_hash_fetcher = CachedFetcher (512 , self ._get_parent_block_hash )
799
- self ._runtime_info_fetcher = CachedFetcher (16 , self ._get_block_runtime_info )
800
- self ._runtime_version_for_fetcher = CachedFetcher (
801
- 512 , self ._get_block_runtime_version_for
802
- )
803
801
804
802
async def __aenter__ (self ):
805
803
if not self ._mock :
@@ -1044,35 +1042,7 @@ async def init_runtime(
1044
1042
if not runtime :
1045
1043
self .last_block_hash = block_hash
1046
1044
1047
- runtime_block_hash = await self .get_parent_block_hash (block_hash )
1048
-
1049
- runtime_info = await self .get_block_runtime_info (runtime_block_hash )
1050
-
1051
- metadata , (metadata_v15 , registry ) = await asyncio .gather (
1052
- self .get_block_metadata (block_hash = runtime_block_hash , decode = True ),
1053
- self ._load_registry_at_block (block_hash = runtime_block_hash ),
1054
- )
1055
- if metadata is None :
1056
- # does this ever happen?
1057
- raise SubstrateRequestException (
1058
- f"No metadata for block '{ runtime_block_hash } '"
1059
- )
1060
- logger .debug (
1061
- f"Retrieved metadata and metadata v15 for { runtime_version } from Substrate node"
1062
- )
1063
-
1064
- runtime = Runtime (
1065
- chain = self .chain ,
1066
- runtime_config = self .runtime_config ,
1067
- metadata = metadata ,
1068
- type_registry = self .type_registry ,
1069
- metadata_v15 = metadata_v15 ,
1070
- runtime_info = runtime_info ,
1071
- registry = registry ,
1072
- )
1073
- self .runtime_cache .add_item (
1074
- runtime_version = runtime_version , runtime = runtime
1075
- )
1045
+ runtime = await self .get_runtime_for_version (runtime_version , block_hash )
1076
1046
1077
1047
self .load_runtime (runtime )
1078
1048
@@ -1086,6 +1056,42 @@ async def init_runtime(
1086
1056
self .ss58_format = ss58_prefix_constant
1087
1057
return runtime
1088
1058
1059
+ @cached_fetcher (max_size = 16 , cache_key_index = 0 )
1060
+ async def get_runtime_for_version (
1061
+ self , runtime_version : int , block_hash : Optional [str ] = None
1062
+ ) -> Runtime :
1063
+ return await self ._get_runtime_for_version (runtime_version , block_hash )
1064
+
1065
+ async def _get_runtime_for_version (
1066
+ self , runtime_version : int , block_hash : Optional [str ] = None
1067
+ ) -> Runtime :
1068
+ runtime_block_hash = await self .get_parent_block_hash (block_hash )
1069
+ runtime_info , metadata , (metadata_v15 , registry ) = await asyncio .gather (
1070
+ self .get_block_runtime_info (runtime_block_hash ),
1071
+ self .get_block_metadata (block_hash = runtime_block_hash , decode = True ),
1072
+ self ._load_registry_at_block (block_hash = runtime_block_hash ),
1073
+ )
1074
+ if metadata is None :
1075
+ # does this ever happen?
1076
+ raise SubstrateRequestException (
1077
+ f"No metadata for block '{ runtime_block_hash } '"
1078
+ )
1079
+ logger .debug (
1080
+ f"Retrieved metadata and metadata v15 for { runtime_version } from Substrate node"
1081
+ )
1082
+
1083
+ runtime = Runtime (
1084
+ chain = self .chain ,
1085
+ runtime_config = self .runtime_config ,
1086
+ metadata = metadata ,
1087
+ type_registry = self .type_registry ,
1088
+ metadata_v15 = metadata_v15 ,
1089
+ runtime_info = runtime_info ,
1090
+ registry = registry ,
1091
+ )
1092
+ self .runtime_cache .add_item (runtime_version = runtime_version , runtime = runtime )
1093
+ return runtime
1094
+
1089
1095
async def create_storage_key (
1090
1096
self ,
1091
1097
pallet : str ,
@@ -1921,8 +1927,9 @@ async def get_metadata(self, block_hash=None) -> MetadataV15:
1921
1927
1922
1928
return runtime .metadata_v15
1923
1929
1930
+ @cached_fetcher (max_size = 512 )
1924
1931
async def get_parent_block_hash (self , block_hash ):
1925
- return await self ._parent_hash_fetcher . execute (block_hash )
1932
+ return await self ._get_parent_block_hash (block_hash )
1926
1933
1927
1934
async def _get_parent_block_hash (self , block_hash ):
1928
1935
block_header = await self .rpc_request ("chain_getHeader" , [block_hash ])
@@ -1967,8 +1974,9 @@ async def get_storage_by_key(self, block_hash: str, storage_key: str) -> Any:
1967
1974
"Unknown error occurred during retrieval of events"
1968
1975
)
1969
1976
1977
+ @cached_fetcher (max_size = 16 )
1970
1978
async def get_block_runtime_info (self , block_hash : str ) -> dict :
1971
- return await self ._runtime_info_fetcher . execute (block_hash )
1979
+ return await self ._get_block_runtime_info (block_hash )
1972
1980
1973
1981
get_block_runtime_version = get_block_runtime_info
1974
1982
@@ -1979,8 +1987,9 @@ async def _get_block_runtime_info(self, block_hash: str) -> dict:
1979
1987
response = await self .rpc_request ("state_getRuntimeVersion" , [block_hash ])
1980
1988
return response .get ("result" )
1981
1989
1990
+ @cached_fetcher (max_size = 512 )
1982
1991
async def get_block_runtime_version_for (self , block_hash : str ):
1983
- return await self ._runtime_version_for_fetcher . execute (block_hash )
1992
+ return await self ._get_block_runtime_version_for (block_hash )
1984
1993
1985
1994
async def _get_block_runtime_version_for (self , block_hash : str ):
1986
1995
"""
@@ -2296,8 +2305,9 @@ async def rpc_request(
2296
2305
else :
2297
2306
raise SubstrateRequestException (result [payload_id ][0 ])
2298
2307
2308
+ @cached_fetcher (max_size = 512 )
2299
2309
async def get_block_hash (self , block_id : int ) -> str :
2300
- return await self ._block_hash_fetcher . execute (block_id )
2310
+ return await self ._get_block_hash (block_id )
2301
2311
2302
2312
async def _get_block_hash (self , block_id : int ) -> str :
2303
2313
return (await self .rpc_request ("chain_getBlockHash" , [block_id ]))["result" ]
0 commit comments