From 0b23133ab20c2e05a162e4bfa16e855e3429c570 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 21 Nov 2023 11:09:07 +1030 Subject: [PATCH] lightningd: don't print out notification msat fields as strings. Reported-by: Shahana Farooqui Changelog-Fixed: JSON-RPC: Plugin notification `msat` fields in `invoice_payment` and `invoice_created` hooks now a number, not a string with "msat" suffix. Changelog-Fixed: JSON-RPC: Plugin hook `payment` `msat` field is now a number, not a string with "msat" suffix. --- common/json_stream.c | 2 +- lightningd/invoice.c | 4 +--- lightningd/notification.c | 7 ++----- tests/test_clnrest.py | 1 - tests/test_plugin.py | 4 ++-- 5 files changed, 6 insertions(+), 12 deletions(-) diff --git a/common/json_stream.c b/common/json_stream.c index 24ccf1291..59c7e8957 100644 --- a/common/json_stream.c +++ b/common/json_stream.c @@ -614,7 +614,7 @@ void json_add_amount_msat(struct json_stream *result, const char *msatfieldname, struct amount_msat msat) { - assert(strends(msatfieldname, "_msat")); + assert(strends(msatfieldname, "_msat") || streq(msatfieldname, "msat")); json_add_u64(result, msatfieldname, msat.millisatoshis); /* Raw: low-level helper */ } diff --git a/lightningd/invoice.c b/lightningd/invoice.c index cf240e628..25df0a172 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -223,9 +223,7 @@ invoice_payment_serialize(struct invoice_payment_hook_payload *payload, json_object_start(stream, "payment"); json_add_escaped_string(stream, "label", payload->label); json_add_preimage(stream, "preimage", &payload->preimage); - json_add_string(stream, "msat", - type_to_string(tmpctx, struct amount_msat, - &payload->msat)); + json_add_amount_msat(stream, "msat", payload->msat); if (payload->ld->developer && payload->set) invoice_payment_add_tlvs(stream, payload->set); diff --git a/lightningd/notification.c b/lightningd/notification.c index 9893bac93..bbefa48ff 100644 --- a/lightningd/notification.c +++ b/lightningd/notification.c @@ -165,8 +165,7 @@ static void invoice_payment_notification_serialize(struct json_stream *stream, const struct bitcoin_outpoint *outpoint) { json_object_start(stream, "invoice_payment"); - json_add_string(stream, "msat", - type_to_string(tmpctx, struct amount_msat, &amount)); + json_add_amount_msat(stream, "msat", amount); json_add_hex(stream, "preimage", &preimage, sizeof(preimage)); if (outpoint) json_add_outpoint(stream, "outpoint", outpoint); @@ -201,9 +200,7 @@ static void invoice_creation_notification_serialize(struct json_stream *stream, { json_object_start(stream, "invoice_creation"); if (amount != NULL) - json_add_string( - stream, "msat", - type_to_string(tmpctx, struct amount_msat, amount)); + json_add_amount_msat(stream, "msat", *amount); json_add_hex(stream, "preimage", &preimage, sizeof(preimage)); json_add_escaped_string(stream, "label", label); diff --git a/tests/test_clnrest.py b/tests/test_clnrest.py index 2e42315e7..e77c87e88 100644 --- a/tests/test_clnrest.py +++ b/tests/test_clnrest.py @@ -375,7 +375,6 @@ def test_clnrest_websocket_rune_no_listnotifications(node_factory): assert len([n for n in notifications if n.find('invoice_creation') > 0]) == 0 -@pytest.mark.xfail(strict=True) def test_clnrest_numeric_msat_notification(node_factory): """Test that msat fields are integers in notifications also.""" # start a node with clnrest diff --git a/tests/test_plugin.py b/tests/test_plugin.py index e86bc18df..7ec59adeb 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -1269,7 +1269,7 @@ def test_invoice_payment_notification(node_factory): l1.dev_pay(inv1['bolt11'], dev_use_shadow=False) l2.daemon.wait_for_log(r"Received invoice_payment event for label {}," - " preimage {}, and amount of {}msat" + " preimage {}, and amount of {}" .format(label, preimage, msats)) @@ -1286,7 +1286,7 @@ def test_invoice_creation_notification(node_factory): l2.rpc.invoice(msats, label, 'description', preimage=preimage) l2.daemon.wait_for_log(r"Received invoice_creation event for label {}," - " preimage {}, and amount of {}msat" + " preimage {}, and amount of {}" .format(label, preimage, msats))