Skip to content

Commit f87f362

Browse files
committed
Chainparams: Use a regular factory for creating chainparams
1 parent 35da2ae commit f87f362

File tree

6 files changed

+58
-39
lines changed

6 files changed

+58
-39
lines changed

src/bitcoin-cli.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ static const int CONTINUE_EXECUTION=-1;
3030

3131
std::string HelpMessageCli()
3232
{
33+
const auto defaultBaseParams = CreateBaseChainParams(CBaseChainParams::MAIN);
34+
const auto testnetBaseParams = CreateBaseChainParams(CBaseChainParams::TESTNET);
3335
std::string strUsage;
3436
strUsage += HelpMessageGroup(_("Options:"));
3537
strUsage += HelpMessageOpt("-?", _("This help message"));
@@ -38,7 +40,7 @@ std::string HelpMessageCli()
3840
AppendParamsHelpMessages(strUsage);
3941
strUsage += HelpMessageOpt("-named", strprintf(_("Pass named instead of positional arguments (default: %s)"), DEFAULT_NAMED));
4042
strUsage += HelpMessageOpt("-rpcconnect=<ip>", strprintf(_("Send commands to node running on <ip> (default: %s)"), DEFAULT_RPCCONNECT));
41-
strUsage += HelpMessageOpt("-rpcport=<port>", strprintf(_("Connect to JSON-RPC on <port> (default: %u or testnet: %u)"), BaseParams(CBaseChainParams::MAIN).RPCPort(), BaseParams(CBaseChainParams::TESTNET).RPCPort()));
43+
strUsage += HelpMessageOpt("-rpcport=<port>", strprintf(_("Connect to JSON-RPC on <port> (default: %u or testnet: %u)"), defaultBaseParams->RPCPort(), testnetBaseParams->RPCPort()));
4244
strUsage += HelpMessageOpt("-rpcwait", _("Wait for RPC server to start"));
4345
strUsage += HelpMessageOpt("-rpcuser=<user>", _("Username for JSON-RPC connections"));
4446
strUsage += HelpMessageOpt("-rpcpassword=<pw>", _("Password for JSON-RPC connections"));

src/chainparams.cpp

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ static CBlock CreateGenesisBlock(uint32_t nTime, uint32_t nNonce, uint32_t nBits
5555
return CreateGenesisBlock(pszTimestamp, genesisOutputScript, nTime, nNonce, nBits, nVersion, genesisReward);
5656
}
5757

58+
void CChainParams::UpdateBIP9Parameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
59+
{
60+
consensus.vDeployments[d].nStartTime = nStartTime;
61+
consensus.vDeployments[d].nTimeout = nTimeout;
62+
}
63+
5864
/**
5965
* Main network
6066
*/
@@ -165,7 +171,6 @@ class CMainParams : public CChainParams {
165171
};
166172
}
167173
};
168-
static CMainParams mainParams;
169174

