Skip to content

Commit e0b66a3

Browse files
ryanofskyjnewbery
authored andcommitted
Remove direct bitcoin calls from qt/peertablemodel.cpp
1 parent d7c2c95 commit e0b66a3

File tree

6 files changed

+57
-28
lines changed

6 files changed

+57
-28
lines changed

src/interface/node.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <interface/handler.h>
1111
#include <interface/wallet.h>
1212
#include <net.h>
13+
#include <net_processing.h>
1314
#include <netaddress.h>
1415
#include <netbase.h>
1516
#include <primitives/block.h>
@@ -79,6 +80,31 @@ class NodeImpl : public Node
7980
{
8081
return g_connman ? g_connman->GetNodeCount(flags) : 0;
8182
}
83+
bool getNodesStats(NodesStats& stats) override
84+
{
85+
stats.clear();
86+
87+
if (g_connman) {
88+
std::vector<CNodeStats> stats_temp;
89+
g_connman->GetNodeStats(stats_temp);
90+
91+
stats.reserve(stats_temp.size());
92+
for (auto& node_stats_temp : stats_temp) {
93+
stats.emplace_back(std::move(node_stats_temp), false, CNodeStateStats());
94+
}
95+
96+
// Try to retrieve the CNodeStateStats for each node.
97+
TRY_LOCK(::cs_main, lockMain);
98+
if (lockMain) {
99+
for (auto& node_stats : stats) {
100+
std::get<1>(node_stats) =
101+
GetNodeStateStats(std::get<0>(node_stats).nodeid, std::get<2>(node_stats));
102+
}
103+
}
104+
return true;
105+
}
106+
return false;
107+
}
82108
int64_t getTotalBytesRecv() override { return g_connman ? g_connman->GetTotalBytesRecv() : 0; }
83109
int64_t getTotalBytesSent() override { return g_connman ? g_connman->GetTotalBytesSent() : 0; }
84110
size_t getMempoolSize() override { return ::mempool.size(); }

src/interface/node.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
#include <stddef.h>
1515
#include <stdint.h>
1616
#include <string>
17+
#include <tuple>
18+
#include <vector>
1719

20+
class CNodeStats;
1821
class proxyType;
22+
struct CNodeStateStats;
1923

2024
namespace interface {
2125

@@ -79,6 +83,10 @@ class Node
7983
//! Get number of connections.
8084
virtual size_t getNodeCount(CConnman::NumConnections flags) = 0;
8185

86+
//! Get stats for connected nodes.
87+
using NodesStats = std::vector<std::tuple<CNodeStats, bool, CNodeStateStats>>;
88+
virtual bool getNodesStats(NodesStats& stats) = 0;
89+
8290
//! Get total bytes recv.
8391
virtual int64_t getTotalBytesRecv() = 0;
8492

src/net_processing.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ class PeerLogicValidation final : public CValidationInterface, public NetEventsI
8686
};
8787

8888
struct CNodeStateStats {
89-
int nMisbehavior;
90-
int nSyncHeight;
91-
int nCommonHeight;
89+
int nMisbehavior = 0;
90+
int nSyncHeight = -1;
91+
int nCommonHeight = -1;
9292
std::vector<int> vHeightInFlight;
9393
};
9494

