Skip to content

Commit 62e00a1

Browse files
committed
Applied raw websocket logger to async substrate interface as well.
1 parent 0ed3af2 commit 62e00a1

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
ResultHandler = Callable[[dict, Any], Awaitable[tuple[dict, bool]]]
7676

7777
logger = logging.getLogger("async_substrate_interface")
78+
raw_websocket_logger = logging.getLogger("raw_websocket")
7879

7980

8081
class AsyncExtrinsicReceipt:
@@ -505,6 +506,7 @@ def __init__(
505506
max_connections=100,
506507
shutdown_timer=5,
507508
options: Optional[dict] = None,
509+
_log_raw_websockets: bool = False,
508510
):
509511
"""
510512
Websocket manager object. Allows for the use of a single websocket connection by multiple
@@ -532,6 +534,8 @@ def __init__(
532534
self._exit_task = None
533535
self._open_subscriptions = 0
534536
self._options = options if options else {}
537+
self._log_raw_websockets = _log_raw_websockets
538+
535539
try:
536540
now = asyncio.get_running_loop().time()
537541
except RuntimeError:
@@ -615,7 +619,10 @@ async def shutdown(self):
615619
async def _recv(self) -> None:
616620
try:
617621
# TODO consider wrapping this in asyncio.wait_for and use that for the timeout logic
618-
response = json.loads(await self.ws.recv(decode=False))
622+
recd = await self.ws.recv(decode=False)
623+
if self._log_raw_websockets:
624+
raw_websocket_logger.debug(f"WEBSOCKET_RECEIVE> {recd.decode()}")
625+
response = json.loads()
619626
self.last_received = await self.loop_time()
620627
async with self._lock:
621628
# note that these 'subscriptions' are all waiting sent messages which have not received
@@ -660,7 +667,10 @@ async def send(self, payload: dict) -> int:
660667
# self._open_subscriptions += 1
661668
await self.max_subscriptions.acquire()
662669
try:
663-
await self.ws.send(json.dumps({**payload, **{"id": original_id}}))
670+
to_send = {**payload, **{"id": original_id}}
671+
if self._log_raw_websockets:
672+
raw_websocket_logger.debug(f"WEBSOCKET_SEND> {to_send}")
673+
await self.ws.send(json.dumps(to_send))
664674
self.last_sent = await self.loop_time()
665675
return original_id
666676
except (ConnectionClosed, ssl.SSLError, EOFError):
@@ -699,6 +709,7 @@ def __init__(
699709
max_retries: int = 5,
700710
retry_timeout: float = 60.0,
701711
_mock: bool = False,
712+
_log_raw_websockets: bool = False,
702713
):
703714
"""
704715
The asyncio-compatible version of the subtensor interface commands we use in bittensor. It is important to
@@ -716,16 +727,19 @@ def __init__(
716727
max_retries: number of times to retry RPC requests before giving up
717728
retry_timeout: how to long wait since the last ping to retry the RPC request
718729
_mock: whether to use mock version of the subtensor interface
730+
_log_raw_websockets: whether to log raw websocket requests during RPC requests
719731
720732
"""
721733
self.max_retries = max_retries
722734
self.retry_timeout = retry_timeout
723735
self.chain_endpoint = url
724736
self.url = url
725737
self._chain = chain_name
738+
self._log_raw_websockets = _log_raw_websockets
726739
if not _mock:
727740
self.ws = Websocket(
728741
url,
742+
_log_raw_websockets=_log_raw_websockets,
729743
options={
730744
"max_size": self.ws_max_size,
731745
"write_limit": 2**16,

async_substrate_interface/substrate_addons.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ def __init__(
262262
max_retries: int = 5,
263263
retry_timeout: float = 60.0,
264264
_mock: bool = False,
265+
_log_raw_websockets: bool = False,
265266
archive_nodes: Optional[list[str]] = None,
266267
):
267268
fallback_chains = fallback_chains or []
@@ -289,6 +290,7 @@ def __init__(
289290
_mock=_mock,
290291
retry_timeout=retry_timeout,
291292
max_retries=max_retries,
293+
_log_raw_websockets=_log_raw_websockets,
292294
)
293295
self._original_methods = {
294296
method: getattr(self, method) for method in RETRY_METHODS

0 commit comments

Comments
 (0)