Skip to content

Commit c82165a

Browse files
committed
qt, refactor: Move InitExecutor class into its own module
This change makes InitExecutor class re-usable by an alternative GUI, e.g., QML-based one.
1 parent dbcf56b commit c82165a

File tree

7 files changed

+136
-95
lines changed

7 files changed

+136
-95
lines changed

build_msvc/libbitcoin_qt/libbitcoin_qt.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<ClCompile Include="..\..\src\qt\csvmodelwriter.cpp" />
2525
<ClCompile Include="..\..\src\qt\editaddressdialog.cpp" />
2626
<ClCompile Include="..\..\src\qt\guiutil.cpp" />
27+
<ClCompile Include="..\..\src\qt\initexecutor.cpp" />
2728
<ClCompile Include="..\..\src\qt\intro.cpp" />
2829
<ClCompile Include="..\..\src\qt\modaloverlay.cpp" />
2930
<ClCompile Include="..\..\src\qt\networkstyle.cpp" />
@@ -78,6 +79,7 @@
7879
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_csvmodelwriter.cpp" />
7980
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_editaddressdialog.cpp" />
8081
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_guiutil.cpp" />
82+
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_initexecutor.cpp" />
8183
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_intro.cpp" />
8284
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_modaloverlay.cpp" />
8385
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_networkstyle.cpp" />

src/Makefile.qt.include

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ QT_MOC_CPP = \
4040
qt/moc_askpassphrasedialog.cpp \
4141
qt/moc_createwalletdialog.cpp \
4242
qt/moc_bantablemodel.cpp \
43+
qt/moc_bitcoin.cpp \
4344
qt/moc_bitcoinaddressvalidator.cpp \
4445
qt/moc_bitcoinamountfield.cpp \
45-
qt/moc_bitcoin.cpp \
4646
qt/moc_bitcoingui.cpp \
4747
qt/moc_bitcoinunits.cpp \
4848
qt/moc_clientmodel.cpp \
@@ -51,6 +51,7 @@ QT_MOC_CPP = \
5151
qt/moc_csvmodelwriter.cpp \
5252
qt/moc_editaddressdialog.cpp \
5353
qt/moc_guiutil.cpp \
54+
qt/moc_initexecutor.cpp \
5455
qt/moc_intro.cpp \
5556
qt/moc_macdockiconhandler.cpp \
5657
qt/moc_macnotificationhandler.cpp \
@@ -109,9 +110,9 @@ BITCOIN_QT_H = \
109110
qt/addresstablemodel.h \
110111
qt/askpassphrasedialog.h \
111112
qt/bantablemodel.h \
113+
qt/bitcoin.h \
112114
qt/bitcoinaddressvalidator.h \
113115
qt/bitcoinamountfield.h \
114-
qt/bitcoin.h \
115116
qt/bitcoingui.h \
116117
qt/bitcoinunits.h \
117118
qt/clientmodel.h \
@@ -122,6 +123,7 @@ BITCOIN_QT_H = \
122123
qt/editaddressdialog.h \
123124
qt/guiconstants.h \
124125
qt/guiutil.h \
126+
qt/initexecutor.h \
125127
qt/intro.h \
126128
qt/macdockiconhandler.h \
127129
qt/macnotificationhandler.h \
@@ -227,6 +229,7 @@ BITCOIN_QT_BASE_CPP = \
227229
qt/clientmodel.cpp \
228230
qt/csvmodelwriter.cpp \
229231
qt/guiutil.cpp \
232+
qt/initexecutor.cpp \
230233
qt/intro.cpp \
231234
qt/modaloverlay.cpp \
232235
qt/networkstyle.cpp \

src/qt/bitcoin.cpp

Lines changed: 13 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,38 @@
77
#endif
88

99
#include <qt/bitcoin.h>
10-
#include <qt/bitcoingui.h>
1110

