Changeset View
Changeset View
Standalone View
Standalone View
src/util.cpp
Show First 20 Lines • Show All 167 Lines • ▼ Show 20 Lines | static bool InterpretBool(const std::string &strValue) { | ||||
return (atoi(strValue) != 0); | return (atoi(strValue) != 0); | ||||
} | } | ||||
/** 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, | |||||
* See also comments around ArgsManager::ArgsManager() below. */ | |||||
static inline bool UseDefaultSection(const ArgsManager &am, | |||||
const std::string &arg) { | |||||
return (am.m_network == CBaseChainParams::MAIN || | |||||
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] == '-'); | ||||
return "-" + am.m_network + "." + arg.substr(1); | return "-" + am.m_network + "." + arg.substr(1); | ||||
} | } | ||||
/** Find arguments in a map and add them to a vector */ | /** Find arguments in a map and add them to a vector */ | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | static inline std::pair<bool, std::string> GetArg(const ArgsManager &am, | ||||
// GetArg(am,"foo")={true,"bar"} | // GetArg(am,"foo")={true,"bar"} | ||||
if (!am.m_network.empty()) { | if (!am.m_network.empty()) { | ||||
found_result = GetArgHelper(am.m_config_args, NetworkArg(am, arg)); | found_result = GetArgHelper(am.m_config_args, NetworkArg(am, arg)); | ||||
if (found_result.first) { | if (found_result.first) { | ||||
return found_result; | return found_result; | ||||
} | } | ||||
} | } | ||||
if (UseDefaultSection(am, arg)) { | |||||
found_result = GetArgHelper(am.m_config_args, arg); | found_result = GetArgHelper(am.m_config_args, arg); | ||||
if (found_result.first) { | if (found_result.first) { | ||||
return found_result; | return found_result; | ||||
} | } | ||||
} | |||||
return found_result; | return found_result; | ||||
} | } | ||||
}; | }; | ||||
/** | /** | ||||
* Interpret -nofoo as if the user supplied -foo=0. | * Interpret -nofoo as if the user supplied -foo=0. | ||||
* | * | ||||
Show All 34 Lines | if (key.substr(option_index, 2) == "no") { | ||||
val = "1"; | val = "1"; | ||||
} else { | } else { | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
ArgsManager::ArgsManager() | |||||
: /* These options would cause cross-contamination if values for mainnet | |||||
* were used while running on regtest/testnet (or vice-versa). | |||||
* Setting them as section_only_args ensures that sharing a config file | |||||
* between mainnet and regtest/testnet won't cause problems due to these | |||||
* parameters by accident. */ | |||||
m_network_only_args{ | |||||
"-addnode", "-connect", "-port", "-bind", | |||||
"-rpcport", "-rpcbind", "-wallet", | |||||
} { | |||||
// nothing to do | |||||
} | |||||
void ArgsManager::WarnForSectionOnlyArgs() { | |||||
// if there's no section selected, don't worry | |||||
if (m_network.empty()) { | |||||
return; | |||||
} | |||||
// if it's okay to use the default section for this network, don't worry | |||||
if (m_network == CBaseChainParams::MAIN) { | |||||
return; | |||||
} | |||||
for (const auto &arg : m_network_only_args) { | |||||
std::pair<bool, std::string> found_result; | |||||
// if this option is overridden it's fine | |||||
found_result = ArgsManagerHelper::GetArgHelper(m_override_args, arg); | |||||
if (found_result.first) { | |||||
continue; | |||||
} | |||||
// if there's a network-specific value for this option, it's fine | |||||
found_result = ArgsManagerHelper::GetArgHelper( | |||||
m_config_args, ArgsManagerHelper::NetworkArg(*this, arg)); | |||||
if (found_result.first) { | |||||
continue; | |||||
} | |||||
// if there isn't a default value for this option, it's fine | |||||
found_result = ArgsManagerHelper::GetArgHelper(m_config_args, arg); | |||||
if (!found_result.first) { | |||||
continue; | |||||
} | |||||
// otherwise, issue a warning | |||||
LogPrintf("Warning: Config setting for %s only applied on %s network " | |||||
"when in [%s] section.\n", | |||||
arg, m_network, m_network); | |||||
} | |||||
} | |||||
void ArgsManager::SelectConfigNetwork(const std::string &network) { | void ArgsManager::SelectConfigNetwork(const std::string &network) { | ||||
m_network = network; | m_network = network; | ||||
} | } | ||||
void ArgsManager::ParseParameters(int argc, const char *const argv[]) { | void ArgsManager::ParseParameters(int argc, const char *const argv[]) { | ||||
LOCK(cs_args); | LOCK(cs_args); | ||||
m_override_args.clear(); | m_override_args.clear(); | ||||
Show All 33 Lines | |||||
std::vector<std::string> ArgsManager::GetArgs(const std::string &strArg) const { | std::vector<std::string> ArgsManager::GetArgs(const std::string &strArg) const { | ||||
std::vector<std::string> result = {}; | std::vector<std::string> result = {}; | ||||
// special case | // special case | ||||
if (IsArgNegated(strArg)) { | if (IsArgNegated(strArg)) { | ||||
return result; | return result; | ||||
} | } | ||||
LOCK(cs_args); | LOCK(cs_args); | ||||
ArgsManagerHelper::AddArgs(result, m_override_args, strArg); | ArgsManagerHelper::AddArgs(result, m_override_args, strArg); | ||||
if (!m_network.empty()) { | if (!m_network.empty()) { | ||||
ArgsManagerHelper::AddArgs( | ArgsManagerHelper::AddArgs( | ||||
result, m_config_args, | result, m_config_args, | ||||
ArgsManagerHelper::NetworkArg(*this, strArg)); | ArgsManagerHelper::NetworkArg(*this, strArg)); | ||||
} | } | ||||
if (ArgsManagerHelper::UseDefaultSection(*this, strArg)) { | |||||
ArgsManagerHelper::AddArgs(result, m_config_args, strArg); | ArgsManagerHelper::AddArgs(result, m_config_args, strArg); | ||||
} | |||||
return result; | return result; | ||||
} | } | ||||
bool ArgsManager::IsArgSet(const std::string &strArg) const { | bool ArgsManager::IsArgSet(const std::string &strArg) const { | ||||
// special case | // special case | ||||
if (IsArgNegated(strArg)) { | if (IsArgNegated(strArg)) { | ||||
return true; | return true; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 530 Lines • Show Last 20 Lines |