PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/qt/clientmodel.cpp

http://github.com/bitcoin/bitcoin
C++ | 301 lines | 237 code | 45 blank | 19 comment | 30 complexity | 39cf37b2d11c5b27da2f1231e3eea5f0 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, MIT
  1. // Copyright (c) 2011-2020 The Bitcoin Core developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include <qt/clientmodel.h>
  5. #include <qt/bantablemodel.h>
  6. #include <qt/guiconstants.h>
  7. #include <qt/guiutil.h>
  8. #include <qt/peertablemodel.h>
  9. #include <clientversion.h>
  10. #include <interfaces/handler.h>
  11. #include <interfaces/node.h>
  12. #include <net.h>
  13. #include <netbase.h>
  14. #include <util/system.h>
  15. #include <stdint.h>
  16. #include <QDebug>
  17. #include <QThread>
  18. #include <QTimer>
  19. static int64_t nLastHeaderTipUpdateNotification = 0;
  20. static int64_t nLastBlockTipUpdateNotification = 0;
  21. ClientModel::ClientModel(interfaces::Node& node, OptionsModel *_optionsModel, QObject *parent) :
  22. QObject(parent),
  23. m_node(node),
  24. optionsModel(_optionsModel),
  25. peerTableModel(nullptr),
  26. banTableModel(nullptr),
  27. m_thread(new QThread(this))
  28. {
  29. cachedBestHeaderHeight = -1;
  30. cachedBestHeaderTime = -1;
  31. peerTableModel = new PeerTableModel(m_node, this);
  32. banTableModel = new BanTableModel(m_node, this);
  33. QTimer* timer = new QTimer;
  34. timer->setInterval(MODEL_UPDATE_DELAY);
  35. connect(timer, &QTimer::timeout, [this] {
  36. // no locking required at this point
  37. // the following calls will acquire the required lock
  38. Q_EMIT mempoolSizeChanged(m_node.getMempoolSize(), m_node.getMempoolDynamicUsage());
  39. Q_EMIT bytesChanged(m_node.getTotalBytesRecv(), m_node.getTotalBytesSent());
  40. });
  41. connect(m_thread, &QThread::finished, timer, &QObject::deleteLater);
  42. connect(m_thread, &QThread::started, [timer] { timer->start(); });
  43. // move timer to thread so that polling doesn't disturb main event loop
  44. timer->moveToThread(m_thread);
  45. m_thread->start();
  46. subscribeToCoreSignals();
  47. }
  48. ClientModel::~ClientModel()
  49. {
  50. unsubscribeFromCoreSignals();
  51. m_thread->quit();
  52. m_thread->wait();
  53. }
  54. int ClientModel::getNumConnections(unsigned int flags) const
  55. {
  56. CConnman::NumConnections connections = CConnman::CONNECTIONS_NONE;
  57. if(flags == CONNECTIONS_IN)
  58. connections = CConnman::CONNECTIONS_IN;
  59. else if (flags == CONNECTIONS_OUT)
  60. connections = CConnman::CONNECTIONS_OUT;
  61. else if (flags == CONNECTIONS_ALL)
  62. connections = CConnman::CONNECTIONS_ALL;
  63. return m_node.getNodeCount(connections);
  64. }
  65. int ClientModel::getHeaderTipHeight() const
  66. {
  67. if (cachedBestHeaderHeight == -1) {
  68. // make sure we initially populate the cache via a cs_main lock
  69. // otherwise we need to wait for a tip update
  70. int height;
  71. int64_t blockTime;
  72. if (m_node.getHeaderTip(height, blockTime)) {
  73. cachedBestHeaderHeight = height;
  74. cachedBestHeaderTime = blockTime;
  75. }
  76. }
  77. return cachedBestHeaderHeight;
  78. }
  79. int64_t ClientModel::getHeaderTipTime() const
  80. {
  81. if (cachedBestHeaderTime == -1) {
  82. int height;
  83. int64_t blockTime;
  84. if (m_node.getHeaderTip(height, blockTime)) {
  85. cachedBestHeaderHeight = height;
  86. cachedBestHeaderTime = blockTime;
  87. }
  88. }
  89. return cachedBestHeaderTime;
  90. }
  91. int ClientModel::getNumBlocks() const
  92. {
  93. if (m_cached_num_blocks == -1) {
  94. m_cached_num_blocks = m_node.getNumBlocks();
  95. }
  96. return m_cached_num_blocks;
  97. }
  98. void ClientModel::updateNumConnections(int numConnections)
  99. {
  100. Q_EMIT numConnectionsChanged(numConnections);
  101. }
  102. void ClientModel::updateNetworkActive(bool networkActive)
  103. {
  104. Q_EMIT networkActiveChanged(networkActive);
  105. }
  106. void ClientModel::updateAlert()
  107. {
  108. Q_EMIT alertsChanged(getStatusBarWarnings());
  109. }
  110. enum BlockSource ClientModel::getBlockSource() const
  111. {
  112. if (m_node.getReindex())
  113. return BlockSource::REINDEX;
  114. else if (m_node.getImporting())
  115. return BlockSource::DISK;
  116. else if (getNumConnections() > 0)
  117. return BlockSource::NETWORK;
  118. return BlockSource::NONE;
  119. }
  120. QString ClientModel::getStatusBarWarnings() const
  121. {
  122. return QString::fromStdString(m_node.getWarnings());
  123. }
  124. OptionsModel *ClientModel::getOptionsModel()
  125. {
  126. return optionsModel;
  127. }
  128. PeerTableModel *ClientModel::getPeerTableModel()
  129. {
  130. return peerTableModel;
  131. }
  132. BanTableModel *ClientModel::getBanTableModel()
  133. {
  134. return banTableModel;
  135. }
  136. QString ClientModel::formatFullVersion() const
  137. {
  138. return QString::fromStdString(FormatFullVersion());
  139. }
  140. QString ClientModel::formatSubVersion() const
  141. {
  142. return QString::fromStdString(strSubVersion);
  143. }
  144. bool ClientModel::isReleaseVersion() const
  145. {
  146. return CLIENT_VERSION_IS_RELEASE;
  147. }
  148. QString ClientModel::formatClientStartupTime() const
  149. {
  150. return QDateTime::fromTime_t(GetStartupTime()).toString();
  151. }
  152. QString ClientModel::dataDir() const
  153. {
  154. return GUIUtil::boostPathToQString(GetDataDir());
  155. }
  156. QString ClientModel::blocksDir() const
  157. {
  158. return GUIUtil::boostPathToQString(GetBlocksDir());
  159. }
  160. void ClientModel::updateBanlist()
  161. {
  162. banTableModel->refresh();
  163. }
  164. // Handlers for core signals
  165. static void ShowProgress(ClientModel *clientmodel, const std::string &title, int nProgress)
  166. {
  167. // emits signal "showProgress"
  168. bool invoked = QMetaObject::invokeMethod(clientmodel, "showProgress", Qt::QueuedConnection,
  169. Q_ARG(QString, QString::fromStdString(title)),
  170. Q_ARG(int, nProgress));
  171. assert(invoked);
  172. }
  173. static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
  174. {
  175. // Too noisy: qDebug() << "NotifyNumConnectionsChanged: " + QString::number(newNumConnections);
  176. bool invoked = QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
  177. Q_ARG(int, newNumConnections));
  178. assert(invoked);
  179. }
  180. static void NotifyNetworkActiveChanged(ClientModel *clientmodel, bool networkActive)
  181. {
  182. bool invoked = QMetaObject::invokeMethod(clientmodel, "updateNetworkActive", Qt::QueuedConnection,
  183. Q_ARG(bool, networkActive));
  184. assert(invoked);
  185. }
  186. static void NotifyAlertChanged(ClientModel *clientmodel)
  187. {
  188. qDebug() << "NotifyAlertChanged";
  189. bool invoked = QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection);
  190. assert(invoked);
  191. }
  192. static void BannedListChanged(ClientModel *clientmodel)
  193. {
  194. qDebug() << QString("%1: Requesting update for peer banlist").arg(__func__);
  195. bool invoked = QMetaObject::invokeMethod(clientmodel, "updateBanlist", Qt::QueuedConnection);
  196. assert(invoked);
  197. }
  198. static void BlockTipChanged(ClientModel *clientmodel, bool initialSync, int height, int64_t blockTime, double verificationProgress, bool fHeader)
  199. {
  200. // lock free async UI updates in case we have a new block tip
  201. // during initial sync, only update the UI if the last update
  202. // was > 250ms (MODEL_UPDATE_DELAY) ago
  203. int64_t now = 0;
  204. if (initialSync)
  205. now = GetTimeMillis();
  206. int64_t& nLastUpdateNotification = fHeader ? nLastHeaderTipUpdateNotification : nLastBlockTipUpdateNotification;
  207. if (fHeader) {
  208. // cache best headers time and height to reduce future cs_main locks
  209. clientmodel->cachedBestHeaderHeight = height;
  210. clientmodel->cachedBestHeaderTime = blockTime;
  211. } else {
  212. clientmodel->m_cached_num_blocks = height;
  213. }
  214. // During initial sync, block notifications, and header notifications from reindexing are both throttled.
  215. if (!initialSync || (fHeader && !clientmodel->node().getReindex()) || now - nLastUpdateNotification > MODEL_UPDATE_DELAY) {
  216. //pass an async signal to the UI thread
  217. bool invoked = QMetaObject::invokeMethod(clientmodel, "numBlocksChanged", Qt::QueuedConnection,
  218. Q_ARG(int, height),
  219. Q_ARG(QDateTime, QDateTime::fromTime_t(blockTime)),
  220. Q_ARG(double, verificationProgress),
  221. Q_ARG(bool, fHeader));
  222. assert(invoked);
  223. nLastUpdateNotification = now;
  224. }
  225. }
  226. void ClientModel::subscribeToCoreSignals()
  227. {
  228. // Connect signals to client
  229. m_handler_show_progress = m_node.handleShowProgress(std::bind(ShowProgress, this, std::placeholders::_1, std::placeholders::_2));
  230. m_handler_notify_num_connections_changed = m_node.handleNotifyNumConnectionsChanged(std::bind(NotifyNumConnectionsChanged, this, std::placeholders::_1));
  231. m_handler_notify_network_active_changed = m_node.handleNotifyNetworkActiveChanged(std::bind(NotifyNetworkActiveChanged, this, std::placeholders::_1));
  232. m_handler_notify_alert_changed = m_node.handleNotifyAlertChanged(std::bind(NotifyAlertChanged, this));
  233. m_handler_banned_list_changed = m_node.handleBannedListChanged(std::bind(BannedListChanged, this));
  234. 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));
  235. 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));
  236. }
  237. void ClientModel::unsubscribeFromCoreSignals()
  238. {
  239. // Disconnect signals from client
  240. m_handler_show_progress->disconnect();
  241. m_handler_notify_num_connections_changed->disconnect();
  242. m_handler_notify_network_active_changed->disconnect();
  243. m_handler_notify_alert_changed->disconnect();
  244. m_handler_banned_list_changed->disconnect();
  245. m_handler_notify_block_tip->disconnect();
  246. m_handler_notify_header_tip->disconnect();
  247. }
  248. bool ClientModel::getProxyInfo(std::string& ip_port) const
  249. {
  250. proxyType ipv4, ipv6;
  251. if (m_node.getProxy((Network) 1, ipv4) && m_node.getProxy((Network) 2, ipv6)) {
  252. ip_port = ipv4.proxy.ToStringIPPort();
  253. return true;
  254. }
  255. return false;
  256. }