Branch data Line data Source code
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 : : /** Time between pings automatically sent out for latency probing and keepalive */ 31 : : static constexpr auto PING_INTERVAL{2min}; 32 : : 33 : 0 : struct CNodeStateStats { 34 : 0 : int nSyncHeight = -1; 35 : 0 : int nCommonHeight = -1; 36 : 0 : int m_starting_height = -1; 37 : : std::chrono::microseconds m_ping_wait; 38 : : std::vector<int> vHeightInFlight; 39 : : bool m_relay_txs; 40 : : CAmount m_fee_filter_received; 41 : 0 : uint64_t m_addr_processed = 0; 42 : 0 : uint64_t m_addr_rate_limited = 0; 43 : 0 : bool m_addr_relay_enabled{false}; 44 : : ServiceFlags their_services; 45 : 0 : int64_t presync_height{-1}; 46 : : }; 47 : : 48 : 0 : class PeerManager : public CValidationInterface, public NetEventsInterface 49 : : { 50 : : public: 51 : 1 : struct Options { 52 : : //! Whether this node is running in -blocksonly mode 53 : 1 : bool ignore_incoming_txs{DEFAULT_BLOCKSONLY}; 54 : : //! Whether transaction reconciliation protocol is enabled 55 : 1 : bool reconcile_txs{DEFAULT_TXRECONCILIATION_ENABLE}; 56 : : //! Maximum number of orphan transactions kept in memory 57 : 1 : uint32_t max_orphan_txs{DEFAULT_MAX_ORPHAN_TRANSACTIONS}; 58 : : //! Number of non-mempool transactions to keep around for block reconstruction. Includes 59 : : //! orphan, replaced, and rejected transactions. 60 : 1 : uint32_t max_extra_txs{DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN}; 61 : : //! Whether all P2P messages are captured to disk 62 : 1 : bool capture_messages{false}; 63 : : //! Whether or not the internal RNG behaves deterministically (this is 64 : : //! a test-only option). 65 : 1 : bool deterministic_rng{false}; 66 : : }; 67 : : 68 : : static std::unique_ptr<PeerManager> make(CConnman& connman, AddrMan& addrman, 69 : : BanMan* banman, ChainstateManager& chainman, 70 : : CTxMemPool& pool, Options opts); 71 : 1 : virtual ~PeerManager() { } 72 : : 73 : : /** 74 : : * Attempt to manually fetch block from a given peer. We must already have the header. 75 : : * 76 : : * @param[in] peer_id The peer id 77 : : * @param[in] block_index The blockindex 78 : : * @returns std::nullopt if a request was successfully made, otherwise an error message 79 : : */ 80 : : virtual std::optional<std::string> FetchBlock(NodeId peer_id, const CBlockIndex& block_index) = 0; 81 : : 82 : : /** Begin running background tasks, should only be called once */ 83 : : virtual void StartScheduledTasks(CScheduler& scheduler) = 0; 84 : : 85 : : /** Get statistics from node state */ 86 : : virtual bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const = 0; 87 : : 88 : : /** Whether this node ignores txs received over p2p. */ 89 : : virtual bool IgnoresIncomingTxs() = 0; 90 : : 91 : : /** Relay transaction to all peers. */ 92 : : virtual void RelayTransaction(const uint256& txid, const uint256& wtxid) = 0; 93 : : 94 : : /** Send ping message to all peers */ 95 : : virtual void SendPings() = 0; 96 : : 97 : : /** Set the best height */ 98 : : virtual void SetBestHeight(int height) = 0; 99 : : 100 : : /* Public for unit testing. */ 101 : : virtual void UnitTestMisbehaving(NodeId peer_id, int howmuch) = 0; 102 : : 103 : : /** 104 : : * Evict extra outbound peers. If we think our tip may be stale, connect to an extra outbound. 105 : : * Public for unit testing. 106 : : */ 107 : : virtual void CheckForStaleTipAndEvictPeers() = 0; 108 : : 109 : : /** Process a single message from a peer. Public for fuzz testing */ 110 : : virtual void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv, 111 : : const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex) = 0; 112 : : 113 : : /** This function is used for testing the stale tip eviction logic, see denialofservice_tests.cpp */ 114 : : virtual void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) = 0; 115 : : }; 116 : : 117 : : #endif // BITCOIN_NET_PROCESSING_H