Skip to content

Commit 102abff

Browse files
committed
gui: Replace interface::Node references with pointers
No change in behavior. Replacing references with pointers allows Node interface creation to be delayed until later during gui startup next commit to support implementing -ipcconnect option
1 parent 91aced7 commit 102abff

File tree

9 files changed

+74
-41
lines changed

9 files changed

+74
-41
lines changed

src/qt/bitcoin.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,9 @@ void BitcoinCore::shutdown()
196196
static int qt_argc = 1;
197197
static const char* qt_argv = "bitcoin-qt";
198198

199-
BitcoinApplication::BitcoinApplication(interfaces::Node& node):
199+
BitcoinApplication::BitcoinApplication():
200200
QApplication(qt_argc, const_cast<char **>(&qt_argv)),
201201
coreThread(nullptr),
202-
m_node(node),
203202
optionsModel(nullptr),
204203
clientModel(nullptr),
205204
window(nullptr),
@@ -249,38 +248,47 @@ void BitcoinApplication::createPaymentServer()
249248

250249
void BitcoinApplication::createOptionsModel(bool resetSettings)
251250
{
252-
optionsModel = new OptionsModel(m_node, this, resetSettings);
251+
optionsModel = new OptionsModel(this, resetSettings);
252+
optionsModel->setNode(node());
253253
}
254254

255255
void BitcoinApplication::createWindow(const NetworkStyle *networkStyle)
256256
{
257-
window = new BitcoinGUI(m_node, platformStyle, networkStyle, nullptr);
257+
window = new BitcoinGUI(node(), platformStyle, networkStyle, nullptr);
258258

259259
pollShutdownTimer = new QTimer(window);
260260
connect(pollShutdownTimer, &QTimer::timeout, window, &BitcoinGUI::detectShutdown);
261261
}
262262

263263
void BitcoinApplication::createSplashScreen(const NetworkStyle *networkStyle)
264264
{
265-
SplashScreen *splash = new SplashScreen(m_node, nullptr, networkStyle);
265+
assert(!m_splash);
266+
m_splash = new SplashScreen(nullptr, networkStyle);
267+
m_splash->setNode(node());
266268
// We don't hold a direct pointer to the splash screen after creation, but the splash
267269
// screen will take care of deleting itself when finish() happens.
268-
splash->show();
269-
connect(this, &BitcoinApplication::splashFinished, splash, &SplashScreen::finish);
270-
connect(this, &BitcoinApplication::requestedShutdown, splash, &QWidget::close);
270+
m_splash->show();
271+
connect(this, &BitcoinApplication::splashFinished, m_splash, &SplashScreen::finish);
272+
connect(this, &BitcoinApplication::requestedShutdown, m_splash, &QWidget::close);
273+
}
274+
275+
void BitcoinApplication::setNode(interfaces::Node& node)
276+
{
277+
assert(!m_node);
278+
m_node = &node;
271279
}
272280

273281
bool BitcoinApplication::baseInitialize()
274282
{
275-
return m_node.baseInitialize();
283+
return node().baseInitialize();
276284
}
277285

