Skip to content

Commit e6d50fc

Browse files
committed
[Qt] update block tip (height and date) without locking cs_main, update always (each block)
1 parent 012fc91 commit e6d50fc

File tree

2 files changed

+23
-35
lines changed

2 files changed

+23
-35
lines changed

src/qt/clientmodel.cpp

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@
2323
#include <QTimer>
2424

2525
static const int64_t nClientStartupTime = GetTime();
26+
static int64_t nLastBlockTipUpdateNotification = 0;
2627

2728
ClientModel::ClientModel(OptionsModel *optionsModel, QObject *parent) :
2829
QObject(parent),
2930
optionsModel(optionsModel),
3031
peerTableModel(0),
3132
banTableModel(0),
32-
cachedNumBlocks(0),
33-
cachedBlockDate(QDateTime()),
34-
cachedReindexing(0),
35-
cachedImporting(0),
3633
pollTimer(0)
3734
{
3835
peerTableModel = new PeerTableModel(this);
@@ -107,32 +104,8 @@ double ClientModel::getVerificationProgress() const
107104

108105
void ClientModel::updateTimer()
109106
{
110-
// Get required lock upfront. This avoids the GUI from getting stuck on
111-
// periodical polls if the core is holding the locks for a longer time -
112-
// for example, during a wallet rescan.
113-
TRY_LOCK(cs_main, lockMain);
114-
if (!lockMain)
115-
return;
116-
117-
// Some quantities (such as number of blocks) change so fast that we don't want to be notified for each change.
118-
// Periodically check and update with a timer.
119-
int newNumBlocks = getNumBlocks();
120-
QDateTime newBlockDate = getLastBlockDate();
121-
122-
// check for changed number of blocks we have, number of blocks peers claim to have, reindexing state and importing state
123-
if (cachedNumBlocks != newNumBlocks ||
124-
cachedBlockDate != newBlockDate ||
125-
cachedReindexing != fReindex ||
126-
cachedImporting != fImporting)
127-
{
128-
cachedNumBlocks = newNumBlocks;
129-
cachedBlockDate = newBlockDate;
130-
cachedReindexing = fReindex;
131-
cachedImporting = fImporting;
132-
133-
Q_EMIT numBlocksChanged(newNumBlocks, newBlockDate);
134-
}
135-
107+
// no locking required at this point
108+
// the following calls will aquire the required lock
136109
Q_EMIT mempoolSizeChanged(getMempoolSize(), getMempoolDynamicUsage());
137110
Q_EMIT bytesChanged(getTotalBytesRecv(), getTotalBytesSent());
138111
}
@@ -261,13 +234,32 @@ static void BannedListChanged(ClientModel *clientmodel)
261234
QMetaObject::invokeMethod(clientmodel, "updateBanlist", Qt::QueuedConnection);
262235
}
263236

237+
static void BlockTipChanged(ClientModel *clientmodel, bool initialSync, const CBlockIndex *pIndex)
238+
{
239+
// lock free async UI updates in case we have a new block tip
240+
// during initial sync, only update the UI if the last update
241+
// was > 250ms (MODEL_UPDATE_DELAY) ago
242+
int64_t now = 0;
243+
if (initialSync)
244+
now = GetTimeMillis();
245+
246+
// if we are in-sync, update the UI regardless of last update time
247+
if (!initialSync || now - nLastBlockTipUpdateNotification > MODEL_UPDATE_DELAY) {
248+
//pass a async signal to the UI thread
249+
Q_EMIT clientmodel->numBlocksChanged(pIndex->nHeight, QDateTime::fromTime_t(pIndex->GetBlockTime()));
250+
nLastBlockTipUpdateNotification = now;
251+
}
252+
253+
}
254+
264255
void ClientModel::subscribeToCoreSignals()
265256
{
266257
// Connect signals to client
267258
uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2));
268259
uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, _1));
269260
uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this, _1, _2));
270261
uiInterface.BannedListChanged.connect(boost::bind(BannedListChanged, this));
262+
uiInterface.NotifyBlockTip.connect(boost::bind(BlockTipChanged, this, _1, _2));
271263
}
272264

273265
void ClientModel::unsubscribeFromCoreSignals()
@@ -277,4 +269,5 @@ void ClientModel::unsubscribeFromCoreSignals()
277269
uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, _1));
278270
uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this, _1, _2));
279271
uiInterface.BannedListChanged.disconnect(boost::bind(BannedListChanged, this));
272+
uiInterface.NotifyBlockTip.disconnect(boost::bind(BlockTipChanged, this, _1, _2));
280273
}

src/qt/clientmodel.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,6 @@ class ClientModel : public QObject
8181
PeerTableModel *peerTableModel;
8282
BanTableModel *banTableModel;
8383

84-
int cachedNumBlocks;
85-
QDateTime cachedBlockDate;
86-
bool cachedReindexing;
87-
bool cachedImporting;
88-
8984
QTimer *pollTimer;
9085

9186
void subscribeToCoreSignals();

0 commit comments

Comments
 (0)