Skip to content

Commit

Permalink
[Qt] update block tip (height and date) without locking cs_main, upda…
Browse files Browse the repository at this point in the history
…te always (each block)
  • Loading branch information
jonasschnelli committed Nov 30, 2015
1 parent 012fc91 commit e6d50fc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 35 deletions.
53 changes: 23 additions & 30 deletions src/qt/clientmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@
#include <QTimer>

static const int64_t nClientStartupTime = GetTime();
static int64_t nLastBlockTipUpdateNotification = 0;

ClientModel::ClientModel(OptionsModel *optionsModel, QObject *parent) :
QObject(parent),
optionsModel(optionsModel),
peerTableModel(0),
banTableModel(0),
cachedNumBlocks(0),
cachedBlockDate(QDateTime()),
cachedReindexing(0),
cachedImporting(0),
pollTimer(0)
{
peerTableModel = new PeerTableModel(this);
Expand Down Expand Up @@ -107,32 +104,8 @@ double ClientModel::getVerificationProgress() const

void ClientModel::updateTimer()
{
// Get required lock upfront. This avoids the GUI from getting stuck on
// periodical polls if the core is holding the locks for a longer time -
// for example, during a wallet rescan.
TRY_LOCK(cs_main, lockMain);
if (!lockMain)
return;

// Some quantities (such as number of blocks) change so fast that we don't want to be notified for each change.
// Periodically check and update with a timer.
int newNumBlocks = getNumBlocks();
QDateTime newBlockDate = getLastBlockDate();

// check for changed number of blocks we have, number of blocks peers claim to have, reindexing state and importing state
if (cachedNumBlocks != newNumBlocks ||
cachedBlockDate != newBlockDate ||
cachedReindexing != fReindex ||
cachedImporting != fImporting)
{
cachedNumBlocks = newNumBlocks;
cachedBlockDate = newBlockDate;
cachedReindexing = fReindex;
cachedImporting = fImporting;

Q_EMIT numBlocksChanged(newNumBlocks, newBlockDate);
}

// no locking required at this point
// the following calls will aquire the required lock
Q_EMIT mempoolSizeChanged(getMempoolSize(), getMempoolDynamicUsage());
Q_EMIT bytesChanged(getTotalBytesRecv(), getTotalBytesSent());
}
Expand Down Expand Up @@ -261,13 +234,32 @@ static void BannedListChanged(ClientModel *clientmodel)
QMetaObject::invokeMethod(clientmodel, "updateBanlist", Qt::QueuedConnection);
}

static void BlockTipChanged(ClientModel *clientmodel, bool initialSync, const CBlockIndex *pIndex)
{
// lock free async UI updates in case we have a new block tip
// during initial sync, only update the UI if the last update
// was > 250ms (MODEL_UPDATE_DELAY) ago
int64_t now = 0;
if (initialSync)
now = GetTimeMillis();

// if we are in-sync, update the UI regardless of last update time
if (!initialSync || now - nLastBlockTipUpdateNotification > MODEL_UPDATE_DELAY) {
//pass a async signal to the UI thread
Q_EMIT clientmodel->numBlocksChanged(pIndex->nHeight, QDateTime::fromTime_t(pIndex->GetBlockTime()));
nLastBlockTipUpdateNotification = now;
}

}

void ClientModel::subscribeToCoreSignals()
{
// Connect signals to client
uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2));
uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, _1));
uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this, _1, _2));
uiInterface.BannedListChanged.connect(boost::bind(BannedListChanged, this));
uiInterface.NotifyBlockTip.connect(boost::bind(BlockTipChanged, this, _1, _2));
}

void ClientModel::unsubscribeFromCoreSignals()
Expand All @@ -277,4 +269,5 @@ void ClientModel::unsubscribeFromCoreSignals()
uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, _1));
uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this, _1, _2));
uiInterface.BannedListChanged.disconnect(boost::bind(BannedListChanged, this));
uiInterface.NotifyBlockTip.disconnect(boost::bind(BlockTipChanged, this, _1, _2));
}
5 changes: 0 additions & 5 deletions src/qt/clientmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ class ClientModel : public QObject
PeerTableModel *peerTableModel;
BanTableModel *banTableModel;

int cachedNumBlocks;
QDateTime cachedBlockDate;
bool cachedReindexing;
bool cachedImporting;

QTimer *pollTimer;

void subscribeToCoreSignals();
Expand Down

0 comments on commit e6d50fc

Please sign in to comment.