Use a proper factory for creating chainparams #8855

pull jtimon wants to merge 3 commits into bitcoin:master from jtimon:0.13-chainparams-factory changing 12 files +86 −81
  1. jtimon commented at 2:03 am on October 1, 2016: contributor

    2 global variables are currently enough for basechainparams and CChainParams, but we’re using N + 1 globals instead. Although N is likely to remain 3 for the foreseeable future (and not many people seemed interested in turning N into infinite in #6382) I believe segwit was an example on how starting a new testchain can be very useful and anything that makes that easier is potentially valuable.

    Traceability: Replaces #6907. Changes from final non-merged #6907:

    • Of course, there were new uses of CChainParams& Params(std::string)
    • post-segwit: UpdateRegtestBIP9Parameters is replaced with UpdateBIP9Parameters since we don’t have a separate global for regtest anymore. An additional assert/error could be added if we wanted to restrict its usage beyond regtest, bit I don’t think the restriction makes sense. Remember that Params() now returns a const ref and init.cpp was the only context was the only context where UpdateRegtestBIP9Parameters was called from.
    • post-C++11: s/boost::scoped_ptr/std::unique_ptr/ as in #8629
    • Return a std::unique_ptr directly instead of a CChainParams*
    • testChainParams could be confused as meaning params from testnet (changed to chainParams)
    • Changes to checkpoint tests no longer required after #9053
  2. fanquake added the label Refactoring on Oct 1, 2016
  3. jtimon force-pushed on Oct 3, 2016
  4. jtimon force-pushed on Oct 3, 2016
  5. jtimon commented at 3:05 pm on October 3, 2016: contributor
    Pushed with more changes added to the OP.
  6. jtimon force-pushed on Oct 18, 2016
  7. jtimon force-pushed on Oct 21, 2016
  8. jtimon commented at 0:44 am on October 21, 2016: contributor
    Added 1 commit. Sorry for gratuitously rebasing…
  9. jtimon force-pushed on Oct 22, 2016
  10. jtimon force-pushed on Nov 3, 2016
  11. jtimon commented at 9:58 pm on November 3, 2016: contributor
    Needed rebase
  12. jtimon force-pushed on Nov 10, 2016
  13. jtimon commented at 11:20 pm on November 10, 2016: contributor
    Needed rebase to adapt to a new test using Params(std::string).
  14. jtimon force-pushed on Nov 14, 2016
  15. jtimon commented at 6:23 pm on November 14, 2016: contributor
    Needed rebase for bench/checkblock.cpp again
  16. in src/chainparamsbase.cpp: in eb6c595e55 outdated
    71+    assert(globalChainBaseParams.get());
    72+    return *globalChainBaseParams;
    73 }
    74 
    75-CBaseChainParams& BaseParams(const std::string& chain)
    76+CBaseChainParams* CBaseChainParams::Factory(const std::string& chain)
    


    sipa commented at 7:34 pm on November 15, 2016:
    Any reason why this does not return a unique_ptr?
  17. in src/chainparamsbase.h: in eb6c595e55 outdated
    21@@ -22,6 +22,12 @@ class CBaseChainParams
    22 
    23     const std::string& DataDir() const { return strDataDir; }
    24     int RPCPort() const { return nRPCPort; }
    25+    /**
    26+     * Creates and returns a CBaseChainParams* of the chosen chain. The caller has to delete the object.
    27+     * @returns A CBaseChainParams* of the chosen chain.
    28+     * @throws a std::runtime_error if the chain is not supported.
    29+     */
    30+    static CBaseChainParams* Factory(const std::string& chain);
    


    sipa commented at 7:35 pm on November 15, 2016:
    I think this should be a function rather than a static method.
  18. in src/bitcoin-cli.cpp: in eb6c595e55 outdated
    31@@ -32,14 +32,16 @@ static const int CONTINUE_EXECUTION=-1;
    32 
    33 std::string HelpMessageCli()
    34 {
    35+    const std::unique_ptr<CBaseChainParams> defaultBaseParams(CBaseChainParams::Factory(CBaseChainParams::MAIN));
    


    sipa commented at 7:37 pm on November 15, 2016:
    Lines like this (as there are many) can avoid some redundancy by using const auto defaultBaseParmams = ... instead.
  19. in src/chainparams.h: in eb6c595e55 outdated
    78+    /**
    79+     * Creates and returns a CChainParams* of the chosen chain. The caller has to delete the object.
    80+     * @returns a CChainParams* of the chosen chain.
    81+     * @throws a std::runtime_error if the chain is not supported.
    82+     */
    83+    static std::unique_ptr<CChainParams> Factory(const std::string& chain);
    


    sipa commented at 7:40 pm on November 15, 2016:
    I think this should be a function rather than a static method.
  20. jtimon force-pushed on Nov 15, 2016
  21. jtimon commented at 11:01 pm on November 15, 2016: contributor
    Updated, hopefully fixing all nits.
  22. in src/chainparams.cpp: in dd882d15c8 outdated
    325+static std::unique_ptr<CChainParams> globalChainParams;
    326 
    327 const CChainParams &Params() {
    328-    assert(pCurrentParams);
    329-    return *pCurrentParams;
    330+    assert(globalChainParams.get());
    


    sipa commented at 2:11 am on November 18, 2016:
    μnit: you can just use assert(globalChainParams) (std::unique_ptr has an implicit conversion to bool, which checks whether there is an owned object),

    jtimon commented at 3:06 am on November 18, 2016:
    Remains from boost::scoped_ptr, fixing now…
  23. in src/chainparamsbase.cpp: in 4f2ab940be outdated
     7@@ -8,8 +8,6 @@
     8 #include "tinyformat.h"
     9 #include "util.h"
    10 
    11-#include <assert.h>
    


    jtimon commented at 2:20 am on November 18, 2016:
    auto-nit: why remove this?
  24. in src/test/pow_tests.cpp: in dd882d15c8 outdated
    17@@ -18,69 +18,59 @@ BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup)
    18 /* Test calculation of next difficulty target with no constraints applying */
    19 BOOST_AUTO_TEST_CASE(get_next_work)
    20 {
    21-    SelectParams(CBaseChainParams::MAIN);
    22-    const Consensus::Params& params = Params().GetConsensus();
    23-
    24+    auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
    


    jtimon commented at 2:41 am on November 18, 2016:
    Also all these could remain const, will push shortly…
  25. sipa commented at 2:57 am on November 18, 2016: member
    utACK dd882d15c846da56a3338626be4e411a003eb8b5
  26. jtimon force-pushed on Nov 18, 2016
  27. jtimon force-pushed on Nov 18, 2016
  28. jtimon commented at 3:02 am on November 18, 2016: contributor
    Solved auto-nits, also rebased.
  29. in src/chainparamsbase.h: in e0e2a3e524 outdated
    31@@ -31,6 +32,13 @@ class CBaseChainParams
    32 };
    33 
    34 /**
    35+ * Creates and returns a CBaseChainParams* of the chosen chain. The caller has to delete the object.
    


    morcos commented at 3:07 am on November 18, 2016:
    Is this comment out of date now?

    jtimon commented at 3:08 am on November 18, 2016:
    Right, the caller doesn’t have to explicitly delete the object anymore, thanks.
  30. jtimon force-pushed on Nov 18, 2016
  31. jtimon commented at 3:25 am on November 18, 2016: contributor
    Updated and hopefully fixed latest small nits. Thanks again for the nits.
  32. jtimon force-pushed on Dec 2, 2016
  33. jtimon commented at 3:10 am on December 2, 2016: contributor
    Needed trivial rebase.
  34. jtimon force-pushed on Jan 8, 2017
  35. jtimon commented at 11:07 pm on January 8, 2017: contributor
    Trivial rebase done.
  36. jtimon force-pushed on Jan 24, 2017
  37. jtimon commented at 11:23 pm on January 24, 2017: contributor
    Needed rebase again.
  38. jtimon force-pushed on Mar 7, 2017
  39. jtimon commented at 0:48 am on March 7, 2017: contributor
    Didn’t need rebase, but the second commit could stop compiling on merge at any time without apparently needing rebase. So I just did it, no changes needed this time.
  40. jtimon force-pushed on Mar 23, 2017
  41. jtimon commented at 8:15 pm on March 23, 2017: contributor
    Rebased again in search of new uses of Params(std::string) (which would make this PR fail on compilation if merged), none found.
  42. laanwj added this to the "Blockers" column in a project

  43. kallewoof commented at 4:37 am on April 3, 2017: member
    utACK 33b78e16e0023191663fa209ac08608b2b48cfab
  44. Chainparams: Use a regular factory for creating chainparams f87f3626e3
  45. Chainparams: Get rid of CChainParams& Params(std::string) 2351a064a6
  46. Chainparams: Use the factory for pow tests c1082a7d35
  47. jtimon force-pushed on May 3, 2017
  48. jtimon commented at 4:32 pm on May 3, 2017: contributor
    Needed trivial rebase after #10075
  49. sipa removed this from the "Blockers" column in a project

  50. in src/init.cpp:1128 in f87f3626e3 outdated
    1124@@ -1123,7 +1125,7 @@ bool AppInitParameterInteraction()
    1125             for (int j=0; j<(int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j)
    1126             {
    1127                 if (vDeploymentParams[0].compare(VersionBitsDeploymentInfo[j].name) == 0) {
    1128-                    UpdateRegtestBIP9Parameters(Consensus::DeploymentPos(j), nStartTime, nTimeout);
    1129+                    UpdateBIP9Parameters(Consensus::DeploymentPos(j), nStartTime, nTimeout);
    


    ryanofsky commented at 10:38 pm on May 8, 2017:

    In commit “Use a proper factory for creating chainparams”

    This seems not equivalent to the previous code. Previously this would only affect the regtest chain returned by Params(REGTEST), but now it affects the chain params returned by Params()


    jtimon commented at 11:42 am on May 9, 2017:
  51. in src/chainparams.cpp:356 in f87f3626e3 outdated
    367+    throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
    368+}
    369+
    370+const CChainParams& Params(const std::string& chain)
    371+{
    372+    globalSwitchingChainParams = CreateChainParams(chain);
    


    ryanofsky commented at 10:46 pm on May 8, 2017:

    In commit “Chainparams: Use a regular factory for creating chainparams”

    This does not seem safe because every new call to Params(chain) will delete the object referenced by the previous call. This could cause crashes and memory corruption if Params(chain) is called multiple times or from different threads.

    A safer implementation would look more like:

    0const CChainParams& Params(const std::string& chain) {
    1  static std::map<std::string, std::unique_ptr<CChainParams>> cachedParams;
    2  auto& params = cachedParams[chain];
    3  if (!params) params = CreateChainParams(chain);
    4  return *params;
    5}
    

    And might also add locking.

    UPDATE: Maybe it doesn’t matter because you are deleting this function in the next commit.

  52. in src/chainparams.h:101 in f87f3626e3 outdated
     96@@ -95,6 +97,13 @@ class CChainParams
     97 };
     98 
     99 /**
    100+ * Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
    101+ * @returns a CChainParams* of the chosen chain.
    


    ryanofsky commented at 11:01 pm on May 8, 2017:

    In commit “Chainparams: Use a regular factory for creating chainparams”

    Technically returns a unique_ptr

  53. in src/chainparamsbase.h:36 in f87f3626e3 outdated
    31@@ -31,6 +32,13 @@ class CBaseChainParams
    32 };
    33 
    34 /**
    35+ * Creates and returns a std::unique_ptr<CBaseChainParams> of the chosen chain.
    36+ * @returns a CBaseChainParams* of the chosen chain.
    


    ryanofsky commented at 11:02 pm on May 8, 2017:

    In commit “Chainparams: Use a regular factory for creating chainparams”

    Technically returns a unique_ptr

  54. ryanofsky commented at 11:19 pm on May 8, 2017: member

    utACK c1082a7d359ad984e84195175a01f2f30671b172, though I would appreciate clarification on UpdateRegtestBIP9Parameters because on the surface it seems like it might change behavior.

    I took notes on everything happening in the first commit since it seemed to be doing a lot of different things:

    • Add CreateChainParams(chain) function returning unique_ptr’s to new params objects.
    • Update Params(chain) function to return const references to temporary params objects that get deleted on the next call, instead of permanent mutable references. Get rid of mainParams testNetParams regTestParams static globals that Params(chain) used to return.
    • Move CChainParams::UpdateBIP9Parameters definition from chainparams.h to chainparams.cpp.
    • Make changes in chainparamsbase.{h,cpp} analagous to those in chainparams.{h,cpp}
    • Update HelpMessage() to call CreateBaseChainParams(chain) instead of BaseParams(chain). Similarly for HelpMessageCli()
  55. laanwj merged this on May 9, 2017
  56. laanwj closed this on May 9, 2017

  57. practicalswift referenced this in commit c973cc5a43 on May 9, 2017
  58. jtimon deleted the branch on May 30, 2017
  59. laanwj referenced this in commit ea3ac5990d on Aug 22, 2017
  60. MarkLTZ referenced this in commit a23ef40881 on Apr 27, 2019
  61. PastaPastaPasta referenced this in commit db9adaee75 on Jun 10, 2019
  62. PastaPastaPasta referenced this in commit 94a681e59a on Jun 19, 2019
  63. PastaPastaPasta referenced this in commit 21ba0aab53 on Jun 20, 2019
  64. PastaPastaPasta referenced this in commit b1824f1195 on Jun 20, 2019
  65. PastaPastaPasta referenced this in commit 4584ddf68f on Jun 22, 2019
  66. PastaPastaPasta referenced this in commit 29a9c5fea3 on Jun 22, 2019
  67. PastaPastaPasta referenced this in commit 28458d73d4 on Jun 22, 2019
  68. PastaPastaPasta referenced this in commit eb536bddfe on Jun 22, 2019
  69. PastaPastaPasta referenced this in commit d5339f0613 on Jun 22, 2019
  70. PastaPastaPasta referenced this in commit cfec97e793 on Jun 22, 2019
  71. PastaPastaPasta referenced this in commit 99506cb6b5 on Jun 22, 2019
  72. PastaPastaPasta referenced this in commit 5615b8c734 on Jun 22, 2019
  73. PastaPastaPasta referenced this in commit cff2edb743 on Jun 22, 2019
  74. PastaPastaPasta referenced this in commit 50652674b5 on Jun 26, 2019
  75. PastaPastaPasta referenced this in commit f19304eaea on Sep 19, 2019
  76. PastaPastaPasta referenced this in commit 8d7406eeb6 on Sep 23, 2019
  77. PastaPastaPasta referenced this in commit cdd561bbcf on Sep 24, 2019
  78. PastaPastaPasta referenced this in commit bc135d8a39 on Nov 19, 2019
  79. PastaPastaPasta referenced this in commit 8762895535 on Nov 21, 2019
  80. PastaPastaPasta referenced this in commit 708cc06597 on Dec 9, 2019
  81. PastaPastaPasta referenced this in commit fbbdd7722c on Jan 1, 2020
  82. PastaPastaPasta referenced this in commit 6095fcc653 on Jan 2, 2020
  83. barrystyle referenced this in commit 4360d5b9d0 on Jan 22, 2020
  84. furszy referenced this in commit ac067073ec on Feb 10, 2021
  85. ckti referenced this in commit 78b510303a on Mar 28, 2021
  86. 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-12-22 06:12 UTC

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