refactor: Revamp ClientModel code to handle core signals #581

pull hebasto wants to merge 7 commits into bitcoin-core:master from hebasto:220410-invoke changing 2 files +42 −90
  1. hebasto commented at 5:20 pm on April 10, 2022: member

    This PR:

    • is a pure refactoring with no behavior change
    • gets rid of QMetaObject::invokeMethod() “dynamic” calls, i.e., without compile-time checks of a called function name and its parameters
    • replaces std::binds with lambdas, making parameter permutation (including parameter omitting) explicit
    • makes code simpler, more concise, and easier to reason about

    Additionally, debug messages have been unified.

  2. qt: Revamp ClientModel code to handle ShowProgress core signal
    No behavior change.
    508e2dca5e
  3. qt: Revamp ClientModel code to handle NumConnectionsChanged core signal
    No behavior change.
    639563d7fe
  4. qt: Revamp ClientModel code to handle NetworkActiveChanged core signal
    No behavior change.
    bfe5140c50
  5. hebasto commented at 8:22 pm on April 10, 2022: member
    Friendly ping @promag @ryanofsky
  6. in src/qt/clientmodel.cpp:302 in 508e2dca5e outdated
    296@@ -306,7 +297,10 @@ static void BlockTipChanged(ClientModel* clientmodel, SynchronizationState sync_
    297 void ClientModel::subscribeToCoreSignals()
    298 {
    299     // Connect signals to client
    300-    m_handler_show_progress = m_node.handleShowProgress(std::bind(ShowProgress, this, std::placeholders::_1, std::placeholders::_2));
    301+    m_handler_show_progress = m_node.handleShowProgress(
    302+        [this](const std::string& title, int progress, [[maybe_unused]] bool resume_possible) {
    303+            Q_EMIT showProgress(QString::fromStdString(title), progress);
    


    promag commented at 9:00 pm on April 10, 2022:

    508e2dca5e91c1ff921f01d260fc62f629f1dc9e

    This is fine because auto connection is used: https://github.com/bitcoin-core/gui/blob/747cdf1d652d8587e9f2e3d4436c3ecdbf56d0a5/src/qt/bitcoingui.cpp#L593

  7. in src/qt/clientmodel.cpp:293 in 639563d7fe outdated
    287@@ -301,7 +288,10 @@ void ClientModel::subscribeToCoreSignals()
    288         [this](const std::string& title, int progress, [[maybe_unused]] bool resume_possible) {
    289             Q_EMIT showProgress(QString::fromStdString(title), progress);
    290         });
    291-    m_handler_notify_num_connections_changed = m_node.handleNotifyNumConnectionsChanged(std::bind(NotifyNumConnectionsChanged, this, std::placeholders::_1));
    292+    m_handler_notify_num_connections_changed = m_node.handleNotifyNumConnectionsChanged(
    293+        [this](int new_num_connections) {
    294+            Q_EMIT numConnectionsChanged(new_num_connections);
    


  8. in src/qt/clientmodel.cpp:258 in 9196d8a928 outdated
    271@@ -284,7 +272,11 @@ void ClientModel::subscribeToCoreSignals()
    272         [this](bool network_active) {
    273             Q_EMIT networkActiveChanged(network_active);
    274         });
    275-    m_handler_notify_alert_changed = m_node.handleNotifyAlertChanged(std::bind(NotifyAlertChanged, this));
    276+    m_handler_notify_alert_changed = m_node.handleNotifyAlertChanged(
    277+        [this]() {
    278+            qDebug() << "NotifyAlertChanged";
    279+            Q_EMIT alertsChanged(getStatusBarWarnings());
    


    promag commented at 9:12 pm on April 10, 2022:

    hebasto commented at 5:02 pm on April 16, 2022:
    Thanks! Updated.
  9. in src/qt/clientmodel.cpp:271 in dd924e3efa outdated
    264@@ -277,7 +265,11 @@ void ClientModel::subscribeToCoreSignals()
    265             qDebug() << "NotifyAlertChanged";
    266             Q_EMIT alertsChanged(getStatusBarWarnings());
    267         });
    268-    m_handler_banned_list_changed = m_node.handleBannedListChanged(std::bind(BannedListChanged, this));
    269+    m_handler_banned_list_changed = m_node.handleBannedListChanged(
    270+        [this]() {
    271+            qDebug() << QString("%1: Requesting update for peer banlist").arg(__func__);
    272+            banTableModel->refresh();
    


    promag commented at 9:17 pm on April 10, 2022:

    dd924e3efa021dc2fe50da9fedba4a87ce3026aa

    I think this is wrong because BanTableModel::refresh() needs to be called on the Qt main thread.

    Maybe:

    0    GUIUtil::ObjectInvoke(banTableModel, [=] {
    1        banTableModel->refresh();
    2    });
    

    hebasto commented at 5:02 pm on April 16, 2022:
    Thanks! Fixed.
  10. promag commented at 9:21 pm on April 10, 2022: contributor
    Concept ACK.
  11. hebasto added the label Refactoring on Apr 12, 2022
  12. shaavan commented at 1:30 pm on April 16, 2022: contributor

    Concept ACK

    To summarize, this PR is replacing the use of QMetaObject::invokeMethod(), std::bind, and std::placeholder with a more straightforward lambda function and direct Q_EMIT signals.

    As per my understanding of the master branch’s implementation:

    • A function is used in which QMetaObject::invokeMethod(obj, member, conn_type, args) is used, which invoked the member signal or slot on the object, with the given args. Reference.
    • This function is then bounded (using std::bind) to “this” and placeholders if needed, which would be replaced by the required argument for the function. Reference.

    In this PR’s implementation:

    • Instead of creating a function that would invokeMethod(), it directly creates it as a lambda function, where the argument values were earlier bounded (using std::bind).
    • It uses Q_EMIT to emit the respective signal immediately
    • All the Q_EMIT signals are bounded under a connect with Qt::ConnectionType as Qt::AutoConnection, which will ensure the signal is invoked correctly based on the receiver (if it is in the same thread or not). Reference.

    However, as @promag mentioned, it is not the case with BanTableModel::refresh().

    0void BanTableModel::refresh()
    1{
    2    Q_EMIT layoutAboutToBeChanged();
    3    priv->refreshBanlist(m_node);
    4    Q_EMIT layoutChanged();
    5}
    

    This function should be invoked under the main thread, but the emit signal it calls (layoutAboutToBeChanged()) is not bound by and connect. Hence, it would be directly emitted irrespective of the current thread. So this might probably cause some issues.

    I hope my notes and understanding of this PR will help my fellow reviewers. In case any of my analyses are erroneous, please do correct me.

    Testing result:

    I successfully compiled this PR in Ubuntu 20.04 with Qt version 5.9.7. I experimented with banning a few peers and sorting them (as BanTableModel::sort() uses refresh()) based on different categories, and I observed no discrepancy in the behavior. Hence I observed no change in the behavior of GUI.

  13. hebasto commented at 1:37 pm on April 16, 2022: member

    @shaavan

    Thank you for your review.

    I successfully compiled this PR in Ubuntu 20.04 with Qt version 5.9.7.

    Want to update? :)

    (bitcoin/bitcoin#24132)

  14. shaavan commented at 1:41 pm on April 16, 2022: contributor

    Want to update? :)

    Oops :sweat_smile:! Let me do so now. And retest this PR ASAP!

  15. qt: Revamp ClientModel code to handle AlertChanged core signal
    No behavior change.
    36b12af7ee
  16. hebasto force-pushed on Apr 16, 2022
  17. qt: Revamp ClientModel code to handle BannedListChanged core signal
    No behavior change.
    48f6d39659
  18. qt: Revamp ClientModel code to handle {Block|Header}Tip core signals
    No behavior change.
    9bd1565f65
  19. qt, doc: Remove unneeded comments
    Function names are self-described.
    bcbf982553
  20. hebasto force-pushed on Apr 16, 2022
  21. hebasto commented at 5:01 pm on April 16, 2022: member

    Updated 78c4e908d985a588aa4a2ee2bb8e0f1ba728d740 -> bcbf982553aba8107fdb0db413d4b9fe8adc8f17 (pr581.01 -> pr581.02, diff):

    • addressed @promag’s comments
    • unified debug messages
  22. promag approved
  23. promag commented at 2:53 pm on April 18, 2022: contributor
    Code review ACK bcbf982553aba8107fdb0db413d4b9fe8adc8f17
  24. w0xlt approved
  25. w0xlt commented at 2:31 pm on April 19, 2022: contributor
  26. hebasto merged this on May 20, 2022
  27. hebasto closed this on May 20, 2022

  28. hebasto deleted the branch on May 20, 2022
  29. sidhujag referenced this in commit 63b578ae44 on May 28, 2022
  30. bitcoin-core locked this on May 20, 2023

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-10-23 00:20 UTC

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