Skip to content

Commit

Permalink
ArgsManager: limit some options to only apply on mainnet when in defa…
Browse files Browse the repository at this point in the history
…ult 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.
  • Loading branch information
ajtowns committed Apr 11, 2018
1 parent 8a9817d commit d1fc4d9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/util.cpp
Expand Up @@ -460,6 +460,13 @@ class ArgsManagerHelper {
public:
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 */
static inline std::string NetworkArg(const ArgsManager& am, const std::string& arg)
{
Expand Down Expand Up @@ -521,9 +528,11 @@ class ArgsManagerHelper {
}
}

found_result = GetArgHelper(am.m_config_args, arg);
if (found_result.first) {
return found_result;
if (UseDefaultSection(am, arg)) {
found_result = GetArgHelper(am.m_config_args, arg);
if (found_result.first) {
return found_result;
}
}

return found_result;
Expand Down Expand Up @@ -575,6 +584,22 @@ static bool InterpretNegatedOption(std::string& key, std::string& val)
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)
{
m_network = network;
Expand Down Expand Up @@ -621,11 +646,16 @@ std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const
if (IsArgNegated(strArg)) return result; // special case

LOCK(cs_args);

ArgsManagerHelper::AddArgs(result, m_override_args, strArg);
if (!m_network.empty()) {
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;
}

Expand Down
5 changes: 5 additions & 0 deletions src/util.h
Expand Up @@ -24,6 +24,7 @@
#include <exception>
#include <map>
#include <memory>
#include <set>
#include <stdint.h>
#include <string>
#include <unordered_set>
Expand Down Expand Up @@ -229,9 +230,13 @@ class ArgsManager
std::map<std::string, std::vector<std::string>> m_override_args;
std::map<std::string, std::vector<std::string>> m_config_args;
std::string m_network;
std::set<std::string> m_network_only_args;

void ReadConfigStream(std::istream& stream);

public:
ArgsManager();

/**
* Select the network in use
*/
Expand Down

0 comments on commit d1fc4d9

Please sign in to comment.