Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bcli: don't get upset at 0-value outputs #3605

Merged
merged 1 commit into from
Mar 31, 2020

Conversation

darosior
Copy link
Collaborator

@darosior darosior commented Mar 25, 2020

@darosior
Copy link
Collaborator Author

darosior commented Mar 25, 2020

Maybe we should also filter out 0-value outputs in getfilteredblock ?
EDIT: cdecker answered on IRC:

Hm, shouldn't cause any issues at the moment, but eventually we want to support liquid assets (which'd have 0sats)
So it may be better not to filter them out

Copy link
Contributor

@rustyrussell rustyrussell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We made a deliberate choice to expose all values in msat; older values can be deprecated at some stage. We were doing a lot of deprecating at the time, so we chose not to do it then.

Creating this function is dangerous; we should instead allow '0msat' to do what we expect in parse_amount_sat.

@darosior
Copy link
Collaborator Author

Ok, amended to modify parse_amount_sat() instead.

@rustyrussell
Copy link
Contributor

No, that's wrong too!

parse_amount_sat() should accept msat amounts which end in 000, OR a literal 0. All it cares
about is that it's not rounding, and the "ends-in-000" test is a terrible way of doing that.

diff --git a/common/amount.c b/common/amount.c
index 7bb2e6d40..f5ae8dd62 100644
--- a/common/amount.c
+++ b/common/amount.c
@@ -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)
@@ -198,8 +199,11 @@ 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"))
diff --git a/common/test/run-amount.c b/common/test/run-amount.c
index 6b510277f..3b3e5596d 100644
--- a/common/test/run-amount.c
+++ b/common/test/run-amount.c
@@ -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");
rusty@rusty-XPS-13-9370:~/devel/cvs/lightning (guilt/wumbo)$ git diff > /tmp/diff
rusty@rusty-XPS-13-9370:~/devel/cvs/lightning (guilt/wumbo)$ cat /tmp/diff 
diff --git a/common/amount.c b/common/amount.c
index 7bb2e6d40..f5ae8dd62 100644
--- a/common/amount.c
+++ b/common/amount.c
@@ -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)
@@ -198,8 +199,11 @@ 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"))
diff --git a/common/test/run-amount.c b/common/test/run-amount.c
index 6b510277f..3b3e5596d 100644
--- a/common/test/run-amount.c
+++ b/common/test/run-amount.c
@@ -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");

@darosior
Copy link
Collaborator Author

Commited your patch..

fixes #3610

Changelog-fixed: bcli now handles 0msat outputs in gettxout.

Co-authored-by: Rusty Russell <rusty@rustcorp.com.au>
@kirelagin
Copy link

👍

Let’s get this merged? Seems to be quite a major issue, I’m not sure how it doesn’t show up more often.

@darosior
Copy link
Collaborator Author

darosior commented Mar 30, 2020 via email

@darosior
Copy link
Collaborator Author

Let’s get this merged?

Btw, maybe not the best way to ask for progress.
Since you encountered this issue you could test it and report the result (I believe an ACK coming from someone who's not a contributor but ran in the issue solved by the patched is still welcome).
You would even be able to keep running the fix until we release it :-)

@kirelagin
Copy link

Oh yeah, we’ve been running it since yesterday, everything is good so far.

It’s just that this fix is an obvious improvement in that it definitely does not make things worse, and, as I said, I think the issue is quite severe (a crash), so I don’t see reasons not to merge it right away.

@rustyrussell rustyrussell merged commit 13fbce9 into ElementsProject:master Mar 31, 2020
@rustyrussell
Copy link
Contributor

Thanks everyone! This was my thinko in the first place, of course!

@darosior darosior deleted the bcli_fixups branch April 1, 2020 00:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bad response to getutxout (bad sats amount)
3 participants