Skip to content

Commit be191ec

Browse files
committed
Updates websocket + bumps version
1 parent 0ce48ba commit be191ec

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

bittensor_cli/src/bittensor/async_substrate_interface.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from collections import defaultdict
55
from dataclasses import dataclass
66
from hashlib import blake2b
7-
from typing import Optional, Any, Union, Callable, Awaitable, cast
7+
from typing import Optional, Any, Union, Callable, Awaitable, cast, TYPE_CHECKING
88

99
from bt_decode import PortableRegistry, decode as decode_by_type_string, MetadataV15
1010
from async_property import async_property
@@ -19,7 +19,11 @@
1919
BlockNotFound,
2020
)
2121
from substrateinterface.storage import StorageKey
22-
import websockets
22+
from websockets.asyncio.client import connect
23+
from websockets.exceptions import ConnectionClosed
24+
25+
if TYPE_CHECKING:
26+
from websockets.asyncio.client import ClientConnection
2327

2428
ResultHandler = Callable[[dict, Any], Awaitable[tuple[dict, bool]]]
2529

@@ -433,7 +437,7 @@ def add_item(
433437
self.block_hashes[block_hash] = runtime
434438

435439
def retrieve(
436-
self, block: Optional[int], block_hash: Optional[str]
440+
self, block: Optional[int] = None, block_hash: Optional[str] = None
437441
) -> Optional["Runtime"]:
438442
if block is not None:
439443
return self.blocks.get(block)
@@ -624,7 +628,7 @@ def __init__(
624628
# TODO allow setting max concurrent connections and rpc subscriptions per connection
625629
# TODO reconnection logic
626630
self.ws_url = ws_url
627-
self.ws: Optional[websockets.WebSocketClientProtocol] = None
631+
self.ws: Optional["ClientConnection"] = None
628632
self.id = 0
629633
self.max_subscriptions = max_subscriptions
630634
self.max_connections = max_connections
@@ -646,15 +650,12 @@ async def __aenter__(self):
646650
self._exit_task.cancel()
647651
if not self._initialized:
648652
self._initialized = True
649-
await self._connect()
653+
self.ws = await asyncio.wait_for(
654+
connect(self.ws_url, **self._options), timeout=10
655+
)
650656
self._receiving_task = asyncio.create_task(self._start_receiving())
651657
return self
652658

653-
async def _connect(self):
654-
self.ws = await asyncio.wait_for(
655-
websockets.connect(self.ws_url, **self._options), timeout=10
656-
)
657-
658659
async def __aexit__(self, exc_type, exc_val, exc_tb):
659660
async with self._lock:
660661
self._in_use -= 1
@@ -695,9 +696,7 @@ async def shutdown(self):
695696

696697
async def _recv(self) -> None:
697698
try:
698-
response = json.loads(
699-
await cast(websockets.WebSocketClientProtocol, self.ws).recv()
700-
)
699+
response = json.loads(await self.ws.recv())
701700
async with self._lock:
702701
self._open_subscriptions -= 1
703702
if "id" in response:
@@ -706,7 +705,7 @@ async def _recv(self) -> None:
706705
self._received[response["params"]["subscription"]] = response
707706
else:
708707
raise KeyError(response)
709-
except websockets.ConnectionClosed:
708+
except ConnectionClosed:
710709
raise
711710
except KeyError as e:
712711
raise e
@@ -717,7 +716,7 @@ async def _start_receiving(self):
717716
await self._recv()
718717
except asyncio.CancelledError:
719718
pass
720-
except websockets.ConnectionClosed:
719+
except ConnectionClosed:
721720
# TODO try reconnect, but only if it's needed
722721
raise
723722

@@ -734,7 +733,7 @@ async def send(self, payload: dict) -> int:
734733
try:
735734
await self.ws.send(json.dumps({**payload, **{"id": original_id}}))
736735
return original_id
737-
except websockets.ConnectionClosed:
736+
except ConnectionClosed:
738737
raise
739738

740739
async def retrieve(self, item_id: int) -> Optional[dict]:
@@ -775,7 +774,6 @@ def __init__(
775774
chain_endpoint,
776775
options={
777776
"max_size": 2**32,
778-
"read_limit": 2**16,
779777
"write_limit": 2**16,
780778
},
781779
)
@@ -1135,7 +1133,7 @@ async def create_storage_key(
11351133
-------
11361134
StorageKey
11371135
"""
1138-
runtime = await self.init_runtime(block_hash=block_hash)
1136+
await self.init_runtime(block_hash=block_hash)
11391137

11401138
return StorageKey.create_from_storage_function(
11411139
pallet,
@@ -1555,7 +1553,7 @@ async def _process_response(
15551553
self,
15561554
response: dict,
15571555
subscription_id: Union[int, str],
1558-
value_scale_type: Optional[str],
1556+
value_scale_type: Optional[str] = None,
15591557
storage_item: Optional[ScaleType] = None,
15601558
runtime: Optional[Runtime] = None,
15611559
result_handler: Optional[ResultHandler] = None,

bittensor_cli/src/commands/subnets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ def format_liquidity_cell(
629629
tempo_cell = (
630630
(f"{subnet.blocks_since_last_step}/{subnet.tempo}{block_change_text}")
631631
if netuid != 0
632-
else "[red]N/A[/red]"
632+
else "-/-"
633633
)
634634

635635
rows.append(

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ rich~=13.7
1515
scalecodec==1.2.11
1616
substrate-interface~=1.7.9
1717
typer~=0.12
18-
websockets==13.0
18+
websockets>=14.1
1919
bittensor-wallet>=2.0.2
2020
bt-decode==0.2.0a0

0 commit comments

Comments
 (0)