1211
#include <chainparams.h>
12+
#include <init.h>
13+
#include <interfaces/handler.h>
14+
#include <interfaces/node.h>
15+
#include <node/context.h>
16+
#include <node/ui_interface.h>
17+
#include <noui.h>
18+
#include <qt/bitcoingui.h>
1319
#include <qt/clientmodel.h>
1420
#include <qt/guiconstants.h>
1521
#include <qt/guiutil.h>
22+
#include <qt/initexecutor.h>
1623
#include <qt/intro.h>
1724
#include <qt/networkstyle.h>
1825
#include <qt/optionsmodel.h>
1926
#include <qt/platformstyle.h>
2027
#include <qt/splashscreen.h>
2128
#include <qt/utilitydialog.h>
2229
#include <qt/winshutdownmonitor.h>
30+
#include <uint256.h>
31+
#include <util/system.h>
32+
#include <util/threadnames.h>
33+
#include <util/translation.h>
34+
#include <validation.h>
2335

2436
#ifdef ENABLE_WALLET
2537
#include <qt/paymentserver.h>
2638
#include <qt/walletcontroller.h>
2739
#include <qt/walletmodel.h>
2840
#endif // ENABLE_WALLET
2941

30-
#include <init.h>
31-
#include <interfaces/handler.h>
32-
#include <interfaces/node.h>
33-
#include <node/context.h>
34-
#include <node/ui_interface.h>
35-
#include <noui.h>
36-
#include <uint256.h>
37-
#include <util/system.h>
38-
#include <util/threadnames.h>
39-
#include <util/translation.h>
40-
#include <validation.h>
41-
4242
#include <boost/signals2/connection.hpp>
4343
#include <memory>
4444

@@ -155,58 +155,6 @@ void DebugMessageHandler(QtMsgType type, const QMessageLogContext& context, cons
155155
}
156156
}
157157

158-
InitExecutor::InitExecutor(interfaces::Node& node) :
159-
QObject(), m_node(node)
160-
{
161-
this->moveToThread(&m_thread);
162-
m_thread.start();
163-
}
164-
165-
InitExecutor::~InitExecutor()
166-
{
167-
qDebug() << __func__ << ": Stopping thread";
168-
m_thread.quit();
169-
m_thread.wait();
170-
qDebug() << __func__ << ": Stopped thread";
171-
}
172-
173-
void InitExecutor::handleRunawayException(const std::exception *e)
174-
{
175-
PrintExceptionContinue(e, "Runaway exception");
176-
Q_EMIT runawayException(QString::fromStdString(m_node.getWarnings().translated));
177-
}
178-
179-
void InitExecutor::initialize()
180-
{
181-
try
182-
{
183-
util::ThreadRename("qt-init");
184-
qDebug() << __func__ << ": Running initialization in thread";
185-
interfaces::BlockAndHeaderTipInfo tip_info;
186-
bool rv = m_node.appInitMain(&tip_info);
187-
Q_EMIT initializeResult(rv, tip_info);
188-
} catch (const std::exception& e) {
189-
handleRunawayException(&e);
190-
} catch (...) {
191-
handleRunawayException(nullptr);
192-
}
193-
}
194-
195-
void InitExecutor::shutdown()
196-
{
197-
try
198-
{
199-
qDebug() << __func__ << ": Running Shutdown in thread";
200-
m_node.appShutdown();
201-
qDebug() << __func__ << ": Shutdown finished";
202-
Q_EMIT shutdownResult();
203-
} catch (const std::exception& e) {
204-
handleRunawayException(&e);
205-
} catch (...) {
206-
handleRunawayException(nullptr);
207-
}
208-
}
209-
210158
static int qt_argc = 1;
211159
static const char* qt_argv = "bitcoin-qt";
212160

src/qt/bitcoin.h

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
#endif
1111

1212
#include <interfaces/node.h>
13+
#include <qt/initexecutor.h>
1314

1415
#include <assert.h>
1516
#include <memory>
1617
#include <optional>
1718

1819
#include <QApplication>
19-
#include <QThread>
2020

2121
class BitcoinGUI;
2222
class ClientModel;
@@ -29,33 +29,6 @@ class WalletController;
2929
class WalletModel;
3030

3131

