qt: Revamp ClientModel code to handle {Block|Header}Tip core signals

No behavior change.
This commit is contained in:
Hennadii Stepanov
2022-04-10 18:55:36 +02:00
parent 48f6d39659
commit 9bd1565f65
2 changed files with 19 additions and 19 deletions

View File

@@ -21,7 +21,6 @@
#include <validation.h> #include <validation.h>
#include <stdint.h> #include <stdint.h>
#include <functional>
#include <QDebug> #include <QDebug>
#include <QMetaObject> #include <QMetaObject>
@@ -216,33 +215,26 @@ QString ClientModel::blocksDir() const
return GUIUtil::PathToQString(gArgs.GetBlocksDirPath()); return GUIUtil::PathToQString(gArgs.GetBlocksDirPath());
} }
// Handlers for core signals void ClientModel::TipChanged(SynchronizationState sync_state, interfaces::BlockTip tip, double verification_progress, bool header)
static void BlockTipChanged(ClientModel* clientmodel, SynchronizationState sync_state, interfaces::BlockTip tip, double verificationProgress, bool fHeader)
{ {
if (fHeader) { if (header) {
// cache best headers time and height to reduce future cs_main locks // cache best headers time and height to reduce future cs_main locks
clientmodel->cachedBestHeaderHeight = tip.block_height; cachedBestHeaderHeight = tip.block_height;
clientmodel->cachedBestHeaderTime = tip.block_time; cachedBestHeaderTime = tip.block_time;
} else { } else {
clientmodel->m_cached_num_blocks = tip.block_height; m_cached_num_blocks = tip.block_height;
WITH_LOCK(clientmodel->m_cached_tip_mutex, clientmodel->m_cached_tip_blocks = tip.block_hash;); 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. // 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; 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)) { if (throttle && now < nLastUpdateNotification + count_milliseconds(MODEL_UPDATE_DELAY)) {
return; return;
} }
bool invoked = QMetaObject::invokeMethod(clientmodel, "numBlocksChanged", Qt::QueuedConnection, Q_EMIT numBlocksChanged(tip.block_height, QDateTime::fromSecsSinceEpoch(tip.block_time), verification_progress, header, sync_state);
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);
nLastUpdateNotification = now; nLastUpdateNotification = now;
} }
@@ -271,8 +263,14 @@ void ClientModel::subscribeToCoreSignals()
qDebug() << "ClienModel: Requesting update for peer banlist"; qDebug() << "ClienModel: Requesting update for peer banlist";
QMetaObject::invokeMethod(banTableModel, [this] { banTableModel->refresh(); }); QMetaObject::invokeMethod(banTableModel, [this] { banTableModel->refresh(); });
}); });
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_block_tip = m_node.handleNotifyBlockTip(
m_handler_notify_header_tip = m_node.handleNotifyHeaderTip(std::bind(BlockTipChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, true)); [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() void ClientModel::unsubscribeFromCoreSignals()

View File

@@ -23,6 +23,7 @@ enum class SynchronizationState;
namespace interfaces { namespace interfaces {
class Handler; class Handler;
class Node; class Node;
struct BlockTip;
} }
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@@ -104,6 +105,7 @@ private:
//! A thread to interact with m_node asynchronously //! A thread to interact with m_node asynchronously
QThread* const m_thread; QThread* const m_thread;
void TipChanged(SynchronizationState sync_state, interfaces::BlockTip tip, double verification_progress, bool header);
void subscribeToCoreSignals(); void subscribeToCoreSignals();
void unsubscribeFromCoreSignals(); void unsubscribeFromCoreSignals();