From c6fd849aa35117c5d623b58c75d15a5daf62f8a5 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 28 Apr 2021 18:36:56 +0200 Subject: [PATCH] pay: Add notification for pay_success --- plugins/keysend.c | 6 +++++- plugins/libplugin-pay.c | 8 ++++++++ plugins/pay.c | 7 ++++++- tests/plugins/custom_notifications.py | 10 ++++++++++ tests/test_plugin.py | 7 ++++++- 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/plugins/keysend.c b/plugins/keysend.c index 69cf7e38b..54f5ecfa1 100644 --- a/plugins/keysend.c +++ b/plugins/keysend.c @@ -377,6 +377,10 @@ static const struct plugin_hook hooks[] = { }, }; +static const char *notification_topics[] = { + "pay_success", +}; + int main(int argc, char *argv[]) { struct feature_set features; @@ -388,5 +392,5 @@ int main(int argc, char *argv[]) plugin_main(argv, init, PLUGIN_STATIC, true, &features, commands, ARRAY_SIZE(commands), NULL, 0, hooks, ARRAY_SIZE(hooks), - NULL, 0, NULL); + notification_topics, ARRAY_SIZE(notification_topics), NULL); } diff --git a/plugins/libplugin-pay.c b/plugins/libplugin-pay.c index d8193fbad..7538022da 100644 --- a/plugins/libplugin-pay.c +++ b/plugins/libplugin-pay.c @@ -1844,6 +1844,8 @@ static void payment_finished(struct payment *p) struct json_stream *ret; struct command *cmd = p->cmd; const char *msg; + struct json_stream *n; + struct payment *root = payment_root(p); /* Either none of the leaf attempts succeeded yet, or we have a * preimage. */ @@ -1885,6 +1887,12 @@ static void payment_finished(struct payment *p) json_add_string(ret, "status", "complete"); + n = plugin_notification_start(p->plugin, "pay_success"); + json_add_sha256(n, "payment_hash", p->payment_hash); + if (root->invstring != NULL) + json_add_string(n, "bolt11", root->invstring); + plugin_notification_end(p->plugin, n); + if (command_finished(cmd, ret)) {/* Ignore result. */} return; } else if (p->aborterror != NULL) { diff --git a/plugins/pay.c b/plugins/pay.c index f5ce5f4ee..c3007dc64 100644 --- a/plugins/pay.c +++ b/plugins/pay.c @@ -2193,12 +2193,17 @@ static const struct plugin_command commands[] = { }, }; +static const char *notification_topics[] = { + "pay_success", + "pay_failure", +}; + int main(int argc, char *argv[]) { setup_locale(); plugin_main(argv, init, PLUGIN_RESTARTABLE, true, NULL, commands, ARRAY_SIZE(commands), NULL, 0, NULL, 0, - NULL, 0, + notification_topics, ARRAY_SIZE(notification_topics), plugin_option("disable-mpp", "flag", "Disable multi-part payments.", flag_option, &disablempp), diff --git a/tests/plugins/custom_notifications.py b/tests/plugins/custom_notifications.py index 092f2d2c2..a3fa9b9aa 100755 --- a/tests/plugins/custom_notifications.py +++ b/tests/plugins/custom_notifications.py @@ -24,6 +24,16 @@ def faulty_emit(plugin): plugin.notify("ididntannouncethis", "Hello world") +@plugin.subscribe("pay_success") +def on_pay_success(origin, payload, **kwargs): + plugin.log( + "Got a pay_success notification from plugin {} for payment_hash {}".format( + origin, + payload['payment_hash'] + ) + ) + + @plugin.subscribe("ididntannouncethis") def on_faulty_emit(origin, payload, **kwargs): """We should never receive this as it gets dropped. diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 4691873bb..a4584ef34 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -2408,10 +2408,15 @@ def test_custom_notification_topics(node_factory): plugin = os.path.join( os.path.dirname(__file__), "plugins", "custom_notifications.py" ) - l1 = node_factory.get_node(options={'plugin': plugin}) + l1, l2 = node_factory.line_graph(2, opts=[{'plugin': plugin}, {}]) l1.rpc.emit() l1.daemon.wait_for_log(r'Got a custom notification Hello world') + inv = l2.rpc.invoice(42, "lbl", "desc")['bolt11'] + l1.rpc.pay(inv) + + l1.daemon.wait_for_log(r'Got a pay_success notification from plugin pay for payment_hash [0-9a-f]{64}') + # And now make sure that we drop unannounced notifications l1.rpc.faulty_emit() l1.daemon.wait_for_log(