Skip to content

Commit e26a3f6

Browse files
committed
Merge pull request #6235
55a8975 Chainparams: Translations: DRY: options and error strings (Jorge Timón) f3525e2 Chainparams: Replace CBaseChainParams::Network enum with string constants (suggested by Wladimir) (Jorge Timón)
2 parents 020c407 + 55a8975 commit e26a3f6

11 files changed

+79
-90
lines changed

src/bitcoin-cli.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ std::string HelpMessageCli()
3131
strUsage += HelpMessageOpt("-?", _("This help message"));
3232
strUsage += HelpMessageOpt("-conf=<file>", strprintf(_("Specify configuration file (default: %s)"), "bitcoin.conf"));
3333
strUsage += HelpMessageOpt("-datadir=<dir>", _("Specify data directory"));
34-
strUsage += HelpMessageOpt("-testnet", _("Use the test network"));
35-
strUsage += HelpMessageOpt("-regtest", _("Enter regression test mode, which uses a special chain in which blocks can be "
36-
"solved instantly. This is intended for regression testing tools and app development."));
34+
AppendParamsHelpMessages(strUsage);
3735
strUsage += HelpMessageOpt("-rpcconnect=<ip>", strprintf(_("Send commands to node running on <ip> (default: %s)"), "127.0.0.1"));
3836
strUsage += HelpMessageOpt("-rpcport=<port>", strprintf(_("Connect to JSON-RPC on <port> (default: %u or testnet: %u)"), 8332, 18332));
3937
strUsage += HelpMessageOpt("-rpcwait", _("Wait for RPC server to start"));
@@ -94,8 +92,10 @@ static bool AppInitRPC(int argc, char* argv[])
9492
return false;
9593
}
9694
// Check for -testnet or -regtest parameter (BaseParams() calls are only valid after this clause)
97-
if (!SelectBaseParamsFromCommandLine()) {
98-
fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n");
95+
try {
96+
SelectBaseParams(ChainNameFromCommandLine());
97+
} catch(std::exception &e) {
98+
fprintf(stderr, "Error: %s\n", e.what());
9999
return false;
100100
}
101101
if (GetBoolArg("-rpcssl", false))

src/bitcoin-tx.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ static bool AppInitRawTx(int argc, char* argv[])
3535
ParseParameters(argc, argv);
3636

3737
// Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
38-
if (!SelectParamsFromCommandLine()) {
39-
fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n");
38+
try {
39+
SelectParams(ChainNameFromCommandLine());
40+
} catch(std::exception &e) {
41+
fprintf(stderr, "Error: %s\n", e.what());
4042
return false;
4143
}
4244

@@ -58,8 +60,7 @@ static bool AppInitRawTx(int argc, char* argv[])
5860
strUsage += HelpMessageOpt("-create", _("Create new, empty TX."));
5961
strUsage += HelpMessageOpt("-json", _("Select JSON output"));
6062
strUsage += HelpMessageOpt("-txid", _("Output only the hex-encoded transaction id of the resultant transaction."));
61-
strUsage += HelpMessageOpt("-regtest", _("Enter regression test mode, which uses a special chain in which blocks can be solved instantly."));
62-
strUsage += HelpMessageOpt("-testnet", _("Use the test network"));
63+
AppendParamsHelpMessages(strUsage);
6364

6465
fprintf(stdout, "%s", strUsage.c_str());
6566

src/bitcoind.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@ bool AppInit(int argc, char* argv[])
107107
return false;
108108
}
109109
// Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
110-
if (!SelectParamsFromCommandLine()) {
111-
fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n");
110+
try {
111+
SelectParams(ChainNameFromCommandLine());
112+
} catch(std::exception &e) {
113+
fprintf(stderr, "Error: %s\n", e.what());
112114
return false;
113115
}
114116

src/chainparams.cpp

Lines changed: 10 additions & 20 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

@@ -266,31 +267,20 @@ const CChainParams &Params() {
266267
return *pCurrentParams;
267268
}
268269

269-
CChainParams &Params(CBaseChainParams::Network network) {
270-
switch (network) {
271-
case CBaseChainParams::MAIN:
270+
CChainParams& Params(const std::string& chain)
271+
{
272+
if (chain == CBaseChainParams::MAIN)
272273
return mainParams;
273-
case CBaseChainParams::TESTNET:
274+
else if (chain == CBaseChainParams::TESTNET)
274275
return testNetParams;
275-
case CBaseChainParams::REGTEST:
276+
else if (chain == CBaseChainParams::REGTEST)
276277
return regTestParams;
277-
default:
278-
assert(false && "Unimplemented network");
279-
return mainParams;
280-
}
278+
else
279+
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
281280
}
282281

283-
void SelectParams(CBaseChainParams::Network network) {
282+
void SelectParams(const std::string& network)
283+
{
284284
SelectBaseParams(network);
285285
pCurrentParams = &Params(network);
286286
}
287-
288-
bool SelectParamsFromCommandLine()
289-
{
290-
CBaseChainParams::Network network = NetworkIdFromCommandLine();
291-
if (network == CBaseChainParams::MAX_NETWORK_TYPES)
292-
return false;
293-
294-
SelectParams(network);
295-
return true;
296-
}

src/chainparams.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,15 @@ class CChainParams
105105
*/
106106
const CChainParams &Params();
107107

108-
/** Return parameters for the given network. */
109-
CChainParams &Params(CBaseChainParams::Network network);
110-
111-
/** Sets the params returned by Params() to those for the given network. */
112-
void SelectParams(CBaseChainParams::Network network);
108+
/**
109+
* @returns CChainParams for the given BIP70 chain name.
110+
*/
111+
CChainParams& Params(const std::string& chain);
113112

114113
/**
115-
* Looks for -regtest or -testnet and then calls SelectParams as appropriate.
116-
* Returns false if an invalid combination is given.
114+
* Sets the params returned by Params() to those for the given BIP70 chain name.
115+
* @throws std::runtime_error when the chain is not supported.
117116
*/
118-
bool SelectParamsFromCommandLine();
117+
void SelectParams(const std::string& chain);
119118

120119
#endif // BITCOIN_CHAINPARAMS_H

src/chainparamsbase.cpp

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,25 @@
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+
17+
void AppendParamsHelpMessages(std::string& strUsage, bool debugHelp)
18+
{
19+
strUsage += HelpMessageGroup(_("Chain selection options:"));
20+
strUsage += HelpMessageOpt("-testnet", _("Use the test chain"));
21+
if (debugHelp) {
22+
strUsage += HelpMessageOpt("-regtest", "Enter regression test mode, which uses a special chain in which blocks can be solved instantly. "
23+
"This is intended for regression testing tools and app development.");
24+
}
25+
}
26+
1227
/**
1328
* Main network
1429
*/
@@ -71,48 +86,32 @@ const CBaseChainParams& BaseParams()
7186
return *pCurrentBaseParams;
7287
}
7388

74-
void SelectBaseParams(CBaseChainParams::Network network)
89+
void SelectBaseParams(const std::string& chain)
7590
{
76-
switch (network) {
77-
case CBaseChainParams::MAIN:
91+
if (chain == CBaseChainParams::MAIN)
7892
pCurrentBaseParams = &mainParams;
79-
break;
80-
case CBaseChainParams::TESTNET:
93+
else if (chain == CBaseChainParams::TESTNET)
8194
pCurrentBaseParams = &testNetParams;
82-
break;
83-
case CBaseChainParams::REGTEST:
95+
else if (chain == CBaseChainParams::REGTEST)
8496
pCurrentBaseParams = &regTestParams;
85-
break;
86-
default:
87-
assert(false && "Unimplemented network");
88-
return;
89-
}
97+
else
98+
throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
9099
}
91100

92-
CBaseChainParams::Network NetworkIdFromCommandLine()
101+
std::string ChainNameFromCommandLine()
93102
{
94103
bool fRegTest = GetBoolArg("-regtest", false);
95104
bool fTestNet = GetBoolArg("-testnet", false);
96105

97106
if (fTestNet && fRegTest)
98-
return CBaseChainParams::MAX_NETWORK_TYPES;
107+
throw std::runtime_error("Invalid combination of -regtest and -testnet.");
99108
if (fRegTest)
100109
return CBaseChainParams::REGTEST;
101110
if (fTestNet)
102111
return CBaseChainParams::TESTNET;
103112
return CBaseChainParams::MAIN;
104113
}
105114

106-
bool SelectBaseParamsFromCommandLine()
107-
{
108-
CBaseChainParams::Network network = NetworkIdFromCommandLine();
109-
if (network == CBaseChainParams::MAX_NETWORK_TYPES)
110-
return false;
111-
112-
SelectBaseParams(network);
113-
return true;
114-
}
115-
116115
bool AreBaseParamsConfigured()
117116
{
118117
return pCurrentBaseParams != NULL;

src/chainparamsbase.h

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@
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;
2522

2623
const std::string& DataDir() const { return strDataDir; }
2724
int RPCPort() const { return nRPCPort; }
@@ -33,26 +30,26 @@ class CBaseChainParams
3330
std::string strDataDir;
3431
};
3532

33+
/**
34+
* Append the help messages for the chainparams options to the
35+
* parameter string.
36+
*/
37+
void AppendParamsHelpMessages(std::string& strUsage, bool debugHelp=true);
38+
3639
/**
3740
* Return the currently selected parameters. This won't change after app
3841
* startup, except for unit tests.
3942
*/
4043
const CBaseChainParams& BaseParams();
4144

4245
/** Sets the params returned by Params() to those for the given network. */
43-
void SelectBaseParams(CBaseChainParams::Network network);
44-
45-
/**
46-
* Looks for -regtest or -testnet and returns the appropriate Network ID.
47-
* Returns MAX_NETWORK_TYPES if an invalid combination is given.
48-
*/
49-
CBaseChainParams::Network NetworkIdFromCommandLine();
46+
void SelectBaseParams(const std::string& chain);
5047

5148
/**
52-
* Calls NetworkIdFromCommandLine() and then calls SelectParams as appropriate.
53-
* Returns false if an invalid combination is given.
49+
* Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
50+
* @return CBaseChainParams::MAX_NETWORK_TYPES if an invalid combination is given. CBaseChainParams::MAIN by default.
5451
*/
55-
bool SelectBaseParamsFromCommandLine();
52+
std::string ChainNameFromCommandLine();
5653

5754
/**
5855
* Return true if SelectBaseParamsFromCommandLine() has been called to select

src/init.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,10 @@ std::string HelpMessage(HelpMessageMode mode)
439439
{
440440
strUsage += HelpMessageOpt("-printpriority", strprintf("Log transaction priority and fee per kB when mining blocks (default: %u)", 0));
441441
strUsage += HelpMessageOpt("-privdb", strprintf("Sets the DB_PRIVATE flag in the wallet db environment (default: %u)", 1));
442-
strUsage += HelpMessageOpt("-regtest", "Enter regression test mode, which uses a special chain in which blocks can be solved instantly. "
443-
"This is intended for regression testing tools and app development.");
444442
}
445443
strUsage += HelpMessageOpt("-shrinkdebugfile", _("Shrink debug.log file on client startup (default: 1 when no -debug)"));
446-
strUsage += HelpMessageOpt("-testnet", _("Use the test network"));
444+
445+
AppendParamsHelpMessages(strUsage, showDebug);
447446

448447
strUsage += HelpMessageGroup(_("Node relay options:"));
449448
if (showDebug)

src/qt/bitcoin.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,10 @@ int main(int argc, char *argv[])
597597
// - Needs to be done before createOptionsModel
598598

599599
// Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
600-
if (!SelectParamsFromCommandLine()) {
601-
QMessageBox::critical(0, QObject::tr("Bitcoin Core"), QObject::tr("Error: Invalid combination of -regtest and -testnet."));
600+
try {
601+
SelectParams(ChainNameFromCommandLine());
602+
} catch(std::exception &e) {
603+
QMessageBox::critical(0, QObject::tr("Bitcoin Core"), QObject::tr("Error: %1").arg(e.what()));
602604
return 1;
603605
}
604606
#ifdef ENABLE_WALLET

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();

0 commit comments

Comments
 (0)