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<CChainParams> 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. laanwj cross-referenced this on Oct 19, 2016 from issue Testnet utxos should be spendable by anyone after N blocks, for some large N by maaku
  8. jtimon force-pushed on Oct 21, 2016
  9. jtimon commented at 12:44 AM on October 21, 2016: contributor

    Added 1 commit. Sorry for gratuitously rebasing...

  10. jtimon force-pushed on Oct 22, 2016
  11. jtimon cross-referenced this on Oct 22, 2016 from issue Testchains: Introduce custom chain whose constructor... by jtimon
  12. jtimon force-pushed on Nov 3, 2016
  13. jtimon commented at 9:58 PM on November 3, 2016: contributor

    Needed rebase

  14. jtimon force-pushed on Nov 10, 2016
  15. jtimon commented at 11:20 PM on November 10, 2016: contributor

    Needed rebase to adapt to a new test using Params(std::string).

  16. jtimon force-pushed on Nov 14, 2016
  17. jtimon commented at 6:23 PM on November 14, 2016: contributor

    Needed rebase for bench/checkblock.cpp again

  18. in src/chainparamsbase.cpp:None 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?

  19. in src/chainparamsbase.h:None 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.

  20. in src/bitcoin-cli.cpp:None 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.

  21. in src/chainparams.h:None 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.

  22. jtimon force-pushed on Nov 15, 2016
  23. jtimon commented at 11:01 PM on November 15, 2016: contributor

    Updated, hopefully fixing all nits.

  24. in src/chainparams.cpp:None 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...

  25. in src/chainparamsbase.cpp:None 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?

  26. in src/test/pow_tests.cpp:None 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...

  27. sipa commented at 2:57 AM on November 18, 2016: member

    utACK dd882d15c846da56a3338626be4e411a003eb8b5

  28. jtimon force-pushed on Nov 18, 2016
  29. jtimon force-pushed on Nov 18, 2016
  30. jtimon commented at 3:02 AM on November 18, 2016: contributor

    Solved auto-nits, also rebased.

  31. in src/chainparamsbase.h:None 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.

  32. jtimon force-pushed on Nov 18, 2016
  33. jtimon commented at 3:25 AM on November 18, 2016: contributor

    Updated and hopefully fixed latest small nits. Thanks again for the nits.

  34. jtimon cross-referenced this on Nov 22, 2016 from issue NOMERGE: WIP: Support block signed custom testchains by jtimon
  35. jtimon force-pushed on Dec 2, 2016
  36. jtimon commented at 3:10 AM on December 2, 2016: contributor

    Needed trivial rebase.

  37. jtimon force-pushed on Jan 8, 2017
  38. jtimon commented at 11:07 PM on January 8, 2017: contributor

    Trivial rebase done.

  39. jtimon force-pushed on Jan 24, 2017
  40. jtimon commented at 11:23 PM on January 24, 2017: contributor

    Needed rebase again.

  41. jtimon force-pushed on Mar 7, 2017
  42. jtimon commented at 12: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.

  43. jtimon cross-referenced this on Mar 22, 2017 from issue Ar 0.14 chainparams factory by jtimon
  44. jtimon force-pushed on Mar 23, 2017
  45. 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.

  46. jtimon cross-referenced this on Mar 24, 2017 from issue 8994: Testchains: Introduce custom chain whose constructor... by jtimon
  47. laanwj added this to the "Blockers" column in a project

  48. kallewoof commented at 4:37 AM on April 3, 2017: member

    utACK 33b78e16e0023191663fa209ac08608b2b48cfab

  49. jtimon cross-referenced this on Apr 6, 2017 from issue Pow: Remove fCheckPOW from CheckBlockHeader by jtimon
  50. Chainparams: Use a regular factory for creating chainparams f87f3626e3
  51. Chainparams: Get rid of CChainParams& Params(std::string) 2351a064a6
  52. Chainparams: Use the factory for pow tests c1082a7d35
  53. jtimon force-pushed on May 3, 2017
  54. jtimon commented at 4:32 PM on May 3, 2017: contributor

    Needed trivial rebase after #10075

  55. sipa removed this from the "Blockers" column in a project

  56. 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:
  57. 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:

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

    And might also add locking.

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

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

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

  60. ryanofsky commented at 11:19 PM on May 8, 2017: contributor

    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()
  61. laanwj merged this on May 9, 2017
  62. laanwj closed this on May 9, 2017

  63. practicalswift referenced this in commit c973cc5a43 on May 9, 2017
  64. jtimon deleted the branch on May 30, 2017
  65. luke-jr cross-referenced this on Aug 10, 2017 from issue Bugfix: Use testnet RequireStandard for -acceptnonstdtxn default by luke-jr
  66. laanwj referenced this in commit ea3ac5990d on Aug 22, 2017
  67. str4d cross-referenced this on Jan 30, 2018 from issue Network upgrade activation mechanism by str4d
  68. str4d cross-referenced this on Mar 27, 2018 from issue Misc upstream PRs by str4d
  69. MarkLTZ referenced this in commit a23ef40881 on Apr 27, 2019
  70. PastaPastaPasta referenced this in commit db9adaee75 on Jun 10, 2019
  71. PastaPastaPasta referenced this in commit 94a681e59a on Jun 19, 2019
  72. PastaPastaPasta referenced this in commit 21ba0aab53 on Jun 20, 2019
  73. PastaPastaPasta referenced this in commit b1824f1195 on Jun 20, 2019
  74. PastaPastaPasta referenced this in commit 4584ddf68f on Jun 22, 2019
  75. PastaPastaPasta referenced this in commit 29a9c5fea3 on Jun 22, 2019
  76. PastaPastaPasta referenced this in commit 28458d73d4 on Jun 22, 2019
  77. PastaPastaPasta referenced this in commit eb536bddfe on Jun 22, 2019
  78. PastaPastaPasta referenced this in commit d5339f0613 on Jun 22, 2019
  79. PastaPastaPasta referenced this in commit cfec97e793 on Jun 22, 2019
  80. PastaPastaPasta referenced this in commit 99506cb6b5 on Jun 22, 2019
  81. PastaPastaPasta referenced this in commit 5615b8c734 on Jun 22, 2019
  82. PastaPastaPasta referenced this in commit cff2edb743 on Jun 22, 2019
  83. PastaPastaPasta referenced this in commit 50652674b5 on Jun 26, 2019
  84. PastaPastaPasta referenced this in commit f19304eaea on Sep 19, 2019
  85. PastaPastaPasta referenced this in commit 8d7406eeb6 on Sep 23, 2019
  86. PastaPastaPasta referenced this in commit cdd561bbcf on Sep 24, 2019
  87. PastaPastaPasta referenced this in commit bc135d8a39 on Nov 19, 2019
  88. PastaPastaPasta referenced this in commit 8762895535 on Nov 21, 2019
  89. PastaPastaPasta referenced this in commit 708cc06597 on Dec 9, 2019
  90. PastaPastaPasta referenced this in commit fbbdd7722c on Jan 1, 2020
  91. PastaPastaPasta referenced this in commit 6095fcc653 on Jan 2, 2020
  92. barrystyle referenced this in commit 4360d5b9d0 on Jan 22, 2020
  93. furszy cross-referenced this on Jan 27, 2021 from issue [Refactor] Several updates to the base chain params structure by furszy
  94. furszy referenced this in commit ac067073ec on Feb 10, 2021
  95. ckti referenced this in commit 78b510303a on Mar 28, 2021
  96. bitcoin 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: 2026-05-20 06:55 UTC