23
23
#include < QTimer>
24
24
25
25
static const int64_t nClientStartupTime = GetTime();
26
+ static int64_t nLastBlockTipUpdateNotification = 0 ;
26
27
27
28
ClientModel::ClientModel (OptionsModel *optionsModel, QObject *parent) :
28
29
QObject(parent),
29
30
optionsModel(optionsModel),
30
31
peerTableModel(0 ),
31
32
banTableModel(0 ),
32
- cachedNumBlocks(0 ),
33
- cachedBlockDate(QDateTime()),
34
- cachedReindexing(0 ),
35
- cachedImporting(0 ),
36
33
pollTimer(0 )
37
34
{
38
35
peerTableModel = new PeerTableModel (this );
@@ -107,32 +104,8 @@ double ClientModel::getVerificationProgress() const
107
104
108
105
void ClientModel::updateTimer ()
109
106
{
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
136
109
Q_EMIT mempoolSizeChanged (getMempoolSize (), getMempoolDynamicUsage ());
137
110
Q_EMIT bytesChanged (getTotalBytesRecv (), getTotalBytesSent ());
138
111
}
@@ -261,13 +234,32 @@ static void BannedListChanged(ClientModel *clientmodel)
261
234
QMetaObject::invokeMethod (clientmodel, " updateBanlist" , Qt::QueuedConnection);
262
235
}
263
236
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
+
264
255
void ClientModel::subscribeToCoreSignals ()
265
256
{
266
257
// Connect signals to client
267
258
uiInterface.ShowProgress .connect (boost::bind (ShowProgress, this , _1, _2));
268
259
uiInterface.NotifyNumConnectionsChanged .connect (boost::bind (NotifyNumConnectionsChanged, this , _1));
269
260
uiInterface.NotifyAlertChanged .connect (boost::bind (NotifyAlertChanged, this , _1, _2));
270
261
uiInterface.BannedListChanged .connect (boost::bind (BannedListChanged, this ));
262
+ uiInterface.NotifyBlockTip .connect (boost::bind (BlockTipChanged, this , _1, _2));
271
263
}
272
264
273
265
void ClientModel::unsubscribeFromCoreSignals ()
@@ -277,4 +269,5 @@ void ClientModel::unsubscribeFromCoreSignals()
277
269
uiInterface.NotifyNumConnectionsChanged .disconnect (boost::bind (NotifyNumConnectionsChanged, this , _1));
278
270
uiInterface.NotifyAlertChanged .disconnect (boost::bind (NotifyAlertChanged, this , _1, _2));
279
271
uiInterface.BannedListChanged .disconnect (boost::bind (BannedListChanged, this ));
272
+ uiInterface.NotifyBlockTip .disconnect (boost::bind (BlockTipChanged, this , _1, _2));
280
273
}
0 commit comments