Skip to content

Commit

Permalink
common/amount: accept 0msat in parse_amount_sat()
Browse files Browse the repository at this point in the history
Changelog-fixed: bcli now handles 0msat outputs in gettxout.

Co-authored-by: Rusty Russell <[email protected]>
  • Loading branch information
darosior and rustyrussell committed Mar 30, 2020
1 parent 8c984fb commit ecb49d1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 6 additions & 1 deletion common/amount.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ bool parse_amount_msat(struct amount_msat *msat, const char *s, size_t slen)
* [0-9]+ => satoshi.
* [0-9]+sat => satoshi.
* [0-9]+000msat => satoshi.
* 0msat => 0 satoshi
* [0-9]+.[0-9]{1,8}btc => satoshi.
*/
bool parse_amount_sat(struct amount_sat *sat, const char *s, size_t slen)
Expand All @@ -198,8 +199,12 @@ bool parse_amount_sat(struct amount_sat *sat, const char *s, size_t slen)
if (!post_decimal_ptr && memeqstr(suffix_ptr, suffix_len, "sat"))
return from_number(&sat->satoshis, s, whole_number_len, 0);
if (!post_decimal_ptr && memeqstr(suffix_ptr, suffix_len, "msat")) {
if (!memends(s, whole_number_len, "000", strlen("000")))
if (!memends(s, whole_number_len, "000", strlen("000"))) {
if (memeqstr(s, whole_number_len, "0"))
return from_number(&sat->satoshis, s,
whole_number_len, 0);
return false;
}
return from_number(&sat->satoshis, s, whole_number_len - 3, 0);
}
if (post_decimal_ptr && memeqstr(suffix_ptr, suffix_len, "btc"))
Expand Down
2 changes: 1 addition & 1 deletion common/test/run-amount.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ int main(void)
PASS_SAT(&sat, "1000msat", 1);
PASS_SAT(&sat, "1000000msat", 1000);
PASS_SAT(&sat, "2100000000000000000msat", 2100000000000000ULL);
FAIL_SAT(&sat, "0msat");
PASS_SAT(&sat, "0msat", 0);
FAIL_SAT(&sat, "100msat");
FAIL_SAT(&sat, "2000000000000000999msat");
FAIL_SAT(&sat, "-1000msat");
Expand Down

0 comments on commit ecb49d1

Please sign in to comment.