Skip to content

Commit

Permalink
amount: minor comment and text improvements and remove unused function.
Browse files Browse the repository at this point in the history
Reported-by: @niftynei
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Feb 21, 2019
1 parent 487ad02 commit 957c39b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
10 changes: 0 additions & 10 deletions common/amount.c
Expand Up @@ -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,
Expand Down
10 changes: 4 additions & 6 deletions common/amount.h
Expand Up @@ -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);

Expand Down Expand Up @@ -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:
Expand Down
10 changes: 9 additions & 1 deletion common/test/run-amount.c
Expand Up @@ -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);
Expand All @@ -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");
Expand All @@ -71,17 +75,20 @@ 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);
PASS_SAT(&sat, "2100000000000000000msat", 2100000000000000ULL);
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");
Expand All @@ -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");
Expand Down

0 comments on commit 957c39b

Please sign in to comment.