clientmodel.h raw

   1  // Copyright (c) 2011-2022 The Limenka 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 LIMENKA_QT_CLIENTMODEL_H
   6  #define LIMENKA_QT_CLIENTMODEL_H
   7  
   8  #include <QObject>
   9  #include <QDateTime>
  10  
  11  #include <atomic>
  12  #include <memory>
  13  #include <stats/stats.h>
  14  #include <sync.h>
  15  #include <uint256.h>
  16  
  17  #include <netaddress.h>
  18  
  19  class BanTableModel;
  20  class CBlockIndex;
  21  class OptionsModel;
  22  class PeerTableModel;
  23  class PeerTableSortProxy;
  24  class PlatformStyle;
  25  enum class SynchronizationState;
  26  struct LocalServiceInfo;
  27  
  28  namespace interfaces {
  29  class Handler;
  30  class Node;
  31  struct BlockTip;
  32  }
  33  
  34  QT_BEGIN_NAMESPACE
  35  class QTimer;
  36  QT_END_NAMESPACE
  37  
  38  enum class BlockSource {
  39      NONE,
  40      DISK,
  41      NETWORK,
  42  };
  43  
  44  enum class SyncType {
  45      HEADER_PRESYNC,
  46      HEADER_SYNC,
  47      BLOCK_SYNC
  48  };
  49  
  50  enum NumConnections {
  51      CONNECTIONS_NONE = 0,
  52      CONNECTIONS_IN   = (1U << 0),
  53      CONNECTIONS_OUT  = (1U << 1),
  54      CONNECTIONS_ALL  = (CONNECTIONS_IN | CONNECTIONS_OUT),
  55  };
  56  
  57  /** Model for Limenka network client. */
  58  class ClientModel : public QObject
  59  {
  60      Q_OBJECT
  61  
  62  public:
  63      explicit ClientModel(interfaces::Node& node, OptionsModel *optionsModel, const PlatformStyle&, QObject *parent = nullptr);
  64      ~ClientModel();
  65  
  66      void stop();
  67  
  68      interfaces::Node& node() const { return m_node; }
  69      OptionsModel *getOptionsModel();
  70      PeerTableModel *getPeerTableModel();
  71      PeerTableSortProxy* peerTableSortProxy();
  72      BanTableModel *getBanTableModel();
  73  
  74      //! Return number of connections, default is in- and outbound (total)
  75      int getNumConnections(unsigned int flags = CONNECTIONS_ALL) const;
  76      std::map<CNetAddr, LocalServiceInfo> getNetLocalAddresses() const;
  77      int getNumBlocks() const;
  78      uint256 getBestBlockHash() EXCLUSIVE_LOCKS_REQUIRED(!m_cached_tip_mutex);
  79      int getHeaderTipHeight() const;
  80      int64_t getHeaderTipTime() const;
  81  
  82      //! Returns the block source of the current importing/syncing state
  83      BlockSource getBlockSource() const;
  84      //! Return warnings to be displayed in status bar
  85      QString getStatusBarWarnings() const;
  86  
  87      QString formatFullVersion() const;
  88      QString formatSubVersion() const;
  89      bool isReleaseVersion() const;
  90      QString formatClientStartupTime() const;
  91      QString dataDir() const;
  92      QString blocksDir() const;
  93  
  94      bool getProxyInfo(std::string& ip_port) const;
  95      bool getTorInfo(QString& out_onion) const;
  96  
  97      // caches for the best header: hash, number of blocks and block time
  98      mutable std::atomic<int> cachedBestHeaderHeight;
  99      mutable std::atomic<int64_t> cachedBestHeaderTime;
 100      mutable std::atomic<int> m_cached_num_blocks{-1};
 101  
 102      Mutex m_cached_tip_mutex;
 103      uint256 m_cached_tip_blocks GUARDED_BY(m_cached_tip_mutex){};
 104  
 105      mempoolSamples_t getMempoolStatsInRange(QDateTime &from, QDateTime &to);
 106  
 107  private:
 108      interfaces::Node& m_node;
 109      std::vector<std::unique_ptr<interfaces::Handler>> m_event_handlers;
 110      boost::signals2::scoped_connection m_connection_mempool_stats_did_change;
 111      OptionsModel *optionsModel;
 112      PeerTableModel* peerTableModel{nullptr};
 113      PeerTableSortProxy* m_peer_table_sort_proxy{nullptr};
 114      BanTableModel* banTableModel{nullptr};
 115  
 116      //! A thread to interact with m_node asynchronously
 117      QThread* const m_thread;
 118  
 119      void TipChanged(SynchronizationState sync_state, interfaces::BlockTip tip, double verification_progress, SyncType synctype) EXCLUSIVE_LOCKS_REQUIRED(!m_cached_tip_mutex);
 120      void subscribeToCoreSignals();
 121      void unsubscribeFromCoreSignals();
 122  
 123  Q_SIGNALS:
 124      void numConnectionsChanged(int count);
 125      void numBlocksChanged(int count, const QDateTime& blockDate, double nVerificationProgress, SyncType header, SynchronizationState sync_state);
 126      void mempoolSizeChanged(long count, size_t mempoolSizeInBytes, size_t mempoolMaxSizeInBytes);
 127      void networkActiveChanged(bool networkActive);
 128      void networkLocalChanged();
 129      void alertsChanged(const QString &warnings);
 130      void bytesChanged(quint64 totalBytesIn, quint64 totalBytesOut);
 131  
 132      //! Fired when a message should be reported to the user
 133      void message(const QString &title, const QString &message, unsigned int style);
 134  
 135      // Show progress dialog e.g. for verifychain
 136      void showProgress(const QString &title, int nProgress);
 137  
 138      void mempoolStatsDidUpdate();
 139  
 140  public Q_SLOTS:
 141      /* stats stack */
 142      void updateMempoolStats();
 143  };
 144  
 145  #endif // LIMENKA_QT_CLIENTMODEL_H
 146