Skip to content

Commit b2b33d9

Browse files
jonasschnelliluke-jr
authored andcommitted
Overhaul network activity toggle
- Rename RPC command "togglenetwork" to "setnetworkactive (true|false)" - Add simple test case - GUI toggle added to connections icon in statusbar
1 parent 32efa79 commit b2b33d9

File tree

9 files changed

+75
-14
lines changed

9 files changed

+75
-14
lines changed

contrib/debian/copyright

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Files: src/qt/res/icons/add.png
3434
src/qt/res/icons/info.png
3535
src/qt/res/icons/key.png
3636
src/qt/res/icons/lock_*.png
37+
src/qt/res/icons/network_disabled.png
3738
src/qt/res/icons/open.png
3839
src/qt/res/icons/overview.png
3940
src/qt/res/icons/quit.png

src/Makefile.qt.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ RES_ICONS = \
264264
qt/res/icons/key.png \
265265
qt/res/icons/lock_closed.png \
266266
qt/res/icons/lock_open.png \
267+
qt/res/icons/network_disabled.png \
267268
qt/res/icons/open.png \
268269
qt/res/icons/overview.png \
269270
qt/res/icons/quit.png \

src/qt/bitcoin.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<file alias="transaction_abandoned">res/icons/transaction_abandoned.png</file>
5353
<file alias="hd_enabled">res/icons/hd_enabled.png</file>
5454
<file alias="hd_disabled">res/icons/hd_disabled.png</file>
55+
<file alias="network_disabled">res/icons/network_disabled.png</file>
5556
</qresource>
5657
<qresource prefix="/movies">
5758
<file alias="spinner-000">res/movies/spinner-000.png</file>

src/qt/bitcoingui.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ BitcoinGUI::BitcoinGUI(const PlatformStyle *_platformStyle, const NetworkStyle *
8383
unitDisplayControl(0),
8484
labelWalletEncryptionIcon(0),
8585
labelWalletHDStatusIcon(0),
86-
labelConnectionsIcon(0),
86+
connectionsControl(0),
8787
labelBlocksIcon(0),
8888
progressBarLabel(0),
8989
progressBar(0),
@@ -195,7 +195,7 @@ BitcoinGUI::BitcoinGUI(const PlatformStyle *_platformStyle, const NetworkStyle *
195195
unitDisplayControl = new UnitDisplayStatusBarControl(platformStyle);
196196
labelWalletEncryptionIcon = new QLabel();
197197
labelWalletHDStatusIcon = new QLabel();
198-
labelConnectionsIcon = new QLabel();
198+
connectionsControl = new NetworkToggleStatusBarControl();
199199
labelBlocksIcon = new QLabel();
200200
if(enableWallet)
201201
{
@@ -206,7 +206,7 @@ BitcoinGUI::BitcoinGUI(const PlatformStyle *_platformStyle, const NetworkStyle *
206206
frameBlocksLayout->addWidget(labelWalletHDStatusIcon);
207207
}
208208
frameBlocksLayout->addStretch();
209-
frameBlocksLayout->addWidget(labelConnectionsIcon);
209+
frameBlocksLayout->addWidget(connectionsControl);
210210
frameBlocksLayout->addStretch();
211211
frameBlocksLayout->addWidget(labelBlocksIcon);
212212
frameBlocksLayout->addStretch();
@@ -480,6 +480,7 @@ void BitcoinGUI::setClientModel(ClientModel *_clientModel)
480480
}
481481
#endif // ENABLE_WALLET
482482
unitDisplayControl->setOptionsModel(_clientModel->getOptionsModel());
483+
connectionsControl->setClientModel(_clientModel);
483484

484485
OptionsModel* optionsModel = _clientModel->getOptionsModel();
485486
if(optionsModel)
@@ -699,13 +700,15 @@ void BitcoinGUI::updateNetworkState()
699700
case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
700701
default: icon = ":/icons/connect_4"; break;
701702
}
702-
labelConnectionsIcon->setPixmap(platformStyle->SingleColorIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
703703

704704
if (clientModel->getNetworkActive()) {
705-
labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
705+
connectionsControl->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
706706
} else {
707-
labelConnectionsIcon->setToolTip(tr("Network activity disabled"));
707+
connectionsControl->setToolTip(tr("Network activity disabled"));
708+
icon = ":/icons/network_disabled";
708709
}
710+
711+
connectionsControl->setPixmap(platformStyle->SingleColorIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
709712
}
710713

711714
void BitcoinGUI::setNumConnections(int count)
@@ -1220,3 +1223,18 @@ void UnitDisplayStatusBarControl::onMenuSelection(QAction* action)
12201223
optionsModel->setDisplayUnit(action->data());
12211224
}
12221225
}
1226+
1227+
void NetworkToggleStatusBarControl::mousePressEvent(QMouseEvent *event)
1228+
{
1229+
if (clientModel) {
1230+
clientModel->setNetworkActive(!clientModel->getNetworkActive());
1231+
}
1232+
}
1233+
1234+
/** Lets the control know about the Client Model */
1235+
void NetworkToggleStatusBarControl::setClientModel(ClientModel *_clientModel)
1236+
{
1237+
if (_clientModel) {
1238+
this->clientModel = _clientModel;
1239+
}
1240+
}

