From 7c9a98aac843c9efabd8653caebc35e968b2f335 Mon Sep 17 00:00:00 2001 From: Jon Lund Steffensen Date: Tue, 26 Mar 2013 02:33:25 +0100 Subject: [PATCH 1/6] Allow network activity to be temporarily suspended. Added the function SetNetworkActive() which when called with argument set to false disconnects all nodes and sets the flag fNetworkActive to false. As long as this flag is false no new connections are attempted and no incoming connections are accepted. Network activity is reenabled by calling the function with argument true. --- src/net.cpp | 29 +++++++++++++++++++++++++++++ src/net.h | 2 ++ 2 files changed, 31 insertions(+) diff --git a/src/net.cpp b/src/net.cpp index cce06f2d6..973e278e0 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -990,6 +990,12 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) { return; } + if (!fNetworkActive) { + LogPrintf("connection from %s dropped: not accepting new connections\n", addr.ToString()); + CloseSocket(hSocket); + return; + } + if (!IsSelectableSocket(hSocket)) { LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString()); @@ -1783,6 +1789,9 @@ bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai // Initiate outbound network connection // boost::this_thread::interruption_point(); + if (!fNetworkActive) { + return false; + } if (!pszDest) { if (IsLocal(addrConnect) || FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) || @@ -2024,8 +2033,28 @@ void Discover(boost::thread_group& threadGroup) #endif } +void CConnman::SetNetworkActive(bool active) +{ + if (fDebug) { + LogPrint("net", "SetNetworkActive: %s\n", active); + } + + if (!active) { + fNetworkActive = false; + + LOCK(cs_vNodes); + // Close sockets to all nodes + BOOST_FOREACH(CNode* pnode, vNodes) { + pnode->CloseSocketDisconnect(); + } + } else { + fNetworkActive = true; + } +} + CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSeed1(nSeed1In) { + fNetworkActive = true; setBannedIsDirty = false; fAddressesInitialized = false; nLastNodeId = 0; diff --git a/src/net.h b/src/net.h index d0b277362..6912c0f97 100644 --- a/src/net.h +++ b/src/net.h @@ -131,6 +131,7 @@ public: bool Start(boost::thread_group& threadGroup, CScheduler& scheduler, std::string& strNodeError, Options options); void Stop(); bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false); + void SetNetworkActive(bool active); bool OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false, bool fFeeler = false); bool CheckIncomingNonce(uint64_t nonce); @@ -370,6 +371,7 @@ private: unsigned int nReceiveFloodSize; std::vector vhListenSocket; + bool fNetworkActive; banmap_t setBanned; CCriticalSection cs_setBanned; bool setBannedIsDirty; From e38993bb36801d492cad87479b8473794f19c9da Mon Sep 17 00:00:00 2001 From: Jon Lund Steffensen Date: Tue, 26 Mar 2013 02:38:24 +0100 Subject: [PATCH 2/6] RPC: Add "togglenetwork" method to toggle network activity temporarily RPC command "togglenetwork" toggles network and returns new state after command. RPC command "getinfo" returns "networkactive" field in output. --- src/net.h | 1 + src/rpc/misc.cpp | 4 +++- src/rpc/net.cpp | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/net.h b/src/net.h index 6912c0f97..1d911664f 100644 --- a/src/net.h +++ b/src/net.h @@ -131,6 +131,7 @@ public: bool Start(boost::thread_group& threadGroup, CScheduler& scheduler, std::string& strNodeError, Options options); void Stop(); bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false); + bool GetNetworkActive() const { return fNetworkActive; }; void SetNetworkActive(bool active); bool OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false, bool fFeeler = false); bool CheckIncomingNonce(uint64_t nonce); diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index 5afcf6353..4bfcc9387 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -89,8 +89,10 @@ UniValue getinfo(const UniValue& params, bool fHelp) #endif obj.push_back(Pair("blocks", (int)chainActive.Height())); obj.push_back(Pair("timeoffset", GetTimeOffset())); - if(g_connman) + if (g_connman) { + obj.push_back(Pair("networkactive", g_connman->GetNetworkActive())); obj.push_back(Pair("connections", (int)g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL))); + } obj.push_back(Pair("proxy", (proxy.IsValid() ? proxy.proxy.ToStringIPPort() : string()))); obj.push_back(Pair("difficulty", (double)GetDifficulty())); obj.push_back(Pair("testnet", Params().TestnetToBeDeprecatedFieldRPC())); diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index b011029f5..7f5b10799 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -571,6 +571,24 @@ UniValue clearbanned(const UniValue& params, bool fHelp) return NullUniValue; } +UniValue togglenetwork(const JSONRPCRequest& request) +{ + if (request.fHelp || request.params.size() != 0) { + throw runtime_error( + "togglenetwork\n" + "Toggle 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()); + + return g_connman->GetNetworkActive(); +} + static const CRPCCommand commands[] = { // category name actor (function) okSafeMode // --------------------- ------------------------ ----------------------- ---------- @@ -585,6 +603,7 @@ static const CRPCCommand commands[] = { "network", "setban", &setban, true }, { "network", "listbanned", &listbanned, true }, { "network", "clearbanned", &clearbanned, true }, + { "network", "togglenetwork", &togglenetwork, true, }, }; void RegisterNetRPCCommands(CRPCTable &t) From 32efa79e0e63b6d3f055326b2e6b794815d408ad Mon Sep 17 00:00:00 2001 From: Jon Lund Steffensen Date: Tue, 26 Mar 2013 03:07:06 +0100 Subject: [PATCH 3/6] Qt: Add GUI feedback and control of network activity state. Add getNetworkActive()/setNetworkActive() method to client model. Send network active status through NotifyNetworkActiveChanged. Indicate in tool tip of gui status bar network indicator whether network activity is disabled. Indicate in debug window whether network activity is disabled and add button to allow user to toggle network activity state. --- src/net.cpp | 2 ++ src/qt/bitcoingui.cpp | 23 ++++++++++++++++++++--- src/qt/bitcoingui.h | 5 +++++ src/qt/clientmodel.cpp | 28 ++++++++++++++++++++++++++++ src/qt/clientmodel.h | 6 ++++++ src/qt/rpcconsole.cpp | 30 ++++++++++++++++++++++++++---- src/qt/rpcconsole.h | 7 +++++++ src/ui_interface.h | 3 +++ 8 files changed, 97 insertions(+), 7 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index 973e278e0..7c5e00c6f 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -2050,6 +2050,8 @@ void CConnman::SetNetworkActive(bool active) } else { fNetworkActive = true; } + + uiInterface.NotifyNetworkActiveChanged(fNetworkActive); } CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSeed1(nSeed1In) diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index 16d7778a2..69cdfc9c0 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -459,8 +459,9 @@ void BitcoinGUI::setClientModel(ClientModel *_clientModel) createTrayIconMenu(); // Keep up to date with client - setNumConnections(_clientModel->getNumConnections()); + updateNetworkState(); connect(_clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int))); + connect(_clientModel, SIGNAL(networkActiveChanged(bool)), this, SLOT(setNetworkActive(bool))); setNumBlocks(_clientModel->getNumBlocks(), _clientModel->getLastBlockDate(), _clientModel->getVerificationProgress(NULL), false); connect(_clientModel, SIGNAL(numBlocksChanged(int,QDateTime,double,bool)), this, SLOT(setNumBlocks(int,QDateTime,double,bool))); @@ -686,8 +687,9 @@ void BitcoinGUI::gotoVerifyMessageTab(QString addr) } #endif // ENABLE_WALLET -void BitcoinGUI::setNumConnections(int count) +void BitcoinGUI::updateNetworkState() { + int count = clientModel->getNumConnections(); QString icon; switch(count) { @@ -698,7 +700,22 @@ void BitcoinGUI::setNumConnections(int count) default: icon = ":/icons/connect_4"; break; } labelConnectionsIcon->setPixmap(platformStyle->SingleColorIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE)); - labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count)); + + if (clientModel->getNetworkActive()) { + labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count)); + } else { + labelConnectionsIcon->setToolTip(tr("Network activity disabled")); + } +} + +void BitcoinGUI::setNumConnections(int count) +{ + updateNetworkState(); +} + +void BitcoinGUI::setNetworkActive(bool networkActive) +{ + updateNetworkState(); } void BitcoinGUI::setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, bool header) diff --git a/src/qt/bitcoingui.h b/src/qt/bitcoingui.h index 41770929b..2eae60d41 100644 --- a/src/qt/bitcoingui.h +++ b/src/qt/bitcoingui.h @@ -144,6 +144,9 @@ private: /** Disconnect core signals from GUI client */ void unsubscribeFromCoreSignals(); + /** Update UI with latest network info from model. */ + void updateNetworkState(); + Q_SIGNALS: /** Signal raised when a URI was entered or dragged to the GUI */ void receivedURI(const QString &uri); @@ -151,6 +154,8 @@ Q_SIGNALS: public Q_SLOTS: /** Set number of connections shown in the UI */ void setNumConnections(int count); + /** Set network state shown in the UI */ + void setNetworkActive(bool networkActive); /** Set number of blocks and last block date shown in the UI */ void setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, bool headers); diff --git a/src/qt/clientmodel.cpp b/src/qt/clientmodel.cpp index 62a70bacd..3e062bffb 100644 --- a/src/qt/clientmodel.cpp +++ b/src/qt/clientmodel.cpp @@ -128,6 +128,11 @@ void ClientModel::updateNumConnections(int numConnections) Q_EMIT numConnectionsChanged(numConnections); } +void ClientModel::updateNetworkActive(bool networkActive) +{ + Q_EMIT networkActiveChanged(networkActive); +} + void ClientModel::updateAlert() { Q_EMIT alertsChanged(getStatusBarWarnings()); @@ -150,6 +155,21 @@ enum BlockSource ClientModel::getBlockSource() const return BLOCK_SOURCE_NONE; } +void ClientModel::setNetworkActive(bool active) +{ + if (g_connman) { + g_connman->SetNetworkActive(active); + } +} + +bool ClientModel::getNetworkActive() const +{ + if (g_connman) { + return g_connman->GetNetworkActive(); + } + return false; +} + QString ClientModel::getStatusBarWarnings() const { return QString::fromStdString(GetWarnings("gui")); @@ -216,6 +236,12 @@ static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConn Q_ARG(int, newNumConnections)); } +static void NotifyNetworkActiveChanged(ClientModel *clientmodel, bool networkActive) +{ + QMetaObject::invokeMethod(clientmodel, "updateNetworkActive", Qt::QueuedConnection, + Q_ARG(bool, networkActive)); +} + static void NotifyAlertChanged(ClientModel *clientmodel) { qDebug() << "NotifyAlertChanged"; @@ -256,6 +282,7 @@ void ClientModel::subscribeToCoreSignals() // Connect signals to client uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2)); uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, _1)); + uiInterface.NotifyNetworkActiveChanged.connect(boost::bind(NotifyNetworkActiveChanged, this, _1)); uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this)); uiInterface.BannedListChanged.connect(boost::bind(BannedListChanged, this)); uiInterface.NotifyBlockTip.connect(boost::bind(BlockTipChanged, this, _1, _2, false)); @@ -267,6 +294,7 @@ void ClientModel::unsubscribeFromCoreSignals() // Disconnect signals from client uiInterface.ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2)); uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, _1)); + uiInterface.NotifyNetworkActiveChanged.disconnect(boost::bind(NotifyNetworkActiveChanged, this, _1)); uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this)); uiInterface.BannedListChanged.disconnect(boost::bind(BannedListChanged, this)); uiInterface.NotifyBlockTip.disconnect(boost::bind(BlockTipChanged, this, _1, _2, false)); diff --git a/src/qt/clientmodel.h b/src/qt/clientmodel.h index 99fd574b9..52ca0e312 100644 --- a/src/qt/clientmodel.h +++ b/src/qt/clientmodel.h @@ -67,6 +67,10 @@ public: bool inInitialBlockDownload() const; //! Return true if core is importing blocks enum BlockSource getBlockSource() const; + //! Return true if network activity in core is enabled + bool getNetworkActive() const; + //! Toggle network activity state in core + void setNetworkActive(bool active); //! Return warnings to be displayed in status bar QString getStatusBarWarnings() const; @@ -90,6 +94,7 @@ Q_SIGNALS: void numConnectionsChanged(int count); void numBlocksChanged(int count, const QDateTime& blockDate, double nVerificationProgress, bool header); void mempoolSizeChanged(long count, size_t mempoolSizeInBytes); + void networkActiveChanged(bool networkActive); void alertsChanged(const QString &warnings); void bytesChanged(quint64 totalBytesIn, quint64 totalBytesOut); @@ -102,6 +107,7 @@ Q_SIGNALS: public Q_SLOTS: void updateTimer(); void updateNumConnections(int numConnections); + void updateNetworkActive(bool networkActive); void updateAlert(); void updateBanlist(); }; diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp index ace9f1cea..05a59378f 100644 --- a/src/qt/rpcconsole.cpp +++ b/src/qt/rpcconsole.cpp @@ -456,6 +456,9 @@ void RPCConsole::setClientModel(ClientModel *model) setNumBlocks(model->getNumBlocks(), model->getLastBlockDate(), model->getVerificationProgress(NULL), false); connect(model, SIGNAL(numBlocksChanged(int,QDateTime,double,bool)), this, SLOT(setNumBlocks(int,QDateTime,double,bool))); + updateNetworkState(); + connect(model, SIGNAL(networkActiveChanged(bool)), this, SLOT(setNetworkActive(bool))); + updateTrafficStats(model->getTotalBytesRecv(), model->getTotalBytesSent()); connect(model, SIGNAL(bytesChanged(quint64,quint64)), this, SLOT(updateTrafficStats(quint64, quint64))); @@ -670,16 +673,30 @@ void RPCConsole::message(int category, const QString &message, bool html) ui->messagesWidget->append(out); } +void RPCConsole::updateNetworkState() +{ + QString connections = QString::number(clientModel->getNumConnections()) + " ("; + connections += tr("In:") + " " + QString::number(clientModel->getNumConnections(CONNECTIONS_IN)) + " / "; + connections += tr("Out:") + " " + QString::number(clientModel->getNumConnections(CONNECTIONS_OUT)) + ")"; + + if(!clientModel->getNetworkActive()) { + connections += " (" + tr("Network activity disabled") + ")"; + } + + ui->numberOfConnections->setText(connections); +} + void RPCConsole::setNumConnections(int count) { if (!clientModel) return; - QString connections = QString::number(count) + " ("; - connections += tr("In:") + " " + QString::number(clientModel->getNumConnections(CONNECTIONS_IN)) + " / "; - connections += tr("Out:") + " " + QString::number(clientModel->getNumConnections(CONNECTIONS_OUT)) + ")"; + updateNetworkState(); +} - ui->numberOfConnections->setText(connections); +void RPCConsole::setNetworkActive(bool networkActive) +{ + updateNetworkState(); } void RPCConsole::setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, bool headers) @@ -1036,3 +1053,8 @@ void RPCConsole::setTabFocus(enum TabTypes tabType) { ui->tabWidget->setCurrentIndex(tabType); } + +void RPCConsole::on_toggleNetworkActiveButton_clicked() +{ + clientModel->setNetworkActive(!clientModel->getNetworkActive()); +} diff --git a/src/qt/rpcconsole.h b/src/qt/rpcconsole.h index 50224a1cc..2a5686b67 100644 --- a/src/qt/rpcconsole.h +++ b/src/qt/rpcconsole.h @@ -61,6 +61,8 @@ protected: private Q_SLOTS: void on_lineEdit_returnPressed(); void on_tabWidget_currentChanged(int index); + /** toggle network activity */ + void on_toggleNetworkActiveButton_clicked(); /** open the debug.log from the current datadir */ void on_openDebugLogfileButton_clicked(); /** change the time range of the network traffic graph */ @@ -88,6 +90,8 @@ public Q_SLOTS: void message(int category, const QString &message, bool html = false); /** Set number of connections shown in the UI */ void setNumConnections(int count); + /** Set network state shown in the UI */ + void setNetworkActive(bool networkActive); /** Set number of blocks and last block date shown in the UI */ void setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, bool headers); /** Set size (number of transactions and memory usage) of the mempool in the UI */ @@ -142,6 +146,9 @@ private: QMenu *banTableContextMenu; int consoleFontSize; QCompleter *autoCompleter; + + /** Update UI with latest network info from model. */ + void updateNetworkState(); }; #endif // BITCOIN_QT_RPCCONSOLE_H diff --git a/src/ui_interface.h b/src/ui_interface.h index 7e6557f8e..d2a393a35 100644 --- a/src/ui_interface.h +++ b/src/ui_interface.h @@ -85,6 +85,9 @@ public: /** Number of network connections changed. */ boost::signals2::signal NotifyNumConnectionsChanged; + /** Network activity state changed. */ + boost::signals2::signal NotifyNetworkActiveChanged; + /** * Status bar alerts changed. */ From b2b33d9017cd7bc92099338773313fdf4572dc4a Mon Sep 17 00:00:00 2001 From: Jonas Schnelli Date: Wed, 19 Nov 2014 13:33:34 +0100 Subject: [PATCH 4/6] Overhaul network activity toggle - Rename RPC command "togglenetwork" to "setnetworkactive (true|false)" - Add simple test case - GUI toggle added to connections icon in statusbar --- contrib/debian/copyright | 1 + src/Makefile.qt.include | 1 + src/qt/bitcoin.qrc | 1 + src/qt/bitcoingui.cpp | 30 ++++++++++++++++++++------ src/qt/bitcoingui.h | 16 +++++++++++++- src/qt/res/icons/network_disabled.png | Bin 0 -> 435 bytes src/rpc/client.cpp | 1 + src/rpc/net.cpp | 17 +++++++++------ src/test/rpc_tests.cpp | 22 +++++++++++++++++++ 9 files changed, 75 insertions(+), 14 deletions(-) create mode 100644 src/qt/res/icons/network_disabled.png diff --git a/contrib/debian/copyright b/contrib/debian/copyright index cc4606ca8..011cb4a4d 100644 --- a/contrib/debian/copyright +++ b/contrib/debian/copyright @@ -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 diff --git a/src/Makefile.qt.include b/src/Makefile.qt.include index 8947aeaca..277cbb9de 100644 --- a/src/Makefile.qt.include +++ b/src/Makefile.qt.include @@ -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 \ diff --git a/src/qt/bitcoin.qrc b/src/qt/bitcoin.qrc index ca5b1fa67..451d39123 100644 --- a/src/qt/bitcoin.qrc +++ b/src/qt/bitcoin.qrc @@ -52,6 +52,7 @@ res/icons/transaction_abandoned.png res/icons/hd_enabled.png res/icons/hd_disabled.png + res/icons/network_disabled.png res/movies/spinner-000.png diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index 69cdfc9c0..4cc30454a 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -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), @@ -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) { @@ -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(); @@ -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) @@ -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) @@ -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; + } +} diff --git a/src/qt/bitcoingui.h b/src/qt/bitcoingui.h index 2eae60d41..83634b959 100644 --- a/src/qt/bitcoingui.h +++ b/src/qt/bitcoingui.h @@ -26,6 +26,7 @@ class PlatformStyle; class RPCConsole; class SendCoinsRecipient; class UnitDisplayStatusBarControl; +class NetworkToggleStatusBarControl; class WalletFrame; class WalletModel; class HelpMessageDialog; @@ -84,7 +85,7 @@ private: UnitDisplayStatusBarControl *unitDisplayControl; QLabel *labelWalletEncryptionIcon; QLabel *labelWalletHDStatusIcon; - QLabel *labelConnectionsIcon; + NetworkToggleStatusBarControl *connectionsControl; QLabel *labelBlocksIcon; QLabel *progressBarLabel; QProgressBar *progressBar; @@ -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 diff --git a/src/qt/res/icons/network_disabled.png b/src/qt/res/icons/network_disabled.png new file mode 100644 index 0000000000000000000000000000000000000000..c802e3981866fc4e431ece80c5295f66f518d41c GIT binary patch literal 435 zcmV;k0ZjghP)W1Ka?^9sc2tHf@s@G4JmEQQ-RW)g(#@1OkCTK!X}+fEGBe(9X}) zTRQj!*f|cp#%_MM7>*umV`=AaWw-i3aQH8WoZe^%558oR!}23DgFJBvjQJ_?D$jE- z;0^d#{H}p#LSPxE(3fl1gNAy6Z#mMs}N7%xr#x0IpeLuj4Hh)^C89pw!hlZ;qT zxQs(`V;fJ*r;5;y_YG(A_6Q4NMRc6W?6VGq2$z@H1n7WR_KDD30xG0{R1upHkRn2t z4xv;LMFg0H`o0vwn#4BprVJ6$bCyvP5k?^*2JVZjW^NZo?pDIr%(cjB7Sj-BYo}WV zPms^Z7GW_=PV@@5sVFv5K7sd@k@AXIp|FjVn;h!@ZmqJ5#nvuXi@R9v?q1002ovPDHLkV1n7kw<-Vt literal 0 HcmV?d00001 diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index c14d9d674..66648b685 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -107,6 +107,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "prioritisetransaction", 2 }, { "setban", 2 }, { "setban", 3 }, + { "setnetworkactive", 0 }, { "getmempoolancestors", 1 }, { "getmempooldescendants", 1 }, }; diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 7f5b10799..045089e2e 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -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" @@ -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); @@ -571,12 +574,12 @@ 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." ); } @@ -584,7 +587,7 @@ UniValue togglenetwork(const JSONRPCRequest& request) 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(); } @@ -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) diff --git a/src/test/rpc_tests.cpp b/src/test/rpc_tests.cpp index a15915aad..03594878e 100644 --- a/src/test/rpc_tests.cpp +++ b/src/test/rpc_tests.cpp @@ -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; From 54cf99745ffa8f437c63f9f86c5c9d5e6bb04028 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Sun, 23 Oct 2016 05:35:52 +0000 Subject: [PATCH 5/6] RPC/Net: Use boolean consistently for networkactive, and remove from getinfo --- src/rpc/misc.cpp | 4 +--- src/rpc/net.cpp | 8 ++++---- src/test/rpc_tests.cpp | 12 ++++++------ 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index 4bfcc9387..5afcf6353 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -89,10 +89,8 @@ UniValue getinfo(const UniValue& params, bool fHelp) #endif obj.push_back(Pair("blocks", (int)chainActive.Height())); obj.push_back(Pair("timeoffset", GetTimeOffset())); - if (g_connman) { - obj.push_back(Pair("networkactive", g_connman->GetNetworkActive())); + if(g_connman) obj.push_back(Pair("connections", (int)g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL))); - } obj.push_back(Pair("proxy", (proxy.IsValid() ? proxy.proxy.ToStringIPPort() : string()))); obj.push_back(Pair("difficulty", (double)GetDifficulty())); obj.push_back(Pair("testnet", Params().TestnetToBeDeprecatedFieldRPC())); diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 045089e2e..c470a546b 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -401,7 +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" + " \"networkactive\": true|false, (bool) whether p2p networking is enabled\n" " \"networks\": [ (array) information per network\n" " {\n" " \"name\": \"xxx\", (string) network (ipv4, ipv6 or onion)\n" @@ -437,7 +437,7 @@ UniValue getnetworkinfo(const UniValue& params, bool fHelp) obj.push_back(Pair("localrelay", fRelayTxes)); obj.push_back(Pair("timeoffset", GetTimeOffset())); if (g_connman) { - obj.push_back(Pair("networkactive", (int)g_connman->GetNetworkActive())); + obj.push_back(Pair("networkactive", g_connman->GetNetworkActive())); obj.push_back(Pair("connections", (int)g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL))); } obj.push_back(Pair("networks", GetNetworksInfo())); @@ -578,8 +578,8 @@ UniValue setnetworkactive(const JSONRPCRequest& request) { if (request.fHelp || request.params.size() != 1) { throw runtime_error( - "setnetworkactive \"true|false\"\n" - "Disable/Re-Enable all network activity temporarily." + "setnetworkactive true|false\n" + "Disable/enable all p2p network activity." ); } diff --git a/src/test/rpc_tests.cpp b/src/test/rpc_tests.cpp index 03594878e..c98d4b11d 100644 --- a/src/test/rpc_tests.cpp +++ b/src/test/rpc_tests.cpp @@ -86,21 +86,21 @@ 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); + bool netState = find_value(r.get_obj(), "networkactive").get_bool(); + BOOST_CHECK_EQUAL(netState, true); 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); + netState = find_value(r.get_obj(), "networkactive").get_bool(); + BOOST_CHECK_EQUAL(netState, false); BOOST_CHECK_NO_THROW(CallRPC("setnetworkactive true")); r = CallRPC("getnetworkinfo"); - netState = find_value(r.get_obj(), "networkactive").get_int(); - BOOST_CHECK_EQUAL(netState, 1); + netState = find_value(r.get_obj(), "networkactive").get_bool(); + BOOST_CHECK_EQUAL(netState, true); } BOOST_AUTO_TEST_CASE(rpc_rawsign) From 19f46f177ec5e1913f9be5b257dad95bc7a57c38 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Mon, 24 Oct 2016 09:21:03 +0000 Subject: [PATCH 6/6] Qt: New network_disabled icon --- contrib/debian/copyright | 4 +- src/qt/res/icons/network_disabled.png | Bin 435 -> 591 bytes src/qt/res/src/network_disabled.svg | 68 ++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/qt/res/src/network_disabled.svg diff --git a/contrib/debian/copyright b/contrib/debian/copyright index 011cb4a4d..b44244ad8 100644 --- a/contrib/debian/copyright +++ b/contrib/debian/copyright @@ -34,7 +34,6 @@ 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 @@ -52,7 +51,10 @@ Comment: Site: https://github.com/stephenhutchings/typicons.font Files: src/qt/res/icons/connect*.png src/qt/res/src/connect-*.svg + src/qt/res/icons/network_disabled.png + src/qt/res/src/network_disabled.svg Copyright: Marco Falke + Luke Dashjr License: Expat Comment: Inspired by Stephan Hutchings Typicons diff --git a/src/qt/res/icons/network_disabled.png b/src/qt/res/icons/network_disabled.png index c802e3981866fc4e431ece80c5295f66f518d41c..49f728693d8c0fc5d63c4911b84f431961631434 100644 GIT binary patch literal 591 zcmV-V0W}3`_cX(*poe#fAC*~6L^bnc#QS2K`(x1&n2`ikYEJ2aWjhZ3CoHI8j)0R0Ix6`+0S4i ziSY=Ba>uqTXfu*?!u5AxaTDKujHcO*CWe z{f(ulC)xePJ&XwjER%8$#<8#9eNjysXSNNGicM;C9Lay?8jDEl{p?1rn*`n zL1&ivZd}LSl3Uh=Mwvu+&U_T-OC0j&hp-{Lgo5@okaA8<#W1Ka?^9sc2tHf@s@G4JmEQQ-RW)g(#@1OkCTK!X}+fEGBe(9X}) zTRQj!*f|cp#%_MM7>*umV`=AaWw-i3aQH8WoZe^%558oR!}23DgFJBvjQJ_?D$jE- z;0^d#{H}p#LSPxE(3fl1gNAy6Z#mMs}N7%xr#x0IpeLuj4Hh)^C89pw!hlZ;qT zxQs(`V;fJ*r;5;y_YG(A_6Q4NMRc6W?6VGq2$z@H1n7WR_KDD30xG0{R1upHkRn2t z4xv;LMFg0H`o0vwn#4BprVJ6$bCyvP5k?^*2JVZjW^NZo?pDIr%(cjB7Sj-BYo}WV zPms^Z7GW_=PV@@5sVFv5K7sd@k@AXIp|FjVn;h!@ZmqJ5#nvuXi@R9v?q1002ovPDHLkV1n7kw<-Vt diff --git a/src/qt/res/src/network_disabled.svg b/src/qt/res/src/network_disabled.svg new file mode 100644 index 000000000..e95a5eb5b --- /dev/null +++ b/src/qt/res/src/network_disabled.svg @@ -0,0 +1,68 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + +