Improve parsing of Denomination string#731
Conversation
|
From one side, this breaks |
|
Concept ACK from me as well. Although I wouldn't say it's required. |
Kixunil
left a comment
There was a problem hiding this comment.
I'm "requesting changes" but if anyone disagrees, please explain.
src/util/amount.rs
Outdated
There was a problem hiding this comment.
I think ifs with eq_ignore_ascii_case would be better - more concise and obvious what it does. It would also allow ridiculous strings like 10 sAtS but I don't see harm in it.
There was a problem hiding this comment.
Maybe we could forbid Msat (any sats starting with capital M) as suspicious - possibly meaning "mega sats"
There was a problem hiding this comment.
Done. In case you are wondering what all the force pushing is about. I removed the suffix 's' but didn't test locally with 1.29 so it was no good :(
The code is pretty ugly.
ce8fc98 to
3f9daff
Compare
src/util/amount.rs
Outdated
There was a problem hiding this comment.
Maybe we could have PossiblyConfusingDenomination variant or something similar.
I'm still unsure if we should allow MSAT, MSat and similar.
There was a problem hiding this comment.
Can do, will re-spin later today.
src/util/amount.rs
Outdated
There was a problem hiding this comment.
How about making s mut, adding:
if s.ends_with('s') || s.ends_with('S') {
s = &s[..(s.len() - 1)];
}And matching only singular from here?
There was a problem hiding this comment.
Ha ha, I wanted to do this as well but remember we cannot index strings in Rust because of utf8. I didn't want to do an allocation so no String methods either. Then I tried strip_suffix, not noticing it was Rust 1.45. Hence I reverted to the ugly if clauses again.
There was a problem hiding this comment.
we cannot index strings in Rust because of utf8
We can, we just have to do it by bytes and given that s and S are ASCII, it's exactly 1 byte, so indexing is fine (and if we're lucky the compiler will even optimize-out bounds check).
There was a problem hiding this comment.
Good points!
Please re-review, its looking hot now :)
7835d98 to
8625501
Compare
Kixunil
left a comment
There was a problem hiding this comment.
I believe instead of having match at the end it'd be more robust to check s.starts_with('M') and reject it at the beginning. If you want to avoid returning PossiblyConfusingDenomination for things like Mfoo, make a helper function that doesn't check M and call it also from error handling with &s[1..]
src/util/amount.rs
Outdated
There was a problem hiding this comment.
Note, s will be stripped here.
src/util/amount.rs
Outdated
There was a problem hiding this comment.
Should we allow mSat? I don't see any harm in it.
src/util/amount.rs
Outdated
There was a problem hiding this comment.
Actually, Mbtc could mean "mega btc"
There was a problem hiding this comment.
ha, I missed another one. Thanks for paying close attention to this one @Kixunil!
8625501 to
6ee3fff
Compare
|
I've had another got at this one, note the addition of a match statement at the start of the function now. |
11d2abc to
42351f0
Compare
|
Could you please explain why the change you made is better than my suggestion? Am I missing something? |
I think the problem between using "m" as milli and "M" as mega is a reason strong enough to keep casing. |
|
@RCasatta thoughts on why just banning |
I guess it would be fine if mega isn't allowed and is the only ambiguity, I also checked in https://github.com/lightning/bolts/blob/master/11-payment-encoding.md and it's not a valid multiplier |
42351f0 to
10010fd
Compare
I missed your suggestion originally, sorry (I'm on holidays ATM just hacking with half my brain :) New and improved version force pushed, incl. helper function as suggested. |
Kixunil
left a comment
There was a problem hiding this comment.
Nearly perfect. I just realized the error message would be confusing for the users and I really, really want to have the message clear. (Clear error messages are like 80% of debugging success.) Sorry about one more round.
|
API breaking because of the new variant. |
There is no reason to force users to use one particular form when providing a denomination string. We can be liberal in what we accept with no loss of clarity. Allow `Denomination` strings to use a variety of forms, in particular lower case and uppercase. Note, we explicitly disallow various forms of `Msat` because it is ambiguous whether this means milli or mega sats. Co-developed-by: Martin Habovštiak <martin.habovstiak@gmail.com>
10010fd to
f690b8e
Compare
|
@Kixunil, I added a |
How many Mega-bitcoin do you have? |
|
I think this is good to merge BTW -- though I think we should wait for 24h after my ACK, as I've gotten complaints before when I show up and ack/merge tons of PRs without warning after weeks of silence. |
|
More than 24 hours passed... Merged |
There is no reason to force users to use a particular format or case for
Denominationstrings. Users may wish to write any of the following and all seem reasonableThe same goes for various other
Denominations.Fixes: #729