Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin: Allow plugins to publish and subscribe to custom notifications #4496

Merged
merged 17 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugins/keysend.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ static const struct plugin_hook hooks[] = {

static const char *notification_topics[] = {
"pay_success",
"pay_failure",
};

int main(int argc, char *argv[])
Expand Down
25 changes: 24 additions & 1 deletion plugins/libplugin-pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,21 @@ static void payment_json_add_attempts(struct json_stream *s,
json_array_end(s);
}

static void payment_notify_failure(struct payment *p, const char *error_message) {
struct payment *root = payment_root(p);
struct json_stream *n;
n = plugin_notification_start(p->plugin, "pay_failure");
Comment on lines +1837 to +1840
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: genuinely weird indent?

json_add_sha256(n, "payment_hash", p->payment_hash);
if (root->invstring != NULL)
json_add_string(n, "bolt11", root->invstring);

json_object_start(n, "error");
json_add_string(n, "message", error_message);
json_object_end(n); /* .error */

plugin_notification_end(p->plugin, n);
}

/* This function is called whenever a payment ends up in a final state, or all
* leafs in the subtree rooted in the payment are all in a final state. It is
* called only once, and it is guaranteed to be called in post-order
Expand Down Expand Up @@ -1901,6 +1916,9 @@ static void payment_finished(struct payment *p)
ret = jsonrpc_stream_fail(cmd, PAY_STOPPED_RETRYING,
p->aborterror);
payment_json_add_attempts(ret, "attempts", p);

payment_notify_failure(p, p->aborterror);

if (command_finished(cmd, ret)) {/* Ignore result. */}
return;
} else if (result.failure == NULL || result.failure->failcode < NODE) {
Expand All @@ -1913,6 +1931,9 @@ static void payment_finished(struct payment *p)
ret = jsonrpc_stream_fail(cmd, PAY_STOPPED_RETRYING,
msg);
payment_json_add_attempts(ret, "attempts", p);

payment_notify_failure(p, msg);

if (command_finished(cmd, ret)) {/* Ignore result. */}
return;

Expand Down Expand Up @@ -1978,7 +1999,9 @@ static void payment_finished(struct payment *p)
*failure->erring_direction);
}

if (command_finished(cmd, ret)) {/* Ignore result. */}
payment_notify_failure(p, failure->message);

if (command_finished(cmd, ret)) { /* Ignore result. */}
return;
}
} else {
Expand Down