Skip to content

Commit 582daf6

Browse files
ryanofskyjnewbery
authored andcommitted
Remove direct bitcoin calls from qt/rpcconsole.cpp
1 parent 3034a46 commit 582daf6

File tree

6 files changed

+137
-67
lines changed

6 files changed

+137
-67
lines changed

src/interface/node.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <netaddress.h>
1616
#include <netbase.h>
1717
#include <primitives/block.h>
18+
#include <rpc/server.h>
1819
#include <scheduler.h>
1920
#include <sync.h>
2021
#include <txmempool.h>
@@ -34,6 +35,7 @@
3435

3536
#include <atomic>
3637
#include <boost/thread/thread.hpp>
38+
#include <univalue.h>
3739

3840
class CWallet;
3941

@@ -114,6 +116,29 @@ class NodeImpl : public Node
114116
}
115117
return false;
116118
}
119+
bool ban(const CNetAddr& net_addr, BanReason reason, int64_t ban_time_offset) override
120+
{
121+
if (g_connman) {
122+
g_connman->Ban(net_addr, reason, ban_time_offset);
123+
return true;
124+
}
125+
return false;
126+
}
127+
bool unban(const CSubNet& ip) override
128+
{
129+
if (g_connman) {
130+
g_connman->Unban(ip);
131+
return true;
132+
}
133+
return false;
134+
}
135+
bool disconnect(NodeId id) override
136+
{
137+
if (g_connman) {
138+
return g_connman->DisconnectNode(id);
139+
}
140+
return false;
141+
}
117142
int64_t getTotalBytesRecv() override { return g_connman ? g_connman->GetTotalBytesRecv() : 0; }
118143
int64_t getTotalBytesSent() override { return g_connman ? g_connman->GetTotalBytesSent() : 0; }
119144
size_t getMempoolSize() override { return ::mempool.size(); }
@@ -160,6 +185,17 @@ class NodeImpl : public Node
160185
}
161186
}
162187
bool getNetworkActive() override { return g_connman && g_connman->GetNetworkActive(); }
188+
UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) override
189+
{
190+
JSONRPCRequest req;
191+
req.params = params;
192+
req.strMethod = command;
193+
req.URI = uri;
194+
return ::tableRPC.execute(req);
195+
}
196+
std::vector<std::string> listRpcCommands() override { return ::tableRPC.listCommands(); }
197+
void rpcSetTimerInterfaceIfUnset(RPCTimerInterface* iface) override { RPCSetTimerInterfaceIfUnset(iface); }
198+
void rpcUnsetTimerInterface(RPCTimerInterface* iface) override { RPCUnsetTimerInterface(iface); }
163199
std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) override
164200
{
165201
return MakeHandler(::uiInterface.InitMessage.connect(fn));

src/interface/node.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <vector>
2020

2121
class CNodeStats;
22+
class RPCTimerInterface;
23+
class UniValue;
2224
class proxyType;
2325
struct CNodeStateStats;
2426

@@ -91,6 +93,15 @@ class Node
9193
//! Get ban map entries.
9294
virtual bool getBanned(banmap_t& banmap) = 0;
9395

96+
//! Ban node.
97+
virtual bool ban(const CNetAddr& net_addr, BanReason reason, int64_t ban_time_offset) = 0;
98+
99+
//! Unban node.
100+
virtual bool unban(const CSubNet& ip) = 0;
101+
102+
//! Disconnect node.
103+
virtual bool disconnect(NodeId id) = 0;
104+
94105
//! Get total bytes recv.
95106
virtual int64_t getTotalBytesRecv() = 0;
96107

@@ -130,6 +141,18 @@ class Node
130141
//! Get network active.
131142
virtual bool getNetworkActive() = 0;
132143

144+
//! Execute rpc command.
145+
virtual UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) = 0;
146+
147+
//! List rpc commands.
148+
virtual std::vector<std::string> listRpcCommands() = 0;
149+
150+
//! Set RPC timer interface if unset.
151+
virtual void rpcSetTimerInterfaceIfUnset(RPCTimerInterface* iface) = 0;
152+
153+
//! Unset RPC timer interface.
154+
virtual void rpcUnsetTimerInterface(RPCTimerInterface* iface) = 0;
155+
133156
//! Register handler for init messages.
134157
using InitMessageFn = std::function<void(const std::string& message)>;
135158
virtual std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) = 0;

