From ea67710e01978047c1f85e6d27b95a3b37360a23 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 25 Jan 2021 12:35:16 +0100 Subject: [PATCH] pyln: Pretty print RPC calls in the testing framework We are printing `repr(obj)` which is not pretty-printed, hard to read, and can't even be copied and inspected to JSON tools. We now print the JSONified and indented calls and responses for easier debugging based on solely the logs (useful for CI!). Changelog-Added: pyln-testing: The RPC client will now pretty-print requests and responses to facilitate log-based debugging. --- contrib/pyln-testing/pyln/testing/utils.py | 26 +++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/contrib/pyln-testing/pyln/testing/utils.py b/contrib/pyln-testing/pyln/testing/utils.py index 86c52b590..905cd1262 100644 --- a/contrib/pyln-testing/pyln/testing/utils.py +++ b/contrib/pyln-testing/pyln/testing/utils.py @@ -592,6 +592,30 @@ class LightningD(TailableProc): return self.proc.returncode +class PrettyPrintingLightningRpc(LightningRpc): + """A version of the LightningRpc that pretty-prints calls and results. + + Useful when debugging based on logs, and less painful to the + eyes. It has some overhead since we re-serialize the request and + result to json in order to pretty print it. + + """ + + def call(self, method, payload=None): + id = self.next_id + self.logger.debug(json.dumps({ + "id": id, + "method": method, + "params": payload + }, indent=2)) + res = LightningRpc.call(self, method, payload) + self.logger.debug(json.dumps({ + "id": id, + "result": res + }, indent=2)) + return res + + class LightningNode(object): def __init__(self, node_id, lightning_dir, bitcoind, executor, valgrind, may_fail=False, may_reconnect=False, allow_broken_log=False, @@ -609,7 +633,7 @@ class LightningNode(object): self.rc = 0 socket_path = os.path.join(lightning_dir, TEST_NETWORK, "lightning-rpc").format(node_id) - self.rpc = LightningRpc(socket_path, self.executor) + self.rpc = PrettyPrintingLightningRpc(socket_path, self.executor) self.daemon = LightningD( lightning_dir, bitcoindproxy=bitcoind.get_proxy(),