Skip to content

Commit e63ee25

Browse files
committed
Async
1 parent c660e19 commit e63ee25

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
cast,
2323
)
2424

25+
import websockets.exceptions
2526
from bt_decode import MetadataV15, PortableRegistry, decode as decode_by_type_string
2627
from scalecodec.base import ScaleBytes, ScaleType, RuntimeConfigurationObject
2728
from scalecodec.type_registry import load_type_registry_preset
@@ -599,6 +600,7 @@ async def _cancel(self):
599600

600601
async def connect(self, force=False):
601602
async with self._lock:
603+
logger.debug(f"Websocket connecting to {self.ws_url}")
602604
if self._sending is None or self._sending.empty():
603605
self._sending = asyncio.Queue()
604606
if self._exit_task:
@@ -723,8 +725,10 @@ async def _start_receiving(self, ws: ClientConnection) -> Exception:
723725
if not fut.done():
724726
fut.set_exception(e)
725727
fut.cancel()
728+
elif isinstance(e, websockets.exceptions.ConnectionClosedOK):
729+
logger.debug("Websocket connection closed.")
726730
else:
727-
logger.debug("Timeout occurred. Reconnecting.")
731+
logger.debug(f"Timeout occurred. Reconnecting.")
728732
return e
729733

730734
async def _start_sending(self, ws) -> Exception:
@@ -753,6 +757,8 @@ async def _start_sending(self, ws) -> Exception:
753757
for i in self._received.keys():
754758
self._received[i].set_exception(e)
755759
self._received[i].cancel()
760+
elif isinstance(e, websockets.exceptions.ConnectionClosedOK):
761+
logger.debug("Websocket connection closed.")
756762
else:
757763
logger.debug("Timeout occurred. Reconnecting.")
758764
return e
@@ -2370,6 +2376,7 @@ async def _make_rpc_request(
23702376
for payload in payloads:
23712377
item_id = await ws.send(payload["payload"])
23722378
request_manager.add_request(item_id, payload["id"])
2379+
logger.debug(f"Submitted payload ID {payload['id']} with websocket ID {item_id}: {payload}")
23732380

23742381
while True:
23752382
for item_id in request_manager.unresponded():
@@ -2390,6 +2397,10 @@ async def _make_rpc_request(
23902397
)
23912398
subscription_added = True
23922399
except KeyError:
2400+
logger.error(
2401+
f"Error received from subtensor for {item_id}: {response}\n"
2402+
f"Currently received responses: {request_manager.get_results()}"
2403+
)
23932404
raise SubstrateRequestException(str(response))
23942405
(
23952406
decoded_response,
@@ -2406,6 +2417,15 @@ async def _make_rpc_request(
24062417
request_manager.add_response(
24072418
item_id, decoded_response, complete
24082419
)
2420+
if len(stringified_response := str(decoded_response)) < 2_000:
2421+
output_response = stringified_response
2422+
# avoids clogging logs up needlessly (esp for Metadata stuff)
2423+
else:
2424+
output_response = f"{stringified_response[:2_000]} (truncated)"
2425+
logger.debug(
2426+
f"Received response for item ID {item_id}:\n{output_response}\n"
2427+
f"Complete: {complete}"
2428+
)
24092429

24102430
if request_manager.is_complete:
24112431
break

0 commit comments

Comments
 (0)