Remove direct bitcoin calls from qt/peertablemodel.cpp

This commit is contained in:
Russell Yanofsky 2017-04-17 15:57:19 -04:00 committed by John Newbery
parent d7c2c95948
commit e0b66a3b7c
6 changed files with 57 additions and 28 deletions

View file

@ -10,6 +10,7 @@
#include <interface/handler.h> #include <interface/handler.h>
#include <interface/wallet.h> #include <interface/wallet.h>
#include <net.h> #include <net.h>
#include <net_processing.h>
#include <netaddress.h> #include <netaddress.h>
#include <netbase.h> #include <netbase.h>
#include <primitives/block.h> #include <primitives/block.h>
@ -79,6 +80,31 @@ class NodeImpl : public Node
{ {
return g_connman ? g_connman->GetNodeCount(flags) : 0; return g_connman ? g_connman->GetNodeCount(flags) : 0;
} }
bool getNodesStats(NodesStats& stats) override
{
stats.clear();
if (g_connman) {
std::vector<CNodeStats> stats_temp;
g_connman->GetNodeStats(stats_temp);
stats.reserve(stats_temp.size());
for (auto& node_stats_temp : stats_temp) {
stats.emplace_back(std::move(node_stats_temp), false, CNodeStateStats());
}
// Try to retrieve the CNodeStateStats for each node.
TRY_LOCK(::cs_main, lockMain);
if (lockMain) {
for (auto& node_stats : stats) {
std::get<1>(node_stats) =
GetNodeStateStats(std::get<0>(node_stats).nodeid, std::get<2>(node_stats));
}
}
return true;
}
return false;
}
int64_t getTotalBytesRecv() override { return g_connman ? g_connman->GetTotalBytesRecv() : 0; } int64_t getTotalBytesRecv() override { return g_connman ? g_connman->GetTotalBytesRecv() : 0; }
int64_t getTotalBytesSent() override { return g_connman ? g_connman->GetTotalBytesSent() : 0; } int64_t getTotalBytesSent() override { return g_connman ? g_connman->GetTotalBytesSent() : 0; }
size_t getMempoolSize() override { return ::mempool.size(); } size_t getMempoolSize() override { return ::mempool.size(); }

View file

@ -14,8 +14,12 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <string> #include <string>
#include <tuple>
#include <vector>
class CNodeStats;
class proxyType; class proxyType;
struct CNodeStateStats;
namespace interface { namespace interface {
@ -79,6 +83,10 @@ public:
//! Get number of connections. //! Get number of connections.
virtual size_t getNodeCount(CConnman::NumConnections flags) = 0; virtual size_t getNodeCount(CConnman::NumConnections flags) = 0;
//! Get stats for connected nodes.
using NodesStats = std::vector<std::tuple<CNodeStats, bool, CNodeStateStats>>;
virtual bool getNodesStats(NodesStats& stats) = 0;
//! Get total bytes recv. //! Get total bytes recv.
virtual int64_t getTotalBytesRecv() = 0; virtual int64_t getTotalBytesRecv() = 0;

View file

@ -86,9 +86,9 @@ private:
}; };
struct CNodeStateStats { struct CNodeStateStats {
int nMisbehavior; int nMisbehavior = 0;
int nSyncHeight; int nSyncHeight = -1;
int nCommonHeight; int nCommonHeight = -1;
std::vector<int> vHeightInFlight; std::vector<int> vHeightInFlight;
}; };

View file