32-
/** Class encapsulating Bitcoin Core startup and shutdown.
33-
* Allows running startup and shutdown in a different thread from the UI thread.
34-
*/
35-
class InitExecutor: public QObject
36-
{
37-
Q_OBJECT
38-
public:
39-
explicit InitExecutor(interfaces::Node& node);
40-
~InitExecutor();
41-
42-
public Q_SLOTS:
43-
void initialize();
44-
void shutdown();
45-
46-
Q_SIGNALS:
47-
void initializeResult(bool success, interfaces::BlockAndHeaderTipInfo tip_info);
48-
void shutdownResult();
49-
void runawayException(const QString &message);
50-
51-
private:
52-
/// Pass fatal exception message to UI thread
53-
void handleRunawayException(const std::exception *e);
54-
55-
interfaces::Node& m_node;
56-
QThread m_thread;
57-
};
58-
5932
/** Main Bitcoin application object */
6033
class BitcoinApplication: public QApplication
6134
{

src/qt/initexecutor.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) 2014-2021 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 <qt/initexecutor.h>
6+
7+
#include <interfaces/node.h>
8+
#include <util/system.h>
9+
#include <util/threadnames.h>
10+
11+
#include <exception>
12+
13+
#include <QDebug>
14+
#include <QObject>
15+
#include <QString>
16+
#include <QThread>
17+
18+
InitExecutor::InitExecutor(interfaces::Node& node) :
19+
QObject(), m_node(node)
20+
{
21+
this->moveToThread(&m_thread);
22+
m_thread.start();
23+
}
24+
25+
InitExecutor::~InitExecutor()
26+
{
27+
qDebug() << __func__ << ": Stopping thread";
28+
m_thread.quit();
29+
m_thread.wait();
30+
qDebug() << __func__ << ": Stopped thread";
31+
}
32+
33+
void InitExecutor::handleRunawayException(const std::exception *e)
34+
{
35+
PrintExceptionContinue(e, "Runaway exception");
36+
Q_EMIT runawayException(QString::fromStdString(m_node.getWarnings().translated));
37+
}
38+
39+
void InitExecutor::initialize()
40+
{
41+
try
42+
{
43+
util::ThreadRename("qt-init");
44+
qDebug() << __func__ << ": Running initialization in thread";
45+
interfaces::BlockAndHeaderTipInfo tip_info;
46+
bool rv = m_node.appInitMain(&tip_info);
47+
Q_EMIT initializeResult(rv, tip_info);
48+
} catch (const std::exception& e) {
49+
handleRunawayException(&e);
50+
} catch (...) {
51+
handleRunawayException(nullptr);
52+
}
53+
}
54+
55+
void InitExecutor::shutdown()
56+
{
57+
try
58+
{
59+
qDebug() << __func__ << ": Running Shutdown in thread";
60+
m_node.appShutdown();
61+
qDebug() << __func__ << ": Shutdown finished";
62+
Q_EMIT shutdownResult();
63+
} catch (const std::exception& e) {
64+
handleRunawayException(&e);
65+
} catch (...) {
66+
handleRunawayException(nullptr);
67+
}
68+
}

src/qt/initexecutor.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2014-2021 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_QT_INITEXECUTOR_H
6+
#define BITCOIN_QT_INITEXECUTOR_H
7+
8+
#include <interfaces/node.h>
9+
10+
#include <exception>
11+
12+
#include <QObject>
13+
#include <QThread>
14+
15+
QT_BEGIN_NAMESPACE
16+
class QString;
17+
QT_END_NAMESPACE
18+
19+
/** Class encapsulating Bitcoin Core startup and shutdown.
20+
* Allows running startup and shutdown in a different thread from the UI thread.
21+
*/
22+
class InitExecutor: public QObject
23+
{
24+
Q_OBJECT
25+
public:
26+
explicit InitExecutor(interfaces::Node& node);
27+
~InitExecutor();
28+
29+
public Q_SLOTS:
30+
void initialize();
31+
void shutdown();
32+
33+
Q_SIGNALS:
34+
void initializeResult(bool success, interfaces::BlockAndHeaderTipInfo tip_info);
35+
void shutdownResult();
36+
void runawayException(const QString &message);
37+
38+
private:
39+
/// Pass fatal exception message to UI thread
40+
void handleRunawayException(const std::exception *e);
41+
42+
interfaces::Node& m_node;
43+
QThread m_thread;
44+
};
45+
46+
#endif // BITCOIN_QT_INITEXECUTOR_H

src/qt/test/test_main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <interfaces/node.h>
1010
#include <qt/bitcoin.h>
11+
#include <qt/initexecutor.h>
1112
#include <qt/test/apptests.h>
1213
#include <qt/test/rpcnestedtests.h>
1314
#include <qt/test/uritests.h>

0 commit comments

Comments
 (0)