Skip to content

Commit 40be8bd

Browse files
authored
Merge pull request #50 from opentensor/feat/thewhaleking/unique-ids-for-ws
Generate UIDs for websockets
2 parents 7d22919 + a4d9d6d commit 40be8bd

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 5 additions & 10 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
51+
from async_substrate_interface.utils import hex_to_bytes, json, generate_unique_id
5252
from async_substrate_interface.utils.decoding import (
5353
_determine_if_old_runtime_call,
5454
_bt_decode_to_dict_or_list,
@@ -507,7 +507,6 @@ def __init__(
507507
# TODO reconnection logic
508508
self.ws_url = ws_url
509509
self.ws: Optional["ClientConnection"] = None
510-
self.id = 0
511510
self.max_subscriptions = max_subscriptions
512511
self.max_connections = max_connections
513512
self.shutdown_timer = shutdown_timer
@@ -543,8 +542,6 @@ async def connect(self, force=False):
543542
connect(self.ws_url, **self._options), timeout=10
544543
)
545544
self._receiving_task = asyncio.create_task(self._start_receiving())
546-
if force:
547-
self.id = 100
548545

549546
async def __aexit__(self, exc_type, exc_val, exc_tb):
550547
async with self._lock: # TODO is this actually what I want to happen?
@@ -556,7 +553,6 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
556553
except asyncio.CancelledError:
557554
pass
558555
if self._in_use == 0 and self.ws is not None:
559-
self.id = 0
560556
self._open_subscriptions = 0
561557
self._exit_task = asyncio.create_task(self._exit_with_timer())
562558

@@ -582,7 +578,6 @@ async def shutdown(self):
582578
self.ws = None
583579
self._initialized = False
584580
self._receiving_task = None
585-
self.id = 0
586581

587582
async def _recv(self) -> None:
588583
try:
@@ -625,8 +620,7 @@ async def send(self, payload: dict) -> int:
625620
id: the internal ID of the request (incremented int)
626621
"""
627622
# async with self._lock:
628-
original_id = self.id
629-
self.id += 1
623+
original_id = generate_unique_id(json.dumps(payload))
630624
# self._open_subscriptions += 1
631625
try:
632626
await self.ws.send(json.dumps({**payload, **{"id": original_id}}))
@@ -735,8 +729,9 @@ async def initialize(self):
735729
chain = await self.rpc_request("system_chain", [])
736730
self._chain = chain.get("result")
737731
init_load = await asyncio.gather(
738-
self.load_registry(), self._first_initialize_runtime(),
739-
return_exceptions=True
732+
self.load_registry(),
733+
self._first_initialize_runtime(),
734+
return_exceptions=True,
740735
)
741736
for potential_exception in init_load:
742737
if isinstance(potential_exception, Exception):

async_substrate_interface/sync_substrate.py

Lines changed: 3 additions & 3 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
33+
from async_substrate_interface.utils import hex_to_bytes, json, generate_unique_id
3434
from async_substrate_interface.utils.decoding import (
3535
_determine_if_old_runtime_call,
3636
_bt_decode_to_dict_or_list,
@@ -1684,9 +1684,9 @@ def _make_rpc_request(
16841684
subscription_added = False
16851685

16861686
ws = self.connect(init=False if attempt == 1 else True)
1687-
item_id = 0
16881687
for payload in payloads:
1689-
item_id += 1
1688+
payload_str = json.dumps(payload["payload"])
1689+
item_id = generate_unique_id(payload_str)
16901690
ws.send(json.dumps({**payload["payload"], **{"id": item_id}}))
16911691
request_manager.add_request(item_id, payload["id"])
16921692

async_substrate_interface/utils/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import importlib
2+
import hashlib
3+
4+
5+
def generate_unique_id(item: str, length=10):
6+
hashed_value = hashlib.sha256(item.encode()).hexdigest()
7+
return hashed_value[:length]
28

39

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

0 commit comments

Comments
 (0)