Skip to content

Commit c1856e2

Browse files
committed
Tests
1 parent a335d4b commit c1856e2

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

async_substrate_interface/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def __init__(
137137
self.metadata_v15 = metadata_v15
138138
self.runtime_info = runtime_info
139139
self.registry = registry
140+
runtime_info = runtime_info or {}
140141
self.runtime_version = runtime_info.get("specVersion")
141142
self.transaction_version = runtime_info.get("transactionVersion")
142143
self.runtime_config = runtime_config or RuntimeConfigurationObject(

tests/unit_tests/asyncio_/test_substrate_interface.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from async_substrate_interface.async_substrate import AsyncSubstrateInterface
88
from async_substrate_interface.types import ScaleObj
9+
from tests.helpers.settings import ARCHIVE_ENTRYPOINT
910

1011

1112
@pytest.mark.asyncio
@@ -113,3 +114,21 @@ async def test_websocket_shutdown_timer():
113114
await substrate.get_chain_head()
114115
await asyncio.sleep(6) # same sleep time as before
115116
assert substrate.ws._initialized is True # connection should still be open
117+
118+
119+
@pytest.mark.asyncio
120+
async def test_runtime_switching():
121+
block = 6067945 # block where a runtime switch happens
122+
async with AsyncSubstrateInterface(
123+
ARCHIVE_ENTRYPOINT, ss58_format=42, chain_name="Bittensor"
124+
) as substrate:
125+
# assures we switch between the runtimes without error
126+
assert await substrate.get_extrinsics(block_number=block - 20) is not None
127+
assert await substrate.get_extrinsics(block_number=block) is not None
128+
assert await substrate.get_extrinsics(block_number=block - 21) is not None
129+
one, two = await asyncio.gather(
130+
substrate.get_extrinsics(block_number=block - 22),
131+
substrate.get_extrinsics(block_number=block + 1),
132+
)
133+
assert one is not None
134+
assert two is not None

tests/unit_tests/sync/test_substrate_interface.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from async_substrate_interface.sync_substrate import SubstrateInterface
44
from async_substrate_interface.types import ScaleObj
55

6+
from tests.helpers.settings import ARCHIVE_ENTRYPOINT
7+
68

79
def test_runtime_call(monkeypatch):
810
substrate = SubstrateInterface("ws://localhost", _mock=True)
@@ -73,3 +75,14 @@ def test_runtime_call(monkeypatch):
7375
"state_call", ["SubstrateApi_SubstrateMethod", "", None]
7476
)
7577
substrate.close()
78+
79+
80+
def test_runtime_switching():
81+
block = 6067945 # block where a runtime switch happens
82+
with SubstrateInterface(
83+
ARCHIVE_ENTRYPOINT, ss58_format=42, chain_name="Bittensor"
84+
) as substrate:
85+
# assures we switch between the runtimes without error
86+
assert substrate.get_extrinsics(block_number=block - 20) is not None
87+
assert substrate.get_extrinsics(block_number=block) is not None
88+
assert substrate.get_extrinsics(block_number=block - 21) is not None

tests/unit_tests/test_types.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from async_substrate_interface.types import ScaleObj
1+
from async_substrate_interface.types import ScaleObj, Runtime, RuntimeCache
22

33

44
def test_scale_object():
@@ -51,3 +51,34 @@ def test_scale_object():
5151
assert inst_dict["a"] == 1
5252
assert inst_dict["b"] == 2
5353
assert [i for i in inst_dict] == ["a", "b"]
54+
55+
56+
def test_runtime_cache():
57+
fake_block = 2
58+
fake_hash = "0xignore"
59+
fake_version = 271
60+
61+
new_fake_block = 3
62+
newer_fake_block = 4
63+
64+
new_fake_hash = "0xnewfakehash"
65+
66+
runtime = Runtime("", None, None)
67+
runtime_cache = RuntimeCache()
68+
# insert our Runtime object into the cache with a set block, hash, and version
69+
runtime_cache.add_item(runtime, fake_block, fake_hash, fake_version)
70+
71+
assert runtime_cache.retrieve(fake_block) is not None
72+
# cache does not yet know that new_fake_block has the same runtime
73+
assert runtime_cache.retrieve(new_fake_block) is None
74+
assert (
75+
runtime_cache.retrieve(new_fake_block, runtime_version=fake_version) is not None
76+
)
77+
# after checking the runtime with the new block, it now knows this runtime should also map to this block
78+
assert runtime_cache.retrieve(new_fake_block) is not None
79+
assert runtime_cache.retrieve(newer_fake_block) is None
80+
assert runtime_cache.retrieve(newer_fake_block, fake_hash) is not None
81+
assert runtime_cache.retrieve(newer_fake_block) is not None
82+
assert runtime_cache.retrieve(block_hash=new_fake_hash) is None
83+
assert runtime_cache.retrieve(fake_block, block_hash=new_fake_hash) is not None
84+
assert runtime_cache.retrieve(block_hash=new_fake_hash) is not None

0 commit comments

Comments
 (0)