From a449a91ae211833a80c2bf2e2a1f46c862d7df87 Mon Sep 17 00:00:00 2001 From: trueptolemy <823220586@qq.com> Date: Mon, 8 Jul 2019 19:36:47 +0800 Subject: [PATCH] JSON: Warp the process of forward payment json object Warp this process as a new function: 'void json_format_forwarding_object()'. This function will be used in 'forward_event' next, and can ensure the consistent json object structure for forward_payment between 'listforwards' API and 'forward_event' notification. --- lightningd/peer_htlcs.c | 86 +++++++++++++++++++++++------------------ lightningd/peer_htlcs.h | 6 +++ 2 files changed, 54 insertions(+), 38 deletions(-) diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index d72d478e8..7056568e6 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -2104,6 +2104,53 @@ static const struct json_command dev_ignore_htlcs = { AUTODATA(json_command, &dev_ignore_htlcs); #endif /* DEVELOPER */ +/* Warp this process to ensure the consistent json object structure + * between 'listforwards' API and 'forward_event' notification. */ +void json_format_forwarding_object(struct json_stream *response, + const char *fieldname, + const struct forwarding *cur) +{ + json_object_start(response, fieldname); + + json_add_hex(response, "payment_hash", + cur->payment_hash, + sizeof(*cur->payment_hash)); + json_add_short_channel_id(response, "in_channel", &cur->channel_in); + json_add_short_channel_id(response, "out_channel", &cur->channel_out); + json_add_amount_msat_compat(response, + cur->msat_in, + "in_msatoshi", "in_msat"); + json_add_amount_msat_compat(response, + cur->msat_out, + "out_msatoshi", "out_msat"); + json_add_amount_msat_compat(response, + cur->fee, + "fee", "fee_msat"); + json_add_string(response, "status", forward_status_name(cur->status)); + + if(cur->failcode != 0) { + json_add_num(response, "failcode", cur->failcode); + json_add_string(response, "failreason", + onion_type_name(cur->failcode)); + } + +#ifdef COMPAT_V070 + /* If a forwarding doesn't have received_time it was created + * before we added the tracking, do not include it here. */ + if (cur->received_time.ts.tv_sec) { + json_add_timeabs(response, "received_time", cur->received_time); + if (cur->resolved_time) + json_add_timeabs(response, "resolved_time", *cur->resolved_time); + } +#else + json_add_timeabs(response, "received_time", cur->received_time); + if (cur->resolved_time) + json_add_timeabs(response, "resolved_time", *cur->resolved_time); +#endif + json_object_end(response); +} + + static void listforwardings_add_forwardings(struct json_stream *response, struct wallet *wallet) { const struct forwarding *forwardings; @@ -2112,44 +2159,7 @@ static void listforwardings_add_forwardings(struct json_stream *response, struct json_array_start(response, "forwards"); for (size_t i=0; ipayment_hash, - sizeof(*cur->payment_hash)); - json_add_short_channel_id(response, "in_channel", &cur->channel_in); - json_add_short_channel_id(response, "out_channel", &cur->channel_out); - json_add_amount_msat_compat(response, - cur->msat_in, - "in_msatoshi", "in_msat"); - json_add_amount_msat_compat(response, - cur->msat_out, - "out_msatoshi", "out_msat"); - json_add_amount_msat_compat(response, - cur->fee, - "fee", "fee_msat"); - json_add_string(response, "status", forward_status_name(cur->status)); - - if(cur->failcode != 0) { - json_add_num(response, "failcode", cur->failcode); - json_add_string(response, "failreason", - onion_type_name(cur->failcode)); - } - -#ifdef COMPAT_V070 - /* If a forwarding doesn't have received_time it was created - * before we added the tracking, do not include it here. */ - if (cur->received_time.ts.tv_sec) { - json_add_timeabs(response, "received_time", cur->received_time); - if (cur->resolved_time) - json_add_timeabs(response, "resolved_time", *cur->resolved_time); - } -#else - json_add_timeabs(response, "received_time", cur->received_time); - if (cur->resolved_time) - json_add_timeabs(response, "resolved_time", *cur->resolved_time); -#endif - json_object_end(response); + json_format_forwarding_object(response, NULL, cur); } json_array_end(response); diff --git a/lightningd/peer_htlcs.h b/lightningd/peer_htlcs.h index d8f9285ed..5a2ec845c 100644 --- a/lightningd/peer_htlcs.h +++ b/lightningd/peer_htlcs.h @@ -14,6 +14,8 @@ struct htlc_out; struct htlc_out_map; struct htlc_stub; struct lightningd; +struct forwarding; +struct json_stream; /* FIXME: Define serialization primitive for this? */ struct channel_info { @@ -69,4 +71,8 @@ void htlcs_reconnect(struct lightningd *ld, void fulfill_htlc(struct htlc_in *hin, const struct preimage *preimage); void fail_htlc(struct htlc_in *hin, enum onion_type failcode); +/* This json process will be both used in 'notify_forward_event()' + * and 'listforwardings_add_forwardings()'*/ +void json_format_forwarding_object(struct json_stream *response, const char *fieldname, + const struct forwarding *cur); #endif /* LIGHTNING_LIGHTNINGD_PEER_HTLCS_H */