Skip to content

Commit d1fc4d9

Browse files
committed
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.
1 parent 8a9817d commit d1fc4d9

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/util.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,13 @@ class ArgsManagerHelper {
460460
public:
461461
typedef std::map<std::string, std::vector<std::string>> MapArgs;
462462

463+
/** Determine whether to use config settings in the default section,
464+
* See also comments around ArgsManager::ArgsManager() below. */
465+
static inline bool UseDefaultSection(const ArgsManager& am, const std::string& arg)
466+
{
467+
return (am.m_network == CBaseChainParams::MAIN || am.m_network_only_args.count(arg) == 0);
468+
}
469+
463470
/** Convert regular argument into the network-specific setting */
464471
static inline std::string NetworkArg(const ArgsManager& am, const std::string& arg)
465472
{
@@ -521,9 +528,11 @@ class ArgsManagerHelper {
521528
}
522529
}
523530

524-
found_result = GetArgHelper(am.m_config_args, arg);
525-
if (found_result.first) {
526-
return found_result;
531+
if (UseDefaultSection(am, arg)) {
532+
found_result = GetArgHelper(am.m_config_args, arg);
533+
if (found_result.first) {
534+
return found_result;
535+
}
527536
}
528537

529538
return found_result;
@@ -575,6 +584,22 @@ static bool InterpretNegatedOption(std::string& key, std::string& val)
575584
return false;
576585
}
577586

587+
ArgsManager::ArgsManager() :
588+
/* These options would cause cross-contamination if values for
589+
* mainnet were used while running on regtest/testnet (or vice-versa).
590+
* Setting them as section_only_args ensures that sharing a config file
591+
* between mainnet and regtest/testnet won't cause problems due to these
592+
* parameters by accident. */
593+
m_network_only_args{
594+
"-addnode", "-connect",
595+
"-port", "-bind",
596+
"-rpcport", "-rpcbind",
597+
"-wallet",
598+
}
599+
{
600+
// nothing to do
601+
}
602+
578603
void ArgsManager::SelectConfigNetwork(const std::string& network)
579604
{
580605
m_network = network;
@@ -621,11 +646,16 @@ std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const
621646
if (IsArgNegated(strArg)) return result; // special case
622647

623648
LOCK(cs_args);
649+
624650
ArgsManagerHelper::AddArgs(result, m_override_args, strArg);
625651
if (!m_network.empty()) {
626652
ArgsManagerHelper::AddArgs(result, m_config_args, ArgsManagerHelper::NetworkArg(*this, strArg));
627653
}
628-
ArgsManagerHelper::AddArgs(result, m_config_args, strArg);
654+
655+
if (ArgsManagerHelper::UseDefaultSection(*this, strArg)) {
656+
ArgsManagerHelper::AddArgs(result, m_config_args, strArg);
657+
}
658+
629659
return result;
630660
}
631661

src/util.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <exception>
2525
#include <map>
2626
#include <memory>
27+
#include <set>
2728
#include <stdint.h>
2829
#include <string>
2930
#include <unordered_set>
@@ -229,9 +230,13 @@ class ArgsManager
229230
std::map<std::string, std::vector<std::string>> m_override_args;
230231
std::map<std::string, std::vector<std::string>> m_config_args;
231232
std::string m_network;
233+
std::set<std::string> m_network_only_args;
234+
232235
void ReadConfigStream(std::istream& stream);
233236

234237
public:
238+
ArgsManager();
239+
235240
/**
236241
* Select the network in use
237242
*/

0 commit comments

Comments
 (0)