Browse Source

Chainparams: Use a regular factory for creating chainparams

0.15
Jorge Timón 10 years ago
parent
commit
f87f3626e3
No known key found for this signature in database
GPG Key ID: 8866C18EA1C944A2
  1. 4
      src/bitcoin-cli.cpp
  2. 45
      src/chainparams.cpp
  3. 13
      src/chainparams.h
  4. 19
      src/chainparamsbase.cpp
  5. 10
      src/chainparamsbase.h
  6. 6
      src/init.cpp

4
src/bitcoin-cli.cpp

@ -30,6 +30,8 @@ static const int CONTINUE_EXECUTION=-1;
std::string HelpMessageCli() std::string HelpMessageCli()
{ {
const auto defaultBaseParams = CreateBaseChainParams(CBaseChainParams::MAIN);
const auto testnetBaseParams = CreateBaseChainParams(CBaseChainParams::TESTNET);
std::string strUsage; std::string strUsage;
strUsage += HelpMessageGroup(_("Options:")); strUsage += HelpMessageGroup(_("Options:"));
strUsage += HelpMessageOpt("-?", _("This help message")); strUsage += HelpMessageOpt("-?", _("This help message"));
@ -38,7 +40,7 @@ std::string HelpMessageCli()
AppendParamsHelpMessages(strUsage); AppendParamsHelpMessages(strUsage);
strUsage += HelpMessageOpt("-named", strprintf(_("Pass named instead of positional arguments (default: %s)"), DEFAULT_NAMED)); strUsage += HelpMessageOpt("-named", strprintf(_("Pass named instead of positional arguments (default: %s)"), DEFAULT_NAMED));
strUsage += HelpMessageOpt("-rpcconnect=<ip>", strprintf(_("Send commands to node running on <ip> (default: %s)"), DEFAULT_RPCCONNECT)); strUsage += HelpMessageOpt("-rpcconnect=<ip>", strprintf(_("Send commands to node running on <ip> (default: %s)"), DEFAULT_RPCCONNECT));
strUsage += HelpMessageOpt("-rpcport=<port>", strprintf(_("Connect to JSON-RPC on <port> (default: %u or testnet: %u)"), BaseParams(CBaseChainParams::MAIN).RPCPort(), BaseParams(CBaseChainParams::TESTNET).RPCPort())); strUsage += HelpMessageOpt("-rpcport=<port>", strprintf(_("Connect to JSON-RPC on <port> (default: %u or testnet: %u)"), defaultBaseParams->RPCPort(), testnetBaseParams->RPCPort()));
strUsage += HelpMessageOpt("-rpcwait", _("Wait for RPC server to start")); strUsage += HelpMessageOpt("-rpcwait", _("Wait for RPC server to start"));
strUsage += HelpMessageOpt("-rpcuser=<user>", _("Username for JSON-RPC connections")); strUsage += HelpMessageOpt("-rpcuser=<user>", _("Username for JSON-RPC connections"));
strUsage += HelpMessageOpt("-rpcpassword=<pw>", _("Password for JSON-RPC connections")); strUsage += HelpMessageOpt("-rpcpassword=<pw>", _("Password for JSON-RPC connections"));

45
src/chainparams.cpp

@ -55,6 +55,12 @@ static CBlock CreateGenesisBlock(uint32_t nTime, uint32_t nNonce, uint32_t nBits
return CreateGenesisBlock(pszTimestamp, genesisOutputScript, nTime, nNonce, nBits, nVersion, genesisReward); return CreateGenesisBlock(pszTimestamp, genesisOutputScript, nTime, nNonce, nBits, nVersion, genesisReward);
} }
void CChainParams::UpdateBIP9Parameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
{
consensus.vDeployments[d].nStartTime = nStartTime;
consensus.vDeployments[d].nTimeout = nTimeout;
}
/** /**
* Main network * Main network
*/ */
@ -165,7 +171,6 @@ public:
}; };
} }
}; };
static CMainParams mainParams;
/** /**
* Testnet (v3) * Testnet (v3)
@ -253,7 +258,6 @@ public:
} }
}; };
static CTestNetParams testNetParams;
/** /**
* Regression test * Regression test
@ -326,42 +330,41 @@ public:
base58Prefixes[EXT_PUBLIC_KEY] = boost::assign::list_of(0x04)(0x35)(0x87)(0xCF).convert_to_container<std::vector<unsigned char> >(); base58Prefixes[EXT_PUBLIC_KEY] = boost::assign::list_of(0x04)(0x35)(0x87)(0xCF).convert_to_container<std::vector<unsigned char> >();
base58Prefixes[EXT_SECRET_KEY] = boost::assign::list_of(0x04)(0x35)(0x83)(0x94).convert_to_container<std::vector<unsigned char> >(); base58Prefixes[EXT_SECRET_KEY] = boost::assign::list_of(0x04)(0x35)(0x83)(0x94).convert_to_container<std::vector<unsigned char> >();
} }
void UpdateBIP9Parameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
{
consensus.vDeployments[d].nStartTime = nStartTime;
consensus.vDeployments[d].nTimeout = nTimeout;
}
}; };
static CRegTestParams regTestParams;
static CChainParams *pCurrentParams = 0; static std::unique_ptr<CChainParams> globalChainParams;
static std::unique_ptr<CChainParams> globalSwitchingChainParams;
const CChainParams &Params() { const CChainParams &Params() {
assert(pCurrentParams); assert(globalChainParams);
return *pCurrentParams; return *globalChainParams;
} }
CChainParams& Params(const std::string& chain) std::unique_ptr<CChainParams> CreateChainParams(const std::string& chain)
{ {
if (chain == CBaseChainParams::MAIN) if (chain == CBaseChainParams::MAIN)
return mainParams; return std::unique_ptr<CChainParams>(new CMainParams());
else if (chain == CBaseChainParams::TESTNET) else if (chain == CBaseChainParams::TESTNET)
return testNetParams; return std::unique_ptr<CChainParams>(new CTestNetParams());
else if (chain == CBaseChainParams::REGTEST) else if (chain == CBaseChainParams::REGTEST)
return regTestParams; return std::unique_ptr<CChainParams>(new CRegTestParams());
else throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain)); }
const CChainParams& Params(const std::string& chain)
{
globalSwitchingChainParams = CreateChainParams(chain);
return *globalSwitchingChainParams;
} }
void SelectParams(const std::string& network) void SelectParams(const std::string& network)
{ {
SelectBaseParams(network); SelectBaseParams(network);
pCurrentParams = &Params(network); globalChainParams = CreateChainParams(network);
} }
void UpdateRegtestBIP9Parameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout) void UpdateBIP9Parameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
{ {
regTestParams.UpdateBIP9Parameters(d, nStartTime, nTimeout); globalChainParams->UpdateBIP9Parameters(d, nStartTime, nTimeout);
} }

13
src/chainparams.h

@ -11,6 +11,7 @@
#include "primitives/block.h" #include "primitives/block.h"
#include "protocol.h" #include "protocol.h"
#include <memory>
#include <vector> #include <vector>
struct CDNSSeedData { struct CDNSSeedData {
@ -75,6 +76,7 @@ public:
const std::vector<SeedSpec6>& FixedSeeds() const { return vFixedSeeds; } const std::vector<SeedSpec6>& FixedSeeds() const { return vFixedSeeds; }
const CCheckpointData& Checkpoints() const { return checkpointData; } const CCheckpointData& Checkpoints() const { return checkpointData; }
const ChainTxData& TxData() const { return chainTxData; } const ChainTxData& TxData() const { return chainTxData; }
void UpdateBIP9Parameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout);
protected: protected:
CChainParams() {} CChainParams() {}
@ -94,6 +96,13 @@ protected:
ChainTxData chainTxData; ChainTxData chainTxData;
}; };
/**
* Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
* @returns a CChainParams* of the chosen chain.
* @throws a std::runtime_error if the chain is not supported.
*/
std::unique_ptr<CChainParams> CreateChainParams(const std::string& chain);
/** /**
* Return the currently selected parameters. This won't change after app * Return the currently selected parameters. This won't change after app
* startup, except for unit tests. * startup, except for unit tests.
@ -103,7 +112,7 @@ const CChainParams &Params();
/** /**
* @returns CChainParams for the given BIP70 chain name. * @returns CChainParams for the given BIP70 chain name.
*/ */
CChainParams& Params(const std::string& chain); const CChainParams& Params(const std::string& chain);
/** /**
* Sets the params returned by Params() to those for the given BIP70 chain name. * Sets the params returned by Params() to those for the given BIP70 chain name.
@ -114,6 +123,6 @@ void SelectParams(const std::string& chain);
/** /**
* Allows modifying the BIP9 regtest parameters. * Allows modifying the BIP9 regtest parameters.
*/ */
void UpdateRegtestBIP9Parameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout); void UpdateBIP9Parameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout);
#endif // BITCOIN_CHAINPARAMS_H #endif // BITCOIN_CHAINPARAMS_H

