From 023923e6a8440357579197e4c6cf672f98814613 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 21 Feb 2019 10:39:55 +1030 Subject: [PATCH] amount: minor comment and text improvements and remove unused function. Reported-by: @niftynei Signed-off-by: Rusty Russell --- common/amount.c | 10 ---------- common/amount.h | 10 ++++------ common/test/run-amount.c | 10 +++++++++- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/common/amount.c b/common/amount.c index e1b2a8b5f..56ddd269d 100644 --- a/common/amount.c +++ b/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, diff --git a/common/amount.h b/common/amount.h index e43c7b7ad..37cb51af1 100644 --- a/common/amount.h +++ b/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: diff --git a/common/test/run-amount.c b/common/test/run-amount.c index e12fb7eee..b64087c89 100644 --- a/common/test/run-amount.c +++ b/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");