170175
/**
171176
* Testnet (v3)
@@ -253,7 +258,6 @@ class CTestNetParams : public CChainParams {
253258

254259
}
255260
};
256-
static CTestNetParams testNetParams;
257261

258262
/**
259263
* Regression test
@@ -326,42 +330,41 @@ class CRegTestParams : public CChainParams {
326330
base58Prefixes[EXT_PUBLIC_KEY] = boost::assign::list_of(0x04)(0x35)(0x87)(0xCF).convert_to_container<std::vector<unsigned char> >();
327331
base58Prefixes[EXT_SECRET_KEY] = boost::assign::list_of(0x04)(0x35)(0x83)(0x94).convert_to_container<std::vector<unsigned char> >();
328332
}
329-
330-
void UpdateBIP9Parameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
331-
{
332-
consensus.vDeployments[d].nStartTime = nStartTime;
333-
consensus.vDeployments[d].nTimeout = nTimeout;
334-
}
335333
};
336-
static CRegTestParams regTestParams;
337334

338-
static CChainParams *pCurrentParams = 0;
335+
static std::unique_ptr<CChainParams> globalChainParams;
336+
static std::unique_ptr<CChainParams> globalSwitchingChainParams;
339337

340338
const CChainParams &Params() {
341-
assert(pCurrentParams);
342-
return *pCurrentParams;
339+
assert(globalChainParams);
340+
return *globalChainParams;
343341
}
344342

345-
CChainParams& Params(const std::string& chain)
343+
std::unique_ptr<CChainParams> CreateChainParams(const std::string& chain)
346344
{
347345
if (chain == CBaseChainParams::MAIN)
348-
return mainParams;
346+
return std::unique_ptr<CChainParams>(new CMainParams());
349347
else if (chain == CBaseChainParams::TESTNET)
350-
return testNetParams;
348+
return std::unique_ptr<CChainParams>(new CTestNetParams());
351349
else if (chain == CBaseChainParams::REGTEST)
352-
return regTestParams;
353-
else
354-
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
350+
return std::unique_ptr<CChainParams>(new CRegTestParams());
351+
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
352+
}
353+
354+
const CChainParams& Params(const std::string& chain)
355+
{
356+
globalSwitchingChainParams = CreateChainParams(chain);
357+
return *globalSwitchingChainParams;
355358
}
356359

357360
void SelectParams(const std::string& network)
358361
{
359362
SelectBaseParams(network);
360-
pCurrentParams = &Params(network);
363+
globalChainParams = CreateChainParams(network);
361364
}
362365

363-
void UpdateRegtestBIP9Parameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
366+
void UpdateBIP9Parameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
364367
{
365-
regTestParams.UpdateBIP9Parameters(d, nStartTime, nTimeout);
368+
globalChainParams->UpdateBIP9Parameters(d, nStartTime, nTimeout);
366369
}
367370

src/chainparams.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "primitives/block.h"
1212
#include "protocol.h"
1313

14+
#include <memory>
1415
#include <vector>
1516

1617
struct CDNSSeedData {
@@ -75,6 +76,7 @@ class CChainParams
7576
const std::vector<SeedSpec6>& FixedSeeds() const { return vFixedSeeds; }
7677
const CCheckpointData& Checkpoints() const { return checkpointData; }
7778
const ChainTxData& TxData() const { return chainTxData; }
79+
void UpdateBIP9Parameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout);
7880
protected:
7981
CChainParams() {}
8082

@@ -94,6 +96,13 @@ class CChainParams
9496
ChainTxData chainTxData;
9597
};
9698

99+
/**
100+
* Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
101+
* @returns a CChainParams* of the chosen chain.
102+
* @throws a std::runtime_error if the chain is not supported.
103+
*/
104+
std::unique_ptr<CChainParams> CreateChainParams(const std::string& chain);
105+
97106
/**
98107
* Return the currently selected parameters. This won't change after app
99108
* startup, except for unit tests.
@@ -103,7 +112,7 @@ const CChainParams &Params();
103112
/**
104113
* @returns CChainParams for the given BIP70 chain name.
105114
*/
106-
CChainParams& Params(const std::string& chain);
115+
const CChainParams& Params(const std::string& chain);
107116

108117
/**
109118
* Sets the params returned by Params() to those for the given BIP70 chain name.
@@ -114,6 +123,6 @@ void SelectParams(const std::string& chain);
114123
/**
115124
* Allows modifying the BIP9 regtest parameters.
116125
*/
117-
void UpdateRegtestBIP9Parameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout);
126+
void UpdateBIP9Parameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout);
118127

119128
#endif // BITCOIN_CHAINPARAMS_H

src/chainparamsbase.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class CBaseMainParams : public CBaseChainParams
3535
nRPCPort = 8332;
3636
}
3737
};
38-
static CBaseMainParams mainParams;
3938

4039
/**
4140
* Testnet (v3)
@@ -49,7 +48,6 @@ class CBaseTestNetParams : public CBaseChainParams
4948
strDataDir = "testnet3";
5049
}
5150
};
52-
static CBaseTestNetParams testNetParams;
5351

5452
/*
5553
* Regression test
@@ -63,31 +61,30 @@ class CBaseRegTestParams : public CBaseChainParams
6361
strDataDir = "regtest";
6462
}
6563
};
66-
static CBaseRegTestParams regTestParams;
6764

68-
static CBaseChainParams* pCurrentBaseParams = 0;
65+
static std::unique_ptr<CBaseChainParams> globalChainBaseParams;
6966

7067
const CBaseChainParams& BaseParams()
7168
{
72-
assert(pCurrentBaseParams);
73-
return *pCurrentBaseParams;
69+
assert(globalChainBaseParams);
70+
return *globalChainBaseParams;
7471
}
7572

76-
CBaseChainParams& BaseParams(const std::string& chain)
73+
std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const std::string& chain)
7774
{
7875
if (chain == CBaseChainParams::MAIN)
79-
return mainParams;
76+
return std::unique_ptr<CBaseChainParams>(new CBaseMainParams());
8077
else if (chain == CBaseChainParams::TESTNET)
81-
return testNetParams;
78+
return std::unique_ptr<CBaseChainParams>(new CBaseTestNetParams());
8279
else if (chain == CBaseChainParams::REGTEST)
83-
return regTestParams;
80+
return std::unique_ptr<CBaseChainParams>(new CBaseRegTestParams());
8481
else
8582
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
8683
}
8784

8885
void SelectBaseParams(const std::string& chain)
8986
{
90-
pCurrentBaseParams = &BaseParams(chain);
87+
globalChainBaseParams = CreateBaseChainParams(chain);
9188
}
9289

9390
std::string ChainNameFromCommandLine()

src/chainparamsbase.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef BITCOIN_CHAINPARAMSBASE_H
66
#define BITCOIN_CHAINPARAMSBASE_H
77

8+
#include <memory>
89
#include <string>
910
#include <vector>
1011

@@ -30,6 +31,13 @@ class CBaseChainParams
3031
std::string strDataDir;
3132
};
3233

34+
/**
35+
* Creates and returns a std::unique_ptr<CBaseChainParams> of the chosen chain.
36+
* @returns a CBaseChainParams* of the chosen chain.
37+
* @throws a std::runtime_error if the chain is not supported.
38+
*/
39+
std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const std::string& chain);
40+
3341
/**
3442
* Append the help messages for the chainparams options to the
3543
* parameter string.
@@ -42,8 +50,6 @@ void AppendParamsHelpMessages(std::string& strUsage, bool debugHelp=true);
4250
*/
4351
const CBaseChainParams& BaseParams();
4452

