From 507f8d46df83cdd9233b61843c49356c3cd05970 Mon Sep 17 00:00:00 2001 From: trueptolemy <823220586@qq.com> Date: Tue, 25 Jun 2019 15:55:09 +0800 Subject: [PATCH] pay: Warp the json process of payment fail field We will also call this warped function in the json process of the 'sendpay_failure' notification. --- lightningd/pay.c | 49 ++++++++++++++++++++++++++++++------------------ lightningd/pay.h | 8 ++++++++ 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/lightningd/pay.c b/lightningd/pay.c index 1b594a2b1..38a6035cf 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -141,6 +141,23 @@ json_add_routefail_info(struct json_stream *js, json_add_hex_talarr(js, "raw_message", msg); } +void json_sendpay_fail_fields(struct json_stream *js, + int pay_errcode, + const u8 *onionreply, + const struct routing_failure *fail) +{ + if (pay_errcode == PAY_UNPARSEABLE_ONION) + json_add_hex_talarr(js, "onionreply", onionreply); + else + json_add_routefail_info(js, + fail->erring_index, + fail->failcode, + &fail->erring_node, + &fail->erring_channel, + fail->channel_dir, + fail->msg); +} + /* onionreply used if pay_errcode == PAY_UNPARSEABLE_ONION */ static struct command_result * sendpay_fail(struct command *cmd, @@ -150,27 +167,23 @@ sendpay_fail(struct command *cmd, const char *details) { struct json_stream *data; - - if (pay_errcode == PAY_UNPARSEABLE_ONION) { - data = json_stream_fail(cmd, PAY_UNPARSEABLE_ONION, - "Malformed error reply"); - json_add_hex_talarr(data, "onionreply", onionreply); - json_object_end(data); - return command_failed(cmd, data); + char *errmsg; + + if (pay_errcode == PAY_UNPARSEABLE_ONION) + errmsg = "Malformed error reply"; + else { + assert(fail); + errmsg = tal_fmt(tmpctx, "failed: %s (%s)", + onion_type_name(fail->failcode), + details); } - assert(fail); data = json_stream_fail(cmd, pay_errcode, - tal_fmt(tmpctx, "failed: %s (%s)", - onion_type_name(fail->failcode), - details)); - json_add_routefail_info(data, - fail->erring_index, - fail->failcode, - &fail->erring_node, - &fail->erring_channel, - fail->channel_dir, - fail->msg); + errmsg); + json_sendpay_fail_fields(data, + pay_errcode, + onionreply, + fail); json_object_end(data); return command_failed(cmd, data); } diff --git a/lightningd/pay.h b/lightningd/pay.h index 2a9a4d351..f5ca389fb 100644 --- a/lightningd/pay.h +++ b/lightningd/pay.h @@ -1,6 +1,7 @@ #ifndef LIGHTNING_LIGHTNINGD_PAY_H #define LIGHTNING_LIGHTNINGD_PAY_H #include "config.h" +#include struct htlc_out; struct lightningd; @@ -8,6 +9,7 @@ struct preimage; struct sha256; struct json_stream; struct wallet_payment; +struct routing_failure; void payment_succeeded(struct lightningd *ld, struct htlc_out *hout, const struct preimage *rval); @@ -22,4 +24,10 @@ void payment_store(struct lightningd *ld, const struct sha256 *payment_hash); void json_add_payment_fields(struct json_stream *response, const struct wallet_payment *t); +/* This json will be also used in 'sendpay_failure' notifictaion. */ +void json_sendpay_fail_fields(struct json_stream *js, + int pay_errcode, + const u8 *onionreply, + const struct routing_failure *fail); + #endif /* LIGHTNING_LIGHTNINGD_PAY_H */