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

View file

@ -103,11 +103,16 @@ protected:
*/
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.

View file

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

View file

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

View file

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

View file

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