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.
This commit is contained in:
trueptolemy 2019-07-08 19:36:47 +08:00 committed by ZmnSCPxj, ZmnSCPxj jxPCSmnZ
parent bcec6bb6cc
commit a449a91ae2
2 changed files with 54 additions and 38 deletions

View File

@ -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; i<tal_count(forwardings); i++) {
const struct forwarding *cur = &forwardings[i];
json_object_start(response, NULL);
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);
json_format_forwarding_object(response, NULL, cur);
}
json_array_end(response);

View File

@ -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 */