fuzz coverage

Coverage Report

Created: 2026-04-24 13:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/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 <private_broadcast.h>
13
#include <protocol.h>
14
#include <uint256.h>
15
#include <util/expected.h>
16
#include <validationinterface.h>
17
18
#include <atomic>
19
#include <chrono>
20
#include <cstdint>
21
#include <memory>
22
#include <optional>
23
#include <string>
24
#include <vector>
25
26
class AddrMan;
27
class CTxMemPool;
28
class ChainstateManager;
29
class BanMan;
30
class CBlockIndex;
31
class CScheduler;
32
class DataStream;
33
class uint256;
34
35
namespace node {
36
class Warnings;
37
} // namespace node
38
39
/** Whether transaction reconciliation protocol should be enabled by default. */
40
static constexpr bool DEFAULT_TXRECONCILIATION_ENABLE{false};
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
/** The compactblocks version we support. See BIP 152. */
52
static constexpr uint64_t CMPCTBLOCKS_VERSION{2};
53
54
struct CNodeStateStats {
55
    int nSyncHeight = -1;
56
    int nCommonHeight = -1;
57
    std::chrono::microseconds m_ping_wait;
58
    std::vector<int> vHeightInFlight;
59
    bool m_relay_txs;
60
    int m_inv_to_send = 0;
61
    uint64_t m_last_inv_seq{0};
62
    CAmount m_fee_filter_received;
63
    uint64_t m_addr_processed = 0;
64
    uint64_t m_addr_rate_limited = 0;
65
    bool m_addr_relay_enabled{false};
66
    ServiceFlags their_services;
67
    int64_t presync_height{-1};
68
    std::chrono::seconds time_offset{0};
69
};
70
71
struct PeerManagerInfo {
72
    std::chrono::seconds median_outbound_time_offset{0s};
73
    bool ignores_incoming_txs{false};
74
};
75
76
class PeerManager : public CValidationInterface, public NetEventsInterface
77
{
78
public:
79
    struct Options {
80
        //! Whether this node is running in -blocksonly mode
81
        bool ignore_incoming_txs{DEFAULT_BLOCKSONLY};
82
        //! Whether transaction reconciliation protocol is enabled
83
        bool reconcile_txs{DEFAULT_TXRECONCILIATION_ENABLE};
84
        //! Number of non-mempool transactions to keep around for block reconstruction. Includes
85
        //! orphan, replaced, and rejected transactions.
86
        uint32_t max_extra_txs{DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN};
87
        //! Whether all P2P messages are captured to disk
88
        bool capture_messages{false};
89
        //! Whether or not the internal RNG behaves deterministically (this is
90
        //! a test-only option).
91
        bool deterministic_rng{false};
92
        //! Number of headers sent in one getheaders message result (this is
93
        //! a test-only option).
94
        uint32_t max_headers_result{MAX_HEADERS_RESULTS};
95
        //! Whether private broadcast is used for sending transactions.
96
        bool private_broadcast{DEFAULT_PRIVATE_BROADCAST};
97
    };
98
99
    static std::unique_ptr<PeerManager> make(CConnman& connman, AddrMan& addrman,
100
                                             BanMan* banman, ChainstateManager& chainman,
101
                                             CTxMemPool& pool, node::Warnings& warnings, Options opts);
102
190k
    virtual ~PeerManager() = default;
103
104
    /**
105
     * Attempt to manually fetch block from a given peer. We must already have the header.
106
     *
107
     * @param[in]  peer_id      The peer id
108
     * @param[in]  block_index  The blockindex
109
     */
110
    virtual util::Expected<void, std::string> FetchBlock(NodeId peer_id, const CBlockIndex& block_index) = 0;
111
112
    /** Begin running background tasks, should only be called once */
113
    virtual void StartScheduledTasks(CScheduler& scheduler) = 0;
114
115
    /** Get statistics from node state */
116
    virtual bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const = 0;
117
118
    virtual std::vector<node::TxOrphanage::OrphanInfo> GetOrphanTransactions() = 0;
119
120
    /** Get peer manager info. */
121
    virtual PeerManagerInfo GetInfo() const = 0;
122
123
    /** Get info about transactions currently being privately broadcast. */
124
    virtual std::vector<PrivateBroadcast::TxBroadcastInfo> GetPrivateBroadcastInfo() const = 0;
125
126
    /**
127
     * Abort private broadcast attempts for transactions currently being privately broadcast.
128
     *
129
     * @param[in] id A transaction identifier. It will be matched against both txid and wtxid for
130
     *               all transactions in the private broadcast queue.
131
     *
132
     * @return Transactions removed from the private broadcast queue. If the provided id matches a
133
     *         txid that corresponds to multiple transactions with different wtxids, multiple
134
     *         transactions may be returned.
135
     */
136
    virtual std::vector<CTransactionRef> AbortPrivateBroadcast(const uint256& id) = 0;
137
138
    /**
139
     * Initiate a transaction broadcast to eligible peers.
140
     * Queue the witness transaction id to `Peer::TxRelay::m_tx_inventory_to_send`
141
     * for each peer. Later, depending on `Peer::TxRelay::m_next_inv_send_time` and if
142
     * the transaction is in the mempool, an `INV` about it may be sent to the peer.
143
     */
144
    virtual void InitiateTxBroadcastToAll(const Txid& txid, const Wtxid& wtxid) = 0;
145
146
    /**
147
     * Initiate a private transaction broadcast. This is done
148
     * asynchronously via short-lived connections to peers on privacy networks.
149
     */
150
    virtual void InitiateTxBroadcastPrivate(const CTransactionRef& tx) = 0;
151
152
    /** Send ping message to all peers */
153
    virtual void SendPings() = 0;
154
155
    /** Set the height of the best block and its time (seconds since epoch). */
156
    virtual void SetBestBlock(int height, std::chrono::seconds time) = 0;
157
158
    /* Public for unit testing. */
159
    virtual void UnitTestMisbehaving(NodeId peer_id) = 0;
160
161
    /**
162
     * Evict extra outbound peers. If we think our tip may be stale, connect to an extra outbound.
163
     * Public for unit testing.
164
     */
165
    virtual void CheckForStaleTipAndEvictPeers() = 0;
166
167
    /** This function is used for testing the stale tip eviction logic, see denialofservice_tests.cpp */
168
    virtual void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) = 0;
169
170
    /**
171
     * Gets the set of service flags which are "desirable" for a given peer.
172
     *
173
     * These are the flags which are required for a peer to support for them
174
     * to be "interesting" to us, ie for us to wish to use one of our few
175
     * outbound connection slots for or for us to wish to prioritize keeping
176
     * their connection around.
177
     *
178
     * Relevant service flags may be peer- and state-specific in that the
179
     * version of the peer may determine which flags are required (eg in the
180
     * case of NODE_NETWORK_LIMITED where we seek out NODE_NETWORK peers
181
     * unless they set NODE_NETWORK_LIMITED and we are out of IBD, in which
182
     * case NODE_NETWORK_LIMITED suffices).
183
     *
184
     * Thus, generally, avoid calling with 'services' == NODE_NONE, unless
185
     * state-specific flags must absolutely be avoided. When called with
186
     * 'services' == NODE_NONE, the returned desirable service flags are
187
     * guaranteed to not change dependent on state - ie they are suitable for
188
     * use when describing peers which we know to be desirable, but for which
189
     * we do not have a confirmed set of service flags.
190
    */
191
    virtual ServiceFlags GetDesirableServiceFlags(ServiceFlags services) const = 0;
192
};
193
194
#endif // BITCOIN_NET_PROCESSING_H