Chainparams: Replace CBaseChainParams::Network enum with string constants (suggested by Wladimir)

This commit is contained in:
Jorge Timón 2015-06-30 21:39:49 +02:00
parent 49793fbb09
commit f3525e24e3
6 changed files with 48 additions and 45 deletions

View File

@ -5,6 +5,7 @@
#include "chainparams.h" #include "chainparams.h"
#include "tinyformat.h"
#include "util.h" #include "util.h"
#include "utilstrencodings.h" #include "utilstrencodings.h"
@ -260,28 +261,27 @@ const CChainParams &Params() {
return *pCurrentParams; return *pCurrentParams;
} }
CChainParams &Params(CBaseChainParams::Network network) { CChainParams& Params(const std::string& chain)
switch (network) { {
case CBaseChainParams::MAIN: if (chain == CBaseChainParams::MAIN)
return mainParams; return mainParams;
case CBaseChainParams::TESTNET: else if (chain == CBaseChainParams::TESTNET)
return testNetParams; return testNetParams;
case CBaseChainParams::REGTEST: else if (chain == CBaseChainParams::REGTEST)
return regTestParams; return regTestParams;
default: else
assert(false && "Unimplemented network"); throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
return mainParams;
}
} }
void SelectParams(CBaseChainParams::Network network) { void SelectParams(const std::string& network)
{
SelectBaseParams(network); SelectBaseParams(network);
pCurrentParams = &Params(network); pCurrentParams = &Params(network);
} }
bool SelectParamsFromCommandLine() bool SelectParamsFromCommandLine()
{ {
CBaseChainParams::Network network = NetworkIdFromCommandLine(); std::string network = ChainNameFromCommandLine();
if (network == CBaseChainParams::MAX_NETWORK_TYPES) if (network == CBaseChainParams::MAX_NETWORK_TYPES)
return false; return false;

View File

@ -103,11 +103,16 @@ protected:
*/ */
const CChainParams &Params(); const CChainParams &Params();
/** Return parameters for the given network. */ /**
CChainParams &Params(CBaseChainParams::Network network); * @returns CChainParams for the given BIP70 chain name.
*/
CChainParams& Params(const std::string& chain);
/** Sets the params returned by Params() to those for the given network. */ /**
void SelectParams(CBaseChainParams::Network network); * Sets the params returned by Params() to those for the given BIP70 chain name.
* @throws std::runtime_error when the chain is not supported.
*/
void SelectParams(const std::string& chain);
/** /**
* Looks for -regtest or -testnet and then calls SelectParams as appropriate. * Looks for -regtest or -testnet and then calls SelectParams as appropriate.

View File

@ -5,10 +5,16 @@
#include "chainparamsbase.h" #include "chainparamsbase.h"
#include "tinyformat.h"
#include "util.h" #include "util.h"
#include <assert.h> #include <assert.h>
const std::string CBaseChainParams::MAIN = "main";
const std::string CBaseChainParams::TESTNET = "test";
const std::string CBaseChainParams::REGTEST = "regtest";
const std::string CBaseChainParams::MAX_NETWORK_TYPES = "unknown_chain";
/** /**
* Main network * Main network
*/ */
@ -71,25 +77,19 @@ const CBaseChainParams& BaseParams()
return *pCurrentBaseParams; return *pCurrentBaseParams;
} }
void SelectBaseParams(CBaseChainParams::Network network) void SelectBaseParams(const std::string& chain)
{ {
switch (network) { if (chain == CBaseChainParams::MAIN)
case CBaseChainParams::MAIN:
pCurrentBaseParams = &mainParams; pCurrentBaseParams = &mainParams;
break; else if (chain == CBaseChainParams::TESTNET)
case CBaseChainParams::TESTNET:
pCurrentBaseParams = &testNetParams; pCurrentBaseParams = &testNetParams;
break; else if (chain == CBaseChainParams::REGTEST)
case CBaseChainParams::REGTEST:
pCurrentBaseParams = &regTestParams; pCurrentBaseParams = &regTestParams;
break; else
default: throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
assert(false && "Unimplemented network");
return;
}
} }
CBaseChainParams::Network NetworkIdFromCommandLine() std::string ChainNameFromCommandLine()
{ {
bool fRegTest = GetBoolArg("-regtest", false); bool fRegTest = GetBoolArg("-regtest", false);
bool fTestNet = GetBoolArg("-testnet", false); bool fTestNet = GetBoolArg("-testnet", false);
@ -105,7 +105,7 @@ CBaseChainParams::Network NetworkIdFromCommandLine()
bool SelectBaseParamsFromCommandLine() bool SelectBaseParamsFromCommandLine()
{ {
CBaseChainParams::Network network = NetworkIdFromCommandLine(); std::string network = ChainNameFromCommandLine();
if (network == CBaseChainParams::MAX_NETWORK_TYPES) if (network == CBaseChainParams::MAX_NETWORK_TYPES)
return false; return false;

View File

@ -15,13 +15,11 @@
class CBaseChainParams class CBaseChainParams
{ {
public: public:
enum Network { /** BIP70 chain name strings (main, test or regtest) */
MAIN, static const std::string MAIN;
TESTNET, static const std::string TESTNET;
REGTEST, static const std::string REGTEST;
static const std::string MAX_NETWORK_TYPES;
MAX_NETWORK_TYPES
};
const std::string& DataDir() const { return strDataDir; } const std::string& DataDir() const { return strDataDir; }
int RPCPort() const { return nRPCPort; } int RPCPort() const { return nRPCPort; }
@ -40,13 +38,13 @@ protected:
const CBaseChainParams& BaseParams(); const CBaseChainParams& BaseParams();
/** 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(CBaseChainParams::Network network); void SelectBaseParams(const std::string& chain);
/** /**
* Looks for -regtest or -testnet and returns the appropriate Network ID. * Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
* Returns MAX_NETWORK_TYPES if an invalid combination is given. * @return CBaseChainParams::MAX_NETWORK_TYPES if an invalid combination is given. CBaseChainParams::MAIN by default.
*/ */
CBaseChainParams::Network NetworkIdFromCommandLine(); std::string ChainNameFromCommandLine();
/** /**
* Calls NetworkIdFromCommandLine() and then calls SelectParams as appropriate. * Calls NetworkIdFromCommandLine() and then calls SelectParams as appropriate.

View File

@ -32,13 +32,13 @@ CWallet* pwalletMain;
extern bool fPrintToConsole; extern bool fPrintToConsole;
extern void noui_connect(); extern void noui_connect();
BasicTestingSetup::BasicTestingSetup(CBaseChainParams::Network network) BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
{ {
ECC_Start(); ECC_Start();
SetupEnvironment(); SetupEnvironment();
fPrintToDebugLog = false; // don't want to write to debug.log file fPrintToDebugLog = false; // don't want to write to debug.log file
fCheckBlockIndex = true; fCheckBlockIndex = true;
SelectParams(network); SelectParams(chainName);
noui_connect(); noui_connect();
} }
@ -47,7 +47,7 @@ BasicTestingSetup::~BasicTestingSetup()
ECC_Stop(); ECC_Stop();
} }
TestingSetup::TestingSetup(CBaseChainParams::Network network) : BasicTestingSetup(network) TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(chainName)
{ {
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
bitdb.MakeMock(); bitdb.MakeMock();

View File

@ -12,7 +12,7 @@
* This just configures logging and chain parameters. * This just configures logging and chain parameters.
*/ */
struct BasicTestingSetup { struct BasicTestingSetup {
BasicTestingSetup(CBaseChainParams::Network network = CBaseChainParams::MAIN); BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
~BasicTestingSetup(); ~BasicTestingSetup();
}; };
@ -25,7 +25,7 @@ struct TestingSetup: public BasicTestingSetup {
boost::filesystem::path pathTemp; boost::filesystem::path pathTemp;
boost::thread_group threadGroup; boost::thread_group threadGroup;
TestingSetup(CBaseChainParams::Network network = CBaseChainParams::MAIN); TestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
~TestingSetup(); ~TestingSetup();
}; };