Files
thehub/hub-qt/clientmodel.cpp
T

283 lines
8.2 KiB
C++
Raw Permalink Normal View History

2017-11-09 19:34:51 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2011-2015 The Bitcoin Core developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2013-11-04 16:20:43 +01:00
2011-05-22 17:19:43 +02:00
#include "clientmodel.h"
2013-01-23 21:51:02 +01:00
2015-06-20 20:27:03 +02:00
#include "bantablemodel.h"
2011-05-28 20:32:19 +02:00
#include "guiconstants.h"
2014-05-23 12:09:59 -05:00
#include "peertablemodel.h"
2011-05-22 17:19:43 +02:00
2013-04-13 00:13:08 -05:00
#include "chainparams.h"
2013-02-10 19:46:42 +01:00
#include "checkpoints.h"
#include "clientversion.h"
2013-04-13 00:13:08 -05:00
#include "net.h"
2015-11-09 11:45:07 +01:00
#include "txmempool.h"
2018-02-12 21:19:28 +01:00
#include "UiInterface.h"
2014-08-21 16:11:09 +02:00
#include "util.h"
2016-12-28 13:20:24 +01:00
#include <Application.h>
2011-06-26 19:23:24 +02:00
2013-04-13 00:13:08 -05:00
#include <stdint.h>
2012-05-05 16:07:14 +02:00
2017-02-21 13:44:48 +01:00
#include <BlocksDB.h>
2013-04-13 00:13:08 -05:00
#include <QDebug>
#include <QTimer>
2018-12-30 15:33:11 +01:00
#include <boost/foreach.hpp>
2013-04-13 00:13:08 -05:00
class CBlockIndex;
2013-04-13 00:13:08 -05:00
static const int64_t nClientStartupTime = GetTime();
static int64_t nLastBlockTipUpdateNotification = 0;
2011-05-22 17:19:43 +02:00
ClientModel::ClientModel(OptionsModel *optionsModel, QObject *parent) :
QObject(parent),
optionsModel(optionsModel),
peerTableModel(0),
2015-06-20 20:27:03 +02:00
banTableModel(0),
pollTimer(0)
2011-05-22 17:19:43 +02:00
{
2014-05-23 12:09:59 -05:00
peerTableModel = new PeerTableModel(this);
2015-06-20 20:27:03 +02:00
banTableModel = new BanTableModel(this);
pollTimer = new QTimer(this);
2012-05-05 16:07:14 +02:00
connect(pollTimer, SIGNAL(timeout()), this, SLOT(updateTimer()));
2013-09-28 19:29:44 +02:00
pollTimer->start(MODEL_UPDATE_DELAY);
2012-05-06 19:40:58 +02:00
subscribeToCoreSignals();
}
ClientModel::~ClientModel()
{
unsubscribeFromCoreSignals();
2011-05-22 17:19:43 +02:00
}
int ClientModel::getNumConnections(unsigned int flags) const
2011-05-22 17:19:43 +02:00
{
LOCK(cs_vNodes);
if (flags == CONNECTIONS_ALL) // Shortcut if we want total
return vNodes.size();
int nNum = 0;
2015-07-06 08:11:34 +02:00
BOOST_FOREACH(const CNode* pnode, vNodes)
if (flags & (pnode->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT))
nNum++;
return nNum;
2011-05-22 17:19:43 +02:00
}
int ClientModel::getNumBlocks() const
2011-05-22 17:19:43 +02:00
{
2014-04-15 17:38:25 +02:00
LOCK(cs_main);
return chainActive.Height();
2011-05-22 17:19:43 +02:00
}
2013-08-23 02:09:32 +10:00
quint64 ClientModel::getTotalBytesRecv() const
{
return CNode::GetTotalBytesRecv();
}
quint64 ClientModel::getTotalBytesSent() const
{
return CNode::GetTotalBytesSent();
}
QDateTime ClientModel::getLastBlockDate() const
{
2014-04-15 17:38:25 +02:00
LOCK(cs_main);
if (chainActive.Tip())
return QDateTime::fromTime_t(chainActive.Tip()->GetBlockTime());
2021-11-02 09:28:35 +01:00
return QDateTime::fromTime_t(Params().GenesisBlock().blockTime()); // Genesis block's time of current network
}
2015-11-09 11:45:07 +01:00
long ClientModel::getMempoolSize() const
{
return mempool.size();
}
size_t ClientModel::getMempoolDynamicUsage() const
{
return mempool.DynamicMemoryUsage();
}
double ClientModel::getVerificationProgress(const CBlockIndex *tipIn) const
2013-02-10 19:46:42 +01:00
{
CBlockIndex *tip = const_cast<CBlockIndex *>(tipIn);
if (!tip)
{
LOCK(cs_main);
tip = chainActive.Tip();
}
return Checkpoints::GuessVerificationProgress(Params().Checkpoints(), tip);
2013-02-10 19:46:42 +01:00
}
2012-05-05 16:07:14 +02:00
void ClientModel::updateTimer()
2011-05-22 17:19:43 +02:00
{
// no locking required at this point
// the following calls will aquire the required lock
2015-11-09 11:45:07 +01:00
Q_EMIT mempoolSizeChanged(getMempoolSize(), getMempoolDynamicUsage());
2015-07-14 13:59:05 +02:00
Q_EMIT bytesChanged(getTotalBytesRecv(), getTotalBytesSent());
2012-05-05 16:07:14 +02:00
}
void ClientModel::updateNumConnections(int numConnections)
{
2015-07-14 13:59:05 +02:00
Q_EMIT numConnectionsChanged(numConnections);
2012-05-05 16:07:14 +02:00
}
2016-03-07 19:44:09 +00:00
void ClientModel::updateAlert()
2012-05-05 16:07:14 +02:00
{
2015-07-14 13:59:05 +02:00
Q_EMIT alertsChanged(getStatusBarWarnings());
2011-05-30 20:20:12 +02:00
}
bool ClientModel::inInitialBlockDownload() const
{
return IsInitialBlockDownload();
}
enum BlockSource ClientModel::getBlockSource() const
{
if (Blocks::DB::instance()->isReindexing())
return BLOCK_SOURCE_REINDEX;
else if (getNumConnections() > 0)
return BLOCK_SOURCE_NETWORK;
return BLOCK_SOURCE_NONE;
}
2011-12-13 14:00:21 -05:00
QString ClientModel::getStatusBarWarnings() const
{
2015-12-01 09:47:13 +01:00
return QString::fromStdString(GetWarnings("gui"));
2011-12-13 14:00:21 -05:00
}
OptionsModel *ClientModel::getOptionsModel()
{
return optionsModel;
}
2014-05-23 12:09:59 -05:00
PeerTableModel *ClientModel::getPeerTableModel()
{
return peerTableModel;
}
2015-06-20 20:27:03 +02:00
BanTableModel *ClientModel::getBanTableModel()
{
return banTableModel;
}
2011-07-01 17:06:36 +02:00
QString ClientModel::formatFullVersion() const
{
return QString::fromStdString(FormatFullVersion());
}
2012-04-07 02:06:53 +02:00
2015-08-06 15:40:50 +02:00
QString ClientModel::formatSubVersion() const
{
2016-12-28 13:20:24 +01:00
return QString::fromStdString(Application::userAgent());
2015-08-06 15:40:50 +02:00
}
2012-04-07 02:06:53 +02:00
QString ClientModel::formatBuildDate() const
{
return QString::fromStdString(CLIENT_DATE);
}
2012-04-09 21:07:25 +02:00
2012-10-24 21:47:07 +02:00
bool ClientModel::isReleaseVersion() const
{
return CLIENT_VERSION_IS_RELEASE;
}
2012-04-09 21:07:25 +02:00
QString ClientModel::clientName() const
{
2016-12-28 13:20:24 +01:00
return QString::fromLatin1(Application::clientName());
2012-04-09 21:07:25 +02:00
}
QString ClientModel::formatClientStartupTime() const
{
return QDateTime::fromTime_t(nClientStartupTime).toString();
}
2012-05-06 19:40:58 +02:00
2015-06-20 20:27:03 +02:00
void ClientModel::updateBanlist()
{
banTableModel->refresh();
}
2012-05-06 19:40:58 +02:00
// Handlers for core signals
2014-05-23 18:04:09 +02:00
static void ShowProgress(ClientModel *clientmodel, const std::string &title, int nProgress)
{
// emits signal "showProgress"
QMetaObject::invokeMethod(clientmodel, "showProgress", Qt::QueuedConnection,
Q_ARG(QString, QString::fromStdString(title)),
Q_ARG(int, nProgress));
}
2012-05-06 19:40:58 +02:00
static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
{
// Too noisy: qDebug() << "NotifyNumConnectionsChanged: " + QString::number(newNumConnections);
2012-05-06 19:40:58 +02:00
QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
Q_ARG(int, newNumConnections));
}
2016-03-07 19:44:09 +00:00
static void NotifyAlertChanged(ClientModel *clientmodel)
2012-05-06 19:40:58 +02:00
{
2016-03-07 19:44:09 +00:00
qDebug() << "NotifyAlertChanged";
QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection);
2012-05-06 19:40:58 +02:00
}
2015-06-20 20:27:28 +02:00
static void BannedListChanged(ClientModel *clientmodel)
{
2015-06-23 21:10:42 +02:00
qDebug() << QString("%1: Requesting update for peer banlist").arg(__func__);
2015-06-20 20:27:28 +02:00
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
QMetaObject::invokeMethod(clientmodel, "numBlocksChanged", Qt::QueuedConnection,
Q_ARG(int, pIndex->nHeight),
Q_ARG(QDateTime, QDateTime::fromTime_t(pIndex->GetBlockTime())),
Q_ARG(double, clientmodel->getVerificationProgress(pIndex)));
nLastBlockTipUpdateNotification = now;
}
}
2012-05-06 19:40:58 +02:00
void ClientModel::subscribeToCoreSignals()
{
// Connect signals to client
2020-12-15 12:54:07 +01:00
uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, boost::placeholders::_1, boost::placeholders::_2));
uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, boost::placeholders::_1));
2016-03-07 19:44:09 +00:00
uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this));
2015-06-20 20:27:28 +02:00
uiInterface.BannedListChanged.connect(boost::bind(BannedListChanged, this));
2020-12-15 12:54:07 +01:00
uiInterface.NotifyBlockTip.connect(boost::bind(BlockTipChanged, this, boost::placeholders::_1, boost::placeholders::_2));
2012-05-06 19:40:58 +02:00
}
void ClientModel::unsubscribeFromCoreSignals()
{
// Disconnect signals from client
2020-12-15 12:54:07 +01:00
uiInterface.ShowProgress.disconnect(boost::bind(ShowProgress, this, boost::placeholders::_1, boost::placeholders::_2));
uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, boost::placeholders::_1));
2016-03-07 19:44:09 +00:00
uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this));
2015-06-20 20:27:28 +02:00
uiInterface.BannedListChanged.disconnect(boost::bind(BannedListChanged, this));
2020-12-15 12:54:07 +01:00
uiInterface.NotifyBlockTip.disconnect(boost::bind(BlockTipChanged, this, boost::placeholders::_1, boost::placeholders::_2));
2012-05-06 19:40:58 +02:00
}