Bitcoin Core  28.1.0
P2P Digital Currency
clientmodel.h
Go to the documentation of this file.
1 // Copyright (c) 2011-2022 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 
5 #ifndef BITCOIN_QT_CLIENTMODEL_H
6 #define BITCOIN_QT_CLIENTMODEL_H
7 
8 #include <QObject>
9 #include <QDateTime>
10 
11 #include <atomic>
12 #include <memory>
13 #include <sync.h>
14 #include <uint256.h>
15 
16 #include <netaddress.h>
17 
18 class BanTableModel;
19 class CBlockIndex;
20 class OptionsModel;
21 class PeerTableModel;
22 class PeerTableSortProxy;
23 enum class SynchronizationState;
24 struct LocalServiceInfo;
25 
26 namespace interfaces {
27 class Handler;
28 class Node;
29 struct BlockTip;
30 }
31 
32 QT_BEGIN_NAMESPACE
33 class QTimer;
34 QT_END_NAMESPACE
35 
36 enum class BlockSource {
37  NONE,
38  DISK,
39  NETWORK,
40 };
41 
42 enum class SyncType {
46 };
47 
50  CONNECTIONS_IN = (1U << 0),
51  CONNECTIONS_OUT = (1U << 1),
53 };
54 
56 class ClientModel : public QObject
57 {
58  Q_OBJECT
59 
60 public:
61  explicit ClientModel(interfaces::Node& node, OptionsModel *optionsModel, QObject *parent = nullptr);
62  ~ClientModel();
63 
64  void stop();
65 
66  interfaces::Node& node() const { return m_node; }
71 
73  int getNumConnections(unsigned int flags = CONNECTIONS_ALL) const;
74  std::map<CNetAddr, LocalServiceInfo> getNetLocalAddresses() const;
75  int getNumBlocks() const;
77  int getHeaderTipHeight() const;
78  int64_t getHeaderTipTime() const;
79 
83  QString getStatusBarWarnings() const;
84 
85  QString formatFullVersion() const;
86  QString formatSubVersion() const;
87  bool isReleaseVersion() const;
88  QString formatClientStartupTime() const;
89  QString dataDir() const;
90  QString blocksDir() const;
91 
92  bool getProxyInfo(std::string& ip_port) const;
93 
94  // caches for the best header: hash, number of blocks and block time
95  mutable std::atomic<int> cachedBestHeaderHeight;
96  mutable std::atomic<int64_t> cachedBestHeaderTime;
97  mutable std::atomic<int> m_cached_num_blocks{-1};
98 
100  uint256 m_cached_tip_blocks GUARDED_BY(m_cached_tip_mutex){};
101 
102 private:
104  std::vector<std::unique_ptr<interfaces::Handler>> m_event_handlers;
109 
111  QThread* const m_thread;
112 
113  void TipChanged(SynchronizationState sync_state, interfaces::BlockTip tip, double verification_progress, SyncType synctype) EXCLUSIVE_LOCKS_REQUIRED(!m_cached_tip_mutex);
114  void subscribeToCoreSignals();
116 
117 Q_SIGNALS:
118  void numConnectionsChanged(int count);
119  void numBlocksChanged(int count, const QDateTime& blockDate, double nVerificationProgress, SyncType header, SynchronizationState sync_state);
120  void mempoolSizeChanged(long count, size_t mempoolSizeInBytes, size_t mempoolMaxSizeInBytes);
121  void networkActiveChanged(bool networkActive);
122  void alertsChanged(const QString &warnings);
123  void bytesChanged(quint64 totalBytesIn, quint64 totalBytesOut);
124 
126  void message(const QString &title, const QString &message, unsigned int style);
127 
128  // Show progress dialog e.g. for verifychain
129  void showProgress(const QString &title, int nProgress);
130 };
131 
132 #endif // BITCOIN_QT_CLIENTMODEL_H
QString formatClientStartupTime() const
void message(const QString &title, const QString &message, unsigned int style)
Fired when a message should be reported to the user.
PeerTableModel * peerTableModel
Definition: clientmodel.h:106
QString formatSubVersion() const
Block tip (could be a header or not, depends on the subscribed signal).
Definition: node.h:282
interfaces::Node & m_node
Definition: clientmodel.h:100
SynchronizationState
Current sync state passed to tip changed callbacks.
Definition: validation.h:82
std::vector< std::unique_ptr< interfaces::Handler > > m_event_handlers
Definition: clientmodel.h:104
bool isReleaseVersion() const
std::atomic< int64_t > cachedBestHeaderTime
Definition: clientmodel.h:96
QString blocksDir() const
QThread *const m_thread
A thread to interact with m_node asynchronously.
Definition: clientmodel.h:111
void networkActiveChanged(bool networkActive)
BanTableModel * banTableModel
Definition: clientmodel.h:108
ClientModel(interfaces::Node &node, OptionsModel *optionsModel, QObject *parent=nullptr)
Definition: clientmodel.cpp:36
OptionsModel * getOptionsModel()
Mutex m_cached_tip_mutex
Definition: clientmodel.h:99
std::atomic< int > cachedBestHeaderHeight
Definition: clientmodel.h:95
PeerTableModel * getPeerTableModel()
void numConnectionsChanged(int count)
QString getStatusBarWarnings() const
Return warnings to be displayed in status bar.
void alertsChanged(const QString &warnings)
void bytesChanged(quint64 totalBytesIn, quint64 totalBytesOut)
uint256 getBestBlockHash() EXCLUSIVE_LOCKS_REQUIRED(!m_cached_tip_mutex)
BlockSource getBlockSource() const
Returns the block source of the current importing/syncing state.
std::atomic< int > m_cached_num_blocks
Definition: clientmodel.h:97
void mempoolSizeChanged(long count, size_t mempoolSizeInBytes, size_t mempoolMaxSizeInBytes)
int getNumConnections(unsigned int flags=CONNECTIONS_ALL) const
Return number of connections, default is in- and outbound (total)
Definition: clientmodel.cpp:84
std::map< CNetAddr, LocalServiceInfo > getNetLocalAddresses() const
NumConnections
Definition: clientmodel.h:48
PeerTableSortProxy * m_peer_table_sort_proxy
Definition: clientmodel.h:107
Qt model providing information about connected peers, similar to the "getpeerinfo" RPC call...
BlockSource
Definition: clientmodel.h:36
void unsubscribeFromCoreSignals()
void numBlocksChanged(int count, const QDateTime &blockDate, double nVerificationProgress, SyncType header, SynchronizationState sync_state)
BanTableModel * getBanTableModel()
interfaces::Node & node() const
Definition: clientmodel.h:66
void subscribeToCoreSignals()
void TipChanged(SynchronizationState sync_state, interfaces::BlockTip tip, double verification_progress, SyncType synctype) EXCLUSIVE_LOCKS_REQUIRED(!m_cached_tip_mutex)
Model for Bitcoin network client.
Definition: clientmodel.h:56
SyncType
Definition: clientmodel.h:42
Definition: messages.h:20
int flags
Definition: bitcoin-tx.cpp:533
256-bit opaque blob.
Definition: uint256.h:178
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:42
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:140
Qt model providing information about banned peers, similar to the "getpeerinfo" RPC call...
Definition: bantablemodel.h:43
static int count
OptionsModel * optionsModel
Definition: clientmodel.h:105
QString dataDir() const
int getNumBlocks() const
uint256 m_cached_tip_blocks GUARDED_BY(m_cached_tip_mutex)
Definition: clientmodel.h:100
PeerTableSortProxy * peerTableSortProxy()
int getHeaderTipHeight() const
Definition: clientmodel.cpp:98
int64_t getHeaderTipTime() const
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:69
void showProgress(const QString &title, int nProgress)
QString formatFullVersion() const
bool getProxyInfo(std::string &ip_port) const