Add PeerTableModel::StatsRole to prevent data layer violation #161

pull hebasto wants to merge 3 commits into bitcoin-core:master from hebasto:201223-ban changing 3 files +17 −27
  1. hebasto commented at 8:22 pm on December 23, 2020: member

    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.

  2. hebasto marked this as a draft on Dec 24, 2020
  3. hebasto force-pushed on Dec 24, 2020
  4. hebasto renamed this:
    Rework RPCConsole::banSelectedNode function
    Add PeerTableModel::StatsRole to prevent data layer violation
    on Dec 24, 2020
  5. hebasto marked this as ready for review on Dec 24, 2020
  6. hebasto commented at 12:47 pm on December 24, 2020: member
    Reworked from scratch.
  7. in src/qt/rpcconsole.cpp:1023 in 4c05fe01ed outdated
    1022     cachedNodeids.clear();
    1023-    for(int i = 0; i < selected.size(); i++)
    1024-    {
    1025-        const CNodeCombinedStats *stats = clientModel->getPeerTableModel()->getNodeStats(selected.at(i).row());
    1026+    for (const auto& peer : GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId)) {
    1027+        const auto stats = peer.data(PeerTableModel::StatsRole).value<CNodeCombinedStats*>();
    


    jonatack commented at 4:23 pm on December 28, 2020:

    6e27ee8b6 To me at least, this would be easier to understand what is going on (and not using auto allows uniform initialization). The same motivation for the other suggestions below.

    0-    for (const auto& peer : GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId)) {
    1-        const auto stats = peer.data(PeerTableModel::StatsRole).value<CNodeCombinedStats*>();
    2+    for (const QModelIndex& peer : GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId)) {
    3+        const CNodeCombinedStats* stats{peer.data(PeerTableModel::StatsRole).value<CNodeCombinedStats*>()};
    

    hebasto commented at 5:30 pm on December 30, 2020:

    AAA Style (Almost Always Auto)

    … easier to understand what is going on

    Maybe GUIUtil::getEntryData requires renaming? GetModelIndexesByColumn? @promag What do you think?


    promag commented at 5:50 pm on December 30, 2020:

    Side node, I’d just ditch getEntryData and hasEntryData, doesn’t improve that much over Qt API. If we keep them then maybe reword to GetSelectedModelIndex or something like that with “selection”.

    In this particular case I think peer should have the type, and leave auto for stats.


    jonatack commented at 6:24 pm on December 30, 2020:
    Re AAA Style, yes, Sutter states the caveat about readability is that people use IDEs. Here, where review is the primary constraint, whatever can aid reviewers in reading and reviewing the code seems like a win, so in practice I’ve found myself using auto only when the type is obvious or when it can avoid a complicated, verbose type. Anyway, just a minor suggestion, feel free to ignore.

    hebasto commented at 7:42 pm on December 30, 2020:
    Thanks! Updated.
  8. in src/qt/rpcconsole.cpp:1082 in 4c05fe01ed outdated
    1078@@ -1081,15 +1079,13 @@ void RPCConsole::peerLayoutChanged()
    1079 
    1080 void RPCConsole::updateDetailWidget()
    1081 {
    1082-    QModelIndexList selected_rows;
    1083-    auto selection_model = ui->peerWidget->selectionModel();
    1084-    if (selection_model) selected_rows = selection_model->selectedRows();
    1085-    if (!clientModel || !clientModel->getPeerTableModel() || selected_rows.size() != 1) {
    1086+    const auto selected_peers = GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId);
    


    jonatack commented at 4:24 pm on December 28, 2020:

    6e27ee8b6

    0    const QList<QModelIndex> selected_peers{GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId)};
    

    hebasto commented at 7:46 pm on December 30, 2020:
  9. in src/qt/rpcconsole.cpp:1088 in 4c05fe01ed outdated
    1088         ui->detailWidget->hide();
    1089         ui->peerHeading->setText(tr("Select a peer to view detailed information."));
    1090         return;
    1091     }
    1092-    const CNodeCombinedStats *stats = clientModel->getPeerTableModel()->getNodeStats(selected_rows.first().row());
    1093+    const auto stats = selected_peers.first().data(PeerTableModel::StatsRole).value<CNodeCombinedStats*>();
    


    jonatack commented at 4:25 pm on December 28, 2020:

    6e27ee8

    0    const CNodeCombinedStats* stats{selected_peers.first().data(PeerTableModel::StatsRole).value<CNodeCombinedStats*>()};
    

    promag commented at 5:37 pm on December 30, 2020:
    Type is already in .value<>(), IIRC auto can be used when the type is clear from the statement.

    jonatack commented at 6:10 pm on December 30, 2020:
    True

    hebasto commented at 7:46 pm on December 30, 2020:
    Marked as “resolved” without changes.
  10. in src/qt/rpcconsole.cpp:1203 in 4c05fe01ed outdated
    1209-        if (detailNodeRow < 0) return;
    1210-
    1211+    for (const auto& peer : GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId)) {
    1212         // Find possible nodes, ban it and clear the selected node
    1213-        const CNodeCombinedStats *stats = clientModel->getPeerTableModel()->getNodeStats(detailNodeRow);
    1214+        const auto stats = peer.data(PeerTableModel::StatsRole).value<CNodeCombinedStats*>();
    


    jonatack commented at 4:26 pm on December 28, 2020:

    6e27ee8b67a8d8a81038b58e8503a3ab0d22f881

    0-    for (const auto& peer : GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId)) {
    1+    for (const QModelIndex& peer : GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId)) {
    2         // Find possible nodes, ban it and clear the selected node
    3-        const auto stats = peer.data(PeerTableModel::StatsRole).value<CNodeCombinedStats*>();
    4+        const CNodeCombinedStats* stats{peer.data(PeerTableModel::StatsRole).value<CNodeCombinedStats*>()};
    

    hebasto commented at 7:47 pm on December 30, 2020:
  11. in src/qt/peertablemodel.h:72 in 4c05fe01ed outdated
    65@@ -66,6 +66,10 @@ class PeerTableModel : public QAbstractTableModel
    66         Subversion = 5
    67     };
    68 
    69+    enum {
    70+        StatsRole = Qt::UserRole
    71+    };
    


    jonatack commented at 4:36 pm on December 28, 2020:
    644f5f5 I’m not very familiar with the gui code, but this enum seems an odd way to do this. Maybe an explanatory comment would be good.


    promag commented at 5:41 pm on December 30, 2020:

    Right, this follow Qt approach, I guess they didn’t want to manually assign a value to each role.

    With multiple roles constexpr looks weird because you’d have to manually increment for each custom role.


    hebasto commented at 6:25 pm on December 30, 2020:

    @promag

    … this follow Qt approach…

    In the recent example they use

    0const int IdRole = Qt::UserRole;
    

    With multiple roles constexpr looks weird because you’d have to manually increment for each custom role.

    Agree.

    Also https://stackoverflow.com/questions/899917/why-do-people-use-enums-in-c-as-constants-while-they-can-use-const


    jonatack commented at 6:28 pm on December 30, 2020:
    Thanks :)
  12. jonatack commented at 4:41 pm on December 28, 2020: contributor
    Light ACK 4c05fe01ed05b549558f135cddbe720ba568ecbc with the caveat that I don’t know the qt framework or this code well. Saw no regression while manually testing. A few suggestions below to aid in reading and understanding the code.
  13. qt: Add PeerTableModel::StatsRole
    This change allows access to CNodeCombinedStats instance directly from
    any view object.
    35007edf9c
  14. qt: Use PeerTableModel::StatsRole
    This change prevents direct calls to the PeerTableModel object that is a
    layer violation.
    49c604077c
  15. qt, refactor: Drop no longer used PeerTableModel::getNodeStats function b3e9bcaac8
  16. hebasto force-pushed on Dec 30, 2020
  17. hebasto commented at 7:42 pm on December 30, 2020: member

    Updated 4c05fe01ed05b549558f135cddbe720ba568ecbc -> b3e9bcaac85a64a1b41d534b28c8cfb1f08e14e5 (pr161.02 -> pr161.03):

    • addressed @jonatack and @promag’s comments
    • rebased to prevent conflicts in pulls that based on this one (#18 and #164)
  18. jonatack commented at 11:45 am on January 2, 2021: contributor

    Tested re-ACK b3e9bcaac85a64a1b41d534b28c8cfb1f08e14e5 per git range-diff ae8f797 4c05fe0 b3e9bca

    Thanks for updating.

  19. MarcoFalke assigned jonasschnelli on Jan 5, 2021
  20. hebasto requested review from promag on Jan 8, 2021
  21. promag approved
  22. promag commented at 4:57 pm on January 8, 2021: contributor

    Code review ACK b3e9bcaac85a64a1b41d534b28c8cfb1f08e14e5.

    As follow ups, maybe it could make sense to split each member of

    0struct CNodeCombinedStats {
    1    CNodeStats nodeStats;
    2    CNodeStateStats nodeStateStats;
    3    bool fNodeStateStatsAvailable;
    4};
    

    on a dedicated role.

    Or just add some other roles like:

    0- const auto stats = peer.data(PeerTableModel::StatsRole).value<CNodeCombinedStats*>();
    1- cachedNodeids.append(stats->nodeStats.nodeid);
    2+ cachedNodeids.append(peer.data(PeerTableModel::NodeIdRole).toInt());
    
  23. jonasschnelli approved
  24. jonasschnelli commented at 8:05 am on January 11, 2021: contributor
    utACK b3e9bcaac85a64a1b41d534b28c8cfb1f08e14e5
  25. jonasschnelli merged this on Jan 11, 2021
  26. jonasschnelli closed this on Jan 11, 2021

  27. hebasto deleted the branch on Jan 11, 2021
  28. sidhujag referenced this in commit a9e45c39e5 on Jan 11, 2021
  29. bitcoin-core locked this on Aug 16, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin-core/gui. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-11-24 02:20 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me