1- import copy
21import logging
32from abc import ABC
43from collections import defaultdict
2019
2120
2221class RuntimeCache :
22+ """
23+ Cache that holds all the Runtime objects used by AsyncSubstrateInterface and SubstrateInterface. See the docstring
24+ for Runtime for more information about Runtime objects specifically.
25+
26+ For SubstrateInterface (sync), this serves purely as a quick way of retrieving a previously loaded Runtime. For
27+ AsyncSubstrateInterface, this is very important, as, while it does the same as for SubstrateInterface, it also
28+ serves as an easy way for a user to fetch a Runtime whose registry or metadata they wish to utilize in some way.
29+
30+ The `last_used` attribute is always updated with the most recently inserted or retrieved Runtime object. If you're
31+ querying numerous blocks at once with different runtimes, and you wish to use the metadata or registry directly, it
32+ is important you are utilizing the correct version.
33+ """
34+
2335 blocks : dict [int , "Runtime" ]
2436 block_hashes : dict [str , "Runtime" ]
2537 versions : dict [int , "Runtime" ]
@@ -37,7 +49,10 @@ def add_item(
3749 block : Optional [int ] = None ,
3850 block_hash : Optional [str ] = None ,
3951 runtime_version : Optional [int ] = None ,
40- ):
52+ ) -> None :
53+ """
54+ Adds a Runtime object to the cache mapped to its version, block number, and/or block hash.
55+ """
4156 self .last_used = runtime
4257 if block is not None :
4358 self .blocks [block ] = runtime
@@ -52,20 +67,35 @@ def retrieve(
5267 block_hash : Optional [str ] = None ,
5368 runtime_version : Optional [int ] = None ,
5469 ) -> Optional ["Runtime" ]:
55- runtime = None
70+ """
71+ Retrieves a Runtime object from the cache, using the key of its block number, block hash, or runtime version.
72+ Retrieval happens in this order. If no Runtime is found mapped to any of your supplied keys, returns `None`.
73+ """
5674 if block is not None :
5775 runtime = self .blocks .get (block )
58- elif block_hash is not None :
76+ if runtime is not None :
77+ self .last_used = runtime
78+ return runtime
79+ if block_hash is not None :
5980 runtime = self .block_hashes .get (block_hash )
60- elif runtime_version is not None :
81+ if runtime is not None :
82+ self .last_used = runtime
83+ return runtime
84+ if runtime_version is not None :
6185 runtime = self .versions .get (runtime_version )
62- if runtime is not None :
63- self .last_used = runtime
64- return runtime
86+ if runtime is not None :
87+ self .last_used = runtime
88+ return runtime
89+ return None
6590
6691
6792class Runtime :
68- runtime_version = None
93+ """
94+ The Runtime object holds the necessary metadata and registry information required to do necessary scale encoding and
95+ decoding. Currently only Metadata V15 is supported for decoding, though we plan to release legacy decoding options.
96+ """
97+
98+ runtime_version : Optional [int ] = None
6999 transaction_version = None
70100 cache_region = None
71101 metadata = None
@@ -79,7 +109,7 @@ class Runtime:
79109
80110 def __init__ (
81111 self ,
82- chain ,
112+ chain : str ,
83113 runtime_config : RuntimeConfigurationObject ,
84114 metadata ,
85115 type_registry ,
@@ -102,6 +132,9 @@ def __init__(
102132 self .load_registry_type_map ()
103133
104134 def load_runtime (self ):
135+ """
136+ Initial loading of the runtime's type registry information.
137+ """
105138 # Update type registry
106139 self .reload_type_registry (use_remote_preset = False , auto_discover = True )
107140
@@ -124,10 +157,6 @@ def load_runtime(self):
124157 def implements_scaleinfo (self ) -> Optional [bool ]:
125158 """
126159 Returns True if current runtime implements a `PortableRegistry` (`MetadataV14` and higher)
127-
128- Returns
129- -------
130- bool
131160 """
132161 if self .metadata :
133162 return self .metadata .portable_registry is not None
@@ -214,7 +243,10 @@ def apply_type_registry_presets(
214243 # Load type registries in runtime configuration
215244 self .runtime_config .update_type_registry (self .type_registry )
216245
217- def load_registry_type_map (self ):
246+ def load_registry_type_map (self ) -> None :
247+ """
248+ Loads the runtime's type mapping according to registry
249+ """
218250 registry_type_map = {}
219251 type_id_to_name = {}
220252 types = json .loads (self .registry .registry )["types" ]
0 commit comments