Skip to content

Commit 9bd1565

Browse files
committed
qt: Revamp ClientModel code to handle {Block|Header}Tip core signals
No behavior change.
1 parent 48f6d39 commit 9bd1565

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

src/qt/clientmodel.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include <validation.h>
2222

2323
#include <stdint.h>
24-
#include <functional>
2524

2625
#include <QDebug>
2726
#include <QMetaObject>
@@ -216,33 +215,26 @@ QString ClientModel::blocksDir() const
216215
return GUIUtil::PathToQString(gArgs.GetBlocksDirPath());
217216
}
218217

219-
// Handlers for core signals
220-
static void BlockTipChanged(ClientModel* clientmodel, SynchronizationState sync_state, interfaces::BlockTip tip, double verificationProgress, bool fHeader)
218+
void ClientModel::TipChanged(SynchronizationState sync_state, interfaces::BlockTip tip, double verification_progress, bool header)
221219
{
222-
if (fHeader) {
220+
if (header) {
223221
// cache best headers time and height to reduce future cs_main locks
224-
clientmodel->cachedBestHeaderHeight = tip.block_height;
225-
clientmodel->cachedBestHeaderTime = tip.block_time;
222+
cachedBestHeaderHeight = tip.block_height;
223+
cachedBestHeaderTime = tip.block_time;
226224
} else {
227-
clientmodel->m_cached_num_blocks = tip.block_height;
228-
WITH_LOCK(clientmodel->m_cached_tip_mutex, clientmodel->m_cached_tip_blocks = tip.block_hash;);
225+
m_cached_num_blocks = tip.block_height;
226+
WITH_LOCK(m_cached_tip_mutex, m_cached_tip_blocks = tip.block_hash;);
229227
}
230228

231229
// Throttle GUI notifications about (a) blocks during initial sync, and (b) both blocks and headers during reindex.
232-
const bool throttle = (sync_state != SynchronizationState::POST_INIT && !fHeader) || sync_state == SynchronizationState::INIT_REINDEX;
230+
const bool throttle = (sync_state != SynchronizationState::POST_INIT && !header) || sync_state == SynchronizationState::INIT_REINDEX;
233231
const int64_t now = throttle ? GetTimeMillis() : 0;
234-
int64_t& nLastUpdateNotification = fHeader ? nLastHeaderTipUpdateNotification : nLastBlockTipUpdateNotification;
232+
int64_t& nLastUpdateNotification = header ? nLastHeaderTipUpdateNotification : nLastBlockTipUpdateNotification;
235233
if (throttle && now < nLastUpdateNotification + count_milliseconds(MODEL_UPDATE_DELAY)) {
236234
return;
237235
}
238236

239-
bool invoked = QMetaObject::invokeMethod(clientmodel, "numBlocksChanged", Qt::QueuedConnection,
240-
Q_ARG(int, tip.block_height),
241-
Q_ARG(QDateTime, QDateTime::fromSecsSinceEpoch(tip.block_time)),
242-
Q_ARG(double, verificationProgress),
243-
Q_ARG(bool, fHeader),
244-
Q_ARG(SynchronizationState, sync_state));
245-
assert(invoked);
237+
Q_EMIT numBlocksChanged(tip.block_height, QDateTime::fromSecsSinceEpoch(tip.block_time), verification_progress, header, sync_state);
246238
nLastUpdateNotification = now;
247239
}
248240

@@ -271,8 +263,14 @@ void ClientModel::subscribeToCoreSignals()
271263
qDebug() << "ClienModel: Requesting update for peer banlist";
272264
QMetaObject::invokeMethod(banTableModel, [this] { banTableModel->refresh(); });
273265
});
274-
m_handler_notify_block_tip = m_node.handleNotifyBlockTip(std::bind(BlockTipChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, false));
275-
m_handler_notify_header_tip = m_node.handleNotifyHeaderTip(std::bind(BlockTipChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, true));
266+
m_handler_notify_block_tip = m_node.handleNotifyBlockTip(
267+
[this](SynchronizationState sync_state, interfaces::BlockTip tip, double verification_progress) {
268+
TipChanged(sync_state, tip, verification_progress, /*header=*/false);
269+
});
270+
m_handler_notify_header_tip = m_node.handleNotifyHeaderTip(
271+
[this](SynchronizationState sync_state, interfaces::BlockTip tip, double verification_progress) {
272+
TipChanged(sync_state, tip, verification_progress, /*header=*/true);
273+
});
276274
}
277275

278276
void ClientModel::unsubscribeFromCoreSignals()

src/qt/clientmodel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ enum class SynchronizationState;
2323
namespace interfaces {
2424
class Handler;
2525
class Node;
26+
struct BlockTip;
2627
}
2728

2829
QT_BEGIN_NAMESPACE
@@ -104,6 +105,7 @@ class ClientModel : public QObject
104105
//! A thread to interact with m_node asynchronously
105106
QThread* const m_thread;
106107

108+
void TipChanged(SynchronizationState sync_state, interfaces::BlockTip tip, double verification_progress, bool header);
107109
void subscribeToCoreSignals();
108110
void unsubscribeFromCoreSignals();
109111

0 commit comments

Comments
 (0)