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 <protocol.h> 13 #include <threadsafety.h> 14 #include <validationinterface.h> 15 16 #include <atomic> 17 #include <chrono> 18 #include <cstdint> 19 #include <memory> 20 #include <optional> 21 #include <string> 22 #include <vector> 23 24 class AddrMan; 25 class CTxMemPool; 26 class ChainstateManager; 27 class BanMan; 28 class CBlockIndex; 29 class CScheduler; 30 class DataStream; 31 class uint256; 32 33 namespace node { 34 class Warnings; 35 } // namespace node 36 37 /** Whether transaction reconciliation protocol should be enabled by default. */ 38 static constexpr bool DEFAULT_TXRECONCILIATION_ENABLE{false}; 39 /** Default number of non-mempool transactions to keep around for block reconstruction. Includes 40 orphan, replaced, and rejected transactions. */ 41 static const uint32_t DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN{100}; 42 static const bool DEFAULT_PEERBLOOMFILTERS = false; 43 static const bool DEFAULT_PEERBLOCKFILTERS = false; 44 /** Maximum number of outstanding CMPCTBLOCK requests for the same block. */ 45 static const unsigned int MAX_CMPCTBLOCKS_INFLIGHT_PER_BLOCK = 3; 46 /** Number of headers sent in one getheaders result. We rely on the assumption that if a peer sends 47 * less than this number, we reached its tip. Changing this value is a protocol upgrade. */ 48 static const unsigned int MAX_HEADERS_RESULTS = 2000; 49 50 struct CNodeStateStats { 51 int nSyncHeight = -1; 52 int nCommonHeight = -1; 53 int m_starting_height = -1; 54 std::chrono::microseconds m_ping_wait; 55 std::vector<int> vHeightInFlight; 56 bool m_relay_txs; 57 int m_inv_to_send = 0; 58 uint64_t m_last_inv_seq{0}; 59 CAmount m_fee_filter_received; 60 uint64_t m_addr_processed = 0; 61 uint64_t m_addr_rate_limited = 0; 62 bool m_addr_relay_enabled{false}; 63 ServiceFlags their_services; 64 int64_t presync_height{-1}; 65 std::chrono::seconds time_offset{0}; 66 }; 67 68 struct PeerManagerInfo { 69 std::chrono::seconds median_outbound_time_offset{0s}; 70 bool ignores_incoming_txs{false}; 71 }; 72 73 class PeerManager : public CValidationInterface, public NetEventsInterface 74 { 75 public: 76 struct Options { 77 //! Whether this node is running in -blocksonly mode 78 bool ignore_incoming_txs{DEFAULT_BLOCKSONLY}; 79 //! Whether transaction reconciliation protocol is enabled 80 bool reconcile_txs{DEFAULT_TXRECONCILIATION_ENABLE}; 81 //! Number of non-mempool transactions to keep around for block reconstruction. Includes 82 //! orphan, replaced, and rejected transactions. 83 uint32_t max_extra_txs{DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN}; 84 //! Whether all P2P messages are captured to disk 85 bool capture_messages{false}; 86 //! Whether or not the internal RNG behaves deterministically (this is 87 //! a test-only option). 88 bool deterministic_rng{false}; 89 //! Number of headers sent in one getheaders message result (this is 90 //! a test-only option). 91 uint32_t max_headers_result{MAX_HEADERS_RESULTS}; 92 }; 93 94 static std::unique_ptr<PeerManager> make(CConnman& connman, AddrMan& addrman, 95 BanMan* banman, ChainstateManager& chainman, 96 CTxMemPool& pool, node::Warnings& warnings, Options opts); 97 virtual ~PeerManager() = default; 98 99 /** 100 * Attempt to manually fetch block from a given peer. We must already have the header. 101 * 102 * @param[in] peer_id The peer id 103 * @param[in] block_index The blockindex 104 * @returns std::nullopt if a request was successfully made, otherwise an error message 105 */ 106 virtual std::optional<std::string> FetchBlock(NodeId peer_id, const CBlockIndex& block_index) = 0; 107 108 /** Begin running background tasks, should only be called once */ 109 virtual void StartScheduledTasks(CScheduler& scheduler) = 0; 110 111 /** Get statistics from node state */ 112 virtual bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const = 0; 113 114 virtual std::vector<node::TxOrphanage::OrphanInfo> GetOrphanTransactions() = 0; 115 116 /** Get peer manager info. */ 117 virtual PeerManagerInfo GetInfo() const = 0; 118 119 /** Relay transaction to all peers. */ 120 virtual void RelayTransaction(const Txid& txid, const Wtxid& wtxid) = 0; 121 122 /** Send ping message to all peers */ 123 virtual void SendPings() = 0; 124 125 /** Set the height of the best block and its time (seconds since epoch). */ 126 virtual void SetBestBlock(int height, std::chrono::seconds time) = 0; 127 128 /* Public for unit testing. */ 129 virtual void UnitTestMisbehaving(NodeId peer_id) = 0; 130 131 /** 132 * Evict extra outbound peers. If we think our tip may be stale, connect to an extra outbound. 133 * Public for unit testing. 134 */ 135 virtual void CheckForStaleTipAndEvictPeers() = 0; 136 137 /** Process a single message from a peer. Public for fuzz testing */ 138 virtual void ProcessMessage(CNode& pfrom, const std::string& msg_type, DataStream& vRecv, 139 const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex) = 0; 140 141 /** This function is used for testing the stale tip eviction logic, see denialofservice_tests.cpp */ 142 virtual void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) = 0; 143 144 /** 145 * Gets the set of service flags which are "desirable" for a given peer. 146 * 147 * These are the flags which are required for a peer to support for them 148 * to be "interesting" to us, ie for us to wish to use one of our few 149 * outbound connection slots for or for us to wish to prioritize keeping 150 * their connection around. 151 * 152 * Relevant service flags may be peer- and state-specific in that the 153 * version of the peer may determine which flags are required (eg in the 154 * case of NODE_NETWORK_LIMITED where we seek out NODE_NETWORK peers 155 * unless they set NODE_NETWORK_LIMITED and we are out of IBD, in which 156 * case NODE_NETWORK_LIMITED suffices). 157 * 158 * Thus, generally, avoid calling with 'services' == NODE_NONE, unless 159 * state-specific flags must absolutely be avoided. When called with 160 * 'services' == NODE_NONE, the returned desirable service flags are 161 * guaranteed to not change dependent on state - ie they are suitable for 162 * use when describing peers which we know to be desirable, but for which 163 * we do not have a confirmed set of service flags. 164 */ 165 virtual ServiceFlags GetDesirableServiceFlags(ServiceFlags services) const = 0; 166 }; 167 168 #endif // BITCOIN_NET_PROCESSING_H