From 9076e416849854918c1a0c3573417c3d842c6343 Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Thu, 20 Feb 2025 13:54:28 +0200 Subject: [PATCH 1/4] Hashing and using the hash as the ID was causing a massive slowdown. This remedies it by using integers. --- async_substrate_interface/async_substrate.py | 4 ++-- async_substrate_interface/sync_substrate.py | 5 ++--- async_substrate_interface/utils/__init__.py | 9 +++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/async_substrate_interface/async_substrate.py b/async_substrate_interface/async_substrate.py index 0037bc6..3b6ce16 100644 --- a/async_substrate_interface/async_substrate.py +++ b/async_substrate_interface/async_substrate.py @@ -48,7 +48,7 @@ SubstrateMixin, Preprocessed, ) -from async_substrate_interface.utils import hex_to_bytes, json, generate_unique_id +from async_substrate_interface.utils import hex_to_bytes, json, get_next_id from async_substrate_interface.utils.decoding import ( _determine_if_old_runtime_call, _bt_decode_to_dict_or_list, @@ -620,7 +620,7 @@ async def send(self, payload: dict) -> int: id: the internal ID of the request (incremented int) """ # async with self._lock: - original_id = generate_unique_id(json.dumps(payload)) + original_id = get_next_id() # self._open_subscriptions += 1 try: await self.ws.send(json.dumps({**payload, **{"id": original_id}})) diff --git a/async_substrate_interface/sync_substrate.py b/async_substrate_interface/sync_substrate.py index f2756bc..6ff3957 100644 --- a/async_substrate_interface/sync_substrate.py +++ b/async_substrate_interface/sync_substrate.py @@ -30,7 +30,7 @@ Preprocessed, ScaleObj, ) -from async_substrate_interface.utils import hex_to_bytes, json, generate_unique_id +from async_substrate_interface.utils import hex_to_bytes, json, get_next_id from async_substrate_interface.utils.decoding import ( _determine_if_old_runtime_call, _bt_decode_to_dict_or_list, @@ -1688,8 +1688,7 @@ def _make_rpc_request( ws = self.connect(init=False if attempt == 1 else True) for payload in payloads: - payload_str = json.dumps(payload["payload"]) - item_id = generate_unique_id(payload_str) + item_id = get_next_id() ws.send(json.dumps({**payload["payload"], **{"id": item_id}})) request_manager.add_request(item_id, payload["id"]) diff --git a/async_substrate_interface/utils/__init__.py b/async_substrate_interface/utils/__init__.py index 29b3ced..1ea04c2 100644 --- a/async_substrate_interface/utils/__init__.py +++ b/async_substrate_interface/utils/__init__.py @@ -1,10 +1,11 @@ import importlib -import hashlib +from itertools import cycle +id_cycle = cycle(range(1, 999)) -def generate_unique_id(item: str, length=10): - hashed_value = hashlib.sha256(item.encode()).hexdigest() - return hashed_value[:length] + +def get_next_id(): + return next(id_cycle) def hex_to_bytes(hex_str: str) -> bytes: From 0e56cdf7fd01fd3e1d5127b9156bd34bd5b5b263 Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Thu, 20 Feb 2025 14:21:08 +0200 Subject: [PATCH 2/4] Add randomness to the ID to make it more unique. --- async_substrate_interface/utils/__init__.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/async_substrate_interface/utils/__init__.py b/async_substrate_interface/utils/__init__.py index 1ea04c2..a65ba1f 100644 --- a/async_substrate_interface/utils/__init__.py +++ b/async_substrate_interface/utils/__init__.py @@ -1,11 +1,18 @@ import importlib from itertools import cycle +import random +import string id_cycle = cycle(range(1, 999)) -def get_next_id(): - return next(id_cycle) +def get_next_id() -> str: + """ + Generates a pseudo-random ID by returning grabbing the next int of a range from 1-998, and prepending it with + two random ascii characters. + """ + random_letters = "".join(random.choices(string.ascii_letters, k=2)) + return f"{random_letters}{next(id_cycle)}" def hex_to_bytes(hex_str: str) -> bytes: From 4922d09bba408a96ad36a8573ab29926d6d1e0b0 Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Thu, 20 Feb 2025 14:22:10 +0200 Subject: [PATCH 3/4] Docstring. --- async_substrate_interface/utils/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/async_substrate_interface/utils/__init__.py b/async_substrate_interface/utils/__init__.py index a65ba1f..e295e02 100644 --- a/async_substrate_interface/utils/__init__.py +++ b/async_substrate_interface/utils/__init__.py @@ -8,7 +8,7 @@ def get_next_id() -> str: """ - Generates a pseudo-random ID by returning grabbing the next int of a range from 1-998, and prepending it with + Generates a pseudo-random ID by returning the next int of a range from 1-998 prepended with two random ascii characters. """ random_letters = "".join(random.choices(string.ascii_letters, k=2)) From a0053860f8a6ca751c9e9b73dcfc1cd57e406ef2 Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Thu, 20 Feb 2025 14:28:38 +0200 Subject: [PATCH 4/4] Added unit test --- tests/unit_tests/test_utils.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 tests/unit_tests/test_utils.py diff --git a/tests/unit_tests/test_utils.py b/tests/unit_tests/test_utils.py new file mode 100644 index 0000000..70c36f5 --- /dev/null +++ b/tests/unit_tests/test_utils.py @@ -0,0 +1,10 @@ +from async_substrate_interface.utils import get_next_id +from string import ascii_letters + + +def test_get_next_id(): + next_id = get_next_id() + assert next_id[0] in ascii_letters + assert next_id[1] in ascii_letters + assert 0 < int(next_id[2:]) < 999 + assert 3 <= len(next_id) <= 5