Skip to content

Commit ab914a6

Browse files
committed
Merge #8996: Network activity toggle
19f46f1 Qt: New network_disabled icon (Luke Dashjr) 54cf997 RPC/Net: Use boolean consistently for networkactive, and remove from getinfo (Luke Dashjr) b2b33d9 Overhaul network activity toggle (Jonas Schnelli) 32efa79 Qt: Add GUI feedback and control of network activity state. (Jon Lund Steffensen) e38993b RPC: Add "togglenetwork" method to toggle network activity temporarily (Jon Lund Steffensen) 7c9a98a Allow network activity to be temporarily suspended. (Jon Lund Steffensen)
2 parents 7977a11 + 19f46f1 commit ab914a6

17 files changed

+285
-13
lines changed

contrib/debian/copyright

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ Comment: Site: https://github.com/stephenhutchings/typicons.font
4747

4848
Files: src/qt/res/icons/connect*.png
4949
src/qt/res/src/connect-*.svg
50+
src/qt/res/icons/network_disabled.png
51+
src/qt/res/src/network_disabled.svg
5052
Copyright: Marco Falke
53+
Luke Dashjr
5154
License: Expat
5255
Comment: Inspired by Stephan Hutchings Typicons
5356

src/Makefile.qt.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ RES_ICONS = \
271271
qt/res/icons/key.png \
272272
qt/res/icons/lock_closed.png \
273273
qt/res/icons/lock_open.png \
274+
qt/res/icons/network_disabled.png \
274275
qt/res/icons/open.png \
275276
qt/res/icons/overview.png \
276277
qt/res/icons/quit.png \

src/net.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,12 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
985985
return;
986986
}
987987

988+
if (!fNetworkActive) {
989+
LogPrintf("connection from %s dropped: not accepting new connections\n", addr.ToString());
990+
CloseSocket(hSocket);
991+
return;
992+
}
993+
988994
if (!IsSelectableSocket(hSocket))
989995
{
990996
LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString());
@@ -1784,6 +1790,9 @@ bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
17841790
// Initiate outbound network connection
17851791
//
17861792
boost::this_thread::interruption_point();
1793+
if (!fNetworkActive) {
1794+
return false;
1795+
}
17871796
if (!pszDest) {
17881797
if (IsLocal(addrConnect) ||
17891798
FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) ||
@@ -2025,8 +2034,30 @@ void Discover(boost::thread_group& threadGroup)
20252034
#endif
20262035
}
20272036

2037+
void CConnman::SetNetworkActive(bool active)
2038+
{
2039+
if (fDebug) {
2040+
LogPrint("net", "SetNetworkActive: %s\n", active);
2041+
}
2042+
2043+
if (!active) {
2044+
fNetworkActive = false;
2045+
2046+
LOCK(cs_vNodes);
2047+
// Close sockets to all nodes
2048+
BOOST_FOREACH(CNode* pnode, vNodes) {
2049+
pnode->CloseSocketDisconnect();
2050+
}
2051+
} else {
2052+
fNetworkActive = true;
2053+
}
2054+
2055+
uiInterface.NotifyNetworkActiveChanged(fNetworkActive);
2056+
}
2057+
20282058
CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSeed1(nSeed1In)
20292059
{
2060+
fNetworkActive = true;
20302061
setBannedIsDirty = false;
20312062
fAddressesInitialized = false;
20322063
nLastNodeId = 0;

src/net.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ class CConnman
131131
bool Start(boost::thread_group& threadGroup, CScheduler& scheduler, std::string& strNodeError, Options options);
132132
void Stop();
133133
bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false);
134+
bool GetNetworkActive() const { return fNetworkActive; };
135+
void SetNetworkActive(bool active);
134136
bool OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false, bool fFeeler = false);
135137
bool CheckIncomingNonce(uint64_t nonce);
136138

@@ -401,6 +403,7 @@ class CConnman
401403
unsigned int nReceiveFloodSize;
402404

