You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

clientmodel.cpp 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Copyright (c) 2011-2013 The Bitcoin developers
  2. // Distributed under the MIT/X11 software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "clientmodel.h"
  5. #include "guiconstants.h"
  6. #include "peertablemodel.h"
  7. #include "alert.h"
  8. #include "chainparams.h"
  9. #include "checkpoints.h"
  10. #include "main.h"
  11. #include "net.h"
  12. #include "ui_interface.h"
  13. #include <stdint.h>
  14. #include <QDateTime>
  15. #include <QDebug>
  16. #include <QTimer>
  17. static const int64_t nClientStartupTime = GetTime();
  18. ClientModel::ClientModel(OptionsModel *optionsModel, QObject *parent) :
  19. QObject(parent),
  20. optionsModel(optionsModel),
  21. peerTableModel(0),
  22. cachedNumBlocks(0),
  23. cachedReindexing(0), cachedImporting(0),
  24. numBlocksAtStartup(-1), pollTimer(0)
  25. {
  26. peerTableModel = new PeerTableModel(this);
  27. pollTimer = new QTimer(this);
  28. connect(pollTimer, SIGNAL(timeout()), this, SLOT(updateTimer()));
  29. pollTimer->start(MODEL_UPDATE_DELAY);
  30. subscribeToCoreSignals();
  31. }
  32. ClientModel::~ClientModel()
  33. {
  34. unsubscribeFromCoreSignals();
  35. }
  36. int ClientModel::getNumConnections(unsigned int flags) const
  37. {
  38. LOCK(cs_vNodes);
  39. if (flags == CONNECTIONS_ALL) // Shortcut if we want total
  40. return vNodes.size();
  41. int nNum = 0;
  42. BOOST_FOREACH(CNode* pnode, vNodes)
  43. if (flags & (pnode->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT))
  44. nNum++;
  45. return nNum;
  46. }
  47. int ClientModel::getNumBlocks() const
  48. {
  49. LOCK(cs_main);
  50. return chainActive.Height();
  51. }
  52. int ClientModel::getNumBlocksAtStartup()
  53. {
  54. if (numBlocksAtStartup == -1) numBlocksAtStartup = getNumBlocks();
  55. return numBlocksAtStartup;
  56. }
  57. quint64 ClientModel::getTotalBytesRecv() const
  58. {
  59. return CNode::GetTotalBytesRecv();
  60. }
  61. quint64 ClientModel::getTotalBytesSent() const
  62. {
  63. return CNode::GetTotalBytesSent();
  64. }
  65. QDateTime ClientModel::getLastBlockDate() const
  66. {
  67. LOCK(cs_main);
  68. if (chainActive.Tip())
  69. return QDateTime::fromTime_t(chainActive.Tip()->GetBlockTime());
  70. else
  71. return QDateTime::fromTime_t(Params().GenesisBlock().nTime); // Genesis block's time of current network
  72. }
  73. double ClientModel::getVerificationProgress() const
  74. {
  75. LOCK(cs_main);
  76. return Checkpoints::GuessVerificationProgress(chainActive.Tip());
  77. }
  78. void ClientModel::updateTimer()
  79. {
  80. // Get required lock upfront. This avoids the GUI from getting stuck on
  81. // periodical polls if the core is holding the locks for a longer time -
  82. // for example, during a wallet rescan.
  83. TRY_LOCK(cs_main, lockMain);
  84. if(!lockMain)
  85. return;
  86. // Some quantities (such as number of blocks) change so fast that we don't want to be notified for each change.
  87. // Periodically check and update with a timer.
  88. int newNumBlocks = getNumBlocks();
  89. // check for changed number of blocks we have, number of blocks peers claim to have, reindexing state and importing state
  90. if (cachedNumBlocks != newNumBlocks ||
  91. cachedReindexing != fReindex || cachedImporting != fImporting)
  92. {
  93. cachedNumBlocks = newNumBlocks;
  94. cachedReindexing = fReindex;
  95. cachedImporting = fImporting;
  96. emit numBlocksChanged(newNumBlocks);
  97. }
  98. emit bytesChanged(getTotalBytesRecv(), getTotalBytesSent());
  99. }
  100. void ClientModel::updateNumConnections(int numConnections)
  101. {
  102. emit numConnectionsChanged(numConnections);
  103. }
  104. void ClientModel::updateAlert(const QString &hash, int status)
  105. {
  106. // Show error message notification for new alert
  107. if(status == CT_NEW)
  108. {
  109. uint256 hash_256;
  110. hash_256.SetHex(hash.toStdString());
  111. CAlert alert = CAlert::getAlertByHash(hash_256);
  112. if(!alert.IsNull())
  113. {
  114. emit message(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), CClientUIInterface::ICON_ERROR);
  115. }
  116. }
  117. emit alertsChanged(getStatusBarWarnings());
  118. }
  119. bool ClientModel::inInitialBlockDownload() const
  120. {
  121. return IsInitialBlockDownload();
  122. }
  123. enum BlockSource ClientModel::getBlockSource() const
  124. {
  125. if (fReindex)
  126. return BLOCK_SOURCE_REINDEX;
  127. else if (fImporting)
  128. return BLOCK_SOURCE_DISK;
  129. else if (getNumConnections() > 0)
  130. return BLOCK_SOURCE_NETWORK;
  131. return BLOCK_SOURCE_NONE;
  132. }
  133. QString ClientModel::getStatusBarWarnings() const
  134. {
  135. return QString::fromStdString(GetWarnings("statusbar"));
  136. }
  137. OptionsModel *ClientModel::getOptionsModel()
  138. {
  139. return optionsModel;
  140. }
  141. PeerTableModel *ClientModel::getPeerTableModel()
  142. {
  143. return peerTableModel;
  144. }
  145. QString ClientModel::formatFullVersion() const
  146. {
  147. return QString::fromStdString(FormatFullVersion());
  148. }
  149. QString ClientModel::formatBuildDate() const
  150. {
  151. return QString::fromStdString(CLIENT_DATE);
  152. }
  153. bool ClientModel::isReleaseVersion() const
  154. {
  155. return CLIENT_VERSION_IS_RELEASE;
  156. }
  157. QString ClientModel::clientName() const
  158. {
  159. return QString::fromStdString(CLIENT_NAME);
  160. }
  161. QString ClientModel::formatClientStartupTime() const
  162. {
  163. return QDateTime::fromTime_t(nClientStartupTime).toString();
  164. }
  165. // Handlers for core signals
  166. static void ShowProgress(ClientModel *clientmodel, const std::string &title, int nProgress)
  167. {
  168. // emits signal "showProgress"
  169. QMetaObject::invokeMethod(clientmodel, "showProgress", Qt::QueuedConnection,
  170. Q_ARG(QString, QString::fromStdString(title)),
  171. Q_ARG(int, nProgress));
  172. }
  173. static void NotifyBlocksChanged(ClientModel *clientmodel)
  174. {
  175. // This notification is too frequent. Don't trigger a signal.
  176. // Don't remove it, though, as it might be useful later.
  177. }
  178. static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
  179. {
  180. // Too noisy: qDebug() << "NotifyNumConnectionsChanged : " + QString::number(newNumConnections);
  181. QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
  182. Q_ARG(int, newNumConnections));
  183. }
  184. static void NotifyAlertChanged(ClientModel *clientmodel, const uint256 &hash, ChangeType status)
  185. {
  186. qDebug() << "NotifyAlertChanged : " + QString::fromStdString(hash.GetHex()) + " status=" + QString::number(status);
  187. QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection,
  188. Q_ARG(QString, QString::fromStdString(hash.GetHex())),
  189. Q_ARG(int, status));
  190. }
  191. void ClientModel::subscribeToCoreSignals()
  192. {
  193. // Connect signals to client
  194. uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2));
  195. uiInterface.NotifyBlocksChanged.connect(boost::bind(NotifyBlocksChanged, this));
  196. uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, _1));
  197. uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this, _1, _2));
  198. }
  199. void ClientModel::unsubscribeFromCoreSignals()
  200. {
  201. // Disconnect signals from client
  202. uiInterface.ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2));
  203. uiInterface.NotifyBlocksChanged.disconnect(boost::bind(NotifyBlocksChanged, this));
  204. uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, _1));
  205. uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this, _1, _2));
  206. }