45-
CBaseChainParams& BaseParams(const std::string& chain);
46-
4753
/** Sets the params returned by Params() to those for the given network. */
4854
void SelectBaseParams(const std::string& chain);
4955

src/init.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ void OnRPCPreCommand(const CRPCCommand& cmd)
328328

329329
std::string HelpMessage(HelpMessageMode mode)
330330
{
331+
const auto defaultBaseParams = CreateBaseChainParams(CBaseChainParams::MAIN);
332+
const auto testnetBaseParams = CreateBaseChainParams(CBaseChainParams::TESTNET);
331333
const bool showDebug = GetBoolArg("-help-debug", false);
332334

333335
// When adding new options to the categories, please keep and ensure alphabetical ordering.
@@ -500,7 +502,7 @@ std::string HelpMessage(HelpMessageMode mode)
500502
strUsage += HelpMessageOpt("-rpcuser=<user>", _("Username for JSON-RPC connections"));
501503
strUsage += HelpMessageOpt("-rpcpassword=<pw>", _("Password for JSON-RPC connections"));
502504
strUsage += HelpMessageOpt("-rpcauth=<userpw>", _("Username and hashed password for JSON-RPC connections. The field <userpw> comes in the format: <USERNAME>:<SALT>$<HASH>. A canonical python script is included in share/rpcuser. The client then connects normally using the rpcuser=<USERNAME>/rpcpassword=<PASSWORD> pair of arguments. This option can be specified multiple times"));
503-
strUsage += HelpMessageOpt("-rpcport=<port>", strprintf(_("Listen for JSON-RPC connections on <port> (default: %u or testnet: %u)"), BaseParams(CBaseChainParams::MAIN).RPCPort(), BaseParams(CBaseChainParams::TESTNET).RPCPort()));
505+
strUsage += HelpMessageOpt("-rpcport=<port>", strprintf(_("Listen for JSON-RPC connections on <port> (default: %u or testnet: %u)"), defaultBaseParams->RPCPort(), testnetBaseParams->RPCPort()));
504506
strUsage += HelpMessageOpt("-rpcallowip=<ip>", _("Allow JSON-RPC connections from specified source. Valid for <ip> are a single IP (e.g. 1.2.3.4), a network/netmask (e.g. 1.2.3.4/255.255.255.0) or a network/CIDR (e.g. 1.2.3.4/24). This option can be specified multiple times"));
505507
strUsage += HelpMessageOpt("-rpcserialversion", strprintf(_("Sets the serialization of raw transaction or block hex returned in non-verbose mode, non-segwit(0) or segwit(1) (default: %d)"), DEFAULT_RPC_SERIALIZE_VERSION));
506508
strUsage += HelpMessageOpt("-rpcthreads=<n>", strprintf(_("Set the number of threads to service RPC calls (default: %d)"), DEFAULT_HTTP_THREADS));
@@ -1123,7 +1125,7 @@ bool AppInitParameterInteraction()
11231125
for (int j=0; j<(int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j)
11241126
{
11251127
if (vDeploymentParams[0].compare(VersionBitsDeploymentInfo[j].name) == 0) {
1126-
UpdateRegtestBIP9Parameters(Consensus::DeploymentPos(j), nStartTime, nTimeout);
1128+
UpdateBIP9Parameters(Consensus::DeploymentPos(j), nStartTime, nTimeout);
11271129
found = true;
11281130
LogPrintf("Setting BIP9 activation parameters for %s to start=%ld, timeout=%ld\n", vDeploymentParams[0], nStartTime, nTimeout);
11291131
break;

0 commit comments

Comments
 (0)