Skip to content

Commit e508fb4

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

File tree

6 files changed

+134
-95
lines changed

6 files changed

+134
-95
lines changed

src/Makefile.qt.include

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ 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 \
46+
qt/moc_bitcoincore.cpp \
4647
qt/moc_bitcoingui.cpp \
4748
qt/moc_bitcoinunits.cpp \
4849
qt/moc_clientmodel.cpp \
@@ -109,9 +110,10 @@ 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 \
116+
qt/bitcoincore.h \
115117
qt/bitcoingui.h \
116118
qt/bitcoinunits.h \
117119
qt/clientmodel.h \
@@ -222,6 +224,7 @@ BITCOIN_QT_BASE_CPP = \
222224
qt/bitcoin.cpp \
223225
qt/bitcoinaddressvalidator.cpp \
224226
qt/bitcoinamountfield.cpp \
227+
qt/bitcoincore.cpp \
225228
qt/bitcoingui.cpp \
226229
qt/bitcoinunits.cpp \
227230
qt/clientmodel.cpp \

src/qt/bitcoin.cpp

Lines changed: 13 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@
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/bitcoincore.h>
19+
#include <qt/bitcoingui.h>
1320
#include <qt/clientmodel.h>
1421
#include <qt/guiconstants.h>
1522
#include <qt/guiutil.h>
@@ -20,25 +27,18 @@
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-
BitcoinCore::BitcoinCore(interfaces::Node& node) :
159-
QObject(), m_node(node)
160-
{
161-
this->moveToThread(&m_thread);
162-
m_thread.start();
163-
}
164-
165-
BitcoinCore::~BitcoinCore()
166-
{
167-
qDebug() << __func__ << ": Stopping thread";
168-
m_thread.quit();
169-
m_thread.wait();
170-
qDebug() << __func__ << ": Stopped thread";
171-
}
172-
173-
void BitcoinCore::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 BitcoinCore::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 BitcoinCore::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,12 +10,12 @@
1010
#endif
1111

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

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

1718
#include <QApplication>
18-
#include <QThread>
1919

2020
class BitcoinGUI;
2121
class ClientModel;
@@ -28,33 +28,6 @@ class WalletController;
2828
class WalletModel;
2929

3030

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

src/qt/bitcoincore.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/bitcoincore.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+
BitcoinCore::BitcoinCore(interfaces::Node& node) :
19+
QObject(), m_node(node)
20+
{
21+
this->moveToThread(&m_thread);
22+
m_thread.start();
23+
}
24+
25+
BitcoinCore::~BitcoinCore()
26+
{
27+
qDebug() << __func__ << ": Stopping thread";
28+
m_thread.quit();
29+
m_thread.wait();
30+
qDebug() << __func__ << ": Stopped thread";
31+
}
32+
33+
void BitcoinCore::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 BitcoinCore::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 BitcoinCore::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/bitcoincore.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_BITCOINCORE_H
6+
#define BITCOIN_QT_BITCOINCORE_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 BitcoinCore: public QObject
23+
{
24+
Q_OBJECT
25+
public:
26+
explicit BitcoinCore(interfaces::Node& node);
27+
~BitcoinCore();
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_BITCOINCORE_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/bitcoincore.h>
1112
#include <qt/test/apptests.h>
1213
#include <qt/test/rpcnestedtests.h>
1314
#include <qt/test/uritests.h>

0 commit comments

Comments
 (0)