Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't edit Chainparams after initialization #13311

Merged
merged 2 commits into from
Sep 24, 2018

Conversation

jtimon
Copy link
Contributor

@jtimon jtimon commented May 23, 2018

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 #8994

@jtimon jtimon force-pushed the b17-regtest-only-params branch from 4b5e16a to 6414f5b Compare May 23, 2018 13:30
@skeees
Copy link
Contributor

skeees commented May 23, 2018

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

@sipa
Copy link
Member

sipa commented May 24, 2018

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

@jtimon
Copy link
Contributor Author

jtimon commented May 27, 2018

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.

@sipa
Copy link
Member

sipa commented May 27, 2018

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.

@maflcko
Copy link
Member

maflcko commented May 30, 2018

Needs rebase due to merge of #13341

@jtimon
Copy link
Contributor Author

jtimon commented Jun 16, 2018

Rebased and separated MOVEONLY commit.

Copy link
Member

@instagibbs instagibbs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK with ultra minor nit


#include <assert.h>

#include <chainparamsseeds.h>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NB: it's still used in init.cpp, only for mempool replacement


#include <assert.h>

#include <chainparamsseeds.h>
#include <boost/algorithm/string/classification.hpp>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NB: it's still used in init.cpp, only for mempool replacement

@@ -55,6 +55,9 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
InitSignatureCache();
InitScriptExecutionCache();
fCheckBlockIndex = true;
// CreateAndProcessBlock() does not support building SegWit blocks, so don't activate in these tests.
// TODO: fix the code to support SegWit blocks.
gArgs.ForceSetArg("-vbparams", "segwit:0:999999999999");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: would be nice to keep using Consensus::BIP9Deployment::NO_TIMEOUT

@jtimon
Copy link
Contributor Author

jtimon commented Jul 19, 2018

rebased

@jtimon jtimon force-pushed the b17-regtest-only-params branch from b2ad565 to 7067657 Compare July 19, 2018 22:35
@jtimon
Copy link
Contributor Author

jtimon commented Jul 19, 2018

Fixed nit in #13311 (comment)

@@ -280,7 +276,7 @@ class CTestNetParams : public CChainParams {
*/
class CRegTestParams : public CChainParams {
public:
CRegTestParams() {
CRegTestParams(ArgsManager& args) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

@Empact Empact Aug 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could make this const ArgsManager& args

Copy link
Contributor

@ajtowns ajtowns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK 7067657731404c28ff87e46ec390992500ca13be

}
}
}

static std::unique_ptr<CChainParams> globalChainParams;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