src/qt/bitcoingui.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ BitcoinGUI::BitcoinGUI(interface::Node& node, const PlatformStyle *_platformStyl
152152
setUnifiedTitleAndToolBarOnMac(true);
153153
#endif
154154

155-
rpcConsole = new RPCConsole(_platformStyle, 0);
155+
rpcConsole = new RPCConsole(node, _platformStyle, 0);
156156
helpMessageDialog = new HelpMessageDialog(node, this, false);
157157
#ifdef ENABLE_WALLET
158158
if(enableWallet)

src/qt/rpcconsole.cpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,17 @@ const QStringList historyFilter = QStringList()
8484
class RPCExecutor : public QObject
8585
{
8686
Q_OBJECT
87+
public:
88+
RPCExecutor(interface::Node& node) : m_node(node) {}
8789

8890
public Q_SLOTS:
8991
void request(const QString &command, const QString &walletID);
9092

9193
Q_SIGNALS:
9294
void reply(int category, const QString &command);
95+
96+
private:
97+
interface::Node& m_node;
9398
};
9499

95100
/** Class for handling RPC timers
@@ -141,13 +146,14 @@ class QtRPCTimerInterface: public RPCTimerInterface
141146
* - Within double quotes, only escape \c " and backslashes before a \c " or another backslash
142147
* - Within single quotes, no escaping is possible and no special interpretation takes place
143148
*
149+
* @param[in] node optional node to execute command on
144150
* @param[out] result stringified Result from the executed command(chain)
145151
* @param[in] strCommand Command line to split
146152
* @param[in] fExecute set true if you want the command to be executed
147153
* @param[out] pstrFilteredOut Command line, filtered to remove any sensitive data
148154
*/
149155

150-
bool RPCConsole::RPCParseCommandLine(std::string &strResult, const std::string &strCommand, const bool fExecute, std::string * const pstrFilteredOut, const std::string *walletID)
156+
bool RPCConsole::RPCParseCommandLine(interface::Node* node, std::string &strResult, const std::string &strCommand, const bool fExecute, std::string * const pstrFilteredOut, const std::string *walletID)
151157
{
152158
std::vector< std::vector<std::string> > stack;
153159
stack.push_back(std::vector<std::string>());
@@ -301,16 +307,17 @@ bool RPCConsole::RPCParseCommandLine(std::string &strResult, const std::string &
301307
if (fExecute) {
302308
// Convert argument list to JSON objects in method-dependent way,
303309
// and pass it along with the method name to the dispatcher.
304-
JSONRPCRequest req;
305-
req.params = RPCConvertValues(stack.back()[0], std::vector<std::string>(stack.back().begin() + 1, stack.back().end()));
306-
req.strMethod = stack.back()[0];
310+
UniValue params = RPCConvertValues(stack.back()[0], std::vector<std::string>(stack.back().begin() + 1, stack.back().end()));
311+
std::string method = stack.back()[0];
312+
std::string uri;
307313
#ifdef ENABLE_WALLET
308314
if (walletID && !walletID->empty()) {
309315
QByteArray encodedName = QUrl::toPercentEncoding(QString::fromStdString(*walletID));
310-
req.URI = "/wallet/"+std::string(encodedName.constData(), encodedName.length());
316+
uri = "/wallet/"+std::string(encodedName.constData(), encodedName.length());
311317
}
312318
#endif
313-
lastResult = tableRPC.execute(req);
319+
assert(node);
320+
lastResult = node->executeRpc(method, params, uri);
314321
}
315322

316323
state = STATE_COMMAND_EXECUTED;
@@ -417,7 +424,7 @@ void RPCExecutor::request(const QString &command, const QString &walletID)
417424
return;
418425
}
419426
std::string wallet_id = walletID.toStdString();
420-
if(!RPCConsole::RPCExecuteCommandLine(result, executableCommand, nullptr, &wallet_id))
427+
if(!RPCConsole::RPCExecuteCommandLine(m_node, result, executableCommand, nullptr, &wallet_id))
421428
{
422429
Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \""));
423430
return;
@@ -444,8 +451,9 @@ void RPCExecutor::request(const QString &command, const QString &walletID)
444451
}
445452
}
446453

