From 07f85cbf7262d427dbe60b7a3f0a50617b076be4 Mon Sep 17 00:00:00 2001 From: trueptolemy <823220586@qq.com> Date: Tue, 25 Jun 2019 15:27:35 +0800 Subject: [PATCH] plugin: A new notification type, 'sendpay_success' `sendpay_success` A notification for topic `sendpay_success` is sent every time a sendpay success(with `complete` status). The json is same as the return value of command `sendpay`/`waitsendpay` when these cammand succeeds. ```json { "sendpay_success": { "id": 1, "payment_hash": "5c85bf402b87d4860f4a728e2e58a2418bda92cd7aea0ce494f11670cfbfb206", "destination": "035d2b1192dfba134e10e540875d366ebc8bc353d5aa766b80c090b39c3a5d885d", "msatoshi": 100000000, "amount_msat": "100000000msat", "msatoshi_sent": 100001001, "amount_sent_msat": "100001001msat", "created_at": 1561390572, "status": "complete", "payment_preimage": "9540d98095fd7f37687ebb7759e733934234d4f934e34433d4998a37de3733ee" } } ``` `sendpay` doesn't wait for the result of sendpay and `waitsendpay` returns the result of sendpay in specified time or timeout, but `sendpay_success` will always return the result anytime when sendpay successes if is was subscribed. --- lightningd/notification.c | 24 ++++++++++++++++++++++++ lightningd/notification.h | 4 ++++ lightningd/pay.c | 5 ++--- lightningd/pay.h | 6 ++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lightningd/notification.c b/lightningd/notification.c index f17750287..30f031ee1 100644 --- a/lightningd/notification.c +++ b/lightningd/notification.c @@ -227,3 +227,27 @@ void notify_forward_event(struct lightningd *ld, jsonrpc_notification_end(n); plugins_notify(ld->plugins, take(n)); } + +static void sendpay_success_notification_serialize(struct json_stream *stream, + const struct wallet_payment *payment) +{ + json_object_start(stream, "sendpay_success"); + json_add_payment_fields(stream, payment); + json_object_end(stream); /* .sendpay_success */ +} + +REGISTER_NOTIFICATION(sendpay_success, + sendpay_success_notification_serialize); + +void notify_sendpay_success(struct lightningd *ld, + const struct wallet_payment *payment) +{ + void (*serialize)(struct json_stream *, + const struct wallet_payment *) = sendpay_success_notification_gen.serialize; + + struct jsonrpc_notification *n = + jsonrpc_notification_start(NULL, "sendpay_success"); + serialize(n->stream, payment); + jsonrpc_notification_end(n); + plugins_notify(ld->plugins, take(n)); +} diff --git a/lightningd/notification.h b/lightningd/notification.h index cf97d81bf..9a57f41e7 100644 --- a/lightningd/notification.h +++ b/lightningd/notification.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -54,4 +55,7 @@ void notify_forward_event(struct lightningd *ld, enum onion_type failcode, struct timeabs *resolved_time); +void notify_sendpay_success(struct lightningd *ld, + const struct wallet_payment *payment); + #endif /* LIGHTNING_LIGHTNINGD_NOTIFICATION_H */ diff --git a/lightningd/pay.c b/lightningd/pay.c index ff52b9aa2..1b594a2b1 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -73,9 +73,8 @@ add_waitsendpay_waiter(struct lightningd *ld, } /* Outputs fields, not a separate object*/ -static void -json_add_payment_fields(struct json_stream *response, - const struct wallet_payment *t) +void json_add_payment_fields(struct json_stream *response, + const struct wallet_payment *t) { json_add_u64(response, "id", t->id); json_add_sha256(response, "payment_hash", &t->payment_hash); diff --git a/lightningd/pay.h b/lightningd/pay.h index a3edef862..2a9a4d351 100644 --- a/lightningd/pay.h +++ b/lightningd/pay.h @@ -6,6 +6,8 @@ struct htlc_out; struct lightningd; struct preimage; struct sha256; +struct json_stream; +struct wallet_payment; void payment_succeeded(struct lightningd *ld, struct htlc_out *hout, const struct preimage *rval); @@ -16,4 +18,8 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout, /* Inform payment system to save the payment. */ void payment_store(struct lightningd *ld, const struct sha256 *payment_hash); +/* This json will be also used in 'sendpay_success' notifictaion. */ +void json_add_payment_fields(struct json_stream *response, + const struct wallet_payment *t); + #endif /* LIGHTNING_LIGHTNINGD_PAY_H */