/ src / net_processing.h
net_processing.h
  1  // Copyright (c) 2009-2010 Satoshi Nakamoto
  2  // Copyright (c) 2009-2022 The Bitcoin Core developers
  3  // Distributed under the MIT software license, see the accompanying
  4  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5  
  6  #ifndef BITCOIN_NET_PROCESSING_H
  7  #define BITCOIN_NET_PROCESSING_H
  8  
  9  #include <net.h>
 10  #include <validationinterface.h>
 11  
 12  class AddrMan;
 13  class CChainParams;
 14  class CTxMemPool;
 15  class ChainstateManager;
 16  
 17  /** Whether transaction reconciliation protocol should be enabled by default. */
 18  static constexpr bool DEFAULT_TXRECONCILIATION_ENABLE{false};
 19  /** Default for -maxorphantx, maximum number of orphan transactions kept in memory */
 20  static const uint32_t DEFAULT_MAX_ORPHAN_TRANSACTIONS{100};
 21  /** Default number of non-mempool transactions to keep around for block reconstruction. Includes
 22      orphan, replaced, and rejected transactions. */
 23  static const uint32_t DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN{100};
 24  static const bool DEFAULT_PEERBLOOMFILTERS = false;
 25  static const bool DEFAULT_PEERBLOCKFILTERS = false;
 26  /** Threshold for marking a node to be discouraged, e.g. disconnected and added to the discouragement filter. */
 27  static const int DISCOURAGEMENT_THRESHOLD{100};
 28  /** Maximum number of outstanding CMPCTBLOCK requests for the same block. */
 29  static const unsigned int MAX_CMPCTBLOCKS_INFLIGHT_PER_BLOCK = 3;
 30  
 31  struct CNodeStateStats {
 32      int nSyncHeight = -1;
 33      int nCommonHeight = -1;
 34      int m_starting_height = -1;
 35      std::chrono::microseconds m_ping_wait;
 36      std::vector<int> vHeightInFlight;
 37      bool m_relay_txs;
 38      CAmount m_fee_filter_received;
 39      uint64_t m_addr_processed = 0;
 40      uint64_t m_addr_rate_limited = 0;
 41      bool m_addr_relay_enabled{false};
 42      ServiceFlags their_services;
 43      int64_t presync_height{-1};
 44  };
 45  
 46  class PeerManager : public CValidationInterface, public NetEventsInterface
 47  {
 48  public:
 49      struct Options {
 50          //! Whether this node is running in -blocksonly mode
 51          bool ignore_incoming_txs{DEFAULT_BLOCKSONLY};
 52          //! Whether transaction reconciliation protocol is enabled
 53          bool reconcile_txs{DEFAULT_TXRECONCILIATION_ENABLE};
 54          //! Maximum number of orphan transactions kept in memory
 55          uint32_t max_orphan_txs{DEFAULT_MAX_ORPHAN_TRANSACTIONS};
 56          //! Number of non-mempool transactions to keep around for block reconstruction. Includes
 57          //! orphan, replaced, and rejected transactions.
 58          uint32_t max_extra_txs{DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN};
 59          //! Whether all P2P messages are captured to disk
 60          bool capture_messages{false};
 61          //! Whether or not the internal RNG behaves deterministically (this is
 62          //! a test-only option).
 63          bool deterministic_rng{false};
 64      };
 65  
 66      static std::unique_ptr<PeerManager> make(CConnman& connman, AddrMan& addrman,
 67                                               BanMan* banman, ChainstateManager& chainman,
 68                                               CTxMemPool& pool, Options opts);
 69      virtual ~PeerManager() { }
 70  
 71      /**
 72       * Attempt to manually fetch block from a given peer. We must already have the header.
 73       *
 74       * @param[in]  peer_id      The peer id
 75       * @param[in]  block_index  The blockindex
 76       * @returns std::nullopt if a request was successfully made, otherwise an error message
 77       */
 78      virtual std::optional<std::string> FetchBlock(NodeId peer_id, const CBlockIndex& block_index) = 0;
 79  
 80      /** Begin running background tasks, should only be called once */
 81      virtual void StartScheduledTasks(CScheduler& scheduler) = 0;
 82  
 83      /** Get statistics from node state */
 84      virtual bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const = 0;
 85  
 86      /** Whether this node ignores txs received over p2p. */
 87      virtual bool IgnoresIncomingTxs() = 0;
 88  
 89      /** Relay transaction to all peers. */
 90      virtual void RelayTransaction(const uint256& txid, const uint256& wtxid) = 0;
 91  
 92      /** Send ping message to all peers */
 93      virtual void SendPings() = 0;
 94  
 95      /** Set the height of the best block and its time (seconds since epoch). */
 96      virtual void SetBestBlock(int height, std::chrono::seconds time) = 0;
 97  
 98      /* Public for unit testing. */
 99      virtual void UnitTestMisbehaving(NodeId peer_id, int howmuch) = 0;
100  
101      /**
102       * Evict extra outbound peers. If we think our tip may be stale, connect to an extra outbound.
103       * Public for unit testing.
104       */
105      virtual void CheckForStaleTipAndEvictPeers() = 0;
106  
107      /** Process a single message from a peer. Public for fuzz testing */
108      virtual void ProcessMessage(CNode& pfrom, const std::string& msg_type, DataStream& vRecv,
109                                  const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex) = 0;
110  
111      /** This function is used for testing the stale tip eviction logic, see denialofservice_tests.cpp */
112      virtual void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) = 0;
113  
114      /**
115       * Gets the set of service flags which are "desirable" for a given peer.
116       *
117       * These are the flags which are required for a peer to support for them
118       * to be "interesting" to us, ie for us to wish to use one of our few
119       * outbound connection slots for or for us to wish to prioritize keeping
120       * their connection around.
121       *
122       * Relevant service flags may be peer- and state-specific in that the
123       * version of the peer may determine which flags are required (eg in the
124       * case of NODE_NETWORK_LIMITED where we seek out NODE_NETWORK peers
125       * unless they set NODE_NETWORK_LIMITED and we are out of IBD, in which
126       * case NODE_NETWORK_LIMITED suffices).
127       *
128       * Thus, generally, avoid calling with 'services' == NODE_NONE, unless
129       * state-specific flags must absolutely be avoided. When called with
130       * 'services' == NODE_NONE, the returned desirable service flags are
131       * guaranteed to not change dependent on state - ie they are suitable for
132       * use when describing peers which we know to be desirable, but for which
133       * we do not have a confirmed set of service flags.
134      */
135      virtual ServiceFlags GetDesirableServiceFlags(ServiceFlags services) const = 0;
136  };
137  
138  #endif // BITCOIN_NET_PROCESSING_H