77import asyncio
88import inspect
99import logging
10+ import os
1011import ssl
1112import warnings
1213from contextlib import suppress
4243 SubstrateRequestException ,
4344 ExtrinsicNotFound ,
4445 BlockNotFound ,
45- MaxRetriesExceeded ,
4646 StateDiscardedError ,
4747)
4848from async_substrate_interface .protocols import Keypair
8181logger = logging .getLogger ("async_substrate_interface" )
8282raw_websocket_logger = logging .getLogger ("raw_websocket" )
8383
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+
8488
8589class AsyncExtrinsicReceipt :
8690 """
@@ -1178,7 +1182,7 @@ async def init_runtime(
11781182 else :
11791183 return await self .get_runtime_for_version (runtime_version , block_hash )
11801184
1181- @cached_fetcher (max_size = 16 , cache_key_index = 0 )
1185+ @cached_fetcher (max_size = SUBSTRATE_RUNTIME_CACHE_SIZE , cache_key_index = 0 )
11821186 async def get_runtime_for_version (
11831187 self , runtime_version : int , block_hash : Optional [str ] = None
11841188 ) -> Runtime :
@@ -2111,7 +2115,7 @@ async def get_metadata(self, block_hash=None) -> MetadataV15:
21112115
21122116 return runtime .metadata_v15
21132117
2114- @cached_fetcher (max_size = 512 )
2118+ @cached_fetcher (max_size = SUBSTRATE_CACHE_METHOD_SIZE )
21152119 async def get_parent_block_hash (self , block_hash ) -> str :
21162120 """
21172121 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:
21662170 "Unknown error occurred during retrieval of events"
21672171 )
21682172
2169- @cached_fetcher (max_size = 16 )
2173+ @cached_fetcher (max_size = SUBSTRATE_RUNTIME_CACHE_SIZE )
21702174 async def get_block_runtime_info (self , block_hash : str ) -> dict :
21712175 """
21722176 Retrieve the runtime info of given block_hash
@@ -2179,7 +2183,7 @@ async def _get_block_runtime_info(self, block_hash: str) -> dict:
21792183 response = await self .rpc_request ("state_getRuntimeVersion" , [block_hash ])
21802184 return response .get ("result" )
21812185
2182- @cached_fetcher (max_size = 512 )
2186+ @cached_fetcher (max_size = SUBSTRATE_CACHE_METHOD_SIZE )
21832187 async def get_block_runtime_version_for (self , block_hash : str ):
21842188 """
21852189 Retrieve the runtime version of the parent of a given block_hash
@@ -2494,7 +2498,7 @@ async def rpc_request(
24942498 else :
24952499 raise SubstrateRequestException (result [payload_id ][0 ])
24962500
2497- @cached_fetcher (max_size = 512 )
2501+ @cached_fetcher (max_size = SUBSTRATE_CACHE_METHOD_SIZE )
24982502 async def get_block_hash (self , block_id : int ) -> str :
24992503 """
25002504 Retrieves the hash of the specified block number
@@ -4022,19 +4026,19 @@ class DiskCachedAsyncSubstrateInterface(AsyncSubstrateInterface):
40224026 Experimental new class that uses disk-caching in addition to memory-caching for the cached methods
40234027 """
40244028
4025- @async_sql_lru_cache (maxsize = 512 )
4029+ @async_sql_lru_cache (maxsize = SUBSTRATE_CACHE_METHOD_SIZE )
40264030 async def get_parent_block_hash (self , block_hash ):
40274031 return await self ._get_parent_block_hash (block_hash )
40284032
4029- @async_sql_lru_cache (maxsize = 16 )
4033+ @async_sql_lru_cache (maxsize = SUBSTRATE_RUNTIME_CACHE_SIZE )
40304034 async def get_block_runtime_info (self , block_hash : str ) -> dict :
40314035 return await self ._get_block_runtime_info (block_hash )
40324036
4033- @async_sql_lru_cache (maxsize = 512 )
4037+ @async_sql_lru_cache (maxsize = SUBSTRATE_CACHE_METHOD_SIZE )
40344038 async def get_block_runtime_version_for (self , block_hash : str ):
40354039 return await self ._get_block_runtime_version_for (block_hash )
40364040
4037- @async_sql_lru_cache (maxsize = 512 )
4041+ @async_sql_lru_cache (maxsize = SUBSTRATE_CACHE_METHOD_SIZE )
40384042 async def get_block_hash (self , block_id : int ) -> str :
40394043 return await self ._get_block_hash (block_id )
40404044
0 commit comments