19
src/chainparamsbase.cpp

@ -35,7 +35,6 @@ public:
nRPCPort = 8332; nRPCPort = 8332;
} }
}; };
static CBaseMainParams mainParams;
/** /**
* Testnet (v3) * Testnet (v3)
@ -49,7 +48,6 @@ public:
strDataDir = "testnet3"; strDataDir = "testnet3";
} }
}; };
static CBaseTestNetParams testNetParams;
/* /*
* Regression test * Regression test
@ -63,31 +61,30 @@ public:
strDataDir = "regtest"; strDataDir = "regtest";
} }
}; };
static CBaseRegTestParams regTestParams;
static CBaseChainParams* pCurrentBaseParams = 0; static std::unique_ptr<CBaseChainParams> globalChainBaseParams;
const CBaseChainParams& BaseParams() const CBaseChainParams& BaseParams()
{ {
assert(pCurrentBaseParams); assert(globalChainBaseParams);
return *pCurrentBaseParams; return *globalChainBaseParams;
} }
CBaseChainParams& BaseParams(const std::string& chain) std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const std::string& chain)
{ {
if (chain == CBaseChainParams::MAIN) if (chain == CBaseChainParams::MAIN)
return mainParams; return std::unique_ptr<CBaseChainParams>(new CBaseMainParams());
else if (chain == CBaseChainParams::TESTNET) else if (chain == CBaseChainParams::TESTNET)
return testNetParams; return std::unique_ptr<CBaseChainParams>(new CBaseTestNetParams());
else if (chain == CBaseChainParams::REGTEST) else if (chain == CBaseChainParams::REGTEST)
return regTestParams; return std::unique_ptr<CBaseChainParams>(new CBaseRegTestParams());
else else
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain)); throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
} }
void SelectBaseParams(const std::string& chain) void SelectBaseParams(const std::string& chain)
{ {
pCurrentBaseParams = &BaseParams(chain); globalChainBaseParams = CreateBaseChainParams(chain);
} }
std::string ChainNameFromCommandLine() std::string ChainNameFromCommandLine()

10
src/chainparamsbase.h

@ -5,6 +5,7 @@
#ifndef BITCOIN_CHAINPARAMSBASE_H #ifndef BITCOIN_CHAINPARAMSBASE_H
#define BITCOIN_CHAINPARAMSBASE_H #define BITCOIN_CHAINPARAMSBASE_H
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@ -30,6 +31,13 @@ protected:
std::string strDataDir; std::string strDataDir;
}; };
/**
* Creates and returns a std::unique_ptr<CBaseChainParams> of the chosen chain.
* @returns a CBaseChainParams* of the chosen chain.
* @throws a std::runtime_error if the chain is not supported.
*/
std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const std::string& chain);
/** /**
* Append the help messages for the chainparams options to the * Append the help messages for the chainparams options to the
* parameter string. * parameter string.
@ -42,8 +50,6 @@ void AppendParamsHelpMessages(std::string& strUsage, bool debugHelp=true);
*/ */
const CBaseChainParams& BaseParams(); const CBaseChainParams& BaseParams();
CBaseChainParams& BaseParams(const std::string& chain);
/** Sets the params returned by Params() to those for the given network. */ /** Sets the params returned by Params() to those for the given network. */
void SelectBaseParams(const std::string& chain); void SelectBaseParams(const std::string& chain);

