/ src / net_processing.h
net_processing.h
  1  // Copyright (c) 2009-2010 Satoshi Nakamoto
  2  // Copyright (c) 2009-present 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 <consensus/amount.h>
 10  #include <net.h>
 11  #include <node/txorphanage.h>
 12  #include <private_broadcast.h>
 13  #include <protocol.h>
 14  #include <uint256.h>
 15  #include <util/expected.h>
 16  #include <validationinterface.h>
 17  
 18  #include <atomic>
 19  #include <chrono>
 20  #include <cstdint>
 21  #include <memory>
 22  #include <optional>
 23  #include <string>
 24  #include <vector>
 25  
 26  class AddrMan;
 27  class CTxMemPool;
 28  class ChainstateManager;
 29  class BanMan;
 30  class CBlockIndex;
 31  class CScheduler;
 32  class DataStream;
 33  class uint256;
 34  
 35  namespace node {
 36  class Warnings;
 37  } // namespace node
 38  
 39  /** Whether transaction reconciliation protocol should be enabled by default. */
 40  static constexpr bool DEFAULT_TXRECONCILIATION_ENABLE{false};
 41  /** Default number of non-mempool transactions to keep around for block reconstruction. Includes
 42      orphan, replaced, and rejected transactions. */
 43  static const uint32_t DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN{100};
 44  static const bool DEFAULT_PEERBLOOMFILTERS = false;
 45  static const bool DEFAULT_PEERBLOCKFILTERS = false;
 46  /** Maximum number of outstanding CMPCTBLOCK requests for the same block. */
 47  static const unsigned int MAX_CMPCTBLOCKS_INFLIGHT_PER_BLOCK = 3;
 48  /** Number of headers sent in one getheaders result. We rely on the assumption that if a peer sends
 49   *  less than this number, we reached its tip. Changing this value is a protocol upgrade. */
 50  static const unsigned int MAX_HEADERS_RESULTS = 2000;
 51  
 52  struct CNodeStateStats {
 53      int nSyncHeight = -1;
 54      int nCommonHeight = -1;
 55      NodeClock::duration m_ping_wait;
 56      std::vector<int> vHeightInFlight;
 57      bool m_relay_txs;
 58      int m_inv_to_send = 0;
 59      uint64_t m_last_inv_seq{0};
 60      CAmount m_fee_filter_received;
 61      uint64_t m_addr_processed = 0;
 62      uint64_t m_addr_rate_limited = 0;
 63      bool m_addr_relay_enabled{false};
 64      ServiceFlags their_services;
 65      int64_t presync_height{-1};
 66      std::chrono::seconds time_offset{0};
 67  };
 68  
 69  struct PeerManagerInfo {
 70      std::chrono::seconds median_outbound_time_offset{0s};
 71      bool ignores_incoming_txs{false};
 72  };
 73  
 74  class PeerManager : public CValidationInterface, public NetEventsInterface
 75  {
 76  public:
 77      struct Options {
 78          //! Whether this node is running in -blocksonly mode
 79          bool ignore_incoming_txs{DEFAULT_BLOCKSONLY};
 80          //! Whether transaction reconciliation protocol is enabled
 81          bool reconcile_txs{DEFAULT_TXRECONCILIATION_ENABLE};
 82          //! Number of non-mempool transactions to keep around for block reconstruction. Includes
 83          //! orphan, replaced, and rejected transactions.
 84          uint32_t max_extra_txs{DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN};
 85          //! Whether all P2P messages are captured to disk
 86          bool capture_messages{false};
 87          //! Whether or not the internal RNG behaves deterministically (this is
 88          //! a test-only option).
 89          bool deterministic_rng{false};
 90          //! Number of headers sent in one getheaders message result (this is
 91          //! a test-only option).
 92          uint32_t max_headers_result{MAX_HEADERS_RESULTS};
 93          //! Whether private broadcast is used for sending transactions.
 94          bool private_broadcast{DEFAULT_PRIVATE_BROADCAST};
 95      };
 96  
 97      static std::unique_ptr<PeerManager> make(CConnman& connman, AddrMan& addrman,
 98                                               BanMan* banman, ChainstateManager& chainman,
 99                                               CTxMemPool& pool, node::Warnings& warnings, Options opts);
100      virtual ~PeerManager() = default;
101  
102      /**
103       * Attempt to manually fetch block from a given peer. We must already have the header.
104       *
105       * @param[in]  peer_id      The peer id
106       * @param[in]  block_index  The blockindex
107       */
108      virtual util::Expected<void, std::string> FetchBlock(NodeId peer_id, const CBlockIndex& block_index) = 0;
109  
110      /** Begin running background tasks, should only be called once */
111      virtual void StartScheduledTasks(CScheduler& scheduler) = 0;
112  
113      /** Get statistics from node state */
114      virtual bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const = 0;
115  
116      virtual std::vector<node::TxOrphanage::OrphanInfo> GetOrphanTransactions() = 0;
117  
118      /** Get peer manager info. */
119      virtual PeerManagerInfo GetInfo() const = 0;
120  
121      /** Get info about transactions currently being privately broadcast. */
122      virtual std::vector<PrivateBroadcast::TxBroadcastInfo> GetPrivateBroadcastInfo() const = 0;
123  
124      /**
125       * Abort private broadcast attempts for transactions currently being privately broadcast.
126       *
127       * @param[in] id A transaction identifier. It will be matched against both txid and wtxid for
128       *               all transactions in the private broadcast queue.
129       *
130       * @return Transactions removed from the private broadcast queue. If the provided id matches a
131       *         txid that corresponds to multiple transactions with different wtxids, multiple
132       *         transactions may be returned.
133       */
134      virtual std::vector<CTransactionRef> AbortPrivateBroadcast(const uint256& id) = 0;
135  
136      /**
137       * Initiate a transaction broadcast to eligible peers.
138       * Queue the witness transaction id to `Peer::TxRelay::m_tx_inventory_to_send`
139       * for each peer. Later, depending on `Peer::TxRelay::m_next_inv_send_time` and if
140       * the transaction is in the mempool, an `INV` about it may be sent to the peer.
141       */
142      virtual void InitiateTxBroadcastToAll(const Txid& txid, const Wtxid& wtxid) = 0;
143  
144      /**
145       * Initiate a private transaction broadcast. This is done
146       * asynchronously via short-lived connections to peers on privacy networks.
147       */
148      virtual void InitiateTxBroadcastPrivate(const CTransactionRef& tx) = 0;
149  
150      /** Send ping message to all peers */
151      virtual void SendPings() = 0;
152  
153      /** Set the height of the best block and its time (seconds since epoch). */
154      virtual void SetBestBlock(int height, std::chrono::seconds time) = 0;
155  
156      /* Public for unit testing. */
157      virtual void UnitTestMisbehaving(NodeId peer_id) = 0;
158  
159      /**
160       * Evict extra outbound peers. If we think our tip may be stale, connect to an extra outbound.
161       * Public for unit testing.
162       */
163      virtual void CheckForStaleTipAndEvictPeers() = 0;
164  
165      /** This function is used for testing the stale tip eviction logic, see denialofservice_tests.cpp */
166      virtual void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) = 0;
167  
168      /**
169       * Gets the set of service flags which are "desirable" for a given peer.
170       *
171       * These are the flags which are required for a peer to support for them
172       * to be "interesting" to us, ie for us to wish to use one of our few
173       * outbound connection slots for or for us to wish to prioritize keeping
174       * their connection around.
175       *
176       * Relevant service flags may be peer- and state-specific in that the
177       * version of the peer may determine which flags are required (eg in the
178       * case of NODE_NETWORK_LIMITED where we seek out NODE_NETWORK peers
179       * unless they set NODE_NETWORK_LIMITED and we are out of IBD, in which
180       * case NODE_NETWORK_LIMITED suffices).
181       *
182       * Thus, generally, avoid calling with 'services' == NODE_NONE, unless
183       * state-specific flags must absolutely be avoided. When called with
184       * 'services' == NODE_NONE, the returned desirable service flags are
185       * guaranteed to not change dependent on state - ie they are suitable for
186       * use when describing peers which we know to be desirable, but for which
187       * we do not have a confirmed set of service flags.
188      */
189      virtual ServiceFlags GetDesirableServiceFlags(ServiceFlags services) const = 0;
190  };
191  
192  #endif // BITCOIN_NET_PROCESSING_H