Skip to content

Commit

Permalink
Overhaul network activity toggle
Browse files Browse the repository at this point in the history
- Rename RPC command "togglenetwork" to "setnetworkactive (true|false)"
- Add simple test case
- GUI toggle added to connections icon in statusbar
  • Loading branch information
jonasschnelli authored and luke-jr committed Oct 24, 2016
1 parent 32efa79 commit b2b33d9
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 14 deletions.
1 change: 1 addition & 0 deletions contrib/debian/copyright
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Files: src/qt/res/icons/add.png
src/qt/res/icons/info.png
src/qt/res/icons/key.png
src/qt/res/icons/lock_*.png
src/qt/res/icons/network_disabled.png
src/qt/res/icons/open.png
src/qt/res/icons/overview.png
src/qt/res/icons/quit.png
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.qt.include
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ RES_ICONS = \
qt/res/icons/key.png \
qt/res/icons/lock_closed.png \
qt/res/icons/lock_open.png \
qt/res/icons/network_disabled.png \
qt/res/icons/open.png \
qt/res/icons/overview.png \
qt/res/icons/quit.png \
Expand Down
1 change: 1 addition & 0 deletions src/qt/bitcoin.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<file alias="transaction_abandoned">res/icons/transaction_abandoned.png</file>
<file alias="hd_enabled">res/icons/hd_enabled.png</file>
<file alias="hd_disabled">res/icons/hd_disabled.png</file>
<file alias="network_disabled">res/icons/network_disabled.png</file>
</qresource>
<qresource prefix="/movies">
<file alias="spinner-000">res/movies/spinner-000.png</file>
Expand Down
30 changes: 24 additions & 6 deletions src/qt/bitcoingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ BitcoinGUI::BitcoinGUI(const PlatformStyle *_platformStyle, const NetworkStyle *
unitDisplayControl(0),
labelWalletEncryptionIcon(0),
labelWalletHDStatusIcon(0),
labelConnectionsIcon(0),
connectionsControl(0),
labelBlocksIcon(0),
progressBarLabel(0),
progressBar(0),
Expand Down Expand Up @@ -195,7 +195,7 @@ BitcoinGUI::BitcoinGUI(const PlatformStyle *_platformStyle, const NetworkStyle *
unitDisplayControl = new UnitDisplayStatusBarControl(platformStyle);
labelWalletEncryptionIcon = new QLabel();
labelWalletHDStatusIcon = new QLabel();
labelConnectionsIcon = new QLabel();
connectionsControl = new NetworkToggleStatusBarControl();
labelBlocksIcon = new QLabel();
if(enableWallet)
{
Expand All @@ -206,7 +206,7 @@ BitcoinGUI::BitcoinGUI(const PlatformStyle *_platformStyle, const NetworkStyle *
frameBlocksLayout->addWidget(labelWalletHDStatusIcon);
}
frameBlocksLayout->addStretch();
frameBlocksLayout->addWidget(labelConnectionsIcon);
frameBlocksLayout->addWidget(connectionsControl);
frameBlocksLayout->addStretch();
frameBlocksLayout->addWidget(labelBlocksIcon);
frameBlocksLayout->addStretch();
Expand Down Expand Up @@ -480,6 +480,7 @@ void BitcoinGUI::setClientModel(ClientModel *_clientModel)
}
#endif // ENABLE_WALLET
unitDisplayControl->setOptionsModel(_clientModel->getOptionsModel());
connectionsControl->setClientModel(_clientModel);

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

if (clientModel->getNetworkActive()) {
labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
connectionsControl->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
} else {
labelConnectionsIcon->setToolTip(tr("Network activity disabled"));
connectionsControl->setToolTip(tr("Network activity disabled"));
icon = ":/icons/network_disabled";
}

connectionsControl->setPixmap(platformStyle->SingleColorIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
}

void BitcoinGUI::setNumConnections(int count)
Expand Down Expand Up @@ -1220,3 +1223,18 @@ void UnitDisplayStatusBarControl::onMenuSelection(QAction* action)
optionsModel->setDisplayUnit(action->data());
}
}

void NetworkToggleStatusBarControl::mousePressEvent(QMouseEvent *event)
{
if (clientModel) {
clientModel->setNetworkActive(!clientModel->getNetworkActive());
}
}

