Skip to content

Commit 58c63d4

Browse files
committed
Add docs
1 parent 86d9807 commit 58c63d4

File tree

3 files changed

+51
-25
lines changed

3 files changed

+51
-25
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -843,11 +843,8 @@ def metadata(self):
843843
@property
844844
def implements_scaleinfo(self) -> Optional[bool]:
845845
"""
846-
Returns True if current runtime implementation a `PortableRegistry` (`MetadataV14` and higher)
847-
848-
Returns
849-
-------
850-
bool
846+
Returns True if most-recently-used runtime implements a `PortableRegistry` (`MetadataV14` and higher). Returns
847+
`None` if no runtime has been loaded.
851848
"""
852849
runtime = self.runtime_cache.last_used
853850
if runtime is not None:

async_substrate_interface/sync_substrate.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,11 +579,8 @@ def metadata(self):
579579
@property
580580
def implements_scaleinfo(self) -> Optional[bool]:
581581
"""
582-
Returns True if current runtime implementation a `PortableRegistry` (`MetadataV14` and higher)
583-
584-
Returns
585-
-------
586-
bool
582+
Returns True if current runtime implements a `PortableRegistry` (`MetadataV14` and higher). Returns `None` if
583+
no currently loaded runtime.
587584
"""
588585
if self.runtime and self.runtime.metadata:
589586
return self.runtime.metadata.portable_registry is not None

async_substrate_interface/types.py

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import copy
21
import logging
32
from abc import ABC
43
from collections import defaultdict
@@ -20,6 +19,19 @@
2019

2120

2221
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+
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

6792
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
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

Comments
 (0)