7
7
import asyncio
8
8
import inspect
9
9
import logging
10
+ import os
10
11
import ssl
11
12
import warnings
12
13
from contextlib import suppress
42
43
SubstrateRequestException ,
43
44
ExtrinsicNotFound ,
44
45
BlockNotFound ,
45
- MaxRetriesExceeded ,
46
46
StateDiscardedError ,
47
47
)
48
48
from async_substrate_interface .protocols import Keypair
81
81
logger = logging .getLogger ("async_substrate_interface" )
82
82
raw_websocket_logger = logging .getLogger ("raw_websocket" )
83
83
84
+ # env vars dictating the cache size of the cached methods
85
+ SUBSTRATE_CACHE_METHOD_SIZE = int (os .getenv ("SUBSTRATE_CACHE_METHOD_SIZE" , "512" ))
86
+ SUBSTRATE_RUNTIME_CACHE_SIZE = int (os .getenv ("SUBSTRATE_RUNTIME_CACHE_SIZE" , "16" ))
87
+
84
88
85
89
class AsyncExtrinsicReceipt :
86
90
"""
@@ -1178,7 +1182,7 @@ async def init_runtime(
1178
1182
else :
1179
1183
return await self .get_runtime_for_version (runtime_version , block_hash )
1180
1184
1181
- @cached_fetcher (max_size = 16 , cache_key_index = 0 )
1185
+ @cached_fetcher (max_size = SUBSTRATE_RUNTIME_CACHE_SIZE , cache_key_index = 0 )
1182
1186
async def get_runtime_for_version (
1183
1187
self , runtime_version : int , block_hash : Optional [str ] = None
1184
1188
) -> Runtime :
@@ -2111,7 +2115,7 @@ async def get_metadata(self, block_hash=None) -> MetadataV15:
2111
2115
2112
2116
return runtime .metadata_v15
2113
2117
2114
- @cached_fetcher (max_size = 512 )
2118
+ @cached_fetcher (max_size = SUBSTRATE_CACHE_METHOD_SIZE )
2115
2119
async def get_parent_block_hash (self , block_hash ) -> str :
2116
2120
"""
2117
2121
Retrieves the block hash of the parent of the given block hash
@@ -2166,7 +2170,7 @@ async def get_storage_by_key(self, block_hash: str, storage_key: str) -> Any:
2166
2170
"Unknown error occurred during retrieval of events"
2167
2171
)
2168
2172
2169
- @cached_fetcher (max_size = 16 )
2173
+ @cached_fetcher (max_size = SUBSTRATE_RUNTIME_CACHE_SIZE )
2170
2174
async def get_block_runtime_info (self , block_hash : str ) -> dict :
2171
2175
"""
2172
2176
Retrieve the runtime info of given block_hash
@@ -2179,7 +2183,7 @@ async def _get_block_runtime_info(self, block_hash: str) -> dict:
2179
2183
response = await self .rpc_request ("state_getRuntimeVersion" , [block_hash ])
2180
2184
return response .get ("result" )
2181
2185
2182
- @cached_fetcher (max_size = 512 )
2186
+ @cached_fetcher (max_size = SUBSTRATE_CACHE_METHOD_SIZE )
2183
2187
async def get_block_runtime_version_for (self , block_hash : str ):
2184
2188
"""
2185
2189
Retrieve the runtime version of the parent of a given block_hash
@@ -2494,7 +2498,7 @@ async def rpc_request(
2494
2498
else :
2495
2499
raise SubstrateRequestException (result [payload_id ][0 ])
2496
2500
2497
- @cached_fetcher (max_size = 512 )
2501
+ @cached_fetcher (max_size = SUBSTRATE_CACHE_METHOD_SIZE )
2498
2502
async def get_block_hash (self , block_id : int ) -> str :
2499
2503
"""
2500
2504
Retrieves the hash of the specified block number
@@ -4022,19 +4026,19 @@ class DiskCachedAsyncSubstrateInterface(AsyncSubstrateInterface):
4022
4026
Experimental new class that uses disk-caching in addition to memory-caching for the cached methods
4023
4027
"""
4024
4028
4025
- @async_sql_lru_cache (maxsize = 512 )
4029
+ @async_sql_lru_cache (maxsize = SUBSTRATE_CACHE_METHOD_SIZE )
4026
4030
async def get_parent_block_hash (self , block_hash ):
4027
4031
return await self ._get_parent_block_hash (block_hash )
4028
4032
4029
- @async_sql_lru_cache (maxsize = 16 )
4033
+ @async_sql_lru_cache (maxsize = SUBSTRATE_RUNTIME_CACHE_SIZE )
4030
4034
async def get_block_runtime_info (self , block_hash : str ) -> dict :
4031
4035
return await self ._get_block_runtime_info (block_hash )
4032
4036
4033
- @async_sql_lru_cache (maxsize = 512 )
4037
+ @async_sql_lru_cache (maxsize = SUBSTRATE_CACHE_METHOD_SIZE )
4034
4038
async def get_block_runtime_version_for (self , block_hash : str ):
4035
4039
return await self ._get_block_runtime_version_for (block_hash )
4036
4040
4037
- @async_sql_lru_cache (maxsize = 512 )
4041
+ @async_sql_lru_cache (maxsize = SUBSTRATE_CACHE_METHOD_SIZE )
4038
4042
async def get_block_hash (self , block_id : int ) -> str :
4039
4043
return await self ._get_block_hash (block_id )
4040
4044
0 commit comments