/** Lets the control know about the Client Model */
void NetworkToggleStatusBarControl::setClientModel(ClientModel *_clientModel)
{
if (_clientModel) {
this->clientModel = _clientModel;
}
}
16 changes: 15 additions & 1 deletion src/qt/bitcoingui.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class PlatformStyle;
class RPCConsole;
class SendCoinsRecipient;
class UnitDisplayStatusBarControl;
class NetworkToggleStatusBarControl;
class WalletFrame;
class WalletModel;
class HelpMessageDialog;
Expand Down Expand Up @@ -84,7 +85,7 @@ class BitcoinGUI : public QMainWindow
UnitDisplayStatusBarControl *unitDisplayControl;
QLabel *labelWalletEncryptionIcon;
QLabel *labelWalletHDStatusIcon;
QLabel *labelConnectionsIcon;
NetworkToggleStatusBarControl *connectionsControl;
QLabel *labelBlocksIcon;
QLabel *progressBarLabel;
QProgressBar *progressBar;
Expand Down Expand Up @@ -265,4 +266,17 @@ private Q_SLOTS:
void onMenuSelection(QAction* action);
};

class NetworkToggleStatusBarControl : public QLabel
{
Q_OBJECT

public:
void setClientModel(ClientModel *clientModel);
protected:
void mousePressEvent(QMouseEvent *event);

private:
ClientModel *clientModel;
};

#endif // BITCOIN_QT_BITCOINGUI_H
Binary file added src/qt/res/icons/network_disabled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "prioritisetransaction", 2 },
{ "setban", 2 },
{ "setban", 3 },
{ "setnetworkactive", 0 },
{ "getmempoolancestors", 1 },
{ "getmempooldescendants", 1 },
};
Expand Down
17 changes: 10 additions & 7 deletions src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ UniValue getnetworkinfo(const UniValue& params, bool fHelp)
" \"localrelay\": true|false, (bool) true if transaction relay is requested from peers\n"
" \"timeoffset\": xxxxx, (numeric) the time offset\n"
" \"connections\": xxxxx, (numeric) the number of connections\n"
" \"networkactive\": x, (numeric) the number of connections\n"
" \"networks\": [ (array) information per network\n"
" {\n"
" \"name\": \"xxx\", (string) network (ipv4, ipv6 or onion)\n"
Expand Down Expand Up @@ -435,8 +436,10 @@ UniValue getnetworkinfo(const UniValue& params, bool fHelp)
obj.push_back(Pair("localservices", strprintf("%016x", g_connman->GetLocalServices())));
obj.push_back(Pair("localrelay", fRelayTxes));
obj.push_back(Pair("timeoffset", GetTimeOffset()));
if(g_connman)
if (g_connman) {
obj.push_back(Pair("networkactive", (int)g_connman->GetNetworkActive()));
obj.push_back(Pair("connections", (int)g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL)));
}
obj.push_back(Pair("networks", GetNetworksInfo()));
obj.push_back(Pair("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK())));
UniValue localAddresses(UniValue::VARR);
Expand Down Expand Up @@ -571,20 +574,20 @@ UniValue clearbanned(const UniValue& params, bool fHelp)
return NullUniValue;
}

UniValue togglenetwork(const JSONRPCRequest& request)
UniValue setnetworkactive(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() != 0) {
if (request.fHelp || request.params.size() != 1) {
throw runtime_error(
"togglenetwork\n"
"Toggle all network activity temporarily."
"setnetworkactive \"true|false\"\n"
"Disable/Re-Enable all network activity temporarily."
);
}

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

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

return g_connman->GetNetworkActive();
}
Expand All @@ -603,7 +606,7 @@ static const CRPCCommand commands[] =
{ "network", "setban", &setban, true },
{ "network", "listbanned", &listbanned, true },
{ "network", "clearbanned", &clearbanned, true },
{ "network", "togglenetwork", &togglenetwork, true, },
{ "network", "setnetworkactive", &setnetworkactive, true, },
};

void RegisterNetRPCCommands(CRPCTable &t)
Expand Down
22 changes: 22 additions & 0 deletions src/test/rpc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,28 @@ BOOST_AUTO_TEST_CASE(rpc_rawparams)
BOOST_CHECK_THROW(CallRPC(string("sendrawtransaction ")+rawtx+" extra"), runtime_error);
}

BOOST_AUTO_TEST_CASE(rpc_togglenetwork)
{
UniValue r;

r = CallRPC("getnetworkinfo");
int netState = find_value(r.get_obj(), "networkactive").get_int();
BOOST_CHECK_EQUAL(netState, 1);

BOOST_CHECK_NO_THROW(CallRPC("setnetworkactive false"));
r = CallRPC("getnetworkinfo");
int numConnection = find_value(r.get_obj(), "connections").get_int();
BOOST_CHECK_EQUAL(numConnection, 0);

netState = find_value(r.get_obj(), "networkactive").get_int();
BOOST_CHECK_EQUAL(netState, 0);

BOOST_CHECK_NO_THROW(CallRPC("setnetworkactive true"));
r = CallRPC("getnetworkinfo");
netState = find_value(r.get_obj(), "networkactive").get_int();
BOOST_CHECK_EQUAL(netState, 1);
}

BOOST_AUTO_TEST_CASE(rpc_rawsign)
{
UniValue r;
Expand Down

0 comments on commit b2b33d9

Please sign in to comment.