LCOV - code coverage report
Current view: top level - src - net_processing.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 2175 2367 91.9 %
Date: 2020-09-26 01:30:44 Functions: 128 129 99.2 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2020 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/validation.h>
      14             : #include <hash.h>
      15             : #include <index/blockfilterindex.h>
      16             : #include <merkleblock.h>
      17             : #include <netbase.h>
      18             : #include <netmessagemaker.h>
      19             : #include <policy/fees.h>
      20             : #include <policy/policy.h>
      21             : #include <primitives/block.h>
      22             : #include <primitives/transaction.h>
      23             : #include <random.h>
      24             : #include <reverse_iterator.h>
      25             : #include <scheduler.h>
      26             : #include <tinyformat.h>
      27             : #include <txmempool.h>
      28             : #include <util/check.h> // For NDEBUG compile time check
      29             : #include <util/strencodings.h>
      30             : #include <util/system.h>
      31             : #include <validation.h>
      32             : 
      33             : #include <memory>
      34             : #include <typeinfo>
      35             : 
      36             : /** Expiration time for orphan transactions in seconds */
      37             : static constexpr int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60;
      38             : /** Minimum time between orphan transactions expire time checks in seconds */
      39             : static constexpr int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60;
      40             : /** How long to cache transactions in mapRelay for normal relay */
      41             : static constexpr std::chrono::seconds RELAY_TX_CACHE_TIME = std::chrono::minutes{15};
      42             : /** How long a transaction has to be in the mempool before it can unconditionally be relayed (even when not in mapRelay). */
      43             : static constexpr std::chrono::seconds UNCONDITIONAL_RELAY_DELAY = std::chrono::minutes{2};
      44             : /** Headers download timeout expressed in microseconds
      45             :  *  Timeout = base + per_header * (expected number of headers) */
      46             : static constexpr int64_t HEADERS_DOWNLOAD_TIMEOUT_BASE = 15 * 60 * 1000000; // 15 minutes
      47             : static constexpr int64_t HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER = 1000; // 1ms/header
      48             : /** Protect at least this many outbound peers from disconnection due to slow/
      49             :  * behind headers chain.
      50             :  */
      51             : static constexpr int32_t MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT = 4;
      52             : /** Timeout for (unprotected) outbound peers to sync to our chainwork, in seconds */
      53             : static constexpr int64_t CHAIN_SYNC_TIMEOUT = 20 * 60; // 20 minutes
      54             : /** How frequently to check for stale tips, in seconds */
      55             : static constexpr int64_t STALE_CHECK_INTERVAL = 10 * 60; // 10 minutes
      56             : /** How frequently to check for extra outbound peers and disconnect, in seconds */
      57             : static constexpr int64_t EXTRA_PEER_CHECK_INTERVAL = 45;
      58             : /** Minimum time an outbound-peer-eviction candidate must be connected for, in order to evict, in seconds */
      59             : static constexpr int64_t MINIMUM_CONNECT_TIME = 30;
      60             : /** SHA256("main address relay")[0:8] */
      61             : static constexpr uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL;
      62             : /// Age after which a stale block will no longer be served if requested as
      63             : /// protection against fingerprinting. Set to one month, denominated in seconds.
      64             : static constexpr int STALE_RELAY_AGE_LIMIT = 30 * 24 * 60 * 60;
      65             : /// Age after which a block is considered historical for purposes of rate
      66             : /// limiting block relay. Set to one week, denominated in seconds.
      67             : static constexpr int HISTORICAL_BLOCK_AGE = 7 * 24 * 60 * 60;
      68             : /** Time between pings automatically sent out for latency probing and keepalive */
      69             : static constexpr std::chrono::minutes PING_INTERVAL{2};
      70             : /** The maximum number of entries in a locator */
      71             : static const unsigned int MAX_LOCATOR_SZ = 101;
      72             : /** The maximum number of entries in an 'inv' protocol message */
      73             : static const unsigned int MAX_INV_SZ = 50000;
      74             : /** Maximum number of in-flight transactions from a peer */
      75             : static constexpr int32_t MAX_PEER_TX_IN_FLIGHT = 100;
      76             : /** Maximum number of announced transactions from a peer */
      77             : static constexpr int32_t MAX_PEER_TX_ANNOUNCEMENTS = 2 * MAX_INV_SZ;
      78             : /** How many microseconds to delay requesting transactions via txids, if we have wtxid-relaying peers */
      79             : static constexpr std::chrono::microseconds TXID_RELAY_DELAY{std::chrono::seconds{2}};
      80             : /** How many microseconds to delay requesting transactions from inbound peers */
      81             : static constexpr std::chrono::microseconds INBOUND_PEER_TX_DELAY{std::chrono::seconds{2}};
      82             : /** How long to wait (in microseconds) before downloading a transaction from an additional peer */
      83             : static constexpr std::chrono::microseconds GETDATA_TX_INTERVAL{std::chrono::seconds{60}};
      84             : /** Maximum delay (in microseconds) for transaction requests to avoid biasing some peers over others. */
      85             : static constexpr std::chrono::microseconds MAX_GETDATA_RANDOM_DELAY{std::chrono::seconds{2}};
      86             : /** How long to wait (in microseconds) before expiring an in-flight getdata request to a peer */
      87             : static constexpr std::chrono::microseconds TX_EXPIRY_INTERVAL{GETDATA_TX_INTERVAL * 10};
      88             : static_assert(INBOUND_PEER_TX_DELAY >= MAX_GETDATA_RANDOM_DELAY,
      89             : "To preserve security, MAX_GETDATA_RANDOM_DELAY should not exceed INBOUND_PEER_DELAY");
      90             : /** Limit to avoid sending big packets. Not used in processing incoming GETDATA for compatibility */
      91             : static const unsigned int MAX_GETDATA_SZ = 1000;
      92             : /** Number of blocks that can be requested at any given time from a single peer. */
      93             : static const int MAX_BLOCKS_IN_TRANSIT_PER_PEER = 16;
      94             : /** Timeout in seconds during which a peer must stall block download progress before being disconnected. */
      95             : static const unsigned int BLOCK_STALLING_TIMEOUT = 2;
      96             : /** Number of headers sent in one getheaders result. We rely on the assumption that if a peer sends
      97             :  *  less than this number, we reached its tip. Changing this value is a protocol upgrade. */
      98             : static const unsigned int MAX_HEADERS_RESULTS = 2000;
      99             : /** Maximum depth of blocks we're willing to serve as compact blocks to peers
     100             :  *  when requested. For older blocks, a regular BLOCK response will be sent. */
     101             : static const int MAX_CMPCTBLOCK_DEPTH = 5;
     102             : /** Maximum depth of blocks we're willing to respond to GETBLOCKTXN requests for. */
     103             : static const int MAX_BLOCKTXN_DEPTH = 10;
     104             : /** Size of the "block download window": how far ahead of our current height do we fetch?
     105             :  *  Larger windows tolerate larger download speed differences between peer, but increase the potential
     106             :  *  degree of disordering of blocks on disk (which make reindexing and pruning harder). We'll probably
     107             :  *  want to make this a per-peer adaptive value at some point. */
     108             : static const unsigned int BLOCK_DOWNLOAD_WINDOW = 1024;
     109             : /** Block download timeout base, expressed in millionths of the block interval (i.e. 10 min) */
     110             : static const int64_t BLOCK_DOWNLOAD_TIMEOUT_BASE = 1000000;
     111             : /** Additional block download timeout per parallel downloading peer (i.e. 5 min) */
     112             : static const int64_t BLOCK_DOWNLOAD_TIMEOUT_PER_PEER = 500000;
     113             : /** Maximum number of headers to announce when relaying blocks with headers message.*/
     114             : static const unsigned int MAX_BLOCKS_TO_ANNOUNCE = 8;
     115             : /** Maximum number of unconnecting headers announcements before DoS score */
     116             : static const int MAX_UNCONNECTING_HEADERS = 10;
     117             : /** Minimum blocks required to signal NODE_NETWORK_LIMITED */
     118             : static const unsigned int NODE_NETWORK_LIMITED_MIN_BLOCKS = 288;
     119             : /** Average delay between local address broadcasts */
     120             : static constexpr std::chrono::hours AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL{24};
     121             : /** Average delay between peer address broadcasts */
     122             : static constexpr std::chrono::seconds AVG_ADDRESS_BROADCAST_INTERVAL{30};
     123             : /** Average delay between trickled inventory transmissions in seconds.
     124             :  *  Blocks and peers with noban permission bypass this, outbound peers get half this delay. */
     125             : static const unsigned int INVENTORY_BROADCAST_INTERVAL = 5;
     126             : /** Maximum rate of inventory items to send per second.
     127             :  *  Limits the impact of low-fee transaction floods. */
     128             : static constexpr unsigned int INVENTORY_BROADCAST_PER_SECOND = 7;
     129             : /** Maximum number of inventory items to send per transmission. */
     130             : static constexpr unsigned int INVENTORY_BROADCAST_MAX = INVENTORY_BROADCAST_PER_SECOND * INVENTORY_BROADCAST_INTERVAL;
     131             : /** The number of most recently announced transactions a peer can request. */
     132             : static constexpr unsigned int INVENTORY_MAX_RECENT_RELAY = 3500;
     133             : /** Verify that INVENTORY_MAX_RECENT_RELAY is enough to cache everything typically
     134             :  *  relayed before unconditional relay from the mempool kicks in. This is only a
     135             :  *  lower bound, and it should be larger to account for higher inv rate to outbound
     136             :  *  peers, and random variations in the broadcast mechanism. */
     137             : static_assert(INVENTORY_MAX_RECENT_RELAY >= INVENTORY_BROADCAST_PER_SECOND * UNCONDITIONAL_RELAY_DELAY / std::chrono::seconds{1}, "INVENTORY_RELAY_MAX too low");
     138             : /** Average delay between feefilter broadcasts in seconds. */
     139             : static constexpr unsigned int AVG_FEEFILTER_BROADCAST_INTERVAL = 10 * 60;
     140             : /** Maximum feefilter broadcast delay after significant change. */
     141             : static constexpr unsigned int MAX_FEEFILTER_CHANGE_DELAY = 5 * 60;
     142             : /** Maximum number of compact filters that may be requested with one getcfilters. See BIP 157. */
     143             : static constexpr uint32_t MAX_GETCFILTERS_SIZE = 1000;
     144             : /** Maximum number of cf hashes that may be requested with one getcfheaders. See BIP 157. */
     145             : static constexpr uint32_t MAX_GETCFHEADERS_SIZE = 2000;
     146             : /** the maximum percentage of addresses from our addrman to return in response to a getaddr message. */
     147             : static constexpr size_t MAX_PCT_ADDR_TO_SEND = 23;
     148             : 
     149        1248 : struct COrphanTx {
     150             :     // When modifying, adapt the copy of this definition in tests/DoS_tests.
     151             :     CTransactionRef tx;
     152             :     NodeId fromPeer;
     153             :     int64_t nTimeExpire;
     154             :     size_t list_pos;
     155             : };
     156         640 : RecursiveMutex g_cs_orphans;
     157         640 : std::map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY(g_cs_orphans);
     158         640 : std::map<uint256, std::map<uint256, COrphanTx>::iterator> g_orphans_by_wtxid GUARDED_BY(g_cs_orphans);
     159             : 
     160             : void EraseOrphansFor(NodeId peer);
     161             : 
     162             : // Internal stuff
     163             : namespace {
     164             :     /** Number of nodes with fSyncStarted. */
     165             :     int nSyncStarted GUARDED_BY(cs_main) = 0;
     166             : 
     167             :     /**
     168             :      * Sources of received blocks, saved to be able punish them when processing
     169             :      * happens afterwards.
     170             :      * Set mapBlockSource[hash].second to false if the node should not be
     171             :      * punished if the block is invalid.
     172             :      */
     173         640 :     std::map<uint256, std::pair<NodeId, bool>> mapBlockSource GUARDED_BY(cs_main);
     174             : 
     175             :     /**
     176             :      * Filter for transactions that were recently rejected by
     177             :      * AcceptToMemoryPool. These are not rerequested until the chain tip
     178             :      * changes, at which point the entire filter is reset.
     179             :      *
     180             :      * Without this filter we'd be re-requesting txs from each of our peers,
     181             :      * increasing bandwidth consumption considerably. For instance, with 100
     182             :      * peers, half of which relay a tx we don't accept, that might be a 50x
     183             :      * bandwidth increase. A flooding attacker attempting to roll-over the
     184             :      * filter using minimum-sized, 60byte, transactions might manage to send
     185             :      * 1000/sec if we have fast peers, so we pick 120,000 to give our peers a
     186             :      * two minute window to send invs to us.
     187             :      *
     188             :      * Decreasing the false positive rate is fairly cheap, so we pick one in a
     189             :      * million to make it highly unlikely for users to have issues with this
     190             :      * filter.
     191             :      *
     192             :      * We typically only add wtxids to this filter. For non-segwit
     193             :      * transactions, the txid == wtxid, so this only prevents us from
     194             :      * re-downloading non-segwit transactions when communicating with
     195             :      * non-wtxidrelay peers -- which is important for avoiding malleation
     196             :      * attacks that could otherwise interfere with transaction relay from
     197             :      * non-wtxidrelay peers. For communicating with wtxidrelay peers, having
     198             :      * the reject filter store wtxids is exactly what we want to avoid
     199             :      * redownload of a rejected transaction.
     200             :      *
     201             :      * In cases where we can tell that a segwit transaction will fail
     202             :      * validation no matter the witness, we may add the txid of such
     203             :      * transaction to the filter as well. This can be helpful when
     204             :      * communicating with txid-relay peers or if we were to otherwise fetch a
     205             :      * transaction via txid (eg in our orphan handling).
     206             :      *
     207             :      * Memory used: 1.3 MB
     208             :      */
     209         640 :     std::unique_ptr<CRollingBloomFilter> recentRejects GUARDED_BY(cs_main);
     210         640 :     uint256 hashRecentRejectsChainTip GUARDED_BY(cs_main);
     211             : 
     212             :     /*
     213             :      * Filter for transactions that have been recently confirmed.
     214             :      * We use this to avoid requesting transactions that have already been
     215             :      * confirnmed.
     216             :      */
     217         640 :     Mutex g_cs_recent_confirmed_transactions;
     218         640 :     std::unique_ptr<CRollingBloomFilter> g_recent_confirmed_transactions GUARDED_BY(g_cs_recent_confirmed_transactions);
     219             : 
     220             :     /** Blocks that are in flight, and that are in the queue to be downloaded. */
     221      133530 :     struct QueuedBlock {
     222             :         uint256 hash;
     223             :         const CBlockIndex* pindex;                               //!< Optional.
     224             :         bool fValidatedHeaders;                                  //!< Whether this block has validated headers at the time of request.
     225             :         std::unique_ptr<PartiallyDownloadedBlock> partialBlock;  //!< Optional, used for CMPCTBLOCK downloads
     226             :     };
     227         640 :     std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> > mapBlocksInFlight GUARDED_BY(cs_main);
     228             : 
     229             :     /** Stack of nodes which we have set to announce using compact blocks */
     230         640 :     std::list<NodeId> lNodesAnnouncingHeaderAndIDs GUARDED_BY(cs_main);
     231             : 
     232             :     /** Number of preferable block download peers. */
     233             :     int nPreferredDownload GUARDED_BY(cs_main) = 0;
     234             : 
     235             :     /** Number of peers from which we're downloading blocks. */
     236             :     int nPeersWithValidatedDownloads GUARDED_BY(cs_main) = 0;
     237             : 
     238             :     /** Number of peers with wtxid relay. */
     239             :     int g_wtxid_relay_peers GUARDED_BY(cs_main) = 0;
     240             : 
     241             :     /** Number of outbound peers with m_chain_sync.m_protect. */
     242             :     int g_outbound_peers_with_protect_from_disconnect GUARDED_BY(cs_main) = 0;
     243             : 
     244             :     /** When our tip was last updated. */
     245             :     std::atomic<int64_t> g_last_tip_update(0);
     246             : 
     247             :     /** Relay map (txid or wtxid -> CTransactionRef) */
     248             :     typedef std::map<uint256, CTransactionRef> MapRelay;
     249         640 :     MapRelay mapRelay GUARDED_BY(cs_main);
     250             :     /** Expiration-time ordered list of (expire time, relay map entry) pairs. */
     251         640 :     std::deque<std::pair<int64_t, MapRelay::iterator>> vRelayExpiration GUARDED_BY(cs_main);
     252             : 
     253             :     struct IteratorComparator
     254             :     {
     255             :         template<typename I>
     256         418 :         bool operator()(const I& a, const I& b) const
     257             :         {
     258         418 :             return &(*a) < &(*b);
     259             :         }
     260             :     };
     261         640 :     std::map<COutPoint, std::set<std::map<uint256, COrphanTx>::iterator, IteratorComparator>> mapOrphanTransactionsByPrev GUARDED_BY(g_cs_orphans);
     262             : 
     263         640 :     std::vector<std::map<uint256, COrphanTx>::iterator> g_orphan_list GUARDED_BY(g_cs_orphans); //! For random eviction
     264             : 
     265             :     static size_t vExtraTxnForCompactIt GUARDED_BY(g_cs_orphans) = 0;
     266         640 :     static std::vector<std::pair<uint256, CTransactionRef>> vExtraTxnForCompact GUARDED_BY(g_cs_orphans);
     267             : } // namespace
     268             : 
     269             : namespace {
     270             : /**
     271             :  * Maintain validation-specific state about nodes, protected by cs_main, instead
     272             :  * by CNode's own locks. This simplifies asynchronous operation, where
     273             :  * processing of incoming data is done after the ProcessMessage call returns,
     274             :  * and we're no longer holding the node's locks.
     275             :  */
     276        1452 : struct CNodeState {
     277             :     //! The peer's address
     278             :     const CService address;
     279             :     //! Whether we have a fully established connection.
     280             :     bool fCurrentlyConnected;
     281             :     //! The best known block we know this peer has announced.
     282             :     const CBlockIndex *pindexBestKnownBlock;
     283             :     //! The hash of the last unknown block this peer has announced.
     284             :     uint256 hashLastUnknownBlock;
     285             :     //! The last full block we both have.
     286             :     const CBlockIndex *pindexLastCommonBlock;
     287             :     //! The best header we have sent our peer.
     288             :     const CBlockIndex *pindexBestHeaderSent;
     289             :     //! Length of current-streak of unconnecting headers announcements
     290             :     int nUnconnectingHeaders;
     291             :     //! Whether we've started headers synchronization with this peer.
     292             :     bool fSyncStarted;
     293             :     //! When to potentially disconnect peer for stalling headers download
     294             :     int64_t nHeadersSyncTimeout;
     295             :     //! Since when we're stalling block download progress (in microseconds), or 0.
     296             :     int64_t nStallingSince;
     297             :     std::list<QueuedBlock> vBlocksInFlight;
     298             :     //! When the first entry in vBlocksInFlight started downloading. Don't care when vBlocksInFlight is empty.
     299             :     int64_t nDownloadingSince;
     300             :     int nBlocksInFlight;
     301             :     int nBlocksInFlightValidHeaders;
     302             :     //! Whether we consider this a preferred download peer.
     303             :     bool fPreferredDownload;
     304             :     //! Whether this peer wants invs or headers (when possible) for block announcements.
     305             :     bool fPreferHeaders;
     306             :     //! Whether this peer wants invs or cmpctblocks (when possible) for block announcements.
     307             :     bool fPreferHeaderAndIDs;
     308             :     /**
     309             :       * Whether this peer will send us cmpctblocks if we request them.
     310             :       * This is not used to gate request logic, as we really only care about fSupportsDesiredCmpctVersion,
     311             :       * but is used as a flag to "lock in" the version of compact blocks (fWantsCmpctWitness) we send.
     312             :       */
     313             :     bool fProvidesHeaderAndIDs;
     314             :     //! Whether this peer can give us witnesses
     315             :     bool fHaveWitness;
     316             :     //! Whether this peer wants witnesses in cmpctblocks/blocktxns
     317             :     bool fWantsCmpctWitness;
     318             :     /**
     319             :      * If we've announced NODE_WITNESS to this peer: whether the peer sends witnesses in cmpctblocks/blocktxns,
     320             :      * otherwise: whether this peer sends non-witnesses in cmpctblocks/blocktxns.
     321             :      */
     322             :     bool fSupportsDesiredCmpctVersion;
     323             : 
     324             :     /** State used to enforce CHAIN_SYNC_TIMEOUT
     325             :       * Only in effect for outbound, non-manual, full-relay connections, with
     326             :       * m_protect == false
     327             :       * Algorithm: if a peer's best known block has less work than our tip,
     328             :       * set a timeout CHAIN_SYNC_TIMEOUT seconds in the future:
     329             :       *   - If at timeout their best known block now has more work than our tip
     330             :       *     when the timeout was set, then either reset the timeout or clear it
     331             :       *     (after comparing against our current tip's work)
     332             :       *   - If at timeout their best known block still has less work than our
     333             :       *     tip did when the timeout was set, then send a getheaders message,
     334             :       *     and set a shorter timeout, HEADERS_RESPONSE_TIME seconds in future.
     335             :       *     If their best known block is still behind when that new timeout is
     336             :       *     reached, disconnect.
     337             :       */
     338             :     struct ChainSyncTimeoutState {
     339             :         //! A timeout used for checking whether our peer has sufficiently synced
     340             :         int64_t m_timeout;
     341             :         //! A header with the work we require on our peer's chain
     342             :         const CBlockIndex * m_work_header;
     343             :         //! After timeout is reached, set to true after sending getheaders
     344             :         bool m_sent_getheaders;
     345             :         //! Whether this peer is protected from disconnection due to a bad/slow chain
     346             :         bool m_protect;
     347             :     };
     348             : 
     349             :     ChainSyncTimeoutState m_chain_sync;
     350             : 
     351             :     //! Time of last new block announcement
     352             :     int64_t m_last_block_announcement;
     353             : 
     354             :     /*
     355             :      * State associated with transaction download.
     356             :      *
     357             :      * Tx download algorithm:
     358             :      *
     359             :      *   When inv comes in, queue up (process_time, txid) inside the peer's
     360             :      *   CNodeState (m_tx_process_time) as long as m_tx_announced for the peer
     361             :      *   isn't too big (MAX_PEER_TX_ANNOUNCEMENTS).
     362             :      *
     363             :      *   The process_time for a transaction is set to nNow for outbound peers,
     364             :      *   nNow + 2 seconds for inbound peers. This is the time at which we'll
     365             :      *   consider trying to request the transaction from the peer in
     366             :      *   SendMessages(). The delay for inbound peers is to allow outbound peers
     367             :      *   a chance to announce before we request from inbound peers, to prevent
     368             :      *   an adversary from using inbound connections to blind us to a
     369             :      *   transaction (InvBlock).
     370             :      *
     371             :      *   When we call SendMessages() for a given peer,
     372             :      *   we will loop over the transactions in m_tx_process_time, looking
     373             :      *   at the transactions whose process_time <= nNow. We'll request each
     374             :      *   such transaction that we don't have already and that hasn't been
     375             :      *   requested from another peer recently, up until we hit the
     376             :      *   MAX_PEER_TX_IN_FLIGHT limit for the peer. Then we'll update
     377             :      *   g_already_asked_for for each requested txid, storing the time of the
     378             :      *   GETDATA request. We use g_already_asked_for to coordinate transaction
     379             :      *   requests amongst our peers.
     380             :      *
     381             :      *   For transactions that we still need but we have already recently
     382             :      *   requested from some other peer, we'll reinsert (process_time, txid)
     383             :      *   back into the peer's m_tx_process_time at the point in the future at
     384             :      *   which the most recent GETDATA request would time out (ie
     385             :      *   GETDATA_TX_INTERVAL + the request time stored in g_already_asked_for).
     386             :      *   We add an additional delay for inbound peers, again to prefer
     387             :      *   attempting download from outbound peers first.
     388             :      *   We also add an extra small random delay up to 2 seconds
     389             :      *   to avoid biasing some peers over others. (e.g., due to fixed ordering
     390             :      *   of peer processing in ThreadMessageHandler).
     391             :      *
     392             :      *   When we receive a transaction from a peer, we remove the txid from the
     393             :      *   peer's m_tx_in_flight set and from their recently announced set
     394             :      *   (m_tx_announced).  We also clear g_already_asked_for for that entry, so
     395             :      *   that if somehow the transaction is not accepted but also not added to
     396             :      *   the reject filter, then we will eventually redownload from other
     397             :      *   peers.
     398             :      */
     399        2904 :     struct TxDownloadState {
     400             :         /* Track when to attempt download of announced transactions (process
     401             :          * time in micros -> txid)
     402             :          */
     403             :         std::multimap<std::chrono::microseconds, GenTxid> m_tx_process_time;
     404             : 
     405             :         //! Store all the transactions a peer has recently announced
     406             :         std::set<uint256> m_tx_announced;
     407             : 
     408             :         //! Store transactions which were requested by us, with timestamp
     409             :         std::map<uint256, std::chrono::microseconds> m_tx_in_flight;
     410             : 
     411             :         //! Periodically check for stuck getdata requests
     412         726 :         std::chrono::microseconds m_check_expiry_timer{0};
     413             :     };
     414             : 
     415             :     TxDownloadState m_tx_download;
     416             : 
     417             :     //! Whether this peer is an inbound connection
     418             :     bool m_is_inbound;
     419             : 
     420             :     //! Whether this peer is a manual connection
     421             :     bool m_is_manual_connection;
     422             : 
     423             :     //! A rolling bloom filter of all announced tx CInvs to this peer.
     424         726 :     CRollingBloomFilter m_recently_announced_invs = CRollingBloomFilter{INVENTORY_MAX_RECENT_RELAY, 0.000001};
     425             : 
     426             :     //! Whether this peer relays txs via wtxid
     427         726 :     bool m_wtxid_relay{false};
     428             : 
     429        1452 :     CNodeState(CAddress addrIn, bool is_inbound, bool is_manual)
     430        1452 :         : address(addrIn), m_is_inbound(is_inbound), m_is_manual_connection(is_manual)
     431         726 :     {
     432         726 :         fCurrentlyConnected = false;
     433         726 :         pindexBestKnownBlock = nullptr;
     434         726 :         hashLastUnknownBlock.SetNull();
     435         726 :         pindexLastCommonBlock = nullptr;
     436         726 :         pindexBestHeaderSent = nullptr;
     437         726 :         nUnconnectingHeaders = 0;
     438         726 :         fSyncStarted = false;
     439         726 :         nHeadersSyncTimeout = 0;
     440         726 :         nStallingSince = 0;
     441         726 :         nDownloadingSince = 0;
     442         726 :         nBlocksInFlight = 0;
     443         726 :         nBlocksInFlightValidHeaders = 0;
     444         726 :         fPreferredDownload = false;
     445         726 :         fPreferHeaders = false;
     446         726 :         fPreferHeaderAndIDs = false;
     447         726 :         fProvidesHeaderAndIDs = false;
     448         726 :         fHaveWitness = false;
     449         726 :         fWantsCmpctWitness = false;
     450         726 :         fSupportsDesiredCmpctVersion = false;
     451         726 :         m_chain_sync = { 0, nullptr, false, false };
     452         726 :         m_last_block_announcement = 0;
     453         726 :         m_recently_announced_invs.reset();
     454        1452 :     }
     455             : };
     456             : 
     457             : // Keeps track of the time (in microseconds) when transactions were requested last time
     458         640 : limitedmap<uint256, std::chrono::microseconds> g_already_asked_for GUARDED_BY(cs_main)(MAX_INV_SZ);
     459             : 
     460             : /** Map maintaining per-node state. */
     461         640 : static std::map<NodeId, CNodeState> mapNodeState GUARDED_BY(cs_main);
     462             : 
     463     2011103 : static CNodeState *State(NodeId pnode) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
     464     2011103 :     std::map<NodeId, CNodeState>::iterator it = mapNodeState.find(pnode);
     465     2011103 :     if (it == mapNodeState.end())
     466         289 :         return nullptr;
     467     2010814 :     return &it->second;
     468     2011103 : }
     469             : 
     470             : /**
     471             :  * Data structure for an individual peer. This struct is not protected by
     472             :  * cs_main since it does not contain validation-critical data.
     473             :  *
     474             :  * Memory is owned by shared pointers and this object is destructed when
     475             :  * the refcount drops to zero.
     476             :  *
     477             :  * TODO: move most members from CNodeState to this structure.
     478             :  * TODO: move remaining application-layer data members from CNode to this structure.
     479             :  */
     480        1452 : struct Peer {
     481             :     /** Same id as the CNode object for this peer */
     482             :     const NodeId m_id{0};
     483             : 
     484             :     /** Protects misbehavior data members */
     485             :     Mutex m_misbehavior_mutex;
     486             :     /** Accumulated misbehavior score for this peer */
     487         726 :     int m_misbehavior_score GUARDED_BY(m_misbehavior_mutex){0};
     488             :     /** Whether this peer should be disconnected and marked as discouraged (unless it has the noban permission). */
     489         726 :     bool m_should_discourage GUARDED_BY(m_misbehavior_mutex){false};
     490             : 
     491        1452 :     Peer(NodeId id) : m_id(id) {}
     492             : };
     493             : 
     494             : using PeerRef = std::shared_ptr<Peer>;
     495             : 
     496             : /**
     497             :  * Map of all Peer objects, keyed by peer id. This map is protected
     498             :  * by the global g_peer_mutex. Once a shared pointer reference is
     499             :  * taken, the lock may be released. Individual fields are protected by
     500             :  * their own locks.
     501             :  */
     502         640 : Mutex g_peer_mutex;
     503         640 : static std::map<NodeId, PeerRef> g_peer_map GUARDED_BY(g_peer_mutex);
     504             : 
     505             : /** Get a shared pointer to the Peer object.
     506             :  *  May return nullptr if the Peer object can't be found. */
     507      299240 : static PeerRef GetPeerRef(NodeId id)
     508             : {
     509      299240 :     LOCK(g_peer_mutex);
     510      299240 :     auto it = g_peer_map.find(id);
     511      299241 :     return it != g_peer_map.end() ? it->second : nullptr;
     512      299241 : }
     513             : 
     514         698 : static void UpdatePreferredDownload(const CNode& node, CNodeState* state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
     515             : {
     516         698 :     nPreferredDownload -= state->fPreferredDownload;
     517             : 
     518             :     // Whether this node should be marked as a preferred download node.
     519         698 :     state->fPreferredDownload = (!node.IsInboundConn() || node.HasPermission(PF_NOBAN)) && !node.IsAddrFetchConn() && !node.fClient;
     520             : 
     521         698 :     nPreferredDownload += state->fPreferredDownload;
     522         698 : }
     523             : 
     524         718 : static void PushNodeVersion(CNode& pnode, CConnman& connman, int64_t nTime)
     525             : {
     526             :     // Note that pnode->GetLocalServices() is a reflection of the local
     527             :     // services we were offering when the CNode object was created for this
     528             :     // peer.
     529         718 :     ServiceFlags nLocalNodeServices = pnode.GetLocalServices();
     530         718 :     uint64_t nonce = pnode.GetLocalNonce();
     531         718 :     int nNodeStartingHeight = pnode.GetMyStartingHeight();
     532         718 :     NodeId nodeid = pnode.GetId();
     533         718 :     CAddress addr = pnode.addr;
     534             : 
     535         718 :     CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService(), addr.nServices));
     536         718 :     CAddress addrMe = CAddress(CService(), nLocalNodeServices);
     537             : 
     538        1432 :     connman.PushMessage(&pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t)nLocalNodeServices, nTime, addrYou, addrMe,
     539         718 :             nonce, strSubVersion, nNodeStartingHeight, ::g_relay_txes && pnode.m_tx_relay != nullptr));
     540             : 
     541         718 :     if (fLogIPs) {
     542           2 :         LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), addrYou.ToString(), nodeid);
     543             :     } else {
     544         716 :         LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), nodeid);
     545             :     }
     546         718 : }
     547             : 
     548             : // Returns a bool indicating whether we requested this block.
     549             : // Also used if a block was /not/ received and timed out or started with another peer
     550       46335 : static bool MarkBlockAsReceived(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
     551       46335 :     std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
     552       46335 :     if (itInFlight != mapBlocksInFlight.end()) {
     553       22215 :         CNodeState *state = State(itInFlight->second.first);
     554       22215 :         assert(state != nullptr);
     555       22215 :         state->nBlocksInFlightValidHeaders -= itInFlight->second.second->fValidatedHeaders;
     556       22215 :         if (state->nBlocksInFlightValidHeaders == 0 && itInFlight->second.second->fValidatedHeaders) {
     557             :             // Last validated block on the queue was received.
     558       10892 :             nPeersWithValidatedDownloads--;
     559       10892 :         }
     560       22215 :         if (state->vBlocksInFlight.begin() == itInFlight->second.second) {
     561             :             // First block on the queue was received, update the start download time for the next one
     562       22157 :             state->nDownloadingSince = std::max(state->nDownloadingSince, GetTimeMicros());
     563       22157 :         }
     564       22215 :         state->vBlocksInFlight.erase(itInFlight->second.second);
     565       22215 :         state->nBlocksInFlight--;
     566       22215 :         state->nStallingSince = 0;
     567       22215 :         mapBlocksInFlight.erase(itInFlight);
     568             :         return true;
     569             :     }
     570       24120 :     return false;
     571       46335 : }
     572             : 
     573             : // returns false, still setting pit, if the block was already in flight from the same peer
     574             : // pit will only be valid as long as the same cs_main lock is being held
     575       22407 : static bool MarkBlockAsInFlight(CTxMemPool& mempool, NodeId nodeid, const uint256& hash, const CBlockIndex* pindex = nullptr, std::list<QueuedBlock>::iterator** pit = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
     576       22407 :     CNodeState *state = State(nodeid);
     577       22407 :     assert(state != nullptr);
     578             : 
     579             :     // Short-circuit most stuff in case it is from the same node
     580       22407 :     std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
     581       22407 :     if (itInFlight != mapBlocksInFlight.end() && itInFlight->second.first == nodeid) {
     582         152 :         if (pit) {
     583         152 :             *pit = &itInFlight->second.second;
     584         152 :         }
     585         152 :         return false;
     586             :     }
     587             : 
     588             :     // Make sure it's not listed somewhere already.
     589       22255 :     MarkBlockAsReceived(hash);
     590             : 
     591       32157 :     std::list<QueuedBlock>::iterator it = state->vBlocksInFlight.insert(state->vBlocksInFlight.end(),
     592       22255 :             {hash, pindex, pindex != nullptr, std::unique_ptr<PartiallyDownloadedBlock>(pit ? new PartiallyDownloadedBlock(&mempool) : nullptr)});
     593       22255 :     state->nBlocksInFlight++;
     594       22255 :     state->nBlocksInFlightValidHeaders += it->fValidatedHeaders;
     595       22255 :     if (state->nBlocksInFlight == 1) {
     596             :         // We're starting a block download (batch) from this peer.
     597       10904 :         state->nDownloadingSince = GetTimeMicros();
     598       10904 :     }
     599       22255 :     if (state->nBlocksInFlightValidHeaders == 1 && pindex != nullptr) {
     600       10904 :         nPeersWithValidatedDownloads++;
     601       10904 :     }
     602       22255 :     itInFlight = mapBlocksInFlight.insert(std::make_pair(hash, std::make_pair(nodeid, it))).first;
     603       22255 :     if (pit)
     604        9902 :         *pit = &itInFlight->second.second;
     605             :     return true;
     606       22407 : }
     607             : 
     608             : /** Check whether the last unknown block a peer advertised is not yet known. */
     609      635553 : static void ProcessBlockAvailability(NodeId nodeid) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
     610      635553 :     CNodeState *state = State(nodeid);
     611      635553 :     assert(state != nullptr);
     612             : 
     613      635553 :     if (!state->hashLastUnknownBlock.IsNull()) {
     614        4784 :         const CBlockIndex* pindex = LookupBlockIndex(state->hashLastUnknownBlock);
     615        4784 :         if (pindex && pindex->nChainWork > 0) {
     616          97 :             if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork) {
     617          97 :                 state->pindexBestKnownBlock = pindex;
     618          97 :             }
     619          97 :             state->hashLastUnknownBlock.SetNull();
     620          97 :         }
     621        4784 :     }
     622      635553 : }
     623             : 
     624             : /** Update tracking information about which blocks a peer is assumed to have. */
     625       18867 : static void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
     626       18867 :     CNodeState *state = State(nodeid);
     627       18867 :     assert(state != nullptr);
     628             : 
     629       18867 :     ProcessBlockAvailability(nodeid);
     630             : 
     631       18867 :     const CBlockIndex* pindex = LookupBlockIndex(hash);
     632       18867 :     if (pindex && pindex->nChainWork > 0) {
     633             :         // An actually better block was announced.
     634       17859 :         if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork) {
     635       17802 :             state->pindexBestKnownBlock = pindex;
     636       17802 :         }
     637             :     } else {
     638             :         // An unknown block was announced; just assume that the latest one is the best one.
     639        1008 :         state->hashLastUnknownBlock = hash;
     640             :     }
     641       18867 : }
     642             : 
     643             : /**
     644             :  * When a peer sends us a valid block, instruct it to announce blocks to us
     645             :  * using CMPCTBLOCK if possible by adding its nodeid to the end of
     646             :  * lNodesAnnouncingHeaderAndIDs, and keeping that list under a certain size by
     647             :  * removing the first element if necessary.
     648             :  */
     649       11988 : static void MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid, CConnman& connman) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
     650             : {
     651       11988 :     AssertLockHeld(cs_main);
     652       11988 :     CNodeState* nodestate = State(nodeid);
     653       11988 :     if (!nodestate || !nodestate->fSupportsDesiredCmpctVersion) {
     654             :         // Never ask from peers who can't provide witnesses.
     655        1765 :         return;
     656             :     }
     657       10223 :     if (nodestate->fProvidesHeaderAndIDs) {
     658       21271 :         for (std::list<NodeId>::iterator it = lNodesAnnouncingHeaderAndIDs.begin(); it != lNodesAnnouncingHeaderAndIDs.end(); it++) {
     659       11048 :             if (*it == nodeid) {
     660       10035 :                 lNodesAnnouncingHeaderAndIDs.erase(it);
     661       10035 :                 lNodesAnnouncingHeaderAndIDs.push_back(nodeid);
     662       10035 :                 return;
     663             :             }
     664             :         }
     665         376 :         connman.ForNode(nodeid, [&connman](CNode* pfrom){
     666         188 :             LockAssertion lock(::cs_main);
     667         188 :             uint64_t nCMPCTBLOCKVersion = (pfrom->GetLocalServices() & NODE_WITNESS) ? 2 : 1;
     668         188 :             if (lNodesAnnouncingHeaderAndIDs.size() >= 3) {
     669             :                 // As per BIP152, we only get 3 of our peers to announce
     670             :                 // blocks using compact encodings.
     671           2 :                 connman.ForNode(lNodesAnnouncingHeaderAndIDs.front(), [&connman, nCMPCTBLOCKVersion](CNode* pnodeStop){
     672           0 :                     connman.PushMessage(pnodeStop, CNetMsgMaker(pnodeStop->GetSendVersion()).Make(NetMsgType::SENDCMPCT, /*fAnnounceUsingCMPCTBLOCK=*/false, nCMPCTBLOCKVersion));
     673           0 :                     return true;
     674           0 :                 });
     675           2 :                 lNodesAnnouncingHeaderAndIDs.pop_front();
     676             :             }
     677         188 :             connman.PushMessage(pfrom, CNetMsgMaker(pfrom->GetSendVersion()).Make(NetMsgType::SENDCMPCT, /*fAnnounceUsingCMPCTBLOCK=*/true, nCMPCTBLOCKVersion));
     678         188 :             lNodesAnnouncingHeaderAndIDs.push_back(pfrom->GetId());
     679             :             return true;
     680         188 :         });
     681         188 :     }
     682       11988 : }
     683             : 
     684          92 : static bool TipMayBeStale(const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
     685             : {
     686          92 :     AssertLockHeld(cs_main);
     687          92 :     if (g_last_tip_update == 0) {
     688           3 :         g_last_tip_update = GetTime();
     689           3 :     }
     690          92 :     return g_last_tip_update < GetTime() - consensusParams.nPowTargetSpacing * 3 && mapBlocksInFlight.empty();
     691             : }
     692             : 
     693       16885 : static bool CanDirectFetch(const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
     694             : {
     695       16885 :     return ::ChainActive().Tip()->GetBlockTime() > GetAdjustedTime() - consensusParams.nPowTargetSpacing * 20;
     696             : }
     697             : 
     698       78686 : static bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
     699             : {
     700       78686 :     if (state->pindexBestKnownBlock && pindex == state->pindexBestKnownBlock->GetAncestor(pindex->nHeight))
     701       22492 :         return true;
     702       56194 :     if (state->pindexBestHeaderSent && pindex == state->pindexBestHeaderSent->GetAncestor(pindex->nHeight))
     703       26105 :         return true;
     704       30089 :     return false;
     705       78686 : }
     706             : 
     707             : /** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
     708             :  *  at most count entries. */
     709      282211 : static void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
     710             : {
     711      282211 :     if (count == 0)
     712             :         return;
     713             : 
     714      282211 :     vBlocks.reserve(vBlocks.size() + count);
     715      282211 :     CNodeState *state = State(nodeid);
     716      282211 :     assert(state != nullptr);
     717             : 
     718             :     // Make sure pindexBestKnownBlock is up to date, we'll need it.
     719      282211 :     ProcessBlockAvailability(nodeid);
     720             : 
     721      282211 :     if (state->pindexBestKnownBlock == nullptr || state->pindexBestKnownBlock->nChainWork < ::ChainActive().Tip()->nChainWork || state->pindexBestKnownBlock->nChainWork < nMinimumChainWork) {
     722             :         // This peer has nothing interesting.
     723      151720 :         return;
     724             :     }
     725             : 
     726      130491 :     if (state->pindexLastCommonBlock == nullptr) {
     727             :         // Bootstrap quickly by guessing a parent of our best tip is the forking point.
     728             :         // Guessing wrong in either direction is not a problem.
     729         441 :         state->pindexLastCommonBlock = ::ChainActive()[std::min(state->pindexBestKnownBlock->nHeight, ::ChainActive().Height())];
     730         441 :     }
     731             : 
     732             :     // If the peer reorganized, our previous pindexLastCommonBlock may not be an ancestor
     733             :     // of its current tip anymore. Go back enough to fix that.
     734      130491 :     state->pindexLastCommonBlock = LastCommonAncestor(state->pindexLastCommonBlock, state->pindexBestKnownBlock);
     735      130491 :     if (state->pindexLastCommonBlock == state->pindexBestKnownBlock)
     736       99278 :         return;
     737             : 
     738       31213 :     std::vector<const CBlockIndex*> vToFetch;
     739       31213 :     const CBlockIndex *pindexWalk = state->pindexLastCommonBlock;
     740             :     // Never fetch further than the best block we know the peer has, or more than BLOCK_DOWNLOAD_WINDOW + 1 beyond the last
     741             :     // linked block we have in common with this peer. The +1 is so we can detect stalling, namely if we would be able to
     742             :     // download that next block if the window were 1 larger.
     743       31213 :     int nWindowEnd = state->pindexLastCommonBlock->nHeight + BLOCK_DOWNLOAD_WINDOW;
     744       31213 :     int nMaxHeight = std::min<int>(state->pindexBestKnownBlock->nHeight, nWindowEnd + 1);
     745      557831 :     NodeId waitingfor = -1;
     746       52639 :     while (pindexWalk->nHeight < nMaxHeight) {
     747             :         // Read up to 128 (or more, if more blocks than that are needed) successors of pindexWalk (towards
     748             :         // pindexBestKnownBlock) into vToFetch. We fetch 128, because CBlockIndex::GetAncestor may be as expensive
     749             :         // as iterating over ~100 CBlockIndex* entries anyway.
     750       31226 :         int nToFetch = std::min(nMaxHeight - pindexWalk->nHeight, std::max<int>(count - vBlocks.size(), 128));
     751       31226 :         vToFetch.resize(nToFetch);
     752       31226 :         pindexWalk = state->pindexBestKnownBlock->GetAncestor(pindexWalk->nHeight + nToFetch);
     753       31226 :         vToFetch[nToFetch - 1] = pindexWalk;
     754     1067665 :         for (unsigned int i = nToFetch - 1; i > 0; i--) {
     755     1036439 :             vToFetch[i - 1] = vToFetch[i]->pprev;
     756             :         }
     757             : 
     758             :         // Iterate over those blocks in vToFetch (in forward direction), adding the ones that
     759             :         // are not yet downloaded and not in flight to vBlocks. In the meantime, update
     760             :         // pindexLastCommonBlock as long as all ancestors are already downloaded, or if it's
     761             :         // already part of our chain (and therefore don't need it even if pruned).
     762      262396 :         for (const CBlockIndex* pindex : vToFetch) {
     763      231170 :             if (!pindex->IsValid(BLOCK_VALID_TREE)) {
     764             :                 // We consider the chain that this peer is on invalid.
     765         246 :                 return;
     766             :             }
     767      230924 :             if (!State(nodeid)->fHaveWitness && IsWitnessEnabled(pindex->pprev, consensusParams)) {
     768             :                 // We wouldn't download this block or its descendants from this peer.
     769         748 :                 return;
     770             :             }
     771      230176 :             if (pindex->nStatus & BLOCK_HAVE_DATA || ::ChainActive().Contains(pindex)) {
     772       35675 :                 if (pindex->HaveTxsDownloaded())
     773       30218 :                     state->pindexLastCommonBlock = pindex;
     774      194501 :             } else if (mapBlocksInFlight.count(pindex->GetBlockHash()) == 0) {
     775             :                 // The block is not already downloaded, and not yet in flight.
     776        9868 :                 if (pindex->nHeight > nWindowEnd) {
     777             :                     // We reached the end of the window.
     778           0 :                     if (vBlocks.size() == 0 && waitingfor != nodeid) {
     779             :                         // We aren't able to fetch anything, but we would be if the download window was one larger.
     780           0 :                         nodeStaller = waitingfor;
     781           0 :                     }
     782           0 :                     return;
     783             :                 }
     784        9868 :                 vBlocks.push_back(pindex);
     785        9868 :                 if (vBlocks.size() == count) {
     786        8806 :                     return;
     787             :                 }
     788      184633 :             } else if (waitingfor == -1) {
     789             :                 // This is the first already-in-flight block.
     790       17786 :                 waitingfor = mapBlocksInFlight[pindex->GetBlockHash()].first;
     791       17786 :             }
     792      231170 :         }
     793       21426 :     }
     794      313424 : }
     795             : 
     796       19340 : void EraseTxRequest(const GenTxid& gtxid) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
     797             : {
     798       19340 :     g_already_asked_for.erase(gtxid.GetHash());
     799       19340 : }
     800             : 
     801       20406 : std::chrono::microseconds GetTxRequestTime(const GenTxid& gtxid) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
     802             : {
     803       20406 :     auto it = g_already_asked_for.find(gtxid.GetHash());
     804       20406 :     if (it != g_already_asked_for.end()) {
     805        1232 :         return it->second;
     806             :     }
     807       19174 :     return {};
     808       20406 : }
     809             : 
     810        9534 : void UpdateTxRequestTime(const GenTxid& gtxid, std::chrono::microseconds request_time) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
     811             : {
     812        9534 :     auto it = g_already_asked_for.find(gtxid.GetHash());
     813        9534 :     if (it == g_already_asked_for.end()) {
     814        9525 :         g_already_asked_for.insert(std::make_pair(gtxid.GetHash(), request_time));
     815        9525 :     } else {
     816           9 :         g_already_asked_for.update(it, request_time);
     817             :     }
     818        9534 : }
     819             : 
     820       10827 : std::chrono::microseconds CalculateTxGetDataTime(const GenTxid& gtxid, std::chrono::microseconds current_time, bool use_inbound_delay, bool use_txid_delay) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
     821             : {
     822             :     std::chrono::microseconds process_time;
     823       10827 :     const auto last_request_time = GetTxRequestTime(gtxid);
     824             :     // First time requesting this tx
     825       10827 :     if (last_request_time.count() == 0) {
     826        9649 :         process_time = current_time;
     827        9649 :     } else {
     828             :         // Randomize the delay to avoid biasing some peers over others (such as due to
     829             :         // fixed ordering of peer processing in ThreadMessageHandler)
     830        1178 :         process_time = last_request_time + GETDATA_TX_INTERVAL + GetRandMicros(MAX_GETDATA_RANDOM_DELAY);
     831             :     }
     832             : 
     833             :     // We delay processing announcements from inbound peers
     834       10827 :     if (use_inbound_delay) process_time += INBOUND_PEER_TX_DELAY;
     835             : 
     836             :     // We delay processing announcements from peers that use txid-relay (instead of wtxid)
     837       10827 :     if (use_txid_delay) process_time += TXID_RELAY_DELAY;
     838             : 
     839             :     return process_time;
     840       10827 : }
     841             : 
     842       10782 : void RequestTx(CNodeState* state, const GenTxid& gtxid, std::chrono::microseconds current_time) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
     843             : {
     844       10782 :     CNodeState::TxDownloadState& peer_download_state = state->m_tx_download;
     845       21564 :     if (peer_download_state.m_tx_announced.size() >= MAX_PEER_TX_ANNOUNCEMENTS ||
     846       10782 :             peer_download_state.m_tx_process_time.size() >= MAX_PEER_TX_ANNOUNCEMENTS ||
     847       10782 :             peer_download_state.m_tx_announced.count(gtxid.GetHash())) {
     848             :         // Too many queued announcements from this peer, or we already have
     849             :         // this announcement
     850           0 :         return;
     851             :     }
     852       10782 :     peer_download_state.m_tx_announced.insert(gtxid.GetHash());
     853             : 
     854             :     // Calculate the time to try requesting this transaction. Use
     855             :     // fPreferredDownload as a proxy for outbound peers.
     856       10782 :     const auto process_time = CalculateTxGetDataTime(gtxid, current_time, !state->fPreferredDownload, !state->m_wtxid_relay && g_wtxid_relay_peers > 0);
     857             : 
     858       10782 :     peer_download_state.m_tx_process_time.emplace(process_time, gtxid);
     859       10782 : }
     860             : 
     861             : } // namespace
     862             : 
     863             : // This function is used for testing the stale tip eviction logic, see
     864             : // denialofservice_tests.cpp
     865           1 : void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds)
     866             : {
     867           1 :     LOCK(cs_main);
     868           1 :     CNodeState *state = State(node);
     869           1 :     if (state) state->m_last_block_announcement = time_in_seconds;
     870           1 : }
     871             : 
     872         726 : void PeerManager::InitializeNode(CNode *pnode) {
     873         726 :     CAddress addr = pnode->addr;
     874         726 :     std::string addrName = pnode->GetAddrName();
     875         726 :     NodeId nodeid = pnode->GetId();
     876             :     {
     877         726 :         LOCK(cs_main);
     878         726 :         mapNodeState.emplace_hint(mapNodeState.end(), std::piecewise_construct, std::forward_as_tuple(nodeid), std::forward_as_tuple(addr, pnode->IsInboundConn(), pnode->IsManualConn()));
     879         726 :     }
     880             :     {
     881         726 :         PeerRef peer = std::make_shared<Peer>(nodeid);
     882         726 :         LOCK(g_peer_mutex);
     883         726 :         g_peer_map.emplace_hint(g_peer_map.end(), nodeid, std::move(peer));
     884         726 :     }
     885         726 :     if (!pnode->IsInboundConn()) {
     886         258 :         PushNodeVersion(*pnode, m_connman, GetTime());
     887             :     }
     888         726 : }
     889             : 
     890           3 : void PeerManager::ReattemptInitialBroadcast(CScheduler& scheduler) const
     891             : {
     892           3 :     std::map<uint256, uint256> unbroadcast_txids = m_mempool.GetUnbroadcastTxs();
     893             : 
     894           6 :     for (const auto& elem : unbroadcast_txids) {
     895             :         // Sanity check: all unbroadcast txns should exist in the mempool
     896           3 :         if (m_mempool.exists(elem.first)) {
     897           3 :             LOCK(cs_main);
     898           3 :             RelayTransaction(elem.first, elem.second, m_connman);
     899           3 :         } else {
     900           0 :             m_mempool.RemoveUnbroadcastTx(elem.first, true);
     901             :         }
     902           0 :     }
     903             : 
     904             :     // Schedule next run for 10-15 minutes in the future.
     905             :     // We add randomness on every cycle to avoid the possibility of P2P fingerprinting.
     906           3 :     const std::chrono::milliseconds delta = std::chrono::minutes{10} + GetRandMillis(std::chrono::minutes{5});
     907           4 :     scheduler.scheduleFromNow([&] { ReattemptInitialBroadcast(scheduler); }, delta);
     908           3 : }
     909             : 
     910         726 : void PeerManager::FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTime) {
     911         726 :     fUpdateConnectionTime = false;
     912         726 :     LOCK(cs_main);
     913             :     int misbehavior{0};
     914             :     {
     915         726 :         PeerRef peer = GetPeerRef(nodeid);
     916         726 :         assert(peer != nullptr);
     917        1452 :         misbehavior = WITH_LOCK(peer->m_misbehavior_mutex, return peer->m_misbehavior_score);
     918         726 :         LOCK(g_peer_mutex);
     919         726 :         g_peer_map.erase(nodeid);
     920         726 :     }
     921         726 :     CNodeState *state = State(nodeid);
     922         726 :     assert(state != nullptr);
     923             : 
     924         726 :     if (state->fSyncStarted)
     925         689 :         nSyncStarted--;
     926             : 
     927         726 :     if (misbehavior == 0 && state->fCurrentlyConnected) {
     928         237 :         fUpdateConnectionTime = true;
     929         237 :     }
     930             : 
     931         766 :     for (const QueuedBlock& entry : state->vBlocksInFlight) {
     932          40 :         mapBlocksInFlight.erase(entry.hash);
     933             :     }
     934         726 :     EraseOrphansFor(nodeid);
     935         726 :     nPreferredDownload -= state->fPreferredDownload;
     936         726 :     nPeersWithValidatedDownloads -= (state->nBlocksInFlightValidHeaders != 0);
     937         726 :     assert(nPeersWithValidatedDownloads >= 0);
     938         726 :     g_outbound_peers_with_protect_from_disconnect -= state->m_chain_sync.m_protect;
     939         726 :     assert(g_outbound_peers_with_protect_from_disconnect >= 0);
     940         726 :     g_wtxid_relay_peers -= state->m_wtxid_relay;
     941         726 :     assert(g_wtxid_relay_peers >= 0);
     942             : 
     943         726 :     mapNodeState.erase(nodeid);
     944             : 
     945         726 :     if (mapNodeState.empty()) {
     946             :         // Do a consistency check after the last peer is removed.
     947         453 :         assert(mapBlocksInFlight.empty());
     948         453 :         assert(nPreferredDownload == 0);
     949         453 :         assert(nPeersWithValidatedDownloads == 0);
     950         453 :         assert(g_outbound_peers_with_protect_from_disconnect == 0);
     951         453 :         assert(g_wtxid_relay_peers == 0);
     952             :     }
     953         726 :     LogPrint(BCLog::NET, "Cleared nodestate for peer=%d\n", nodeid);
     954         726 : }
     955             : 
     956        8635 : bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
     957             :     {
     958        8635 :         LOCK(cs_main);
     959        8635 :         CNodeState* state = State(nodeid);
     960        8635 :         if (state == nullptr)
     961           0 :             return false;
     962        8635 :         stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
     963        8635 :         stats.nCommonHeight = state->pindexLastCommonBlock ? state->pindexLastCommonBlock->nHeight : -1;
     964       12407 :         for (const QueuedBlock& queue : state->vBlocksInFlight) {
     965        3772 :             if (queue.pindex)
     966        3772 :                 stats.vHeightInFlight.push_back(queue.pindex->nHeight);
     967             :         }
     968        8635 :     }
     969             : 
     970        8635 :     PeerRef peer = GetPeerRef(nodeid);
     971        8635 :     if (peer == nullptr) return false;
     972       17270 :     stats.m_misbehavior_score = WITH_LOCK(peer->m_misbehavior_mutex, return peer->m_misbehavior_score);
     973             : 
     974        8635 :     return true;
     975        8635 : }
     976             : 
     977             : //////////////////////////////////////////////////////////////////////////////
     978             : //
     979             : // mapOrphanTransactions
     980             : //
     981             : 
     982         398 : static void AddToCompactExtraTransactions(const CTransactionRef& tx) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
     983             : {
     984         398 :     size_t max_extra_txn = gArgs.GetArg("-blockreconstructionextratxn", DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN);
     985         398 :     if (max_extra_txn <= 0)
     986           0 :         return;
     987         398 :     if (!vExtraTxnForCompact.size())
     988          16 :         vExtraTxnForCompact.resize(max_extra_txn);
     989         398 :     vExtraTxnForCompact[vExtraTxnForCompactIt] = std::make_pair(tx->GetWitnessHash(), tx);
     990         398 :     vExtraTxnForCompactIt = (vExtraTxnForCompactIt + 1) % max_extra_txn;
     991         398 : }
     992             : 
     993         239 : bool AddOrphanTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
     994             : {
     995         239 :     const uint256& hash = tx->GetHash();
     996         239 :     if (mapOrphanTransactions.count(hash))
     997          21 :         return false;
     998             : 
     999             :     // Ignore big transactions, to avoid a
    1000             :     // send-big-orphans memory exhaustion attack. If a peer has a legitimate
    1001             :     // large transaction with a missing parent then we assume
    1002             :     // it will rebroadcast it later, after the parent transaction(s)
    1003             :     // have been mined or received.
    1004             :     // 100 orphans, each of which is at most 100,000 bytes big is
    1005             :     // at most 10 megabytes of orphans and somewhat more byprev index (in the worst case):
    1006         218 :     unsigned int sz = GetTransactionWeight(*tx);
    1007         218 :     if (sz > MAX_STANDARD_TX_WEIGHT)
    1008             :     {
    1009          10 :         LogPrint(BCLog::MEMPOOL, "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString());
    1010          10 :         return false;
    1011             :     }
    1012             : 
    1013         208 :     auto ret = mapOrphanTransactions.emplace(hash, COrphanTx{tx, peer, GetTime() + ORPHAN_TX_EXPIRE_TIME, g_orphan_list.size()});
    1014         208 :     assert(ret.second);
    1015         208 :     g_orphan_list.push_back(ret.first);
    1016             :     // Allow for lookups in the orphan pool by wtxid, as well as txid
    1017         208 :     g_orphans_by_wtxid.emplace(tx->GetWitnessHash(), ret.first);
    1018         417 :     for (const CTxIn& txin : tx->vin) {
    1019         209 :         mapOrphanTransactionsByPrev[txin.prevout].insert(ret.first);
    1020             :     }
    1021             : 
    1022         208 :     AddToCompactExtraTransactions(tx);
    1023             : 
    1024         208 :     LogPrint(BCLog::MEMPOOL, "stored orphan tx %s (mapsz %u outsz %u)\n", hash.ToString(),
    1025             :              mapOrphanTransactions.size(), mapOrphanTransactionsByPrev.size());
    1026             :     return true;
    1027         239 : }
    1028             : 
    1029         208 : int static EraseOrphanTx(uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
    1030             : {
    1031         208 :     std::map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.find(hash);
    1032         208 :     if (it == mapOrphanTransactions.end())
    1033           0 :         return 0;
    1034         417 :     for (const CTxIn& txin : it->second.tx->vin)
    1035             :     {
    1036         209 :         auto itPrev = mapOrphanTransactionsByPrev.find(txin.prevout);
    1037         209 :         if (itPrev == mapOrphanTransactionsByPrev.end())
    1038           0 :             continue;
    1039         209 :         itPrev->second.erase(it);
    1040         209 :         if (itPrev->second.empty())
    1041         209 :             mapOrphanTransactionsByPrev.erase(itPrev);
    1042         209 :     }
    1043             : 
    1044         208 :     size_t old_pos = it->second.list_pos;
    1045         208 :     assert(g_orphan_list[old_pos] == it);
    1046         208 :     if (old_pos + 1 != g_orphan_list.size()) {
    1047             :         // Unless we're deleting the last entry in g_orphan_list, move the last
    1048             :         // entry to the position we're deleting.
    1049         176 :         auto it_last = g_orphan_list.back();
    1050         176 :         g_orphan_list[old_pos] = it_last;
    1051         176 :         it_last->second.list_pos = old_pos;
    1052         176 :     }
    1053         208 :     g_orphan_list.pop_back();
    1054         208 :     g_orphans_by_wtxid.erase(it->second.tx->GetWitnessHash());
    1055             : 
    1056         208 :     mapOrphanTransactions.erase(it);
    1057             :     return 1;
    1058         208 : }
    1059             : 
    1060         729 : void EraseOrphansFor(NodeId peer)
    1061             : {
    1062         729 :     LOCK(g_cs_orphans);
    1063         729 :     int nErased = 0;
    1064         729 :     std::map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
    1065        1064 :     while (iter != mapOrphanTransactions.end())
    1066             :     {
    1067         335 :         std::map<uint256, COrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
    1068         335 :         if (maybeErase->second.fromPeer == peer)
    1069             :         {
    1070         109 :             nErased += EraseOrphanTx(maybeErase->second.tx->GetHash());
    1071         109 :         }
    1072         335 :     }
    1073         729 :     if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx from peer=%d\n", nErased, peer);
    1074         729 : }
    1075             : 
    1076             : 
    1077         132 : unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
    1078             : {
    1079         132 :     LOCK(g_cs_orphans);
    1080             : 
    1081             :     unsigned int nEvicted = 0;
    1082             :     static int64_t nNextSweep;
    1083         132 :     int64_t nNow = GetTime();
    1084         132 :     if (nNextSweep <= nNow) {
    1085             :         // Sweep out expired orphan pool entries:
    1086           5 :         int nErased = 0;
    1087           5 :         int64_t nMinExpTime = nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL;
    1088           5 :         std::map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
    1089          83 :         while (iter != mapOrphanTransactions.end())
    1090             :         {
    1091          78 :             std::map<uint256, COrphanTx>::iterator maybeErase = iter++;
    1092          78 :             if (maybeErase->second.nTimeExpire <= nNow) {
    1093           0 :                 nErased += EraseOrphanTx(maybeErase->second.tx->GetHash());
    1094           0 :             } else {
    1095          78 :                 nMinExpTime = std::min(maybeErase->second.nTimeExpire, nMinExpTime);
    1096             :             }
    1097          78 :         }
    1098             :         // Sweep again 5 minutes after the next entry that expires in order to batch the linear scan.
    1099           5 :         nNextSweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL;
    1100           5 :         if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx due to expiration\n", nErased);
    1101           5 :     }
    1102         132 :     FastRandomContext rng;
    1103         207 :     while (mapOrphanTransactions.size() > nMaxOrphans)
    1104             :     {
    1105             :         // Evict a random orphan:
    1106          75 :         size_t randompos = rng.randrange(g_orphan_list.size());
    1107          75 :         EraseOrphanTx(g_orphan_list[randompos]->first);
    1108          75 :         ++nEvicted;
    1109             :     }
    1110             :     return nEvicted;
    1111         132 : }
    1112             : 
    1113         542 : void PeerManager::Misbehaving(const NodeId pnode, const int howmuch, const std::string& message)
    1114             : {
    1115         542 :     assert(howmuch > 0);
    1116             : 
    1117         542 :     PeerRef peer = GetPeerRef(pnode);
    1118         542 :     if (peer == nullptr) return;
    1119             : 
    1120         542 :     LOCK(peer->m_misbehavior_mutex);
    1121         542 :     peer->m_misbehavior_score += howmuch;
    1122         542 :     const std::string message_prefixed = message.empty() ? "" : (": " + message);
    1123         542 :     if (peer->m_misbehavior_score >= DISCOURAGEMENT_THRESHOLD && peer->m_misbehavior_score - howmuch < DISCOURAGEMENT_THRESHOLD) {
    1124          97 :         LogPrint(BCLog::NET, "Misbehaving: peer=%d (%d -> %d) DISCOURAGE THRESHOLD EXCEEDED%s\n", pnode, peer->m_misbehavior_score - howmuch, peer->m_misbehavior_score, message_prefixed);
    1125          97 :         peer->m_should_discourage = true;
    1126          97 :     } else {
    1127         445 :         LogPrint(BCLog::NET, "Misbehaving: peer=%d (%d -> %d)%s\n", pnode, peer->m_misbehavior_score - howmuch, peer->m_misbehavior_score, message_prefixed);
    1128             :     }
    1129         542 : }
    1130             : 
    1131         430 : bool PeerManager::MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationState& state,
    1132             :                                           bool via_compact_block, const std::string& message)
    1133             : {
    1134         430 :     switch (state.GetResult()) {
    1135             :     case BlockValidationResult::BLOCK_RESULT_UNSET:
    1136             :         break;
    1137             :     // The node is providing invalid data:
    1138             :     case BlockValidationResult::BLOCK_CONSENSUS:
    1139             :     case BlockValidationResult::BLOCK_MUTATED:
    1140         399 :         if (!via_compact_block) {
    1141         381 :             Misbehaving(nodeid, 100, message);
    1142         381 :             return true;
    1143             :         }
    1144             :         break;
    1145             :     case BlockValidationResult::BLOCK_CACHED_INVALID:
    1146             :         {
    1147          19 :             LOCK(cs_main);
    1148          19 :             CNodeState *node_state = State(nodeid);
    1149          19 :             if (node_state == nullptr) {
    1150           0 :                 break;
    1151             :             }
    1152             : 
    1153             :             // Discourage outbound (but not inbound) peers if on an invalid chain.
    1154             :             // Exempt HB compact block peers and manual connections.
    1155          19 :             if (!via_compact_block && !node_state->m_is_inbound && !node_state->m_is_manual_connection) {
    1156           0 :                 Misbehaving(nodeid, 100, message);
    1157           0 :                 return true;
    1158             :             }
    1159          19 :             break;
    1160          19 :         }
    1161             :     case BlockValidationResult::BLOCK_INVALID_HEADER:
    1162             :     case BlockValidationResult::BLOCK_CHECKPOINT:
    1163             :     case BlockValidationResult::BLOCK_INVALID_PREV:
    1164          10 :         Misbehaving(nodeid, 100, message);
    1165          10 :         return true;
    1166             :     // Conflicting (but not necessarily invalid) data or different policy:
    1167             :     case BlockValidationResult::BLOCK_MISSING_PREV:
    1168             :         // TODO: Handle this much more gracefully (10 DoS points is super arbitrary)
    1169           1 :         Misbehaving(nodeid, 10, message);
    1170           1 :         return true;
    1171             :     case BlockValidationResult::BLOCK_RECENT_CONSENSUS_CHANGE:
    1172             :     case BlockValidationResult::BLOCK_TIME_FUTURE:
    1173             :         break;
    1174             :     }
    1175          38 :     if (message != "") {
    1176          19 :         LogPrint(BCLog::NET, "peer=%d: %s\n", nodeid, message);
    1177             :     }
    1178          38 :     return false;
    1179         430 : }
    1180             : 
    1181         258 : bool PeerManager::MaybePunishNodeForTx(NodeId nodeid, const TxValidationState& state, const std::string& message)
    1182             : {
    1183         258 :     switch (state.GetResult()) {
    1184             :     case TxValidationResult::TX_RESULT_UNSET:
    1185             :         break;
    1186             :     // The node is providing invalid data:
    1187             :     case TxValidationResult::TX_CONSENSUS:
    1188          25 :         Misbehaving(nodeid, 100, message);
    1189          25 :         return true;
    1190             :     // Conflicting (but not necessarily invalid) data or different policy:
    1191             :     case TxValidationResult::TX_RECENT_CONSENSUS_CHANGE:
    1192             :     case TxValidationResult::TX_INPUTS_NOT_STANDARD:
    1193             :     case TxValidationResult::TX_NOT_STANDARD:
    1194             :     case TxValidationResult::TX_MISSING_INPUTS:
    1195             :     case TxValidationResult::TX_PREMATURE_SPEND:
    1196             :     case TxValidationResult::TX_WITNESS_MUTATED:
    1197             :     case TxValidationResult::TX_WITNESS_STRIPPED:
    1198             :     case TxValidationResult::TX_CONFLICT:
    1199             :     case TxValidationResult::TX_MEMPOOL_POLICY:
    1200             :         break;
    1201             :     }
    1202         233 :     if (message != "") {
    1203           0 :         LogPrint(BCLog::NET, "peer=%d: %s\n", nodeid, message);
    1204             :     }
    1205         233 :     return false;
    1206         258 : }
    1207             : 
    1208             : 
    1209             : //////////////////////////////////////////////////////////////////////////////
    1210             : //
    1211             : // blockchain -> download logic notification
    1212             : //
    1213             : 
    1214             : // To prevent fingerprinting attacks, only send blocks/headers outside of the
    1215             : // active chain if they are no more than a month older (both in time, and in
    1216             : // best equivalent proof of work) than the best header chain we know about and
    1217             : // we fully-validated them at some point.
    1218       15143 : static bool BlockRequestAllowed(const CBlockIndex* pindex, const Consensus::Params& consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
    1219             : {
    1220       15143 :     AssertLockHeld(cs_main);
    1221       15143 :     if (::ChainActive().Contains(pindex)) return true;
    1222          16 :     return pindex->IsValid(BLOCK_VALID_SCRIPTS) && (pindexBestHeader != nullptr) &&
    1223           7 :         (pindexBestHeader->GetBlockTime() - pindex->GetBlockTime() < STALE_RELAY_AGE_LIMIT) &&
    1224           5 :         (GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, consensusParams) < STALE_RELAY_AGE_LIMIT);
    1225       15143 : }
    1226             : 
    1227        1220 : PeerManager::PeerManager(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
    1228             :                          CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool)
    1229         610 :     : m_chainparams(chainparams),
    1230         610 :       m_connman(connman),
    1231         610 :       m_banman(banman),
    1232         610 :       m_chainman(chainman),
    1233         610 :       m_mempool(pool),
    1234         610 :       m_stale_tip_check_time(0)
    1235        1220 : {
    1236             :     // Initialize global variables that cannot be constructed at startup.
    1237         610 :     recentRejects.reset(new CRollingBloomFilter(120000, 0.000001));
    1238             : 
    1239             :     // Blocks don't typically have more than 4000 transactions, so this should
    1240             :     // be at least six blocks (~1 hr) worth of transactions that we can store,
    1241             :     // inserting both a txid and wtxid for every observed transaction.
    1242             :     // If the number of transactions appearing in a block goes up, or if we are
    1243             :     // seeing getdata requests more than an hour after initial announcement, we
    1244             :     // can increase this number.
    1245             :     // The false positive rate of 1/1M should come out to less than 1
    1246             :     // transaction per day that would be inadvertently ignored (which is the
    1247             :     // same probability that we have in the reject filter).
    1248         610 :     g_recent_confirmed_transactions.reset(new CRollingBloomFilter(48000, 0.000001));
    1249             : 
    1250             :     // Stale tip checking and peer eviction are on two different timers, but we
    1251             :     // don't want them to get out of sync due to drift in the scheduler, so we
    1252             :     // combine them in one function and schedule at the quicker (peer-eviction)
    1253             :     // timer.
    1254             :     static_assert(EXTRA_PEER_CHECK_INTERVAL < STALE_CHECK_INTERVAL, "peer eviction timer should be less than stale tip check timer");
    1255         808 :     scheduler.scheduleEvery([this] { this->CheckForStaleTipAndEvictPeers(); }, std::chrono::seconds{EXTRA_PEER_CHECK_INTERVAL});
    1256             : 
    1257             :     // schedule next run for 10-15 minutes in the future
    1258         610 :     const std::chrono::milliseconds delta = std::chrono::minutes{10} + GetRandMillis(std::chrono::minutes{5});
    1259         612 :     scheduler.scheduleFromNow([&] { ReattemptInitialBroadcast(scheduler); }, delta);
    1260        1220 : }
    1261             : 
    1262             : /**
    1263             :  * Evict orphan txn pool entries (EraseOrphanTx) based on a newly connected
    1264             :  * block. Also save the time of the last tip update.
    1265             :  */
    1266       41189 : void PeerManager::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
    1267             : {
    1268             :     {
    1269       41189 :         LOCK(g_cs_orphans);
    1270             : 
    1271       41189 :         std::vector<uint256> vOrphanErase;
    1272             : 
    1273      125812 :         for (const CTransactionRef& ptx : pblock->vtx) {
    1274       84623 :             const CTransaction& tx = *ptx;
    1275             : 
    1276             :             // Which orphan pool entries must we evict?
    1277      200036 :             for (const auto& txin : tx.vin) {
    1278      115413 :                 auto itByPrev = mapOrphanTransactionsByPrev.find(txin.prevout);
    1279      115413 :                 if (itByPrev == mapOrphanTransactionsByPrev.end()) continue;
    1280          38 :                 for (auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); ++mi) {
    1281          19 :                     const CTransaction& orphanTx = *(*mi)->second.tx;
    1282          19 :                     const uint256& orphanHash = orphanTx.GetHash();
    1283          19 :                     vOrphanErase.push_back(orphanHash);
    1284           0 :                 }
    1285      115413 :             }
    1286             :         }
    1287             : 
    1288             :         // Erase orphan transactions included or precluded by this block
    1289       41189 :         if (vOrphanErase.size()) {
    1290           1 :             int nErased = 0;
    1291          20 :             for (const uint256& orphanHash : vOrphanErase) {
    1292          19 :                 nErased += EraseOrphanTx(orphanHash);
    1293             :             }
    1294           1 :             LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx included or conflicted by block\n", nErased);
    1295           1 :         }
    1296             : 
    1297       41189 :         g_last_tip_update = GetTime();
    1298       41189 :     }
    1299             :     {
    1300       41189 :         LOCK(g_cs_recent_confirmed_transactions);
    1301      125812 :         for (const auto& ptx : pblock->vtx) {
    1302       84623 :             g_recent_confirmed_transactions->insert(ptx->GetHash());
    1303       84623 :             if (ptx->GetHash() != ptx->GetWitnessHash()) {
    1304       38035 :                 g_recent_confirmed_transactions->insert(ptx->GetWitnessHash());
    1305             :             }
    1306             :         }
    1307       41189 :     }
    1308       41189 : }
    1309             : 
    1310        2900 : void PeerManager::BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex)
    1311             : {
    1312             :     // To avoid relay problems with transactions that were previously
    1313             :     // confirmed, clear our filter of recently confirmed transactions whenever
    1314             :     // there's a reorg.
    1315             :     // This means that in a 1-block reorg (where 1 block is disconnected and
    1316             :     // then another block reconnected), our filter will drop to having only one
    1317             :     // block's worth of transactions in it, but that should be fine, since
    1318             :     // presumably the most common case of relaying a confirmed transaction
    1319             :     // should be just after a new block containing it is found.
    1320        2900 :     LOCK(g_cs_recent_confirmed_transactions);
    1321        2900 :     g_recent_confirmed_transactions->reset();
    1322        2900 : }
    1323             : 
    1324             : // All of the following cache a recent block, and are protected by cs_most_recent_block
    1325         640 : static RecursiveMutex cs_most_recent_block;
    1326         640 : static std::shared_ptr<const CBlock> most_recent_block GUARDED_BY(cs_most_recent_block);
    1327         640 : static std::shared_ptr<const CBlockHeaderAndShortTxIDs> most_recent_compact_block GUARDED_BY(cs_most_recent_block);
    1328         640 : static uint256 most_recent_block_hash GUARDED_BY(cs_most_recent_block);
    1329             : static bool fWitnessesPresentInMostRecentCompactBlock GUARDED_BY(cs_most_recent_block);
    1330             : 
    1331             : /**
    1332             :  * Maintain state about the best-seen block and fast-announce a compact block
    1333             :  * to compatible peers.
    1334             :  */
    1335       34695 : void PeerManager::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) {
    1336       34695 :     std::shared_ptr<const CBlockHeaderAndShortTxIDs> pcmpctblock = std::make_shared<const CBlockHeaderAndShortTxIDs> (*pblock, true);
    1337       34695 :     const CNetMsgMaker msgMaker(PROTOCOL_VERSION);
    1338             : 
    1339       34695 :     LOCK(cs_main);
    1340             : 
    1341             :     static int nHighestFastAnnounce = 0;
    1342       34695 :     if (pindex->nHeight <= nHighestFastAnnounce)
    1343         229 :         return;
    1344       34466 :     nHighestFastAnnounce = pindex->nHeight;
    1345             : 
    1346       34466 :     bool fWitnessEnabled = IsWitnessEnabled(pindex->pprev, m_chainparams.GetConsensus());
    1347       34466 :     uint256 hashBlock(pblock->GetHash());
    1348             : 
    1349             :     {
    1350       34466 :         LOCK(cs_most_recent_block);
    1351       34466 :         most_recent_block_hash = hashBlock;
    1352       34466 :         most_recent_block = pblock;
    1353       34466 :         most_recent_compact_block = pcmpctblock;
    1354       34466 :         fWitnessesPresentInMostRecentCompactBlock = fWitnessEnabled;
    1355       34466 :     }
    1356             : 
    1357       81713 :     m_connman.ForEachNode([this, &pcmpctblock, pindex, &msgMaker, fWitnessEnabled, &hashBlock](CNode* pnode) {
    1358       47247 :         LockAssertion lock(::cs_main);
    1359             : 
    1360             :         // TODO: Avoid the repeated-serialization here
    1361       47247 :         if (pnode->nVersion < INVALID_CB_NO_BAN_VERSION || pnode->fDisconnect)
    1362           0 :             return;
    1363       47247 :         ProcessBlockAvailability(pnode->GetId());
    1364       47247 :         CNodeState &state = *State(pnode->GetId());
    1365             :         // If the peer has, or we announced to them the previous block already,
    1366             :         // but we don't think they have this one, go ahead and announce it
    1367       59140 :         if (state.fPreferHeaderAndIDs && (!fWitnessEnabled || state.fWantsCmpctWitness) &&
    1368       12946 :                 !PeerHasHeader(&state, pindex) && PeerHasHeader(&state, pindex->pprev)) {
    1369             : 
    1370       11028 :             LogPrint(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", "PeerManager::NewPoWValidBlock",
    1371             :                     hashBlock.ToString(), pnode->GetId());
    1372       11028 :             m_connman.PushMessage(pnode, msgMaker.Make(NetMsgType::CMPCTBLOCK, *pcmpctblock));
    1373       11028 :             state.pindexBestHeaderSent = pindex;
    1374       11028 :         }
    1375       47247 :     });
    1376       34695 : }
    1377             : 
    1378             : /**
    1379             :  * Update our best height and announce any block hashes which weren't previously
    1380             :  * in ::ChainActive() to our peers.
    1381             :  */
    1382       39878 : void PeerManager::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
    1383       39878 :     const int nNewHeight = pindexNew->nHeight;
    1384       39878 :     m_connman.SetBestHeight(nNewHeight);
    1385             : 
    1386       39878 :     SetServiceFlagsIBDCache(!fInitialDownload);
    1387       39878 :     if (!fInitialDownload) {
    1388             :         // Find the hashes of all blocks that weren't previously in the best chain.
    1389       35323 :         std::vector<uint256> vHashes;
    1390             :         const CBlockIndex *pindexToAnnounce = pindexNew;
    1391       70781 :         while (pindexToAnnounce != pindexFork) {
    1392       35466 :             vHashes.push_back(pindexToAnnounce->GetBlockHash());
    1393       35466 :             pindexToAnnounce = pindexToAnnounce->pprev;
    1394       35466 :             if (vHashes.size() == MAX_BLOCKS_TO_ANNOUNCE) {
    1395             :                 // Limit announcements in case of a huge reorganization.
    1396             :                 // Rely on the peer's synchronization mechanism in that case.
    1397             :                 break;
    1398             :             }
    1399             :         }
    1400             :         // Relay inventory, but don't relay old inventory during initial block download.
    1401       83718 :         m_connman.ForEachNode([nNewHeight, &vHashes](CNode* pnode) {
    1402       48395 :             LOCK(pnode->cs_inventory);
    1403       48395 :             if (nNewHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : 0)) {
    1404       97019 :                 for (const uint256& hash : reverse_iterate(vHashes)) {
    1405       48624 :                     pnode->vBlockHashesToAnnounce.push_back(hash);
    1406           0 :                 }
    1407       48395 :             }
    1408       48395 :         });
    1409       35323 :         m_connman.WakeMessageHandler();
    1410       35323 :     }
    1411       39878 : }
    1412             : 
    1413             : /**
    1414             :  * Handle invalid block rejection and consequent peer discouragement, maintain which
    1415             :  * peers announce compact blocks.
    1416             :  */
    1417       42642 : void PeerManager::BlockChecked(const CBlock& block, const BlockValidationState& state) {
    1418       42642 :     LOCK(cs_main);
    1419             : 
    1420       42642 :     const uint256 hash(block.GetHash());
    1421       42642 :     std::map<uint256, std::pair<NodeId, bool>>::iterator it = mapBlockSource.find(hash);
    1422             : 
    1423             :     // If the block failed validation, we know where it came from and we're still connected
    1424             :     // to that peer, maybe punish.
    1425       43069 :     if (state.IsInvalid() &&
    1426         427 :         it != mapBlockSource.end() &&
    1427         407 :         State(it->second.first)) {
    1428         407 :             MaybePunishNodeForBlock(/*nodeid=*/ it->second.first, state, /*via_compact_block=*/ !it->second.second);
    1429         407 :     }
    1430             :     // Check that:
    1431             :     // 1. The block is valid
    1432             :     // 2. We're not in initial block download
    1433             :     // 3. This is currently the best block we're aware of. We haven't updated
    1434             :     //    the tip yet so we have no way to check this directly here. Instead we
    1435             :     //    just check that there are currently no other blocks in flight.
    1436       79624 :     else if (state.IsValid() &&
    1437       42215 :              !::ChainstateActive().IsInitialBlockDownload() &&
    1438       37389 :              mapBlocksInFlight.count(hash) == mapBlocksInFlight.size()) {
    1439       29672 :         if (it != mapBlockSource.end()) {
    1440       11988 :             MaybeSetPeerAsAnnouncingHeaderAndIDs(it->second.first, m_connman);
    1441             :         }
    1442             :     }
    1443       42642 :     if (it != mapBlockSource.end())
    1444       22801 :         mapBlockSource.erase(it);
    1445       42642 : }
    1446             : 
    1447             : //////////////////////////////////////////////////////////////////////////////
    1448             : //
    1449             : // Messages
    1450             : //
    1451             : 
    1452             : 
    1453       34970 : bool static AlreadyHaveTx(const GenTxid& gtxid, const CTxMemPool& mempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
    1454             : {
    1455       34970 :     assert(recentRejects);
    1456       34970 :     if (::ChainActive().Tip()->GetBlockHash() != hashRecentRejectsChainTip) {
    1457             :         // If the chain tip has changed previously rejected transactions
    1458             :         // might be now valid, e.g. due to a nLockTime'd tx becoming valid,
    1459             :         // or a double-spend. Reset the rejects filter and give those
    1460             :         // txs a second chance.
    1461         585 :         hashRecentRejectsChainTip = ::ChainActive().Tip()->GetBlockHash();
    1462         585 :         recentRejects->reset();
    1463         585 :     }
    1464             : 
    1465       34970 :     const uint256& hash = gtxid.GetHash();
    1466             : 
    1467             :     {
    1468       34970 :         LOCK(g_cs_orphans);
    1469       34970 :         if (!gtxid.IsWtxid() && mapOrphanTransactions.count(hash)) {
    1470          21 :             return true;
    1471       34949 :         } else if (gtxid.IsWtxid() && g_orphans_by_wtxid.count(hash)) {
    1472           0 :             return true;
    1473             :         }
    1474       34970 :     }
    1475             : 
    1476             :     {
    1477       34949 :         LOCK(g_cs_recent_confirmed_transactions);
    1478       34949 :         if (g_recent_confirmed_transactions->contains(hash)) return true;
    1479       34949 :     }
    1480             : 
    1481       34181 :     return recentRejects->contains(hash) || mempool.exists(gtxid);
    1482       34970 : }
    1483             : 
    1484        1033 : bool static AlreadyHaveBlock(const uint256& block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
    1485             : {
    1486        1033 :     return LookupBlockIndex(block_hash) != nullptr;
    1487             : }
    1488             : 
    1489       18271 : void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman& connman)
    1490             : {
    1491       49204 :     connman.ForEachNode([&txid, &wtxid](CNode* pnode)
    1492             :     {
    1493       30933 :         LockAssertion lock(::cs_main);
    1494             : 
    1495       30933 :         CNodeState &state = *State(pnode->GetId());
    1496       30933 :         if (state.m_wtxid_relay) {
    1497       30814 :             pnode->PushTxInventory(wtxid);
    1498             :         } else {
    1499         119 :             pnode->PushTxInventory(txid);
    1500             :         }
    1501       30933 :     });
    1502       18271 : }
    1503             : 
    1504          10 : static void RelayAddress(const CAddress& addr, bool fReachable, const CConnman& connman)
    1505             : {
    1506             : 
    1507             :     // Relay to a limited number of other nodes
    1508             :     // Use deterministic randomness to send to the same nodes for 24 hours
    1509             :     // at a time so the m_addr_knowns of the chosen nodes prevent repeats
    1510          10 :     uint64_t hashAddr = addr.GetHash();
    1511          10 :     const CSipHasher hasher = connman.GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24 * 60 * 60));
    1512          10 :     FastRandomContext insecure_rand;
    1513             : 
    1514             :     // Relay reachable addresses to 2 peers. Unreachable addresses are relayed randomly to 1 or 2 peers.
    1515          10 :     unsigned int nRelayNodes = (fReachable || (hasher.Finalize() & 1)) ? 2 : 1;
    1516             : 
    1517          10 :     std::array<std::pair<uint64_t, CNode*>,2> best{{{0, nullptr}, {0, nullptr}}};
    1518          10 :     assert(nRelayNodes <= best.size());
    1519             : 
    1520          30 :     auto sortfunc = [&best, &hasher, nRelayNodes](CNode* pnode) {
    1521          20 :         if (pnode->RelayAddrsWithConn()) {
    1522          20 :             uint64_t hashKey = CSipHasher(hasher).Write(pnode->GetId()).Finalize();
    1523          26 :             for (unsigned int i = 0; i < nRelayNodes; i++) {
    1524          26 :                  if (hashKey > best[i].first) {
    1525          20 :                      std::copy(best.begin() + i, best.begin() + nRelayNodes - 1, best.begin() + i + 1);
    1526          20 :                      best[i] = std::make_pair(hashKey, pnode);
    1527          20 :                      break;
    1528             :                  }
    1529             :             }
    1530          20 :         }
    1531          20 :     };
    1532             : 
    1533          20 :     auto pushfunc = [&addr, &best, nRelayNodes, &insecure_rand] {
    1534          30 :         for (unsigned int i = 0; i < nRelayNodes && best[i].first != 0; i++) {
    1535          20 :             best[i].second->PushAddress(addr, insecure_rand);
    1536             :         }
    1537          10 :     };
    1538             : 
    1539          10 :     connman.ForEachNodeThen(std::move(sortfunc), std::move(pushfunc));
    1540          10 : }
    1541             : 
    1542       15127 : void static ProcessGetBlockData(CNode& pfrom, const CChainParams& chainparams, const CInv& inv, CConnman& connman)
    1543             : {
    1544             :     bool send = false;
    1545       15127 :     std::shared_ptr<const CBlock> a_recent_block;
    1546       15127 :     std::shared_ptr<const CBlockHeaderAndShortTxIDs> a_recent_compact_block;
    1547             :     bool fWitnessesPresentInARecentCompactBlock;
    1548       15127 :     const Consensus::Params& consensusParams = chainparams.GetConsensus();
    1549             :     {
    1550       15127 :         LOCK(cs_most_recent_block);
    1551       15127 :         a_recent_block = most_recent_block;
    1552       15127 :         a_recent_compact_block = most_recent_compact_block;
    1553       15127 :         fWitnessesPresentInARecentCompactBlock = fWitnessesPresentInMostRecentCompactBlock;
    1554       15127 :     }
    1555             : 
    1556             :     bool need_activate_chain = false;
    1557             :     {
    1558       15127 :         LOCK(cs_main);
    1559       15127 :         const CBlockIndex* pindex = LookupBlockIndex(inv.hash);
    1560       15127 :         if (pindex) {
    1561       15132 :             if (pindex->HaveTxsDownloaded() && !pindex->IsValid(BLOCK_VALID_SCRIPTS) &&
    1562           5 :                     pindex->IsValid(BLOCK_VALID_TREE)) {
    1563             :                 // If we have the block and all of its parents, but have not yet validated it,
    1564             :                 // we might be in the middle of connecting it (ie in the unlock of cs_main
    1565             :                 // before ActivateBestChain but after AcceptBlock).
    1566             :                 // In this case, we need to run ActivateBestChain prior to checking the relay
    1567             :                 // conditions below.
    1568             :                 need_activate_chain = true;
    1569           4 :             }
    1570             :         }
    1571       15127 :     } // release cs_main before calling ActivateBestChain
    1572       15127 :     if (need_activate_chain) {
    1573           4 :         BlockValidationState state;
    1574           4 :         if (!ActivateBestChain(state, chainparams, a_recent_block)) {
    1575           0 :             LogPrint(BCLog::NET, "failed to activate chain (%s)\n", state.ToString());
    1576             :         }
    1577           4 :     }
    1578             : 
    1579       15127 :     LOCK(cs_main);
    1580       15127 :     const CBlockIndex* pindex = LookupBlockIndex(inv.hash);
    1581       15127 :     if (pindex) {
    1582       15127 :         send = BlockRequestAllowed(pindex, consensusParams);
    1583       15127 :         if (!send) {
    1584           2 :             LogPrint(BCLog::NET, "%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom.GetId());
    1585             :         }
    1586             :     }
    1587       15127 :     const CNetMsgMaker msgMaker(pfrom.GetSendVersion());
    1588             :     // disconnect node in case we have reached the outbound limit for serving historical blocks
    1589       15130 :     if (send &&
    1590       15125 :         connman.OutboundTargetReached(true) &&
    1591         823 :         (((pindexBestHeader != nullptr) && (pindexBestHeader->GetBlockTime() - pindex->GetBlockTime() > HISTORICAL_BLOCK_AGE)) || inv.IsMsgFilteredBlk()) &&
    1592           3 :         !pfrom.HasPermission(PF_DOWNLOAD) // nodes with the download permission may exceed target
    1593             :     ) {
    1594           2 :         LogPrint(BCLog::NET, "historical block serving limit reached, disconnect peer=%d\n", pfrom.GetId());
    1595             : 
    1596             :         //disconnect node
    1597           2 :         pfrom.fDisconnect = true;
    1598             :         send = false;
    1599           2 :     }
    1600             :     // Avoid leaking prune-height by never sending blocks below the NODE_NETWORK_LIMITED threshold
    1601       15284 :     if (send && !pfrom.HasPermission(PF_NOBAN) && (
    1602       11869 :             (((pfrom.GetLocalServices() & NODE_NETWORK_LIMITED) == NODE_NETWORK_LIMITED) && ((pfrom.GetLocalServices() & NODE_NETWORK) != NODE_NETWORK) && (::ChainActive().Tip()->nHeight - pindex->nHeight > (int)NODE_NETWORK_LIMITED_MIN_BLOCKS + 2 /* add two blocks buffer extension for possible races */) )
    1603             :        )) {
    1604           1 :         LogPrint(BCLog::NET, "Ignore block request below NODE_NETWORK_LIMITED threshold from peer=%d\n", pfrom.GetId());
    1605             : 
    1606             :         //disconnect node and prevent it from stalling (would otherwise wait for the missing block)
    1607           1 :         pfrom.fDisconnect = true;
    1608             :         send = false;
    1609           1 :     }
    1610             :     // Pruned nodes may have deleted the block, so check whether
    1611             :     // it's available before trying to send.
    1612       15127 :     if (send && (pindex->nStatus & BLOCK_HAVE_DATA))
    1613             :     {
    1614       15122 :         std::shared_ptr<const CBlock> pblock;
    1615       15122 :         if (a_recent_block && a_recent_block->GetHash() == pindex->GetBlockHash()) {
    1616        3980 :             pblock = a_recent_block;
    1617       15122 :         } else if (inv.IsMsgWitnessBlk()) {
    1618             :             // Fast-path: in this case it is possible to serve the block directly from disk,
    1619             :             // as the network format matches the format on disk
    1620        6213 :             std::vector<uint8_t> block_data;
    1621        6213 :             if (!ReadRawBlockFromDisk(block_data, pindex, chainparams.MessageStart())) {
    1622           0 :                 assert(!"cannot load block from disk");
    1623             :             }
    1624        6213 :             connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::BLOCK, MakeSpan(block_data)));
    1625             :             // Don't set pblock as we've sent the block
    1626        6213 :         } else {
    1627             :             // Send block from disk
    1628        4929 :             std::shared_ptr<CBlock> pblockRead = std::make_shared<CBlock>();
    1629        4929 :             if (!ReadBlockFromDisk(*pblockRead, pindex, consensusParams))
    1630           0 :                 assert(!"cannot load block from disk");
    1631        4929 :             pblock = pblockRead;
    1632        4929 :         }
    1633       15122 :         if (pblock) {
    1634        8909 :             if (inv.IsMsgBlk()) {
    1635        8421 :                 connman.PushMessage(&pfrom, msgMaker.Make(SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::BLOCK, *pblock));
    1636        8909 :             } else if (inv.IsMsgWitnessBlk()) {
    1637         286 :                 connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::BLOCK, *pblock));
    1638         488 :             } else if (inv.IsMsgFilteredBlk()) {
    1639             :                 bool sendMerkleBlock = false;
    1640           7 :                 CMerkleBlock merkleBlock;
    1641           7 :                 if (pfrom.m_tx_relay != nullptr) {
    1642           7 :                     LOCK(pfrom.m_tx_relay->cs_filter);
    1643           7 :                     if (pfrom.m_tx_relay->pfilter) {
    1644             :                         sendMerkleBlock = true;
    1645           5 :                         merkleBlock = CMerkleBlock(*pblock, *pfrom.m_tx_relay->pfilter);
    1646           5 :                     }
    1647           7 :                 }
    1648           7 :                 if (sendMerkleBlock) {
    1649           5 :                     connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::MERKLEBLOCK, merkleBlock));
    1650             :                     // CMerkleBlock just contains hashes, so also push any transactions in the block the client did not see
    1651             :                     // This avoids hurting performance by pointlessly requiring a round-trip
    1652             :                     // Note that there is currently no way for a node to request any single transactions we didn't send here -
    1653             :                     // they must either disconnect and retry or request the full block.
    1654             :                     // Thus, the protocol spec specified allows for us to provide duplicate txn here,
    1655             :                     // however we MUST always provide at least what the remote peer needs
    1656             :                     typedef std::pair<unsigned int, uint256> PairType;
    1657           8 :                     for (PairType& pair : merkleBlock.vMatchedTxn)
    1658           3 :                         connman.PushMessage(&pfrom, msgMaker.Make(SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::TX, *pblock->vtx[pair.first]));
    1659           5 :                 }
    1660             :                 // else
    1661             :                     // no response
    1662         202 :             } else if (inv.IsMsgCmpctBlk()) {
    1663             :                 // If a peer is asking for old blocks, we're almost guaranteed
    1664             :                 // they won't have a useful mempool to match against a compact block,
    1665             :                 // and we don't feel like constructing the object for them, so
    1666             :                 // instead we respond with the full, non-compact block.
    1667         195 :                 bool fPeerWantsWitness = State(pfrom.GetId())->fWantsCmpctWitness;
    1668         195 :                 int nSendFlags = fPeerWantsWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
    1669         195 :                 if (CanDirectFetch(consensusParams) && pindex->nHeight >= ::ChainActive().Height() - MAX_CMPCTBLOCK_DEPTH) {
    1670         171 :                     if ((fPeerWantsWitness || !fWitnessesPresentInARecentCompactBlock) && a_recent_compact_block && a_recent_compact_block->header.GetHash() == pindex->GetBlockHash()) {
    1671          83 :                         connman.PushMessage(&pfrom, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, *a_recent_compact_block));
    1672          83 :                     } else {
    1673          88 :                         CBlockHeaderAndShortTxIDs cmpctblock(*pblock, fPeerWantsWitness);
    1674          88 :                         connman.PushMessage(&pfrom, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
    1675          88 :                     }
    1676             :                 } else {
    1677          24 :                     connman.PushMessage(&pfrom, msgMaker.Make(nSendFlags, NetMsgType::BLOCK, *pblock));
    1678             :                 }
    1679         195 :             }
    1680             :         }
    1681             : 
    1682             :         // Trigger the peer node to send a getblocks request for the next batch of inventory
    1683       15122 :         if (inv.hash == pfrom.hashContinue)
    1684             :         {
    1685             :             // Send immediately. This must send even if redundant,
    1686             :             // and we want it right after the last block so they don't
    1687             :             // wait for other stuff first.
    1688           0 :             std::vector<CInv> vInv;
    1689           0 :             vInv.push_back(CInv(MSG_BLOCK, ::ChainActive().Tip()->GetBlockHash()));
    1690           0 :             connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::INV, vInv));
    1691           0 :             pfrom.hashContinue.SetNull();
    1692           0 :         }
    1693       15122 :     }
    1694       15127 : }
    1695             : 
    1696             : //! Determine whether or not a peer can request a transaction, and return it (or nullptr if not found or not allowed).
    1697        9579 : static CTransactionRef FindTxForGetData(const CTxMemPool& mempool, const CNode& peer, const GenTxid& gtxid, const std::chrono::seconds mempool_req, const std::chrono::seconds now) LOCKS_EXCLUDED(cs_main)
    1698             : {
    1699        9579 :     auto txinfo = mempool.info(gtxid);
    1700        9579 :     if (txinfo.tx) {
    1701             :         // If a TX could have been INVed in reply to a MEMPOOL request,
    1702             :         // or is older than UNCONDITIONAL_RELAY_DELAY, permit the request
    1703             :         // unconditionally.
    1704        9518 :         if ((mempool_req.count() && txinfo.m_time <= mempool_req) || txinfo.m_time <= now - UNCONDITIONAL_RELAY_DELAY) {
    1705           2 :             return std::move(txinfo.tx);
    1706             :         }
    1707             :     }
    1708             : 
    1709             :     {
    1710        9577 :         LOCK(cs_main);
    1711             :         // Otherwise, the transaction must have been announced recently.
    1712        9577 :         if (State(peer.GetId())->m_recently_announced_invs.contains(gtxid.GetHash())) {
    1713             :             // If it was, it can be relayed from either the mempool...
    1714        9575 :             if (txinfo.tx) return std::move(txinfo.tx);
    1715             :             // ... or the relay pool.
    1716          61 :             auto mi = mapRelay.find(gtxid.GetHash());
    1717          61 :             if (mi != mapRelay.end()) return mi->second;
    1718          61 :         }
    1719        9577 :     }
    1720             : 
    1721           2 :     return {};
    1722        9579 : }
    1723             : 
    1724       19733 : void static ProcessGetData(CNode& pfrom, const CChainParams& chainparams, CConnman& connman, CTxMemPool& mempool, const std::atomic<bool>& interruptMsgProc) LOCKS_EXCLUDED(cs_main)
    1725             : {
    1726       19733 :     AssertLockNotHeld(cs_main);
    1727             : 
    1728       19733 :     std::deque<CInv>::iterator it = pfrom.vRecvGetData.begin();
    1729       19733 :     std::vector<CInv> vNotFound;
    1730       19733 :     const CNetMsgMaker msgMaker(pfrom.GetSendVersion());
    1731             : 
    1732       19733 :     const std::chrono::seconds now = GetTime<std::chrono::seconds>();
    1733             :     // Get last mempool request time
    1734       19733 :     const std::chrono::seconds mempool_req = pfrom.m_tx_relay != nullptr ? pfrom.m_tx_relay->m_last_mempool_req.load()
    1735           0 :                                                                           : std::chrono::seconds::min();
    1736             : 
    1737             :     // Process as many TX items from the front of the getdata queue as
    1738             :     // possible, since they're common and it's efficient to batch process
    1739             :     // them.
    1740       29312 :     while (it != pfrom.vRecvGetData.end() && it->IsGenTxMsg()) {
    1741        9580 :         if (interruptMsgProc) return;
    1742             :         // The send buffer provides backpressure. If there's no space in
    1743             :         // the buffer, pause processing until the next call.
    1744        9580 :         if (pfrom.fPauseSend) break;
    1745             : 
    1746        9579 :         const CInv &inv = *it++;
    1747             : 
    1748        9579 :         if (pfrom.m_tx_relay == nullptr) {
    1749             :             // Ignore GETDATA requests for transactions from blocks-only peers.
    1750           0 :             continue;
    1751             :         }
    1752             : 
    1753        9579 :         CTransactionRef tx = FindTxForGetData(mempool, pfrom, ToGenTxid(inv), mempool_req, now);
    1754        9579 :         if (tx) {
    1755             :             // WTX and WITNESS_TX imply we serialize with witness
    1756        9577 :             int nSendFlags = (inv.IsMsgTx() ? SERIALIZE_TRANSACTION_NO_WITNESS : 0);
    1757        9577 :             connman.PushMessage(&pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *tx));
    1758        9577 :             mempool.RemoveUnbroadcastTx(tx->GetHash());
    1759             :             // As we're going to send tx, make sure its unconfirmed parents are made requestable.
    1760        9577 :             std::vector<uint256> parent_ids_to_add;
    1761             :             {
    1762        9577 :                 LOCK(mempool.cs);
    1763        9577 :                 auto txiter = mempool.GetIter(tx->GetHash());
    1764        9577 :                 if (txiter) {
    1765        9514 :                     const CTxMemPoolEntry::Parents& parents = (*txiter)->GetMemPoolParentsConst();
    1766        9514 :                     parent_ids_to_add.reserve(parents.size());
    1767        9693 :                     for (const CTxMemPoolEntry& parent : parents) {
    1768         179 :                         if (parent.GetTime() > now - UNCONDITIONAL_RELAY_DELAY) {
    1769         179 :                             parent_ids_to_add.push_back(parent.GetTx().GetHash());
    1770             :                         }
    1771           0 :                     }
    1772        9514 :                 }
    1773        9577 :             }
    1774        9756 :             for (const uint256& parent_txid : parent_ids_to_add) {
    1775             :                 // Relaying a transaction with a recent but unconfirmed parent.
    1776         358 :                 if (WITH_LOCK(pfrom.m_tx_relay->cs_tx_inventory, return !pfrom.m_tx_relay->filterInventoryKnown.contains(parent_txid))) {
    1777           0 :                     LOCK(cs_main);
    1778           0 :                     State(pfrom.GetId())->m_recently_announced_invs.insert(parent_txid);
    1779           0 :                 }
    1780             :             }
    1781        9577 :         } else {
    1782           2 :             vNotFound.push_back(inv);
    1783             :         }
    1784        9579 :     }
    1785             : 
    1786             :     // Only process one BLOCK item per call, since they're uncommon and can be
    1787             :     // expensive to process.
    1788       19733 :     if (it != pfrom.vRecvGetData.end() && !pfrom.fPauseSend) {
    1789       15128 :         const CInv &inv = *it++;
    1790       15128 :         if (inv.IsGenBlkMsg()) {
    1791       15127 :             ProcessGetBlockData(pfrom, chainparams, inv, connman);
    1792             :         }
    1793             :         // else: If the first item on the queue is an unknown type, we erase it
    1794             :         // and continue processing the queue on the next call.
    1795       15128 :     }
    1796             : 
    1797       19733 :     pfrom.vRecvGetData.erase(pfrom.vRecvGetData.begin(), it);
    1798             : 
    1799       19733 :     if (!vNotFound.empty()) {
    1800             :         // Let the peer know that we didn't find what it asked for, so it doesn't
    1801             :         // have to wait around forever.
    1802             :         // SPV clients care about this message: it's needed when they are
    1803             :         // recursively walking the dependencies of relevant unconfirmed
    1804             :         // transactions. SPV clients want to do that because they want to know
    1805             :         // about (and store and rebroadcast and risk analyze) the dependencies
    1806             :         // of transactions relevant to them, without having to download the
    1807             :         // entire memory pool.
    1808             :         // Also, other nodes can use these messages to automatically request a
    1809             :         // transaction from some other peer that annnounced it, and stop
    1810             :         // waiting for us to respond.
    1811             :         // In normal operation, we often send NOTFOUND messages for parents of
    1812             :         // transactions that we relay; if a peer is missing a parent, they may
    1813             :         // assume we have them and request the parents from us.
    1814           2 :         connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::NOTFOUND, vNotFound));
    1815           2 :     }
    1816       19733 : }
    1817             : 
    1818       12486 : static uint32_t GetFetchFlags(const CNode& pfrom) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
    1819             :     uint32_t nFetchFlags = 0;
    1820       12486 :     if ((pfrom.GetLocalServices() & NODE_WITNESS) && State(pfrom.GetId())->fHaveWitness) {
    1821             :         nFetchFlags |= MSG_WITNESS_FLAG;
    1822       12337 :     }
    1823       12486 :     return nFetchFlags;
    1824             : }
    1825             : 
    1826        2239 : void PeerManager::SendBlockTransactions(CNode& pfrom, const CBlock& block, const BlockTransactionsRequest& req) {
    1827        2239 :     BlockTransactions resp(req);
    1828        5843 :     for (size_t i = 0; i < req.indexes.size(); i++) {
    1829        3604 :         if (req.indexes[i] >= block.vtx.size()) {
    1830           0 :             Misbehaving(pfrom.GetId(), 100, "getblocktxn with out-of-bounds tx indices");
    1831           0 :             return;
    1832             :         }
    1833        3604 :         resp.txn[i] = block.vtx[req.indexes[i]];
    1834             :     }
    1835        2239 :     LOCK(cs_main);
    1836        2239 :     const CNetMsgMaker msgMaker(pfrom.GetSendVersion());
    1837        2239 :     int nSendFlags = State(pfrom.GetId())->fWantsCmpctWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
    1838        2239 :     m_connman.PushMessage(&pfrom, msgMaker.Make(nSendFlags, NetMsgType::BLOCKTXN, resp));
    1839        2239 : }
    1840             : 
    1841        5919 : void PeerManager::ProcessHeadersMessage(CNode& pfrom, const std::vector<CBlockHeader>& headers, bool via_compact_block)
    1842             : {
    1843        5919 :     const CNetMsgMaker msgMaker(pfrom.GetSendVersion());
    1844        5919 :     size_t nCount = headers.size();
    1845             : 
    1846        5919 :     if (nCount == 0) {
    1847             :         // Nothing interesting. Stop asking this peers for more headers.
    1848          21 :         return;
    1849             :     }
    1850             : 
    1851        5898 :     bool received_new_header = false;
    1852        5898 :     const CBlockIndex *pindexLast = nullptr;
    1853             :     {
    1854        5898 :         LOCK(cs_main);
    1855        5898 :         CNodeState *nodestate = State(pfrom.GetId());
    1856             : 
    1857             :         // If this looks like it could be a block announcement (nCount <
    1858             :         // MAX_BLOCKS_TO_ANNOUNCE), use special logic for handling headers that
    1859             :         // don't connect:
    1860             :         // - Send a getheaders message in response to try to connect the chain.
    1861             :         // - The peer can send up to MAX_UNCONNECTING_HEADERS in a row that
    1862             :         //   don't connect before giving DoS points
    1863             :         // - Once a headers message is received that is valid and does connect,
    1864             :         //   nUnconnectingHeaders gets reset back to 0.
    1865        5898 :         if (!LookupBlockIndex(headers[0].hashPrevBlock) && nCount < MAX_BLOCKS_TO_ANNOUNCE) {
    1866          69 :             nodestate->nUnconnectingHeaders++;
    1867          69 :             m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETHEADERS, ::ChainActive().GetLocator(pindexBestHeader), uint256()));
    1868          69 :             LogPrint(BCLog::NET, "received header %s: missing prev block %s, sending getheaders (%d) to end (peer=%d, nUnconnectingHeaders=%d)\n",
    1869             :                     headers[0].GetHash().ToString(),
    1870             :                     headers[0].hashPrevBlock.ToString(),
    1871             :                     pindexBestHeader->nHeight,
    1872             :                     pfrom.GetId(), nodestate->nUnconnectingHeaders);
    1873             :             // Set hashLastUnknownBlock for this peer, so that if we
    1874             :             // eventually get the headers - even from a different peer -
    1875             :             // we can use this peer to download.
    1876          69 :             UpdateBlockAvailability(pfrom.GetId(), headers.back().GetHash());
    1877             : 
    1878          69 :             if (nodestate->nUnconnectingHeaders % MAX_UNCONNECTING_HEADERS == 0) {
    1879           5 :                 Misbehaving(pfrom.GetId(), 20, strprintf("%d non-connecting headers", nodestate->nUnconnectingHeaders));
    1880           5 :             }
    1881          69 :             return;
    1882             :         }
    1883             : 
    1884        5829 :         uint256 hashLastBlock;
    1885      184331 :         for (const CBlockHeader& header : headers) {
    1886      178502 :             if (!hashLastBlock.IsNull() && header.hashPrevBlock != hashLastBlock) {
    1887           0 :                 Misbehaving(pfrom.GetId(), 20, "non-continuous headers sequence");
    1888           0 :                 return;
    1889             :             }
    1890      178502 :             hashLastBlock = header.GetHash();
    1891      178502 :         }
    1892             : 
    1893             :         // If we don't have the last header, then they'll have given us
    1894             :         // something new (if these headers are valid).
    1895        5829 :         if (!LookupBlockIndex(hashLastBlock)) {
    1896             :             received_new_header = true;
    1897        2305 :         }
    1898        5898 :     }
    1899             : 
    1900        5829 :     BlockValidationState state;
    1901        5829 :     if (!m_chainman.ProcessNewBlockHeaders(headers, state, m_chainparams, &pindexLast)) {
    1902          21 :         if (state.IsInvalid()) {
    1903          21 :             MaybePunishNodeForBlock(pfrom.GetId(), state, via_compact_block, "invalid header received");
    1904          21 :             return;
    1905             :         }
    1906             :     }
    1907             : 
    1908             :     {
    1909        5808 :         LOCK(cs_main);
    1910        5808 :         CNodeState *nodestate = State(pfrom.GetId());
    1911        5808 :         if (nodestate->nUnconnectingHeaders > 0) {
    1912          11 :             LogPrint(BCLog::NET, "peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n", pfrom.GetId(), nodestate->nUnconnectingHeaders);
    1913             :         }
    1914        5808 :         nodestate->nUnconnectingHeaders = 0;
    1915             : 
    1916        5808 :         assert(pindexLast);
    1917        5808 :         UpdateBlockAvailability(pfrom.GetId(), pindexLast->GetBlockHash());
    1918             : 
    1919             :         // From here, pindexBestKnownBlock should be guaranteed to be non-null,
    1920             :         // because it is set in UpdateBlockAvailability. Some nullptr checks
    1921             :         // are still present, however, as belt-and-suspenders.
    1922             : 
    1923        5808 :         if (received_new_header && pindexLast->nChainWork > ::ChainActive().Tip()->nChainWork) {
    1924        2248 :             nodestate->m_last_block_announcement = GetTime();
    1925        2248 :         }
    1926             : 
    1927        5808 :         if (nCount == MAX_HEADERS_RESULTS) {
    1928             :             // Headers message had its maximum size; the peer may have more headers.
    1929             :             // TODO: optimize: if pindexLast is an ancestor of ::ChainActive().Tip or pindexBestHeader, continue
    1930             :             // from there instead.
    1931           2 :             LogPrint(BCLog::NET, "more getheaders (%d) to end to peer=%d (startheight:%d)\n", pindexLast->nHeight, pfrom.GetId(), pfrom.nStartingHeight);
    1932           2 :             m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETHEADERS, ::ChainActive().GetLocator(pindexLast), uint256()));
    1933           2 :         }
    1934             : 
    1935        5808 :         bool fCanDirectFetch = CanDirectFetch(m_chainparams.GetConsensus());
    1936             :         // If this set of headers is valid and ends in a block with at least as
    1937             :         // much work as our tip, download as much as possible.
    1938        5808 :         if (fCanDirectFetch && pindexLast->IsValid(BLOCK_VALID_TREE) && ::ChainActive().Tip()->nChainWork <= pindexLast->nChainWork) {
    1939        4563 :             std::vector<const CBlockIndex*> vToFetch;
    1940        4563 :             const CBlockIndex *pindexWalk = pindexLast;
    1941             :             // Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
    1942       41622 :             while (pindexWalk && !::ChainActive().Contains(pindexWalk) && vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
    1943       70793 :                 if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
    1944       33734 :                         !mapBlocksInFlight.count(pindexWalk->GetBlockHash()) &&
    1945       15360 :                         (!IsWitnessEnabled(pindexWalk->pprev, m_chainparams.GetConsensus()) || State(pfrom.GetId())->fHaveWitness)) {
    1946             :                     // We don't have this block, and it's not yet in flight.
    1947       15359 :                     vToFetch.push_back(pindexWalk);
    1948             :                 }
    1949       37059 :                 pindexWalk = pindexWalk->pprev;
    1950             :             }
    1951             :             // If pindexWalk still isn't on our main chain, we're looking at a
    1952             :             // very large reorg at a time we think we're close to caught up to
    1953             :             // the main chain -- this shouldn't really happen.  Bail out on the
    1954             :             // direct fetch and rely on parallel download instead.
    1955        4563 :             if (!::ChainActive().Contains(pindexWalk)) {
    1956         719 :                 LogPrint(BCLog::NET, "Large reorg, won't direct fetch to %s (%d)\n",
    1957             :                         pindexLast->GetBlockHash().ToString(),
    1958             :                         pindexLast->nHeight);
    1959             :             } else {
    1960        3844 :                 std::vector<CInv> vGetData;
    1961             :                 // Download as much as possible, from earliest to latest.
    1962        6466 :                 for (const CBlockIndex *pindex : reverse_iterate(vToFetch)) {
    1963        2622 :                     if (nodestate->nBlocksInFlight >= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
    1964             :                         // Can't download any more from this peer
    1965         137 :                         break;
    1966             :                     }
    1967        2485 :                     uint32_t nFetchFlags = GetFetchFlags(pfrom);
    1968        2485 :                     vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
    1969        2485 :                     MarkBlockAsInFlight(m_mempool, pfrom.GetId(), pindex->GetBlockHash(), pindex);
    1970        2485 :                     LogPrint(BCLog::NET, "Requesting block %s from  peer=%d\n",
    1971             :                             pindex->GetBlockHash().ToString(), pfrom.GetId());
    1972        2485 :                 }
    1973        3844 :                 if (vGetData.size() > 1) {
    1974         247 :                     LogPrint(BCLog::NET, "Downloading blocks toward %s (%d) via headers direct fetch\n",
    1975             :                             pindexLast->GetBlockHash().ToString(), pindexLast->nHeight);
    1976             :                 }
    1977        3844 :                 if (vGetData.size() > 0) {
    1978        1946 :                     if (nodestate->fSupportsDesiredCmpctVersion && vGetData.size() == 1 && mapBlocksInFlight.size() == 1 && pindexLast->pprev->IsValid(BLOCK_VALID_CHAIN)) {
    1979             :                         // In any case, we want to download using a compact block, not a regular one
    1980         191 :                         vGetData[0] = CInv(MSG_CMPCT_BLOCK, vGetData[0].hash);
    1981         191 :                     }
    1982        1946 :                     m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, vGetData));
    1983        1946 :                 }
    1984        3844 :             }
    1985        4563 :         }
    1986             :         // If we're in IBD, we want outbound peers that will serve us a useful
    1987             :         // chain. Disconnect peers that are on chains with insufficient work.
    1988        5808 :         if (::ChainstateActive().IsInitialBlockDownload() && nCount != MAX_HEADERS_RESULTS) {
    1989             :             // When nCount < MAX_HEADERS_RESULTS, we know we have no more
    1990             :             // headers to fetch from this peer.
    1991         375 :             if (nodestate->pindexBestKnownBlock && nodestate->pindexBestKnownBlock->nChainWork < nMinimumChainWork) {
    1992             :                 // This peer has too little work on their headers chain to help
    1993             :                 // us sync -- disconnect if it is an outbound disconnection
    1994             :                 // candidate.
    1995             :                 // Note: We compare their tip to nMinimumChainWork (rather than
    1996             :                 // ::ChainActive().Tip()) because we won't start block download
    1997             :                 // until we have a headers chain that has at least
    1998             :                 // nMinimumChainWork, even if a peer has a chain past our tip,
    1999             :                 // as an anti-DoS measure.
    2000          49 :                 if (pfrom.IsOutboundOrBlockRelayConn()) {
    2001           0 :                     LogPrintf("Disconnecting outbound peer %d -- headers chain has insufficient work\n", pfrom.GetId());
    2002           0 :                     pfrom.fDisconnect = true;
    2003           0 :                 }
    2004             :             }
    2005             :         }
    2006             : 
    2007        5808 :         if (!pfrom.fDisconnect && pfrom.IsFullOutboundConn() && nodestate->pindexBestKnownBlock != nullptr) {
    2008             :             // If this is an outbound full-relay peer, check to see if we should protect
    2009             :             // it from the bad/lagging chain logic.
    2010             :             // Note that block-relay-only peers are already implicitly protected, so we
    2011             :             // only consider setting m_protect for the full-relay peers.
    2012           0 :             if (g_outbound_peers_with_protect_from_disconnect < MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT && nodestate->pindexBestKnownBlock->nChainWork >= ::ChainActive().Tip()->nChainWork && !nodestate->m_chain_sync.m_protect) {
    2013           0 :                 LogPrint(BCLog::NET, "Protecting outbound peer=%d from eviction\n", pfrom.GetId());
    2014           0 :                 nodestate->m_chain_sync.m_protect = true;
    2015           0 :                 ++g_outbound_peers_with_protect_from_disconnect;
    2016           0 :             }
    2017             :         }
    2018        5808 :     }
    2019             : 
    2020        5808 :     return;
    2021        5919 : }
    2022             : 
    2023        9363 : void PeerManager::ProcessOrphanTx(std::set<uint256>& orphan_work_set, std::list<CTransactionRef>& removed_txn)
    2024             : {
    2025        9363 :     AssertLockHeld(cs_main);
    2026        9363 :     AssertLockHeld(g_cs_orphans);
    2027        9363 :     std::set<NodeId> setMisbehaving;
    2028        9368 :     bool done = false;
    2029        9368 :     while (!done && !orphan_work_set.empty()) {
    2030           5 :         const uint256 orphanHash = *orphan_work_set.begin();
    2031           5 :         orphan_work_set.erase(orphan_work_set.begin());
    2032             : 
    2033           5 :         auto orphan_it = mapOrphanTransactions.find(orphanHash);
    2034           5 :         if (orphan_it == mapOrphanTransactions.end()) continue;
    2035             : 
    2036           5 :         const CTransactionRef porphanTx = orphan_it->second.tx;
    2037           5 :         const CTransaction& orphanTx = *porphanTx;
    2038           5 :         NodeId fromPeer = orphan_it->second.fromPeer;
    2039             :         // Use a new TxValidationState because orphans come from different peers (and we call
    2040             :         // MaybePunishNodeForTx based on the source peer from the orphan map, not based on the peer
    2041             :         // that relayed the previous transaction).
    2042           5 :         TxValidationState orphan_state;
    2043             : 
    2044           5 :         if (setMisbehaving.count(fromPeer)) continue;
    2045           5 :         if (AcceptToMemoryPool(m_mempool, orphan_state, porphanTx, &removed_txn, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
    2046           3 :             LogPrint(BCLog::MEMPOOL, "   accepted orphan tx %s\n", orphanHash.ToString());
    2047           3 :             RelayTransaction(orphanHash, porphanTx->GetWitnessHash(), m_connman);
    2048           8 :             for (unsigned int i = 0; i < orphanTx.vout.size(); i++) {
    2049           5 :                 auto it_by_prev = mapOrphanTransactionsByPrev.find(COutPoint(orphanHash, i));
    2050           5 :                 if (it_by_prev != mapOrphanTransactionsByPrev.end()) {
    2051           6 :                     for (const auto& elem : it_by_prev->second) {
    2052           3 :                         orphan_work_set.insert(elem->first);
    2053           0 :                     }
    2054           3 :                 }
    2055           5 :             }
    2056           3 :             EraseOrphanTx(orphanHash);
    2057             :             done = true;
    2058           5 :         } else if (orphan_state.GetResult() != TxValidationResult::TX_MISSING_INPUTS) {
    2059           2 :             if (orphan_state.IsInvalid()) {
    2060             :                 // Punish peer that gave us an invalid orphan tx
    2061           2 :                 if (MaybePunishNodeForTx(fromPeer, orphan_state)) {
    2062           1 :                     setMisbehaving.insert(fromPeer);
    2063           1 :                 }
    2064           2 :                 LogPrint(BCLog::MEMPOOL, "   invalid orphan tx %s from peer=%d. %s\n",
    2065             :                     orphanHash.ToString(),
    2066             :                     fromPeer,
    2067             :                     orphan_state.ToString());
    2068             :             }
    2069             :             // Has inputs but not accepted to mempool
    2070             :             // Probably non-standard or insufficient fee
    2071           2 :             LogPrint(BCLog::MEMPOOL, "   removed orphan tx %s\n", orphanHash.ToString());
    2072           2 :             if (orphan_state.GetResult() != TxValidationResult::TX_WITNESS_STRIPPED) {
    2073             :                 // We can add the wtxid of this transaction to our reject filter.
    2074             :                 // Do not add txids of witness transactions or witness-stripped
    2075             :                 // transactions to the filter, as they can have been malleated;
    2076             :                 // adding such txids to the reject filter would potentially
    2077             :                 // interfere with relay of valid transactions from peers that
    2078             :                 // do not support wtxid-based relay. See
    2079             :                 // https://github.com/bitcoin/bitcoin/issues/8279 for details.
    2080             :                 // We can remove this restriction (and always add wtxids to
    2081             :                 // the filter even for witness stripped transactions) once
    2082             :                 // wtxid-based relay is broadly deployed.
    2083             :                 // See also comments in https://github.com/bitcoin/bitcoin/pull/18044#discussion_r443419034
    2084             :                 // for concerns around weakening security of unupgraded nodes
    2085             :                 // if we start doing this too early.
    2086           2 :                 assert(recentRejects);
    2087           2 :                 recentRejects->insert(orphanTx.GetWitnessHash());
    2088             :                 // If the transaction failed for TX_INPUTS_NOT_STANDARD,
    2089             :                 // then we know that the witness was irrelevant to the policy
    2090             :                 // failure, since this check depends only on the txid
    2091             :                 // (the scriptPubKey being spent is covered by the txid).
    2092             :                 // Add the txid to the reject filter to prevent repeated
    2093             :                 // processing of this transaction in the event that child
    2094             :                 // transactions are later received (resulting in
    2095             :                 // parent-fetching by txid via the orphan-handling logic).
    2096           2 :                 if (orphan_state.GetResult() == TxValidationResult::TX_INPUTS_NOT_STANDARD && orphanTx.GetWitnessHash() != orphanTx.GetHash()) {
    2097             :                     // We only add the txid if it differs from the wtxid, to
    2098             :                     // avoid wasting entries in the rolling bloom filter.
    2099           0 :                     recentRejects->insert(orphanTx.GetHash());
    2100             :                 }
    2101             :             }
    2102           2 :             EraseOrphanTx(orphanHash);
    2103             :             done = true;
    2104           2 :         }
    2105           5 :         m_mempool.check(&::ChainstateActive().CoinsTip());
    2106           5 :     }
    2107        9363 : }
    2108             : 
    2109             : /**
    2110             :  * Validation logic for compact filters request handling.
    2111             :  *
    2112             :  * May disconnect from the peer in the case of a bad request.
    2113             :  *
    2114             :  * @param[in]   peer            The peer that we received the request from
    2115             :  * @param[in]   chain_params    Chain parameters
    2116             :  * @param[in]   filter_type     The filter type the request is for. Must be basic filters.
    2117             :  * @param[in]   start_height    The start height for the request
    2118             :  * @param[in]   stop_hash       The stop_hash for the request
    2119             :  * @param[in]   max_height_diff The maximum number of items permitted to request, as specified in BIP 157
    2120             :  * @param[out]  stop_index      The CBlockIndex for the stop_hash block, if the request can be serviced.
    2121             :  * @param[out]  filter_index    The filter index, if the request can be serviced.
    2122             :  * @return                      True if the request can be serviced.
    2123             :  */
    2124          14 : static bool PrepareBlockFilterRequest(CNode& peer, const CChainParams& chain_params,
    2125             :                                       BlockFilterType filter_type, uint32_t start_height,
    2126             :                                       const uint256& stop_hash, uint32_t max_height_diff,
    2127             :                                       const CBlockIndex*& stop_index,
    2128             :                                       BlockFilterIndex*& filter_index)
    2129             : {
    2130          14 :     const bool supported_filter_type =
    2131          14 :         (filter_type == BlockFilterType::BASIC &&
    2132          13 :          (peer.GetLocalServices() & NODE_COMPACT_FILTERS));
    2133          14 :     if (!supported_filter_type) {
    2134           4 :         LogPrint(BCLog::NET, "peer %d requested unsupported block filter type: %d\n",
    2135             :                  peer.GetId(), static_cast<uint8_t>(filter_type));
    2136           4 :         peer.fDisconnect = true;
    2137           4 :         return false;
    2138             :     }
    2139             : 
    2140             :     {
    2141          10 :         LOCK(cs_main);
    2142          10 :         stop_index = LookupBlockIndex(stop_hash);
    2143             : 
    2144             :         // Check that the stop block exists and the peer would be allowed to fetch it.
    2145          10 :         if (!stop_index || !BlockRequestAllowed(stop_index, chain_params.GetConsensus())) {
    2146           1 :             LogPrint(BCLog::NET, "peer %d requested invalid block hash: %s\n",
    2147             :                      peer.GetId(), stop_hash.ToString());
    2148           1 :             peer.fDisconnect = true;
    2149           1 :             return false;
    2150             :         }
    2151          10 :     }
    2152             : 
    2153           9 :     uint32_t stop_height = stop_index->nHeight;
    2154           9 :     if (start_height > stop_height) {
    2155           0 :         LogPrint(BCLog::NET, "peer %d sent invalid getcfilters/getcfheaders with " /* Continued */
    2156             :                  "start height %d and stop height %d\n",
    2157             :                  peer.GetId(), start_height, stop_height);
    2158           0 :         peer.fDisconnect = true;
    2159           0 :         return false;
    2160             :     }
    2161           9 :     if (stop_height - start_height >= max_height_diff) {
    2162           2 :         LogPrint(BCLog::NET, "peer %d requested too many cfilters/cfheaders: %d / %d\n",
    2163             :                  peer.GetId(), stop_height - start_height + 1, max_height_diff);
    2164           2 :         peer.fDisconnect = true;
    2165           2 :         return false;
    2166             :     }
    2167             : 
    2168           7 :     filter_index = GetBlockFilterIndex(filter_type);
    2169           7 :     if (!filter_index) {
    2170           0 :         LogPrint(BCLog::NET, "Filter index for supported type %s not found\n", BlockFilterTypeName(filter_type));
    2171           0 :         return false;
    2172             :     }
    2173             : 
    2174           7 :     return true;
    2175          14 : }
    2176             : 
    2177             : /**
    2178             :  * Handle a cfilters request.
    2179             :  *
    2180             :  * May disconnect from the peer in the case of a bad request.
    2181             :  *
    2182             :  * @param[in]   peer            The peer that we received the request from
    2183             :  * @param[in]   vRecv           The raw message received
    2184             :  * @param[in]   chain_params    Chain parameters
    2185             :  * @param[in]   connman         Pointer to the connection manager
    2186             :  */
    2187           4 : static void ProcessGetCFilters(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
    2188             :                                CConnman& connman)
    2189             : {
    2190           4 :     uint8_t filter_type_ser;
    2191           4 :     uint32_t start_height;
    2192           4 :     uint256 stop_hash;
    2193             : 
    2194           4 :     vRecv >> filter_type_ser >> start_height >> stop_hash;
    2195             : 
    2196           4 :     const BlockFilterType filter_type = static_cast<BlockFilterType>(filter_type_ser);
    2197             : 
    2198           4 :     const CBlockIndex* stop_index;
    2199           4 :     BlockFilterIndex* filter_index;
    2200           4 :     if (!PrepareBlockFilterRequest(peer, chain_params, filter_type, start_height, stop_hash,
    2201             :                                    MAX_GETCFILTERS_SIZE, stop_index, filter_index)) {
    2202           2 :         return;
    2203             :     }
    2204             : 
    2205           2 :     std::vector<BlockFilter> filters;
    2206           2 :     if (!filter_index->LookupFilterRange(start_height, stop_index, filters)) {
    2207           0 :         LogPrint(BCLog::NET, "Failed to find block filter in index: filter_type=%s, start_height=%d, stop_hash=%s\n",
    2208             :                      BlockFilterTypeName(filter_type), start_height, stop_hash.ToString());
    2209           0 :         return;
    2210             :     }
    2211             : 
    2212          13 :     for (const auto& filter : filters) {
    2213          22 :         CSerializedNetMsg msg = CNetMsgMaker(peer.GetSendVersion())
    2214          11 :             .Make(NetMsgType::CFILTER, filter);
    2215          11 :         connman.PushMessage(&peer, std::move(msg));
    2216          11 :     }
    2217           4 : }
    2218             : 
    2219             : /**
    2220             :  * Handle a cfheaders request.
    2221             :  *
    2222             :  * May disconnect from the peer in the case of a bad request.
    2223             :  *
    2224             :  * @param[in]   peer            The peer that we received the request from
    2225             :  * @param[in]   vRecv           The raw message received
    2226             :  * @param[in]   chain_params    Chain parameters
    2227             :  * @param[in]   connman         Pointer to the connection manager
    2228             :  */
    2229           4 : static void ProcessGetCFHeaders(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
    2230             :                                 CConnman& connman)
    2231             : {
    2232           4 :     uint8_t filter_type_ser;
    2233           4 :     uint32_t start_height;
    2234           4 :     uint256 stop_hash;
    2235             : 
    2236           4 :     vRecv >> filter_type_ser >> start_height >> stop_hash;
    2237             : 
    2238           4 :     const BlockFilterType filter_type = static_cast<BlockFilterType>(filter_type_ser);
    2239             : 
    2240           4 :     const CBlockIndex* stop_index;
    2241           4 :     BlockFilterIndex* filter_index;
    2242           4 :     if (!PrepareBlockFilterRequest(peer, chain_params, filter_type, start_height, stop_hash,
    2243             :                                    MAX_GETCFHEADERS_SIZE, stop_index, filter_index)) {
    2244           2 :         return;
    2245             :     }
    2246             : 
    2247           2 :     uint256 prev_header;
    2248           2 :     if (start_height > 0) {
    2249             :         const CBlockIndex* const prev_block =
    2250           2 :             stop_index->GetAncestor(static_cast<int>(start_height - 1));
    2251           2 :         if (!filter_index->LookupFilterHeader(prev_block, prev_header)) {
    2252           0 :             LogPrint(BCLog::NET, "Failed to find block filter header in index: filter_type=%s, block_hash=%s\n",
    2253             :                          BlockFilterTypeName(filter_type), prev_block->GetBlockHash().ToString());
    2254           0 :             return;
    2255             :         }
    2256           2 :     }
    2257             : 
    2258           2 :     std::vector<uint256> filter_hashes;
    2259           2 :     if (!filter_index->LookupFilterHashRange(start_height, stop_index, filter_hashes)) {
    2260           0 :         LogPrint(BCLog::NET, "Failed to find block filter hashes in index: filter_type=%s, start_height=%d, stop_hash=%s\n",
    2261             :                      BlockFilterTypeName(filter_type), start_height, stop_hash.ToString());
    2262           0 :         return;
    2263             :     }
    2264             : 
    2265           4 :     CSerializedNetMsg msg = CNetMsgMaker(peer.GetSendVersion())
    2266           2 :         .Make(NetMsgType::CFHEADERS,
    2267             :               filter_type_ser,
    2268           2 :               stop_index->GetBlockHash(),
    2269             :               prev_header,
    2270             :               filter_hashes);
    2271           2 :     connman.PushMessage(&peer, std::move(msg));
    2272           4 : }
    2273             : 
    2274             : /**
    2275             :  * Handle a getcfcheckpt request.
    2276             :  *
    2277             :  * May disconnect from the peer in the case of a bad request.
    2278             :  *
    2279             :  * @param[in]   peer            The peer that we received the request from
    2280             :  * @param[in]   vRecv           The raw message received
    2281             :  * @param[in]   chain_params    Chain parameters
    2282             :  * @param[in]   connman         Pointer to the connection manager
    2283             :  */
    2284           6 : static void ProcessGetCFCheckPt(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
    2285             :                                 CConnman& connman)
    2286             : {
    2287           6 :     uint8_t filter_type_ser;
    2288           6 :     uint256 stop_hash;
    2289             : 
    2290           6 :     vRecv >> filter_type_ser >> stop_hash;
    2291             : 
    2292           6 :     const BlockFilterType filter_type = static_cast<BlockFilterType>(filter_type_ser);
    2293             : 
    2294           6 :     const CBlockIndex* stop_index;
    2295           6 :     BlockFilterIndex* filter_index;
    2296           6 :     if (!PrepareBlockFilterRequest(peer, chain_params, filter_type, /*start_height=*/0, stop_hash,
    2297           6 :                                    /*max_height_diff=*/std::numeric_limits<uint32_t>::max(),
    2298             :                                    stop_index, filter_index)) {
    2299           3 :         return;
    2300             :     }
    2301             : 
    2302           3 :     std::vector<uint256> headers(stop_index->nHeight / CFCHECKPT_INTERVAL);
    2303             : 
    2304             :     // Populate headers.
    2305           3 :     const CBlockIndex* block_index = stop_index;
    2306           7 :     for (int i = headers.size() - 1; i >= 0; i--) {
    2307           4 :         int height = (i + 1) * CFCHECKPT_INTERVAL;
    2308           4 :         block_index = block_index->GetAncestor(height);
    2309             : 
    2310           4 :         if (!filter_index->LookupFilterHeader(block_index, headers[i])) {
    2311           0 :             LogPrint(BCLog::NET, "Failed to find block filter header in index: filter_type=%s, block_hash=%s\n",
    2312             :                          BlockFilterTypeName(filter_type), block_index->GetBlockHash().ToString());
    2313           0 :             return;
    2314             :         }
    2315           4 :     }
    2316             : 
    2317           6 :     CSerializedNetMsg msg = CNetMsgMaker(peer.GetSendVersion())
    2318           3 :         .Make(NetMsgType::CFCHECKPT,
    2319             :               filter_type_ser,
    2320           3 :               stop_index->GetBlockHash(),
    2321             :               headers);
    2322           3 :     connman.PushMessage(&peer, std::move(msg));
    2323           6 : }
    2324             : 
    2325       90591 : void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
    2326             :                                          const std::chrono::microseconds time_received,
    2327             :                                          const std::atomic<bool>& interruptMsgProc)
    2328             : {
    2329       90595 :     LogPrint(BCLog::NET, "received: %s (%u bytes) peer=%d\n", SanitizeString(msg_type), vRecv.size(), pfrom.GetId());
    2330       90591 :     if (gArgs.IsArgSet("-dropmessagestest") && GetRand(gArgs.GetArg("-dropmessagestest", 0)) == 0)
    2331             :     {
    2332           0 :         LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n");
    2333           0 :         return;
    2334             :     }
    2335             : 
    2336             : 
    2337       90591 :     if (msg_type == NetMsgType::VERSION) {
    2338             :         // Each connection can only send one version message
    2339         699 :         if (pfrom.nVersion != 0)
    2340             :         {
    2341           0 :             Misbehaving(pfrom.GetId(), 1, "redundant version message");
    2342           0 :             return;
    2343             :         }
    2344             : 
    2345         699 :         int64_t nTime;
    2346         699 :         CAddress addrMe;
    2347         699 :         CAddress addrFrom;
    2348         699 :         uint64_t nNonce = 1;
    2349         699 :         uint64_t nServiceInt;
    2350         699 :         ServiceFlags nServices;
    2351         699 :         int nVersion;
    2352             :         int nSendVersion;
    2353         699 :         std::string cleanSubVer;
    2354         699 :         int nStartingHeight = -1;
    2355         699 :         bool fRelay = true;
    2356             : 
    2357         699 :         vRecv >> nVersion >> nServiceInt >> nTime >> addrMe;
    2358         699 :         nSendVersion = std::min(nVersion, PROTOCOL_VERSION);
    2359         699 :         nServices = ServiceFlags(nServiceInt);
    2360         699 :         if (!pfrom.IsInboundConn())
    2361             :         {
    2362         238 :             m_connman.SetServices(pfrom.addr, nServices);
    2363             :         }
    2364         699 :         if (pfrom.ExpectServicesFromConn() && !HasAllDesirableServiceFlags(nServices))
    2365             :         {
    2366           0 :             LogPrint(BCLog::NET, "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom.GetId(), nServices, GetDesirableServiceFlags(nServices));
    2367           0 :             pfrom.fDisconnect = true;
    2368           0 :             return;
    2369             :         }
    2370             : 
    2371         699 :         if (nVersion < MIN_PEER_PROTO_VERSION) {
    2372             :             // disconnect from peers older than this proto version
    2373           1 :             LogPrint(BCLog::NET, "peer=%d using obsolete version %i; disconnecting\n", pfrom.GetId(), nVersion);
    2374           1 :             pfrom.fDisconnect = true;
    2375           1 :             return;
    2376             :         }
    2377             : 
    2378         698 :         if (!vRecv.empty())
    2379         698 :             vRecv >> addrFrom >> nNonce;
    2380         698 :         if (!vRecv.empty()) {
    2381         698 :             std::string strSubVer;
    2382         698 :             vRecv >> LIMITED_STRING(strSubVer, MAX_SUBVERSION_LENGTH);
    2383         698 :             cleanSubVer = SanitizeString(strSubVer);
    2384         698 :         }
    2385         698 :         if (!vRecv.empty()) {
    2386         698 :             vRecv >> nStartingHeight;
    2387             :         }
    2388         698 :         if (!vRecv.empty())
    2389         698 :             vRecv >> fRelay;
    2390             :         // Disconnect if we connected to ourself
    2391         698 :         if (pfrom.IsInboundConn() && !m_connman.CheckIncomingNonce(nNonce))
    2392             :         {
    2393           0 :             LogPrintf("connected to self at %s, disconnecting\n", pfrom.addr.ToString());
    2394           0 :             pfrom.fDisconnect = true;
    2395           0 :             return;
    2396             :         }
    2397             : 
    2398         698 :         if (pfrom.IsInboundConn() && addrMe.IsRoutable())
    2399             :         {
    2400           0 :             SeenLocal(addrMe);
    2401             :         }
    2402             : 
    2403             :         // Be shy and don't send version until we hear
    2404         698 :         if (pfrom.IsInboundConn())
    2405         460 :             PushNodeVersion(pfrom, m_connman, GetAdjustedTime());
    2406             : 
    2407         698 :         if (nVersion >= WTXID_RELAY_VERSION) {
    2408         697 :             m_connman.PushMessage(&pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::WTXIDRELAY));
    2409         697 :         }
    2410             : 
    2411         698 :         m_connman.PushMessage(&pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERACK));
    2412             : 
    2413         698 :         pfrom.nServices = nServices;
    2414         698 :         pfrom.SetAddrLocal(addrMe);
    2415             :         {
    2416         698 :             LOCK(pfrom.cs_SubVer);
    2417         698 :             pfrom.cleanSubVer = cleanSubVer;
    2418         698 :         }
    2419         698 :         pfrom.nStartingHeight = nStartingHeight;
    2420             : 
    2421             :         // set nodes not relaying blocks and tx and not serving (parts) of the historical blockchain as "clients"
    2422         698 :         pfrom.fClient = (!(nServices & NODE_NETWORK) && !(nServices & NODE_NETWORK_LIMITED));
    2423             : 
    2424             :         // set nodes not capable of serving the complete blockchain history as "limited nodes"
    2425         698 :         pfrom.m_limited_node = (!(nServices & NODE_NETWORK) && (nServices & NODE_NETWORK_LIMITED));
    2426             : 
    2427         698 :         if (pfrom.m_tx_relay != nullptr) {
    2428         698 :             LOCK(pfrom.m_tx_relay->cs_filter);
    2429         698 :             pfrom.m_tx_relay->fRelayTxes = fRelay; // set to true after we get the first filter* message
    2430         698 :         }
    2431             : 
    2432             :         // Change version
    2433         698 :         pfrom.SetSendVersion(nSendVersion);
    2434         698 :         pfrom.nVersion = nVersion;
    2435             : 
    2436         698 :         if((nServices & NODE_WITNESS))
    2437             :         {
    2438         693 :             LOCK(cs_main);
    2439         693 :             State(pfrom.GetId())->fHaveWitness = true;
    2440         693 :         }
    2441             : 
    2442             :         // Potentially mark this peer as a preferred download peer.
    2443             :         {
    2444         698 :         LOCK(cs_main);
    2445         698 :         UpdatePreferredDownload(pfrom, State(pfrom.GetId()));
    2446         698 :         }
    2447             : 
    2448         698 :         if (!pfrom.IsInboundConn() && !pfrom.IsBlockOnlyConn()) {
    2449             :             // For outbound peers, we try to relay our address (so that other
    2450             :             // nodes can try to find us more quickly, as we have no guarantee
    2451             :             // that an outbound peer is even aware of how to reach us) and do a
    2452             :             // one-time address fetch (to help populate/update our addrman). If
    2453             :             // we're starting up for the first time, our addrman may be pretty
    2454             :             // empty and no one will know who we are, so these mechanisms are
    2455             :             // important to help us connect to the network.
    2456             :             //
    2457             :             // We also update the addrman to record connection success for
    2458             :             // these peers (which include OUTBOUND_FULL_RELAY and FEELER
    2459             :             // connections) so that addrman will have an up-to-date notion of
    2460             :             // which peers are online and available.
    2461             :             //
    2462             :             // We skip these operations for BLOCK_RELAY peers to avoid
    2463             :             // potentially leaking information about our BLOCK_RELAY
    2464             :             // connections via the addrman or address relay.
    2465         238 :             if (fListen && !::ChainstateActive().IsInitialBlockDownload())
    2466             :             {
    2467         153 :                 CAddress addr = GetLocalAddress(&pfrom.addr, pfrom.GetLocalServices());
    2468         153 :                 FastRandomContext insecure_rand;
    2469         153 :                 if (addr.IsRoutable())
    2470             :                 {
    2471           0 :                     LogPrint(BCLog::NET, "ProcessMessages: advertising address %s\n", addr.ToString());
    2472           0 :                     pfrom.PushAddress(addr, insecure_rand);
    2473         153 :                 } else if (IsPeerAddrLocalGood(&pfrom)) {
    2474           0 :                     addr.SetIP(addrMe);
    2475           0 :                     LogPrint(BCLog::NET, "ProcessMessages: advertising address %s\n", addr.ToString());
    2476           0 :                     pfrom.PushAddress(addr, insecure_rand);
    2477             :                 }
    2478         153 :             }
    2479             : 
    2480             :             // Get recent addresses
    2481         238 :             m_connman.PushMessage(&pfrom, CNetMsgMaker(nSendVersion).Make(NetMsgType::GETADDR));
    2482         238 :             pfrom.fGetAddr = true;
    2483             : 
    2484             :             // Moves address from New to Tried table in Addrman, resolves
    2485             :             // tried-table collisions, etc.
    2486         238 :             m_connman.MarkAddressGood(pfrom.addr);
    2487             :         }
    2488             : 
    2489         698 :         std::string remoteAddr;
    2490         698 :         if (fLogIPs)
    2491           2 :             remoteAddr = ", peeraddr=" + pfrom.addr.ToString();
    2492             : 
    2493         698 :         LogPrint(BCLog::NET, "receive version message: %s: version %d, blocks=%d, us=%s, peer=%d%s\n",
    2494             :                   cleanSubVer, pfrom.nVersion,
    2495             :                   pfrom.nStartingHeight, addrMe.ToString(), pfrom.GetId(),
    2496             :                   remoteAddr);
    2497             : 
    2498         698 :         int64_t nTimeOffset = nTime - GetTime();
    2499         698 :         pfrom.nTimeOffset = nTimeOffset;
    2500         698 :         AddTimeData(pfrom.addr, nTimeOffset);
    2501             : 
    2502             :         // If the peer is old enough to have the old alert system, send it the final alert.
    2503         698 :         if (pfrom.nVersion <= 70012) {
    2504           0 :             CDataStream finalAlert(ParseHex("60010000000000000000000000ffffff7f00000000ffffff7ffeffff7f01ffffff7f00000000ffffff7f00ffffff7f002f555247454e543a20416c657274206b657920636f6d70726f6d697365642c2075706772616465207265717569726564004630440220653febd6410f470f6bae11cad19c48413becb1ac2c17f908fd0fd53bdc3abd5202206d0e9c96fe88d4a0f01ed9dedae2b6f9e00da94cad0fecaae66ecf689bf71b50"), SER_NETWORK, PROTOCOL_VERSION);
    2505           0 :             m_connman.PushMessage(&pfrom, CNetMsgMaker(nSendVersion).Make("alert", finalAlert));
    2506           0 :         }
    2507             : 
    2508             :         // Feeler connections exist only to verify if address is online.
    2509         698 :         if (pfrom.IsFeelerConn()) {
    2510           0 :             pfrom.fDisconnect = true;
    2511           0 :         }
    2512             :         return;
    2513         699 :     }
    2514             : 
    2515       89892 :     if (pfrom.nVersion == 0) {
    2516             :         // Must have a version message before anything else
    2517         102 :         Misbehaving(pfrom.GetId(), 1, "non-version message before version handshake");
    2518         102 :         return;
    2519             :     }
    2520             : 
    2521             :     // At this point, the outgoing message serialization version can't change.
    2522       89790 :     const CNetMsgMaker msgMaker(pfrom.GetSendVersion());
    2523             : 
    2524       89790 :     if (msg_type == NetMsgType::VERACK)
    2525             :     {
    2526         697 :         pfrom.SetRecvVersion(std::min(pfrom.nVersion.load(), PROTOCOL_VERSION));
    2527             : 
    2528         697 :         if (!pfrom.IsInboundConn()) {
    2529             :             // Mark this node as currently connected, so we update its timestamp later.
    2530         238 :             LOCK(cs_main);
    2531         238 :             State(pfrom.GetId())->fCurrentlyConnected = true;
    2532         238 :             LogPrintf("New outbound peer connected: version: %d, blocks=%d, peer=%d%s (%s)\n",
    2533         238 :                       pfrom.nVersion.load(), pfrom.nStartingHeight,
    2534         238 :                       pfrom.GetId(), (fLogIPs ? strprintf(", peeraddr=%s", pfrom.addr.ToString()) : ""),
    2535         238 :                       pfrom.m_tx_relay == nullptr ? "block-relay" : "full-relay");
    2536         238 :         }
    2537             : 
    2538         697 :         if (pfrom.nVersion >= SENDHEADERS_VERSION) {
    2539             :             // Tell our peer we prefer to receive headers rather than inv's
    2540             :             // We send this to non-NODE NETWORK peers as well, because even
    2541             :             // non-NODE NETWORK peers can announce blocks (such as pruning
    2542             :             // nodes)
    2543         697 :             m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::SENDHEADERS));
    2544         697 :         }
    2545         697 :         if (pfrom.nVersion >= SHORT_IDS_BLOCKS_VERSION) {
    2546             :             // Tell our peer we are willing to provide version 1 or 2 cmpctblocks
    2547             :             // However, we do not request new block announcements using
    2548             :             // cmpctblock messages.
    2549             :             // We send this to non-NODE NETWORK peers as well, because
    2550             :             // they may wish to request compact blocks from us
    2551         697 :             bool fAnnounceUsingCMPCTBLOCK = false;
    2552         697 :             uint64_t nCMPCTBLOCKVersion = 2;
    2553         697 :             if (pfrom.GetLocalServices() & NODE_WITNESS)
    2554         695 :                 m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
    2555         697 :             nCMPCTBLOCKVersion = 1;
    2556         697 :             m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
    2557         697 :         }
    2558         697 :         pfrom.fSuccessfullyConnected = true;
    2559         697 :         return;
    2560             :     }
    2561             : 
    2562             :     // Feature negotiation of wtxidrelay should happen between VERSION and
    2563             :     // VERACK, to avoid relay problems from switching after a connection is up
    2564       89093 :     if (msg_type == NetMsgType::WTXIDRELAY) {
    2565         691 :         if (pfrom.fSuccessfullyConnected) {
    2566             :             // Disconnect peers that send wtxidrelay message after VERACK; this
    2567             :             // must be negotiated between VERSION and VERACK.
    2568           0 :             pfrom.fDisconnect = true;
    2569           0 :             return;
    2570             :         }
    2571         691 :         if (pfrom.nVersion >= WTXID_RELAY_VERSION) {
    2572         691 :             LOCK(cs_main);
    2573         691 :             if (!State(pfrom.GetId())->m_wtxid_relay) {
    2574         691 :                 State(pfrom.GetId())->m_wtxid_relay = true;
    2575         691 :                 g_wtxid_relay_peers++;
    2576         691 :             }
    2577         691 :         }
    2578         691 :         return;
    2579             :     }
    2580             : 
    2581       88402 :     if (!pfrom.fSuccessfullyConnected) {
    2582             :         // Must have a verack message before anything else
    2583           4 :         Misbehaving(pfrom.GetId(), 1, "non-verack message before version handshake");
    2584           4 :         return;
    2585             :     }
    2586             : 
    2587       88398 :     if (msg_type == NetMsgType::ADDR) {
    2588          15 :         std::vector<CAddress> vAddr;
    2589          15 :         vRecv >> vAddr;
    2590             : 
    2591          15 :         if (!pfrom.RelayAddrsWithConn()) {
    2592           0 :             return;
    2593             :         }
    2594          15 :         if (vAddr.size() > MAX_ADDR_TO_SEND)
    2595             :         {
    2596           1 :             Misbehaving(pfrom.GetId(), 20, strprintf("addr message size = %u", vAddr.size()));
    2597           1 :             return;
    2598             :         }
    2599             : 
    2600             :         // Store the new addresses
    2601          14 :         std::vector<CAddress> vAddrOk;
    2602          14 :         int64_t nNow = GetAdjustedTime();
    2603          14 :         int64_t nSince = nNow - 10 * 60;
    2604       10027 :         for (CAddress& addr : vAddr)
    2605             :         {
    2606       10013 :             if (interruptMsgProc)
    2607           0 :                 return;
    2608             : 
    2609             :             // We only bother storing full nodes, though this may include
    2610             :             // things which we would not make an outbound connection to, in
    2611             :             // part because we may make feeler connections to them.
    2612       10013 :             if (!MayHaveUsefulAddressDB(addr.nServices) && !HasAllDesirableServiceFlags(addr.nServices))
    2613           0 :                 continue;
    2614             : 
    2615       10013 :             if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
    2616           3 :                 addr.nTime = nNow - 5 * 24 * 60 * 60;
    2617       10013 :             pfrom.AddAddressKnown(addr);
    2618       10013 :             if (m_banman && (m_banman->IsDiscouraged(addr) || m_banman->IsBanned(addr))) {
    2619             :                 // Do not process banned/discouraged addresses beyond remembering we received them
    2620           0 :                 continue;
    2621             :             }
    2622       10013 :             bool fReachable = IsReachable(addr);
    2623       10013 :             if (addr.nTime > nSince && !pfrom.fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
    2624             :             {
    2625             :                 // Relay to a limited number of other nodes
    2626          10 :                 RelayAddress(addr, fReachable, m_connman);
    2627             :             }
    2628             :             // Do not store addresses outside our network
    2629       10013 :             if (fReachable)
    2630       10013 :                 vAddrOk.push_back(addr);
    2631       10013 :         }
    2632          14 :         m_connman.AddNewAddresses(vAddrOk, pfrom.addr, 2 * 60 * 60);
    2633          14 :         if (vAddr.size() < 1000)
    2634           4 :             pfrom.fGetAddr = false;
    2635          14 :         if (pfrom.IsAddrFetchConn())
    2636           0 :             pfrom.fDisconnect = true;
    2637          14 :         return;
    2638          15 :     }
    2639             : 
    2640       88383 :     if (msg_type == NetMsgType::SENDHEADERS) {
    2641         480 :         LOCK(cs_main);
    2642         480 :         State(pfrom.GetId())->fPreferHeaders = true;
    2643             :         return;
    2644         480 :     }
    2645             : 
    2646       87903 :     if (msg_type == NetMsgType::SENDCMPCT) {
    2647        1154 :         bool fAnnounceUsingCMPCTBLOCK = false;
    2648        1154 :         uint64_t nCMPCTBLOCKVersion = 0;
    2649        1154 :         vRecv >> fAnnounceUsingCMPCTBLOCK >> nCMPCTBLOCKVersion;
    2650        1154 :         if (nCMPCTBLOCKVersion == 1 || ((pfrom.GetLocalServices() & NODE_WITNESS) && nCMPCTBLOCKVersion == 2)) {
    2651        1150 :             LOCK(cs_main);
    2652             :             // fProvidesHeaderAndIDs is used to "lock in" version of compact blocks we send (fWantsCmpctWitness)
    2653        1150 :             if (!State(pfrom.GetId())->fProvidesHeaderAndIDs) {
    2654         480 :                 State(pfrom.GetId())->fProvidesHeaderAndIDs = true;
    2655         480 :                 State(pfrom.GetId())->fWantsCmpctWitness = nCMPCTBLOCKVersion == 2;
    2656         480 :             }
    2657        1150 :             if (State(pfrom.GetId())->fWantsCmpctWitness == (nCMPCTBLOCKVersion == 2)) // ignore later version announces
    2658         675 :                 State(pfrom.GetId())->fPreferHeaderAndIDs = fAnnounceUsingCMPCTBLOCK;
    2659        1150 :             if (!State(pfrom.GetId())->fSupportsDesiredCmpctVersion) {
    2660         484 :                 if (pfrom.GetLocalServices() & NODE_WITNESS)
    2661         482 :                     State(pfrom.GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 2);
    2662             :                 else
    2663           2 :                     State(pfrom.GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 1);
    2664             :             }
    2665        1150 :         }
    2666             :         return;
    2667        1154 :     }
    2668             : 
    2669       86749 :     if (msg_type == NetMsgType::INV) {
    2670        9131 :         std::vector<CInv> vInv;
    2671        9131 :         vRecv >> vInv;
    2672        9131 :         if (vInv.size() > MAX_INV_SZ)
    2673             :         {
    2674           1 :             Misbehaving(pfrom.GetId(), 20, strprintf("inv message size = %u", vInv.size()));
    2675           1 :             return;
    2676             :         }
    2677             : 
    2678             :         // We won't accept tx inv's if we're in blocks-only mode, or this is a
    2679             :         // block-relay-only peer
    2680        9130 :         bool fBlocksOnly = !g_relay_txes || (pfrom.m_tx_relay == nullptr);
    2681             : 
    2682             :         // Allow peers with relay permission to send data other than blocks in blocks only mode
    2683        9130 :         if (pfrom.HasPermission(PF_RELAY)) {
    2684             :             fBlocksOnly = false;
    2685           0 :         }
    2686             : 
    2687        9130 :         LOCK(cs_main);
    2688             : 
    2689        9130 :         const auto current_time = GetTime<std::chrono::microseconds>();
    2690       49720 :         uint256* best_block{nullptr};
    2691             : 
    2692       24860 :         for (CInv& inv : vInv) {
    2693       15730 :             if (interruptMsgProc) return;
    2694             : 
    2695             :             // Ignore INVs that don't match wtxidrelay setting.
    2696             :             // Note that orphan parent fetching always uses MSG_TX GETDATAs regardless of the wtxidrelay setting.
    2697             :             // This is fine as no INV messages are involved in that process.
    2698       15730 :             if (State(pfrom.GetId())->m_wtxid_relay) {
    2699       15719 :                 if (inv.IsMsgTx()) continue;
    2700             :             } else {
    2701          11 :                 if (inv.IsMsgWtx()) continue;
    2702             :             }
    2703             : 
    2704       15710 :             if (inv.IsMsgBlk()) {
    2705        1033 :                 const bool fAlreadyHave = AlreadyHaveBlock(inv.hash);
    2706        1033 :                 LogPrint(BCLog::NET, "got inv: %s  %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId());
    2707             : 
    2708        1033 :                 UpdateBlockAvailability(pfrom.GetId(), inv.hash);
    2709        1033 :                 if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count(inv.hash)) {
    2710             :                     // Headers-first is the primary method of announcement on
    2711             :                     // the network. If a node fell back to sending blocks by inv,
    2712             :                     // it's probably for a re-org. The final block hash
    2713             :                     // provided should be the highest, so send a getheaders and
    2714             :                     // then fetch the blocks we need to catch up.
    2715             :                     best_block = &inv.hash;
    2716         939 :                 }
    2717       15710 :             } else if (inv.IsGenTxMsg()) {
    2718       14677 :                 const GenTxid gtxid = ToGenTxid(inv);
    2719       14677 :                 const bool fAlreadyHave = AlreadyHaveTx(gtxid, m_mempool);
    2720       14677 :                 LogPrint(BCLog::NET, "got inv: %s  %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId());
    2721             : 
    2722       14677 :                 pfrom.AddKnownTx(inv.hash);
    2723       14677 :                 if (fBlocksOnly) {
    2724           0 :                     LogPrint(BCLog::NET, "transaction (%s) inv sent in violation of protocol, disconnecting peer=%d\n", inv.hash.ToString(), pfrom.GetId());
    2725           0 :                     pfrom.fDisconnect = true;
    2726           0 :                     return;
    2727       14677 :                 } else if (!fAlreadyHave && !m_chainman.ActiveChainstate().IsInitialBlockDownload()) {
    2728       10677 :                     RequestTx(State(pfrom.GetId()), gtxid, current_time);
    2729             :                 }
    2730       14677 :             } else {
    2731           0 :                 LogPrint(BCLog::NET, "Unknown inv type \"%s\" received from peer=%d\n", inv.ToString(), pfrom.GetId());
    2732             :             }
    2733       15710 :         }
    2734             : 
    2735        9130 :         if (best_block != nullptr) {
    2736         939 :             m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETHEADERS, ::ChainActive().GetLocator(pindexBestHeader), *best_block));
    2737         939 :             LogPrint(BCLog::NET, "getheaders (%d) %s to peer=%d\n", pindexBestHeader->nHeight, best_block->ToString(), pfrom.GetId());
    2738             :         }
    2739             : 
    2740        9130 :         return;
    2741        9131 :     }
    2742             : 
    2743       77618 :     if (msg_type == NetMsgType::GETDATA) {
    2744       18952 :         std::vector<CInv> vInv;
    2745       18952 :         vRecv >> vInv;
    2746       18952 :         if (vInv.size() > MAX_INV_SZ)
    2747             :         {
    2748           1 :             Misbehaving(pfrom.GetId(), 20, strprintf("getdata message size = %u", vInv.size()));
    2749           1 :             return;
    2750             :         }
    2751             : 
    2752       18951 :         LogPrint(BCLog::NET, "received getdata (%u invsz) peer=%d\n", vInv.size(), pfrom.GetId());
    2753             : 
    2754       18951 :         if (vInv.size() > 0) {
    2755       18951 :             LogPrint(BCLog::NET, "received getdata for: %s peer=%d\n", vInv[0].ToString(), pfrom.GetId());
    2756             :         }
    2757             : 
    2758       18951 :         pfrom.vRecvGetData.insert(pfrom.vRecvGetData.end(), vInv.begin(), vInv.end());
    2759       18951 :         ProcessGetData(pfrom, m_chainparams, m_connman, m_mempool, interruptMsgProc);
    2760       18951 :         return;
    2761       18952 :     }
    2762             : 
    2763       58666 :     if (msg_type == NetMsgType::GETBLOCKS) {
    2764           4 :         CBlockLocator locator;
    2765           4 :         uint256 hashStop;
    2766           4 :         vRecv >> locator >> hashStop;
    2767             : 
    2768           4 :         if (locator.vHave.size() > MAX_LOCATOR_SZ) {
    2769           1 :             LogPrint(BCLog::NET, "getblocks locator size %lld > %d, disconnect peer=%d\n", locator.vHave.size(), MAX_LOCATOR_SZ, pfrom.GetId());
    2770           1 :             pfrom.fDisconnect = true;
    2771           1 :             return;
    2772             :         }
    2773             : 
    2774             :         // We might have announced the currently-being-connected tip using a
    2775             :         // compact block, which resulted in the peer sending a getblocks
    2776             :         // request, which we would otherwise respond to without the new block.
    2777             :         // To avoid this situation we simply verify that we are on our best
    2778             :         // known chain now. This is super overkill, but we handle it better
    2779             :         // for getheaders requests, and there are no known nodes which support
    2780             :         // compact blocks but still use getblocks to request blocks.
    2781             :         {
    2782           3 :             std::shared_ptr<const CBlock> a_recent_block;
    2783             :             {
    2784           3 :                 LOCK(cs_most_recent_block);
    2785           3 :                 a_recent_block = most_recent_block;
    2786           3 :             }
    2787           3 :             BlockValidationState state;
    2788           3 :             if (!ActivateBestChain(state, m_chainparams, a_recent_block)) {
    2789           0 :                 LogPrint(BCLog::NET, "failed to activate chain (%s)\n", state.ToString());
    2790             :             }
    2791           3 :         }
    2792             : 
    2793           3 :         LOCK(cs_main);
    2794             : 
    2795             :         // Find the last block the caller has in the main chain
    2796           3 :         const CBlockIndex* pindex = FindForkInGlobalIndex(::ChainActive(), locator);
    2797             : 
    2798             :         // Send the rest of the chain
    2799           3 :         if (pindex)
    2800           3 :             pindex = ::ChainActive().Next(pindex);
    2801           3 :         int nLimit = 500;
    2802           3 :         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());
    2803          22 :         for (; pindex; pindex = ::ChainActive().Next(pindex))
    2804             :         {
    2805          19 :             if (pindex->GetBlockHash() == hashStop)
    2806             :             {
    2807           0 :                 LogPrint(BCLog::NET, "  getblocks stopping at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
    2808             :                 break;
    2809             :             }
    2810             :             // If pruning, don't inv blocks unless we have on disk and are likely to still have
    2811             :             // for some reasonable time window (1 hour) that block relay might require.
    2812          19 :             const int nPrunedBlocksLikelyToHave = MIN_BLOCKS_TO_KEEP - 3600 / m_chainparams.GetConsensus().nPowTargetSpacing;
    2813          19 :             if (fPruneMode && (!(pindex->nStatus & BLOCK_HAVE_DATA) || pindex->nHeight <= ::ChainActive().Tip()->nHeight - nPrunedBlocksLikelyToHave))
    2814             :             {
    2815           0 :                 LogPrint(BCLog::NET, " getblocks stopping, pruned or too old block at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
    2816           0 :                 break;
    2817             :             }
    2818          38 :             WITH_LOCK(pfrom.cs_inventory, pfrom.vInventoryBlockToSend.push_back(pindex->GetBlockHash()));
    2819          19 :             if (--nLimit <= 0)
    2820             :             {
    2821             :                 // When this block is requested, we'll send an inv that'll
    2822             :                 // trigger the peer to getblocks the next batch of inventory.
    2823           0 :                 LogPrint(BCLog::NET, "  getblocks stopping at limit %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
    2824           0 :                 pfrom.hashContinue = pindex->GetBlockHash();
    2825           0 :                 break;
    2826             :             }
    2827          19 :         }
    2828             :         return;
    2829           4 :     }
    2830             : 
    2831       58662 :     if (msg_type == NetMsgType::GETBLOCKTXN) {
    2832        2243 :         BlockTransactionsRequest req;
    2833        2243 :         vRecv >> req;
    2834             : 
    2835        2243 :         std::shared_ptr<const CBlock> recent_block;
    2836             :         {
    2837        2243 :             LOCK(cs_most_recent_block);
    2838        2243 :             if (most_recent_block_hash == req.blockhash)
    2839        2187 :                 recent_block = most_recent_block;
    2840             :             // Unlock cs_most_recent_block to avoid cs_main lock inversion
    2841        2243 :         }
    2842        2243 :         if (recent_block) {
    2843        2187 :             SendBlockTransactions(pfrom, *recent_block, req);
    2844        2187 :             return;
    2845             :         }
    2846             : 
    2847          56 :         LOCK(cs_main);
    2848             : 
    2849          56 :         const CBlockIndex* pindex = LookupBlockIndex(req.blockhash);
    2850          56 :         if (!pindex || !(pindex->nStatus & BLOCK_HAVE_DATA)) {
    2851           2 :             LogPrint(BCLog::NET, "Peer %d sent us a getblocktxn for a block we don't have\n", pfrom.GetId());
    2852           2 :             return;
    2853             :         }
    2854             : 
    2855          54 :         if (pindex->nHeight < ::ChainActive().Height() - MAX_BLOCKTXN_DEPTH) {
    2856             :             // If an older block is requested (should never happen in practice,
    2857             :             // but can happen in tests) send a block response instead of a
    2858             :             // blocktxn response. Sending a full block response instead of a
    2859             :             // small blocktxn response is preferable in the case where a peer
    2860             :             // might maliciously send lots of getblocktxn requests to trigger
    2861             :             // expensive disk reads, because it will require the peer to
    2862             :             // actually receive all the data read from disk over the network.
    2863           2 :             LogPrint(BCLog::NET, "Peer %d sent us a getblocktxn for a block > %i deep\n", pfrom.GetId(), MAX_BLOCKTXN_DEPTH);
    2864           2 :             CInv inv;
    2865           2 :             inv.type = State(pfrom.GetId())->fWantsCmpctWitness ? MSG_WITNESS_BLOCK : MSG_BLOCK;
    2866           2 :             inv.hash = req.blockhash;
    2867           2 :             pfrom.vRecvGetData.push_back(inv);
    2868             :             // The message processing loop will go around again (without pausing) and we'll respond then (without cs_main)
    2869             :             return;
    2870           2 :         }
    2871             : 
    2872          52 :         CBlock block;
    2873          52 :         bool ret = ReadBlockFromDisk(block, pindex, m_chainparams.GetConsensus());
    2874          52 :         assert(ret);
    2875             : 
    2876          52 :         SendBlockTransactions(pfrom, block, req);
    2877             :         return;
    2878        2243 :     }
    2879             : 
    2880       56419 :     if (msg_type == NetMsgType::GETHEADERS) {
    2881        1438 :         CBlockLocator locator;
    2882        1438 :         uint256 hashStop;
    2883        1438 :         vRecv >> locator >> hashStop;
    2884             : 
    2885        1438 :         if (locator.vHave.size() > MAX_LOCATOR_SZ) {
    2886           1 :             LogPrint(BCLog::NET, "getheaders locator size %lld > %d, disconnect peer=%d\n", locator.vHave.size(), MAX_LOCATOR_SZ, pfrom.GetId());
    2887           1 :             pfrom.fDisconnect = true;
    2888           1 :             return;
    2889             :         }
    2890             : 
    2891        1437 :         LOCK(cs_main);
    2892        1437 :         if (::ChainstateActive().IsInitialBlockDownload() && !pfrom.HasPermission(PF_DOWNLOAD)) {
    2893         158 :             LogPrint(BCLog::NET, "Ignoring getheaders from peer=%d because node is in initial block download\n", pfrom.GetId());
    2894         158 :             return;
    2895             :         }
    2896             : 
    2897        1279 :         CNodeState *nodestate = State(pfrom.GetId());
    2898             :         const CBlockIndex* pindex = nullptr;
    2899        1279 :         if (locator.IsNull())
    2900             :         {
    2901             :             // If locator is null, return the hashStop block
    2902           7 :             pindex = LookupBlockIndex(hashStop);
    2903           7 :             if (!pindex) {
    2904           0 :                 return;
    2905             :             }
    2906             : 
    2907           7 :             if (!BlockRequestAllowed(pindex, m_chainparams.GetConsensus())) {
    2908           2 :                 LogPrint(BCLog::NET, "%s: ignoring request from peer=%i for old block header that isn't in the main chain\n", __func__, pfrom.GetId());
    2909           2 :                 return;
    2910             :             }
    2911             :         }
    2912             :         else
    2913             :         {
    2914             :             // Find the last block the caller has in the main chain
    2915        1272 :             pindex = FindForkInGlobalIndex(::ChainActive(), locator);
    2916        1272 :             if (pindex)
    2917        1272 :                 pindex = ::ChainActive().Next(pindex);
    2918             :         }
    2919             : 
    2920             :         // we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
    2921        1277 :         std::vector<CBlock> vHeaders;
    2922             :         int nLimit = MAX_HEADERS_RESULTS;
    2923        1277 :         LogPrint(BCLog::NET, "getheaders %d to %s from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), pfrom.GetId());
    2924      166997 :         for (; pindex; pindex = ::ChainActive().Next(pindex))
    2925             :         {
    2926      166647 :             vHeaders.push_back(pindex->GetBlockHeader());
    2927      166647 :             if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
    2928             :                 break;
    2929             :         }
    2930             :         // pindex can be nullptr either if we sent ::ChainActive().Tip() OR
    2931             :         // if our peer has ::ChainActive().Tip() (and thus we are sending an empty
    2932             :         // headers message). In both cases it's safe to update
    2933             :         // pindexBestHeaderSent to be our tip.
    2934             :         //
    2935             :         // It is important that we simply reset the BestHeaderSent value here,
    2936             :         // and not max(BestHeaderSent, newHeaderSent). We might have announced
    2937             :         // the currently-being-connected tip using a compact block, which
    2938             :         // resulted in the peer sending a headers request, which we respond to
    2939             :         // without the new block. By resetting the BestHeaderSent, we ensure we
    2940             :         // will re-announce the new block via headers (or compact blocks again)
    2941             :         // in the SendMessages logic.
    2942        1277 :         nodestate->pindexBestHeaderSent = pindex ? pindex : ::ChainActive().Tip();
    2943        1277 :         m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
    2944             :         return;
    2945        2715 :     }
    2946             : 
    2947       54981 :     if (msg_type == NetMsgType::TX) {
    2948             :         // Stop processing the transaction early if
    2949             :         // 1) We are in blocks only mode and peer has no relay permission
    2950             :         // 2) This peer is a block-relay-only peer
    2951        9673 :         if ((!g_relay_txes && !pfrom.HasPermission(PF_RELAY)) || (pfrom.m_tx_relay == nullptr))
    2952             :         {
    2953           1 :             LogPrint(BCLog::NET, "transaction sent in violation of protocol peer=%d\n", pfrom.GetId());
    2954           1 :             pfrom.fDisconnect = true;
    2955           1 :             return;
    2956             :         }
    2957             : 
    2958        9672 :         CTransactionRef ptx;
    2959        9672 :         vRecv >> ptx;
    2960        9670 :         const CTransaction& tx = *ptx;
    2961             : 
    2962        9670 :         const uint256& txid = ptx->GetHash();
    2963        9670 :         const uint256& wtxid = ptx->GetWitnessHash();
    2964             : 
    2965        9670 :         LOCK2(cs_main, g_cs_orphans);
    2966             : 
    2967        9670 :         CNodeState* nodestate = State(pfrom.GetId());
    2968             : 
    2969        9670 :         const uint256& hash = nodestate->m_wtxid_relay ? wtxid : txid;
    2970        9670 :         pfrom.AddKnownTx(hash);
    2971        9670 :         if (nodestate->m_wtxid_relay && txid != wtxid) {
    2972             :             // Insert txid into filterInventoryKnown, even for
    2973             :             // wtxidrelay peers. This prevents re-adding of
    2974             :             // unconfirmed parents to the recently_announced
    2975             :             // filter, when a child tx is requested. See
    2976             :             // ProcessGetData().
    2977         721 :             pfrom.AddKnownTx(txid);
    2978             :         }
    2979             : 
    2980        9670 :         TxValidationState state;
    2981             : 
    2982       29010 :         for (const GenTxid& gtxid : {GenTxid(false, txid), GenTxid(true, wtxid)}) {
    2983       19340 :             nodestate->m_tx_download.m_tx_announced.erase(gtxid.GetHash());
    2984       19340 :             nodestate->m_tx_download.m_tx_in_flight.erase(gtxid.GetHash());
    2985       19340 :             EraseTxRequest(gtxid);
    2986             :         }
    2987             : 
    2988        9670 :         std::list<CTransactionRef> lRemovedTxn;
    2989             : 
    2990             :         // We do the AlreadyHaveTx() check using wtxid, rather than txid - in the
    2991             :         // absence of witness malleation, this is strictly better, because the
    2992             :         // recent rejects filter may contain the wtxid but rarely contains
    2993             :         // the txid of a segwit transaction that has been rejected.
    2994             :         // In the presence of witness malleation, it's possible that by only
    2995             :         // doing the check with wtxid, we could overlook a transaction which
    2996             :         // was confirmed with a different witness, or exists in our mempool
    2997             :         // with a different witness, but this has limited downside:
    2998             :         // mempool validation does its own lookup of whether we have the txid
    2999             :         // already; and an adversary can already relay us old transactions
    3000             :         // (older than our recency filter) if trying to DoS us, without any need
    3001             :         // for witness malleation.
    3002       19286 :         if (!AlreadyHaveTx(GenTxid(/* is_wtxid=*/true, wtxid), m_mempool) &&
    3003        9616 :             AcceptToMemoryPool(m_mempool, state, ptx, &lRemovedTxn, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
    3004        9360 :             m_mempool.check(&::ChainstateActive().CoinsTip());
    3005        9360 :             RelayTransaction(tx.GetHash(), tx.GetWitnessHash(), m_connman);
    3006       38013 :             for (unsigned int i = 0; i < tx.vout.size(); i++) {
    3007       28653 :                 auto it_by_prev = mapOrphanTransactionsByPrev.find(COutPoint(txid, i));
    3008       28653 :                 if (it_by_prev != mapOrphanTransactionsByPrev.end()) {
    3009           4 :                     for (const auto& elem : it_by_prev->second) {
    3010           2 :                         pfrom.orphan_work_set.insert(elem->first);
    3011           0 :                     }
    3012           2 :                 }
    3013       28653 :             }
    3014             : 
    3015        9360 :             pfrom.nLastTXTime = GetTime();
    3016             : 
    3017        9360 :             LogPrint(BCLog::MEMPOOL, "AcceptToMemoryPool: peer=%d: accepted %s (poolsz %u txn, %u kB)\n",
    3018             :                 pfrom.GetId(),
    3019             :                 tx.GetHash().ToString(),
    3020             :                 m_mempool.size(), m_mempool.DynamicMemoryUsage() / 1000);
    3021             : 
    3022             :             // Recursively process any orphan transactions that depended on this one
    3023        9360 :             ProcessOrphanTx(pfrom.orphan_work_set, lRemovedTxn);
    3024             :         }
    3025         310 :         else if (state.GetResult() == TxValidationResult::TX_MISSING_INPUTS)
    3026             :         {
    3027             :             bool fRejectedParents = false; // It may be the case that the orphans parents have all been rejected
    3028             : 
    3029             :             // Deduplicate parent txids, so that we don't have to loop over
    3030             :             // the same parent txid more than once down below.
    3031         130 :             std::vector<uint256> unique_parents;
    3032         130 :             unique_parents.reserve(tx.vin.size());
    3033         261 :             for (const CTxIn& txin : tx.vin) {
    3034             :                 // We start with all parents, and then remove duplicates below.
    3035         131 :                 unique_parents.push_back(txin.prevout.hash);
    3036             :             }
    3037         130 :             std::sort(unique_parents.begin(), unique_parents.end());
    3038         130 :             unique_parents.erase(std::unique(unique_parents.begin(), unique_parents.end()), unique_parents.end());
    3039         261 :             for (const uint256& parent_txid : unique_parents) {
    3040         131 :                 if (recentRejects->contains(parent_txid)) {
    3041             :                     fRejectedParents = true;
    3042           1 :                     break;
    3043             :                 }
    3044         130 :             }
    3045         130 :             if (!fRejectedParents) {
    3046         129 :                 const auto current_time = GetTime<std::chrono::microseconds>();
    3047             : 
    3048         259 :                 for (const uint256& parent_txid : unique_parents) {
    3049             :                     // Here, we only have the txid (and not wtxid) of the
    3050             :                     // inputs, so we only request in txid mode, even for
    3051             :                     // wtxidrelay peers.
    3052             :                     // Eventually we should replace this with an improved
    3053             :                     // protocol for getting all unconfirmed parents.
    3054         130 :                     const GenTxid gtxid{/* is_wtxid=*/false, parent_txid};
    3055         130 :                     pfrom.AddKnownTx(parent_txid);
    3056         130 :                     if (!AlreadyHaveTx(gtxid, m_mempool)) RequestTx(State(pfrom.GetId()), gtxid, current_time);
    3057         130 :                 }
    3058         129 :                 AddOrphanTx(ptx, pfrom.GetId());
    3059             : 
    3060             :                 // DoS prevention: do not allow mapOrphanTransactions to grow unbounded (see CVE-2012-3789)
    3061         129 :                 unsigned int nMaxOrphanTx = (unsigned int)std::max((int64_t)0, gArgs.GetArg("-maxorphantx", DEFAULT_MAX_ORPHAN_TRANSACTIONS));
    3062         129 :                 unsigned int nEvicted = LimitOrphanTxSize(nMaxOrphanTx);
    3063         129 :                 if (nEvicted > 0) {
    3064           1 :                     LogPrint(BCLog::MEMPOOL, "mapOrphan overflow, removed %u tx\n", nEvicted);
    3065             :                 }
    3066         129 :             } else {
    3067           1 :                 LogPrint(BCLog::MEMPOOL, "not keeping orphan with rejected parents %s\n",tx.GetHash().ToString());
    3068             :                 // We will continue to reject this tx since it has rejected
    3069             :                 // parents so avoid re-requesting it from other peers.
    3070             :                 // Here we add both the txid and the wtxid, as we know that
    3071             :                 // regardless of what witness is provided, we will not accept
    3072             :                 // this, so we don't need to allow for redownload of this txid
    3073             :                 // from any of our non-wtxidrelay peers.
    3074           1 :                 recentRejects->insert(tx.GetHash());
    3075           1 :                 recentRejects->insert(tx.GetWitnessHash());
    3076             :             }
    3077         130 :         } else {
    3078         180 :             if (state.GetResult() != TxValidationResult::TX_WITNESS_STRIPPED) {
    3079             :                 // We can add the wtxid of this transaction to our reject filter.
    3080             :                 // Do not add txids of witness transactions or witness-stripped
    3081             :                 // transactions to the filter, as they can have been malleated;
    3082             :                 // adding such txids to the reject filter would potentially
    3083             :                 // interfere with relay of valid transactions from peers that
    3084             :                 // do not support wtxid-based relay. See
    3085             :                 // https://github.com/bitcoin/bitcoin/issues/8279 for details.
    3086             :                 // We can remove this restriction (and always add wtxids to
    3087             :                 // the filter even for witness stripped transactions) once
    3088             :                 // wtxid-based relay is broadly deployed.
    3089             :                 // See also comments in https://github.com/bitcoin/bitcoin/pull/18044#discussion_r443419034
    3090             :                 // for concerns around weakening security of unupgraded nodes
    3091             :                 // if we start doing this too early.
    3092         179 :                 assert(recentRejects);
    3093         179 :                 recentRejects->insert(tx.GetWitnessHash());
    3094             :                 // If the transaction failed for TX_INPUTS_NOT_STANDARD,
    3095             :                 // then we know that the witness was irrelevant to the policy
    3096             :                 // failure, since this check depends only on the txid
    3097             :                 // (the scriptPubKey being spent is covered by the txid).
    3098             :                 // Add the txid to the reject filter to prevent repeated
    3099             :                 // processing of this transaction in the event that child
    3100             :                 // transactions are later received (resulting in
    3101             :                 // parent-fetching by txid via the orphan-handling logic).
    3102         179 :                 if (state.GetResult() == TxValidationResult::TX_INPUTS_NOT_STANDARD && tx.GetWitnessHash() != tx.GetHash()) {
    3103           1 :                     recentRejects->insert(tx.GetHash());
    3104             :                 }
    3105         179 :                 if (RecursiveDynamicUsage(*ptx) < 100000) {
    3106         178 :                     AddToCompactExtraTransactions(ptx);
    3107             :                 }
    3108           1 :             } else if (tx.HasWitness() && RecursiveDynamicUsage(*ptx) < 100000) {
    3109           0 :                 AddToCompactExtraTransactions(ptx);
    3110             :             }
    3111             : 
    3112         180 :             if (pfrom.HasPermission(PF_FORCERELAY)) {
    3113             :                 // Always relay transactions received from peers with forcerelay permission, even
    3114             :                 // if they were already in the mempool,
    3115             :                 // allowing the node to function as a gateway for
    3116             :                 // nodes hidden behind it.
    3117           2 :                 if (!m_mempool.exists(tx.GetHash())) {
    3118           1 :                     LogPrintf("Not relaying non-mempool transaction %s from forcerelay peer=%d\n", tx.GetHash().ToString(), pfrom.GetId());
    3119           1 :                 } else {
    3120           1 :                     LogPrintf("Force relaying tx %s from peer=%d\n", tx.GetHash().ToString(), pfrom.GetId());
    3121           1 :                     RelayTransaction(tx.GetHash(), tx.GetWitnessHash(), m_connman);
    3122             :                 }
    3123             :             }
    3124             :         }
    3125             : 
    3126        9682 :         for (const CTransactionRef& removedTx : lRemovedTxn)
    3127          12 :             AddToCompactExtraTransactions(removedTx);
    3128             : 
    3129             :         // If a tx has been detected by recentRejects, we will have reached
    3130             :         // this point and the tx will have been ignored. Because we haven't run
    3131             :         // the tx through AcceptToMemoryPool, we won't have computed a DoS
    3132             :         // score for it or determined exactly why we consider it invalid.
    3133             :         //
    3134             :         // This means we won't penalize any peer subsequently relaying a DoSy
    3135             :         // tx (even if we penalized the first peer who gave it to us) because
    3136             :         // we have to account for recentRejects showing false positives. In
    3137             :         // other words, we shouldn't penalize a peer if we aren't *sure* they
    3138             :         // submitted a DoSy tx.
    3139             :         //
    3140             :         // Note that recentRejects doesn't just record DoSy or invalid
    3141             :         // transactions, but any tx not accepted by the mempool, which may be
    3142             :         // due to node policy (vs. consensus). So we can't blanket penalize a
    3143             :         // peer simply for relaying a tx that our recentRejects has caught,
    3144             :         // regardless of false positives.
    3145             : 
    3146        9670 :         if (state.IsInvalid()) {
    3147         256 :             LogPrint(BCLog::MEMPOOLREJ, "%s from peer=%d was not accepted: %s\n", tx.GetHash().ToString(),
    3148             :                 pfrom.GetId(),
    3149             :                 state.ToString());
    3150         256 :             MaybePunishNodeForTx(pfrom.GetId(), state);
    3151         256 :         }
    3152             :         return;
    3153        9672 :     }
    3154             : 
    3155       45308 :     if (msg_type == NetMsgType::CMPCTBLOCK)
    3156             :     {
    3157             :         // Ignore cmpctblock received while importing
    3158       11978 :         if (fImporting || fReindex) {
    3159           0 :             LogPrint(BCLog::NET, "Unexpected cmpctblock message received from peer %d\n", pfrom.GetId());
    3160           0 :             return;
    3161             :         }
    3162             : 
    3163       11978 :         CBlockHeaderAndShortTxIDs cmpctblock;
    3164       11978 :         vRecv >> cmpctblock;
    3165             : 
    3166             :         bool received_new_header = false;
    3167             : 
    3168             :         {
    3169       11978 :         LOCK(cs_main);
    3170             : 
    3171       11978 :         if (!LookupBlockIndex(cmpctblock.header.hashPrevBlock)) {
    3172             :             // Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
    3173          19 :             if (!::ChainstateActive().IsInitialBlockDownload())
    3174          19 :                 m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETHEADERS, ::ChainActive().GetLocator(pindexBestHeader), uint256()));
    3175          19 :             return;
    3176             :         }
    3177             : 
    3178       11959 :         if (!LookupBlockIndex(cmpctblock.header.GetHash())) {
    3179             :             received_new_header = true;
    3180       10917 :         }
    3181       11978 :         }
    3182             : 
    3183       11959 :         const CBlockIndex *pindex = nullptr;
    3184       11959 :         BlockValidationState state;
    3185       11959 :         if (!m_chainman.ProcessNewBlockHeaders({cmpctblock.header}, state, m_chainparams, &pindex)) {
    3186           2 :             if (state.IsInvalid()) {
    3187           2 :                 MaybePunishNodeForBlock(pfrom.GetId(), state, /*via_compact_block*/ true, "invalid header via cmpctblock");
    3188           2 :                 return;
    3189             :             }
    3190             :         }
    3191             : 
    3192             :         // When we succeed in decoding a block's txids from a cmpctblock
    3193             :         // message we typically jump to the BLOCKTXN handling code, with a
    3194             :         // dummy (empty) BLOCKTXN message, to re-use the logic there in
    3195             :         // completing processing of the putative block (without cs_main).
    3196       11957 :         bool fProcessBLOCKTXN = false;
    3197       11957 :         CDataStream blockTxnMsg(SER_NETWORK, PROTOCOL_VERSION);
    3198             : 
    3199             :         // If we end up treating this as a plain headers message, call that as well
    3200             :         // without cs_main.
    3201       11957 :         bool fRevertToHeaderProcessing = false;
    3202             : 
    3203             :         // Keep a CBlock for "optimistic" compactblock reconstructions (see
    3204             :         // below)
    3205       11957 :         std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
    3206       11957 :         bool fBlockReconstructed = false;
    3207             : 
    3208             :         {
    3209       11957 :         LOCK2(cs_main, g_cs_orphans);
    3210             :         // If AcceptBlockHeader returned true, it set pindex
    3211       11957 :         assert(pindex);
    3212       11957 :         UpdateBlockAvailability(pfrom.GetId(), pindex->GetBlockHash());
    3213             : 
    3214       11957 :         CNodeState *nodestate = State(pfrom.GetId());
    3215             : 
    3216             :         // If this was a new header with more work than our tip, update the
    3217             :         // peer's last block announcement time
    3218       11957 :         if (received_new_header && pindex->nChainWork > ::ChainActive().Tip()->nChainWork) {
    3219       10882 :             nodestate->m_last_block_announcement = GetTime();
    3220       10882 :         }
    3221             : 
    3222       11957 :         std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator blockInFlightIt = mapBlocksInFlight.find(pindex->GetBlockHash());
    3223       11957 :         bool fAlreadyInFlight = blockInFlightIt != mapBlocksInFlight.end();
    3224             : 
    3225       11957 :         if (pindex->nStatus & BLOCK_HAVE_DATA) // Nothing to do here
    3226         706 :             return;
    3227             : 
    3228       11251 :         if (pindex->nChainWork <= ::ChainActive().Tip()->nChainWork || // We know something better
    3229       11214 :                 pindex->nTx != 0) { // We had this block at some point, but pruned it
    3230          37 :             if (fAlreadyInFlight) {
    3231             :                 // We requested this block for some reason, but our mempool will probably be useless
    3232             :                 // so we just grab the block via normal getdata
    3233           4 :                 std::vector<CInv> vInv(1);
    3234           4 :                 vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom), cmpctblock.header.GetHash());
    3235           4 :                 m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
    3236           4 :             }
    3237          37 :             return;
    3238             :         }
    3239             : 
    3240             :         // If we're not close to tip yet, give up and let parallel block fetch work its magic
    3241       11214 :         if (!fAlreadyInFlight && !CanDirectFetch(m_chainparams.GetConsensus()))
    3242           0 :             return;
    3243             : 
    3244       11214 :         if (IsWitnessEnabled(pindex->pprev, m_chainparams.GetConsensus()) && !nodestate->fSupportsDesiredCmpctVersion) {
    3245             :             // Don't bother trying to process compact blocks from v1 peers
    3246             :             // after segwit activates.
    3247           1 :             return;
    3248             :         }
    3249             : 
    3250             :         // We want to be a bit conservative just to be extra careful about DoS
    3251             :         // possibilities in compact block processing...
    3252       11213 :         if (pindex->nHeight <= ::ChainActive().Height() + 2) {
    3253       10328 :             if ((!fAlreadyInFlight && nodestate->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) ||
    3254         213 :                  (fAlreadyInFlight && blockInFlightIt->second.first == pfrom.GetId())) {
    3255       10054 :                 std::list<QueuedBlock>::iterator* queuedBlockIt = nullptr;
    3256       10054 :                 if (!MarkBlockAsInFlight(m_mempool, pfrom.GetId(), pindex->GetBlockHash(), pindex, &queuedBlockIt)) {
    3257         152 :                     if (!(*queuedBlockIt)->partialBlock)
    3258         152 :                         (*queuedBlockIt)->partialBlock.reset(new PartiallyDownloadedBlock(&m_mempool));
    3259             :                     else {
    3260             :                         // The block was already in flight using compact blocks from the same peer
    3261           0 :                         LogPrint(BCLog::NET, "Peer sent us compact block we were already syncing!\n");
    3262           0 :                         return;
    3263             :                     }
    3264         152 :                 }
    3265             : 
    3266       10054 :                 PartiallyDownloadedBlock& partialBlock = *(*queuedBlockIt)->partialBlock;
    3267       10054 :                 ReadStatus status = partialBlock.InitData(cmpctblock, vExtraTxnForCompact);
    3268       10054 :                 if (status == READ_STATUS_INVALID) {
    3269           1 :                     MarkBlockAsReceived(pindex->GetBlockHash()); // Reset in-flight state in case Misbehaving does not result in a disconnect
    3270           1 :                     Misbehaving(pfrom.GetId(), 100, "invalid compact block");
    3271           1 :                     return;
    3272       10053 :                 } else if (status == READ_STATUS_FAILED) {
    3273             :                     // Duplicate txindexes, the block is now in-flight, so just request it
    3274           0 :                     std::vector<CInv> vInv(1);
    3275           0 :                     vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom), cmpctblock.header.GetHash());
    3276           0 :                     m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
    3277             :                     return;
    3278           0 :                 }
    3279             : 
    3280       10053 :                 BlockTransactionsRequest req;
    3281       32919 :                 for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
    3282       22866 :                     if (!partialBlock.IsTxAvailable(i))
    3283        3560 :                         req.indexes.push_back(i);
    3284             :                 }
    3285       10053 :                 if (req.indexes.empty()) {
    3286             :                     // Dirty hack to jump to BLOCKTXN code (TODO: move message handling into their own functions)
    3287        7828 :                     BlockTransactions txn;
    3288        7828 :                     txn.blockhash = cmpctblock.header.GetHash();
    3289        7828 :                     blockTxnMsg << txn;
    3290             :                     fProcessBLOCKTXN = true;
    3291        7828 :                 } else {
    3292        2225 :                     req.blockhash = pindex->GetBlockHash();
    3293        2225 :                     m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETBLOCKTXN, req));
    3294             :                 }
    3295       20107 :             } else {
    3296             :                 // This block is either already in flight from a different
    3297             :                 // peer, or this peer has too many blocks outstanding to
    3298             :                 // download from.
    3299             :                 // Optimistically try to reconstruct anyway since we might be
    3300             :                 // able to without any round trips.
    3301          61 :                 PartiallyDownloadedBlock tempBlock(&m_mempool);
    3302          61 :                 ReadStatus status = tempBlock.InitData(cmpctblock, vExtraTxnForCompact);
    3303          61 :                 if (status != READ_STATUS_OK) {
    3304             :                     // TODO: don't ignore failures
    3305           0 :                     return;
    3306             :                 }
    3307          61 :                 std::vector<CTransactionRef> dummy;
    3308          61 :                 status = tempBlock.FillBlock(*pblock, dummy);
    3309          61 :                 if (status == READ_STATUS_OK) {
    3310             :                     fBlockReconstructed = true;
    3311          52 :                 }
    3312          61 :             }
    3313             :         } else {
    3314        1098 :             if (fAlreadyInFlight) {
    3315             :                 // We requested this block, but its far into the future, so our
    3316             :                 // mempool will probably be useless - request the block normally
    3317         118 :                 std::vector<CInv> vInv(1);
    3318         118 :                 vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom), cmpctblock.header.GetHash());
    3319         118 :                 m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
    3320             :                 return;
    3321         118 :             } else {
    3322             :                 // If this was an announce-cmpctblock, we want the same treatment as a header message
    3323             :                 fRevertToHeaderProcessing = true;
    3324             :             }
    3325             :         }
    3326       11957 :         } // cs_main
    3327             : 
    3328       11094 :         if (fProcessBLOCKTXN) {
    3329        7828 :             return ProcessMessage(pfrom, NetMsgType::BLOCKTXN, blockTxnMsg, time_received, interruptMsgProc);
    3330             :         }
    3331             : 
    3332        3266 :         if (fRevertToHeaderProcessing) {
    3333             :             // Headers received from HB compact block peers are permitted to be
    3334             :             // relayed before full validation (see BIP 152), so we don't want to disconnect
    3335             :             // the peer if the header turns out to be for an invalid block.
    3336             :             // Note that if a peer tries to build on an invalid chain, that
    3337             :             // will be detected and the peer will be disconnected/discouraged.
    3338         980 :             return ProcessHeadersMessage(pfrom, {cmpctblock.header}, /*via_compact_block=*/true);
    3339             :         }
    3340             : 
    3341        2286 :         if (fBlockReconstructed) {
    3342             :             // If we got here, we were able to optimistically reconstruct a
    3343             :             // block that is in flight from some other peer.
    3344             :             {
    3345          52 :                 LOCK(cs_main);
    3346          52 :                 mapBlockSource.emplace(pblock->GetHash(), std::make_pair(pfrom.GetId(), false));
    3347          52 :             }
    3348          52 :             bool fNewBlock = false;
    3349             :             // Setting fForceProcessing to true means that we bypass some of
    3350             :             // our anti-DoS protections in AcceptBlock, which filters
    3351             :             // unrequested blocks that might be trying to waste our resources
    3352             :             // (eg disk space). Because we only try to reconstruct blocks when
    3353             :             // we're close to caught up (via the CanDirectFetch() requirement
    3354             :             // above, combined with the behavior of not requesting blocks until
    3355             :             // we have a chain with at least nMinimumChainWork), and we ignore
    3356             :             // compact blocks with less work than our tip, it is safe to treat
    3357             :             // reconstructed compact blocks as having been requested.
    3358          52 :             m_chainman.ProcessNewBlock(m_chainparams, pblock, /*fForceProcessing=*/true, &fNewBlock);
    3359          52 :             if (fNewBlock) {
    3360          51 :                 pfrom.nLastBlockTime = GetTime();
    3361          51 :             } else {
    3362           1 :                 LOCK(cs_main);
    3363           1 :                 mapBlockSource.erase(pblock->GetHash());
    3364           1 :             }
    3365          52 :             LOCK(cs_main); // hold cs_main for CBlockIndex::IsValid()
    3366          52 :             if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS)) {
    3367             :                 // Clear download state for this block, which is in
    3368             :                 // process from some other peer.  We do this after calling
    3369             :                 // ProcessNewBlock so that a malleated cmpctblock announcement
    3370             :                 // can't be used to interfere with block relay.
    3371          51 :                 MarkBlockAsReceived(pblock->GetHash());
    3372          51 :             }
    3373          52 :         }
    3374        2286 :         return;
    3375       11978 :     }
    3376             : 
    3377       33330 :     if (msg_type == NetMsgType::BLOCKTXN)
    3378             :     {
    3379             :         // Ignore blocktxn received while importing
    3380       10052 :         if (fImporting || fReindex) {
    3381           0 :             LogPrint(BCLog::NET, "Unexpected blocktxn message received from peer %d\n", pfrom.GetId());
    3382           0 :             return;
    3383             :         }
    3384             : 
    3385       10052 :         BlockTransactions resp;
    3386       10052 :         vRecv >> resp;
    3387             : 
    3388       10052 :         std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
    3389       10052 :         bool fBlockRead = false;
    3390             :         {
    3391       10052 :             LOCK(cs_main);
    3392             : 
    3393       10052 :             std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator it = mapBlocksInFlight.find(resp.blockhash);
    3394       20104 :             if (it == mapBlocksInFlight.end() || !it->second.second->partialBlock ||
    3395       10052 :                     it->second.first != pfrom.GetId()) {
    3396           0 :                 LogPrint(BCLog::NET, "Peer %d sent us block transactions for block we weren't expecting\n", pfrom.GetId());
    3397           0 :                 return;
    3398             :             }
    3399             : 
    3400       10052 :             PartiallyDownloadedBlock& partialBlock = *it->second.second->partialBlock;
    3401       10052 :             ReadStatus status = partialBlock.FillBlock(*pblock, resp.txn);
    3402       10052 :             if (status == READ_STATUS_INVALID) {
    3403           0 :                 MarkBlockAsReceived(resp.blockhash); // Reset in-flight state in case Misbehaving does not result in a disconnect
    3404           0 :                 Misbehaving(pfrom.GetId(), 100, "invalid compact block/non-matching block transactions");
    3405           0 :                 return;
    3406       10052 :             } else if (status == READ_STATUS_FAILED) {
    3407             :                 // Might have collided, fall back to getdata now :(
    3408           1 :                 std::vector<CInv> invs;
    3409           1 :                 invs.push_back(CInv(MSG_BLOCK | GetFetchFlags(pfrom), resp.blockhash));
    3410           1 :                 m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, invs));
    3411           1 :             } else {
    3412             :                 // Block is either okay, or possibly we received
    3413             :                 // READ_STATUS_CHECKBLOCK_FAILED.
    3414             :                 // Note that CheckBlock can only fail for one of a few reasons:
    3415             :                 // 1. bad-proof-of-work (impossible here, because we've already
    3416             :                 //    accepted the header)
    3417             :                 // 2. merkleroot doesn't match the transactions given (already
    3418             :                 //    caught in FillBlock with READ_STATUS_FAILED, so
    3419             :                 //    impossible here)
    3420             :                 // 3. the block is otherwise invalid (eg invalid coinbase,
    3421             :                 //    block is too big, too many legacy sigops, etc).
    3422             :                 // So if CheckBlock failed, #3 is the only possibility.
    3423             :                 // Under BIP 152, we don't discourage the peer unless proof of work is
    3424             :                 // invalid (we don't require all the stateless checks to have
    3425             :                 // been run).  This is handled below, so just treat this as
    3426             :                 // though the block was successfully read, and rely on the
    3427             :                 // handling in ProcessNewBlock to ensure the block index is
    3428             :                 // updated, etc.
    3429       10051 :                 MarkBlockAsReceived(resp.blockhash); // it is now an empty pointer
    3430             :                 fBlockRead = true;
    3431             :                 // mapBlockSource is used for potentially punishing peers and
    3432             :                 // updating which peers send us compact blocks, so the race
    3433             :                 // between here and cs_main in ProcessNewBlock is fine.
    3434             :                 // BIP 152 permits peers to relay compact blocks after validating
    3435             :                 // the header only; we should not punish peers if the block turns
    3436             :                 // out to be invalid.
    3437       10051 :                 mapBlockSource.emplace(resp.blockhash, std::make_pair(pfrom.GetId(), false));
    3438             :             }
    3439       20104 :         } // Don't hold cs_main when we call into ProcessNewBlock
    3440       10052 :         if (fBlockRead) {
    3441       10051 :             bool fNewBlock = false;
    3442             :             // Since we requested this block (it was in mapBlocksInFlight), force it to be processed,
    3443             :             // even if it would not be a candidate for new tip (missing previous block, chain not long enough, etc)
    3444             :             // This bypasses some anti-DoS logic in AcceptBlock (eg to prevent
    3445             :             // disk-space attacks), but this should be safe due to the
    3446             :             // protections in the compact block handler -- see related comment
    3447             :             // in compact block optimistic reconstruction handling.
    3448       10051 :             m_chainman.ProcessNewBlock(m_chainparams, pblock, /*fForceProcessing=*/true, &fNewBlock);
    3449       10051 :             if (fNewBlock) {
    3450       10050 :                 pfrom.nLastBlockTime = GetTime();
    3451       10050 :             } else {
    3452           1 :                 LOCK(cs_main);
    3453           1 :                 mapBlockSource.erase(pblock->GetHash());
    3454           1 :             }
    3455       10051 :         }
    3456       10052 :         return;
    3457       10052 :     }
    3458             : 
    3459       23278 :     if (msg_type == NetMsgType::HEADERS)
    3460             :     {
    3461             :         // Ignore headers received while importing
    3462        4940 :         if (fImporting || fReindex) {
    3463           0 :             LogPrint(BCLog::NET, "Unexpected headers message received from peer %d\n", pfrom.GetId());
    3464           0 :             return;
    3465             :         }
    3466             : 
    3467        4940 :         std::vector<CBlockHeader> headers;
    3468             : 
    3469             :         // Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks.
    3470        4940 :         unsigned int nCount = ReadCompactSize(vRecv);
    3471        4940 :         if (nCount > MAX_HEADERS_RESULTS) {
    3472           1 :             Misbehaving(pfrom.GetId(), 20, strprintf("headers message size = %u", nCount));
    3473           1 :             return;
    3474             :         }
    3475        4939 :         headers.resize(nCount);
    3476      182530 :         for (unsigned int n = 0; n < nCount; n++) {
    3477      177591 :             vRecv >> headers[n];
    3478      177591 :             ReadCompactSize(vRecv); // ignore tx count; assume it is 0.
    3479             :         }
    3480             : 
    3481        4939 :         return ProcessHeadersMessage(pfrom, headers, /*via_compact_block=*/false);
    3482        4940 :     }
    3483             : 
    3484       18338 :     if (msg_type == NetMsgType::BLOCK)
    3485             :     {
    3486             :         // Ignore block received while importing
    3487       13979 :         if (fImporting || fReindex) {
    3488           0 :             LogPrint(BCLog::NET, "Unexpected block message received from peer %d\n", pfrom.GetId());
    3489           0 :             return;
    3490             :         }
    3491             : 
    3492       13979 :         std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
    3493       13979 :         vRecv >> *pblock;
    3494             : 
    3495       13977 :         LogPrint(BCLog::NET, "received block %s peer=%d\n", pblock->GetHash().ToString(), pfrom.GetId());
    3496             : 
    3497             :         bool forceProcessing = false;
    3498       13977 :         const uint256 hash(pblock->GetHash());
    3499             :         {
    3500       13977 :             LOCK(cs_main);
    3501             :             // Also always process if we requested the block explicitly, as we may
    3502             :             // need it even though it is not a candidate for a new best tip.
    3503       13977 :             forceProcessing |= MarkBlockAsReceived(hash);
    3504             :             // mapBlockSource is only used for punishing peers and setting
    3505             :             // which peers send us compact blocks, so the race between here and
    3506             :             // cs_main in ProcessNewBlock is fine.
    3507       13977 :             mapBlockSource.emplace(hash, std::make_pair(pfrom.GetId(), true));
    3508       13977 :         }
    3509       13977 :         bool fNewBlock = false;
    3510       13977 :         m_chainman.ProcessNewBlock(m_chainparams, pblock, forceProcessing, &fNewBlock);
    3511       13977 :         if (fNewBlock) {
    3512       12491 :             pfrom.nLastBlockTime = GetTime();
    3513       12491 :         } else {
    3514        1486 :             LOCK(cs_main);
    3515        1486 :             mapBlockSource.erase(pblock->GetHash());
    3516        1486 :         }
    3517             :         return;
    3518       13979 :     }
    3519             : 
    3520        4359 :     if (msg_type == NetMsgType::GETADDR) {
    3521             :         // This asymmetric behavior for inbound and outbound connections was introduced
    3522             :         // to prevent a fingerprinting attack: an attacker can send specific fake addresses
    3523             :         // to users' AddrMan and later request them by sending getaddr messages.
    3524             :         // Making nodes which are behind NAT and can only make outgoing connections ignore
    3525             :         // the getaddr message mitigates the attack.
    3526         245 :         if (!pfrom.IsInboundConn()) {
    3527           0 :             LogPrint(BCLog::NET, "Ignoring \"getaddr\" from outbound connection. peer=%d\n", pfrom.GetId());
    3528           0 :             return;
    3529             :         }
    3530         245 :         if (!pfrom.RelayAddrsWithConn()) {
    3531           0 :             LogPrint(BCLog::NET, "Ignoring \"getaddr\" from block-relay-only connection. peer=%d\n", pfrom.GetId());
    3532           0 :             return;
    3533             :         }
    3534             : 
    3535             :         // Only send one GetAddr response per connection to reduce resource waste
    3536             :         //  and discourage addr stamping of INV announcements.
    3537         245 :         if (pfrom.fSentAddr) {
    3538           0 :             LogPrint(BCLog::NET, "Ignoring repeated \"getaddr\". peer=%d\n", pfrom.GetId());
    3539           0 :             return;
    3540             :         }
    3541         245 :         pfrom.fSentAddr = true;
    3542             : 
    3543         245 :         pfrom.vAddrToSend.clear();
    3544         245 :         std::vector<CAddress> vAddr;
    3545         245 :         if (pfrom.HasPermission(PF_ADDR)) {
    3546           1 :             vAddr = m_connman.GetAddresses(MAX_ADDR_TO_SEND, MAX_PCT_ADDR_TO_SEND);
    3547           1 :         } else {
    3548         244 :             vAddr = m_connman.GetAddresses(pfrom.addr.GetNetwork(), MAX_ADDR_TO_SEND, MAX_PCT_ADDR_TO_SEND);
    3549             :         }
    3550         245 :         FastRandomContext insecure_rand;
    3551        5219 :         for (const CAddress &addr : vAddr) {
    3552        4974 :             pfrom.PushAddress(addr, insecure_rand);
    3553             :         }
    3554             :         return;
    3555         245 :     }
    3556             : 
    3557        4114 :     if (msg_type == NetMsgType::MEMPOOL) {
    3558           2 :         if (!(pfrom.GetLocalServices() & NODE_BLOOM) && !pfrom.HasPermission(PF_MEMPOOL))
    3559             :         {
    3560           1 :             if (!pfrom.HasPermission(PF_NOBAN))
    3561             :             {
    3562           1 :                 LogPrint(BCLog::NET, "mempool request with bloom filters disabled, disconnect peer=%d\n", pfrom.GetId());
    3563           1 :                 pfrom.fDisconnect = true;
    3564           1 :             }
    3565           1 :             return;
    3566             :         }
    3567             : 
    3568           1 :         if (m_connman.OutboundTargetReached(false) && !pfrom.HasPermission(PF_MEMPOOL))
    3569             :         {
    3570           0 :             if (!pfrom.HasPermission(PF_NOBAN))
    3571             :             {
    3572           0 :                 LogPrint(BCLog::NET, "mempool request with bandwidth limit reached, disconnect peer=%d\n", pfrom.GetId());
    3573           0 :                 pfrom.fDisconnect = true;
    3574           0 :             }
    3575           0 :             return;
    3576             :         }
    3577             : 
    3578           1 :         if (pfrom.m_tx_relay != nullptr) {
    3579           1 :             LOCK(pfrom.m_tx_relay->cs_tx_inventory);
    3580           1 :             pfrom.m_tx_relay->fSendMempool = true;
    3581           1 :         }
    3582           1 :         return;
    3583             :     }
    3584             : 
    3585        4112 :     if (msg_type == NetMsgType::PING) {
    3586        2540 :         if (pfrom.nVersion > BIP0031_VERSION)
    3587             :         {
    3588        2540 :             uint64_t nonce = 0;
    3589        2540 :             vRecv >> nonce;
    3590             :             // Echo the message back with the nonce. This allows for two useful features:
    3591             :             //
    3592             :             // 1) A remote node can quickly check if the connection is operational
    3593             :             // 2) Remote nodes can measure the latency of the network thread. If this node
    3594             :             //    is overloaded it won't respond to pings quickly and the remote node can
    3595             :             //    avoid sending us more work, like chain download requests.
    3596             :             //
    3597             :             // The nonce stops the remote getting confused between different pings: without
    3598             :             // it, if the remote node sends a ping once per second and this node takes 5
    3599             :             // seconds to respond to each, the 5th ping the remote sends would appear to
    3600             :             // return very quickly.
    3601        2540 :             m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::PONG, nonce));
    3602        2540 :         }
    3603        2540 :         return;
    3604             :     }
    3605             : 
    3606        1572 :     if (msg_type == NetMsgType::PONG) {
    3607         825 :         const auto ping_end = time_received;
    3608         825 :         uint64_t nonce = 0;
    3609         825 :         size_t nAvail = vRecv.in_avail();
    3610             :         bool bPingFinished = false;
    3611         825 :         std::string sProblem;
    3612             : 
    3613         825 :         if (nAvail >= sizeof(nonce)) {
    3614         824 :             vRecv >> nonce;
    3615             : 
    3616             :             // Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
    3617         824 :             if (pfrom.nPingNonceSent != 0) {
    3618         823 :                 if (nonce == pfrom.nPingNonceSent) {
    3619             :                     // Matching pong received, this ping is no longer outstanding
    3620             :                     bPingFinished = true;
    3621         821 :                     const auto ping_time = ping_end - pfrom.m_ping_start.load();
    3622         821 :                     if (ping_time.count() > 0) {
    3623             :                         // Successful ping time measurement, replace previous
    3624         741 :                         pfrom.nPingUsecTime = count_microseconds(ping_time);
    3625         741 :                         pfrom.nMinPingUsecTime = std::min(pfrom.nMinPingUsecTime.load(), count_microseconds(ping_time));
    3626         741 :                     } else {
    3627             :                         // This should never happen
    3628          80 :                         sProblem = "Timing mishap";
    3629             :                     }
    3630         821 :                 } else {
    3631             :                     // Nonce mismatches are normal when pings are overlapping
    3632           2 :                     sProblem = "Nonce mismatch";
    3633           2 :                     if (nonce == 0) {
    3634             :                         // This is most likely a bug in another implementation somewhere; cancel this ping
    3635             :                         bPingFinished = true;
    3636           1 :                         sProblem = "Nonce zero";
    3637             :                     }
    3638             :                 }
    3639             :             } else {
    3640           1 :                 sProblem = "Unsolicited pong without ping";
    3641             :             }
    3642             :         } else {
    3643             :             // This is most likely a bug in another implementation somewhere; cancel this ping
    3644             :             bPingFinished = true;
    3645           1 :             sProblem = "Short payload";
    3646             :         }
    3647             : 
    3648         825 :         if (!(sProblem.empty())) {
    3649          84 :             LogPrint(BCLog::NET, "pong peer=%d: %s, %x expected, %x received, %u bytes\n",
    3650             :                 pfrom.GetId(),
    3651             :                 sProblem,
    3652             :                 pfrom.nPingNonceSent,
    3653             :                 nonce,
    3654             :                 nAvail);
    3655             :         }
    3656         825 :         if (bPingFinished) {
    3657         823 :             pfrom.nPingNonceSent = 0;
    3658         823 :         }
    3659             :         return;
    3660         825 :     }
    3661             : 
    3662         747 :     if (msg_type == NetMsgType::FILTERLOAD) {
    3663          10 :         if (!(pfrom.GetLocalServices() & NODE_BLOOM)) {
    3664           1 :             pfrom.fDisconnect = true;
    3665           1 :             return;
    3666             :         }
    3667           9 :         CBloomFilter filter;
    3668           9 :         vRecv >> filter;
    3669             : 
    3670           9 :         if (!filter.IsWithinSizeConstraints())
    3671             :         {
    3672             :             // There is no excuse for sending a too-large filter
    3673           2 :             Misbehaving(pfrom.GetId(), 100, "too-large bloom filter");
    3674           2 :         }
    3675           7 :         else if (pfrom.m_tx_relay != nullptr)
    3676             :         {
    3677           7 :             LOCK(pfrom.m_tx_relay->cs_filter);
    3678           7 :             pfrom.m_tx_relay->pfilter.reset(new CBloomFilter(filter));
    3679           7 :             pfrom.m_tx_relay->fRelayTxes = true;
    3680           7 :         }
    3681             :         return;
    3682           9 :     }
    3683             : 
    3684         737 :     if (msg_type == NetMsgType::FILTERADD) {
    3685           7 :         if (!(pfrom.GetLocalServices() & NODE_BLOOM)) {
    3686           1 :             pfrom.fDisconnect = true;
    3687           1 :             return;
    3688             :         }
    3689           6 :         std::vector<unsigned char> vData;
    3690           6 :         vRecv >> vData;
    3691             : 
    3692             :         // Nodes must NEVER send a data item > 520 bytes (the max size for a script data object,
    3693             :         // and thus, the maximum size any matched object can have) in a filteradd message
    3694             :         bool bad = false;
    3695           6 :         if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE) {
    3696             :             bad = true;
    3697           6 :         } else if (pfrom.m_tx_relay != nullptr) {
    3698           5 :             LOCK(pfrom.m_tx_relay->cs_filter);
    3699           5 :             if (pfrom.m_tx_relay->pfilter) {
    3700           3 :                 pfrom.m_tx_relay->pfilter->insert(vData);
    3701             :             } else {
    3702             :                 bad = true;
    3703             :             }
    3704           5 :         }
    3705           6 :         if (bad) {
    3706           3 :             Misbehaving(pfrom.GetId(), 100, "bad filteradd message");
    3707           3 :         }
    3708             :         return;
    3709           6 :     }
    3710             : 
    3711         730 :     if (msg_type == NetMsgType::FILTERCLEAR) {
    3712           5 :         if (!(pfrom.GetLocalServices() & NODE_BLOOM)) {
    3713           1 :             pfrom.fDisconnect = true;
    3714           1 :             return;
    3715             :         }
    3716           4 :         if (pfrom.m_tx_relay == nullptr) {
    3717           0 :             return;
    3718             :         }
    3719           4 :         LOCK(pfrom.m_tx_relay->cs_filter);
    3720           4 :         pfrom.m_tx_relay->pfilter = nullptr;
    3721           4 :         pfrom.m_tx_relay->fRelayTxes = true;
    3722             :         return;
    3723           4 :     }
    3724             : 
    3725         725 :     if (msg_type == NetMsgType::FEEFILTER) {
    3726         628 :         CAmount newFeeFilter = 0;
    3727         628 :         vRecv >> newFeeFilter;
    3728         628 :         if (MoneyRange(newFeeFilter)) {
    3729         628 :             if (pfrom.m_tx_relay != nullptr) {
    3730         628 :                 LOCK(pfrom.m_tx_relay->cs_feeFilter);
    3731         628 :                 pfrom.m_tx_relay->minFeeFilter = newFeeFilter;
    3732         628 :             }
    3733         628 :             LogPrint(BCLog::NET, "received: feefilter of %s from peer=%d\n", CFeeRate(newFeeFilter).ToString(), pfrom.GetId());
    3734             :         }
    3735             :         return;
    3736         628 :     }
    3737             : 
    3738          97 :     if (msg_type == NetMsgType::GETCFILTERS) {
    3739           4 :         ProcessGetCFilters(pfrom, vRecv, m_chainparams, m_connman);
    3740           4 :         return;
    3741             :     }
    3742             : 
    3743          93 :     if (msg_type == NetMsgType::GETCFHEADERS) {
    3744           4 :         ProcessGetCFHeaders(pfrom, vRecv, m_chainparams, m_connman);
    3745           4 :         return;
    3746             :     }
    3747             : 
    3748          89 :     if (msg_type == NetMsgType::GETCFCHECKPT) {
    3749           6 :         ProcessGetCFCheckPt(pfrom, vRecv, m_chainparams, m_connman);
    3750           6 :         return;
    3751             :     }
    3752             : 
    3753          83 :     if (msg_type == NetMsgType::NOTFOUND) {
    3754             :         // Remove the NOTFOUND transactions from the peer
    3755           3 :         LOCK(cs_main);
    3756           3 :         CNodeState *state = State(pfrom.GetId());
    3757           3 :         std::vector<CInv> vInv;
    3758           3 :         vRecv >> vInv;
    3759           3 :         if (vInv.size() <= MAX_PEER_TX_IN_FLIGHT + MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
    3760           6 :             for (CInv &inv : vInv) {
    3761           3 :                 if (inv.IsGenTxMsg()) {
    3762             :                     // If we receive a NOTFOUND message for a txid we requested, erase
    3763             :                     // it from our data structures for this peer.
    3764           3 :                     auto in_flight_it = state->m_tx_download.m_tx_in_flight.find(inv.hash);
    3765           3 :                     if (in_flight_it == state->m_tx_download.m_tx_in_flight.end()) {
    3766             :                         // Skip any further work if this is a spurious NOTFOUND
    3767             :                         // message.
    3768           1 :                         continue;
    3769             :                     }
    3770           2 :                     state->m_tx_download.m_tx_in_flight.erase(in_flight_it);
    3771           2 :                     state->m_tx_download.m_tx_announced.erase(inv.hash);
    3772           3 :                 }
    3773           2 :             }
    3774           3 :         }
    3775             :         return;
    3776           3 :     }
    3777             : 
    3778             :     // Ignore unknown commands for extensibility
    3779          80 :     LogPrint(BCLog::NET, "Unknown command \"%s\" from peer=%d\n", SanitizeString(msg_type), pfrom.GetId());
    3780          80 :     return;
    3781       90595 : }
    3782             : 
    3783      289338 : bool PeerManager::MaybeDiscourageAndDisconnect(CNode& pnode)
    3784             : {
    3785      289338 :     const NodeId peer_id{pnode.GetId()};
    3786      289338 :     PeerRef peer = GetPeerRef(peer_id);
    3787      289338 :     if (peer == nullptr) return false;
    3788             : 
    3789             :     {
    3790      289338 :         LOCK(peer->m_misbehavior_mutex);
    3791             : 
    3792             :         // There's nothing to do if the m_should_discourage flag isn't set
    3793      289338 :         if (!peer->m_should_discourage) return false;
    3794             : 
    3795          97 :         peer->m_should_discourage = false;
    3796      289338 :     } // peer.m_misbehavior_mutex
    3797             : 
    3798          97 :     if (pnode.HasPermission(PF_NOBAN)) {
    3799             :         // We never disconnect or discourage peers for bad behavior if they have the NOBAN permission flag
    3800           7 :         LogPrintf("Warning: not punishing noban peer %d!\n", peer_id);
    3801           7 :         return false;
    3802             :     }
    3803             : 
    3804          90 :     if (pnode.IsManualConn()) {
    3805             :         // We never disconnect or discourage manual peers for bad behavior
    3806           1 :         LogPrintf("Warning: not punishing manually connected peer %d!\n", peer_id);
    3807           1 :         return false;
    3808             :     }
    3809             : 
    3810          89 :     if (pnode.addr.IsLocal()) {
    3811             :         // We disconnect local peers for bad behavior but don't discourage (since that would discourage
    3812             :         // all peers on the same local address)
    3813          86 :         LogPrintf("Warning: disconnecting but not discouraging local peer %d!\n", peer_id);
    3814          86 :         pnode.fDisconnect = true;
    3815          86 :         return true;
    3816             :     }
    3817             : 
    3818             :     // Normal case: Disconnect the peer and discourage all nodes sharing the address
    3819           3 :     LogPrintf("Disconnecting and discouraging peer %d!\n", peer_id);
    3820           3 :     if (m_banman) m_banman->Discourage(pnode.addr);
    3821           3 :     m_connman.DisconnectNode(pnode.addr);
    3822           3 :     return true;
    3823      289338 : }
    3824             : 
    3825      289333 : bool PeerManager::ProcessMessages(CNode* pfrom, std::atomic<bool>& interruptMsgProc)
    3826             : {
    3827             :     //
    3828             :     // Message format
    3829             :     //  (4) message start
    3830             :     //  (12) command
    3831             :     //  (4) size
    3832             :     //  (4) checksum
    3833             :     //  (x) data
    3834             :     //
    3835             :     bool fMoreWork = false;
    3836             : 
    3837      289333 :     if (!pfrom->vRecvGetData.empty())
    3838         782 :         ProcessGetData(*pfrom, m_chainparams, m_connman, m_mempool, interruptMsgProc);
    3839             : 
    3840      289333 :     if (!pfrom->orphan_work_set.empty()) {
    3841           3 :         std::list<CTransactionRef> removed_txn;
    3842           3 :         LOCK2(cs_main, g_cs_orphans);
    3843           3 :         ProcessOrphanTx(pfrom->orphan_work_set, removed_txn);
    3844           3 :         for (const CTransactionRef& removedTx : removed_txn) {
    3845           0 :             AddToCompactExtraTransactions(removedTx);
    3846           0 :         }
    3847           3 :     }
    3848             : 
    3849      289333 :     if (pfrom->fDisconnect)
    3850           0 :         return false;
    3851             : 
    3852             :     // this maintains the order of responses
    3853             :     // and prevents vRecvGetData to grow unbounded
    3854      289333 :     if (!pfrom->vRecvGetData.empty()) return true;
    3855      288797 :     if (!pfrom->orphan_work_set.empty()) return true;
    3856             : 
    3857             :     // Don't bother if send buffer is too full to respond anyway
    3858      288795 :     if (pfrom->fPauseSend)
    3859         966 :         return false;
    3860             : 
    3861      287829 :     std::list<CNetMessage> msgs;
    3862             :     {
    3863      287829 :         LOCK(pfrom->cs_vProcessMsg);
    3864      287829 :         if (pfrom->vProcessMsg.empty())
    3865      205063 :             return false;
    3866             :         // Just take one message
    3867       82766 :         msgs.splice(msgs.begin(), pfrom->vProcessMsg, pfrom->vProcessMsg.begin());
    3868       82766 :         pfrom->nProcessQueueSize -= msgs.front().m_raw_message_size;
    3869       82766 :         pfrom->fPauseRecv = pfrom->nProcessQueueSize > m_connman.GetReceiveFloodSize();
    3870       82766 :         fMoreWork = !pfrom->vProcessMsg.empty();
    3871      287829 :     }
    3872       82766 :     CNetMessage& msg(msgs.front());
    3873             : 
    3874       82766 :     msg.SetVersion(pfrom->GetRecvVersion());
    3875             :     // Check network magic
    3876       82766 :     if (!msg.m_valid_netmagic) {
    3877           1 :         LogPrint(BCLog::NET, "PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg.m_command), pfrom->GetId());
    3878           1 :         pfrom->fDisconnect = true;
    3879           1 :         return false;
    3880             :     }
    3881             : 
    3882             :     // Check header
    3883       82765 :     if (!msg.m_valid_header)
    3884             :     {
    3885           1 :         LogPrint(BCLog::NET, "PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(msg.m_command), pfrom->GetId());
    3886           1 :         return fMoreWork;
    3887             :     }
    3888       82764 :     const std::string& msg_type = msg.m_command;
    3889             : 
    3890             :     // Message size
    3891       82764 :     unsigned int nMessageSize = msg.m_message_size;
    3892             : 
    3893             :     // Checksum
    3894       82764 :     CDataStream& vRecv = msg.m_recv;
    3895       82764 :     if (!msg.m_valid_checksum)
    3896             :     {
    3897           1 :         LogPrint(BCLog::NET, "%s(%s, %u bytes): CHECKSUM ERROR peer=%d\n", __func__,
    3898             :            SanitizeString(msg_type), nMessageSize, pfrom->GetId());
    3899           1 :         return fMoreWork;
    3900             :     }
    3901             : 
    3902             :     try {
    3903       82763 :         ProcessMessage(*pfrom, msg_type, vRecv, msg.m_time, interruptMsgProc);
    3904       82759 :         if (interruptMsgProc)
    3905           2 :             return false;
    3906       82757 :         if (!pfrom->vRecvGetData.empty())
    3907         246 :             fMoreWork = true;
    3908           4 :     } catch (const std::exception& e) {
    3909           4 :         LogPrint(BCLog::NET, "%s(%s, %u bytes): Exception '%s' (%s) caught\n", __func__, SanitizeString(msg_type), nMessageSize, e.what(), typeid(e).name());
    3910           4 :     } catch (...) {
    3911           0 :         LogPrint(BCLog::NET, "%s(%s, %u bytes): Unknown exception caught\n", __func__, SanitizeString(msg_type), nMessageSize);
    3912           4 :     }
    3913             : 
    3914       82761 :     return fMoreWork;
    3915      372101 : }
    3916             : 
    3917      287228 : void PeerManager::ConsiderEviction(CNode& pto, int64_t time_in_seconds)
    3918             : {
    3919      287228 :     AssertLockHeld(cs_main);
    3920             : 
    3921      287228 :     CNodeState &state = *State(pto.GetId());
    3922      287228 :     const CNetMsgMaker msgMaker(pto.GetSendVersion());
    3923             : 
    3924      287228 :     if (!state.m_chain_sync.m_protect && pto.IsOutboundOrBlockRelayConn() && state.fSyncStarted) {
    3925             :         // This is an outbound peer subject to disconnection if they don't
    3926             :         // announce a block with as much work as the current tip within
    3927             :         // CHAIN_SYNC_TIMEOUT + HEADERS_RESPONSE_TIME seconds (note: if
    3928             :         // their chain has more work than ours, we should sync to it,
    3929             :         // unless it's invalid, in which case we should find that out and
    3930             :         // disconnect from them elsewhere).
    3931           3 :         if (state.pindexBestKnownBlock != nullptr && state.pindexBestKnownBlock->nChainWork >= ::ChainActive().Tip()->nChainWork) {
    3932           0 :             if (state.m_chain_sync.m_timeout != 0) {
    3933           0 :                 state.m_chain_sync.m_timeout = 0;
    3934           0 :                 state.m_chain_sync.m_work_header = nullptr;
    3935           0 :                 state.m_chain_sync.m_sent_getheaders = false;
    3936           0 :             }
    3937           3 :         } else if (state.m_chain_sync.m_timeout == 0 || (state.m_chain_sync.m_work_header != nullptr && state.pindexBestKnownBlock != nullptr && state.pindexBestKnownBlock->nChainWork >= state.m_chain_sync.m_work_header->nChainWork)) {
    3938             :             // Our best block known by this peer is behind our tip, and we're either noticing
    3939             :             // that for the first time, OR this peer was able to catch up to some earlier point
    3940             :             // where we checked against our tip.
    3941             :             // Either way, set a new timeout based on current tip.
    3942           1 :             state.m_chain_sync.m_timeout = time_in_seconds + CHAIN_SYNC_TIMEOUT;
    3943           1 :             state.m_chain_sync.m_work_header = ::ChainActive().Tip();
    3944           1 :             state.m_chain_sync.m_sent_getheaders = false;
    3945           3 :         } else if (state.m_chain_sync.m_timeout > 0 && time_in_seconds > state.m_chain_sync.m_timeout) {
    3946             :             // No evidence yet that our peer has synced to a chain with work equal to that
    3947             :             // of our tip, when we first detected it was behind. Send a single getheaders
    3948             :             // message to give the peer a chance to update us.
    3949           2 :             if (state.m_chain_sync.m_sent_getheaders) {
    3950             :                 // They've run out of time to catch up!
    3951           1 :                 LogPrintf("Disconnecting outbound peer %d for old chain, best known block = %s\n", pto.GetId(), state.pindexBestKnownBlock != nullptr ? state.pindexBestKnownBlock->GetBlockHash().ToString() : "<none>");
    3952           1 :                 pto.fDisconnect = true;
    3953           1 :             } else {
    3954           1 :                 assert(state.m_chain_sync.m_work_header);
    3955           1 :                 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());
    3956           1 :                 m_connman.PushMessage(&pto, msgMaker.Make(NetMsgType::GETHEADERS, ::ChainActive().GetLocator(state.m_chain_sync.m_work_header->pprev), uint256()));
    3957           1 :                 state.m_chain_sync.m_sent_getheaders = true;
    3958             :                 constexpr int64_t HEADERS_RESPONSE_TIME = 120; // 2 minutes
    3959             :                 // Bump the timeout to allow a response, which could clear the timeout
    3960             :                 // (if the response shows the peer has synced), reset the timeout (if
    3961             :                 // the peer syncs to the required work but not to our tip), or result
    3962             :                 // in disconnect (if we advance to the timeout and pindexBestKnownBlock
    3963             :                 // has not sufficiently progressed)
    3964           1 :                 state.m_chain_sync.m_timeout = time_in_seconds + HEADERS_RESPONSE_TIME;
    3965             :             }
    3966             :         }
    3967             :     }
    3968      287228 : }
    3969             : 
    3970         202 : void PeerManager::EvictExtraOutboundPeers(int64_t time_in_seconds)
    3971             : {
    3972             :     // Check whether we have too many outbound peers
    3973         202 :     int extra_peers = m_connman.GetExtraOutboundCount();
    3974         202 :     if (extra_peers > 0) {
    3975             :         // If we have more outbound peers than we target, disconnect one.
    3976             :         // Pick the outbound peer that least recently announced
    3977             :         // us a new block, with ties broken by choosing the more recent
    3978             :         // connection (higher node id)
    3979           2 :         NodeId worst_peer = -1;
    3980           2 :         int64_t oldest_block_announcement = std::numeric_limits<int64_t>::max();
    3981             : 
    3982          20 :         m_connman.ForEachNode([&](CNode* pnode) {
    3983          18 :             LockAssertion lock(::cs_main);
    3984             : 
    3985             :             // Ignore non-outbound peers, or nodes marked for disconnect already
    3986          18 :             if (!pnode->IsOutboundOrBlockRelayConn() || pnode->fDisconnect) return;
    3987          18 :             CNodeState *state = State(pnode->GetId());
    3988          18 :             if (state == nullptr) return; // shouldn't be possible, but just in case
    3989             :             // Don't evict our protected peers
    3990          18 :             if (state->m_chain_sync.m_protect) return;
    3991             :             // Don't evict our block-relay-only peers.
    3992          18 :             if (pnode->m_tx_relay == nullptr) return;
    3993          18 :             if (state->m_last_block_announcement < oldest_block_announcement || (state->m_last_block_announcement == oldest_block_announcement && pnode->GetId() > worst_peer)) {
    3994          17 :                 worst_peer = pnode->GetId();
    3995          17 :                 oldest_block_announcement = state->m_last_block_announcement;
    3996          17 :             }
    3997          36 :         });
    3998           2 :         if (worst_peer != -1) {
    3999           4 :             bool disconnected = m_connman.ForNode(worst_peer, [&](CNode *pnode) {
    4000           2 :                 LockAssertion lock(::cs_main);
    4001             : 
    4002             :                 // Only disconnect a peer that has been connected to us for
    4003             :                 // some reasonable fraction of our check-frequency, to give
    4004             :                 // it time for new information to have arrived.
    4005             :                 // Also don't disconnect any peer we're trying to download a
    4006             :                 // block from.
    4007           2 :                 CNodeState &state = *State(pnode->GetId());
    4008           2 :                 if (time_in_seconds - pnode->nTimeConnected > MINIMUM_CONNECT_TIME && state.nBlocksInFlight == 0) {
    4009           2 :                     LogPrint(BCLog::NET, "disconnecting extra outbound peer=%d (last block announcement received at time %d)\n", pnode->GetId(), oldest_block_announcement);
    4010           2 :                     pnode->fDisconnect = true;
    4011           2 :                     return true;
    4012             :                 } else {
    4013           0 :                     LogPrint(BCLog::NET, "keeping outbound peer=%d chosen for eviction (connect time: %d, blocks_in_flight: %d)\n", pnode->GetId(), pnode->nTimeConnected, state.nBlocksInFlight);
    4014           0 :                     return false;
    4015             :                 }
    4016           2 :             });
    4017           2 :             if (disconnected) {
    4018             :                 // If we disconnected an extra peer, that means we successfully
    4019             :                 // connected to at least one peer after the last time we
    4020             :                 // detected a stale tip. Don't try any more extra peers until
    4021             :                 // we next detect a stale tip, to limit the load we put on the
    4022             :                 // network from these extra connections.
    4023           2 :                 m_connman.SetTryNewOutboundPeer(false);
    4024           2 :             }
    4025           2 :         }
    4026           2 :     }
    4027         202 : }
    4028             : 
    4029         202 : void PeerManager::CheckForStaleTipAndEvictPeers()
    4030             : {
    4031         202 :     LOCK(cs_main);
    4032             : 
    4033         202 :     int64_t time_in_seconds = GetTime();
    4034             : 
    4035         202 :     EvictExtraOutboundPeers(time_in_seconds);
    4036             : 
    4037         202 :     if (time_in_seconds > m_stale_tip_check_time) {
    4038             :         // Check whether our tip is stale, and if so, allow using an extra
    4039             :         // outbound peer
    4040          92 :         if (!fImporting && !fReindex && m_connman.GetNetworkActive() && m_connman.GetUseAddrmanOutgoing() && TipMayBeStale(m_chainparams.GetConsensus())) {
    4041           1 :             LogPrintf("Potential stale tip detected, will try using extra outbound peer (last tip update: %d seconds ago)\n", time_in_seconds - g_last_tip_update);
    4042           1 :             m_connman.SetTryNewOutboundPeer(true);
    4043          91 :         } else if (m_connman.GetTryNewOutboundPeer()) {
    4044           0 :             m_connman.SetTryNewOutboundPeer(false);
    4045             :         }
    4046          92 :         m_stale_tip_check_time = time_in_seconds + STALE_CHECK_INTERVAL;
    4047          92 :     }
    4048         202 : }
    4049             : 
    4050             : namespace {
    4051             : class CompareInvMempoolOrder
    4052             : {
    4053             :     CTxMemPool *mp;
    4054             :     bool m_wtxid_relay;
    4055             : public:
    4056      140330 :     explicit CompareInvMempoolOrder(CTxMemPool *_mempool, bool use_wtxid)
    4057       70165 :     {
    4058       70165 :         mp = _mempool;
    4059       70165 :         m_wtxid_relay = use_wtxid;
    4060      140330 :     }
    4061             : 
    4062       64099 :     bool operator()(std::set<uint256>::iterator a, std::set<uint256>::iterator b)
    4063             :     {
    4064             :         /* As std::make_heap produces a max-heap, we want the entries with the
    4065             :          * fewest ancestors/highest fee to sort later. */
    4066       64099 :         return mp->CompareDepthAndScore(*b, *a, m_wtxid_relay);
    4067             :     }
    4068             : };
    4069             : }
    4070             : 
    4071      289338 : bool PeerManager::SendMessages(CNode* pto)
    4072             : {
    4073      289338 :     const Consensus::Params& consensusParams = m_chainparams.GetConsensus();
    4074             : 
    4075             :     // We must call MaybeDiscourageAndDisconnect first, to ensure that we'll
    4076             :     // disconnect misbehaving peers even before the version handshake is complete.
    4077      289338 :     if (MaybeDiscourageAndDisconnect(*pto)) return true;
    4078             : 
    4079             :     // Don't send anything until the version handshake is complete
    4080      289249 :     if (!pto->fSuccessfullyConnected || pto->fDisconnect)
    4081        2021 :         return true;
    4082             : 
    4083             :     // If we get here, the outgoing message serialization version is set and can't change.
    4084      287228 :     const CNetMsgMaker msgMaker(pto->GetSendVersion());
    4085             : 
    4086             :     //
    4087             :     // Message: ping
    4088             :     //
    4089             :     bool pingSend = false;
    4090      287228 :     if (pto->fPingQueued) {
    4091             :         // RPC ping request by user
    4092             :         pingSend = true;
    4093           3 :     }
    4094      287228 :     if (pto->nPingNonceSent == 0 && pto->m_ping_start.load() + PING_INTERVAL < GetTime<std::chrono::microseconds>()) {
    4095             :         // Ping automatically sent as a latency probe & keepalive.
    4096             :         pingSend = true;
    4097         825 :     }
    4098      287228 :     if (pingSend) {
    4099         828 :         uint64_t nonce = 0;
    4100        1656 :         while (nonce == 0) {
    4101         828 :             GetRandBytes((unsigned char*)&nonce, sizeof(nonce));
    4102             :         }
    4103         828 :         pto->fPingQueued = false;
    4104         828 :         pto->m_ping_start = GetTime<std::chrono::microseconds>();
    4105         828 :         if (pto->nVersion > BIP0031_VERSION) {
    4106         824 :             pto->nPingNonceSent = nonce;
    4107         824 :             m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::PING, nonce));
    4108         824 :         } else {
    4109             :             // Peer is too old to support ping command with nonce, pong will never arrive.
    4110           4 :             pto->nPingNonceSent = 0;
    4111           4 :             m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::PING));
    4112             :         }
    4113         828 :     }
    4114             : 
    4115             :     {
    4116      287228 :         LOCK(cs_main);
    4117             : 
    4118      287228 :         CNodeState &state = *State(pto->GetId());
    4119             : 
    4120             :         // Address refresh broadcast
    4121      287228 :         int64_t nNow = GetTimeMicros();
    4122      287228 :         auto current_time = GetTime<std::chrono::microseconds>();
    4123             : 
    4124      287228 :         if (pto->RelayAddrsWithConn() && !::ChainstateActive().IsInitialBlockDownload() && pto->m_next_local_addr_send < current_time) {
    4125         662 :             AdvertiseLocal(pto);
    4126         662 :             pto->m_next_local_addr_send = PoissonNextSend(current_time, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
    4127         662 :         }
    4128             : 
    4129             :         //
    4130             :         // Message: addr
    4131             :         //
    4132      287228 :         if (pto->RelayAddrsWithConn() && pto->m_next_addr_send < current_time) {
    4133        1296 :             pto->m_next_addr_send = PoissonNextSend(current_time, AVG_ADDRESS_BROADCAST_INTERVAL);
    4134        1296 :             std::vector<CAddress> vAddr;
    4135        1296 :             vAddr.reserve(pto->vAddrToSend.size());
    4136        1296 :             assert(pto->m_addr_known);
    4137        6284 :             for (const CAddress& addr : pto->vAddrToSend)
    4138             :             {
    4139        4988 :                 if (!pto->m_addr_known->contains(addr.GetKey()))
    4140             :                 {
    4141        4988 :                     pto->m_addr_known->insert(addr.GetKey());
    4142        4988 :                     vAddr.push_back(addr);
    4143             :                     // receiver rejects addr messages larger than MAX_ADDR_TO_SEND
    4144        4988 :                     if (vAddr.size() >= MAX_ADDR_TO_SEND)
    4145             :                     {
    4146           0 :                         m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::ADDR, vAddr));
    4147           0 :                         vAddr.clear();
    4148           0 :                     }
    4149             :                 }
    4150             :             }
    4151        1296 :             pto->vAddrToSend.clear();
    4152        1296 :             if (!vAddr.empty())
    4153          11 :                 m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::ADDR, vAddr));
    4154             :             // we only send the big addr message once
    4155        1296 :             if (pto->vAddrToSend.capacity() > 40)
    4156           6 :                 pto->vAddrToSend.shrink_to_fit();
    4157        1296 :         }
    4158             : 
    4159             :         // Start block sync
    4160      287228 :         if (pindexBestHeader == nullptr)
    4161           0 :             pindexBestHeader = ::ChainActive().Tip();
    4162      287228 :         bool fFetch = state.fPreferredDownload || (nPreferredDownload == 0 && !pto->fClient && !pto->IsAddrFetchConn()); // Download if this is a nice peer, or we have no nice peers and this one might do.
    4163      287228 :         if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex) {
    4164             :             // Only actively request headers from a single peer, unless we're close to today.
    4165        1803 :             if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
    4166         689 :                 state.fSyncStarted = true;
    4167         689 :                 state.nHeadersSyncTimeout = GetTimeMicros() + HEADERS_DOWNLOAD_TIMEOUT_BASE + HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER * (GetAdjustedTime() - pindexBestHeader->GetBlockTime())/(consensusParams.nPowTargetSpacing);
    4168         689 :                 nSyncStarted++;
    4169         689 :                 const CBlockIndex *pindexStart = pindexBestHeader;
    4170             :                 /* If possible, start at the block preceding the currently
    4171             :                    best known header.  This ensures that we always get a
    4172             :                    non-empty list of headers back as long as the peer
    4173             :                    is up-to-date.  With a non-empty response, we can initialise
    4174             :                    the peer's known best block.  This wouldn't be possible
    4175             :                    if we requested starting at pindexBestHeader and
    4176             :                    got back an empty response.  */
    4177         689 :                 if (pindexStart->pprev)
    4178         533 :                     pindexStart = pindexStart->pprev;
    4179         689 :                 LogPrint(BCLog::NET, "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->nHeight, pto->GetId(), pto->nStartingHeight);
    4180         689 :                 m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::GETHEADERS, ::ChainActive().GetLocator(pindexStart), uint256()));
    4181         689 :             }
    4182             :         }
    4183             : 
    4184             :         //
    4185             :         // Try sending block announcements via headers
    4186             :         //
    4187             :         {
    4188             :             // If we have less than MAX_BLOCKS_TO_ANNOUNCE in our
    4189             :             // list of block hashes we're relaying, and our peer wants
    4190             :             // headers announcements, then find the first header
    4191             :             // not yet known to our peer but would connect, and send.
    4192             :             // If no header would connect, or if we have too many
    4193             :             // blocks, or if the peer doesn't want headers, just
    4194             :             // add all to the inv queue.
    4195      287228 :             LOCK(pto->cs_inventory);
    4196      287228 :             std::vector<CBlock> vHeaders;
    4197      574456 :             bool fRevertToInv = ((!state.fPreferHeaders &&
    4198      287228 :                                  (!state.fPreferHeaderAndIDs || pto->vBlockHashesToAnnounce.size() > 1)) ||
    4199      232968 :                                 pto->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE);
    4200      555494 :             const CBlockIndex *pBestIndex = nullptr; // last header queued for delivery
    4201      287228 :             ProcessBlockAvailability(pto->GetId()); // ensure pindexBestKnownBlock is up-to-date
    4202             : 
    4203      287228 :             if (!fRevertToInv) {
    4204      304792 :                 bool fFoundStartingHeader = false;
    4205             :                 // Try to find first header that our peer doesn't have, and
    4206             :                 // then send all headers past that one.  If we come across any
    4207             :                 // headers that aren't on ::ChainActive(), give up.
    4208      269467 :                 for (const uint256 &hash : pto->vBlockHashesToAnnounce) {
    4209       36526 :                     const CBlockIndex* pindex = LookupBlockIndex(hash);
    4210       36526 :                     assert(pindex);
    4211       36526 :                     if (::ChainActive()[pindex->nHeight] != pindex) {
    4212             :                         // Bail out if we reorged away from this block
    4213             :                         fRevertToInv = true;
    4214           1 :                         break;
    4215             :                     }
    4216       36525 :                     if (pBestIndex != nullptr && pindex->pprev != pBestIndex) {
    4217             :                         // This means that the list of blocks to announce don't
    4218             :                         // connect to each other.
    4219             :                         // This shouldn't really be possible to hit during
    4220             :                         // regular operation (because reorgs should take us to
    4221             :                         // a chain that has some block not on the prior chain,
    4222             :                         // which should be caught by the prior check), but one
    4223             :                         // way this could happen is by using invalidateblock /
    4224             :                         // reconsiderblock repeatedly on the tip, causing it to
    4225             :                         // be added multiple times to vBlockHashesToAnnounce.
    4226             :                         // Robustly deal with this rare situation by reverting
    4227             :                         // to an inv.
    4228             :                         fRevertToInv = true;
    4229           0 :                         break;
    4230             :                     }
    4231             :                     pBestIndex = pindex;
    4232       36525 :                     if (fFoundStartingHeader) {
    4233             :                         // add this to the headers message
    4234         499 :                         vHeaders.push_back(pindex->GetBlockHeader());
    4235       36525 :                     } else if (PeerHasHeader(&state, pindex)) {
    4236       29874 :                         continue; // keep looking for the first new block
    4237        6152 :                     } else if (pindex->pprev == nullptr || PeerHasHeader(&state, pindex->pprev)) {
    4238             :                         // Peer doesn't have this header but they do have the prior one.
    4239             :                         // Start sending headers.
    4240             :                         fFoundStartingHeader = true;
    4241        4952 :                         vHeaders.push_back(pindex->GetBlockHeader());
    4242             :                     } else {
    4243             :                         // Peer doesn't have this header or the prior one -- nothing will
    4244             :                         // connect, so bail out.
    4245             :                         fRevertToInv = true;
    4246        1200 :                         break;
    4247             :                     }
    4248       10902 :                 }
    4249      232941 :             }
    4250      287228 :             if (!fRevertToInv && !vHeaders.empty()) {
    4251        4952 :                 if (vHeaders.size() == 1 && state.fPreferHeaderAndIDs) {
    4252             :                     // We only send up to 1 block as header-and-ids, as otherwise
    4253             :                     // probably means we're doing an initial-ish-sync or they're slow
    4254        1075 :                     LogPrint(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", __func__,
    4255             :                             vHeaders.front().GetHash().ToString(), pto->GetId());
    4256             : 
    4257        1075 :                     int nSendFlags = state.fWantsCmpctWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
    4258             : 
    4259             :                     bool fGotBlockFromCache = false;
    4260             :                     {
    4261        1075 :                         LOCK(cs_most_recent_block);
    4262        1075 :                         if (most_recent_block_hash == pBestIndex->GetBlockHash()) {
    4263         116 :                             if (state.fWantsCmpctWitness || !fWitnessesPresentInMostRecentCompactBlock)
    4264          47 :                                 m_connman.PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, *most_recent_compact_block));
    4265             :                             else {
    4266          69 :                                 CBlockHeaderAndShortTxIDs cmpctblock(*most_recent_block, state.fWantsCmpctWitness);
    4267          69 :                                 m_connman.PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
    4268          69 :                             }
    4269             :                             fGotBlockFromCache = true;
    4270         116 :                         }
    4271        1075 :                     }
    4272        1075 :                     if (!fGotBlockFromCache) {
    4273         959 :                         CBlock block;
    4274         959 :                         bool ret = ReadBlockFromDisk(block, pBestIndex, consensusParams);
    4275         959 :                         assert(ret);
    4276         959 :                         CBlockHeaderAndShortTxIDs cmpctblock(block, state.fWantsCmpctWitness);
    4277         959 :                         m_connman.PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
    4278         959 :                     }
    4279        1075 :                     state.pindexBestHeaderSent = pBestIndex;
    4280        4952 :                 } else if (state.fPreferHeaders) {
    4281        3877 :                     if (vHeaders.size() > 1) {
    4282         315 :                         LogPrint(BCLog::NET, "%s: %u headers, range (%s, %s), to peer=%d\n", __func__,
    4283             :                                 vHeaders.size(),
    4284             :                                 vHeaders.front().GetHash().ToString(),
    4285             :                                 vHeaders.back().GetHash().ToString(), pto->GetId());
    4286             :                     } else {
    4287        3562 :                         LogPrint(BCLog::NET, "%s: sending header %s to peer=%d\n", __func__,
    4288             :                                 vHeaders.front().GetHash().ToString(), pto->GetId());
    4289             :                     }
    4290        3877 :                     m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
    4291        3877 :                     state.pindexBestHeaderSent = pBestIndex;
    4292        3877 :                 } else
    4293             :                     fRevertToInv = true;
    4294             :             }
    4295      287228 :             if (fRevertToInv) {
    4296             :                 // If falling back to using an inv, just try to inv the tip.
    4297             :                 // The last entry in vBlockHashesToAnnounce was our tip at some point
    4298             :                 // in the past.
    4299       55488 :                 if (!pto->vBlockHashesToAnnounce.empty()) {
    4300       11669 :                     const uint256 &hashToAnnounce = pto->vBlockHashesToAnnounce.back();
    4301       11669 :                     const CBlockIndex* pindex = LookupBlockIndex(hashToAnnounce);
    4302       11669 :                     assert(pindex);
    4303             : 
    4304             :                     // Warn if we're announcing a block that is not on the main chain.
    4305             :                     // This should be very rare and could be optimized out.
    4306             :                     // Just log for now.
    4307       11669 :                     if (::ChainActive()[pindex->nHeight] != pindex) {
    4308           3 :                         LogPrint(BCLog::NET, "Announcing block %s not on main chain (tip=%s)\n",
    4309             :                             hashToAnnounce.ToString(), ::ChainActive().Tip()->GetBlockHash().ToString());
    4310             :                     }
    4311             : 
    4312             :                     // If the peer's chain has this block, don't inv it back.
    4313       11669 :                     if (!PeerHasHeader(&state, pindex)) {
    4314        9979 :                         pto->vInventoryBlockToSend.push_back(hashToAnnounce);
    4315        9979 :                         LogPrint(BCLog::NET, "%s: sending inv peer=%d hash=%s\n", __func__,
    4316             :                             pto->GetId(), hashToAnnounce.ToString());
    4317             :                     }
    4318       11669 :                 }
    4319             :             }
    4320      287228 :             pto->vBlockHashesToAnnounce.clear();
    4321      287228 :         }
    4322             : 
    4323             :         //
    4324             :         // Message: inventory
    4325             :         //
    4326      287228 :         std::vector<CInv> vInv;
    4327             :         {
    4328      287228 :             LOCK(pto->cs_inventory);
    4329      287228 :             vInv.reserve(std::max<size_t>(pto->vInventoryBlockToSend.size(), INVENTORY_BROADCAST_MAX));
    4330             : 
    4331             :             // Add blocks
    4332      297226 :             for (const uint256& hash : pto->vInventoryBlockToSend) {
    4333        9998 :                 vInv.push_back(CInv(MSG_BLOCK, hash));
    4334        9998 :                 if (vInv.size() == MAX_INV_SZ) {
    4335           0 :                     m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
    4336           0 :                     vInv.clear();
    4337           0 :                 }
    4338             :             }
    4339      287228 :             pto->vInventoryBlockToSend.clear();
    4340             : 
    4341      287228 :             if (pto->m_tx_relay != nullptr) {
    4342      287228 :                 LOCK(pto->m_tx_relay->cs_tx_inventory);
    4343             :                 // Check whether periodic sends should happen
    4344      287228 :                 bool fSendTrickle = pto->HasPermission(PF_NOBAN);
    4345      287228 :                 if (pto->m_tx_relay->nNextInvSend < current_time) {
    4346             :                     fSendTrickle = true;
    4347       16854 :                     if (pto->IsInboundConn()) {
    4348       13350 :                         pto->m_tx_relay->nNextInvSend = std::chrono::microseconds{m_connman.PoissonNextSendInbound(nNow, INVENTORY_BROADCAST_INTERVAL)};
    4349       13350 :                     } else {
    4350             :                         // Use half the delay for outbound peers, as there is less privacy concern for them.
    4351        3504 :                         pto->m_tx_relay->nNextInvSend = PoissonNextSend(current_time, std::chrono::seconds{INVENTORY_BROADCAST_INTERVAL >> 1});
    4352             :                     }
    4353             :                 }
    4354             : 
    4355             :                 // Time to send but the peer has requested we not relay transactions.
    4356      287228 :                 if (fSendTrickle) {
    4357       70165 :                     LOCK(pto->m_tx_relay->cs_filter);
    4358       70165 :                     if (!pto->m_tx_relay->fRelayTxes) pto->m_tx_relay->setInventoryTxToSend.clear();
    4359       70165 :                 }
    4360             : 
    4361             :                 // Respond to BIP35 mempool requests
    4362      287228 :                 if (fSendTrickle && pto->m_tx_relay->fSendMempool) {
    4363           1 :                     auto vtxinfo = m_mempool.infoAll();
    4364           1 :                     pto->m_tx_relay->fSendMempool = false;
    4365           1 :                     CFeeRate filterrate;
    4366             :                     {
    4367           1 :                         LOCK(pto->m_tx_relay->cs_feeFilter);
    4368           1 :                         filterrate = CFeeRate(pto->m_tx_relay->minFeeFilter);
    4369           1 :                     }
    4370             : 
    4371           1 :                     LOCK(pto->m_tx_relay->cs_filter);
    4372             : 
    4373           2 :                     for (const auto& txinfo : vtxinfo) {
    4374           1 :                         const uint256& hash = state.m_wtxid_relay ? txinfo.tx->GetWitnessHash() : txinfo.tx->GetHash();
    4375           1 :                         CInv inv(state.m_wtxid_relay ? MSG_WTX : MSG_TX, hash);
    4376           1 :                         pto->m_tx_relay->setInventoryTxToSend.erase(hash);
    4377             :                         // Don't send transactions that peers will not put into their mempool
    4378           1 :                         if (txinfo.fee < filterrate.GetFee(txinfo.vsize)) {
    4379           0 :                             continue;
    4380             :                         }
    4381           1 :                         if (pto->m_tx_relay->pfilter) {
    4382           1 :                             if (!pto->m_tx_relay->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
    4383             :                         }
    4384           1 :                         pto->m_tx_relay->filterInventoryKnown.insert(hash);
    4385             :                         // Responses to MEMPOOL requests bypass the m_recently_announced_invs filter.
    4386           1 :                         vInv.push_back(inv);
    4387           1 :                         if (vInv.size() == MAX_INV_SZ) {
    4388           0 :                             m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
    4389           0 :                             vInv.clear();
    4390           0 :                         }
    4391           1 :                     }
    4392           1 :                     pto->m_tx_relay->m_last_mempool_req = GetTime<std::chrono::seconds>();
    4393           1 :                 }
    4394             : 
    4395             :                 // Determine transactions to relay
    4396      287228 :                 if (fSendTrickle) {
    4397             :                     // Produce a vector with all candidates for sending
    4398       70165 :                     std::vector<std::set<uint256>::iterator> vInvTx;
    4399       70165 :                     vInvTx.reserve(pto->m_tx_relay->setInventoryTxToSend.size());
    4400       90915 :                     for (std::set<uint256>::iterator it = pto->m_tx_relay->setInventoryTxToSend.begin(); it != pto->m_tx_relay->setInventoryTxToSend.end(); it++) {
    4401       20750 :                         vInvTx.push_back(it);
    4402             :                     }
    4403       70165 :                     CFeeRate filterrate;
    4404             :                     {
    4405       70165 :                         LOCK(pto->m_tx_relay->cs_feeFilter);
    4406       70165 :                         filterrate = CFeeRate(pto->m_tx_relay->minFeeFilter);
    4407       70165 :                     }
    4408             :                     // Topologically and fee-rate sort the inventory we send for privacy and priority reasons.
    4409             :                     // A heap is used so that not all items need sorting if only a few are being sent.
    4410       70165 :                     CompareInvMempoolOrder compareInvMempoolOrder(&m_mempool, state.m_wtxid_relay);
    4411       70165 :                     std::make_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
    4412             :                     // No reason to drain out at many times the network's capacity,
    4413             :                     // especially since we have many peers and some will draw much shorter delays.
    4414       90328 :                     unsigned int nRelayedTransactions = 0;
    4415       70165 :                     LOCK(pto->m_tx_relay->cs_filter);
    4416       90328 :                     while (!vInvTx.empty() && nRelayedTransactions < INVENTORY_BROADCAST_MAX) {
    4417             :                         // Fetch the top element from the heap
    4418       20163 :                         std::pop_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
    4419       20163 :                         std::set<uint256>::iterator it = vInvTx.back();
    4420       20163 :                         vInvTx.pop_back();
    4421       20163 :                         uint256 hash = *it;
    4422       20163 :                         CInv inv(state.m_wtxid_relay ? MSG_WTX : MSG_TX, hash);
    4423             :                         // Remove it from the to-be-sent set
    4424       20163 :                         pto->m_tx_relay->setInventoryTxToSend.erase(it);
    4425             :                         // Check if not in the filter already
    4426       20163 :                         if (pto->m_tx_relay->filterInventoryKnown.contains(hash)) {
    4427        2840 :                             continue;
    4428             :                         }
    4429             :                         // Not in the mempool anymore? don't bother sending it.
    4430       17323 :                         auto txinfo = m_mempool.info(ToGenTxid(inv));
    4431       17323 :                         if (!txinfo.tx) {
    4432        2428 :                             continue;
    4433             :                         }
    4434       14895 :                         auto txid = txinfo.tx->GetHash();
    4435       14895 :                         auto wtxid = txinfo.tx->GetWitnessHash();
    4436             :                         // Peer told you to not send transactions at that feerate? Don't bother sending it.
    4437       14895 :                         if (txinfo.fee < filterrate.GetFee(txinfo.vsize)) {
    4438           4 :                             continue;
    4439             :                         }
    4440       14891 :                         if (pto->m_tx_relay->pfilter && !pto->m_tx_relay->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
    4441             :                         // Send
    4442       14889 :                         State(pto->GetId())->m_recently_announced_invs.insert(hash);
    4443       14889 :                         vInv.push_back(inv);
    4444       14889 :                         nRelayedTransactions++;
    4445             :                         {
    4446             :                             // Expire old relay messages
    4447       14889 :                             while (!vRelayExpiration.empty() && vRelayExpiration.front().first < nNow)
    4448             :                             {
    4449           0 :                                 mapRelay.erase(vRelayExpiration.front().second);
    4450           0 :                                 vRelayExpiration.pop_front();
    4451             :                             }
    4452             : 
    4453       14889 :                             auto ret = mapRelay.emplace(txid, std::move(txinfo.tx));
    4454       14889 :                             if (ret.second) {
    4455       11563 :                                 vRelayExpiration.emplace_back(nNow + std::chrono::microseconds{RELAY_TX_CACHE_TIME}.count(), ret.first);
    4456       11563 :                             }
    4457             :                             // Add wtxid-based lookup into mapRelay as well, so that peers can request by wtxid
    4458       14889 :                             auto ret2 = mapRelay.emplace(wtxid, ret.first->second);
    4459       14889 :                             if (ret2.second) {
    4460         738 :                                 vRelayExpiration.emplace_back(nNow + std::chrono::microseconds{RELAY_TX_CACHE_TIME}.count(), ret2.first);
    4461         738 :                             }
    4462       14889 :                         }
    4463       14889 :                         if (vInv.size() == MAX_INV_SZ) {
    4464           0 :                             m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
    4465           0 :                             vInv.clear();
    4466           0 :                         }
    4467       14889 :                         pto->m_tx_relay->filterInventoryKnown.insert(hash);
    4468       14889 :                         if (hash != txid) {
    4469             :                             // Insert txid into filterInventoryKnown, even for
    4470             :                             // wtxidrelay peers. This prevents re-adding of
    4471             :                             // unconfirmed parents to the recently_announced
    4472             :                             // filter, when a child tx is requested. See
    4473             :                             // ProcessGetData().
    4474        1159 :                             pto->m_tx_relay->filterInventoryKnown.insert(txid);
    4475             :                         }
    4476       20163 :                     }
    4477       70165 :                 }
    4478      287228 :             }
    4479      287228 :         }
    4480      287228 :         if (!vInv.empty())
    4481       18228 :             m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
    4482             : 
    4483             :         // Detect whether we're stalling
    4484      287228 :         current_time = GetTime<std::chrono::microseconds>();
    4485             :         // nNow is the current system time (GetTimeMicros is not mockable) and
    4486             :         // should be replaced by the mockable current_time eventually
    4487      287228 :         nNow = GetTimeMicros();
    4488      287228 :         if (state.nStallingSince && state.nStallingSince < nNow - 1000000 * BLOCK_STALLING_TIMEOUT) {
    4489             :             // Stalling only triggers when the block download window cannot move. During normal steady state,
    4490             :             // the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
    4491             :             // should only happen during initial block download.
    4492           0 :             LogPrintf("Peer=%d is stalling block download, disconnecting\n", pto->GetId());
    4493           0 :             pto->fDisconnect = true;
    4494           0 :             return true;
    4495             :         }
    4496             :         // In case there is a block that has been in flight from this peer for 2 + 0.5 * N times the block interval
    4497             :         // (with N the number of peers from which we're downloading validated blocks), disconnect due to timeout.
    4498             :         // We compensate for other peers to prevent killing off peers due to our own downstream link
    4499             :         // being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
    4500             :         // to unreasonably increase our timeout.
    4501      287228 :         if (state.vBlocksInFlight.size() > 0) {
    4502       20563 :             QueuedBlock &queuedBlock = state.vBlocksInFlight.front();
    4503       20563 :             int nOtherPeersWithValidatedDownloads = nPeersWithValidatedDownloads - (state.nBlocksInFlightValidHeaders > 0);
    4504       20563 :             if (nNow > state.nDownloadingSince + consensusParams.nPowTargetSpacing * (BLOCK_DOWNLOAD_TIMEOUT_BASE + BLOCK_DOWNLOAD_TIMEOUT_PER_PEER * nOtherPeersWithValidatedDownloads)) {
    4505           0 :                 LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", queuedBlock.hash.ToString(), pto->GetId());
    4506           0 :                 pto->fDisconnect = true;
    4507           0 :                 return true;
    4508             :             }
    4509       20563 :         }
    4510             :         // Check for headers sync timeouts
    4511      287228 :         if (state.fSyncStarted && state.nHeadersSyncTimeout < std::numeric_limits<int64_t>::max()) {
    4512             :             // Detect whether this is a stalling initial-headers-sync peer
    4513        7831 :             if (pindexBestHeader->GetBlockTime() <= GetAdjustedTime() - 24 * 60 * 60) {
    4514        7188 :                 if (nNow > state.nHeadersSyncTimeout && nSyncStarted == 1 && (nPreferredDownload - state.fPreferredDownload >= 1)) {
    4515             :                     // Disconnect a peer (without the noban permission) if it is our only sync peer,
    4516             :                     // and we have others we could be using instead.
    4517             :                     // Note: If all our peers are inbound, then we won't
    4518             :                     // disconnect our sync peer for stalling; we have bigger
    4519             :                     // problems if we can't get any outbound peers.
    4520           0 :                     if (!pto->HasPermission(PF_NOBAN)) {
    4521           0 :                         LogPrintf("Timeout downloading headers from peer=%d, disconnecting\n", pto->GetId());
    4522           0 :                         pto->fDisconnect = true;
    4523           0 :                         return true;
    4524             :                     } else {
    4525           0 :                         LogPrintf("Timeout downloading headers from noban peer=%d, not disconnecting\n", pto->GetId());
    4526             :                         // Reset the headers sync state so that we have a
    4527             :                         // chance to try downloading from a different peer.
    4528             :                         // Note: this will also result in at least one more
    4529             :                         // getheaders message to be sent to
    4530             :                         // this peer (eventually).
    4531           0 :                         state.fSyncStarted = false;
    4532           0 :                         nSyncStarted--;
    4533           0 :                         state.nHeadersSyncTimeout = 0;
    4534             :                     }
    4535           0 :                 }
    4536             :             } else {
    4537             :                 // After we've caught up once, reset the timeout so we can't trigger
    4538             :                 // disconnect later.
    4539         643 :                 state.nHeadersSyncTimeout = std::numeric_limits<int64_t>::max();
    4540             :             }
    4541             :         }
    4542             : 
    4543             :         // Check that outbound peers have reasonable chains
    4544             :         // GetTime() is used by this anti-DoS logic so we can test this using mocktime
    4545      287228 :         ConsiderEviction(*pto, GetTime());
    4546             : 
    4547             :         //
    4548             :         // Message: getdata (blocks)
    4549             :         //
    4550      287228 :         std::vector<CInv> vGetData;
    4551      287228 :         if (!pto->fClient && ((fFetch && !pto->m_limited_node) || !::ChainstateActive().IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
    4552      282211 :             std::vector<const CBlockIndex*> vToDownload;
    4553      282211 :             NodeId staller = -1;
    4554      282211 :             FindNextBlocksToDownload(pto->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller, consensusParams);
    4555      292079 :             for (const CBlockIndex *pindex : vToDownload) {
    4556        9868 :                 uint32_t nFetchFlags = GetFetchFlags(*pto);
    4557        9868 :                 vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
    4558        9868 :                 MarkBlockAsInFlight(m_mempool, pto->GetId(), pindex->GetBlockHash(), pindex);
    4559        9868 :                 LogPrint(BCLog::NET, "Requesting block %s (%d) peer=%d\n", pindex->GetBlockHash().ToString(),
    4560             :                     pindex->nHeight, pto->GetId());
    4561             :             }
    4562      282211 :             if (state.nBlocksInFlight == 0 && staller != -1) {
    4563           0 :                 if (State(staller)->nStallingSince == 0) {
    4564           0 :                     State(staller)->nStallingSince = nNow;
    4565           0 :                     LogPrint(BCLog::NET, "Stall started peer=%d\n", staller);
    4566             :                 }
    4567             :             }
    4568      282211 :         }
    4569             : 
    4570             :         //
    4571             :         // Message: getdata (non-blocks)
    4572             :         //
    4573             : 
    4574             :         // For robustness, expire old requests after a long timeout, so that
    4575             :         // we can resume downloading transactions from a peer even if they
    4576             :         // were unresponsive in the past.
    4577             :         // Eventually we should consider disconnecting peers, but this is
    4578             :         // conservative.
    4579      287228 :         if (state.m_tx_download.m_check_expiry_timer <= current_time) {
    4580         852 :             for (auto it=state.m_tx_download.m_tx_in_flight.begin(); it != state.m_tx_download.m_tx_in_flight.end();) {
    4581         100 :                 if (it->second <= current_time - TX_EXPIRY_INTERVAL) {
    4582         100 :                     LogPrint(BCLog::NET, "timeout of inflight tx %s from peer=%d\n", it->first.ToString(), pto->GetId());
    4583         100 :                     state.m_tx_download.m_tx_announced.erase(it->first);
    4584         100 :                     state.m_tx_download.m_tx_in_flight.erase(it++);
    4585         100 :                 } else {
    4586           0 :                     ++it;
    4587             :                 }
    4588             :             }
    4589             :             // On average, we do this check every TX_EXPIRY_INTERVAL. Randomize
    4590             :             // so that we're not doing this for all peers at the same time.
    4591         752 :             state.m_tx_download.m_check_expiry_timer = current_time + TX_EXPIRY_INTERVAL / 2 + GetRandMicros(TX_EXPIRY_INTERVAL);
    4592         752 :         }
    4593             : 
    4594      287228 :         auto& tx_process_time = state.m_tx_download.m_tx_process_time;
    4595      297721 :         while (!tx_process_time.empty() && tx_process_time.begin()->first <= current_time && state.m_tx_download.m_tx_in_flight.size() < MAX_PEER_TX_IN_FLIGHT) {
    4596       10493 :             const GenTxid gtxid = tx_process_time.begin()->second;
    4597             :             // Erase this entry from tx_process_time (it may be added back for
    4598             :             // processing at a later time, see below)
    4599       10493 :             tx_process_time.erase(tx_process_time.begin());
    4600       10493 :             CInv inv(gtxid.IsWtxid() ? MSG_WTX : (MSG_TX | GetFetchFlags(*pto)), gtxid.GetHash());
    4601       10493 :             if (!AlreadyHaveTx(ToGenTxid(inv), m_mempool)) {
    4602             :                 // If this transaction was last requested more than 1 minute ago,
    4603             :                 // then request.
    4604        9579 :                 const auto last_request_time = GetTxRequestTime(gtxid);
    4605        9579 :                 if (last_request_time <= current_time - GETDATA_TX_INTERVAL) {
    4606        9534 :                     LogPrint(BCLog::NET, "Requesting %s peer=%d\n", inv.ToString(), pto->GetId());
    4607        9534 :                     vGetData.push_back(inv);
    4608        9534 :                     if (vGetData.size() >= MAX_GETDATA_SZ) {
    4609           0 :                         m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::GETDATA, vGetData));
    4610           0 :                         vGetData.clear();
    4611           0 :                     }
    4612        9534 :                     UpdateTxRequestTime(gtxid, current_time);
    4613        9534 :                     state.m_tx_download.m_tx_in_flight.emplace(gtxid.GetHash(), current_time);
    4614        9534 :                 } else {
    4615             :                     // This transaction is in flight from someone else; queue
    4616             :                     // up processing to happen after the download times out
    4617             :                     // (with a slight delay for inbound peers, to prefer
    4618             :                     // requests to outbound peers).
    4619             :                     // Don't apply the txid-delay to re-requests of a
    4620             :                     // transaction; the heuristic of delaying requests to
    4621             :                     // txid-relay peers is to save bandwidth on initial
    4622             :                     // announcement of a transaction, and doesn't make sense
    4623             :                     // for a followup request if our first peer times out (and
    4624             :                     // would open us up to an attacker using inbound
    4625             :                     // wtxid-relay to prevent us from requesting transactions
    4626             :                     // from outbound txid-relay peers).
    4627          45 :                     const auto next_process_time = CalculateTxGetDataTime(gtxid, current_time, !state.fPreferredDownload, false);
    4628          45 :                     tx_process_time.emplace(next_process_time, gtxid);
    4629          45 :                 }
    4630        9579 :             } else {
    4631             :                 // We have already seen this transaction, no need to download.
    4632         914 :                 state.m_tx_download.m_tx_announced.erase(gtxid.GetHash());
    4633         914 :                 state.m_tx_download.m_tx_in_flight.erase(gtxid.GetHash());
    4634             :             }
    4635       10493 :         }
    4636             : 
    4637             : 
    4638      287228 :         if (!vGetData.empty())
    4639       13942 :             m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::GETDATA, vGetData));
    4640             : 
    4641             :         //
    4642             :         // Message: feefilter
    4643             :         //
    4644      574452 :         if (pto->m_tx_relay != nullptr && pto->nVersion >= FEEFILTER_VERSION && gArgs.GetBoolArg("-feefilter", DEFAULT_FEEFILTER) &&
    4645      287224 :             !pto->HasPermission(PF_FORCERELAY) // peers with the forcerelay permission should not filter txs to us
    4646             :         ) {
    4647      287118 :             CAmount currentFilter = m_mempool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFeePerK();
    4648      287118 :             int64_t timeNow = GetTimeMicros();
    4649      287118 :             static FeeFilterRounder g_filter_rounder{CFeeRate{DEFAULT_MIN_RELAY_TX_FEE}};
    4650      287118 :             if (m_chainman.ActiveChainstate().IsInitialBlockDownload()) {
    4651             :                 // Received tx-inv messages are discarded when the active
    4652             :                 // chainstate is in IBD, so tell the peer to not send them.
    4653             :                 currentFilter = MAX_MONEY;
    4654        8912 :             } else {
    4655      278206 :                 static const CAmount MAX_FILTER{g_filter_rounder.round(MAX_MONEY)};
    4656      278206 :                 if (pto->m_tx_relay->lastSentFeeFilter == MAX_FILTER) {
    4657             :                     // Send the current filter if we sent MAX_FILTER previously
    4658             :                     // and made it out of IBD.
    4659         175 :                     pto->m_tx_relay->nextSendTimeFeeFilter = timeNow - 1;
    4660         175 :                 }
    4661             :             }
    4662      287118 :             if (timeNow > pto->m_tx_relay->nextSendTimeFeeFilter) {
    4663         961 :                 CAmount filterToSend = g_filter_rounder.round(currentFilter);
    4664             :                 // We always have a fee filter of at least minRelayTxFee
    4665         961 :                 filterToSend = std::max(filterToSend, ::minRelayTxFee.GetFeePerK());
    4666         961 :                 if (filterToSend != pto->m_tx_relay->lastSentFeeFilter) {
    4667         864 :                     m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::FEEFILTER, filterToSend));
    4668         864 :                     pto->m_tx_relay->lastSentFeeFilter = filterToSend;
    4669         864 :                 }
    4670         961 :                 pto->m_tx_relay->nextSendTimeFeeFilter = PoissonNextSend(timeNow, AVG_FEEFILTER_BROADCAST_INTERVAL);
    4671         961 :             }
    4672             :             // If the fee filter has changed substantially and it's still more than MAX_FEEFILTER_CHANGE_DELAY
    4673             :             // until scheduled broadcast, then move the broadcast to within MAX_FEEFILTER_CHANGE_DELAY.
    4674      286295 :             else if (timeNow + MAX_FEEFILTER_CHANGE_DELAY * 1000000 < pto->m_tx_relay->nextSendTimeFeeFilter &&
    4675         569 :                      (currentFilter < 3 * pto->m_tx_relay->lastSentFeeFilter / 4 || currentFilter > 4 * pto->m_tx_relay->lastSentFeeFilter / 3)) {
    4676         569 :                 pto->m_tx_relay->nextSendTimeFeeFilter = timeNow + GetRandInt(MAX_FEEFILTER_CHANGE_DELAY) * 1000000;
    4677         569 :             }
    4678      287118 :         }
    4679      287228 :     } // release cs_main
    4680      287228 :     return true;
    4681      289338 : }
    4682             : 
    4683             : class CNetProcessingCleanup
    4684             : {
    4685             : public:
    4686        1280 :     CNetProcessingCleanup() {}
    4687        1280 :     ~CNetProcessingCleanup() {
    4688             :         // orphan transactions
    4689         640 :         mapOrphanTransactions.clear();
    4690         640 :         mapOrphanTransactionsByPrev.clear();
    4691         640 :         g_orphans_by_wtxid.clear();
    4692        1280 :     }
    4693             : };
    4694         640 : static CNetProcessingCleanup instance_of_cnetprocessingcleanup;

Generated by: LCOV version 1.15