src/qt/bitcoingui.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class PlatformStyle;
2626
class RPCConsole;
2727
class SendCoinsRecipient;
2828
class UnitDisplayStatusBarControl;
29+
class NetworkToggleStatusBarControl;
2930
class WalletFrame;
3031
class WalletModel;
3132
class HelpMessageDialog;
@@ -84,7 +85,7 @@ class BitcoinGUI : public QMainWindow
8485
UnitDisplayStatusBarControl *unitDisplayControl;
8586
QLabel *labelWalletEncryptionIcon;
8687
QLabel *labelWalletHDStatusIcon;
87-
QLabel *labelConnectionsIcon;
88+
NetworkToggleStatusBarControl *connectionsControl;
8889
QLabel *labelBlocksIcon;
8990
QLabel *progressBarLabel;
9091
QProgressBar *progressBar;
@@ -265,4 +266,17 @@ private Q_SLOTS:
265266
void onMenuSelection(QAction* action);
266267
};
267268

269+
class NetworkToggleStatusBarControl : public QLabel
270+
{
271+
Q_OBJECT
272+
273+
public:
274+
void setClientModel(ClientModel *clientModel);
275+
protected:
276+
void mousePressEvent(QMouseEvent *event);
277+
278+
private:
279+
ClientModel *clientModel;
280+
};
281+
268282
#endif // BITCOIN_QT_BITCOINGUI_H

src/qt/res/icons/network_disabled.png

435 Bytes
Loading

src/rpc/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
107107
{ "prioritisetransaction", 2 },
108108
{ "setban", 2 },
109109
{ "setban", 3 },
110+
{ "setnetworkactive", 0 },
110111
{ "getmempoolancestors", 1 },
111112
{ "getmempooldescendants", 1 },
112113
};

src/rpc/net.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ UniValue getnetworkinfo(const UniValue& params, bool fHelp)
401401
" \"localrelay\": true|false, (bool) true if transaction relay is requested from peers\n"
402402
" \"timeoffset\": xxxxx, (numeric) the time offset\n"
403403
" \"connections\": xxxxx, (numeric) the number of connections\n"
404+
" \"networkactive\": x, (numeric) the number of connections\n"
404405
" \"networks\": [ (array) information per network\n"
405406
" {\n"
406407
" \"name\": \"xxx\", (string) network (ipv4, ipv6 or onion)\n"
@@ -435,8 +436,10 @@ UniValue getnetworkinfo(const UniValue& params, bool fHelp)
435436
obj.push_back(Pair("localservices", strprintf("%016x", g_connman->GetLocalServices())));
436437
obj.push_back(Pair("localrelay", fRelayTxes));
437438
obj.push_back(Pair("timeoffset", GetTimeOffset()));
438-
if(g_connman)
439+
if (g_connman) {
440+
obj.push_back(Pair("networkactive", (int)g_connman->GetNetworkActive()));
439441
obj.push_back(Pair("connections", (int)g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL)));
442+
}
440443
obj.push_back(Pair("networks", GetNetworksInfo()));
441444
obj.push_back(Pair("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK())));
442445
UniValue localAddresses(UniValue::VARR);
@@ -571,20 +574,20 @@ UniValue clearbanned(const UniValue& params, bool fHelp)
571574
return NullUniValue;
572575
}
573576

574-
UniValue togglenetwork(const JSONRPCRequest& request)
577+
UniValue setnetworkactive(const JSONRPCRequest& request)
575578
{
576-
if (request.fHelp || request.params.size() != 0) {
579+
if (request.fHelp || request.params.size() != 1) {
577580
throw runtime_error(
578-
"togglenetwork\n"
579-
"Toggle all network activity temporarily."
581+
"setnetworkactive \"true|false\"\n"
582+
"Disable/Re-Enable all network activity temporarily."
580583
);
581584
}
582585

583586
if (!g_connman) {
584587
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
585588
}
586589

587-
g_connman->SetNetworkActive(!g_connman->GetNetworkActive());
590+
g_connman->SetNetworkActive(request.params[0].get_bool());
588591

589592
return g_connman->GetNetworkActive();
590593
}
@@ -603,7 +606,7 @@ static const CRPCCommand commands[] =
603606
{ "network", "setban", &setban, true },
604607
{ "network", "listbanned", &listbanned, true },
605608
{ "network", "clearbanned", &clearbanned, true },
606-
{ "network", "togglenetwork", &togglenetwork, true, },
609+
{ "network", "setnetworkactive", &setnetworkactive, true, },
607610
};
608611

609612
void RegisterNetRPCCommands(CRPCTable &t)

src/test/rpc_tests.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,28 @@ BOOST_AUTO_TEST_CASE(rpc_rawparams)
8181
BOOST_CHECK_THROW(CallRPC(string("sendrawtransaction ")+rawtx+" extra"), runtime_error);
8282
}
8383

84+
BOOST_AUTO_TEST_CASE(rpc_togglenetwork)
85+
{
86+
UniValue r;
87+
88+
r = CallRPC("getnetworkinfo");
89+
int netState = find_value(r.get_obj(), "networkactive").get_int();
90+
BOOST_CHECK_EQUAL(netState, 1);
91+
92+
BOOST_CHECK_NO_THROW(CallRPC("setnetworkactive false"));
93+
r = CallRPC("getnetworkinfo");
94+
int numConnection = find_value(r.get_obj(), "connections").get_int();
95+
BOOST_CHECK_EQUAL(numConnection, 0);
96+
97+
netState = find_value(r.get_obj(), "networkactive").get_int();
98+
BOOST_CHECK_EQUAL(netState, 0);
99+
100+
BOOST_CHECK_NO_THROW(CallRPC("setnetworkactive true"));
101+
r = CallRPC("getnetworkinfo");
102+
netState = find_value(r.get_obj(), "networkactive").get_int();
103+
BOOST_CHECK_EQUAL(netState, 1);
104+
}
105+
84106
BOOST_AUTO_TEST_CASE(rpc_rawsign)
85107
{
86108
UniValue r;

0 commit comments

Comments
 (0)