Coverage Report

Created: 2026-06-16 16:41

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