{
globalChainParams->UpdateVersionBitsParameters(d, nStartTime, nTimeout);
SelectBaseParams(network);
globalChainParams = CreateChainParams(network, gArgs);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@promag
Copy link
Contributor

promag commented Jul 20, 2018

Concept ACK.

@jtimon jtimon force-pushed the b17-regtest-only-params branch from 7067657 to ca040b5 Compare July 20, 2018 01:29
@jtimon
Copy link
Contributor Author

jtimon commented Jul 20, 2018

@@ -58,6 +59,9 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
InitSignatureCache();
InitScriptExecutionCache();
fCheckBlockIndex = true;
// CreateAndProcessBlock() does not support building SegWit blocks, so don't activate in these tests.
// TODO: fix the code to support SegWit blocks.
gArgs.ForceSetArg("-vbparams", strprintf("segwit:0:%d", (int64_t)Consensus::BIP9Deployment::NO_TIMEOUT));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2018-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]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove that casting, I get:

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! A false positive then. Thanks for investigating.

}
bool found = false;
for (int j=0; j<(int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
if (vDeploymentParams[0].compare(VersionBitsDeploymentInfo[j].name) == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2018-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]

@@ -270,7 +268,7 @@ class CTestNetParams : public CChainParams {
*/
class CRegTestParams : public CChainParams {
public:
CRegTestParams() {
CRegTestParams(const ArgsManager& args) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2018-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]

@jtimon jtimon force-pushed the b17-regtest-only-params branch from 18d5f26 to bd74fb9 Compare September 19, 2018 16:02
@jtimon
Copy link
Contributor Author

jtimon commented Sep 19, 2018

Fixed 2 of the 3 clang warnings.

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK bd74fb9e4c271bd901dfbb5ad1929b44fcadd735

std::vector<std::string> vDeploymentParams;
boost::split(vDeploymentParams, strDeployment, boost::is_any_of(":"));
if (vDeploymentParams.size() != 3) {
throw std::runtime_error("Version bits parameters malformed, expecting deployment:start:end");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

@jtimon jtimon Sep 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

throw std::runtime_error(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2]));
}
bool found = false;
for (int j=0; j<(int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2018-09-22 22:20:08 cpplint(pr=13311): src/chainparams.cpp:376:  Missing spaces around <  [whitespace/operators] [3]

@jtimon jtimon force-pushed the b17-regtest-only-params branch from bd74fb9 to 6fa901f Compare September 23, 2018 20:59
@jtimon
Copy link
Contributor Author

jtimon commented Sep 23, 2018

Fixed latest style nit.

@promag
Copy link
Contributor

promag commented Sep 23, 2018

utACK 6fa901f.

@maflcko
Copy link
Member

maflcko commented Sep 24, 2018

re-utACK 6fa901f

@maflcko maflcko merged commit 6fa901f into bitcoin:master Sep 24, 2018
maflcko pushed a commit that referenced this pull request Sep 24, 2018
6fa901f Don't edit Chainparams after initialization (Jorge Timón)
980b38f MOVEONLY: Move versionbits info out of versionbits.o (Jorge Timón)

Pull request description:

  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 #8994

Tree-SHA512: 79771d729a63a720e743a9c77d5e2d80369f072d66202a43c1304e83a7d0ef7c6103d4968a03aea9666cc89a7203c618da972124a677b38cfe62ddaeb28f9f5d
jtimon added a commit to ElementsProject/elements that referenced this pull request Oct 13, 2018
06f1b42 Don't edit Chainparams after initialization (Jorge Timón)
875a47c MOVEONLY: Move versionbits info out of versionbits.o (Jorge Timón)

Pull request description:

  This is a backport of bitcoin/bitcoin#13311 since it's already merged, merging this in elements-0.17 should at least make the next rebase slightly easier. And since we're heavily touching chainparams in elements, I think it's a good thing.

Tree-SHA512: 177f6396a34ecf87b609b39a108cc2b1e5b49bd8d12b705a4ae0241c7a5007db754ff01d6d045ee1e6a219ec6c6767663cd98558752c83961c4aff3378aef5a2
maflcko pushed a commit to maflcko/bitcoin-core that referenced this pull request Jun 3, 2021
5f23531 CRegTestParams: Use `args` instead of `gArgs`. (Kiminuo)

Pull request description:

  This PR is a very minor follow-up to bitcoin#13311.

  I believe that `gArgs` was just overlooked at the modified line.

ACKs for top commit:
  MarcoFalke:
    cr ACK 5f23531

Tree-SHA512: f4e4ed6b23fca60e88825b502f20a1341ee2e4429bc8a2a7e419057adb643abda11be2061fe7ee076931657736e629aff88fd2c33737c84c330dc9d64f368c30
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jun 3, 2021
5f23531 CRegTestParams: Use `args` instead of `gArgs`. (Kiminuo)

Pull request description:

  This PR is a very minor follow-up to bitcoin#13311.

  I believe that `gArgs` was just overlooked at the modified line.

ACKs for top commit:
  MarcoFalke:
    cr ACK 5f23531

Tree-SHA512: f4e4ed6b23fca60e88825b502f20a1341ee2e4429bc8a2a7e419057adb643abda11be2061fe7ee076931657736e629aff88fd2c33737c84c330dc9d64f368c30
rkarthik2k21 pushed a commit to rkarthik2k21/dash that referenced this pull request Aug 12, 2021
6fa901f Don't edit Chainparams after initialization (Jorge Timón)
980b38f MOVEONLY: Move versionbits info out of versionbits.o (Jorge Timón)

Pull request description:

  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 bitcoin#8994

Tree-SHA512: 79771d729a63a720e743a9c77d5e2d80369f072d66202a43c1304e83a7d0ef7c6103d4968a03aea9666cc89a7203c618da972124a677b38cfe62ddaeb28f9f5d
rkarthik2k21 pushed a commit to rkarthik2k21/dash that referenced this pull request Aug 25, 2021
6fa901f Don't edit Chainparams after initialization (Jorge Timón)
980b38f MOVEONLY: Move versionbits info out of versionbits.o (Jorge Timón)

Pull request description:

  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 bitcoin#8994

Tree-SHA512: 79771d729a63a720e743a9c77d5e2d80369f072d66202a43c1304e83a7d0ef7c6103d4968a03aea9666cc89a7203c618da972124a677b38cfe62ddaeb28f9f5d
rkarthik2k21 added a commit to rkarthik2k21/dash that referenced this pull request Aug 25, 2021
UdjinM6 added a commit to dashpay/dash that referenced this pull request Sep 2, 2021
* Merge bitcoin#13311: Don't edit Chainparams after initialization

6fa901f Don't edit Chainparams after initialization (Jorge Timón)
980b38f MOVEONLY: Move versionbits info out of versionbits.o (Jorge Timón)

Pull request description:

  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 bitcoin#8994

Tree-SHA512: 79771d729a63a720e743a9c77d5e2d80369f072d66202a43c1304e83a7d0ef7c6103d4968a03aea9666cc89a7203c618da972124a677b38cfe62ddaeb28f9f5d

* Resolve Merge with bitcoin#13311

* Incorporated review changes

* Apply suggestions from code review

* Update src/chainparams.cpp

* Update src/chainparams.cpp

Co-authored-by: PastaPastaPasta <6443210+PastaPastaPasta@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: PastaPastaPasta <6443210+PastaPastaPasta@users.noreply.github.com>

Co-authored-by: MarcoFalke <falke.marco@gmail.com>
Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
Co-authored-by: PastaPastaPasta <6443210+PastaPastaPasta@users.noreply.github.com>
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.