From fad4fa7e2fb95b7ced9007060ebfd0e8f181f5d8 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Wed, 8 Apr 2020 19:47:56 -0400 Subject: [PATCH 1/3] node: Add args alias for gArgs global --- src/bitcoind.cpp | 2 +- src/init.cpp | 6 +++++- src/init.h | 4 ++-- src/interfaces/node.cpp | 2 +- src/node/context.h | 2 ++ src/test/util/setup_common.cpp | 1 + src/test/util/setup_common.h | 3 ++- 7 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index f26eb45fce..bf444dd0bc 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -53,7 +53,7 @@ static bool AppInit(int argc, char* argv[]) // Parameters // // 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(strprintf("Error parsing command line arguments: %s\n", error)); diff --git a/src/init.cpp b/src/init.cpp index de2db694fd..de32c0ad71 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -297,6 +297,7 @@ void Shutdown(NodeContext& node) GetMainSignals().UnregisterBackgroundSignalScheduler(); globalVerifyHandle.reset(); ECC_Stop(); + node.args = nullptr; if (node.mempool) node.mempool = nullptr; node.scheduler.reset(); @@ -360,8 +361,11 @@ static void OnRPCStopped() LogPrint(BCLog::RPC, "RPC stopped.\n"); } -void SetupServerArgs() +void SetupServerArgs(NodeContext& node) { + assert(!node.args); + node.args = &gArgs; + SetupHelpOptions(gArgs); gArgs.AddArg("-help-debug", "Print help message with debugging options and exit", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST); // server-only for now diff --git a/src/init.h b/src/init.h index f74ae5a47a..0c44035b00 100644 --- a/src/init.h +++ b/src/init.h @@ -54,9 +54,9 @@ bool AppInitLockDataDirectory(); bool AppInitMain(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/interfaces/node.cpp b/src/interfaces/node.cpp index 6e5fdc61b5..5ebbd61584 100644 --- a/src/interfaces/node.cpp +++ b/src/interfaces/node.cpp @@ -97,7 +97,7 @@ class NodeImpl : public Node StopMapPort(); } } - 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); } size_t getNodeCount(CConnman::NumConnections flags) override { diff --git a/src/node/context.h b/src/node/context.h index 1c592b456b..7b6dd37361 100644 --- a/src/node/context.h +++ b/src/node/context.h @@ -8,6 +8,7 @@ #include #include +class ArgsManager; class BanMan; class CConnman; class CScheduler; @@ -33,6 +34,7 @@ struct NodeContext { CTxMemPool* mempool{nullptr}; // Currently a raw pointer because the memory is not managed by this struct std::unique_ptr peer_logic; std::unique_ptr banman; + ArgsManager* args{nullptr}; // Currently a raw pointer because the memory is not managed by this struct std::unique_ptr chain; std::vector> chain_clients; std::unique_ptr scheduler; diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index 32d50e49b9..b49e2a56d0 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -159,6 +159,7 @@ TestingSetup::~TestingSetup() g_rpc_node = nullptr; m_node.connman.reset(); m_node.banman.reset(); + m_node.args = nullptr; m_node.mempool = nullptr; m_node.scheduler.reset(); UnloadBlockIndex(); diff --git a/src/test/util/setup_common.h b/src/test/util/setup_common.h index 0930309c3a..41547c8b3a 100644 --- a/src/test/util/setup_common.h +++ b/src/test/util/setup_common.h @@ -73,9 +73,11 @@ static constexpr CAmount CENT{1000000}; */ struct BasicTestingSetup { ECCVerifyHandle globalVerifyHandle; + NodeContext m_node; explicit BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN); ~BasicTestingSetup(); + private: const fs::path m_path_root; }; @@ -84,7 +86,6 @@ struct BasicTestingSetup { * Included are coins database, script check threads setup. */ struct TestingSetup : public BasicTestingSetup { - NodeContext m_node; boost::thread_group threadGroup; explicit TestingSetup(const std::string& chainName = CBaseChainParams::MAIN); From fa0cbd48c418ec14e1d91bffea206bce20bd1e56 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Wed, 8 Apr 2020 20:02:20 -0400 Subject: [PATCH 2/3] test: Add optional extra_args to testing setup --- src/bench/bench_bitcoin.cpp | 2 ++ src/test/util/setup_common.cpp | 26 +++++++++++++++++++++++--- src/test/util/setup_common.h | 4 ++-- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/bench/bench_bitcoin.cpp b/src/bench/bench_bitcoin.cpp index 9235d5fe6a..2d44264e53 100644 --- a/src/bench/bench_bitcoin.cpp +++ b/src/bench/bench_bitcoin.cpp @@ -73,6 +73,8 @@ int main(int argc, char** argv) gArgs.GetArg("-plot-height", DEFAULT_PLOT_HEIGHT))); } + gArgs.ClearArgs(); // gArgs no longer needed. Clear it here to avoid interactions with the testing setup in the benches + benchmark::BenchRunner::RunAll(*printer, evaluations, scaling_factor, regex_filter, is_list_only); return EXIT_SUCCESS; diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index b49e2a56d0..0d455d48b3 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -65,17 +66,34 @@ std::ostream& operator<<(std::ostream& os, const uint256& num) return os; } -BasicTestingSetup::BasicTestingSetup(const std::string& chainName) +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()} { + const std::vector arguments = Cat( + { + "dummy", + "-printtoconsole=0", + "-logtimemicros", + "-debug", + "-debugexclude=libevent", + "-debugexclude=leveldb", + }, + extra_args); 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"); if (G_TEST_LOG_FUN) LogInstance().PushBackCallback(G_TEST_LOG_FUN); InitLogging(); + AppInitParameterInteraction(); LogInstance().StartLogging(); SHA256AutoDetect(); ECC_Start(); @@ -95,10 +113,12 @@ 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 CChainParams& chainparams = Params(); // Ideally we'd move all the RPC tests to the functional testing framework diff --git a/src/test/util/setup_common.h b/src/test/util/setup_common.h index 41547c8b3a..47fcf92ea0 100644 --- a/src/test/util/setup_common.h +++ b/src/test/util/setup_common.h @@ -75,7 +75,7 @@ struct BasicTestingSetup { ECCVerifyHandle globalVerifyHandle; NodeContext m_node; - explicit BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN); + explicit BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN, const std::vector& extra_args = {}); ~BasicTestingSetup(); private: @@ -88,7 +88,7 @@ struct BasicTestingSetup { 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(); }; From fa69f88486e900aacf3fc768671f947927173226 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Wed, 8 Apr 2020 19:48:38 -0400 Subject: [PATCH 3/3] fuzz: Disable debug log file --- src/test/fuzz/process_message.cpp | 9 +++++++-- src/test/fuzz/process_messages.cpp | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/test/fuzz/process_message.cpp b/src/test/fuzz/process_message.cpp index 9e3586d162..0ed3721636 100644 --- a/src/test/fuzz/process_message.cpp +++ b/src/test/fuzz/process_message.cpp @@ -57,12 +57,17 @@ const std::map> EXPECTED_DESERIALIZATION_EXCE {"Unknown transaction optional data: iostream error", {"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 index 12a5dbb607..bcbf65bdca 100644 --- a/src/test/fuzz/process_messages.cpp +++ b/src/test/fuzz/process_messages.cpp @@ -16,11 +16,16 @@ #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++) {