Changeset View
Changeset View
Standalone View
Standalone View
src/util/system.cpp
| Show First 20 Lines • Show All 189 Lines • ▼ Show 20 Lines | |||||
| /** Internal helper functions for ArgsManager */ | /** Internal helper functions for ArgsManager */ | ||||
| class ArgsManagerHelper { | class ArgsManagerHelper { | ||||
| public: | public: | ||||
| typedef std::map<std::string, std::vector<std::string>> MapArgs; | typedef std::map<std::string, std::vector<std::string>> MapArgs; | ||||
| /** Determine whether to use config settings in the default section, | /** Determine whether to use config settings in the default section, | ||||
| * See also comments around ArgsManager::ArgsManager() below. */ | * See also comments around ArgsManager::ArgsManager() below. */ | ||||
| static inline bool UseDefaultSection(const ArgsManager &am, | static inline bool UseDefaultSection(const ArgsManager &am, | ||||
| const std::string &arg) { | const std::string &arg) | ||||
| EXCLUSIVE_LOCKS_REQUIRED(am.cs_args) { | |||||
| return (am.m_network == CBaseChainParams::MAIN || | return (am.m_network == CBaseChainParams::MAIN || | ||||
| am.m_network_only_args.count(arg) == 0); | am.m_network_only_args.count(arg) == 0); | ||||
| } | } | ||||
| /** Convert regular argument into the network-specific setting */ | /** Convert regular argument into the network-specific setting */ | ||||
| static inline std::string NetworkArg(const ArgsManager &am, | static inline std::string NetworkArg(const ArgsManager &am, | ||||
| const std::string &arg) { | const std::string &arg) { | ||||
| assert(arg.length() > 1 && arg[0] == '-'); | assert(arg.length() > 1 && arg[0] == '-'); | ||||
| ▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | static inline std::pair<bool, std::string> GetArg(const ArgsManager &am, | ||||
| return found_result; | return found_result; | ||||
| } | } | ||||
| /* Special test for -testnet and -regtest args, because we don't want to be | /* Special test for -testnet and -regtest args, because we don't want to be | ||||
| * confused by craziness like "[regtest] testnet=1" | * confused by craziness like "[regtest] testnet=1" | ||||
| */ | */ | ||||
| static inline bool GetNetBoolArg(const ArgsManager &am, | static inline bool GetNetBoolArg(const ArgsManager &am, | ||||
| const std::string &net_arg) { | const std::string &net_arg) | ||||
| EXCLUSIVE_LOCKS_REQUIRED(am.cs_args) { | |||||
| std::pair<bool, std::string> found_result(false, std::string()); | std::pair<bool, std::string> found_result(false, std::string()); | ||||
| found_result = GetArgHelper(am.m_override_args, net_arg, true); | found_result = GetArgHelper(am.m_override_args, net_arg, true); | ||||
| if (!found_result.first) { | if (!found_result.first) { | ||||
| found_result = GetArgHelper(am.m_config_args, net_arg, true); | found_result = GetArgHelper(am.m_config_args, net_arg, true); | ||||
| if (!found_result.first) { | if (!found_result.first) { | ||||
| // not set | // not set | ||||
| return false; | return false; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | : /* These options would cause cross-contamination if values for mainnet | ||||
| m_network_only_args{ | m_network_only_args{ | ||||
| "-addnode", "-connect", "-port", "-bind", | "-addnode", "-connect", "-port", "-bind", | ||||
| "-rpcport", "-rpcbind", "-wallet", | "-rpcport", "-rpcbind", "-wallet", | ||||
| } { | } { | ||||
| // nothing to do | // nothing to do | ||||
| } | } | ||||
| void ArgsManager::WarnForSectionOnlyArgs() { | void ArgsManager::WarnForSectionOnlyArgs() { | ||||
| LOCK(cs_args); | |||||
| // if there's no section selected, don't worry | // if there's no section selected, don't worry | ||||
| if (m_network.empty()) { | if (m_network.empty()) { | ||||
| return; | return; | ||||
| } | } | ||||
| // if it's okay to use the default section for this network, don't worry | // if it's okay to use the default section for this network, don't worry | ||||
| if (m_network == CBaseChainParams::MAIN) { | if (m_network == CBaseChainParams::MAIN) { | ||||
| return; | return; | ||||
| Show All 24 Lines | for (const auto &arg : m_network_only_args) { | ||||
| // otherwise, issue a warning | // otherwise, issue a warning | ||||
| LogPrintf("Warning: Config setting for %s only applied on %s network " | LogPrintf("Warning: Config setting for %s only applied on %s network " | ||||
| "when in [%s] section.\n", | "when in [%s] section.\n", | ||||
| arg, m_network, m_network); | arg, m_network, m_network); | ||||
| } | } | ||||
| } | } | ||||
| void ArgsManager::SelectConfigNetwork(const std::string &network) { | void ArgsManager::SelectConfigNetwork(const std::string &network) { | ||||
| LOCK(cs_args); | |||||
| m_network = network; | m_network = network; | ||||
| } | } | ||||
| bool ParseKeyValue(std::string &key, std::string &val) { | bool ParseKeyValue(std::string &key, std::string &val) { | ||||
| size_t is_index = key.find('='); | size_t is_index = key.find('='); | ||||
| if (is_index != std::string::npos) { | if (is_index != std::string::npos) { | ||||
| val = key.substr(is_index + 1); | val = key.substr(is_index + 1); | ||||
| key.erase(is_index); | key.erase(is_index); | ||||
| ▲ Show 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | bool ArgsManager::IsArgKnown(const std::string &key) const { | ||||
| std::string arg_no_net; | std::string arg_no_net; | ||||
| if (option_index == std::string::npos) { | if (option_index == std::string::npos) { | ||||
| arg_no_net = key; | arg_no_net = key; | ||||
| } else { | } else { | ||||
| arg_no_net = | arg_no_net = | ||||
| std::string("-") + key.substr(option_index + 1, std::string::npos); | std::string("-") + key.substr(option_index + 1, std::string::npos); | ||||
| } | } | ||||
| LOCK(cs_args); | |||||
| for (const auto &arg_map : m_available_args) { | for (const auto &arg_map : m_available_args) { | ||||
| if (arg_map.second.count(arg_no_net)) { | if (arg_map.second.count(arg_no_net)) { | ||||
| return true; | return true; | ||||
| } | } | ||||
| } | } | ||||
| return false; | return false; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 127 Lines • ▼ Show 20 Lines | |||||
| void ArgsManager::AddArg(const std::string &name, const std::string &help, | void ArgsManager::AddArg(const std::string &name, const std::string &help, | ||||
| const bool debug_only, const OptionsCategory &cat) { | const bool debug_only, const OptionsCategory &cat) { | ||||
| // Split arg name from its help param | // Split arg name from its help param | ||||
| size_t eq_index = name.find('='); | size_t eq_index = name.find('='); | ||||
| if (eq_index == std::string::npos) { | if (eq_index == std::string::npos) { | ||||
| eq_index = name.size(); | eq_index = name.size(); | ||||
| } | } | ||||
| LOCK(cs_args); | |||||
| std::map<std::string, Arg> &arg_map = m_available_args[cat]; | std::map<std::string, Arg> &arg_map = m_available_args[cat]; | ||||
| auto ret = arg_map.emplace( | auto ret = arg_map.emplace( | ||||
| name.substr(0, eq_index), | name.substr(0, eq_index), | ||||
| Arg(name.substr(eq_index, name.size() - eq_index), help, debug_only)); | Arg(name.substr(eq_index, name.size() - eq_index), help, debug_only)); | ||||
| // Make sure an insertion actually happened. | // Make sure an insertion actually happened. | ||||
| assert(ret.second); | assert(ret.second); | ||||
| } | } | ||||
| void ArgsManager::ClearArg(const std::string &strArg) { | void ArgsManager::ClearArg(const std::string &strArg) { | ||||
| LOCK(cs_args); | LOCK(cs_args); | ||||
| m_override_args.erase(strArg); | m_override_args.erase(strArg); | ||||
| m_config_args.erase(strArg); | m_config_args.erase(strArg); | ||||
| } | } | ||||
| std::string ArgsManager::GetHelpMessage() const { | std::string ArgsManager::GetHelpMessage() const { | ||||
| const bool show_debug = gArgs.GetBoolArg("-help-debug", false); | const bool show_debug = gArgs.GetBoolArg("-help-debug", false); | ||||
| std::string usage = ""; | std::string usage = ""; | ||||
| LOCK(cs_args); | |||||
| for (const auto &arg_map : m_available_args) { | for (const auto &arg_map : m_available_args) { | ||||
| switch (arg_map.first) { | switch (arg_map.first) { | ||||
| case OptionsCategory::OPTIONS: | case OptionsCategory::OPTIONS: | ||||
| usage += HelpMessageGroup("Options:"); | usage += HelpMessageGroup("Options:"); | ||||
| break; | break; | ||||
| case OptionsCategory::CONNECTION: | case OptionsCategory::CONNECTION: | ||||
| usage += HelpMessageGroup("Connection options:"); | usage += HelpMessageGroup("Connection options:"); | ||||
| break; | break; | ||||
| ▲ Show 20 Lines • Show All 287 Lines • ▼ Show 20 Lines | if (stream.good()) { | ||||
| } | } | ||||
| // if there is an -includeconf in the override args, but it is empty, | // if there is an -includeconf in the override args, but it is empty, | ||||
| // that means the user passed '-noincludeconf' on the command line, in | // that means the user passed '-noincludeconf' on the command line, in | ||||
| // which case we should not include anything | // which case we should not include anything | ||||
| bool emptyIncludeConf; | bool emptyIncludeConf; | ||||
| { | { | ||||
| LOCK(cs_args); | LOCK(cs_args); | ||||
| emptyIncludeConf = m_override_args.count("-includeconf") == 0; | emptyIncludeConf = m_override_args.count("-includeconf") == 0; | ||||
| } | } | ||||
jasonbcox: https://reviews.bitcoinabc.org/D3035#inline-18705
@markblundeberg
@Fabien
FYI | |||||
| if (emptyIncludeConf) { | if (emptyIncludeConf) { | ||||
| std::string chain_id = GetChainName(); | std::string chain_id = GetChainName(); | ||||
| std::vector<std::string> includeconf(GetArgs("-includeconf")); | std::vector<std::string> includeconf(GetArgs("-includeconf")); | ||||
| { | { | ||||
| // We haven't set m_network yet (that happens in | // We haven't set m_network yet (that happens in | ||||
| // SelectParams()), so manually check for network.includeconf | // SelectParams()), so manually check for network.includeconf | ||||
| // args. | // args. | ||||
| std::vector<std::string> includeconf_net( | std::vector<std::string> includeconf_net( | ||||
| ▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | if (!fs::is_directory(GetDataDir(false))) { | ||||
| error = strprintf("specified data directory \"%s\" does not exist.", | error = strprintf("specified data directory \"%s\" does not exist.", | ||||
| gArgs.GetArg("-datadir", "").c_str()); | gArgs.GetArg("-datadir", "").c_str()); | ||||
| return false; | return false; | ||||
| } | } | ||||
| return true; | return true; | ||||
| } | } | ||||
| std::string ArgsManager::GetChainName() const { | std::string ArgsManager::GetChainName() const { | ||||
| LOCK(cs_args); | |||||
| bool fRegTest = ArgsManagerHelper::GetNetBoolArg(*this, "-regtest"); | bool fRegTest = ArgsManagerHelper::GetNetBoolArg(*this, "-regtest"); | ||||
| bool fTestNet = ArgsManagerHelper::GetNetBoolArg(*this, "-testnet"); | bool fTestNet = ArgsManagerHelper::GetNetBoolArg(*this, "-testnet"); | ||||
| if (fTestNet && fRegTest) { | if (fTestNet && fRegTest) { | ||||
| throw std::runtime_error( | throw std::runtime_error( | ||||
| "Invalid combination of -regtest and -testnet."); | "Invalid combination of -regtest and -testnet."); | ||||
| } | } | ||||
| if (fRegTest) { | if (fRegTest) { | ||||
| ▲ Show 20 Lines • Show All 325 Lines • Show Last 20 Lines | |||||
https://reviews.bitcoinabc.org/D3035#inline-18705
@markblundeberg
@Fabien
FYI