Skip to content

Commit 22838d2

Browse files
committed
Merge #236: Introduce PeersIndicator component, Add to BlockClock
c7c73b8 qml: add PeersIndicator to BlockClock (jarolrod) 4f3368a qml: Add `PeersIndicator` component (Hennadii Stepanov) b22b88b qml: Add `NodeModel::numOutboundPeers` property (Hennadii Stepanov) 475c63e net: Notify about connection number by type (Hennadii Stepanov) Pull request description: This is a rebased version of, and replaces, #127 Introduces the actual `PeersIndicator` component and adds it to the `BlockClock` | Dark Mode | Light Mode | | --------- | ---------- | | <img width="752" alt="Screen Shot 2023-01-31 at 12 48 23 AM" src="https://user-images.githubusercontent.com/23396902/215676831-6573a94f-2030-4bf2-b45b-9d325be42600.png"> | <img width="752" alt="Screen Shot 2023-01-31 at 12 49 09 AM" src="https://user-images.githubusercontent.com/23396902/215676882-96f82c03-43ff-449d-b5d9-d7016fb0540b.png"> | [![Windows](https://img.shields.io/badge/OS-Windows-green)](https://api.cirrus-ci.com/v1/artifact/github/bitcoin-core/gui-qml/win64/insecure_win_gui.zip?branch=pull/236) [![Intel macOS](https://img.shields.io/badge/OS-Intel%20macOS-green)](https://api.cirrus-ci.com/v1/artifact/github/bitcoin-core/gui-qml/macos/insecure_mac_gui.zip?branch=pull/236) [![Apple Silicon macOS](https://img.shields.io/badge/OS-Apple%20Silicon%20macOS-green)](https://api.cirrus-ci.com/v1/artifact/github/bitcoin-core/gui-qml/macos_arm64/insecure_mac_arm64_gui.zip?branch=pull/236) [![ARM64 Android](https://img.shields.io/badge/OS-Android-green)](https://api.cirrus-ci.com/v1/artifact/github/bitcoin-core/gui-qml/android/insecure_android_apk.zip?branch=pull/236) ACKs for top commit: johnny9: ACK c7c73b8 Tree-SHA512: f5af2cd53bcecdb9441f412e94ad6557c48aad7998c2da69f1e0174682a0761151ca72b27a094b12d3f9055dff61694b9205297f5a4db9559bd0ed0ff3c4fdca
2 parents e8799fd + c7c73b8 commit 22838d2

File tree

12 files changed

+127
-23
lines changed

12 files changed

+127
-23
lines changed

src/Makefile.qt.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ QML_RES_QML = \
333333
qml/components/ConnectionOptions.qml \
334334
qml/components/ConnectionSettings.qml \
335335
qml/components/DeveloperOptions.qml \
336+
qml/components/PeersIndicator.qml \
336337
qml/components/StorageLocations.qml \
337338
qml/components/StorageOptions.qml \
338339
qml/components/StorageSettings.qml \

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
@@ -47,6 +47,7 @@ class BanMan;
4747
class CNode;
4848
class CScheduler;
4949
struct bilingual_str;
50+
struct PeersNumByType;
5051

5152
/** Default for -whitelistrelay. */
5253
static const bool DEFAULT_WHITELISTRELAY = true;
@@ -1017,7 +1018,11 @@ class CConnman
10171018
std::list<CNode*> m_nodes_disconnected;
10181019
mutable RecursiveMutex m_nodes_mutex;
10191020
std::atomic<NodeId> nLastNodeId{0};
1020-
unsigned int nPrevNodeCount{0};
1021+
int m_num_outbound_full_relay{0};
1022+
int m_num_block_relay{0};
1023+
int m_num_manual{0};
1024+
int m_num_inbound{0};
1025+
10211026

