-
Notifications
You must be signed in to change notification settings - Fork 335
refactor: Revamp ClientModel code to handle core signals
#581
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
508e2dc
qt: Revamp ClientModel code to handle ShowProgress core signal
hebasto 639563d
qt: Revamp ClientModel code to handle NumConnectionsChanged core signal
hebasto bfe5140
qt: Revamp ClientModel code to handle NetworkActiveChanged core signal
hebasto 36b12af
qt: Revamp ClientModel code to handle AlertChanged core signal
hebasto 48f6d39
qt: Revamp ClientModel code to handle BannedListChanged core signal
hebasto 9bd1565
qt: Revamp ClientModel code to handle {Block|Header}Tip core signals
hebasto bcbf982
qt, doc: Remove unneeded comments
hebasto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,9 +21,9 @@ | |
| #include <validation.h> | ||
|
|
||
| #include <stdint.h> | ||
| #include <functional> | ||
|
|
||
| #include <QDebug> | ||
| #include <QMetaObject> | ||
| #include <QThread> | ||
| #include <QTimer> | ||
|
|
||
|
|
@@ -148,21 +148,6 @@ uint256 ClientModel::getBestBlockHash() | |
| return m_cached_tip_blocks; | ||
| } | ||
|
|
||
| void ClientModel::updateNumConnections(int numConnections) | ||
| { | ||
| Q_EMIT numConnectionsChanged(numConnections); | ||
| } | ||
|
|
||
| void ClientModel::updateNetworkActive(bool networkActive) | ||
| { | ||
| Q_EMIT networkActiveChanged(networkActive); | ||
| } | ||
|
|
||
| void ClientModel::updateAlert() | ||
| { | ||
| Q_EMIT alertsChanged(getStatusBarWarnings()); | ||
| } | ||
|
|
||
| enum BlockSource ClientModel::getBlockSource() const | ||
| { | ||
| if (m_node.getReindex()) | ||
|
|
@@ -230,94 +215,65 @@ QString ClientModel::blocksDir() const | |
| return GUIUtil::PathToQString(gArgs.GetBlocksDirPath()); | ||
| } | ||
|
|
||
| void ClientModel::updateBanlist() | ||
| { | ||
| banTableModel->refresh(); | ||
| } | ||
|
|
||
| // Handlers for core signals | ||
| static void ShowProgress(ClientModel *clientmodel, const std::string &title, int nProgress) | ||
| { | ||
| // emits signal "showProgress" | ||
| bool invoked = QMetaObject::invokeMethod(clientmodel, "showProgress", Qt::QueuedConnection, | ||
| Q_ARG(QString, QString::fromStdString(title)), | ||
| Q_ARG(int, nProgress)); | ||
| assert(invoked); | ||
| } | ||
|
|
||
| static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections) | ||
| { | ||
| // Too noisy: qDebug() << "NotifyNumConnectionsChanged: " + QString::number(newNumConnections); | ||
| bool invoked = QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection, | ||
| Q_ARG(int, newNumConnections)); | ||
| assert(invoked); | ||
| } | ||
|
|
||
| static void NotifyNetworkActiveChanged(ClientModel *clientmodel, bool networkActive) | ||
| { | ||
| bool invoked = QMetaObject::invokeMethod(clientmodel, "updateNetworkActive", Qt::QueuedConnection, | ||
| Q_ARG(bool, networkActive)); | ||
| assert(invoked); | ||
| } | ||
|
|
||
| static void NotifyAlertChanged(ClientModel *clientmodel) | ||
| { | ||
| qDebug() << "NotifyAlertChanged"; | ||
| bool invoked = QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection); | ||
| assert(invoked); | ||
| } | ||
|
|
||
| static void BannedListChanged(ClientModel *clientmodel) | ||
| { | ||
| qDebug() << QString("%1: Requesting update for peer banlist").arg(__func__); | ||
| bool invoked = QMetaObject::invokeMethod(clientmodel, "updateBanlist", Qt::QueuedConnection); | ||
| assert(invoked); | ||
| } | ||
|
|
||
| static void BlockTipChanged(ClientModel* clientmodel, SynchronizationState sync_state, interfaces::BlockTip tip, double verificationProgress, bool fHeader) | ||
| void ClientModel::TipChanged(SynchronizationState sync_state, interfaces::BlockTip tip, double verification_progress, bool header) | ||
| { | ||
| if (fHeader) { | ||
| if (header) { | ||
| // cache best headers time and height to reduce future cs_main locks | ||
| clientmodel->cachedBestHeaderHeight = tip.block_height; | ||
| clientmodel->cachedBestHeaderTime = tip.block_time; | ||
| cachedBestHeaderHeight = tip.block_height; | ||
| cachedBestHeaderTime = tip.block_time; | ||
| } else { | ||
| clientmodel->m_cached_num_blocks = tip.block_height; | ||
| WITH_LOCK(clientmodel->m_cached_tip_mutex, clientmodel->m_cached_tip_blocks = tip.block_hash;); | ||
| m_cached_num_blocks = tip.block_height; | ||
| WITH_LOCK(m_cached_tip_mutex, m_cached_tip_blocks = tip.block_hash;); | ||
| } | ||
|
|
||
| // Throttle GUI notifications about (a) blocks during initial sync, and (b) both blocks and headers during reindex. | ||
| const bool throttle = (sync_state != SynchronizationState::POST_INIT && !fHeader) || sync_state == SynchronizationState::INIT_REINDEX; | ||
| const bool throttle = (sync_state != SynchronizationState::POST_INIT && !header) || sync_state == SynchronizationState::INIT_REINDEX; | ||
| const int64_t now = throttle ? GetTimeMillis() : 0; | ||
| int64_t& nLastUpdateNotification = fHeader ? nLastHeaderTipUpdateNotification : nLastBlockTipUpdateNotification; | ||
| int64_t& nLastUpdateNotification = header ? nLastHeaderTipUpdateNotification : nLastBlockTipUpdateNotification; | ||
| if (throttle && now < nLastUpdateNotification + count_milliseconds(MODEL_UPDATE_DELAY)) { | ||
| return; | ||
| } | ||
|
|
||
| bool invoked = QMetaObject::invokeMethod(clientmodel, "numBlocksChanged", Qt::QueuedConnection, | ||
| Q_ARG(int, tip.block_height), | ||
| Q_ARG(QDateTime, QDateTime::fromSecsSinceEpoch(tip.block_time)), | ||
| Q_ARG(double, verificationProgress), | ||
| Q_ARG(bool, fHeader), | ||
| Q_ARG(SynchronizationState, sync_state)); | ||
| assert(invoked); | ||
| Q_EMIT numBlocksChanged(tip.block_height, QDateTime::fromSecsSinceEpoch(tip.block_time), verification_progress, header, sync_state); | ||
| nLastUpdateNotification = now; | ||
| } | ||
|
|
||
| void ClientModel::subscribeToCoreSignals() | ||
| { | ||
| // Connect signals to client | ||
| m_handler_show_progress = m_node.handleShowProgress(std::bind(ShowProgress, this, std::placeholders::_1, std::placeholders::_2)); | ||
| m_handler_notify_num_connections_changed = m_node.handleNotifyNumConnectionsChanged(std::bind(NotifyNumConnectionsChanged, this, std::placeholders::_1)); | ||
| m_handler_notify_network_active_changed = m_node.handleNotifyNetworkActiveChanged(std::bind(NotifyNetworkActiveChanged, this, std::placeholders::_1)); | ||
| m_handler_notify_alert_changed = m_node.handleNotifyAlertChanged(std::bind(NotifyAlertChanged, this)); | ||
| m_handler_banned_list_changed = m_node.handleBannedListChanged(std::bind(BannedListChanged, this)); | ||
| m_handler_notify_block_tip = m_node.handleNotifyBlockTip(std::bind(BlockTipChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, false)); | ||
| m_handler_notify_header_tip = m_node.handleNotifyHeaderTip(std::bind(BlockTipChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, true)); | ||
| m_handler_show_progress = m_node.handleShowProgress( | ||
| [this](const std::string& title, int progress, [[maybe_unused]] bool resume_possible) { | ||
| Q_EMIT showProgress(QString::fromStdString(title), progress); | ||
| }); | ||
| m_handler_notify_num_connections_changed = m_node.handleNotifyNumConnectionsChanged( | ||
| [this](int new_num_connections) { | ||
| Q_EMIT numConnectionsChanged(new_num_connections); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| }); | ||
| m_handler_notify_network_active_changed = m_node.handleNotifyNetworkActiveChanged( | ||
| [this](bool network_active) { | ||
| Q_EMIT networkActiveChanged(network_active); | ||
| }); | ||
| m_handler_notify_alert_changed = m_node.handleNotifyAlertChanged( | ||
| [this]() { | ||
| qDebug() << "ClientModel: NotifyAlertChanged"; | ||
| Q_EMIT alertsChanged(getStatusBarWarnings()); | ||
hebasto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
| m_handler_banned_list_changed = m_node.handleBannedListChanged( | ||
| [this]() { | ||
| qDebug() << "ClienModel: Requesting update for peer banlist"; | ||
| QMetaObject::invokeMethod(banTableModel, [this] { banTableModel->refresh(); }); | ||
| }); | ||
| m_handler_notify_block_tip = m_node.handleNotifyBlockTip( | ||
| [this](SynchronizationState sync_state, interfaces::BlockTip tip, double verification_progress) { | ||
| TipChanged(sync_state, tip, verification_progress, /*header=*/false); | ||
| }); | ||
| m_handler_notify_header_tip = m_node.handleNotifyHeaderTip( | ||
| [this](SynchronizationState sync_state, interfaces::BlockTip tip, double verification_progress) { | ||
| TipChanged(sync_state, tip, verification_progress, /*header=*/true); | ||
| }); | ||
| } | ||
|
|
||
| void ClientModel::unsubscribeFromCoreSignals() | ||
| { | ||
| // Disconnect signals from client | ||
| m_handler_show_progress->disconnect(); | ||
| m_handler_notify_num_connections_changed->disconnect(); | ||
| m_handler_notify_network_active_changed->disconnect(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
508e2dc
This is fine because auto connection is used:
gui/src/qt/bitcoingui.cpp
Line 593 in 747cdf1