6
src/init.cpp

@ -328,6 +328,8 @@ void OnRPCPreCommand(const CRPCCommand& cmd)
std::string HelpMessage(HelpMessageMode mode) std::string HelpMessage(HelpMessageMode mode)
{ {
const auto defaultBaseParams = CreateBaseChainParams(CBaseChainParams::MAIN);
const auto testnetBaseParams = CreateBaseChainParams(CBaseChainParams::TESTNET);
const bool showDebug = GetBoolArg("-help-debug", false); const bool showDebug = GetBoolArg("-help-debug", false);
// When adding new options to the categories, please keep and ensure alphabetical ordering. // When adding new options to the categories, please keep and ensure alphabetical ordering.
@ -500,7 +502,7 @@ std::string HelpMessage(HelpMessageMode mode)
strUsage += HelpMessageOpt("-rpcuser=<user>", _("Username for JSON-RPC connections")); strUsage += HelpMessageOpt("-rpcuser=<user>", _("Username for JSON-RPC connections"));
strUsage += HelpMessageOpt("-rpcpassword=<pw>", _("Password for JSON-RPC connections")); strUsage += HelpMessageOpt("-rpcpassword=<pw>", _("Password for JSON-RPC connections"));
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")); 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"));
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())); strUsage += HelpMessageOpt("-rpcport=<port>", strprintf(_("Listen for JSON-RPC connections on <port> (default: %u or testnet: %u)"), defaultBaseParams->RPCPort(), testnetBaseParams->RPCPort()));
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")); 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"));
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)); 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));
strUsage += HelpMessageOpt("-rpcthreads=<n>", strprintf(_("Set the number of threads to service RPC calls (default: %d)"), DEFAULT_HTTP_THREADS)); 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()
for (int j=0; j<(int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) for (int j=0; j<(int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j)
{ {
if (vDeploymentParams[0].compare(VersionBitsDeploymentInfo[j].name) == 0) { if (vDeploymentParams[0].compare(VersionBitsDeploymentInfo[j].name) == 0) {
UpdateRegtestBIP9Parameters(Consensus::DeploymentPos(j), nStartTime, nTimeout); UpdateBIP9Parameters(Consensus::DeploymentPos(j), nStartTime, nTimeout);
found = true; found = true;
LogPrintf("Setting BIP9 activation parameters for %s to start=%ld, timeout=%ld\n", vDeploymentParams[0], nStartTime, nTimeout); LogPrintf("Setting BIP9 activation parameters for %s to start=%ld, timeout=%ld\n", vDeploymentParams[0], nStartTime, nTimeout);
break; break;

Loading…
Cancel
Save