Skip to content

Commit ce2a21c

Browse files
committed
Merge branch 'feat/thewhaleking/distribute-runtime' into feat/thewhaleking/python-ss58-conversion
# Conflicts: # tests/unit_tests/asyncio_/test_substrate_interface.py # tests/unit_tests/sync/test_substrate_interface.py
2 parents fe74e25 + fc2a7bd commit ce2a21c

File tree

6 files changed

+70
-147
lines changed

6 files changed

+70
-147
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,9 +1379,9 @@ async def get_metadata_errors(
13791379

13801380
async def get_metadata_error(
13811381
self,
1382-
module_name,
1383-
error_name,
1384-
block_hash=None,
1382+
module_name: str,
1383+
error_name: str,
1384+
block_hash: Optional[str] = None,
13851385
runtime: Optional[Runtime] = None,
13861386
):
13871387
"""
@@ -3114,9 +3114,9 @@ async def get_metadata_constants(self, block_hash=None) -> list[dict]:
31143114

31153115
async def get_metadata_constant(
31163116
self,
3117-
module_name,
3118-
constant_name,
3119-
block_hash=None,
3117+
module_name: str,
3118+
constant_name: str,
3119+
block_hash: Optional[str] = None,
31203120
runtime: Optional[Runtime] = None,
31213121
):
31223122
"""

async_substrate_interface/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,8 +649,8 @@ def is_valid_ss58_address(self, value: str) -> bool:
649649
def serialize_storage_item(
650650
self,
651651
storage_item: ScaleType,
652-
module,
653-
spec_version_id,
652+
module: str,
653+
spec_version_id: int,
654654
runtime: Optional[Runtime] = None,
655655
) -> dict:
656656
"""
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pytest
2+
3+
from async_substrate_interface.async_substrate import AsyncSubstrateInterface
4+
from async_substrate_interface.types import ScaleObj
5+
from tests.helpers.settings import ARCHIVE_ENTRYPOINT
6+
7+
8+
@pytest.mark.asyncio
9+
async def test_legacy_decoding():
10+
# roughly 4000 blocks before metadata v15 was added
11+
pre_metadata_v15_block = 3_010_611
12+
13+
async with AsyncSubstrateInterface(ARCHIVE_ENTRYPOINT) as substrate:
14+
block_hash = await substrate.get_block_hash(pre_metadata_v15_block)
15+
events = await substrate.get_events(block_hash)
16+
assert isinstance(events, list)
17+
18+
query_map_result = await substrate.query_map(
19+
module="SubtensorModule",
20+
storage_function="NetworksAdded",
21+
block_hash=block_hash,
22+
)
23+
async for key, value in query_map_result:
24+
assert isinstance(key, int)
25+
assert isinstance(value, ScaleObj)
26+
27+
timestamp = await substrate.query(
28+
"Timestamp",
29+
"Now",
30+
block_hash=block_hash,
31+
)
32+
assert timestamp.value == 1716358476004
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from async_substrate_interface.sync_substrate import SubstrateInterface
2+
from async_substrate_interface.types import ScaleObj
3+
from tests.helpers.settings import ARCHIVE_ENTRYPOINT
4+
5+
6+
def test_legacy_decoding():
7+
# roughly 4000 blocks before metadata v15 was added
8+
pre_metadata_v15_block = 3_010_611
9+
10+
with SubstrateInterface(ARCHIVE_ENTRYPOINT) as substrate:
11+
block_hash = substrate.get_block_hash(pre_metadata_v15_block)
12+
events = substrate.get_events(block_hash)
13+
assert isinstance(events, list)
14+
15+
query_map_result = substrate.query_map(
16+
module="SubtensorModule",
17+
storage_function="NetworksAdded",
18+
block_hash=block_hash,
19+
)
20+
for key, value in query_map_result:
21+
assert isinstance(key, int)
22+
assert isinstance(value, ScaleObj)
23+
24+
timestamp = substrate.query(
25+
"Timestamp",
26+
"Now",
27+
block_hash=block_hash,
28+
)
29+
assert timestamp.value == 1716358476004

tests/unit_tests/asyncio_/test_substrate_interface.py

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
from unittest.mock import AsyncMock, MagicMock, ANY
33

44
import pytest
5-
from scalecodec import ss58_encode
65
from websockets.exceptions import InvalidURI
76

87
from async_substrate_interface.async_substrate import AsyncSubstrateInterface
98
from async_substrate_interface.types import ScaleObj
10-
from tests.helpers.settings import ARCHIVE_ENTRYPOINT, LATENT_LITE_ENTRYPOINT
119

1210

1311
@pytest.mark.asyncio
@@ -101,7 +99,7 @@ async def test_runtime_call(monkeypatch):
10199
@pytest.mark.asyncio
102100
async def test_websocket_shutdown_timer():
103101
# using default ws shutdown timer of 5.0 seconds
104-
async with AsyncSubstrateInterface(LATENT_LITE_ENTRYPOINT) as substrate:
102+
async with AsyncSubstrateInterface("wss://lite.sub.latent.to:443") as substrate:
105103
await substrate.get_chain_head()
106104
await asyncio.sleep(6)
107105
assert (
@@ -115,70 +113,3 @@ async def test_websocket_shutdown_timer():
115113
await substrate.get_chain_head()
116114
await asyncio.sleep(6) # same sleep time as before
117115
assert substrate.ws._initialized is True # connection should still be open
118-
119-
120-
@pytest.mark.asyncio
121-
async def test_legacy_decoding():
122-
# roughly 4000 blocks before metadata v15 was added
123-
pre_metadata_v15_block = 3_010_611
124-
125-
async with AsyncSubstrateInterface(ARCHIVE_ENTRYPOINT) as substrate:
126-
block_hash = await substrate.get_block_hash(pre_metadata_v15_block)
127-
events = await substrate.get_events(block_hash)
128-
assert isinstance(events, list)
129-
130-
query_map_result = await substrate.query_map(
131-
module="SubtensorModule",
132-
storage_function="NetworksAdded",
133-
block_hash=block_hash,
134-
)
135-
async for key, value in query_map_result:
136-
assert isinstance(key, int)
137-
assert isinstance(value, ScaleObj)
138-
139-
timestamp = await substrate.query(
140-
"Timestamp",
141-
"Now",
142-
block_hash=block_hash,
143-
)
144-
assert timestamp.value == 1716358476004
145-
146-
147-
@pytest.mark.asyncio
148-
async def test_ss58_conversion():
149-
async with AsyncSubstrateInterface(
150-
LATENT_LITE_ENTRYPOINT, ss58_format=42, decode_ss58=False
151-
) as substrate:
152-
block_hash = await substrate.get_chain_finalised_head()
153-
qm = await substrate.query_map(
154-
"SubtensorModule",
155-
"OwnedHotkeys",
156-
block_hash=block_hash,
157-
)
158-
# only do the first page, bc otherwise this will be massive
159-
for key, value in qm.records:
160-
assert isinstance(key, tuple)
161-
assert isinstance(value, ScaleObj)
162-
assert isinstance(value.value, list)
163-
assert len(key) == 1
164-
for key_tuple in value.value:
165-
assert len(key_tuple[0]) == 32
166-
random_key = key_tuple[0]
167-
168-
ss58_of_key = ss58_encode(bytes(random_key), substrate.ss58_format)
169-
assert isinstance(ss58_of_key, str)
170-
171-
substrate.decode_ss58 = True # change to decoding True
172-
173-
qm = await substrate.query_map(
174-
"SubtensorModule",
175-
"OwnedHotkeys",
176-
block_hash=block_hash,
177-
)
178-
for key, value in qm.records:
179-
assert isinstance(key, str)
180-
assert isinstance(value, ScaleObj)
181-
assert isinstance(value.value, list)
182-
if len(value.value) > 0:
183-
for decoded_key in value.value:
184-
assert isinstance(decoded_key, str)
Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
from unittest.mock import MagicMock
22

3-
from scalecodec import ss58_encode
4-
53
from async_substrate_interface.sync_substrate import SubstrateInterface
64
from async_substrate_interface.types import ScaleObj
75

8-
from tests.helpers.settings import ARCHIVE_ENTRYPOINT, LATENT_LITE_ENTRYPOINT
9-
106

117
def test_runtime_call(monkeypatch):
128
substrate = SubstrateInterface("ws://localhost", _mock=True)
@@ -77,68 +73,3 @@ def test_runtime_call(monkeypatch):
7773
"state_call", ["SubstrateApi_SubstrateMethod", "", None]
7874
)
7975
substrate.close()
80-
81-
82-
def test_legacy_decoding():
83-
# roughly 4000 blocks before metadata v15 was added
84-
pre_metadata_v15_block = 3_010_611
85-
86-
with SubstrateInterface(ARCHIVE_ENTRYPOINT) as substrate:
87-
block_hash = substrate.get_block_hash(pre_metadata_v15_block)
88-
events = substrate.get_events(block_hash)
89-
assert isinstance(events, list)
90-
91-
query_map_result = substrate.query_map(
92-
module="SubtensorModule",
93-
storage_function="NetworksAdded",
94-
block_hash=block_hash,
95-
)
96-
for key, value in query_map_result:
97-
assert isinstance(key, int)
98-
assert isinstance(value, ScaleObj)
99-
100-
timestamp = substrate.query(
101-
"Timestamp",
102-
"Now",
103-
block_hash=block_hash,
104-
)
105-
assert timestamp.value == 1716358476004
106-
107-
108-
def test_ss58_conversion():
109-
with SubstrateInterface(
110-
LATENT_LITE_ENTRYPOINT, ss58_format=42, decode_ss58=False
111-
) as substrate:
112-
block_hash = substrate.get_chain_finalised_head()
113-
qm = substrate.query_map(
114-
"SubtensorModule",
115-
"OwnedHotkeys",
116-
block_hash=block_hash,
117-
)
118-
# only do the first page, bc otherwise this will be massive
119-
for key, value in qm.records:
120-
assert isinstance(key, tuple)
121-
assert isinstance(value, ScaleObj)
122-
assert isinstance(value.value, list)
123-
assert len(key) == 1
124-
for key_tuple in value.value:
125-
assert len(key_tuple[0]) == 32
126-
random_key = key_tuple[0]
127-
128-
ss58_of_key = ss58_encode(bytes(random_key), substrate.ss58_format)
129-
assert isinstance(ss58_of_key, str)
130-
131-
substrate.decode_ss58 = True # change to decoding True
132-
133-
qm = substrate.query_map(
134-
"SubtensorModule",
135-
"OwnedHotkeys",
136-
block_hash=block_hash,
137-
)
138-
for key, value in qm.records:
139-
assert isinstance(key, str)
140-
assert isinstance(value, ScaleObj)
141-
assert isinstance(value.value, list)
142-
if len(value.value) > 0:
143-
for decoded_key in value.value:
144-
assert isinstance(decoded_key, str)

0 commit comments

Comments
 (0)