Skip to content

Commit 40d0b0a

Browse files
committed
Merge bitcoin/bitcoin#27239: refactor: Consistently use context args over gArgs in node/interfaces
fa3e9b4 refactor: Consistently use args over gArgs in init.cpp (MarcoFalke) fa89112 refactor: Consistently use context args over gArgs in node/interfaces (MarcoFalke) Pull request description: Currently `node/interfaces.cpp` uses a mix of `gArgs` vs `m_context->args`. This is fine, because outside of tests those should be identical. However, it makes the code inconsistent and harder to use in tests. Fix that by using `args` from the context consistently. Do the same in `init.cpp`, where `gArgs` and `args` are inconsistently used in the same scope or even line. ACKs for top commit: ryanofsky: Code review ACK fa3e9b4 Tree-SHA512: bcd52f176794ebb1ecb9e1922411f7b84d212ae13bad314a1961b85f3077f645fca71fb0124c0889ebfdd8b59a0903b99b9985b1a4fb8f152aa6d7f0126fe5c7
2 parents 87af64a + fa3e9b4 commit 40d0b0a

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

src/init.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ bool AppInitParameterInteraction(const ArgsManager& args, bool use_syscall_sandb
867867
InitWarning(warnings);
868868
}
869869

870-
if (!fs::is_directory(gArgs.GetBlocksDirPath())) {
870+
if (!fs::is_directory(args.GetBlocksDirPath())) {
871871
return InitError(strprintf(_("Specified blocks directory \"%s\" does not exist."), args.GetArg("-blocksdir", "")));
872872
}
873873

@@ -1224,7 +1224,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12241224
if (args.IsArgSet("-asmap")) {
12251225
fs::path asmap_path = args.GetPathArg("-asmap", DEFAULT_ASMAP_FILENAME);
12261226
if (!asmap_path.is_absolute()) {
1227-
asmap_path = gArgs.GetDataDirNet() / asmap_path;
1227+
asmap_path = args.GetDataDirNet() / asmap_path;
12281228
}
12291229
if (!fs::exists(asmap_path)) {
12301230
InitError(strprintf(_("Could not find asmap file %s"), fs::quoted(fs::PathToString(asmap_path))));
@@ -1254,7 +1254,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12541254
}
12551255

12561256
assert(!node.banman);
1257-
node.banman = std::make_unique<BanMan>(gArgs.GetDataDirNet() / "banlist", &uiInterface, args.GetIntArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
1257+
node.banman = std::make_unique<BanMan>(args.GetDataDirNet() / "banlist", &uiInterface, args.GetIntArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
12581258
assert(!node.connman);
12591259
node.connman = std::make_unique<CConnman>(GetRand<uint64_t>(),
12601260
GetRand<uint64_t>(),
@@ -1618,12 +1618,12 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
16181618

16191619
// ********************************************************* Step 11: import blocks
16201620

1621-
if (!CheckDiskSpace(gArgs.GetDataDirNet())) {
1622-
InitError(strprintf(_("Error: Disk space is low for %s"), fs::quoted(fs::PathToString(gArgs.GetDataDirNet()))));
1621+
if (!CheckDiskSpace(args.GetDataDirNet())) {
1622+
InitError(strprintf(_("Error: Disk space is low for %s"), fs::quoted(fs::PathToString(args.GetDataDirNet()))));
16231623
return false;
16241624
}
1625-
if (!CheckDiskSpace(gArgs.GetBlocksDirPath())) {
1626-
InitError(strprintf(_("Error: Disk space is low for %s"), fs::quoted(fs::PathToString(gArgs.GetBlocksDirPath()))));
1625+
if (!CheckDiskSpace(args.GetBlocksDirPath())) {
1626+
InitError(strprintf(_("Error: Disk space is low for %s"), fs::quoted(fs::PathToString(args.GetBlocksDirPath()))));
16271627
return false;
16281628
}
16291629

@@ -1715,7 +1715,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17151715
if (node.peerman) node.peerman->SetBestHeight(chain_active_height);
17161716

17171717
// Map ports with UPnP or NAT-PMP.
1718-
StartMapPort(args.GetBoolArg("-upnp", DEFAULT_UPNP), gArgs.GetBoolArg("-natpmp", DEFAULT_NATPMP));
1718+
StartMapPort(args.GetBoolArg("-upnp", DEFAULT_UPNP), args.GetBoolArg("-natpmp", DEFAULT_NATPMP));
17191719

17201720
CConnman::Options connOptions;
17211721
connOptions.nLocalServices = nLocalServices;

src/node/interfaces.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ class NodeImpl : public Node
8686
{
8787
public:
8888
explicit NodeImpl(NodeContext& context) { setContext(&context); }
89-
void initLogging() override { InitLogging(*Assert(m_context->args)); }
90-
void initParameterInteraction() override { InitParameterInteraction(*Assert(m_context->args)); }
89+
void initLogging() override { InitLogging(args()); }
90+
void initParameterInteraction() override { InitParameterInteraction(args()); }
9191
bilingual_str getWarnings() override { return GetWarnings(true); }
9292
uint32_t getLogCategories() override { return LogInstance().GetCategoryMask(); }
9393
bool baseInitialize() override
9494
{
95-
if (!AppInitBasicSetup(gArgs)) return false;
96-
if (!AppInitParameterInteraction(gArgs, /*use_syscall_sandbox=*/false)) return false;
95+
if (!AppInitBasicSetup(args())) return false;
96+
if (!AppInitParameterInteraction(args(), /*use_syscall_sandbox=*/false)) return false;
9797

9898
m_context->kernel = std::make_unique<kernel::Context>();
9999
if (!AppInitSanityChecks(*m_context->kernel)) return false;
@@ -116,7 +116,7 @@ class NodeImpl : public Node
116116
{
117117
StartShutdown();
118118
// Stop RPC for clean shutdown if any of waitfor* commands is executed.
119-
if (gArgs.GetBoolArg("-server", false)) {
119+
if (args().GetBoolArg("-server", false)) {
120120
InterruptRPC();
121121
StopRPC();
122122
}
@@ -125,28 +125,28 @@ class NodeImpl : public Node
125125
bool isSettingIgnored(const std::string& name) override
126126
{
127127
bool ignored = false;
128-
gArgs.LockSettings([&](util::Settings& settings) {
128+
args().LockSettings([&](util::Settings& settings) {
129129
if (auto* options = util::FindKey(settings.command_line_options, name)) {
130130
ignored = !options->empty();
131131
}
132132
});
133133
return ignored;
134134
}
135-
util::SettingsValue getPersistentSetting(const std::string& name) override { return gArgs.GetPersistentSetting(name); }
135+
util::SettingsValue getPersistentSetting(const std::string& name) override { return args().GetPersistentSetting(name); }
136136
void updateRwSetting(const std::string& name, const util::SettingsValue& value) override
137137
{
138-
gArgs.LockSettings([&](util::Settings& settings) {
138+
args().LockSettings([&](util::Settings& settings) {
139139
if (value.isNull()) {
140140
settings.rw_settings.erase(name);
141141
} else {
142142
settings.rw_settings[name] = value;
143143
}
144144
});
145-
gArgs.WriteSettingsFile();
145+
args().WriteSettingsFile();
146146
}
147147
void forceSetting(const std::string& name, const util::SettingsValue& value) override
148148
{
149-
gArgs.LockSettings([&](util::Settings& settings) {
149+
args().LockSettings([&](util::Settings& settings) {
150150
if (value.isNull()) {
151151
settings.forced_settings.erase(name);
152152
} else {
@@ -156,11 +156,11 @@ class NodeImpl : public Node
156156
}
157157
void resetSettings() override
158158
{
159-
gArgs.WriteSettingsFile(/*errors=*/nullptr, /*backup=*/true);
160-
gArgs.LockSettings([&](util::Settings& settings) {
159+
args().WriteSettingsFile(/*errors=*/nullptr, /*backup=*/true);
160+
args().LockSettings([&](util::Settings& settings) {
161161
settings.rw_settings.clear();
162162
});
163-
gArgs.WriteSettingsFile();
163+
args().WriteSettingsFile();
164164
}
165165
void mapPort(bool use_upnp, bool use_natpmp) override { StartMapPort(use_upnp, use_natpmp); }
166166
bool getProxy(Network net, Proxy& proxy_info) override { return GetProxy(net, proxy_info); }
@@ -237,7 +237,7 @@ class NodeImpl : public Node
237237
{
238238
#ifdef ENABLE_EXTERNAL_SIGNER
239239
std::vector<ExternalSigner> signers = {};
240-
const std::string command = gArgs.GetArg("-signer", "");
240+
const std::string command = args().GetArg("-signer", "");
241241
if (command == "") return {};
242242
ExternalSigner::Enumerate(command, signers, Params().NetworkIDString());
243243
std::vector<std::unique_ptr<interfaces::ExternalSigner>> result;
@@ -388,6 +388,7 @@ class NodeImpl : public Node
388388
{
389389
m_context = context;
390390
}
391+
ArgsManager& args() { return *Assert(Assert(m_context)->args); }
391392
ChainstateManager& chainman() { return *Assert(m_context->chainman); }
392393
NodeContext* m_context{nullptr};
393394
};
@@ -744,16 +745,16 @@ class ChainImpl : public Chain
744745
int rpcSerializationFlags() override { return RPCSerializationFlags(); }
745746
util::SettingsValue getSetting(const std::string& name) override
746747
{
747-
return gArgs.GetSetting(name);
748+
return args().GetSetting(name);
748749
}
749750
std::vector<util::SettingsValue> getSettingsList(const std::string& name) override
750751
{
751-
return gArgs.GetSettingsList(name);
752+
return args().GetSettingsList(name);
752753
}
753754
util::SettingsValue getRwSetting(const std::string& name) override
754755
{
755756
util::SettingsValue result;
756-
gArgs.LockSettings([&](const util::Settings& settings) {
757+
args().LockSettings([&](const util::Settings& settings) {
757758
if (const util::SettingsValue* value = util::FindKey(settings.rw_settings, name)) {
758759
result = *value;
759760
}
@@ -762,14 +763,14 @@ class ChainImpl : public Chain
762763
}
763764
bool updateRwSetting(const std::string& name, const util::SettingsValue& value, bool write) override
764765
{
765-
gArgs.LockSettings([&](util::Settings& settings) {
766+
args().LockSettings([&](util::Settings& settings) {
766767
if (value.isNull()) {
767768
settings.rw_settings.erase(name);
768769
} else {
769770
settings.rw_settings[name] = value;
770771
}
771772
});
772-
return !write || gArgs.WriteSettingsFile();
773+
return !write || args().WriteSettingsFile();
773774
}
774775
void requestMempoolTransactions(Notifications& notifications) override
775776
{
@@ -785,6 +786,7 @@ class ChainImpl : public Chain
785786
}
786787

787788
NodeContext* context() override { return &m_node; }
789+
ArgsManager& args() { return *Assert(m_node.args); }
788790
ChainstateManager& chainman() { return *Assert(m_node.chainman); }
789791
NodeContext& m_node;
790792
};

0 commit comments

Comments
 (0)