From 401bd9f8ef391ec611e6779c4baec04fa8285774 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 20 May 2019 14:37:40 +0930 Subject: [PATCH] json: rename json_add_amount_sat to json_add_amount_sat_compat. New fields don't have to be spelled out twice. The raw version are called _only, so we don't miss a call accidentally. We can rename them when we finally deprecated old fields. Signed-off-by: Rusty Russell --- lightningd/gossip_control.c | 4 +- lightningd/invoice.c | 13 +-- lightningd/json.c | 34 +++++-- lightningd/json.h | 28 ++++-- lightningd/opening_control.c | 8 +- lightningd/pay.c | 8 +- lightningd/peer_control.c | 105 ++++++++++---------- lightningd/peer_htlcs.c | 18 ++-- lightningd/test/run-invoice-select-inchan.c | 28 +++--- wallet/test/run-wallet.c | 28 +++--- wallet/walletrpc.c | 16 +-- 11 files changed, 160 insertions(+), 130 deletions(-) diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 0d8fd0dbf..7e6c0fcf5 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -378,8 +378,8 @@ static void json_add_halfchan(struct json_stream *response, json_add_short_channel_id(response, "short_channel_id", &e->short_channel_id); json_add_bool(response, "public", e->public); - json_add_amount_sat(response, e->sat, - "satoshis", "amount_msat"); + json_add_amount_sat_compat(response, e->sat, + "satoshis", "amount_msat"); json_add_num(response, "message_flags", he->message_flags); json_add_num(response, "channel_flags", he->channel_flags); json_add_bool(response, "active", diff --git a/lightningd/invoice.c b/lightningd/invoice.c index 37e01e363..2aab607a1 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -51,13 +51,14 @@ static void json_add_invoice(struct json_stream *response, json_add_string(response, "bolt11", inv->bolt11); json_add_hex(response, "payment_hash", &inv->rhash, sizeof(inv->rhash)); if (inv->msat) - json_add_amount_msat(response, *inv->msat, - "msatoshi", "amount_msat"); + json_add_amount_msat_compat(response, *inv->msat, + "msatoshi", "amount_msat"); json_add_string(response, "status", invoice_status_str(inv)); if (inv->state == PAID) { json_add_u64(response, "pay_index", inv->pay_index); - json_add_amount_msat(response, inv->received, - "msatoshi_received", "amount_received_msat"); + json_add_amount_msat_compat(response, inv->received, + "msatoshi_received", + "amount_received_msat"); json_add_u64(response, "paid_at", inv->paid_timestamp); } @@ -1093,8 +1094,8 @@ static struct command_result *json_decodepay(struct command *cmd, json_add_u64(response, "expiry", b11->expiry); json_add_node_id(response, "payee", &b11->receiver_id); if (b11->msat) - json_add_amount_msat(response, *b11->msat, - "msatoshi", "amount_msat"); + json_add_amount_msat_compat(response, *b11->msat, + "msatoshi", "amount_msat"); if (b11->description) { struct json_escaped *esc = json_escape(NULL, b11->description); json_add_escaped_string(response, "description", take(esc)); diff --git a/lightningd/json.c b/lightningd/json.c index d73f9be01..4a9f2b8cb 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -33,7 +33,7 @@ json_add_route_hop(struct json_stream *r, char const *n, json_add_short_channel_id(r, "channel", &h->channel_id); json_add_num(r, "direction", h->direction); - json_add_amount_msat(r, h->amount, "msatoshi", "amount_msat"); + json_add_amount_msat_compat(r, h->amount, "msatoshi", "amount_msat"); json_add_num(r, "delay", h->delay); json_object_end(r); } @@ -347,23 +347,37 @@ void json_add_escaped_string(struct json_stream *result, const char *fieldname, tal_free(esc); } -void json_add_amount_msat(struct json_stream *result, - struct amount_msat msat, - const char *rawfieldname, - const char *msatfieldname) +void json_add_amount_msat_compat(struct json_stream *result, + struct amount_msat msat, + const char *rawfieldname, + const char *msatfieldname) { json_add_u64(result, rawfieldname, msat.millisatoshis); /* Raw: low-level helper */ + json_add_amount_msat_only(result, msatfieldname, msat); +} + +void json_add_amount_msat_only(struct json_stream *result, + const char *msatfieldname, + struct amount_msat msat) +{ json_add_member(result, msatfieldname, "\"%s\"", type_to_string(tmpctx, struct amount_msat, &msat)); } -void json_add_amount_sat(struct json_stream *result, - struct amount_sat sat, - const char *rawfieldname, - const char *msatfieldname) +void json_add_amount_sat_compat(struct json_stream *result, + struct amount_sat sat, + const char *rawfieldname, + const char *msatfieldname) { - struct amount_msat msat; json_add_u64(result, rawfieldname, sat.satoshis); /* Raw: low-level helper */ + json_add_amount_sat_only(result, msatfieldname, sat); +} + +void json_add_amount_sat_only(struct json_stream *result, + const char *msatfieldname, + struct amount_sat sat) +{ + struct amount_msat msat; if (amount_sat_to_msat(&msat, sat)) json_add_member(result, msatfieldname, "\"%s\"", type_to_string(tmpctx, struct amount_msat, &msat)); diff --git a/lightningd/json.h b/lightningd/json.h index 3c2aa02bb..78d47fb22 100644 --- a/lightningd/json.h +++ b/lightningd/json.h @@ -143,17 +143,29 @@ void json_add_hex_talarr(struct json_stream *result, const tal_t *data); /* Adds both a 'raw' number field and an 'amount_msat' field */ -void json_add_amount_msat(struct json_stream *result, - struct amount_msat msat, - const char *rawfieldname, - const char *msatfieldname) +void json_add_amount_msat_compat(struct json_stream *result, + struct amount_msat msat, + const char *rawfieldname, + const char *msatfieldname) NO_NULL_ARGS; /* Adds both a 'raw' number field and an 'amount_msat' field */ -void json_add_amount_sat(struct json_stream *result, - struct amount_sat sat, - const char *rawfieldname, - const char *msatfieldname) +void json_add_amount_sat_compat(struct json_stream *result, + struct amount_sat sat, + const char *rawfieldname, + const char *msatfieldname) + NO_NULL_ARGS; + +/* Adds an 'msat' field */ +void json_add_amount_msat_only(struct json_stream *result, + const char *msatfieldname, + struct amount_msat msat) + NO_NULL_ARGS; + +/* Adds an 'msat' field */ +void json_add_amount_sat_only(struct json_stream *result, + const char *msatfieldname, + struct amount_sat sat) NO_NULL_ARGS; enum address_parse_result { diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 1e442b730..be2da28fe 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -132,10 +132,10 @@ void json_add_uncommitted_channel(struct json_stream *response, /* These should never fail. */ if (amount_sat_to_msat(&total, uc->fc->wtx.amount) && amount_msat_sub(&ours, total, uc->fc->push)) { - json_add_amount_msat(response, ours, - "msatoshi_to_us", "to_us_msat"); - json_add_amount_msat(response, total, - "msatoshi_total", "total_msat"); + json_add_amount_msat_compat(response, ours, + "msatoshi_to_us", "to_us_msat"); + json_add_amount_msat_compat(response, total, + "msatoshi_total", "total_msat"); } json_object_end(response); } diff --git a/lightningd/pay.c b/lightningd/pay.c index 77efc8efe..13ac3fbfd 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -78,10 +78,10 @@ json_add_payment_fields(struct json_stream *response, json_add_u64(response, "id", t->id); json_add_hex(response, "payment_hash", &t->payment_hash, sizeof(t->payment_hash)); json_add_node_id(response, "destination", &t->destination); - json_add_amount_msat(response, t->msatoshi, - "msatoshi", "amount_msat"); - json_add_amount_msat(response, t->msatoshi_sent, - "msatoshi_sent", "amount_sent_msat"); + json_add_amount_msat_compat(response, t->msatoshi, + "msatoshi", "amount_msat"); + json_add_amount_msat_compat(response, t->msatoshi_sent, + "msatoshi_sent", "amount_sent_msat"); json_add_u64(response, "created_at", t->timestamp); switch (t->status) { diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 1ebd75479..b84b6caf4 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -445,8 +445,8 @@ static void json_add_htlcs(struct lightningd *ld, json_object_start(response, NULL); json_add_string(response, "direction", "in"); json_add_u64(response, "id", hin->key.id); - json_add_amount_msat(response, hin->msat, - "msatoshi", "amount_msat"); + json_add_amount_msat_compat(response, hin->msat, + "msatoshi", "amount_msat"); json_add_u64(response, "expiry", hin->cltv_expiry); json_add_hex(response, "payment_hash", &hin->payment_hash, sizeof(hin->payment_hash)); @@ -464,8 +464,8 @@ static void json_add_htlcs(struct lightningd *ld, json_object_start(response, NULL); json_add_string(response, "direction", "out"); json_add_u64(response, "id", hout->key.id); - json_add_amount_msat(response, hout->msat, - "msatoshi", "amount_msat"); + json_add_amount_msat_compat(response, hout->msat, + "msatoshi", "amount_msat"); json_add_u64(response, "expiry", hout->cltv_expiry); json_add_hex(response, "payment_hash", &hout->payment_hash, sizeof(hout->payment_hash)); @@ -563,23 +563,23 @@ static void json_add_channel(struct lightningd *ld, &channel->funding)); funding_msat = AMOUNT_MSAT(0); } - json_add_amount_msat(response, channel->our_msat, - "msatoshi_to_us", "to_us_msat"); - json_add_amount_msat(response, channel->msat_to_us_min, - "msatoshi_to_us_min", "min_to_us_msat"); - json_add_amount_msat(response, channel->msat_to_us_max, - "msatoshi_to_us_max", "max_to_us_msat"); - json_add_amount_msat(response, funding_msat, - "msatoshi_total", "total_msat"); + json_add_amount_msat_compat(response, channel->our_msat, + "msatoshi_to_us", "to_us_msat"); + json_add_amount_msat_compat(response, channel->msat_to_us_min, + "msatoshi_to_us_min", "min_to_us_msat"); + json_add_amount_msat_compat(response, channel->msat_to_us_max, + "msatoshi_to_us_max", "max_to_us_msat"); + json_add_amount_msat_compat(response, funding_msat, + "msatoshi_total", "total_msat"); /* channel config */ - json_add_amount_sat(response, - channel->our_config.dust_limit, - "dust_limit_satoshis", "dust_limit_msat"); - json_add_amount_msat(response, - channel->our_config.max_htlc_value_in_flight, - "max_htlc_value_in_flight_msat", - "max_total_htlc_in_msat"); + json_add_amount_sat_compat(response, + channel->our_config.dust_limit, + "dust_limit_satoshis", "dust_limit_msat"); + json_add_amount_msat_compat(response, + channel->our_config.max_htlc_value_in_flight, + "max_htlc_value_in_flight_msat", + "max_total_htlc_in_msat"); /* The `channel_reserve_satoshis` is imposed on * the *other* side (see `channel_reserve_msat` @@ -588,26 +588,26 @@ static void json_add_channel(struct lightningd *ld, * is imposed on their side, while their * configuration `channel_reserve_satoshis` is * imposed on ours. */ - json_add_amount_sat(response, - channel->our_config.channel_reserve, - "their_channel_reserve_satoshis", - "their_reserve_msat"); - json_add_amount_sat(response, - channel->channel_info.their_config.channel_reserve, - "our_channel_reserve_satoshis", - "our_reserve_msat"); + json_add_amount_sat_compat(response, + channel->our_config.channel_reserve, + "their_channel_reserve_satoshis", + "their_reserve_msat"); + json_add_amount_sat_compat(response, + channel->channel_info.their_config.channel_reserve, + "our_channel_reserve_satoshis", + "our_reserve_msat"); /* Compute how much we can send via this channel. */ if (!amount_msat_sub_sat(&spendable, channel->our_msat, channel->channel_info.their_config.channel_reserve)) spendable = AMOUNT_MSAT(0); - json_add_amount_msat(response, spendable, - "spendable_msatoshi", "spendable_msat"); - json_add_amount_msat(response, - channel->our_config.htlc_minimum, - "htlc_minimum_msat", - "minimum_htlc_in_msat"); + json_add_amount_msat_compat(response, spendable, + "spendable_msatoshi", "spendable_msat"); + json_add_amount_msat_compat(response, + channel->our_config.htlc_minimum, + "htlc_minimum_msat", + "minimum_htlc_in_msat"); /* The `to_self_delay` is imposed on the *other* * side, so our configuration `to_self_delay` is @@ -635,28 +635,28 @@ static void json_add_channel(struct lightningd *ld, wallet_channel_stats_load(ld->wallet, channel->dbid, &channel_stats); json_add_u64(response, "in_payments_offered", channel_stats.in_payments_offered); - json_add_amount_msat(response, - channel_stats.in_msatoshi_offered, - "in_msatoshi_offered", - "in_offered_msat"); + json_add_amount_msat_compat(response, + channel_stats.in_msatoshi_offered, + "in_msatoshi_offered", + "in_offered_msat"); json_add_u64(response, "in_payments_fulfilled", channel_stats.in_payments_fulfilled); - json_add_amount_msat(response, - channel_stats.in_msatoshi_fulfilled, - "in_msatoshi_fulfilled", - "in_fulfilled_msat"); + json_add_amount_msat_compat(response, + channel_stats.in_msatoshi_fulfilled, + "in_msatoshi_fulfilled", + "in_fulfilled_msat"); json_add_u64(response, "out_payments_offered", channel_stats.out_payments_offered); - json_add_amount_msat(response, - channel_stats.out_msatoshi_offered, - "out_msatoshi_offered", - "out_offered_msat"); + json_add_amount_msat_compat(response, + channel_stats.out_msatoshi_offered, + "out_msatoshi_offered", + "out_offered_msat"); json_add_u64(response, "out_payments_fulfilled", channel_stats.out_payments_fulfilled); - json_add_amount_msat(response, - channel_stats.out_msatoshi_fulfilled, - "out_msatoshi_fulfilled", - "out_fulfilled_msat"); + json_add_amount_msat_compat(response, + channel_stats.out_msatoshi_fulfilled, + "out_msatoshi_fulfilled", + "out_fulfilled_msat"); json_add_htlcs(ld, response, channel); json_object_end(response); @@ -1301,9 +1301,10 @@ static struct command_result *json_getinfo(struct command *cmd, json_add_string(response, "version", version()); json_add_num(response, "blockheight", get_block_height(cmd->ld->topology)); json_add_string(response, "network", get_chainparams(cmd->ld)->network_name); - json_add_amount_msat(response, - wallet_total_forward_fees(cmd->ld->wallet), - "msatoshi_fees_collected", "fees_collected_msat"); + json_add_amount_msat_compat(response, + wallet_total_forward_fees(cmd->ld->wallet), + "msatoshi_fees_collected", + "fees_collected_msat"); json_object_end(response); return command_success(cmd, response); } diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index a7b5252e5..caec71e17 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -1904,15 +1904,15 @@ static void listforwardings_add_forwardings(struct json_stream *response, struct 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(response, - cur->msat_in, - "in_msatoshi", "in_msat"); - json_add_amount_msat(response, - cur->msat_out, - "out_msatoshi", "out_msat"); - json_add_amount_msat(response, - cur->fee, - "fee", "fee_msat"); + 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) { diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index 26d385bca..714714055 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -126,20 +126,20 @@ void json_add_address_internal(struct json_stream *response UNNEEDED, const char *fieldname UNNEEDED, const struct wireaddr_internal *addr UNNEEDED) { fprintf(stderr, "json_add_address_internal called!\n"); abort(); } -/* Generated stub for json_add_amount_msat */ -void json_add_amount_msat(struct json_stream *result UNNEEDED, - struct amount_msat msat UNNEEDED, - const char *rawfieldname UNNEEDED, - const char *msatfieldname) - -{ fprintf(stderr, "json_add_amount_msat called!\n"); abort(); } -/* Generated stub for json_add_amount_sat */ -void json_add_amount_sat(struct json_stream *result UNNEEDED, - struct amount_sat sat UNNEEDED, - const char *rawfieldname UNNEEDED, - const char *msatfieldname) - -{ fprintf(stderr, "json_add_amount_sat called!\n"); abort(); } +/* Generated stub for json_add_amount_msat_compat */ +void json_add_amount_msat_compat(struct json_stream *result UNNEEDED, + struct amount_msat msat UNNEEDED, + const char *rawfieldname UNNEEDED, + const char *msatfieldname) + +{ fprintf(stderr, "json_add_amount_msat_compat called!\n"); abort(); } +/* Generated stub for json_add_amount_sat_compat */ +void json_add_amount_sat_compat(struct json_stream *result UNNEEDED, + struct amount_sat sat UNNEEDED, + const char *rawfieldname UNNEEDED, + const char *msatfieldname) + +{ fprintf(stderr, "json_add_amount_sat_compat called!\n"); abort(); } /* Generated stub for json_add_bool */ void json_add_bool(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED, bool value UNNEEDED) diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 9eb5fecaa..7f429a3be 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -205,20 +205,20 @@ void json_add_address_internal(struct json_stream *response UNNEEDED, const char *fieldname UNNEEDED, const struct wireaddr_internal *addr UNNEEDED) { fprintf(stderr, "json_add_address_internal called!\n"); abort(); } -/* Generated stub for json_add_amount_msat */ -void json_add_amount_msat(struct json_stream *result UNNEEDED, - struct amount_msat msat UNNEEDED, - const char *rawfieldname UNNEEDED, - const char *msatfieldname) - -{ fprintf(stderr, "json_add_amount_msat called!\n"); abort(); } -/* Generated stub for json_add_amount_sat */ -void json_add_amount_sat(struct json_stream *result UNNEEDED, - struct amount_sat sat UNNEEDED, - const char *rawfieldname UNNEEDED, - const char *msatfieldname) - -{ fprintf(stderr, "json_add_amount_sat called!\n"); abort(); } +/* Generated stub for json_add_amount_msat_compat */ +void json_add_amount_msat_compat(struct json_stream *result UNNEEDED, + struct amount_msat msat UNNEEDED, + const char *rawfieldname UNNEEDED, + const char *msatfieldname) + +{ fprintf(stderr, "json_add_amount_msat_compat called!\n"); abort(); } +/* Generated stub for json_add_amount_sat_compat */ +void json_add_amount_sat_compat(struct json_stream *result UNNEEDED, + struct amount_sat sat UNNEEDED, + const char *rawfieldname UNNEEDED, + const char *msatfieldname) + +{ fprintf(stderr, "json_add_amount_sat_compat called!\n"); abort(); } /* Generated stub for json_add_bool */ void json_add_bool(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED, bool value UNNEEDED) diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index c4567bc6a..db716494b 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -460,8 +460,8 @@ static struct command_result *json_listfunds(struct command *cmd, json_object_start(response, NULL); json_add_txid(response, "txid", &utxos[i]->txid); json_add_num(response, "output", utxos[i]->outnum); - json_add_amount_sat(response, utxos[i]->amount, - "value", "amount_msat"); + json_add_amount_sat_compat(response, utxos[i]->amount, + "value", "amount_msat"); /* @close_info is for outputs that are not yet claimable */ if (utxos[i]->close_info == NULL) { @@ -506,11 +506,13 @@ static struct command_result *json_listfunds(struct command *cmd, "short_channel_id", c->scid); - json_add_amount_sat(response, - amount_msat_to_sat_round_down(c->our_msat), - "channel_sat", "our_amount_msat"); - json_add_amount_sat(response, c->funding, - "channel_total_sat", "amount_msat"); + json_add_amount_sat_compat(response, + amount_msat_to_sat_round_down(c->our_msat), + "channel_sat", + "our_amount_msat"); + json_add_amount_sat_compat(response, c->funding, + "channel_total_sat", + "amount_msat"); json_add_txid(response, "funding_txid", &c->funding_txid); json_object_end(response);