Skip to content

Commit e7053c5

Browse files
committed
WIP check-in
1 parent 9367d83 commit e7053c5

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,7 @@ def __init__(
755755
ws_shutdown_timer: how long after the last connection your websocket should close
756756
757757
"""
758+
super().__init__(type_registry, type_registry_preset, use_remote_preset)
758759
self.max_retries = max_retries
759760
self.retry_timeout = retry_timeout
760761
self.chain_endpoint = url
@@ -923,6 +924,7 @@ async def decode_scale(
923924
_attempt=1,
924925
_retries=3,
925926
return_scale_obj: bool = False,
927+
block_hash: Optional[str] = None,
926928
runtime: Optional[Runtime] = None,
927929
) -> Union[ScaleObj, Any]:
928930
"""
@@ -936,8 +938,9 @@ async def decode_scale(
936938
_attempt: the number of attempts to pull the registry before timing out
937939
_retries: the number of retries to pull the registry before timing out
938940
return_scale_obj: Whether to return the decoded value wrapped in a SCALE-object-like wrapper, or raw.
939-
runtime: Optional Runtime object whose registry to use for decoding. If not specified, the currently-loaded
940-
`self.runtime` will be used.
941+
block_hash: Hash of the block where the desired runtime is located. Ignored if supplying `runtime`
942+
runtime: Optional Runtime object whose registry to use for decoding. If not specified, runtime will be
943+
loaded based on the block hash specified (or latest block if no block_hash is specified)
941944
942945
Returns:
943946
Decoded object
@@ -949,8 +952,8 @@ async def decode_scale(
949952
return ss58_encode(scale_bytes, SS58_FORMAT)
950953
else:
951954
if not runtime:
952-
await self._wait_for_registry(_attempt, _retries)
953-
runtime_registry = self.runtime.registry
955+
runtime = await self.init_runtime(block_hash=block_hash)
956+
runtime_registry = runtime.registry
954957
else:
955958
runtime_registry = runtime.registry
956959
obj = decode_by_type_string(type_string, runtime_registry, scale_bytes)
@@ -2949,13 +2952,17 @@ async def runtime_call(
29492952

29502953
# RPC request
29512954
result_data = await self.rpc_request(
2952-
"state_call", [f"{api}_{method}", param_data.hex(), block_hash]
2955+
"state_call",
2956+
[f"{api}_{method}", param_data.hex(), block_hash],
2957+
runtime=runtime,
29532958
)
29542959
output_type_string = f"scale_info::{runtime_call_def['output']}"
29552960

29562961
# Decode result
29572962
result_bytes = hex_to_bytes(result_data["result"])
2958-
result_obj = ScaleObj(await self.decode_scale(output_type_string, result_bytes))
2963+
result_obj = ScaleObj(
2964+
await self.decode_scale(output_type_string, result_bytes, runtime=runtime)
2965+
)
29592966

29602967
return result_obj
29612968

@@ -3317,7 +3324,7 @@ async def query_map(
33173324
self.last_block_hash = block_hash
33183325
runtime = await self.init_runtime(block_hash=block_hash)
33193326

3320-
metadata_pallet = self.runtime.metadata.get_metadata_pallet(module)
3327+
metadata_pallet = runtime.metadata.get_metadata_pallet(module)
33213328
if not metadata_pallet:
33223329
raise ValueError(f'Pallet "{module}" not found')
33233330
storage_item = metadata_pallet.get_storage_function(storage_function)
@@ -3344,8 +3351,8 @@ async def query_map(
33443351
module,
33453352
storage_item.value["name"],
33463353
params,
3347-
runtime_config=self.runtime_config,
3348-
metadata=self.runtime.metadata,
3354+
runtime_config=runtime.runtime_config,
3355+
metadata=runtime.metadata,
33493356
)
33503357
prefix = storage_key.to_hex()
33513358

@@ -3360,6 +3367,7 @@ async def query_map(
33603367
response = await self.rpc_request(
33613368
method="state_getKeysPaged",
33623369
params=[prefix, page_size, start_key, block_hash],
3370+
runtime=runtime,
33633371
)
33643372

33653373
if "error" in response:
@@ -3375,7 +3383,9 @@ async def query_map(
33753383

33763384
# Retrieve corresponding value
33773385
response = await self.rpc_request(
3378-
method="state_queryStorageAt", params=[result_keys, block_hash]
3386+
method="state_queryStorageAt",
3387+
params=[result_keys, block_hash],
3388+
runtime=runtime,
33793389
)
33803390

33813391
if "error" in response:

async_substrate_interface/types.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,37 @@ class SubstrateMixin(ABC):
485485
type_registry: Optional[dict]
486486
ss58_format: Optional[int]
487487
ws_max_size = 2**32
488-
runtime: Runtime = None
488+
runtime: Runtime = None # TODO remove
489+
490+
def __init__(
491+
self,
492+
type_registry: Optional[dict] = None,
493+
type_registry_preset: Optional[str] = None,
494+
use_remote_preset: bool = False,
495+
):
496+
# We load a very basic RuntimeConfigurationObject that is only used for the initial metadata decoding
497+
self.runtime_config = RuntimeConfigurationObject()
498+
self.runtime_config.update_type_registry(load_type_registry_preset(name="core"))
499+
if type_registry_preset is not None:
500+
type_registry_preset_dict = load_type_registry_preset(
501+
name=type_registry_preset, use_remote_preset=use_remote_preset
502+
)
503+
if not type_registry_preset_dict:
504+
raise ValueError(
505+
f"Type registry preset '{type_registry_preset}' not found"
506+
)
507+
else:
508+
type_registry_preset_dict = None
509+
510+
if type_registry_preset_dict:
511+
self.runtime_config.update_type_registry(
512+
load_type_registry_preset("legacy", use_remote_preset=use_remote_preset)
513+
)
514+
if type_registry_preset != "legacy":
515+
self.runtime_config.update_type_registry(type_registry_preset_dict)
516+
if type_registry:
517+
# Load type registries in runtime configuration
518+
self.runtime_config.update_type_registry(type_registry)
489519

490520
@property
491521
def chain(self):

async_substrate_interface/utils/cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def make_cache_key(self, args: tuple, kwargs: dict) -> Hashable:
228228

229229
if self._cache_key_index is not None:
230230
key_name = list(bound.arguments)[self._cache_key_index]
231-
return bound.arguments[key_name]
231+
return bound.arguments[key_name][self._cache_key_index]
232232

233233
return (tuple(bound.arguments.items()),)
234234

0 commit comments

Comments
 (0)