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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // Copyright (c) 2011-2016 The Starwels 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 "clientmodel.h"
  5. #include "bantablemodel.h"
  6. #include "guiconstants.h"
  7. #include "guiutil.h"
  8. #include "peertablemodel.h"
  9. #include "chain.h"
  10. #include "chainparams.h"
  11. #include "checkpoints.h"
  12. #include "clientversion.h"
  13. #include "validation.h"
  14. #include "net.h"
  15. #include "txmempool.h"
  16. #include "ui_interface.h"
  17. #include "util.h"
  18. #include "warnings.h"
  19. #include <stdint.h>
  20. #include <QDebug>
  21. #include <QTimer>
  22. class CBlockIndex;
  23. static int64_t nLastHeaderTipUpdateNotification = 0;
  24. static int64_t nLastBlockTipUpdateNotification = 0;
  25. ClientModel::ClientModel(OptionsModel *_optionsModel, QObject *parent) :
  26. QObject(parent),
  27. optionsModel(_optionsModel),
  28. peerTableModel(0),
  29. banTableModel(0),
  30. pollTimer(0)
  31. {
  32. cachedBestHeaderHeight = -1;
  33. cachedBestHeaderTime = -1;
  34. peerTableModel = new PeerTableModel(this);
  35. banTableModel = new BanTableModel(this);
  36. pollTimer = new QTimer(this);
  37. connect(pollTimer, SIGNAL(timeout()), this, SLOT(updateTimer()));
  38. pollTimer->start(MODEL_UPDATE_DELAY);
  39. subscribeToCoreSignals();
  40. }
  41. ClientModel::~ClientModel()
  42. {
  43. unsubscribeFromCoreSignals();
  44. }
  45. int ClientModel::getNumConnections(unsigned int flags) const
  46. {
  47. CConnman::NumConnections connections = CConnman::CONNECTIONS_NONE;
  48. if(flags == CONNECTIONS_IN)
  49. connections = CConnman::CONNECTIONS_IN;
  50. else if (flags == CONNECTIONS_OUT)
  51. connections = CConnman::CONNECTIONS_OUT;
  52. else if (flags == CONNECTIONS_ALL)
  53. connections = CConnman::CONNECTIONS_ALL;
  54. if(g_connman)
  55. return g_connman->GetNodeCount(connections);
  56. return 0;
  57. }
  58. int ClientModel::getNumBlocks() const
  59. {
  60. LOCK(cs_main);
  61. return chainActive.Height();
  62. }
  63. int ClientModel::getHeaderTipHeight() const
  64. {
  65. if (cachedBestHeaderHeight == -1) {
  66. // make sure we initially populate the cache via a cs_main lock
  67. // otherwise we need to wait for a tip update
  68. LOCK(cs_main);
  69. if (pindexBestHeader) {
  70. cachedBestHeaderHeight = pindexBestHeader->nHeight;
  71. cachedBestHeaderTime = pindexBestHeader->GetBlockTime();
  72. }
  73. }
  74. return cachedBestHeaderHeight;
  75. }
  76. int64_t ClientModel::getHeaderTipTime() const
  77. {
  78. if (cachedBestHeaderTime == -1) {
  79. LOCK(cs_main);
  80. if (pindexBestHeader) {
  81. cachedBestHeaderHeight = pindexBestHeader->nHeight;
  82. cachedBestHeaderTime = pindexBestHeader->GetBlockTime();
  83. }
  84. }
  85. return cachedBestHeaderTime;
  86. }
  87. quint64 ClientModel::getTotalBytesRecv() const
  88. {
  89. if(!g_connman)
  90. return 0;
  91. return g_connman->GetTotalBytesRecv();
  92. }
  93. quint64 ClientModel::getTotalBytesSent() const
  94. {
  95. if(!g_connman)
  96. return 0;
  97. return g_connman->GetTotalBytesSent();
  98. }
  99. QDateTime ClientModel::getLastBlockDate() const
  100. {
  101. LOCK(cs_main);
  102. if (chainActive.Tip())
  103. return QDateTime::fromTime_t(chainActive.Tip()->GetBlockTime());
  104. return QDateTime::fromTime_t(Params().GenesisBlock().GetBlockTime()); // Genesis block's time of current network
  105. }
  106. long ClientModel::getMempoolSize() const
  107. {
  108. return mempool.size();
  109. }
  110. size_t ClientModel::getMempoolDynamicUsage() const
  111. {
  112. return mempool.DynamicMemoryUsage();
  113. }
  114. double ClientModel::getVerificationProgress(const CBlockIndex *tipIn) const
  115. {
  116. CBlockIndex *tip = const_cast<CBlockIndex *>(tipIn);
  117. if (!tip)
  118. {
  119. LOCK(cs_main);
  120. tip = chainActive.Tip();
  121. }
  122. return GuessVerificationProgress(Params().TxData(), tip);
  123. }
  124. void ClientModel::updateTimer()
  125. {
  126. // no locking required at this point
  127. // the following calls will acquire the required lock
  128. Q_EMIT mempoolSizeChanged(getMempoolSize(), getMempoolDynamicUsage());
  129. Q_EMIT bytesChanged(getTotalBytesRecv(), getTotalBytesSent());
  130. }
  131. void ClientModel::updateNumConnections(int numConnections)
  132. {
  133. Q_EMIT numConnectionsChanged(numConnections);
  134. }
  135. void ClientModel::updateNetworkActive(bool networkActive)
  136. {
  137. Q_EMIT networkActiveChanged(networkActive);
  138. }
  139. void ClientModel::updateAlert()
  140. {
  141. Q_EMIT alertsChanged(getStatusBarWarnings());
  142. }
  143. bool ClientModel::inInitialBlockDownload() const
  144. {
  145. return IsInitialBlockDownload();
  146. }
  147. enum BlockSource ClientModel::getBlockSource() const
  148. {
  149. if (fReindex)
  150. return BLOCK_SOURCE_REINDEX;
  151. else if (fImporting)
  152. return BLOCK_SOURCE_DISK;
  153. else if (getNumConnections() > 0)
  154. return BLOCK_SOURCE_NETWORK;
  155. return BLOCK_SOURCE_NONE;
  156. }
  157. void ClientModel::setNetworkActive(bool active)
  158. {
  159. if (g_connman) {
  160. g_connman->SetNetworkActive(active);
  161. }
  162. }
  163. bool ClientModel::getNetworkActive() const
  164. {
  165. if (g_connman) {
  166. return g_connman->GetNetworkActive();
  167. }
  168. return false;
  169. }
  170. QString ClientModel::getStatusBarWarnings() const
  171. {
  172. return QString::fromStdString(GetWarnings("gui"));
  173. }
  174. OptionsModel *ClientModel::getOptionsModel()
  175. {
  176. return optionsModel;
  177. }
  178. PeerTableModel *ClientModel::getPeerTableModel()
  179. {
  180. return peerTableModel;
  181. }
  182. BanTableModel *ClientModel::getBanTableModel()
  183. {
  184. return banTableModel;
  185. }
  186. QString ClientModel::formatFullVersion() const
  187. {
  188. return QString::fromStdString(FormatFullVersion());
  189. }
  190. QString ClientModel::formatSubVersion() const
  191. {
  192. return QString::fromStdString(strSubVersion);
  193. }
  194. bool ClientModel::isReleaseVersion() const
  195. {
  196. return CLIENT_VERSION_IS_RELEASE;
  197. }
  198. QString ClientModel::formatClientStartupTime() const
  199. {
  200. return QDateTime::fromTime_t(GetStartupTime()).toString();
  201. }
  202. QString ClientModel::dataDir() const
  203. {
  204. return GUIUtil::boostPathToQString(GetDataDir());
  205. }
  206. void ClientModel::updateBanlist()
  207. {
  208. banTableModel->refresh();
  209. }
  210. // Handlers for core signals
  211. static void ShowProgress(ClientModel *clientmodel, const std::string &title, int nProgress)
  212. {
  213. // emits signal "showProgress"
  214. QMetaObject::invokeMethod(clientmodel, "showProgress", Qt::QueuedConnection,
  215. Q_ARG(QString, QString::fromStdString(title)),
  216. Q_ARG(int, nProgress));
  217. }
  218. static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
  219. {
  220. // Too noisy: qDebug() << "NotifyNumConnectionsChanged: " + QString::number(newNumConnections);
  221. QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
  222. Q_ARG(int, newNumConnections));
  223. }
  224. static void NotifyNetworkActiveChanged(ClientModel *clientmodel, bool networkActive)
  225. {
  226. QMetaObject::invokeMethod(clientmodel, "updateNetworkActive", Qt::QueuedConnection,
  227. Q_ARG(bool, networkActive));
  228. }
  229. static void NotifyAlertChanged(ClientModel *clientmodel)
  230. {
  231. qDebug() << "NotifyAlertChanged";
  232. QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection);
  233. }
  234. static void BannedListChanged(ClientModel *clientmodel)
  235. {
  236. qDebug() << QString("%1: Requesting update for peer banlist").arg(__func__);
  237. QMetaObject::invokeMethod(clientmodel, "updateBanlist", Qt::QueuedConnection);
  238. }
  239. static void BlockTipChanged(ClientModel *clientmodel, bool initialSync, const CBlockIndex *pIndex, bool fHeader)
  240. {
  241. // lock free async UI updates in case we have a new block tip
  242. // during initial sync, only update the UI if the last update
  243. // was > 250ms (MODEL_UPDATE_DELAY) ago
  244. int64_t now = 0;
  245. if (initialSync)
  246. now = GetTimeMillis();
  247. int64_t& nLastUpdateNotification = fHeader ? nLastHeaderTipUpdateNotification : nLastBlockTipUpdateNotification;
  248. if (fHeader) {
  249. // cache best headers time and height to reduce future cs_main locks
  250. clientmodel->cachedBestHeaderHeight = pIndex->nHeight;
  251. clientmodel->cachedBestHeaderTime = pIndex->GetBlockTime();
  252. }
  253. // if we are in-sync, update the UI regardless of last update time
  254. if (!initialSync || now - nLastUpdateNotification > MODEL_UPDATE_DELAY) {
  255. //pass an async signal to the UI thread
  256. QMetaObject::invokeMethod(clientmodel, "numBlocksChanged", Qt::QueuedConnection,
  257. Q_ARG(int, pIndex->nHeight),
  258. Q_ARG(QDateTime, QDateTime::fromTime_t(pIndex->GetBlockTime())),
  259. Q_ARG(double, clientmodel->getVerificationProgress(pIndex)),
  260. Q_ARG(bool, fHeader));
  261. nLastUpdateNotification = now;
  262. }
  263. }
  264. void ClientModel::subscribeToCoreSignals()
  265. {
  266. // Connect signals to client
  267. uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2));
  268. uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, _1));
  269. uiInterface.NotifyNetworkActiveChanged.connect(boost::bind(NotifyNetworkActiveChanged, this, _1));
  270. uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this));
  271. uiInterface.BannedListChanged.connect(boost::bind(BannedListChanged, this));
  272. uiInterface.NotifyBlockTip.connect(boost::bind(BlockTipChanged, this, _1, _2, false));
  273. uiInterface.NotifyHeaderTip.connect(boost::bind(BlockTipChanged, this, _1, _2, true));
  274. }
  275. void ClientModel::unsubscribeFromCoreSignals()
  276. {
  277. // Disconnect signals from client
  278. uiInterface.ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2));
  279. uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, _1));
  280. uiInterface.NotifyNetworkActiveChanged.disconnect(boost::bind(NotifyNetworkActiveChanged, this, _1));
  281. uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this));
  282. uiInterface.BannedListChanged.disconnect(boost::bind(BannedListChanged, this));
  283. uiInterface.NotifyBlockTip.disconnect(boost::bind(BlockTipChanged, this, _1, _2, false));
  284. uiInterface.NotifyHeaderTip.disconnect(boost::bind(BlockTipChanged, this, _1, _2, true));
  285. }