1
- import copy
2
1
import logging
3
2
from abc import ABC
4
3
from collections import defaultdict
20
19
21
20
22
21
class 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
+
23
35
blocks : dict [int , "Runtime" ]
24
36
block_hashes : dict [str , "Runtime" ]
25
37
versions : dict [int , "Runtime" ]
@@ -37,7 +49,10 @@ def add_item(
37
49
block : Optional [int ] = None ,
38
50
block_hash : Optional [str ] = None ,
39
51
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
+ """
41
56
self .last_used = runtime
42
57
if block is not None :
43
58
self .blocks [block ] = runtime
@@ -52,20 +67,35 @@ def retrieve(
52
67
block_hash : Optional [str ] = None ,
53
68
runtime_version : Optional [int ] = None ,
54
69
) -> 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
+ """
56
74
if block is not None :
57
75
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 :
59
80
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 :
61
85
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
65
90
66
91
67
92
class 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
69
99
transaction_version = None
70
100
cache_region = None
71
101
metadata = None
@@ -79,7 +109,7 @@ class Runtime:
79
109
80
110
def __init__ (
81
111
self ,
82
- chain ,
112
+ chain : str ,
83
113
runtime_config : RuntimeConfigurationObject ,
84
114
metadata ,
85
115
type_registry ,
@@ -102,6 +132,9 @@ def __init__(
102
132
self .load_registry_type_map ()
103
133
104
134
def load_runtime (self ):
135
+ """
136
+ Initial loading of the runtime's type registry information.
137
+ """
105
138
# Update type registry
106
139
self .reload_type_registry (use_remote_preset = False , auto_discover = True )
107
140
@@ -124,10 +157,6 @@ def load_runtime(self):
124
157
def implements_scaleinfo (self ) -> Optional [bool ]:
125
158
"""
126
159
Returns True if current runtime implements a `PortableRegistry` (`MetadataV14` and higher)
127
-
128
- Returns
129
- -------
130
- bool
131
160
"""
132
161
if self .metadata :
133
162
return self .metadata .portable_registry is not None
@@ -214,7 +243,10 @@ def apply_type_registry_presets(
214
243
# Load type registries in runtime configuration
215
244
self .runtime_config .update_type_registry (self .type_registry )
216
245
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
+ """
218
250
registry_type_map = {}
219
251
type_id_to_name = {}
220
252
types = json .loads (self .registry .registry )["types" ]
0 commit comments