From 8517e9709e883ef59603d56b45dd2e06f8b7c7d7 Mon Sep 17 00:00:00 2001 From: Philip Kaufmann Date: Mon, 9 Feb 2015 11:19:01 +0100 Subject: [PATCH] [Qt] rework setNumBlocks to have blockDate as parameter - reduces some functional overhead and simplifies the code --- src/qt/bitcoingui.cpp | 9 ++++----- src/qt/bitcoingui.h | 4 ++-- src/qt/clientmodel.cpp | 18 ++++++++++++------ src/qt/clientmodel.h | 5 +++-- src/qt/rpcconsole.cpp | 9 ++++----- src/qt/rpcconsole.h | 4 ++-- src/qt/sendcoinsdialog.cpp | 2 +- 7 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index 342099208..1ec968ff2 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -436,8 +436,8 @@ void BitcoinGUI::setClientModel(ClientModel *clientModel) setNumConnections(clientModel->getNumConnections()); connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int))); - setNumBlocks(clientModel->getNumBlocks()); - connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int))); + setNumBlocks(clientModel->getNumBlocks(), clientModel->getLastBlockDate()); + connect(clientModel, SIGNAL(numBlocksChanged(int,QDateTime)), this, SLOT(setNumBlocks(int,QDateTime))); // Receive and report messages from client model connect(clientModel, SIGNAL(message(QString,QString,unsigned int)), this, SLOT(message(QString,QString,unsigned int))); @@ -653,7 +653,7 @@ void BitcoinGUI::setNumConnections(int count) labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count)); } -void BitcoinGUI::setNumBlocks(int count) +void BitcoinGUI::setNumBlocks(int count, const QDateTime& blockDate) { if(!clientModel) return; @@ -681,9 +681,8 @@ void BitcoinGUI::setNumBlocks(int count) QString tooltip; - QDateTime lastBlockDate = clientModel->getLastBlockDate(); QDateTime currentDate = QDateTime::currentDateTime(); - qint64 secs = lastBlockDate.secsTo(currentDate); + qint64 secs = blockDate.secsTo(currentDate); tooltip = tr("Processed %n blocks of transaction history.", "", count); diff --git a/src/qt/bitcoingui.h b/src/qt/bitcoingui.h index fd776d63f..5a289a904 100644 --- a/src/qt/bitcoingui.h +++ b/src/qt/bitcoingui.h @@ -143,8 +143,8 @@ signals: public slots: /** Set number of connections shown in the UI */ void setNumConnections(int count); - /** Set number of blocks shown in the UI */ - void setNumBlocks(int count); + /** Set number of blocks and last block date shown in the UI */ + void setNumBlocks(int count, const QDateTime& blockDate); /** Notify the user of an event from the core network or transaction handling code. @param[in] title the message box / notification title diff --git a/src/qt/clientmodel.cpp b/src/qt/clientmodel.cpp index 729eb84a1..dc32f8157 100644 --- a/src/qt/clientmodel.cpp +++ b/src/qt/clientmodel.cpp @@ -18,7 +18,6 @@ #include -#include #include #include @@ -29,6 +28,7 @@ ClientModel::ClientModel(OptionsModel *optionsModel, QObject *parent) : optionsModel(optionsModel), peerTableModel(0), cachedNumBlocks(0), + cachedBlockDate(QDateTime()), cachedReindexing(0), cachedImporting(0), pollTimer(0) @@ -79,10 +79,11 @@ quint64 ClientModel::getTotalBytesSent() const QDateTime ClientModel::getLastBlockDate() const { LOCK(cs_main); + if (chainActive.Tip()) return QDateTime::fromTime_t(chainActive.Tip()->GetBlockTime()); - else - return QDateTime::fromTime_t(Params().GenesisBlock().GetBlockTime()); // Genesis block's time of current network + + return QDateTime::fromTime_t(Params().GenesisBlock().GetBlockTime()); // Genesis block's time of current network } double ClientModel::getVerificationProgress() const @@ -97,21 +98,26 @@ void ClientModel::updateTimer() // 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) + 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 || - cachedReindexing != fReindex || cachedImporting != fImporting) + cachedBlockDate != newBlockDate || + cachedReindexing != fReindex || + cachedImporting != fImporting) { cachedNumBlocks = newNumBlocks; + cachedBlockDate = newBlockDate; cachedReindexing = fReindex; cachedImporting = fImporting; - emit numBlocksChanged(newNumBlocks); + emit numBlocksChanged(newNumBlocks, newBlockDate); } emit bytesChanged(getTotalBytesRecv(), getTotalBytesSent()); diff --git a/src/qt/clientmodel.h b/src/qt/clientmodel.h index 7b74728b2..214701810 100644 --- a/src/qt/clientmodel.h +++ b/src/qt/clientmodel.h @@ -6,6 +6,7 @@ #define BITCOIN_QT_CLIENTMODEL_H #include +#include class AddressTableModel; class OptionsModel; @@ -15,7 +16,6 @@ class TransactionTableModel; class CWallet; QT_BEGIN_NAMESPACE -class QDateTime; class QTimer; QT_END_NAMESPACE @@ -73,6 +73,7 @@ private: PeerTableModel *peerTableModel; int cachedNumBlocks; + QDateTime cachedBlockDate; bool cachedReindexing; bool cachedImporting; @@ -83,7 +84,7 @@ private: signals: void numConnectionsChanged(int count); - void numBlocksChanged(int count); + void numBlocksChanged(int count, const QDateTime& blockDate); void alertsChanged(const QString &warnings); void bytesChanged(quint64 totalBytesIn, quint64 totalBytesOut); diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp index 9f3991c4c..ccde44fb2 100644 --- a/src/qt/rpcconsole.cpp +++ b/src/qt/rpcconsole.cpp @@ -293,8 +293,8 @@ void RPCConsole::setClientModel(ClientModel *model) setNumConnections(model->getNumConnections()); connect(model, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int))); - setNumBlocks(model->getNumBlocks()); - connect(model, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int))); + setNumBlocks(model->getNumBlocks(), model->getLastBlockDate()); + connect(model, SIGNAL(numBlocksChanged(int,QDateTime)), this, SLOT(setNumBlocks(int,QDateTime))); updateTrafficStats(model->getTotalBytesRecv(), model->getTotalBytesSent()); connect(model, SIGNAL(bytesChanged(quint64,quint64)), this, SLOT(updateTrafficStats(quint64, quint64))); @@ -404,11 +404,10 @@ void RPCConsole::setNumConnections(int count) ui->numberOfConnections->setText(connections); } -void RPCConsole::setNumBlocks(int count) +void RPCConsole::setNumBlocks(int count, const QDateTime& blockDate) { ui->numberOfBlocks->setText(QString::number(count)); - if(clientModel) - ui->lastBlockTime->setText(clientModel->getLastBlockDate().toString()); + ui->lastBlockTime->setText(blockDate.toString()); } void RPCConsole::on_lineEdit_returnPressed() diff --git a/src/qt/rpcconsole.h b/src/qt/rpcconsole.h index fff5cfbf5..8737be35d 100644 --- a/src/qt/rpcconsole.h +++ b/src/qt/rpcconsole.h @@ -63,8 +63,8 @@ public slots: void message(int category, const QString &message, bool html = false); /** Set number of connections shown in the UI */ void setNumConnections(int count); - /** Set number of blocks shown in the UI */ - void setNumBlocks(int count); + /** Set number of blocks and last block date shown in the UI */ + void setNumBlocks(int count, const QDateTime& blockDate); /** Go forward or back in history */ void browseHistory(int offset); /** Scroll console view to end */ diff --git a/src/qt/sendcoinsdialog.cpp b/src/qt/sendcoinsdialog.cpp index 5aef2d753..4f3230a8c 100644 --- a/src/qt/sendcoinsdialog.cpp +++ b/src/qt/sendcoinsdialog.cpp @@ -121,7 +121,7 @@ void SendCoinsDialog::setClientModel(ClientModel *clientModel) this->clientModel = clientModel; if (clientModel) { - connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(updateSmartFeeLabel())); + connect(clientModel, SIGNAL(numBlocksChanged(int,QDateTime)), this, SLOT(updateSmartFeeLabel())); } }