Skip to content

Commit

Permalink
ArgsManager: limit some options to only apply on mainnet when in default
Browse files Browse the repository at this point in the history
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 authored and random-zebra committed Apr 19, 2021
1 parent ef505ea commit 1bddffe
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
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,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 @@ -188,9 +195,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 @@ -242,6 +251,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 @@ -288,11 +313,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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <atomic>
#include <exception>
#include <map>
#include <memory>
#include <set>
#include <stdint.h>
#include <string>
#include <unordered_set>
Expand Down Expand Up @@ -126,10 +128,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 1bddffe

Please sign in to comment.