Skip to content

Commit 31f0844

Browse files
committed
Merge remote-tracking branch 'origin/staging' into feat/thewhaleking/warn-users-about-too-old-blocks
2 parents 36da8e7 + 8f1c6c4 commit 31f0844

File tree

6 files changed

+63
-27
lines changed

6 files changed

+63
-27
lines changed

CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
# Changelog
22

3+
## 1.0.2 /2025-02-19
4+
5+
## What's Changed
6+
* Closes the connection on the object being garbage-collected by @thewhaleking in https://github.com/opentensor/async-substrate-interface/pull/51
7+
* Generate UIDs for websockets by @thewhaleking in https://github.com/opentensor/async-substrate-interface/pull/50
8+
* Dynamically pulls the info for Vec<AccountId> from the metadata by @thewhaleking in https://github.com/opentensor/async-substrate-interface/pull/47
9+
* Fix readme by @igorsyl in https://github.com/opentensor/async-substrate-interface/pull/46
10+
* Handle options with bt-decode by @thewhaleking in https://github.com/opentensor/async-substrate-interface/pull/52
11+
* Backmerge main to staging 101 by @ibraheem-opentensor in https://github.com/opentensor/async-substrate-interface/pull/53
12+
* Handles None change_data by @ibraheem-opentensor in https://github.com/opentensor/async-substrate-interface/pull/54
13+
14+
## New Contributors
15+
* @igorsyl made their first contribution in https://github.com/opentensor/async-substrate-interface/pull/46
16+
17+
**Full Changelog**: https://github.com/opentensor/async-substrate-interface/compare/v1.0.1...v1.0.2
18+
19+
## 1.0.1 /2025-02-17
20+
21+
## What's Changed
22+
* Updates type for vec acc id by @ibraheem-opentensor in https://github.com/opentensor/async-substrate-interface/pull/45
23+
* Backmerge main staging 101 by @ibraheem-opentensor in https://github.com/opentensor/async-substrate-interface/pull/48
24+
25+
**Full Changelog**: https://github.com/opentensor/async-substrate-interface/compare/v1.0.0...v1.0.1
26+
327
## 1.0.0 /2025-02-13
428

529
## What's new

async_substrate_interface/async_substrate.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
SubstrateMixin,
4949
Preprocessed,
5050
)
51-
from async_substrate_interface.utils import hex_to_bytes, json, generate_unique_id
51+
from async_substrate_interface.utils import hex_to_bytes, json, get_next_id
5252
from async_substrate_interface.utils.decoding import (
5353
_determine_if_old_runtime_call,
5454
_bt_decode_to_dict_or_list,
@@ -620,7 +620,7 @@ async def send(self, payload: dict) -> int:
620620
id: the internal ID of the request (incremented int)
621621
"""
622622
# async with self._lock:
623-
original_id = generate_unique_id(json.dumps(payload))
623+
original_id = get_next_id()
624624
# self._open_subscriptions += 1
625625
try:
626626
await self.ws.send(json.dumps({**payload, **{"id": original_id}}))
@@ -904,15 +904,14 @@ async def decode_scale(
904904
Returns:
905905
Decoded object
906906
"""
907-
if scale_bytes == b"\x00":
908-
obj = None
907+
if scale_bytes == b"":
908+
return None
909+
if type_string == "scale_info::0": # Is an AccountId
910+
# Decode AccountId bytes to SS58 address
911+
return ss58_encode(scale_bytes, SS58_FORMAT)
909912
else:
910-
if type_string == "scale_info::0": # Is an AccountId
911-
# Decode AccountId bytes to SS58 address
912-
return ss58_encode(scale_bytes, SS58_FORMAT)
913-
else:
914-
await self._wait_for_registry(_attempt, _retries)
915-
obj = decode_by_type_string(type_string, self.registry, scale_bytes)
913+
await self._wait_for_registry(_attempt, _retries)
914+
obj = decode_by_type_string(type_string, self.registry, scale_bytes)
916915
if return_scale_obj:
917916
return ScaleObj(obj)
918917
else:
@@ -2245,7 +2244,7 @@ async def query_multi(
22452244
# Decode result for specified storage_key
22462245
storage_key = storage_key_map[change_storage_key]
22472246
if change_data is None:
2248-
change_data = b"\x00"
2247+
change_data = b""
22492248
else:
22502249
change_data = bytes.fromhex(change_data[2:])
22512250
result.append(

async_substrate_interface/sync_substrate.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
Preprocessed,
3131
ScaleObj,
3232
)
33-
from async_substrate_interface.utils import hex_to_bytes, json, generate_unique_id
33+
from async_substrate_interface.utils import hex_to_bytes, json, get_next_id
3434
from async_substrate_interface.utils.decoding import (
3535
_determine_if_old_runtime_call,
3636
_bt_decode_to_dict_or_list,
@@ -652,15 +652,11 @@ def decode_scale(
652652
Returns:
653653
Decoded object
654654
"""
655-
656-
if scale_bytes == b"\x00":
657-
obj = None
655+
if type_string == "scale_info::0": # Is an AccountId
656+
# Decode AccountId bytes to SS58 address
657+
return ss58_encode(scale_bytes, SS58_FORMAT)
658658
else:
659-
if type_string == "scale_info::0": # Is an AccountId
660-
# Decode AccountId bytes to SS58 address
661-
return ss58_encode(scale_bytes, SS58_FORMAT)
662-
else:
663-
obj = decode_by_type_string(type_string, self.registry, scale_bytes)
659+
obj = decode_by_type_string(type_string, self.registry, scale_bytes)
664660
if return_scale_obj:
665661
return ScaleObj(obj)
666662
else:
@@ -1688,8 +1684,7 @@ def _make_rpc_request(
16881684

16891685
ws = self.connect(init=False if attempt == 1 else True)
16901686
for payload in payloads:
1691-
payload_str = json.dumps(payload["payload"])
1692-
item_id = generate_unique_id(payload_str)
1687+
item_id = get_next_id()
16931688
ws.send(json.dumps({**payload["payload"], **{"id": item_id}}))
16941689
request_manager.add_request(item_id, payload["id"])
16951690

async_substrate_interface/utils/__init__.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import importlib
2-
import hashlib
2+
from itertools import cycle
3+
import random
4+
import string
35

6+
id_cycle = cycle(range(1, 999))
47

5-
def generate_unique_id(item: str, length=10):
6-
hashed_value = hashlib.sha256(item.encode()).hexdigest()
7-
return hashed_value[:length]
8+
9+
def get_next_id() -> str:
10+
"""
11+
Generates a pseudo-random ID by returning the next int of a range from 1-998 prepended with
12+
two random ascii characters.
13+
"""
14+
random_letters = "".join(random.choices(string.ascii_letters, k=2))
15+
return f"{random_letters}{next(id_cycle)}"
816

917

1018
def hex_to_bytes(hex_str: str) -> bytes:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "async-substrate-interface"
3-
version = "1.0.0"
3+
version = "1.0.2"
44
description = "Asyncio library for interacting with substrate. Mostly API-compatible with py-substrate-interface"
55
readme = "README.md"
66
license = { file = "LICENSE" }

tests/unit_tests/test_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from async_substrate_interface.utils import get_next_id
2+
from string import ascii_letters
3+
4+
5+
def test_get_next_id():
6+
next_id = get_next_id()
7+
assert next_id[0] in ascii_letters
8+
assert next_id[1] in ascii_letters
9+
assert 0 < int(next_id[2:]) < 999
10+
assert 3 <= len(next_id) <= 5

0 commit comments

Comments
 (0)