src/qt/clientmodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ ClientModel::ClientModel(interface::Node& node, OptionsModel *_optionsModel, QOb
4242
{
4343
cachedBestHeaderHeight = -1;
4444
cachedBestHeaderTime = -1;
45-
peerTableModel = new PeerTableModel(this);
45+
peerTableModel = new PeerTableModel(m_node, this);
4646
banTableModel = new BanTableModel(this);
4747
pollTimer = new QTimer(this);
4848
connect(pollTimer, SIGNAL(timeout()), this, SLOT(updateTimer()));

src/qt/peertablemodel.cpp

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <qt/guiconstants.h>
99
#include <qt/guiutil.h>
1010

11+
#include <interface/node.h>
1112
#include <validation.h> // for cs_main
1213
#include <sync.h>
1314

@@ -56,38 +57,26 @@ class PeerTablePriv
5657
std::map<NodeId, int> mapNodeRows;
5758

5859
/** Pull a full list of peers from vNodes into our cache */
59-
void refreshPeers()
60+
void refreshPeers(interface::Node& node)
6061
{
6162
{
6263
cachedNodeStats.clear();
63-
std::vector<CNodeStats> vstats;
64-
if(g_connman)
65-
g_connman->GetNodeStats(vstats);
64+
65+
interface::Node::NodesStats nodes_stats;
66+
node.getNodesStats(nodes_stats);
6667
#if QT_VERSION >= 0x040700
67-
cachedNodeStats.reserve(vstats.size());
68+
cachedNodeStats.reserve(nodes_stats.size());
6869
#endif
69-
for (const CNodeStats& nodestats : vstats)
70+
for (auto& node_stats : nodes_stats)
7071
{
7172
CNodeCombinedStats stats;
72-
stats.nodeStateStats.nMisbehavior = 0;
73-
stats.nodeStateStats.nSyncHeight = -1;
74-
stats.nodeStateStats.nCommonHeight = -1;
75-
stats.fNodeStateStatsAvailable = false;
76-
stats.nodeStats = nodestats;
73+
stats.nodeStats = std::get<0>(node_stats);
74+
stats.fNodeStateStatsAvailable = std::get<1>(node_stats);
75+
stats.nodeStateStats = std::get<2>(node_stats);
7776
cachedNodeStats.append(stats);
7877
}
7978
}
8079

81-
// Try to retrieve the CNodeStateStats for each node.
82-
{
83-
TRY_LOCK(cs_main, lockMain);
84-
if (lockMain)
85-
{
86-
for (CNodeCombinedStats &stats : cachedNodeStats)
87-
stats.fNodeStateStatsAvailable = GetNodeStateStats(stats.nodeStats.nodeid, stats.nodeStateStats);
88-
}
89-
}
90-
9180
if (sortColumn >= 0)
9281
// sort cacheNodeStats (use stable sort to prevent rows jumping around unnecessarily)
9382
qStableSort(cachedNodeStats.begin(), cachedNodeStats.end(), NodeLessThan(sortColumn, sortOrder));
@@ -113,8 +102,9 @@ class PeerTablePriv
113102
}
114103
};
115104

116-
PeerTableModel::PeerTableModel(ClientModel *parent) :
105+
PeerTableModel::PeerTableModel(interface::Node& node, ClientModel *parent) :
117106
QAbstractTableModel(parent),
107+
m_node(node),
118108
clientModel(parent),
119109
timer(0)
120110
{
@@ -235,7 +225,7 @@ const CNodeCombinedStats *PeerTableModel::getNodeStats(int idx)
235225
void PeerTableModel::refresh()
236226
{
237227
Q_EMIT layoutAboutToBeChanged();
238-
priv->refreshPeers();
228+
priv->refreshPeers(m_node);
239229
Q_EMIT layoutChanged();
240230
}
241231

src/qt/peertablemodel.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
class ClientModel;
1515
class PeerTablePriv;
1616

17+
namespace interface {
18+
class Node;
19+
}
20+
1721
QT_BEGIN_NAMESPACE
1822
class QTimer;
1923
QT_END_NAMESPACE
@@ -45,7 +49,7 @@ class PeerTableModel : public QAbstractTableModel
4549
Q_OBJECT
4650

4751
public:
48-
explicit PeerTableModel(ClientModel *parent = 0);
52+
explicit PeerTableModel(interface::Node& node, ClientModel *parent = 0);
4953
~PeerTableModel();
5054
const CNodeCombinedStats *getNodeStats(int idx);
5155
int getRowByNodeId(NodeId nodeid);
@@ -76,6 +80,7 @@ public Q_SLOTS:
7680
void refresh();
7781

7882
private:
83+
interface::Node& m_node;
7984
ClientModel *clientModel;
8085
QStringList columns;
8186
std::unique_ptr<PeerTablePriv> priv;

0 commit comments

Comments
 (0)