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