447-
RPCConsole::RPCConsole(const PlatformStyle *_platformStyle, QWidget *parent) :
454+
RPCConsole::RPCConsole(interface::Node& node, const PlatformStyle *_platformStyle, QWidget *parent) :
448455
QWidget(parent),
456+
m_node(node),
449457
ui(new Ui::RPCConsole),
450458
clientModel(0),
451459
historyPtr(0),
@@ -494,7 +502,7 @@ RPCConsole::RPCConsole(const PlatformStyle *_platformStyle, QWidget *parent) :
494502
rpcTimerInterface = new QtRPCTimerInterface();
495503
// avoid accidentally overwriting an existing, non QTThread
496504
// based timer interface
497-
RPCSetTimerInterfaceIfUnset(rpcTimerInterface);
505+
m_node.rpcSetTimerInterfaceIfUnset(rpcTimerInterface);
498506

499507
setTrafficGraphRange(INITIAL_TRAFFIC_GRAPH_MINS);
500508

@@ -509,7 +517,7 @@ RPCConsole::~RPCConsole()
509517
{
510518
QSettings settings;
511519
settings.setValue("RPCConsoleWindowGeometry", saveGeometry());
512-
RPCUnsetTimerInterface(rpcTimerInterface);
520+
m_node.rpcUnsetTimerInterface(rpcTimerInterface);
513521
delete rpcTimerInterface;
514522
delete ui;
515523
}
@@ -669,7 +677,7 @@ void RPCConsole::setClientModel(ClientModel *model)
669677

