From 82e8db4ba48bb9b77a2a160f3b1b186a09ac7a31 Mon Sep 17 00:00:00 2001 From: trueptolemy <823220586@qq.com> Date: Tue, 25 Jun 2019 15:45:16 +0800 Subject: [PATCH] plugin: Another new notification type, 'sendpay_failure' (The json when sendpay successes is too different when sendpay fails, so divide the sendpay result into two notifications: `sendpay_success` and `sendpay_failure`) `sendpay_failure` A notification for topic `sendpay_failure` is sent every time a sendpay success(with `failed` status). The json is same as the return value of command `sendpay`/`waitsendpay` when this cammand fails. ```json { "sendpay_failure": { "code": 204, "message": "failed: WIRE_UNKNOWN_NEXT_PEER (reply from remote)", "data": { "id": 2, "payment_hash": "9036e3bdbd2515f1e653cb9f22f8e4c49b73aa2c36e937c926f43e33b8db8851", "destination": "035d2b1192dfba134e10e540875d366ebc8bc353d5aa766b80c090b39c3a5d885d", "msatoshi": 100000000, "amount_msat": "100000000msat", "msatoshi_sent": 100001001, "amount_sent_msat": "100001001msat", "created_at": 1561395134, "status": "failed", "erring_index": 1, "failcode": 16394, "failcodename": "WIRE_UNKNOWN_NEXT_PEER", "erring_node": "022d223620a359a47ff7f7ac447c85c46c923da53389221a0054c11c1e3ca31d59", "erring_channel": "103x2x1", "erring_direction": 0 } } } ``` `sendpay` doesn't wait for the result of sendpay and `waitsendpay` returns the result of sendpay in specified time or timeout, but `sendpay_failure` will always return the result anytime when sendpay fails if is was subscribed. --- lightningd/notification.c | 49 +++++++++++++++++++++++++++++++++++++++ lightningd/notification.h | 7 ++++++ 2 files changed, 56 insertions(+) diff --git a/lightningd/notification.c b/lightningd/notification.c index 30f031ee1..4877805d5 100644 --- a/lightningd/notification.c +++ b/lightningd/notification.c @@ -251,3 +251,52 @@ void notify_sendpay_success(struct lightningd *ld, jsonrpc_notification_end(n); plugins_notify(ld->plugins, take(n)); } + +static void sendpay_failure_notification_serialize(struct json_stream *stream, + const struct wallet_payment *payment, + int pay_errcode, + const u8 *onionreply, + const struct routing_failure *fail, + char *errmsg) +{ + json_object_start(stream, "sendpay_failure"); + + /* In line with the format of json error returned + * by sendpay_fail(). */ + json_add_member(stream, "code", false, "%d", pay_errcode); + json_add_string(stream, "message", errmsg); + + json_object_start(stream, "data"); + json_sendpay_fail_fields(stream, + payment, + pay_errcode, + onionreply, + fail); + + json_object_end(stream); /* .data */ + json_object_end(stream); /* .sendpay_failure */ +} + +REGISTER_NOTIFICATION(sendpay_failure, + sendpay_failure_notification_serialize); + +void notify_sendpay_failure(struct lightningd *ld, + const struct wallet_payment *payment, + int pay_errcode, + const u8 *onionreply, + const struct routing_failure *fail, + char *errmsg) +{ + void (*serialize)(struct json_stream *, + const struct wallet_payment *, + int, + const u8 *, + const struct routing_failure *, + char *) = sendpay_failure_notification_gen.serialize; + + struct jsonrpc_notification *n = + jsonrpc_notification_start(NULL, "sendpay_failure"); + serialize(n->stream, payment, pay_errcode, onionreply, fail, errmsg); + jsonrpc_notification_end(n); + plugins_notify(ld->plugins, take(n)); +} diff --git a/lightningd/notification.h b/lightningd/notification.h index 9a57f41e7..95026d08e 100644 --- a/lightningd/notification.h +++ b/lightningd/notification.h @@ -58,4 +58,11 @@ void notify_forward_event(struct lightningd *ld, void notify_sendpay_success(struct lightningd *ld, const struct wallet_payment *payment); +void notify_sendpay_failure(struct lightningd *ld, + const struct wallet_payment *payment, + int pay_errcode, + const u8 *onionreply, + const struct routing_failure *fail, + char *errmsg); + #endif /* LIGHTNING_LIGHTNINGD_NOTIFICATION_H */