Bitcoin Core  24.1.0
P2P Digital Currency
clientmodel.h
Go to the documentation of this file.
1 // Copyright (c) 2011-2021 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 class BanTableModel;
17 class CBlockIndex;
18 class OptionsModel;
19 class PeerTableModel;
20 class PeerTableSortProxy;
21 enum class SynchronizationState;
22 
23 namespace interfaces {
24 class Handler;
25 class Node;
26 struct BlockTip;
27 }
28 
29 QT_BEGIN_NAMESPACE
30 class QTimer;
31 QT_END_NAMESPACE
32 
33 enum class BlockSource {
34  NONE,
35  REINDEX,
36  DISK,
37  NETWORK
38 };
39 
40 enum class SyncType {
44 };
45 
48  CONNECTIONS_IN = (1U << 0),
49  CONNECTIONS_OUT = (1U << 1),
51 };
52 
54 class ClientModel : public QObject
55 {
56  Q_OBJECT
57 
58 public:
59  explicit ClientModel(interfaces::Node& node, OptionsModel *optionsModel, QObject *parent = nullptr);
60  ~ClientModel();
61 
62  interfaces::Node& node() const { return m_node; }
67 
69  int getNumConnections(unsigned int flags = CONNECTIONS_ALL) const;
70  int getNumBlocks() const;
72  int getHeaderTipHeight() const;
73  int64_t getHeaderTipTime() const;
74 
76  enum BlockSource getBlockSource() const;
78  QString getStatusBarWarnings() const;
79 
80  QString formatFullVersion() const;
81  QString formatSubVersion() const;
82  bool isReleaseVersion() const;
83  QString formatClientStartupTime() const;
84  QString dataDir() const;
85  QString blocksDir() const;
86 
87  bool getProxyInfo(std::string& ip_port) const;
88 
89  // caches for the best header: hash, number of blocks and block time
90  mutable std::atomic<int> cachedBestHeaderHeight;
91  mutable std::atomic<int64_t> cachedBestHeaderTime;
92  mutable std::atomic<int> m_cached_num_blocks{-1};
93 
95  uint256 m_cached_tip_blocks GUARDED_BY(m_cached_tip_mutex){};
96 
97 private:
99  std::unique_ptr<interfaces::Handler> m_handler_show_progress;
100  std::unique_ptr<interfaces::Handler> m_handler_notify_num_connections_changed;
101  std::unique_ptr<interfaces::Handler> m_handler_notify_network_active_changed;
102  std::unique_ptr<interfaces::Handler> m_handler_notify_alert_changed;
103  std::unique_ptr<interfaces::Handler> m_handler_banned_list_changed;
104  std::unique_ptr<interfaces::Handler> m_handler_notify_block_tip;
105  std::unique_ptr<interfaces::Handler> m_handler_notify_header_tip;
110 
112  QThread* const m_thread;
113 
114  void TipChanged(SynchronizationState sync_state, interfaces::BlockTip tip, double verification_progress, SyncType synctype) EXCLUSIVE_LOCKS_REQUIRED(!m_cached_tip_mutex);
115  void subscribeToCoreSignals();
117 
118 Q_SIGNALS:
119  void numConnectionsChanged(int count);
120  void numBlocksChanged(int count, const QDateTime& blockDate, double nVerificationProgress, SyncType header, SynchronizationState sync_state);
121  void mempoolSizeChanged(long count, size_t mempoolSizeInBytes);
122  void networkActiveChanged(bool networkActive);
123  void alertsChanged(const QString &warnings);
124  void bytesChanged(quint64 totalBytesIn, quint64 totalBytesOut);
125 
127  void message(const QString &title, const QString &message, unsigned int style);
128 
129  // Show progress dialog e.g. for verifychain
130  void showProgress(const QString &title, int nProgress);
131 };
132 
133 #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:107
QString formatSubVersion() const
Block tip (could be a header or not, depends on the subscribed signal).
Definition: node.h:276
interfaces::Node & m_node
Definition: clientmodel.h:95
SynchronizationState
Current sync state passed to tip changed callbacks.
Definition: validation.h:88
bool isReleaseVersion() const
std::atomic< int64_t > cachedBestHeaderTime
Definition: clientmodel.h:91
QString blocksDir() const
QThread *const m_thread
A thread to interact with m_node asynchronously.
Definition: clientmodel.h:112
void networkActiveChanged(bool networkActive)
BanTableModel * banTableModel
Definition: clientmodel.h:109
ClientModel(interfaces::Node &node, OptionsModel *optionsModel, QObject *parent=nullptr)
Definition: clientmodel.cpp:33
std::unique_ptr< interfaces::Handler > m_handler_show_progress
Definition: clientmodel.h:99
OptionsModel * getOptionsModel()
Mutex m_cached_tip_mutex
Definition: clientmodel.h:94
std::atomic< int > cachedBestHeaderHeight
Definition: clientmodel.h:90
PeerTableModel * getPeerTableModel()
void numConnectionsChanged(int count)
QString getStatusBarWarnings() const
Return warnings to be displayed in status bar.
void alertsChanged(const QString &warnings)
std::unique_ptr< interfaces::Handler > m_handler_notify_block_tip
Definition: clientmodel.h:104
void bytesChanged(quint64 totalBytesIn, quint64 totalBytesOut)
uint256 getBestBlockHash() EXCLUSIVE_LOCKS_REQUIRED(!m_cached_tip_mutex)
std::atomic< int > m_cached_num_blocks
Definition: clientmodel.h:92
int getNumConnections(unsigned int flags=CONNECTIONS_ALL) const
Return number of connections, default is in- and outbound (total)
Definition: clientmodel.cpp:78
enum BlockSource getBlockSource() const
Returns enum BlockSource of the current importing/syncing state.
NumConnections
Definition: clientmodel.h:46
PeerTableSortProxy * m_peer_table_sort_proxy
Definition: clientmodel.h:108
Qt model providing information about connected peers, similar to the "getpeerinfo" RPC call...
BlockSource
Definition: clientmodel.h:33
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:62
void subscribeToCoreSignals()
std::unique_ptr< interfaces::Handler > m_handler_banned_list_changed
Definition: clientmodel.h:103
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:54
std::unique_ptr< interfaces::Handler > m_handler_notify_header_tip
Definition: clientmodel.h:105
SyncType
Definition: clientmodel.h:40
Definition: init.h:25
int flags
Definition: bitcoin-tx.cpp:525
256-bit opaque blob.
Definition: uint256.h:119
std::unique_ptr< interfaces::Handler > m_handler_notify_num_connections_changed
Definition: clientmodel.h:100
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:40
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:151
Qt model providing information about connected peers, similar to the "getpeerinfo" RPC call...
Definition: bantablemodel.h:43
static int count
Definition: tests.c:33
OptionsModel * optionsModel
Definition: clientmodel.h:106
void mempoolSizeChanged(long count, size_t mempoolSizeInBytes)
QString dataDir() const
std::unique_ptr< interfaces::Handler > m_handler_notify_network_active_changed
Definition: clientmodel.h:101
int getNumBlocks() const
uint256 m_cached_tip_blocks GUARDED_BY(m_cached_tip_mutex)
Definition: clientmodel.h:95
PeerTableSortProxy * peerTableSortProxy()
int getHeaderTipHeight() const
Definition: clientmodel.cpp:92
std::unique_ptr< interfaces::Handler > m_handler_notify_alert_changed
Definition: clientmodel.h:102
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