1 // Copyright (c) 2011-2020 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 }
27 
28 QT_BEGIN_NAMESPACE
29 class QTimer;
30 QT_END_NAMESPACE
31 
32 enum class BlockSource {
33     NONE,
34     REINDEX,
35     DISK,
36     NETWORK
37 };
38 
39 enum NumConnections {
40     CONNECTIONS_NONE = 0,
41     CONNECTIONS_IN   = (1U << 0),
42     CONNECTIONS_OUT  = (1U << 1),
43     CONNECTIONS_ALL  = (CONNECTIONS_IN | CONNECTIONS_OUT),
44 };
45 
46 /** Model for Bitcoin network client. */
47 class ClientModel : public QObject
48 {
49     Q_OBJECT
50 
51 public:
52     explicit ClientModel(interfaces::Node& node, OptionsModel *optionsModel, QObject *parent = nullptr);
53     ~ClientModel();
54 
node()55     interfaces::Node& node() const { return m_node; }
56     OptionsModel *getOptionsModel();
57     PeerTableModel *getPeerTableModel();
58     PeerTableSortProxy* peerTableSortProxy();
59     BanTableModel *getBanTableModel();
60 
61     //! Return number of connections, default is in- and outbound (total)
62     int getNumConnections(unsigned int flags = CONNECTIONS_ALL) const;
63     int getNumBlocks() const;
64     uint256 getBestBlockHash();
65     int getHeaderTipHeight() const;
66     int64_t getHeaderTipTime() const;
67 
68     //! Returns enum BlockSource of the current importing/syncing state
69     enum BlockSource getBlockSource() const;
70     //! Return warnings to be displayed in status bar
71     QString getStatusBarWarnings() const;
72 
73     QString formatFullVersion() const;
74     QString formatSubVersion() const;
75     bool isReleaseVersion() const;
76     QString formatClientStartupTime() const;
77     QString dataDir() const;
78     QString blocksDir() const;
79 
80     bool getProxyInfo(std::string& ip_port) const;
81 
82     // caches for the best header: hash, number of blocks and block time
83     mutable std::atomic<int> cachedBestHeaderHeight;
84     mutable std::atomic<int64_t> cachedBestHeaderTime;
85     mutable std::atomic<int> m_cached_num_blocks{-1};
86 
87     Mutex m_cached_tip_mutex;
GUARDED_BY(m_cached_tip_mutex)88     uint256 m_cached_tip_blocks GUARDED_BY(m_cached_tip_mutex){};
89 
90 private:
91     interfaces::Node& m_node;
92     std::unique_ptr<interfaces::Handler> m_handler_show_progress;
93     std::unique_ptr<interfaces::Handler> m_handler_notify_num_connections_changed;
94     std::unique_ptr<interfaces::Handler> m_handler_notify_network_active_changed;
95     std::unique_ptr<interfaces::Handler> m_handler_notify_alert_changed;
96     std::unique_ptr<interfaces::Handler> m_handler_banned_list_changed;
97     std::unique_ptr<interfaces::Handler> m_handler_notify_block_tip;
98     std::unique_ptr<interfaces::Handler> m_handler_notify_header_tip;
99     OptionsModel *optionsModel;
100     PeerTableModel *peerTableModel;
101     PeerTableSortProxy* m_peer_table_sort_proxy{nullptr};
102     BanTableModel *banTableModel;
103 
104     //! A thread to interact with m_node asynchronously
105     QThread* const m_thread;
106 
107     void subscribeToCoreSignals();
108     void unsubscribeFromCoreSignals();
109 
110 Q_SIGNALS:
111     void numConnectionsChanged(int count);
112     void numBlocksChanged(int count, const QDateTime& blockDate, double nVerificationProgress, bool header, SynchronizationState sync_state);
113     void mempoolSizeChanged(long count, size_t mempoolSizeInBytes);
114     void networkActiveChanged(bool networkActive);
115     void alertsChanged(const QString &warnings);
116     void bytesChanged(quint64 totalBytesIn, quint64 totalBytesOut);
117 
118     //! Fired when a message should be reported to the user
119     void message(const QString &title, const QString &message, unsigned int style);
120 
121     // Show progress dialog e.g. for verifychain
122     void showProgress(const QString &title, int nProgress);
123 
124 public Q_SLOTS:
125     void updateNumConnections(int numConnections);
126     void updateNetworkActive(bool networkActive);
127     void updateAlert();
128     void updateBanlist();
129 };
130 
131 #endif // BITCOIN_QT_CLIENTMODEL_H
132