mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-15 11:59:16 +01:00
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: https://github.com/ElementsProject/lightning/issues/6848 Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
f2f6c73977
commit
8b84c8945b
2 changed files with 45 additions and 1 deletions
|
@ -1,5 +1,6 @@
|
|||
import json5
|
||||
import ipaddress
|
||||
import pyln.client
|
||||
|
||||
|
||||
CERTS_PATH, REST_PROTOCOL, REST_HOST, REST_PORT, REST_CSP, REST_CORS_ORIGINS = "", "", "", "", "", []
|
||||
|
@ -64,8 +65,29 @@ def set_config(options):
|
|||
return None
|
||||
|
||||
|
||||
def convert_millisatoshis(item):
|
||||
"""
|
||||
The global JSON encoder has been replaced (see
|
||||
monkey_patch_json!) by one that turns Millisatoshi class object
|
||||
into strings ending in msat. We do not want the http response
|
||||
to be encoded like that! pyln-client should probably not do that,
|
||||
but meanwhile, convert them to integers.
|
||||
"""
|
||||
if isinstance(item, dict):
|
||||
ret = {}
|
||||
for k in item:
|
||||
ret[k] = convert_millisatoshis(item[k])
|
||||
elif isinstance(item, list):
|
||||
ret = [convert_millisatoshis(i) for i in item]
|
||||
elif isinstance(item, pyln.client.Millisatoshi):
|
||||
ret = int(item)
|
||||
else:
|
||||
ret = item
|
||||
return ret
|
||||
|
||||
|
||||
def call_rpc_method(plugin, rpc_method, payload):
|
||||
return plugin.rpc.call(rpc_method, payload)
|
||||
return convert_millisatoshis(plugin.rpc.call(rpc_method, payload))
|
||||
|
||||
|
||||
def verify_rune(plugin, rune, rpc_method, rpc_params):
|
||||
|
|
|
@ -225,6 +225,28 @@ def test_clnrest_rpc_method(node_factory):
|
|||
assert 'bolt11' in response.json()
|
||||
|
||||
|
||||
def test_clnrest_large_response(node_factory):
|
||||
"""Test a large reply still works (and msat fields are integers!)"""
|
||||
# start a node with clnrest
|
||||
l1, base_url, ca_cert = start_node_with_clnrest(node_factory)
|
||||
http_session = http_session_with_retry()
|
||||
|
||||
# Add 500 invoices, test list
|
||||
NUM_INVOICES = 500
|
||||
for i in range(NUM_INVOICES):
|
||||
l1.rpc.invoice(amount_msat=100, label=str(i), description="inv")
|
||||
|
||||
rune = l1.rpc.createrune()['rune']
|
||||
response = http_session.post(base_url + '/v1/listinvoices', headers={'Rune': rune},
|
||||
verify=ca_cert)
|
||||
# No, this doesn't return JSON, it *parses* it into a Python object!
|
||||
resp = response.json()
|
||||
|
||||
# Make sure it hasn't turned msat fields into strings!
|
||||
assert not isinstance(resp['invoices'][0]['amount_msat'], Millisatoshi)
|
||||
assert len(resp['invoices']) == NUM_INVOICES
|
||||
|
||||
|
||||
# Tests for websocket are written separately to avoid flake8
|
||||
# to complain with the errors F811 like this "F811 redefinition of
|
||||
# unused 'message'".
|
||||
|
|
Loading…
Add table
Reference in a new issue