Skip to content

util: make ParseMoney return a std::optional<CAmount> - #22220

Merged
fanquake merged 2 commits into
bitcoin:masterfrom
fanquake:parse_money_optional
Aug 24, 2021
Merged

fanquake merged 2 commits into
bitcoin:masterfrom
fanquake:parse_money_optional

Conversation

@fanquake

Copy link
Copy Markdown
Member

Related discussion in #22193.

@maflcko

maflcko commented Jun 11, 2021 •

Copy link
Copy Markdown
Member

I was hoping to have both fixes in the same pull in different commits, since they require the reviewers to look up all call sites anyway:

@theStack

Copy link
Copy Markdown
Contributor

Concept ACK

@kiminuo

kiminuo commented Jun 11, 2021

Copy link
Copy Markdown
Contributor

Concept ACK


Just out of curiosity, I was interested how this changes perf numbers for ParseMoney. It's not important though because the function is not used in a repeated way anywhere. Anyway, a benchmark says:

ns/op op/s err% total benchmark
1,200.00 833,333.33 1.7% 0.00 MoneyStrParseMoney
1,388.89 720,000.00 1.8% 0.00 MoneyStrParseMoneyNew

(No guarrantees, I have tried this for the first time.)

@DrahtBot

DrahtBot commented Jun 11, 2021 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #22766 (refactor: Clarify and disable unused ArgsManager flags by ryanofsky)
  • #22183 (Remove gArgs from wallet.h and wallet.cpp by kiminuo)
  • #22044 (Sanitize fee options by amadeuszpawlik)
  • #20452 (util: Replace use of locale dependent atoi(…) with locale-independent std::from_chars(…) (C++17) by practicalswift)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@laanwj

laanwj commented Jun 11, 2021

Copy link
Copy Markdown
Member

Concept ACK, I like this kind of API much better than passing around separate booleans or error codes.

@practicalswift

practicalswift commented Jun 11, 2021 •

Copy link
Copy Markdown
Contributor

Concept ACK

Non-blocking nit: Personally I think auto foo = [RHS]; is appropriate in contexts where the type of foo is immediately obvious from reading [RHS] (such as when [RHS] is std::make_unique<SomeType>(…), v.begin() or similar), or when the type is irrelevant. In the context of this PR I think std::optional<CAmount> parsed = ParseMoney(…); would be slightly more clear compared to the suggested auto parsed = ParseMoney(…);.

Comment thread src/init.cpp Outdated
return InitError(AmountErrMsg("incrementalrelayfee", args.GetArg("-incrementalrelayfee", "")));
incrementalRelayFee = CFeeRate(n);
const auto arg = args.GetArg("-incrementalrelayfee", "");
if (auto inc_relay_fee = ParseMoney(arg)) {

@laanwj laanwj Aug 2, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

TFW no rust-like unwrapping pattern matching if let Some(inc_relay_fee) = Parsemoney(arg) {

@laanwj

laanwj commented Aug 2, 2021

Copy link
Copy Markdown
Member

In the context of this PR I think std::optional parsed = ParseMoney(…); would be slightly more clear compared to the suggested auto parsed = ParseMoney(…);.

I hate the verbosity but I do agree in this case, the return type is not obvious.

@fanquake
fanquake force-pushed the parse_money_optional branch from 81ceb56 to b3c0e67 Compare August 3, 2021 05:08
@fanquake

fanquake commented Aug 3, 2021

Copy link
Copy Markdown
Member Author

Check money range inside ParseMoney

I've added that now.

I hate the verbosity but I do agree in this case, the return type is not obvious.

Changed to using std::optional<CAmount> over auto.

I've also rebased and removed some unrelated arg refactoring. Will PR that separately.

@maflcko maflcko left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

approach ACK, didn't review the src/wallet/wallet.cpp part yet

Comment thread src/init.cpp Resolved
Comment thread src/init.cpp Resolved
Comment thread src/wallet/wallet.cpp Resolved
@fanquake
fanquake force-pushed the parse_money_optional branch from b3c0e67 to 6aeed8d Compare August 3, 2021 08:12
@fanquake

fanquake commented Aug 3, 2021

Copy link
Copy Markdown
Member Author

approach ACK, didn't review the src/wallet/wallet.cpp part yet

Addressed suggestions so far.

Comment thread src/wallet/wallet.cpp Resolved
Comment thread src/wallet/wallet.cpp Resolved
Comment thread src/miner.cpp Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think this improves clarity. Previously, ParseMoney() would only be called if -blockmintxfee is set. Now, we call ParseMoney() even if -blockmintxfee has not been set, and rely on the fact that it'll return a nullopt if we pass it an empty string.

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.

Reverted to something more like the previous code.

@fanquake
fanquake force-pushed the parse_money_optional branch from 974abfb to f7752ad Compare August 4, 2021 11:49
Fabcien pushed a commit to Fabcien/bitcoin that referenced this pull request Aug 4, 2021
…utType>

32fa49a make ParseOutputType return a std::optional<OutputType> (fanquake)

Pull request description:

  Similar to bitcoin#22220. Skipped using `auto` here for the same reasons outlined in that PR.

ACKs for top commit:
  jnewbery:
    utACK 32fa49a
  jonatack:
    Code review ACK 32fa49a and debian clang 13 debug build is clean / unit tests locally are green
  MarcoFalke:
    review ACK 32fa49a 🍢

Tree-SHA512: 7752193117669b800889226185d49d164395697853828f8acb568f07651789bc5b2cddc45555957450353886e46b9a1e13c77a5e730a14c6ee621fabc8dc3d10
@maflcko

maflcko commented Aug 23, 2021

Copy link
Copy Markdown
Member

review ACK f7752ad 📄

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

review ACK f7752adba5dd35fccd3f2144cfcf03538ebf275b 📄
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUh9JgwApNfnr4XDam6JFxoS7iOSf7DQeVVVn0nExZiL2GHSj9w0cy83qgzgTGMb
pMxS4Qym8eonS05VJ1lhkFiGWmZ1dyNdxzHw9roxsFw6KXKyzV4SUG9RSKc01fzL
6pNeJ4tC05HuYV6k6MjiI6lRetkWXBf6BYHQFA7QYW1CGi3FVpfavhmd3o5uXr+b
pgb2sWxokm6Gnqs0yq2ZlR3D+hCDxPrgjpj1C0MJ1VC3R4raSwYxetyBrSBFJj/v
oFOFOgVNOhFohA7r+xoehN8TWeoMvywGKxzLGjcGaQSc/HQSKVvtUOeke6mTJBze
jEtSCQlrcxqd+kdVE/+j9NTesaZldMnp84dMEzfbc3dvSz/MThsT3nO/+eG26D9j
wKeynSG82aLymQpH7OqcFho7+FpkqsGjYEwnnpUD5S8EwauQvGoY0Ab/cW2axYMQ
aP/V9Q0PlMiA+rcm/Ut9QFZ4WHhl/eC2FZbUwFW8j6An3sYNRhEietOo+jdc2EN2
cYVt+7du
=cjvz
-----END PGP SIGNATURE-----

Timestamp of file with hash 78c354d0f3f17696d32ea10f11f0a9424812baff1f8ecdedfe5fcbc144129d53 -

@fanquake
fanquake merged commit 61a843e into bitcoin:master Aug 24, 2021
@fanquake
fanquake deleted the parse_money_optional branch August 24, 2021 02:51
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants