Coverage Report

Created: 2026-07-29 23:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/net_processing.cpp
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
#include <net_processing.h>
7
8
#include <addrman.h>
9
#include <arith_uint256.h>
10
#include <banman.h>
11
#include <blockencodings.h>
12
#include <blockfilter.h>
13
#include <chain.h>
14
#include <chainparams.h>
15
#include <common/bloom.h>
16
#include <consensus/amount.h>
17
#include <consensus/params.h>
18
#include <consensus/validation.h>
19
#include <core_memusage.h>
20
#include <crypto/siphash.h>
21
#include <deploymentstatus.h>
22
#include <flatfile.h>
23
#include <headerssync.h>
24
#include <index/blockfilterindex.h>
25
#include <kernel/types.h>
26
#include <logging.h>
27
#include <merkleblock.h>
28
#include <net.h>
29
#include <net_permissions.h>
30
#include <netaddress.h>
31
#include <netbase.h>
32
#include <netmessagemaker.h>
33
#include <node/blockstorage.h>
34
#include <node/connection_types.h>
35
#include <node/protocol_version.h>
36
#include <node/timeoffsets.h>
37
#include <node/txdownloadman.h>
38
#include <node/txorphanage.h>
39
#include <node/txreconciliation.h>
40
#include <node/warnings.h>
41
#include <policy/feerate.h>
42
#include <policy/fees/block_policy_estimator.h>
43
#include <policy/packages.h>
44
#include <policy/policy.h>
45
#include <primitives/block.h>
46
#include <primitives/transaction.h>
47
#include <private_broadcast.h>
48
#include <protocol.h>
49
#include <random.h>
50
#include <scheduler.h>
51
#include <script/script.h>
52
#include <serialize.h>
53
#include <span.h>
54
#include <streams.h>
55
#include <sync.h>
56
#include <tinyformat.h>
57
#include <txmempool.h>
58
#include <uint256.h>
59
#include <util/check.h>
60
#include <util/strencodings.h>
61
#include <util/time.h>
62
#include <util/tokenbucket.h>
63
#include <util/trace.h>
64
#include <validation.h>
65
66
#include <algorithm>
67
#include <array>
68
#include <atomic>
69
#include <compare>
70
#include <cstddef>
71
#include <deque>
72
#include <exception>
73
#include <functional>
74
#include <future>
75
#include <initializer_list>
76
#include <iterator>
77
#include <limits>
78
#include <list>
79
#include <map>
80
#include <memory>
81
#include <optional>
82
#include <queue>
83
#include <ranges>
84
#include <ratio>
85
#include <set>
86
#include <span>
87
#include <typeinfo>
88
#include <utility>
89
90
using kernel::ChainstateRole;
91
using namespace util::hex_literals;
92
93
TRACEPOINT_SEMAPHORE(net, inbound_message);
94
TRACEPOINT_SEMAPHORE(net, misbehaving_connection);
95
96
/** Headers download timeout.
97
 *  Timeout = base + per_header * (expected number of headers) */
98
static constexpr auto HEADERS_DOWNLOAD_TIMEOUT_BASE = 15min;
99
static constexpr auto HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER = 1ms;
100
/** How long to wait for a peer to respond to a getheaders request */
101
static constexpr auto HEADERS_RESPONSE_TIME{2min};
102
/** Protect at least this many outbound peers from disconnection due to slow/
103
 * behind headers chain.
104
 */
105
static constexpr int32_t MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT = 4;
106
/** Timeout for (unprotected) outbound peers to sync to our chainwork */
107
static constexpr auto CHAIN_SYNC_TIMEOUT{20min};
108
/** How frequently to check for stale tips */
109
static constexpr auto STALE_CHECK_INTERVAL{10min};
110
/** How frequently to check for extra outbound peers and disconnect */
111
static constexpr auto EXTRA_PEER_CHECK_INTERVAL{45s};
112
/** Minimum time an outbound-peer-eviction candidate must be connected for, in order to evict */
113
static constexpr auto MINIMUM_CONNECT_TIME{30s};
114
/** SHA256("main address relay")[0:8] */
115
static constexpr uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL;
116
/// Age after which a stale block will no longer be served if requested as
117
/// protection against fingerprinting. Set to one month, denominated in seconds.
118
static constexpr int STALE_RELAY_AGE_LIMIT = 30 * 24 * 60 * 60;
119
/// Age after which a block is considered historical for purposes of rate
120
/// limiting block relay. Set to one week, denominated in seconds.
121
static constexpr int HISTORICAL_BLOCK_AGE = 7 * 24 * 60 * 60;
122
/** Time between pings automatically sent out for latency probing and keepalive */
123
static constexpr auto PING_INTERVAL{2min};
124
/** The maximum number of entries in a locator */
125
static const unsigned int MAX_LOCATOR_SZ = 101;
126
/** The maximum number of entries in an 'inv' protocol message */
127
static const unsigned int MAX_INV_SZ = 50000;
128
/** Limit to avoid sending big packets. Not used in processing incoming GETDATA for compatibility */
129
static const unsigned int MAX_GETDATA_SZ = 1000;
130
/** Number of blocks that can be requested at any given time from a single peer. */
131
static const int MAX_BLOCKS_IN_TRANSIT_PER_PEER = 16;
132
/** Default time during which a peer must stall block download progress before being disconnected.
133
 * the actual timeout is increased temporarily if peers are disconnected for hitting the timeout */
134
static constexpr auto BLOCK_STALLING_TIMEOUT_DEFAULT{2s};
135
/** Maximum timeout for stalling block download. */
136
static constexpr auto BLOCK_STALLING_TIMEOUT_MAX{64s};
137
/** Maximum depth of blocks we're willing to serve as compact blocks to peers
138
 *  when requested. For older blocks, a regular BLOCK response will be sent. */
139
static const int MAX_CMPCTBLOCK_DEPTH = 5;
140
/** Maximum depth of blocks we're willing to respond to GETBLOCKTXN requests for. */
141
static const int MAX_BLOCKTXN_DEPTH = 10;
142
static_assert(MAX_BLOCKTXN_DEPTH <= MIN_BLOCKS_TO_KEEP, "MAX_BLOCKTXN_DEPTH too high");
143
/** Size of the "block download window": how far ahead of our current height do we fetch?
144
 *  Larger windows tolerate larger download speed differences between peer, but increase the potential
145
 *  degree of disordering of blocks on disk (which make reindexing and pruning harder). We'll probably
146
 *  want to make this a per-peer adaptive value at some point. */
147
static const unsigned int BLOCK_DOWNLOAD_WINDOW = 1024;
148
/** Block download timeout base, expressed in multiples of the block interval (i.e. 10 min) */
149
static constexpr double BLOCK_DOWNLOAD_TIMEOUT_BASE = 1;
150
/** Additional block download timeout per parallel downloading peer (i.e. 5 min) */
151
static constexpr double BLOCK_DOWNLOAD_TIMEOUT_PER_PEER = 0.5;
152
/** Maximum number of headers to announce when relaying blocks with headers message.*/
153
static const unsigned int MAX_BLOCKS_TO_ANNOUNCE = 8;
154
/** Minimum blocks required to signal NODE_NETWORK_LIMITED */
155
static const unsigned int NODE_NETWORK_LIMITED_MIN_BLOCKS = 288;
156
/** Window, in blocks, for connecting to NODE_NETWORK_LIMITED peers */
157
static const unsigned int NODE_NETWORK_LIMITED_ALLOW_CONN_BLOCKS = 144;
158
/** Average delay between local address broadcasts */
159
static constexpr auto AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL{24h};
160
/** Average delay between peer address broadcasts */
161
static constexpr auto AVG_ADDRESS_BROADCAST_INTERVAL{30s};
162
/** Delay between rotating the peers we relay a particular address to */
163
static constexpr auto ROTATE_ADDR_RELAY_DEST_INTERVAL{24h};
164
/** Average delay between trickled inventory transmissions for inbound peers.
165
 *  Blocks and peers with NetPermissionFlags::NoBan permission bypass this. */
166
static constexpr auto INBOUND_INVENTORY_BROADCAST_INTERVAL{5s};
167
/** Average delay between trickled inventory transmissions for outbound peers.
168
 *  Use a smaller delay as there is less privacy concern for them.
169
 *  Blocks and peers with NetPermissionFlags::NoBan permission bypass this. */
170
static constexpr auto OUTBOUND_INVENTORY_BROADCAST_INTERVAL{2s};
171
/** Multiplier for the inventory bucket rate for outbounds */
172
static constexpr double OUTBOUND_INVENTORY_BUCKET_MULTIPLIER{Ticks<SecondsDouble>(INBOUND_INVENTORY_BROADCAST_INTERVAL) / Ticks<SecondsDouble>(OUTBOUND_INVENTORY_BROADCAST_INTERVAL)};
173
/** Delay between checking inventory bucket and backlog */
174
static constexpr auto INVENTORY_BUCKET_CHECK_DELAY{100ms};
175
/** Empty backlog target capacity */
176
static constexpr size_t INVENTORY_BUCKET_BACKLOG_CAPACITY{300};
177
/** Delay between inventory bucket backlog heartbeat log entries */
178
static constexpr auto INVENTORY_BUCKET_BACKLOG_HEARTBEAT{2000ms};
179
/** Minimum backlog to trigger heartbeat log entries */
180
static constexpr size_t INVENTORY_BUCKET_BACKLOG_HEARTBEAT_MIN{100};
181
/** Average delay between feefilter broadcasts in seconds. */
182
static constexpr auto AVG_FEEFILTER_BROADCAST_INTERVAL{10min};
183
/** Maximum feefilter broadcast delay after significant change. */
184
static constexpr auto MAX_FEEFILTER_CHANGE_DELAY{5min};
185
/** Maximum number of compact filters that may be requested with one getcfilters. See BIP 157. */
186
static constexpr uint32_t MAX_GETCFILTERS_SIZE = 1000;
187
/** Maximum number of cf hashes that may be requested with one getcfheaders. See BIP 157. */
188
static constexpr uint32_t MAX_GETCFHEADERS_SIZE = 2000;
189
/** the maximum percentage of addresses from our addrman to return in response to a getaddr message. */
190
static constexpr size_t MAX_PCT_ADDR_TO_SEND = 23;
191
/** The maximum number of address records permitted in an ADDR message. */
192
static constexpr size_t MAX_ADDR_TO_SEND{1000};
193
/** The maximum rate of address records we're willing to process on average. Can be bypassed using
194
 *  the NetPermissionFlags::Addr permission. */
195
static constexpr double MAX_ADDR_RATE_PER_SECOND{0.1};
196
/** The soft limit of the address processing token bucket (the regular MAX_ADDR_RATE_PER_SECOND
197
 *  based increments won't go above this, but the MAX_ADDR_TO_SEND increment following GETADDR
198
 *  is exempt from this limit). */
199
static constexpr size_t MAX_ADDR_PROCESSING_TOKEN_BUCKET{MAX_ADDR_TO_SEND};
200
/** For private broadcast, send a transaction to this many peers. */
201
static constexpr size_t NUM_PRIVATE_BROADCAST_PER_TX{3};
202
/** Private broadcast connections must complete within this time. Disconnect the peer if it takes longer. */
203
static constexpr auto PRIVATE_BROADCAST_MAX_CONNECTION_LIFETIME{3min};
204
205
// Internal stuff
206
namespace {
207
/** Blocks that are in flight, and that are in the queue to be downloaded. */
208
struct QueuedBlock {
209
    /** BlockIndex. We must have this since we only request blocks when we've already validated the header. */
210
    const CBlockIndex* pindex;
211
    /** Optional, used for CMPCTBLOCK downloads */
212
    std::unique_ptr<PartiallyDownloadedBlock> partialBlock;
213
};
214
215
/**
216
 * Data structure for an individual peer. This struct is not protected by
217
 * cs_main since it does not contain validation-critical data.
218
 *
219
 * Memory is owned by shared pointers and this object is destructed when
220
 * the refcount drops to zero.
221
 *
222
 * Mutexes inside this struct must not be held when locking m_peer_mutex.
223
 *
224
 * TODO: move most members from CNodeState to this structure.
225
 * TODO: move remaining application-layer data members from CNode to this structure.
226
 */
227
struct Peer {
228
    /** Same id as the CNode object for this peer */
229
    const NodeId m_id{0};
230
231
    /** Services we offered to this peer.
232
     *
233
     *  This is supplied by CConnman during peer initialization. It's const
234
     *  because there is no protocol defined for renegotiating services
235
     *  initially offered to a peer. The set of local services we offer should
236
     *  not change after initialization.
237
     *
238
     *  An interesting example of this is NODE_NETWORK and initial block
239
     *  download: a node which starts up from scratch doesn't have any blocks
240
     *  to serve, but still advertises NODE_NETWORK because it will eventually
241
     *  fulfill this role after IBD completes. P2P code is written in such a
242
     *  way that it can gracefully handle peers who don't make good on their
243
     *  service advertisements. */
244
    const ServiceFlags m_our_services;
245
    /** Services this peer offered to us. */
246
    std::atomic<ServiceFlags> m_their_services{NODE_NONE};
247
248
    //! Whether this peer is an inbound connection
249
    const bool m_is_inbound;
250
251
    /** Protects misbehavior data members */
252
    Mutex m_misbehavior_mutex;
253
    /** Whether this peer should be disconnected and marked as discouraged (unless it has NetPermissionFlags::NoBan permission). */
254
    bool m_should_discourage GUARDED_BY(m_misbehavior_mutex){false};
255
256
    /** Protects block inventory data members */
257
    Mutex m_block_inv_mutex;
258
    /** List of blocks that we'll announce via an `inv` message.
259
     * There is no final sorting before sending, as they are always sent
260
     * immediately and in the order requested. */
261
    std::vector<uint256> m_blocks_for_inv_relay GUARDED_BY(m_block_inv_mutex);
262
    /** Unfiltered list of blocks that we'd like to announce via a `headers`
263
     * message. If we can't announce via a `headers` message, we'll fall back to
264
     * announcing via `inv`. */
265
    std::vector<uint256> m_blocks_for_headers_relay GUARDED_BY(m_block_inv_mutex);
266
    /** The final block hash that we sent in an `inv` message to this peer.
267
     * When the peer requests this block, we send an `inv` message to trigger
268
     * the peer to request the next sequence of block hashes.
269
     * Most peers use headers-first syncing, which doesn't use this mechanism */
270
    uint256 m_continuation_block GUARDED_BY(m_block_inv_mutex) {};
271
272
    /** Set to true once initial VERSION message was sent (only relevant for outbound peers). */
273
    bool m_outbound_version_message_sent GUARDED_BY(NetEventsInterface::g_msgproc_mutex){false};
274
275
    /** The pong reply we're expecting, or 0 if no pong expected. */
276
    std::atomic<uint64_t> m_ping_nonce_sent{0};
277
    /** When the last ping was sent, or 0 if no ping was ever sent */
278
    std::atomic<NodeClock::time_point> m_ping_start{NodeClock::epoch};
279
    /** Whether a ping has been requested by the user */
280
    std::atomic<bool> m_ping_queued{false};
281
282
    /** Whether this peer relays txs via wtxid */
283
    std::atomic<bool> m_wtxid_relay{false};
284
    /** The feerate in the most recent BIP133 `feefilter` message sent to the peer.
285
     *  It is *not* a p2p protocol violation for the peer to send us
286
     *  transactions with a lower fee rate than this. See BIP133. */
287
    CAmount m_fee_filter_sent GUARDED_BY(NetEventsInterface::g_msgproc_mutex){0};
288
    /** Timestamp after which we will send the next BIP133 `feefilter` message
289
      * to the peer. */
290
    std::chrono::microseconds m_next_send_feefilter GUARDED_BY(NetEventsInterface::g_msgproc_mutex){0};
291
292
    struct TxRelay {
293
        mutable RecursiveMutex m_bloom_filter_mutex;
294
        /** Whether we relay transactions to this peer. */
295
        bool m_relay_txs GUARDED_BY(m_bloom_filter_mutex){false};
296
        /** A bloom filter for which transactions to announce to the peer. See BIP37. */
297
        std::unique_ptr<CBloomFilter> m_bloom_filter PT_GUARDED_BY(m_bloom_filter_mutex) GUARDED_BY(m_bloom_filter_mutex){nullptr};
298
299
        mutable RecursiveMutex m_tx_inventory_mutex;
300
        /** A filter of all the (w)txids that the peer has announced to
301
         *  us or we have announced to the peer. We use this to avoid announcing
302
         *  the same (w)txid to a peer that already has the transaction. */
303
        CRollingBloomFilter m_tx_inventory_known_filter GUARDED_BY(m_tx_inventory_mutex){50000, 0.000001};
304
        /** Vector of wtxids we still have to announce. For non-wtxid-relay peers,
305
         *  we retrieve the txid from the corresponding mempool transaction when
306
         *  constructing the `inv` message. We use the mempool to sort transactions
307
         *  in dependency order before relay, so this does not have to be sorted. */
308
        std::vector<Wtxid> m_tx_inventory_to_send GUARDED_BY(m_tx_inventory_mutex);
309
        /** Whether the peer has requested us to send our complete mempool. Only
310
         *  permitted if the peer has NetPermissionFlags::Mempool or we advertise
311
         *  NODE_BLOOM. See BIP35. */
312
        bool m_send_mempool GUARDED_BY(m_tx_inventory_mutex){false};
313
        /** The next time after which we will send an `inv` message containing
314
         *  transaction announcements to this peer. */
315
        std::chrono::microseconds m_next_inv_send_time GUARDED_BY(m_tx_inventory_mutex){0};
316
        /** The mempool sequence num at which we sent the last `inv` message to this peer.
317
         *  Can relay txs with lower sequence numbers than this (see CTxMempool::info_for_relay). */
318
        uint64_t m_last_inv_sequence GUARDED_BY(m_tx_inventory_mutex){1};
319
320
        /** Minimum fee rate with which to filter transaction announcements to this node. See BIP133. */
321
        std::atomic<CAmount> m_fee_filter_received{0};
322
    };
323
324
    /* Initializes a TxRelay struct for this peer. Can be called at most once for a peer. */
325
    TxRelay* SetTxRelay() EXCLUSIVE_LOCKS_REQUIRED(!m_tx_relay_mutex)
326
1.56k
    {
327
1.56k
        LOCK(m_tx_relay_mutex);
328
1.56k
        Assume(!m_tx_relay);
329
1.56k
        m_tx_relay = std::make_unique<Peer::TxRelay>();
330
1.56k
        return m_tx_relay.get();
331
1.56k
    };
332
333
    TxRelay* GetTxRelay() EXCLUSIVE_LOCKS_REQUIRED(!m_tx_relay_mutex)
334
530k
    {
335
530k
        return WITH_LOCK(m_tx_relay_mutex, return m_tx_relay.get());
336
530k
    };
337
338
    /** A vector of addresses to send to the peer, limited to MAX_ADDR_TO_SEND. */
339
    std::vector<CAddress> m_addrs_to_send GUARDED_BY(NetEventsInterface::g_msgproc_mutex);
340
    /** Probabilistic filter to track recent addr messages relayed with this
341
     *  peer. Used to avoid relaying redundant addresses to this peer.
342
     *
343
     *  We initialize this filter for outbound peers (other than
344
     *  block-relay-only connections) or when an inbound peer sends us an
345
     *  address related message (ADDR, ADDRV2, GETADDR).
346
     *
347
     *  Presence of this filter must correlate with m_addr_relay_enabled.
348
     **/
349
    std::unique_ptr<CRollingBloomFilter> m_addr_known GUARDED_BY(NetEventsInterface::g_msgproc_mutex);
350
    /** Whether we are participating in address relay with this connection.
351
     *
352
     *  We set this bool to true for outbound peers (other than
353
     *  block-relay-only connections), or when an inbound peer sends us an
354
     *  address related message (ADDR, ADDRV2, GETADDR).
355
     *
356
     *  We use this bool to decide whether a peer is eligible for gossiping
357
     *  addr messages. This avoids relaying to peers that are unlikely to
358
     *  forward them, effectively blackholing self announcements. Reasons
359
     *  peers might support addr relay on the link include that they connected
360
     *  to us as a block-relay-only peer or they are a light client.
361
     *
362
     *  This field must correlate with whether m_addr_known has been
363
     *  initialized.*/
364
    std::atomic_bool m_addr_relay_enabled{false};
365
    /** Whether a getaddr request to this peer is outstanding. */
366
    bool m_getaddr_sent GUARDED_BY(NetEventsInterface::g_msgproc_mutex){false};
367
    /** Guards address sending timers. */
368
    mutable Mutex m_addr_send_times_mutex;
369
    /** Time point to send the next ADDR message to this peer. */
370
    std::chrono::microseconds m_next_addr_send GUARDED_BY(m_addr_send_times_mutex){0};
371
    /** Time point to possibly re-announce our local address to this peer. */
372
    std::chrono::microseconds m_next_local_addr_send GUARDED_BY(m_addr_send_times_mutex){0};
373
    /** Whether the peer has signaled support for receiving ADDRv2 (BIP155)
374
     *  messages, indicating a preference to receive ADDRv2 instead of ADDR ones. */
375
    std::atomic_bool m_wants_addrv2{false};
376
    /** Whether this peer has already sent us a getaddr message. */
377
    bool m_getaddr_recvd GUARDED_BY(NetEventsInterface::g_msgproc_mutex){false};
378
    /** Number of addresses that can be processed from this peer. Start at 1 to
379
     *  permit self-announcement. */
380
    double m_addr_token_bucket GUARDED_BY(NetEventsInterface::g_msgproc_mutex){1.0};
381
    /** When m_addr_token_bucket was last updated */
382
    NodeClock::time_point m_addr_token_timestamp GUARDED_BY(NetEventsInterface::g_msgproc_mutex){NodeClock::now()};
383
    /** Total number of addresses that were dropped due to rate limiting. */
384
    std::atomic<uint64_t> m_addr_rate_limited{0};
385
    /** Total number of addresses that were processed (excludes rate-limited ones). */
386
    std::atomic<uint64_t> m_addr_processed{0};
387
388
    /** Whether we've sent this peer a getheaders in response to an inv prior to initial-headers-sync completing */
389
    bool m_inv_triggered_getheaders_before_sync GUARDED_BY(NetEventsInterface::g_msgproc_mutex){false};
390
391
    /** Protects m_getdata_requests **/
392
    Mutex m_getdata_requests_mutex;
393
    /** Work queue of items requested by this peer **/
394
    std::deque<CInv> m_getdata_requests GUARDED_BY(m_getdata_requests_mutex);
395
396
    /** Time of the last getheaders message to this peer */
397
    NodeClock::time_point m_last_getheaders_timestamp GUARDED_BY(NetEventsInterface::g_msgproc_mutex){};
398
399
    /** Protects m_headers_sync **/
400
    Mutex m_headers_sync_mutex;
401
    /** Headers-sync state for this peer (eg for initial sync, or syncing large
402
     * reorgs) **/
403
    std::unique_ptr<HeadersSyncState> m_headers_sync PT_GUARDED_BY(m_headers_sync_mutex) GUARDED_BY(m_headers_sync_mutex) {};
404
405
    /** Whether we've sent our peer a sendheaders message. **/
406
    std::atomic<bool> m_sent_sendheaders{false};
407
408
    /** When to potentially disconnect peer for stalling headers download */
409
    std::chrono::microseconds m_headers_sync_timeout GUARDED_BY(NetEventsInterface::g_msgproc_mutex){0us};
410
411
    /** Whether this peer wants invs or headers (when possible) for block announcements */
412
    bool m_prefers_headers GUARDED_BY(NetEventsInterface::g_msgproc_mutex){false};
413
414
    /** Time offset computed during the version handshake based on the
415
     * timestamp the peer sent in the version message. */
416
    std::atomic<std::chrono::seconds> m_time_offset{0s};
417
418
    explicit Peer(NodeId id, ServiceFlags our_services, bool is_inbound)
419
1.74k
        : m_id{id}
420
1.74k
        , m_our_services{our_services}
421
1.74k
        , m_is_inbound{is_inbound}
422
1.74k
    {}
423
424
private:
425
    mutable Mutex m_tx_relay_mutex;
426
427
    /** Transaction relay data. May be a nullptr. */
428
    std::unique_ptr<TxRelay> m_tx_relay GUARDED_BY(m_tx_relay_mutex);
429
};
430
431
using PeerRef = std::shared_ptr<Peer>;
432
433
/**
434
 * Maintain validation-specific state about nodes, protected by cs_main, instead
435
 * by CNode's own locks. This simplifies asynchronous operation, where
436
 * processing of incoming data is done after the ProcessMessage call returns,
437
 * and we're no longer holding the node's locks.
438
 */
439
struct CNodeState {
440
    //! The best known block we know this peer has announced.
441
    const CBlockIndex* pindexBestKnownBlock{nullptr};
442
    //! The hash of the last unknown block this peer has announced.
443
    uint256 hashLastUnknownBlock{};
444
    //! The last full block we both have.
445
    const CBlockIndex* pindexLastCommonBlock{nullptr};
446
    //! The best header we have sent our peer.
447
    const CBlockIndex* pindexBestHeaderSent{nullptr};
448
    //! Whether we've started headers synchronization with this peer.
449
    bool fSyncStarted{false};
450
    //! Since when we're stalling block download progress (in microseconds), or 0.
451
    std::chrono::microseconds m_stalling_since{0us};
452
    std::list<QueuedBlock> vBlocksInFlight;
453
    //! When the first entry in vBlocksInFlight started downloading. Don't care when vBlocksInFlight is empty.
454
    std::chrono::microseconds m_downloading_since{0us};
455
    //! Whether we consider this a preferred download peer.
456
    bool fPreferredDownload{false};
457
    /** Whether this peer wants invs or cmpctblocks (when possible) for block announcements. */
458
    bool m_requested_hb_cmpctblocks{false};
459
    /** Whether this peer will send us cmpctblocks if we request them. */
460
    bool m_provides_cmpctblocks{false};
461
462
    /** State used to enforce CHAIN_SYNC_TIMEOUT and EXTRA_PEER_CHECK_INTERVAL logic.
463
      *
464
      * Both are only in effect for outbound, non-manual, non-protected connections.
465
      * Any peer protected (m_protect = true) is not chosen for eviction. A peer is
466
      * marked as protected if all of these are true:
467
      *   - its connection type is IsBlockOnlyConn() == false
468
      *   - it gave us a valid connecting header
469
      *   - we haven't reached MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT yet
470
      *   - its chain tip has at least as much work as ours
471
      *
472
      * CHAIN_SYNC_TIMEOUT: if a peer's best known block has less work than our tip,
473
      * set a timeout CHAIN_SYNC_TIMEOUT in the future:
474
      *   - If at timeout their best known block now has more work than our tip
475
      *     when the timeout was set, then either reset the timeout or clear it
476
      *     (after comparing against our current tip's work)
477
      *   - If at timeout their best known block still has less work than our
478
      *     tip did when the timeout was set, then send a getheaders message,
479
      *     and set a shorter timeout, HEADERS_RESPONSE_TIME seconds in future.
480
      *     If their best known block is still behind when that new timeout is
481
      *     reached, disconnect.
482
      *
483
      * EXTRA_PEER_CHECK_INTERVAL: after each interval, if we have too many outbound peers,
484
      * drop the outbound one that least recently announced us a new block.
485
      */
486
    struct ChainSyncTimeoutState {
487
        //! A timeout used for checking whether our peer has sufficiently synced
488
        std::chrono::seconds m_timeout{0s};
489
        //! A header with the work we require on our peer's chain
490
        const CBlockIndex* m_work_header{nullptr};
491
        //! After timeout is reached, set to true after sending getheaders
492
        bool m_sent_getheaders{false};
493
        //! Whether this peer is protected from disconnection due to a bad/slow chain
494
        bool m_protect{false};
495
    };
496
497
    ChainSyncTimeoutState m_chain_sync;
498
499
    //! Time of last new block announcement
500
    int64_t m_last_block_announcement{0};
501
};
502
503
struct InvToSendBucket {
504
    const double count_floor{0};
505
    std::vector<Wtxid> backlog;
506
    util::TokenBucket<NodeClock> size_bucket;
507
    util::TokenBucket<NodeClock> count_bucket;
508
509
    /* Initialization rationale:
510
     *
511
     * Count bucket: Fills at rate*mult, total/initial capacity of 30s with mult=1
512
     * Size bucket: Fills at 12MB every 600s, times mult so expected to be 6 times
513
     *   the rate at which blocks can confirm transactions, but at least 3 times that in
514
     *   the worst case. High limit to avoid triggering even with large spikes, but a
515
     *   modest initial value to ensure that frequent node restarts don't raise the limit
516
     *   too much.
517
     * Count floor: In order to avoid sorting the global backlog too often, we ensure
518
     *   that we always remove at least an average INV message's number of transactions
519
     *   each time we do work. (Or 50kB if the size bucket is the limiting factor)
520
     */
521
522
    static constexpr double SIZE_INIT{12'000'000}; // 12 MB initially
523
    static constexpr double SIZE_CAP{50'000'000}; // 50 MB maximum
524
    static constexpr double SIZE_REFILL{20'000}; // 20kB/s = 12MB/600s
525
526
    static constexpr double INBOUND_COUNT_SECONDS{30}; // cap/initial at 30s/mult worth of txs
527
528
    InvToSendBucket(unsigned int rate, double mult)
529
2.41k
        : count_floor{-1.0 * rate * count_seconds(INBOUND_INVENTORY_BROADCAST_INTERVAL)},
530
2.41k
          size_bucket(/*rate=*/SIZE_REFILL * mult, /*value=*/SIZE_INIT, /*cap=*/SIZE_CAP),
531
2.41k
          count_bucket(/*rate=*/rate * mult, /*value=*/rate * INBOUND_COUNT_SECONDS, /*cap=*/rate * INBOUND_COUNT_SECONDS)
532
2.41k
    {
533
2.41k
    }
534
535
    bool avail() const
536
156k
    {
537
156k
        return !backlog.empty() && size_bucket.value() > 0 && count_bucket.value() > 0;
538
156k
    }
539
540
    void increment(NodeClock::time_point now)
541
156k
    {
542
156k
        size_bucket.increment(now);
543
156k
        count_bucket.increment(now);
544
156k
    }
545
546
    std::vector<Wtxid> TakeForProcessing(CTxMemPool& mempool) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);
547
548
    bool decrement(double size)
549
54.1k
    {
550
54.1k
        bool size_ok = size_bucket.decrement(size, /*floor=*/-50e3);
551
54.1k
        bool count_ok = count_bucket.decrement(1, /*floor=*/count_floor);
552
54.1k
        return size_ok && count_ok;
553
54.1k
    }
554
555
    PeerManagerInfo::InvBucketInfo info() const
556
1.88k
    {
557
1.88k
        return {
558
1.88k
            .backlog_count = backlog.size(),
559
1.88k
            .count_bucket = count_bucket.value(),
560
1.88k
            .size_bucket = size_bucket.value(),
561
1.88k
        };
562
1.88k
    }
563
};
564
565
class PeerManagerImpl final : public PeerManager
566
{
567
public:
568
    PeerManagerImpl(CConnman& connman, AddrMan& addrman,
569
                    BanMan* banman, ChainstateManager& chainman,
570
                    CTxMemPool& pool, node::Warnings& warnings, Options opts);
571
572
    /** Overridden from CValidationInterface. */
573
    void ActiveTipChange(const CBlockIndex& new_tip, bool) override
574
        EXCLUSIVE_LOCKS_REQUIRED(!m_tx_download_mutex);
575
    void BlockConnected(const ChainstateRole& role, const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected) override
576
        EXCLUSIVE_LOCKS_REQUIRED(!m_tx_download_mutex);
577
    void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex) override
578
        EXCLUSIVE_LOCKS_REQUIRED(!m_tx_download_mutex);
579
    void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override
580
        EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
581
    void BlockChecked(const std::shared_ptr<const CBlock>& block, const BlockValidationState& state) override
582
        EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
583
    void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) override
584
        EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex);
585
586
    /** Implement NetEventsInterface */
587
    void InitializeNode(const CNode& node, ServiceFlags our_services) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_tx_download_mutex);
588
    void FinalizeNode(const CNode& node) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_headers_presync_mutex, !m_tx_download_mutex);
589
    bool HasAllDesirableServiceFlags(ServiceFlags services) const override;
590
    bool ProcessMessages(CNode& node, std::atomic<bool>& interrupt) override
591
        EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_most_recent_block_mutex, !m_headers_presync_mutex, g_msgproc_mutex, !m_tx_download_mutex, !m_inv_to_send_mutex);
592
    bool SendMessages(CNode& node) override
593
        EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_most_recent_block_mutex, g_msgproc_mutex, !m_tx_download_mutex, !m_inv_to_send_mutex);
594
595
    /** Implement PeerManager */
596
    void StartScheduledTasks(CScheduler& scheduler) override;
597
    void CheckForStaleTipAndEvictPeers() override;
598
    util::Expected<void, std::string> FetchBlock(NodeId peer_id, const CBlockIndex& block_index) override
599
        EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
600
    bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
601
    std::vector<node::TxOrphanage::OrphanInfo> GetOrphanTransactions() override EXCLUSIVE_LOCKS_REQUIRED(!m_tx_download_mutex);
602
    PeerManagerInfo GetInfo() const override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_inv_to_send_mutex);
603
    std::vector<PrivateBroadcast::TxBroadcastInfo> GetPrivateBroadcastInfo() const override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
604
    std::vector<CTransactionRef> AbortPrivateBroadcast(const uint256& id) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
605
    void SendPings() override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
606
    void InitiateTxBroadcastToAll(const Wtxid& wtxid) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_inv_to_send_mutex);
607
    node::TransactionError InitiateTxBroadcastPrivate(const CTransactionRef& tx) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
608
    void SetBestBlock(int height, std::chrono::seconds time) override
609
92.3k
    {
610
92.3k
        m_best_height = height;
611
92.3k
        m_best_block_time = time;
612
92.3k
    };
613
4
    void UnitTestMisbehaving(NodeId peer_id) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex) { Misbehaving(*Assert(GetPeerRef(peer_id)), ""); };
614
    void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) override;
615
    ServiceFlags GetDesirableServiceFlags(ServiceFlags services) const override;
616
617
private:
618
    void ProcessMessage(Peer& peer, CNode& pfrom, const std::string& msg_type, DataStream& vRecv, NodeClock::time_point time_received,
619
                        const std::atomic<bool>& interruptMsgProc)
620
        EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_most_recent_block_mutex, !m_headers_presync_mutex, g_msgproc_mutex, !m_tx_download_mutex, !m_inv_to_send_mutex);
621
622
    /** Consider evicting an outbound peer based on the amount of time they've been behind our tip */
623
    void ConsiderEviction(CNode& pto, Peer& peer, std::chrono::seconds time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main, g_msgproc_mutex);
624
625
    /** If we have extra outbound peers, try to disconnect the one with the oldest block announcement */
626
    void EvictExtraOutboundPeers(NodeClock::time_point now) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
627
628
    /** Retrieve unbroadcast transactions from the mempool and reattempt sending to peers */
629
    void ReattemptInitialBroadcast(CScheduler& scheduler) EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_inv_to_send_mutex);
630
631
    /** Rebroadcast stale private transactions (already broadcast but not received back from the network). */
632
    void ReattemptPrivateBroadcast(CScheduler& scheduler);
633
634
    /** Get a shared pointer to the Peer object.
635
     *  May return an empty shared_ptr if the Peer object can't be found. */
636
    PeerRef GetPeerRef(NodeId id) const EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
637
638
    /** Get a shared pointer to the Peer object and remove it from m_peer_map.
639
     *  May return an empty shared_ptr if the Peer object can't be found. */
640
    PeerRef RemovePeer(NodeId id) EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
641
642
    /// Get all existing peers in m_peer_map.
643
    std::vector<PeerRef> GetAllPeers() const EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
644
645
    /** Mark a peer as misbehaving, which will cause it to be disconnected and its
646
     *  address discouraged. */
647
    void Misbehaving(Peer& peer, const std::string& message);
648
649
    /**
650
     * Potentially mark a node discouraged based on the contents of a BlockValidationState object
651
     *
652
     * @param[in] via_compact_block this bool is passed in because net_processing should
653
     * punish peers differently depending on whether the data was provided in a compact
654
     * block message or not. If the compact block had a valid header, but contained invalid
655
     * txs, the peer should not be punished. See BIP 152.
656
     */
657
    void MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationState& state,
658
                                 bool via_compact_block, const std::string& message = "")
659
        EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
660
661
    /** Maybe disconnect a peer and discourage future connections from its address.
662
     *
663
     * @param[in]   pnode     The node to check.
664
     * @param[in]   peer      The peer object to check.
665
     * @return                True if the peer was marked for disconnection in this function
666
     */
667
    bool MaybeDiscourageAndDisconnect(CNode& pnode, Peer& peer);
668
669
    /** If an inbound peer wants tx relay and we are at capacity for those, attempt to
670
     *  evict a tx-relaying inbound peer - possibly node itself, unless it is protected.
671
     *  Only if no peer can be evicted, disconnect node.
672
     *
673
     * @param[in]   node          The node that wants to relay txs to us.
674
     * @param[in]   msg_type      The message that triggered this check, for logging.
675
     * @param[in]   protect_peer  Peer that is exempt from being evicted.
676
     * @return                    True if the node was disconnected because no eviction candidate
677
     *                            was found. If false is returned, a non-protected node may still have
678
     *                            been marked for disconnection via regular eviction.
679
     */
680
    bool MaybeDisconnectForTxRelayCapacity(CNode& node, const std::string& msg_type,
681
                                           std::optional<NodeId> protect_peer = std::nullopt);
682
683
    /** Handle a transaction whose result was not MempoolAcceptResult::ResultType::VALID.
684
     * @param[in]   first_time_failure            Whether we should consider inserting into vExtraTxnForCompact, adding
685
     *                                            a new orphan to resolve, or looking for a package to submit.
686
     *                                            Set to true for transactions just received over p2p.
687
     *                                            Set to false if the tx has already been rejected before,
688
     *                                            e.g. is already in the orphanage, to avoid adding duplicate entries.
689
     * Updates m_txrequest, m_lazy_recent_rejects, m_lazy_recent_rejects_reconsiderable, m_orphanage, and vExtraTxnForCompact.
690
     *
691
     * @returns a PackageToValidate if this transaction has a reconsiderable failure and an eligible package was found,
692
     * or std::nullopt otherwise.
693
     */
694
    std::optional<node::PackageToValidate> ProcessInvalidTx(NodeId nodeid, const CTransactionRef& tx, const TxValidationState& result,
695
                                                      bool first_time_failure)
696
        EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, g_msgproc_mutex, m_tx_download_mutex);
697
698
    /** Handle a transaction whose result was MempoolAcceptResult::ResultType::VALID.
699
     * Updates m_txrequest, m_orphanage, and vExtraTxnForCompact. Also queues the tx for relay. */
700
    void ProcessValidTx(NodeId nodeid, const CTransactionRef& tx, const std::list<CTransactionRef>& replaced_transactions)
701
        EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, g_msgproc_mutex, m_tx_download_mutex, !m_inv_to_send_mutex);
702
703
    /** Handle the results of package validation: calls ProcessValidTx and ProcessInvalidTx for
704
     * individual transactions, and caches rejection for the package as a group.
705
     */
706
    void ProcessPackageResult(const node::PackageToValidate& package_to_validate, const PackageMempoolAcceptResult& package_result)
707
        EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, g_msgproc_mutex, m_tx_download_mutex, !m_inv_to_send_mutex);
708
709
    /**
710
     * Reconsider orphan transactions after a parent has been accepted to the mempool.
711
     *
712
     * @peer[in]  peer     The peer whose orphan transactions we will reconsider. Generally only
713
     *                     one orphan will be reconsidered on each call of this function. If an
714
     *                     accepted orphan has orphaned children, those will need to be
715
     *                     reconsidered, creating more work, possibly for other peers.
716
     * @return             True if meaningful work was done (an orphan was accepted/rejected).
717
     *                     If no meaningful work was done, then the work set for this peer
718
     *                     will be empty.
719
     */
720
    bool ProcessOrphanTx(Peer& peer)
721
        EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, g_msgproc_mutex, !m_tx_download_mutex, !m_inv_to_send_mutex);
722
723
    /** Process a single headers message from a peer.
724
     *
725
     * @param[in]   pfrom     CNode of the peer
726
     * @param[in]   peer      The peer sending us the headers
727
     * @param[in]   headers   The headers received. Note that this may be modified within ProcessHeadersMessage.
728
     * @param[in]   via_compact_block   Whether this header came in via compact block handling.
729
    */
730
    void ProcessHeadersMessage(CNode& pfrom, Peer& peer,
731
                               std::vector<CBlockHeader>&& headers,
732
                               bool via_compact_block)
733
        EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_headers_presync_mutex, g_msgproc_mutex);
734
    /** Various helpers for headers processing, invoked by ProcessHeadersMessage() */
735
    /** Return true if headers are continuous and have valid proof-of-work (DoS points assigned on failure) */
736
    bool CheckHeadersPoW(const std::vector<CBlockHeader>& headers, Peer& peer);
737
    /** Calculate an anti-DoS work threshold for headers chains */
738
    arith_uint256 GetAntiDoSWorkThreshold();
739
    /** Deal with state tracking and headers sync for peers that send
740
     * non-connecting headers (this can happen due to BIP 130 headers
741
     * announcements for blocks interacting with the 2hr (MAX_FUTURE_BLOCK_TIME) rule). */
742
    void HandleUnconnectingHeaders(CNode& pfrom, Peer& peer, const std::vector<CBlockHeader>& headers) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
743
    /** Return true if the headers connect to each other, false otherwise */
744
    bool CheckHeadersAreContinuous(const std::vector<CBlockHeader>& headers) const;
745
    /** Try to continue a low-work headers sync that has already begun.
746
     * Assumes the caller has already verified the headers connect, and has
747
     * checked that each header satisfies the proof-of-work target included in
748
     * the header.
749
     *  @param[in]  peer                            The peer we're syncing with.
750
     *  @param[in]  pfrom                           CNode of the peer
751
     *  @param[in,out] headers                      The headers to be processed.
752
     *  @return     True if the passed in headers were successfully processed
753
     *              as the continuation of a low-work headers sync in progress;
754
     *              false otherwise.
755
     *              If false, the passed in headers will be returned back to
756
     *              the caller.
757
     *              If true, the returned headers may be empty, indicating
758
     *              there is no more work for the caller to do; or the headers
759
     *              may be populated with entries that have passed anti-DoS
760
     *              checks (and therefore may be validated for block index
761
     *              acceptance by the caller).
762
     */
763
    bool IsContinuationOfLowWorkHeadersSync(Peer& peer, CNode& pfrom,
764
            std::vector<CBlockHeader>& headers)
765
        EXCLUSIVE_LOCKS_REQUIRED(peer.m_headers_sync_mutex, !m_headers_presync_mutex, g_msgproc_mutex);
766
    /** Check work on a headers chain to be processed, and if insufficient,
767
     * initiate our anti-DoS headers sync mechanism.
768
     *
769
     * @param[in]   peer                The peer whose headers we're processing.
770
     * @param[in]   pfrom               CNode of the peer
771
     * @param[in]   chain_start_header  Where these headers connect in our index.
772
     * @param[in,out]   headers             The headers to be processed.
773
     *
774
     * @return      True if chain was low work (headers will be empty after
775
     *              calling); false otherwise.
776
     */
777
    bool TryLowWorkHeadersSync(Peer& peer, CNode& pfrom,
778
                               const CBlockIndex& chain_start_header,
779
                               std::vector<CBlockHeader>& headers)
780
        EXCLUSIVE_LOCKS_REQUIRED(!peer.m_headers_sync_mutex, !m_peer_mutex, !m_headers_presync_mutex, g_msgproc_mutex);
781
782
    /** Return true if the given header is an ancestor of
783
     *  m_chainman.m_best_header or our current tip */
784
    bool IsAncestorOfBestHeaderOrTip(const CBlockIndex* header) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
785
786
    /** Request further headers from this peer with a given locator.
787
     * We don't issue a getheaders message if we have a recent one outstanding.
788
     * This returns true if a getheaders is actually sent, and false otherwise.
789
     */
790
    bool MaybeSendGetHeaders(CNode& pfrom, const CBlockLocator& locator, Peer& peer) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
791
    /** Potentially fetch blocks from this peer upon receipt of a new headers tip */
792
    void HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex& last_header);
793
    /** Update peer state based on received headers message */
794
    void UpdatePeerStateForReceivedHeaders(CNode& pfrom, Peer& peer, const CBlockIndex& last_header, bool received_new_header, bool may_have_more_headers)
795
        EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
796
797
    void SendBlockTransactions(CNode& pfrom, Peer& peer, const CBlock& block, const BlockTransactionsRequest& req);
798
799
    /** Send a message to a peer */
800
20.0k
    void PushMessage(CNode& node, CSerializedNetMsg&& msg) const { m_connman.PushMessage(&node, std::move(msg)); }
801
    template <typename... Args>
802
    void MakeAndPushMessage(CNode& node, std::string msg_type, Args&&... args) const
803
146k
    {
804
146k
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
146k
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<bool, unsigned long const&>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool&&, unsigned long const&) const
Line
Count
Source
803
1.89k
    {
804
1.89k
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
1.89k
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<std::vector<CInv, std::allocator<CInv>>&>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::vector<CInv, std::allocator<CInv>>&) const
Line
Count
Source
803
61.4k
    {
804
61.4k
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
61.4k
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<ParamsWrapper<TransactionSerParams, CTransaction const>>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, ParamsWrapper<TransactionSerParams, CTransaction const>&&) const
Line
Count
Source
803
13.1k
    {
804
13.1k
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
13.1k
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<std::span<std::byte const, 18446744073709551615ul>>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::span<std::byte const, 18446744073709551615ul>&&) const
Line
Count
Source
803
28.4k
    {
804
28.4k
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
28.4k
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<ParamsWrapper<TransactionSerParams, CBlock const>>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, ParamsWrapper<TransactionSerParams, CBlock const>&&) const
Line
Count
Source
803
8.31k
    {
804
8.31k
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
8.31k
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<CMerkleBlock&>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CMerkleBlock&) const
Line
Count
Source
803
4
    {
804
4
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
4
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<CBlockHeaderAndShortTxIDs const&>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CBlockHeaderAndShortTxIDs const&) const
Line
Count
Source
803
185
    {
804
185
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
185
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<CBlockHeaderAndShortTxIDs&>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CBlockHeaderAndShortTxIDs&) const
Line
Count
Source
803
2.36k
    {
804
2.36k
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
2.36k
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<int, unsigned long&, long&, unsigned long&, ParamsWrapper<CNetAddr::SerParams, CService>, unsigned long&, ParamsWrapper<CNetAddr::SerParams, CService>, unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, int&, bool&>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int&&, unsigned long&, long&, unsigned long&, ParamsWrapper<CNetAddr::SerParams, CService>&&, unsigned long&, ParamsWrapper<CNetAddr::SerParams, CService>&&, unsigned long&&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, int&, bool&) const
Line
Count
Source
803
1.67k
    {
804
1.67k
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
1.67k
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>) const
Line
Count
Source
803
6.13k
    {
804
6.13k
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
6.13k
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<unsigned int const&, unsigned long const&>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned int const&, unsigned long const&) const
Line
Count
Source
803
8
    {
804
8
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
8
    }
Unexecuted instantiation: net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<std::array<std::byte, 168ul> const&>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::array<std::byte, 168ul> const&) const
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<std::vector<CInv, std::allocator<CInv>>>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::vector<CInv, std::allocator<CInv>>&&) const
Line
Count
Source
803
14
    {
804
14
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
14
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<CBlockLocator const&, uint256>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CBlockLocator const&, uint256&&) const
Line
Count
Source
803
2.97k
    {
804
2.97k
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
2.97k
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<BlockTransactions&>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, BlockTransactions&) const
Line
Count
Source
803
549
    {
804
549
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
549
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<std::vector<CBlockHeader, std::allocator<CBlockHeader>>>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::vector<CBlockHeader, std::allocator<CBlockHeader>>&&) const
Line
Count
Source
803
9
    {
804
9
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
9
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<ParamsWrapper<TransactionSerParams, std::vector<CBlock, std::allocator<CBlock>>>>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, ParamsWrapper<TransactionSerParams, std::vector<CBlock, std::allocator<CBlock>>>&&) const
Line
Count
Source
803
6.09k
    {
804
6.09k
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
6.09k
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<BlockTransactionsRequest&>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, BlockTransactionsRequest&) const
Line
Count
Source
803
539
    {
804
539
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
539
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<unsigned long&>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned long&) const
Line
Count
Source
803
10.3k
    {
804
10.3k
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
10.3k
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<BlockFilter const&>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, BlockFilter const&) const
Line
Count
Source
803
11
    {
804
11
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
11
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<unsigned char&, uint256, uint256&, std::vector<uint256, std::allocator<uint256>>&>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned char&, uint256&&, uint256&, std::vector<uint256, std::allocator<uint256>>&) const
Line
Count
Source
803
2
    {
804
2
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
2
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<unsigned char&, uint256, std::vector<uint256, std::allocator<uint256>>&>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned char&, uint256&&, std::vector<uint256, std::allocator<uint256>>&) const
Line
Count
Source
803
3
    {
804
3
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
3
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<ParamsWrapper<CAddress::SerParams, std::vector<CAddress, std::allocator<CAddress>>>>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, ParamsWrapper<CAddress::SerParams, std::vector<CAddress, std::allocator<CAddress>>>&&) const
Line
Count
Source
803
130
    {
804
130
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
130
    }
net_processing.cpp:void (anonymous namespace)::PeerManagerImpl::MakeAndPushMessage<long&>(CNode&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, long&) const
Line
Count
Source
803
1.74k
    {
804
1.74k
        m_connman.PushMessage(&node, NetMsg::Make(std::move(msg_type), std::forward<Args>(args)...));
805
1.74k
    }
806
    template <typename... Args>
807
    void MakeAndPushFeature(CNode& node, std::string_view feature_id, Args&&... args) const
808
    {
809
        if (!Assume(feature_id.size() >= 4 && feature_id.size() <= MAX_FEATUREID_LENGTH)) return;
810
        std::vector<unsigned char> feature_data;
811
        VectorWriter{feature_data, 0, std::forward<Args>(args)...};
812
        if (!Assume(feature_data.size() <= MAX_FEATUREDATA_LENGTH)) return;
813
        MakeAndPushMessage(node, NetMsgType::FEATURE, feature_id, std::move(feature_data));
814
    }
815
816
    /** Send a version message to a peer */
817
    void PushNodeVersion(CNode& pnode, const Peer& peer);
818
819
    /** Send a ping message every PING_INTERVAL or if requested via RPC (peer.m_ping_queued is true).
820
     *  May mark the peer to be disconnected if a ping has timed out.
821
     *  We use mockable time for ping timeouts, so setmocktime may cause pings
822
     *  to time out. */
823
    void MaybeSendPing(CNode& node_to, Peer& peer, NodeClock::time_point now);
824
825
    /** Send `addr` messages on a regular schedule. */
826
    void MaybeSendAddr(CNode& node, Peer& peer, std::chrono::microseconds current_time) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
827
828
    /** Send a single `sendheaders` message, after we have completed headers sync with a peer. */
829
    void MaybeSendSendHeaders(CNode& node, Peer& peer) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
830
831
    /** Relay (gossip) an address to a few randomly chosen nodes.
832
     *
833
     * @param[in] originator   The id of the peer that sent us the address. We don't want to relay it back.
834
     * @param[in] addr         Address to relay.
835
     * @param[in] fReachable   Whether the address' network is reachable. We relay unreachable
836
     *                         addresses less.
837
     */
838
    void RelayAddress(NodeId originator, const CAddress& addr, bool fReachable) EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, g_msgproc_mutex);
839
840
    /** Send `feefilter` message. */
841
    void MaybeSendFeefilter(CNode& node, Peer& peer, std::chrono::microseconds current_time) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
842
843
    FastRandomContext m_rng GUARDED_BY(NetEventsInterface::g_msgproc_mutex);
844
845
    FeeFilterRounder m_fee_filter_rounder GUARDED_BY(NetEventsInterface::g_msgproc_mutex);
846
847
    const CChainParams& m_chainparams;
848
    CConnman& m_connman;
849
    AddrMan& m_addrman;
850
    /** Pointer to this node's banman. May be nullptr - check existence before dereferencing. */
851
    BanMan* const m_banman;
852
    ChainstateManager& m_chainman;
853
    CTxMemPool& m_mempool;
854
855
    /** Synchronizes tx download including TxRequestTracker, rejection filters, and TxOrphanage.
856
     * Lock invariants:
857
     * - A txhash (txid or wtxid) in m_txrequest is not also in m_orphanage.
858
     * - A txhash (txid or wtxid) in m_txrequest is not also in m_lazy_recent_rejects.
859
     * - A txhash (txid or wtxid) in m_txrequest is not also in m_lazy_recent_rejects_reconsiderable.
860
     * - A txhash (txid or wtxid) in m_txrequest is not also in m_lazy_recent_confirmed_transactions.
861
     * - Each data structure's limits hold (m_orphanage max size, m_txrequest per-peer limits, etc).
862
     */
863
    Mutex m_tx_download_mutex ACQUIRED_BEFORE(m_mempool.cs);
864
    node::TxDownloadManager m_txdownloadman GUARDED_BY(m_tx_download_mutex);
865
866
    std::unique_ptr<TxReconciliationTracker> m_txreconciliation;
867
868
    /** The height of the best chain */
869
    std::atomic<int> m_best_height{-1};
870
    /** The time of the best chain tip block */
871
    std::atomic<std::chrono::seconds> m_best_block_time{0s};
872
873
    /** Next time to check for stale tip */
874
    std::chrono::seconds m_stale_tip_check_time GUARDED_BY(cs_main){0s};
875
876
    node::Warnings& m_warnings;
877
    TimeOffsets m_outbound_time_offsets{m_warnings};
878
879
    const Options m_opts;
880
881
    bool RejectIncomingTxs(const CNode& peer) const;
882
883
    /** Whether we've completed initial sync yet, for determining when to turn
884
      * on extra block-relay-only peers. */
885
    bool m_initial_sync_finished GUARDED_BY(cs_main){false};
886
887
    /** Protects m_peer_map. This mutex must not be locked while holding a lock
888
     *  on any of the mutexes inside a Peer object. */
889
    mutable Mutex m_peer_mutex;
890
    /**
891
     * Map of all Peer objects, keyed by peer id. This map is protected
892
     * by the m_peer_mutex. Once a shared pointer reference is
893
     * taken, the lock may be released. Individual fields are protected by
894
     * their own locks.
895
     */
896
    std::map<NodeId, PeerRef> m_peer_map GUARDED_BY(m_peer_mutex);
897
898
    /** Map maintaining per-node state. */
899
    std::map<NodeId, CNodeState> m_node_states GUARDED_BY(cs_main);
900
901
    /** Get a pointer to a const CNodeState, used when not mutating the CNodeState object. */
902
    const CNodeState* State(NodeId pnode) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
903
    /** Get a pointer to a mutable CNodeState. */
904
    CNodeState* State(NodeId pnode) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
905
906
    uint32_t GetFetchFlags(const Peer& peer) const;
907
908
    std::map<uint64_t, std::chrono::microseconds> m_next_inv_to_inbounds_per_network_key GUARDED_BY(g_msgproc_mutex);
909
910
    /** Number of nodes with fSyncStarted. */
911
    int nSyncStarted GUARDED_BY(cs_main) = 0;
912
913
    /** Hash of the last block we received via INV */
914
    uint256 m_last_block_inv_triggering_headers_sync GUARDED_BY(g_msgproc_mutex){};
915
916
    /**
917
     * Sources of received blocks, saved to be able punish them when processing
918
     * happens afterwards.
919
     * Set mapBlockSource[hash].second to false if the node should not be
920
     * punished if the block is invalid.
921
     */
922
    std::map<uint256, std::pair<NodeId, bool>> mapBlockSource GUARDED_BY(cs_main);
923
924
    /** Number of peers with wtxid relay. */
925
    std::atomic<int> m_wtxid_relay_peers{0};
926
927
    /** Number of outbound peers with m_chain_sync.m_protect. */
928
    int m_outbound_peers_with_protect_from_disconnect GUARDED_BY(cs_main) = 0;
929
930
    /** Number of preferable block download peers. */
931
    int m_num_preferred_download_peers GUARDED_BY(cs_main){0};
932
933
    /** Stalling timeout for blocks in IBD */
934
    std::atomic<std::chrono::seconds> m_block_stalling_timeout{BLOCK_STALLING_TIMEOUT_DEFAULT};
935
936
    /**
937
     * For sending `inv`s to inbound peers, we use a single (exponentially
938
     * distributed) timer for all peers with the same network key. If we used a separate timer for each
939
     * peer, a spy node could make multiple inbound connections to us to
940
     * accurately determine when we received a transaction (and potentially
941
     * determine the transaction's origin). Each network key has its own timer
942
     * to make fingerprinting harder. */
943
    std::chrono::microseconds NextInvToInbounds(std::chrono::microseconds now,
944
                                                std::chrono::seconds average_interval,
945
                                                uint64_t network_key) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
946
947
948
    // All of the following cache a recent block, and are protected by m_most_recent_block_mutex
949
    Mutex m_most_recent_block_mutex;
950
    std::shared_ptr<const CBlock> m_most_recent_block GUARDED_BY(m_most_recent_block_mutex);
951
    std::shared_ptr<const CBlockHeaderAndShortTxIDs> m_most_recent_compact_block GUARDED_BY(m_most_recent_block_mutex);
952
    uint256 m_most_recent_block_hash GUARDED_BY(m_most_recent_block_mutex);
953
    std::unique_ptr<const std::map<GenTxid, CTransactionRef>> m_most_recent_block_txs GUARDED_BY(m_most_recent_block_mutex);
954
955
    // Data about the low-work headers synchronization, aggregated from all peers' HeadersSyncStates.
956
    /** Mutex guarding the other m_headers_presync_* variables. */
957
    Mutex m_headers_presync_mutex;
958
    /** A type to represent statistics about a peer's low-work headers sync.
959
     *
960
     * - The first field is the total verified amount of work in that synchronization.
961
     * - The second is:
962
     *   - nullopt: the sync is in REDOWNLOAD phase (phase 2).
963
     *   - {height, timestamp}: the sync has the specified tip height and block timestamp (phase 1).
964
     */
965
    using HeadersPresyncStats = std::pair<arith_uint256, std::optional<std::pair<int64_t, uint32_t>>>;
966
    /** Statistics for all peers in low-work headers sync. */
967
    std::map<NodeId, HeadersPresyncStats> m_headers_presync_stats GUARDED_BY(m_headers_presync_mutex) {};
968
    /** The peer with the most-work entry in m_headers_presync_stats. */
969
    NodeId m_headers_presync_bestpeer GUARDED_BY(m_headers_presync_mutex) {-1};
970
    /** The m_headers_presync_stats improved, and needs signalling. */
971
    std::atomic_bool m_headers_presync_should_signal{false};
972
973
    /** Height of the highest block announced using BIP 152 high-bandwidth mode. */
974
    int m_highest_fast_announce GUARDED_BY(::cs_main){0};
975
976
    /** Have we requested this block from a peer */
977
    bool IsBlockRequested(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
978
979
    /** Have we requested this block from an outbound peer */
980
    bool IsBlockRequestedFromOutbound(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main, !m_peer_mutex);
981
982
    /** Remove this block from our tracked requested blocks. Called if:
983
     *  - the block has been received from a peer
984
     *  - the request for the block has timed out
985
     * If "from_peer" is specified, then only remove the block if it is in
986
     * flight from that peer (to avoid one peer's network traffic from
987
     * affecting another's state).
988
     */
989
    void RemoveBlockRequest(const uint256& hash, std::optional<NodeId> from_peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
990
991
    /* Mark a block as in flight
992
     * Returns false, still setting pit, if the block was already in flight from the same peer
993
     * pit will only be valid as long as the same cs_main lock is being held
994
     */
995
    bool BlockRequested(NodeId nodeid, const CBlockIndex& block, std::list<QueuedBlock>::iterator** pit = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
996
997
    bool TipMayBeStale() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
998
999
    /** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
1000
     *  at most count entries.
1001
     */
1002
    void FindNextBlocksToDownload(const Peer& peer, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1003
1004
    /** Request blocks for the background chainstate, if one is in use. */
1005
    void TryDownloadingHistoricalBlocks(const Peer& peer, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, const CBlockIndex* from_tip, const CBlockIndex* target_block) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1006
1007
    /**
1008
    * \brief Find next blocks to download from a peer after a starting block.
1009
    *
1010
    * \param vBlocks      Vector of blocks to download which will be appended to.
1011
    * \param peer         Peer which blocks will be downloaded from.
1012
    * \param state        Pointer to the state of the peer.
1013
    * \param pindexWalk   Pointer to the starting block to add to vBlocks.
1014
    * \param count        Maximum number of blocks to allow in vBlocks. No more
1015
    *                     blocks will be added if it reaches this size.
1016
    * \param nWindowEnd   Maximum height of blocks to allow in vBlocks. No
1017
    *                     blocks will be added above this height.
1018
    * \param activeChain  Optional pointer to a chain to compare against. If
1019
    *                     provided, any next blocks which are already contained
1020
    *                     in this chain will not be appended to vBlocks, but
1021
    *                     instead will be used to update the
1022
    *                     state->pindexLastCommonBlock pointer.
1023
    * \param nodeStaller  Optional pointer to a NodeId variable that will receive
1024
    *                     the ID of another peer that might be causing this peer
1025
    *                     to stall. This is set to the ID of the peer which
1026
    *                     first requested the first in-flight block in the
1027
    *                     download window. It is only set if vBlocks is empty at
1028
    *                     the end of this function call and if increasing
1029
    *                     nWindowEnd by 1 would cause it to be non-empty (which
1030
    *                     indicates the download might be stalled because every
1031
    *                     block in the window is in flight and no other peer is
1032
    *                     trying to download the next block).
1033
    */
1034
    void FindNextBlocks(std::vector<const CBlockIndex*>& vBlocks, const Peer& peer, CNodeState *state, const CBlockIndex *pindexWalk, unsigned int count, int nWindowEnd, const CChain* activeChain=nullptr, NodeId* nodeStaller=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1035
1036
    /* Multimap used to preserve insertion order */
1037
    typedef std::multimap<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator>> BlockDownloadMap;
1038
    BlockDownloadMap mapBlocksInFlight GUARDED_BY(cs_main);
1039
1040
    /** When our tip was last updated. */
1041
    std::atomic<std::chrono::seconds> m_last_tip_update{0s};
1042
1043
    /** Determine whether or not a peer can request a transaction, and return it (or nullptr if not found or not allowed). */
1044
    CTransactionRef FindTxForGetData(const Peer::TxRelay& tx_relay, const GenTxid& gtxid)
1045
        EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex, !tx_relay.m_tx_inventory_mutex);
1046
1047
    void ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic<bool>& interruptMsgProc)
1048
        EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex, peer.m_getdata_requests_mutex, NetEventsInterface::g_msgproc_mutex)
1049
        LOCKS_EXCLUDED(::cs_main);
1050
1051
    /** Process a new block. Perform any post-processing housekeeping */
1052
    void ProcessBlock(CNode& node, const std::shared_ptr<const CBlock>& block, bool force_processing, bool min_pow_checked);
1053
1054
    /** Process compact block txns  */
1055
    void ProcessCompactBlockTxns(CNode& pfrom, Peer& peer, const BlockTransactions& block_transactions)
1056
        EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex, !m_most_recent_block_mutex);
1057
1058
    /**
1059
     * Schedule an INV for a transaction to be sent to the given peer (via `PushMessage()`).
1060
     * The transaction is picked from the list of transactions for private broadcast.
1061
     * It is assumed that the connection to the peer is `ConnectionType::PRIVATE_BROADCAST`.
1062
     * Avoid calling this for other peers since it will degrade privacy.
1063
     */
1064
    void PushPrivateBroadcastTx(CNode& node) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex, !m_most_recent_block_mutex);
1065
1066
    /**
1067
     * When a peer sends us a valid block, instruct it to announce blocks to us
1068
     * using CMPCTBLOCK if possible by adding its nodeid to the end of
1069
     * lNodesAnnouncingHeaderAndIDs, and keeping that list under a certain size by
1070
     * removing the first element if necessary.
1071
     */
1072
    void MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid) EXCLUSIVE_LOCKS_REQUIRED(cs_main, !m_peer_mutex);
1073
1074
    /** Stack of nodes which we have set to announce using compact blocks */
1075
    std::list<NodeId> lNodesAnnouncingHeaderAndIDs GUARDED_BY(cs_main);
1076
1077
    /** Number of peers from which we're downloading blocks. */
1078
    int m_peers_downloading_from GUARDED_BY(cs_main) = 0;
1079
1080
    void AddToCompactExtraTransactions(const CTransactionRef& tx) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
1081
1082
    /** Orphan/conflicted/etc transactions that are kept for compact block reconstruction.
1083
     *  The last -blockreconstructionextratxn/DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN of
1084
     *  these are kept in a ring buffer */
1085
    std::vector<std::pair<Wtxid, CTransactionRef>> vExtraTxnForCompact GUARDED_BY(g_msgproc_mutex);
1086
    /** Offset into vExtraTxnForCompact to insert the next tx */
1087
    size_t vExtraTxnForCompactIt GUARDED_BY(g_msgproc_mutex) = 0;
1088
1089
    /** Check whether the last unknown block a peer advertised is not yet known. */
1090
    void ProcessBlockAvailability(NodeId nodeid) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1091
    /** Update tracking information about which blocks a peer is assumed to have. */
1092
    void UpdateBlockAvailability(NodeId nodeid, const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1093
    bool CanDirectFetch() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1094
1095
    /**
1096
     * Estimates the distance, in blocks, between the best-known block and the network chain tip.
1097
     * Utilizes the best-block time and the chainparams blocks spacing to approximate it.
1098
     */
1099
    int64_t ApproximateBestBlockDepth() const;
1100
1101
    /**
1102
     * To prevent fingerprinting attacks, only send blocks/headers outside of
1103
     * the active chain if they are no more than a month older (both in time,
1104
     * and in best equivalent proof of work) than the best header chain we know
1105
     * about and we fully-validated them at some point.
1106
     */
1107
    bool BlockRequestAllowed(const CBlockIndex& block_index) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1108
    bool AlreadyHaveBlock(const uint256& block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1109
    void ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv& inv)
1110
        EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex, !m_most_recent_block_mutex);
1111
1112
    /**
1113
     * Validation logic for compact filters request handling.
1114
     *
1115
     * May disconnect from the peer in the case of a bad request.
1116
     *
1117
     * @param[in]   node            The node that we received the request from
1118
     * @param[in]   peer            The peer that we received the request from
1119
     * @param[in]   filter_type     The filter type the request is for. Must be basic filters.
1120
     * @param[in]   start_height    The start height for the request
1121
     * @param[in]   stop_hash       The stop_hash for the request
1122
     * @param[in]   max_height_diff The maximum number of items permitted to request, as specified in BIP 157
1123
     * @param[out]  stop_index      The CBlockIndex for the stop_hash block, if the request can be serviced.
1124
     * @param[out]  filter_index    The filter index, if the request can be serviced.
1125
     * @return                      True if the request can be serviced.
1126
     */
1127
    bool PrepareBlockFilterRequest(CNode& node, Peer& peer,
1128
                                   BlockFilterType filter_type, uint32_t start_height,
1129
                                   const uint256& stop_hash, uint32_t max_height_diff,
1130
                                   const CBlockIndex*& stop_index,
1131
                                   BlockFilterIndex*& filter_index);
1132
1133
    /**
1134
     * Handle a cfilters request.
1135
     *
1136
     * May disconnect from the peer in the case of a bad request.
1137
     *
1138
     * @param[in]   node            The node that we received the request from
1139
     * @param[in]   peer            The peer that we received the request from
1140
     * @param[in]   vRecv           The raw message received
1141
     */
1142
    void ProcessGetCFilters(CNode& node, Peer& peer, DataStream& vRecv);
1143
1144
    /**
1145
     * Handle a cfheaders request.
1146
     *
1147
     * May disconnect from the peer in the case of a bad request.
1148
     *
1149
     * @param[in]   node            The node that we received the request from
1150
     * @param[in]   peer            The peer that we received the request from
1151
     * @param[in]   vRecv           The raw message received
1152
     */
1153
    void ProcessGetCFHeaders(CNode& node, Peer& peer, DataStream& vRecv);
1154
1155
    /**
1156
     * Handle a getcfcheckpt request.
1157
     *
1158
     * May disconnect from the peer in the case of a bad request.
1159
     *
1160
     * @param[in]   node            The node that we received the request from
1161
     * @param[in]   peer            The peer that we received the request from
1162
     * @param[in]   vRecv           The raw message received
1163
     */
1164
    void ProcessGetCFCheckPt(CNode& node, Peer& peer, DataStream& vRecv);
1165
1166
    void ProcessPong(CNode& pfrom, Peer& peer, NodeClock::time_point ping_end, DataStream& vRecv);
1167
1168
    /** Checks if address relay is permitted with peer. If needed, initializes
1169
     * the m_addr_known bloom filter and sets m_addr_relay_enabled to true.
1170
     *
1171
     *  @return   True if address relay is enabled with peer
1172
     *            False if address relay is disallowed
1173
     */
1174
    bool SetupAddressRelay(const CNode& node, Peer& peer) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
1175
1176
    void ProcessAddrs(std::string_view msg_type, CNode& pfrom, Peer& peer, std::vector<CAddress>&& vAddr, const std::atomic<bool>& interruptMsgProc)
1177
        EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex, !m_peer_mutex);
1178
1179
    void AddAddressKnown(Peer& peer, const CAddress& addr) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
1180
    void PushAddress(Peer& peer, const CAddress& addr) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
1181
1182
    void LogBlockHeader(const CBlockIndex& index, const CNode& peer, bool via_compact_block);
1183
1184
    /// The transactions to be broadcast privately.
1185
    PrivateBroadcast m_tx_for_private_broadcast;
1186
1187
    mutable Mutex m_inv_to_send_mutex ACQUIRED_BEFORE(m_mempool.cs);
1188
    InvToSendBucket m_inbound_inv_bucket GUARDED_BY(m_inv_to_send_mutex);
1189
    InvToSendBucket m_outbound_inv_bucket GUARDED_BY(m_inv_to_send_mutex);
1190
    std::atomic<NodeClock::time_point> m_next_inv_bucket_check{NodeClock::time_point::min()};
1191
    std::optional<NodeClock::time_point> m_next_inv_bucket_heartbeat GUARDED_BY(m_inv_to_send_mutex);
1192
1193
    void ProcessInvBacklog(NodeClock::time_point now, bool backlog_bumped=false) EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_inv_to_send_mutex);
1194
};
1195
1196
const CNodeState* PeerManagerImpl::State(NodeId pnode) const
1197
2.49M
{
1198
2.49M
    std::map<NodeId, CNodeState>::const_iterator it = m_node_states.find(pnode);
1199
2.49M
    if (it == m_node_states.end())
1200
292
        return nullptr;
1201
2.49M
    return &it->second;
1202
2.49M
}
1203
1204
CNodeState* PeerManagerImpl::State(NodeId pnode)
1205
2.47M
{
1206
2.47M
    return const_cast<CNodeState*>(std::as_const(*this).State(pnode));
1207
2.47M
}
1208
1209
/**
1210
 * Whether the peer supports the address. For example, a peer that does not
1211
 * implement BIP155 cannot receive Tor v3 addresses because it requires
1212
 * ADDRv2 (BIP155) encoding.
1213
 */
1214
static bool IsAddrCompatible(const Peer& peer, const CAddress& addr)
1215
19.5k
{
1216
19.5k
    return peer.m_wants_addrv2 || addr.IsAddrV1Compatible();
1217
19.5k
}
1218
1219
void PeerManagerImpl::AddAddressKnown(Peer& peer, const CAddress& addr)
1220
1.27k
{
1221
1.27k
    assert(peer.m_addr_known);
1222
1.27k
    peer.m_addr_known->insert(addr.GetKey());
1223
1.27k
}
1224
1225
void PeerManagerImpl::PushAddress(Peer& peer, const CAddress& addr)
1226
19.0k
{
1227
    // Known checking here is only to save space from duplicates.
1228
    // Before sending, we'll filter it again for known addresses that were
1229
    // added after addresses were pushed.
1230
19.0k
    assert(peer.m_addr_known);
1231
19.0k
    if (addr.IsValid() && !peer.m_addr_known->contains(addr.GetKey()) && IsAddrCompatible(peer, addr)) {
1232
19.0k
        if (peer.m_addrs_to_send.size() >= MAX_ADDR_TO_SEND) {
1233
0
            peer.m_addrs_to_send[m_rng.randrange(peer.m_addrs_to_send.size())] = addr;
1234
19.0k
        } else {
1235
19.0k
            peer.m_addrs_to_send.push_back(addr);
1236
19.0k
        }
1237
19.0k
    }
1238
19.0k
}
1239
1240
static void AddKnownTx(Peer& peer, const uint256& hash)
1241
43.5k
{
1242
43.5k
    auto tx_relay = peer.GetTxRelay();
1243
43.5k
    if (!tx_relay) return;
1244
1245
43.5k
    LOCK(tx_relay->m_tx_inventory_mutex);
1246
43.5k
    tx_relay->m_tx_inventory_known_filter.insert(hash);
1247
43.5k
}
1248
1249
/** Whether this peer can serve us blocks. */
1250
static bool CanServeBlocks(const Peer& peer)
1251
553k
{
1252
553k
    return peer.m_their_services & (NODE_NETWORK|NODE_NETWORK_LIMITED);
1253
553k
}
1254
1255
/** Whether this peer can only serve limited recent blocks (e.g. because
1256
 *  it prunes old blocks) */
1257
static bool IsLimitedPeer(const Peer& peer)
1258
420k
{
1259
420k
    return (!(peer.m_their_services & NODE_NETWORK) &&
1260
420k
             (peer.m_their_services & NODE_NETWORK_LIMITED));
1261
420k
}
1262
1263
/** Whether this peer can serve us witness data */
1264
static bool CanServeWitnesses(const Peer& peer)
1265
2.54M
{
1266
2.54M
    return peer.m_their_services & NODE_WITNESS;
1267
2.54M
}
1268
1269
std::chrono::microseconds PeerManagerImpl::NextInvToInbounds(std::chrono::microseconds now,
1270
                                                             std::chrono::seconds average_interval,
1271
                                                             uint64_t network_key)
1272
3.25k
{
1273
3.25k
    auto [it, inserted] = m_next_inv_to_inbounds_per_network_key.try_emplace(network_key, 0us);
1274
3.25k
    auto& timer{it->second};
1275
3.25k
    if (timer < now) {
1276
1.35k
        timer = now + m_rng.rand_exp_duration(average_interval);
1277
1.35k
    }
1278
3.25k
    return timer;
1279
3.25k
}
1280
1281
bool PeerManagerImpl::IsBlockRequested(const uint256& hash)
1282
751k
{
1283
751k
    return mapBlocksInFlight.contains(hash);
1284
751k
}
1285
1286
bool PeerManagerImpl::IsBlockRequestedFromOutbound(const uint256& hash)
1287
7
{
1288
24
    for (auto range = mapBlocksInFlight.equal_range(hash); range.first != range.second; range.first++) {
1289
17
        auto [nodeid, block_it] = range.first->second;
1290
17
        PeerRef peer{GetPeerRef(nodeid)};
1291
17
        if (peer && !peer->m_is_inbound) return true;
1292
17
    }
1293
1294
7
    return false;
1295
7
}
1296
1297
void PeerManagerImpl::RemoveBlockRequest(const uint256& hash, std::optional<NodeId> from_peer)
1298
165k
{
1299
165k
    auto range = mapBlocksInFlight.equal_range(hash);
1300
165k
    if (range.first == range.second) {
1301
        // Block was not requested from any peer
1302
110k
        return;
1303
110k
    }
1304
1305
    // We should not have requested too many of this block
1306
55.1k
    Assume(mapBlocksInFlight.count(hash) <= MAX_CMPCTBLOCKS_INFLIGHT_PER_BLOCK);
1307
1308
110k
    while (range.first != range.second) {
1309
55.4k
        const auto& [node_id, list_it]{range.first->second};
1310
1311
55.4k
        if (from_peer && *from_peer != node_id) {
1312
672
            range.first++;
1313
672
            continue;
1314
672
        }
1315
1316
54.8k
        CNodeState& state = *Assert(State(node_id));
1317
1318
54.8k
        if (state.vBlocksInFlight.begin() == list_it) {
1319
            // First block on the queue was received, update the start download time for the next one
1320
53.9k
            state.m_downloading_since = std::max(state.m_downloading_since, GetTime<std::chrono::microseconds>());
1321
53.9k
        }
1322
54.8k
        state.vBlocksInFlight.erase(list_it);
1323
1324
54.8k
        if (state.vBlocksInFlight.empty()) {
1325
            // Last validated block on the queue for this peer was received.
1326
19.0k
            m_peers_downloading_from--;
1327
19.0k
        }
1328
54.8k
        state.m_stalling_since = 0us;
1329
1330
54.8k
        range.first = mapBlocksInFlight.erase(range.first);
1331
54.8k
    }
1332
55.1k
}
1333
1334
bool PeerManagerImpl::BlockRequested(NodeId nodeid, const CBlockIndex& block, std::list<QueuedBlock>::iterator** pit)
1335
55.1k
{
1336
55.1k
    const uint256& hash{block.GetBlockHash()};
1337
1338
55.1k
    CNodeState *state = State(nodeid);
1339
55.1k
    assert(state != nullptr);
1340
1341
55.1k
    Assume(mapBlocksInFlight.count(hash) <= MAX_CMPCTBLOCKS_INFLIGHT_PER_BLOCK);
1342
1343
    // Short-circuit most stuff in case it is from the same node
1344
55.4k
    for (auto range = mapBlocksInFlight.equal_range(hash); range.first != range.second; range.first++) {
1345
577
        if (range.first->second.first == nodeid) {
1346
240
            if (pit) {
1347
240
                *pit = &range.first->second.second;
1348
240
            }
1349
240
            return false;
1350
240
        }
1351
577
    }
1352
1353
    // Make sure it's not being fetched already from same peer.
1354
54.9k
    RemoveBlockRequest(hash, nodeid);
1355
1356
54.9k
    std::list<QueuedBlock>::iterator it = state->vBlocksInFlight.insert(state->vBlocksInFlight.end(),
1357
54.9k
            {&block, std::unique_ptr<PartiallyDownloadedBlock>(pit ? new PartiallyDownloadedBlock(&m_mempool) : nullptr)});
1358
54.9k
    if (state->vBlocksInFlight.size() == 1) {
1359
        // We're starting a block download (batch) from this peer.
1360
19.1k
        state->m_downloading_since = GetTime<std::chrono::microseconds>();
1361
19.1k
        m_peers_downloading_from++;
1362
19.1k
    }
1363
54.9k
    auto itInFlight = mapBlocksInFlight.insert(std::make_pair(hash, std::make_pair(nodeid, it)));
1364
54.9k
    if (pit) {
1365
17.5k
        *pit = &itInFlight->second.second;
1366
17.5k
    }
1367
54.9k
    return true;
1368
55.1k
}
1369
1370
void PeerManagerImpl::MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid)
1371
20.7k
{
1372
20.7k
    AssertLockHeld(cs_main);
1373
1374
    // When in -blocksonly mode, never request high-bandwidth mode from peers. Our
1375
    // mempool will not contain the transactions necessary to reconstruct the
1376
    // compact block.
1377
20.7k
    if (m_opts.ignore_incoming_txs) return;
1378
1379
20.7k
    CNodeState* nodestate = State(nodeid);
1380
20.7k
    PeerRef peer{GetPeerRef(nodeid)};
1381
20.7k
    if (!nodestate || !nodestate->m_provides_cmpctblocks) {
1382
        // Don't request compact blocks if the peer has not signalled support
1383
1.97k
        return;
1384
1.97k
    }
1385
1386
18.7k
    int num_outbound_hb_peers = 0;
1387
23.0k
    for (std::list<NodeId>::iterator it = lNodesAnnouncingHeaderAndIDs.begin(); it != lNodesAnnouncingHeaderAndIDs.end(); it++) {
1388
22.6k
        if (*it == nodeid) {
1389
18.4k
            lNodesAnnouncingHeaderAndIDs.erase(it);
1390
18.4k
            lNodesAnnouncingHeaderAndIDs.push_back(nodeid);
1391
18.4k
            return;
1392
18.4k
        }
1393
4.27k
        PeerRef peer_ref{GetPeerRef(*it)};
1394
4.27k
        if (peer_ref && !peer_ref->m_is_inbound) ++num_outbound_hb_peers;
1395
4.27k
    }
1396
315
    if (peer && peer->m_is_inbound) {
1397
        // If we're adding an inbound HB peer, make sure we're not removing
1398
        // our last outbound HB peer in the process.
1399
142
        if (lNodesAnnouncingHeaderAndIDs.size() >= 3 && num_outbound_hb_peers == 1) {
1400
8
            PeerRef remove_peer{GetPeerRef(lNodesAnnouncingHeaderAndIDs.front())};
1401
8
            if (remove_peer && !remove_peer->m_is_inbound) {
1402
                // Put the HB outbound peer in the second slot, so that it
1403
                // doesn't get removed.
1404
3
                std::swap(lNodesAnnouncingHeaderAndIDs.front(), *std::next(lNodesAnnouncingHeaderAndIDs.begin()));
1405
3
            }
1406
8
        }
1407
142
    }
1408
315
    const bool nodeid_was_appended{m_connman.ForNode(nodeid, [this](CNode* pfrom) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
1409
315
        AssertLockHeld(::cs_main);
1410
315
        MakeAndPushMessage(*pfrom, NetMsgType::SENDCMPCT, /*high_bandwidth=*/true, /*version=*/CMPCTBLOCKS_VERSION);
1411
        // save BIP152 bandwidth state: we select peer to be high-bandwidth
1412
315
        pfrom->m_bip152_highbandwidth_to = true;
1413
315
        lNodesAnnouncingHeaderAndIDs.push_back(pfrom->GetId());
1414
315
        return true;
1415
315
    })};
1416
315
    if (nodeid_was_appended && lNodesAnnouncingHeaderAndIDs.size() > 3) {
1417
        // As per BIP152, we only get 3 of our peers to announce
1418
        // blocks using compact encodings.
1419
40
        m_connman.ForNode(lNodesAnnouncingHeaderAndIDs.front(), [this](CNode* pnodeStop) {
1420
11
            MakeAndPushMessage(*pnodeStop, NetMsgType::SENDCMPCT, /*high_bandwidth=*/false, /*version=*/CMPCTBLOCKS_VERSION);
1421
            // save BIP152 bandwidth state: we select peer to be low-bandwidth
1422
11
            pnodeStop->m_bip152_highbandwidth_to = false;
1423
11
            return true;
1424
11
        });
1425
40
        lNodesAnnouncingHeaderAndIDs.pop_front();
1426
40
    }
1427
315
}
1428
1429
bool PeerManagerImpl::TipMayBeStale()
1430
4
{
1431
4
    AssertLockHeld(cs_main);
1432
4
    const Consensus::Params& consensusParams = m_chainparams.GetConsensus();
1433
4
    if (m_last_tip_update.load() == 0s) {
1434
2
        m_last_tip_update = GetTime<std::chrono::seconds>();
1435
2
    }
1436
4
    return m_last_tip_update.load() < GetTime<std::chrono::seconds>() - std::chrono::seconds{consensusParams.nPowTargetSpacing * 3} && mapBlocksInFlight.empty();
1437
4
}
1438
1439
int64_t PeerManagerImpl::ApproximateBestBlockDepth() const
1440
818
{
1441
818
    return (GetTime<std::chrono::seconds>() - m_best_block_time.load()).count() / m_chainparams.GetConsensus().nPowTargetSpacing;
1442
818
}
1443
1444
bool PeerManagerImpl::CanDirectFetch()
1445
62.2k
{
1446
62.2k
    return m_chainman.ActiveChain().Tip()->Time() > NodeClock::now() - m_chainparams.GetConsensus().PowTargetSpacing() * 20;
1447
62.2k
}
1448
1449
static bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1450
124k
{
1451
124k
    if (state->pindexBestKnownBlock && pindex == state->pindexBestKnownBlock->GetAncestor(pindex->nHeight))
1452
39.2k
        return true;
1453
85.3k
    if (state->pindexBestHeaderSent && pindex == state->pindexBestHeaderSent->GetAncestor(pindex->nHeight))
1454
45.5k
        return true;
1455
39.7k
    return false;
1456
85.3k
}
1457
1458
866k
void PeerManagerImpl::ProcessBlockAvailability(NodeId nodeid) {
1459
866k
    CNodeState *state = State(nodeid);
1460
866k
    assert(state != nullptr);
1461
1462
866k
    if (!state->hashLastUnknownBlock.IsNull()) {
1463
6.72k
        const CBlockIndex* pindex = m_chainman.m_blockman.LookupBlockIndex(state->hashLastUnknownBlock);
1464
6.72k
        if (pindex && pindex->nChainWork > 0) {
1465
802
            if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork) {
1466
802
                state->pindexBestKnownBlock = pindex;
1467
802
            }
1468
802
            state->hashLastUnknownBlock.SetNull();
1469
802
        }
1470
6.72k
    }
1471
866k
}
1472
1473
30.0k
void PeerManagerImpl::UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
1474
30.0k
    CNodeState *state = State(nodeid);
1475
30.0k
    assert(state != nullptr);
1476
1477
30.0k
    ProcessBlockAvailability(nodeid);
1478
1479
30.0k
    const CBlockIndex* pindex = m_chainman.m_blockman.LookupBlockIndex(hash);
1480
30.0k
    if (pindex && pindex->nChainWork > 0) {
1481
        // An actually better block was announced.
1482
28.3k
        if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork) {
1483
27.8k
            state->pindexBestKnownBlock = pindex;
1484
27.8k
        }
1485
28.3k
    } else {
1486
        // An unknown block was announced; just assume that the latest one is the best one.
1487
1.69k
        state->hashLastUnknownBlock = hash;
1488
1.69k
    }
1489
30.0k
}
1490
1491
// Logic for calculating which blocks to download from a given peer, given our current tip.
1492
void PeerManagerImpl::FindNextBlocksToDownload(const Peer& peer, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller)
1493
377k
{
1494
377k
    if (count == 0)
1495
0
        return;
1496
1497
377k
    vBlocks.reserve(vBlocks.size() + count);
1498
377k
    CNodeState *state = State(peer.m_id);
1499
377k
    assert(state != nullptr);
1500
1501
    // Make sure pindexBestKnownBlock is up to date, we'll need it.
1502
377k
    ProcessBlockAvailability(peer.m_id);
1503
1504
377k
    if (state->pindexBestKnownBlock == nullptr || state->pindexBestKnownBlock->nChainWork < m_chainman.ActiveChain().Tip()->nChainWork || state->pindexBestKnownBlock->nChainWork < m_chainman.MinimumChainWork()) {
1505
        // This peer has nothing interesting.
1506
244k
        return;
1507
244k
    }
1508
1509
    // When syncing with AssumeUtxo and the snapshot has not yet been validated,
1510
    // abort downloading blocks from peers that don't have the snapshot block in their best chain.
1511
    // We can't reorg to this chain due to missing undo data until validation completes,
1512
    // so downloading blocks from it would be futile.
1513
133k
    const CBlockIndex* snap_base{m_chainman.CurrentChainstate().SnapshotBase()};
1514
133k
    if (snap_base && m_chainman.CurrentChainstate().m_assumeutxo == Assumeutxo::UNVALIDATED &&
1515
133k
        state->pindexBestKnownBlock->GetAncestor(snap_base->nHeight) != snap_base) {
1516
0
        LogDebug(BCLog::NET, "Not downloading blocks from peer=%d, which doesn't have the snapshot block in its best chain.\n", peer.m_id);
1517
0
        return;
1518
0
    }
1519
1520
    // Determine the forking point between the peer's chain and our chain:
1521
    // pindexLastCommonBlock is required to be an ancestor of pindexBestKnownBlock, and will be used as a starting point.
1522
    // It is being set to the fork point between the peer's best known block and the current tip, unless it is already set to
1523
    // an ancestor with more work than the fork point.
1524
133k
    auto fork_point = LastCommonAncestor(state->pindexBestKnownBlock, m_chainman.ActiveTip());
1525
133k
    if (state->pindexLastCommonBlock == nullptr ||
1526
133k
        fork_point->nChainWork > state->pindexLastCommonBlock->nChainWork ||
1527
133k
        state->pindexBestKnownBlock->GetAncestor(state->pindexLastCommonBlock->nHeight) != state->pindexLastCommonBlock) {
1528
43.6k
        state->pindexLastCommonBlock = fork_point;
1529
43.6k
    }
1530
133k
    if (state->pindexLastCommonBlock == state->pindexBestKnownBlock)
1531
92.4k
        return;
1532
1533
40.7k
    const CBlockIndex *pindexWalk = state->pindexLastCommonBlock;
1534
    // Never fetch further than the best block we know the peer has, or more than BLOCK_DOWNLOAD_WINDOW + 1 beyond the last
1535
    // linked block we have in common with this peer. The +1 is so we can detect stalling, namely if we would be able to
1536
    // download that next block if the window were 1 larger.
1537
40.7k
    int nWindowEnd = state->pindexLastCommonBlock->nHeight + BLOCK_DOWNLOAD_WINDOW;
1538
1539
40.7k
    FindNextBlocks(vBlocks, peer, state, pindexWalk, count, nWindowEnd, &m_chainman.ActiveChain(), &nodeStaller);
1540
40.7k
}
1541
1542
void PeerManagerImpl::TryDownloadingHistoricalBlocks(const Peer& peer, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, const CBlockIndex *from_tip, const CBlockIndex* target_block)
1543
1.56k
{
1544
1.56k
    Assert(from_tip);
1545
1.56k
    Assert(target_block);
1546
1547
1.56k
    if (vBlocks.size() >= count) {
1548
475
        return;
1549
475
    }
1550
1551
1.08k
    vBlocks.reserve(count);
1552
1.08k
    CNodeState *state = Assert(State(peer.m_id));
1553
1554
1.08k
    if (state->pindexBestKnownBlock == nullptr || state->pindexBestKnownBlock->GetAncestor(target_block->nHeight) != target_block) {
1555
        // This peer can't provide us the complete series of blocks leading up to the
1556
        // assumeutxo snapshot base.
1557
        //
1558
        // Presumably this peer's chain has less work than our ActiveChain()'s tip, or else we
1559
        // will eventually crash when we try to reorg to it. Let other logic
1560
        // deal with whether we disconnect this peer.
1561
        //
1562
        // TODO at some point in the future, we might choose to request what blocks
1563
        // this peer does have from the historical chain, despite it not having a
1564
        // complete history beneath the snapshot base.
1565
86
        return;
1566
86
    }
1567
1568
1.00k
    FindNextBlocks(vBlocks, peer, state, from_tip, count, std::min<int>(from_tip->nHeight + BLOCK_DOWNLOAD_WINDOW, target_block->nHeight));
1569
1.00k
}
1570
1571
void PeerManagerImpl::FindNextBlocks(std::vector<const CBlockIndex*>& vBlocks, const Peer& peer, CNodeState *state, const CBlockIndex *pindexWalk, unsigned int count, int nWindowEnd, const CChain* activeChain, NodeId* nodeStaller)
1572
41.7k
{
1573
41.7k
    std::vector<const CBlockIndex*> vToFetch;
1574
41.7k
    int nMaxHeight = std::min<int>(state->pindexBestKnownBlock->nHeight, nWindowEnd + 1);
1575
41.7k
    bool is_limited_peer = IsLimitedPeer(peer);
1576
41.7k
    NodeId waitingfor = -1;
1577
62.9k
    while (pindexWalk->nHeight < nMaxHeight) {
1578
        // Read up to 128 (or more, if more blocks than that are needed) successors of pindexWalk (towards
1579
        // pindexBestKnownBlock) into vToFetch. We fetch 128, because CBlockIndex::GetAncestor may be as expensive
1580
        // as iterating over ~100 CBlockIndex* entries anyway.
1581
55.2k
        int nToFetch = std::min(nMaxHeight - pindexWalk->nHeight, std::max<int>(count - vBlocks.size(), 128));
1582
55.2k
        vToFetch.resize(nToFetch);
1583
55.2k
        pindexWalk = state->pindexBestKnownBlock->GetAncestor(pindexWalk->nHeight + nToFetch);
1584
55.2k
        vToFetch[nToFetch - 1] = pindexWalk;
1585
5.77M
        for (unsigned int i = nToFetch - 1; i > 0; i--) {
1586
5.71M
            vToFetch[i - 1] = vToFetch[i]->pprev;
1587
5.71M
        }
1588
1589
        // Iterate over those blocks in vToFetch (in forward direction), adding the ones that
1590
        // are not yet downloaded and not in flight to vBlocks. In the meantime, update
1591
        // pindexLastCommonBlock as long as all ancestors are already downloaded, or if it's
1592
        // already part of our chain (and therefore don't need it even if pruned).
1593
2.48M
        for (const CBlockIndex* pindex : vToFetch) {
1594
2.48M
            if (!pindex->IsValid(BLOCK_VALID_TREE)) {
1595
                // We consider the chain that this peer is on invalid.
1596
349
                return;
1597
349
            }
1598
1599
2.48M
            if (!CanServeWitnesses(peer) && DeploymentActiveAt(*pindex, m_chainman, Consensus::DEPLOYMENT_SEGWIT)) {
1600
                // We wouldn't download this block or its descendants from this peer.
1601
164
                return;
1602
164
            }
1603
1604
2.48M
            if (pindex->nStatus & BLOCK_HAVE_DATA || (activeChain && activeChain->Contains(*pindex))) {
1605
1.80M
                if (activeChain && pindex->HaveNumChainTxs()) {
1606
10.3k
                    state->pindexLastCommonBlock = pindex;
1607
10.3k
                }
1608
1.80M
                continue;
1609
1.80M
            }
1610
1611
            // Is block in-flight?
1612
685k
            if (IsBlockRequested(pindex->GetBlockHash())) {
1613
641k
                if (waitingfor == -1) {
1614
                    // This is the first already-in-flight block.
1615
40.2k
                    waitingfor = mapBlocksInFlight.lower_bound(pindex->GetBlockHash())->second.first;
1616
40.2k
                }
1617
641k
                continue;
1618
641k
            }
1619
1620
            // The block is not already downloaded, and not yet in flight.
1621
44.4k
            if (pindex->nHeight > nWindowEnd) {
1622
                // We reached the end of the window.
1623
358
                if (vBlocks.size() == 0 && waitingfor != peer.m_id) {
1624
                    // We aren't able to fetch anything, but we would be if the download window was one larger.
1625
266
                    if (nodeStaller) *nodeStaller = waitingfor;
1626
266
                }
1627
358
                return;
1628
358
            }
1629
1630
            // Don't request blocks that go further than what limited peers can provide
1631
44.1k
            if (is_limited_peer && (state->pindexBestKnownBlock->nHeight - pindex->nHeight >= static_cast<int>(NODE_NETWORK_LIMITED_MIN_BLOCKS) - 2 /* two blocks buffer for possible races */)) {
1632
9.21k
                continue;
1633
9.21k
            }
1634
1635
34.9k
            vBlocks.push_back(pindex);
1636
34.9k
            if (vBlocks.size() == count) {
1637
33.1k
                return;
1638
33.1k
            }
1639
34.9k
        }
1640
55.2k
    }
1641
41.7k
}
1642
1643
} // namespace
1644
1645
void PeerManagerImpl::PushNodeVersion(CNode& pnode, const Peer& peer)
1646
1.67k
{
1647
1.67k
    uint64_t my_services;
1648
1.67k
    int64_t my_time;
1649
1.67k
    uint64_t your_services;
1650
1.67k
    CService your_addr;
1651
1.67k
    std::string my_user_agent;
1652
1.67k
    int my_height;
1653
1.67k
    bool my_tx_relay;
1654
1.67k
    if (pnode.IsPrivateBroadcastConn()) {
1655
18
        my_services = NODE_NONE;
1656
18
        my_time = 0;
1657
18
        your_services = NODE_NONE;
1658
18
        your_addr = CService{};
1659
18
        my_user_agent = "/pynode:0.0.1/"; // Use a constant other than the default (or user-configured). See https://github.com/bitcoin/bitcoin/pull/27509#discussion_r1214671917
1660
18
        my_height = 0;
1661
18
        my_tx_relay = false;
1662
1.65k
    } else {
1663
1.65k
        const CAddress& addr{pnode.addr};
1664
1.65k
        my_services = peer.m_our_services;
1665
1.65k
        my_time = TicksSinceEpoch<std::chrono::seconds>(NodeClock::now());
1666
1.65k
        your_services = addr.nServices;
1667
1.65k
        your_addr = addr.IsRoutable() && !IsProxy(addr) && addr.IsAddrV1Compatible() ? CService{addr} : CService{};
1668
1.65k
        my_user_agent = strSubVersion;
1669
1.65k
        my_height = m_best_height;
1670
1.65k
        my_tx_relay = !RejectIncomingTxs(pnode);
1671
1.65k
    }
1672
1673
1.67k
    MakeAndPushMessage(
1674
1.67k
        pnode,
1675
1.67k
        NetMsgType::VERSION,
1676
1.67k
        pnode.AdvertisedVersion(),
1677
1.67k
        my_services,
1678
1.67k
        my_time,
1679
        // your_services + CNetAddr::V1(your_addr) is the pre-version-31402 serialization of your_addr (without nTime)
1680
1.67k
        your_services, CNetAddr::V1(your_addr),
1681
        // same, for a dummy address
1682
1.67k
        my_services, CNetAddr::V1(CService{}),
1683
1.67k
        pnode.GetLocalNonce(),
1684
1.67k
        my_user_agent,
1685
1.67k
        my_height,
1686
1.67k
        my_tx_relay);
1687
1688
1.67k
    LogDebug(
1689
1.67k
        BCLog::NET, "send version message: version=%d, blocks=%d%s, txrelay=%d, peer=%d\n",
1690
1.67k
        pnode.AdvertisedVersion(), my_height,
1691
1.67k
        fLogIPs ? strprintf(", them=%s", your_addr.ToStringAddrPort()) : "",
1692
1.67k
        my_tx_relay, pnode.GetId());
1693
1.67k
}
1694
1695
void PeerManagerImpl::UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds)
1696
1
{
1697
1
    LOCK(cs_main);
1698
1
    CNodeState *state = State(node);
1699
1
    if (state) state->m_last_block_announcement = time_in_seconds;
1700
1
}
1701
1702
void PeerManagerImpl::InitializeNode(const CNode& node, ServiceFlags our_services)
1703
1.74k
{
1704
1.74k
    NodeId nodeid = node.GetId();
1705
1.74k
    {
1706
1.74k
        LOCK(cs_main); // For m_node_states
1707
1.74k
        m_node_states.try_emplace(m_node_states.end(), nodeid);
1708
1.74k
    }
1709
1.74k
    WITH_LOCK(m_tx_download_mutex, m_txdownloadman.CheckIsEmpty(nodeid));
1710
1711
1.74k
    if (NetPermissions::HasFlag(node.m_permission_flags, NetPermissionFlags::BloomFilter)) {
1712
4
        our_services = static_cast<ServiceFlags>(our_services | NODE_BLOOM);
1713
4
    }
1714
1715
1.74k
    PeerRef peer = std::make_shared<Peer>(nodeid, our_services, node.IsInboundConn());
1716
1.74k
    {
1717
1.74k
        LOCK(m_peer_mutex);
1718
1.74k
        m_peer_map.emplace_hint(m_peer_map.end(), nodeid, peer);
1719
1.74k
    }
1720
1.74k
}
1721
1722
void PeerManagerImpl::ReattemptInitialBroadcast(CScheduler& scheduler)
1723
9
{
1724
9
    std::set<Txid> unbroadcast_txids = m_mempool.GetUnbroadcastTxs();
1725
1726
9
    for (const auto& txid : unbroadcast_txids) {
1727
5
        CTransactionRef tx = m_mempool.get(txid);
1728
1729
5
        if (tx != nullptr) {
1730
5
            InitiateTxBroadcastToAll(tx->GetWitnessHash());
1731
5
        } else {
1732
0
            m_mempool.RemoveUnbroadcastTx(txid, true);
1733
0
        }
1734
5
    }
1735
1736
    // Schedule next run for 10-15 minutes in the future.
1737
    // We add randomness on every cycle to avoid the possibility of P2P fingerprinting.
1738
9
    const auto delta = 10min + FastRandomContext().randrange<std::chrono::milliseconds>(5min);
1739
9
    scheduler.scheduleFromNow([&] { ReattemptInitialBroadcast(scheduler); }, delta);
1740
9
}
1741
1742
void PeerManagerImpl::ReattemptPrivateBroadcast(CScheduler& scheduler)
1743
7
{
1744
    // Remove stale transactions that are no longer relevant (e.g. already in
1745
    // the mempool or mined) and count the remaining ones.
1746
7
    size_t num_for_rebroadcast{0};
1747
7
    const auto stale_txs = m_tx_for_private_broadcast.GetStale();
1748
7
    if (!stale_txs.empty()) {
1749
2
        for (const auto& stale_tx : stale_txs) {
1750
            // Only hold lock per single submission
1751
2
            LOCK(cs_main);
1752
2
            auto mempool_acceptable = m_chainman.ProcessTransaction(stale_tx, /*test_accept=*/true);
1753
2
            if (mempool_acceptable.m_result_type == MempoolAcceptResult::ResultType::VALID) {
1754
1
                LogDebug(BCLog::PRIVBROADCAST,
1755
1
                         "Reattempting broadcast of stale txid=%s wtxid=%s",
1756
1
                         stale_tx->GetHash().ToString(), stale_tx->GetWitnessHash().ToString());
1757
1
                ++num_for_rebroadcast;
1758
1
            } else {
1759
1
                LogDebug(BCLog::PRIVBROADCAST, "Giving up broadcast attempts for txid=%s wtxid=%s: %s",
1760
1
                         stale_tx->GetHash().ToString(), stale_tx->GetWitnessHash().ToString(),
1761
1
                         mempool_acceptable.m_state.ToString());
1762
1
                m_tx_for_private_broadcast.Remove(stale_tx);
1763
1
            }
1764
2
        }
1765
1766
        // This could overshoot, but that is ok - we will open some private connections in vain.
1767
1
        m_connman.m_private_broadcast.NumToOpenAdd(num_for_rebroadcast);
1768
1
    }
1769
1770
7
    const auto delta{2min + FastRandomContext().randrange<std::chrono::milliseconds>(1min)};
1771
7
    scheduler.scheduleFromNow([&] { ReattemptPrivateBroadcast(scheduler); }, delta);
1772
7
}
1773
1774
void PeerManagerImpl::FinalizeNode(const CNode& node)
1775
1.73k
{
1776
1.73k
    NodeId nodeid = node.GetId();
1777
1.73k
    {
1778
1.73k
    LOCK(cs_main);
1779
1.73k
    {
1780
        // We remove the PeerRef from g_peer_map here, but we don't always
1781
        // destruct the Peer. Sometimes another thread is still holding a
1782
        // PeerRef, so the refcount is >= 1. Be careful not to do any
1783
        // processing here that assumes Peer won't be changed before it's
1784
        // destructed.
1785
1.73k
        PeerRef peer = RemovePeer(nodeid);
1786
1.73k
        assert(peer != nullptr);
1787
1.73k
        m_wtxid_relay_peers -= peer->m_wtxid_relay;
1788
1.73k
        assert(m_wtxid_relay_peers >= 0);
1789
1.73k
    }
1790
1.73k
    CNodeState *state = State(nodeid);
1791
1.73k
    assert(state != nullptr);
1792
1793
1.73k
    if (state->fSyncStarted)
1794
1.51k
        nSyncStarted--;
1795
1796
1.73k
    for (const QueuedBlock& entry : state->vBlocksInFlight) {
1797
116
        auto range = mapBlocksInFlight.equal_range(entry.pindex->GetBlockHash());
1798
232
        while (range.first != range.second) {
1799
116
            auto [node_id, list_it] = range.first->second;
1800
116
            if (node_id != nodeid) {
1801
0
                range.first++;
1802
116
            } else {
1803
116
                range.first = mapBlocksInFlight.erase(range.first);
1804
116
            }
1805
116
        }
1806
116
    }
1807
1.73k
    {
1808
1.73k
        LOCK(m_tx_download_mutex);
1809
1.73k
        m_txdownloadman.DisconnectedPeer(nodeid);
1810
1.73k
    }
1811
1.73k
    if (m_txreconciliation) m_txreconciliation->ForgetPeer(nodeid);
1812
1.73k
    m_num_preferred_download_peers -= state->fPreferredDownload;
1813
1.73k
    m_peers_downloading_from -= (!state->vBlocksInFlight.empty());
1814
1.73k
    assert(m_peers_downloading_from >= 0);
1815
1.73k
    m_outbound_peers_with_protect_from_disconnect -= state->m_chain_sync.m_protect;
1816
1.73k
    assert(m_outbound_peers_with_protect_from_disconnect >= 0);
1817
1818
1.73k
    m_node_states.erase(nodeid);
1819
1820
1.73k
    if (m_node_states.empty()) {
1821
        // Do a consistency check after the last peer is removed.
1822
892
        assert(mapBlocksInFlight.empty());
1823
892
        assert(m_num_preferred_download_peers == 0);
1824
892
        assert(m_peers_downloading_from == 0);
1825
892
        assert(m_outbound_peers_with_protect_from_disconnect == 0);
1826
892
        assert(m_wtxid_relay_peers == 0);
1827
892
        WITH_LOCK(m_tx_download_mutex, m_txdownloadman.CheckIsEmpty());
1828
892
    }
1829
1.73k
    } // cs_main
1830
1.73k
    if (node.fSuccessfullyConnected &&
1831
1.73k
        !node.IsBlockOnlyConn() && !node.IsPrivateBroadcastConn() && !node.IsInboundConn()) {
1832
        // Only change visible addrman state for full outbound peers.  We don't
1833
        // call Connected() for feeler connections since they don't have
1834
        // fSuccessfullyConnected set. Also don't call Connected() for private broadcast
1835
        // connections since they could leak information in addrman.
1836
529
        m_addrman.Connected(node.addr);
1837
529
    }
1838
1.73k
    {
1839
1.73k
        LOCK(m_headers_presync_mutex);
1840
1.73k
        m_headers_presync_stats.erase(nodeid);
1841
1.73k
    }
1842
1.73k
    if (node.IsPrivateBroadcastConn() &&
1843
1.73k
        !m_tx_for_private_broadcast.DidNodeConfirmReception(nodeid) &&
1844
1.73k
        m_tx_for_private_broadcast.HavePendingTransactions()) {
1845
1846
6
        m_connman.m_private_broadcast.NumToOpenAdd(1);
1847
6
    }
1848
1.73k
    LogDebug(BCLog::NET, "Cleared nodestate for peer=%d\n", nodeid);
1849
1.73k
}
1850
1851
bool PeerManagerImpl::HasAllDesirableServiceFlags(ServiceFlags services) const
1852
1.82k
{
1853
    // Shortcut for (services & GetDesirableServiceFlags(services)) == GetDesirableServiceFlags(services)
1854
1.82k
    return !(GetDesirableServiceFlags(services) & (~services));
1855
1.82k
}
1856
1857
ServiceFlags PeerManagerImpl::GetDesirableServiceFlags(ServiceFlags services) const
1858
1.85k
{
1859
1.85k
    if (services & NODE_NETWORK_LIMITED) {
1860
        // Limited peers are desirable when we are close to the tip.
1861
818
        if (ApproximateBestBlockDepth() < NODE_NETWORK_LIMITED_ALLOW_CONN_BLOCKS) {
1862
547
            return ServiceFlags(NODE_NETWORK_LIMITED | NODE_WITNESS);
1863
547
        }
1864
818
    }
1865
1.30k
    return ServiceFlags(NODE_NETWORK | NODE_WITNESS);
1866
1.85k
}
1867
1868
PeerRef PeerManagerImpl::GetPeerRef(NodeId id) const
1869
821k
{
1870
821k
    LOCK(m_peer_mutex);
1871
821k
    auto it = m_peer_map.find(id);
1872
821k
    return it != m_peer_map.end() ? it->second : nullptr;
1873
821k
}
1874
1875
PeerRef PeerManagerImpl::RemovePeer(NodeId id)
1876
1.73k
{
1877
1.73k
    PeerRef ret;
1878
1.73k
    LOCK(m_peer_mutex);
1879
1.73k
    auto it = m_peer_map.find(id);
1880
1.73k
    if (it != m_peer_map.end()) {
1881
1.73k
        ret = std::move(it->second);
1882
1.73k
        m_peer_map.erase(it);
1883
1.73k
    }
1884
1.73k
    return ret;
1885
1.73k
}
1886
1887
std::vector<PeerRef> PeerManagerImpl::GetAllPeers() const
1888
26.9k
{
1889
26.9k
    std::vector<PeerRef> peers;
1890
26.9k
    LOCK(m_peer_mutex);
1891
26.9k
    peers.reserve(m_peer_map.size());
1892
40.9k
    for (const auto& [_, peer] : m_peer_map) {
1893
40.9k
        peers.push_back(peer);
1894
40.9k
    }
1895
26.9k
    return peers;
1896
26.9k
}
1897
1898
bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const
1899
15.4k
{
1900
15.4k
    {
1901
15.4k
        LOCK(cs_main);
1902
15.4k
        const CNodeState* state = State(nodeid);
1903
15.4k
        if (state == nullptr)
1904
3
            return false;
1905
15.4k
        stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
1906
15.4k
        stats.nCommonHeight = state->pindexLastCommonBlock ? state->pindexLastCommonBlock->nHeight : -1;
1907
21.1k
        for (const QueuedBlock& queue : state->vBlocksInFlight) {
1908
21.1k
            if (queue.pindex)
1909
21.1k
                stats.vHeightInFlight.push_back(queue.pindex->nHeight);
1910
21.1k
        }
1911
15.4k
    }
1912
1913
0
    PeerRef peer = GetPeerRef(nodeid);
1914
15.4k
    if (peer == nullptr) return false;
1915
15.4k
    stats.their_services = peer->m_their_services;
1916
    // It is common for nodes with good ping times to suddenly become lagged,
1917
    // due to a new block arriving or other large transfer.
1918
    // Merely reporting pingtime might fool the caller into thinking the node was still responsive,
1919
    // since pingtime does not update until the ping is complete, which might take a while.
1920
    // So, if a ping is taking an unusually long time in flight,
1921
    // the caller can immediately detect that this is happening.
1922
15.4k
    NodeClock::duration ping_wait{0us};
1923
15.4k
    if ((0 != peer->m_ping_nonce_sent) && (peer->m_ping_start.load() > NodeClock::epoch)) {
1924
74
        ping_wait = NodeClock::now() - peer->m_ping_start.load();
1925
74
    }
1926
1927
15.4k
    if (auto tx_relay = peer->GetTxRelay(); tx_relay != nullptr) {
1928
14.7k
        stats.m_relay_txs = WITH_LOCK(tx_relay->m_bloom_filter_mutex, return tx_relay->m_relay_txs);
1929
14.7k
        stats.m_fee_filter_received = tx_relay->m_fee_filter_received.load();
1930
14.7k
        LOCK(tx_relay->m_tx_inventory_mutex);
1931
14.7k
        stats.m_last_inv_seq = tx_relay->m_last_inv_sequence;
1932
14.7k
        stats.m_inv_to_send = tx_relay->m_tx_inventory_to_send.size();
1933
14.7k
    } else {
1934
743
        stats.m_relay_txs = false;
1935
743
        stats.m_fee_filter_received = 0;
1936
743
        stats.m_inv_to_send = 0;
1937
743
    }
1938
1939
15.4k
    stats.m_ping_wait = ping_wait;
1940
15.4k
    stats.m_addr_processed = peer->m_addr_processed.load();
1941
15.4k
    stats.m_addr_rate_limited = peer->m_addr_rate_limited.load();
1942
15.4k
    stats.m_addr_relay_enabled = peer->m_addr_relay_enabled.load();
1943
15.4k
    {
1944
15.4k
        LOCK(peer->m_headers_sync_mutex);
1945
15.4k
        if (peer->m_headers_sync) {
1946
12
            stats.presync_height = peer->m_headers_sync->GetPresyncHeight();
1947
12
        }
1948
15.4k
    }
1949
15.4k
    stats.time_offset = peer->m_time_offset;
1950
1951
15.4k
    return true;
1952
15.4k
}
1953
1954
std::vector<node::TxOrphanage::OrphanInfo> PeerManagerImpl::GetOrphanTransactions()
1955
226
{
1956
226
    LOCK(m_tx_download_mutex);
1957
226
    return m_txdownloadman.GetOrphanTransactions();
1958
226
}
1959
1960
PeerManagerInfo PeerManagerImpl::GetInfo() const
1961
942
{
1962
942
    LOCK(m_inv_to_send_mutex);
1963
942
    return PeerManagerInfo{
1964
942
        .median_outbound_time_offset = m_outbound_time_offsets.Median(),
1965
942
        .ignores_incoming_txs = m_opts.ignore_incoming_txs,
1966
942
        .private_broadcast = m_opts.private_broadcast,
1967
942
        .tx_send_rate = m_opts.tx_send_rate,
1968
942
        .inbound_bucket = m_inbound_inv_bucket.info(),
1969
942
        .outbound_bucket = m_outbound_inv_bucket.info(),
1970
942
    };
1971
942
}
1972
1973
std::vector<PrivateBroadcast::TxBroadcastInfo> PeerManagerImpl::GetPrivateBroadcastInfo() const
1974
12
{
1975
12
    return m_tx_for_private_broadcast.GetBroadcastInfo();
1976
12
}
1977
1978
std::vector<CTransactionRef> PeerManagerImpl::AbortPrivateBroadcast(const uint256& id)
1979
3
{
1980
3
    const auto snapshot{m_tx_for_private_broadcast.GetBroadcastInfo()};
1981
3
    std::vector<CTransactionRef> removed_txs;
1982
1983
3
    size_t connections_cancelled{0};
1984
10.0k
    for (const auto& tx_info : snapshot) {
1985
10.0k
        const CTransactionRef& tx{tx_info.tx};
1986
10.0k
        if (tx->GetHash().ToUint256() != id && tx->GetWitnessHash().ToUint256() != id) continue;
1987
2
        if (const auto peer_acks{m_tx_for_private_broadcast.Remove(tx)}) {
1988
2
            removed_txs.push_back(tx);
1989
2
            if (NUM_PRIVATE_BROADCAST_PER_TX > *peer_acks) {
1990
2
                connections_cancelled += (NUM_PRIVATE_BROADCAST_PER_TX - *peer_acks);
1991
2
            }
1992
2
        }
1993
2
    }
1994
3
    m_connman.m_private_broadcast.NumToOpenSub(connections_cancelled);
1995
1996
3
    return removed_txs;
1997
3
}
1998
1999
void PeerManagerImpl::AddToCompactExtraTransactions(const CTransactionRef& tx)
2000
1.34k
{
2001
1.34k
    if (m_opts.max_extra_txs == 0) return;
2002
1.34k
    if (vExtraTxnForCompact.size() < m_opts.max_extra_txs) {
2003
926
        if (vExtraTxnForCompact.empty()) vExtraTxnForCompact.reserve(m_opts.max_extra_txs);
2004
926
        vExtraTxnForCompact.emplace_back(tx->GetWitnessHash(), tx);
2005
926
    } else {
2006
416
        vExtraTxnForCompact[vExtraTxnForCompactIt] = std::make_pair(tx->GetWitnessHash(), tx);
2007
416
    }
2008
1.34k
    vExtraTxnForCompactIt = (vExtraTxnForCompactIt + 1) % m_opts.max_extra_txs;
2009
1.34k
}
2010
2011
void PeerManagerImpl::Misbehaving(Peer& peer, const std::string& message)
2012
577
{
2013
577
    LOCK(peer.m_misbehavior_mutex);
2014
2015
577
    const std::string message_prefixed = message.empty() ? "" : (": " + message);
2016
577
    peer.m_should_discourage = true;
2017
577
    LogDebug(BCLog::NET, "Misbehaving: peer=%d%s\n", peer.m_id, message_prefixed);
2018
577
    TRACEPOINT(net, misbehaving_connection,
2019
577
        peer.m_id,
2020
577
        message.c_str()
2021
577
    );
2022
577
}
2023
2024
void PeerManagerImpl::MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationState& state,
2025
                                              bool via_compact_block, const std::string& message)
2026
503
{
2027
503
    PeerRef peer{GetPeerRef(nodeid)};
2028
503
    switch (state.GetResult()) {
2029
0
    case BlockValidationResult::BLOCK_RESULT_UNSET:
2030
0
        break;
2031
1
    case BlockValidationResult::BLOCK_HEADER_LOW_WORK:
2032
        // We didn't try to process the block because the header chain may have
2033
        // too little work.
2034
1
        break;
2035
    // The node is providing invalid data:
2036
449
    case BlockValidationResult::BLOCK_CONSENSUS:
2037
449
    case BlockValidationResult::BLOCK_MUTATED:
2038
449
        if (!via_compact_block) {
2039
433
            if (peer) Misbehaving(*peer, message);
2040
433
            return;
2041
433
        }
2042
16
        break;
2043
16
    case BlockValidationResult::BLOCK_CACHED_INVALID:
2044
0
        {
2045
            // Discourage outbound (but not inbound) peers if on an invalid chain.
2046
            // Exempt HB compact block peers. Manual connections are always protected from discouragement.
2047
0
            if (peer && !via_compact_block && !peer->m_is_inbound) {
2048
0
                if (peer) Misbehaving(*peer, message);
2049
0
                return;
2050
0
            }
2051
0
            break;
2052
0
        }
2053
6
    case BlockValidationResult::BLOCK_INVALID_HEADER:
2054
9
    case BlockValidationResult::BLOCK_INVALID_PREV:
2055
9
        if (peer) Misbehaving(*peer, message);
2056
9
        return;
2057
    // Conflicting (but not necessarily invalid) data or different policy:
2058
2
    case BlockValidationResult::BLOCK_MISSING_PREV:
2059
2
        if (peer) Misbehaving(*peer, message);
2060
2
        return;
2061
42
    case BlockValidationResult::BLOCK_TIME_FUTURE:
2062
42
        break;
2063
503
    }
2064
59
    if (message != "") {
2065
39
        LogDebug(BCLog::NET, "peer=%d: %s\n", nodeid, message);
2066
39
    }
2067
59
}
2068
2069
bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex& block_index)
2070
37.0k
{
2071
37.0k
    AssertLockHeld(cs_main);
2072
37.0k
    if (m_chainman.ActiveChain().Contains(block_index)) return true;
2073
174
    return block_index.IsValid(BLOCK_VALID_SCRIPTS) && (m_chainman.m_best_header != nullptr) &&
2074
174
           (m_chainman.m_best_header->GetBlockTime() - block_index.GetBlockTime() < STALE_RELAY_AGE_LIMIT) &&
2075
174
           (GetBlockProofEquivalentTime(*m_chainman.m_best_header, block_index, *m_chainman.m_best_header, m_chainparams.GetConsensus()) < STALE_RELAY_AGE_LIMIT);
2076
37.0k
}
2077
2078
util::Expected<void, std::string> PeerManagerImpl::FetchBlock(NodeId peer_id, const CBlockIndex& block_index)
2079
5
{
2080
5
    if (m_chainman.m_blockman.LoadingBlocks()) return util::Unexpected{"Loading blocks ..."};
2081
2082
    // The lock must be taken here before fetching Peer so another thread does
2083
    // not delete the CNodeState from under the current thread, causing an
2084
    // assertion failure in BlockRequested. This lock can be replaced with a
2085
    // net-specific lock when more of CNodeState is moved into Peer.
2086
5
    LOCK(cs_main);
2087
2088
    // Ensure this peer exists and hasn't been disconnected
2089
5
    PeerRef peer = GetPeerRef(peer_id);
2090
5
    if (peer == nullptr) return util::Unexpected{"Peer does not exist"};
2091
2092
    // Ignore pre-segwit peers
2093
3
    if (!CanServeWitnesses(*peer)) return util::Unexpected{"Pre-SegWit peer"};
2094
2095
    // Forget about all prior requests
2096
2
    RemoveBlockRequest(block_index.GetBlockHash(), std::nullopt);
2097
2098
    // Mark block as in-flight
2099
2
    if (!BlockRequested(peer_id, block_index)) return util::Unexpected{"Already requested from this peer"};
2100
2101
    // Construct message to request the block
2102
2
    const uint256& hash{block_index.GetBlockHash()};
2103
2
    std::vector<CInv> invs{CInv(MSG_BLOCK | MSG_WITNESS_FLAG, hash)};
2104
2105
    // Send block request message to the peer
2106
2
    bool success = m_connman.ForNode(peer_id, [this, &invs](CNode* node) {
2107
2
        this->MakeAndPushMessage(*node, NetMsgType::GETDATA, invs);
2108
2
        return true;
2109
2
    });
2110
2111
2
    if (!success) return util::Unexpected{"Peer not fully connected"};
2112
2113
2
    LogDebug(BCLog::NET, "Requesting block %s from peer=%d\n",
2114
2
                 hash.ToString(), peer_id);
2115
2
    return {};
2116
2
}
2117
2118
std::unique_ptr<PeerManager> PeerManager::make(CConnman& connman, AddrMan& addrman,
2119
                                               BanMan* banman, ChainstateManager& chainman,
2120
                                               CTxMemPool& pool, node::Warnings& warnings, Options opts)
2121
1.20k
{
2122
1.20k
    return std::make_unique<PeerManagerImpl>(connman, addrman, banman, chainman, pool, warnings, opts);
2123
1.20k
}
2124
2125
PeerManagerImpl::PeerManagerImpl(CConnman& connman, AddrMan& addrman,
2126
                                 BanMan* banman, ChainstateManager& chainman,
2127
                                 CTxMemPool& pool, node::Warnings& warnings, Options opts)
2128
1.20k
    : m_rng{opts.deterministic_rng},
2129
1.20k
      m_fee_filter_rounder{CFeeRate{DEFAULT_MIN_RELAY_TX_FEE}, m_rng},
2130
1.20k
      m_chainparams(chainman.GetParams()),
2131
1.20k
      m_connman(connman),
2132
1.20k
      m_addrman(addrman),
2133
1.20k
      m_banman(banman),
2134
1.20k
      m_chainman(chainman),
2135
1.20k
      m_mempool(pool),
2136
1.20k
      m_txdownloadman(node::TxDownloadOptions{pool, m_rng, opts.deterministic_rng}),
2137
1.20k
      m_warnings{warnings},
2138
1.20k
      m_opts{opts},
2139
1.20k
      m_inbound_inv_bucket(/*rate=*/m_opts.tx_send_rate, /*mult=*/1.0),
2140
1.20k
      m_outbound_inv_bucket(/*rate=*/m_opts.tx_send_rate, /*mult=*/OUTBOUND_INVENTORY_BUCKET_MULTIPLIER)
2141
1.20k
{
2142
    // While Erlay support is incomplete, it must be enabled explicitly via -txreconciliation.
2143
    // This argument can go away after Erlay support is complete.
2144
1.20k
    if (opts.reconcile_txs) {
2145
5
        m_txreconciliation = std::make_unique<TxReconciliationTracker>(TXRECONCILIATION_VERSION);
2146
5
    }
2147
1.20k
}
2148
2149
void PeerManagerImpl::StartScheduledTasks(CScheduler& scheduler)
2150
1.00k
{
2151
    // Stale tip checking and peer eviction are on two different timers, but we
2152
    // don't want them to get out of sync due to drift in the scheduler, so we
2153
    // combine them in one function and schedule at the quicker (peer-eviction)
2154
    // timer.
2155
1.00k
    static_assert(EXTRA_PEER_CHECK_INTERVAL < STALE_CHECK_INTERVAL, "peer eviction timer should be less than stale tip check timer");
2156
1.00k
    scheduler.scheduleEvery([this] { this->CheckForStaleTipAndEvictPeers(); }, std::chrono::seconds{EXTRA_PEER_CHECK_INTERVAL});
2157
2158
    // schedule next run for 10-15 minutes in the future
2159
1.00k
    const auto delta = 10min + FastRandomContext().randrange<std::chrono::milliseconds>(5min);
2160
1.00k
    scheduler.scheduleFromNow([&] { ReattemptInitialBroadcast(scheduler); }, delta);
2161
2162
1.00k
    if (m_opts.private_broadcast) {
2163
6
        scheduler.scheduleFromNow([&] { ReattemptPrivateBroadcast(scheduler); }, 0min);
2164
6
    }
2165
1.00k
}
2166
2167
void PeerManagerImpl::ActiveTipChange(const CBlockIndex& new_tip, bool is_ibd)
2168
94.1k
{
2169
    // Ensure mempool mutex was released, otherwise deadlock may occur if another thread holding
2170
    // m_tx_download_mutex waits on the mempool mutex.
2171
94.1k
    AssertLockNotHeld(m_mempool.cs);
2172
94.1k
    AssertLockNotHeld(m_tx_download_mutex);
2173
2174
94.1k
    if (!is_ibd) {
2175
79.9k
        LOCK(m_tx_download_mutex);
2176
        // If the chain tip has changed, previously rejected transactions might now be valid, e.g. due
2177
        // to a timelock. Reset the rejection filters to give those transactions another chance if we
2178
        // see them again.
2179
79.9k
        m_txdownloadman.ActiveTipChange();
2180
79.9k
    }
2181
94.1k
}
2182
2183
/**
2184
 * Evict orphan txn pool entries based on a newly connected
2185
 * block, remember the recently confirmed transactions, and delete tracked
2186
 * announcements for them. Also save the time of the last tip update and
2187
 * possibly reduce dynamic block stalling timeout.
2188
 */
2189
void PeerManagerImpl::BlockConnected(
2190
    const ChainstateRole& role,
2191
    const std::shared_ptr<const CBlock>& pblock,
2192
    const CBlockIndex* pindex)
2193
103k
{
2194
    // Update this for all chainstate roles so that we don't mistakenly see peers
2195
    // helping us do background IBD as having a stale tip.
2196
103k
    m_last_tip_update = GetTime<std::chrono::seconds>();
2197
2198
    // In case the dynamic timeout was doubled once or more, reduce it slowly back to its default value
2199
103k
    auto stalling_timeout = m_block_stalling_timeout.load();
2200
103k
    Assume(stalling_timeout >= BLOCK_STALLING_TIMEOUT_DEFAULT);
2201
103k
    if (stalling_timeout != BLOCK_STALLING_TIMEOUT_DEFAULT) {
2202
16
        const auto new_timeout = std::max(std::chrono::duration_cast<std::chrono::seconds>(stalling_timeout * 0.85), BLOCK_STALLING_TIMEOUT_DEFAULT);
2203
16
        if (m_block_stalling_timeout.compare_exchange_strong(stalling_timeout, new_timeout)) {
2204
16
            LogDebug(BCLog::NET, "Decreased stalling timeout to %d seconds\n", count_seconds(new_timeout));
2205
16
        }
2206
16
    }
2207
2208
    // The following task can be skipped since we don't maintain a mempool for
2209
    // the historical chainstate, or during ibd since we don't receive incoming
2210
    // transactions from peers into the mempool.
2211
103k
    if (!role.historical && !m_chainman.IsInitialBlockDownload()) {
2212
88.2k
        LOCK(m_tx_download_mutex);
2213
88.2k
        m_txdownloadman.BlockConnected(pblock);
2214
88.2k
    }
2215
103k
}
2216
2217
void PeerManagerImpl::BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex)
2218
13.2k
{
2219
13.2k
    LOCK(m_tx_download_mutex);
2220
13.2k
    m_txdownloadman.BlockDisconnected();
2221
13.2k
}
2222
2223
/**
2224
 * Maintain state about the best-seen block and fast-announce a compact block
2225
 * to compatible peers.
2226
 */
2227
void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock)
2228
77.4k
{
2229
77.4k
    auto pcmpctblock = std::make_shared<const CBlockHeaderAndShortTxIDs>(*pblock, FastRandomContext().rand64());
2230
2231
77.4k
    LOCK(cs_main);
2232
2233
77.4k
    if (pindex->nHeight <= m_highest_fast_announce)
2234
2.91k
        return;
2235
74.4k
    m_highest_fast_announce = pindex->nHeight;
2236
2237
74.4k
    if (!DeploymentActiveAt(*pindex, m_chainman, Consensus::DEPLOYMENT_SEGWIT)) return;
2238
2239
73.1k
    uint256 hashBlock(pblock->GetHash());
2240
73.1k
    const std::shared_future<CSerializedNetMsg> lazy_ser{
2241
73.1k
        std::async(std::launch::deferred, [&] { return NetMsg::Make(NetMsgType::CMPCTBLOCK, *pcmpctblock); })};
2242
2243
73.1k
    {
2244
73.1k
        auto most_recent_block_txs = std::make_unique<std::map<GenTxid, CTransactionRef>>();
2245
103k
        for (const auto& tx : pblock->vtx) {
2246
103k
            most_recent_block_txs->emplace(tx->GetHash(), tx);
2247
103k
            most_recent_block_txs->emplace(tx->GetWitnessHash(), tx);
2248
103k
        }
2249
2250
73.1k
        LOCK(m_most_recent_block_mutex);
2251
73.1k
        m_most_recent_block_hash = hashBlock;
2252
73.1k
        m_most_recent_block = pblock;
2253
73.1k
        m_most_recent_compact_block = pcmpctblock;
2254
73.1k
        m_most_recent_block_txs = std::move(most_recent_block_txs);
2255
73.1k
    }
2256
2257
74.3k
    m_connman.ForEachNode([this, pindex, &lazy_ser, &hashBlock](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
2258
74.3k
        AssertLockHeld(::cs_main);
2259
2260
74.3k
        if (pnode->GetCommonVersion() < INVALID_CB_NO_BAN_VERSION || pnode->fDisconnect)
2261
0
            return;
2262
74.3k
        ProcessBlockAvailability(pnode->GetId());
2263
74.3k
        CNodeState &state = *State(pnode->GetId());
2264
        // If the peer has, or we announced to them the previous block already,
2265
        // but we don't think they have this one, go ahead and announce it
2266
74.3k
        if (state.m_requested_hb_cmpctblocks && !PeerHasHeader(&state, pindex) && PeerHasHeader(&state, pindex->pprev)) {
2267
2268
20.0k
            LogDebug(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", "PeerManager::NewPoWValidBlock",
2269
20.0k
                    hashBlock.ToString(), pnode->GetId());
2270
2271
20.0k
            const CSerializedNetMsg& ser_cmpctblock{lazy_ser.get()};
2272
20.0k
            PushMessage(*pnode, ser_cmpctblock.Copy());
2273
20.0k
            state.pindexBestHeaderSent = pindex;
2274
20.0k
        }
2275
74.3k
    });
2276
73.1k
}
2277
2278
/**
2279
 * Update our best height and announce any block hashes which weren't previously
2280
 * in m_chainman.ActiveChain() to our peers.
2281
 */
2282
void PeerManagerImpl::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
2283
91.2k
{
2284
91.2k
    SetBestBlock(pindexNew->nHeight, std::chrono::seconds{pindexNew->GetBlockTime()});
2285
2286
    // Don't relay inventory during initial block download.
2287
91.2k
    if (fInitialDownload) return;
2288
2289
    // Find the hashes of all blocks that weren't previously in the best chain.
2290
77.0k
    std::vector<uint256> vHashes;
2291
77.0k
    const CBlockIndex *pindexToAnnounce = pindexNew;
2292
154k
    while (pindexToAnnounce != pindexFork) {
2293
77.7k
        vHashes.push_back(pindexToAnnounce->GetBlockHash());
2294
77.7k
        pindexToAnnounce = pindexToAnnounce->pprev;
2295
77.7k
        if (vHashes.size() == MAX_BLOCKS_TO_ANNOUNCE) {
2296
            // Limit announcements in case of a huge reorganization.
2297
            // Rely on the peer's synchronization mechanism in that case.
2298
68
            break;
2299
68
        }
2300
77.7k
    }
2301
2302
77.0k
    {
2303
77.0k
        LOCK(m_peer_mutex);
2304
80.0k
        for (auto& it : m_peer_map) {
2305
80.0k
            Peer& peer = *it.second;
2306
80.0k
            LOCK(peer.m_block_inv_mutex);
2307
80.8k
            for (const uint256& hash : vHashes | std::views::reverse) {
2308
80.8k
                peer.m_blocks_for_headers_relay.push_back(hash);
2309
80.8k
            }
2310
80.0k
        }
2311
77.0k
    }
2312
2313
77.0k
    m_connman.WakeMessageHandler();
2314
77.0k
}
2315
2316
/**
2317
 * Handle invalid block rejection and consequent peer discouragement, maintain which
2318
 * peers announce compact blocks.
2319
 */
2320
void PeerManagerImpl::BlockChecked(const std::shared_ptr<const CBlock>& block, const BlockValidationState& state)
2321
106k
{
2322
106k
    LOCK(cs_main);
2323
2324
106k
    const uint256 hash(block->GetHash());
2325
106k
    std::map<uint256, std::pair<NodeId, bool>>::iterator it = mapBlockSource.find(hash);
2326
2327
    // If the block failed validation, we know where it came from and we're still connected
2328
    // to that peer, maybe punish.
2329
106k
    if (state.IsInvalid() &&
2330
106k
        it != mapBlockSource.end() &&
2331
106k
        State(it->second.first)) {
2332
461
            MaybePunishNodeForBlock(/*nodeid=*/ it->second.first, state, /*via_compact_block=*/ !it->second.second);
2333
461
    }
2334
    // Check that:
2335
    // 1. The block is valid
2336
    // 2. We're not in initial block download
2337
    // 3. This is currently the best block we're aware of. We haven't updated
2338
    //    the tip yet so we have no way to check this directly here. Instead we
2339
    //    just check that there are currently no other blocks in flight.
2340
105k
    else if (state.IsValid() &&
2341
105k
             !m_chainman.IsInitialBlockDownload() &&
2342
105k
             mapBlocksInFlight.count(hash) == mapBlocksInFlight.size()) {
2343
64.6k
        if (it != mapBlockSource.end()) {
2344
20.7k
            MaybeSetPeerAsAnnouncingHeaderAndIDs(it->second.first);
2345
20.7k
        }
2346
64.6k
    }
2347
106k
    if (it != mapBlockSource.end())
2348
54.0k
        mapBlockSource.erase(it);
2349
106k
}
2350
2351
//////////////////////////////////////////////////////////////////////////////
2352
//
2353
// Messages
2354
//
2355
2356
bool PeerManagerImpl::AlreadyHaveBlock(const uint256& block_hash)
2357
1.66k
{
2358
1.66k
    return m_chainman.m_blockman.LookupBlockIndex(block_hash) != nullptr;
2359
1.66k
}
2360
2361
void PeerManagerImpl::SendPings()
2362
5
{
2363
5
    LOCK(m_peer_mutex);
2364
7
    for(auto& it : m_peer_map) it.second->m_ping_queued = true;
2365
5
}
2366
2367
std::vector<Wtxid> InvToSendBucket::TakeForProcessing(CTxMemPool& mempool)
2368
40.4k
{
2369
40.4k
    AssertLockHeld(mempool.cs);
2370
2371
40.4k
    size_t n_to_take = static_cast<size_t>(std::max<double>(count_bucket.value() - count_floor, 0));
2372
2373
40.4k
    std::vector<Wtxid> best;
2374
2375
40.4k
    auto itervec = mempool.ExtractBestByMiningScoreWithTopology(backlog, n_to_take);
2376
40.4k
    bool tokens_left = true;
2377
54.1k
    for (auto txiter : itervec) {
2378
54.1k
        auto& wtxid = txiter->GetTx().GetWitnessHash();
2379
54.1k
        if (tokens_left) {
2380
54.1k
            best.push_back(wtxid);
2381
54.1k
            if (!decrement(txiter->GetTx().ComputeTotalSize())) {
2382
22
                tokens_left = false;
2383
22
            }
2384
54.1k
        } else {
2385
28
            backlog.push_back(wtxid);
2386
28
        }
2387
54.1k
    }
2388
2389
    // if the backlog is now empty, consider shrinking it if it's oversized
2390
40.4k
    if (backlog.empty() && backlog.capacity() > INVENTORY_BUCKET_BACKLOG_CAPACITY) {
2391
3
        std::vector<Wtxid> dummy;
2392
3
        dummy.reserve(INVENTORY_BUCKET_BACKLOG_CAPACITY);
2393
3
        dummy.swap(backlog);
2394
3
    }
2395
2396
40.4k
    return best;
2397
40.4k
}
2398
2399
void PeerManagerImpl::ProcessInvBacklog(NodeClock::time_point now, bool backlog_bumped)
2400
417k
{
2401
    // Don't run the body of this function unless it's been a little
2402
    // while since the last run, or we just added a new tx to the backlog.
2403
417k
    if (!backlog_bumped && now <= m_next_inv_bucket_check.load()) return;
2404
78.0k
    m_next_inv_bucket_check = now + INVENTORY_BUCKET_CHECK_DELAY;
2405
2406
78.0k
    LOCK(m_inv_to_send_mutex);
2407
78.0k
    m_inbound_inv_bucket.increment(now);
2408
78.0k
    m_outbound_inv_bucket.increment(now);
2409
2410
    // Regular heartbeat logging when there's a backlog
2411
78.0k
    if (!m_next_inv_bucket_heartbeat.has_value()) {
2412
58.8k
        if (m_inbound_inv_bucket.backlog.size() >= INVENTORY_BUCKET_BACKLOG_HEARTBEAT_MIN || m_outbound_inv_bucket.backlog.size() >= INVENTORY_BUCKET_BACKLOG_HEARTBEAT_MIN) {
2413
5
            m_next_inv_bucket_heartbeat = now;
2414
5
        }
2415
58.8k
    }
2416
78.0k
    if (m_next_inv_bucket_heartbeat.has_value() && now >= *m_next_inv_bucket_heartbeat) {
2417
330
        LogDebug(BCLog::NET, "Transaction rate-limiting backlog inbound=%d itok=%.1f isz=%.1f outbound=%d otok=%.1f osz=%.1f",
2418
330
                 m_inbound_inv_bucket.backlog.size(),
2419
330
                 m_inbound_inv_bucket.count_bucket.value(),
2420
330
                 m_inbound_inv_bucket.size_bucket.value(),
2421
330
                 m_outbound_inv_bucket.backlog.size(),
2422
330
                 m_outbound_inv_bucket.count_bucket.value(),
2423
330
                 m_outbound_inv_bucket.size_bucket.value());
2424
330
        if (m_inbound_inv_bucket.backlog.empty() && m_outbound_inv_bucket.backlog.empty()) {
2425
4
            m_next_inv_bucket_heartbeat = std::nullopt;
2426
326
        } else {
2427
326
            m_next_inv_bucket_heartbeat = now + INVENTORY_BUCKET_BACKLOG_HEARTBEAT;
2428
326
        }
2429
330
    }
2430
2431
    // Early exit to skip pointlessly touching mempool lock
2432
78.0k
    bool in_avail = m_inbound_inv_bucket.avail();
2433
78.0k
    bool out_avail = m_outbound_inv_bucket.avail();
2434
78.0k
    if (!in_avail && !out_avail) return;
2435
2436
26.9k
    std::vector<Wtxid> for_inbound;
2437
26.9k
    std::vector<Wtxid> for_outbound;
2438
2439
26.9k
    {
2440
26.9k
        LOCK(m_mempool.cs);
2441
26.9k
        if (in_avail) for_inbound = m_inbound_inv_bucket.TakeForProcessing(m_mempool);
2442
26.9k
        if (out_avail) for_outbound = m_outbound_inv_bucket.TakeForProcessing(m_mempool);
2443
26.9k
    }
2444
2445
26.9k
    if (!for_inbound.empty() || !for_outbound.empty()) {
2446
26.9k
        bool any_inbound_connected = false;
2447
26.9k
        bool any_outbound_connected = false;
2448
40.9k
        for (const PeerRef& peer_ref : GetAllPeers()) {
2449
40.9k
            if (!peer_ref) continue;
2450
40.9k
            Peer& peer{*peer_ref};
2451
40.9k
            auto tx_relay = peer.GetTxRelay();
2452
40.9k
            if (!tx_relay) continue;
2453
2454
40.9k
            LOCK(tx_relay->m_tx_inventory_mutex);
2455
            // Only queue transactions for announcement once the version handshake
2456
            // is completed. The time of arrival for these transactions is
2457
            // otherwise at risk of leaking to a spy, if the spy is able to
2458
            // distinguish transactions received during the handshake from the rest
2459
            // in the announcement.
2460
40.9k
            if (tx_relay->m_next_inv_send_time == 0s) continue;
2461
40.9k
            if (peer.m_is_inbound) {
2462
22.0k
                any_inbound_connected = true;
2463
22.0k
            } else {
2464
18.9k
                any_outbound_connected = true;
2465
18.9k
            }
2466
41.1k
            for (auto& i : (peer.m_is_inbound ? for_inbound : for_outbound)) {
2467
41.1k
                tx_relay->m_tx_inventory_to_send.push_back(i);
2468
41.1k
            }
2469
40.9k
        }
2470
2471
        // if the node has no in/outbound connections, clear the corresponding backlog entirely
2472
        // this reduces wasted memory, and avoids having the bucket artificially empty for when
2473
        // future peers do connect.
2474
26.9k
        if (!any_inbound_connected) m_inbound_inv_bucket.backlog.clear();
2475
26.9k
        if (!any_outbound_connected) m_outbound_inv_bucket.backlog.clear();
2476
26.9k
    }
2477
26.9k
}
2478
2479
void PeerManagerImpl::InitiateTxBroadcastToAll(const Wtxid& wtxid)
2480
32.5k
{
2481
32.5k
    {
2482
32.5k
        LOCK(m_inv_to_send_mutex);
2483
32.5k
        m_inbound_inv_bucket.backlog.push_back(wtxid);
2484
32.5k
        m_outbound_inv_bucket.backlog.push_back(wtxid);
2485
32.5k
    }
2486
32.5k
    ProcessInvBacklog(NodeClock::now(), /*backlog_bumped=*/true);
2487
32.5k
}
2488
2489
node::TransactionError PeerManagerImpl::InitiateTxBroadcastPrivate(const CTransactionRef& tx)
2490
10.0k
{
2491
10.0k
    const auto txstr{strprintf("txid=%s, wtxid=%s", tx->GetHash().ToString(), tx->GetWitnessHash().ToString())};
2492
10.0k
    switch (m_tx_for_private_broadcast.Add(tx)) {
2493
10.0k
    case PrivateBroadcast::AddResult::Added:
2494
10.0k
        LogDebug(BCLog::PRIVBROADCAST, "Requesting %d new connections due to %s", NUM_PRIVATE_BROADCAST_PER_TX, txstr);
2495
10.0k
        m_connman.m_private_broadcast.NumToOpenAdd(NUM_PRIVATE_BROADCAST_PER_TX);
2496
10.0k
        return node::TransactionError::OK;
2497
2
    case PrivateBroadcast::AddResult::AlreadyPresent:
2498
2
        LogDebug(BCLog::PRIVBROADCAST, "Ignoring unnecessary request to schedule an already scheduled transaction: %s", txstr);
2499
2
        return node::TransactionError::OK;
2500
5
    case PrivateBroadcast::AddResult::QueueFull:
2501
5
        LogDebug(BCLog::PRIVBROADCAST, "Rejecting private broadcast, queue full (cap=%u): %s", PrivateBroadcast::MAX_TRANSACTIONS, txstr);
2502
5
        return node::TransactionError::PRIVATE_BROADCAST_FULL;
2503
10.0k
    } // no default case, so the compiler can warn about missing cases
2504
10.0k
    assert(false);
2505
0
}
2506
2507
void PeerManagerImpl::RelayAddress(NodeId originator,
2508
                                   const CAddress& addr,
2509
                                   bool fReachable)
2510
53
{
2511
    // We choose the same nodes within a given 24h window (if the list of connected
2512
    // nodes does not change) and we don't relay to nodes that already know an
2513
    // address. So within 24h we will likely relay a given address once. This is to
2514
    // prevent a peer from unjustly giving their address better propagation by sending
2515
    // it to us repeatedly.
2516
2517
53
    if (!fReachable && !addr.IsRelayable()) return;
2518
2519
    // Relay to a limited number of other nodes
2520
    // Use deterministic randomness to send to the same nodes for 24 hours
2521
    // at a time so the m_addr_knowns of the chosen nodes prevent repeats
2522
53
    const uint64_t hash_addr{CServiceHash(0, 0)(addr)};
2523
53
    const auto current_time{GetTime<std::chrono::seconds>()};
2524
    // Adding address hash makes exact rotation time different per address, while preserving periodicity.
2525
53
    const uint64_t time_addr{(static_cast<uint64_t>(count_seconds(current_time)) + hash_addr) / count_seconds(ROTATE_ADDR_RELAY_DEST_INTERVAL)};
2526
53
    const CSipHasher hasher{m_connman.GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY)
2527
53
                                .Write(hash_addr)
2528
53
                                .Write(time_addr)};
2529
2530
    // Relay reachable addresses to 2 peers. Unreachable addresses are relayed randomly to 1 or 2 peers.
2531
53
    unsigned int nRelayNodes = (fReachable || (hasher.Finalize() & 1)) ? 2 : 1;
2532
2533
53
    std::array<std::pair<uint64_t, Peer*>, 2> best{{{0, nullptr}, {0, nullptr}}};
2534
53
    assert(nRelayNodes <= best.size());
2535
2536
53
    LOCK(m_peer_mutex);
2537
2538
569
    for (auto& [id, peer] : m_peer_map) {
2539
569
        if (peer->m_addr_relay_enabled && id != originator && IsAddrCompatible(*peer, addr)) {
2540
512
            uint64_t hashKey = CSipHasher(hasher).Write(id).Finalize();
2541
1.23k
            for (unsigned int i = 0; i < nRelayNodes; i++) {
2542
915
                 if (hashKey > best[i].first) {
2543
194
                     std::copy(best.begin() + i, best.begin() + nRelayNodes - 1, best.begin() + i + 1);
2544
194
                     best[i] = std::make_pair(hashKey, peer.get());
2545
194
                     break;
2546
194
                 }
2547
915
            }
2548
512
        }
2549
569
    };
2550
2551
135
    for (unsigned int i = 0; i < nRelayNodes && best[i].first != 0; i++) {
2552
82
        PushAddress(*best[i].second, addr);
2553
82
    }
2554
53
}
2555
2556
void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv& inv)
2557
37.0k
{
2558
37.0k
    std::shared_ptr<const CBlock> a_recent_block;
2559
37.0k
    std::shared_ptr<const CBlockHeaderAndShortTxIDs> a_recent_compact_block;
2560
37.0k
    {
2561
37.0k
        LOCK(m_most_recent_block_mutex);
2562
37.0k
        a_recent_block = m_most_recent_block;
2563
37.0k
        a_recent_compact_block = m_most_recent_compact_block;
2564
37.0k
    }
2565
2566
37.0k
    bool need_activate_chain = false;
2567
37.0k
    {
2568
37.0k
        LOCK(cs_main);
2569
37.0k
        const CBlockIndex* pindex = m_chainman.m_blockman.LookupBlockIndex(inv.hash);
2570
37.0k
        if (pindex) {
2571
37.0k
            if (pindex->HaveNumChainTxs() && !pindex->IsValid(BLOCK_VALID_SCRIPTS) &&
2572
37.0k
                    pindex->IsValid(BLOCK_VALID_TREE)) {
2573
                // If we have the block and all of its parents, but have not yet validated it,
2574
                // we might be in the middle of connecting it (ie in the unlock of cs_main
2575
                // before ActivateBestChain but after AcceptBlock).
2576
                // In this case, we need to run ActivateBestChain prior to checking the relay
2577
                // conditions below.
2578
4
                need_activate_chain = true;
2579
4
            }
2580
37.0k
        }
2581
37.0k
    } // release cs_main before calling ActivateBestChain
2582
37.0k
    if (need_activate_chain) {
2583
4
        BlockValidationState state;
2584
4
        if (!m_chainman.ActiveChainstate().ActivateBestChain(state, a_recent_block)) {
2585
0
            LogDebug(BCLog::NET, "failed to activate chain (%s)\n", state.ToString());
2586
0
        }
2587
4
    }
2588
2589
37.0k
    const CBlockIndex* pindex{nullptr};
2590
37.0k
    const CBlockIndex* tip{nullptr};
2591
37.0k
    bool can_direct_fetch{false};
2592
37.0k
    FlatFilePos block_pos{};
2593
37.0k
    {
2594
37.0k
        LOCK(cs_main);
2595
37.0k
        pindex = m_chainman.m_blockman.LookupBlockIndex(inv.hash);
2596
37.0k
        if (!pindex) {
2597
0
            return;
2598
0
        }
2599
37.0k
        if (!BlockRequestAllowed(*pindex)) {
2600
1
            LogDebug(BCLog::NET, "%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom.GetId());
2601
1
            return;
2602
1
        }
2603
        // disconnect node in case we have reached the outbound limit for serving historical blocks
2604
37.0k
        if (m_connman.OutboundTargetReached(true) &&
2605
37.0k
            (((m_chainman.m_best_header != nullptr) && (m_chainman.m_best_header->GetBlockTime() - pindex->GetBlockTime() > HISTORICAL_BLOCK_AGE)) || inv.IsMsgFilteredBlk()) &&
2606
37.0k
            !pfrom.HasPermission(NetPermissionFlags::Download) // nodes with the download permission may exceed target
2607
37.0k
        ) {
2608
2
            LogDebug(BCLog::NET, "historical block serving limit reached, %s", pfrom.DisconnectMsg());
2609
2
            pfrom.fDisconnect = true;
2610
2
            return;
2611
2
        }
2612
37.0k
        tip = m_chainman.ActiveChain().Tip();
2613
        // Avoid leaking prune-height by never sending blocks below the NODE_NETWORK_LIMITED threshold
2614
37.0k
        if (!pfrom.HasPermission(NetPermissionFlags::NoBan) && (
2615
35.0k
                (((peer.m_our_services & NODE_NETWORK_LIMITED) == NODE_NETWORK_LIMITED) && ((peer.m_our_services & NODE_NETWORK) != NODE_NETWORK) && (tip->nHeight - pindex->nHeight > (int)NODE_NETWORK_LIMITED_MIN_BLOCKS + 2 /* add two blocks buffer extension for possible races */) )
2616
35.0k
           )) {
2617
2
            LogDebug(BCLog::NET, "Ignore block request below NODE_NETWORK_LIMITED threshold, %s", pfrom.DisconnectMsg());
2618
            //disconnect node and prevent it from stalling (would otherwise wait for the missing block)
2619
2
            pfrom.fDisconnect = true;
2620
2
            return;
2621
2
        }
2622
        // Pruned nodes may have deleted the block, so check whether
2623
        // it's available before trying to send.
2624
37.0k
        if (!(pindex->nStatus & BLOCK_HAVE_DATA)) {
2625
0
            return;
2626
0
        }
2627
37.0k
        can_direct_fetch = CanDirectFetch();
2628
37.0k
        block_pos = pindex->GetBlockPos();
2629
37.0k
    }
2630
2631
0
    std::shared_ptr<const CBlock> pblock;
2632
37.0k
    if (a_recent_block && a_recent_block->GetHash() == inv.hash) {
2633
3.74k
        pblock = a_recent_block;
2634
33.3k
    } else if (inv.IsMsgWitnessBlk()) {
2635
        // Fast-path: in this case it is possible to serve the block directly from disk,
2636
        // as the network format matches the format on disk
2637
28.4k
        if (const auto block_data{m_chainman.m_blockman.ReadRawBlock(block_pos)}) {
2638
28.4k
            MakeAndPushMessage(pfrom, NetMsgType::BLOCK, std::span{*block_data});
2639
28.4k
        } else {
2640
0
            if (WITH_LOCK(m_chainman.GetMutex(), return m_chainman.m_blockman.IsBlockPruned(*pindex))) {
2641
0
                LogDebug(BCLog::NET, "Block was pruned before it could be read, %s", pfrom.DisconnectMsg());
2642
0
            } else {
2643
0
                LogError("Cannot load block from disk, %s", pfrom.DisconnectMsg());
2644
0
            }
2645
0
            pfrom.fDisconnect = true;
2646
0
            return;
2647
0
        }
2648
        // Don't set pblock as we've sent the block
2649
28.4k
    } else {
2650
        // Send block from disk
2651
4.88k
        std::shared_ptr<CBlock> pblockRead = std::make_shared<CBlock>();
2652
4.88k
        if (!m_chainman.m_blockman.ReadBlock(*pblockRead, block_pos, inv.hash)) {
2653
0
            if (WITH_LOCK(m_chainman.GetMutex(), return m_chainman.m_blockman.IsBlockPruned(*pindex))) {
2654
0
                LogDebug(BCLog::NET, "Block was pruned before it could be read, %s", pfrom.DisconnectMsg());
2655
0
            } else {
2656
0
                LogError("Cannot load block from disk, %s", pfrom.DisconnectMsg());
2657
0
            }
2658
0
            pfrom.fDisconnect = true;
2659
0
            return;
2660
0
        }
2661
4.88k
        pblock = pblockRead;
2662
4.88k
    }
2663
37.0k
    if (pblock) {
2664
8.63k
        if (inv.IsMsgBlk()) {
2665
7.90k
            MakeAndPushMessage(pfrom, NetMsgType::BLOCK, TX_NO_WITNESS(*pblock));
2666
7.90k
        } else if (inv.IsMsgWitnessBlk()) {
2667
361
            MakeAndPushMessage(pfrom, NetMsgType::BLOCK, TX_WITH_WITNESS(*pblock));
2668
368
        } else if (inv.IsMsgFilteredBlk()) {
2669
7
            bool sendMerkleBlock = false;
2670
7
            CMerkleBlock merkleBlock;
2671
7
            if (auto tx_relay = peer.GetTxRelay(); tx_relay != nullptr) {
2672
7
                LOCK(tx_relay->m_bloom_filter_mutex);
2673
7
                if (tx_relay->m_bloom_filter) {
2674
4
                    sendMerkleBlock = true;
2675
4
                    merkleBlock = CMerkleBlock(*pblock, *tx_relay->m_bloom_filter);
2676
4
                }
2677
7
            }
2678
7
            if (sendMerkleBlock) {
2679
4
                MakeAndPushMessage(pfrom, NetMsgType::MERKLEBLOCK, merkleBlock);
2680
                // CMerkleBlock just contains hashes, so also push any transactions in the block the client did not see
2681
                // This avoids hurting performance by pointlessly requiring a round-trip
2682
                // Note that there is currently no way for a node to request any single transactions we didn't send here -
2683
                // they must either disconnect and retry or request the full block.
2684
                // Thus, the protocol spec specified allows for us to provide duplicate txn here,
2685
                // however we MUST always provide at least what the remote peer needs
2686
4
                for (const auto& [tx_idx, _] : merkleBlock.vMatchedTxn)
2687
2
                    MakeAndPushMessage(pfrom, NetMsgType::TX, TX_NO_WITNESS(*pblock->vtx[tx_idx]));
2688
4
            }
2689
            // else
2690
            // no response
2691
361
        } else if (inv.IsMsgCmpctBlk()) {
2692
            // If a peer is asking for old blocks, we're almost guaranteed
2693
            // they won't have a useful mempool to match against a compact block,
2694
            // and we don't feel like constructing the object for them, so
2695
            // instead we respond with the full, non-compact block.
2696
361
            if (can_direct_fetch && pindex->nHeight >= tip->nHeight - MAX_CMPCTBLOCK_DEPTH) {
2697
310
                if (a_recent_compact_block && a_recent_compact_block->header.GetHash() == inv.hash) {
2698
185
                    MakeAndPushMessage(pfrom, NetMsgType::CMPCTBLOCK, *a_recent_compact_block);
2699
185
                } else {
2700
125
                    CBlockHeaderAndShortTxIDs cmpctblock{*pblock, m_rng.rand64()};
2701
125
                    MakeAndPushMessage(pfrom, NetMsgType::CMPCTBLOCK, cmpctblock);
2702
125
                }
2703
310
            } else {
2704
51
                MakeAndPushMessage(pfrom, NetMsgType::BLOCK, TX_WITH_WITNESS(*pblock));
2705
51
            }
2706
361
        }
2707
8.63k
    }
2708
2709
37.0k
    {
2710
37.0k
        LOCK(peer.m_block_inv_mutex);
2711
        // Trigger the peer node to send a getblocks request for the next batch of inventory
2712
37.0k
        if (inv.hash == peer.m_continuation_block) {
2713
            // Send immediately. This must send even if redundant,
2714
            // and we want it right after the last block so they don't
2715
            // wait for other stuff first.
2716
0
            std::vector<CInv> vInv;
2717
0
            vInv.emplace_back(MSG_BLOCK, tip->GetBlockHash());
2718
0
            MakeAndPushMessage(pfrom, NetMsgType::INV, vInv);
2719
0
            peer.m_continuation_block.SetNull();
2720
0
        }
2721
37.0k
    }
2722
37.0k
}
2723
2724
CTransactionRef PeerManagerImpl::FindTxForGetData(const Peer::TxRelay& tx_relay, const GenTxid& gtxid)
2725
13.1k
{
2726
    // If a tx was in the mempool prior to the last INV for this peer, permit the request.
2727
13.1k
    auto txinfo{std::visit(
2728
13.1k
        [&](const auto& id) {
2729
13.1k
            return m_mempool.info_for_relay(id, WITH_LOCK(tx_relay.m_tx_inventory_mutex, return tx_relay.m_last_inv_sequence));
2730
13.1k
        },
net_processing.cpp:_ZZN12_GLOBAL__N_115PeerManagerImpl16FindTxForGetDataERKNS_4Peer7TxRelayERK7GenTxidENK3$_0clI22transaction_identifierILb0EEEEDaRKT_
Line
Count
Source
2728
73
        [&](const auto& id) {
2729
73
            return m_mempool.info_for_relay(id, WITH_LOCK(tx_relay.m_tx_inventory_mutex, return tx_relay.m_last_inv_sequence));
2730
73
        },
net_processing.cpp:_ZZN12_GLOBAL__N_115PeerManagerImpl16FindTxForGetDataERKNS_4Peer7TxRelayERK7GenTxidENK3$_0clI22transaction_identifierILb1EEEEDaRKT_
Line
Count
Source
2728
13.1k
        [&](const auto& id) {
2729
13.1k
            return m_mempool.info_for_relay(id, WITH_LOCK(tx_relay.m_tx_inventory_mutex, return tx_relay.m_last_inv_sequence));
2730
13.1k
        },
2731
13.1k
        gtxid)};
2732
13.1k
    if (txinfo.tx) {
2733
13.1k
        return std::move(txinfo.tx);
2734
13.1k
    }
2735
2736
    // Or it might be from the most recent block
2737
33
    {
2738
33
        LOCK(m_most_recent_block_mutex);
2739
33
        if (m_most_recent_block_txs != nullptr) {
2740
33
            auto it = m_most_recent_block_txs->find(gtxid);
2741
33
            if (it != m_most_recent_block_txs->end()) return it->second;
2742
33
        }
2743
33
    }
2744
2745
14
    return {};
2746
33
}
2747
2748
void PeerManagerImpl::ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic<bool>& interruptMsgProc)
2749
43.2k
{
2750
43.2k
    AssertLockNotHeld(cs_main);
2751
2752
43.2k
    auto tx_relay = peer.GetTxRelay();
2753
2754
43.2k
    std::deque<CInv>::iterator it = peer.m_getdata_requests.begin();
2755
43.2k
    std::vector<CInv> vNotFound;
2756
2757
    // Process as many TX items from the front of the getdata queue as
2758
    // possible, since they're common and it's efficient to batch process
2759
    // them.
2760
56.4k
    while (it != peer.m_getdata_requests.end() && it->IsGenTxMsg()) {
2761
13.1k
        if (interruptMsgProc) return;
2762
        // The send buffer provides backpressure. If there's no space in
2763
        // the buffer, pause processing until the next call.
2764
13.1k
        if (pfrom.fPauseSend) break;
2765
2766
13.1k
        const CInv &inv = *it++;
2767
2768
13.1k
        if (tx_relay == nullptr) {
2769
            // Ignore GETDATA requests for transactions from block-relay-only
2770
            // peers and peers that asked us not to announce transactions.
2771
1
            continue;
2772
1
        }
2773
2774
13.1k
        if (auto tx{FindTxForGetData(*tx_relay, ToGenTxid(inv))}) {
2775
            // WTX and WITNESS_TX imply we serialize with witness
2776
13.1k
            const auto maybe_with_witness = (inv.IsMsgTx() ? TX_NO_WITNESS : TX_WITH_WITNESS);
2777
13.1k
            MakeAndPushMessage(pfrom, NetMsgType::TX, maybe_with_witness(*tx));
2778
13.1k
            m_mempool.RemoveUnbroadcastTx(tx->GetHash());
2779
13.1k
        } else {
2780
14
            vNotFound.push_back(inv);
2781
14
        }
2782
13.1k
    }
2783
2784
    // Only process one BLOCK item per call, since they're uncommon and can be
2785
    // expensive to process.
2786
43.2k
    if (it != peer.m_getdata_requests.end() && !pfrom.fPauseSend) {
2787
37.0k
        const CInv &inv = *it++;
2788
37.0k
        if (inv.IsGenBlkMsg()) {
2789
37.0k
            ProcessGetBlockData(pfrom, peer, inv);
2790
37.0k
        }
2791
        // else: If the first item on the queue is an unknown type, we erase it
2792
        // and continue processing the queue on the next call.
2793
        // NOTE: previously we wouldn't do so and the peer sending us a malformed GETDATA could
2794
        // result in never making progress and this thread using 100% allocated CPU. See
2795
        // https://bitcoincore.org/en/2024/07/03/disclose-getdata-cpu.
2796
37.0k
    }
2797
2798
43.2k
    peer.m_getdata_requests.erase(peer.m_getdata_requests.begin(), it);
2799
2800
43.2k
    if (!vNotFound.empty()) {
2801
        // Let the peer know that we didn't find what it asked for, so it doesn't
2802
        // have to wait around forever.
2803
        // SPV clients care about this message: it's needed when they are
2804
        // recursively walking the dependencies of relevant unconfirmed
2805
        // transactions. SPV clients want to do that because they want to know
2806
        // about (and store and rebroadcast and risk analyze) the dependencies
2807
        // of transactions relevant to them, without having to download the
2808
        // entire memory pool.
2809
        // Also, other nodes can use these messages to automatically request a
2810
        // transaction from some other peer that announced it, and stop
2811
        // waiting for us to respond.
2812
        // In normal operation, we often send NOTFOUND messages for parents of
2813
        // transactions that we relay; if a peer is missing a parent, they may
2814
        // assume we have them and request the parents from us.
2815
11
        MakeAndPushMessage(pfrom, NetMsgType::NOTFOUND, vNotFound);
2816
11
    }
2817
43.2k
}
2818
2819
uint32_t PeerManagerImpl::GetFetchFlags(const Peer& peer) const
2820
37.6k
{
2821
37.6k
    uint32_t nFetchFlags = 0;
2822
37.6k
    if (CanServeWitnesses(peer)) {
2823
37.6k
        nFetchFlags |= MSG_WITNESS_FLAG;
2824
37.6k
    }
2825
37.6k
    return nFetchFlags;
2826
37.6k
}
2827
2828
void PeerManagerImpl::SendBlockTransactions(CNode& pfrom, Peer& peer, const CBlock& block, const BlockTransactionsRequest& req)
2829
550
{
2830
550
    BlockTransactions resp(req);
2831
1.99k
    for (size_t i = 0; i < req.indexes.size(); i++) {
2832
1.44k
        if (req.indexes[i] >= block.vtx.size()) {
2833
1
            Misbehaving(peer, "getblocktxn with out-of-bounds tx indices");
2834
1
            return;
2835
1
        }
2836
1.44k
        resp.txn[i] = block.vtx[req.indexes[i]];
2837
1.44k
    }
2838
2839
549
    if (util::log::ShouldDebugLog(BCLog::CMPCTBLOCK)) {
2840
549
        uint32_t tx_requested_size{0};
2841
1.44k
        for (const auto& tx : resp.txn) tx_requested_size += tx->ComputeTotalSize();
2842
549
        LogDebug(BCLog::CMPCTBLOCK, "%s sent us a GETBLOCKTXN for block %s, sending a BLOCKTXN with %u txns. (%u bytes)", pfrom.LogPeer(), block.GetHash().ToString(), resp.txn.size(), tx_requested_size);
2843
549
    }
2844
549
    MakeAndPushMessage(pfrom, NetMsgType::BLOCKTXN, resp);
2845
549
}
2846
2847
bool PeerManagerImpl::CheckHeadersPoW(const std::vector<CBlockHeader>& headers, Peer& peer)
2848
7.28k
{
2849
    // Do these headers have proof-of-work matching what's claimed?
2850
7.28k
    if (!HasValidProofOfWork(headers, m_chainparams.GetConsensus())) {
2851
1
        Misbehaving(peer, "header with invalid proof of work");
2852
1
        return false;
2853
1
    }
2854
2855
    // Are these headers connected to each other?
2856
7.28k
    if (!CheckHeadersAreContinuous(headers)) {
2857
1
        Misbehaving(peer, "non-continuous headers sequence");
2858
1
        return false;
2859
1
    }
2860
7.28k
    return true;
2861
7.28k
}
2862
2863
arith_uint256 PeerManagerImpl::GetAntiDoSWorkThreshold()
2864
60.9k
{
2865
60.9k
    arith_uint256 near_chaintip_work = 0;
2866
60.9k
    LOCK(cs_main);
2867
60.9k
    if (m_chainman.ActiveChain().Tip() != nullptr) {
2868
60.9k
        const CBlockIndex *tip = m_chainman.ActiveChain().Tip();
2869
        // Use a 144 block buffer, so that we'll accept headers that fork from
2870
        // near our tip.
2871
60.9k
        near_chaintip_work = tip->nChainWork - std::min<arith_uint256>(144*GetBlockProof(*tip), tip->nChainWork);
2872
60.9k
    }
2873
60.9k
    return std::max(near_chaintip_work, m_chainman.MinimumChainWork());
2874
60.9k
}
2875
2876
/**
2877
 * Special handling for unconnecting headers that might be part of a block
2878
 * announcement.
2879
 *
2880
 * We'll send a getheaders message in response to try to connect the chain.
2881
 */
2882
void PeerManagerImpl::HandleUnconnectingHeaders(CNode& pfrom, Peer& peer,
2883
        const std::vector<CBlockHeader>& headers)
2884
199
{
2885
    // Try to fill in the missing headers.
2886
199
    const CBlockIndex* best_header{WITH_LOCK(cs_main, return m_chainman.m_best_header)};
2887
199
    if (MaybeSendGetHeaders(pfrom, GetLocator(best_header), peer)) {
2888
199
        LogDebug(BCLog::NET, "received header %s: missing prev block %s, sending getheaders (%d) to end (peer=%d)\n",
2889
199
            headers[0].GetHash().ToString(),
2890
199
            headers[0].hashPrevBlock.ToString(),
2891
199
            best_header->nHeight,
2892
199
            pfrom.GetId());
2893
199
    }
2894
2895
    // Set hashLastUnknownBlock for this peer, so that if we
2896
    // eventually get the headers - even from a different peer -
2897
    // we can use this peer to download.
2898
199
    WITH_LOCK(cs_main, UpdateBlockAvailability(pfrom.GetId(), headers.back().GetHash()));
2899
199
}
2900
2901
bool PeerManagerImpl::CheckHeadersAreContinuous(const std::vector<CBlockHeader>& headers) const
2902
7.28k
{
2903
7.28k
    uint256 hashLastBlock;
2904
455k
    for (const CBlockHeader& header : headers) {
2905
455k
        if (!hashLastBlock.IsNull() && header.hashPrevBlock != hashLastBlock) {
2906
1
            return false;
2907
1
        }
2908
455k
        hashLastBlock = header.GetHash();
2909
455k
    }
2910
7.28k
    return true;
2911
7.28k
}
2912
2913
bool PeerManagerImpl::IsContinuationOfLowWorkHeadersSync(Peer& peer, CNode& pfrom, std::vector<CBlockHeader>& headers)
2914
7.28k
{
2915
7.28k
    if (peer.m_headers_sync) {
2916
21
        auto result = peer.m_headers_sync->ProcessNextHeaders(headers, headers.size() == m_opts.max_headers_result);
2917
        // If it is a valid continuation, we should treat the existing getheaders request as responded to.
2918
21
        if (result.success) peer.m_last_getheaders_timestamp = {};
2919
21
        if (result.request_more) {
2920
16
            auto locator = peer.m_headers_sync->NextHeadersRequestLocator();
2921
            // If we were instructed to ask for a locator, it should not be empty.
2922
16
            Assume(!locator.vHave.empty());
2923
            // We can only be instructed to request more if processing was successful.
2924
16
            Assume(result.success);
2925
16
            if (!locator.vHave.empty()) {
2926
                // It should be impossible for the getheaders request to fail,
2927
                // because we just cleared the last getheaders timestamp.
2928
16
                bool sent_getheaders = MaybeSendGetHeaders(pfrom, locator, peer);
2929
16
                Assume(sent_getheaders);
2930
16
                LogDebug(BCLog::NET, "more getheaders (from %s) to peer=%d\n",
2931
16
                    locator.vHave.front().ToString(), pfrom.GetId());
2932
16
            }
2933
16
        }
2934
2935
21
        if (peer.m_headers_sync->GetState() == HeadersSyncState::State::FINAL) {
2936
5
            peer.m_headers_sync.reset(nullptr);
2937
2938
            // Delete this peer's entry in m_headers_presync_stats.
2939
            // If this is m_headers_presync_bestpeer, it will be replaced later
2940
            // by the next peer that triggers the else{} branch below.
2941
5
            LOCK(m_headers_presync_mutex);
2942
5
            m_headers_presync_stats.erase(pfrom.GetId());
2943
16
        } else {
2944
            // Build statistics for this peer's sync.
2945
16
            HeadersPresyncStats stats;
2946
16
            stats.first = peer.m_headers_sync->GetPresyncWork();
2947
16
            if (peer.m_headers_sync->GetState() == HeadersSyncState::State::PRESYNC) {
2948
8
                stats.second = {peer.m_headers_sync->GetPresyncHeight(),
2949
8
                                peer.m_headers_sync->GetPresyncTime()};
2950
8
            }
2951
2952
            // Update statistics in stats.
2953
16
            LOCK(m_headers_presync_mutex);
2954
16
            m_headers_presync_stats[pfrom.GetId()] = stats;
2955
16
            auto best_it = m_headers_presync_stats.find(m_headers_presync_bestpeer);
2956
16
            bool best_updated = false;
2957
16
            if (best_it == m_headers_presync_stats.end()) {
2958
                // If the cached best peer is outdated, iterate over all remaining ones (including
2959
                // newly updated one) to find the best one.
2960
4
                NodeId peer_best{-1};
2961
4
                const HeadersPresyncStats* stat_best{nullptr};
2962
4
                for (const auto& [peer, stat] : m_headers_presync_stats) {
2963
4
                    if (!stat_best || stat > *stat_best) {
2964
4
                        peer_best = peer;
2965
4
                        stat_best = &stat;
2966
4
                    }
2967
4
                }
2968
4
                m_headers_presync_bestpeer = peer_best;
2969
4
                best_updated = (peer_best == pfrom.GetId());
2970
12
            } else if (best_it->first == pfrom.GetId() || stats > best_it->second) {
2971
                // pfrom was and remains the best peer, or pfrom just became best.
2972
12
                m_headers_presync_bestpeer = pfrom.GetId();
2973
12
                best_updated = true;
2974
12
            }
2975
16
            if (best_updated && stats.second.has_value()) {
2976
                // If the best peer updated, and it is in its first phase, signal.
2977
8
                m_headers_presync_should_signal = true;
2978
8
            }
2979
16
        }
2980
2981
21
        if (result.success) {
2982
            // We only overwrite the headers passed in if processing was
2983
            // successful.
2984
21
            headers.swap(result.pow_validated_headers);
2985
21
        }
2986
2987
21
        return result.success;
2988
21
    }
2989
    // Either we didn't have a sync in progress, or something went wrong
2990
    // processing these headers, or we are returning headers to the caller to
2991
    // process.
2992
7.26k
    return false;
2993
7.28k
}
2994
2995
bool PeerManagerImpl::TryLowWorkHeadersSync(Peer& peer, CNode& pfrom, const CBlockIndex& chain_start_header, std::vector<CBlockHeader>& headers)
2996
1.77k
{
2997
    // Calculate the claimed total work on this chain.
2998
1.77k
    arith_uint256 total_work = chain_start_header.nChainWork + CalculateClaimedHeadersWork(headers);
2999
3000
    // Our dynamic anti-DoS threshold (minimum work required on a headers chain
3001
    // before we'll store it)
3002
1.77k
    arith_uint256 minimum_chain_work = GetAntiDoSWorkThreshold();
3003
3004
    // Avoid DoS via low-difficulty-headers by only processing if the headers
3005
    // are part of a chain with sufficient work.
3006
1.77k
    if (total_work < minimum_chain_work) {
3007
        // Only try to sync with this peer if their headers message was full;
3008
        // otherwise they don't have more headers after this so no point in
3009
        // trying to sync their too-little-work chain.
3010
468
        if (headers.size() == m_opts.max_headers_result) {
3011
            // Note: we could advance to the last header in this set that is
3012
            // known to us, rather than starting at the first header (which we
3013
            // may already have); however this is unlikely to matter much since
3014
            // ProcessHeadersMessage() already handles the case where all
3015
            // headers in a received message are already known and are
3016
            // ancestors of m_best_header or chainActive.Tip(), by skipping
3017
            // this logic in that case. So even if the first header in this set
3018
            // of headers is known, some header in this set must be new, so
3019
            // advancing to the first unknown header would be a small effect.
3020
6
            LOCK(peer.m_headers_sync_mutex);
3021
6
            peer.m_headers_sync.reset(new HeadersSyncState(peer.m_id, m_chainparams.GetConsensus(),
3022
6
                m_chainparams.HeadersSync(), chain_start_header, minimum_chain_work));
3023
3024
            // Now a HeadersSyncState object for tracking this synchronization
3025
            // is created, process the headers using it as normal. Failures are
3026
            // handled inside of IsContinuationOfLowWorkHeadersSync.
3027
6
            (void)IsContinuationOfLowWorkHeadersSync(peer, pfrom, headers);
3028
462
        } else {
3029
462
            LogDebug(BCLog::NET, "Ignoring low-work chain (height=%u) from peer=%d\n", chain_start_header.nHeight + headers.size(), pfrom.GetId());
3030
462
        }
3031
3032
        // The peer has not yet given us a chain that meets our work threshold,
3033
        // so we want to prevent further processing of the headers in any case.
3034
468
        headers = {};
3035
468
        return true;
3036
468
    }
3037
3038
1.30k
    return false;
3039
1.77k
}
3040
3041
bool PeerManagerImpl::IsAncestorOfBestHeaderOrTip(const CBlockIndex* header)
3042
7.06k
{
3043
7.06k
    if (header == nullptr) {
3044
2.97k
        return false;
3045
4.09k
    } else if (m_chainman.m_best_header != nullptr && header == m_chainman.m_best_header->GetAncestor(header->nHeight)) {
3046
4.07k
        return true;
3047
4.07k
    } else if (m_chainman.ActiveChain().Contains(*header)) {
3048
2
        return true;
3049
2
    }
3050
12
    return false;
3051
7.06k
}
3052
3053
bool PeerManagerImpl::MaybeSendGetHeaders(CNode& pfrom, const CBlockLocator& locator, Peer& peer)
3054
3.30k
{
3055
3.30k
    const auto current_time = NodeClock::now();
3056
3057
    // Only allow a new getheaders message to go out if we don't have a recent
3058
    // one already in-flight
3059
3.30k
    if (current_time - peer.m_last_getheaders_timestamp > HEADERS_RESPONSE_TIME) {
3060
2.97k
        MakeAndPushMessage(pfrom, NetMsgType::GETHEADERS, locator, uint256());
3061
2.97k
        peer.m_last_getheaders_timestamp = current_time;
3062
2.97k
        return true;
3063
2.97k
    }
3064
332
    return false;
3065
3.30k
}
3066
3067
/*
3068
 * Given a new headers tip ending in last_header, potentially request blocks towards that tip.
3069
 * We require that the given tip have at least as much work as our tip, and for
3070
 * our current tip to be "close to synced" (see CanDirectFetch()).
3071
 */
3072
void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex& last_header)
3073
6.56k
{
3074
6.56k
    LOCK(cs_main);
3075
6.56k
    CNodeState *nodestate = State(pfrom.GetId());
3076
3077
6.56k
    if (CanDirectFetch() && last_header.IsValid(BLOCK_VALID_TREE) && m_chainman.ActiveChain().Tip()->nChainWork <= last_header.nChainWork) {
3078
4.19k
        std::vector<const CBlockIndex*> vToFetch;
3079
4.19k
        const CBlockIndex* pindexWalk{&last_header};
3080
        // Calculate all the blocks we'd need to switch to last_header, up to a limit.
3081
33.9k
        while (pindexWalk && !m_chainman.ActiveChain().Contains(*pindexWalk) && vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
3082
29.7k
            if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
3083
29.7k
                    !IsBlockRequested(pindexWalk->GetBlockHash()) &&
3084
29.7k
                    (!DeploymentActiveAt(*pindexWalk, m_chainman, Consensus::DEPLOYMENT_SEGWIT) || CanServeWitnesses(peer))) {
3085
                // We don't have this block, and it's not yet in flight.
3086
18.1k
                vToFetch.push_back(pindexWalk);
3087
18.1k
            }
3088
29.7k
            pindexWalk = pindexWalk->pprev;
3089
29.7k
        }
3090
        // If pindexWalk still isn't on our main chain, we're looking at a
3091
        // very large reorg at a time we think we're close to caught up to
3092
        // the main chain -- this shouldn't really happen.  Bail out on the
3093
        // direct fetch and rely on parallel download instead.
3094
        // Common ancestor must exist (genesis).
3095
4.19k
        if (!m_chainman.ActiveChain().Contains(*Assert(pindexWalk))) {
3096
877
            LogDebug(BCLog::NET, "Large reorg, won't direct fetch to %s (%d)\n",
3097
877
                     last_header.GetBlockHash().ToString(),
3098
877
                     last_header.nHeight);
3099
3.31k
        } else {
3100
3.31k
            std::vector<CInv> vGetData;
3101
            // Download as much as possible, from earliest to latest.
3102
3.31k
            for (const CBlockIndex* pindex : vToFetch | std::views::reverse) {
3103
2.58k
                if (nodestate->vBlocksInFlight.size() >= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
3104
                    // Can't download any more from this peer
3105
111
                    break;
3106
111
                }
3107
2.47k
                uint32_t nFetchFlags = GetFetchFlags(peer);
3108
2.47k
                vGetData.emplace_back(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash());
3109
2.47k
                BlockRequested(pfrom.GetId(), *pindex);
3110
2.47k
                LogDebug(BCLog::NET, "Requesting block %s from peer=%d",
3111
2.47k
                         pindex->GetBlockHash().ToString(), pfrom.GetId());
3112
2.47k
            }
3113
3.31k
            if (vGetData.size() > 1) {
3114
379
                LogDebug(BCLog::NET, "Downloading blocks toward %s (%d) via headers direct fetch\n",
3115
379
                         last_header.GetBlockHash().ToString(),
3116
379
                         last_header.nHeight);
3117
379
            }
3118
3.31k
            if (vGetData.size() > 0) {
3119
1.72k
                if (!m_opts.ignore_incoming_txs &&
3120
1.72k
                        nodestate->m_provides_cmpctblocks &&
3121
1.72k
                        vGetData.size() == 1 &&
3122
1.72k
                        mapBlocksInFlight.size() == 1 &&
3123
1.72k
                        last_header.pprev->IsValid(BLOCK_VALID_CHAIN)) {
3124
                    // In any case, we want to download using a compact block, not a regular one
3125
365
                    vGetData[0] = CInv(MSG_CMPCT_BLOCK, vGetData[0].hash);
3126
365
                }
3127
1.72k
                MakeAndPushMessage(pfrom, NetMsgType::GETDATA, vGetData);
3128
1.72k
            }
3129
3.31k
        }
3130
4.19k
    }
3131
6.56k
}
3132
3133
/**
3134
 * Given receipt of headers from a peer ending in last_header, along with
3135
 * whether that header was new and whether the headers message was full,
3136
 * update the state we keep for the peer.
3137
 */
3138
void PeerManagerImpl::UpdatePeerStateForReceivedHeaders(CNode& pfrom, Peer& peer,
3139
        const CBlockIndex& last_header, bool received_new_header, bool may_have_more_headers)
3140
6.56k
{
3141
6.56k
    LOCK(cs_main);
3142
6.56k
    CNodeState *nodestate = State(pfrom.GetId());
3143
3144
6.56k
    UpdateBlockAvailability(pfrom.GetId(), last_header.GetBlockHash());
3145
3146
    // From here, pindexBestKnownBlock should be guaranteed to be non-null,
3147
    // because it is set in UpdateBlockAvailability. Some nullptr checks
3148
    // are still present, however, as belt-and-suspenders.
3149
3150
6.56k
    if (received_new_header && last_header.nChainWork > m_chainman.ActiveChain().Tip()->nChainWork) {
3151
2.31k
        nodestate->m_last_block_announcement = GetTime();
3152
2.31k
    }
3153
3154
    // If we're in IBD, we want outbound peers that will serve us a useful
3155
    // chain. Disconnect peers that are on chains with insufficient work.
3156
6.56k
    if (m_chainman.IsInitialBlockDownload() && !may_have_more_headers) {
3157
        // If the peer has no more headers to give us, then we know we have
3158
        // their tip.
3159
1.07k
        if (nodestate->pindexBestKnownBlock && nodestate->pindexBestKnownBlock->nChainWork < m_chainman.MinimumChainWork()) {
3160
            // This peer has too little work on their headers chain to help
3161
            // us sync -- disconnect if it is an outbound disconnection
3162
            // candidate.
3163
            // Note: We compare their tip to the minimum chain work (rather than
3164
            // m_chainman.ActiveChain().Tip()) because we won't start block download
3165
            // until we have a headers chain that has at least
3166
            // the minimum chain work, even if a peer has a chain past our tip,
3167
            // as an anti-DoS measure.
3168
572
            if (pfrom.IsOutboundOrBlockRelayConn()) {
3169
0
                LogInfo("outbound peer headers chain has insufficient work, %s", pfrom.DisconnectMsg());
3170
0
                pfrom.fDisconnect = true;
3171
0
            }
3172
572
        }
3173
1.07k
    }
3174
3175
    // If this is an outbound full-relay peer, check to see if we should protect
3176
    // it from the bad/lagging chain logic.
3177
    // Note that outbound block-relay peers are excluded from this protection, and
3178
    // thus always subject to eviction under the bad/lagging chain logic.
3179
    // See ChainSyncTimeoutState.
3180
6.56k
    if (!pfrom.fDisconnect && pfrom.IsFullOutboundConn() && nodestate->pindexBestKnownBlock != nullptr) {
3181
46
        if (m_outbound_peers_with_protect_from_disconnect < MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT && nodestate->pindexBestKnownBlock->nChainWork >= m_chainman.ActiveChain().Tip()->nChainWork && !nodestate->m_chain_sync.m_protect) {
3182
18
            LogDebug(BCLog::NET, "Protecting outbound peer=%d from eviction\n", pfrom.GetId());
3183
18
            nodestate->m_chain_sync.m_protect = true;
3184
18
            ++m_outbound_peers_with_protect_from_disconnect;
3185
18
        }
3186
46
    }
3187
6.56k
}
3188
3189
void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
3190
                                            std::vector<CBlockHeader>&& headers,
3191
                                            bool via_compact_block)
3192
7.62k
{
3193
7.62k
    size_t nCount = headers.size();
3194
3195
7.62k
    if (nCount == 0) {
3196
        // Nothing interesting. Stop asking this peers for more headers.
3197
        // If we were in the middle of headers sync, receiving an empty headers
3198
        // message suggests that the peer suddenly has nothing to give us
3199
        // (perhaps it reorged to our chain). Clear download state for this peer.
3200
344
        LOCK(peer.m_headers_sync_mutex);
3201
344
        if (peer.m_headers_sync) {
3202
0
            peer.m_headers_sync.reset(nullptr);
3203
0
            LOCK(m_headers_presync_mutex);
3204
0
            m_headers_presync_stats.erase(pfrom.GetId());
3205
0
        }
3206
        // A headers message with no headers cannot be an announcement, so assume
3207
        // it is a response to our last getheaders request, if there is one.
3208
344
        peer.m_last_getheaders_timestamp = {};
3209
344
        return;
3210
344
    }
3211
3212
    // Before we do any processing, make sure these pass basic sanity checks.
3213
    // We'll rely on headers having valid proof-of-work further down, as an
3214
    // anti-DoS criteria (note: this check is required before passing any
3215
    // headers into HeadersSyncState).
3216
7.28k
    if (!CheckHeadersPoW(headers, peer)) {
3217
        // Misbehaving() calls are handled within CheckHeadersPoW(), so we can
3218
        // just return. (Note that even if a header is announced via compact
3219
        // block, the header itself should be valid, so this type of error can
3220
        // always be punished.)
3221
2
        return;
3222
2
    }
3223
3224
7.28k
    const CBlockIndex *pindexLast = nullptr;
3225
3226
    // We'll set already_validated_work to true if these headers are
3227
    // successfully processed as part of a low-work headers sync in progress
3228
    // (either in PRESYNC or REDOWNLOAD phase).
3229
    // If true, this will mean that any headers returned to us (ie during
3230
    // REDOWNLOAD) can be validated without further anti-DoS checks.
3231
7.28k
    bool already_validated_work = false;
3232
3233
    // If we're in the middle of headers sync, let it do its magic.
3234
7.28k
    bool have_headers_sync = false;
3235
7.28k
    {
3236
7.28k
        LOCK(peer.m_headers_sync_mutex);
3237
3238
7.28k
        already_validated_work = IsContinuationOfLowWorkHeadersSync(peer, pfrom, headers);
3239
3240
        // The headers we passed in may have been:
3241
        // - untouched, perhaps if no headers-sync was in progress, or some
3242
        //   failure occurred
3243
        // - erased, such as if the headers were successfully processed and no
3244
        //   additional headers processing needs to take place (such as if we
3245
        //   are still in PRESYNC)
3246
        // - replaced with headers that are now ready for validation, such as
3247
        //   during the REDOWNLOAD phase of a low-work headers sync.
3248
        // So just check whether we still have headers that we need to process,
3249
        // or not.
3250
7.28k
        if (headers.empty()) {
3251
12
            return;
3252
12
        }
3253
3254
7.27k
        have_headers_sync = !!peer.m_headers_sync;
3255
7.27k
    }
3256
3257
    // Do these headers connect to something in our block index?
3258
7.27k
    const CBlockIndex *chain_start_header{WITH_LOCK(::cs_main, return m_chainman.m_blockman.LookupBlockIndex(headers[0].hashPrevBlock))};
3259
7.27k
    bool headers_connect_blockindex{chain_start_header != nullptr};
3260
3261
7.27k
    if (!headers_connect_blockindex) {
3262
        // This could be a BIP 130 block announcement, use
3263
        // special logic for handling headers that don't connect, as this
3264
        // could be benign.
3265
199
        HandleUnconnectingHeaders(pfrom, peer, headers);
3266
199
        return;
3267
199
    }
3268
3269
    // If headers connect, assume that this is in response to any outstanding getheaders
3270
    // request we may have sent, and clear out the time of our last request. Non-connecting
3271
    // headers cannot be a response to a getheaders request.
3272
7.07k
    peer.m_last_getheaders_timestamp = {};
3273
3274
    // If the headers we received are already in memory and an ancestor of
3275
    // m_best_header or our tip, skip anti-DoS checks. These headers will not
3276
    // use any more memory (and we are not leaking information that could be
3277
    // used to fingerprint us).
3278
7.07k
    const CBlockIndex *last_received_header{nullptr};
3279
7.07k
    {
3280
7.07k
        LOCK(cs_main);
3281
7.07k
        last_received_header = m_chainman.m_blockman.LookupBlockIndex(headers.back().GetHash());
3282
7.07k
        already_validated_work = already_validated_work || IsAncestorOfBestHeaderOrTip(last_received_header);
3283
7.07k
    }
3284
3285
    // If our peer has NetPermissionFlags::NoBan privileges, then bypass our
3286
    // anti-DoS logic (this saves bandwidth when we connect to a trusted peer
3287
    // on startup).
3288
7.07k
    if (pfrom.HasPermission(NetPermissionFlags::NoBan)) {
3289
2.82k
        already_validated_work = true;
3290
2.82k
    }
3291
3292
    // At this point, the headers connect to something in our block index.
3293
    // Do anti-DoS checks to determine if we should process or store for later
3294
    // processing.
3295
7.07k
    if (!already_validated_work && TryLowWorkHeadersSync(peer, pfrom,
3296
1.77k
                                                         *chain_start_header, headers)) {
3297
        // If we successfully started a low-work headers sync, then there
3298
        // should be no headers to process any further.
3299
468
        Assume(headers.empty());
3300
468
        return;
3301
468
    }
3302
3303
    // At this point, we have a set of headers with sufficient work on them
3304
    // which can be processed.
3305
3306
    // If we don't have the last header, then this peer will have given us
3307
    // something new (if these headers are valid).
3308
6.60k
    bool received_new_header{last_received_header == nullptr};
3309
3310
    // Now process all the headers.
3311
6.60k
    BlockValidationState state;
3312
6.60k
    const bool processed{m_chainman.ProcessNewBlockHeaders(headers,
3313
6.60k
                                                           /*min_pow_checked=*/true,
3314
6.60k
                                                           state, &pindexLast)};
3315
6.60k
    if (!processed) {
3316
38
        if (state.IsInvalid()) {
3317
38
            if (!pfrom.IsInboundConn() && state.GetResult() == BlockValidationResult::BLOCK_CACHED_INVALID) {
3318
                // Warn user if outgoing peers send us headers of blocks that we previously marked as invalid.
3319
0
                LogWarning("%s (received from peer=%i). "
3320
0
                           "If this happens with all peers, consider database corruption (that -reindex may fix) "
3321
0
                           "or a potential consensus incompatibility.",
3322
0
                           state.GetDebugMessage(), pfrom.GetId());
3323
0
            }
3324
38
            MaybePunishNodeForBlock(pfrom.GetId(), state, via_compact_block, "invalid header received");
3325
38
            return;
3326
38
        }
3327
38
    }
3328
6.60k
    assert(pindexLast);
3329
3330
6.56k
    if (processed && received_new_header) {
3331
2.47k
        LogBlockHeader(*pindexLast, pfrom, /*via_compact_block=*/false);
3332
2.47k
    }
3333
3334
    // Consider fetching more headers if we are not using our headers-sync mechanism.
3335
6.56k
    if (nCount == m_opts.max_headers_result && !have_headers_sync) {
3336
        // Headers message had its maximum size; the peer may have more headers.
3337
16
        if (MaybeSendGetHeaders(pfrom, GetLocator(pindexLast), peer)) {
3338
16
            LogDebug(BCLog::NET, "more getheaders (%d) to end to peer=%d", pindexLast->nHeight, pfrom.GetId());
3339
16
        }
3340
16
    }
3341
3342
6.56k
    UpdatePeerStateForReceivedHeaders(pfrom, peer, *pindexLast, received_new_header, nCount == m_opts.max_headers_result);
3343
3344
    // Consider immediately downloading blocks.
3345
6.56k
    HeadersDirectFetchBlocks(pfrom, peer, *pindexLast);
3346
3347
6.56k
    return;
3348
6.56k
}
3349
3350
std::optional<node::PackageToValidate> PeerManagerImpl::ProcessInvalidTx(NodeId nodeid, const CTransactionRef& ptx, const TxValidationState& state,
3351
                                       bool first_time_failure)
3352
841
{
3353
841
    AssertLockNotHeld(m_peer_mutex);
3354
841
    AssertLockHeld(g_msgproc_mutex);
3355
841
    AssertLockHeld(m_tx_download_mutex);
3356
3357
841
    PeerRef peer{GetPeerRef(nodeid)};
3358
3359
841
    LogDebug(BCLog::MEMPOOLREJ, "%s (wtxid=%s) from peer=%d was not accepted: %s\n",
3360
841
        ptx->GetHash().ToString(),
3361
841
        ptx->GetWitnessHash().ToString(),
3362
841
        nodeid,
3363
841
        state.ToString());
3364
3365
841
    const auto& [add_extra_compact_tx, unique_parents, package_to_validate] = m_txdownloadman.MempoolRejectedTx(ptx, state, nodeid, first_time_failure);
3366
3367
841
    if (add_extra_compact_tx && RecursiveDynamicUsage(*ptx) < 100000) {
3368
709
        AddToCompactExtraTransactions(ptx);
3369
709
    }
3370
841
    for (const Txid& parent_txid : unique_parents) {
3371
618
        if (peer) AddKnownTx(*peer, parent_txid.ToUint256());
3372
618
    }
3373
3374
841
    return package_to_validate;
3375
841
}
3376
3377
void PeerManagerImpl::ProcessValidTx(NodeId nodeid, const CTransactionRef& tx, const std::list<CTransactionRef>& replaced_transactions)
3378
12.0k
{
3379
12.0k
    AssertLockNotHeld(m_peer_mutex);
3380
12.0k
    AssertLockHeld(g_msgproc_mutex);
3381
12.0k
    AssertLockHeld(m_tx_download_mutex);
3382
3383
12.0k
    m_txdownloadman.MempoolAcceptedTx(tx);
3384
3385
12.0k
    LogDebug(BCLog::MEMPOOL, "AcceptToMemoryPool: peer=%d: accepted %s (wtxid=%s) (poolsz %u txn, %u kB)\n",
3386
12.0k
             nodeid,
3387
12.0k
             tx->GetHash().ToString(),
3388
12.0k
             tx->GetWitnessHash().ToString(),
3389
12.0k
             m_mempool.size(), m_mempool.DynamicMemoryUsage() / 1000);
3390
3391
12.0k
    InitiateTxBroadcastToAll(tx->GetWitnessHash());
3392
3393
12.0k
    for (const CTransactionRef& removedTx : replaced_transactions) {
3394
633
        AddToCompactExtraTransactions(removedTx);
3395
633
    }
3396
12.0k
}
3397
3398
void PeerManagerImpl::ProcessPackageResult(const node::PackageToValidate& package_to_validate, const PackageMempoolAcceptResult& package_result)
3399
30
{
3400
30
    AssertLockNotHeld(m_peer_mutex);
3401
30
    AssertLockHeld(g_msgproc_mutex);
3402
30
    AssertLockHeld(m_tx_download_mutex);
3403
3404
30
    const auto& package = package_to_validate.m_txns;
3405
30
    const auto& senders = package_to_validate.m_senders;
3406
3407
30
    if (package_result.m_state.IsInvalid()) {
3408
3
        m_txdownloadman.MempoolRejectedPackage(package);
3409
3
    }
3410
    // We currently only expect to process 1-parent-1-child packages. Remove if this changes.
3411
30
    if (!Assume(package.size() == 2)) return;
3412
3413
    // Iterate backwards to erase in-package descendants from the orphanage before they become
3414
    // relevant in AddChildrenToWorkSet.
3415
30
    auto package_iter = package.rbegin();
3416
30
    auto senders_iter = senders.rbegin();
3417
90
    while (package_iter != package.rend()) {
3418
60
        const auto& tx = *package_iter;
3419
60
        const NodeId nodeid = *senders_iter;
3420
60
        const auto it_result{package_result.m_tx_results.find(tx->GetWitnessHash())};
3421
3422
        // It is not guaranteed that a result exists for every transaction.
3423
60
        if (it_result != package_result.m_tx_results.end()) {
3424
60
            const auto& tx_result = it_result->second;
3425
60
            switch (tx_result.m_result_type) {
3426
54
                case MempoolAcceptResult::ResultType::VALID:
3427
54
                {
3428
54
                    ProcessValidTx(nodeid, tx, tx_result.m_replaced_transactions);
3429
54
                    break;
3430
0
                }
3431
6
                case MempoolAcceptResult::ResultType::INVALID:
3432
6
                case MempoolAcceptResult::ResultType::DIFFERENT_WITNESS:
3433
6
                {
3434
                    // Don't add to vExtraTxnForCompact, as these transactions should have already been
3435
                    // added there when added to the orphanage or rejected for TX_RECONSIDERABLE.
3436
                    // This should be updated if package submission is ever used for transactions
3437
                    // that haven't already been validated before.
3438
6
                    ProcessInvalidTx(nodeid, tx, tx_result.m_state, /*first_time_failure=*/false);
3439
6
                    break;
3440
6
                }
3441
0
                case MempoolAcceptResult::ResultType::MEMPOOL_ENTRY:
3442
0
                {
3443
                    // AlreadyHaveTx() should be catching transactions that are already in mempool.
3444
0
                    Assume(false);
3445
0
                    break;
3446
6
                }
3447
60
            }
3448
60
        }
3449
60
        package_iter++;
3450
60
        senders_iter++;
3451
60
    }
3452
30
}
3453
3454
// NOTE: the orphan processing used to be uninterruptible and quadratic, which could allow a peer to stall the node for
3455
// hours with specially crafted transactions. See https://bitcoincore.org/en/2024/07/03/disclose-orphan-dos.
3456
bool PeerManagerImpl::ProcessOrphanTx(Peer& peer)
3457
389k
{
3458
389k
    AssertLockHeld(g_msgproc_mutex);
3459
389k
    LOCK2(::cs_main, m_tx_download_mutex);
3460
3461
389k
    CTransactionRef porphanTx = nullptr;
3462
3463
389k
    while (CTransactionRef porphanTx = m_txdownloadman.GetTxToReconsider(peer.m_id)) {
3464
47
        const MempoolAcceptResult result = m_chainman.ProcessTransaction(porphanTx);
3465
47
        const TxValidationState& state = result.m_state;
3466
47
        const Txid& orphanHash = porphanTx->GetHash();
3467
47
        const Wtxid& orphan_wtxid = porphanTx->GetWitnessHash();
3468
3469
47
        if (result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
3470
39
            LogDebug(BCLog::TXPACKAGES, "   accepted orphan tx %s (wtxid=%s)\n", orphanHash.ToString(), orphan_wtxid.ToString());
3471
39
            ProcessValidTx(peer.m_id, porphanTx, result.m_replaced_transactions);
3472
39
            return true;
3473
39
        } else if (state.GetResult() != TxValidationResult::TX_MISSING_INPUTS) {
3474
7
            LogDebug(BCLog::TXPACKAGES, "   invalid orphan tx %s (wtxid=%s) from peer=%d. %s\n",
3475
7
                orphanHash.ToString(),
3476
7
                orphan_wtxid.ToString(),
3477
7
                peer.m_id,
3478
7
                state.ToString());
3479
3480
7
            if (Assume(state.IsInvalid() &&
3481
7
                       state.GetResult() != TxValidationResult::TX_UNKNOWN &&
3482
7
                       state.GetResult() != TxValidationResult::TX_NO_MEMPOOL &&
3483
7
                       state.GetResult() != TxValidationResult::TX_RESULT_UNSET)) {
3484
7
                ProcessInvalidTx(peer.m_id, porphanTx, state, /*first_time_failure=*/false);
3485
7
            }
3486
7
            return true;
3487
7
        }
3488
47
    }
3489
3490
389k
    return false;
3491
389k
}
3492
3493
bool PeerManagerImpl::PrepareBlockFilterRequest(CNode& node, Peer& peer,
3494
                                                BlockFilterType filter_type, uint32_t start_height,
3495
                                                const uint256& stop_hash, uint32_t max_height_diff,
3496
                                                const CBlockIndex*& stop_index,
3497
                                                BlockFilterIndex*& filter_index)
3498
15
{
3499
15
    const bool supported_filter_type =
3500
15
        (filter_type == BlockFilterType::BASIC &&
3501
15
         (peer.m_our_services & NODE_COMPACT_FILTERS));
3502
15
    if (!supported_filter_type) {
3503
4
        LogDebug(BCLog::NET, "peer requested unsupported block filter type: %d, %s",
3504
4
                 static_cast<uint8_t>(filter_type), node.DisconnectMsg());
3505
4
        node.fDisconnect = true;
3506
4
        return false;
3507
4
    }
3508
3509
11
    {
3510
11
        LOCK(cs_main);
3511
11
        stop_index = m_chainman.m_blockman.LookupBlockIndex(stop_hash);
3512
3513
        // Check that the stop block exists and the peer would be allowed to fetch it.
3514
11
        if (!stop_index || !BlockRequestAllowed(*stop_index)) {
3515
1
            LogDebug(BCLog::NET, "peer requested invalid block hash: %s, %s",
3516
1
                     stop_hash.ToString(), node.DisconnectMsg());
3517
1
            node.fDisconnect = true;
3518
1
            return false;
3519
1
        }
3520
11
    }
3521
3522
10
    uint32_t stop_height = stop_index->nHeight;
3523
10
    if (start_height > stop_height) {
3524
1
        LogDebug(BCLog::NET, "peer sent invalid getcfilters/getcfheaders with "
3525
1
                 "start height %d and stop height %d, %s",
3526
1
                 start_height, stop_height, node.DisconnectMsg());
3527
1
        node.fDisconnect = true;
3528
1
        return false;
3529
1
    }
3530
9
    if (stop_height - start_height >= max_height_diff) {
3531
2
        LogDebug(BCLog::NET, "peer requested too many cfilters/cfheaders: %d / %d, %s",
3532
2
                 stop_height - start_height + 1, max_height_diff, node.DisconnectMsg());
3533
2
        node.fDisconnect = true;
3534
2
        return false;
3535
2
    }
3536
3537
7
    filter_index = GetBlockFilterIndex(filter_type);
3538
7
    if (!filter_index) {
3539
0
        LogDebug(BCLog::NET, "Filter index for supported type %s not found\n", BlockFilterTypeName(filter_type));
3540
0
        return false;
3541
0
    }
3542
3543
7
    return true;
3544
7
}
3545
3546
void PeerManagerImpl::ProcessGetCFilters(CNode& node, Peer& peer, DataStream& vRecv)
3547
4
{
3548
4
    uint8_t filter_type_ser;
3549
4
    uint32_t start_height;
3550
4
    uint256 stop_hash;
3551
3552
4
    vRecv >> filter_type_ser >> start_height >> stop_hash;
3553
3554
4
    const BlockFilterType filter_type = static_cast<BlockFilterType>(filter_type_ser);
3555
3556
4
    const CBlockIndex* stop_index;
3557
4
    BlockFilterIndex* filter_index;
3558
4
    if (!PrepareBlockFilterRequest(node, peer, filter_type, start_height, stop_hash,
3559
4
                                   MAX_GETCFILTERS_SIZE, stop_index, filter_index)) {
3560
2
        return;
3561
2
    }
3562
3563
2
    std::vector<BlockFilter> filters;
3564
2
    if (!filter_index->LookupFilterRange(start_height, stop_index, filters)) {
3565
0
        LogDebug(BCLog::NET, "Failed to find block filter in index: filter_type=%s, start_height=%d, stop_hash=%s\n",
3566
0
                     BlockFilterTypeName(filter_type), start_height, stop_hash.ToString());
3567
0
        return;
3568
0
    }
3569
3570
11
    for (const auto& filter : filters) {
3571
11
        MakeAndPushMessage(node, NetMsgType::CFILTER, filter);
3572
11
    }
3573
2
}
3574
3575
void PeerManagerImpl::ProcessGetCFHeaders(CNode& node, Peer& peer, DataStream& vRecv)
3576
5
{
3577
5
    uint8_t filter_type_ser;
3578
5
    uint32_t start_height;
3579
5
    uint256 stop_hash;
3580
3581
5
    vRecv >> filter_type_ser >> start_height >> stop_hash;
3582
3583
5
    const BlockFilterType filter_type = static_cast<BlockFilterType>(filter_type_ser);
3584
3585
5
    const CBlockIndex* stop_index;
3586
5
    BlockFilterIndex* filter_index;
3587
5
    if (!PrepareBlockFilterRequest(node, peer, filter_type, start_height, stop_hash,
3588
5
                                   MAX_GETCFHEADERS_SIZE, stop_index, filter_index)) {
3589
3
        return;
3590
3
    }
3591
3592
2
    uint256 prev_header;
3593
2
    if (start_height > 0) {
3594
2
        const CBlockIndex* const prev_block =
3595
2
            stop_index->GetAncestor(static_cast<int>(start_height - 1));
3596
2
        if (!filter_index->LookupFilterHeader(prev_block, prev_header)) {
3597
0
            LogDebug(BCLog::NET, "Failed to find block filter header in index: filter_type=%s, block_hash=%s\n",
3598
0
                         BlockFilterTypeName(filter_type), prev_block->GetBlockHash().ToString());
3599
0
            return;
3600
0
        }
3601
2
    }
3602
3603
2
    std::vector<uint256> filter_hashes;
3604
2
    if (!filter_index->LookupFilterHashRange(start_height, stop_index, filter_hashes)) {
3605
0
        LogDebug(BCLog::NET, "Failed to find block filter hashes in index: filter_type=%s, start_height=%d, stop_hash=%s\n",
3606
0
                     BlockFilterTypeName(filter_type), start_height, stop_hash.ToString());
3607
0
        return;
3608
0
    }
3609
3610
2
    MakeAndPushMessage(node, NetMsgType::CFHEADERS,
3611
2
              filter_type_ser,
3612
2
              stop_index->GetBlockHash(),
3613
2
              prev_header,
3614
2
              filter_hashes);
3615
2
}
3616
3617
void PeerManagerImpl::ProcessGetCFCheckPt(CNode& node, Peer& peer, DataStream& vRecv)
3618
6
{
3619
6
    uint8_t filter_type_ser;
3620
6
    uint256 stop_hash;
3621
3622
6
    vRecv >> filter_type_ser >> stop_hash;
3623
3624
6
    const BlockFilterType filter_type = static_cast<BlockFilterType>(filter_type_ser);
3625
3626
6
    const CBlockIndex* stop_index;
3627
6
    BlockFilterIndex* filter_index;
3628
6
    if (!PrepareBlockFilterRequest(node, peer, filter_type, /*start_height=*/0, stop_hash,
3629
6
                                   /*max_height_diff=*/std::numeric_limits<uint32_t>::max(),
3630
6
                                   stop_index, filter_index)) {
3631
3
        return;
3632
3
    }
3633
3634
3
    std::vector<uint256> headers(stop_index->nHeight / CFCHECKPT_INTERVAL);
3635
3636
    // Populate headers.
3637
3
    const CBlockIndex* block_index = stop_index;
3638
7
    for (int i = headers.size() - 1; i >= 0; i--) {
3639
4
        int height = (i + 1) * CFCHECKPT_INTERVAL;
3640
4
        block_index = block_index->GetAncestor(height);
3641
3642
4
        if (!filter_index->LookupFilterHeader(block_index, headers[i])) {
3643
0
            LogDebug(BCLog::NET, "Failed to find block filter header in index: filter_type=%s, block_hash=%s\n",
3644
0
                         BlockFilterTypeName(filter_type), block_index->GetBlockHash().ToString());
3645
0
            return;
3646
0
        }
3647
4
    }
3648
3649
3
    MakeAndPushMessage(node, NetMsgType::CFCHECKPT,
3650
3
              filter_type_ser,
3651
3
              stop_index->GetBlockHash(),
3652
3
              headers);
3653
3
}
3654
3655
void PeerManagerImpl::ProcessBlock(CNode& node, const std::shared_ptr<const CBlock>& block, bool force_processing, bool min_pow_checked)
3656
55.3k
{
3657
55.3k
    bool new_block{false};
3658
55.3k
    m_chainman.ProcessNewBlock(block, force_processing, min_pow_checked, &new_block);
3659
55.3k
    if (new_block) {
3660
54.8k
        node.m_last_block_time = GetTime<std::chrono::seconds>();
3661
        // In case this block came from a different peer than we requested
3662
        // from, we can erase the block request now anyway (as we just stored
3663
        // this block to disk).
3664
54.8k
        LOCK(cs_main);
3665
54.8k
        RemoveBlockRequest(block->GetHash(), std::nullopt);
3666
54.8k
    } else {
3667
513
        LOCK(cs_main);
3668
513
        mapBlockSource.erase(block->GetHash());
3669
513
    }
3670
55.3k
}
3671
3672
void PeerManagerImpl::ProcessCompactBlockTxns(CNode& pfrom, Peer& peer, const BlockTransactions& block_transactions)
3673
17.7k
{
3674
17.7k
    std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
3675
17.7k
    bool fBlockRead{false};
3676
17.7k
    {
3677
17.7k
        LOCK(cs_main);
3678
3679
17.7k
        auto range_flight = mapBlocksInFlight.equal_range(block_transactions.blockhash);
3680
17.7k
        size_t already_in_flight = std::distance(range_flight.first, range_flight.second);
3681
17.7k
        bool requested_block_from_this_peer{false};
3682
3683
        // Multimap ensures ordering of outstanding requests. It's either empty or first in line.
3684
17.7k
        bool first_in_flight = already_in_flight == 0 || (range_flight.first->second.first == pfrom.GetId());
3685
3686
18.0k
        while (range_flight.first != range_flight.second) {
3687
18.0k
            auto [node_id, block_it] = range_flight.first->second;
3688
18.0k
            if (node_id == pfrom.GetId() && block_it->partialBlock) {
3689
17.7k
                requested_block_from_this_peer = true;
3690
17.7k
                break;
3691
17.7k
            }
3692
318
            range_flight.first++;
3693
318
        }
3694
3695
17.7k
        if (!requested_block_from_this_peer) {
3696
14
            LogDebug(BCLog::NET, "Peer %d sent us block transactions for block we weren't expecting\n", pfrom.GetId());
3697
14
            return;
3698
14
        }
3699
3700
17.7k
        PartiallyDownloadedBlock& partialBlock = *range_flight.first->second.second->partialBlock;
3701
3702
17.7k
        if (partialBlock.header.IsNull()) {
3703
            // It is possible for the header to be empty if a previous call to FillBlock wiped the header, but left
3704
            // the PartiallyDownloadedBlock pointer around (i.e. did not call RemoveBlockRequest). In this case, we
3705
            // should not call LookupBlockIndex below.
3706
1
            RemoveBlockRequest(block_transactions.blockhash, pfrom.GetId());
3707
1
            Misbehaving(peer, "previous compact block reconstruction attempt failed");
3708
1
            LogDebug(BCLog::NET, "Peer %d sent compact block transactions multiple times", pfrom.GetId());
3709
1
            return;
3710
1
        }
3711
3712
        // We should not have gotten this far in compact block processing unless it's attached to a known header
3713
17.7k
        const CBlockIndex* prev_block{Assume(m_chainman.m_blockman.LookupBlockIndex(partialBlock.header.hashPrevBlock))};
3714
17.7k
        ReadStatus status = partialBlock.FillBlock(*pblock, block_transactions.txn,
3715
17.7k
                                                   /*segwit_active=*/DeploymentActiveAfter(prev_block, m_chainman, Consensus::DEPLOYMENT_SEGWIT));
3716
17.7k
        if (status == READ_STATUS_INVALID) {
3717
0
            RemoveBlockRequest(block_transactions.blockhash, pfrom.GetId()); // Reset in-flight state in case Misbehaving does not result in a disconnect
3718
0
            Misbehaving(peer, "invalid compact block/non-matching block transactions");
3719
0
            return;
3720
17.7k
        } else if (status == READ_STATUS_FAILED) {
3721
3
            if (first_in_flight) {
3722
                // Might have collided, fall back to getdata now :(
3723
                // We keep the failed partialBlock to disallow processing another compact block announcement from the same
3724
                // peer for the same block. We let the full block download below continue under the same m_downloading_since
3725
                // timer.
3726
2
                std::vector<CInv> invs;
3727
2
                invs.emplace_back(MSG_BLOCK | GetFetchFlags(peer), block_transactions.blockhash);
3728
2
                MakeAndPushMessage(pfrom, NetMsgType::GETDATA, invs);
3729
2
            } else {
3730
1
                RemoveBlockRequest(block_transactions.blockhash, pfrom.GetId());
3731
1
                LogDebug(BCLog::NET, "Peer %d sent us a compact block but it failed to reconstruct, waiting on first download to complete\n", pfrom.GetId());
3732
1
                return;
3733
1
            }
3734
17.7k
        } else {
3735
            // Block is okay for further processing
3736
17.7k
            RemoveBlockRequest(block_transactions.blockhash, pfrom.GetId()); // it is now an empty pointer
3737
17.7k
            fBlockRead = true;
3738
            // mapBlockSource is used for potentially punishing peers and
3739
            // updating which peers send us compact blocks, so the race
3740
            // between here and cs_main in ProcessNewBlock is fine.
3741
            // BIP 152 permits peers to relay compact blocks after validating
3742
            // the header only; we should not punish peers if the block turns
3743
            // out to be invalid.
3744
17.7k
            mapBlockSource.emplace(block_transactions.blockhash, std::make_pair(pfrom.GetId(), false));
3745
17.7k
        }
3746
17.7k
    } // Don't hold cs_main when we call into ProcessNewBlock
3747
17.7k
    if (fBlockRead) {
3748
        // Since we requested this block (it was in mapBlocksInFlight), force it to be processed,
3749
        // even if it would not be a candidate for new tip (missing previous block, chain not long enough, etc)
3750
        // This bypasses some anti-DoS logic in AcceptBlock (eg to prevent
3751
        // disk-space attacks), but this should be safe due to the
3752
        // protections in the compact block handler -- see related comment
3753
        // in compact block optimistic reconstruction handling.
3754
17.7k
        ProcessBlock(pfrom, pblock, /*force_processing=*/true, /*min_pow_checked=*/true);
3755
17.7k
    }
3756
17.7k
    return;
3757
17.7k
}
3758
3759
21.2k
void PeerManagerImpl::LogBlockHeader(const CBlockIndex& index, const CNode& peer, bool via_compact_block) {
3760
    // To prevent log spam, this function should only be called after it was determined that a
3761
    // header is both new and valid.
3762
    //
3763
    // These messages are valuable for detecting potential selfish mining behavior;
3764
    // if multiple displacing headers are seen near simultaneously across many
3765
    // nodes in the network, this might be an indication of selfish mining.
3766
    // In addition it can be used to identify peers which send us a header, but
3767
    // don't followup with a complete and valid (compact) block.
3768
    // Having this log by default when not in IBD ensures broad availability of
3769
    // this data in case investigation is merited.
3770
21.2k
    const auto msg = strprintf(
3771
21.2k
        "Saw new %sheader hash=%s height=%d %s",
3772
21.2k
        via_compact_block ? "cmpctblock " : "",
3773
21.2k
        index.GetBlockHash().ToString(),
3774
21.2k
        index.nHeight,
3775
21.2k
        peer.LogPeer()
3776
21.2k
    );
3777
21.2k
    if (m_chainman.IsInitialBlockDownload()) {
3778
1.03k
        LogDebug(BCLog::VALIDATION, "%s", msg);
3779
20.2k
    } else {
3780
20.2k
        LogInfo("%s", msg);
3781
20.2k
    }
3782
21.2k
}
3783
3784
void PeerManagerImpl::PushPrivateBroadcastTx(CNode& node)
3785
14
{
3786
14
    Assume(node.IsPrivateBroadcastConn());
3787
3788
14
    const auto opt_tx{m_tx_for_private_broadcast.PickTxForSend(node.GetId(), CService{node.addr})};
3789
14
    if (!opt_tx) {
3790
0
        LogDebug(BCLog::PRIVBROADCAST, "Disconnecting: no more transactions for private broadcast (connected in vain), %s", node.LogPeer());
3791
0
        node.fDisconnect = true;
3792
0
        return;
3793
0
    }
3794
14
    const CTransactionRef& tx{*opt_tx};
3795
3796
14
    LogDebug(BCLog::PRIVBROADCAST, "P2P handshake completed, sending INV for txid=%s%s, %s",
3797
14
             tx->GetHash().ToString(), tx->HasWitness() ? strprintf(", wtxid=%s", tx->GetWitnessHash().ToString()) : "",
3798
14
             node.LogPeer());
3799
3800
14
    MakeAndPushMessage(node, NetMsgType::INV, std::vector<CInv>{{CInv{MSG_TX, tx->GetHash().ToUint256()}}});
3801
14
}
3802
3803
void PeerManagerImpl::ProcessMessage(Peer& peer, CNode& pfrom, const std::string& msg_type, DataStream& vRecv,
3804
                                     const NodeClock::time_point time_received,
3805
                                     const std::atomic<bool>& interruptMsgProc)
3806
157k
{
3807
157k
    AssertLockHeld(g_msgproc_mutex);
3808
3809
157k
    LogDebug(BCLog::NET, "received: %s (%u bytes) peer=%d\n", SanitizeString(msg_type), vRecv.size(), pfrom.GetId());
3810
3811
3812
157k
    if (msg_type == NetMsgType::VERSION) {
3813
1.65k
        if (pfrom.nVersion != 0) {
3814
1
            LogDebug(BCLog::NET, "redundant version message from peer=%d\n", pfrom.GetId());
3815
1
            return;
3816
1
        }
3817
3818
1.65k
        int64_t nTime;
3819
1.65k
        CService addrMe;
3820
1.65k
        uint64_t nNonce = 1;
3821
1.65k
        ServiceFlags nServices;
3822
1.65k
        int nVersion;
3823
1.65k
        std::string cleanSubVer;
3824
1.65k
        int starting_height = -1;
3825
1.65k
        bool fRelay = true;
3826
3827
1.65k
        vRecv >> nVersion >> Using<CustomUintFormatter<8>>(nServices) >> nTime;
3828
1.65k
        if (nTime < 0) {
3829
0
            nTime = 0;
3830
0
        }
3831
1.65k
        vRecv.ignore(8); // Ignore the addrMe service bits sent by the peer
3832
1.65k
        vRecv >> CNetAddr::V1(addrMe);
3833
1.65k
        if (!pfrom.IsInboundConn() && !pfrom.IsPrivateBroadcastConn())
3834
574
        {
3835
            // Overwrites potentially existing services. In contrast to this,
3836
            // unvalidated services received via gossip relay in ADDR/ADDRV2
3837
            // messages are only ever added but cannot replace existing ones.
3838
574
            m_addrman.SetServices(pfrom.addr, nServices);
3839
574
        }
3840
1.65k
        if (pfrom.ExpectServicesFromConn() && !HasAllDesirableServiceFlags(nServices))
3841
25
        {
3842
25
            LogDebug(BCLog::NET, "peer does not offer the expected services (%08x offered, %08x expected), %s",
3843
25
                     nServices,
3844
25
                     GetDesirableServiceFlags(nServices),
3845
25
                     pfrom.DisconnectMsg());
3846
25
            pfrom.fDisconnect = true;
3847
25
            return;
3848
25
        }
3849
3850
1.63k
        if (nVersion < MIN_PEER_PROTO_VERSION) {
3851
            // disconnect from peers older than this proto version
3852
1
            LogDebug(BCLog::NET, "peer using obsolete version %i, %s", nVersion, pfrom.DisconnectMsg());
3853
1
            pfrom.fDisconnect = true;
3854
1
            return;
3855
1
        }
3856
3857
1.63k
        if (!vRecv.empty()) {
3858
            // The version message includes information about the sending node which we don't use:
3859
            //   - 8 bytes (service bits)
3860
            //   - 16 bytes (ipv6 address)
3861
            //   - 2 bytes (port)
3862
1.63k
            vRecv.ignore(26);
3863
1.63k
            vRecv >> nNonce;
3864
1.63k
        }
3865
1.63k
        if (!vRecv.empty()) {
3866
1.63k
            std::string strSubVer;
3867
1.63k
            vRecv >> LIMITED_STRING(strSubVer, MAX_SUBVERSION_LENGTH);
3868
1.63k
            cleanSubVer = SanitizeString(strSubVer);
3869
1.63k
        }
3870
1.63k
        if (!vRecv.empty()) {
3871
1.63k
            vRecv >> starting_height;
3872
1.63k
        }
3873
1.63k
        if (!vRecv.empty())
3874
1.63k
            vRecv >> fRelay;
3875
        // Disconnect if we connected to ourself
3876
1.63k
        if (pfrom.IsInboundConn() && !m_connman.CheckIncomingNonce(nNonce))
3877
2
        {
3878
2
            LogInfo("connected to self at %s, disconnecting\n", pfrom.addr.ToStringAddrPort());
3879
2
            pfrom.fDisconnect = true;
3880
2
            return;
3881
2
        }
3882
3883
1.62k
        if (pfrom.IsInboundConn() && addrMe.IsRoutable())
3884
0
        {
3885
0
            SeenLocal(addrMe);
3886
0
        }
3887
3888
        // Inbound peers send us their version message when they connect.
3889
        // We send our version message in response.
3890
1.62k
        if (pfrom.IsInboundConn()) {
3891
1.06k
            PushNodeVersion(pfrom, peer);
3892
1.06k
        }
3893
3894
        // Change version
3895
1.62k
        const int greatest_common_version = std::min(nVersion, pfrom.AdvertisedVersion());
3896
1.62k
        pfrom.SetCommonVersion(greatest_common_version);
3897
1.62k
        pfrom.nVersion = nVersion;
3898
3899
1.62k
        pfrom.m_has_all_wanted_services = HasAllDesirableServiceFlags(nServices);
3900
1.62k
        peer.m_their_services = nServices;
3901
1.62k
        pfrom.SetAddrLocal(addrMe);
3902
1.62k
        {
3903
1.62k
            LOCK(pfrom.m_subver_mutex);
3904
1.62k
            pfrom.cleanSubVer = cleanSubVer;
3905
1.62k
        }
3906
3907
        // Only initialize the Peer::TxRelay m_relay_txs data structure if:
3908
        // - this isn't an outbound block-relay-only connection, and
3909
        // - this isn't an outbound feeler connection, and
3910
        // - fRelay=true (the peer wishes to receive transaction announcements)
3911
        //   or we're offering NODE_BLOOM to this peer. NODE_BLOOM means that
3912
        //   the peer may turn on transaction relay later.
3913
1.62k
        if (!pfrom.IsBlockOnlyConn() &&
3914
1.62k
            !pfrom.IsFeelerConn() &&
3915
1.62k
            (fRelay || (peer.m_our_services & NODE_BLOOM))) {
3916
1.56k
            auto* const tx_relay = peer.SetTxRelay();
3917
1.56k
            {
3918
1.56k
                LOCK(tx_relay->m_bloom_filter_mutex);
3919
1.56k
                tx_relay->m_relay_txs = fRelay; // set to true after we get the first filter* message
3920
1.56k
            }
3921
1.56k
            if (fRelay) pfrom.m_relays_txs = true;
3922
1.56k
        }
3923
3924
1.62k
        const auto mapped_as{m_connman.GetMappedAS(pfrom.addr)};
3925
1.62k
        LogDebug(BCLog::NET, "receive version message: %s: version %d, blocks=%d, us=%s, txrelay=%d, %s%s",
3926
1.62k
                  cleanSubVer.empty() ? "<no user agent>" : cleanSubVer, pfrom.nVersion,
3927
1.62k
                  starting_height, addrMe.ToStringAddrPort(), fRelay, pfrom.LogPeer(),
3928
1.62k
                  (mapped_as ? strprintf(", mapped_as=%d", mapped_as) : ""));
3929
3930
1.62k
        if (pfrom.IsPrivateBroadcastConn()) {
3931
15
            if (fRelay) {
3932
14
                MakeAndPushMessage(pfrom, NetMsgType::VERACK);
3933
14
            } else {
3934
1
                LogDebug(BCLog::PRIVBROADCAST, "Disconnecting: does not support transaction relay (connected in vain), %s",
3935
1
                         pfrom.LogPeer());
3936
1
                pfrom.fDisconnect = true;
3937
1
            }
3938
15
            return;
3939
15
        }
3940
3941
1.61k
        if (greatest_common_version >= WTXID_RELAY_VERSION) {
3942
1.61k
            MakeAndPushMessage(pfrom, NetMsgType::WTXIDRELAY);
3943
1.61k
        }
3944
3945
        // Signal ADDRv2 support (BIP155).
3946
1.61k
        if (greatest_common_version >= 70016) {
3947
            // BIP155 defines addrv2 and sendaddrv2 for all protocol versions, but some
3948
            // implementations reject messages they don't know. As a courtesy, don't send
3949
            // it to nodes with a version before 70016, as no software is known to support
3950
            // BIP155 that doesn't announce at least that protocol version number.
3951
1.61k
            MakeAndPushMessage(pfrom, NetMsgType::SENDADDRV2);
3952
1.61k
        }
3953
3954
1.61k
        if (greatest_common_version >= WTXID_RELAY_VERSION && m_txreconciliation) {
3955
            // Per BIP-330, we announce txreconciliation support if:
3956
            // - protocol version per the peer's VERSION message supports WTXID_RELAY;
3957
            // - transaction relay is supported per the peer's VERSION message
3958
            // - this is not a block-relay-only connection and not a feeler
3959
            // - this is not an addr fetch connection;
3960
            // - we are not in -blocksonly mode.
3961
15
            const auto* tx_relay = peer.GetTxRelay();
3962
15
            if (tx_relay && WITH_LOCK(tx_relay->m_bloom_filter_mutex, return tx_relay->m_relay_txs) &&
3963
15
                !pfrom.IsAddrFetchConn() && !m_opts.ignore_incoming_txs) {
3964
8
                const uint64_t recon_salt = m_txreconciliation->PreRegisterPeer(pfrom.GetId());
3965
8
                MakeAndPushMessage(pfrom, NetMsgType::SENDTXRCNCL,
3966
8
                                   TXRECONCILIATION_VERSION, recon_salt);
3967
8
            }
3968
15
        }
3969
3970
1.61k
        if (greatest_common_version >= FEATURE_VERSION) {
3971
            // announce supported features
3972
            // MakeAndPushFeature(pfrom, NetMsgFeature::FOO, uint32_t{1});
3973
1.59k
        }
3974
3975
        // If we have too many tx-relaying inbound peers, attempt to evict an existing one.
3976
        // Only if this fails, disconnect this peer.
3977
1.61k
        if (MaybeDisconnectForTxRelayCapacity(pfrom, msg_type, /*protect_peer=*/pfrom.GetId())) return;
3978
1.61k
        MakeAndPushMessage(pfrom, NetMsgType::VERACK);
3979
3980
        // Potentially mark this peer as a preferred download peer.
3981
1.61k
        {
3982
1.61k
            LOCK(cs_main);
3983
1.61k
            CNodeState* state = State(pfrom.GetId());
3984
1.61k
            state->fPreferredDownload = (!pfrom.IsInboundConn() || pfrom.HasPermission(NetPermissionFlags::NoBan)) && !pfrom.IsAddrFetchConn() && CanServeBlocks(peer);
3985
1.61k
            m_num_preferred_download_peers += state->fPreferredDownload;
3986
1.61k
        }
3987
3988
        // Attempt to initialize address relay for outbound peers and use result
3989
        // to decide whether to send GETADDR, so that we don't send it to
3990
        // inbound, feelers, or outbound block-relay-only peers.
3991
1.61k
        bool send_getaddr{false};
3992
1.61k
        if (!pfrom.IsInboundConn()) {
3993
550
            send_getaddr = SetupAddressRelay(pfrom, peer);
3994
550
        }
3995
1.61k
        if (send_getaddr) {
3996
            // Do a one-time address fetch to help populate/update our addrman.
3997
            // If we're starting up for the first time, our addrman may be pretty
3998
            // empty, so this mechanism is important to help us connect to the network.
3999
            // We skip this for block-relay-only peers. We want to avoid
4000
            // potentially leaking addr information and we do not want to
4001
            // indicate to the peer that we will participate in addr relay.
4002
515
            MakeAndPushMessage(pfrom, NetMsgType::GETADDR);
4003
515
            peer.m_getaddr_sent = true;
4004
            // When requesting a getaddr, accept an additional MAX_ADDR_TO_SEND addresses in response
4005
            // (bypassing the MAX_ADDR_PROCESSING_TOKEN_BUCKET limit).
4006
515
            peer.m_addr_token_bucket += MAX_ADDR_TO_SEND;
4007
515
        }
4008
4009
1.61k
        if (!pfrom.IsInboundConn()) {
4010
            // For non-inbound connections, we update the addrman to record
4011
            // connection success so that addrman will have an up-to-date
4012
            // notion of which peers are online and available.
4013
            //
4014
            // While we strive to not leak information about block-relay-only
4015
            // connections via the addrman, not moving an address to the tried
4016
            // table is also potentially detrimental because new-table entries
4017
            // are subject to eviction in the event of addrman collisions.  We
4018
            // mitigate the information-leak by never calling
4019
            // AddrMan::Connected() on block-relay-only peers; see
4020
            // FinalizeNode().
4021
            //
4022
            // This moves an address from New to Tried table in Addrman,
4023
            // resolves tried-table collisions, etc.
4024
550
            m_addrman.Good(pfrom.addr);
4025
550
        }
4026
4027
1.61k
        peer.m_time_offset = NodeSeconds{std::chrono::seconds{nTime}} - Now<NodeSeconds>();
4028
1.61k
        if (!pfrom.IsInboundConn()) {
4029
            // Don't use timedata samples from inbound peers to make it
4030
            // harder for others to create false warnings about our clock being out of sync.
4031
550
            m_outbound_time_offsets.Add(peer.m_time_offset);
4032
550
            m_outbound_time_offsets.WarnIfOutOfSync();
4033
550
        }
4034
4035
        // If the peer is old enough to have the old alert system, send it the final alert.
4036
1.61k
        if (greatest_common_version <= 70012) {
4037
0
            constexpr auto finalAlert{"60010000000000000000000000ffffff7f00000000ffffff7ffeffff7f01ffffff7f00000000ffffff7f00ffffff7f002f555247454e543a20416c657274206b657920636f6d70726f6d697365642c2075706772616465207265717569726564004630440220653febd6410f470f6bae11cad19c48413becb1ac2c17f908fd0fd53bdc3abd5202206d0e9c96fe88d4a0f01ed9dedae2b6f9e00da94cad0fecaae66ecf689bf71b50"_hex};
4038
0
            MakeAndPushMessage(pfrom, "alert", finalAlert);
4039
0
        }
4040
4041
        // Feeler connections exist only to verify if address is online.
4042
1.61k
        if (pfrom.IsFeelerConn()) {
4043
5
            LogDebug(BCLog::NET, "feeler connection completed, %s", pfrom.DisconnectMsg());
4044
5
            pfrom.fDisconnect = true;
4045
5
        }
4046
1.61k
        return;
4047
1.61k
    }
4048
4049
155k
    if (pfrom.nVersion == 0) {
4050
        // Must have a version message before anything else
4051
6
        LogDebug(BCLog::NET, "non-version message before version handshake. Message \"%s\" from peer=%d\n", SanitizeString(msg_type), pfrom.GetId());
4052
6
        return;
4053
6
    }
4054
4055
155k
    if (msg_type == NetMsgType::VERACK) {
4056
1.58k
        if (pfrom.fSuccessfullyConnected) {
4057
2
            LogDebug(BCLog::NET, "ignoring redundant verack message from peer=%d\n", pfrom.GetId());
4058
2
            return;
4059
2
        }
4060
4061
1.58k
        auto new_peer_msg = [&]() {
4062
1.58k
            const auto mapped_as{m_connman.GetMappedAS(pfrom.addr)};
4063
1.58k
            return strprintf("New %s peer connected: transport: %s, version: %d, %s%s",
4064
1.58k
                pfrom.ConnectionTypeAsString(),
4065
1.58k
                TransportTypeAsString(pfrom.m_transport->GetInfo().transport_type),
4066
1.58k
                pfrom.nVersion.load(), pfrom.LogPeer(),
4067
1.58k
                (mapped_as ? strprintf(", mapped_as=%d", mapped_as) : ""));
4068
1.58k
        };
4069
4070
        // Log successful connections unconditionally for outbound, but not for inbound as those
4071
        // can be triggered by an attacker at high rate.
4072
1.58k
        if (pfrom.IsInboundConn()) {
4073
1.02k
            LogDebug(BCLog::NET, "%s", new_peer_msg());
4074
1.02k
        } else {
4075
558
            LogInfo("%s", new_peer_msg());
4076
558
        }
4077
4078
1.58k
        if (auto tx_relay = peer.GetTxRelay()) {
4079
            // `TxRelay::m_tx_inventory_to_send` must be empty before the
4080
            // version handshake is completed as
4081
            // `TxRelay::m_next_inv_send_time` is first initialised in
4082
            // `SendMessages` after the verack is received. Any transactions
4083
            // received during the version handshake would otherwise
4084
            // immediately be advertised without random delay, potentially
4085
            // leaking the time of arrival to a spy.
4086
1.52k
            Assume(WITH_LOCK(
4087
1.52k
                tx_relay->m_tx_inventory_mutex,
4088
1.52k
                return tx_relay->m_tx_inventory_to_send.empty() &&
4089
1.52k
                       tx_relay->m_next_inv_send_time == 0s));
4090
1.52k
        }
4091
4092
1.58k
        if (pfrom.IsPrivateBroadcastConn()) {
4093
14
            pfrom.fSuccessfullyConnected = true;
4094
            // The peer may intend to later send us NetMsgType::FEEFILTER limiting
4095
            // cheap transactions, but we don't wait for that and thus we may send
4096
            // them a transaction below their threshold. This is ok because this
4097
            // relay logic is designed to work even in cases when the peer drops
4098
            // the transaction (due to it being too cheap, or for other reasons).
4099
14
            PushPrivateBroadcastTx(pfrom);
4100
14
            return;
4101
14
        }
4102
4103
1.56k
        if (pfrom.GetCommonVersion() >= SHORT_IDS_BLOCKS_VERSION) {
4104
            // Tell our peer we are willing to provide version 2 cmpctblocks.
4105
            // However, we do not request new block announcements using
4106
            // cmpctblock messages.
4107
            // We send this to non-NODE NETWORK peers as well, because
4108
            // they may wish to request compact blocks from us
4109
1.56k
            MakeAndPushMessage(pfrom, NetMsgType::SENDCMPCT, /*high_bandwidth=*/false, /*version=*/CMPCTBLOCKS_VERSION);
4110
1.56k
        }
4111
4112
1.56k
        if (m_txreconciliation) {
4113
11
            if (!peer.m_wtxid_relay || !m_txreconciliation->IsPeerRegistered(pfrom.GetId())) {
4114
                // We could have optimistically pre-registered/registered the peer. In that case,
4115
                // we should forget about the reconciliation state here if this wasn't followed
4116
                // by WTXIDRELAY (since WTXIDRELAY can't be announced later).
4117
11
                m_txreconciliation->ForgetPeer(pfrom.GetId());
4118
11
            }
4119
11
        }
4120
4121
1.56k
        {
4122
1.56k
            LOCK2(::cs_main, m_tx_download_mutex);
4123
1.56k
            const CNodeState* state = State(pfrom.GetId());
4124
1.56k
            m_txdownloadman.ConnectedPeer(pfrom.GetId(), node::TxDownloadConnectionInfo {
4125
1.56k
                .m_preferred = state->fPreferredDownload,
4126
1.56k
                .m_relay_permissions = pfrom.HasPermission(NetPermissionFlags::Relay),
4127
1.56k
                .m_wtxid_relay = peer.m_wtxid_relay,
4128
1.56k
            });
4129
1.56k
        }
4130
4131
1.56k
        pfrom.fSuccessfullyConnected = true;
4132
1.56k
        return;
4133
1.58k
    }
4134
4135
154k
    if (msg_type == NetMsgType::SENDHEADERS) {
4136
641
        peer.m_prefers_headers = true;
4137
641
        return;
4138
641
    }
4139
4140
153k
    if (msg_type == NetMsgType::SENDCMPCT) {
4141
1.14k
        uint8_t sendcmpct_hb{0};
4142
1.14k
        uint64_t sendcmpct_version{0};
4143
1.14k
        vRecv >> sendcmpct_hb >> sendcmpct_version;
4144
4145
        // BIP152: the first integer is interpreted as a boolean and MUST have a
4146
        // value of either 1 or 0.
4147
1.14k
        if (sendcmpct_hb > 1) {
4148
1
            Misbehaving(peer, "invalid sendcmpct announce field");
4149
1
            return;
4150
1
        }
4151
4152
        // Only support compact block relay with witnesses
4153
1.14k
        if (sendcmpct_version != CMPCTBLOCKS_VERSION) return;
4154
4155
1.13k
        LOCK(cs_main);
4156
1.13k
        CNodeState* nodestate = State(pfrom.GetId());
4157
1.13k
        nodestate->m_provides_cmpctblocks = true;
4158
1.13k
        nodestate->m_requested_hb_cmpctblocks = sendcmpct_hb;
4159
        // save whether peer selects us as BIP152 high-bandwidth peer
4160
        // (receiving sendcmpct(1) signals high-bandwidth, sendcmpct(0) low-bandwidth)
4161
1.13k
        pfrom.m_bip152_highbandwidth_from = sendcmpct_hb;
4162
1.13k
        return;
4163
1.14k
    }
4164
4165
    // BIP339 defines feature negotiation of wtxidrelay, which must happen between
4166
    // VERSION and VERACK to avoid relay problems from switching after a connection is up.
4167
152k
    if (msg_type == NetMsgType::WTXIDRELAY) {
4168
1.51k
        if (pfrom.fSuccessfullyConnected) {
4169
            // Disconnect peers that send a wtxidrelay message after VERACK.
4170
0
            LogDebug(BCLog::NET, "wtxidrelay received after verack, %s", pfrom.DisconnectMsg());
4171
0
            pfrom.fDisconnect = true;
4172
0
            return;
4173
0
        }
4174
1.51k
        if (pfrom.GetCommonVersion() >= WTXID_RELAY_VERSION) {
4175
1.51k
            if (!peer.m_wtxid_relay) {
4176
1.51k
                peer.m_wtxid_relay = true;
4177
1.51k
                m_wtxid_relay_peers++;
4178
1.51k
            } else {
4179
0
                LogDebug(BCLog::NET, "ignoring duplicate wtxidrelay from peer=%d\n", pfrom.GetId());
4180
0
            }
4181
1.51k
        } else {
4182
2
            LogDebug(BCLog::NET, "ignoring wtxidrelay due to old common version=%d from peer=%d\n", pfrom.GetCommonVersion(), pfrom.GetId());
4183
2
        }
4184
1.51k
        return;
4185
1.51k
    }
4186
4187
    // BIP155 defines feature negotiation of addrv2 and sendaddrv2, which must happen
4188
    // between VERSION and VERACK.
4189
150k
    if (msg_type == NetMsgType::SENDADDRV2) {
4190
789
        if (pfrom.fSuccessfullyConnected) {
4191
            // Disconnect peers that send a SENDADDRV2 message after VERACK.
4192
1
            LogDebug(BCLog::NET, "sendaddrv2 received after verack, %s", pfrom.DisconnectMsg());
4193
1
            pfrom.fDisconnect = true;
4194
1
            return;
4195
1
        }
4196
788
        peer.m_wants_addrv2 = true;
4197
788
        return;
4198
789
    }
4199
4200
150k
    if (msg_type == NetMsgType::FEATURE) {
4201
60
        if (pfrom.fSuccessfullyConnected) {
4202
            // Disconnect peers that send a FEATURE message after VERACK.
4203
2
            LogDebug(BCLog::NET, "feature received after verack, %s", pfrom.DisconnectMsg());
4204
2
            pfrom.fDisconnect = true;
4205
2
            return;
4206
58
        } else if (pfrom.GetCommonVersion() < FEATURE_VERSION) {
4207
            // Disconnect peers that send a FEATURE message without valid version negotiation.
4208
2
            LogDebug(BCLog::NET, "feature received with incompatible version %d, %s", pfrom.GetCommonVersion(), pfrom.DisconnectMsg());
4209
2
            pfrom.fDisconnect = true;
4210
2
            return;
4211
2
        }
4212
4213
56
        std::string feature_id;
4214
56
        DataStream feature_data;
4215
56
        try {
4216
56
            vRecv >> LIMITED_STRING(feature_id, MAX_FEATUREID_LENGTH);
4217
56
            std::vector<unsigned char> feature_data_vec;
4218
56
            vRecv >> LIMITED_VECTOR(feature_data_vec, MAX_FEATUREDATA_LENGTH);
4219
56
            feature_data = DataStream(feature_data_vec);
4220
56
        } catch (const std::exception&) {
4221
8
            feature_id.clear(); // use empty feature_id as error indicator
4222
8
        }
4223
56
        if (feature_id.size() < 4 || !vRecv.empty()) {
4224
14
            LogDebug(BCLog::NET, "invalid feature payload, %s", pfrom.DisconnectMsg());
4225
14
            pfrom.fDisconnect = true;
4226
14
            return;
4227
14
        }
4228
4229
        // if (feature_id == NetMsgFeature::FOO) {
4230
        //     ...
4231
        //     return;
4232
        // }
4233
4234
        // ignore unknown feature_id
4235
42
        LogDebug(BCLog::NET, "unknown feature advertised: %s", SanitizeString(feature_id));
4236
42
        return;
4237
56
    }
4238
4239
    // Received from a peer demonstrating readiness to announce transactions via reconciliations.
4240
    // This feature negotiation must happen between VERSION and VERACK to avoid relay problems
4241
    // from switching announcement protocols after the connection is up.
4242
149k
    if (msg_type == NetMsgType::SENDTXRCNCL) {
4243
9
        if (!m_txreconciliation) {
4244
1
            LogDebug(BCLog::NET, "sendtxrcncl from peer=%d ignored, as our node does not have txreconciliation enabled\n", pfrom.GetId());
4245
1
            return;
4246
1
        }
4247
4248
8
        if (pfrom.fSuccessfullyConnected) {
4249
1
            LogDebug(BCLog::NET, "sendtxrcncl received after verack, %s", pfrom.DisconnectMsg());
4250
1
            pfrom.fDisconnect = true;
4251
1
            return;
4252
1
        }
4253
4254
        // Peer must not offer us reconciliations if we specified no tx relay support in VERSION.
4255
7
        if (RejectIncomingTxs(pfrom)) {
4256
1
            LogDebug(BCLog::NET, "sendtxrcncl received to which we indicated no tx relay, %s", pfrom.DisconnectMsg());
4257
1
            pfrom.fDisconnect = true;
4258
1
            return;
4259
1
        }
4260
4261
        // Peer must not offer us reconciliations if they specified no tx relay support in VERSION.
4262
        // This flag might also be false in other cases, but the RejectIncomingTxs check above
4263
        // eliminates them, so that this flag fully represents what we are looking for.
4264
6
        const auto* tx_relay = peer.GetTxRelay();
4265
6
        if (!tx_relay || !WITH_LOCK(tx_relay->m_bloom_filter_mutex, return tx_relay->m_relay_txs)) {
4266
0
            LogDebug(BCLog::NET, "sendtxrcncl received which indicated no tx relay to us, %s", pfrom.DisconnectMsg());
4267
0
            pfrom.fDisconnect = true;
4268
0
            return;
4269
0
        }
4270
4271
6
        uint32_t peer_txreconcl_version;
4272
6
        uint64_t remote_salt;
4273
6
        vRecv >> peer_txreconcl_version >> remote_salt;
4274
4275
6
        const ReconciliationRegisterResult result = m_txreconciliation->RegisterPeer(pfrom.GetId(), pfrom.IsInboundConn(),
4276
6
                                                                                     peer_txreconcl_version, remote_salt);
4277
6
        switch (result) {
4278
1
        case ReconciliationRegisterResult::NOT_FOUND:
4279
1
            LogDebug(BCLog::NET, "Ignore unexpected txreconciliation signal from peer=%d\n", pfrom.GetId());
4280
1
            break;
4281
3
        case ReconciliationRegisterResult::SUCCESS:
4282
3
            break;
4283
1
        case ReconciliationRegisterResult::ALREADY_REGISTERED:
4284
1
            LogDebug(BCLog::NET, "txreconciliation protocol violation (sendtxrcncl received from already registered peer), %s", pfrom.DisconnectMsg());
4285
1
            pfrom.fDisconnect = true;
4286
1
            return;
4287
1
        case ReconciliationRegisterResult::PROTOCOL_VIOLATION:
4288
1
            LogDebug(BCLog::NET, "txreconciliation protocol violation, %s", pfrom.DisconnectMsg());
4289
1
            pfrom.fDisconnect = true;
4290
1
            return;
4291
6
        }
4292
4
        return;
4293
6
    }
4294
4295
149k
    if (!pfrom.fSuccessfullyConnected) {
4296
8
        LogDebug(BCLog::NET, "Unsupported message \"%s\" prior to verack from peer=%d\n", SanitizeString(msg_type), pfrom.GetId());
4297
8
        return;
4298
8
    }
4299
4300
149k
    if (pfrom.IsPrivateBroadcastConn()) {
4301
28
        if (msg_type != NetMsgType::PONG && msg_type != NetMsgType::GETDATA) {
4302
2
            LogDebug(BCLog::PRIVBROADCAST, "Ignoring incoming message '%s', %s", msg_type, pfrom.LogPeer());
4303
2
            return;
4304
2
        }
4305
28
    }
4306
4307
149k
    if (msg_type == NetMsgType::ADDR || msg_type == NetMsgType::ADDRV2) {
4308
59
        const auto ser_params{
4309
59
            msg_type == NetMsgType::ADDRV2 ?
4310
            // Set V2 param so that the CNetAddr and CAddress
4311
            // unserialize methods know that an address in v2 format is coming.
4312
6
            CAddress::V2_NETWORK :
4313
59
            CAddress::V1_NETWORK,
4314
59
        };
4315
4316
59
        std::vector<CAddress> vAddr;
4317
59
        vRecv >> ser_params(vAddr);
4318
59
        ProcessAddrs(msg_type, pfrom, peer, std::move(vAddr), interruptMsgProc);
4319
59
        return;
4320
59
    }
4321
4322
149k
    if (msg_type == NetMsgType::INV) {
4323
10.6k
        std::vector<CInv> vInv;
4324
10.6k
        vRecv >> vInv;
4325
10.6k
        if (vInv.size() > MAX_INV_SZ)
4326
1
        {
4327
1
            Misbehaving(peer, strprintf("inv message size = %u", vInv.size()));
4328
1
            return;
4329
1
        }
4330
4331
10.6k
        const bool reject_tx_invs{RejectIncomingTxs(pfrom)};
4332
4333
10.6k
        LOCK2(cs_main, m_tx_download_mutex);
4334
4335
10.6k
        const auto current_time{GetTime<std::chrono::microseconds>()};
4336
10.6k
        uint256* best_block{nullptr};
4337
4338
28.5k
        for (CInv& inv : vInv) {
4339
28.5k
            if (interruptMsgProc) return;
4340
4341
            // Ignore INVs that don't match wtxidrelay setting.
4342
            // Note that orphan parent fetching always uses MSG_TX GETDATAs regardless of the wtxidrelay setting.
4343
            // This is fine as no INV messages are involved in that process.
4344
28.5k
            if (peer.m_wtxid_relay) {
4345
28.4k
                if (inv.IsMsgTx()) continue;
4346
28.4k
            } else {
4347
18
                if (inv.IsMsgWtx()) continue;
4348
18
            }
4349
4350
28.5k
            if (inv.IsMsgBlk()) {
4351
1.66k
                const bool fAlreadyHave = AlreadyHaveBlock(inv.hash);
4352
1.66k
                LogDebug(BCLog::NET, "got inv: %s %s peer=%d", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId());
4353
4354
1.66k
                UpdateBlockAvailability(pfrom.GetId(), inv.hash);
4355
1.66k
                if (!fAlreadyHave && !m_chainman.m_blockman.LoadingBlocks() && !IsBlockRequested(inv.hash)) {
4356
                    // Headers-first is the primary method of announcement on
4357
                    // the network. If a node fell back to sending blocks by
4358
                    // inv, it may be for a re-org, or because we haven't
4359
                    // completed initial headers sync. The final block hash
4360
                    // provided should be the highest, so send a getheaders and
4361
                    // then fetch the blocks we need to catch up.
4362
1.50k
                    best_block = &inv.hash;
4363
1.50k
                }
4364
26.8k
            } else if (inv.IsGenTxMsg()) {
4365
26.8k
                if (reject_tx_invs) {
4366
2
                    LogDebug(BCLog::NET, "transaction (%s) inv sent in violation of protocol, %s", inv.hash.ToString(), pfrom.DisconnectMsg());
4367
2
                    pfrom.fDisconnect = true;
4368
2
                    return;
4369
2
                }
4370
26.8k
                const GenTxid gtxid = ToGenTxid(inv);
4371
26.8k
                AddKnownTx(peer, inv.hash);
4372
4373
26.8k
                if (!m_chainman.IsInitialBlockDownload()) {
4374
26.8k
                    const bool fAlreadyHave{m_txdownloadman.AddTxAnnouncement(pfrom.GetId(), gtxid, current_time)};
4375
26.8k
                    LogDebug(BCLog::NET, "got inv: %s %s peer=%d", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId());
4376
26.8k
                }
4377
26.8k
            } else {
4378
0
                LogDebug(BCLog::NET, "Unknown inv type \"%s\" received from peer=%d\n", inv.ToString(), pfrom.GetId());
4379
0
            }
4380
28.5k
        }
4381
4382
10.6k
        if (best_block != nullptr) {
4383
            // If we haven't started initial headers-sync with this peer, then
4384
            // consider sending a getheaders now. On initial startup, there's a
4385
            // reliability vs bandwidth tradeoff, where we are only trying to do
4386
            // initial headers sync with one peer at a time, with a long
4387
            // timeout (at which point, if the sync hasn't completed, we will
4388
            // disconnect the peer and then choose another). In the meantime,
4389
            // as new blocks are found, we are willing to add one new peer per
4390
            // block to sync with as well, to sync quicker in the case where
4391
            // our initial peer is unresponsive (but less bandwidth than we'd
4392
            // use if we turned on sync with all peers).
4393
1.50k
            CNodeState& state{*Assert(State(pfrom.GetId()))};
4394
1.50k
            if (state.fSyncStarted || (!peer.m_inv_triggered_getheaders_before_sync && *best_block != m_last_block_inv_triggering_headers_sync)) {
4395
1.48k
                if (MaybeSendGetHeaders(pfrom, GetLocator(m_chainman.m_best_header), peer)) {
4396
1.15k
                    LogDebug(BCLog::NET, "getheaders (%d) %s to peer=%d\n",
4397
1.15k
                            m_chainman.m_best_header->nHeight, best_block->ToString(),
4398
1.15k
                            pfrom.GetId());
4399
1.15k
                }
4400
1.48k
                if (!state.fSyncStarted) {
4401
14
                    peer.m_inv_triggered_getheaders_before_sync = true;
4402
                    // Update the last block hash that triggered a new headers
4403
                    // sync, so that we don't turn on headers sync with more
4404
                    // than 1 new peer every new block.
4405
14
                    m_last_block_inv_triggering_headers_sync = *best_block;
4406
14
                }
4407
1.48k
            }
4408
1.50k
        }
4409
4410
10.6k
        return;
4411
10.6k
    }
4412
4413
139k
    if (msg_type == NetMsgType::GETDATA) {
4414
42.0k
        std::vector<CInv> vInv;
4415
42.0k
        vRecv >> vInv;
4416
42.0k
        if (vInv.size() > MAX_INV_SZ)
4417
1
        {
4418
1
            Misbehaving(peer, strprintf("getdata message size = %u", vInv.size()));
4419
1
            return;
4420
1
        }
4421
4422
42.0k
        LogDebug(BCLog::NET, "received getdata (%u invsz) peer=%d\n", vInv.size(), pfrom.GetId());
4423
4424
42.0k
        if (vInv.size() > 0) {
4425
42.0k
            LogDebug(BCLog::NET, "received getdata for: %s peer=%d\n", vInv[0].ToString(), pfrom.GetId());
4426
42.0k
        }
4427
4428
42.0k
        if (pfrom.IsPrivateBroadcastConn()) {
4429
13
            const auto pushed_tx_opt{m_tx_for_private_broadcast.GetTxForNode(pfrom.GetId())};
4430
13
            if (!pushed_tx_opt) {
4431
0
                LogDebug(BCLog::PRIVBROADCAST, "Disconnecting: got GETDATA without sending an INV, %s",
4432
0
                         pfrom.LogPeer());
4433
0
                pfrom.fDisconnect = true;
4434
0
                return;
4435
0
            }
4436
4437
13
            const CTransactionRef& pushed_tx{*pushed_tx_opt};
4438
4439
            // The GETDATA request must contain exactly one inv and it must be for the transaction
4440
            // that we INVed to the peer earlier.
4441
13
            if (vInv.size() == 1 && vInv[0].IsMsgTx() && vInv[0].hash == pushed_tx->GetHash().ToUint256()) {
4442
4443
13
                MakeAndPushMessage(pfrom, NetMsgType::TX, TX_WITH_WITNESS(*pushed_tx));
4444
4445
13
                peer.m_ping_queued = true; // Ensure a ping will be sent: mimic a request via RPC.
4446
13
                MaybeSendPing(pfrom, peer, NodeClock::now());
4447
13
            } else {
4448
0
                LogDebug(BCLog::PRIVBROADCAST, "Disconnecting: got an unexpected GETDATA message, %s",
4449
0
                         pfrom.LogPeer());
4450
0
                pfrom.fDisconnect = true;
4451
0
            }
4452
13
            return;
4453
13
        }
4454
4455
42.0k
        {
4456
42.0k
            LOCK(peer.m_getdata_requests_mutex);
4457
42.0k
            peer.m_getdata_requests.insert(peer.m_getdata_requests.end(), vInv.begin(), vInv.end());
4458
42.0k
            ProcessGetData(pfrom, peer, interruptMsgProc);
4459
42.0k
        }
4460
4461
42.0k
        return;
4462
42.0k
    }
4463
4464
97.1k
    if (msg_type == NetMsgType::GETBLOCKS) {
4465
4
        CBlockLocator locator;
4466
4
        uint256 hashStop;
4467
4
        vRecv >> locator >> hashStop;
4468
4469
4
        if (locator.vHave.size() > MAX_LOCATOR_SZ) {
4470
1
            LogDebug(BCLog::NET, "getblocks locator size %lld > %d, %s", locator.vHave.size(), MAX_LOCATOR_SZ, pfrom.DisconnectMsg());
4471
1
            pfrom.fDisconnect = true;
4472
1
            return;
4473
1
        }
4474
4475
        // We might have announced the currently-being-connected tip using a
4476
        // compact block, which resulted in the peer sending a getblocks
4477
        // request, which we would otherwise respond to without the new block.
4478
        // To avoid this situation we simply verify that we are on our best
4479
        // known chain now. This is super overkill, but we handle it better
4480
        // for getheaders requests, and there are no known nodes which support
4481
        // compact blocks but still use getblocks to request blocks.
4482
3
        {
4483
3
            std::shared_ptr<const CBlock> a_recent_block;
4484
3
            {
4485
3
                LOCK(m_most_recent_block_mutex);
4486
3
                a_recent_block = m_most_recent_block;
4487
3
            }
4488
3
            BlockValidationState state;
4489
3
            if (!m_chainman.ActiveChainstate().ActivateBestChain(state, a_recent_block)) {
4490
0
                LogDebug(BCLog::NET, "failed to activate chain (%s)\n", state.ToString());
4491
0
            }
4492
3
        }
4493
4494
3
        LOCK(cs_main);
4495
4496
        // Find the last block the caller has in the main chain
4497
3
        const CBlockIndex* pindex = m_chainman.ActiveChainstate().FindForkInGlobalIndex(locator);
4498
4499
        // Send the rest of the chain
4500
3
        if (pindex)
4501
3
            pindex = m_chainman.ActiveChain().Next(*pindex);
4502
3
        int nLimit = 500;
4503
3
        LogDebug(BCLog::NET, "getblocks %d to %s limit %d from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), nLimit, pfrom.GetId());
4504
22
        for (; pindex; pindex = m_chainman.ActiveChain().Next(*pindex))
4505
19
        {
4506
19
            if (pindex->GetBlockHash() == hashStop)
4507
0
            {
4508
0
                LogDebug(BCLog::NET, " getblocks stopping at %d %s", pindex->nHeight, pindex->GetBlockHash().ToString());
4509
0
                break;
4510
0
            }
4511
            // If pruning, don't inv blocks unless we have on disk and are likely to still have
4512
            // for some reasonable time window (1 hour) that block relay might require.
4513
19
            const int nPrunedBlocksLikelyToHave = MIN_BLOCKS_TO_KEEP - 3600 / m_chainparams.GetConsensus().nPowTargetSpacing;
4514
19
            if (m_chainman.m_blockman.IsPruneMode() && (!(pindex->nStatus & BLOCK_HAVE_DATA) || pindex->nHeight <= m_chainman.ActiveChain().Tip()->nHeight - nPrunedBlocksLikelyToHave)) {
4515
0
                LogDebug(BCLog::NET, " getblocks stopping, pruned or too old block at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
4516
0
                break;
4517
0
            }
4518
19
            WITH_LOCK(peer.m_block_inv_mutex, peer.m_blocks_for_inv_relay.push_back(pindex->GetBlockHash()));
4519
19
            if (--nLimit <= 0) {
4520
                // When this block is requested, we'll send an inv that'll
4521
                // trigger the peer to getblocks the next batch of inventory.
4522
0
                LogDebug(BCLog::NET, " getblocks stopping at limit %d %s", pindex->nHeight, pindex->GetBlockHash().ToString());
4523
0
                WITH_LOCK(peer.m_block_inv_mutex, {peer.m_continuation_block = pindex->GetBlockHash();});
4524
0
                break;
4525
0
            }
4526
19
        }
4527
3
        return;
4528
4
    }
4529
4530
97.1k
    if (msg_type == NetMsgType::GETBLOCKTXN) {
4531
552
        BlockTransactionsRequest req;
4532
552
        vRecv >> req;
4533
        // Verify differential encoding invariant: indexes must be strictly increasing
4534
        // DifferenceFormatter should guarantee this property during deserialization
4535
1.44k
        for (size_t i = 1; i < req.indexes.size(); ++i) {
4536
891
            Assume(req.indexes[i] > req.indexes[i-1]);
4537
891
        }
4538
4539
552
        std::shared_ptr<const CBlock> recent_block;
4540
552
        {
4541
552
            LOCK(m_most_recent_block_mutex);
4542
552
            if (m_most_recent_block_hash == req.blockhash)
4543
494
                recent_block = m_most_recent_block;
4544
            // Unlock m_most_recent_block_mutex to avoid cs_main lock inversion
4545
552
        }
4546
552
        if (recent_block) {
4547
494
            SendBlockTransactions(pfrom, peer, *recent_block, req);
4548
494
            return;
4549
494
        }
4550
4551
58
        FlatFilePos block_pos{};
4552
58
        {
4553
58
            LOCK(cs_main);
4554
4555
58
            const CBlockIndex* pindex = m_chainman.m_blockman.LookupBlockIndex(req.blockhash);
4556
58
            if (!pindex || !(pindex->nStatus & BLOCK_HAVE_DATA)) {
4557
1
                LogDebug(BCLog::NET, "Peer %d sent us a getblocktxn for a block we don't have\n", pfrom.GetId());
4558
1
                return;
4559
1
            }
4560
4561
57
            if (pindex->nHeight >= m_chainman.ActiveChain().Height() - MAX_BLOCKTXN_DEPTH) {
4562
56
                block_pos = pindex->GetBlockPos();
4563
56
            }
4564
57
        }
4565
4566
57
        if (!block_pos.IsNull()) {
4567
56
            CBlock block;
4568
56
            const bool ret{m_chainman.m_blockman.ReadBlock(block, block_pos, req.blockhash)};
4569
            // If height is above MAX_BLOCKTXN_DEPTH then this block cannot get
4570
            // pruned after we release cs_main above, so this read should never fail.
4571
56
            assert(ret);
4572
4573
56
            SendBlockTransactions(pfrom, peer, block, req);
4574
56
            return;
4575
56
        }
4576
4577
        // If an older block is requested (should never happen in practice,
4578
        // but can happen in tests) send a block response instead of a
4579
        // blocktxn response. Sending a full block response instead of a
4580
        // small blocktxn response is preferable in the case where a peer
4581
        // might maliciously send lots of getblocktxn requests to trigger
4582
        // expensive disk reads, because it will require the peer to
4583
        // actually receive all the data read from disk over the network.
4584
1
        LogDebug(BCLog::NET, "Peer %d sent us a getblocktxn for a block > %i deep\n", pfrom.GetId(), MAX_BLOCKTXN_DEPTH);
4585
1
        CInv inv{MSG_WITNESS_BLOCK, req.blockhash};
4586
1
        WITH_LOCK(peer.m_getdata_requests_mutex, peer.m_getdata_requests.push_back(inv));
4587
        // The message processing loop will go around again (without pausing) and we'll respond then
4588
1
        return;
4589
57
    }
4590
4591
96.5k
    if (msg_type == NetMsgType::GETHEADERS) {
4592
2.02k
        CBlockLocator locator;
4593
2.02k
        uint256 hashStop;
4594
2.02k
        vRecv >> locator >> hashStop;
4595
4596
2.02k
        if (locator.vHave.size() > MAX_LOCATOR_SZ) {
4597
1
            LogDebug(BCLog::NET, "getheaders locator size %lld > %d, %s", locator.vHave.size(), MAX_LOCATOR_SZ, pfrom.DisconnectMsg());
4598
1
            pfrom.fDisconnect = true;
4599
1
            return;
4600
1
        }
4601
4602
2.02k
        if (m_chainman.m_blockman.LoadingBlocks()) {
4603
0
            LogDebug(BCLog::NET, "Ignoring getheaders from peer=%d while importing/reindexing\n", pfrom.GetId());
4604
0
            return;
4605
0
        }
4606
4607
2.02k
        LOCK(cs_main);
4608
4609
        // Don't serve headers from our active chain until our chainwork is at least
4610
        // the minimum chain work. This prevents us from starting a low-work headers
4611
        // sync that will inevitably be aborted by our peer.
4612
2.02k
        if (m_chainman.ActiveTip() == nullptr ||
4613
2.02k
                (m_chainman.ActiveTip()->nChainWork < m_chainman.MinimumChainWork() && !pfrom.HasPermission(NetPermissionFlags::Download))) {
4614
9
            LogDebug(BCLog::NET, "Ignoring getheaders from peer=%d because active chain has too little work; sending empty response\n", pfrom.GetId());
4615
            // Just respond with an empty headers message, to tell the peer to
4616
            // go away but not treat us as unresponsive.
4617
9
            MakeAndPushMessage(pfrom, NetMsgType::HEADERS, std::vector<CBlockHeader>());
4618
9
            return;
4619
9
        }
4620
4621
2.01k
        CNodeState *nodestate = State(pfrom.GetId());
4622
2.01k
        const CBlockIndex* pindex = nullptr;
4623
2.01k
        if (locator.IsNull())
4624
6
        {
4625
            // If locator is null, return the hashStop block
4626
6
            pindex = m_chainman.m_blockman.LookupBlockIndex(hashStop);
4627
6
            if (!pindex) {
4628
0
                return;
4629
0
            }
4630
6
            if (!BlockRequestAllowed(*pindex)) {
4631
2
                LogDebug(BCLog::NET, "%s: ignoring request from peer=%i for old block header that isn't in the main chain\n", __func__, pfrom.GetId());
4632
2
                return;
4633
2
            }
4634
6
        }
4635
2.00k
        else
4636
2.00k
        {
4637
            // Find the last block the caller has in the main chain
4638
2.00k
            pindex = m_chainman.ActiveChainstate().FindForkInGlobalIndex(locator);
4639
2.00k
            if (pindex)
4640
2.00k
                pindex = m_chainman.ActiveChain().Next(*pindex);
4641
2.00k
        }
4642
4643
        // we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
4644
2.01k
        std::vector<CBlock> vHeaders;
4645
2.01k
        int nLimit = m_opts.max_headers_result;
4646
2.01k
        LogDebug(BCLog::NET, "getheaders %d to %s from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), pfrom.GetId());
4647
420k
        for (; pindex; pindex = m_chainman.ActiveChain().Next(*pindex))
4648
418k
        {
4649
418k
            vHeaders.emplace_back(pindex->GetBlockHeader());
4650
418k
            if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
4651
33
                break;
4652
418k
        }
4653
        // pindex can be nullptr either if we sent m_chainman.ActiveChain().Tip() OR
4654
        // if our peer has m_chainman.ActiveChain().Tip() (and thus we are sending an empty
4655
        // headers message). In both cases it's safe to update
4656
        // pindexBestHeaderSent to be our tip.
4657
        //
4658
        // It is important that we simply reset the BestHeaderSent value here,
4659
        // and not max(BestHeaderSent, newHeaderSent). We might have announced
4660
        // the currently-being-connected tip using a compact block, which
4661
        // resulted in the peer sending a headers request, which we respond to
4662
        // without the new block. By resetting the BestHeaderSent, we ensure we
4663
        // will re-announce the new block via headers (or compact blocks again)
4664
        // in the SendMessages logic.
4665
2.01k
        nodestate->pindexBestHeaderSent = pindex ? pindex : m_chainman.ActiveChain().Tip();
4666
2.01k
        MakeAndPushMessage(pfrom, NetMsgType::HEADERS, TX_WITH_WITNESS(vHeaders));
4667
2.01k
        return;
4668
2.01k
    }
4669
4670
94.5k
    if (msg_type == NetMsgType::TX) {
4671
16.0k
        if (RejectIncomingTxs(pfrom)) {
4672
2
            LogDebug(BCLog::NET, "transaction sent in violation of protocol, %s", pfrom.DisconnectMsg());
4673
2
            pfrom.fDisconnect = true;
4674
2
            return;
4675
2
        }
4676
4677
        // Stop processing the transaction early if we are still in IBD since we don't
4678
        // have enough information to validate it yet. Sending unsolicited transactions
4679
        // is not considered a protocol violation, so don't punish the peer.
4680
16.0k
        if (m_chainman.IsInitialBlockDownload()) return;
4681
4682
16.0k
        CTransactionRef ptx;
4683
16.0k
        vRecv >> TX_WITH_WITNESS(ptx);
4684
4685
16.0k
        const Txid& txid = ptx->GetHash();
4686
16.0k
        const Wtxid& wtxid = ptx->GetWitnessHash();
4687
4688
16.0k
        const uint256& hash = peer.m_wtxid_relay ? wtxid.ToUint256() : txid.ToUint256();
4689
16.0k
        AddKnownTx(peer, hash);
4690
4691
16.0k
        if (const auto num_broadcasted{m_tx_for_private_broadcast.Remove(ptx)}) {
4692
1
            LogDebug(BCLog::PRIVBROADCAST, "Received our privately broadcast transaction (txid=%s) from the "
4693
1
                                           "network from %s; stopping private broadcast attempts",
4694
1
                     txid.ToString(), pfrom.LogPeer());
4695
1
            if (NUM_PRIVATE_BROADCAST_PER_TX > num_broadcasted.value()) {
4696
                // Not all of the initial NUM_PRIVATE_BROADCAST_PER_TX connections were needed.
4697
                // Tell CConnman it does not need to start the remaining ones.
4698
0
                m_connman.m_private_broadcast.NumToOpenSub(NUM_PRIVATE_BROADCAST_PER_TX - num_broadcasted.value());
4699
0
            }
4700
1
        }
4701
4702
16.0k
        LOCK2(cs_main, m_tx_download_mutex);
4703
4704
16.0k
        const auto& [should_validate, package_to_validate] = m_txdownloadman.ReceivedTx(pfrom.GetId(), ptx);
4705
16.0k
        if (!should_validate) {
4706
3.31k
            if (pfrom.HasPermission(NetPermissionFlags::ForceRelay)) {
4707
                // Always relay transactions received from peers with forcerelay
4708
                // permission, even if they were already in the mempool, allowing
4709
                // the node to function as a gateway for nodes hidden behind it.
4710
2
                if (!m_mempool.exists(txid)) {
4711
1
                    LogInfo("Not relaying non-mempool transaction %s (wtxid=%s) from forcerelay peer=%d\n",
4712
1
                              txid.ToString(), wtxid.ToString(), pfrom.GetId());
4713
1
                } else {
4714
1
                    LogInfo("Force relaying tx %s (wtxid=%s) from peer=%d\n",
4715
1
                              txid.ToString(), wtxid.ToString(), pfrom.GetId());
4716
1
                    InitiateTxBroadcastToAll(wtxid);
4717
1
                }
4718
2
            }
4719
4720
3.31k
            if (package_to_validate) {
4721
11
                const auto package_result{ProcessNewPackage(m_chainman.ActiveChainstate(), m_mempool, package_to_validate->m_txns, /*test_accept=*/false, /*client_maxfeerate=*/std::nullopt)};
4722
11
                LogDebug(BCLog::TXPACKAGES, "package evaluation for %s: %s\n", package_to_validate->ToString(),
4723
11
                         package_result.m_state.IsValid() ? "package accepted" : "package rejected");
4724
11
                ProcessPackageResult(package_to_validate.value(), package_result);
4725
11
            }
4726
3.31k
            return;
4727
3.31k
        }
4728
4729
        // ReceivedTx should not be telling us to validate the tx and a package.
4730
12.7k
        Assume(!package_to_validate.has_value());
4731
4732
12.7k
        const MempoolAcceptResult result = m_chainman.ProcessTransaction(ptx);
4733
12.7k
        const TxValidationState& state = result.m_state;
4734
4735
12.7k
        if (result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
4736
11.9k
            ProcessValidTx(pfrom.GetId(), ptx, result.m_replaced_transactions);
4737
11.9k
            pfrom.m_last_tx_time = GetTime<std::chrono::seconds>();
4738
11.9k
        }
4739
12.7k
        if (state.IsInvalid()) {
4740
828
            if (auto package_to_validate{ProcessInvalidTx(pfrom.GetId(), ptx, state, /*first_time_failure=*/true)}) {
4741
19
                const auto package_result{ProcessNewPackage(m_chainman.ActiveChainstate(), m_mempool, package_to_validate->m_txns, /*test_accept=*/false, /*client_maxfeerate=*/std::nullopt)};
4742
19
                LogDebug(BCLog::TXPACKAGES, "package evaluation for %s: %s\n", package_to_validate->ToString(),
4743
19
                         package_result.m_state.IsValid() ? "package accepted" : "package rejected");
4744
19
                ProcessPackageResult(package_to_validate.value(), package_result);
4745
19
            }
4746
828
        }
4747
4748
12.7k
        return;
4749
16.0k
    }
4750
4751
78.4k
    if (msg_type == NetMsgType::CMPCTBLOCK)
4752
21.6k
    {
4753
        // Ignore cmpctblock received while importing
4754
21.6k
        if (m_chainman.m_blockman.LoadingBlocks()) {
4755
0
            LogDebug(BCLog::CMPCTBLOCK, "%s sent us a compact block even though we are still loading blocks!", pfrom.LogPeer());
4756
0
            return;
4757
21.6k
        } else if (m_opts.ignore_incoming_txs) {
4758
2
            LogDebug(BCLog::CMPCTBLOCK, "%s sent us a compact block even though we are blocksonly!", pfrom.LogPeer());
4759
2
            return;
4760
2
        }
4761
4762
21.6k
        {
4763
21.6k
            LOCK(cs_main);
4764
21.6k
            const CNodeState *nodestate = State(pfrom.GetId());
4765
21.6k
            if (!nodestate->m_provides_cmpctblocks) {
4766
2
                LogDebug(BCLog::CMPCTBLOCK, "%s sent us a compact block despite never having sent us a SENDCMPCT!", pfrom.LogPeer());
4767
2
                return;
4768
2
            }
4769
21.6k
        }
4770
4771
21.6k
        CBlockHeaderAndShortTxIDs cmpctblock;
4772
21.6k
        vRecv >> cmpctblock;
4773
4774
21.6k
        bool received_new_header = false;
4775
21.6k
        const auto blockhash = cmpctblock.header.GetHash();
4776
4777
21.6k
        {
4778
21.6k
        LOCK(cs_main);
4779
4780
21.6k
        const CBlockIndex* prev_block = m_chainman.m_blockman.LookupBlockIndex(cmpctblock.header.hashPrevBlock);
4781
21.6k
        if (!prev_block) {
4782
            // Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
4783
39
            if (!m_chainman.IsInitialBlockDownload()) {
4784
39
                MaybeSendGetHeaders(pfrom, GetLocator(m_chainman.m_best_header), peer);
4785
39
            }
4786
39
            return;
4787
21.6k
        } else if (prev_block->nChainWork + GetBlockProof(cmpctblock.header) < GetAntiDoSWorkThreshold()) {
4788
            // If we get a low-work header in a compact block, we can ignore it.
4789
8
            LogDebug(BCLog::NET, "Ignoring low-work compact block from peer %d\n", pfrom.GetId());
4790
8
            return;
4791
8
        }
4792
4793
21.5k
        if (!m_chainman.m_blockman.LookupBlockIndex(blockhash)) {
4794
18.7k
            received_new_header = true;
4795
18.7k
        }
4796
21.5k
        }
4797
4798
0
        const CBlockIndex *pindex = nullptr;
4799
21.5k
        BlockValidationState state;
4800
21.5k
        if (!m_chainman.ProcessNewBlockHeaders({{cmpctblock.header}}, /*min_pow_checked=*/true, state, &pindex)) {
4801
4
            if (state.IsInvalid()) {
4802
4
                MaybePunishNodeForBlock(pfrom.GetId(), state, /*via_compact_block=*/true, "invalid header via cmpctblock");
4803
4
                return;
4804
4
            }
4805
4
        }
4806
4807
        // If AcceptBlockHeader returned true, it set pindex
4808
21.5k
        Assert(pindex);
4809
21.5k
        if (received_new_header) {
4810
18.7k
            LogBlockHeader(*pindex, pfrom, /*via_compact_block=*/true);
4811
18.7k
        }
4812
4813
21.5k
        bool fProcessBLOCKTXN = false;
4814
4815
        // If we end up treating this as a plain headers message, call that as well
4816
        // without cs_main.
4817
21.5k
        bool fRevertToHeaderProcessing = false;
4818
4819
        // Keep a CBlock for "optimistic" compactblock reconstructions (see
4820
        // below)
4821
21.5k
        std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
4822
21.5k
        bool fBlockReconstructed = false;
4823
4824
21.5k
        {
4825
21.5k
        LOCK(cs_main);
4826
21.5k
        UpdateBlockAvailability(pfrom.GetId(), pindex->GetBlockHash());
4827
4828
21.5k
        CNodeState *nodestate = State(pfrom.GetId());
4829
4830
        // If this was a new header with more work than our tip, update the
4831
        // peer's last block announcement time
4832
21.5k
        if (received_new_header && pindex->nChainWork > m_chainman.ActiveChain().Tip()->nChainWork) {
4833
18.5k
            nodestate->m_last_block_announcement = GetTime();
4834
18.5k
        }
4835
4836
21.5k
        if (pindex->nStatus & BLOCK_HAVE_DATA) // Nothing to do here
4837
2.18k
            return;
4838
4839
19.4k
        auto range_flight = mapBlocksInFlight.equal_range(pindex->GetBlockHash());
4840
19.4k
        size_t already_in_flight = std::distance(range_flight.first, range_flight.second);
4841
19.4k
        bool requested_block_from_this_peer{false};
4842
4843
        // Multimap ensures ordering of outstanding requests. It's either empty or first in line.
4844
19.4k
        bool first_in_flight = already_in_flight == 0 || (range_flight.first->second.first == pfrom.GetId());
4845
4846
19.8k
        while (range_flight.first != range_flight.second) {
4847
652
            if (range_flight.first->second.first == pfrom.GetId()) {
4848
244
                requested_block_from_this_peer = true;
4849
244
                break;
4850
244
            }
4851
408
            range_flight.first++;
4852
408
        }
4853
4854
19.4k
        if (!requested_block_from_this_peer && !pfrom.m_bip152_highbandwidth_to) {
4855
3
            LogDebug(BCLog::CMPCTBLOCK, "%s, not marked as high-bandwidth, sent us an unsolicited compact block!", pfrom.LogPeer());
4856
3
            return;
4857
3
        }
4858
4859
19.4k
        if (pindex->nChainWork <= m_chainman.ActiveChain().Tip()->nChainWork || // We know something better
4860
19.4k
                pindex->nTx != 0) { // We had this block at some point, but pruned it
4861
172
            if (requested_block_from_this_peer) {
4862
                // We requested this block for some reason, but our mempool will probably be useless
4863
                // so we just grab the block via normal getdata
4864
4
                std::vector<CInv> vInv(1);
4865
4
                vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(peer), blockhash);
4866
4
                MakeAndPushMessage(pfrom, NetMsgType::GETDATA, vInv);
4867
4
            }
4868
172
            return;
4869
172
        }
4870
4871
        // If we're not close to tip yet, give up and let parallel block fetch work its magic
4872
19.2k
        if (!already_in_flight && !CanDirectFetch()) {
4873
9
            return;
4874
9
        }
4875
4876
        // We want to be a bit conservative just to be extra careful about DoS
4877
        // possibilities in compact block processing...
4878
19.2k
        if (pindex->nHeight <= m_chainman.ActiveChain().Height() + 2) {
4879
17.7k
            if ((already_in_flight < MAX_CMPCTBLOCKS_INFLIGHT_PER_BLOCK && nodestate->vBlocksInFlight.size() < MAX_BLOCKS_IN_TRANSIT_PER_PEER) ||
4880
17.7k
                 requested_block_from_this_peer) {
4881
17.7k
                std::list<QueuedBlock>::iterator* queuedBlockIt = nullptr;
4882
17.7k
                if (!BlockRequested(pfrom.GetId(), *pindex, &queuedBlockIt)) {
4883
240
                    if (!(*queuedBlockIt)->partialBlock)
4884
240
                        (*queuedBlockIt)->partialBlock.reset(new PartiallyDownloadedBlock(&m_mempool));
4885
0
                    else {
4886
                        // The block was already in flight using compact blocks from the same peer
4887
0
                        LogDebug(BCLog::NET, "Peer sent us compact block we were already syncing!\n");
4888
0
                        return;
4889
0
                    }
4890
240
                }
4891
4892
17.7k
                PartiallyDownloadedBlock& partialBlock = *(*queuedBlockIt)->partialBlock;
4893
17.7k
                ReadStatus status = partialBlock.InitData(cmpctblock, vExtraTxnForCompact);
4894
17.7k
                if (status == READ_STATUS_INVALID) {
4895
2
                    RemoveBlockRequest(pindex->GetBlockHash(), pfrom.GetId()); // Reset in-flight state in case Misbehaving does not result in a disconnect
4896
2
                    Misbehaving(peer, "invalid compact block");
4897
2
                    return;
4898
17.7k
                } else if (status == READ_STATUS_FAILED) {
4899
0
                    if (first_in_flight)  {
4900
                        // Duplicate txindexes, the block is now in-flight, so just request it
4901
0
                        std::vector<CInv> vInv(1);
4902
0
                        vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(peer), blockhash);
4903
0
                        MakeAndPushMessage(pfrom, NetMsgType::GETDATA, vInv);
4904
0
                    } else {
4905
                        // Give up for this peer and wait for other peer(s)
4906
0
                        RemoveBlockRequest(pindex->GetBlockHash(), pfrom.GetId());
4907
0
                    }
4908
0
                    return;
4909
0
                }
4910
4911
17.7k
                BlockTransactionsRequest req;
4912
48.3k
                for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
4913
30.6k
                    if (!partialBlock.IsTxAvailable(i))
4914
1.51k
                        req.indexes.push_back(i);
4915
30.6k
                }
4916
17.7k
                if (req.indexes.empty()) {
4917
17.2k
                    fProcessBLOCKTXN = true;
4918
17.2k
                } else if (first_in_flight) {
4919
                    // We will try to round-trip any compact blocks we get on failure,
4920
                    // as long as it's first...
4921
523
                    req.blockhash = pindex->GetBlockHash();
4922
523
                    MakeAndPushMessage(pfrom, NetMsgType::GETBLOCKTXN, req);
4923
523
                } else if (pfrom.m_bip152_highbandwidth_to &&
4924
19
                    (!pfrom.IsInboundConn() ||
4925
19
                    IsBlockRequestedFromOutbound(blockhash) ||
4926
19
                    already_in_flight < MAX_CMPCTBLOCKS_INFLIGHT_PER_BLOCK - 1)) {
4927
                    // ... or it's a hb relay peer and:
4928
                    // - peer is outbound, or
4929
                    // - we already have an outbound attempt in flight(so we'll take what we can get), or
4930
                    // - it's not the final parallel download slot (which we may reserve for first outbound)
4931
16
                    req.blockhash = pindex->GetBlockHash();
4932
16
                    MakeAndPushMessage(pfrom, NetMsgType::GETBLOCKTXN, req);
4933
16
                } else {
4934
                    // Give up for this peer and wait for other peer(s)
4935
3
                    RemoveBlockRequest(pindex->GetBlockHash(), pfrom.GetId());
4936
3
                }
4937
17.7k
            } else {
4938
                // This block is either already in flight from a different
4939
                // peer, or this peer has too many blocks outstanding to
4940
                // download from.
4941
                // Optimistically try to reconstruct anyway since we might be
4942
                // able to without any round trips.
4943
1
                PartiallyDownloadedBlock tempBlock(&m_mempool);
4944
1
                ReadStatus status = tempBlock.InitData(cmpctblock, vExtraTxnForCompact);
4945
1
                if (status != READ_STATUS_OK) {
4946
                    // TODO: don't ignore failures
4947
0
                    return;
4948
0
                }
4949
1
                std::vector<CTransactionRef> dummy;
4950
1
                const CBlockIndex* prev_block{Assume(m_chainman.m_blockman.LookupBlockIndex(cmpctblock.header.hashPrevBlock))};
4951
1
                status = tempBlock.FillBlock(*pblock, dummy,
4952
1
                                             /*segwit_active=*/DeploymentActiveAfter(prev_block, m_chainman, Consensus::DEPLOYMENT_SEGWIT));
4953
1
                if (status == READ_STATUS_OK) {
4954
1
                    fBlockReconstructed = true;
4955
1
                }
4956
1
            }
4957
17.7k
        } else {
4958
1.46k
            if (requested_block_from_this_peer) {
4959
                // We requested this block, but its far into the future, so our
4960
                // mempool will probably be useless - request the block normally
4961
0
                std::vector<CInv> vInv(1);
4962
0
                vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(peer), blockhash);
4963
0
                MakeAndPushMessage(pfrom, NetMsgType::GETDATA, vInv);
4964
0
                return;
4965
1.46k
            } else {
4966
                // If this was an announce-cmpctblock, we want the same treatment as a header message
4967
1.46k
                fRevertToHeaderProcessing = true;
4968
1.46k
            }
4969
1.46k
        }
4970
19.2k
        } // cs_main
4971
4972
19.2k
        if (fProcessBLOCKTXN) {
4973
17.2k
            BlockTransactions txn;
4974
17.2k
            txn.blockhash = blockhash;
4975
17.2k
            return ProcessCompactBlockTxns(pfrom, peer, txn);
4976
17.2k
        }
4977
4978
2.00k
        if (fRevertToHeaderProcessing) {
4979
            // Headers received from HB compact block peers are permitted to be
4980
            // relayed before full validation (see BIP 152), so we don't want to disconnect
4981
            // the peer if the header turns out to be for an invalid block.
4982
            // Note that if a peer tries to build on an invalid chain, that
4983
            // will be detected and the peer will be disconnected/discouraged.
4984
1.46k
            return ProcessHeadersMessage(pfrom, peer, {cmpctblock.header}, /*via_compact_block=*/true);
4985
1.46k
        }
4986
4987
543
        if (fBlockReconstructed) {
4988
            // If we got here, we were able to optimistically reconstruct a
4989
            // block that is in flight from some other peer.
4990
1
            {
4991
1
                LOCK(cs_main);
4992
1
                mapBlockSource.emplace(pblock->GetHash(), std::make_pair(pfrom.GetId(), false));
4993
1
            }
4994
            // Setting force_processing to true means that we bypass some of
4995
            // our anti-DoS protections in AcceptBlock, which filters
4996
            // unrequested blocks that might be trying to waste our resources
4997
            // (eg disk space). Because we only try to reconstruct blocks when
4998
            // we're close to caught up (via the CanDirectFetch() requirement
4999
            // above, combined with the behavior of not requesting blocks until
5000
            // we have a chain with at least the minimum chain work), and we ignore
5001
            // compact blocks with less work than our tip, it is safe to treat
5002
            // reconstructed compact blocks as having been requested.
5003
1
            ProcessBlock(pfrom, pblock, /*force_processing=*/true, /*min_pow_checked=*/true);
5004
1
            LOCK(cs_main); // hold cs_main for CBlockIndex::IsValid()
5005
1
            if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS)) {
5006
                // Clear download state for this block, which is in
5007
                // process from some other peer.  We do this after calling
5008
                // ProcessNewBlock so that a malleated cmpctblock announcement
5009
                // can't be used to interfere with block relay.
5010
1
                RemoveBlockRequest(pblock->GetHash(), std::nullopt);
5011
1
            }
5012
1
        }
5013
543
        return;
5014
2.00k
    }
5015
5016
56.8k
    if (msg_type == NetMsgType::BLOCKTXN)
5017
534
    {
5018
        // Ignore blocktxn received while importing
5019
534
        if (m_chainman.m_blockman.LoadingBlocks()) {
5020
0
            LogDebug(BCLog::NET, "Unexpected blocktxn message received from peer %d\n", pfrom.GetId());
5021
0
            return;
5022
0
        }
5023
5024
534
        BlockTransactions resp;
5025
534
        vRecv >> resp;
5026
5027
534
        return ProcessCompactBlockTxns(pfrom, peer, resp);
5028
534
    }
5029
5030
56.3k
    if (msg_type == NetMsgType::HEADERS)
5031
6.16k
    {
5032
        // Ignore headers received while importing
5033
6.16k
        if (m_chainman.m_blockman.LoadingBlocks()) {
5034
0
            LogDebug(BCLog::NET, "Unexpected headers message received from peer %d\n", pfrom.GetId());
5035
0
            return;
5036
0
        }
5037
5038
6.16k
        std::vector<CBlockHeader> headers;
5039
5040
        // Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks.
5041
6.16k
        unsigned int nCount = ReadCompactSize(vRecv);
5042
6.16k
        if (nCount > m_opts.max_headers_result) {
5043
1
            Misbehaving(peer, strprintf("headers message size = %u", nCount));
5044
1
            return;
5045
1
        }
5046
6.16k
        headers.resize(nCount);
5047
460k
        for (unsigned int n = 0; n < nCount; n++) {
5048
454k
            vRecv >> headers[n];
5049
454k
            ReadCompactSize(vRecv); // ignore tx count; assume it is 0.
5050
454k
        }
5051
5052
6.16k
        ProcessHeadersMessage(pfrom, peer, std::move(headers), /*via_compact_block=*/false);
5053
5054
        // Check if the headers presync progress needs to be reported to validation.
5055
        // This needs to be done without holding the m_headers_presync_mutex lock.
5056
6.16k
        if (m_headers_presync_should_signal.exchange(false)) {
5057
8
            HeadersPresyncStats stats;
5058
8
            {
5059
8
                LOCK(m_headers_presync_mutex);
5060
8
                auto it = m_headers_presync_stats.find(m_headers_presync_bestpeer);
5061
8
                if (it != m_headers_presync_stats.end()) stats = it->second;
5062
8
            }
5063
8
            if (stats.second) {
5064
8
                m_chainman.ReportHeadersPresync(stats.second->first, stats.second->second);
5065
8
            }
5066
8
        }
5067
5068
6.16k
        return;
5069
6.16k
    }
5070
5071
50.1k
    if (msg_type == NetMsgType::BLOCK)
5072
37.7k
    {
5073
        // Ignore block received while importing
5074
37.7k
        if (m_chainman.m_blockman.LoadingBlocks()) {
5075
0
            LogDebug(BCLog::NET, "Unexpected block message received from peer %d\n", pfrom.GetId());
5076
0
            return;
5077
0
        }
5078
5079
37.7k
        std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
5080
37.7k
        vRecv >> TX_WITH_WITNESS(*pblock);
5081
5082
37.7k
        LogDebug(BCLog::NET, "received block %s peer=%d\n", pblock->GetHash().ToString(), pfrom.GetId());
5083
5084
37.7k
        const CBlockIndex* prev_block{WITH_LOCK(m_chainman.GetMutex(), return m_chainman.m_blockman.LookupBlockIndex(pblock->hashPrevBlock))};
5085
5086
        // Check for possible mutation if it connects to something we know so we can check for DEPLOYMENT_SEGWIT being active
5087
37.7k
        if (prev_block && IsBlockMutated(/*block=*/*pblock,
5088
37.7k
                           /*check_witness_root=*/DeploymentActiveAfter(prev_block, m_chainman, Consensus::DEPLOYMENT_SEGWIT))) {
5089
112
            LogDebug(BCLog::NET, "Received mutated block from peer=%d\n", peer.m_id);
5090
112
            Misbehaving(peer, "mutated block");
5091
112
            WITH_LOCK(cs_main, RemoveBlockRequest(pblock->GetHash(), peer.m_id));
5092
112
            return;
5093
112
        }
5094
5095
37.6k
        bool forceProcessing = false;
5096
37.6k
        const uint256 hash(pblock->GetHash());
5097
37.6k
        bool min_pow_checked = false;
5098
37.6k
        {
5099
37.6k
            LOCK(cs_main);
5100
            // Always process the block if we requested it, since we may
5101
            // need it even when it's not a candidate for a new best tip.
5102
37.6k
            forceProcessing = IsBlockRequested(hash);
5103
37.6k
            RemoveBlockRequest(hash, pfrom.GetId());
5104
            // mapBlockSource is only used for punishing peers and setting
5105
            // which peers send us compact blocks, so the race between here and
5106
            // cs_main in ProcessNewBlock is fine.
5107
37.6k
            mapBlockSource.emplace(hash, std::make_pair(pfrom.GetId(), true));
5108
5109
            // Check claimed work on this block against our anti-dos thresholds.
5110
37.6k
            if (prev_block && prev_block->nChainWork + GetBlockProof(*pblock) >= GetAntiDoSWorkThreshold()) {
5111
24.7k
                min_pow_checked = true;
5112
24.7k
            }
5113
37.6k
        }
5114
37.6k
        ProcessBlock(pfrom, pblock, forceProcessing, min_pow_checked);
5115
37.6k
        return;
5116
37.7k
    }
5117
5118
12.4k
    if (msg_type == NetMsgType::GETADDR) {
5119
        // This asymmetric behavior for inbound and outbound connections was introduced
5120
        // to prevent a fingerprinting attack: an attacker can send specific fake addresses
5121
        // to users' AddrMan and later request them by sending getaddr messages.
5122
        // Making nodes which are behind NAT and can only make outgoing connections ignore
5123
        // the getaddr message mitigates the attack.
5124
1.02k
        if (!pfrom.IsInboundConn()) {
5125
9
            LogDebug(BCLog::NET, "Ignoring \"getaddr\" from %s connection. peer=%d\n", pfrom.ConnectionTypeAsString(), pfrom.GetId());
5126
9
            return;
5127
9
        }
5128
5129
        // Since this must be an inbound connection, SetupAddressRelay will
5130
        // never fail.
5131
1.02k
        Assume(SetupAddressRelay(pfrom, peer));
5132
5133
        // Only send one GetAddr response per connection to reduce resource waste
5134
        // and discourage addr stamping of INV announcements.
5135
1.02k
        if (peer.m_getaddr_recvd) {
5136
1
            LogDebug(BCLog::NET, "Ignoring repeated \"getaddr\". peer=%d\n", pfrom.GetId());
5137
1
            return;
5138
1
        }
5139
1.01k
        peer.m_getaddr_recvd = true;
5140
5141
1.01k
        peer.m_addrs_to_send.clear();
5142
1.01k
        std::vector<CAddress> vAddr;
5143
1.01k
        if (pfrom.HasPermission(NetPermissionFlags::Addr)) {
5144
36
            vAddr = m_connman.GetAddressesUnsafe(MAX_ADDR_TO_SEND, MAX_PCT_ADDR_TO_SEND, /*network=*/std::nullopt);
5145
983
        } else {
5146
983
            vAddr = m_connman.GetAddresses(pfrom, MAX_ADDR_TO_SEND, MAX_PCT_ADDR_TO_SEND);
5147
983
        }
5148
18.9k
        for (const CAddress &addr : vAddr) {
5149
18.9k
            PushAddress(peer, addr);
5150
18.9k
        }
5151
1.01k
        return;
5152
1.02k
    }
5153
5154
11.3k
    if (msg_type == NetMsgType::MEMPOOL) {
5155
        // Only process received mempool messages if we advertise NODE_BLOOM
5156
        // or if the peer has mempool permissions.
5157
4
        if (!(peer.m_our_services & NODE_BLOOM) && !pfrom.HasPermission(NetPermissionFlags::Mempool))
5158
1
        {
5159
1
            if (!pfrom.HasPermission(NetPermissionFlags::NoBan))
5160
1
            {
5161
1
                LogDebug(BCLog::NET, "mempool request with bloom filters disabled, %s", pfrom.DisconnectMsg());
5162
1
                pfrom.fDisconnect = true;
5163
1
            }
5164
1
            return;
5165
1
        }
5166
5167
3
        if (m_connman.OutboundTargetReached(false) && !pfrom.HasPermission(NetPermissionFlags::Mempool))
5168
1
        {
5169
1
            if (!pfrom.HasPermission(NetPermissionFlags::NoBan))
5170
1
            {
5171
1
                LogDebug(BCLog::NET, "mempool request with bandwidth limit reached, %s", pfrom.DisconnectMsg());
5172
1
                pfrom.fDisconnect = true;
5173
1
            }
5174
1
            return;
5175
1
        }
5176
5177
2
        if (auto tx_relay = peer.GetTxRelay(); tx_relay != nullptr) {
5178
2
            LOCK(tx_relay->m_tx_inventory_mutex);
5179
2
            tx_relay->m_send_mempool = true;
5180
2
        }
5181
2
        return;
5182
3
    }
5183
5184
11.3k
    if (msg_type == NetMsgType::PING) {
5185
7.85k
        if (pfrom.GetCommonVersion() > BIP0031_VERSION) {
5186
7.85k
            uint64_t nonce = 0;
5187
7.85k
            vRecv >> nonce;
5188
            // Echo the message back with the nonce. This allows for two useful features:
5189
            //
5190
            // 1) A remote node can quickly check if the connection is operational
5191
            // 2) Remote nodes can measure the latency of the network thread. If this node
5192
            //    is overloaded it won't respond to pings quickly and the remote node can
5193
            //    avoid sending us more work, like chain download requests.
5194
            //
5195
            // The nonce stops the remote getting confused between different pings: without
5196
            // it, if the remote node sends a ping once per second and this node takes 5
5197
            // seconds to respond to each, the 5th ping the remote sends would appear to
5198
            // return very quickly.
5199
7.85k
            MakeAndPushMessage(pfrom, NetMsgType::PONG, nonce);
5200
7.85k
        }
5201
7.85k
        return;
5202
7.85k
    }
5203
5204
3.52k
    if (msg_type == NetMsgType::PONG) {
5205
2.50k
        ProcessPong(pfrom, peer, /*ping_end=*/time_received, vRecv);
5206
2.50k
        return;
5207
2.50k
    }
5208
5209
1.01k
    if (msg_type == NetMsgType::FILTERLOAD) {
5210
11
        if (!(peer.m_our_services & NODE_BLOOM)) {
5211
1
            LogDebug(BCLog::NET, "filterload received despite not offering bloom services, %s", pfrom.DisconnectMsg());
5212
1
            pfrom.fDisconnect = true;
5213
1
            return;
5214
1
        }
5215
10
        CBloomFilter filter;
5216
10
        vRecv >> filter;
5217
5218
10
        if (!filter.IsWithinSizeConstraints())
5219
2
        {
5220
            // There is no excuse for sending a too-large filter
5221
2
            Misbehaving(peer, "too-large bloom filter");
5222
8
        } else if (auto tx_relay = peer.GetTxRelay(); tx_relay != nullptr) {
5223
8
            {
5224
8
                LOCK(tx_relay->m_bloom_filter_mutex);
5225
8
                tx_relay->m_bloom_filter.reset(new CBloomFilter(filter));
5226
8
                tx_relay->m_relay_txs = true;
5227
8
            }
5228
8
            pfrom.m_bloom_filter_loaded = true;
5229
8
            pfrom.m_relays_txs = true;
5230
8
            MaybeDisconnectForTxRelayCapacity(pfrom, msg_type);
5231
8
        }
5232
10
        return;
5233
11
    }
5234
5235
1.00k
    if (msg_type == NetMsgType::FILTERADD) {
5236
7
        if (!(peer.m_our_services & NODE_BLOOM)) {
5237
1
            LogDebug(BCLog::NET, "filteradd received despite not offering bloom services, %s", pfrom.DisconnectMsg());
5238
1
            pfrom.fDisconnect = true;
5239
1
            return;
5240
1
        }
5241
6
        std::vector<unsigned char> vData;
5242
6
        vRecv >> vData;
5243
5244
        // Nodes must NEVER send a data item > MAX_SCRIPT_ELEMENT_SIZE bytes (the max size for a script data object,
5245
        // and thus, the maximum size any matched object can have) in a filteradd message
5246
6
        bool bad = false;
5247
6
        if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE) {
5248
1
            bad = true;
5249
5
        } else if (auto tx_relay = peer.GetTxRelay(); tx_relay != nullptr) {
5250
5
            LOCK(tx_relay->m_bloom_filter_mutex);
5251
5
            if (tx_relay->m_bloom_filter) {
5252
3
                tx_relay->m_bloom_filter->insert(vData);
5253
3
            } else {
5254
2
                bad = true;
5255
2
            }
5256
5
        }
5257
6
        if (bad) {
5258
3
            Misbehaving(peer, "bad filteradd message");
5259
3
        }
5260
6
        return;
5261
7
    }
5262
5263
1.00k
    if (msg_type == NetMsgType::FILTERCLEAR) {
5264
5
        if (!(peer.m_our_services & NODE_BLOOM)) {
5265
1
            LogDebug(BCLog::NET, "filterclear received despite not offering bloom services, %s", pfrom.DisconnectMsg());
5266
1
            pfrom.fDisconnect = true;
5267
1
            return;
5268
1
        }
5269
4
        auto tx_relay = peer.GetTxRelay();
5270
4
        if (!tx_relay) return;
5271
5272
4
        {
5273
4
            LOCK(tx_relay->m_bloom_filter_mutex);
5274
4
            tx_relay->m_bloom_filter = nullptr;
5275
4
            tx_relay->m_relay_txs = true;
5276
4
        }
5277
4
        pfrom.m_bloom_filter_loaded = false;
5278
4
        pfrom.m_relays_txs = true;
5279
4
        MaybeDisconnectForTxRelayCapacity(pfrom, msg_type);
5280
4
        return;
5281
4
    }
5282
5283
996
    if (msg_type == NetMsgType::FEEFILTER) {
5284
968
        CAmount newFeeFilter = 0;
5285
968
        vRecv >> newFeeFilter;
5286
968
        if (MoneyRange(newFeeFilter)) {
5287
968
            if (auto tx_relay = peer.GetTxRelay(); tx_relay != nullptr) {
5288
968
                tx_relay->m_fee_filter_received = newFeeFilter;
5289
968
            }
5290
968
            LogDebug(BCLog::NET, "received: feefilter of %s from peer=%d\n", CFeeRate(newFeeFilter).ToString(), pfrom.GetId());
5291
968
        }
5292
968
        return;
5293
968
    }
5294
5295
28
    if (msg_type == NetMsgType::GETCFILTERS) {
5296
4
        ProcessGetCFilters(pfrom, peer, vRecv);
5297
4
        return;
5298
4
    }
5299
5300
24
    if (msg_type == NetMsgType::GETCFHEADERS) {
5301
5
        ProcessGetCFHeaders(pfrom, peer, vRecv);
5302
5
        return;
5303
5
    }
5304
5305
19
    if (msg_type == NetMsgType::GETCFCHECKPT) {
5306
6
        ProcessGetCFCheckPt(pfrom, peer, vRecv);
5307
6
        return;
5308
6
    }
5309
5310
13
    if (msg_type == NetMsgType::NOTFOUND) {
5311
7
        std::vector<CInv> vInv;
5312
7
        vRecv >> vInv;
5313
7
        std::vector<GenTxid> tx_invs;
5314
7
        if (vInv.size() <= node::MAX_PEER_TX_ANNOUNCEMENTS + MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
5315
7
            for (CInv &inv : vInv) {
5316
7
                if (inv.IsGenTxMsg()) {
5317
7
                    tx_invs.emplace_back(ToGenTxid(inv));
5318
7
                }
5319
7
            }
5320
7
        }
5321
7
        LOCK(m_tx_download_mutex);
5322
7
        m_txdownloadman.ReceivedNotFound(pfrom.GetId(), tx_invs);
5323
7
        return;
5324
7
    }
5325
5326
    // Ignore unknown message types for extensibility
5327
6
    LogDebug(BCLog::NET, "Unknown message type \"%s\" from peer=%d", SanitizeString(msg_type), pfrom.GetId());
5328
6
    return;
5329
13
}
5330
5331
bool PeerManagerImpl::MaybeDiscourageAndDisconnect(CNode& pnode, Peer& peer)
5332
389k
{
5333
389k
    {
5334
389k
        LOCK(peer.m_misbehavior_mutex);
5335
5336
        // There's nothing to do if the m_should_discourage flag isn't set
5337
389k
        if (!peer.m_should_discourage) return false;
5338
5339
577
        peer.m_should_discourage = false;
5340
577
    } // peer.m_misbehavior_mutex
5341
5342
577
    if (pnode.HasPermission(NetPermissionFlags::NoBan)) {
5343
        // We never disconnect or discourage peers for bad behavior if they have NetPermissionFlags::NoBan permission
5344
480
        LogWarning("Not punishing noban peer %d!", peer.m_id);
5345
480
        return false;
5346
480
    }
5347
5348
97
    if (pnode.IsManualConn()) {
5349
        // We never disconnect or discourage manual peers for bad behavior
5350
0
        LogWarning("Not punishing manually connected peer %d!", peer.m_id);
5351
0
        return false;
5352
0
    }
5353
5354
97
    if (pnode.addr.IsLocal()) {
5355
        // We disconnect local peers for bad behavior but don't discourage (since that would discourage
5356
        // all peers on the same local address)
5357
93
        LogDebug(BCLog::NET, "Warning: disconnecting but not discouraging %s peer %d!\n",
5358
93
                 pnode.m_inbound_onion ? "inbound onion" : "local", peer.m_id);
5359
93
        pnode.fDisconnect = true;
5360
93
        return true;
5361
93
    }
5362
5363
    // Normal case: Disconnect the peer and discourage all nodes sharing the address
5364
4
    LogDebug(BCLog::NET, "Disconnecting and discouraging peer %d!\n", peer.m_id);
5365
4
    if (m_banman) m_banman->Discourage(pnode.addr);
5366
4
    m_connman.DisconnectNode(pnode.addr);
5367
4
    return true;
5368
97
}
5369
5370
bool PeerManagerImpl::MaybeDisconnectForTxRelayCapacity(CNode& node, const std::string& msg_type, std::optional<NodeId> protect_peer)
5371
1.62k
{
5372
1.62k
    if (!node.IsInboundConn() || !node.m_relays_txs) return false;
5373
1.04k
    if (m_connman.EvictTxPeerIfFull(protect_peer)) return false;
5374
5375
4
    LogDebug(BCLog::NET, "failed to find a tx-relaying eviction candidate - connection dropped after %s message, peer=%d\n", msg_type, node.GetId());
5376
4
    node.fDisconnect = true;
5377
4
    return true;
5378
1.04k
}
5379
5380
bool PeerManagerImpl::ProcessMessages(CNode& node, std::atomic<bool>& interruptMsgProc)
5381
389k
{
5382
389k
    AssertLockNotHeld(m_tx_download_mutex);
5383
389k
    AssertLockHeld(g_msgproc_mutex);
5384
5385
389k
    PeerRef maybe_peer{GetPeerRef(node.GetId())};
5386
389k
    if (maybe_peer == nullptr) return false;
5387
389k
    Peer& peer{*maybe_peer};
5388
5389
    // For outbound connections, ensure that the initial VERSION message
5390
    // has been sent first before processing any incoming messages
5391
389k
    if (!node.IsInboundConn() && !peer.m_outbound_version_message_sent) return false;
5392
5393
389k
    {
5394
389k
        LOCK(peer.m_getdata_requests_mutex);
5395
389k
        if (!peer.m_getdata_requests.empty()) {
5396
1.23k
            ProcessGetData(node, peer, interruptMsgProc);
5397
1.23k
        }
5398
389k
    }
5399
5400
389k
    const bool processed_orphan = ProcessOrphanTx(peer);
5401
5402
389k
    if (node.fDisconnect)
5403
1
        return false;
5404
5405
389k
    if (processed_orphan) return true;
5406
5407
    // this maintains the order of responses
5408
    // and prevents m_getdata_requests to grow unbounded
5409
389k
    {
5410
389k
        LOCK(peer.m_getdata_requests_mutex);
5411
389k
        if (!peer.m_getdata_requests.empty()) return true;
5412
389k
    }
5413
5414
    // Don't bother if send buffer is too full to respond anyway
5415
388k
    if (node.fPauseSend) return false;
5416
5417
388k
    auto poll_result{node.PollMessage()};
5418
388k
    if (!poll_result) {
5419
        // No message to process
5420
231k
        return false;
5421
231k
    }
5422
5423
157k
    CNetMessage& msg{poll_result->first};
5424
157k
    bool fMoreWork = poll_result->second;
5425
5426
157k
    TRACEPOINT(net, inbound_message,
5427
157k
        node.GetId(),
5428
157k
        node.m_addr_name.c_str(),
5429
157k
        node.ConnectionTypeAsString().c_str(),
5430
157k
        msg.m_type.c_str(),
5431
157k
        msg.m_recv.size(),
5432
157k
        msg.m_recv.data()
5433
157k
    );
5434
5435
157k
    if (m_opts.capture_messages) {
5436
7
        CaptureMessage(node.addr, msg.m_type, MakeUCharSpan(msg.m_recv), /*is_incoming=*/true);
5437
7
    }
5438
5439
157k
    try {
5440
157k
        ProcessMessage(peer, node, msg.m_type, msg.m_recv, msg.m_time, interruptMsgProc);
5441
157k
        if (interruptMsgProc) return false;
5442
157k
        {
5443
157k
            LOCK(peer.m_getdata_requests_mutex);
5444
157k
            if (!peer.m_getdata_requests.empty()) fMoreWork = true;
5445
157k
        }
5446
        // Does this peer have an orphan ready to reconsider?
5447
        // (Note: we may have provided a parent for an orphan provided
5448
        //  by another peer that was already processed; in that case,
5449
        //  the extra work may not be noticed, possibly resulting in an
5450
        //  unnecessary 100ms delay)
5451
157k
        LOCK(m_tx_download_mutex);
5452
157k
        if (m_txdownloadman.HaveMoreWork(peer.m_id)) fMoreWork = true;
5453
157k
    } catch (const std::exception& e) {
5454
12
        LogDebug(BCLog::NET, "%s(%s, %u bytes): Exception '%s' (%s) caught\n", __func__, SanitizeString(msg.m_type), msg.m_message_size, e.what(), typeid(e).name());
5455
12
    } catch (...) {
5456
0
        LogDebug(BCLog::NET, "%s(%s, %u bytes): Unknown exception caught\n", __func__, SanitizeString(msg.m_type), msg.m_message_size);
5457
0
    }
5458
5459
157k
    return fMoreWork;
5460
157k
}
5461
5462
void PeerManagerImpl::ConsiderEviction(CNode& pto, Peer& peer, std::chrono::seconds time_in_seconds)
5463
384k
{
5464
384k
    AssertLockHeld(cs_main);
5465
5466
384k
    CNodeState &state = *State(pto.GetId());
5467
5468
384k
    if (!state.m_chain_sync.m_protect && pto.IsOutboundOrBlockRelayConn() && state.fSyncStarted) {
5469
        // This is an outbound peer subject to disconnection if they don't
5470
        // announce a block with as much work as the current tip within
5471
        // CHAIN_SYNC_TIMEOUT + HEADERS_RESPONSE_TIME seconds (note: if
5472
        // their chain has more work than ours, we should sync to it,
5473
        // unless it's invalid, in which case we should find that out and
5474
        // disconnect from them elsewhere).
5475
8.66k
        if (state.pindexBestKnownBlock != nullptr && state.pindexBestKnownBlock->nChainWork >= m_chainman.ActiveChain().Tip()->nChainWork) {
5476
            // The outbound peer has sent us a block with at least as much work as our current tip, so reset the timeout if it was set
5477
163
            if (state.m_chain_sync.m_timeout != 0s) {
5478
5
                state.m_chain_sync.m_timeout = 0s;
5479
5
                state.m_chain_sync.m_work_header = nullptr;
5480
5
                state.m_chain_sync.m_sent_getheaders = false;
5481
5
            }
5482
8.50k
        } else if (state.m_chain_sync.m_timeout == 0s || (state.m_chain_sync.m_work_header != nullptr && state.pindexBestKnownBlock != nullptr && state.pindexBestKnownBlock->nChainWork >= state.m_chain_sync.m_work_header->nChainWork)) {
5483
            // At this point we know that the outbound peer has either never sent us a block/header or they have, but its tip is behind ours
5484
            // AND
5485
            // we are noticing this for the first time (m_timeout is 0)
5486
            // OR we noticed this at some point within the last CHAIN_SYNC_TIMEOUT + HEADERS_RESPONSE_TIME seconds and set a timeout
5487
            // for them, they caught up to our tip at the time of setting the timer but not to our current one (we've also advanced).
5488
            // Either way, set a new timeout based on our current tip.
5489
127
            state.m_chain_sync.m_timeout = time_in_seconds + CHAIN_SYNC_TIMEOUT;
5490
127
            state.m_chain_sync.m_work_header = m_chainman.ActiveChain().Tip();
5491
127
            state.m_chain_sync.m_sent_getheaders = false;
5492
8.37k
        } else if (state.m_chain_sync.m_timeout > 0s && time_in_seconds > state.m_chain_sync.m_timeout) {
5493
            // No evidence yet that our peer has synced to a chain with work equal to that
5494
            // of our tip, when we first detected it was behind. Send a single getheaders
5495
            // message to give the peer a chance to update us.
5496
35
            if (state.m_chain_sync.m_sent_getheaders) {
5497
                // They've run out of time to catch up!
5498
6
                LogInfo("Outbound peer has old chain, best known block = %s, %s", state.pindexBestKnownBlock != nullptr ? state.pindexBestKnownBlock->GetBlockHash().ToString() : "<none>", pto.DisconnectMsg());
5499
6
                pto.fDisconnect = true;
5500
29
            } else {
5501
29
                assert(state.m_chain_sync.m_work_header);
5502
                // Here, we assume that the getheaders message goes out,
5503
                // because it'll either go out or be skipped because of a
5504
                // getheaders in-flight already, in which case the peer should
5505
                // still respond to us with a sufficiently high work chain tip.
5506
29
                MaybeSendGetHeaders(pto,
5507
29
                        GetLocator(state.m_chain_sync.m_work_header->pprev),
5508
29
                        peer);
5509
29
                LogDebug(BCLog::NET, "sending getheaders to outbound peer=%d to verify chain work (current best known block:%s, benchmark blockhash: %s)\n", pto.GetId(), state.pindexBestKnownBlock != nullptr ? state.pindexBestKnownBlock->GetBlockHash().ToString() : "<none>", state.m_chain_sync.m_work_header->GetBlockHash().ToString());
5510
29
                state.m_chain_sync.m_sent_getheaders = true;
5511
                // Bump the timeout to allow a response, which could clear the timeout
5512
                // (if the response shows the peer has synced), reset the timeout (if
5513
                // the peer syncs to the required work but not to our tip), or result
5514
                // in disconnect (if we advance to the timeout and pindexBestKnownBlock
5515
                // has not sufficiently progressed)
5516
29
                state.m_chain_sync.m_timeout = time_in_seconds + HEADERS_RESPONSE_TIME;
5517
29
            }
5518
35
        }
5519
8.66k
    }
5520
384k
}
5521
5522
void PeerManagerImpl::EvictExtraOutboundPeers(NodeClock::time_point now)
5523
124
{
5524
    // If we have any extra block-relay-only peers, disconnect the youngest unless
5525
    // it's given us a block -- in which case, compare with the second-youngest, and
5526
    // out of those two, disconnect the peer who least recently gave us a block.
5527
    // The youngest block-relay-only peer would be the extra peer we connected
5528
    // to temporarily in order to sync our tip; see net.cpp.
5529
    // Note that we use higher nodeid as a measure for most recent connection.
5530
124
    if (m_connman.GetExtraBlockRelayCount() > 0) {
5531
3
        std::pair<NodeId, std::chrono::seconds> youngest_peer{-1, 0}, next_youngest_peer{-1, 0};
5532
5533
9
        m_connman.ForEachNode([&](CNode* pnode) {
5534
9
            if (!pnode->IsBlockOnlyConn() || pnode->fDisconnect) return;
5535
9
            if (pnode->GetId() > youngest_peer.first) {
5536
9
                next_youngest_peer = youngest_peer;
5537
9
                youngest_peer.first = pnode->GetId();
5538
9
                youngest_peer.second = pnode->m_last_block_time;
5539
9
            }
5540
9
        });
5541
3
        NodeId to_disconnect = youngest_peer.first;
5542
3
        if (youngest_peer.second > next_youngest_peer.second) {
5543
            // Our newest block-relay-only peer gave us a block more recently;
5544
            // disconnect our second youngest.
5545
1
            to_disconnect = next_youngest_peer.first;
5546
1
        }
5547
3
        m_connman.ForNode(to_disconnect, [&](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
5548
3
            AssertLockHeld(::cs_main);
5549
            // Make sure we're not getting a block right now, and that
5550
            // we've been connected long enough for this eviction to happen
5551
            // at all.
5552
            // Note that we only request blocks from a peer if we learn of a
5553
            // valid headers chain with at least as much work as our tip.
5554
3
            CNodeState *node_state = State(pnode->GetId());
5555
3
            if (node_state == nullptr ||
5556
3
                (now - pnode->m_connected >= MINIMUM_CONNECT_TIME && node_state->vBlocksInFlight.empty())) {
5557
2
                pnode->fDisconnect = true;
5558
2
                LogDebug(BCLog::NET, "disconnecting extra block-relay-only peer=%d (last block received at time %d)\n",
5559
2
                         pnode->GetId(), count_seconds(pnode->m_last_block_time));
5560
2
                return true;
5561
2
            } else {
5562
1
                LogDebug(BCLog::NET, "keeping block-relay-only peer=%d chosen for eviction (connect time: %d, blocks_in_flight: %d)\n",
5563
1
                         pnode->GetId(), TicksSinceEpoch<std::chrono::seconds>(pnode->m_connected), node_state->vBlocksInFlight.size());
5564
1
            }
5565
1
            return false;
5566
3
        });
5567
3
    }
5568
5569
    // Check whether we have too many outbound-full-relay peers
5570
124
    if (m_connman.GetExtraFullOutboundCount() > 0) {
5571
        // If we have more outbound-full-relay peers than we target, disconnect one.
5572
        // Pick the outbound-full-relay peer that least recently announced
5573
        // us a new block, with ties broken by choosing the more recent
5574
        // connection (higher node id)
5575
        // Protect peers from eviction if we don't have another connection
5576
        // to their network, counting both outbound-full-relay and manual peers.
5577
4
        NodeId worst_peer = -1;
5578
4
        int64_t oldest_block_announcement = std::numeric_limits<int64_t>::max();
5579
5580
38
        m_connman.ForEachNode([&](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_connman.GetNodesMutex()) {
5581
38
            AssertLockHeld(::cs_main);
5582
5583
            // Only consider outbound-full-relay peers that are not already
5584
            // marked for disconnection
5585
38
            if (!pnode->IsFullOutboundConn() || pnode->fDisconnect) return;
5586
38
            CNodeState *state = State(pnode->GetId());
5587
38
            if (state == nullptr) return; // shouldn't be possible, but just in case
5588
            // Don't evict our protected peers
5589
38
            if (state->m_chain_sync.m_protect) return;
5590
            // If this is the only connection on a particular network that is
5591
            // OUTBOUND_FULL_RELAY or MANUAL, protect it.
5592
38
            if (!m_connman.MultipleManualOrFullOutboundConns(pnode->addr.GetNetwork())) return;
5593
37
            if (state->m_last_block_announcement < oldest_block_announcement || (state->m_last_block_announcement == oldest_block_announcement && pnode->GetId() > worst_peer)) {
5594
34
                worst_peer = pnode->GetId();
5595
34
                oldest_block_announcement = state->m_last_block_announcement;
5596
34
            }
5597
37
        });
5598
4
        if (worst_peer != -1) {
5599
4
            bool disconnected = m_connman.ForNode(worst_peer, [&](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
5600
4
                AssertLockHeld(::cs_main);
5601
5602
                // Only disconnect a peer that has been connected to us for
5603
                // some reasonable fraction of our check-frequency, to give
5604
                // it time for new information to have arrived.
5605
                // Also don't disconnect any peer we're trying to download a
5606
                // block from.
5607
4
                CNodeState &state = *State(pnode->GetId());
5608
4
                if (now - pnode->m_connected > MINIMUM_CONNECT_TIME && state.vBlocksInFlight.empty()) {
5609
4
                    LogDebug(BCLog::NET, "disconnecting extra outbound peer=%d (last block announcement received at time %d)\n", pnode->GetId(), oldest_block_announcement);
5610
4
                    pnode->fDisconnect = true;
5611
4
                    return true;
5612
4
                } else {
5613
0
                    LogDebug(BCLog::NET, "keeping outbound peer=%d chosen for eviction (connect time: %d, blocks_in_flight: %d)\n",
5614
0
                             pnode->GetId(), TicksSinceEpoch<std::chrono::seconds>(pnode->m_connected), state.vBlocksInFlight.size());
5615
0
                    return false;
5616
0
                }
5617
4
            });
5618
4
            if (disconnected) {
5619
                // If we disconnected an extra peer, that means we successfully
5620
                // connected to at least one peer after the last time we
5621
                // detected a stale tip. Don't try any more extra peers until
5622
                // we next detect a stale tip, to limit the load we put on the
5623
                // network from these extra connections.
5624
4
                m_connman.SetTryNewOutboundPeer(false);
5625
4
            }
5626
4
        }
5627
4
    }
5628
124
}
5629
5630
void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
5631
124
{
5632
124
    LOCK(cs_main);
5633
5634
124
    const auto current_time{NodeClock::now()};
5635
124
    auto now{GetTime<std::chrono::seconds>()};
5636
5637
124
    EvictExtraOutboundPeers(current_time);
5638
5639
124
    if (now > m_stale_tip_check_time) {
5640
        // Check whether our tip is stale, and if so, allow using an extra
5641
        // outbound peer
5642
58
        if (!m_chainman.m_blockman.LoadingBlocks() && m_connman.GetNetworkActive() && m_connman.GetUseAddrmanOutgoing() && TipMayBeStale()) {
5643
1
            LogInfo("Potential stale tip detected, will try using extra outbound peer (last tip update: %d seconds ago)\n",
5644
1
                      count_seconds(now - m_last_tip_update.load()));
5645
1
            m_connman.SetTryNewOutboundPeer(true);
5646
57
        } else if (m_connman.GetTryNewOutboundPeer()) {
5647
0
            m_connman.SetTryNewOutboundPeer(false);
5648
0
        }
5649
58
        m_stale_tip_check_time = now + STALE_CHECK_INTERVAL;
5650
58
    }
5651
5652
124
    if (!m_initial_sync_finished && CanDirectFetch()) {
5653
46
        m_connman.StartExtraBlockRelayPeers();
5654
46
        m_initial_sync_finished = true;
5655
46
    }
5656
124
}
5657
5658
void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer, NodeClock::time_point now)
5659
384k
{
5660
384k
    if (m_connman.ShouldRunInactivityChecks(node_to, now) &&
5661
384k
        peer.m_ping_nonce_sent &&
5662
384k
        now > peer.m_ping_start.load() + TIMEOUT_INTERVAL)
5663
1
    {
5664
        // The ping timeout is using mocktime. To disable the check during
5665
        // testing, increase -peertimeout.
5666
1
        LogDebug(BCLog::NET, "ping timeout: %fs, %s", Ticks<SecondsDouble>(now - peer.m_ping_start.load()), node_to.DisconnectMsg());
5667
1
        node_to.fDisconnect = true;
5668
1
        return;
5669
1
    }
5670
5671
384k
    bool pingSend = false;
5672
5673
384k
    if (peer.m_ping_queued) {
5674
        // RPC ping request by user
5675
20
        pingSend = true;
5676
20
    }
5677
5678
384k
    if (peer.m_ping_nonce_sent == 0 && now > peer.m_ping_start.load() + PING_INTERVAL) {
5679
        // Ping automatically sent as a latency probe & keepalive.
5680
2.51k
        pingSend = true;
5681
2.51k
    }
5682
5683
384k
    if (pingSend) {
5684
2.52k
        uint64_t nonce;
5685
2.52k
        do {
5686
2.52k
            nonce = FastRandomContext().rand64();
5687
2.52k
        } while (nonce == 0);
5688
2.52k
        peer.m_ping_queued = false;
5689
2.52k
        peer.m_ping_start = now;
5690
2.52k
        if (node_to.GetCommonVersion() > BIP0031_VERSION) {
5691
2.52k
            peer.m_ping_nonce_sent = nonce;
5692
2.52k
            MakeAndPushMessage(node_to, NetMsgType::PING, nonce);
5693
2.52k
        } else {
5694
            // Peer is too old to support ping message type with nonce, pong will never arrive.
5695
0
            peer.m_ping_nonce_sent = 0;
5696
0
            MakeAndPushMessage(node_to, NetMsgType::PING);
5697
0
        }
5698
2.52k
    }
5699
384k
}
5700
5701
void PeerManagerImpl::MaybeSendAddr(CNode& node, Peer& peer, std::chrono::microseconds current_time)
5702
384k
{
5703
    // Nothing to do for non-address-relay peers
5704
384k
    if (!peer.m_addr_relay_enabled) return;
5705
5706
381k
    LOCK(peer.m_addr_send_times_mutex);
5707
    // Periodically advertise our local address to the peer.
5708
381k
    if (fListen && !m_chainman.IsInitialBlockDownload() &&
5709
381k
        peer.m_next_local_addr_send < current_time) {
5710
        // If we've sent before, clear the bloom filter for the peer, so that our
5711
        // self-announcement will actually go out.
5712
        // This might be unnecessary if the bloom filter has already rolled
5713
        // over since our last self-announcement, but there is only a small
5714
        // bandwidth cost that we can incur by doing this (which happens
5715
        // once a day on average).
5716
1.65k
        if (peer.m_next_local_addr_send != 0us) {
5717
248
            peer.m_addr_known->reset();
5718
248
        }
5719
1.65k
        if (std::optional<CService> local_service = GetLocalAddrForPeer(node)) {
5720
25
            CAddress local_addr{*local_service, peer.m_our_services, Now<NodeSeconds>()};
5721
25
            if (peer.m_next_local_addr_send == 0us) {
5722
                // Send the initial self-announcement in its own message. This makes sure
5723
                // rate-limiting with limited start-tokens doesn't ignore it if the first
5724
                // message ends up containing multiple addresses.
5725
5
                if (IsAddrCompatible(peer, local_addr)) {
5726
5
                    std::vector<CAddress> self_announcement{local_addr};
5727
5
                    if (peer.m_wants_addrv2) {
5728
2
                        MakeAndPushMessage(node, NetMsgType::ADDRV2, CAddress::V2_NETWORK(self_announcement));
5729
3
                    } else {
5730
3
                        MakeAndPushMessage(node, NetMsgType::ADDR, CAddress::V1_NETWORK(self_announcement));
5731
3
                    }
5732
5
                }
5733
20
            } else {
5734
                // All later self-announcements are sent together with the other addresses.
5735
20
                PushAddress(peer, local_addr);
5736
20
            }
5737
25
        }
5738
1.65k
        peer.m_next_local_addr_send = current_time + m_rng.rand_exp_duration(AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
5739
1.65k
    }
5740
5741
    // We sent an `addr` message to this peer recently. Nothing more to do.
5742
381k
    if (current_time <= peer.m_next_addr_send) return;
5743
5744
2.91k
    peer.m_next_addr_send = current_time + m_rng.rand_exp_duration(AVG_ADDRESS_BROADCAST_INTERVAL);
5745
5746
2.91k
    if (!Assume(peer.m_addrs_to_send.size() <= MAX_ADDR_TO_SEND)) {
5747
        // Should be impossible since we always check size before adding to
5748
        // m_addrs_to_send. Recover by trimming the vector.
5749
0
        peer.m_addrs_to_send.resize(MAX_ADDR_TO_SEND);
5750
0
    }
5751
5752
    // Remove addr records that the peer already knows about, and add new
5753
    // addrs to the m_addr_known filter on the same pass.
5754
19.0k
    auto addr_already_known = [&peer](const CAddress& addr) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex) {
5755
19.0k
        bool ret = peer.m_addr_known->contains(addr.GetKey());
5756
19.0k
        if (!ret) peer.m_addr_known->insert(addr.GetKey());
5757
19.0k
        return ret;
5758
19.0k
    };
5759
2.91k
    peer.m_addrs_to_send.erase(std::remove_if(peer.m_addrs_to_send.begin(), peer.m_addrs_to_send.end(), addr_already_known),
5760
2.91k
                           peer.m_addrs_to_send.end());
5761
5762
    // No addr messages to send
5763
2.91k
    if (peer.m_addrs_to_send.empty()) return;
5764
5765
125
    if (peer.m_wants_addrv2) {
5766
12
        MakeAndPushMessage(node, NetMsgType::ADDRV2, CAddress::V2_NETWORK(peer.m_addrs_to_send));
5767
113
    } else {
5768
113
        MakeAndPushMessage(node, NetMsgType::ADDR, CAddress::V1_NETWORK(peer.m_addrs_to_send));
5769
113
    }
5770
125
    peer.m_addrs_to_send.clear();
5771
5772
    // we only send the big addr message once
5773
125
    if (peer.m_addrs_to_send.capacity() > 40) {
5774
21
        peer.m_addrs_to_send.shrink_to_fit();
5775
21
    }
5776
125
}
5777
5778
void PeerManagerImpl::MaybeSendSendHeaders(CNode& node, Peer& peer)
5779
384k
{
5780
    // Delay sending SENDHEADERS (BIP 130) until we're done with an
5781
    // initial-headers-sync with this peer. Receiving headers announcements for
5782
    // new blocks while trying to sync their headers chain is problematic,
5783
    // because of the state tracking done.
5784
384k
    if (!peer.m_sent_sendheaders && node.GetCommonVersion() >= SENDHEADERS_VERSION) {
5785
162k
        LOCK(cs_main);
5786
162k
        CNodeState &state = *State(node.GetId());
5787
162k
        if (state.pindexBestKnownBlock != nullptr &&
5788
162k
                state.pindexBestKnownBlock->nChainWork > m_chainman.MinimumChainWork()) {
5789
            // Tell our peer we prefer to receive headers rather than inv's
5790
            // We send this to non-NODE NETWORK peers as well, because even
5791
            // non-NODE NETWORK peers can announce blocks (such as pruning
5792
            // nodes)
5793
774
            MakeAndPushMessage(node, NetMsgType::SENDHEADERS);
5794
774
            peer.m_sent_sendheaders = true;
5795
774
        }
5796
162k
    }
5797
384k
}
5798
5799
void PeerManagerImpl::MaybeSendFeefilter(CNode& pto, Peer& peer, std::chrono::microseconds current_time)
5800
384k
{
5801
384k
    if (m_opts.ignore_incoming_txs) return;
5802
384k
    if (pto.GetCommonVersion() < FEEFILTER_VERSION) return;
5803
    // peers with the forcerelay permission should not filter txs to us
5804
384k
    if (pto.HasPermission(NetPermissionFlags::ForceRelay)) return;
5805
    // Don't send feefilter messages to outbound block-relay-only peers since they should never announce
5806
    // transactions to us, regardless of feefilter state.
5807
384k
    if (pto.IsBlockOnlyConn()) return;
5808
5809
382k
    CAmount currentFilter = m_mempool.GetMinFee().GetFeePerK();
5810
5811
382k
    if (m_chainman.IsInitialBlockDownload()) {
5812
        // Received tx-inv messages are discarded when the active
5813
        // chainstate is in IBD, so tell the peer to not send them.
5814
24.5k
        currentFilter = MAX_MONEY;
5815
358k
    } else {
5816
358k
        static const CAmount MAX_FILTER{m_fee_filter_rounder.round(MAX_MONEY)};
5817
358k
        if (peer.m_fee_filter_sent == MAX_FILTER) {
5818
            // Send the current filter if we sent MAX_FILTER previously
5819
            // and made it out of IBD.
5820
221
            peer.m_next_send_feefilter = 0us;
5821
221
        }
5822
358k
    }
5823
382k
    if (current_time > peer.m_next_send_feefilter) {
5824
2.87k
        CAmount filterToSend = m_fee_filter_rounder.round(currentFilter);
5825
        // We always have a fee filter of at least the min relay fee
5826
2.87k
        filterToSend = std::max(filterToSend, m_mempool.m_opts.min_relay_feerate.GetFeePerK());
5827
2.87k
        if (filterToSend != peer.m_fee_filter_sent) {
5828
1.74k
            MakeAndPushMessage(pto, NetMsgType::FEEFILTER, filterToSend);
5829
1.74k
            peer.m_fee_filter_sent = filterToSend;
5830
1.74k
        }
5831
2.87k
        peer.m_next_send_feefilter = current_time + m_rng.rand_exp_duration(AVG_FEEFILTER_BROADCAST_INTERVAL);
5832
2.87k
    }
5833
    // If the fee filter has changed substantially and it's still more than MAX_FEEFILTER_CHANGE_DELAY
5834
    // until scheduled broadcast, then move the broadcast to within MAX_FEEFILTER_CHANGE_DELAY.
5835
379k
    else if (current_time + MAX_FEEFILTER_CHANGE_DELAY < peer.m_next_send_feefilter &&
5836
379k
                (currentFilter < 3 * peer.m_fee_filter_sent / 4 || currentFilter > 4 * peer.m_fee_filter_sent / 3)) {
5837
1.72k
        peer.m_next_send_feefilter = current_time + m_rng.randrange<std::chrono::microseconds>(MAX_FEEFILTER_CHANGE_DELAY);
5838
1.72k
    }
5839
382k
}
5840
5841
bool PeerManagerImpl::RejectIncomingTxs(const CNode& peer) const
5842
28.3k
{
5843
    // block-relay-only peers may never send txs to us
5844
28.3k
    if (peer.IsBlockOnlyConn()) return true;
5845
28.3k
    if (peer.IsFeelerConn()) return true;
5846
    // In -blocksonly mode, peers need the 'relay' permission to send txs to us
5847
28.3k
    if (m_opts.ignore_incoming_txs && !peer.HasPermission(NetPermissionFlags::Relay)) return true;
5848
28.3k
    return false;
5849
28.3k
}
5850
5851
void PeerManagerImpl::ProcessPong(CNode& pfrom, Peer& peer, const NodeClock::time_point ping_end, DataStream& vRecv)
5852
2.50k
{
5853
2.50k
    uint64_t nonce = 0;
5854
2.50k
    const size_t nAvail{vRecv.size()};
5855
2.50k
    bool bPingFinished = false;
5856
2.50k
    std::string sProblem;
5857
5858
2.50k
    if (nAvail >= sizeof(nonce)) {
5859
2.50k
        vRecv >> nonce;
5860
5861
        // Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
5862
2.50k
        if (peer.m_ping_nonce_sent != 0) {
5863
2.50k
            if (nonce == peer.m_ping_nonce_sent) {
5864
                // Matching pong received, this ping is no longer outstanding
5865
2.50k
                bPingFinished = true;
5866
2.50k
                const auto ping_time = ping_end - peer.m_ping_start.load();
5867
2.50k
                if (ping_time.count() >= 0) {
5868
                    // Let connman know about this successful ping-pong
5869
2.50k
                    pfrom.PongReceived(ping_time);
5870
2.50k
                    if (pfrom.IsPrivateBroadcastConn()) {
5871
13
                        m_tx_for_private_broadcast.NodeConfirmedReception(pfrom.GetId());
5872
13
                        LogDebug(BCLog::PRIVBROADCAST, "Got a PONG (the transaction will probably reach the network), marking for disconnect, %s",
5873
13
                                 pfrom.LogPeer());
5874
13
                        pfrom.fDisconnect = true;
5875
13
                    }
5876
2.50k
                } else {
5877
                    // This should never happen
5878
0
                    sProblem = "Timing mishap";
5879
0
                }
5880
2.50k
            } else {
5881
                // Nonce mismatches are normal when pings are overlapping
5882
2
                sProblem = "Nonce mismatch";
5883
2
                if (nonce == 0) {
5884
                    // This is most likely a bug in another implementation somewhere; cancel this ping
5885
1
                    bPingFinished = true;
5886
1
                    sProblem = "Nonce zero";
5887
1
                }
5888
2
            }
5889
2.50k
        } else {
5890
1
            sProblem = "Unsolicited pong without ping";
5891
1
        }
5892
2.50k
    } else {
5893
        // This is most likely a bug in another implementation somewhere; cancel this ping
5894
1
        bPingFinished = true;
5895
1
        sProblem = "Short payload";
5896
1
    }
5897
5898
2.50k
    if (!(sProblem.empty())) {
5899
4
        LogDebug(BCLog::NET, "pong peer=%d: %s, %x expected, %x received, %u bytes\n",
5900
4
                 pfrom.GetId(),
5901
4
                 sProblem,
5902
4
                 peer.m_ping_nonce_sent,
5903
4
                 nonce,
5904
4
                 nAvail);
5905
4
    }
5906
2.50k
    if (bPingFinished) {
5907
2.50k
        peer.m_ping_nonce_sent = 0;
5908
2.50k
    }
5909
2.50k
}
5910
5911
bool PeerManagerImpl::SetupAddressRelay(const CNode& node, Peer& peer)
5912
1.62k
{
5913
    // We don't participate in addr relay with outbound block-relay-only
5914
    // connections to prevent providing adversaries with the additional
5915
    // information of addr traffic to infer the link.
5916
1.62k
    if (node.IsBlockOnlyConn()) return false;
5917
5918
    // We don't participate in addr relay with feeler connections because
5919
    // they are disconnected shortly after the handshake completes,
5920
    // before the node will receive the addr response.
5921
1.58k
    if (node.IsFeelerConn()) return false;
5922
5923
1.58k
    if (!peer.m_addr_relay_enabled.exchange(true)) {
5924
        // During version message processing (non-block-relay-only outbound peers)
5925
        // or on first addr-related message we have received (inbound peers), initialize
5926
        // m_addr_known.
5927
1.53k
        peer.m_addr_known = std::make_unique<CRollingBloomFilter>(5000, 0.001);
5928
1.53k
    }
5929
5930
1.58k
    return true;
5931
1.58k
}
5932
5933
void PeerManagerImpl::ProcessAddrs(std::string_view msg_type, CNode& pfrom, Peer& peer, std::vector<CAddress>&& vAddr, const std::atomic<bool>& interruptMsgProc)
5934
51
{
5935
51
    AssertLockNotHeld(m_peer_mutex);
5936
51
    AssertLockHeld(g_msgproc_mutex);
5937
5938
51
    if (!SetupAddressRelay(pfrom, peer)) {
5939
5
        LogDebug(BCLog::NET, "ignoring %s message from %s peer=%d\n", msg_type, pfrom.ConnectionTypeAsString(), pfrom.GetId());
5940
5
        return;
5941
5
    }
5942
5943
46
    if (vAddr.size() > MAX_ADDR_TO_SEND)
5944
2
    {
5945
2
        Misbehaving(peer, strprintf("%s message size = %u", msg_type, vAddr.size()));
5946
2
        return;
5947
2
    }
5948
5949
    // Store the new addresses
5950
44
    std::vector<CAddress> vAddrOk;
5951
5952
    // Update/increment addr rate limiting bucket.
5953
44
    const auto current_time{NodeClock::now()};
5954
44
    if (peer.m_addr_token_bucket < MAX_ADDR_PROCESSING_TOKEN_BUCKET) {
5955
        // Don't increment bucket if it's already full
5956
40
        const auto time_diff{current_time - peer.m_addr_token_timestamp};
5957
40
        const double increment{std::max(Ticks<SecondsDouble>(time_diff), 0.0) * MAX_ADDR_RATE_PER_SECOND};
5958
40
        peer.m_addr_token_bucket = std::min<double>(peer.m_addr_token_bucket + increment, MAX_ADDR_PROCESSING_TOKEN_BUCKET);
5959
40
    }
5960
44
    peer.m_addr_token_timestamp = current_time;
5961
5962
44
    const bool rate_limited = !pfrom.HasPermission(NetPermissionFlags::Addr);
5963
44
    uint64_t num_proc = 0;
5964
44
    uint64_t num_rate_limit = 0;
5965
44
    std::shuffle(vAddr.begin(), vAddr.end(), m_rng);
5966
44
    for (CAddress& addr : vAddr)
5967
3.27k
    {
5968
3.27k
        if (interruptMsgProc)
5969
0
            return;
5970
5971
        // Apply rate limiting.
5972
3.27k
        if (peer.m_addr_token_bucket < 1.0) {
5973
2.01k
            if (rate_limited) {
5974
1.99k
                ++num_rate_limit;
5975
1.99k
                continue;
5976
1.99k
            }
5977
2.01k
        } else {
5978
1.25k
            peer.m_addr_token_bucket -= 1.0;
5979
1.25k
        }
5980
        // We only bother storing full nodes, though this may include
5981
        // things which we would not make an outbound connection to, in
5982
        // part because we may make feeler connections to them.
5983
1.27k
        if (!MayHaveUsefulAddressDB(addr.nServices) && !HasAllDesirableServiceFlags(addr.nServices))
5984
0
            continue;
5985
5986
1.27k
        if (addr.nTime <= NodeSeconds{100000000s} || addr.nTime > current_time + 10min) {
5987
0
            addr.nTime = std::chrono::time_point_cast<std::chrono::seconds>(current_time - 5 * 24h);
5988
0
        }
5989
1.27k
        AddAddressKnown(peer, addr);
5990
1.27k
        if (m_banman && (m_banman->IsDiscouraged(addr) || m_banman->IsBanned(addr))) {
5991
            // Do not process banned/discouraged addresses beyond remembering we received them
5992
0
            continue;
5993
0
        }
5994
1.27k
        ++num_proc;
5995
1.27k
        const bool reachable{g_reachable_nets.Contains(addr)};
5996
1.27k
        if (addr.nTime > current_time - 10min && !peer.m_getaddr_sent && vAddr.size() <= 10 && addr.IsRoutable()) {
5997
            // Relay to a limited number of other nodes
5998
53
            RelayAddress(pfrom.GetId(), addr, reachable);
5999
53
        }
6000
        // Do not store addresses outside our network
6001
1.27k
        if (reachable) {
6002
1.27k
            vAddrOk.push_back(addr);
6003
1.27k
        }
6004
1.27k
    }
6005
44
    peer.m_addr_processed += num_proc;
6006
44
    peer.m_addr_rate_limited += num_rate_limit;
6007
44
    LogDebug(BCLog::NET, "Received addr: %u addresses (%u processed, %u rate-limited) from peer=%d\n",
6008
44
             vAddr.size(), num_proc, num_rate_limit, pfrom.GetId());
6009
6010
44
    m_addrman.Add(vAddrOk, pfrom.addr, /*time_penalty=*/2h);
6011
44
    if (vAddr.size() < 1000) peer.m_getaddr_sent = false;
6012
6013
    // AddrFetch: Require multiple addresses to avoid disconnecting on self-announcements
6014
44
    if (pfrom.IsAddrFetchConn() && vAddr.size() > 1) {
6015
1
        LogDebug(BCLog::NET, "addrfetch connection completed, %s", pfrom.DisconnectMsg());
6016
1
        pfrom.fDisconnect = true;
6017
1
    }
6018
44
}
6019
6020
bool PeerManagerImpl::SendMessages(CNode& node)
6021
389k
{
6022
389k
    AssertLockNotHeld(m_tx_download_mutex);
6023
389k
    AssertLockHeld(g_msgproc_mutex);
6024
6025
389k
    PeerRef maybe_peer{GetPeerRef(node.GetId())};
6026
389k
    if (!maybe_peer) return false;
6027
389k
    Peer& peer{*maybe_peer};
6028
389k
    const Consensus::Params& consensusParams = m_chainparams.GetConsensus();
6029
6030
    // We must call MaybeDiscourageAndDisconnect first, to ensure that we'll
6031
    // disconnect misbehaving peers even before the version handshake is complete.
6032
389k
    if (MaybeDiscourageAndDisconnect(node, peer)) return true;
6033
6034
    // Initiate version handshake for outbound connections
6035
389k
    if (!node.IsInboundConn() && !peer.m_outbound_version_message_sent) {
6036
608
        PushNodeVersion(node, peer);
6037
608
        peer.m_outbound_version_message_sent = true;
6038
608
    }
6039
6040
    // Don't send anything until the version handshake is complete
6041
389k
    if (!node.fSuccessfullyConnected || node.fDisconnect)
6042
5.16k
        return true;
6043
6044
384k
    const auto now{NodeClock::now()};
6045
384k
    const auto current_time{GetTime<std::chrono::microseconds>()};
6046
6047
    // The logic below does not apply to private broadcast peers, so skip it.
6048
    // Also in CConnman::PushMessage() we make sure that unwanted messages are
6049
    // not sent. This here is just an optimization.
6050
384k
    if (node.IsPrivateBroadcastConn()) {
6051
119
        if (node.m_connected + PRIVATE_BROADCAST_MAX_CONNECTION_LIFETIME < now) {
6052
0
            LogDebug(BCLog::PRIVBROADCAST, "Disconnecting: did not complete the transaction send within %d seconds, %s",
6053
0
                     count_seconds(PRIVATE_BROADCAST_MAX_CONNECTION_LIFETIME), node.LogPeer());
6054
0
            node.fDisconnect = true;
6055
0
        }
6056
119
        return true;
6057
119
    }
6058
6059
384k
    if (node.IsAddrFetchConn() && now - node.m_connected > 10 * AVG_ADDRESS_BROADCAST_INTERVAL) {
6060
1
        LogDebug(BCLog::NET, "addrfetch connection timeout, %s", node.DisconnectMsg());
6061
1
        node.fDisconnect = true;
6062
1
        return true;
6063
1
    }
6064
6065
384k
    MaybeSendPing(node, peer, now);
6066
6067
    // MaybeSendPing may have marked peer for disconnection
6068
384k
    if (node.fDisconnect) return true;
6069
6070
384k
    MaybeSendAddr(node, peer, current_time);
6071
6072
384k
    MaybeSendSendHeaders(node, peer);
6073
6074
384k
    ProcessInvBacklog(now);
6075
6076
384k
    {
6077
384k
        LOCK(cs_main);
6078
6079
384k
        CNodeState &state = *State(node.GetId());
6080
6081
        // Start block sync
6082
384k
        if (m_chainman.m_best_header == nullptr) {
6083
0
            m_chainman.m_best_header = m_chainman.ActiveChain().Tip();
6084
0
        }
6085
6086
        // Determine whether we might try initial headers sync or parallel
6087
        // block download from this peer -- this mostly affects behavior while
6088
        // in IBD (once out of IBD, we sync from all peers).
6089
384k
        bool sync_blocks_and_headers_from_peer = false;
6090
384k
        if (state.fPreferredDownload) {
6091
225k
            sync_blocks_and_headers_from_peer = true;
6092
225k
        } else if (CanServeBlocks(peer) && !node.IsAddrFetchConn()) {
6093
            // Typically this is an inbound peer. If we don't have any outbound
6094
            // peers, or if we aren't downloading any blocks from such peers,
6095
            // then allow block downloads from this peer, too.
6096
            // We prefer downloading blocks from outbound peers to avoid
6097
            // putting undue load on (say) some home user who is just making
6098
            // outbound connections to the network, but if our only source of
6099
            // the latest blocks is from an inbound peer, we have to be sure to
6100
            // eventually download it (and not just wait indefinitely for an
6101
            // outbound peer to have it).
6102
156k
            if (m_num_preferred_download_peers == 0 || mapBlocksInFlight.empty()) {
6103
151k
                sync_blocks_and_headers_from_peer = true;
6104
151k
            }
6105
156k
        }
6106
6107
384k
        if (!state.fSyncStarted && CanServeBlocks(peer) && !m_chainman.m_blockman.LoadingBlocks()) {
6108
            // Only actively request headers from a single peer, unless we're close to today.
6109
6.58k
            if ((nSyncStarted == 0 && sync_blocks_and_headers_from_peer) || m_chainman.m_best_header->Time() > NodeClock::now() - 24h) {
6110
1.51k
                const CBlockIndex* pindexStart = m_chainman.m_best_header;
6111
                /* If possible, start at the block preceding the currently
6112
                   best known header.  This ensures that we always get a
6113
                   non-empty list of headers back as long as the peer
6114
                   is up-to-date.  With a non-empty response, we can initialise
6115
                   the peer's known best block.  This wouldn't be possible
6116
                   if we requested starting at m_chainman.m_best_header and
6117
                   got back an empty response.  */
6118
1.51k
                if (pindexStart->pprev)
6119
1.28k
                    pindexStart = pindexStart->pprev;
6120
1.51k
                if (MaybeSendGetHeaders(node, GetLocator(pindexStart), peer)) {
6121
1.51k
                    LogDebug(BCLog::NET, "initial getheaders (%d) to peer=%d", pindexStart->nHeight, node.GetId());
6122
6123
1.51k
                    state.fSyncStarted = true;
6124
1.51k
                    peer.m_headers_sync_timeout = current_time + HEADERS_DOWNLOAD_TIMEOUT_BASE +
6125
1.51k
                        (
6126
                         // Convert HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER to microseconds before scaling
6127
                         // to maintain precision
6128
1.51k
                         std::chrono::microseconds{HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER} *
6129
1.51k
                         Ticks<std::chrono::seconds>(NodeClock::now() - m_chainman.m_best_header->Time()) / consensusParams.nPowTargetSpacing
6130
1.51k
                        );
6131
1.51k
                    nSyncStarted++;
6132
1.51k
                }
6133
1.51k
            }
6134
6.58k
        }
6135
6136
        //
6137
        // Try sending block announcements via headers
6138
        //
6139
384k
        {
6140
            // If we have no more than MAX_BLOCKS_TO_ANNOUNCE in our
6141
            // list of block hashes we're relaying, and our peer wants
6142
            // headers announcements, then find the first header
6143
            // not yet known to our peer but would connect, and send.
6144
            // If no header would connect, or if we have too many
6145
            // blocks, or if the peer doesn't want headers, just
6146
            // add all to the inv queue.
6147
384k
            LOCK(peer.m_block_inv_mutex);
6148
384k
            std::vector<CBlock> vHeaders;
6149
384k
            bool fRevertToInv = ((!peer.m_prefers_headers &&
6150
384k
                                 (!state.m_requested_hb_cmpctblocks || peer.m_blocks_for_headers_relay.size() > 1)) ||
6151
384k
                                 peer.m_blocks_for_headers_relay.size() > MAX_BLOCKS_TO_ANNOUNCE);
6152
384k
            const CBlockIndex *pBestIndex = nullptr; // last header queued for delivery
6153
384k
            ProcessBlockAvailability(node.GetId()); // ensure pindexBestKnownBlock is up-to-date
6154
6155
384k
            if (!fRevertToInv) {
6156
206k
                bool fFoundStartingHeader = false;
6157
                // Try to find first header that our peer doesn't have, and
6158
                // then send all headers past that one.  If we come across any
6159
                // headers that aren't on m_chainman.ActiveChain(), give up.
6160
206k
                for (const uint256& hash : peer.m_blocks_for_headers_relay) {
6161
46.7k
                    const CBlockIndex* pindex = m_chainman.m_blockman.LookupBlockIndex(hash);
6162
46.7k
                    assert(pindex);
6163
46.7k
                    if (m_chainman.ActiveChain()[pindex->nHeight] != pindex) {
6164
                        // Bail out if we reorged away from this block
6165
0
                        fRevertToInv = true;
6166
0
                        break;
6167
0
                    }
6168
46.7k
                    if (pBestIndex != nullptr && pindex->pprev != pBestIndex) {
6169
                        // This means that the list of blocks to announce don't
6170
                        // connect to each other.
6171
                        // This shouldn't really be possible to hit during
6172
                        // regular operation (because reorgs should take us to
6173
                        // a chain that has some block not on the prior chain,
6174
                        // which should be caught by the prior check), but one
6175
                        // way this could happen is by using invalidateblock /
6176
                        // reconsiderblock repeatedly on the tip, causing it to
6177
                        // be added multiple times to m_blocks_for_headers_relay.
6178
                        // Robustly deal with this rare situation by reverting
6179
                        // to an inv.
6180
0
                        fRevertToInv = true;
6181
0
                        break;
6182
0
                    }
6183
46.7k
                    pBestIndex = pindex;
6184
46.7k
                    if (fFoundStartingHeader) {
6185
                        // add this to the headers message
6186
865
                        vHeaders.emplace_back(pindex->GetBlockHeader());
6187
45.9k
                    } else if (PeerHasHeader(&state, pindex)) {
6188
39.4k
                        continue; // keep looking for the first new block
6189
39.4k
                    } else if (pindex->pprev == nullptr || PeerHasHeader(&state, pindex->pprev)) {
6190
                        // Peer doesn't have this header but they do have the prior one.
6191
                        // Start sending headers.
6192
6.38k
                        fFoundStartingHeader = true;
6193
6.38k
                        vHeaders.emplace_back(pindex->GetBlockHeader());
6194
6.38k
                    } else {
6195
                        // Peer doesn't have this header or the prior one -- nothing will
6196
                        // connect, so bail out.
6197
101
                        fRevertToInv = true;
6198
101
                        break;
6199
101
                    }
6200
46.7k
                }
6201
206k
            }
6202
384k
            if (!fRevertToInv && !vHeaders.empty()) {
6203
6.38k
                if (vHeaders.size() == 1 && state.m_requested_hb_cmpctblocks) {
6204
                    // We only send up to 1 block as header-and-ids, as otherwise
6205
                    // probably means we're doing an initial-ish-sync or they're slow
6206
2.30k
                    LogDebug(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", __func__,
6207
2.30k
                            vHeaders.front().GetHash().ToString(), node.GetId());
6208
6209
2.30k
                    std::optional<CSerializedNetMsg> cached_cmpctblock_msg;
6210
2.30k
                    {
6211
2.30k
                        LOCK(m_most_recent_block_mutex);
6212
2.30k
                        if (m_most_recent_block_hash == pBestIndex->GetBlockHash()) {
6213
69
                            cached_cmpctblock_msg = NetMsg::Make(NetMsgType::CMPCTBLOCK, *m_most_recent_compact_block);
6214
69
                        }
6215
2.30k
                    }
6216
2.30k
                    if (cached_cmpctblock_msg.has_value()) {
6217
69
                        PushMessage(node, std::move(cached_cmpctblock_msg.value()));
6218
2.23k
                    } else {
6219
2.23k
                        CBlock block;
6220
2.23k
                        const bool ret{m_chainman.m_blockman.ReadBlock(block, *pBestIndex)};
6221
2.23k
                        assert(ret);
6222
2.23k
                        CBlockHeaderAndShortTxIDs cmpctblock{block, m_rng.rand64()};
6223
2.23k
                        MakeAndPushMessage(node, NetMsgType::CMPCTBLOCK, cmpctblock);
6224
2.23k
                    }
6225
2.30k
                    state.pindexBestHeaderSent = pBestIndex;
6226
4.08k
                } else if (peer.m_prefers_headers) {
6227
4.08k
                    if (vHeaders.size() > 1) {
6228
675
                        LogDebug(BCLog::NET, "%s: %u headers, range (%s, %s), to peer=%d\n", __func__,
6229
675
                                vHeaders.size(),
6230
675
                                vHeaders.front().GetHash().ToString(),
6231
675
                                vHeaders.back().GetHash().ToString(), node.GetId());
6232
3.40k
                    } else {
6233
3.40k
                        LogDebug(BCLog::NET, "%s: sending header %s to peer=%d\n", __func__,
6234
3.40k
                                vHeaders.front().GetHash().ToString(), node.GetId());
6235
3.40k
                    }
6236
4.08k
                    MakeAndPushMessage(node, NetMsgType::HEADERS, TX_WITH_WITNESS(vHeaders));
6237
4.08k
                    state.pindexBestHeaderSent = pBestIndex;
6238
4.08k
                } else
6239
0
                    fRevertToInv = true;
6240
6.38k
            }
6241
384k
            if (fRevertToInv) {
6242
                // If falling back to using an inv, just try to inv the tip.
6243
                // The last entry in m_blocks_for_headers_relay was our tip at some point
6244
                // in the past.
6245
178k
                if (!peer.m_blocks_for_headers_relay.empty()) {
6246
26.4k
                    const uint256& hashToAnnounce = peer.m_blocks_for_headers_relay.back();
6247
26.4k
                    const CBlockIndex* pindex = m_chainman.m_blockman.LookupBlockIndex(hashToAnnounce);
6248
26.4k
                    assert(pindex);
6249
6250
                    // Warn if we're announcing a block that is not on the main chain.
6251
                    // This should be very rare and could be optimized out.
6252
                    // Just log for now.
6253
26.4k
                    if (m_chainman.ActiveChain()[pindex->nHeight] != pindex) {
6254
1
                        LogDebug(BCLog::NET, "Announcing block %s not on main chain (tip=%s)\n",
6255
1
                            hashToAnnounce.ToString(), m_chainman.ActiveChain().Tip()->GetBlockHash().ToString());
6256
1
                    }
6257
6258
                    // If the peer's chain has this block, don't inv it back.
6259
26.4k
                    if (!PeerHasHeader(&state, pindex)) {
6260
10.5k
                        peer.m_blocks_for_inv_relay.push_back(hashToAnnounce);
6261
10.5k
                        LogDebug(BCLog::NET, "%s: sending inv peer=%d hash=%s\n", __func__,
6262
10.5k
                            node.GetId(), hashToAnnounce.ToString());
6263
10.5k
                    }
6264
26.4k
                }
6265
178k
            }
6266
384k
            peer.m_blocks_for_headers_relay.clear();
6267
384k
        }
6268
6269
        //
6270
        // Message: inventory
6271
        //
6272
0
        std::vector<CInv> vInv;
6273
384k
        {
6274
384k
            LOCK(peer.m_block_inv_mutex);
6275
384k
            vInv.reserve(peer.m_blocks_for_inv_relay.size());
6276
6277
            // Add blocks
6278
384k
            for (const uint256& hash : peer.m_blocks_for_inv_relay) {
6279
10.5k
                vInv.emplace_back(MSG_BLOCK, hash);
6280
10.5k
                if (vInv.size() == MAX_INV_SZ) {
6281
0
                    MakeAndPushMessage(node, NetMsgType::INV, vInv);
6282
0
                    vInv.clear();
6283
0
                }
6284
10.5k
            }
6285
384k
            peer.m_blocks_for_inv_relay.clear();
6286
384k
        }
6287
6288
384k
        if (auto tx_relay = peer.GetTxRelay(); tx_relay != nullptr) {
6289
381k
                LOCK(tx_relay->m_tx_inventory_mutex);
6290
                // Check whether periodic sends should happen
6291
381k
                bool fSendTrickle = node.HasPermission(NetPermissionFlags::NoBan);
6292
381k
                if (tx_relay->m_next_inv_send_time < current_time) {
6293
5.87k
                    fSendTrickle = true;
6294
5.87k
                    if (node.IsInboundConn()) {
6295
3.25k
                        tx_relay->m_next_inv_send_time = NextInvToInbounds(current_time, INBOUND_INVENTORY_BROADCAST_INTERVAL, node.m_network_key);
6296
3.25k
                    } else {
6297
2.61k
                        tx_relay->m_next_inv_send_time = current_time + m_rng.rand_exp_duration(OUTBOUND_INVENTORY_BROADCAST_INTERVAL);
6298
2.61k
                    }
6299
5.87k
                }
6300
6301
                // Time to send but the peer has requested we not relay transactions.
6302
381k
                if (fSendTrickle) {
6303
122k
                    LOCK(tx_relay->m_bloom_filter_mutex);
6304
122k
                    if (!tx_relay->m_relay_txs) tx_relay->m_tx_inventory_to_send.clear();
6305
122k
                }
6306
6307
                // Respond to BIP35 mempool requests
6308
381k
                if (fSendTrickle && tx_relay->m_send_mempool) {
6309
1
                    auto vtxinfo = m_mempool.infoAll();
6310
6311
                    // Ensure we'll respond to GETDATA requests for anything we're about to announce
6312
1
                    tx_relay->m_last_inv_sequence = WITH_LOCK(m_mempool.cs, return m_mempool.GetSequence());
6313
6314
1
                    tx_relay->m_send_mempool = false;
6315
1
                    const CFeeRate filterrate{tx_relay->m_fee_filter_received.load()};
6316
6317
                    // we'll send everything in the mempool momentarily, so this is redundant
6318
1
                    tx_relay->m_tx_inventory_to_send.clear();
6319
6320
1
                    LOCK(tx_relay->m_bloom_filter_mutex);
6321
6322
2
                    for (const auto& txinfo : vtxinfo) {
6323
2
                        const Txid& txid{txinfo.tx->GetHash()};
6324
2
                        const Wtxid& wtxid{txinfo.tx->GetWitnessHash()};
6325
2
                        const auto inv = peer.m_wtxid_relay ?
6326
2
                                             CInv{MSG_WTX, wtxid.ToUint256()} :
6327
2
                                             CInv{MSG_TX, txid.ToUint256()};
6328
6329
                        // Don't send transactions that peers will not put into their mempool
6330
2
                        if (txinfo.fee < filterrate.GetFee(txinfo.vsize)) {
6331
0
                            continue;
6332
0
                        }
6333
2
                        if (tx_relay->m_bloom_filter) {
6334
2
                            if (!tx_relay->m_bloom_filter->IsRelevantAndUpdate(*txinfo.tx)) continue;
6335
2
                        }
6336
1
                        tx_relay->m_tx_inventory_known_filter.insert(inv.hash);
6337
1
                        vInv.push_back(inv);
6338
1
                        if (vInv.size() == MAX_INV_SZ) {
6339
0
                            MakeAndPushMessage(node, NetMsgType::INV, vInv);
6340
0
                            vInv.clear();
6341
0
                        }
6342
1
                    }
6343
1
                }
6344
6345
                // Determine transactions to relay
6346
381k
                if (fSendTrickle) {
6347
                    // Topologically and fee-rate sort the inventory we send for privacy and priority reasons.
6348
                    // (sorted from higher priority to lowest, skipping low fee)
6349
122k
                    const CFeeRate filterrate{tx_relay->m_fee_filter_received.load()};
6350
6351
122k
                    auto inv_tx = [&]() EXCLUSIVE_LOCKS_REQUIRED(tx_relay->m_tx_inventory_mutex) {
6352
122k
                        auto& invs = tx_relay->m_tx_inventory_to_send;
6353
122k
                        std::vector<CTransactionRef> res;
6354
6355
122k
                        if (invs.size() == 0) return res;
6356
6357
                        // if previous allocations were excessive, shrink to the current size
6358
14.3k
                        if (invs.capacity() > 2 * invs.size()) invs.shrink_to_fit();
6359
6360
14.3k
                        LOCK(m_mempool.cs);
6361
14.3k
                        auto txiters = m_mempool.ExtractBestByMiningScoreWithTopology(invs, invs.size());
6362
14.3k
                        res.reserve(txiters.size());
6363
38.2k
                        for (auto txiter : txiters) {
6364
38.2k
                            if (txiter->GetFee() < filterrate.GetFee(txiter->GetTxSize())) {
6365
30
                                continue; // higher feerate CPFP txs may follow, so just skip, don't stop
6366
30
                            }
6367
38.2k
                            res.push_back(txiter->GetSharedTx());
6368
38.2k
                        }
6369
                        // Ensure we'll respond to GETDATA requests for anything we're about to announce
6370
14.3k
                        tx_relay->m_last_inv_sequence = m_mempool.GetSequence();
6371
14.3k
                        return res;
6372
122k
                    }();
6373
6374
122k
                    LOCK(tx_relay->m_bloom_filter_mutex);
6375
122k
                    vInv.reserve(std::min<size_t>(MAX_INV_SZ, vInv.size() + inv_tx.size()));
6376
122k
                    for (auto& tx : inv_tx) {
6377
                        // `TxRelay::m_tx_inventory_known_filter` contains either txids or wtxids
6378
                        // depending on whether our peer supports wtxid-relay. Therefore, first
6379
                        // construct the inv and then use its hash for the filter check.
6380
38.2k
                        const auto inv = peer.m_wtxid_relay ?
6381
38.1k
                                             CInv{MSG_WTX, tx->GetWitnessHash().ToUint256()} :
6382
38.2k
                                             CInv{MSG_TX, tx->GetHash().ToUint256()};
6383
                        // Check if not in the filter already
6384
38.2k
                        if (tx_relay->m_tx_inventory_known_filter.contains(inv.hash)) {
6385
20.6k
                            continue;
6386
20.6k
                        }
6387
17.5k
                        if (tx_relay->m_bloom_filter && !tx_relay->m_bloom_filter->IsRelevantAndUpdate(*tx)) continue;
6388
                        // Send
6389
17.5k
                        vInv.push_back(inv);
6390
17.5k
                        if (vInv.size() == MAX_INV_SZ) {
6391
0
                            MakeAndPushMessage(node, NetMsgType::INV, vInv);
6392
0
                            vInv.clear();
6393
0
                        }
6394
17.5k
                        tx_relay->m_tx_inventory_known_filter.insert(inv.hash);
6395
17.5k
                    }
6396
122k
                }
6397
381k
        }
6398
384k
        if (!vInv.empty())
6399
19.5k
            MakeAndPushMessage(node, NetMsgType::INV, vInv);
6400
6401
        // Detect whether we're stalling
6402
384k
        auto stalling_timeout = m_block_stalling_timeout.load();
6403
384k
        if (state.m_stalling_since.count() && state.m_stalling_since < current_time - stalling_timeout) {
6404
            // Stalling only triggers when the block download window cannot move. During normal steady state,
6405
            // the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
6406
            // should only happen during initial block download.
6407
6
            LogInfo("Peer is stalling block download, %s", node.DisconnectMsg());
6408
6
            node.fDisconnect = true;
6409
            // Increase timeout for the next peer so that we don't disconnect multiple peers if our own
6410
            // bandwidth is insufficient.
6411
6
            const auto new_timeout = std::min(2 * stalling_timeout, BLOCK_STALLING_TIMEOUT_MAX);
6412
6
            if (stalling_timeout != new_timeout && m_block_stalling_timeout.compare_exchange_strong(stalling_timeout, new_timeout)) {
6413
6
                LogDebug(BCLog::NET, "Increased stalling timeout temporarily to %d seconds\n", count_seconds(new_timeout));
6414
6
            }
6415
6
            return true;
6416
6
        }
6417
        // In case there is a block that has been in flight from this peer for block_interval * (1 + 0.5 * N)
6418
        // (with N the number of peers from which we're downloading validated blocks), disconnect due to timeout.
6419
        // We compensate for other peers to prevent killing off peers due to our own downstream link
6420
        // being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
6421
        // to unreasonably increase our timeout.
6422
384k
        if (state.vBlocksInFlight.size() > 0) {
6423
40.7k
            QueuedBlock &queuedBlock = state.vBlocksInFlight.front();
6424
40.7k
            int nOtherPeersWithValidatedDownloads = m_peers_downloading_from - 1;
6425
40.7k
            if (current_time > state.m_downloading_since + std::chrono::seconds{consensusParams.nPowTargetSpacing} * (BLOCK_DOWNLOAD_TIMEOUT_BASE + BLOCK_DOWNLOAD_TIMEOUT_PER_PEER * nOtherPeersWithValidatedDownloads)) {
6426
0
                LogInfo("Timeout downloading block %s, %s", queuedBlock.pindex->GetBlockHash().ToString(), node.DisconnectMsg());
6427
0
                node.fDisconnect = true;
6428
0
                return true;
6429
0
            }
6430
40.7k
        }
6431
        // Check for headers sync timeouts
6432
384k
        if (state.fSyncStarted && peer.m_headers_sync_timeout < std::chrono::microseconds::max()) {
6433
            // Detect whether this is a stalling initial-headers-sync peer
6434
13.8k
            if (m_chainman.m_best_header->Time() <= NodeClock::now() - 24h) {
6435
12.4k
                if (current_time > peer.m_headers_sync_timeout && nSyncStarted == 1 && (m_num_preferred_download_peers - state.fPreferredDownload >= 1)) {
6436
                    // Disconnect a peer (without NetPermissionFlags::NoBan permission) if it is our only sync peer,
6437
                    // and we have others we could be using instead.
6438
                    // Note: If all our peers are inbound, then we won't
6439
                    // disconnect our sync peer for stalling; we have bigger
6440
                    // problems if we can't get any outbound peers.
6441
2
                    if (!node.HasPermission(NetPermissionFlags::NoBan)) {
6442
1
                        LogInfo("Timeout downloading headers, %s", node.DisconnectMsg());
6443
1
                        node.fDisconnect = true;
6444
1
                        return true;
6445
1
                    } else {
6446
1
                        LogInfo("Timeout downloading headers from noban peer, not %s", node.DisconnectMsg());
6447
                        // Reset the headers sync state so that we have a
6448
                        // chance to try downloading from a different peer.
6449
                        // Note: this will also result in at least one more
6450
                        // getheaders message to be sent to
6451
                        // this peer (eventually).
6452
1
                        state.fSyncStarted = false;
6453
1
                        nSyncStarted--;
6454
1
                        peer.m_headers_sync_timeout = 0us;
6455
1
                    }
6456
2
                }
6457
12.4k
            } else {
6458
                // After we've caught up once, reset the timeout so we can't trigger
6459
                // disconnect later.
6460
1.41k
                peer.m_headers_sync_timeout = std::chrono::microseconds::max();
6461
1.41k
            }
6462
13.8k
        }
6463
6464
        // Check that outbound peers have reasonable chains
6465
        // GetTime() is used by this anti-DoS logic so we can test this using mocktime
6466
384k
        ConsiderEviction(node, peer, GetTime<std::chrono::seconds>());
6467
6468
        //
6469
        // Message: getdata (blocks)
6470
        //
6471
384k
        std::vector<CInv> vGetData;
6472
384k
        if (CanServeBlocks(peer) && ((sync_blocks_and_headers_from_peer && !IsLimitedPeer(peer)) || !m_chainman.IsInitialBlockDownload()) && state.vBlocksInFlight.size() < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
6473
377k
            std::vector<const CBlockIndex*> vToDownload;
6474
377k
            NodeId staller = -1;
6475
378k
            auto get_inflight_budget = [&state]() {
6476
378k
                return std::max(0, MAX_BLOCKS_IN_TRANSIT_PER_PEER - static_cast<int>(state.vBlocksInFlight.size()));
6477
378k
            };
6478
6479
            // If there are multiple chainstates, download blocks for the
6480
            // current chainstate first, to prioritize getting to network tip
6481
            // before downloading historical blocks.
6482
377k
            FindNextBlocksToDownload(peer, get_inflight_budget(), vToDownload, staller);
6483
377k
            auto historical_blocks{m_chainman.GetHistoricalBlockRange()};
6484
377k
            if (historical_blocks && !IsLimitedPeer(peer)) {
6485
                // If the first needed historical block is not an ancestor of the last,
6486
                // we need to start requesting blocks from their last common ancestor.
6487
1.56k
                const CBlockIndex* from_tip = LastCommonAncestor(historical_blocks->first, historical_blocks->second);
6488
1.56k
                TryDownloadingHistoricalBlocks(
6489
1.56k
                    peer,
6490
1.56k
                    get_inflight_budget(),
6491
1.56k
                    vToDownload, from_tip, historical_blocks->second);
6492
1.56k
            }
6493
377k
            for (const CBlockIndex *pindex : vToDownload) {
6494
34.9k
                uint32_t nFetchFlags = GetFetchFlags(peer);
6495
34.9k
                vGetData.emplace_back(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash());
6496
34.9k
                BlockRequested(node.GetId(), *pindex);
6497
34.9k
                LogDebug(BCLog::NET, "Requesting block %s (%d) peer=%d\n", pindex->GetBlockHash().ToString(),
6498
34.9k
                    pindex->nHeight, node.GetId());
6499
34.9k
            }
6500
377k
            if (state.vBlocksInFlight.empty() && staller != -1) {
6501
168
                if (State(staller)->m_stalling_since == 0us) {
6502
6
                    State(staller)->m_stalling_since = current_time;
6503
6
                    LogDebug(BCLog::NET, "Stall started peer=%d\n", staller);
6504
6
                }
6505
168
            }
6506
377k
        }
6507
6508
        //
6509
        // Message: getdata (transactions)
6510
        //
6511
384k
        {
6512
384k
            LOCK(m_tx_download_mutex);
6513
384k
            for (const GenTxid& gtxid : m_txdownloadman.GetRequestsToSend(node.GetId(), current_time)) {
6514
22.5k
                vGetData.emplace_back(gtxid.IsWtxid() ? MSG_WTX : (MSG_TX | GetFetchFlags(peer)), gtxid.ToUint256());
6515
22.5k
                if (vGetData.size() >= MAX_GETDATA_SZ) {
6516
10
                    MakeAndPushMessage(node, NetMsgType::GETDATA, vGetData);
6517
10
                    vGetData.clear();
6518
10
                }
6519
22.5k
            }
6520
384k
        }
6521
6522
384k
        if (!vGetData.empty())
6523
40.0k
            MakeAndPushMessage(node, NetMsgType::GETDATA, vGetData);
6524
384k
    } // release cs_main
6525
0
    MaybeSendFeefilter(node, peer, current_time);
6526
384k
    return true;
6527
384k
}