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 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "clientmodel.h"
  2. #include "guiconstants.h"
  3. #include "optionsmodel.h"
  4. #include "addresstablemodel.h"
  5. #include "transactiontablemodel.h"
  6. #include "chainparams.h"
  7. #include "alert.h"
  8. #include "main.h"
  9. #include "checkpoints.h"
  10. #include "ui_interface.h"
  11. #include <QDateTime>
  12. #include <QTimer>
  13. static const int64 nClientStartupTime = GetTime();
  14. ClientModel::ClientModel(OptionsModel *optionsModel, QObject *parent) :
  15. QObject(parent), optionsModel(optionsModel),
  16. cachedNumBlocks(0), cachedNumBlocksOfPeers(0),
  17. cachedReindexing(0), cachedImporting(0),
  18. numBlocksAtStartup(-1), pollTimer(0)
  19. {
  20. pollTimer = new QTimer(this);
  21. pollTimer->setInterval(MODEL_UPDATE_DELAY);
  22. pollTimer->start();
  23. connect(pollTimer, SIGNAL(timeout()), this, SLOT(updateTimer()));
  24. subscribeToCoreSignals();
  25. }
  26. ClientModel::~ClientModel()
  27. {
  28. unsubscribeFromCoreSignals();
  29. }
  30. int ClientModel::getNumConnections() const
  31. {
  32. return vNodes.size();
  33. }
  34. int ClientModel::getNumBlocks() const
  35. {
  36. return nBestHeight;
  37. }
  38. int ClientModel::getNumBlocksAtStartup()
  39. {
  40. if (numBlocksAtStartup == -1) numBlocksAtStartup = getNumBlocks();
  41. return numBlocksAtStartup;
  42. }
  43. QDateTime ClientModel::getLastBlockDate() const
  44. {
  45. if (pindexBest)
  46. return QDateTime::fromTime_t(pindexBest->GetBlockTime());
  47. else if(!isTestNet())
  48. return QDateTime::fromTime_t(1231006505); // Genesis block's time
  49. else
  50. return QDateTime::fromTime_t(1296688602); // Genesis block's time (testnet)
  51. }
  52. double ClientModel::getVerificationProgress() const
  53. {
  54. return Checkpoints::GuessVerificationProgress(pindexBest);
  55. }
  56. void ClientModel::updateTimer()
  57. {
  58. // Some quantities (such as number of blocks) change so fast that we don't want to be notified for each change.
  59. // Periodically check and update with a timer.
  60. int newNumBlocks = getNumBlocks();
  61. int newNumBlocksOfPeers = getNumBlocksOfPeers();
  62. // check for changed number of blocks we have, number of blocks peers claim to have, reindexing state and importing state
  63. if (cachedNumBlocks != newNumBlocks || cachedNumBlocksOfPeers != newNumBlocksOfPeers ||
  64. cachedReindexing != fReindex || cachedImporting != fImporting)
  65. {
  66. cachedNumBlocks = newNumBlocks;
  67. cachedNumBlocksOfPeers = newNumBlocksOfPeers;
  68. cachedReindexing = fReindex;
  69. cachedImporting = fImporting;
  70. // ensure we return the maximum of newNumBlocksOfPeers and newNumBlocks to not create weird displays in the GUI
  71. emit numBlocksChanged(newNumBlocks, std::max(newNumBlocksOfPeers, newNumBlocks));
  72. }
  73. }
  74. void ClientModel::updateNumConnections(int numConnections)
  75. {
  76. emit numConnectionsChanged(numConnections);
  77. }
  78. void ClientModel::updateAlert(const QString &hash, int status)
  79. {
  80. // Show error message notification for new alert
  81. if(status == CT_NEW)
  82. {
  83. uint256 hash_256;
  84. hash_256.SetHex(hash.toStdString());
  85. CAlert alert = CAlert::getAlertByHash(hash_256);
  86. if(!alert.IsNull())
  87. {
  88. emit message(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), CClientUIInterface::ICON_ERROR);
  89. }
  90. }
  91. emit alertsChanged(getStatusBarWarnings());
  92. }
  93. bool ClientModel::isTestNet() const
  94. {
  95. return TestNet();
  96. }
  97. bool ClientModel::inInitialBlockDownload() const
  98. {
  99. return IsInitialBlockDownload();
  100. }
  101. enum BlockSource ClientModel::getBlockSource() const
  102. {
  103. if (fReindex)
  104. return BLOCK_SOURCE_REINDEX;
  105. else if (fImporting)
  106. return BLOCK_SOURCE_DISK;
  107. else if (getNumConnections() > 0)
  108. return BLOCK_SOURCE_NETWORK;
  109. return BLOCK_SOURCE_NONE;
  110. }
  111. int ClientModel::getNumBlocksOfPeers() const
  112. {
  113. return GetNumBlocksOfPeers();
  114. }
  115. QString ClientModel::getStatusBarWarnings() const
  116. {
  117. return QString::fromStdString(GetWarnings("statusbar"));
  118. }
  119. OptionsModel *ClientModel::getOptionsModel()
  120. {
  121. return optionsModel;
  122. }
  123. QString ClientModel::formatFullVersion() const
  124. {
  125. return QString::fromStdString(FormatFullVersion());
  126. }
  127. QString ClientModel::formatBuildDate() const
  128. {
  129. return QString::fromStdString(CLIENT_DATE);
  130. }
  131. bool ClientModel::isReleaseVersion() const
  132. {
  133. return CLIENT_VERSION_IS_RELEASE;
  134. }
  135. QString ClientModel::clientName() const
  136. {
  137. return QString::fromStdString(CLIENT_NAME);
  138. }
  139. QString ClientModel::formatClientStartupTime() const
  140. {
  141. return QDateTime::fromTime_t(nClientStartupTime).toString();
  142. }
  143. // Handlers for core signals
  144. static void NotifyBlocksChanged(ClientModel *clientmodel)
  145. {
  146. // This notification is too frequent. Don't trigger a signal.
  147. // Don't remove it, though, as it might be useful later.
  148. }
  149. static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
  150. {
  151. // Too noisy: OutputDebugStringF("NotifyNumConnectionsChanged %i\n", newNumConnections);
  152. QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
  153. Q_ARG(int, newNumConnections));
  154. }
  155. static void NotifyAlertChanged(ClientModel *clientmodel, const uint256 &hash, ChangeType status)
  156. {
  157. OutputDebugStringF("NotifyAlertChanged %s status=%i\n", hash.GetHex().c_str(), status);
  158. QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection,
  159. Q_ARG(QString, QString::fromStdString(hash.GetHex())),
  160. Q_ARG(int, status));
  161. }
  162. void ClientModel::subscribeToCoreSignals()
  163. {
  164. // Connect signals to client
  165. uiInterface.NotifyBlocksChanged.connect(boost::bind(NotifyBlocksChanged, this));
  166. uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, _1));
  167. uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this, _1, _2));
  168. }
  169. void ClientModel::unsubscribeFromCoreSignals()
  170. {
  171. // Disconnect signals from client
  172. uiInterface.NotifyBlocksChanged.disconnect(boost::bind(NotifyBlocksChanged, this));
  173. uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, _1));
  174. uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this, _1, _2));
  175. }