Skip to content

Commit c4fcaca

Browse files
committed
Cleaner runtime cache update
1 parent ea08a69 commit c4fcaca

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

async_substrate_interface/types.py

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from abc import ABC
33
from collections import defaultdict, deque
44
from collections.abc import Iterable
5+
from contextlib import suppress
56
from dataclasses import dataclass
67
from datetime import datetime
78
from typing import Optional, Union, Any
@@ -35,8 +36,8 @@ class RuntimeCache:
3536
is important you are utilizing the correct version.
3637
"""
3738

38-
blocks: dict[int, "Runtime"]
39-
block_hashes: dict[str, "Runtime"]
39+
blocks: dict[int, str]
40+
block_hashes: dict[str, int]
4041
versions: dict[int, "Runtime"]
4142
last_used: Optional["Runtime"]
4243

@@ -57,10 +58,10 @@ def add_item(
5758
Adds a Runtime object to the cache mapped to its version, block number, and/or block hash.
5859
"""
5960
self.last_used = runtime
60-
if block is not None:
61-
self.blocks[block] = runtime
62-
if block_hash is not None:
63-
self.block_hashes[block_hash] = runtime
61+
if block is not None and block_hash is not None:
62+
self.blocks[block] = block_hash
63+
if block_hash is not None and runtime_version is not None:
64+
self.block_hashes[block_hash] = runtime_version
6465
if runtime_version is not None:
6566
self.versions[runtime_version] = runtime
6667

@@ -74,33 +75,29 @@ def retrieve(
7475
Retrieves a Runtime object from the cache, using the key of its block number, block hash, or runtime version.
7576
Retrieval happens in this order. If no Runtime is found mapped to any of your supplied keys, returns `None`.
7677
"""
78+
runtime = None
7779
if block is not None:
78-
runtime = self.blocks.get(block)
79-
if runtime is not None:
80-
if block_hash is not None:
81-
# if lookup occurs for block_hash and block, but only block matches, also map to block_hash
82-
self.add_item(runtime, block_hash=block_hash)
80+
if block_hash is not None:
81+
self.blocks[block] = block_hash
82+
if runtime_version is not None:
83+
self.block_hashes[block_hash] = runtime_version
84+
with suppress(KeyError):
85+
runtime = self.versions[self.block_hashes[self.blocks[block]]]
8386
self.last_used = runtime
8487
return runtime
8588
if block_hash is not None:
86-
runtime = self.block_hashes.get(block_hash)
87-
if runtime is not None:
88-
if block is not None:
89-
# if lookup occurs for block_hash and block, but only block_hash matches, also map to block
90-
self.add_item(runtime, block=block)
89+
if runtime_version is not None:
90+
self.block_hashes[block_hash] = runtime_version
91+
with suppress(KeyError):
92+
runtime = self.versions[self.block_hashes[block_hash]]
9193
self.last_used = runtime
9294
return runtime
9395
if runtime_version is not None:
94-
runtime = self.versions.get(runtime_version)
95-
if runtime is not None:
96-
# if runtime_version matches, also map to block and block_hash (if supplied)
97-
if block is not None:
98-
self.add_item(runtime, block=block)
99-
if block_hash is not None:
100-
self.add_item(runtime, block_hash=block_hash)
96+
with suppress(KeyError):
97+
runtime = self.versions[runtime_version]
10198
self.last_used = runtime
10299
return runtime
103-
return None
100+
return runtime
104101

105102
async def load_from_disk(self, chain_endpoint: str):
106103
db = AsyncSqliteDB(chain_endpoint=chain_endpoint)

0 commit comments

Comments
 (0)