Don’t edit Chainparams after initialization #13311

pull jtimon wants to merge 2 commits into bitcoin:master from jtimon:b17-regtest-only-params changing 12 files +101 −83
  1. jtimon commented at 1:26 pm on May 23, 2018: contributor

    This encapsulates the “-vbparams” option, which is only meant for regtest, directly on CRegTestParams.

    This is a refactor and doesn’t change functionality.

    Related to https://github.com/bitcoin/bitcoin/pull/8994

  2. jtimon force-pushed on May 23, 2018
  3. fanquake added the label Refactoring on May 23, 2018
  4. skeees commented at 5:45 pm on May 23, 2018: contributor
    to ease review, might be good to split this into a moveonly commit and then a subsequent one with changes if thats possible?
  5. sipa commented at 5:42 pm on May 24, 2018: member
    Why move the version bits information to a separate file? This introduces a circular dependency between chainparams and versionbitsinfo,
  6. jtimon commented at 10:36 am on May 27, 2018: contributor

    to ease review, might be good to split this into a moveonly commit and then a subsequent one with changes if thats possible?

    Sure, but I would like to wait for some concept acks first.

    Why move the version bits information to a separate file? This introduces a circular dependency between chainparams and versionbitsinfo,

    No, it does not. After this, versionbits depends onchainparams, which depends on versionbitsinfo, which depends on consensus/params. The separation is precisely to avoid a circular dependency between chainparams and versionbits.

  7. sipa commented at 8:01 pm on May 27, 2018: member

    No, it does not. After this, versionbits depends onchainparams, which depends on versionbitsinfo, which depends on consensus/params. The separation is precisely to avoid a circular dependency between chainparams and versionbits.

    My apologies! I confused chainparams and consensus/params when reading this.

  8. MarcoFalke commented at 1:58 pm on May 30, 2018: member
    Needs rebase due to merge of #13341
  9. MarcoFalke added the label Needs rebase on Jun 6, 2018
  10. DrahtBot removed the label Needs rebase on Jun 6, 2018
  11. DrahtBot added the label Needs rebase on Jun 6, 2018
  12. jtimon force-pushed on Jun 16, 2018
  13. jtimon commented at 8:42 pm on June 16, 2018: contributor
    Rebased and separated MOVEONLY commit.
  14. DrahtBot removed the label Needs rebase on Jun 19, 2018
  15. in src/chainparams.cpp:18 in c3d5082ce3 outdated
    14 
    15 #include <assert.h>
    16 
    17-#include <chainparamsseeds.h>
    18+#include <boost/algorithm/string/classification.hpp>
    19+#include <boost/algorithm/string/split.hpp>
    


    instagibbs commented at 0:07 am on July 12, 2018:
    NB: it’s still used in init.cpp, only for mempool replacement
  16. in src/chainparams.cpp:17 in c3d5082ce3 outdated
    13+#include <versionbitsinfo.h>
    14 
    15 #include <assert.h>
    16 
    17-#include <chainparamsseeds.h>
    18+#include <boost/algorithm/string/classification.hpp>
    


    instagibbs commented at 0:11 am on July 12, 2018:
    NB: it’s still used in init.cpp, only for mempool replacement
  17. in src/test/test_bitcoin.cpp:60 in c3d5082ce3 outdated
    54@@ -55,6 +55,9 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
    55         InitSignatureCache();
    56         InitScriptExecutionCache();
    57         fCheckBlockIndex = true;
    58+        // CreateAndProcessBlock() does not support building SegWit blocks, so don't activate in these tests.
    59+        // TODO: fix the code to support SegWit blocks.
    60+        gArgs.ForceSetArg("-vbparams", "segwit:0:999999999999");
    


    instagibbs commented at 0:14 am on July 12, 2018:
    nit: would be nice to keep using Consensus::BIP9Deployment::NO_TIMEOUT
  18. instagibbs approved
  19. instagibbs commented at 0:23 am on July 12, 2018: member
    utACK with ultra minor nit
  20. DrahtBot added the label Needs rebase on Jul 12, 2018
  21. jtimon force-pushed on Jul 19, 2018
  22. jtimon commented at 5:41 am on July 19, 2018: contributor
    rebased
  23. jtimon force-pushed on Jul 19, 2018
  24. jtimon commented at 10:36 pm on July 19, 2018: contributor
    Fixed nit in #13311 (review)
  25. in src/chainparams.cpp:400 in 7067657731 outdated
    395+            }
    396+        }
    397+    }
    398+}
    399+
    400 static std::unique_ptr<CChainParams> globalChainParams;
    


    ajtowns commented at 11:11 pm on July 19, 2018:
    Could change this to unique_ptr<const CChainParams> to better ensure that params aren’t changed after initialisation? (Means CreateChainParams() return value also needs to change, but that seems to work fine)
  26. in src/chainparams.cpp:427 in 7067657731 outdated
    429-void UpdateVersionBitsParameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
    430+void SelectParams(const std::string& network)
    431 {
    432-    globalChainParams->UpdateVersionBitsParameters(d, nStartTime, nTimeout);
    433+    SelectBaseParams(network);
    434+    globalChainParams = CreateChainParams(network, gArgs);
    


    ajtowns commented at 11:23 pm on July 19, 2018:
    Why not reference gArgs directly in CreateChainParams() when using REGTEST? This is the only place that actually calls CreateChainParams with the args parameter and the only place that calls it with chain==REGTEST. That way you don’t need to have two different ways of calling CreateChainParams.

    jtimon commented at 1:23 am on July 20, 2018:
    I can remove the new way of calling CreateChainParams, but the idea was that some unittests could create their own custom ArgsManager instead of using gArgs. This would only serve for testing functions that explicitly take a CChainParams instead of relaying on Params().

    promag commented at 10:51 pm on August 19, 2018:

    Tend to agree with @ajtowns.

    but the idea was that some unittests could create their own custom ArgsManager instead of using gArgs

    If needed it can mutate gArgs? Or this can be done when needed?


    jtimon commented at 8:20 pm on August 20, 2018:
    I guess one can mutate gArgs to a given value for a given test and then back to its original value afterwards. Or as you say, make this change afterwards when it’s going to be used.
  27. in src/chainparams.cpp:279 in 7067657731 outdated
    275@@ -280,7 +276,7 @@ class CTestNetParams : public CChainParams {
    276  */
    277 class CRegTestParams : public CChainParams {
    278 public:
    279-    CRegTestParams() {
    280+    CRegTestParams(ArgsManager& args) {
    


    sipa commented at 11:25 pm on July 19, 2018:
    Is it necessary to pass in an entire ArgsManager object? It seems only the -vbparams value is used. What about just passing in the vbparams value?

    jtimon commented at 11:26 pm on July 19, 2018:
    Well, the point of that is to make it easy to add further params, like in #8994 (probably not all of them are interesting for testing).

    Empact commented at 11:23 pm on August 20, 2018:
    Better to just pass the needed arg IMO, and only couple these classes if preferable based on the eventual use. https://martinfowler.com/bliki/Yagni.html

    ajtowns commented at 4:39 am on August 27, 2018:
    Could make this const ArgsManager& args
  28. ajtowns commented at 11:27 pm on July 19, 2018: member
    utACK 7067657731404c28ff87e46ec390992500ca13be
  29. DrahtBot removed the label Needs rebase on Jul 20, 2018
  30. promag commented at 1:13 am on July 20, 2018: member
    Concept ACK.
  31. jtimon force-pushed on Jul 20, 2018
  32. jtimon commented at 1:31 am on July 20, 2018: contributor
  33. laanwj added this to the "Blockers" column in a project

  34. in src/chainparams.cpp:363 in ca040b5b76 outdated
    358+    void UpdateVersionBitsParameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
    359+    {
    360+        consensus.vDeployments[d].nStartTime = nStartTime;
    361+        consensus.vDeployments[d].nTimeout = nTimeout;
    362+    }
    363+    void UpdateVersionBitsParametersFromArgs(ArgsManager& args);
    


    ajtowns commented at 4:40 am on August 27, 2018:
    const ArgsManager& here as well
  35. in src/chainparams.cpp:414 in ca040b5b76 outdated
    412         return std::unique_ptr<CChainParams>(new CMainParams());
    413     else if (chain == CBaseChainParams::TESTNET)
    414         return std::unique_ptr<CChainParams>(new CTestNetParams());
    415     else if (chain == CBaseChainParams::REGTEST)
    416-        return std::unique_ptr<CChainParams>(new CRegTestParams());
    417+        return std::unique_ptr<CChainParams>(new CRegTestParams(args));
    


    ajtowns commented at 4:41 am on August 27, 2018:
    I still think dropping the args param and referencing gArgs here directly makes more sense. Otherwise, could make args be const here too.

    l2a5b1 commented at 9:50 pm on September 1, 2018:

    Yes, that makes sense.

    By doing just that and dropping the ArgsManager& args argument from std::unique_ptr<const CChainParams> CreateChainParams(const std::string& chain, ArgsManager& args) we can remove std::unique_ptr<const CChainParams> CreateChainParams(const std::string& chain) as you also have pointed out here: #13311 (review).

  36. ajtowns approved
  37. ajtowns commented at 4:42 am on August 27, 2018: member
    utACK ca040b5b76ddeeace6da4f8111169ffd6234414a
  38. in src/chainparams.cpp:369 in ca040b5b76 outdated
    365 
    366-static std::unique_ptr<CChainParams> globalChainParams;
    367+void CRegTestParams::UpdateVersionBitsParametersFromArgs(ArgsManager& args)
    368+{
    369+    // Allow overriding version bits parameters for testing
    370+    if (args.IsArgSet("-vbparams")) {
    


    l2a5b1 commented at 3:52 pm on September 1, 2018:

    This function can exit early if the -vbparams arg is not set.

    0void CRegTestParams::UpdateVersionBitsParametersFromArgs(ArgsManager& args)
    1{
    2    if (!args.IsArgSet("-vbparams")) return;
    3
    4    for (const std::string& strDeployment : args.GetArgs("-vbparams")) {
    5        ...
    6    }
    7}
    
  39. in src/chainparams.h:113 in ca040b5b76 outdated
    107@@ -107,7 +108,9 @@ class CChainParams
    108  * @returns a CChainParams* of the chosen chain.
    109  * @throws a std::runtime_error if the chain is not supported.
    110  */
    111-std::unique_ptr<CChainParams> CreateChainParams(const std::string& chain);
    112+std::unique_ptr<const CChainParams> CreateChainParams(const std::string& chain);
    113+/** Extended version taking custom args (only useful for regtest). */
    114+std::unique_ptr<const CChainParams> CreateChainParams(const std::string& chain, ArgsManager& args);
    


    l2a5b1 commented at 9:00 pm on September 1, 2018:

    I tend to agree with @ajtowns that we can do without std::unique_ptr<const CChainParams> CreateChainParams(const std::string& chain, ArgsManager& args).

    If you want to keep it then maybe you can remove its declaration from chainparams.h. There is no need to expose it; it only uses internal linkage at the moment.

  40. jtimon force-pushed on Sep 5, 2018
  41. jtimon commented at 0:06 am on September 5, 2018: contributor
    Hopefully fixed all pending nits in the last commit to be squashed, please remind me if I left anything out. Also sorry about the wait, back from vacation.
  42. ajtowns commented at 0:04 am on September 6, 2018: member
    Looks good to me, fwiw
  43. DrahtBot added the label Needs rebase on Sep 6, 2018
  44. jtimon force-pushed on Sep 7, 2018
  45. jtimon commented at 10:09 pm on September 7, 2018: contributor
    Of course, @DrahtBot , from now on I will rebase all my open PRs whenever you ask. Starting today with this PR. Thanks
  46. DrahtBot removed the label Needs rebase on Sep 7, 2018
  47. DrahtBot commented at 1:49 am on September 10, 2018: member
    • #14309 (scripted-diff: Use non-throwing type-safe ChainType where possible by MarcoFalke)
    • #14053 (Add address-based index (attempt 4?) by marcinja)
    • #14045 (refactor: Fix the chainparamsbase -> util circular dependency by Empact)
    • #13751 (Utils and libraries: Drops the boost/algorithm/string/split.hpp dependency by 251Labs)
    • #10823 (Allow all mempool txs to be replaced after a configurable timeout (default 6h) by greenaddress)
    • #10443 (Add fee_est tool for debugging fee estimation code 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.

  48. ken2812221 commented at 8:04 pm on September 12, 2018: contributor
    utACK 18d5f26
  49. in src/chainparams.cpp:404 in 18d5f2621f outdated
    402         return std::unique_ptr<CChainParams>(new CMainParams());
    403     else if (chain == CBaseChainParams::TESTNET)
    404         return std::unique_ptr<CChainParams>(new CTestNetParams());
    405     else if (chain == CBaseChainParams::REGTEST)
    406-        return std::unique_ptr<CChainParams>(new CRegTestParams());
    407+        return std::unique_ptr<CChainParams>(new CRegTestParams(gArgs));
    


    promag commented at 9:19 pm on September 16, 2018:
    Any reason to not use gArgs global in CRegTestParams constructor?

    ajtowns commented at 12:54 pm on September 18, 2018:
    The class only uses the global for initialisation; I think it makes sense to supply it as a parameter for initialisation rather than making the entire class dependent on the global. YMMV, FWIW, etc.
  50. promag commented at 9:19 pm on September 16, 2018: member
    utACK 18d5f26, with a little question.
  51. jtimon commented at 10:37 pm on September 16, 2018: contributor

    Any reason to use the global instead of a parameter? I think it’s more encapsulated this way, but I tend to be alergic to globals ib general. If people prefer a global I guess I will have to change it.

    On Sun, 16 Sep 2018, 23:21 João Barbosa, notifications@github.com wrote:

    @promag commented on this pull request.

    utACK 18d5f26 https://github.com/bitcoin/bitcoin/commit/18d5f2621f2ade428e631d34b95e23c8be862ec8, with a little question.

    In src/chainparams.cpp https://github.com/bitcoin/bitcoin/pull/13311#discussion_r217928196:

    { if (chain == CBaseChainParams::MAIN) return std::unique_ptr(new CMainParams()); else if (chain == CBaseChainParams::TESTNET) return std::unique_ptr(new CTestNetParams()); else if (chain == CBaseChainParams::REGTEST)

    •    return std::unique_ptr<CChainParams>(new CRegTestParams());
      
    •    return std::unique_ptr<CChainParams>(new CRegTestParams(gArgs));
      

    Any reason to not use gArgs global in CRegTestParams constructor?

    — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/bitcoin/bitcoin/pull/13311#pullrequestreview-155758424, or mute the thread https://github.com/notifications/unsubscribe-auth/AA9jStqUTYlVdHuFThL_C3Io272IQd7eks5ubsDAgaJpZM4UKcd3 .

  52. ajtowns commented at 1:03 pm on September 18, 2018: member
    utACK 18d5f2621f2ade428e631d34b95e23c8be862ec8
  53. in src/test/test_bitcoin.cpp:64 in 18d5f2621f outdated
    58@@ -58,6 +59,9 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
    59     InitSignatureCache();
    60     InitScriptExecutionCache();
    61     fCheckBlockIndex = true;
    62+    // CreateAndProcessBlock() does not support building SegWit blocks, so don't activate in these tests.
    63+    // TODO: fix the code to support SegWit blocks.
    64+    gArgs.ForceSetArg("-vbparams", strprintf("segwit:0:%d", (int64_t)Consensus::BIP9Deployment::NO_TIMEOUT));
    


    practicalswift commented at 3:05 pm on September 18, 2018:
    02018-09-18 16:46:43 clang-tidy(pr=13311): src/test/test_bitcoin.cpp:64:61: warning: redundant cast to the same type [google-readability-casting]
    

    jtimon commented at 4:02 pm on September 19, 2018:

    If I remove that casting, I get:

    0test/qt_test_test_bitcoin_qt-test_bitcoin.o: In function `tinyformat::detail::FormatArg::FormatArg<long>(long const&)':
    1/home/jt/code/bitcoin/src/./tinyformat.h:508: undefined reference to `Consensus::BIP9Deployment::NO_TIMEOUT'
    

    practicalswift commented at 4:11 pm on September 19, 2018:
    Cool! A false positive then. Thanks for investigating.
  54. in src/chainparams.cpp:377 in 18d5f2621f outdated
    373+        if (!ParseInt64(vDeploymentParams[2], &nTimeout)) {
    374+            throw std::runtime_error(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2]));
    375+        }
    376+        bool found = false;
    377+        for (int j=0; j<(int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
    378+            if (vDeploymentParams[0].compare(VersionBitsDeploymentInfo[j].name) == 0) {
    


    practicalswift commented at 3:06 pm on September 18, 2018:
    02018-09-18 16:46:43 clang-tidy(pr=13311): src/chainparams.cpp:377:17: warning: do not use 'compare' to test equality of strings; use the string equality operator instead [misc-string-compare]
    
  55. in src/chainparams.cpp:271 in 18d5f2621f outdated
    267@@ -270,7 +268,7 @@ class CTestNetParams : public CChainParams {
    268  */
    269 class CRegTestParams : public CChainParams {
    270 public:
    271-    CRegTestParams() {
    272+    CRegTestParams(const ArgsManager& args) {
    


    practicalswift commented at 3:07 pm on September 18, 2018:
    02018-09-18 16:46:43 clang-tidy(pr=13311): src/chainparams.cpp:271:5: warning: single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor]
    
  56. jtimon force-pushed on Sep 19, 2018
  57. jtimon commented at 4:03 pm on September 19, 2018: contributor
    Fixed 2 of the 3 clang warnings.
  58. in src/chainparams.cpp:366 in bd74fb9e4c outdated
    362+
    363+    for (const std::string& strDeployment : args.GetArgs("-vbparams")) {
    364+        std::vector<std::string> vDeploymentParams;
    365+        boost::split(vDeploymentParams, strDeployment, boost::is_any_of(":"));
    366+        if (vDeploymentParams.size() != 3) {
    367+            throw std::runtime_error("Version bits parameters malformed, expecting deployment:start:end");
    


    MarcoFalke commented at 6:46 pm on September 19, 2018:

    I don’t like that this introduces more exceptions in code that can normally hit them by a mere typo. I’d prefer if we used exceptions only in exceptional circumstances, since it is not easy to follow the control flow with exceptions.

    Note that the our other command line parsing is done with functions that return bool instead of void and append the error to a passed in string reference.

    I see that the caller needs to throw, but that isn’t reason enough to “poison” unrelated code.


    jtimon commented at 8:47 am on September 20, 2018:
    I think there’s absolutely no problem in initialization exceptions that will prevent the binary from starting, this one should be cached (depending on binary) on bitcoin-cli.cpp, bitcoin-tx.cpp or bitcoind.cpp, when SelectParams is called, just like https://github.com/bitcoin/bitcoin/blob/master/src/chainparamsbase.cpp#L42
  59. MarcoFalke commented at 6:46 pm on September 19, 2018: member
    utACK bd74fb9e4c271bd901dfbb5ad1929b44fcadd735
  60. MOVEONLY: Move versionbits info out of versionbits.o 980b38f8a1
  61. Don't edit Chainparams after initialization 6fa901fb47
  62. in src/chainparams.cpp:376 in bd74fb9e4c outdated
    372+        }
    373+        if (!ParseInt64(vDeploymentParams[2], &nTimeout)) {
    374+            throw std::runtime_error(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2]));
    375+        }
    376+        bool found = false;
    377+        for (int j=0; j<(int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
    


    practicalswift commented at 8:20 am on September 23, 2018:
    02018-09-22 22:20:08 cpplint(pr=13311): src/chainparams.cpp:376:  Missing spaces around <  [whitespace/operators] [3]
    
  63. jtimon force-pushed on Sep 23, 2018
  64. jtimon commented at 8:59 pm on September 23, 2018: contributor
    Fixed latest style nit.
  65. promag commented at 11:25 pm on September 23, 2018: member
    utACK 6fa901f.
  66. MarcoFalke commented at 8:45 pm on September 24, 2018: member
    re-utACK 6fa901f
  67. MarcoFalke merged this on Sep 24, 2018
  68. MarcoFalke closed this on Sep 24, 2018

  69. MarcoFalke referenced this in commit 4dac24db23 on Sep 24, 2018
  70. meshcollider removed this from the "Blockers" column in a project

  71. jtimon referenced this in commit a31ab0ec26 on Oct 13, 2018
  72. MarcoFalke referenced this in commit 0011167191 on Jun 3, 2021
  73. sidhujag referenced this in commit 1bfa968792 on Jun 3, 2021
  74. rkarthik2k21 referenced this in commit dae58c5554 on Aug 12, 2021
  75. rkarthik2k21 referenced this in commit 90b9a42a9b on Aug 25, 2021
  76. rkarthik2k21 referenced this in commit 187ffd0898 on Aug 25, 2021
  77. UdjinM6 referenced this in commit f456edb07e on Sep 2, 2021
  78. MarcoFalke 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-06-29 10:13 UTC

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