refactor: Remove redundant and confusing calls to IsArgSet #31896

pull maflcko wants to merge 3 commits into bitcoin:master from maflcko:2502-less-is-arg-set changing 5 files +42 −45
  1. maflcko commented at 3:25 pm on February 18, 2025: member

    IsArgSet is problematic:

    • It returns whether an arg has been set, even if it has been negated. IsArgSet is sometimes used to check for a truthy value, which is wrong, but usually harmless. Cleanup of those cases may or may not be done in a follow-up.
    • In most other cases, calling it is redundant, because the immediately following Get*Arg calls can already return an std::optional nullopt value to indicate an unset arg.

    So relieve both issues by removing all IsArgSet that are redundant.

  2. DrahtBot commented at 3:25 pm on February 18, 2025: contributor

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

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/31896.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK pablomartin4btc, ryanofsky
    Concept ACK yancyribbens, jonatack

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #32138 (wallet, rpc: remove settxfee and paytxfee by polespinasa)
    • #29278 (Wallet: Add maxfeerate wallet startup option by ismaelsadeeq)
    • #25722 (refactor: Use util::Result class for wallet loading by ryanofsky)

    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.

  3. DrahtBot added the label Refactoring on Feb 18, 2025
  4. ryanofsky approved
  5. ryanofsky commented at 9:13 pm on February 18, 2025: contributor
    Code review ACK fa3fb3c23fae287b161112b9b04cf0937a1051c6. Nice refactoring to get rid of pointless complexity and repetition in this code.
  6. yancyribbens commented at 10:34 pm on February 18, 2025: contributor

    It is confusing, because the provided fallback is dead code. So it would be better to just call GetArg without a fallback.

    I’m not 100% sure when reading the commit what is meant by dead code. Is it that the arg -blockmintxfee is a no longer used switch here? I ask because I notice some other calls to GetArg with fallbacks such as:

    0src/init.cpp:879:    if (args.IsArgSet("-upnp")) {
    
  7. in src/init.cpp:1014 in fa3fb3c23f outdated
    1011@@ -1012,20 +1012,20 @@ bool AppInitParameterInteraction(const ArgsManager& args)
    1012 
    1013     // Sanity check argument for min fee for including tx in block
    1014     // TODO: Harmonize which arguments need sanity checking and where that happens
    


    yancyribbens commented at 10:35 pm on February 18, 2025:
    Is this TODO still accurate? Not sure what arguments need Harmonizing.

    maflcko commented at 7:43 am on February 19, 2025:
    Not sure. It is not immediately actionable and probably opinionated or unclear, so best to remove. Though, I don’t want to do it here. Maybe a follow-up pull could do it?

    yancyribbens commented at 3:26 pm on February 19, 2025:
    I agree the TODO isn’t very clear and possibly no longer applicable. Maybe it could be rounded up with other unclear comments and TODOs in a separate PR. A PR to just remove this one TODO line seems silly.

    maflcko commented at 2:40 pm on March 16, 2025:
    removed it here. Should be trivial to re-review
  8. yancyribbens commented at 10:37 pm on February 18, 2025: contributor
    Looks like a worthwhile refactor. Better than an LLM could do ;)
  9. ryanofsky commented at 11:40 pm on February 18, 2025: contributor

    It is confusing, because the provided fallback is dead code. So it would be better to just call GetArg without a fallback.

    I’m not 100% sure when reading the commit what is meant by dead code.

    It is just saying the fallback argument is unused. For example in:

    0   if (gArgs.IsArgSet("-color")) {
    1   {
    2        const std::string color{gArgs.GetArg("-color", DEFAULT_COLOR_SETTING)};
    3        ...
    4   }
    

    The DEFAULT_COLOR_SETTING argument is unused, and any fallback value that is passed would be ignored because IsArgSet returned true.

  10. jonatack commented at 5:48 pm on February 22, 2025: member
    Concept ACK
  11. in src/wallet/wallet.cpp:3130 in fa3fb3c23f outdated
    3122-        std::optional<CAmount> min_tx_fee = ParseMoney(args.GetArg("-mintxfee", ""));
    3123+    if (const auto arg{args.GetArg("-mintxfee")}) {
    3124+        std::optional<CAmount> min_tx_fee = ParseMoney(*arg);
    3125         if (!min_tx_fee) {
    3126-            error = AmountErrMsg("mintxfee", args.GetArg("-mintxfee", ""));
    3127+            error = AmountErrMsg("mintxfee", *arg);
    


    pablomartin4btc commented at 4:00 pm on February 24, 2025:
    Also redundant/ repeated calls to args.GetArg were removed…
  12. pablomartin4btc commented at 4:27 pm on February 24, 2025: member

    cr & tACK fa3fb3c23fae287b161112b9b04cf0937a1051c6

    What about the IsArgSet ones followed by GetPathArg?

  13. DrahtBot requested review from jonatack on Feb 24, 2025
  14. DrahtBot requested review from yancyribbens on Feb 24, 2025
  15. maflcko commented at 8:10 am on February 25, 2025: member

    What about the IsArgSet ones followed by GetPathArg?

    Not sure what you mean, but GetPathArg does not return std::optional<_>, so I don’t think the changes that are done here are applicable to it. There may or may not be cleanups related to GetPathArg, but my recommendation would be to create a separate, unrelated and independent issue or pull request to discuss or propse changes.

  16. achow101 added the label Has ACKs on Mar 4, 2025
  17. achow101 removed the label Has ACKs on Mar 4, 2025
  18. maflcko requested review from pablomartin4btc on Mar 20, 2025
  19. pablomartin4btc approved
  20. pablomartin4btc commented at 10:36 am on March 20, 2025: member

    re-ACK fa0cf040baa74b161b4b00ddea95bfb5aac0bb53

    Since my last review: outdated TODO comment was removed (on new commit) - no impact.

  21. DrahtBot requested review from ryanofsky on Mar 20, 2025
  22. fanquake commented at 7:25 am on March 21, 2025: member
    @ryanofsky Want to take another look?
  23. ryanofsky approved
  24. ryanofsky commented at 4:32 pm on March 24, 2025: contributor
    Code review ACK fa0cf040baa74b161b4b00ddea95bfb5aac0bb53. Only change since last review is removing a nearby todo comment that was vague
  25. DrahtBot added the label Needs rebase on Mar 24, 2025
  26. in src/bitcoin-cli.cpp:1062 in fa41685f43 outdated
    1077@@ -1078,7 +1078,7 @@ static void ParseGetInfoResult(UniValue& result)
    1078     }
    1079 #endif
    1080 
    1081-    if (gArgs.IsArgSet("-color")) {
    1082+    {
    


    jonatack commented at 5:52 pm on March 24, 2025:
    fa41685f438bba00761 For the 3 conditionals dropped in this commit, any reason to keep the scope braces?

    maflcko commented at 9:47 am on March 25, 2025:
    Yes, I like scope braces, so that the scope is limited for symbols inside the scope. Also, to retain the prior scope like is was previously in the code.
  27. in src/init.cpp:1184 in fa3fb3c23f outdated
    1156@@ -1157,11 +1157,10 @@ bool CheckHostPortOptions(const ArgsManager& args) {
    1157         "-port",
    1158         "-rpcport",
    1159     }) {
    1160-        if (args.IsArgSet(port_option)) {
    1161-            const std::string port = args.GetArg(port_option, "");
    1162+        if (const auto port{args.GetArg(port_option)}) {
    


    jonatack commented at 6:00 pm on March 24, 2025:

    fa3fb3c23fae287b161112b9b04cf0937a1051c6 For all GetArg calls in this commit, perhaps I am confused but should they be const ref instead? Would also be clearer to continue to specify the type here, as before on line 1161, to avoid having to look up the type returned by GetArg().

    0        if (const std::string& port{args.GetArg(port_option)}) {
    

    maflcko commented at 9:47 am on March 25, 2025:

    The getters return a value, not a pointer or reference. C++ allows to bind the return value to either a reference or a value, but the end result is the same and either is fine here in this context.

    However, the type is not std::string as claimed by you. (This can be seen by trying to compile your suggestion with a C++ compiler and observing the compile error).

    Also, the type wasn’t mentioned previously in the code for most of the GetArg calls, so it seems better to leave it this way.

  28. in src/init.cpp:1040 in fa0cf040ba outdated
    1009@@ -1010,8 +1010,6 @@ bool AppInitParameterInteraction(const ArgsManager& args)
    1010         return InitError(Untranslated("peertimeout must be a positive integer."));
    1011     }
    1012 
    1013-    // Sanity check argument for min fee for including tx in block
    1014-    // TODO: Harmonize which arguments need sanity checking and where that happens
    


    jonatack commented at 6:04 pm on March 24, 2025:
    fa0cf040baa74b161b4b00ddea95bfb5aac0bb53 for anyone curious, these two lines are from daec955fd68b (https://github.com/bitcoin/bitcoin/pull/9380)
  29. jonatack commented at 6:28 pm on March 24, 2025: member

    Concept ACK.

    Part of the commit message of fa41685f438bba00761423d464bbb8dc5ea7ddf6 seems incorrect (maybe was pasted from fa3fb3c23fae287b161112b9b04cf0937a1051c6 without updating, not sure).

  30. refactor: Remove IsArgSet guard when fallback value is provided
    Checking for IsArgSet before calling GetArg while providing the args
    default value as fallback is both confusing and fragile.
    
    It is confusing, because the provided fallback is dead code. So it would
    be better to just call GetArg without a fallback.
    
    However, ignoring the fallback value is fragile, because it would not be
    sanitized.
    
    Fix all issues by sanitizing the fallback value.
    fa29842c1f
  31. refactor: Remove redundant call to IsArgSet
    Checking for IsArgSet before calling GetArg while providing an arbitrary
    default value as fallback is both confusing and fragile.
    
    It is confusing, because the provided fallback is dead code. So it would
    be better to just call GetArg without a fallback.
    
    Even better would be to provide the true fallback value and sanitize it
    as if it were user-input, but this can be done in a follow-up.
    
    Removing the redundant call to IsArgSet will have to be done either way,
    so do it now.
    fa2b529f92
  32. doc: Remove outdated and stale todo comment
    If anything is left to be done, a new discussion issue or pull request
    can be created.
    0000fb3fd9
  33. maflcko force-pushed on Mar 25, 2025
  34. DrahtBot removed the label Needs rebase on Mar 25, 2025
  35. maflcko requested review from pablomartin4btc on Mar 25, 2025
  36. pablomartin4btc approved
  37. pablomartin4btc commented at 4:09 pm on March 25, 2025: member

    re-ACK 0000fb3fd9f01f8a8cbe9de0bdc080023b6f00fc

    No changes, just rebased (basically due to this #31278).

  38. DrahtBot requested review from jonatack on Mar 25, 2025
  39. DrahtBot requested review from ryanofsky on Mar 25, 2025
  40. ryanofsky approved
  41. ryanofsky commented at 4:29 pm on March 25, 2025: contributor
    Code review ACK 0000fb3fd9f01f8a8cbe9de0bdc080023b6f00fc. No changes since last review other than rebase.
  42. fanquake merged this on Mar 27, 2025
  43. fanquake closed this on Mar 27, 2025

  44. maflcko deleted the branch on Mar 27, 2025

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2025-03-31 09:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me