403405
std::vector<ListenSocket> vhListenSocket;
406+
bool fNetworkActive;
404407
banmap_t setBanned;
405408
CCriticalSection cs_setBanned;
406409
bool setBannedIsDirty;

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: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ BitcoinGUI::BitcoinGUI(const PlatformStyle *_platformStyle, const NetworkStyle *
8686
unitDisplayControl(0),
8787
labelWalletEncryptionIcon(0),
8888
labelWalletHDStatusIcon(0),
89-
labelConnectionsIcon(0),
89+
connectionsControl(0),
9090
labelBlocksIcon(0),
9191
progressBarLabel(0),
9292
progressBar(0),
@@ -199,7 +199,7 @@ BitcoinGUI::BitcoinGUI(const PlatformStyle *_platformStyle, const NetworkStyle *
199199
unitDisplayControl = new UnitDisplayStatusBarControl(platformStyle);
200200
labelWalletEncryptionIcon = new QLabel();
201201
labelWalletHDStatusIcon = new QLabel();
202-
labelConnectionsIcon = new QLabel();
202+
connectionsControl = new NetworkToggleStatusBarControl();
203203
labelBlocksIcon = new QLabel();
204204
if(enableWallet)
205205
{
@@ -210,7 +210,7 @@ BitcoinGUI::BitcoinGUI(const PlatformStyle *_platformStyle, const NetworkStyle *
210210
frameBlocksLayout->addWidget(labelWalletHDStatusIcon);
211211
}
212212
frameBlocksLayout->addStretch();
213-
frameBlocksLayout->addWidget(labelConnectionsIcon);
213+
frameBlocksLayout->addWidget(connectionsControl);
214214
frameBlocksLayout->addStretch();
215215
frameBlocksLayout->addWidget(labelBlocksIcon);
216216
frameBlocksLayout->addStretch();
@@ -469,8 +469,9 @@ void BitcoinGUI::setClientModel(ClientModel *_clientModel)
469469
createTrayIconMenu();
470470

471471
// Keep up to date with client
472-
setNumConnections(_clientModel->getNumConnections());
472+
updateNetworkState();
473473
connect(_clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
474+
connect(_clientModel, SIGNAL(networkActiveChanged(bool)), this, SLOT(setNetworkActive(bool)));
474475

475476
setNumBlocks(_clientModel->getNumBlocks(), _clientModel->getLastBlockDate(), _clientModel->getVerificationProgress(NULL), false);
476477
connect(_clientModel, SIGNAL(numBlocksChanged(int,QDateTime,double,bool)), this, SLOT(setNumBlocks(int,QDateTime,double,bool)));
@@ -489,6 +490,7 @@ void BitcoinGUI::setClientModel(ClientModel *_clientModel)
489490
}
490491
#endif // ENABLE_WALLET
491492
unitDisplayControl->setOptionsModel(_clientModel->getOptionsModel());
493+
connectionsControl->setClientModel(_clientModel);
492494

493495
OptionsModel* optionsModel = _clientModel->getOptionsModel();
494496
if(optionsModel)
@@ -698,8 +700,9 @@ void BitcoinGUI::gotoVerifyMessageTab(QString addr)
698700
}
699701
#endif // ENABLE_WALLET
700702

701-
void BitcoinGUI::setNumConnections(int count)
703+
void BitcoinGUI::updateNetworkState()
702704
{
705+
int count = clientModel->getNumConnections();
703706
QString icon;
704707
switch(count)
705708
{
@@ -709,8 +712,25 @@ void BitcoinGUI::setNumConnections(int count)
709712
case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
710713
default: icon = ":/icons/connect_4"; break;
711714
}
712-
labelConnectionsIcon->setPixmap(platformStyle->SingleColorIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
713-
labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
715+
716+
if (clientModel->getNetworkActive()) {
717+
connectionsControl->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
718+
} else {
719+
connectionsControl->setToolTip(tr("Network activity disabled"));
720+
icon = ":/icons/network_disabled";
721+
}
722+
723+
connectionsControl->setPixmap(platformStyle->SingleColorIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
724+
}
725+
726+
void BitcoinGUI::setNumConnections(int count)
727+
{
728+
updateNetworkState();
729+
}
730+
731+
void BitcoinGUI::setNetworkActive(bool networkActive)
732+
{
733+
updateNetworkState();
714734
}
715735

716736
void BitcoinGUI::setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, bool header)
@@ -1211,3 +1231,18 @@ void UnitDisplayStatusBarControl::onMenuSelection(QAction* action)
12111231
optionsModel->setDisplayUnit(action->data());
12121232
}
12131233
}
1234+
1235+
void NetworkToggleStatusBarControl::mousePressEvent(QMouseEvent *event)
1236+
{
1237+
if (clientModel) {
1238+
clientModel->setNetworkActive(!clientModel->getNetworkActive());
1239+
}
1240+
}
1241+
1242+
/** Lets the control know about the Client Model */
1243+
void NetworkToggleStatusBarControl::setClientModel(ClientModel *_clientModel)
1244+
{
1245+
if (_clientModel) {
1246+
this->clientModel = _clientModel;
1247+
}
1248+
}

src/qt/bitcoingui.h

Lines changed: 20 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;
@@ -85,7 +86,7 @@ class BitcoinGUI : public QMainWindow
8586
UnitDisplayStatusBarControl *unitDisplayControl;
8687
QLabel *labelWalletEncryptionIcon;
8788
QLabel *labelWalletHDStatusIcon;
88-
QLabel *labelConnectionsIcon;
89+
NetworkToggleStatusBarControl *connectionsControl;
8990
QLabel *labelBlocksIcon;
9091
QLabel *progressBarLabel;
9192
QProgressBar *progressBar;
@@ -146,13 +147,18 @@ class BitcoinGUI : public QMainWindow
146147
/** Disconnect core signals from GUI client */
147148
void unsubscribeFromCoreSignals();
148149

150+
/** Update UI with latest network info from model. */
151+
void updateNetworkState();
152+
149153
Q_SIGNALS:
150154
/** Signal raised when a URI was entered or dragged to the GUI */
151155
void receivedURI(const QString &uri);
152156

153157
public Q_SLOTS:
154158
/** Set number of connections shown in the UI */
155159
void setNumConnections(int count);
160+
/** Set network state shown in the UI */
161+
void setNetworkActive(bool networkActive);
156162
/** Set number of blocks and last block date shown in the UI */
157163
void setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, bool headers);
158164

