pay: Add notification for pay_success

This commit is contained in:
Christian Decker 2021-04-28 18:36:56 +02:00 committed by Rusty Russell
parent 98aa3c3da7
commit c6fd849aa3
5 changed files with 35 additions and 3 deletions

View File

@ -377,6 +377,10 @@ static const struct plugin_hook hooks[] = {
}, },
}; };
static const char *notification_topics[] = {
"pay_success",
};
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
struct feature_set features; struct feature_set features;
@ -388,5 +392,5 @@ int main(int argc, char *argv[])
plugin_main(argv, init, PLUGIN_STATIC, true, &features, commands, plugin_main(argv, init, PLUGIN_STATIC, true, &features, commands,
ARRAY_SIZE(commands), NULL, 0, hooks, ARRAY_SIZE(hooks), ARRAY_SIZE(commands), NULL, 0, hooks, ARRAY_SIZE(hooks),
NULL, 0, NULL); notification_topics, ARRAY_SIZE(notification_topics), NULL);
} }

View File

@ -1844,6 +1844,8 @@ static void payment_finished(struct payment *p)
struct json_stream *ret; struct json_stream *ret;
struct command *cmd = p->cmd; struct command *cmd = p->cmd;
const char *msg; 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 /* Either none of the leaf attempts succeeded yet, or we have a
* preimage. */ * preimage. */
@ -1885,6 +1887,12 @@ static void payment_finished(struct payment *p)
json_add_string(ret, "status", "complete"); 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. */} if (command_finished(cmd, ret)) {/* Ignore result. */}
return; return;
} else if (p->aborterror != NULL) { } else if (p->aborterror != NULL) {

View File

@ -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[]) int main(int argc, char *argv[])
{ {
setup_locale(); setup_locale();
plugin_main(argv, init, PLUGIN_RESTARTABLE, true, NULL, commands, plugin_main(argv, init, PLUGIN_RESTARTABLE, true, NULL, commands,
ARRAY_SIZE(commands), NULL, 0, NULL, 0, ARRAY_SIZE(commands), NULL, 0, NULL, 0,
NULL, 0, notification_topics, ARRAY_SIZE(notification_topics),
plugin_option("disable-mpp", "flag", plugin_option("disable-mpp", "flag",
"Disable multi-part payments.", "Disable multi-part payments.",
flag_option, &disablempp), flag_option, &disablempp),

View File

@ -24,6 +24,16 @@ def faulty_emit(plugin):
plugin.notify("ididntannouncethis", "Hello world") 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") @plugin.subscribe("ididntannouncethis")
def on_faulty_emit(origin, payload, **kwargs): def on_faulty_emit(origin, payload, **kwargs):
"""We should never receive this as it gets dropped. """We should never receive this as it gets dropped.

View File

@ -2408,10 +2408,15 @@ def test_custom_notification_topics(node_factory):
plugin = os.path.join( plugin = os.path.join(
os.path.dirname(__file__), "plugins", "custom_notifications.py" 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.rpc.emit()
l1.daemon.wait_for_log(r'Got a custom notification Hello world') 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 # And now make sure that we drop unannounced notifications
l1.rpc.faulty_emit() l1.rpc.faulty_emit()
l1.daemon.wait_for_log( l1.daemon.wait_for_log(