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