Skip to content

Only use Runtime objects in AsyncSubstrateInterface #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
610 changes: 361 additions & 249 deletions async_substrate_interface/async_substrate.py

Large diffs are not rendered by default.

264 changes: 148 additions & 116 deletions async_substrate_interface/sync_substrate.py

Large diffs are not rendered by default.

649 changes: 338 additions & 311 deletions async_substrate_interface/types.py

Large diffs are not rendered by default.

28 changes: 25 additions & 3 deletions async_substrate_interface/utils/decoding.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Union, TYPE_CHECKING

from bt_decode import AxonInfo, PrometheusInfo, decode_list
from scalecodec import ScaleBytes

from async_substrate_interface.utils import hex_to_bytes
from async_substrate_interface.types import ScaleObj
Expand Down Expand Up @@ -55,10 +56,16 @@ def _bt_decode_to_dict_or_list(obj) -> Union[dict, list[dict]]:
def _decode_scale_list_with_runtime(
type_strings: list[str],
scale_bytes_list: list[bytes],
runtime_registry,
runtime: "Runtime",
return_scale_obj: bool = False,
):
obj = decode_list(type_strings, runtime_registry, scale_bytes_list)
if runtime.metadata_v15 is not None:
obj = decode_list(type_strings, runtime.registry, scale_bytes_list)
else:
obj = [
legacy_scale_decode(x, y, runtime)
for (x, y) in zip(type_strings, scale_bytes_list)
]
if return_scale_obj:
return [ScaleObj(x) for x in obj]
else:
Expand Down Expand Up @@ -109,7 +116,7 @@ def concat_hash_len(key_hasher: str) -> int:
all_decoded = _decode_scale_list_with_runtime(
pre_decoded_key_types + pre_decoded_value_types,
pre_decoded_keys + pre_decoded_values,
runtime.registry,
runtime,
)
middl_index = len(all_decoded) // 2
decoded_keys = all_decoded[:middl_index]
Expand All @@ -132,3 +139,18 @@ def concat_hash_len(key_hasher: str) -> int:
item_value = dv
result.append([item_key, item_value])
return result


def legacy_scale_decode(
type_string: str, scale_bytes: Union[str, ScaleBytes], runtime: "Runtime"
):
if isinstance(scale_bytes, (str, bytes)):
scale_bytes = ScaleBytes(scale_bytes)

obj = runtime.runtime_config.create_scale_object(
type_string=type_string, data=scale_bytes, metadata=runtime.metadata
)

obj.decode(check_remaining=runtime.config.get("strict_scale_decode"))

return obj.value
2 changes: 2 additions & 0 deletions tests/helpers/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@
AURA_NODE_URL = (
environ.get("SUBSTRATE_AURA_NODE_URL") or "wss://acala-rpc-1.aca-api.network"
)

ARCHIVE_ENTRYPOINT = "wss://archive.chain.opentensor.ai:443"
38 changes: 34 additions & 4 deletions tests/unit_tests/asyncio_/test_substrate_interface.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import asyncio
from unittest.mock import AsyncMock, MagicMock
from unittest.mock import AsyncMock, MagicMock, ANY

import pytest
from websockets.exceptions import InvalidURI

from async_substrate_interface.async_substrate import AsyncSubstrateInterface
from async_substrate_interface.types import ScaleObj
from tests.helpers.settings import ARCHIVE_ENTRYPOINT


@pytest.mark.asyncio
Expand Down Expand Up @@ -64,7 +65,7 @@ async def test_runtime_call(monkeypatch):

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

# Check decode_scale called correctly
substrate.decode_scale.assert_called_once_with("scale_info::1", b"\x00")
substrate.decode_scale.assert_called_once_with(
"scale_info::1", b"\x00", runtime=ANY
)

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

# Check RPC request called for the state_call
substrate.rpc_request.assert_any_call(
"state_call", ["SubstrateApi_SubstrateMethod", "", None]
"state_call", ["SubstrateApi_SubstrateMethod", "", None], runtime=ANY
)


Expand All @@ -111,3 +114,30 @@ async def test_websocket_shutdown_timer():
await substrate.get_chain_head()
await asyncio.sleep(6) # same sleep time as before
assert substrate.ws._initialized is True # connection should still be open


@pytest.mark.asyncio
async def test_legacy_decoding():
# roughly 4000 blocks before metadata v15 was added
pre_metadata_v15_block = 3_010_611

async with AsyncSubstrateInterface(ARCHIVE_ENTRYPOINT) as substrate:
block_hash = await substrate.get_block_hash(pre_metadata_v15_block)
events = await substrate.get_events(block_hash)
assert isinstance(events, list)
Comment on lines +119 to +127
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd avoid do real calls within kinda unit tests bc it's already e2e testing actually

The same for another tests


query_map_result = await substrate.query_map(
module="SubtensorModule",
storage_function="NetworksAdded",
block_hash=block_hash,
)
async for key, value in query_map_result:
assert isinstance(key, int)
assert isinstance(value, ScaleObj)

timestamp = await substrate.query(
"Timestamp",
"Now",
block_hash=block_hash,
)
assert timestamp.value == 1716358476004
29 changes: 29 additions & 0 deletions tests/unit_tests/sync/test_substrate_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from async_substrate_interface.sync_substrate import SubstrateInterface
from async_substrate_interface.types import ScaleObj

from tests.helpers.settings import ARCHIVE_ENTRYPOINT


def test_runtime_call(monkeypatch):
substrate = SubstrateInterface("ws://localhost", _mock=True)
Expand Down Expand Up @@ -72,3 +74,30 @@ def test_runtime_call(monkeypatch):
substrate.rpc_request.assert_any_call(
"state_call", ["SubstrateApi_SubstrateMethod", "", None]
)
substrate.close()


def test_legacy_decoding():
# roughly 4000 blocks before metadata v15 was added
pre_metadata_v15_block = 3_010_611

with SubstrateInterface(ARCHIVE_ENTRYPOINT) as substrate:
block_hash = substrate.get_block_hash(pre_metadata_v15_block)
events = substrate.get_events(block_hash)
assert isinstance(events, list)

query_map_result = substrate.query_map(
module="SubtensorModule",
storage_function="NetworksAdded",
block_hash=block_hash,
)
for key, value in query_map_result:
assert isinstance(key, int)
assert isinstance(value, ScaleObj)

timestamp = substrate.query(
"Timestamp",
"Now",
block_hash=block_hash,
)
assert timestamp.value == 1716358476004
7 changes: 5 additions & 2 deletions tests/unit_tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ async def error_method(x):
@pytest.mark.asyncio
async def test_cached_fetcher_eviction():
"""Tests that LRU eviction works in CachedFetcher."""
mock_method = mock.AsyncMock(side_effect=lambda x: f"val_{x}")
fetcher = CachedFetcher(max_size=2, method=mock_method)

async def side_effect_method(x):
return f"val_{x}"

fetcher = CachedFetcher(max_size=2, method=side_effect_method)

# Fill cache
await fetcher("key1")
Expand Down
Loading