ArgsManager: limit some options to only apply on mainnet when in default section

When specified in bitcoin.conf without using the [regtest] or [test]
section header, or a "regtest." or "test." prefix, the "addnode",
"connect", "port", "bind", "rpcport", "rpcbind", and "wallet" settings
will only be applied when running on mainnet.
This commit is contained in:
Anthony Towns 2018-04-04 18:06:00 +10:00
parent 8a9817d175
commit d1fc4d95af
2 changed files with 39 additions and 4 deletions

View file

@ -460,6 +460,13 @@ 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, const std::string& arg) static inline std::string NetworkArg(const ArgsManager& am, const std::string& arg)
{ {
@ -521,9 +528,11 @@ public:
} }
} }
found_result = GetArgHelper(am.m_config_args, arg); if (UseDefaultSection(am, arg)) {
if (found_result.first) { found_result = GetArgHelper(am.m_config_args, arg);
return found_result; if (found_result.first) {
return found_result;
}
} }
return found_result; return found_result;
@ -575,6 +584,22 @@ static bool InterpretNegatedOption(std::string& key, std::string& val)
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::SelectConfigNetwork(const std::string& network) void ArgsManager::SelectConfigNetwork(const std::string& network)
{ {
m_network = network; m_network = network;
@ -621,11 +646,16 @@ std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const
if (IsArgNegated(strArg)) return result; // special case if (IsArgNegated(strArg)) return result; // special case
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(result, m_config_args, ArgsManagerHelper::NetworkArg(*this, strArg)); ArgsManagerHelper::AddArgs(result, m_config_args, ArgsManagerHelper::NetworkArg(*this, strArg));
} }
ArgsManagerHelper::AddArgs(result, m_config_args, strArg);
if (ArgsManagerHelper::UseDefaultSection(*this, strArg)) {
ArgsManagerHelper::AddArgs(result, m_config_args, strArg);
}
return result; return result;
} }

View file

@ -24,6 +24,7 @@
#include <exception> #include <exception>
#include <map> #include <map>
#include <memory> #include <memory>
#include <set>
#include <stdint.h> #include <stdint.h>
#include <string> #include <string>
#include <unordered_set> #include <unordered_set>
@ -229,9 +230,13 @@ protected:
std::map<std::string, std::vector<std::string>> m_override_args; std::map<std::string, std::vector<std::string>> m_override_args;
std::map<std::string, std::vector<std::string>> m_config_args; std::map<std::string, std::vector<std::string>> m_config_args;
std::string m_network; std::string m_network;
std::set<std::string> m_network_only_args;
void ReadConfigStream(std::istream& stream); void ReadConfigStream(std::istream& stream);
public: public:
ArgsManager();
/** /**
* Select the network in use * Select the network in use
*/ */