2019-12-30 06:39:22 -03:00
|
|
|
// Copyright (c) 2011-2019 The Bitcoin Core developers
|
2014-12-13 01:09:33 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-11-04 12:20:43 -03:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <qt/clientmodel.h>
|
|
|
|
|
|
|
|
#include <qt/bantablemodel.h>
|
|
|
|
#include <qt/guiconstants.h>
|
|
|
|
#include <qt/guiutil.h>
|
|
|
|
#include <qt/peertablemodel.h>
|
|
|
|
|
|
|
|
#include <clientversion.h>
|
2018-04-07 04:42:02 -03:00
|
|
|
#include <interfaces/handler.h>
|
|
|
|
#include <interfaces/node.h>
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <net.h>
|
2017-10-02 11:59:32 -03:00
|
|
|
#include <netbase.h>
|
2018-10-22 19:51:11 -03:00
|
|
|
#include <util/system.h>
|
2011-06-26 13:23:24 -04:00
|
|
|
|
2013-04-13 02:13:08 -03:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2013-09-04 05:52:45 -04:00
|
|
|
#include <QDebug>
|
2019-10-14 16:46:34 -03:00
|
|
|
#include <QThread>
|
2013-04-13 02:13:08 -03:00
|
|
|
#include <QTimer>
|
2012-05-05 10:07:14 -04:00
|
|
|
|
2016-04-28 11:18:45 -03:00
|
|
|
static int64_t nLastHeaderTipUpdateNotification = 0;
|
2015-11-26 12:39:40 -03:00
|
|
|
static int64_t nLastBlockTipUpdateNotification = 0;
|
2011-05-22 11:19:43 -04:00
|
|
|
|
2018-04-07 04:42:02 -03:00
|
|
|
ClientModel::ClientModel(interfaces::Node& node, OptionsModel *_optionsModel, QObject *parent) :
|
2014-06-03 08:42:20 -04:00
|
|
|
QObject(parent),
|
2017-04-17 16:37:36 -03:00
|
|
|
m_node(node),
|
2016-09-09 08:43:29 -03:00
|
|
|
optionsModel(_optionsModel),
|
2018-07-30 06:37:09 -04:00
|
|
|
peerTableModel(nullptr),
|
|
|
|
banTableModel(nullptr),
|
2019-10-14 16:46:34 -03:00
|
|
|
m_thread(new QThread(this))
|
2011-05-22 11:19:43 -04:00
|
|
|
{
|
2017-04-19 13:45:11 -03:00
|
|
|
cachedBestHeaderHeight = -1;
|
|
|
|
cachedBestHeaderTime = -1;
|
2017-04-17 16:57:19 -03:00
|
|
|
peerTableModel = new PeerTableModel(m_node, this);
|
2017-04-17 17:02:44 -03:00
|
|
|
banTableModel = new BanTableModel(m_node, this);
|
2019-10-14 16:46:34 -03:00
|
|
|
|
|
|
|
QTimer* timer = new QTimer;
|
|
|
|
timer->setInterval(MODEL_UPDATE_DELAY);
|
|
|
|
connect(timer, &QTimer::timeout, [this] {
|
|
|
|
// no locking required at this point
|
|
|
|
// the following calls will acquire the required lock
|
|
|
|
Q_EMIT mempoolSizeChanged(m_node.getMempoolSize(), m_node.getMempoolDynamicUsage());
|
|
|
|
Q_EMIT bytesChanged(m_node.getTotalBytesRecv(), m_node.getTotalBytesSent());
|
|
|
|
});
|
|
|
|
connect(m_thread, &QThread::finished, timer, &QObject::deleteLater);
|
|
|
|
connect(m_thread, &QThread::started, [timer] { timer->start(); });
|
|
|
|
// move timer to thread so that polling doesn't disturb main event loop
|
|
|
|
timer->moveToThread(m_thread);
|
|
|
|
m_thread->start();
|
2012-05-06 13:40:58 -04:00
|
|
|
|
|
|
|
subscribeToCoreSignals();
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientModel::~ClientModel()
|
|
|
|
{
|
|
|
|
unsubscribeFromCoreSignals();
|
2019-10-14 16:46:34 -03:00
|
|
|
|
|
|
|
m_thread->quit();
|
|
|
|
m_thread->wait();
|
2011-05-22 11:19:43 -04:00
|
|
|
}
|
|
|
|
|
2014-02-16 15:48:27 -03:00
|
|
|
int ClientModel::getNumConnections(unsigned int flags) const
|
2011-05-22 11:19:43 -04:00
|
|
|
{
|
2016-04-16 19:30:03 -03:00
|
|
|
CConnman::NumConnections connections = CConnman::CONNECTIONS_NONE;
|
2014-02-16 15:48:27 -03:00
|
|
|
|
2016-04-16 19:30:03 -03:00
|
|
|
if(flags == CONNECTIONS_IN)
|
|
|
|
connections = CConnman::CONNECTIONS_IN;
|
|
|
|
else if (flags == CONNECTIONS_OUT)
|
|
|
|
connections = CConnman::CONNECTIONS_OUT;
|
|
|
|
else if (flags == CONNECTIONS_ALL)
|
|
|
|
connections = CConnman::CONNECTIONS_ALL;
|
2014-02-16 15:48:27 -03:00
|
|
|
|
2017-04-17 16:37:36 -03:00
|
|
|
return m_node.getNodeCount(connections);
|
2011-05-22 11:19:43 -04:00
|
|
|
}
|
|
|
|
|
2017-04-20 04:51:05 -03:00
|
|
|
int ClientModel::getHeaderTipHeight() const
|
2016-07-19 09:50:50 -04:00
|
|
|
{
|
2017-04-19 13:45:11 -03:00
|
|
|
if (cachedBestHeaderHeight == -1) {
|
|
|
|
// make sure we initially populate the cache via a cs_main lock
|
|
|
|
// otherwise we need to wait for a tip update
|
2017-04-17 16:37:36 -03:00
|
|
|
int height;
|
|
|
|
int64_t blockTime;
|
|
|
|
if (m_node.getHeaderTip(height, blockTime)) {
|
|
|
|
cachedBestHeaderHeight = height;
|
|
|
|
cachedBestHeaderTime = blockTime;
|
2017-04-19 13:45:11 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return cachedBestHeaderHeight;
|
2016-07-19 09:50:50 -04:00
|
|
|
}
|
|
|
|
|
2017-04-20 04:51:05 -03:00
|
|
|
int64_t ClientModel::getHeaderTipTime() const
|
2016-09-13 11:36:24 -03:00
|
|
|
{
|
2017-04-19 13:45:11 -03:00
|
|
|
if (cachedBestHeaderTime == -1) {
|
2017-04-17 16:37:36 -03:00
|
|
|
int height;
|
|
|
|
int64_t blockTime;
|
|
|
|
if (m_node.getHeaderTip(height, blockTime)) {
|
|
|
|
cachedBestHeaderHeight = height;
|
|
|
|
cachedBestHeaderTime = blockTime;
|
2017-04-19 13:45:11 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return cachedBestHeaderTime;
|
2016-09-13 11:36:24 -03:00
|
|
|
}
|
|
|
|
|
2012-05-05 10:07:14 -04:00
|
|
|
void ClientModel::updateNumConnections(int numConnections)
|
|
|
|
{
|
2015-07-14 08:59:05 -03:00
|
|
|
Q_EMIT numConnectionsChanged(numConnections);
|
2012-05-05 10:07:14 -04:00
|
|
|
}
|
|
|
|
|
2013-03-25 23:07:06 -03:00
|
|
|
void ClientModel::updateNetworkActive(bool networkActive)
|
|
|
|
{
|
|
|
|
Q_EMIT networkActiveChanged(networkActive);
|
|
|
|
}
|
|
|
|
|
2016-03-06 07:07:25 -03:00
|
|
|
void ClientModel::updateAlert()
|
2012-05-05 10:07:14 -04:00
|
|
|
{
|
2015-07-14 08:59:05 -03:00
|
|
|
Q_EMIT alertsChanged(getStatusBarWarnings());
|
2011-05-30 14:20:12 -04:00
|
|
|
}
|
|
|
|
|
2012-10-21 16:23:13 -03:00
|
|
|
enum BlockSource ClientModel::getBlockSource() const
|
2012-09-13 09:33:52 -03:00
|
|
|
{
|
2017-04-17 16:37:36 -03:00
|
|
|
if (m_node.getReindex())
|
2018-03-09 11:03:40 -03:00
|
|
|
return BlockSource::REINDEX;
|
2017-04-17 16:37:36 -03:00
|
|
|
else if (m_node.getImporting())
|
2018-03-09 11:03:40 -03:00
|
|
|
return BlockSource::DISK;
|
2013-04-10 10:45:49 -03:00
|
|
|
else if (getNumConnections() > 0)
|
2018-03-09 11:03:40 -03:00
|
|
|
return BlockSource::NETWORK;
|
2013-04-10 10:45:49 -03:00
|
|
|
|
2018-03-09 11:03:40 -03:00
|
|
|
return BlockSource::NONE;
|
2012-09-13 09:33:52 -03:00
|
|
|
}
|
|
|
|
|
2011-12-13 16:00:21 -03:00
|
|
|
QString ClientModel::getStatusBarWarnings() const
|
|
|
|
{
|
2019-12-15 12:34:03 -03:00
|
|
|
return QString::fromStdString(m_node.getWarnings());
|
2011-12-13 16:00:21 -03:00
|
|
|
}
|
|
|
|
|
2011-05-31 16:24:53 -04:00
|
|
|
OptionsModel *ClientModel::getOptionsModel()
|
|
|
|
{
|
2011-06-03 15:03:20 -04:00
|
|
|
return optionsModel;
|
|
|
|
}
|
|
|
|
|
2014-05-23 13:09:59 -04:00
|
|
|
PeerTableModel *ClientModel::getPeerTableModel()
|
|
|
|
{
|
|
|
|
return peerTableModel;
|
|
|
|
}
|
|
|
|
|
2015-06-20 15:27:03 -03:00
|
|
|
BanTableModel *ClientModel::getBanTableModel()
|
|
|
|
{
|
|
|
|
return banTableModel;
|
|
|
|
}
|
|
|
|
|
2011-07-01 11:06:36 -04:00
|
|
|
QString ClientModel::formatFullVersion() const
|
|
|
|
{
|
|
|
|
return QString::fromStdString(FormatFullVersion());
|
|
|
|
}
|
2012-04-06 21:06:53 -03:00
|
|
|
|
2015-08-06 10:40:50 -03:00
|
|
|
QString ClientModel::formatSubVersion() const
|
|
|
|
{
|
|
|
|
return QString::fromStdString(strSubVersion);
|
|
|
|
}
|
|
|
|
|
2012-10-24 16:47:07 -03:00
|
|
|
bool ClientModel::isReleaseVersion() const
|
|
|
|
{
|
|
|
|
return CLIENT_VERSION_IS_RELEASE;
|
|
|
|
}
|
|
|
|
|
2012-05-21 17:05:54 -04:00
|
|
|
QString ClientModel::formatClientStartupTime() const
|
2012-05-11 18:28:58 -04:00
|
|
|
{
|
2017-05-14 14:18:26 -04:00
|
|
|
return QDateTime::fromTime_t(GetStartupTime()).toString();
|
2012-05-11 18:28:58 -04:00
|
|
|
}
|
2012-05-06 13:40:58 -04:00
|
|
|
|
2016-03-22 04:40:10 -03:00
|
|
|
QString ClientModel::dataDir() const
|
|
|
|
{
|
2016-11-07 08:11:59 -03:00
|
|
|
return GUIUtil::boostPathToQString(GetDataDir());
|
2016-03-22 04:40:10 -03:00
|
|
|
}
|
|
|
|
|
2018-10-02 17:12:17 -03:00
|
|
|
QString ClientModel::blocksDir() const
|
|
|
|
{
|
|
|
|
return GUIUtil::boostPathToQString(GetBlocksDir());
|
|
|
|
}
|
|
|
|
|
2015-06-20 15:27:03 -03:00
|
|
|
void ClientModel::updateBanlist()
|
|
|
|
{
|
|
|
|
banTableModel->refresh();
|
|
|
|
}
|
|
|
|
|
2012-05-06 13:40:58 -04:00
|
|
|
// Handlers for core signals
|
2014-05-23 12:04:09 -04:00
|
|
|
static void ShowProgress(ClientModel *clientmodel, const std::string &title, int nProgress)
|
|
|
|
{
|
|
|
|
// emits signal "showProgress"
|
2019-07-06 12:16:01 -04:00
|
|
|
bool invoked = QMetaObject::invokeMethod(clientmodel, "showProgress", Qt::QueuedConnection,
|
2014-05-23 12:04:09 -04:00
|
|
|
Q_ARG(QString, QString::fromStdString(title)),
|
|
|
|
Q_ARG(int, nProgress));
|
2019-07-06 12:16:01 -04:00
|
|
|
assert(invoked);
|
2014-05-23 12:04:09 -04:00
|
|
|
}
|
|
|
|
|
2012-05-06 13:40:58 -04:00
|
|
|
static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
|
|
|
|
{
|
2015-01-08 07:44:25 -03:00
|
|
|
// Too noisy: qDebug() << "NotifyNumConnectionsChanged: " + QString::number(newNumConnections);
|
2019-07-06 12:16:01 -04:00
|
|
|
bool invoked = QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
|
2012-05-06 13:40:58 -04:00
|
|
|
Q_ARG(int, newNumConnections));
|
2019-07-06 12:16:01 -04:00
|
|
|
assert(invoked);
|
2012-05-06 13:40:58 -04:00
|
|
|
}
|
|
|
|
|
2013-03-25 23:07:06 -03:00
|
|
|
static void NotifyNetworkActiveChanged(ClientModel *clientmodel, bool networkActive)
|
|
|
|
{
|
2019-07-06 12:16:01 -04:00
|
|
|
bool invoked = QMetaObject::invokeMethod(clientmodel, "updateNetworkActive", Qt::QueuedConnection,
|
2013-03-25 23:07:06 -03:00
|
|
|
Q_ARG(bool, networkActive));
|
2019-07-06 12:16:01 -04:00
|
|
|
assert(invoked);
|
2013-03-25 23:07:06 -03:00
|
|
|
}
|
|
|
|
|
2016-03-06 07:07:25 -03:00
|
|
|
static void NotifyAlertChanged(ClientModel *clientmodel)
|
2012-05-06 13:40:58 -04:00
|
|
|
{
|
2016-03-06 07:07:25 -03:00
|
|
|
qDebug() << "NotifyAlertChanged";
|
2019-07-06 12:16:01 -04:00
|
|
|
bool invoked = QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection);
|
|
|
|
assert(invoked);
|
2012-05-06 13:40:58 -04:00
|
|
|
}
|
|
|
|
|
2015-06-20 15:27:28 -03:00
|
|
|
static void BannedListChanged(ClientModel *clientmodel)
|
|
|
|
{
|
2015-06-23 16:10:42 -03:00
|
|
|
qDebug() << QString("%1: Requesting update for peer banlist").arg(__func__);
|
2019-07-06 12:16:01 -04:00
|
|
|
bool invoked = QMetaObject::invokeMethod(clientmodel, "updateBanlist", Qt::QueuedConnection);
|
|
|
|
assert(invoked);
|
2015-06-20 15:27:28 -03:00
|
|
|
}
|
|
|
|
|
2017-04-17 16:37:36 -03:00
|
|
|
static void BlockTipChanged(ClientModel *clientmodel, bool initialSync, int height, int64_t blockTime, double verificationProgress, bool fHeader)
|
2015-11-26 12:39:40 -03:00
|
|
|
{
|
|
|
|
// 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();
|
|
|
|
|
2016-04-28 11:18:45 -03:00
|
|
|
int64_t& nLastUpdateNotification = fHeader ? nLastHeaderTipUpdateNotification : nLastBlockTipUpdateNotification;
|
|
|
|
|
2017-04-19 13:45:11 -03:00
|
|
|
if (fHeader) {
|
|
|
|
// cache best headers time and height to reduce future cs_main locks
|
2017-04-17 16:37:36 -03:00
|
|
|
clientmodel->cachedBestHeaderHeight = height;
|
|
|
|
clientmodel->cachedBestHeaderTime = blockTime;
|
2017-04-19 13:45:11 -03:00
|
|
|
}
|
2020-02-11 17:34:59 -03:00
|
|
|
|
|
|
|
// During initial sync, block notifications, and header notifications from reindexing are both throttled.
|
|
|
|
if (!initialSync || (fHeader && !clientmodel->node().getReindex()) || now - nLastUpdateNotification > MODEL_UPDATE_DELAY) {
|
2017-06-19 18:57:31 -04:00
|
|
|
//pass an async signal to the UI thread
|
2019-07-06 12:16:01 -04:00
|
|
|
bool invoked = QMetaObject::invokeMethod(clientmodel, "numBlocksChanged", Qt::QueuedConnection,
|
2017-04-17 16:37:36 -03:00
|
|
|
Q_ARG(int, height),
|
|
|
|
Q_ARG(QDateTime, QDateTime::fromTime_t(blockTime)),
|
|
|
|
Q_ARG(double, verificationProgress),
|
2016-04-28 11:18:45 -03:00
|
|
|
Q_ARG(bool, fHeader));
|
2019-07-06 12:16:01 -04:00
|
|
|
assert(invoked);
|
2016-04-28 11:18:45 -03:00
|
|
|
nLastUpdateNotification = now;
|
2015-11-26 12:39:40 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-06 13:40:58 -04:00
|
|
|
void ClientModel::subscribeToCoreSignals()
|
|
|
|
{
|
|
|
|
// Connect signals to client
|
2018-10-17 12:51:17 -03:00
|
|
|
m_handler_show_progress = m_node.handleShowProgress(std::bind(ShowProgress, this, std::placeholders::_1, std::placeholders::_2));
|
|
|
|
m_handler_notify_num_connections_changed = m_node.handleNotifyNumConnectionsChanged(std::bind(NotifyNumConnectionsChanged, this, std::placeholders::_1));
|
|
|
|
m_handler_notify_network_active_changed = m_node.handleNotifyNetworkActiveChanged(std::bind(NotifyNetworkActiveChanged, this, std::placeholders::_1));
|
|
|
|
m_handler_notify_alert_changed = m_node.handleNotifyAlertChanged(std::bind(NotifyAlertChanged, this));
|
|
|
|
m_handler_banned_list_changed = m_node.handleBannedListChanged(std::bind(BannedListChanged, this));
|
|
|
|
m_handler_notify_block_tip = m_node.handleNotifyBlockTip(std::bind(BlockTipChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, false));
|
|
|
|
m_handler_notify_header_tip = m_node.handleNotifyHeaderTip(std::bind(BlockTipChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, true));
|
2012-05-06 13:40:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClientModel::unsubscribeFromCoreSignals()
|
|
|
|
{
|
|
|
|
// Disconnect signals from client
|
2017-04-17 16:37:36 -03:00
|
|
|
m_handler_show_progress->disconnect();
|
|
|
|
m_handler_notify_num_connections_changed->disconnect();
|
|
|
|
m_handler_notify_network_active_changed->disconnect();
|
|
|
|
m_handler_notify_alert_changed->disconnect();
|
|
|
|
m_handler_banned_list_changed->disconnect();
|
|
|
|
m_handler_notify_block_tip->disconnect();
|
|
|
|
m_handler_notify_header_tip->disconnect();
|
2012-05-06 13:40:58 -04:00
|
|
|
}
|
2017-10-02 11:59:32 -03:00
|
|
|
|
|
|
|
bool ClientModel::getProxyInfo(std::string& ip_port) const
|
|
|
|
{
|
|
|
|
proxyType ipv4, ipv6;
|
|
|
|
if (m_node.getProxy((Network) 1, ipv4) && m_node.getProxy((Network) 2, ipv6)) {
|
|
|
|
ip_port = ipv4.proxy.ToStringIPPort();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|