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