10221027
/**
10231028
* 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/qml/bitcoin_qml.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<file>components/BlockCounter.qml</file>
66
<file>components/ConnectionOptions.qml</file>
77
<file>components/ConnectionSettings.qml</file>
8+
<file>components/PeersIndicator.qml</file>
89
<file>components/DeveloperOptions.qml</file>
910
<file>components/StorageLocations.qml</file>
1011
<file>components/StorageOptions.qml</file>

src/qml/components/BlockClock.qml

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,12 @@ Item {
6666
color: Theme.color.neutral4
6767
}
6868

69-
RowLayout {
70-
id: peersIndicator
69+
PeersIndicator {
7170
anchors.top: subText.bottom
7271
anchors.topMargin: 20
7372
anchors.horizontalCenter: root.horizontalCenter
74-
spacing: 5
75-
Repeater {
76-
model: 5
77-
Rectangle {
78-
width: 3
79-
height: width
80-
radius: width/2
81-
color: Theme.color.neutral9
82-
}
83-
}
73+
numOutboundPeers: nodeModel.numOutboundPeers
74+
maxNumOutboundPeers: nodeModel.maxNumOutboundPeers
8475
}
8576

8677
MouseArea {

src/qml/components/PeersIndicator.qml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2023 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
import QtQuick 2.15
6+
import QtQuick.Layouts 1.15
7+
import "../controls"
8+
9+
RowLayout {
10+
id: root
11+
required property int numOutboundPeers
12+
required property int maxNumOutboundPeers
13+
property int size: 5
14+
15+
spacing: 5
16+
Repeater {
17+
model: 5
18+
Rectangle {
19+
width: 3
20+
height: 3
21+
radius: width / 2
22+
color: Theme.color.neutral9
23+
opacity: (index === 0 && root.numOutboundPeers > 0) || (index + 1 <= root.size * root.numOutboundPeers / root.maxNumOutboundPeers) ? 0.95 : 0.45
24+
Behavior on opacity { OpacityAnimator { duration: 100 } }
25+
SequentialAnimation on opacity {
26+
loops: Animation.Infinite
27+
running: numOutboundPeers === 0 && index === 0
28+
SmoothedAnimation { to: 0; velocity: 2.2 }
29+
SmoothedAnimation { to: 1; velocity: 2.2 }
30+
}
31+
}
32+
}
33+
}

src/qml/nodemodel.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <qml/nodemodel.h>
66

77
#include <interfaces/node.h>
8+
#include <net.h>
9+
#include <node/interface_ui.h>
810
#include <validation.h>
911

1012
#include <cassert>
@@ -18,6 +20,7 @@ NodeModel::NodeModel(interfaces::Node& node)
1820
: m_node{node}
1921
{
2022
ConnectToBlockTipSignal();
23+
ConnectToNumConnectionsChangedSignal();
2124
}
2225

2326
void NodeModel::setBlockTipHeight(int new_height)
@@ -28,6 +31,14 @@ void NodeModel::setBlockTipHeight(int new_height)
2831
}
2932
}
3033

34+
void NodeModel::setNumOutboundPeers(int new_num)
35+
{
36+
if (new_num != m_num_outbound_peers) {
37+
m_num_outbound_peers = new_num;
38+
Q_EMIT numOutboundPeersChanged();
39+
}
40+
}
41+
3142
void NodeModel::setRemainingSyncTime(double new_progress)
3243
{
3344
int currentTime = QDateTime::currentDateTime().toMSecsSinceEpoch();
@@ -129,3 +140,13 @@ void NodeModel::ConnectToBlockTipSignal()
129140
});
130141
});
131142
}
143+
144+
void NodeModel::ConnectToNumConnectionsChangedSignal()
145+
{
146+
assert(!m_handler_notify_num_peers_changed);
147+
148+
m_handler_notify_num_peers_changed = m_node.handleNotifyNumConnectionsChanged(
149+
[this](PeersNumByType new_num_peers) {
150+
setNumOutboundPeers(new_num_peers.outbound_full_relay + new_num_peers.block_relay);
151+
});
152+
}

src/qml/nodemodel.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class NodeModel : public QObject
2525
{
2626
Q_OBJECT
2727
Q_PROPERTY(int blockTipHeight READ blockTipHeight NOTIFY blockTipHeightChanged)
28+
Q_PROPERTY(int numOutboundPeers READ numOutboundPeers NOTIFY numOutboundPeersChanged)
29+
Q_PROPERTY(int maxNumOutboundPeers READ maxNumOutboundPeers CONSTANT)
2830
Q_PROPERTY(int remainingSyncTime READ remainingSyncTime NOTIFY remainingSyncTimeChanged)
2931
Q_PROPERTY(double verificationProgress READ verificationProgress NOTIFY verificationProgressChanged)
3032
Q_PROPERTY(bool pause READ pause WRITE setPause NOTIFY pauseChanged)
@@ -34,6 +36,9 @@ class NodeModel : public QObject
3436

3537
int blockTipHeight() const { return m_block_tip_height; }
3638
void setBlockTipHeight(int new_height);
39+
int numOutboundPeers() const { return m_num_outbound_peers; }
40+
void setNumOutboundPeers(int new_num);
41+
int maxNumOutboundPeers() const { return m_max_num_outbound_peers; }
3742
int remainingSyncTime() const { return m_remaining_sync_time; }
3843
void setRemainingSyncTime(double new_progress);
3944
double verificationProgress() const { return m_verification_progress; }
@@ -51,6 +56,7 @@ public Q_SLOTS:
5156

5257
Q_SIGNALS:
5358
void blockTipHeightChanged();
59+
void numOutboundPeersChanged();
5460
void remainingSyncTimeChanged();
5561
void requestedInitialize();
5662
void requestedShutdown();
@@ -66,6 +72,8 @@ public Q_SLOTS:
6672
private:
6773
// Properties that are exposed to QML.
6874
int m_block_tip_height{0};
75+
int m_num_outbound_peers{0};
76+
static constexpr int m_max_num_outbound_peers{MAX_OUTBOUND_FULL_RELAY_CONNECTIONS + MAX_BLOCK_RELAY_ONLY_CONNECTIONS};
6977
int m_remaining_sync_time{0};
7078
double m_verification_progress{0.0};
7179
bool m_pause{false};
@@ -76,8 +84,10 @@ public Q_SLOTS:
7684

7785
interfaces::Node& m_node;
7886
std::unique_ptr<interfaces::Handler> m_handler_notify_block_tip;
87+
std::unique_ptr<interfaces::Handler> m_handler_notify_num_peers_changed;
7988

8089
void ConnectToBlockTipSignal();
90+
void ConnectToNumConnectionsChangedSignal();
8191
};
8292

8393
#endif // BITCOIN_QML_NODEMODEL_H

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>
@@ -243,8 +244,8 @@ void ClientModel::subscribeToCoreSignals()
243244
Q_EMIT showProgress(QString::fromStdString(title), progress);
244245
});
245246
m_handler_notify_num_connections_changed = m_node.handleNotifyNumConnectionsChanged(
246-
[this](int new_num_connections) {
247-
Q_EMIT numConnectionsChanged(new_num_connections);
247+
[this](PeersNumByType new_num_connections) {
248+
Q_EMIT numConnectionsChanged(new_num_connections.total);
248249
});
249250
m_handler_notify_network_active_changed = m_node.handleNotifyNetworkActiveChanged(
250251
[this](bool network_active) {

0 commit comments

Comments
 (0)