Skip to content

Commit a9e45c3

Browse files
jonasschnellijagdeep sidhu
authored and
jagdeep sidhu
committed
Merge bitcoin-core/gui#161: Add PeerTableModel::StatsRole to prevent 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 #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
1 parent da64cb0 commit a9e45c3

File tree

3 files changed

+17
-27
lines changed

3 files changed

+17
-27
lines changed

src/qt/peertablemodel.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ QVariant PeerTableModel::data(const QModelIndex &index, int role) const
185185
default:
186186
return QVariant();
187187
}
188+
} else if (role == StatsRole) {
189+
switch (index.column()) {
190+
case NetNodeId: return QVariant::fromValue(rec);
191+
default: return QVariant();
192+
}
188193
}
189194

190195
return QVariant();
@@ -220,11 +225,6 @@ QModelIndex PeerTableModel::index(int row, int column, const QModelIndex &parent
220225
return QModelIndex();
221226
}
222227

223-
const CNodeCombinedStats *PeerTableModel::getNodeStats(int idx)
224-
{
225-
return priv->index(idx);
226-
}
227-
228228
void PeerTableModel::refresh()
229229
{
230230
Q_EMIT layoutAboutToBeChanged();

src/qt/peertablemodel.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct CNodeCombinedStats {
2828
CNodeStateStats nodeStateStats;
2929
bool fNodeStateStatsAvailable;
3030
};
31+
Q_DECLARE_METATYPE(CNodeCombinedStats*)
3132

3233
class NodeLessThan
3334
{
@@ -52,7 +53,6 @@ class PeerTableModel : public QAbstractTableModel
5253
public:
5354
explicit PeerTableModel(interfaces::Node& node, QObject* parent);
5455
~PeerTableModel();
55-
const CNodeCombinedStats *getNodeStats(int idx);
5656
int getRowByNodeId(NodeId nodeid);
5757
void startAutoRefresh();
5858
void stopAutoRefresh();
@@ -67,6 +67,10 @@ class PeerTableModel : public QAbstractTableModel
6767
Subversion = 6
6868
};
6969

70+
enum {
71+
StatsRole = Qt::UserRole,
72+
};
73+
7074
/** @name Methods overridden from QAbstractTableModel
7175
@{*/
7276
int rowCount(const QModelIndex &parent) const override;

src/qt/rpcconsole.cpp

+7-21
Original file line numberDiff line numberDiff line change
@@ -1068,11 +1068,9 @@ void RPCConsole::updateTrafficStats(quint64 totalBytesIn, quint64 totalBytesOut)
10681068

10691069
void RPCConsole::peerLayoutAboutToChange()
10701070
{
1071-
QModelIndexList selected = ui->peerWidget->selectionModel()->selectedIndexes();
10721071
cachedNodeids.clear();
1073-
for(int i = 0; i < selected.size(); i++)
1074-
{
1075-
const CNodeCombinedStats *stats = clientModel->getPeerTableModel()->getNodeStats(selected.at(i).row());
1072+
for (const QModelIndex& peer : GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId)) {
1073+
const auto stats = peer.data(PeerTableModel::StatsRole).value<CNodeCombinedStats*>();
10761074
cachedNodeids.append(stats->nodeStats.nodeid);
10771075
}
10781076
}
@@ -1131,15 +1129,13 @@ void RPCConsole::peerLayoutChanged()
11311129

11321130
void RPCConsole::updateDetailWidget()
11331131
{
1134-
QModelIndexList selected_rows;
1135-
auto selection_model = ui->peerWidget->selectionModel();
1136-
if (selection_model) selected_rows = selection_model->selectedRows();
1137-
if (!clientModel || !clientModel->getPeerTableModel() || selected_rows.size() != 1) {
1132+
const QList<QModelIndex> selected_peers = GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId);
1133+
if (!clientModel || !clientModel->getPeerTableModel() || selected_peers.size() != 1) {
11381134
ui->detailWidget->hide();
11391135
ui->peerHeading->setText(tr("Select a peer to view detailed information."));
11401136
return;
11411137
}
1142-
const CNodeCombinedStats *stats = clientModel->getPeerTableModel()->getNodeStats(selected_rows.first().row());
1138+
const auto stats = selected_peers.first().data(PeerTableModel::StatsRole).value<CNodeCombinedStats*>();
11431139
// update the detail ui with latest node information
11441140
QString peerAddrDetails(QString::fromStdString(stats->nodeStats.addrName) + " ");
11451141
peerAddrDetails += tr("(peer id: %1)").arg(QString::number(stats->nodeStats.nodeid));
@@ -1252,19 +1248,9 @@ void RPCConsole::banSelectedNode(int bantime)
12521248
if (!clientModel)
12531249
return;
12541250

1255-
// Get selected peer addresses
1256-
QList<QModelIndex> nodes = GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId);
1257-
for(int i = 0; i < nodes.count(); i++)
1258-
{
1259-
// Get currently selected peer address
1260-
NodeId id = nodes.at(i).data().toLongLong();
1261-
1262-
// Get currently selected peer address
1263-
int detailNodeRow = clientModel->getPeerTableModel()->getRowByNodeId(id);
1264-
if (detailNodeRow < 0) return;
1265-
1251+
for (const QModelIndex& peer : GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId)) {
12661252
// Find possible nodes, ban it and clear the selected node
1267-
const CNodeCombinedStats *stats = clientModel->getPeerTableModel()->getNodeStats(detailNodeRow);
1253+
const auto stats = peer.data(PeerTableModel::StatsRole).value<CNodeCombinedStats*>();
12681254
if (stats) {
12691255
m_node.ban(stats->nodeStats.addr, bantime);
12701256
m_node.disconnectByAddress(stats->nodeStats.addr);

0 commit comments

Comments
 (0)