/Users/eugenesiegel/btc/bitcoin/src/net_processing.h
| Line | Count | Source | 
| 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 |  | /** The compactblocks version we support. See BIP 152. */ | 
| 50 |  | static constexpr uint64_t CMPCTBLOCKS_VERSION{2}; | 
| 51 |  |  | 
| 52 |  | struct CNodeStateStats { | 
| 53 |  |     int nSyncHeight = -1; | 
| 54 |  |     int nCommonHeight = -1; | 
| 55 |  |     int m_starting_height = -1; | 
| 56 |  |     std::chrono::microseconds m_ping_wait; | 
| 57 |  |     std::vector<int> vHeightInFlight; | 
| 58 |  |     bool m_relay_txs; | 
| 59 |  |     int m_inv_to_send = 0; | 
| 60 |  |     uint64_t m_last_inv_seq{0}; | 
| 61 |  |     CAmount m_fee_filter_received; | 
| 62 |  |     uint64_t m_addr_processed = 0; | 
| 63 |  |     uint64_t m_addr_rate_limited = 0; | 
| 64 |  |     bool m_addr_relay_enabled{false}; | 
| 65 |  |     ServiceFlags their_services; | 
| 66 |  |     int64_t presync_height{-1}; | 
| 67 |  |     std::chrono::seconds time_offset{0}; | 
| 68 |  | }; | 
| 69 |  |  | 
| 70 |  | struct PeerManagerInfo { | 
| 71 |  |     std::chrono::seconds median_outbound_time_offset{0s}; | 
| 72 |  |     bool ignores_incoming_txs{false}; | 
| 73 |  | }; | 
| 74 |  |  | 
| 75 |  | class PeerManager : public CValidationInterface, public NetEventsInterface | 
| 76 |  | { | 
| 77 |  | public: | 
| 78 |  |     struct Options { | 
| 79 |  |         //! Whether this node is running in -blocksonly mode | 
| 80 |  |         bool ignore_incoming_txs{DEFAULT_BLOCKSONLY}; | 
| 81 |  |         //! Whether transaction reconciliation protocol is enabled | 
| 82 |  |         bool reconcile_txs{DEFAULT_TXRECONCILIATION_ENABLE}; | 
| 83 |  |         //! Number of non-mempool transactions to keep around for block reconstruction. Includes | 
| 84 |  |         //! orphan, replaced, and rejected transactions. | 
| 85 |  |         uint32_t max_extra_txs{DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN}; | 
| 86 |  |         //! Whether all P2P messages are captured to disk | 
| 87 |  |         bool capture_messages{false}; | 
| 88 |  |         //! Whether or not the internal RNG behaves deterministically (this is | 
| 89 |  |         //! a test-only option). | 
| 90 |  |         bool deterministic_rng{false}; | 
| 91 |  |         //! Number of headers sent in one getheaders message result (this is | 
| 92 |  |         //! a test-only option). | 
| 93 |  |         uint32_t max_headers_result{MAX_HEADERS_RESULTS}; | 
| 94 |  |     }; | 
| 95 |  |  | 
| 96 |  |     static std::unique_ptr<PeerManager> make(CConnman& connman, AddrMan& addrman, | 
| 97 |  |                                              BanMan* banman, ChainstateManager& chainman, | 
| 98 |  |                                              CTxMemPool& pool, node::Warnings& warnings, Options opts); | 
| 99 | 51.2k |     virtual ~PeerManager() = default; | 
| 100 |  |  | 
| 101 |  |     /** | 
| 102 |  |      * Attempt to manually fetch block from a given peer. We must already have the header. | 
| 103 |  |      * | 
| 104 |  |      * @param[in]  peer_id      The peer id | 
| 105 |  |      * @param[in]  block_index  The blockindex | 
| 106 |  |      * @returns std::nullopt if a request was successfully made, otherwise an error message | 
| 107 |  |      */ | 
| 108 |  |     virtual std::optional<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 |  |     /** Relay transaction to all peers. */ | 
| 122 |  |     virtual void RelayTransaction(const Txid& txid, const Wtxid& wtxid) = 0; | 
| 123 |  |  | 
| 124 |  |     /** Send ping message to all peers */ | 
| 125 |  |     virtual void SendPings() = 0; | 
| 126 |  |  | 
| 127 |  |     /** Set the height of the best block and its time (seconds since epoch). */ | 
| 128 |  |     virtual void SetBestBlock(int height, std::chrono::seconds time) = 0; | 
| 129 |  |  | 
| 130 |  |     /* Public for unit testing. */ | 
| 131 |  |     virtual void UnitTestMisbehaving(NodeId peer_id) = 0; | 
| 132 |  |  | 
| 133 |  |     /** | 
| 134 |  |      * Evict extra outbound peers. If we think our tip may be stale, connect to an extra outbound. | 
| 135 |  |      * Public for unit testing. | 
| 136 |  |      */ | 
| 137 |  |     virtual void CheckForStaleTipAndEvictPeers() = 0; | 
| 138 |  |  | 
| 139 |  |     /** Process a single message from a peer. Public for fuzz testing */ | 
| 140 |  |     virtual void ProcessMessage(CNode& pfrom, const std::string& msg_type, DataStream& vRecv, | 
| 141 |  |                                 const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex) = 0; | 
| 142 |  |  | 
| 143 |  |     /** This function is used for testing the stale tip eviction logic, see denialofservice_tests.cpp */ | 
| 144 |  |     virtual void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) = 0; | 
| 145 |  |  | 
| 146 |  |     /** | 
| 147 |  |      * Gets the set of service flags which are "desirable" for a given peer. | 
| 148 |  |      * | 
| 149 |  |      * These are the flags which are required for a peer to support for them | 
| 150 |  |      * to be "interesting" to us, ie for us to wish to use one of our few | 
| 151 |  |      * outbound connection slots for or for us to wish to prioritize keeping | 
| 152 |  |      * their connection around. | 
| 153 |  |      * | 
| 154 |  |      * Relevant service flags may be peer- and state-specific in that the | 
| 155 |  |      * version of the peer may determine which flags are required (eg in the | 
| 156 |  |      * case of NODE_NETWORK_LIMITED where we seek out NODE_NETWORK peers | 
| 157 |  |      * unless they set NODE_NETWORK_LIMITED and we are out of IBD, in which | 
| 158 |  |      * case NODE_NETWORK_LIMITED suffices). | 
| 159 |  |      * | 
| 160 |  |      * Thus, generally, avoid calling with 'services' == NODE_NONE, unless | 
| 161 |  |      * state-specific flags must absolutely be avoided. When called with | 
| 162 |  |      * 'services' == NODE_NONE, the returned desirable service flags are | 
| 163 |  |      * guaranteed to not change dependent on state - ie they are suitable for | 
| 164 |  |      * use when describing peers which we know to be desirable, but for which | 
| 165 |  |      * we do not have a confirmed set of service flags. | 
| 166 |  |     */ | 
| 167 |  |     virtual ServiceFlags GetDesirableServiceFlags(ServiceFlags services) const = 0; | 
| 168 |  | }; | 
| 169 |  |  | 
| 170 |  | #endif // BITCOIN_NET_PROCESSING_H |