diff --git a/src/bench/bench_bitcoin.cpp b/src/bench/bench_bitcoin.cpp --- a/src/bench/bench_bitcoin.cpp +++ b/src/bench/bench_bitcoin.cpp @@ -109,6 +109,10 @@ printer.reset(new benchmark::JunitPrinter()); } + // gArgs no longer needed. Clear it here to avoid interactions with the + // testing setup in the benches + gArgs.ClearArgs(); + benchmark::BenchRunner::RunAll(*printer, evaluations, scaling_factor, regex_filter, is_list_only); diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -62,7 +62,7 @@ // // If Qt is used, parameters/bitcoin.conf are parsed in qt/bitcoin.cpp's // main() - SetupServerArgs(); + SetupServerArgs(node); std::string error; if (!gArgs.ParseParameters(argc, argv, error)) { return InitError(Untranslated( diff --git a/src/init.h b/src/init.h --- a/src/init.h +++ b/src/init.h @@ -76,9 +76,9 @@ NodeContext &node); /** - * Setup the arguments for gArgs. + * Register all arguments with the ArgsManager */ -void SetupServerArgs(); +void SetupServerArgs(NodeContext &node); /** Returns licensing information (for -version) */ std::string LicenseInfo(); diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -316,6 +316,7 @@ GetMainSignals().UnregisterBackgroundSignalScheduler(); globalVerifyHandle.reset(); ECC_Stop(); + node.args = nullptr; node.mempool = nullptr; node.chainman = nullptr; node.scheduler.reset(); @@ -366,7 +367,10 @@ LogPrint(BCLog::RPC, "RPC stopped.\n"); } -void SetupServerArgs() { +void SetupServerArgs(NodeContext &node) { + assert(!node.args); + node.args = &gArgs; + SetupHelpOptions(gArgs); // server-only for now gArgs.AddArg("-help-debug", diff --git a/src/interfaces/node.cpp b/src/interfaces/node.cpp --- a/src/interfaces/node.cpp +++ b/src/interfaces/node.cpp @@ -128,9 +128,7 @@ StopMapPort(); } } - // PR18571: return SetupServerArgs(*m_context) - // See https://reviews.bitcoinabc.org/D7993 - void setupServerArgs() override { return SetupServerArgs(); } + void setupServerArgs() override { return SetupServerArgs(*m_context); } bool getProxy(Network net, proxyType &proxy_info) override { return GetProxy(net, proxy_info); } diff --git a/src/node/context.h b/src/node/context.h --- a/src/node/context.h +++ b/src/node/context.h @@ -9,6 +9,7 @@ #include #include +class ArgsManager; class BanMan; class CConnman; class CScheduler; @@ -38,6 +39,8 @@ // Currently a raw pointer because the memory is not managed by this struct ChainstateManager *chainman{nullptr}; std::unique_ptr banman; + // Currently a raw pointer because the memory is not managed by this struct + ArgsManager *args{nullptr}; std::unique_ptr chain; std::vector> chain_clients; std::unique_ptr scheduler; diff --git a/src/test/fixture.cpp b/src/test/fixture.cpp --- a/src/test/fixture.cpp +++ b/src/test/fixture.cpp @@ -6,6 +6,8 @@ #include +#include + #include namespace utf = boost::unit_test::framework; @@ -15,29 +17,24 @@ * test case. */ struct CustomArgumentsFixture { - std::string error; - CustomArgumentsFixture() { - const std::string testsuitename = "-testsuitename"; + auto &master_test_suite = utf::master_test_suite(); - const std::set testArgs = { - testsuitename, - "-axionactivationtime", - }; + for (int i = 1; i < master_test_suite.argc; i++) { + std::string key(master_test_suite.argv[i]); + std::string val; - for (const auto &arg : testArgs) { - gArgs.AddArg(arg, "", ArgsManager::ALLOW_ANY, - OptionsCategory::HIDDEN); - } + if (!ParseKeyValue(key, val)) { + break; + } - auto &master_test_suite = utf::master_test_suite(); - if (!gArgs.ParseParameters(master_test_suite.argc, - master_test_suite.argv, error)) { - throw utf::setup_error(error); - } + if (key == "-testsuitename") { + master_test_suite.p_name.value = val; + continue; + } - master_test_suite.p_name.value = - gArgs.GetArg(testsuitename, master_test_suite.p_name.value); + fixture_extra_args.push_back(master_test_suite.argv[i]); + } } ~CustomArgumentsFixture(){}; diff --git a/src/test/fuzz/process_message.cpp b/src/test/fuzz/process_message.cpp --- a/src/test/fuzz/process_message.cpp +++ b/src/test/fuzz/process_message.cpp @@ -68,11 +68,16 @@ {"block", "blocktxn", "cmpctblock", "tx"}}, }; -const RegTestingSetup *g_setup; +const TestingSetup *g_setup; } // namespace void initialize() { - static RegTestingSetup setup{}; + static TestingSetup setup{ + CBaseChainParams::REGTEST, + { + "-nodebuglogfile", + }, + }; g_setup = &setup; for (int i = 0; i < 2 * COINBASE_MATURITY; i++) { diff --git a/src/test/fuzz/process_messages.cpp b/src/test/fuzz/process_messages.cpp --- a/src/test/fuzz/process_messages.cpp +++ b/src/test/fuzz/process_messages.cpp @@ -17,10 +17,15 @@ #include #include -const RegTestingSetup *g_setup; +const TestingSetup *g_setup; void initialize() { - static RegTestingSetup setup{}; + static TestingSetup setup{ + CBaseChainParams::REGTEST, + { + "-nodebuglogfile", + }, + }; g_setup = &setup; for (int i = 0; i < 2 * COINBASE_MATURITY; i++) { diff --git a/src/test/util/setup_common.h b/src/test/util/setup_common.h --- a/src/test/util/setup_common.h +++ b/src/test/util/setup_common.h @@ -92,6 +92,8 @@ static constexpr Amount CENT(COIN / 100); +extern std::vector fixture_extra_args; + /** * Basic testing setup. * This just configures logging, data dir and chain parameters. @@ -101,7 +103,8 @@ NodeContext m_node; explicit BasicTestingSetup( - const std::string &chainName = CBaseChainParams::MAIN); + const std::string &chainName = CBaseChainParams::MAIN, + const std::vector &extra_args = {}); ~BasicTestingSetup(); private: @@ -115,8 +118,8 @@ struct TestingSetup : public BasicTestingSetup { boost::thread_group threadGroup; - explicit TestingSetup( - const std::string &chainName = CBaseChainParams::MAIN); + explicit TestingSetup(const std::string &chainName = CBaseChainParams::MAIN, + const std::vector &extra_args = {}); ~TestingSetup(); }; diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -83,17 +84,40 @@ return os; } -BasicTestingSetup::BasicTestingSetup(const std::string &chainName) +std::vector fixture_extra_args{}; + +BasicTestingSetup::BasicTestingSetup( + const std::string &chainName, const std::vector &extra_args) : m_path_root{fs::temp_directory_path() / "test_common_" PACKAGE_NAME / g_insecure_rand_ctx_temp_path.rand256().ToString()} { + std::vector arguments = Cat( + { + "dummy", + "-printtoconsole=0", + "-logtimemicros", + "-debug", + "-debugexclude=libevent", + "-debugexclude=leveldb", + }, + extra_args); + arguments = Cat(arguments, fixture_extra_args); + auto &config = const_cast(GetConfig()); SetMockTime(0); fs::create_directories(m_path_root); gArgs.ForceSetArg("-datadir", m_path_root.string()); ClearDatadirCache(); + { + SetupServerArgs(m_node); + std::string error; + const bool success{m_node.args->ParseParameters( + arguments.size(), arguments.data(), error)}; + assert(success); + assert(error.empty()); + } SelectParams(chainName); SeedInsecureRand(); - gArgs.ForceSetArg("-printtoconsole", "0"); InitLogging(); + AppInitParameterInteraction(config); LogInstance().StartLogging(); SHA256AutoDetect(); ECC_Start(); @@ -102,7 +126,7 @@ InitSignatureCache(); InitScriptExecutionCache(); - m_node.chain = interfaces::MakeChain(m_node, Params()); + m_node.chain = interfaces::MakeChain(m_node, config.GetChainParams()); g_wallet_init_interface.Construct(m_node); fCheckBlockIndex = true; @@ -116,11 +140,13 @@ BasicTestingSetup::~BasicTestingSetup() { LogInstance().DisconnectTestLogger(); fs::remove_all(m_path_root); + gArgs.ClearArgs(); ECC_Stop(); } -TestingSetup::TestingSetup(const std::string &chainName) - : BasicTestingSetup(chainName) { +TestingSetup::TestingSetup(const std::string &chainName, + const std::vector &extra_args) + : BasicTestingSetup(chainName, extra_args) { const Config &config = GetConfig(); const CChainParams &chainparams = config.GetChainParams(); @@ -198,6 +224,7 @@ GetMainSignals().UnregisterBackgroundSignalScheduler(); m_node.connman.reset(); m_node.banman.reset(); + m_node.args = nullptr; m_node.mempool = nullptr; m_node.scheduler.reset(); UnloadBlockIndex();