Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
clientmodel.cpp
Go to the documentation of this file.
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 
5 #include "clientmodel.h"
6 
7 #include "guiconstants.h"
8 
9 #include "alert.h"
10 #include "chainparams.h"
11 #include "checkpoints.h"
12 #include "main.h"
13 #include "net.h"
14 #include "ui_interface.h"
15 
16 #include <stdint.h>
17 
18 #include <QDateTime>
19 #include <QDebug>
20 #include <QTimer>
21 
22 static const int64_t nClientStartupTime = GetTime();
23 
25  QObject(parent), optionsModel(optionsModel),
26  cachedNumBlocks(0),
27  cachedReindexing(0), cachedImporting(0),
28  numBlocksAtStartup(-1), pollTimer(0)
29 {
30  pollTimer = new QTimer(this);
31  connect(pollTimer, SIGNAL(timeout()), this, SLOT(updateTimer()));
33 
35 }
36 
38 {
40 }
41 
42 int ClientModel::getNumConnections(unsigned int flags) const
43 {
44  LOCK(cs_vNodes);
45  if (flags == CONNECTIONS_ALL) // Shortcut if we want total
46  return vNodes.size();
47 
48  int nNum = 0;
49  BOOST_FOREACH(CNode* pnode, vNodes)
50  if (flags & (pnode->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT))
51  nNum++;
52 
53  return nNum;
54 }
55 
57 {
58  LOCK(cs_main);
59  return chainActive.Height();
60 }
61 
63 {
65  return numBlocksAtStartup;
66 }
67 
69 {
70  return CNode::GetTotalBytesRecv();
71 }
72 
74 {
75  return CNode::GetTotalBytesSent();
76 }
77 
79 {
80  LOCK(cs_main);
81  if (chainActive.Tip())
82  return QDateTime::fromTime_t(chainActive.Tip()->GetBlockTime());
83  else
84  return QDateTime::fromTime_t(Params().GenesisBlock().nTime); // Genesis block's time of current network
85 }
86 
88 {
89  LOCK(cs_main);
91 }
92 
94 {
95  // Get required lock upfront. This avoids the GUI from getting stuck on
96  // periodical polls if the core is holding the locks for a longer time -
97  // for example, during a wallet rescan.
98  TRY_LOCK(cs_main, lockMain);
99  if(!lockMain)
100  return;
101  // Some quantities (such as number of blocks) change so fast that we don't want to be notified for each change.
102  // Periodically check and update with a timer.
103  int newNumBlocks = getNumBlocks();
104 
105  // check for changed number of blocks we have, number of blocks peers claim to have, reindexing state and importing state
106  if (cachedNumBlocks != newNumBlocks ||
108  {
109  cachedNumBlocks = newNumBlocks;
112 
113  emit numBlocksChanged(newNumBlocks);
114  }
115 
117 }
118 
119 void ClientModel::updateNumConnections(int numConnections)
120 {
121  emit numConnectionsChanged(numConnections);
122 }
123 
124 void ClientModel::updateAlert(const QString &hash, int status)
125 {
126  // Show error message notification for new alert
127  if(status == CT_NEW)
128  {
129  uint256 hash_256;
130  hash_256.SetHex(hash.toStdString());
131  CAlert alert = CAlert::getAlertByHash(hash_256);
132  if(!alert.IsNull())
133  {
134  emit message(tr("Network Alert"), QString::fromStdString(alert.strStatusBar), CClientUIInterface::ICON_ERROR);
135  }
136  }
137 
139 }
140 
142 {
143  QString netname(QString::fromStdString(Params().DataDir()));
144  if(netname.isEmpty())
145  netname = "main";
146  return netname;
147 }
148 
150 {
151  return IsInitialBlockDownload();
152 }
153 
155 {
156  if (fReindex)
157  return BLOCK_SOURCE_REINDEX;
158  else if (fImporting)
159  return BLOCK_SOURCE_DISK;
160  else if (getNumConnections() > 0)
161  return BLOCK_SOURCE_NETWORK;
162 
163  return BLOCK_SOURCE_NONE;
164 }
165 
167 {
168  return QString::fromStdString(GetWarnings("statusbar"));
169 }
170 
172 {
173  return optionsModel;
174 }
175 
177 {
178  return QString::fromStdString(FormatFullVersion());
179 }
180 
182 {
183  return QString::fromStdString(CLIENT_DATE);
184 }
185 
187 {
189 }
190 
191 QString ClientModel::clientName() const
192 {
193  return QString::fromStdString(CLIENT_NAME);
194 }
195 
197 {
198  return QDateTime::fromTime_t(nClientStartupTime).toString();
199 }
200 
201 // Handlers for core signals
202 static void NotifyBlocksChanged(ClientModel *clientmodel)
203 {
204  // This notification is too frequent. Don't trigger a signal.
205  // Don't remove it, though, as it might be useful later.
206 }
207 
208 static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
209 {
210  // Too noisy: qDebug() << "NotifyNumConnectionsChanged : " + QString::number(newNumConnections);
211  QMetaObject::invokeMethod(clientmodel, "updateNumConnections", Qt::QueuedConnection,
212  Q_ARG(int, newNumConnections));
213 }
214 
215 static void NotifyAlertChanged(ClientModel *clientmodel, const uint256 &hash, ChangeType status)
216 {
217  qDebug() << "NotifyAlertChanged : " + QString::fromStdString(hash.GetHex()) + " status=" + QString::number(status);
218  QMetaObject::invokeMethod(clientmodel, "updateAlert", Qt::QueuedConnection,
219  Q_ARG(QString, QString::fromStdString(hash.GetHex())),
220  Q_ARG(int, status));
221 }
222 
224 {
225  // Connect signals to client
226  uiInterface.NotifyBlocksChanged.connect(boost::bind(NotifyBlocksChanged, this));
228  uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this, _1, _2));
229 }
230 
232 {
233  // Disconnect signals from client
234  uiInterface.NotifyBlocksChanged.disconnect(boost::bind(NotifyBlocksChanged, this));
235  uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, _1));
236  uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this, _1, _2));
237 }
void numBlocksChanged(int count)
CClientUIInterface uiInterface
Definition: util.cpp:100
void SetHex(const char *psz)
Definition: uint256.h:305
void message(const QString &title, const QString &message, unsigned int style)
Fired when a message should be reported to the user.
static uint64_t GetTotalBytesRecv()
Definition: net.cpp:1858
#define TRY_LOCK(cs, name)
Definition: sync.h:158
bool fImporting
Definition: main.cpp:52
static const int64_t nClientStartupTime
Definition: clientmodel.cpp:22
quint64 getTotalBytesRecv() const
Definition: clientmodel.cpp:68
const std::string CLIENT_NAME
int getNumConnections(unsigned int flags=CONNECTIONS_ALL) const
Return number of connections, default is in- and outbound (total)
Definition: clientmodel.cpp:42
CCriticalSection cs_main
Definition: main.cpp:43
string GetWarnings(string strFor)
Format a string that describes several potential problems detected by the core.
Definition: main.cpp:3204
OptionsModel * getOptionsModel()
QString formatClientStartupTime() const
bool fReindex
Definition: main.cpp:53
QString getStatusBarWarnings() const
Return warnings to be displayed in status bar.
std::string strStatusBar
Definition: alert.h:46
void numConnectionsChanged(int count)
CChain chainActive
The currently-connected chain of blocks.
Definition: main.cpp:48
bool isReleaseVersion() const
void alertsChanged(const QString &warnings)
QString getNetworkName() const
Return network (main, testnet3, regtest)
vector< CNode * > vNodes
Definition: net.cpp:63
bool IsNull() const
Definition: alert.cpp:93
void bytesChanged(quint64 totalBytesIn, quint64 totalBytesOut)
bool inInitialBlockDownload() const
Return true if core is doing initial block download.
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or NULL if none.
Definition: main.h:1012
#define CLIENT_VERSION_IS_RELEASE
int Height() const
Return the maximal height in the chain.
Definition: main.h:1043
bool cachedImporting
Definition: clientmodel.h:77
double getVerificationProgress() const
Definition: clientmodel.cpp:87
ChangeType
General change type (added, updated, removed).
Definition: ui_interface.h:20
bool fInbound
Definition: net.h:227
bool IsInitialBlockDownload()
Check whether we are doing an initial block download (synchronizing from disk or network) ...
Definition: main.cpp:1341
An alert is a combination of a serialized CUnsignedAlert and a signature.
Definition: alert.h:75
#define LOCK(cs)
Definition: sync.h:156
QDateTime getLastBlockDate() const
Definition: clientmodel.cpp:78
BlockSource
Definition: clientmodel.h:21
void updateAlert(const QString &hash, int status)
void unsubscribeFromCoreSignals()
QString clientName() const
void subscribeToCoreSignals()
QString formatBuildDate() const
static void NotifyAlertChanged(ClientModel *clientmodel, const uint256 &hash, ChangeType status)
static uint64_t GetTotalBytesSent()
Definition: net.cpp:1864
std::string GetHex() const
Definition: uint256.h:297
Model for Bitcoin network client.
Definition: clientmodel.h:36
double GuessVerificationProgress(CBlockIndex *pindex, bool fSigchecks)
int64_t GetTime()
Definition: util.cpp:1215
int cachedNumBlocks
Definition: clientmodel.h:75
QTimer * pollTimer
Definition: clientmodel.h:81
static void NotifyBlocksChanged(ClientModel *clientmodel)
static const int MODEL_UPDATE_DELAY
Definition: guiconstants.h:9
int getNumBlocks() const
Definition: clientmodel.cpp:56
boost::signals2::signal< void(int newNumConnections)> NotifyNumConnectionsChanged
Number of network connections changed.
Definition: ui_interface.h:87
quint64 getTotalBytesSent() const
Definition: clientmodel.cpp:73
256-bit unsigned integer
Definition: uint256.h:531
int numBlocksAtStartup
Definition: clientmodel.h:79
bool cachedReindexing
Definition: clientmodel.h:76
ClientModel(OptionsModel *optionsModel, QObject *parent=0)
Definition: clientmodel.cpp:24
boost::signals2::signal< void(const uint256 &hash, ChangeType status)> NotifyAlertChanged
New, updated or cancelled alert.
Definition: ui_interface.h:93
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:20
const CChainParams & Params()
Return the currently selected parameters.
void updateTimer()
Definition: clientmodel.cpp:93
static CAlert getAlertByHash(const uint256 &hash)
Definition: alert.cpp:158
string FormatFullVersion()
Definition: util.cpp:1325
boost::signals2::signal< void()> NotifyBlocksChanged
Block chain changed.
Definition: ui_interface.h:84
OptionsModel * optionsModel
Definition: clientmodel.h:73
int getNumBlocksAtStartup()
Definition: clientmodel.cpp:62
Information about a peer.
Definition: net.h:193
void updateNumConnections(int numConnections)
int64_t GetBlockTime() const
Definition: main.h:810
static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
QString formatFullVersion() const
CCriticalSection cs_vNodes
Definition: net.cpp:64
enum BlockSource getBlockSource() const
Return true if core is importing blocks.
const std::string CLIENT_DATE