diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 2191ed39a..796792347 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -130,8 +130,10 @@ void json_add_uncommitted_channel(struct json_stream *response, msatoshi_total = uc->fc->wtx.amount.satoshis * 1000; our_msatoshi = msatoshi_total - uc->fc->push_msat; - json_add_u64(response, "msatoshi_to_us", our_msatoshi); - json_add_u64(response, "msatoshi_total", msatoshi_total); + json_add_amount_msat(response, (struct amount_msat){our_msatoshi}, + "msatoshi_to_us", "to_us_msat"); + json_add_amount_msat(response, (struct amount_msat){msatoshi_total}, + "msatoshi_total", "total_msat"); json_object_end(response); } diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 833f40d50..e0e3ac585 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -486,16 +486,27 @@ static void json_add_htlcs(struct lightningd *ld, json_array_end(response); } +/* We do this replication manually because it's an array. */ +static void json_add_sat_only(struct json_stream *result, + const char *fieldname, + struct amount_sat sat) +{ + struct amount_msat msat; + + if (amount_sat_to_msat(&msat, sat)) + json_add_member(result, fieldname, "\"%s\"", + type_to_string(tmpctx, struct amount_msat, &msat)); +} + static void json_add_channel(struct lightningd *ld, struct json_stream *response, const char *key, const struct channel *channel) { struct channel_id cid; struct channel_stats channel_stats; + struct amount_msat spendable; struct peer *p = channel->peer; - u64 our_reserve_msat = - channel->channel_info.their_config.channel_reserve_satoshis * 1000; json_object_start(response, key); json_add_string(response, "state", channel_state_name(channel)); if (channel->last_tx) { @@ -537,19 +548,45 @@ static void json_add_channel(struct lightningd *ld, } json_object_end(response); - json_add_u64(response, "msatoshi_to_us", channel->our_msatoshi); - json_add_u64(response, "msatoshi_to_us_min", - channel->msatoshi_to_us_min); - json_add_u64(response, "msatoshi_to_us_max", - channel->msatoshi_to_us_max); - json_add_u64(response, "msatoshi_total", - channel->funding_satoshi * 1000); + json_object_start(response, "funding_msat"); + if (channel->funder == LOCAL) { + json_add_sat_only(response, + pubkey_to_hexstr(tmpctx, &p->id), + AMOUNT_SAT(0)); + json_add_sat_only(response, + pubkey_to_hexstr(tmpctx, &ld->id), + (struct amount_sat){channel->funding_satoshi}); + } else { + json_add_sat_only(response, + pubkey_to_hexstr(tmpctx, &ld->id), + AMOUNT_SAT(0)); + json_add_sat_only(response, + pubkey_to_hexstr(tmpctx, &p->id), + (struct amount_sat){channel->funding_satoshi}); + } + json_object_end(response); + + json_add_amount_msat(response, + (struct amount_msat){channel->our_msatoshi}, + "msatoshi_to_us", "to_us_msat"); + json_add_amount_msat(response, + (struct amount_msat){channel->msatoshi_to_us_min}, + "msatoshi_to_us_min", "min_to_us_msat"); + json_add_amount_msat(response, + (struct amount_msat){channel->msatoshi_to_us_max}, + "msatoshi_to_us_max", "max_to_us_msat"); + json_add_amount_msat(response, + (struct amount_msat){channel->funding_satoshi * 1000}, + "msatoshi_total", "total_msat"); /* channel config */ - json_add_u64(response, "dust_limit_satoshis", - channel->our_config.dust_limit_satoshis); - json_add_u64(response, "max_htlc_value_in_flight_msat", - channel->our_config.max_htlc_value_in_flight_msat); + json_add_amount_sat(response, + (struct amount_sat){channel->our_config.dust_limit_satoshis}, + "dust_limit_satoshis", "dust_limit_msat"); + json_add_amount_msat(response, + (struct amount_msat){channel->our_config.max_htlc_value_in_flight_msat}, + "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` @@ -558,19 +595,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_u64(response, "their_channel_reserve_satoshis", - channel->our_config.channel_reserve_satoshis); - json_add_u64( - response, "our_channel_reserve_satoshis", - channel->channel_info.their_config.channel_reserve_satoshis); + json_add_amount_sat(response, + (struct amount_sat){channel->our_config.channel_reserve_satoshis}, + "their_channel_reserve_satoshis", + "their_reserve_msat"); + json_add_amount_sat(response, + (struct amount_sat){channel->channel_info.their_config.channel_reserve_satoshis}, + "our_channel_reserve_satoshis", + "our_reserve_msat"); /* Compute how much we can send via this channel. */ - if (channel->our_msatoshi <= our_reserve_msat) - json_add_u64(response, "spendable_msatoshi", 0); - else - json_add_u64(response, "spendable_msatoshi", - channel->our_msatoshi - our_reserve_msat); - json_add_u64(response, "htlc_minimum_msat", - channel->our_config.htlc_minimum_msat); + if (!amount_msat_sub_sat(&spendable, + (struct amount_msat){channel->our_msatoshi}, + (struct amount_sat){channel->channel_info.their_config.channel_reserve_satoshis})) + spendable = AMOUNT_MSAT(0); + + json_add_amount_msat(response, spendable, + "spendable_msatoshi", "spendable_msat"); + json_add_amount_msat(response, + (struct amount_msat){channel->our_config.htlc_minimum_msat}, + "htlc_minimum_msat", + "minimum_htlc_in_msat"); /* The `to_self_delay` is imposed on the *other* * side, so our configuration `to_self_delay` is @@ -598,20 +642,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_u64(response, "in_msatoshi_offered", - channel_stats.in_msatoshi_offered); + json_add_amount_msat(response, + (struct amount_msat){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_u64(response, "in_msatoshi_fulfilled", - channel_stats.in_msatoshi_fulfilled); + json_add_amount_msat(response, + (struct amount_msat){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_u64(response, "out_msatoshi_offered", - channel_stats.out_msatoshi_offered); + json_add_amount_msat(response, + (struct amount_msat){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_u64(response, "out_msatoshi_fulfilled", - channel_stats.out_msatoshi_fulfilled); + json_add_amount_msat(response, + (struct amount_msat){channel_stats.out_msatoshi_fulfilled}, + "out_msatoshi_fulfilled", + "out_fulfilled_msat"); json_add_htlcs(ld, response, channel); json_object_end(response); diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index ae3360893..bdc111afa 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -1841,9 +1841,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_num(response, "in_msatoshi", cur->msatoshi_in); - json_add_num(response, "out_msatoshi", cur->msatoshi_out); - json_add_num(response, "fee", cur->fee); + json_add_amount_msat(response, + (struct amount_msat) { cur->msatoshi_in }, + "in_msatoshi", "in_msat"); + json_add_amount_msat(response, + (struct amount_msat) { cur->msatoshi_out }, + "out_msatoshi", "out_msat"); + json_add_amount_msat(response, + (struct amount_msat) { cur->fee }, + "fee", "fee_msat"); json_add_string(response, "status", forward_status_name(cur->status)); json_object_end(response); } diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index 617d34043..a380f4d47 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -6,6 +6,15 @@ bool deprecated_apis = false; /* AUTOGENERATED MOCKS START */ +/* Generated stub for amount_msat_sub_sat */ + bool amount_msat_sub_sat(struct amount_msat *val UNNEEDED, + struct amount_msat a UNNEEDED, + struct amount_sat b UNNEEDED) +{ fprintf(stderr, "amount_msat_sub_sat called!\n"); abort(); } +/* Generated stub for amount_sat_to_msat */ + bool amount_sat_to_msat(struct amount_msat *msat UNNEEDED, + struct amount_sat sat UNNEEDED) +{ fprintf(stderr, "amount_sat_to_msat called!\n"); abort(); } /* Generated stub for bitcoind_gettxout */ void bitcoind_gettxout(struct bitcoind *bitcoind UNNEEDED, const struct bitcoin_txid *txid UNNEEDED, const u32 outnum UNNEEDED, @@ -127,6 +136,13 @@ void json_add_amount_msat(struct json_stream *result 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 *satfieldname) + +{ fprintf(stderr, "json_add_amount_sat 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) @@ -149,9 +165,10 @@ void json_add_hex_talarr(struct json_stream *result UNNEEDED, void json_add_log(struct json_stream *result UNNEEDED, const struct log_book *lr UNNEEDED, enum log_level minlevel UNNEEDED) { fprintf(stderr, "json_add_log called!\n"); abort(); } -/* Generated stub for json_add_null */ -void json_add_null(struct json_stream *stream UNNEEDED, const char *fieldname UNNEEDED) -{ fprintf(stderr, "json_add_null called!\n"); abort(); } +/* Generated stub for json_add_member */ +void json_add_member(struct json_stream *js UNNEEDED, const char *fieldname UNNEEDED, + const char *fmt UNNEEDED, ...) +{ fprintf(stderr, "json_add_member called!\n"); abort(); } /* Generated stub for json_add_num */ void json_add_num(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED, unsigned int value UNNEEDED) diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 57eef91f3..394f95d0e 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -30,6 +30,15 @@ static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, const c bool deprecated_apis = true; /* AUTOGENERATED MOCKS START */ +/* Generated stub for amount_msat_sub_sat */ + bool amount_msat_sub_sat(struct amount_msat *val UNNEEDED, + struct amount_msat a UNNEEDED, + struct amount_sat b UNNEEDED) +{ fprintf(stderr, "amount_msat_sub_sat called!\n"); abort(); } +/* Generated stub for amount_sat_to_msat */ + bool amount_sat_to_msat(struct amount_msat *msat UNNEEDED, + struct amount_sat sat UNNEEDED) +{ fprintf(stderr, "amount_sat_to_msat called!\n"); abort(); } /* Generated stub for bitcoind_gettxout */ void bitcoind_gettxout(struct bitcoind *bitcoind UNNEEDED, const struct bitcoin_txid *txid UNNEEDED, const u32 outnum UNNEEDED, @@ -206,6 +215,13 @@ void json_add_amount_msat(struct json_stream *result 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 *satfieldname) + +{ fprintf(stderr, "json_add_amount_sat 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) @@ -223,9 +239,10 @@ void json_add_hex_talarr(struct json_stream *result UNNEEDED, void json_add_log(struct json_stream *result UNNEEDED, const struct log_book *lr UNNEEDED, enum log_level minlevel UNNEEDED) { fprintf(stderr, "json_add_log called!\n"); abort(); } -/* Generated stub for json_add_null */ -void json_add_null(struct json_stream *stream UNNEEDED, const char *fieldname UNNEEDED) -{ fprintf(stderr, "json_add_null called!\n"); abort(); } +/* Generated stub for json_add_member */ +void json_add_member(struct json_stream *js UNNEEDED, const char *fieldname UNNEEDED, + const char *fmt UNNEEDED, ...) +{ fprintf(stderr, "json_add_member called!\n"); abort(); } /* Generated stub for json_add_num */ void json_add_num(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED, unsigned int value UNNEEDED)