Skip to content

Commit 10d3443

Browse files
committed
Adds ability to log raw websockets for debugging.
1 parent e0b7aa3 commit 10d3443

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

async_substrate_interface/substrate_addons.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ def __init__(
117117
max_retries: int = 5,
118118
retry_timeout: float = 60.0,
119119
_mock: bool = False,
120+
_log_raw_websockets: bool = False,
120121
archive_nodes: Optional[list[str]] = None,
121122
):
122123
fallback_chains = fallback_chains or []
@@ -150,6 +151,7 @@ def __init__(
150151
_mock=_mock,
151152
retry_timeout=retry_timeout,
152153
max_retries=max_retries,
154+
_log_raw_websockets=_log_raw_websockets,
153155
)
154156
initialized = True
155157
logger.info(f"Connected to {chain_url}")

async_substrate_interface/sync_substrate.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
ResultHandler = Callable[[dict, Any], tuple[dict, bool]]
5454

5555
logger = logging.getLogger("async_substrate_interface")
56+
raw_websocket_logger = logging.getLogger("raw_websocket")
5657

5758

5859
class ExtrinsicReceipt:
@@ -485,6 +486,7 @@ def __init__(
485486
max_retries: int = 5,
486487
retry_timeout: float = 60.0,
487488
_mock: bool = False,
489+
_log_raw_websockets: bool = False,
488490
):
489491
"""
490492
The sync compatible version of the subtensor interface commands we use in bittensor. Use this instance only
@@ -501,6 +503,7 @@ def __init__(
501503
max_retries: number of times to retry RPC requests before giving up
502504
retry_timeout: how to long wait since the last ping to retry the RPC request
503505
_mock: whether to use mock version of the subtensor interface
506+
_log_raw_websockets: whether to log raw websocket requests during RPC requests
504507
505508
"""
506509
self.max_retries = max_retries
@@ -527,6 +530,7 @@ def __init__(
527530
self.registry_type_map = {}
528531
self.type_id_to_name = {}
529532
self._mock = _mock
533+
self.log_raw_websockets = _log_raw_websockets
530534
if not _mock:
531535
self.ws = self.connect(init=True)
532536
self.initialize()
@@ -1831,12 +1835,18 @@ def _make_rpc_request(
18311835
ws = self.connect(init=False if attempt == 1 else True)
18321836
for payload in payloads:
18331837
item_id = get_next_id()
1834-
ws.send(json.dumps({**payload["payload"], **{"id": item_id}}))
1838+
to_send = {**payload["payload"], **{"id": item_id}}
1839+
if self.log_raw_websockets:
1840+
raw_websocket_logger.debug(f"WEBSOCKET_SEND> {to_send}")
1841+
ws.send(json.dumps(to_send))
18351842
request_manager.add_request(item_id, payload["id"])
18361843

18371844
while True:
18381845
try:
1839-
response = json.loads(ws.recv(timeout=self.retry_timeout, decode=False))
1846+
recd = ws.recv(timeout=self.retry_timeout, decode=False)
1847+
if self.log_raw_websockets:
1848+
raw_websocket_logger.debug(f"WEBSOCKET_RECEIVE> {recd.decode()}")
1849+
response = json.loads(recd)
18401850
except (TimeoutError, ConnectionClosed):
18411851
if attempt >= self.max_retries:
18421852
logger.warning(

0 commit comments

Comments
 (0)