Browse Source

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 <rusty@rustcorp.com.au>
ppa
Rusty Russell 4 years ago
committed by Christian Decker
parent
commit
2de467274e
  1. 2
      channeld/channeld.c
  2. 40
      common/amount.c
  3. 8
      common/amount.h
  4. 12
      common/test/run-amount.c
  5. 2
      plugins/offers_inv_hook.c
  6. 4
      plugins/pay.c
  7. 4
      plugins/spender/multifundchannel.c
  8. 2
      plugins/spender/openchannel.c

2
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));

40
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);
}
static const char *fmt_amount_sat_ptr(const tal_t *ctx,
const struct amount_sat *sat)
{
return tal_fmt(ctx, "%"PRIu64"sat", sat->satoshis);
return fmt_amount_sat(ctx, *sat);
}
REGISTER_TYPE_TO_STRING(amount_sat, fmt_amount_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. */

8
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.

12
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);

2
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,

4
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",

4
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);

2
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();

Loading…
Cancel
Save