278286
void BitcoinApplication::startThread()
279287
{
280288
if(coreThread)
281289
return;
282290
coreThread = new QThread(this);
283-
BitcoinCore *executor = new BitcoinCore(m_node);
291+
BitcoinCore *executor = new BitcoinCore(node());
284292
executor->moveToThread(coreThread);
285293

286294
/* communication to and from thread */
@@ -334,7 +342,7 @@ void BitcoinApplication::requestShutdown()
334342
window->unsubscribeFromCoreSignals();
335343
// Request node shutdown, which can interrupt long operations, like
336344
// rescanning a wallet.
337-
m_node.startShutdown();
345+
node().startShutdown();
338346
// Unsetting the client model can cause the current thread to wait for node
339347
// to complete an operation, like wait for a RPC execution to complete.
340348
window->setClientModel(nullptr);
@@ -356,7 +364,7 @@ void BitcoinApplication::initializeResult(bool success, interfaces::BlockAndHead
356364
{
357365
// Log this only after AppInitMain finishes, as then logging setup is guaranteed complete
358366
qInfo() << "Platform customization:" << platformStyle->getName();
359-
clientModel = new ClientModel(m_node, optionsModel);
367+
clientModel = new ClientModel(node(), optionsModel);
360368
window->setClientModel(clientModel, &tip_info);
361369
#ifdef ENABLE_WALLET
362370
if (WalletModel::isWalletEnabled()) {
@@ -456,7 +464,8 @@ int GuiMain(int argc, char* argv[])
456464
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
457465
#endif
458466

459-
BitcoinApplication app(*node);
467+
BitcoinApplication app;
468+
app.setNode(*node);
460469

461470
/// 2. Parse command-line options. We do this after qt in order to show an error if there are problems parsing these
462471
// Command-line options take precedence:
@@ -612,10 +621,10 @@ int GuiMain(int argc, char* argv[])
612621
}
613622
} catch (const std::exception& e) {
614623
PrintExceptionContinue(&e, "Runaway exception");
615-
app.handleRunawayException(QString::fromStdString(node->getWarnings().translated));
624+
app.handleRunawayException(QString::fromStdString(app.node().getWarnings().translated));
616625
} catch (...) {
617626
PrintExceptionContinue(nullptr, "Runaway exception");
618-
app.handleRunawayException(QString::fromStdString(node->getWarnings().translated));
627+
app.handleRunawayException(QString::fromStdString(app.node().getWarnings().translated));
619628
}
620629
return rv;
621630
}

src/qt/bitcoin.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#endif
1111

1212
#include <QApplication>
13+
#include <assert.h>
1314
#include <memory>
1415

1516
#include <interfaces/node.h>
@@ -20,6 +21,7 @@ class NetworkStyle;
2021
class OptionsModel;
2122
class PaymentServer;
2223
class PlatformStyle;
24+
class SplashScreen;
2325
class WalletController;
2426
class WalletModel;
2527

@@ -54,7 +56,7 @@ class BitcoinApplication: public QApplication
5456
{
5557
Q_OBJECT
5658
public:
57-
explicit BitcoinApplication(interfaces::Node& node);
59+
explicit BitcoinApplication();
5860
~BitcoinApplication();
5961

6062
#ifdef ENABLE_WALLET
@@ -88,6 +90,9 @@ class BitcoinApplication: public QApplication
8890
/// Setup platform style
8991
void setupPlatformStyle();
9092

93+
interfaces::Node& node() const { assert(m_node); return *m_node; }
94+
void setNode(interfaces::Node& node);
95+
9196
public Q_SLOTS:
9297
void initializeResult(bool success, interfaces::BlockAndHeaderTipInfo tip_info);
9398
void shutdownResult();
@@ -102,7 +107,6 @@ public Q_SLOTS:
102107

103108
private:
104109
QThread *coreThread;
105-
interfaces::Node& m_node;
106110
OptionsModel *optionsModel;
107111
ClientModel *clientModel;
108112
BitcoinGUI *window;
@@ -114,6 +118,8 @@ public Q_SLOTS:
114118
int returnValue;
115119
const PlatformStyle *platformStyle;
116120
std::unique_ptr<QWidget> shutdownWindow;
121+
SplashScreen* m_splash = nullptr;
122+
interfaces::Node* m_node = nullptr;
117123

118124
void startThread();
119125
};

src/qt/optionsmodel.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ const char *DEFAULT_GUI_PROXY_HOST = "127.0.0.1";
2727

2828
static const QString GetDefaultProxyAddress();
2929

30-
OptionsModel::OptionsModel(interfaces::Node& node, QObject *parent, bool resetSettings) :
31-
QAbstractListModel(parent), m_node(node)
30+
OptionsModel::OptionsModel(QObject *parent, bool resetSettings) :
31+
QAbstractListModel(parent)
3232
{
3333
Init(resetSettings);
3434
}
@@ -353,7 +353,7 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in
353353
break;
354354
case MapPortUPnP: // core option - can be changed on-the-fly
355355
settings.setValue("fUseUPnP", value.toBool());
356-
m_node.mapPort(value.toBool());
356+
node().mapPort(value.toBool());
357357
break;
358358
case MinimizeOnClose:
359359
fMinimizeOnClose = value.toBool();

src/qt/optionsmodel.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include <QAbstractListModel>
1313

14+
#include <assert.h>
15+
1416
namespace interfaces {
1517
class Node;
1618
}
@@ -39,7 +41,7 @@ class OptionsModel : public QAbstractListModel
3941
Q_OBJECT
4042

4143
public:
42-
explicit OptionsModel(interfaces::Node& node, QObject *parent = nullptr, bool resetSettings = false);
44+
explicit OptionsModel(QObject *parent = nullptr, bool resetSettings = false);
4345

4446
enum OptionID {
4547
StartAtStartup, // bool
@@ -92,10 +94,11 @@ class OptionsModel : public QAbstractListModel
9294
void setRestartRequired(bool fRequired);
9395
bool isRestartRequired() const;
9496

95-
interfaces::Node& node() const { return m_node; }
97+
interfaces::Node& node() const { assert(m_node); return *m_node; }
98+
void setNode(interfaces::Node& node) { assert(!m_node); m_node = &node; }
9699

97100
private:
98-
interfaces::Node& m_node;
101+
interfaces::Node* m_node = nullptr;
99102
/* Qt-only settings */
100103
bool fHideTrayIcon;
101104
bool fMinimizeToTray;

src/qt/splashscreen.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
#include <QScreen>
2525

2626

27-
SplashScreen::SplashScreen(interfaces::Node& node, Qt::WindowFlags f, const NetworkStyle *networkStyle) :
28-
QWidget(nullptr, f), curAlignment(0), m_node(node)
27+
SplashScreen::SplashScreen(Qt::WindowFlags f, const NetworkStyle *networkStyle) :
28+
QWidget(nullptr, f), curAlignment(0)
2929
{
3030
// set reference point, paddings
3131
int paddingRight = 50;
@@ -124,22 +124,33 @@ SplashScreen::SplashScreen(interfaces::Node& node, Qt::WindowFlags f, const Netw
124124
setFixedSize(r.size());
125125
move(QGuiApplication::primaryScreen()->geometry().center() - r.center());
126126

127-
subscribeToCoreSignals();
128127
installEventFilter(this);
129128

130129
GUIUtil::handleCloseWindowShortcut(this);
131130
}
132131

133132
SplashScreen::~SplashScreen()
134133
{
135-
unsubscribeFromCoreSignals();
134+
if (m_node) unsubscribeFromCoreSignals();
135+
}
136+
137+
void SplashScreen::setNode(interfaces::Node& node)
138+
{
139+
assert(!m_node);
140+
m_node = &node;
141+
subscribeToCoreSignals();
142+
}
143+
144+
void SplashScreen::shutdown()
145+
{
146+
if (m_node) m_node->startShutdown();
136147
}
137148

138149
bool SplashScreen::eventFilter(QObject * obj, QEvent * ev) {
139150
if (ev->type() == QEvent::KeyPress) {
140151
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
141152
if (keyEvent->key() == Qt::Key_Q) {
142-
m_node.startShutdown();
153+
shutdown();
143154
}
144155
}
145156
return QObject::eventFilter(obj, ev);
@@ -183,10 +194,10 @@ void SplashScreen::ConnectWallet(std::unique_ptr<interfaces::Wallet> wallet)
183194
void SplashScreen::subscribeToCoreSignals()
184195
{
185196
// Connect signals to client
186-
m_handler_init_message = m_node.handleInitMessage(std::bind(InitMessage, this, std::placeholders::_1));
187-
m_handler_show_progress = m_node.handleShowProgress(std::bind(ShowProgress, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
197+
m_handler_init_message = m_node->handleInitMessage(std::bind(InitMessage, this, std::placeholders::_1));
198+
m_handler_show_progress = m_node->handleShowProgress(std::bind(ShowProgress, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
188199
#ifdef ENABLE_WALLET
189-
m_handler_load_wallet = m_node.handleLoadWallet([this](std::unique_ptr<interfaces::Wallet> wallet) { ConnectWallet(std::move(wallet)); });
200+
m_handler_load_wallet = m_node->handleLoadWallet([this](std::unique_ptr<interfaces::Wallet> wallet) { ConnectWallet(std::move(wallet)); });
190201
#endif
191202
}
192203

@@ -221,6 +232,6 @@ void SplashScreen::paintEvent(QPaintEvent *event)
221232

222233
void SplashScreen::closeEvent(QCloseEvent *event)
223234
{
224-
m_node.startShutdown(); // allows an "emergency" shutdown during startup
235+
shutdown(); // allows an "emergency" shutdown during startup
225236
event->ignore();
226237
}

src/qt/splashscreen.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ class SplashScreen : public QWidget
2828
Q_OBJECT
2929

3030
public:
31-
explicit SplashScreen(interfaces::Node& node, Qt::WindowFlags f, const NetworkStyle *networkStyle);
31+
explicit SplashScreen(Qt::WindowFlags f, const NetworkStyle *networkStyle);
3232
~SplashScreen();
33+
void setNode(interfaces::Node& node);
3334

3435
protected:
3536
void paintEvent(QPaintEvent *event) override;
@@ -50,6 +51,8 @@ public Q_SLOTS:
5051
void subscribeToCoreSignals();
5152
/** Disconnect core signals to splash screen */
5253
void unsubscribeFromCoreSignals();
54+
/** Initiate shutdown */
55+
void shutdown();
5356
/** Connect wallet signals to splash screen */
5457
void ConnectWallet(std::unique_ptr<interfaces::Wallet> wallet);
5558

@@ -58,7 +61,7 @@ public Q_SLOTS:
5861
QColor curColor;
5962
int curAlignment;
6063

61-
interfaces::Node& m_node;
64+
interfaces::Node* m_node = nullptr;
6265
std::unique_ptr<interfaces::Handler> m_handler_init_message;
6366
std::unique_ptr<interfaces::Handler> m_handler_show_progress;
6467
std::unique_ptr<interfaces::Handler> m_handler_load_wallet;

src/qt/test/addressbooktests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void TestAddAddressesToSendBook(interfaces::Node& node)
108108

109109
// Initialize relevant QT models.
110110
std::unique_ptr<const PlatformStyle> platformStyle(PlatformStyle::instantiate("other"));
111-
OptionsModel optionsModel(node);
111+
OptionsModel optionsModel;
112112
ClientModel clientModel(node, &optionsModel);
113113
AddWallet(wallet);
114114
WalletModel walletModel(interfaces::MakeWallet(wallet), clientModel, platformStyle.get());

src/qt/test/test_main.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ int main(int argc, char* argv[])
6868

6969
// Don't remove this, it's needed to access
7070
// QApplication:: and QCoreApplication:: in the tests
71-
BitcoinApplication app(*node);
71+
BitcoinApplication app;
72+
app.setNode(*node);
7273
app.setApplicationName("Bitcoin-Qt-test");
7374

74-
node->context()->args = &gArgs; // Make gArgs available in the NodeContext
75+
app.node().context()->args = &gArgs; // Make gArgs available in the NodeContext
7576
AppTests app_tests(app);
7677
if (QTest::qExec(&app_tests) != 0) {
7778
fInvalid = true;
@@ -80,7 +81,7 @@ int main(int argc, char* argv[])
8081
if (QTest::qExec(&test1) != 0) {
8182
fInvalid = true;
8283
}
83-
RPCNestedTests test3(*node);
84+
RPCNestedTests test3(app.node());
8485
if (QTest::qExec(&test3) != 0) {
8586
fInvalid = true;
8687
}
@@ -89,11 +90,11 @@ int main(int argc, char* argv[])
8990
fInvalid = true;
9091
}
9192
#ifdef ENABLE_WALLET
92-
WalletTests test5(*node);
93+
WalletTests test5(app.node());
9394
if (QTest::qExec(&test5) != 0) {
9495
fInvalid = true;
9596
}
96-
AddressBookTests test6(*node);
97+
AddressBookTests test6(app.node());
9798
if (QTest::qExec(&test6) != 0) {
9899
fInvalid = true;
99100
}

src/qt/test/wallettests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ void TestGUI(interfaces::Node& node)
163163
std::unique_ptr<const PlatformStyle> platformStyle(PlatformStyle::instantiate("other"));
164164
SendCoinsDialog sendCoinsDialog(platformStyle.get());
165165
TransactionView transactionView(platformStyle.get());
166-
OptionsModel optionsModel(node);
166+
OptionsModel optionsModel;
167167
ClientModel clientModel(node, &optionsModel);
168168
AddWallet(wallet);
169169
WalletModel walletModel(interfaces::MakeWallet(wallet), clientModel, platformStyle.get());

0 commit comments

Comments
 (0)