Skip to content

Commit 8b84c89

Browse files
rustyrussellnepet
authored andcommitted
clnrest: don't convert *_msat fields to strings.
We have a global JSON encoder hack, which means that any field ending in msat gets special treatment (so we can safely talk to lightningd, even if a field expects satoshi amounts, we are explicit). However, requests uses the JSON parser and DOES NOT want this conversion when sending it out as an HTTP response! The simplest local fix we could find was Shahana's suggestion to iterate and covert away from Millisatoshi(): the reverse of what our JSON encoder does. Fixes: #6848 Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent f2f6c73 commit 8b84c89

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

plugins/clnrest/utilities/shared.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json5
22
import ipaddress
3+
import pyln.client
34

45

56
CERTS_PATH, REST_PROTOCOL, REST_HOST, REST_PORT, REST_CSP, REST_CORS_ORIGINS = "", "", "", "", "", []
@@ -64,8 +65,29 @@ def set_config(options):
6465
return None
6566

6667

68+
def convert_millisatoshis(item):
69+
"""
70+
The global JSON encoder has been replaced (see
71+
monkey_patch_json!) by one that turns Millisatoshi class object
72+
into strings ending in msat. We do not want the http response
73+
to be encoded like that! pyln-client should probably not do that,
74+
but meanwhile, convert them to integers.
75+
"""
76+
if isinstance(item, dict):
77+
ret = {}
78+
for k in item:
79+
ret[k] = convert_millisatoshis(item[k])
80+
elif isinstance(item, list):
81+
ret = [convert_millisatoshis(i) for i in item]
82+
elif isinstance(item, pyln.client.Millisatoshi):
83+
ret = int(item)
84+
else:
85+
ret = item
86+
return ret
87+
88+
6789
def call_rpc_method(plugin, rpc_method, payload):
68-
return plugin.rpc.call(rpc_method, payload)
90+
return convert_millisatoshis(plugin.rpc.call(rpc_method, payload))
6991

7092

7193
def verify_rune(plugin, rune, rpc_method, rpc_params):

tests/test_clnrest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,28 @@ def test_clnrest_rpc_method(node_factory):
225225
assert 'bolt11' in response.json()
226226

227227

228+
def test_clnrest_large_response(node_factory):
229+
"""Test a large reply still works (and msat fields are integers!)"""
230+
# start a node with clnrest
231+
l1, base_url, ca_cert = start_node_with_clnrest(node_factory)
232+
http_session = http_session_with_retry()
233+
234+
# Add 500 invoices, test list
235+
NUM_INVOICES = 500
236+
for i in range(NUM_INVOICES):
237+
l1.rpc.invoice(amount_msat=100, label=str(i), description="inv")
238+
239+
rune = l1.rpc.createrune()['rune']
240+
response = http_session.post(base_url + '/v1/listinvoices', headers={'Rune': rune},
241+
verify=ca_cert)
242+
# No, this doesn't return JSON, it *parses* it into a Python object!
243+
resp = response.json()
244+
245+
# Make sure it hasn't turned msat fields into strings!
246+
assert not isinstance(resp['invoices'][0]['amount_msat'], Millisatoshi)
247+
assert len(resp['invoices']) == NUM_INVOICES
248+
249+
228250
# Tests for websocket are written separately to avoid flake8
229251
# to complain with the errors F811 like this "F811 redefinition of
230252
# unused 'message'".

0 commit comments

Comments
 (0)