Skip to content

Commit fa40017

Browse files
author
MarcoFalke
committed
init: Pass reference to ArgsManager around instead of relying on global
1 parent 197450f commit fa40017

File tree

5 files changed

+35
-34
lines changed

5 files changed

+35
-34
lines changed

src/bitcoind.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,9 @@ static bool AppInit(int argc, char* argv[])
5050

5151
util::ThreadSetInternalName("init");
5252

53-
//
54-
// Parameters
55-
//
5653
// If Qt is used, parameters/bitcoin.conf are parsed in qt/bitcoin.cpp's main()
5754
SetupServerArgs(node);
55+
ArgsManager& args = *Assert(node.args);
5856
std::string error;
5957
if (!gArgs.ParseParameters(argc, argv, error)) {
6058
return InitError(Untranslated(strprintf("Error parsing command line arguments: %s\n", error)));
@@ -109,15 +107,13 @@ static bool AppInit(int argc, char* argv[])
109107
// -server defaults to true for bitcoind but not for the GUI so do this here
110108
gArgs.SoftSetBoolArg("-server", true);
111109
// Set this early so that parameter interactions go to console
112-
InitLogging();
113-
InitParameterInteraction();
114-
if (!AppInitBasicSetup())
115-
{
110+
InitLogging(args);
111+
InitParameterInteraction(args);
112+
if (!AppInitBasicSetup(args)) {
116113
// InitError will have been called with detailed error, which ends up on console
117114
return false;
118115
}
119-
if (!AppInitParameterInteraction())
120-
{
116+
if (!AppInitParameterInteraction(args)) {
121117
// InitError will have been called with detailed error, which ends up on console
122118
return false;
123119
}

src/init.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,14 @@ static const char* DEFAULT_ASMAP_FILENAME="ip_asn.map";
107107
*/
108108
static const char* BITCOIN_PID_FILENAME = "bitcoind.pid";
109109

110-
static fs::path GetPidFile()
110+
static fs::path GetPidFile(const ArgsManager& args)
111111
{
112112
return AbsPathForConfigVal(fs::path(gArgs.GetArg("-pid", BITCOIN_PID_FILENAME)));
113113
}
114114

115-
NODISCARD static bool CreatePidFile()
115+
NODISCARD static bool CreatePidFile(const ArgsManager& args)
116116
{
117-
fsbridge::ofstream file{GetPidFile()};
117+
fsbridge::ofstream file{GetPidFile(args)};
118118
if (file) {
119119
#ifdef WIN32
120120
tfm::format(file, "%d\n", GetCurrentProcessId());
@@ -123,7 +123,7 @@ NODISCARD static bool CreatePidFile()
123123
#endif
124124
return true;
125125
} else {
126-
return InitError(strprintf(_("Unable to create the PID file '%s': %s"), GetPidFile().string(), std::strerror(errno)));
126+
return InitError(strprintf(_("Unable to create the PID file '%s': %s"), GetPidFile(args).string(), std::strerror(errno)));
127127
}
128128
}
129129

@@ -180,6 +180,7 @@ void Shutdown(NodeContext& node)
180180
TRY_LOCK(g_shutdown_mutex, lock_shutdown);
181181
if (!lock_shutdown) return;
182182
LogPrintf("%s: In progress...\n", __func__);
183+
Assert(node.args);
183184

184185
/// Note: Shutdown() must be able to handle cases in which initialization failed part of the way,
185186
/// for example if the data directory was found to be locked.
@@ -230,7 +231,7 @@ void Shutdown(NodeContext& node)
230231
node.connman.reset();
231232
node.banman.reset();
232233

233-
if (::mempool.IsLoaded() && gArgs.GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
234+
if (::mempool.IsLoaded() && node.args->GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
234235
DumpMempool(::mempool);
235236
}
236237

@@ -301,19 +302,19 @@ void Shutdown(NodeContext& node)
301302
GetMainSignals().UnregisterBackgroundSignalScheduler();
302303
globalVerifyHandle.reset();
303304
ECC_Stop();
304-
node.args = nullptr;
305305
node.mempool = nullptr;
306306
node.chainman = nullptr;
307307
node.scheduler.reset();
308308

309309
try {
310-
if (!fs::remove(GetPidFile())) {
310+
if (!fs::remove(GetPidFile(*node.args))) {
311311
LogPrintf("%s: Unable to remove PID file: File does not exist\n", __func__);
312312
}
313313
} catch (const fs::filesystem_error& e) {
314314
LogPrintf("%s: Unable to remove PID file: %s\n", __func__, fsbridge::get_filesystem_error_message(e));
315315
}
316316

317+
node.args = nullptr;
317318
LogPrintf("%s: done\n", __func__);
318319
}
319320

@@ -372,7 +373,7 @@ void SetupServerArgs(NodeContext& node)
372373
node.args = &gArgs;
373374
ArgsManager& argsman = *node.args;
374375

375-
SetupHelpOptions(gArgs);
376+
SetupHelpOptions(argsman);
376377
argsman.AddArg("-help-debug", "Print help message with debugging options and exit", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST); // server-only for now
377378

378379
const auto defaultBaseParams = CreateBaseChainParams(CBaseChainParams::MAIN);
@@ -684,7 +685,7 @@ static void CleanupBlockRevFiles()
684685
}
685686
}
686687

687-
static void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFiles)
688+
static void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFiles, const ArgsManager& args)
688689
{
689690
const CChainParams& chainparams = Params();
690691
ScheduleBatchPriority();
@@ -780,6 +781,7 @@ static bool InitSanityCheck()
780781

781782
static bool AppInitServers(const util::Ref& context, NodeContext& node)
782783
{
784+
const ArgsManager& args = *Assert(node.args);
783785
RPCServer::OnStarted(&OnRPCStarted);
784786
RPCServer::OnStopped(&OnRPCStopped);
785787
if (!InitHTTPServer())
@@ -794,7 +796,7 @@ static bool AppInitServers(const util::Ref& context, NodeContext& node)
794796
}
795797

796798
// Parameter interaction based on rules
797-
void InitParameterInteraction()
799+
void InitParameterInteraction(ArgsManager& args)
798800
{
799801
// when specifying an explicit binding address, you want to listen on it
800802
// even when -connect or -proxy is specified
@@ -863,7 +865,7 @@ void InitParameterInteraction()
863865
* Note that this is called very early in the process lifetime, so you should be
864866
* careful about what global state you rely on here.
865867
*/
866-
void InitLogging()
868+
void InitLogging(const ArgsManager& args)
867869
{
868870
LogInstance().m_print_to_file = !gArgs.IsArgNegated("-debuglogfile");
869871
LogInstance().m_file_path = AbsPathForConfigVal(gArgs.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
@@ -909,7 +911,7 @@ std::set<BlockFilterType> g_enabled_filter_types;
909911
std::terminate();
910912
};
911913

912-
bool AppInitBasicSetup()
914+
bool AppInitBasicSetup(ArgsManager& args)
913915
{
914916
// ********************************************************* Step 1: setup
915917
#ifdef _MSC_VER
@@ -951,7 +953,7 @@ bool AppInitBasicSetup()
951953
return true;
952954
}
953955

954-
bool AppInitParameterInteraction()
956+
bool AppInitParameterInteraction(const ArgsManager& args)
955957
{
956958
const CChainParams& chainparams = Params();
957959
// ********************************************************* Step 2: parameter interactions
@@ -1247,9 +1249,10 @@ bool AppInitLockDataDirectory()
12471249

12481250
bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12491251
{
1252+
const ArgsManager& args = *Assert(node.args);
12501253
const CChainParams& chainparams = Params();
12511254
// ********************************************************* Step 4a: application initialization
1252-
if (!CreatePidFile()) {
1255+
if (!CreatePidFile(args)) {
12531256
// Detailed error printed inside CreatePidFile().
12541257
return false;
12551258
}
@@ -1853,7 +1856,9 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
18531856
vImportFiles.push_back(strFile);
18541857
}
18551858

1856-
g_load_block = std::thread(&TraceThread<std::function<void()>>, "loadblk", [=, &chainman]{ ThreadImport(chainman, vImportFiles); });
1859+
g_load_block = std::thread(&TraceThread<std::function<void()>>, "loadblk", [=, &chainman, &args] {
1860+
ThreadImport(chainman, vImportFiles, args);
1861+
});
18571862

18581863
// Wait for genesis block to be processed
18591864
{

src/init.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
#include <memory>
1010
#include <string>
11-
#include <util/system.h>
1211

12+
class ArgsManager;
1313
struct NodeContext;
1414
namespace interfaces {
1515
struct BlockAndHeaderTipInfo;
@@ -25,21 +25,21 @@ class Ref;
2525
void Interrupt(NodeContext& node);
2626
void Shutdown(NodeContext& node);
2727
//!Initialize the logging infrastructure
28-
void InitLogging();
28+
void InitLogging(const ArgsManager& args);
2929
//!Parameter interaction: change current parameters depending on various rules
30-
void InitParameterInteraction();
30+
void InitParameterInteraction(ArgsManager& args);
3131

3232
/** Initialize bitcoin core: Basic context setup.
3333
* @note This can be done before daemonization. Do not call Shutdown() if this function fails.
3434
* @pre Parameters should be parsed and config file should be read.
3535
*/
36-
bool AppInitBasicSetup();
36+
bool AppInitBasicSetup(ArgsManager& args);
3737
/**
3838
* Initialization: parameter interaction.
3939
* @note This can be done before daemonization. Do not call Shutdown() if this function fails.
4040
* @pre Parameters should be parsed and config file should be read, AppInitBasicSetup should have been called.
4141
*/
42-
bool AppInitParameterInteraction();
42+
bool AppInitParameterInteraction(const ArgsManager& args);
4343
/**
4444
* Initialization sanity checks: ecc init, sanity checks, dir lock.
4545
* @note This can be done before daemonization. Do not call Shutdown() if this function fails.

src/interfaces/node.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ class NodeImpl : public Node
7171
uint64_t getAssumedBlockchainSize() override { return Params().AssumedBlockchainSize(); }
7272
uint64_t getAssumedChainStateSize() override { return Params().AssumedChainStateSize(); }
7373
std::string getNetwork() override { return Params().NetworkIDString(); }
74-
void initLogging() override { InitLogging(); }
75-
void initParameterInteraction() override { InitParameterInteraction(); }
74+
void initLogging() override { InitLogging(gArgs); }
75+
void initParameterInteraction() override { InitParameterInteraction(gArgs); }
7676
bilingual_str getWarnings() override { return GetWarnings(true); }
7777
uint32_t getLogCategories() override { return LogInstance().GetCategoryMask(); }
7878
bool baseInitialize() override
7979
{
80-
return AppInitBasicSetup() && AppInitParameterInteraction() && AppInitSanityChecks() &&
80+
return AppInitBasicSetup(gArgs) && AppInitParameterInteraction(gArgs) && AppInitSanityChecks() &&
8181
AppInitLockDataDirectory();
8282
}
8383
bool appInitMain(interfaces::BlockAndHeaderTipInfo* tip_info) override

src/test/util/setup_common.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName, const std::ve
9797
SelectParams(chainName);
9898
SeedInsecureRand();
9999
if (G_TEST_LOG_FUN) LogInstance().PushBackCallback(G_TEST_LOG_FUN);
100-
InitLogging();
101-
AppInitParameterInteraction();
100+
InitLogging(*m_node.args);
101+
AppInitParameterInteraction(*m_node.args);
102102
LogInstance().StartLogging();
103103
SHA256AutoDetect();
104104
ECC_Start();

0 commit comments

Comments
 (0)