LCOV - code coverage report
Current view: top level - src - net_processing.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 27.3 % 2627 716
Test Date: 2024-08-28 04:44:32 Functions: 42.9 % 154 66
Branches: 11.8 % 4803 565

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

Generated by: LCOV version 2.0-1