Skip to content

Commit 475c63e

Browse files
hebastojarolrod
authored andcommitted
net: Notify about connection number by type
1 parent f3150f7 commit 475c63e

File tree

6 files changed

+58
-11
lines changed

6 files changed

+58
-11
lines changed

src/interfaces/node.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ enum class SynchronizationState;
3333
enum class TransactionError;
3434
struct CNodeStateStats;
3535
struct bilingual_str;
36+
struct PeersNumByType;
3637
namespace node {
3738
struct NodeContext;
3839
} // namespace node
@@ -238,7 +239,7 @@ class Node
238239
virtual std::unique_ptr<Handler> handleInitWallet(InitWalletFn fn) = 0;
239240

240241
//! Register handler for number of connections changed messages.
241-
using NotifyNumConnectionsChangedFn = std::function<void(int new_num_connections)>;
242+
using NotifyNumConnectionsChangedFn = std::function<void(PeersNumByType new_num_connections)>;
242243
virtual std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) = 0;
243244

244245
//! Register handler for network active messages.

src/net.cpp

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,15 +1133,47 @@ void CConnman::DisconnectNodes()
11331133

11341134
void CConnman::NotifyNumConnectionsChanged()
11351135
{
1136-
size_t nodes_size;
1136+
decltype(m_nodes) nodes_copy;
11371137
{
11381138
LOCK(m_nodes_mutex);
1139-
nodes_size = m_nodes.size();
1139+
nodes_copy = m_nodes;
11401140
}
1141-
if(nodes_size != nPrevNodeCount) {
1142-
nPrevNodeCount = nodes_size;
1141+
1142+
int num_outbound_full_relay{0};
1143+
int num_block_relay{0};
1144+
int num_manual{0};
1145+
int num_inbound{0};
1146+
for (const auto* node : nodes_copy) {
1147+
switch (node->m_conn_type) {
1148+
case ConnectionType::OUTBOUND_FULL_RELAY:
1149+
++num_outbound_full_relay;
1150+
break;
1151+
case ConnectionType::BLOCK_RELAY:
1152+
++num_block_relay;
1153+
break;
1154+
case ConnectionType::MANUAL:
1155+
++num_manual;
1156+
break;
1157+
case ConnectionType::INBOUND:
1158+
++num_inbound;
1159+
break;
1160+
case ConnectionType::FEELER:
1161+
case ConnectionType::ADDR_FETCH:
1162+
break;
1163+
}
1164+
}
1165+
1166+
if (num_outbound_full_relay != m_num_outbound_full_relay ||
1167+
num_block_relay != m_num_block_relay ||
1168+
num_manual != m_num_manual ||
1169+
num_inbound != m_num_inbound) {
1170+
m_num_outbound_full_relay = num_outbound_full_relay;
1171+
m_num_block_relay = num_block_relay;
1172+
m_num_manual = num_manual;
1173+
m_num_inbound = num_inbound;
11431174
if (m_client_interface) {
1144-
m_client_interface->NotifyNumConnectionsChanged(nodes_size);
1175+
m_client_interface->NotifyNumConnectionsChanged(
1176+
{num_outbound_full_relay, num_block_relay, num_manual, num_inbound, static_cast<int>(nodes_copy.size())});
11451177
}
11461178
}
11471179
}

src/net.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class BanMan;
4646
class CNode;
4747
class CScheduler;
4848
struct bilingual_str;
49+
struct PeersNumByType;
4950

5051
/** Default for -whitelistrelay. */
5152
static const bool DEFAULT_WHITELISTRELAY = true;
@@ -1010,7 +1011,11 @@ class CConnman
10101011
std::list<CNode*> m_nodes_disconnected;
10111012
mutable RecursiveMutex m_nodes_mutex;
10121013
std::atomic<NodeId> nLastNodeId{0};
1013-
unsigned int nPrevNodeCount{0};
1014+
int m_num_outbound_full_relay{0};
1015+
int m_num_block_relay{0};
1016+
int m_num_manual{0};
1017+
int m_num_inbound{0};
1018+
10141019

10151020
/**
10161021
* Cache responses to addr requests to minimize privacy leak.

src/node/interface_ui.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ bool CClientUIInterface::ThreadSafeMessageBox(const bilingual_str& message, cons
4848
bool CClientUIInterface::ThreadSafeQuestion(const bilingual_str& message, const std::string& non_interactive_message, const std::string& caption, unsigned int style) { return g_ui_signals.ThreadSafeQuestion(message, non_interactive_message, caption, style).value_or(false);}
4949
void CClientUIInterface::InitMessage(const std::string& message) { return g_ui_signals.InitMessage(message); }
5050
void CClientUIInterface::InitWallet() { return g_ui_signals.InitWallet(); }
51-
void CClientUIInterface::NotifyNumConnectionsChanged(int newNumConnections) { return g_ui_signals.NotifyNumConnectionsChanged(newNumConnections); }
51+
void CClientUIInterface::NotifyNumConnectionsChanged(PeersNumByType newNumConnections) { return g_ui_signals.NotifyNumConnectionsChanged(newNumConnections); }
5252
void CClientUIInterface::NotifyNetworkActiveChanged(bool networkActive) { return g_ui_signals.NotifyNetworkActiveChanged(networkActive); }
5353
void CClientUIInterface::NotifyAlertChanged() { return g_ui_signals.NotifyAlertChanged(); }
5454
void CClientUIInterface::ShowProgress(const std::string& title, int nProgress, bool resume_possible) { return g_ui_signals.ShowProgress(title, nProgress, resume_possible); }

src/node/interface_ui.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ class connection;
2020
}
2121
} // namespace boost
2222

23+
struct PeersNumByType {
24+
int outbound_full_relay{0};
25+
int block_relay{0};
26+
int manual{0};
27+
int inbound{0};
28+
int total{0};
29+
};
30+
2331
/** Signals for UI communication. */
2432
class CClientUIInterface
2533
{
@@ -85,7 +93,7 @@ class CClientUIInterface
8593
ADD_SIGNALS_DECL_WRAPPER(InitWallet, void, );
8694

8795
/** Number of network connections changed. */
88-
ADD_SIGNALS_DECL_WRAPPER(NotifyNumConnectionsChanged, void, int newNumConnections);
96+
ADD_SIGNALS_DECL_WRAPPER(NotifyNumConnectionsChanged, void, PeersNumByType newNumConnections);
8997

9098
/** Network activity state changed. */
9199
ADD_SIGNALS_DECL_WRAPPER(NotifyNetworkActiveChanged, void, bool networkActive);

src/qt/clientmodel.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <interfaces/node.h>
1616
#include <net.h>
1717
#include <netbase.h>
18+
#include <node/interface_ui.h>
1819
#include <util/system.h>
1920
#include <util/threadnames.h>
2021
#include <util/time.h>
@@ -245,8 +246,8 @@ void ClientModel::subscribeToCoreSignals()
245246
Q_EMIT showProgress(QString::fromStdString(title), progress);
246247
});
247248
m_handler_notify_num_connections_changed = m_node.handleNotifyNumConnectionsChanged(
248-
[this](int new_num_connections) {
249-
Q_EMIT numConnectionsChanged(new_num_connections);
249+
[this](PeersNumByType new_num_connections) {
250+
Q_EMIT numConnectionsChanged(new_num_connections.total);
250251
});
251252
m_handler_notify_network_active_changed = m_node.handleNotifyNetworkActiveChanged(
252253
[this](bool network_active) {

0 commit comments

Comments
 (0)