LCOV - code coverage report
Current view: top level - src - net_processing.h Coverage Total Hit
Test: fuzz_coverage.info Lines: 66.7 % 3 2
Test Date: 2025-04-25 04:10:03 Functions: - 0 0
Branches: 50.0 % 2 1

             Branch data     Line data    Source code
       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 <protocol.h>
      12                 :             : #include <threadsafety.h>
      13                 :             : #include <txorphanage.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 for -maxorphantx, maximum number of orphan transactions kept in memory */
      40                 :             : static const uint32_t DEFAULT_MAX_ORPHAN_TRANSACTIONS{100};
      41                 :             : /** Default number of non-mempool transactions to keep around for block reconstruction. Includes
      42                 :             :     orphan, replaced, and rejected transactions. */
      43                 :             : static const uint32_t DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN{100};
      44                 :             : static const bool DEFAULT_PEERBLOOMFILTERS = false;
      45                 :             : static const bool DEFAULT_PEERBLOCKFILTERS = false;
      46                 :             : /** Maximum number of outstanding CMPCTBLOCK requests for the same block. */
      47                 :             : static const unsigned int MAX_CMPCTBLOCKS_INFLIGHT_PER_BLOCK = 3;
      48                 :             : /** Number of headers sent in one getheaders result. We rely on the assumption that if a peer sends
      49                 :             :  *  less than this number, we reached its tip. Changing this value is a protocol upgrade. */
      50                 :             : static const unsigned int MAX_HEADERS_RESULTS = 2000;
      51                 :             : 
      52         [ +  - ]:       16136 : 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                 :             :     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                 :         756 : 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                 :             :         //! Maximum number of orphan transactions kept in memory
      82                 :             :         uint32_t max_orphan_txs{DEFAULT_MAX_ORPHAN_TRANSACTIONS};
      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                 :           0 :     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<TxOrphanage::OrphanTxBase> 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 uint256& txid, const uint256& 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
        

Generated by: LCOV version 2.0-1