Skip to content

Commit f3525e2

Browse files
committed
Chainparams: Replace CBaseChainParams::Network enum with string constants (suggested by Wladimir)
1 parent 49793fb commit f3525e2

File tree

6 files changed

+48
-45
lines changed

6 files changed

+48
-45
lines changed

src/chainparams.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "chainparams.h"
77

8+
#include "tinyformat.h"
89
#include "util.h"
910
#include "utilstrencodings.h"
1011

@@ -260,28 +261,27 @@ const CChainParams &Params() {
260261
return *pCurrentParams;
261262
}
262263

263-
CChainParams &Params(CBaseChainParams::Network network) {
264-
switch (network) {
265-
case CBaseChainParams::MAIN:
264+
CChainParams& Params(const std::string& chain)
265+
{
266+
if (chain == CBaseChainParams::MAIN)
266267
return mainParams;
267-
case CBaseChainParams::TESTNET:
268+
else if (chain == CBaseChainParams::TESTNET)
268269
return testNetParams;
269-
case CBaseChainParams::REGTEST:
270+
else if (chain == CBaseChainParams::REGTEST)
270271
return regTestParams;
271-
default:
272-
assert(false && "Unimplemented network");
273-
return mainParams;
274-
}
272+
else
273+
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
275274
}
276275

277-
void SelectParams(CBaseChainParams::Network network) {
276+
void SelectParams(const std::string& network)
277+
{
278278
SelectBaseParams(network);
279279
pCurrentParams = &Params(network);
280280
}
281281

282282
bool SelectParamsFromCommandLine()
283283
{
284-
CBaseChainParams::Network network = NetworkIdFromCommandLine();
284+
std::string network = ChainNameFromCommandLine();
285285
if (network == CBaseChainParams::MAX_NETWORK_TYPES)
286286
return false;
287287

src/chainparams.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,16 @@ class CChainParams
103103
*/
104104
const CChainParams &Params();
105105

106-
/** Return parameters for the given network. */
107-
CChainParams &Params(CBaseChainParams::Network network);
106+
/**
107+
* @returns CChainParams for the given BIP70 chain name.
108+
*/
109+
CChainParams& Params(const std::string& chain);
108110

109-
/** Sets the params returned by Params() to those for the given network. */
110-
void SelectParams(CBaseChainParams::Network network);
111+
/**
112+
* Sets the params returned by Params() to those for the given BIP70 chain name.
113+
* @throws std::runtime_error when the chain is not supported.
114+
*/
115+
void SelectParams(const std::string& chain);
111116

112117
/**
113118
* Looks for -regtest or -testnet and then calls SelectParams as appropriate.

src/chainparamsbase.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55

66
#include "chainparamsbase.h"
77

8+
#include "tinyformat.h"
89
#include "util.h"
910

1011
#include <assert.h>
1112

13+
const std::string CBaseChainParams::MAIN = "main";
14+
const std::string CBaseChainParams::TESTNET = "test";
15+
const std::string CBaseChainParams::REGTEST = "regtest";
16+
const std::string CBaseChainParams::MAX_NETWORK_TYPES = "unknown_chain";
17+
1218
/**
1319
* Main network
1420
*/
@@ -71,25 +77,19 @@ const CBaseChainParams& BaseParams()
7177
return *pCurrentBaseParams;
7278
}
7379

74-
void SelectBaseParams(CBaseChainParams::Network network)
80+
void SelectBaseParams(const std::string& chain)
7581
{
76-
switch (network) {
77-
case CBaseChainParams::MAIN:
82+
if (chain == CBaseChainParams::MAIN)
7883
pCurrentBaseParams = &mainParams;
79-
break;
80-
case CBaseChainParams::TESTNET:
84+
else if (chain == CBaseChainParams::TESTNET)
8185
pCurrentBaseParams = &testNetParams;
82-
break;
83-
case CBaseChainParams::REGTEST:
86+
else if (chain == CBaseChainParams::REGTEST)
8487
pCurrentBaseParams = &regTestParams;
85-
break;
86-
default:
87-
assert(false && "Unimplemented network");
88-
return;
89-
}
88+
else
89+
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
9090
}
9191

92-
CBaseChainParams::Network NetworkIdFromCommandLine()
92+
std::string ChainNameFromCommandLine()
9393
{
9494
bool fRegTest = GetBoolArg("-regtest", false);
9595
bool fTestNet = GetBoolArg("-testnet", false);
@@ -105,7 +105,7 @@ CBaseChainParams::Network NetworkIdFromCommandLine()
105105

106106
bool SelectBaseParamsFromCommandLine()
107107
{
108-
CBaseChainParams::Network network = NetworkIdFromCommandLine();
108+
std::string network = ChainNameFromCommandLine();
109109
if (network == CBaseChainParams::MAX_NETWORK_TYPES)
110110
return false;
111111

src/chainparamsbase.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
class CBaseChainParams
1616
{
1717
public:
18-
enum Network {
19-
MAIN,
20-
TESTNET,
21-
REGTEST,
22-
23-
MAX_NETWORK_TYPES
24-
};
18+
/** BIP70 chain name strings (main, test or regtest) */
19+
static const std::string MAIN;
20+
static const std::string TESTNET;
21+
static const std::string REGTEST;
22+
static const std::string MAX_NETWORK_TYPES;
2523

2624
const std::string& DataDir() const { return strDataDir; }
2725
int RPCPort() const { return nRPCPort; }
@@ -40,13 +38,13 @@ class CBaseChainParams
4038
const CBaseChainParams& BaseParams();
4139

4240
/** Sets the params returned by Params() to those for the given network. */
43-
void SelectBaseParams(CBaseChainParams::Network network);
41+
void SelectBaseParams(const std::string& chain);
4442

4543
/**
46-
* Looks for -regtest or -testnet and returns the appropriate Network ID.
47-
* Returns MAX_NETWORK_TYPES if an invalid combination is given.
44+
* Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
45+
* @return CBaseChainParams::MAX_NETWORK_TYPES if an invalid combination is given. CBaseChainParams::MAIN by default.
4846
*/
49-
CBaseChainParams::Network NetworkIdFromCommandLine();
47+
std::string ChainNameFromCommandLine();
5048

5149
/**
5250
* Calls NetworkIdFromCommandLine() and then calls SelectParams as appropriate.

src/test/test_bitcoin.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ CWallet* pwalletMain;
3232
extern bool fPrintToConsole;
3333
extern void noui_connect();
3434

35-
BasicTestingSetup::BasicTestingSetup(CBaseChainParams::Network network)
35+
BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
3636
{
3737
ECC_Start();
3838
SetupEnvironment();
3939
fPrintToDebugLog = false; // don't want to write to debug.log file
4040
fCheckBlockIndex = true;
41-
SelectParams(network);
41+
SelectParams(chainName);
4242
noui_connect();
4343
}
4444

@@ -47,7 +47,7 @@ BasicTestingSetup::~BasicTestingSetup()
4747
ECC_Stop();
4848
}
4949

50-
TestingSetup::TestingSetup(CBaseChainParams::Network network) : BasicTestingSetup(network)
50+
TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(chainName)
5151
{
5252
#ifdef ENABLE_WALLET
5353
bitdb.MakeMock();

src/test/test_bitcoin.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* This just configures logging and chain parameters.
1313
*/
1414
struct BasicTestingSetup {
15-
BasicTestingSetup(CBaseChainParams::Network network = CBaseChainParams::MAIN);
15+
BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
1616
~BasicTestingSetup();
1717
};
1818

@@ -25,7 +25,7 @@ struct TestingSetup: public BasicTestingSetup {
2525
boost::filesystem::path pathTemp;
2626
boost::thread_group threadGroup;
2727

28-
TestingSetup(CBaseChainParams::Network network = CBaseChainParams::MAIN);
28+
TestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
2929
~TestingSetup();
3030
};
3131

0 commit comments

Comments
 (0)