Skip to content

Commit d0cea98

Browse files
committed
WIP check-in
1 parent e7053c5 commit d0cea98

File tree

5 files changed

+44
-22
lines changed

5 files changed

+44
-22
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,6 @@ def load_runtime(self, runtime):
966966
# Update type registry
967967
runtime.reload_type_registry(use_remote_preset=False, auto_discover=True)
968968

969-
runtime.runtime_config.set_active_spec_version_id(runtime.runtime_version)
970969
runtime.runtime_config.set_active_spec_version_id(runtime.runtime_version)
971970
if runtime.implements_scaleinfo:
972971
logger.debug("Adding PortableRegistry from metadata to type registry")
@@ -1032,7 +1031,7 @@ async def init_runtime(
10321031
if self.ss58_format is None:
10331032
# Check and apply runtime constants
10341033
ss58_prefix_constant = await self.get_constant(
1035-
"System", "SS58Prefix", block_hash=block_hash
1034+
"System", "SS58Prefix", block_hash=block_hash, runtime=runtime
10361035
)
10371036

10381037
if ss58_prefix_constant:
@@ -1458,8 +1457,8 @@ async def decode_block(block_data, block_data_hash=None) -> dict[str, Any]:
14581457
try:
14591458
extrinsic_decoder = extrinsic_cls(
14601459
data=ScaleBytes(extrinsic_data),
1461-
metadata=self.runtime.metadata,
1462-
runtime_config=self.runtime_config,
1460+
metadata=runtime.metadata,
1461+
runtime_config=runtime.runtime_config,
14631462
)
14641463
extrinsic_decoder.decode(check_remaining=True)
14651464
block_data["extrinsics"][idx] = extrinsic_decoder
@@ -2299,17 +2298,24 @@ async def rpc_request(
22992298
params + [block_hash] if block_hash else params,
23002299
)
23012300
]
2302-
result = await self._make_rpc_request(payloads, result_handler=result_handler)
2301+
result = await self._make_rpc_request(
2302+
payloads, result_handler=result_handler, runtime=runtime
2303+
)
23032304
if "error" in result[payload_id][0]:
23042305
if "Failed to get runtime version" in (
23052306
err_msg := result[payload_id][0]["error"]["message"]
23062307
):
23072308
logger.warning(
23082309
"Failed to get runtime. Re-fetching from chain, and retrying."
23092310
)
2310-
await self.init_runtime(block_hash=block_hash)
2311+
runtime = await self.init_runtime(block_hash=block_hash)
23112312
return await self.rpc_request(
2312-
method, params, result_handler, block_hash, reuse_block_hash
2313+
method,
2314+
params,
2315+
result_handler,
2316+
block_hash,
2317+
reuse_block_hash,
2318+
runtime=runtime,
23132319
)
23142320
elif (
23152321
"Client error: Api called for an unknown Block: State already discarded"
@@ -3036,7 +3042,13 @@ async def get_metadata_constants(self, block_hash=None) -> list[dict]:
30363042

30373043
return constant_list
30383044

3039-
async def get_metadata_constant(self, module_name, constant_name, block_hash=None):
3045+
async def get_metadata_constant(
3046+
self,
3047+
module_name,
3048+
constant_name,
3049+
block_hash=None,
3050+
runtime: Optional[Runtime] = None,
3051+
):
30403052
"""
30413053
Retrieves the details of a constant for given module name, call function name and block_hash
30423054
(or chaintip if block_hash is omitted)
@@ -3045,13 +3057,15 @@ async def get_metadata_constant(self, module_name, constant_name, block_hash=Non
30453057
module_name: name of the module you are querying
30463058
constant_name: name of the constant you are querying
30473059
block_hash: hash of the block at which to make the runtime API call
3060+
runtime: Runtime whose metadata you are querying.
30483061
30493062
Returns:
30503063
MetadataModuleConstants
30513064
"""
3052-
await self.init_runtime(block_hash=block_hash)
3065+
if not runtime:
3066+
runtime = await self.init_runtime(block_hash=block_hash)
30533067

3054-
for module in self.runtime.metadata.pallets:
3068+
for module in runtime.metadata.pallets:
30553069
if module_name == module.name and module.constants:
30563070
for constant in module.constants:
30573071
if constant_name == constant.value["name"]:
@@ -3063,6 +3077,7 @@ async def get_constant(
30633077
constant_name: str,
30643078
block_hash: Optional[str] = None,
30653079
reuse_block_hash: bool = False,
3080+
runtime: Optional[Runtime] = None,
30663081
) -> Optional[ScaleObj]:
30673082
"""
30683083
Returns the decoded `ScaleType` object of the constant for given module name, call function name and block_hash
@@ -3073,18 +3088,22 @@ async def get_constant(
30733088
constant_name: Name of the constant to query
30743089
block_hash: Hash of the block at which to make the runtime API call
30753090
reuse_block_hash: Reuse last-used block hash if set to true
3091+
runtime: Runtime to use for querying the constant
30763092
30773093
Returns:
30783094
ScaleType from the runtime call
30793095
"""
30803096
block_hash = await self._get_current_block_hash(block_hash, reuse_block_hash)
30813097
constant = await self.get_metadata_constant(
3082-
module_name, constant_name, block_hash=block_hash
3098+
module_name, constant_name, block_hash=block_hash, runtime=runtime
30833099
)
30843100
if constant:
30853101
# Decode to ScaleType
30863102
return await self.decode_scale(
3087-
constant.type, bytes(constant.constant_value), return_scale_obj=True
3103+
constant.type,
3104+
bytes(constant.constant_value),
3105+
return_scale_obj=True,
3106+
runtime=runtime,
30883107
)
30893108
else:
30903109
return None

async_substrate_interface/sync_substrate.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ def __init__(
506506
_log_raw_websockets: whether to log raw websocket requests during RPC requests
507507
508508
"""
509+
super().__init__(type_registry, type_registry_preset, use_remote_preset)
509510
self.max_retries = max_retries
510511
self.retry_timeout = retry_timeout
511512
self.chain_endpoint = url
@@ -526,9 +527,6 @@ def __init__(
526527
ss58_format=self.ss58_format, implements_scale_info=True
527528
)
528529
self.metadata_version_hex = "0x0f000000" # v15
529-
self.reload_type_registry()
530-
self.registry_type_map = {}
531-
self.type_id_to_name = {}
532530
self._mock = _mock
533531
self.log_raw_websockets = _log_raw_websockets
534532
if not _mock:

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][self._cache_key_index]
231+
return bound.arguments[key_name]
232232

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

tests/unit_tests/asyncio_/test_substrate_interface.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import asyncio
2-
from unittest.mock import AsyncMock, MagicMock
2+
from unittest.mock import AsyncMock, MagicMock, ANY
33

44
import pytest
55
from websockets.exceptions import InvalidURI
@@ -64,7 +64,7 @@ async def test_runtime_call(monkeypatch):
6464

6565
# Patch RPC request with correct behavior
6666
substrate.rpc_request = AsyncMock(
67-
side_effect=lambda method, params: {
67+
side_effect=lambda method, params, runtime: {
6868
"result": "0x00" if method == "state_call" else {"parentHash": "0xDEADBEEF"}
6969
}
7070
)
@@ -83,14 +83,16 @@ async def test_runtime_call(monkeypatch):
8383
assert result.value == "decoded_result"
8484

8585
# Check decode_scale called correctly
86-
substrate.decode_scale.assert_called_once_with("scale_info::1", b"\x00")
86+
substrate.decode_scale.assert_called_once_with(
87+
"scale_info::1", b"\x00", runtime=ANY
88+
)
8789

8890
# encode_scale should not be called since no inputs
8991
substrate.encode_scale.assert_not_called()
9092

9193
# Check RPC request called for the state_call
9294
substrate.rpc_request.assert_any_call(
93-
"state_call", ["SubstrateApi_SubstrateMethod", "", None]
95+
"state_call", ["SubstrateApi_SubstrateMethod", "", None], runtime=ANY
9496
)
9597

9698

tests/unit_tests/test_cache.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,11 @@ async def error_method(x):
7171
@pytest.mark.asyncio
7272
async def test_cached_fetcher_eviction():
7373
"""Tests that LRU eviction works in CachedFetcher."""
74-
mock_method = mock.AsyncMock(side_effect=lambda x: f"val_{x}")
75-
fetcher = CachedFetcher(max_size=2, method=mock_method)
74+
75+
async def side_effect_method(x):
76+
return f"val_{x}"
77+
78+
fetcher = CachedFetcher(max_size=2, method=side_effect_method)
7679

7780
# Fill cache
7881
await fetcher("key1")

0 commit comments

Comments
 (0)