Skip to content

Commit c0f2756

Browse files
ryanofskyjnewbery
authored andcommitted
Remove direct bitcoin calls from qt/optionsmodel.cpp
1 parent 71e0d90 commit c0f2756

File tree

7 files changed

+57
-27
lines changed

7 files changed

+57
-27
lines changed

src/interface/node.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#include <chainparams.h>
88
#include <init.h>
99
#include <interface/handler.h>
10+
#include <net.h>
11+
#include <netaddress.h>
12+
#include <netbase.h>
1013
#include <scheduler.h>
1114
#include <ui_interface.h>
1215
#include <util.h>
@@ -24,6 +27,8 @@ class NodeImpl : public Node
2427
gArgs.ParseParameters(argc, argv);
2528
}
2629
void readConfigFile(const std::string& conf_path) override { gArgs.ReadConfigFile(conf_path); }
30+
bool softSetArg(const std::string& arg, const std::string& value) override { return gArgs.SoftSetArg(arg, value); }
31+
bool softSetBoolArg(const std::string& arg, bool value) override { return gArgs.SoftSetBoolArg(arg, value); }
2732
void selectParams(const std::string& network) override { SelectParams(network); }
2833
void initLogging() override { InitLogging(); }
2934
void initParameterInteraction() override { InitParameterInteraction(); }
@@ -40,6 +45,16 @@ class NodeImpl : public Node
4045
Shutdown();
4146
}
4247
void startShutdown() override { StartShutdown(); }
48+
void mapPort(bool use_upnp) override
49+
{
50+
if (use_upnp) {
51+
StartMapPort();
52+
} else {
53+
InterruptMapPort();
54+
StopMapPort();
55+
}
56+
}
57+
bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); }
4358
std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) override
4459
{
4560
return MakeHandler(::uiInterface.InitMessage.connect(fn));

src/interface/node.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
#ifndef BITCOIN_INTERFACE_NODE_H
66
#define BITCOIN_INTERFACE_NODE_H
77

8+
#include <netaddress.h> // For Network
9+
810
#include <functional>
911
#include <memory>
1012
#include <string>
1113

14+
class proxyType;
15+
1216
namespace interface {
1317

1418
class Handler;
@@ -22,6 +26,12 @@ class Node
2226
//! Set command line arguments.
2327
virtual void parseParameters(int argc, const char* const argv[]) = 0;
2428

29+
//! Set a command line argument if it doesn't already have a value
30+
virtual bool softSetArg(const std::string& arg, const std::string& value) = 0;
31+
32+
//! Set a command line boolean argument if it doesn't already have a value
33+
virtual bool softSetBoolArg(const std::string& arg, bool value) = 0;
34+
2535
//! Load settings from configuration file.
2636
virtual void readConfigFile(const std::string& conf_path) = 0;
2737

@@ -49,6 +59,12 @@ class Node
4959
//! Start shutdown.
5060
virtual void startShutdown() = 0;
5161

62+
//! Map port.
63+
virtual void mapPort(bool use_upnp) = 0;
64+
65+
//! Get proxy.
66+
virtual bool getProxy(Network net, proxyType& proxy_info) = 0;
67+
5268
//! Register handler for init messages.
5369
using InitMessageFn = std::function<void(const std::string& message)>;
5470
virtual std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) = 0;

src/qt/bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ void BitcoinApplication::createPaymentServer()
362362

363363
void BitcoinApplication::createOptionsModel(bool resetSettings)
364364
{
365-
optionsModel = new OptionsModel(nullptr, resetSettings);
365+
optionsModel = new OptionsModel(m_node, nullptr, resetSettings);
366366
}
367367

368368
void BitcoinApplication::createWindow(const NetworkStyle *networkStyle)

src/qt/optionsmodel.cpp

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,21 @@
1111
#include <qt/bitcoinunits.h>
1212
#include <qt/guiutil.h>
1313

14-
#include <init.h>
14+
#include <interface/node.h>
1515
#include <validation.h> // For DEFAULT_SCRIPTCHECK_THREADS
1616
#include <net.h>
1717
#include <netbase.h>
1818
#include <txdb.h> // for -dbcache defaults
1919
#include <qt/intro.h>
2020

21-
#ifdef ENABLE_WALLET
22-
#include <wallet/wallet.h>
23-
#include <wallet/walletdb.h>
24-
#endif
25-
2621
#include <QNetworkProxy>
2722
#include <QSettings>
2823
#include <QStringList>
2924

3025
const char *DEFAULT_GUI_PROXY_HOST = "127.0.0.1";
3126

32-
OptionsModel::OptionsModel(QObject *parent, bool resetSettings) :
33-
QAbstractListModel(parent)
27+
OptionsModel::OptionsModel(interface::Node& node, QObject *parent, bool resetSettings) :
28+
QAbstractListModel(parent), m_node(node)
3429
{
3530
Init(resetSettings);
3631
}
@@ -93,12 +88,12 @@ void OptionsModel::Init(bool resetSettings)
9388
// Main
9489
if (!settings.contains("nDatabaseCache"))
9590
settings.setValue("nDatabaseCache", (qint64)nDefaultDbCache);
96-
if (!gArgs.SoftSetArg("-dbcache", settings.value("nDatabaseCache").toString().toStdString()))
91+
if (!m_node.softSetArg("-dbcache", settings.value("nDatabaseCache").toString().toStdString()))
9792
addOverriddenOption("-dbcache");
9893

9994
if (!settings.contains("nThreadsScriptVerif"))
10095
settings.setValue("nThreadsScriptVerif", DEFAULT_SCRIPTCHECK_THREADS);
101-
if (!gArgs.SoftSetArg("-par", settings.value("nThreadsScriptVerif").toString().toStdString()))
96+
if (!m_node.softSetArg("-par", settings.value("nThreadsScriptVerif").toString().toStdString()))
10297
addOverriddenOption("-par");
10398

10499
if (!settings.contains("strDataDir"))
@@ -108,27 +103,27 @@ void OptionsModel::Init(bool resetSettings)
108103
#ifdef ENABLE_WALLET
109104
if (!settings.contains("bSpendZeroConfChange"))
110105
settings.setValue("bSpendZeroConfChange", true);
111-
if (!gArgs.SoftSetBoolArg("-spendzeroconfchange", settings.value("bSpendZeroConfChange").toBool()))
106+
if (!m_node.softSetBoolArg("-spendzeroconfchange", settings.value("bSpendZeroConfChange").toBool()))
112107
addOverriddenOption("-spendzeroconfchange");
113108
#endif
114109

115110
// Network
116111
if (!settings.contains("fUseUPnP"))
117112
settings.setValue("fUseUPnP", DEFAULT_UPNP);
118-
if (!gArgs.SoftSetBoolArg("-upnp", settings.value("fUseUPnP").toBool()))
113+
if (!m_node.softSetBoolArg("-upnp", settings.value("fUseUPnP").toBool()))
119114
addOverriddenOption("-upnp");
120115

121116
if (!settings.contains("fListen"))
122117
settings.setValue("fListen", DEFAULT_LISTEN);
123-
if (!gArgs.SoftSetBoolArg("-listen", settings.value("fListen").toBool()))
118+
if (!m_node.softSetBoolArg("-listen", settings.value("fListen").toBool()))
124119
addOverriddenOption("-listen");
125120

126121
if (!settings.contains("fUseProxy"))
127122
settings.setValue("fUseProxy", false);
128123
if (!settings.contains("addrProxy"))
129124
settings.setValue("addrProxy", QString("%1:%2").arg(DEFAULT_GUI_PROXY_HOST, DEFAULT_GUI_PROXY_PORT));
130125
// Only try to set -proxy, if user has enabled fUseProxy
131-
if (settings.value("fUseProxy").toBool() && !gArgs.SoftSetArg("-proxy", settings.value("addrProxy").toString().toStdString()))
126+
if (settings.value("fUseProxy").toBool() && !m_node.softSetArg("-proxy", settings.value("addrProxy").toString().toStdString()))
132127
addOverriddenOption("-proxy");
133128
else if(!settings.value("fUseProxy").toBool() && !gArgs.GetArg("-proxy", "").empty())
134129
addOverriddenOption("-proxy");
@@ -138,15 +133,15 @@ void OptionsModel::Init(bool resetSettings)
138133
if (!settings.contains("addrSeparateProxyTor"))
139134
settings.setValue("addrSeparateProxyTor", QString("%1:%2").arg(DEFAULT_GUI_PROXY_HOST, DEFAULT_GUI_PROXY_PORT));
140135
// Only try to set -onion, if user has enabled fUseSeparateProxyTor
141-
if (settings.value("fUseSeparateProxyTor").toBool() && !gArgs.SoftSetArg("-onion", settings.value("addrSeparateProxyTor").toString().toStdString()))
136+
if (settings.value("fUseSeparateProxyTor").toBool() && !m_node.softSetArg("-onion", settings.value("addrSeparateProxyTor").toString().toStdString()))
142137
addOverriddenOption("-onion");
143138
else if(!settings.value("fUseSeparateProxyTor").toBool() && !gArgs.GetArg("-onion", "").empty())
144139
addOverriddenOption("-onion");
145140

146141
// Display
147142
if (!settings.contains("language"))
148143
settings.setValue("language", "");
149-
if (!gArgs.SoftSetArg("-lang", settings.value("language").toString().toStdString()))
144+
if (!m_node.softSetArg("-lang", settings.value("language").toString().toStdString()))
150145
addOverriddenOption("-lang");
151146

152147
language = settings.value("language").toString();
@@ -315,12 +310,7 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in
315310
break;
316311
case MapPortUPnP: // core option - can be changed on-the-fly
317312
settings.setValue("fUseUPnP", value.toBool());
318-
if (value.toBool()) {
319-
StartMapPort();
320-
} else {
321-
InterruptMapPort();
322-
StopMapPort();
323-
}
313+
m_node.mapPort(value.toBool());
324314
break;
325315
case MinimizeOnClose:
326316
fMinimizeOnClose = value.toBool();
@@ -453,7 +443,7 @@ bool OptionsModel::getProxySettings(QNetworkProxy& proxy) const
453443
// Directly query current base proxy, because
454444
// GUI settings can be overridden with -proxy.
455445
proxyType curProxy;
456-
if (GetProxy(NET_IPV4, curProxy)) {
446+
if (m_node.getProxy(NET_IPV4, curProxy)) {
457447
proxy.setType(QNetworkProxy::Socks5Proxy);
458448
proxy.setHostName(QString::fromStdString(curProxy.proxy.ToStringIP()));
459449
proxy.setPort(curProxy.proxy.GetPort());

src/qt/optionsmodel.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
#include <QAbstractListModel>
1111

12+
namespace interface {
13+
class Node;
14+
}
15+
1216
QT_BEGIN_NAMESPACE
1317
class QNetworkProxy;
1418
QT_END_NAMESPACE
@@ -27,7 +31,7 @@ class OptionsModel : public QAbstractListModel
2731
Q_OBJECT
2832

2933
public:
30-
explicit OptionsModel(QObject *parent = 0, bool resetSettings = false);
34+
explicit OptionsModel(interface::Node& node, QObject *parent = 0, bool resetSettings = false);
3135

3236
enum OptionID {
3337
StartAtStartup, // bool
@@ -76,6 +80,7 @@ class OptionsModel : public QAbstractListModel
7680
bool isRestartRequired() const;
7781

7882
private:
83+
interface::Node& m_node;
7984
/* Qt-only settings */
8085
bool fHideTrayIcon;
8186
bool fMinimizeToTray;

src/qt/test/paymentservertests.cpp

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

1010
#include <amount.h>
1111
#include <chainparams.h>
12+
#include <interface/node.h>
1213
#include <random.h>
1314
#include <script/script.h>
1415
#include <script/standard.h>
@@ -66,7 +67,8 @@ static SendCoinsRecipient handleRequest(PaymentServer* server, std::vector<unsig
6667
void PaymentServerTests::paymentServerTests()
6768
{
6869
SelectParams(CBaseChainParams::MAIN);
69-
OptionsModel optionsModel;
70+
auto node = interface::MakeNode();
71+
OptionsModel optionsModel(*node);
7072
PaymentServer* server = new PaymentServer(nullptr, false);
7173
X509_STORE* caStore = X509_STORE_new();
7274
X509_STORE_add_cert(caStore, parse_b64der_cert(caCert1_BASE64));

src/qt/test/wallettests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <qt/test/wallettests.h>
22

3+
#include <interface/node.h>
34
#include <qt/bitcoinamountfield.h>
45
#include <qt/callback.h>
56
#include <qt/optionsmodel.h>
@@ -175,7 +176,8 @@ void TestGUI()
175176
std::unique_ptr<const PlatformStyle> platformStyle(PlatformStyle::instantiate("other"));
176177
SendCoinsDialog sendCoinsDialog(platformStyle.get());
177178
TransactionView transactionView(platformStyle.get());
178-
OptionsModel optionsModel;
179+
auto node = interface::MakeNode();
180+
OptionsModel optionsModel(*node);
179181
WalletModel walletModel(platformStyle.get(), &wallet, &optionsModel);
180182
sendCoinsDialog.setModel(&walletModel);
181183
transactionView.setModel(&walletModel);

0 commit comments

Comments
 (0)