Skip to content

Commit eeff271

Browse files
committed
Deduplicate bitcoind and bitcoin-qt init code
Add common InitConfig function to deduplicate bitcoind and bitcoin-qt code parsing the config file, creating the datadir, and selecting the network. There is a slight change of behavior for bitcoin-qt: When there is a problem reading the configuration file, the error prefix is changed from "Error: Cannot parse configuration file:" to "Error reading configuration file: ". The new message is more accurate and makes the GUI consistent with the CLI.
1 parent 8e225dc commit eeff271

File tree

5 files changed

+62
-33
lines changed

5 files changed

+62
-33
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ BITCOIN_CORE_H = \
134134
clientversion.h \
135135
coins.h \
136136
common/bloom.h \
137+
common/init.h \
137138
common/run_command.h \
138139
common/url.h \
139140
compat/assumptions.h \
@@ -640,6 +641,7 @@ libbitcoin_common_a_SOURCES = \
640641
chainparams.cpp \
641642
coins.cpp \
642643
common/bloom.cpp \
644+
common/init.cpp \
643645
common/interfaces.cpp \
644646
common/run_command.cpp \
645647
compressor.cpp \

src/bitcoind.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <chainparams.h>
1111
#include <clientversion.h>
12+
#include <common/init.h>
1213
#include <common/url.h>
1314
#include <compat/compat.h>
1415
#include <init.h>
@@ -150,17 +151,8 @@ static bool AppInit(NodeContext& node, int argc, char* argv[])
150151
std::any context{&node};
151152
try
152153
{
153-
if (!CheckDataDirOption()) {
154-
return InitError(Untranslated(strprintf("Specified data directory \"%s\" does not exist.", args.GetArg("-datadir", ""))));
155-
}
156-
if (!args.ReadConfigFiles(error, true)) {
157-
return InitError(Untranslated(strprintf("Error reading configuration file: %s", error)));
158-
}
159-
// Check for chain settings (Params() calls are only valid after this clause)
160-
try {
161-
SelectParams(args.GetChainName());
162-
} catch (const std::exception& e) {
163-
return InitError(Untranslated(strprintf("%s", e.what())));
154+
if (auto error = common::InitConfig(args)) {
155+
return InitError(*error);
164156
}
165157

166158
// Error out when loose non-argument tokens are encountered on command line

src/common/init.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) 2023 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <common/init.h>
6+
#include <chainparams.h>
7+
#include <util/system.h>
8+
9+
#include <optional>
10+
11+
namespace common {
12+
std::optional<bilingual_str> InitConfig(ArgsManager& args)
13+
{
14+
if (!CheckDataDirOption()) {
15+
return strprintf(_("Specified data directory \"%s\" does not exist."), args.GetArg("-datadir", ""));
16+
}
17+
std::string error;
18+
if (!args.ReadConfigFiles(error, true)) {
19+
return strprintf(_("Error reading configuration file: %s"), error);
20+
}
21+
// Check for chain settings (Params() calls are only valid after this clause)
22+
try {
23+
SelectParams(args.GetChainName());
24+
} catch (const std::exception& e) {
25+
return Untranslated(e.what());
26+
}
27+
return {};
28+
}
29+
} // namespace commond

src/common/init.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2023 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_COMMON_INIT_H
6+
#define BITCOIN_COMMON_INIT_H
7+
8+
#include <util/translation.h>
9+
10+
#include <optional>
11+
12+
class ArgsManager;
13+
14+
namespace common {
15+
/* Read config files and create datadir if it does not exist. Return error on failure */
16+
std::optional<bilingual_str> InitConfig(ArgsManager& args);
17+
} // namespace common
18+
19+
#endif // BITCOIN_COMMON_INIT_H

src/qt/bitcoin.cpp

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <qt/bitcoin.h>
1010

1111
#include <chainparams.h>
12+
#include <common/init.h>
1213
#include <init.h>
1314
#include <interfaces/handler.h>
1415
#include <interfaces/init.h>
@@ -586,33 +587,19 @@ int GuiMain(int argc, char* argv[])
586587
// Gracefully exit if the user cancels
587588
if (!Intro::showIfNeeded(did_show_intro, prune_MiB)) return EXIT_SUCCESS;
588589

589-
/// 6a. Determine availability of data directory
590-
if (!CheckDataDirOption()) {
591-
InitError(strprintf(Untranslated("Specified data directory \"%s\" does not exist."), gArgs.GetArg("-datadir", "")));
592-
QMessageBox::critical(nullptr, PACKAGE_NAME,
593-
QObject::tr("Error: Specified data directory \"%1\" does not exist.").arg(QString::fromStdString(gArgs.GetArg("-datadir", ""))));
594-
return EXIT_FAILURE;
595-
}
596590
try {
597-
/// 6b. Parse bitcoin.conf
598-
/// - Do not call gArgs.GetDataDirNet() before this step finishes
599-
if (!gArgs.ReadConfigFiles(error, true)) {
600-
InitError(strprintf(Untranslated("Error reading configuration file: %s"), error));
601-
QMessageBox::critical(nullptr, PACKAGE_NAME,
602-
QObject::tr("Error: Cannot parse configuration file: %1.").arg(QString::fromStdString(error)));
603-
return EXIT_FAILURE;
604-
}
605-
606-
/// 7. Determine network (and switch to network specific options)
591+
/// 6-7. Parse bitcoin.conf, determine network, switch to network specific options, and create datadir.
592+
// - Do not call gArgs.GetDataDirNet() before this step finishes
607593
// - Do not call Params() before this step
608-
// - Do this after parsing the configuration file, as the network can be switched there
609594
// - QSettings() will use the new application name after this, resulting in network-specific settings
610595
// - Needs to be done before createOptionsModel
611-
612-
// Check for chain settings (Params() calls are only valid after this clause)
613-
SelectParams(gArgs.GetChainName());
596+
if (auto error = common::InitConfig(gArgs)) {
597+
InitError(*error);
598+
QMessageBox::critical(nullptr, PACKAGE_NAME, QObject::tr("Error: %1").arg(QString::fromStdString(error->translated)));
599+
return EXIT_FAILURE;
600+
}
614601
} catch(std::exception &e) {
615-
InitError(Untranslated(strprintf("%s", e.what())));
602+
InitError(Untranslated(e.what()));
616603
QMessageBox::critical(nullptr, PACKAGE_NAME, QObject::tr("Error: %1").arg(e.what()));
617604
return EXIT_FAILURE;
618605
}

0 commit comments

Comments
 (0)