From 2de467274e33a83b7b434d79cb3528deee9df348 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 9 Jan 2021 14:55:17 +1030 Subject: [PATCH] common/amount: make fmt_amount_sat / fmt_amount_msat etc take copy. We pass by copy everywhere else, let's do it here too. Signed-off-by: Rusty Russell --- channeld/channeld.c | 2 +- common/amount.c | 40 +++++++++++++++++++----------- common/amount.h | 8 +++--- common/test/run-amount.c | 12 ++++----- plugins/offers_inv_hook.c | 2 +- plugins/pay.c | 4 +-- plugins/spender/multifundchannel.c | 4 +-- plugins/spender/openchannel.c | 2 +- 8 files changed, 43 insertions(+), 31 deletions(-) diff --git a/channeld/channeld.c b/channeld/channeld.c index 705d11352..5cc5f34f4 100644 --- a/channeld/channeld.c +++ b/channeld/channeld.c @@ -2900,7 +2900,7 @@ static void handle_offer_htlc(struct peer *peer, const u8 *inmsg) /* FIXME: Fuzz the boundaries a bit to avoid probing? */ case CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED: failwiremsg = towire_temporary_channel_failure(inmsg, get_local_channel_update(inmsg, peer)); - failstr = tal_fmt(inmsg, "Capacity exceeded - HTLC fee: %s", fmt_amount_sat(inmsg, &htlc_fee)); + failstr = tal_fmt(inmsg, "Capacity exceeded - HTLC fee: %s", fmt_amount_sat(inmsg, htlc_fee)); goto failed; case CHANNEL_ERR_HTLC_BELOW_MINIMUM: failwiremsg = towire_amount_below_minimum(inmsg, amount, get_local_channel_update(inmsg, peer)); diff --git a/common/amount.c b/common/amount.c index e56caad75..74bf58198 100644 --- a/common/amount.c +++ b/common/amount.c @@ -30,42 +30,54 @@ struct amount_sat amount_msat_to_sat_round_down(struct amount_msat msat) /* Different formatting by amounts: btc, sat and msat */ const char *fmt_amount_msat_btc(const tal_t *ctx, - const struct amount_msat *msat, + struct amount_msat msat, bool append_unit) { - if (msat->millisatoshis == 0) + if (msat.millisatoshis == 0) return tal_fmt(ctx, append_unit ? "0btc" : "0"); return tal_fmt(ctx, "%"PRIu64".%011"PRIu64"%s", - msat->millisatoshis / MSAT_PER_BTC, - msat->millisatoshis % MSAT_PER_BTC, + msat.millisatoshis / MSAT_PER_BTC, + msat.millisatoshis % MSAT_PER_BTC, append_unit ? "btc" : ""); } -const char *fmt_amount_msat(const tal_t *ctx, const struct amount_msat *msat) +const char *fmt_amount_msat(const tal_t *ctx, struct amount_msat msat) { - return tal_fmt(ctx, "%"PRIu64"msat", msat->millisatoshis); + return tal_fmt(ctx, "%"PRIu64"msat", msat.millisatoshis); } -REGISTER_TYPE_TO_STRING(amount_msat, fmt_amount_msat); + +static const char *fmt_amount_msat_ptr(const tal_t *ctx, + const struct amount_msat *msat) +{ + return fmt_amount_msat(ctx, *msat); +} +REGISTER_TYPE_TO_STRING(amount_msat, fmt_amount_msat_ptr); const char *fmt_amount_sat_btc(const tal_t *ctx, - const struct amount_sat *sat, + struct amount_sat sat, bool append_unit) { - if (sat->satoshis == 0) + if (sat.satoshis == 0) return tal_fmt(ctx, append_unit ? "0btc" : "0"); return tal_fmt(ctx, "%"PRIu64".%08"PRIu64"%s", - sat->satoshis / SAT_PER_BTC, - sat->satoshis % SAT_PER_BTC, + sat.satoshis / SAT_PER_BTC, + sat.satoshis % SAT_PER_BTC, append_unit ? "btc" : ""); } -const char *fmt_amount_sat(const tal_t *ctx, const struct amount_sat *sat) +const char *fmt_amount_sat(const tal_t *ctx, struct amount_sat sat) { - return tal_fmt(ctx, "%"PRIu64"sat", sat->satoshis); + return tal_fmt(ctx, "%"PRIu64"sat", sat.satoshis); } -REGISTER_TYPE_TO_STRING(amount_sat, fmt_amount_sat); + +static const char *fmt_amount_sat_ptr(const tal_t *ctx, + const struct amount_sat *sat) +{ + return fmt_amount_sat(ctx, *sat); +} +REGISTER_TYPE_TO_STRING(amount_sat, fmt_amount_sat_ptr); static bool breakup(const char *str, size_t slen, /* Length of first numeric part. */ diff --git a/common/amount.h b/common/amount.h index 6a5f60699..3b1a84ea5 100644 --- a/common/amount.h +++ b/common/amount.h @@ -158,17 +158,17 @@ struct amount_sat amount_tx_fee(u32 fee_per_kw, size_t weight); /* Different formatting by amounts: btc, sat and msat */ /* => 1.23456789012btc (11 decimals!) */ const char *fmt_amount_msat_btc(const tal_t *ctx, - const struct amount_msat *msat, + struct amount_msat msat, bool append_unit); /* => 1234msat */ -const char *fmt_amount_msat(const tal_t *ctx, const struct amount_msat *msat); +const char *fmt_amount_msat(const tal_t *ctx, struct amount_msat msat); /* => 1.23456789btc (8 decimals!) */ const char *fmt_amount_sat_btc(const tal_t *ctx, - const struct amount_sat *sat, + struct amount_sat sat, bool append_unit); /* => 1234sat */ -const char *fmt_amount_sat(const tal_t *ctx, const struct amount_sat *sat); +const char *fmt_amount_sat(const tal_t *ctx, struct amount_sat sat); /* Valid strings: * [0-9]+ => millisatoshi. diff --git a/common/test/run-amount.c b/common/test/run-amount.c index 7c4704cc9..8cee214cf 100644 --- a/common/test/run-amount.c +++ b/common/test/run-amount.c @@ -189,8 +189,8 @@ int main(int argc, char *argv[]) const char *with, *without; msat.millisatoshis = i; - with = fmt_amount_msat_btc(tmpctx, &msat, true); - without = fmt_amount_msat_btc(tmpctx, &msat, false); + with = fmt_amount_msat_btc(tmpctx, msat, true); + without = fmt_amount_msat_btc(tmpctx, msat, false); assert(strends(with, "btc")); assert(strlen(with) == strlen(without) + 3); assert(strncmp(with, without, strlen(without)) == 0); @@ -199,7 +199,7 @@ int main(int argc, char *argv[]) assert(parse_amount_msat(&msat, with, strlen(with))); assert(msat.millisatoshis == i); - with = fmt_amount_msat(tmpctx, &msat); + with = fmt_amount_msat(tmpctx, msat); without = tal_fmt(tmpctx, "%"PRIu64, msat.millisatoshis); assert(strends(with, "msat")); assert(strlen(with) == strlen(without) + 4); @@ -215,8 +215,8 @@ int main(int argc, char *argv[]) const char *with, *without; sat.satoshis = i; - with = fmt_amount_sat_btc(tmpctx, &sat, true); - without = fmt_amount_sat_btc(tmpctx, &sat, false); + with = fmt_amount_sat_btc(tmpctx, sat, true); + without = fmt_amount_sat_btc(tmpctx, sat, false); assert(strends(with, "btc")); assert(strlen(with) == strlen(without) + 3); assert(strncmp(with, without, strlen(without)) == 0); @@ -225,7 +225,7 @@ int main(int argc, char *argv[]) assert(parse_amount_sat(&sat, with, strlen(with))); assert(sat.satoshis == i); - with = fmt_amount_sat(tmpctx, &sat); + with = fmt_amount_sat(tmpctx, sat); without = tal_fmt(tmpctx, "%"PRIu64, sat.satoshis); assert(strends(with, "sat")); assert(strlen(with) == strlen(without) + 3); diff --git a/plugins/offers_inv_hook.c b/plugins/offers_inv_hook.c index 5d0dd7f25..1752f78e1 100644 --- a/plugins/offers_inv_hook.c +++ b/plugins/offers_inv_hook.c @@ -295,7 +295,7 @@ static struct command_result *listoffers_done(struct command *cmd, /* We could allow invoices for less, I suppose. */ if (!amount_msat_eq(expected, amt)) return fail_inv(cmd, inv, "Expected invoice for %s", - fmt_amount_msat(tmpctx, &expected)); + fmt_amount_msat(tmpctx, expected)); } plugin_log(cmd->plugin, LOG_INFORM, diff --git a/plugins/pay.c b/plugins/pay.c index af28acde2..26240f6a1 100644 --- a/plugins/pay.c +++ b/plugins/pay.c @@ -1767,10 +1767,10 @@ static void add_new_entry(struct json_stream *ret, * failures. */ if (pm->amount != NULL && pm->num_nonfailed_parts > 0) json_add_string(ret, "amount_msat", - fmt_amount_msat(tmpctx, pm->amount)); + fmt_amount_msat(tmpctx, *pm->amount)); json_add_string(ret, "amount_sent_msat", - fmt_amount_msat(tmpctx, &pm->amount_sent)); + fmt_amount_msat(tmpctx, pm->amount_sent)); if (pm->num_nonfailed_parts > 1) json_add_u64(ret, "number_of_parts", diff --git a/plugins/spender/multifundchannel.c b/plugins/spender/multifundchannel.c index ffb566dc6..35d5d6052 100644 --- a/plugins/spender/multifundchannel.c +++ b/plugins/spender/multifundchannel.c @@ -1050,7 +1050,7 @@ fundchannel_start_dest(struct multifundchannel_destination *dest) json_add_node_id(req->js, "id", &dest->id); assert(!dest->all); json_add_string(req->js, "amount", - fmt_amount_sat(tmpctx, &dest->amount)); + fmt_amount_sat(tmpctx, dest->amount)); if (mfc->cmtmt_feerate_str) json_add_string(req->js, "feerate", mfc->cmtmt_feerate_str); @@ -1058,7 +1058,7 @@ fundchannel_start_dest(struct multifundchannel_destination *dest) json_add_string(req->js, "feerate", mfc->feerate_str); json_add_bool(req->js, "announce", dest->announce); json_add_string(req->js, "push_msat", - fmt_amount_msat(tmpctx, &dest->push_msat)); + fmt_amount_msat(tmpctx, dest->push_msat)); if (dest->close_to_str) json_add_string(req->js, "close_to", dest->close_to_str); diff --git a/plugins/spender/openchannel.c b/plugins/spender/openchannel.c index 51a3698f5..5e4bd2cba 100644 --- a/plugins/spender/openchannel.c +++ b/plugins/spender/openchannel.c @@ -1014,7 +1014,7 @@ openchannel_init_dest(struct multifundchannel_destination *dest) json_add_node_id(req->js, "id", &dest->id); assert(!dest->all); json_add_string(req->js, "amount", - fmt_amount_sat(tmpctx, &dest->amount)); + fmt_amount_sat(tmpctx, dest->amount)); /* Copy the original parent down */ tal_wally_start();