Skip to content

Commit 55a8975

Browse files
committed
Chainparams: Translations: DRY: options and error strings
Also remove SelectBaseParamsFromCommandLine and SelectParamsFromCommandLine
1 parent f3525e2 commit 55a8975

File tree

9 files changed

+37
-51
lines changed

9 files changed

+37
-51
lines changed

src/bitcoin-cli.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ std::string HelpMessageCli()
2323
strUsage += HelpMessageOpt("-?", _("This help message"));
2424
strUsage += HelpMessageOpt("-conf=<file>", strprintf(_("Specify configuration file (default: %s)"), "bitcoin.conf"));
2525
strUsage += HelpMessageOpt("-datadir=<dir>", _("Specify data directory"));
26-
strUsage += HelpMessageOpt("-testnet", _("Use the test network"));
27-
strUsage += HelpMessageOpt("-regtest", _("Enter regression test mode, which uses a special chain in which blocks can be "
28-
"solved instantly. This is intended for regression testing tools and app development."));
26+
AppendParamsHelpMessages(strUsage);
2927
strUsage += HelpMessageOpt("-rpcconnect=<ip>", strprintf(_("Send commands to node running on <ip> (default: %s)"), "127.0.0.1"));
3028
strUsage += HelpMessageOpt("-rpcport=<port>", strprintf(_("Connect to JSON-RPC on <port> (default: %u or testnet: %u)"), 8332, 18332));
3129
strUsage += HelpMessageOpt("-rpcwait", _("Wait for RPC server to start"));
@@ -88,8 +86,10 @@ static bool AppInitRPC(int argc, char* argv[])
8886
return false;
8987
}
9088
// Check for -testnet or -regtest parameter (BaseParams() calls are only valid after this clause)
91-
if (!SelectBaseParamsFromCommandLine()) {
92-
fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n");
89+
try {
90+
SelectBaseParams(ChainNameFromCommandLine());
91+
} catch(std::exception &e) {
92+
fprintf(stderr, "Error: %s\n", e.what());
9393
return false;
9494
}
9595
return true;

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
@@ -102,8 +102,10 @@ bool AppInit(int argc, char* argv[])
102102
return false;
103103
}
104104
// Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
105-
if (!SelectParamsFromCommandLine()) {
106-
fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n");
105+
try {
106+
SelectParams(ChainNameFromCommandLine());
107+
} catch(std::exception &e) {
108+
fprintf(stderr, "Error: %s\n", e.what());
107109
return false;
108110
}
109111

src/chainparams.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,3 @@ void SelectParams(const std::string& network)
278278
SelectBaseParams(network);
279279
pCurrentParams = &Params(network);
280280
}
281-
282-
bool SelectParamsFromCommandLine()
283-
{
284-
std::string network = ChainNameFromCommandLine();
285-
if (network == CBaseChainParams::MAX_NETWORK_TYPES)
286-
return false;
287-
288-
SelectParams(network);
289-
return true;
290-
}

src/chainparams.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,4 @@ CChainParams& Params(const std::string& chain);
114114
*/
115115
void SelectParams(const std::string& chain);
116116

117-
/**
118-
* Looks for -regtest or -testnet and then calls SelectParams as appropriate.
119-
* Returns false if an invalid combination is given.
120-
*/
121-
bool SelectParamsFromCommandLine();
122-
123117
#endif // BITCOIN_CHAINPARAMS_H

src/chainparamsbase.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@
1313
const std::string CBaseChainParams::MAIN = "main";
1414
const std::string CBaseChainParams::TESTNET = "test";
1515
const std::string CBaseChainParams::REGTEST = "regtest";
16-
const std::string CBaseChainParams::MAX_NETWORK_TYPES = "unknown_chain";
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+
}
1726

1827
/**
1928
* Main network
@@ -95,24 +104,14 @@ std::string ChainNameFromCommandLine()
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-
std::string network = ChainNameFromCommandLine();
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: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class CBaseChainParams
1919
static const std::string MAIN;
2020
static const std::string TESTNET;
2121
static const std::string REGTEST;
22-
static const std::string MAX_NETWORK_TYPES;
2322

2423
const std::string& DataDir() const { return strDataDir; }
2524
int RPCPort() const { return nRPCPort; }
@@ -31,6 +30,12 @@ class CBaseChainParams
3130
std::string strDataDir;
3231
};
3332

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+
3439
/**
3540
* Return the currently selected parameters. This won't change after app
3641
* startup, except for unit tests.
@@ -46,12 +51,6 @@ void SelectBaseParams(const std::string& chain);
4651
*/
4752
std::string ChainNameFromCommandLine();
4853

49-
/**
50-
* Calls NetworkIdFromCommandLine() and then calls SelectParams as appropriate.
51-
* Returns false if an invalid combination is given.
52-
*/
53-
bool SelectBaseParamsFromCommandLine();
54-
5554
/**
5655
* Return true if SelectBaseParamsFromCommandLine() has been called to select
5756
* a network.

src/init.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,10 @@ std::string HelpMessage(HelpMessageMode mode)
396396
{
397397
strUsage += HelpMessageOpt("-printpriority", strprintf("Log transaction priority and fee per kB when mining blocks (default: %u)", 0));
398398
strUsage += HelpMessageOpt("-privdb", strprintf("Sets the DB_PRIVATE flag in the wallet db environment (default: %u)", 1));
399-
strUsage += HelpMessageOpt("-regtest", "Enter regression test mode, which uses a special chain in which blocks can be solved instantly. "
400-
"This is intended for regression testing tools and app development.");
401399
}
402400
strUsage += HelpMessageOpt("-shrinkdebugfile", _("Shrink debug.log file on client startup (default: 1 when no -debug)"));
403-
strUsage += HelpMessageOpt("-testnet", _("Use the test network"));
401+
402+
AppendParamsHelpMessages(strUsage, showDebug);
404403

405404
strUsage += HelpMessageGroup(_("Node relay options:"));
406405
if (showDebug)

src/qt/bitcoin.cpp

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

606606
// Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
607-
if (!SelectParamsFromCommandLine()) {
608-
QMessageBox::critical(0, QObject::tr("Bitcoin Core"), QObject::tr("Error: Invalid combination of -regtest and -testnet."));
607+
try {
608+
SelectParams(ChainNameFromCommandLine());
609+
} catch(std::exception &e) {
610+
QMessageBox::critical(0, QObject::tr("Bitcoin Core"), QObject::tr("Error: %1").arg(e.what()));
609611
return 1;
610612
}
611613
#ifdef ENABLE_WALLET

0 commit comments

Comments
 (0)