@ -42,7 +42,7 @@ ClientModel::ClientModel(interface::Node& node, OptionsModel *_optionsModel, QOb
{ {
cachedBestHeaderHeight = -1; cachedBestHeaderHeight = -1;
cachedBestHeaderTime = -1; cachedBestHeaderTime = -1;
peerTableModel = new PeerTableModel(this); peerTableModel = new PeerTableModel(m_node, this);
banTableModel = new BanTableModel(this); banTableModel = new BanTableModel(this);
pollTimer = new QTimer(this); pollTimer = new QTimer(this);
connect(pollTimer, SIGNAL(timeout()), this, SLOT(updateTimer())); connect(pollTimer, SIGNAL(timeout()), this, SLOT(updateTimer()));

View file

@ -8,6 +8,7 @@
#include <qt/guiconstants.h> #include <qt/guiconstants.h>
#include <qt/guiutil.h> #include <qt/guiutil.h>
#include <interface/node.h>
#include <validation.h> // for cs_main #include <validation.h> // for cs_main
#include <sync.h> #include <sync.h>
@ -56,38 +57,26 @@ public:
std::map<NodeId, int> mapNodeRows; std::map<NodeId, int> mapNodeRows;
/** Pull a full list of peers from vNodes into our cache */ /** Pull a full list of peers from vNodes into our cache */
void refreshPeers() void refreshPeers(interface::Node& node)
{ {
{ {
cachedNodeStats.clear(); cachedNodeStats.clear();
std::vector<CNodeStats> vstats;
if(g_connman) interface::Node::NodesStats nodes_stats;
g_connman->GetNodeStats(vstats); node.getNodesStats(nodes_stats);
#if QT_VERSION >= 0x040700 #if QT_VERSION >= 0x040700
cachedNodeStats.reserve(vstats.size()); cachedNodeStats.reserve(nodes_stats.size());
#endif #endif
for (const CNodeStats& nodestats : vstats) for (auto& node_stats : nodes_stats)
{ {
CNodeCombinedStats stats; CNodeCombinedStats stats;
stats.nodeStateStats.nMisbehavior = 0; stats.nodeStats = std::get<0>(node_stats);
stats.nodeStateStats.nSyncHeight = -1; stats.fNodeStateStatsAvailable = std::get<1>(node_stats);
stats.nodeStateStats.nCommonHeight = -1; stats.nodeStateStats = std::get<2>(node_stats);
stats.fNodeStateStatsAvailable = false;
stats.nodeStats = nodestats;
cachedNodeStats.append(stats); cachedNodeStats.append(stats);
} }
} }
// Try to retrieve the CNodeStateStats for each node.
{
TRY_LOCK(cs_main, lockMain);
if (lockMain)
{
for (CNodeCombinedStats &stats : cachedNodeStats)
stats.fNodeStateStatsAvailable = GetNodeStateStats(stats.nodeStats.nodeid, stats.nodeStateStats);
}
}
if (sortColumn >= 0) if (sortColumn >= 0)
// sort cacheNodeStats (use stable sort to prevent rows jumping around unnecessarily) // sort cacheNodeStats (use stable sort to prevent rows jumping around unnecessarily)
qStableSort(cachedNodeStats.begin(), cachedNodeStats.end(), NodeLessThan(sortColumn, sortOrder)); qStableSort(cachedNodeStats.begin(), cachedNodeStats.end(), NodeLessThan(sortColumn, sortOrder));
@ -113,8 +102,9 @@ public:
} }
}; };
PeerTableModel::PeerTableModel(ClientModel *parent) : PeerTableModel::PeerTableModel(interface::Node& node, ClientModel *parent) :
QAbstractTableModel(parent), QAbstractTableModel(parent),
m_node(node),
clientModel(parent), clientModel(parent),
timer(0) timer(0)
{ {
@ -235,7 +225,7 @@ const CNodeCombinedStats *PeerTableModel::getNodeStats(int idx)
void PeerTableModel::refresh() void PeerTableModel::refresh()
{ {
Q_EMIT layoutAboutToBeChanged(); Q_EMIT layoutAboutToBeChanged();
priv->refreshPeers(); priv->refreshPeers(m_node);
Q_EMIT layoutChanged(); Q_EMIT layoutChanged();
} }

View file

@ -14,6 +14,10 @@
class ClientModel; class ClientModel;
class PeerTablePriv; class PeerTablePriv;
namespace interface {
class Node;
}
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QTimer; class QTimer;
QT_END_NAMESPACE QT_END_NAMESPACE
@ -45,7 +49,7 @@ class PeerTableModel : public QAbstractTableModel
Q_OBJECT Q_OBJECT
public: public:
explicit PeerTableModel(ClientModel *parent = 0); explicit PeerTableModel(interface::Node& node, ClientModel *parent = 0);
~PeerTableModel(); ~PeerTableModel();
const CNodeCombinedStats *getNodeStats(int idx); const CNodeCombinedStats *getNodeStats(int idx);
int getRowByNodeId(NodeId nodeid); int getRowByNodeId(NodeId nodeid);
@ -76,6 +80,7 @@ public Q_SLOTS:
void refresh(); void refresh();
private: private:
interface::Node& m_node;
ClientModel *clientModel; ClientModel *clientModel;
QStringList columns; QStringList columns;
std::unique_ptr<PeerTablePriv> priv; std::unique_ptr<PeerTablePriv> priv;