Skip to content

Commit

Permalink
Merge bitcoin-core/gui#161: Add PeerTableModel::StatsRole to prevent …
Browse files Browse the repository at this point in the history
…data layer violation

b3e9bca qt, refactor: Drop no longer used PeerTableModel::getNodeStats function (Hennadii Stepanov)
49c6040 qt: Use PeerTableModel::StatsRole (Hennadii Stepanov)
35007ed qt: Add PeerTableModel::StatsRole (Hennadii Stepanov)

Pull request description:

  This PR allows to access to the `CNodeCombinedStats` instance directly from any view object.

  The `PeerTableModel::getNodeStats` member function removed as a kind of layer violation.

  No behavior changes.

  Also other pulls (bugfixes) are based on this one: #18 and dashpay#164.

ACKs for top commit:
  jonatack:
    Tested re-ACK b3e9bca per `git range-diff ae8f797 4c05fe0 b3e9bca`
  promag:
    Code review ACK b3e9bca.
  jonasschnelli:
    utACK b3e9bca

Tree-SHA512: 6ba50d5dd2c0373655d491ce8b130c47d598da2db5ff4b00633f447404c7e70f8562ead53ddf166e851384d9632ff9146a770c99845c2cdd3ff7250677e4c130
  • Loading branch information
jonasschnelli authored and PastaPastaPasta committed Jun 4, 2024
1 parent 44f237a commit 53f9d2c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 27 deletions.
10 changes: 5 additions & 5 deletions src/qt/peertablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ QVariant PeerTableModel::data(const QModelIndex &index, int role) const
default:
return QVariant();
}
} else if (role == StatsRole) {
switch (index.column()) {
case NetNodeId: return QVariant::fromValue(rec);
default: return QVariant();
}
}

return QVariant();
Expand Down Expand Up @@ -221,11 +226,6 @@ QModelIndex PeerTableModel::index(int row, int column, const QModelIndex &parent
return QModelIndex();
}

const CNodeCombinedStats *PeerTableModel::getNodeStats(int idx)
{
return priv->index(idx);
}

void PeerTableModel::refresh()
{
Q_EMIT layoutAboutToBeChanged();
Expand Down
6 changes: 5 additions & 1 deletion src/qt/peertablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct CNodeCombinedStats {
CNodeStateStats nodeStateStats;
bool fNodeStateStatsAvailable;
};
Q_DECLARE_METATYPE(CNodeCombinedStats*)

class NodeLessThan
{
Expand All @@ -52,7 +53,6 @@ class PeerTableModel : public QAbstractTableModel
public:
explicit PeerTableModel(interfaces::Node& node, QObject* parent);
~PeerTableModel();
const CNodeCombinedStats *getNodeStats(int idx);
int getRowByNodeId(NodeId nodeid);
void startAutoRefresh();
void stopAutoRefresh();
Expand All @@ -67,6 +67,10 @@ class PeerTableModel : public QAbstractTableModel
Subversion = 6
};

enum {
StatsRole = Qt::UserRole,
};

/** @name Methods overridden from QAbstractTableModel
@{*/
int rowCount(const QModelIndex &parent) const override;
Expand Down
28 changes: 7 additions & 21 deletions src/qt/rpcconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1153,11 +1153,9 @@ void RPCConsole::setTrafficGraphRange(TrafficGraphData::GraphRange range)

void RPCConsole::peerLayoutAboutToChange()
{
QModelIndexList selected = ui->peerWidget->selectionModel()->selectedIndexes();
cachedNodeids.clear();
for(int i = 0; i < selected.size(); i++)
{
const CNodeCombinedStats *stats = clientModel->getPeerTableModel()->getNodeStats(selected.at(i).row());
for (const QModelIndex& peer : GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId)) {
const auto stats = peer.data(PeerTableModel::StatsRole).value<CNodeCombinedStats*>();
cachedNodeids.append(stats->nodeStats.nodeid);
}
}
Expand Down Expand Up @@ -1216,15 +1214,13 @@ void RPCConsole::peerLayoutChanged()

void RPCConsole::updateDetailWidget()
{
QModelIndexList selected_rows;
auto selection_model = ui->peerWidget->selectionModel();
if (selection_model) selected_rows = selection_model->selectedRows();
if (!clientModel || !clientModel->getPeerTableModel() || selected_rows.size() != 1) {
const QList<QModelIndex> selected_peers = GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId);
if (!clientModel || !clientModel->getPeerTableModel() || selected_peers.size() != 1) {
ui->detailWidget->hide();
ui->peerHeading->setText(tr("Select a peer to view detailed information."));
return;
}
const CNodeCombinedStats *stats = clientModel->getPeerTableModel()->getNodeStats(selected_rows.first().row());
const auto stats = selected_peers.first().data(PeerTableModel::StatsRole).value<CNodeCombinedStats*>();
// update the detail ui with latest node information
QString peerAddrDetails(QString::fromStdString(stats->nodeStats.m_addr_name) + " ");
peerAddrDetails += tr("(peer: %1)").arg(QString::number(stats->nodeStats.nodeid));
Expand Down Expand Up @@ -1383,19 +1379,9 @@ void RPCConsole::banSelectedNode(int bantime)
if (!clientModel)
return;

// Get selected peer addresses
QList<QModelIndex> nodes = GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId);
for(int i = 0; i < nodes.count(); i++)
{
// Get currently selected peer address
NodeId id = nodes.at(i).data().toLongLong();

// Get currently selected peer address
int detailNodeRow = clientModel->getPeerTableModel()->getRowByNodeId(id);
if (detailNodeRow < 0) return;

for (const QModelIndex& peer : GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId)) {
// Find possible nodes, ban it and clear the selected node
const CNodeCombinedStats *stats = clientModel->getPeerTableModel()->getNodeStats(detailNodeRow);
const auto stats = peer.data(PeerTableModel::StatsRole).value<CNodeCombinedStats*>();
if (stats) {
m_node.ban(stats->nodeStats.addr, bantime);
m_node.disconnectByAddress(stats->nodeStats.addr);
Expand Down

0 comments on commit 53f9d2c

Please sign in to comment.