Introduce an ArgsManager class encapsulating cs_args, mapArgs and mapMultiArgs #9494

pull jtimon wants to merge 4 commits into bitcoin:master from jtimon:0.13-args-class changing 7 files +165 −96
  1. jtimon commented at 4:02 pm on January 9, 2017: contributor

    Continues #9243 by @TheBlueMatt Introduce an ArgsManager class encapsulating cs_args, mapArgs and mapMultiArgs.

    To reduce disruption, the used functions are kept as wrapers. The wrapers can be removed later, see https://github.com/jtimon/bitcoin/compare/0.13-args-class...jtimon:0.13-args-remove-wrapers for the less disruptive ones to remove.

  2. jtimon force-pushed on Jan 9, 2017
  3. TheBlueMatt commented at 8:35 pm on January 9, 2017: member

    Concept ACK. I think we had discussed doing this when #9243 went in.

    On January 9, 2017 11:02:04 AM EST, “Jorge Timón” notifications@github.com wrote:

    Continues #9243 by @TheBlueMatt Introduce an ArgsManager class encapsulating cs_args, mapArgs and mapMultiArgs.

    To reduce disruption, the used functions are kept as wrapers. The wrapers can be removed later, see https://github.com/jtimon/bitcoin/compare/0.13-args-class...jtimon:0.13-args-remove-wrapers for the less disruptive ones to remove.

    You can view, comment on, or merge this pull request online at:

    #9494

    – Commit Summary –

    • Util: Create ArgsManager class
    • Util: Expose ArgsManager argsGlobal
    • Util: s/mapMultiArgs.count(/argsGlobal.IsArgSet(/
    • Util: Introduce ArgsManager::ArgsAt()
    • Util: Put mapMultiArgs inside ArgsManager

    – File Changes –

    M src/httprpc.cpp (4) M src/httpserver.cpp (8) M src/init.cpp (48) M src/net.cpp (10) M src/test/util_tests.cpp (77) M src/util.cpp (83) M src/util.h (28)

    – Patch Links –

    https://github.com/bitcoin/bitcoin/pull/9494.patch https://github.com/bitcoin/bitcoin/pull/9494.diff

    – You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub: https://github.com/bitcoin/bitcoin/pull/9494

  4. fanquake added the label Refactoring on Jan 9, 2017
  5. jtimon commented at 10:47 pm on January 9, 2017: contributor

    Yes, we discussed it in #9243, but that this seems approximately what was discussed previously is still very useful, thanks.

    What concerns me more of the current code is the fact that the methods IsArgSet, GetArg, GetBoolArg and ArgsAt aren’t const, because they need to LOCK(cs_args);. The first “solution” that comes to mind is to make a superclass with just these methods (but they remain non-const).

  6. dcousens commented at 6:50 am on January 11, 2017: contributor
    concept ACK :+1: , will review again when able, once-over utACK 8a96939
  7. dcousens approved
  8. in src/util.cpp: in 8a96939c16 outdated
    379 }
    380 
    381-bool IsArgSet(const std::string& strArg)
    382+const std::vector<std::string>& ArgsManager::ArgsAt(const std::string& strArg) const
    383+{
    384+    return mapMultiArgs.at(strArg);
    


    jtimon commented at 1:34 pm on January 11, 2017:
    I assume this should get LOCK(cs_args); too? If so, the method can’t be const.

    TheBlueMatt commented at 5:05 pm on March 29, 2017:
    Yes, it also cant return a reference, needs to make a copy.
  9. jtimon force-pushed on Jan 31, 2017
  10. jtimon commented at 9:38 pm on January 31, 2017: contributor
    Needed rebase.
  11. jtimon force-pushed on Jan 31, 2017
  12. jtimon force-pushed on Mar 23, 2017
  13. jtimon commented at 10:14 pm on March 23, 2017: contributor
    Needed rebase (due to “Refactor: Remove using namespace ” PRs).
  14. laanwj commented at 12:55 pm on March 24, 2017: member
    Concept ACK, seems a move in the right direction.
  15. sipa commented at 9:57 pm on March 24, 2017: member
    utACK c67209128a114e3260ab93c3e8a63b1b23663b34
  16. TheBlueMatt commented at 1:52 am on March 25, 2017: member

    Concept ACK, will review.

    On March 24, 2017 2:57:16 PM PDT, Pieter Wuille notifications@github.com wrote:

    utACK c67209128a114e3260ab93c3e8a63b1b23663b34

    – You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub: https://github.com/bitcoin/bitcoin/pull/9494#issuecomment-289152860

  17. in src/httprpc.cpp:96 in 28f4ecb93a outdated
    92@@ -93,7 +93,7 @@ static bool multiUserAuthorized(std::string strUserPass)
    93     std::string strUser = strUserPass.substr(0, strUserPass.find(":"));
    94     std::string strPass = strUserPass.substr(strUserPass.find(":") + 1);
    95 
    96-    if (mapMultiArgs.count("-rpcauth") > 0) {
    97+    if (argsGlobal.IsArgSet("-rpcauth") > 0) {
    


    TheBlueMatt commented at 2:01 pm on March 28, 2017:
    nit: kill the > 0 here?
  18. TheBlueMatt commented at 3:51 pm on March 28, 2017: member
    Switching mapMultiArgs.count() to argsGlobal.IsArgSet makes a few things incorrect, because the SoftSetArgs stuff doesn’t update mapMultiArgs but does update mapArgs. I think the correct solution here is to keep mapMultiArgs and mapArgs consistent (@luke-jr also needed that for the multiwallet pull), though that does mean ArgsAt needs to return a copy and not a reference (which I think is OK - arg usage doesn’t need to be fast).
  19. jtimon commented at 6:46 pm on March 28, 2017: contributor

    Switching mapMultiArgs.count() to argsGlobal.IsArgSet makes a few things incorrect, because the SoftSetArgs stuff doesn’t update mapMultiArgs but does update mapArgs.

    Maybe a solution could be to use argsGlobal.IsArgsAt().size() > 0 in those cases? I mean, I think in all those cases will just try to loop through an empty vector (ie do nothing) inside the if clause, but I haven’t made sure of that. I could go through them and maybe replace some BOOST_FOREACH for C++ fors while at it. Never mind, it seems mapMultiArgs.count("-bind") is not like that, but could be fixed with argsGlobal.IsArgsAt().size() > 0 By “making mapMultiArgs and mapArg” I assume you just mean redundantly setting to mapMultiArgs in ArgsManager::SoftSetArg too as it is done in ArgsManager::ArgsAt. Perhaps that is simpler and easier to reason about.

    Thoughts?

  20. TheBlueMatt commented at 7:08 pm on March 28, 2017: member
    I’d prefer to just fix the issue - I think its super strange that SoftSetArg, etc dont keep mapMultiArgs and mapArgs consistent.
  21. jtimon force-pushed on Mar 29, 2017
  22. jtimon commented at 2:57 pm on March 29, 2017: contributor
    Fixed nits. Also removed a bunch of BOOST_FOREACH for free in the commit that introduces ArgsManager::ArgsAt().
  23. jtimon force-pushed on Mar 29, 2017
  24. jtimon force-pushed on Mar 29, 2017
  25. jtimon commented at 9:25 pm on March 29, 2017: contributor
    Fixed nit.
  26. jtimon force-pushed on Apr 4, 2017
  27. jtimon commented at 3:10 pm on April 4, 2017: contributor
    Needed rebase.
  28. JeremyRubin commented at 8:28 pm on April 5, 2017: contributor
    put my review of this code on the wrong PR. #10119 (review)
  29. jtimon force-pushed on Apr 5, 2017
  30. jtimon commented at 10:48 pm on April 5, 2017: contributor
    Needed rebase again. Btw, should I squash the last commit in #10119 (everything else is just equal to this PR) to the first commit here or leave it separated?
  31. jtimon force-pushed on Apr 6, 2017
  32. jtimon commented at 8:28 pm on April 6, 2017: contributor
    Needed rebase again.
  33. in src/util.cpp:417 in 996d2ae6ac outdated
    414+        mapMultiArgs[str].push_back(strValue);
    415     }
    416 }
    417 
    418-bool IsArgSet(const std::string& strArg)
    419+std::vector<std::string> ArgsManager::ArgsAt(const std::string& strArg)
    


    luke-jr commented at 7:44 pm on May 4, 2017:
    GetArgs seems like a better name?
  34. in src/util.cpp:477 in 996d2ae6ac outdated
    481     LOCK(cs_args);
    482     mapArgs[strArg] = strValue;
    483+    mapMultiArgs[strArg].push_back(strValue);
    484 }
    485 
    486+void ParseParameters(int argc, const char* const argv[])
    


    luke-jr commented at 7:46 pm on May 4, 2017:
    Suggest moving these to the header as static inline.

    jtimon commented at 11:49 am on May 5, 2017:
    I suspect a performance improvement is expected by doing so. Thanks
  35. in src/util.h:248 in 996d2ae6ac outdated
    233@@ -226,6 +234,20 @@ bool SoftSetBoolArg(const std::string& strArg, bool fValue);
    234 
    235 // Forces a arg setting, used only in testing
    236 void ForceSetArg(const std::string& strArg, const std::string& strValue);
    237+};
    238+
    239+extern ArgsManager argsGlobal;
    


    luke-jr commented at 7:46 pm on May 4, 2017:
    Can we just call it Args? :)

    jtimon commented at 11:53 am on May 5, 2017:
    I was thinking that maybe some functions could get a local ArgsManager& args parameters instead of using the global. Or do you mean s/ArgsManager/Args/ ? I’m fine with that. Also, maybe s/argsGlobal/gArgs/ too, but I don’t like extern ArgsManager args

    luke-jr commented at 2:29 pm on May 5, 2017:
    I meant extern ArgsManager Args, but gArgs is probably good enough.
  36. in src/init.cpp:737 in 996d2ae6ac outdated
    733@@ -734,7 +734,7 @@ void InitParameterInteraction()
    734             LogPrintf("%s: parameter interaction: -whitebind set -> setting -listen=1\n", __func__);
    735     }
    736 
    737-    if (mapMultiArgs.count("-connect") && mapMultiArgs.at("-connect").size() > 0) {
    738+    if (argsGlobal.IsArgSet("-connect") && argsGlobal.ArgsAt("-connect").size() > 0) {
    


    luke-jr commented at 7:48 pm on May 4, 2017:
    Under what conditions would IsArgSet be true, and ArgsAt().size() be zero?

    jtimon commented at 11:55 am on May 5, 2017:
    After the last change suggested by @TheBlueMatt this should never be possible, good catch.
  37. in src/init.cpp:909 in 996d2ae6ac outdated
    903@@ -904,9 +904,9 @@ bool AppInitParameterInteraction()
    904         InitWarning(strprintf(_("Reducing -maxconnections from %d to %d, because of system limitations."), nUserMaxConnections, nMaxConnections));
    905 
    906     // ********************************************************* Step 3: parameter-to-internal-flags
    907-    if (mapMultiArgs.count("-debug") > 0) {
    908+    if (argsGlobal.IsArgSet("-debug")) {
    909         // Special-case: if -debug=0/-nodebug is set, turn off debugging messages
    910-        const std::vector<std::string>& categories = mapMultiArgs.at("-debug");
    911+        const std::vector<std::string>& categories = argsGlobal.ArgsAt("-debug");
    


    luke-jr commented at 7:49 pm on May 4, 2017:
    Can you reference a temporary like this?

    sipa commented at 0:11 am on May 5, 2017:

    You can. The lifetime of the temporary is extended by taking a reference to it. This code is effectively equivalent to:

    0const std::vector<std::string> categories_tmp = argsGlobal.ArgsAt("-debug");
    1const std::vector<std::string>& categories = categories_tmp;
    

    jtimon commented at 11:57 am on May 5, 2017:
    At the same time, I didn’t do it on purpose here. I’m happy to remove the “&” if that’s preferred.
  38. luke-jr commented at 7:54 pm on May 4, 2017: member
    Looks basically okay.
  39. sipa added this to the "Blockers" column in a project

  40. jtimon force-pushed on May 5, 2017
  41. jtimon force-pushed on May 5, 2017
  42. jtimon commented at 1:11 pm on May 5, 2017: contributor
    Fixed some nits. I made the wrappers static inline and removed the less disruptive ones (which include ParseParameters and ReadConfigFile) as I was doing in #10119 (just squashed it to the first commit here, plus adapted 1 new use of SoftSetArg after rebase), but since that’s a little bit more disruptive I didn’t made the methods ParseParameters and ReadConfigFile inline yet. Also found more cases where ArgsAt/GetArgs didn’t need to be called after calling IsArgSet.
  43. jtimon force-pushed on May 6, 2017
  44. jtimon force-pushed on May 7, 2017
  45. jtimon commented at 7:04 pm on May 7, 2017: contributor

    Renamed s/globalArgs/gArgs/ as suggested discussed with @luke-jr here. @TheBlueMatt asked for using scripted-diff so I ended up “bikeshedding the history” quite a bit, but I think it’s clearer now. I tried to follow @luke-jr ’s suggestion #9494 (review) in more places, but I don’t understand why this breaks the python tests yet: https://github.com/jtimon/bitcoin/commit/32bffa0162a0ebd66c554dab4b35705e664845dc

    Also, sorry for going back and forth with removing some wrappers directly or not, but with scripted-diff I think it’s better to just remove them all at once after this. But if we want to do the simpler ones without scripted-diff here I still have that in https://github.com/jtimon/bitcoin/tree/b15-args-class-wrappers

  46. in src/util.cpp:478 in b10ec118d3 outdated
    481-void ForceSetArg(const std::string& strArg, const std::string& strValue)
    482+void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strValue)
    483 {
    484     LOCK(cs_args);
    485     mapArgs[strArg] = strValue;
    486+    _mapMultiArgs[strArg].push_back(strValue);
    


    TheBlueMatt commented at 5:25 pm on May 8, 2017:
    Its somewhat annoying to have an intermediate commit which breaks our locking in the middle - maybe move this one line to the “Put mapMultiArgs inside ArgsManager” commit? It should at least break fewer things by doing so.
  47. TheBlueMatt commented at 5:34 pm on May 8, 2017: member
    utACK f19f5b5dbd0e3b6f73ded3818014eed3f728e5fc
  48. in src/util.h:246 in b10ec118d3 outdated
    242@@ -235,6 +243,55 @@ bool SoftSetBoolArg(const std::string& strArg, bool fValue);
    243 
    244 // Forces a arg setting, used only in testing
    245 void ForceSetArg(const std::string& strArg, const std::string& strValue);
    246+};
    


    ryanofsky commented at 6:33 pm on May 8, 2017:

    “Introduce an ArgsManager class encapsulating cs_args, mapArgs and mapMultiArgs”

    Indentation for all the methods above is missing. Though it’s understandable not to add it now to make diff smaller.


    jtimon commented at 8:08 pm on May 8, 2017:
    Yes, that’s exactly the point, but I have done similar things in the past and not fixed them later, something I feel bad about, for example: https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp#L1313 Please, help me make sure that it gets done this time.
  49. ryanofsky commented at 7:02 pm on May 8, 2017: member
    utACK f19f5b5dbd0e3b6f73ded3818014eed3f728e5fc
  50. in src/init.cpp:916 in f19f5b5dbd outdated
    910@@ -911,9 +911,9 @@ bool AppInitParameterInteraction()
    911         InitWarning(strprintf(_("Reducing -maxconnections from %d to %d, because of system limitations."), nUserMaxConnections, nMaxConnections));
    912 
    913     // ********************************************************* Step 3: parameter-to-internal-flags
    914-    if (gArgs.IsArgSet("-debug") > 0) {
    915+    if (gArgs.IsArgSet("-debug")) {
    916         // Special-case: if -debug=0/-nodebug is set, turn off debugging messages
    917-        const std::vector<std::string>& categories = gArgs.GetArgs("-debug");
    918+        const std::vector<std::string> categories = gArgs.GetArgs("-debug");
    


    theuni commented at 7:51 pm on May 8, 2017:
    why?

    jtimon commented at 8:09 pm on May 8, 2017:

    Because #9494 (review) and I don’t care either way for the reasons @sipa explained.

    EDIT: yet I’m again happy to turn it back.

  51. Util: Create ArgsManager class...
    - Introduce ArgsManager::GetArgs()
    - Adapt util_tests.cpp to ArgsManager
    f2957ce6cd
  52. scripted-diff: Util: Encapsulate mapMultiArgs behind gArgs
    -BEGIN VERIFY SCRIPT-
    sed -i 's/mapMultiArgs.count(/gArgs.IsArgSet(/g' ./src/*.h ./src/*.cpp ./src/*/*.h ./src/*/*.cpp ./src/*/*/*.h ./src/*/*/*.cpp ;
    sed -i 's/mapMultiArgs.at("/gArgs.GetArgs("/g' ./src/*.h ./src/*.cpp ./src/*/*.h ./src/*/*.cpp ./src/*/*/*.h ./src/*/*/*.cpp ;
    -END VERIFY SCRIPT-
    b3cbd554d9
  53. Util: Put mapMultiArgs inside ArgsManager
    - Set ArgsManager::mapMultiArgs in ArgsManager::SoftSetArg, ForceSetArg, SoftSetBoolArg
    52922456b8
  54. Util: Small improvements in gArgs usage
    - Don't check gArgs.IsArgSet() is greater than 0
    - Remove unneeded calls and local variables
    78da882edd
  55. jtimon force-pushed on May 9, 2017
  56. theuni commented at 7:39 pm on May 9, 2017: member
    utACK f19f5b5dbd0e3b6f73ded3818014eed3f728e5fc
  57. jtimon commented at 7:41 pm on May 9, 2017: contributor
    Sorry, just pushed again changing the history as @TheBlueMatt suggested. 78da882 should be equivalent to f19f5b5
  58. ryanofsky commented at 3:41 pm on May 10, 2017: member
    utACK 78da882edd54ed62f4a0035590460a97cb9ff282. Confirmed no diff since previous review, just mapMultiArgs commit history change.
  59. laanwj commented at 5:39 am on May 15, 2017: member
    utACK 78da882
  60. laanwj merged this on May 15, 2017
  61. laanwj closed this on May 15, 2017

  62. laanwj referenced this in commit 41987aa92f on May 15, 2017
  63. laanwj removed this from the "Blockers" column in a project

  64. jtimon deleted the branch on May 18, 2017
  65. PastaPastaPasta referenced this in commit 5be631aae0 on Jun 10, 2019
  66. PastaPastaPasta referenced this in commit 0d5a6d2883 on Jun 10, 2019
  67. PastaPastaPasta referenced this in commit 3ec86c4ba5 on Jun 11, 2019
  68. PastaPastaPasta referenced this in commit 279d12c316 on Jun 11, 2019
  69. PastaPastaPasta referenced this in commit b48179429a on Jun 15, 2019
  70. PastaPastaPasta referenced this in commit d735365461 on Jun 19, 2019
  71. PastaPastaPasta referenced this in commit d5cff4f874 on Jun 19, 2019
  72. PastaPastaPasta referenced this in commit f256ab80b6 on Jun 19, 2019
  73. PastaPastaPasta referenced this in commit 2f37fec22e on Jun 19, 2019
  74. PastaPastaPasta referenced this in commit 341655271a on Jun 19, 2019
  75. PastaPastaPasta referenced this in commit 4cf1eeeb7e on Jun 20, 2019
  76. PastaPastaPasta referenced this in commit 1cfcd598d0 on Jun 22, 2019
  77. PastaPastaPasta referenced this in commit fdd1cd5921 on Jun 22, 2019
  78. PastaPastaPasta referenced this in commit 1a55d0c08d on Jun 22, 2019
  79. PastaPastaPasta referenced this in commit f028511edd on Jun 22, 2019
  80. PastaPastaPasta referenced this in commit 0e556be703 on Jun 22, 2019
  81. barrystyle referenced this in commit b7f0bb2622 on Jan 22, 2020
  82. furszy referenced this in commit de5b240881 on Oct 28, 2020
  83. DrahtBot locked this on Sep 8, 2021

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: 2024-07-03 13:13 UTC

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