Skip to content

Improve parsing of Denomination string#731

Merged
RCasatta merged 2 commits intorust-bitcoin:masterfrom
tcharding:denomination-string
Dec 27, 2021
Merged

Improve parsing of Denomination string#731
RCasatta merged 2 commits intorust-bitcoin:masterfrom
tcharding:denomination-string

Conversation

@tcharding
Copy link
Copy Markdown
Member

There is no reason to force users to use a particular format or case for Denomination strings. Users may wish to write any of the following and all seem reasonable

  • 100 sats
  • 100 sat
  • 100 SAT

The same goes for various other Denominations.

  • Patch 1 enables usage of "sats", "sat", "bit", "bits"
  • Patch 2 enables usage of various lower/uper case formatting

Fixes: #729

@dr-orlovsky
Copy link
Copy Markdown
Collaborator

From one side, this breaks Display/FromStr round-trip. From the other, it was already broken and this will be a required companion for the proposal by @Kixunil on new formatting for Amount. So concept ACK

@Kixunil
Copy link
Copy Markdown
Collaborator

Kixunil commented Dec 1, 2021

Concept ACK from me as well. Although I wouldn't say it's required.

Copy link
Copy Markdown
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

I'm "requesting changes" but if anyone disagrees, please explain.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe we could forbid Msat (any sats starting with capital M) as suspicious - possibly meaning "mega sats"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@tcharding tcharding force-pushed the denomination-string branch 3 times, most recently from ce8fc98 to 3f9daff Compare December 2, 2021 05:12
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe we could have PossiblyConfusingDenomination variant or something similar.

I'm still unsure if we should allow MSAT, MSat and similar.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Can do, will re-spin later today.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good points!

Please re-review, its looking hot now :)

@Kixunil Kixunil added the waiting for author This can only progress if the author responds to a request. label Dec 2, 2021
@tcharding tcharding force-pushed the denomination-string branch 2 times, most recently from 7835d98 to 8625501 Compare December 3, 2021 04:02
Copy link
Copy Markdown
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

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..]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Note, s will be stripped here.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should we allow mSat? I don't see any harm in it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Actually, Mbtc could mean "mega btc"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

ha, I missed another one. Thanks for paying close attention to this one @Kixunil!

@tcharding
Copy link
Copy Markdown
Member Author

I've had another got at this one, note the addition of a match statement at the start of the function now.

@tcharding tcharding requested a review from Kixunil December 10, 2021 00:16
@tcharding tcharding force-pushed the denomination-string branch 2 times, most recently from 11d2abc to 42351f0 Compare December 10, 2021 02:14
@Kixunil
Copy link
Copy Markdown
Collaborator

Kixunil commented Dec 10, 2021

Could you please explain why the change you made is better than my suggestion? Am I missing something?

@RCasatta
Copy link
Copy Markdown
Collaborator

There is no reason to force users to use a particular format or case for Denomination strings.

I think the problem between using "m" as milli and "M" as mega is a reason strong enough to keep casing.

@Kixunil
Copy link
Copy Markdown
Collaborator

Kixunil commented Dec 10, 2021

@RCasatta thoughts on why just banning M is not sufficient?

@RCasatta
Copy link
Copy Markdown
Collaborator

@RCasatta thoughts on why just banning M is not sufficient?

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

@tcharding
Copy link
Copy Markdown
Member Author

Could you please explain why the change you made is better than my suggestion? Am I missing something?

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.

@tcharding
Copy link
Copy Markdown
Member Author

I created an issue thanks to your findings in BOLT 11 @RCasatta: #741

@Kixunil Kixunil added enhancement and removed waiting for author This can only progress if the author responds to a request. labels Dec 13, 2021
Copy link
Copy Markdown
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

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.

@Kixunil Kixunil added waiting for author This can only progress if the author responds to a request. API break This PR requires a version bump for the next release labels Dec 13, 2021
@Kixunil
Copy link
Copy Markdown
Collaborator

Kixunil commented Dec 14, 2021

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>
@tcharding
Copy link
Copy Markdown
Member Author

@Kixunil, I added a Co-developed-by: tag to the second commit of this PR, you've been a driving force of this work.

@tcharding tcharding requested a review from Kixunil December 22, 2021 02:54
Copy link
Copy Markdown
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

ACK f690b8e

Thanks!

Copy link
Copy Markdown
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK f690b8e

lol @ the "this denomination is uncommon" megabitcoin error

@tcharding
Copy link
Copy Markdown
Member Author

tcharding commented Dec 23, 2021

lol @ the "this denomination is uncommon" megabitcoin error

How many Mega-bitcoin do you have?

@apoelstra
Copy link
Copy Markdown
Member

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.

@RCasatta RCasatta merged commit 9e1f256 into rust-bitcoin:master Dec 27, 2021
@RCasatta
Copy link
Copy Markdown
Collaborator

More than 24 hours passed... Merged

@tcharding tcharding deleted the denomination-string branch March 15, 2022 04:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API break This PR requires a version bump for the next release enhancement waiting for author This can only progress if the author responds to a request.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

non-uniform usage of plural in Denomination

5 participants