Skip to content

Commit 1aa6590

Browse files
committed
Fix (Async)SubstrateInterface.metadata
1 parent 04574d7 commit 1aa6590

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,23 @@ async def initialize(self):
823823
async def __aexit__(self, exc_type, exc_val, exc_tb):
824824
pass
825825

826+
@property
827+
def metadata(self):
828+
warnings.warn(
829+
"Calling AsyncSubstrateInterface.metadata is deprecated, as metadata is runtime-dependent, and it"
830+
"can be unclear which for runtime you seek the metadata. You should instead use the specific runtime's "
831+
"metadata. For now, the most recently used runtime will be given.",
832+
category=DeprecationWarning,
833+
)
834+
runtime = self.runtime_cache.last_used
835+
if not runtime or runtime.metadata is None:
836+
raise AttributeError(
837+
"Metadata not found. This generally indicates that the AsyncSubstrateInterface object "
838+
"is not properly async initialized."
839+
)
840+
else:
841+
return runtime.metadata
842+
826843
@property
827844
async def properties(self):
828845
if self._properties is None:

async_substrate_interface/sync_substrate.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,16 @@ def initialize(self):
566566
def __exit__(self, exc_type, exc_val, exc_tb):
567567
self.ws.close()
568568

569+
@property
570+
def metadata(self):
571+
if not self.runtime or self.runtime.metadata is None:
572+
raise AttributeError(
573+
"Metadata not found. This generally indicates that the AsyncSubstrateInterface object "
574+
"is not properly async initialized."
575+
)
576+
else:
577+
return self.runtime.metadata
578+
569579
@property
570580
def properties(self):
571581
if self._properties is None:

async_substrate_interface/types.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ class RuntimeCache:
2323
blocks: dict[int, "Runtime"]
2424
block_hashes: dict[str, "Runtime"]
2525
versions: dict[int, "Runtime"]
26+
last_used: Optional["Runtime"]
2627

2728
def __init__(self):
2829
self.blocks = {}
2930
self.block_hashes = {}
3031
self.versions = {}
32+
self.last_used = None
3133

3234
def add_item(
3335
self,
@@ -36,6 +38,7 @@ def add_item(
3638
block_hash: Optional[str] = None,
3739
runtime_version: Optional[int] = None,
3840
):
41+
self.last_used = runtime
3942
if block is not None:
4043
self.blocks[block] = runtime
4144
if block_hash is not None:
@@ -49,14 +52,16 @@ def retrieve(
4952
block_hash: Optional[str] = None,
5053
runtime_version: Optional[int] = None,
5154
) -> Optional["Runtime"]:
55+
runtime = None
5256
if block is not None:
53-
return self.blocks.get(block)
57+
runtime = self.blocks.get(block)
5458
elif block_hash is not None:
55-
return self.block_hashes.get(block_hash)
59+
runtime = self.block_hashes.get(block_hash)
5660
elif runtime_version is not None:
57-
return self.versions.get(runtime_version)
58-
else:
59-
return None
61+
runtime = self.versions.get(runtime_version)
62+
if runtime is not None:
63+
self.last_used = runtime
64+
return runtime
6065

6166

6267
class Runtime:
@@ -560,16 +565,6 @@ def chain(self):
560565
"""
561566
return self._chain
562567

563-
@property
564-
def metadata(self):
565-
if not self.runtime or self.runtime.metadata is None:
566-
raise AttributeError(
567-
"Metadata not found. This generally indicates that the AsyncSubstrateInterface object "
568-
"is not properly async initialized."
569-
)
570-
else:
571-
return self.runtime.metadata
572-
573568
@property
574569
def implements_scaleinfo(self) -> Optional[bool]:
575570
"""

0 commit comments

Comments
 (0)