@@ -264,4 +270,17 @@ private Q_SLOTS:
264270
void onMenuSelection(QAction* action);
265271
};
266272

273+
class NetworkToggleStatusBarControl : public QLabel
274+
{
275+
Q_OBJECT
276+
277+
public:
278+
void setClientModel(ClientModel *clientModel);
279+
protected:
280+
void mousePressEvent(QMouseEvent *event);
281+
282+
private:
283+
ClientModel *clientModel;
284+
};
285+
267286
#endif // BITCOIN_QT_BITCOINGUI_H

src/qt/clientmodel.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ void ClientModel::updateNumConnections(int numConnections)
145145
Q_EMIT numConnectionsChanged(numConnections);
146146
}
147147

148+
void ClientModel::updateNetworkActive(bool networkActive)
149+
{
150+
Q_EMIT networkActiveChanged(networkActive);
151+
}
152+
148153
void ClientModel::updateAlert()
149154
{
150155
Q_EMIT alertsChanged(getStatusBarWarnings());
@@ -167,6 +172,21 @@ enum BlockSource ClientModel::getBlockSource() const
167172
return BLOCK_SOURCE_NONE;
168173
}
169174

175+
void ClientModel::setNetworkActive(bool active)
176+
{
177+
if (g_connman) {
178+
g_connman->SetNetworkActive(active);
179+
}
180+
}
181+
182+
bool ClientModel::getNetworkActive() const
183+
{
184+
if (g_connman) {
185+
return g_connman->GetNetworkActive();
186+
}
187+
return false;
188+
}
189+
170190
QString ClientModel::getStatusBarWarnings() const
171191
{
172192
return QString::fromStdString(GetWarnings("gui"));
@@ -233,6 +253,12 @@ static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConn
233253
Q_ARG(int, newNumConnections));
234254
}
235255

256+
static void NotifyNetworkActiveChanged(ClientModel *clientmodel, bool networkActive)
257+
{
258+
QMetaObject::invokeMethod(clientmodel, "updateNetworkActive", Qt::QueuedConnection,
259+
Q_ARG(bool, networkActive));
260+
}
261+
236262
static void NotifyAlertChanged(ClientModel *clientmodel)
237263
{
238264
qDebug() << "NotifyAlertChanged";
@@ -273,6 +299,7 @@ void ClientModel::subscribeToCoreSignals()
273299
// Connect signals to client
274300
uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2));
275301
uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, _1));
302+
uiInterface.NotifyNetworkActiveChanged.connect(boost::bind(NotifyNetworkActiveChanged, this, _1));
276303
uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this));
277304
uiInterface.BannedListChanged.connect(boost::bind(BannedListChanged, this));
278305
uiInterface.NotifyBlockTip.connect(boost::bind(BlockTipChanged, this, _1, _2, false));
@@ -284,6 +311,7 @@ void ClientModel::unsubscribeFromCoreSignals()
284311
// Disconnect signals from client
285312
uiInterface.ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2));
286313
uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, _1));
314+
uiInterface.NotifyNetworkActiveChanged.disconnect(boost::bind(NotifyNetworkActiveChanged, this, _1));
287315
uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this));
288316
uiInterface.BannedListChanged.disconnect(boost::bind(BannedListChanged, this));
289317
uiInterface.NotifyBlockTip.disconnect(boost::bind(BlockTipChanged, this, _1, _2, false));

src/qt/clientmodel.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ class ClientModel : public QObject
6868
bool inInitialBlockDownload() const;
6969
//! Return true if core is importing blocks
7070
enum BlockSource getBlockSource() const;
71+
//! Return true if network activity in core is enabled
72+
bool getNetworkActive() const;
73+
//! Toggle network activity state in core
74+
void setNetworkActive(bool active);
7175
//! Return warnings to be displayed in status bar
7276
QString getStatusBarWarnings() const;
7377

@@ -91,6 +95,7 @@ class ClientModel : public QObject
9195
void numConnectionsChanged(int count);
9296
void numBlocksChanged(int count, const QDateTime& blockDate, double nVerificationProgress, bool header);
9397
void mempoolSizeChanged(long count, size_t mempoolSizeInBytes);
98+
void networkActiveChanged(bool networkActive);
9499
void alertsChanged(const QString &warnings);
95100
void bytesChanged(quint64 totalBytesIn, quint64 totalBytesOut);
96101

@@ -103,6 +108,7 @@ class ClientModel : public QObject
103108
public Q_SLOTS:
104109
void updateTimer();
105110
void updateNumConnections(int numConnections);
111+
void updateNetworkActive(bool networkActive);
106112
void updateAlert();
107113
void updateBanlist();
108114
};

src/qt/res/icons/network_disabled.png

591 Bytes
Loading

0 commit comments

Comments
 (0)