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.
This commit is contained in:
trueptolemy 2019-06-25 15:55:09 +08:00 committed by Rusty Russell
parent 07f85cbf72
commit 507f8d46df
2 changed files with 38 additions and 17 deletions

View File

@ -141,6 +141,23 @@ json_add_routefail_info(struct json_stream *js,
json_add_hex_talarr(js, "raw_message", msg); 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 */ /* onionreply used if pay_errcode == PAY_UNPARSEABLE_ONION */
static struct command_result * static struct command_result *
sendpay_fail(struct command *cmd, sendpay_fail(struct command *cmd,
@ -150,27 +167,23 @@ sendpay_fail(struct command *cmd,
const char *details) const char *details)
{ {
struct json_stream *data; struct json_stream *data;
char *errmsg;
if (pay_errcode == PAY_UNPARSEABLE_ONION) { if (pay_errcode == PAY_UNPARSEABLE_ONION)
data = json_stream_fail(cmd, PAY_UNPARSEABLE_ONION, errmsg = "Malformed error reply";
"Malformed error reply"); else {
json_add_hex_talarr(data, "onionreply", onionreply); assert(fail);
json_object_end(data); errmsg = tal_fmt(tmpctx, "failed: %s (%s)",
return command_failed(cmd, data); onion_type_name(fail->failcode),
details);
} }
assert(fail);
data = json_stream_fail(cmd, pay_errcode, data = json_stream_fail(cmd, pay_errcode,
tal_fmt(tmpctx, "failed: %s (%s)", errmsg);
onion_type_name(fail->failcode), json_sendpay_fail_fields(data,
details)); pay_errcode,
json_add_routefail_info(data, onionreply,
fail->erring_index, fail);
fail->failcode,
&fail->erring_node,
&fail->erring_channel,
fail->channel_dir,
fail->msg);
json_object_end(data); json_object_end(data);
return command_failed(cmd, data); return command_failed(cmd, data);
} }

View File

@ -1,6 +1,7 @@
#ifndef LIGHTNING_LIGHTNINGD_PAY_H #ifndef LIGHTNING_LIGHTNINGD_PAY_H
#define LIGHTNING_LIGHTNINGD_PAY_H #define LIGHTNING_LIGHTNINGD_PAY_H
#include "config.h" #include "config.h"
#include <ccan/short_types/short_types.h>
struct htlc_out; struct htlc_out;
struct lightningd; struct lightningd;
@ -8,6 +9,7 @@ struct preimage;
struct sha256; struct sha256;
struct json_stream; struct json_stream;
struct wallet_payment; struct wallet_payment;
struct routing_failure;
void payment_succeeded(struct lightningd *ld, struct htlc_out *hout, void payment_succeeded(struct lightningd *ld, struct htlc_out *hout,
const struct preimage *rval); 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, void json_add_payment_fields(struct json_stream *response,
const struct wallet_payment *t); 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 */ #endif /* LIGHTNING_LIGHTNINGD_PAY_H */