Move ChainNameFromCommandLine into ArgsManager and rename to GetChainName

pull/585/head
Anthony Towns 7 years ago
parent 5f0c6a7b0e
commit 11b6b5b86e

@ -114,7 +114,7 @@ static int AppInitRPC(int argc, char* argv[])
} }
// Check for -testnet or -regtest parameter (BaseParams() calls are only valid after this clause) // Check for -testnet or -regtest parameter (BaseParams() calls are only valid after this clause)
try { try {
SelectBaseParams(ChainNameFromCommandLine()); SelectBaseParams(gArgs.GetChainName());
} catch (const std::exception& e) { } catch (const std::exception& e) {
fprintf(stderr, "Error: %s\n", e.what()); fprintf(stderr, "Error: %s\n", e.what());
return EXIT_FAILURE; return EXIT_FAILURE;

@ -44,7 +44,7 @@ static int AppInitRawTx(int argc, char* argv[])
// Check for -testnet or -regtest parameter (Params() calls are only valid after this clause) // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
try { try {
SelectParams(ChainNameFromCommandLine()); SelectParams(gArgs.GetChainName());
} catch (const std::exception& e) { } catch (const std::exception& e) {
fprintf(stderr, "Error: %s\n", e.what()); fprintf(stderr, "Error: %s\n", e.what());
return EXIT_FAILURE; return EXIT_FAILURE;

@ -111,7 +111,7 @@ bool AppInit(int argc, char* argv[])
} }
// Check for -testnet or -regtest parameter (Params() calls are only valid after this clause) // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
try { try {
SelectParams(ChainNameFromCommandLine()); SelectParams(gArgs.GetChainName());
} catch (const std::exception& e) { } catch (const std::exception& e) {
fprintf(stderr, "Error: %s\n", e.what()); fprintf(stderr, "Error: %s\n", e.what());
return false; return false;

@ -49,17 +49,3 @@ void SelectBaseParams(const std::string& chain)
{ {
globalChainBaseParams = CreateBaseChainParams(chain); globalChainBaseParams = CreateBaseChainParams(chain);
} }
std::string ChainNameFromCommandLine()
{
bool fRegTest = gArgs.GetBoolArg("-regtest", false);
bool fTestNet = gArgs.GetBoolArg("-testnet", false);
if (fTestNet && fRegTest)
throw std::runtime_error("Invalid combination of -regtest and -testnet.");
if (fRegTest)
return CBaseChainParams::REGTEST;
if (fTestNet)
return CBaseChainParams::TESTNET;
return CBaseChainParams::MAIN;
}

@ -54,10 +54,4 @@ 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(const std::string& chain); void SelectBaseParams(const std::string& chain);
/**
* 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.
*/
std::string ChainNameFromCommandLine();
#endif // BITCOIN_CHAINPARAMSBASE_H #endif // BITCOIN_CHAINPARAMSBASE_H

@ -630,7 +630,7 @@ int main(int argc, char *argv[])
// Check for -testnet or -regtest parameter (Params() calls are only valid after this clause) // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
try { try {
node->selectParams(ChainNameFromCommandLine()); node->selectParams(gArgs.GetChainName());
} catch(std::exception &e) { } catch(std::exception &e) {
QMessageBox::critical(0, QObject::tr(PACKAGE_NAME), QObject::tr("Error: %1").arg(e.what())); QMessageBox::critical(0, QObject::tr(PACKAGE_NAME), QObject::tr("Error: %1").arg(e.what()));
return EXIT_FAILURE; return EXIT_FAILURE;

@ -599,7 +599,7 @@ TableViewLastColumnResizingFixer::TableViewLastColumnResizingFixer(QTableView* t
#ifdef WIN32 #ifdef WIN32
fs::path static StartupShortcutPath() fs::path static StartupShortcutPath()
{ {
std::string chain = ChainNameFromCommandLine(); std::string chain = gArgs.GetChainName();
if (chain == CBaseChainParams::MAIN) if (chain == CBaseChainParams::MAIN)
return GetSpecialFolderPath(CSIDL_STARTUP) / "Bitcoin.lnk"; return GetSpecialFolderPath(CSIDL_STARTUP) / "Bitcoin.lnk";
if (chain == CBaseChainParams::TESTNET) // Remove this special case when CBaseChainParams::TESTNET = "testnet4" if (chain == CBaseChainParams::TESTNET) // Remove this special case when CBaseChainParams::TESTNET = "testnet4"
@ -697,7 +697,7 @@ fs::path static GetAutostartDir()
fs::path static GetAutostartFilePath() fs::path static GetAutostartFilePath()
{ {
std::string chain = ChainNameFromCommandLine(); std::string chain = gArgs.GetChainName();
if (chain == CBaseChainParams::MAIN) if (chain == CBaseChainParams::MAIN)
return GetAutostartDir() / "bitcoin.desktop"; return GetAutostartDir() / "bitcoin.desktop";
return GetAutostartDir() / strprintf("bitcoin-%s.lnk", chain); return GetAutostartDir() / strprintf("bitcoin-%s.lnk", chain);
@ -739,7 +739,7 @@ bool SetStartOnSystemStartup(bool fAutoStart)
fs::ofstream optionFile(GetAutostartFilePath(), std::ios_base::out|std::ios_base::trunc); fs::ofstream optionFile(GetAutostartFilePath(), std::ios_base::out|std::ios_base::trunc);
if (!optionFile.good()) if (!optionFile.good())
return false; return false;
std::string chain = ChainNameFromCommandLine(); std::string chain = gArgs.GetChainName();
// Write a bitcoin.desktop file to the autostart directory: // Write a bitcoin.desktop file to the autostart directory:
optionFile << "[Desktop Entry]\n"; optionFile << "[Desktop Entry]\n";
optionFile << "Type=Application\n"; optionFile << "Type=Application\n";

@ -764,6 +764,20 @@ void ArgsManager::ReadConfigFile(const std::string& confPath)
} }
} }
std::string ArgsManager::GetChainName() const
{
bool fRegTest = GetBoolArg("-regtest", false);
bool fTestNet = GetBoolArg("-testnet", false);
if (fTestNet && fRegTest)
throw std::runtime_error("Invalid combination of -regtest and -testnet.");
if (fRegTest)
return CBaseChainParams::REGTEST;
if (fTestNet)
return CBaseChainParams::TESTNET;
return CBaseChainParams::MAIN;
}
#ifndef WIN32 #ifndef WIN32
fs::path GetPidFile() fs::path GetPidFile()
{ {

@ -306,6 +306,12 @@ public:
// been set. Also called directly in testing. // been set. Also called directly in testing.
void ForceSetArg(const std::string& strArg, const std::string& strValue); void ForceSetArg(const std::string& strArg, const std::string& strValue);
/**
* Looks for -regtest, -testnet and returns the appropriate BIP70 chain name.
* @return CBaseChainParams::MAIN by default; raises runtime error if an invalid combination is given.
*/
std::string GetChainName() const;
private: private:
// Munge -nofoo into -foo=0 and track the value as negated. // Munge -nofoo into -foo=0 and track the value as negated.

Loading…
Cancel
Save