Browse Source

amount: minor comment and text improvements and remove unused function.

Reported-by: @niftynei
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
confirmed-only
Rusty Russell 6 years ago
parent
commit
023923e6a8
  1. 10
      common/amount.c
  2. 10
      common/amount.h
  3. 10
      common/test/run-amount.c

10
common/amount.c

@ -24,16 +24,6 @@ struct amount_sat amount_msat_to_sat_round_down(struct amount_msat msat)
return sat;
}
/* You may not be able to do this. */
bool amount_msat_to_sat_exact(struct amount_sat *sat,
const struct amount_msat *msat)
{
if (msat->millisatoshis % MSAT_PER_SAT != 0)
return false;
sat->satoshis = msat->millisatoshis / MSAT_PER_SAT;
return true;
}
/* Different formatting by amounts: btc, sat and msat */
const char *fmt_amount_msat_btc(const tal_t *ctx,
const struct amount_msat *msat,

10
common/amount.h

@ -39,10 +39,6 @@ struct amount_msat {
WARN_UNUSED_RESULT bool amount_sat_to_msat(struct amount_msat *msat,
struct amount_sat sat);
/* This may require rounding. */
WARN_UNUSED_RESULT bool amount_msat_to_sat_exact(struct amount_sat *,
const struct amount_msat *);
/* You can always truncate millisatoshis->satoshis. */
struct amount_sat amount_msat_to_sat_round_down(struct amount_msat msat);
@ -110,16 +106,18 @@ WARN_UNUSED_RESULT bool amount_msat_add_fee(struct amount_msat *amt,
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,
bool append_unit);
/* 1234msat */
/* => 1234msat */
const char *fmt_amount_msat(const tal_t *ctx, const struct amount_msat *msat);
/* => 1.23456789btc (8 decimals!) */
const char *fmt_amount_sat_btc(const tal_t *ctx,
const struct amount_sat *sat,
bool append_unit);
/* 1234sat */
/* => 1234sat */
const char *fmt_amount_sat(const tal_t *ctx, const struct amount_sat *sat);
/* Valid strings:

10
common/test/run-amount.c

@ -36,15 +36,17 @@ int main(void)
FAIL_MSAT(&msat, "0.00000000");
FAIL_MSAT(&msat, "0.00000000000");
FAIL_MSAT(&msat, "0.00000000msat");
FAIL_MSAT(&msat, "0.00000000000msat");
FAIL_MSAT(&msat, "-1");
PASS_MSAT(&msat, "0msat", 0);
PASS_MSAT(&msat, "1msat", 1);
PASS_MSAT(&msat, "2100000000000000000msat", 2100000000000000000ULL);
FAIL_MSAT(&msat, "-1msat");
PASS_MSAT(&msat, "0sat", 0);
PASS_MSAT(&msat, "1sat", 1000);
PASS_MSAT(&msat, "2100000000000000sat", 2100000000000000000ULL);
FAIL_MSAT(&msat, "-1sat");
PASS_MSAT(&msat, "0.00000000btc", 0);
PASS_MSAT(&msat, "0.00000000000btc", 0);
@ -55,6 +57,8 @@ int main(void)
FAIL_MSAT(&msat, "1btc");
FAIL_MSAT(&msat, "1.0000000btc");
FAIL_MSAT(&msat, "1.000000000btc");
FAIL_MSAT(&msat, "-1.23456789btc");
FAIL_MSAT(&msat, "-1.23456789012btc");
/* Overflowingly big. */
FAIL_MSAT(&msat, "21000000000000000000000000.00000000btc");
@ -71,10 +75,12 @@ int main(void)
FAIL_SAT(&sat, "0.00000000000");
FAIL_SAT(&sat, "0.00000000sat");
FAIL_SAT(&sat, "0.00000000000msat");
FAIL_SAT(&sat, "-1");
PASS_SAT(&sat, "0sat", 0);
PASS_SAT(&sat, "1sat", 1);
PASS_SAT(&sat, "2100000000000000sat", 2100000000000000ULL);
FAIL_SAT(&sat, "-1sat");
PASS_SAT(&sat, "1000msat", 1);
PASS_SAT(&sat, "1000000msat", 1000);
@ -82,6 +88,7 @@ int main(void)
FAIL_SAT(&sat, "0msat");
FAIL_SAT(&sat, "100msat");
FAIL_SAT(&sat, "2000000000000000999msat");
FAIL_SAT(&sat, "-1000msat");
PASS_SAT(&sat, "0.00000000btc", 0);
FAIL_SAT(&sat, "0.00000000000btc");
@ -92,6 +99,7 @@ int main(void)
FAIL_SAT(&sat, "1btc");
FAIL_SAT(&sat, "1.0000000btc");
FAIL_SAT(&sat, "1.000000000btc");
FAIL_SAT(&sat, "-1.23456789btc");
/* Overflowingly big. */
FAIL_SAT(&sat, "21000000000000000000000000.00000000btc");

Loading…
Cancel
Save