670678
//Setup autocomplete and attach it
671679
QStringList wordList;
672-
std::vector<std::string> commandList = tableRPC.listCommands();
680+
std::vector<std::string> commandList = m_node.listRpcCommands();
673681
for (size_t i = 0; i < commandList.size(); ++i)
674682
{
675683
wordList << commandList[i].c_str();
@@ -884,7 +892,7 @@ void RPCConsole::on_lineEdit_returnPressed()
884892
std::string strFilteredCmd;
885893
try {
886894
std::string dummy;
887-
if (!RPCParseCommandLine(dummy, cmd.toStdString(), false, &strFilteredCmd)) {
895+
if (!RPCParseCommandLine(nullptr, dummy, cmd.toStdString(), false, &strFilteredCmd)) {
888896
// Failed to parse command, so we cannot even filter it for the history
889897
throw std::runtime_error("Invalid command line");
890898
}
@@ -957,7 +965,7 @@ void RPCConsole::browseHistory(int offset)
957965

958966
void RPCConsole::startExecutor()
959967
{
960-
RPCExecutor *executor = new RPCExecutor();
968+
RPCExecutor *executor = new RPCExecutor(m_node);
961969
executor->moveToThread(&thread);
962970

963971
// Replies from executor object must go to this object
@@ -1183,24 +1191,21 @@ void RPCConsole::showBanTableContextMenu(const QPoint& point)
11831191

11841192
void RPCConsole::disconnectSelectedNode()
11851193
{
1186-
if(!g_connman)
1187-
return;
1188-
11891194
// Get selected peer addresses
11901195
QList<QModelIndex> nodes = GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId);
11911196
for(int i = 0; i < nodes.count(); i++)
11921197
{
11931198
// Get currently selected peer address
11941199
NodeId id = nodes.at(i).data().toLongLong();
11951200
// Find the node, disconnect it and clear the selected node
1196-
if(g_connman->DisconnectNode(id))
1201+
if(m_node.disconnect(id))
11971202
clearSelectedNode();
11981203
}
11991204
}
12001205

12011206
void RPCConsole::banSelectedNode(int bantime)
12021207
{
1203-
if (!clientModel || !g_connman)
1208+
if (!clientModel)
12041209
return;
12051210

12061211
// Get selected peer addresses
@@ -1218,7 +1223,7 @@ void RPCConsole::banSelectedNode(int bantime)
12181223
// Find possible nodes, ban it and clear the selected node
12191224
const CNodeCombinedStats *stats = clientModel->getPeerTableModel()->getNodeStats(detailNodeRow);
12201225
if(stats) {
1221-
g_connman->Ban(stats->nodeStats.addr, BanReasonManuallyAdded, bantime);
1226+
m_node.ban(stats->nodeStats.addr, BanReasonManuallyAdded, bantime);
12221227
}
12231228
}
12241229
clearSelectedNode();
@@ -1239,9 +1244,8 @@ void RPCConsole::unbanSelectedNode()
12391244
CSubNet possibleSubnet;
12401245

12411246
LookupSubNet(strNode.toStdString().c_str(), possibleSubnet);
1242-
if (possibleSubnet.IsValid() && g_connman)
1247+
if (possibleSubnet.IsValid() && m_node.unban(possibleSubnet))
12431248
{
1244-
g_connman->Unban(possibleSubnet);
12451249
clientModel->getBanTableModel()->refresh();
12461250
}
12471251
}

src/qt/rpcconsole.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class PlatformStyle;
1919
class RPCTimerInterface;
2020
class WalletModel;
2121

22+
namespace interface {
23+
class Node;
24+
}
25+
2226
namespace Ui {
2327
class RPCConsole;
2428
}
@@ -34,12 +38,12 @@ class RPCConsole: public QWidget
3438
Q_OBJECT
3539

3640
public:
37-
explicit RPCConsole(const PlatformStyle *platformStyle, QWidget *parent);
41+
explicit RPCConsole(interface::Node& node, const PlatformStyle *platformStyle, QWidget *parent);
3842
~RPCConsole();
3943

40-
static bool RPCParseCommandLine(std::string &strResult, const std::string &strCommand, bool fExecute, std::string * const pstrFilteredOut = nullptr, const std::string *walletID = nullptr);
41-
static bool RPCExecuteCommandLine(std::string &strResult, const std::string &strCommand, std::string * const pstrFilteredOut = nullptr, const std::string *walletID = nullptr) {
42-
return RPCParseCommandLine(strResult, strCommand, true, pstrFilteredOut, walletID);
44+
static bool RPCParseCommandLine(interface::Node* node, std::string &strResult, const std::string &strCommand, bool fExecute, std::string * const pstrFilteredOut = nullptr, const std::string *walletID = nullptr);
45+
static bool RPCExecuteCommandLine(interface::Node& node, std::string &strResult, const std::string &strCommand, std::string * const pstrFilteredOut = nullptr, const std::string *walletID = nullptr) {
46+
return RPCParseCommandLine(&node, strResult, strCommand, true, pstrFilteredOut, walletID);
4347
}
4448

4549
void setClientModel(ClientModel *model);
@@ -140,6 +144,7 @@ public Q_SLOTS:
140144

141145
};
142146

147+
interface::Node& m_node;
143148
Ui::RPCConsole *ui;
144149
ClientModel *clientModel;
145150
QStringList history;

0 commit comments

Comments
 (0)