2014-12-02 09:16:52 +01:00
|
|
|
// Copyright (c) 2011-2014 The Bitcoin developers
|
2015-01-15 11:31:35 +01:00
|
|
|
// Copyright (c) 2014-2015 The Darkcoin developers
|
2013-11-04 16:20:43 +01:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2011-05-22 17:19:43 +02:00
|
|
|
#include "clientmodel.h"
|
2013-01-23 21:51:02 +01:00
|
|
|
|
2011-05-28 20:32:19 +02:00
|
|
|
#include "guiconstants.h"
|
2011-05-22 17:19:43 +02:00
|
|
|
|
2012-08-28 23:04:54 +02:00
|
|
|
#include "alert.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "chainparams.h"
|
2013-02-10 19:46:42 +01:00
|
|
|
#include "checkpoints.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "main.h"
|
|
|
|
#include "net.h"
|
2012-05-05 16:07:14 +02:00
|
|
|
#include "ui_interface.h"
|
2015-02-24 14:24:42 +01:00
|
|
|
#include "masternodeman.h"
|
2011-06-26 19:23:24 +02:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2011-07-08 18:05:10 +02:00
|
|
|
#include <QDateTime>
|
2013-09-04 11:52:45 +02:00
|
|
|
#include <QDebug>
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <QTimer>
|
2012-05-05 16:07:14 +02:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
static const int64_t nClientStartupTime = GetTime();
|
2011-05-22 17:19:43 +02:00
|
|
|
|
2011-07-29 14:36:35 +02:00
|
|
|
ClientModel::ClientModel(OptionsModel *optionsModel, QObject *parent) :
|
|
|
|
QObject(parent), optionsModel(optionsModel),
|
2015-02-24 14:24:42 +01:00
|
|
|
cachedNumBlocks(0), cachedMasternodeCountString(""),
|
2013-04-06 12:14:05 +02:00
|
|
|
cachedReindexing(0), cachedImporting(0),
|
|
|
|
numBlocksAtStartup(-1), pollTimer(0)
|
2011-05-22 17:19:43 +02:00
|
|
|
{
|
2012-07-05 17:43:28 +02:00
|
|
|
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
|
|
|
|
2015-02-24 14:24:42 +01:00
|
|
|
pollMnTimer = new QTimer(this);
|
|
|
|
connect(pollMnTimer, SIGNAL(timeout()), this, SLOT(updateMnTimer()));
|
|
|
|
// no need to update as frequent as data for balances/txes/blocks
|
|
|
|
pollMnTimer->start(MODEL_UPDATE_DELAY * 4);
|
|
|
|
|
2012-05-06 19:40:58 +02:00
|
|
|
subscribeToCoreSignals();
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientModel::~ClientModel()
|
|
|
|
{
|
|
|
|
unsubscribeFromCoreSignals();
|
2011-05-22 17:19:43 +02:00
|
|
|
}
|
|
|
|
|
2014-02-16 19:48:27 +01:00
|
|
|
int ClientModel::getNumConnections(unsigned int flags) const
|
2011-05-22 17:19:43 +02:00
|
|
|
{
|
2014-02-16 19:48:27 +01:00
|
|
|
LOCK(cs_vNodes);
|
|
|
|
if (flags == CONNECTIONS_ALL) // Shortcut if we want total
|
|
|
|
return vNodes.size();
|
|
|
|
|
|
|
|
int nNum = 0;
|
|
|
|
BOOST_FOREACH(CNode* pnode, vNodes)
|
|
|
|
if (flags & (pnode->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT))
|
|
|
|
nNum++;
|
|
|
|
|
|
|
|
return nNum;
|
2011-05-22 17:19:43 +02:00
|
|
|
}
|
|
|
|
|
2015-02-24 14:24:42 +01:00
|
|
|
QString ClientModel::getMasternodeCountString() const
|
|
|
|
{
|
|
|
|
return QString::number((int)mnodeman.CountEnabled()) + " / " + QString::number((int)mnodeman.size());
|
|
|
|
}
|
|
|
|
|
2011-06-18 13:13:48 +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);
|
2013-10-10 23:07:44 +02:00
|
|
|
return chainActive.Height();
|
2011-05-22 17:19:43 +02:00
|
|
|
}
|
|
|
|
|
2011-09-10 11:43:45 +02:00
|
|
|
int ClientModel::getNumBlocksAtStartup()
|
|
|
|
{
|
|
|
|
if (numBlocksAtStartup == -1) numBlocksAtStartup = getNumBlocks();
|
|
|
|
return numBlocksAtStartup;
|
|
|
|
}
|
|
|
|
|
2013-08-22 18:09:32 +02:00
|
|
|
quint64 ClientModel::getTotalBytesRecv() const
|
|
|
|
{
|
|
|
|
return CNode::GetTotalBytesRecv();
|
|
|
|
}
|
|
|
|
|
|
|
|
quint64 ClientModel::getTotalBytesSent() const
|
|
|
|
{
|
|
|
|
return CNode::GetTotalBytesSent();
|
|
|
|
}
|
|
|
|
|
2011-07-08 18:05:10 +02:00
|
|
|
QDateTime ClientModel::getLastBlockDate() const
|
|
|
|
{
|
2014-04-15 17:38:25 +02:00
|
|
|
LOCK(cs_main);
|
2013-10-10 23:07:44 +02:00
|
|
|
if (chainActive.Tip())
|
|
|
|
return QDateTime::fromTime_t(chainActive.Tip()->GetBlockTime());
|
2013-04-04 22:15:47 +02:00
|
|
|
else
|
2013-10-12 16:21:37 +02:00
|
|
|
return QDateTime::fromTime_t(Params().GenesisBlock().nTime); // Genesis block's time of current network
|
2011-07-08 18:05:10 +02:00
|
|
|
}
|
|
|
|
|
2013-02-10 19:46:42 +01:00
|
|
|
double ClientModel::getVerificationProgress() const
|
|
|
|
{
|
2014-04-15 17:38:25 +02:00
|
|
|
LOCK(cs_main);
|
2013-10-10 23:07:44 +02:00
|
|
|
return Checkpoints::GuessVerificationProgress(chainActive.Tip());
|
2013-02-10 19:46:42 +01:00
|
|
|
}
|
2013-02-10 19:01:30 +01:00
|
|
|
|
2012-05-05 16:07:14 +02:00
|
|
|
void ClientModel::updateTimer()
|
2011-05-22 17:19:43 +02:00
|
|
|
{
|
2014-04-23 08:40:48 +02:00
|
|
|
// 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;
|
2012-05-05 16:07:14 +02:00
|
|
|
// 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.
|
2011-07-17 14:06:43 +02:00
|
|
|
int newNumBlocks = getNumBlocks();
|
|
|
|
|
2013-04-06 12:14:05 +02:00
|
|
|
// check for changed number of blocks we have, number of blocks peers claim to have, reindexing state and importing state
|
2014-05-05 19:43:14 +02:00
|
|
|
if (cachedNumBlocks != newNumBlocks ||
|
2013-04-06 12:14:05 +02:00
|
|
|
cachedReindexing != fReindex || cachedImporting != fImporting)
|
2012-08-01 23:32:32 +02:00
|
|
|
{
|
|
|
|
cachedNumBlocks = newNumBlocks;
|
2013-04-06 12:14:05 +02:00
|
|
|
cachedReindexing = fReindex;
|
|
|
|
cachedImporting = fImporting;
|
2012-05-05 16:07:14 +02:00
|
|
|
|
2014-05-05 19:43:14 +02:00
|
|
|
emit numBlocksChanged(newNumBlocks);
|
2012-08-01 23:32:32 +02:00
|
|
|
}
|
2013-08-22 18:09:32 +02:00
|
|
|
|
|
|
|
emit bytesChanged(getTotalBytesRecv(), getTotalBytesSent());
|
2012-05-05 16:07:14 +02:00
|
|
|
}
|
|
|
|
|
2015-02-24 14:24:42 +01:00
|
|
|
void ClientModel::updateMnTimer()
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
QString newMasternodeCountString = getMasternodeCountString();
|
|
|
|
|
|
|
|
if (cachedMasternodeCountString != newMasternodeCountString)
|
|
|
|
{
|
|
|
|
cachedMasternodeCountString = newMasternodeCountString;
|
|
|
|
|
|
|
|
emit strMasternodesChanged(cachedMasternodeCountString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-05 16:07:14 +02:00
|
|
|
void ClientModel::updateNumConnections(int numConnections)
|
|
|
|
{
|
|
|
|
emit numConnectionsChanged(numConnections);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientModel::updateAlert(const QString &hash, int status)
|
|
|
|
{
|
|
|
|
// Show error message notification for new alert
|
|
|
|
if(status == CT_NEW)
|
2012-03-24 18:48:18 +01:00
|
|
|
{
|
2012-05-05 16:07:14 +02:00
|
|
|
uint256 hash_256;
|
|
|
|
hash_256.SetHex(hash.toStdString());
|
|
|
|
CAlert alert = CAlert::getAlertByHash(hash_256);
|
|
|
|
if(!alert.IsNull())
|
|
|
|
{
|
2012-12-03 13:24:42 +01:00
|
|
|
emit message(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), CClientUIInterface::ICON_ERROR);
|
2012-05-05 16:07:14 +02:00
|
|
|
}
|
2012-03-24 18:48:18 +01:00
|
|
|
}
|
2011-07-17 14:06:43 +02:00
|
|
|
|
2012-10-24 21:47:07 +02:00
|
|
|
emit alertsChanged(getStatusBarWarnings());
|
2011-05-30 20:20:12 +02:00
|
|
|
}
|
|
|
|
|
2013-12-18 09:45:36 +01:00
|
|
|
QString ClientModel::getNetworkName() const
|
2011-06-30 19:14:42 +02:00
|
|
|
{
|
2013-12-18 09:45:36 +01:00
|
|
|
QString netname(QString::fromStdString(Params().DataDir()));
|
|
|
|
if(netname.isEmpty())
|
|
|
|
netname = "main";
|
|
|
|
return netname;
|
2011-06-30 19:14:42 +02:00
|
|
|
}
|
|
|
|
|
2011-06-18 13:13:48 +02:00
|
|
|
bool ClientModel::inInitialBlockDownload() const
|
|
|
|
{
|
|
|
|
return IsInitialBlockDownload();
|
|
|
|
}
|
|
|
|
|
2012-10-21 21:23:13 +02:00
|
|
|
enum BlockSource ClientModel::getBlockSource() const
|
2012-09-13 14:33:52 +02:00
|
|
|
{
|
2012-10-21 21:23:13 +02:00
|
|
|
if (fReindex)
|
|
|
|
return BLOCK_SOURCE_REINDEX;
|
2013-04-10 15:45:49 +02:00
|
|
|
else if (fImporting)
|
2012-10-21 21:23:13 +02:00
|
|
|
return BLOCK_SOURCE_DISK;
|
2013-04-10 15:45:49 +02:00
|
|
|
else if (getNumConnections() > 0)
|
|
|
|
return BLOCK_SOURCE_NETWORK;
|
|
|
|
|
|
|
|
return BLOCK_SOURCE_NONE;
|
2012-09-13 14:33:52 +02:00
|
|
|
}
|
|
|
|
|
2011-12-13 20:00:21 +01:00
|
|
|
QString ClientModel::getStatusBarWarnings() const
|
|
|
|
{
|
|
|
|
return QString::fromStdString(GetWarnings("statusbar"));
|
|
|
|
}
|
|
|
|
|
2011-05-31 22:24:53 +02:00
|
|
|
OptionsModel *ClientModel::getOptionsModel()
|
|
|
|
{
|
2011-06-03 21:03:20 +02:00
|
|
|
return optionsModel;
|
|
|
|
}
|
|
|
|
|
2011-07-01 17:06:36 +02:00
|
|
|
QString ClientModel::formatFullVersion() const
|
|
|
|
{
|
|
|
|
return QString::fromStdString(FormatFullVersion());
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
return QString::fromStdString(CLIENT_NAME);
|
|
|
|
}
|
2012-05-12 00:28:58 +02:00
|
|
|
|
2012-05-21 23:05:54 +02:00
|
|
|
QString ClientModel::formatClientStartupTime() const
|
2012-05-12 00:28:58 +02:00
|
|
|
{
|
2012-05-21 23:05:54 +02:00
|
|
|
return QDateTime::fromTime_t(nClientStartupTime).toString();
|
2012-05-12 00:28:58 +02:00
|
|
|
}
|
2012-05-06 19:40:58 +02:00
|
|
|
|
|
|
|
// Handlers for core signals
|
|
|
|
static void NotifyBlocksChanged(ClientModel *clientmodel)
|
|
|
|
{
|
|
|
|
// This notification is too frequent. Don't trigger a signal.
|
|
|
|
// Don't remove it, though, as it might be useful later.
|
|
|
|
}
|
|
|
|
|
|
|
|
static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
|
|
|
|
{
|
2013-09-04 11:52:45 +02:00
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void NotifyAlertChanged(ClientModel *clientmodel, const uint256 &hash, ChangeType status)
|
|
|
|
{
|
2013-09-04 11:52:45 +02:00
|
|
|
qDebug() << "NotifyAlertChanged : " + QString::fromStdString(hash.GetHex()) + " status=" + QString::number(status);
|
2012-05-06 19:40:58 +02:00
|
|
|
QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection,
|
|
|
|
Q_ARG(QString, QString::fromStdString(hash.GetHex())),
|
|
|
|
Q_ARG(int, status));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientModel::subscribeToCoreSignals()
|
|
|
|
{
|
|
|
|
// Connect signals to client
|
|
|
|
uiInterface.NotifyBlocksChanged.connect(boost::bind(NotifyBlocksChanged, this));
|
|
|
|
uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, _1));
|
|
|
|
uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this, _1, _2));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientModel::unsubscribeFromCoreSignals()
|
|
|
|
{
|
|
|
|
// Disconnect signals from client
|
|
|
|
uiInterface.NotifyBlocksChanged.disconnect(boost::bind(NotifyBlocksChanged, this));
|
|
|
|
uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, _1));
|
|
|
|
uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this, _1, _2));
|
|
|
|
}
|