Bitcoin Core  24.1.0
P2P Digital Currency
net_processing.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2021 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <net_processing.h>
7 
8 #include <addrman.h>
9 #include <banman.h>
10 #include <blockencodings.h>
11 #include <blockfilter.h>
12 #include <chainparams.h>
13 #include <consensus/amount.h>
14 #include <consensus/validation.h>
15 #include <deploymentstatus.h>
16 #include <hash.h>
17 #include <headerssync.h>
18 #include <index/blockfilterindex.h>
19 #include <merkleblock.h>
20 #include <netbase.h>
21 #include <netmessagemaker.h>
22 #include <node/blockstorage.h>
23 #include <policy/fees.h>
24 #include <policy/policy.h>
25 #include <policy/settings.h>
26 #include <primitives/block.h>
27 #include <primitives/transaction.h>
28 #include <random.h>
29 #include <reverse_iterator.h>
30 #include <scheduler.h>
31 #include <streams.h>
32 #include <sync.h>
33 #include <timedata.h>
34 #include <tinyformat.h>
35 #include <txmempool.h>
36 #include <txorphanage.h>
37 #include <txrequest.h>
38 #include <util/check.h> // For NDEBUG compile time check
39 #include <util/strencodings.h>
40 #include <util/system.h>
41 #include <util/trace.h>
42 #include <validation.h>
43 
44 #include <algorithm>
45 #include <atomic>
46 #include <chrono>
47 #include <future>
48 #include <memory>
49 #include <optional>
50 #include <typeinfo>
51 
54 using node::fImporting;
55 using node::fPruneMode;
56 using node::fReindex;
57 
59 static constexpr auto RELAY_TX_CACHE_TIME = 15min;
61 static constexpr auto UNCONDITIONAL_RELAY_DELAY = 2min;
64 static constexpr auto HEADERS_DOWNLOAD_TIMEOUT_BASE = 15min;
65 static constexpr auto HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER = 1ms;
67 static constexpr auto HEADERS_RESPONSE_TIME{2min};
71 static constexpr int32_t MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT = 4;
73 static constexpr auto CHAIN_SYNC_TIMEOUT{20min};
75 static constexpr auto STALE_CHECK_INTERVAL{10min};
77 static constexpr auto EXTRA_PEER_CHECK_INTERVAL{45s};
79 static constexpr auto MINIMUM_CONNECT_TIME{30s};
81 static constexpr uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL;
84 static constexpr int STALE_RELAY_AGE_LIMIT = 30 * 24 * 60 * 60;
87 static constexpr int HISTORICAL_BLOCK_AGE = 7 * 24 * 60 * 60;
89 static constexpr auto PING_INTERVAL{2min};
91 static const unsigned int MAX_LOCATOR_SZ = 101;
93 static const unsigned int MAX_INV_SZ = 50000;
96 static constexpr int32_t MAX_PEER_TX_REQUEST_IN_FLIGHT = 100;
101 static constexpr int32_t MAX_PEER_TX_ANNOUNCEMENTS = 5000;
103 static constexpr auto TXID_RELAY_DELAY{2s};
105 static constexpr auto NONPREF_PEER_TX_DELAY{2s};
107 static constexpr auto OVERLOADED_PEER_TX_DELAY{2s};
109 static constexpr auto GETDATA_TX_INTERVAL{60s};
111 static const unsigned int MAX_GETDATA_SZ = 1000;
113 static const int MAX_BLOCKS_IN_TRANSIT_PER_PEER = 16;
115 static constexpr auto BLOCK_STALLING_TIMEOUT{2s};
118 static const unsigned int MAX_HEADERS_RESULTS = 2000;
121 static const int MAX_CMPCTBLOCK_DEPTH = 5;
123 static const int MAX_BLOCKTXN_DEPTH = 10;
128 static const unsigned int BLOCK_DOWNLOAD_WINDOW = 1024;
130 static constexpr double BLOCK_DOWNLOAD_TIMEOUT_BASE = 1;
132 static constexpr double BLOCK_DOWNLOAD_TIMEOUT_PER_PEER = 0.5;
134 static const unsigned int MAX_BLOCKS_TO_ANNOUNCE = 8;
136 static const int MAX_UNCONNECTING_HEADERS = 10;
138 static const unsigned int NODE_NETWORK_LIMITED_MIN_BLOCKS = 288;
140 static constexpr auto AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL{24h};
142 static constexpr auto AVG_ADDRESS_BROADCAST_INTERVAL{30s};
144 static constexpr auto ROTATE_ADDR_RELAY_DEST_INTERVAL{24h};
147 static constexpr auto INBOUND_INVENTORY_BROADCAST_INTERVAL{5s};
151 static constexpr auto OUTBOUND_INVENTORY_BROADCAST_INTERVAL{2s};
154 static constexpr unsigned int INVENTORY_BROADCAST_PER_SECOND = 7;
158 static constexpr unsigned int INVENTORY_MAX_RECENT_RELAY = 3500;
163 static_assert(INVENTORY_MAX_RECENT_RELAY >= INVENTORY_BROADCAST_PER_SECOND * UNCONDITIONAL_RELAY_DELAY / std::chrono::seconds{1}, "INVENTORY_RELAY_MAX too low");
165 static constexpr auto AVG_FEEFILTER_BROADCAST_INTERVAL{10min};
167 static constexpr auto MAX_FEEFILTER_CHANGE_DELAY{5min};
169 static constexpr uint32_t MAX_GETCFILTERS_SIZE = 1000;
171 static constexpr uint32_t MAX_GETCFHEADERS_SIZE = 2000;
173 static constexpr size_t MAX_PCT_ADDR_TO_SEND = 23;
175 static constexpr size_t MAX_ADDR_TO_SEND{1000};
178 static constexpr double MAX_ADDR_RATE_PER_SECOND{0.1};
184 static constexpr uint64_t CMPCTBLOCKS_VERSION{2};
185 
186 // Internal stuff
187 namespace {
189 struct QueuedBlock {
191  const CBlockIndex* pindex;
193  std::unique_ptr<PartiallyDownloadedBlock> partialBlock;
194 };
195 
208 struct Peer {
210  const NodeId m_id{0};
211 
225  const ServiceFlags m_our_services;
227  std::atomic<ServiceFlags> m_their_services{NODE_NONE};
228 
230  Mutex m_misbehavior_mutex;
232  int m_misbehavior_score GUARDED_BY(m_misbehavior_mutex){0};
234  bool m_should_discourage GUARDED_BY(m_misbehavior_mutex){false};
235 
237  Mutex m_block_inv_mutex;
241  std::vector<uint256> m_blocks_for_inv_relay GUARDED_BY(m_block_inv_mutex);
245  std::vector<uint256> m_blocks_for_headers_relay GUARDED_BY(m_block_inv_mutex);
250  uint256 m_continuation_block GUARDED_BY(m_block_inv_mutex) {};
251 
253  std::atomic<int> m_starting_height{-1};
254 
256  std::atomic<uint64_t> m_ping_nonce_sent{0};
258  std::atomic<std::chrono::microseconds> m_ping_start{0us};
260  std::atomic<bool> m_ping_queued{false};
261 
263  std::atomic<bool> m_wtxid_relay{false};
267  CAmount m_fee_filter_sent{0};
270  std::chrono::microseconds m_next_send_feefilter{0};
271 
272  struct TxRelay {
273  mutable RecursiveMutex m_bloom_filter_mutex;
280  bool m_relay_txs GUARDED_BY(m_bloom_filter_mutex){false};
282  std::unique_ptr<CBloomFilter> m_bloom_filter PT_GUARDED_BY(m_bloom_filter_mutex) GUARDED_BY(m_bloom_filter_mutex){nullptr};
283 
284  mutable RecursiveMutex m_tx_inventory_mutex;
288  CRollingBloomFilter m_tx_inventory_known_filter GUARDED_BY(m_tx_inventory_mutex){50000, 0.000001};
293  std::set<uint256> m_tx_inventory_to_send GUARDED_BY(m_tx_inventory_mutex);
296  bool m_send_mempool GUARDED_BY(m_tx_inventory_mutex){false};
298  std::atomic<std::chrono::seconds> m_last_mempool_req{0s};
301  std::chrono::microseconds m_next_inv_send_time{0};
302 
304  std::atomic<CAmount> m_fee_filter_received{0};
305  };
306 
307  /* Initializes a TxRelay struct for this peer. Can be called at most once for a peer. */
308  TxRelay* SetTxRelay() EXCLUSIVE_LOCKS_REQUIRED(!m_tx_relay_mutex)
309  {
310  LOCK(m_tx_relay_mutex);
311  Assume(!m_tx_relay);
312  m_tx_relay = std::make_unique<Peer::TxRelay>();
313  return m_tx_relay.get();
314  };
315 
316  TxRelay* GetTxRelay() EXCLUSIVE_LOCKS_REQUIRED(!m_tx_relay_mutex)
317  {
318  return WITH_LOCK(m_tx_relay_mutex, return m_tx_relay.get());
319  };
320 
322  std::vector<CAddress> m_addrs_to_send;
332  std::unique_ptr<CRollingBloomFilter> m_addr_known;
347  std::atomic_bool m_addr_relay_enabled{false};
349  bool m_getaddr_sent{false};
351  mutable Mutex m_addr_send_times_mutex;
353  std::chrono::microseconds m_next_addr_send GUARDED_BY(m_addr_send_times_mutex){0};
355  std::chrono::microseconds m_next_local_addr_send GUARDED_BY(m_addr_send_times_mutex){0};
358  std::atomic_bool m_wants_addrv2{false};
360  bool m_getaddr_recvd{false};
363  double m_addr_token_bucket{1.0};
365  std::chrono::microseconds m_addr_token_timestamp{GetTime<std::chrono::microseconds>()};
367  std::atomic<uint64_t> m_addr_rate_limited{0};
369  std::atomic<uint64_t> m_addr_processed{0};
370 
372  std::set<uint256> m_orphan_work_set GUARDED_BY(g_cs_orphans);
373 
375  bool m_inv_triggered_getheaders_before_sync{false};
376 
378  Mutex m_getdata_requests_mutex;
380  std::deque<CInv> m_getdata_requests GUARDED_BY(m_getdata_requests_mutex);
381 
383  NodeClock::time_point m_last_getheaders_timestamp{};
384 
386  Mutex m_headers_sync_mutex;
389  std::unique_ptr<HeadersSyncState> m_headers_sync PT_GUARDED_BY(m_headers_sync_mutex) GUARDED_BY(m_headers_sync_mutex) {};
390 
392  std::atomic<bool> m_sent_sendheaders{false};
393 
394  explicit Peer(NodeId id, ServiceFlags our_services)
395  : m_id{id}
396  , m_our_services{our_services}
397  {}
398 
399 private:
400  Mutex m_tx_relay_mutex;
401 
405  std::unique_ptr<TxRelay> m_tx_relay GUARDED_BY(m_tx_relay_mutex);
406 };
407 
408 using PeerRef = std::shared_ptr<Peer>;
409 
416 struct CNodeState {
418  const CBlockIndex* pindexBestKnownBlock{nullptr};
420  uint256 hashLastUnknownBlock{};
422  const CBlockIndex* pindexLastCommonBlock{nullptr};
424  const CBlockIndex* pindexBestHeaderSent{nullptr};
426  int nUnconnectingHeaders{0};
428  bool fSyncStarted{false};
430  std::chrono::microseconds m_headers_sync_timeout{0us};
432  std::chrono::microseconds m_stalling_since{0us};
433  std::list<QueuedBlock> vBlocksInFlight;
435  std::chrono::microseconds m_downloading_since{0us};
436  int nBlocksInFlight{0};
438  bool fPreferredDownload{false};
440  bool fPreferHeaders{false};
442  bool m_requested_hb_cmpctblocks{false};
444  bool m_provides_cmpctblocks{false};
445 
470  struct ChainSyncTimeoutState {
472  std::chrono::seconds m_timeout{0s};
474  const CBlockIndex* m_work_header{nullptr};
476  bool m_sent_getheaders{false};
478  bool m_protect{false};
479  };
480 
481  ChainSyncTimeoutState m_chain_sync;
482 
484  int64_t m_last_block_announcement{0};
485 
487  const bool m_is_inbound;
488 
490  CRollingBloomFilter m_recently_announced_invs = CRollingBloomFilter{INVENTORY_MAX_RECENT_RELAY, 0.000001};
491 
492  CNodeState(bool is_inbound) : m_is_inbound(is_inbound) {}
493 };
494 
495 class PeerManagerImpl final : public PeerManager
496 {
497 public:
498  PeerManagerImpl(CConnman& connman, AddrMan& addrman,
499  BanMan* banman, ChainstateManager& chainman,
500  CTxMemPool& pool, bool ignore_incoming_txs);
501 
503  void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected) override
504  EXCLUSIVE_LOCKS_REQUIRED(!m_recent_confirmed_transactions_mutex);
505  void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex) override
506  EXCLUSIVE_LOCKS_REQUIRED(!m_recent_confirmed_transactions_mutex);
507  void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override
508  EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
509  void BlockChecked(const CBlock& block, const BlockValidationState& state) override
510  EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
511  void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) override
512  EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex);
513 
515  void InitializeNode(CNode& node, ServiceFlags our_services) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
516  void FinalizeNode(const CNode& node) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_headers_presync_mutex);
517  bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt) override
518  EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_recent_confirmed_transactions_mutex, !m_most_recent_block_mutex, !m_headers_presync_mutex);
519  bool SendMessages(CNode* pto) override EXCLUSIVE_LOCKS_REQUIRED(pto->cs_sendProcessing)
520  EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_recent_confirmed_transactions_mutex, !m_most_recent_block_mutex);
521 
523  void StartScheduledTasks(CScheduler& scheduler) override;
524  void CheckForStaleTipAndEvictPeers() override;
525  std::optional<std::string> FetchBlock(NodeId peer_id, const CBlockIndex& block_index) override
526  EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
527  bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
528  bool IgnoresIncomingTxs() override { return m_ignore_incoming_txs; }
529  void SendPings() override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
530  void RelayTransaction(const uint256& txid, const uint256& wtxid) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
531  void SetBestHeight(int height) override { m_best_height = height; };
532  void UnitTestMisbehaving(NodeId peer_id, int howmuch) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex) { Misbehaving(*Assert(GetPeerRef(peer_id)), howmuch, ""); };
533  void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
534  const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) override
535  EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_recent_confirmed_transactions_mutex, !m_most_recent_block_mutex, !m_headers_presync_mutex);
536  void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) override;
537 
538 private:
540  void ConsiderEviction(CNode& pto, Peer& peer, std::chrono::seconds time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
541 
543  void EvictExtraOutboundPeers(std::chrono::seconds now) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
544 
546  void ReattemptInitialBroadcast(CScheduler& scheduler) EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
547 
550  PeerRef GetPeerRef(NodeId id) const EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
551 
554  PeerRef RemovePeer(NodeId id) EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
555 
560  void Misbehaving(Peer& peer, int howmuch, const std::string& message);
561 
572  bool MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationState& state,
573  bool via_compact_block, const std::string& message = "")
574  EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
575 
581  bool MaybePunishNodeForTx(NodeId nodeid, const TxValidationState& state, const std::string& message = "")
582  EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
583 
590  bool MaybeDiscourageAndDisconnect(CNode& pnode, Peer& peer);
591 
592  void ProcessOrphanTx(std::set<uint256>& orphan_work_set) EXCLUSIVE_LOCKS_REQUIRED(cs_main, g_cs_orphans)
593  EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
601  void ProcessHeadersMessage(CNode& pfrom, Peer& peer,
602  std::vector<CBlockHeader>&& headers,
603  bool via_compact_block)
604  EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_headers_presync_mutex);
607  bool CheckHeadersPoW(const std::vector<CBlockHeader>& headers, const Consensus::Params& consensusParams, Peer& peer);
609  arith_uint256 GetAntiDoSWorkThreshold();
613  void HandleFewUnconnectingHeaders(CNode& pfrom, Peer& peer, const std::vector<CBlockHeader>& headers);
615  bool CheckHeadersAreContinuous(const std::vector<CBlockHeader>& headers) const;
634  bool IsContinuationOfLowWorkHeadersSync(Peer& peer, CNode& pfrom,
635  std::vector<CBlockHeader>& headers)
636  EXCLUSIVE_LOCKS_REQUIRED(peer.m_headers_sync_mutex, !m_headers_presync_mutex);
649  bool TryLowWorkHeadersSync(Peer& peer, CNode& pfrom,
650  const CBlockIndex* chain_start_header,
651  std::vector<CBlockHeader>& headers)
652  EXCLUSIVE_LOCKS_REQUIRED(!peer.m_headers_sync_mutex, !m_peer_mutex, !m_headers_presync_mutex);
653 
656  bool IsAncestorOfBestHeaderOrTip(const CBlockIndex* header) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
657 
662  bool MaybeSendGetHeaders(CNode& pfrom, const CBlockLocator& locator, Peer& peer);
664  void HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex* pindexLast);
666  void UpdatePeerStateForReceivedHeaders(CNode& pfrom, const CBlockIndex *pindexLast, bool received_new_header, bool may_have_more_headers);
667 
668  void SendBlockTransactions(CNode& pfrom, Peer& peer, const CBlock& block, const BlockTransactionsRequest& req);
669 
673  void AddTxAnnouncement(const CNode& node, const GenTxid& gtxid, std::chrono::microseconds current_time)
675 
677  void PushNodeVersion(CNode& pnode, const Peer& peer);
678 
683  void MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::microseconds now);
684 
686  void MaybeSendAddr(CNode& node, Peer& peer, std::chrono::microseconds current_time);
687 
689  void MaybeSendSendHeaders(CNode& node, Peer& peer);
690 
698  void RelayAddress(NodeId originator, const CAddress& addr, bool fReachable) EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
699 
701  void MaybeSendFeefilter(CNode& node, Peer& peer, std::chrono::microseconds current_time);
702 
703  const CChainParams& m_chainparams;
704  CConnman& m_connman;
705  AddrMan& m_addrman;
707  BanMan* const m_banman;
708  ChainstateManager& m_chainman;
709  CTxMemPool& m_mempool;
710  TxRequestTracker m_txrequest GUARDED_BY(::cs_main);
711 
713  std::atomic<int> m_best_height{-1};
714 
716  std::chrono::seconds m_stale_tip_check_time GUARDED_BY(cs_main){0s};
717 
719  const bool m_ignore_incoming_txs;
720 
721  bool RejectIncomingTxs(const CNode& peer) const;
722 
725  bool m_initial_sync_finished GUARDED_BY(cs_main){false};
726 
729  mutable Mutex m_peer_mutex;
736  std::map<NodeId, PeerRef> m_peer_map GUARDED_BY(m_peer_mutex);
737 
739  std::map<NodeId, CNodeState> m_node_states GUARDED_BY(cs_main);
740 
742  const CNodeState* State(NodeId pnode) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
744  CNodeState* State(NodeId pnode) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
745 
746  uint32_t GetFetchFlags(const Peer& peer) const;
747 
748  std::atomic<std::chrono::microseconds> m_next_inv_to_inbounds{0us};
749 
751  int nSyncStarted GUARDED_BY(cs_main) = 0;
752 
754  uint256 m_last_block_inv_triggering_headers_sync{};
755 
762  std::map<uint256, std::pair<NodeId, bool>> mapBlockSource GUARDED_BY(cs_main);
763 
765  std::atomic<int> m_wtxid_relay_peers{0};
766 
768  int m_outbound_peers_with_protect_from_disconnect GUARDED_BY(cs_main) = 0;
769 
771  int m_num_preferred_download_peers GUARDED_BY(cs_main){0};
772 
773  bool AlreadyHaveTx(const GenTxid& gtxid)
774  EXCLUSIVE_LOCKS_REQUIRED(cs_main, !m_recent_confirmed_transactions_mutex);
775 
810  CRollingBloomFilter m_recent_rejects GUARDED_BY(::cs_main){120'000, 0.000'001};
811  uint256 hashRecentRejectsChainTip GUARDED_BY(cs_main);
812 
813  /*
814  * Filter for transactions that have been recently confirmed.
815  * We use this to avoid requesting transactions that have already been
816  * confirnmed.
817  *
818  * Blocks don't typically have more than 4000 transactions, so this should
819  * be at least six blocks (~1 hr) worth of transactions that we can store,
820  * inserting both a txid and wtxid for every observed transaction.
821  * If the number of transactions appearing in a block goes up, or if we are
822  * seeing getdata requests more than an hour after initial announcement, we
823  * can increase this number.
824  * The false positive rate of 1/1M should come out to less than 1
825  * transaction per day that would be inadvertently ignored (which is the
826  * same probability that we have in the reject filter).
827  */
828  Mutex m_recent_confirmed_transactions_mutex;
829  CRollingBloomFilter m_recent_confirmed_transactions GUARDED_BY(m_recent_confirmed_transactions_mutex){48'000, 0.000'001};
830 
837  std::chrono::microseconds NextInvToInbounds(std::chrono::microseconds now,
838  std::chrono::seconds average_interval);
839 
840 
841  // All of the following cache a recent block, and are protected by m_most_recent_block_mutex
842  Mutex m_most_recent_block_mutex;
843  std::shared_ptr<const CBlock> m_most_recent_block GUARDED_BY(m_most_recent_block_mutex);
844  std::shared_ptr<const CBlockHeaderAndShortTxIDs> m_most_recent_compact_block GUARDED_BY(m_most_recent_block_mutex);
845  uint256 m_most_recent_block_hash GUARDED_BY(m_most_recent_block_mutex);
846 
847  // Data about the low-work headers synchronization, aggregated from all peers' HeadersSyncStates.
849  Mutex m_headers_presync_mutex;
857  using HeadersPresyncStats = std::pair<arith_uint256, std::optional<std::pair<int64_t, uint32_t>>>;
859  std::map<NodeId, HeadersPresyncStats> m_headers_presync_stats GUARDED_BY(m_headers_presync_mutex) {};
861  NodeId m_headers_presync_bestpeer GUARDED_BY(m_headers_presync_mutex) {-1};
863  std::atomic_bool m_headers_presync_should_signal{false};
864 
866  int m_highest_fast_announce{0};
867 
869  bool IsBlockRequested(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
870 
878  void RemoveBlockRequest(const uint256& hash, std::optional<NodeId> from_peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
879 
880  /* Mark a block as in flight
881  * Returns false, still setting pit, if the block was already in flight from the same peer
882  * pit will only be valid as long as the same cs_main lock is being held
883  */
884  bool BlockRequested(NodeId nodeid, const CBlockIndex& block, std::list<QueuedBlock>::iterator** pit = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
885 
886  bool TipMayBeStale() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
887 
891  void FindNextBlocksToDownload(const Peer& peer, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
892 
893  std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> > mapBlocksInFlight GUARDED_BY(cs_main);
894 
896  std::atomic<std::chrono::seconds> m_last_tip_update{0s};
897 
899  CTransactionRef FindTxForGetData(const CNode& peer, const GenTxid& gtxid, const std::chrono::seconds mempool_req, const std::chrono::seconds now) LOCKS_EXCLUDED(cs_main);
900 
901  void ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic<bool>& interruptMsgProc)
902  EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex, peer.m_getdata_requests_mutex) LOCKS_EXCLUDED(::cs_main);
903 
905  void ProcessBlock(CNode& node, const std::shared_ptr<const CBlock>& block, bool force_processing, bool min_pow_checked);
906 
908  typedef std::map<uint256, CTransactionRef> MapRelay;
909  MapRelay mapRelay GUARDED_BY(cs_main);
911  std::deque<std::pair<std::chrono::microseconds, MapRelay::iterator>> g_relay_expiration GUARDED_BY(cs_main);
912 
919  void MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
920 
922  std::list<NodeId> lNodesAnnouncingHeaderAndIDs GUARDED_BY(cs_main);
923 
925  int m_peers_downloading_from GUARDED_BY(cs_main) = 0;
926 
928  TxOrphanage m_orphanage;
929 
930  void AddToCompactExtraTransactions(const CTransactionRef& tx) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
931 
935  std::vector<std::pair<uint256, CTransactionRef>> vExtraTxnForCompact GUARDED_BY(g_cs_orphans);
937  size_t vExtraTxnForCompactIt GUARDED_BY(g_cs_orphans) = 0;
938 
940  void ProcessBlockAvailability(NodeId nodeid) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
942  void UpdateBlockAvailability(NodeId nodeid, const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
943  bool CanDirectFetch() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
944 
951  bool BlockRequestAllowed(const CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
952  bool AlreadyHaveBlock(const uint256& block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
953  void ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv& inv)
954  EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex);
955 
971  bool PrepareBlockFilterRequest(CNode& node, Peer& peer,
972  BlockFilterType filter_type, uint32_t start_height,
973  const uint256& stop_hash, uint32_t max_height_diff,
974  const CBlockIndex*& stop_index,
975  BlockFilterIndex*& filter_index);
976 
986  void ProcessGetCFilters(CNode& node, Peer& peer, CDataStream& vRecv);
987 
997  void ProcessGetCFHeaders(CNode& node, Peer& peer, CDataStream& vRecv);
998 
1008  void ProcessGetCFCheckPt(CNode& node, Peer& peer, CDataStream& vRecv);
1009 
1016  bool SetupAddressRelay(const CNode& node, Peer& peer);
1017 };
1018 
1019 const CNodeState* PeerManagerImpl::State(NodeId pnode) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1020 {
1021  std::map<NodeId, CNodeState>::const_iterator it = m_node_states.find(pnode);
1022  if (it == m_node_states.end())
1023  return nullptr;
1024  return &it->second;
1025 }
1026 
1028 {
1029  return const_cast<CNodeState*>(std::as_const(*this).State(pnode));
1030 }
1031 
1037 static bool IsAddrCompatible(const Peer& peer, const CAddress& addr)
1038 {
1039  return peer.m_wants_addrv2 || addr.IsAddrV1Compatible();
1040 }
1041 
1042 static void AddAddressKnown(Peer& peer, const CAddress& addr)
1043 {
1044  assert(peer.m_addr_known);
1045  peer.m_addr_known->insert(addr.GetKey());
1046 }
1047 
1048 static void PushAddress(Peer& peer, const CAddress& addr, FastRandomContext& insecure_rand)
1049 {
1050  // Known checking here is only to save space from duplicates.
1051  // Before sending, we'll filter it again for known addresses that were
1052  // added after addresses were pushed.
1053  assert(peer.m_addr_known);
1054  if (addr.IsValid() && !peer.m_addr_known->contains(addr.GetKey()) && IsAddrCompatible(peer, addr)) {
1055  if (peer.m_addrs_to_send.size() >= MAX_ADDR_TO_SEND) {
1056  peer.m_addrs_to_send[insecure_rand.randrange(peer.m_addrs_to_send.size())] = addr;
1057  } else {
1058  peer.m_addrs_to_send.push_back(addr);
1059  }
1060  }
1061 }
1062 
1063 static void AddKnownTx(Peer& peer, const uint256& hash)
1064 {
1065  auto tx_relay = peer.GetTxRelay();
1066  if (!tx_relay) return;
1067 
1068  LOCK(tx_relay->m_tx_inventory_mutex);
1069  tx_relay->m_tx_inventory_known_filter.insert(hash);
1070 }
1071 
1073 static bool CanServeBlocks(const Peer& peer)
1074 {
1075  return peer.m_their_services & (NODE_NETWORK|NODE_NETWORK_LIMITED);
1076 }
1077 
1080 static bool IsLimitedPeer(const Peer& peer)
1081 {
1082  return (!(peer.m_their_services & NODE_NETWORK) &&
1083  (peer.m_their_services & NODE_NETWORK_LIMITED));
1084 }
1085 
1087 static bool CanServeWitnesses(const Peer& peer)
1088 {
1089  return peer.m_their_services & NODE_WITNESS;
1090 }
1091 
1092 std::chrono::microseconds PeerManagerImpl::NextInvToInbounds(std::chrono::microseconds now,
1093  std::chrono::seconds average_interval)
1094 {
1095  if (m_next_inv_to_inbounds.load() < now) {
1096  // If this function were called from multiple threads simultaneously
1097  // it would possible that both update the next send variable, and return a different result to their caller.
1098  // This is not possible in practice as only the net processing thread invokes this function.
1099  m_next_inv_to_inbounds = GetExponentialRand(now, average_interval);
1100  }
1101  return m_next_inv_to_inbounds;
1102 }
1103 
1104 bool PeerManagerImpl::IsBlockRequested(const uint256& hash)
1105 {
1106  return mapBlocksInFlight.find(hash) != mapBlocksInFlight.end();
1107 }
1108 
1109 void PeerManagerImpl::RemoveBlockRequest(const uint256& hash, std::optional<NodeId> from_peer)
1110 {
1111  auto it = mapBlocksInFlight.find(hash);
1112  if (it == mapBlocksInFlight.end()) {
1113  // Block was not requested
1114  return;
1115  }
1116 
1117  auto [node_id, list_it] = it->second;
1118 
1119  if (from_peer && node_id != *from_peer) {
1120  // Block was requested by another peer
1121  return;
1122  }
1123 
1124  CNodeState *state = State(node_id);
1125  assert(state != nullptr);
1126 
1127  if (state->vBlocksInFlight.begin() == list_it) {
1128  // First block on the queue was received, update the start download time for the next one
1129  state->m_downloading_since = std::max(state->m_downloading_since, GetTime<std::chrono::microseconds>());
1130  }
1131  state->vBlocksInFlight.erase(list_it);
1132 
1133  state->nBlocksInFlight--;
1134  if (state->nBlocksInFlight == 0) {
1135  // Last validated block on the queue was received.
1136  m_peers_downloading_from--;
1137  }
1138  state->m_stalling_since = 0us;
1139  mapBlocksInFlight.erase(it);
1140 }
1141 
1142 bool PeerManagerImpl::BlockRequested(NodeId nodeid, const CBlockIndex& block, std::list<QueuedBlock>::iterator** pit)
1143 {
1144  const uint256& hash{block.GetBlockHash()};
1145 
1146  CNodeState *state = State(nodeid);
1147  assert(state != nullptr);
1148 
1149  // Short-circuit most stuff in case it is from the same node
1150  std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
1151  if (itInFlight != mapBlocksInFlight.end() && itInFlight->second.first == nodeid) {
1152  if (pit) {
1153  *pit = &itInFlight->second.second;
1154  }
1155  return false;
1156  }
1157 
1158  // Make sure it's not listed somewhere already.
1159  RemoveBlockRequest(hash, std::nullopt);
1160 
1161  std::list<QueuedBlock>::iterator it = state->vBlocksInFlight.insert(state->vBlocksInFlight.end(),
1162  {&block, std::unique_ptr<PartiallyDownloadedBlock>(pit ? new PartiallyDownloadedBlock(&m_mempool) : nullptr)});
1163  state->nBlocksInFlight++;
1164  if (state->nBlocksInFlight == 1) {
1165  // We're starting a block download (batch) from this peer.
1166  state->m_downloading_since = GetTime<std::chrono::microseconds>();
1167  m_peers_downloading_from++;
1168  }
1169  itInFlight = mapBlocksInFlight.insert(std::make_pair(hash, std::make_pair(nodeid, it))).first;
1170  if (pit) {
1171  *pit = &itInFlight->second.second;
1172  }
1173  return true;
1174 }
1175 
1176 void PeerManagerImpl::MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid)
1177 {
1179 
1180  // When in -blocksonly mode, never request high-bandwidth mode from peers. Our
1181  // mempool will not contain the transactions necessary to reconstruct the
1182  // compact block.
1183  if (m_ignore_incoming_txs) return;
1184 
1185  CNodeState* nodestate = State(nodeid);
1186  if (!nodestate || !nodestate->m_provides_cmpctblocks) {
1187  // Don't request compact blocks if the peer has not signalled support
1188  return;
1189  }
1190 
1191  int num_outbound_hb_peers = 0;
1192  for (std::list<NodeId>::iterator it = lNodesAnnouncingHeaderAndIDs.begin(); it != lNodesAnnouncingHeaderAndIDs.end(); it++) {
1193  if (*it == nodeid) {
1194  lNodesAnnouncingHeaderAndIDs.erase(it);
1195  lNodesAnnouncingHeaderAndIDs.push_back(nodeid);
1196  return;
1197  }
1198  CNodeState *state = State(*it);
1199  if (state != nullptr && !state->m_is_inbound) ++num_outbound_hb_peers;
1200  }
1201  if (nodestate->m_is_inbound) {
1202  // If we're adding an inbound HB peer, make sure we're not removing
1203  // our last outbound HB peer in the process.
1204  if (lNodesAnnouncingHeaderAndIDs.size() >= 3 && num_outbound_hb_peers == 1) {
1205  CNodeState *remove_node = State(lNodesAnnouncingHeaderAndIDs.front());
1206  if (remove_node != nullptr && !remove_node->m_is_inbound) {
1207  // Put the HB outbound peer in the second slot, so that it
1208  // doesn't get removed.
1209  std::swap(lNodesAnnouncingHeaderAndIDs.front(), *std::next(lNodesAnnouncingHeaderAndIDs.begin()));
1210  }
1211  }
1212  }
1213  m_connman.ForNode(nodeid, [this](CNode* pfrom) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
1215  if (lNodesAnnouncingHeaderAndIDs.size() >= 3) {
1216  // As per BIP152, we only get 3 of our peers to announce
1217  // blocks using compact encodings.
1218  m_connman.ForNode(lNodesAnnouncingHeaderAndIDs.front(), [this](CNode* pnodeStop){
1219  m_connman.PushMessage(pnodeStop, CNetMsgMaker(pnodeStop->GetCommonVersion()).Make(NetMsgType::SENDCMPCT, /*high_bandwidth=*/false, /*version=*/CMPCTBLOCKS_VERSION));
1220  // save BIP152 bandwidth state: we select peer to be low-bandwidth
1221  pnodeStop->m_bip152_highbandwidth_to = false;
1222  return true;
1223  });
1224  lNodesAnnouncingHeaderAndIDs.pop_front();
1225  }
1226  m_connman.PushMessage(pfrom, CNetMsgMaker(pfrom->GetCommonVersion()).Make(NetMsgType::SENDCMPCT, /*high_bandwidth=*/true, /*version=*/CMPCTBLOCKS_VERSION));
1227  // save BIP152 bandwidth state: we select peer to be high-bandwidth
1228  pfrom->m_bip152_highbandwidth_to = true;
1229  lNodesAnnouncingHeaderAndIDs.push_back(pfrom->GetId());
1230  return true;
1231  });
1232 }
1233 
1234 bool PeerManagerImpl::TipMayBeStale()
1235 {
1237  const Consensus::Params& consensusParams = m_chainparams.GetConsensus();
1238  if (m_last_tip_update.load() == 0s) {
1239  m_last_tip_update = GetTime<std::chrono::seconds>();
1240  }
1241  return m_last_tip_update.load() < GetTime<std::chrono::seconds>() - std::chrono::seconds{consensusParams.nPowTargetSpacing * 3} && mapBlocksInFlight.empty();
1242 }
1243 
1244 bool PeerManagerImpl::CanDirectFetch()
1245 {
1246  return m_chainman.ActiveChain().Tip()->Time() > GetAdjustedTime() - m_chainparams.GetConsensus().PowTargetSpacing() * 20;
1247 }
1248 
1249 static bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1250 {
1251  if (state->pindexBestKnownBlock && pindex == state->pindexBestKnownBlock->GetAncestor(pindex->nHeight))
1252  return true;
1253  if (state->pindexBestHeaderSent && pindex == state->pindexBestHeaderSent->GetAncestor(pindex->nHeight))
1254  return true;
1255  return false;
1256 }
1257 
1258 void PeerManagerImpl::ProcessBlockAvailability(NodeId nodeid) {
1259  CNodeState *state = State(nodeid);
1260  assert(state != nullptr);
1261 
1262  if (!state->hashLastUnknownBlock.IsNull()) {
1263  const CBlockIndex* pindex = m_chainman.m_blockman.LookupBlockIndex(state->hashLastUnknownBlock);
1264  if (pindex && pindex->nChainWork > 0) {
1265  if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork) {
1266  state->pindexBestKnownBlock = pindex;
1267  }
1268  state->hashLastUnknownBlock.SetNull();
1269  }
1270  }
1271 }
1272 
1273 void PeerManagerImpl::UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
1274  CNodeState *state = State(nodeid);
1275  assert(state != nullptr);
1276 
1277  ProcessBlockAvailability(nodeid);
1278 
1279  const CBlockIndex* pindex = m_chainman.m_blockman.LookupBlockIndex(hash);
1280  if (pindex && pindex->nChainWork > 0) {
1281  // An actually better block was announced.
1282  if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork) {
1283  state->pindexBestKnownBlock = pindex;
1284  }
1285  } else {
1286  // An unknown block was announced; just assume that the latest one is the best one.
1287  state->hashLastUnknownBlock = hash;
1288  }
1289 }
1290 
1291 void PeerManagerImpl::FindNextBlocksToDownload(const Peer& peer, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller)
1292 {
1293  if (count == 0)
1294  return;
1295 
1296  vBlocks.reserve(vBlocks.size() + count);
1297  CNodeState *state = State(peer.m_id);
1298  assert(state != nullptr);
1299 
1300  // Make sure pindexBestKnownBlock is up to date, we'll need it.
1301  ProcessBlockAvailability(peer.m_id);
1302 
1303  if (state->pindexBestKnownBlock == nullptr || state->pindexBestKnownBlock->nChainWork < m_chainman.ActiveChain().Tip()->nChainWork || state->pindexBestKnownBlock->nChainWork < nMinimumChainWork) {
1304  // This peer has nothing interesting.
1305  return;
1306  }
1307 
1308  if (state->pindexLastCommonBlock == nullptr) {
1309  // Bootstrap quickly by guessing a parent of our best tip is the forking point.
1310  // Guessing wrong in either direction is not a problem.
1311  state->pindexLastCommonBlock = m_chainman.ActiveChain()[std::min(state->pindexBestKnownBlock->nHeight, m_chainman.ActiveChain().Height())];
1312  }
1313 
1314  // If the peer reorganized, our previous pindexLastCommonBlock may not be an ancestor
1315  // of its current tip anymore. Go back enough to fix that.
1316  state->pindexLastCommonBlock = LastCommonAncestor(state->pindexLastCommonBlock, state->pindexBestKnownBlock);
1317  if (state->pindexLastCommonBlock == state->pindexBestKnownBlock)
1318  return;
1319 
1320  std::vector<const CBlockIndex*> vToFetch;
1321  const CBlockIndex *pindexWalk = state->pindexLastCommonBlock;
1322  // Never fetch further than the best block we know the peer has, or more than BLOCK_DOWNLOAD_WINDOW + 1 beyond the last
1323  // linked block we have in common with this peer. The +1 is so we can detect stalling, namely if we would be able to
1324  // download that next block if the window were 1 larger.
1325  int nWindowEnd = state->pindexLastCommonBlock->nHeight + BLOCK_DOWNLOAD_WINDOW;
1326  int nMaxHeight = std::min<int>(state->pindexBestKnownBlock->nHeight, nWindowEnd + 1);
1327  NodeId waitingfor = -1;
1328  while (pindexWalk->nHeight < nMaxHeight) {
1329  // Read up to 128 (or more, if more blocks than that are needed) successors of pindexWalk (towards
1330  // pindexBestKnownBlock) into vToFetch. We fetch 128, because CBlockIndex::GetAncestor may be as expensive
1331  // as iterating over ~100 CBlockIndex* entries anyway.
1332  int nToFetch = std::min(nMaxHeight - pindexWalk->nHeight, std::max<int>(count - vBlocks.size(), 128));
1333  vToFetch.resize(nToFetch);
1334  pindexWalk = state->pindexBestKnownBlock->GetAncestor(pindexWalk->nHeight + nToFetch);
1335  vToFetch[nToFetch - 1] = pindexWalk;
1336  for (unsigned int i = nToFetch - 1; i > 0; i--) {
1337  vToFetch[i - 1] = vToFetch[i]->pprev;
1338  }
1339 
1340  // Iterate over those blocks in vToFetch (in forward direction), adding the ones that
1341  // are not yet downloaded and not in flight to vBlocks. In the meantime, update
1342  // pindexLastCommonBlock as long as all ancestors are already downloaded, or if it's
1343  // already part of our chain (and therefore don't need it even if pruned).
1344  for (const CBlockIndex* pindex : vToFetch) {
1345  if (!pindex->IsValid(BLOCK_VALID_TREE)) {
1346  // We consider the chain that this peer is on invalid.
1347  return;
1348  }
1349  if (!CanServeWitnesses(peer) && DeploymentActiveAt(*pindex, m_chainman, Consensus::DEPLOYMENT_SEGWIT)) {
1350  // We wouldn't download this block or its descendants from this peer.
1351  return;
1352  }
1353  if (pindex->nStatus & BLOCK_HAVE_DATA || m_chainman.ActiveChain().Contains(pindex)) {
1354  if (pindex->HaveTxsDownloaded())
1355  state->pindexLastCommonBlock = pindex;
1356  } else if (!IsBlockRequested(pindex->GetBlockHash())) {
1357  // The block is not already downloaded, and not yet in flight.
1358  if (pindex->nHeight > nWindowEnd) {
1359  // We reached the end of the window.
1360  if (vBlocks.size() == 0 && waitingfor != peer.m_id) {
1361  // We aren't able to fetch anything, but we would be if the download window was one larger.
1362  nodeStaller = waitingfor;
1363  }
1364  return;
1365  }
1366  vBlocks.push_back(pindex);
1367  if (vBlocks.size() == count) {
1368  return;
1369  }
1370  } else if (waitingfor == -1) {
1371  // This is the first already-in-flight block.
1372  waitingfor = mapBlocksInFlight[pindex->GetBlockHash()].first;
1373  }
1374  }
1375  }
1376 }
1377 
1378 } // namespace
1379 
1380 void PeerManagerImpl::PushNodeVersion(CNode& pnode, const Peer& peer)
1381 {
1382  uint64_t my_services{peer.m_our_services};
1383  const int64_t nTime{count_seconds(GetTime<std::chrono::seconds>())};
1384  uint64_t nonce = pnode.GetLocalNonce();
1385  const int nNodeStartingHeight{m_best_height};
1386  NodeId nodeid = pnode.GetId();
1387  CAddress addr = pnode.addr;
1388 
1389  CService addr_you = addr.IsRoutable() && !IsProxy(addr) && addr.IsAddrV1Compatible() ? addr : CService();
1390  uint64_t your_services{addr.nServices};
1391 
1392  const bool tx_relay = !m_ignore_incoming_txs && !pnode.IsBlockOnlyConn() && !pnode.IsFeelerConn();
1393  m_connman.PushMessage(&pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERSION, PROTOCOL_VERSION, my_services, nTime,
1394  your_services, addr_you, // Together the pre-version-31402 serialization of CAddress "addrYou" (without nTime)
1395  my_services, CService(), // Together the pre-version-31402 serialization of CAddress "addrMe" (without nTime)
1396  nonce, strSubVersion, nNodeStartingHeight, tx_relay));
1397 
1398  if (fLogIPs) {
1399  LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, them=%s, txrelay=%d, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addr_you.ToString(), tx_relay, nodeid);
1400  } else {
1401  LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, txrelay=%d, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, tx_relay, nodeid);
1402  }
1403 }
1404 
1405 void PeerManagerImpl::AddTxAnnouncement(const CNode& node, const GenTxid& gtxid, std::chrono::microseconds current_time)
1406 {
1407  AssertLockHeld(::cs_main); // For m_txrequest
1408  NodeId nodeid = node.GetId();
1409  if (!node.HasPermission(NetPermissionFlags::Relay) && m_txrequest.Count(nodeid) >= MAX_PEER_TX_ANNOUNCEMENTS) {
1410  // Too many queued announcements from this peer
1411  return;
1412  }
1413  const CNodeState* state = State(nodeid);
1414 
1415  // Decide the TxRequestTracker parameters for this announcement:
1416  // - "preferred": if fPreferredDownload is set (= outbound, or NetPermissionFlags::NoBan permission)
1417  // - "reqtime": current time plus delays for:
1418  // - NONPREF_PEER_TX_DELAY for announcements from non-preferred connections
1419  // - TXID_RELAY_DELAY for txid announcements while wtxid peers are available
1420  // - OVERLOADED_PEER_TX_DELAY for announcements from peers which have at least
1421  // MAX_PEER_TX_REQUEST_IN_FLIGHT requests in flight (and don't have NetPermissionFlags::Relay).
1422  auto delay{0us};
1423  const bool preferred = state->fPreferredDownload;
1424  if (!preferred) delay += NONPREF_PEER_TX_DELAY;
1425  if (!gtxid.IsWtxid() && m_wtxid_relay_peers > 0) delay += TXID_RELAY_DELAY;
1426  const bool overloaded = !node.HasPermission(NetPermissionFlags::Relay) &&
1427  m_txrequest.CountInFlight(nodeid) >= MAX_PEER_TX_REQUEST_IN_FLIGHT;
1428  if (overloaded) delay += OVERLOADED_PEER_TX_DELAY;
1429  m_txrequest.ReceivedInv(nodeid, gtxid, preferred, current_time + delay);
1430 }
1431 
1432 void PeerManagerImpl::UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds)
1433 {
1434  LOCK(cs_main);
1435  CNodeState *state = State(node);
1436  if (state) state->m_last_block_announcement = time_in_seconds;
1437 }
1438 
1439 void PeerManagerImpl::InitializeNode(CNode& node, ServiceFlags our_services)
1440 {
1441  NodeId nodeid = node.GetId();
1442  {
1443  LOCK(cs_main);
1444  m_node_states.emplace_hint(m_node_states.end(), std::piecewise_construct, std::forward_as_tuple(nodeid), std::forward_as_tuple(node.IsInboundConn()));
1445  assert(m_txrequest.Count(nodeid) == 0);
1446  }
1447  PeerRef peer = std::make_shared<Peer>(nodeid, our_services);
1448  {
1449  LOCK(m_peer_mutex);
1450  m_peer_map.emplace_hint(m_peer_map.end(), nodeid, peer);
1451  }
1452  if (!node.IsInboundConn()) {
1453  PushNodeVersion(node, *peer);
1454  }
1455 }
1456 
1457 void PeerManagerImpl::ReattemptInitialBroadcast(CScheduler& scheduler)
1458 {
1459  std::set<uint256> unbroadcast_txids = m_mempool.GetUnbroadcastTxs();
1460 
1461  for (const auto& txid : unbroadcast_txids) {
1462  CTransactionRef tx = m_mempool.get(txid);
1463 
1464  if (tx != nullptr) {
1465  RelayTransaction(txid, tx->GetWitnessHash());
1466  } else {
1467  m_mempool.RemoveUnbroadcastTx(txid, true);
1468  }
1469  }
1470 
1471  // Schedule next run for 10-15 minutes in the future.
1472  // We add randomness on every cycle to avoid the possibility of P2P fingerprinting.
1473  const std::chrono::milliseconds delta = 10min + GetRandMillis(5min);
1474  scheduler.scheduleFromNow([&] { ReattemptInitialBroadcast(scheduler); }, delta);
1475 }
1476 
1477 void PeerManagerImpl::FinalizeNode(const CNode& node)
1478 {
1479  NodeId nodeid = node.GetId();
1480  int misbehavior{0};
1481  {
1482  LOCK(cs_main);
1483  {
1484  // We remove the PeerRef from g_peer_map here, but we don't always
1485  // destruct the Peer. Sometimes another thread is still holding a
1486  // PeerRef, so the refcount is >= 1. Be careful not to do any
1487  // processing here that assumes Peer won't be changed before it's
1488  // destructed.
1489  PeerRef peer = RemovePeer(nodeid);
1490  assert(peer != nullptr);
1491  misbehavior = WITH_LOCK(peer->m_misbehavior_mutex, return peer->m_misbehavior_score);
1492  m_wtxid_relay_peers -= peer->m_wtxid_relay;
1493  assert(m_wtxid_relay_peers >= 0);
1494  }
1495  CNodeState *state = State(nodeid);
1496  assert(state != nullptr);
1497 
1498  if (state->fSyncStarted)
1499  nSyncStarted--;
1500 
1501  for (const QueuedBlock& entry : state->vBlocksInFlight) {
1502  mapBlocksInFlight.erase(entry.pindex->GetBlockHash());
1503  }
1504  WITH_LOCK(g_cs_orphans, m_orphanage.EraseForPeer(nodeid));
1505  m_txrequest.DisconnectedPeer(nodeid);
1506  m_num_preferred_download_peers -= state->fPreferredDownload;
1507  m_peers_downloading_from -= (state->nBlocksInFlight != 0);
1508  assert(m_peers_downloading_from >= 0);
1509  m_outbound_peers_with_protect_from_disconnect -= state->m_chain_sync.m_protect;
1510  assert(m_outbound_peers_with_protect_from_disconnect >= 0);
1511 
1512  m_node_states.erase(nodeid);
1513 
1514  if (m_node_states.empty()) {
1515  // Do a consistency check after the last peer is removed.
1516  assert(mapBlocksInFlight.empty());
1517  assert(m_num_preferred_download_peers == 0);
1518  assert(m_peers_downloading_from == 0);
1519  assert(m_outbound_peers_with_protect_from_disconnect == 0);
1520  assert(m_wtxid_relay_peers == 0);
1521  assert(m_txrequest.Size() == 0);
1522  assert(m_orphanage.Size() == 0);
1523  }
1524  } // cs_main
1525  if (node.fSuccessfullyConnected && misbehavior == 0 &&
1526  !node.IsBlockOnlyConn() && !node.IsInboundConn()) {
1527  // Only change visible addrman state for full outbound peers. We don't
1528  // call Connected() for feeler connections since they don't have
1529  // fSuccessfullyConnected set.
1530  m_addrman.Connected(node.addr);
1531  }
1532  {
1533  LOCK(m_headers_presync_mutex);
1534  m_headers_presync_stats.erase(nodeid);
1535  }
1536  LogPrint(BCLog::NET, "Cleared nodestate for peer=%d\n", nodeid);
1537 }
1538 
1539 PeerRef PeerManagerImpl::GetPeerRef(NodeId id) const
1540 {
1541  LOCK(m_peer_mutex);
1542  auto it = m_peer_map.find(id);
1543  return it != m_peer_map.end() ? it->second : nullptr;
1544 }
1545 
1546 PeerRef PeerManagerImpl::RemovePeer(NodeId id)
1547 {
1548  PeerRef ret;
1549  LOCK(m_peer_mutex);
1550  auto it = m_peer_map.find(id);
1551  if (it != m_peer_map.end()) {
1552  ret = std::move(it->second);
1553  m_peer_map.erase(it);
1554  }
1555  return ret;
1556 }
1557 
1558 bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const
1559 {
1560  {
1561  LOCK(cs_main);
1562  const CNodeState* state = State(nodeid);
1563  if (state == nullptr)
1564  return false;
1565  stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
1566  stats.nCommonHeight = state->pindexLastCommonBlock ? state->pindexLastCommonBlock->nHeight : -1;
1567  for (const QueuedBlock& queue : state->vBlocksInFlight) {
1568  if (queue.pindex)
1569  stats.vHeightInFlight.push_back(queue.pindex->nHeight);
1570  }
1571  }
1572 
1573  PeerRef peer = GetPeerRef(nodeid);
1574  if (peer == nullptr) return false;
1575  stats.their_services = peer->m_their_services;
1576  stats.m_starting_height = peer->m_starting_height;
1577  // It is common for nodes with good ping times to suddenly become lagged,
1578  // due to a new block arriving or other large transfer.
1579  // Merely reporting pingtime might fool the caller into thinking the node was still responsive,
1580  // since pingtime does not update until the ping is complete, which might take a while.
1581  // So, if a ping is taking an unusually long time in flight,
1582  // the caller can immediately detect that this is happening.
1583  auto ping_wait{0us};
1584  if ((0 != peer->m_ping_nonce_sent) && (0 != peer->m_ping_start.load().count())) {
1585  ping_wait = GetTime<std::chrono::microseconds>() - peer->m_ping_start.load();
1586  }
1587 
1588  if (auto tx_relay = peer->GetTxRelay(); tx_relay != nullptr) {
1589  stats.m_relay_txs = WITH_LOCK(tx_relay->m_bloom_filter_mutex, return tx_relay->m_relay_txs);
1590  stats.m_fee_filter_received = tx_relay->m_fee_filter_received.load();
1591  } else {
1592  stats.m_relay_txs = false;
1593  stats.m_fee_filter_received = 0;
1594  }
1595 
1596  stats.m_ping_wait = ping_wait;
1597  stats.m_addr_processed = peer->m_addr_processed.load();
1598  stats.m_addr_rate_limited = peer->m_addr_rate_limited.load();
1599  stats.m_addr_relay_enabled = peer->m_addr_relay_enabled.load();
1600  {
1601  LOCK(peer->m_headers_sync_mutex);
1602  if (peer->m_headers_sync) {
1603  stats.presync_height = peer->m_headers_sync->GetPresyncHeight();
1604  }
1605  }
1606 
1607  return true;
1608 }
1609 
1610 void PeerManagerImpl::AddToCompactExtraTransactions(const CTransactionRef& tx)
1611 {
1612  size_t max_extra_txn = gArgs.GetIntArg("-blockreconstructionextratxn", DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN);
1613  if (max_extra_txn <= 0)
1614  return;
1615  if (!vExtraTxnForCompact.size())
1616  vExtraTxnForCompact.resize(max_extra_txn);
1617  vExtraTxnForCompact[vExtraTxnForCompactIt] = std::make_pair(tx->GetWitnessHash(), tx);
1618  vExtraTxnForCompactIt = (vExtraTxnForCompactIt + 1) % max_extra_txn;
1619 }
1620 
1621 void PeerManagerImpl::Misbehaving(Peer& peer, int howmuch, const std::string& message)
1622 {
1623  assert(howmuch > 0);
1624 
1625  LOCK(peer.m_misbehavior_mutex);
1626  const int score_before{peer.m_misbehavior_score};
1627  peer.m_misbehavior_score += howmuch;
1628  const int score_now{peer.m_misbehavior_score};
1629 
1630  const std::string message_prefixed = message.empty() ? "" : (": " + message);
1631  std::string warning;
1632 
1633  if (score_now >= DISCOURAGEMENT_THRESHOLD && score_before < DISCOURAGEMENT_THRESHOLD) {
1634  warning = " DISCOURAGE THRESHOLD EXCEEDED";
1635  peer.m_should_discourage = true;
1636  }
1637 
1638  LogPrint(BCLog::NET, "Misbehaving: peer=%d (%d -> %d)%s%s\n",
1639  peer.m_id, score_before, score_now, warning, message_prefixed);
1640 }
1641 
1642 bool PeerManagerImpl::MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationState& state,
1643  bool via_compact_block, const std::string& message)
1644 {
1645  PeerRef peer{GetPeerRef(nodeid)};
1646  switch (state.GetResult()) {
1648  break;
1650  // We didn't try to process the block because the header chain may have
1651  // too little work.
1652  break;
1653  // The node is providing invalid data:
1656  if (!via_compact_block) {
1657  if (peer) Misbehaving(*peer, 100, message);
1658  return true;
1659  }
1660  break;
1662  {
1663  LOCK(cs_main);
1664  CNodeState *node_state = State(nodeid);
1665  if (node_state == nullptr) {
1666  break;
1667  }
1668 
1669  // Discourage outbound (but not inbound) peers if on an invalid chain.
1670  // Exempt HB compact block peers. Manual connections are always protected from discouragement.
1671  if (!via_compact_block && !node_state->m_is_inbound) {
1672  if (peer) Misbehaving(*peer, 100, message);
1673  return true;
1674  }
1675  break;
1676  }
1680  if (peer) Misbehaving(*peer, 100, message);
1681  return true;
1682  // Conflicting (but not necessarily invalid) data or different policy:
1684  // TODO: Handle this much more gracefully (10 DoS points is super arbitrary)
1685  if (peer) Misbehaving(*peer, 10, message);
1686  return true;
1689  break;
1690  }
1691  if (message != "") {
1692  LogPrint(BCLog::NET, "peer=%d: %s\n", nodeid, message);
1693  }
1694  return false;
1695 }
1696 
1697 bool PeerManagerImpl::MaybePunishNodeForTx(NodeId nodeid, const TxValidationState& state, const std::string& message)
1698 {
1699  PeerRef peer{GetPeerRef(nodeid)};
1700  switch (state.GetResult()) {
1702  break;
1703  // The node is providing invalid data:
1705  if (peer) Misbehaving(*peer, 100, message);
1706  return true;
1707  // Conflicting (but not necessarily invalid) data or different policy:
1718  break;
1719  }
1720  if (message != "") {
1721  LogPrint(BCLog::NET, "peer=%d: %s\n", nodeid, message);
1722  }
1723  return false;
1724 }
1725 
1726 bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex* pindex)
1727 {
1729  if (m_chainman.ActiveChain().Contains(pindex)) return true;
1730  return pindex->IsValid(BLOCK_VALID_SCRIPTS) && (m_chainman.m_best_header != nullptr) &&
1731  (m_chainman.m_best_header->GetBlockTime() - pindex->GetBlockTime() < STALE_RELAY_AGE_LIMIT) &&
1732  (GetBlockProofEquivalentTime(*m_chainman.m_best_header, *pindex, *m_chainman.m_best_header, m_chainparams.GetConsensus()) < STALE_RELAY_AGE_LIMIT);
1733 }
1734 
1735 std::optional<std::string> PeerManagerImpl::FetchBlock(NodeId peer_id, const CBlockIndex& block_index)
1736 {
1737  if (fImporting) return "Importing...";
1738  if (fReindex) return "Reindexing...";
1739 
1740  // Ensure this peer exists and hasn't been disconnected
1741  PeerRef peer = GetPeerRef(peer_id);
1742  if (peer == nullptr) return "Peer does not exist";
1743 
1744  // Ignore pre-segwit peers
1745  if (!CanServeWitnesses(*peer)) return "Pre-SegWit peer";
1746 
1747  LOCK(cs_main);
1748 
1749  // Mark block as in-flight unless it already is (for this peer).
1750  // If a block was already in-flight for a different peer, its BLOCKTXN
1751  // response will be dropped.
1752  if (!BlockRequested(peer_id, block_index)) return "Already requested from this peer";
1753 
1754  // Construct message to request the block
1755  const uint256& hash{block_index.GetBlockHash()};
1756  std::vector<CInv> invs{CInv(MSG_BLOCK | MSG_WITNESS_FLAG, hash)};
1757 
1758  // Send block request message to the peer
1759  bool success = m_connman.ForNode(peer_id, [this, &invs](CNode* node) {
1760  const CNetMsgMaker msgMaker(node->GetCommonVersion());
1761  this->m_connman.PushMessage(node, msgMaker.Make(NetMsgType::GETDATA, invs));
1762  return true;
1763  });
1764 
1765  if (!success) return "Peer not fully connected";
1766 
1767  LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n",
1768  hash.ToString(), peer_id);
1769  return std::nullopt;
1770 }
1771 
1772 std::unique_ptr<PeerManager> PeerManager::make(CConnman& connman, AddrMan& addrman,
1773  BanMan* banman, ChainstateManager& chainman,
1774  CTxMemPool& pool, bool ignore_incoming_txs)
1775 {
1776  return std::make_unique<PeerManagerImpl>(connman, addrman, banman, chainman, pool, ignore_incoming_txs);
1777 }
1778 
1779 PeerManagerImpl::PeerManagerImpl(CConnman& connman, AddrMan& addrman,
1780  BanMan* banman, ChainstateManager& chainman,
1781  CTxMemPool& pool, bool ignore_incoming_txs)
1782  : m_chainparams(chainman.GetParams()),
1783  m_connman(connman),
1784  m_addrman(addrman),
1785  m_banman(banman),
1786  m_chainman(chainman),
1787  m_mempool(pool),
1788  m_ignore_incoming_txs(ignore_incoming_txs)
1789 {
1790 }
1791 
1792 void PeerManagerImpl::StartScheduledTasks(CScheduler& scheduler)
1793 {
1794  // Stale tip checking and peer eviction are on two different timers, but we
1795  // don't want them to get out of sync due to drift in the scheduler, so we
1796  // combine them in one function and schedule at the quicker (peer-eviction)
1797  // timer.
1798  static_assert(EXTRA_PEER_CHECK_INTERVAL < STALE_CHECK_INTERVAL, "peer eviction timer should be less than stale tip check timer");
1799  scheduler.scheduleEvery([this] { this->CheckForStaleTipAndEvictPeers(); }, std::chrono::seconds{EXTRA_PEER_CHECK_INTERVAL});
1800 
1801  // schedule next run for 10-15 minutes in the future
1802  const std::chrono::milliseconds delta = 10min + GetRandMillis(5min);
1803  scheduler.scheduleFromNow([&] { ReattemptInitialBroadcast(scheduler); }, delta);
1804 }
1805 
1811 void PeerManagerImpl::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
1812 {
1813  m_orphanage.EraseForBlock(*pblock);
1814  m_last_tip_update = GetTime<std::chrono::seconds>();
1815 
1816  {
1817  LOCK(m_recent_confirmed_transactions_mutex);
1818  for (const auto& ptx : pblock->vtx) {
1819  m_recent_confirmed_transactions.insert(ptx->GetHash());
1820  if (ptx->GetHash() != ptx->GetWitnessHash()) {
1821  m_recent_confirmed_transactions.insert(ptx->GetWitnessHash());
1822  }
1823  }
1824  }
1825  {
1826  LOCK(cs_main);
1827  for (const auto& ptx : pblock->vtx) {
1828  m_txrequest.ForgetTxHash(ptx->GetHash());
1829  m_txrequest.ForgetTxHash(ptx->GetWitnessHash());
1830  }
1831  }
1832 }
1833 
1834 void PeerManagerImpl::BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex)
1835 {
1836  // To avoid relay problems with transactions that were previously
1837  // confirmed, clear our filter of recently confirmed transactions whenever
1838  // there's a reorg.
1839  // This means that in a 1-block reorg (where 1 block is disconnected and
1840  // then another block reconnected), our filter will drop to having only one
1841  // block's worth of transactions in it, but that should be fine, since
1842  // presumably the most common case of relaying a confirmed transaction
1843  // should be just after a new block containing it is found.
1844  LOCK(m_recent_confirmed_transactions_mutex);
1845  m_recent_confirmed_transactions.reset();
1846 }
1847 
1852 void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock)
1853 {
1854  auto pcmpctblock = std::make_shared<const CBlockHeaderAndShortTxIDs>(*pblock);
1855  const CNetMsgMaker msgMaker(PROTOCOL_VERSION);
1856 
1857  LOCK(cs_main);
1858 
1859  if (pindex->nHeight <= m_highest_fast_announce)
1860  return;
1861  m_highest_fast_announce = pindex->nHeight;
1862 
1863  if (!DeploymentActiveAt(*pindex, m_chainman, Consensus::DEPLOYMENT_SEGWIT)) return;
1864 
1865  uint256 hashBlock(pblock->GetHash());
1866  const std::shared_future<CSerializedNetMsg> lazy_ser{
1867  std::async(std::launch::deferred, [&] { return msgMaker.Make(NetMsgType::CMPCTBLOCK, *pcmpctblock); })};
1868 
1869  {
1870  LOCK(m_most_recent_block_mutex);
1871  m_most_recent_block_hash = hashBlock;
1872  m_most_recent_block = pblock;
1873  m_most_recent_compact_block = pcmpctblock;
1874  }
1875 
1876  m_connman.ForEachNode([this, pindex, &lazy_ser, &hashBlock](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
1878 
1879  if (pnode->GetCommonVersion() < INVALID_CB_NO_BAN_VERSION || pnode->fDisconnect)
1880  return;
1881  ProcessBlockAvailability(pnode->GetId());
1882  CNodeState &state = *State(pnode->GetId());
1883  // If the peer has, or we announced to them the previous block already,
1884  // but we don't think they have this one, go ahead and announce it
1885  if (state.m_requested_hb_cmpctblocks && !PeerHasHeader(&state, pindex) && PeerHasHeader(&state, pindex->pprev)) {
1886 
1887  LogPrint(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", "PeerManager::NewPoWValidBlock",
1888  hashBlock.ToString(), pnode->GetId());
1889 
1890  const CSerializedNetMsg& ser_cmpctblock{lazy_ser.get()};
1891  m_connman.PushMessage(pnode, ser_cmpctblock.Copy());
1892  state.pindexBestHeaderSent = pindex;
1893  }
1894  });
1895 }
1896 
1901 void PeerManagerImpl::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
1902 {
1903  SetBestHeight(pindexNew->nHeight);
1904  SetServiceFlagsIBDCache(!fInitialDownload);
1905 
1906  // Don't relay inventory during initial block download.
1907  if (fInitialDownload) return;
1908 
1909  // Find the hashes of all blocks that weren't previously in the best chain.
1910  std::vector<uint256> vHashes;
1911  const CBlockIndex *pindexToAnnounce = pindexNew;
1912  while (pindexToAnnounce != pindexFork) {
1913  vHashes.push_back(pindexToAnnounce->GetBlockHash());
1914  pindexToAnnounce = pindexToAnnounce->pprev;
1915  if (vHashes.size() == MAX_BLOCKS_TO_ANNOUNCE) {
1916  // Limit announcements in case of a huge reorganization.
1917  // Rely on the peer's synchronization mechanism in that case.
1918  break;
1919  }
1920  }
1921 
1922  {
1923  LOCK(m_peer_mutex);
1924  for (auto& it : m_peer_map) {
1925  Peer& peer = *it.second;
1926  LOCK(peer.m_block_inv_mutex);
1927  for (const uint256& hash : reverse_iterate(vHashes)) {
1928  peer.m_blocks_for_headers_relay.push_back(hash);
1929  }
1930  }
1931  }
1932 
1933  m_connman.WakeMessageHandler();
1934 }
1935 
1940 void PeerManagerImpl::BlockChecked(const CBlock& block, const BlockValidationState& state)
1941 {
1942  LOCK(cs_main);
1943 
1944  const uint256 hash(block.GetHash());
1945  std::map<uint256, std::pair<NodeId, bool>>::iterator it = mapBlockSource.find(hash);
1946 
1947  // If the block failed validation, we know where it came from and we're still connected
1948  // to that peer, maybe punish.
1949  if (state.IsInvalid() &&
1950  it != mapBlockSource.end() &&
1951  State(it->second.first)) {
1952  MaybePunishNodeForBlock(/*nodeid=*/ it->second.first, state, /*via_compact_block=*/ !it->second.second);
1953  }
1954  // Check that:
1955  // 1. The block is valid
1956  // 2. We're not in initial block download
1957  // 3. This is currently the best block we're aware of. We haven't updated
1958  // the tip yet so we have no way to check this directly here. Instead we
1959  // just check that there are currently no other blocks in flight.
1960  else if (state.IsValid() &&
1961  !m_chainman.ActiveChainstate().IsInitialBlockDownload() &&
1962  mapBlocksInFlight.count(hash) == mapBlocksInFlight.size()) {
1963  if (it != mapBlockSource.end()) {
1964  MaybeSetPeerAsAnnouncingHeaderAndIDs(it->second.first);
1965  }
1966  }
1967  if (it != mapBlockSource.end())
1968  mapBlockSource.erase(it);
1969 }
1970 
1972 //
1973 // Messages
1974 //
1975 
1976 
1977 bool PeerManagerImpl::AlreadyHaveTx(const GenTxid& gtxid)
1978 {
1979  if (m_chainman.ActiveChain().Tip()->GetBlockHash() != hashRecentRejectsChainTip) {
1980  // If the chain tip has changed previously rejected transactions
1981  // might be now valid, e.g. due to a nLockTime'd tx becoming valid,
1982  // or a double-spend. Reset the rejects filter and give those
1983  // txs a second chance.
1984  hashRecentRejectsChainTip = m_chainman.ActiveChain().Tip()->GetBlockHash();
1985  m_recent_rejects.reset();
1986  }
1987 
1988  const uint256& hash = gtxid.GetHash();
1989 
1990  if (m_orphanage.HaveTx(gtxid)) return true;
1991 
1992  {
1993  LOCK(m_recent_confirmed_transactions_mutex);
1994  if (m_recent_confirmed_transactions.contains(hash)) return true;
1995  }
1996 
1997  return m_recent_rejects.contains(hash) || m_mempool.exists(gtxid);
1998 }
1999 
2000 bool PeerManagerImpl::AlreadyHaveBlock(const uint256& block_hash)
2001 {
2002  return m_chainman.m_blockman.LookupBlockIndex(block_hash) != nullptr;
2003 }
2004 
2005 void PeerManagerImpl::SendPings()
2006 {
2007  LOCK(m_peer_mutex);
2008  for(auto& it : m_peer_map) it.second->m_ping_queued = true;
2009 }
2010 
2011 void PeerManagerImpl::RelayTransaction(const uint256& txid, const uint256& wtxid)
2012 {
2013  LOCK(m_peer_mutex);
2014  for(auto& it : m_peer_map) {
2015  Peer& peer = *it.second;
2016  auto tx_relay = peer.GetTxRelay();
2017  if (!tx_relay) continue;
2018 
2019  LOCK(tx_relay->m_tx_inventory_mutex);
2020  // Only queue transactions for announcement once the version handshake
2021  // is completed. The time of arrival for these transactions is
2022  // otherwise at risk of leaking to a spy, if the spy is able to
2023  // distinguish transactions received during the handshake from the rest
2024  // in the announcement.
2025  if (tx_relay->m_next_inv_send_time == 0s) continue;
2026 
2027  const uint256& hash{peer.m_wtxid_relay ? wtxid : txid};
2028  if (!tx_relay->m_tx_inventory_known_filter.contains(hash)) {
2029  tx_relay->m_tx_inventory_to_send.insert(hash);
2030  }
2031  };
2032 }
2033 
2034 void PeerManagerImpl::RelayAddress(NodeId originator,
2035  const CAddress& addr,
2036  bool fReachable)
2037 {
2038  // We choose the same nodes within a given 24h window (if the list of connected
2039  // nodes does not change) and we don't relay to nodes that already know an
2040  // address. So within 24h we will likely relay a given address once. This is to
2041  // prevent a peer from unjustly giving their address better propagation by sending
2042  // it to us repeatedly.
2043 
2044  if (!fReachable && !addr.IsRelayable()) return;
2045 
2046  // Relay to a limited number of other nodes
2047  // Use deterministic randomness to send to the same nodes for 24 hours
2048  // at a time so the m_addr_knowns of the chosen nodes prevent repeats
2049  const uint64_t hash_addr{CServiceHash(0, 0)(addr)};
2050  const auto current_time{GetTime<std::chrono::seconds>()};
2051  // Adding address hash makes exact rotation time different per address, while preserving periodicity.
2052  const uint64_t time_addr{(static_cast<uint64_t>(count_seconds(current_time)) + hash_addr) / count_seconds(ROTATE_ADDR_RELAY_DEST_INTERVAL)};
2053  const CSipHasher hasher{m_connman.GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY)
2054  .Write(hash_addr)
2055  .Write(time_addr)};
2056  FastRandomContext insecure_rand;
2057 
2058  // Relay reachable addresses to 2 peers. Unreachable addresses are relayed randomly to 1 or 2 peers.
2059  unsigned int nRelayNodes = (fReachable || (hasher.Finalize() & 1)) ? 2 : 1;
2060 
2061  std::array<std::pair<uint64_t, Peer*>, 2> best{{{0, nullptr}, {0, nullptr}}};
2062  assert(nRelayNodes <= best.size());
2063 
2064  LOCK(m_peer_mutex);
2065 
2066  for (auto& [id, peer] : m_peer_map) {
2067  if (peer->m_addr_relay_enabled && id != originator && IsAddrCompatible(*peer, addr)) {
2068  uint64_t hashKey = CSipHasher(hasher).Write(id).Finalize();
2069  for (unsigned int i = 0; i < nRelayNodes; i++) {
2070  if (hashKey > best[i].first) {
2071  std::copy(best.begin() + i, best.begin() + nRelayNodes - 1, best.begin() + i + 1);
2072  best[i] = std::make_pair(hashKey, peer.get());
2073  break;
2074  }
2075  }
2076  }
2077  };
2078 
2079  for (unsigned int i = 0; i < nRelayNodes && best[i].first != 0; i++) {
2080  PushAddress(*best[i].second, addr, insecure_rand);
2081  }
2082 }
2083 
2084 void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv& inv)
2085 {
2086  std::shared_ptr<const CBlock> a_recent_block;
2087  std::shared_ptr<const CBlockHeaderAndShortTxIDs> a_recent_compact_block;
2088  {
2089  LOCK(m_most_recent_block_mutex);
2090  a_recent_block = m_most_recent_block;
2091  a_recent_compact_block = m_most_recent_compact_block;
2092  }
2093 
2094  bool need_activate_chain = false;
2095  {
2096  LOCK(cs_main);
2097  const CBlockIndex* pindex = m_chainman.m_blockman.LookupBlockIndex(inv.hash);
2098  if (pindex) {
2099  if (pindex->HaveTxsDownloaded() && !pindex->IsValid(BLOCK_VALID_SCRIPTS) &&
2100  pindex->IsValid(BLOCK_VALID_TREE)) {
2101  // If we have the block and all of its parents, but have not yet validated it,
2102  // we might be in the middle of connecting it (ie in the unlock of cs_main
2103  // before ActivateBestChain but after AcceptBlock).
2104  // In this case, we need to run ActivateBestChain prior to checking the relay
2105  // conditions below.
2106  need_activate_chain = true;
2107  }
2108  }
2109  } // release cs_main before calling ActivateBestChain
2110  if (need_activate_chain) {
2111  BlockValidationState state;
2112  if (!m_chainman.ActiveChainstate().ActivateBestChain(state, a_recent_block)) {
2113  LogPrint(BCLog::NET, "failed to activate chain (%s)\n", state.ToString());
2114  }
2115  }
2116 
2117  LOCK(cs_main);
2118  const CBlockIndex* pindex = m_chainman.m_blockman.LookupBlockIndex(inv.hash);
2119  if (!pindex) {
2120  return;
2121  }
2122  if (!BlockRequestAllowed(pindex)) {
2123  LogPrint(BCLog::NET, "%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom.GetId());
2124  return;
2125  }
2126  const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
2127  // disconnect node in case we have reached the outbound limit for serving historical blocks
2128  if (m_connman.OutboundTargetReached(true) &&
2129  (((m_chainman.m_best_header != nullptr) && (m_chainman.m_best_header->GetBlockTime() - pindex->GetBlockTime() > HISTORICAL_BLOCK_AGE)) || inv.IsMsgFilteredBlk()) &&
2130  !pfrom.HasPermission(NetPermissionFlags::Download) // nodes with the download permission may exceed target
2131  ) {
2132  LogPrint(BCLog::NET, "historical block serving limit reached, disconnect peer=%d\n", pfrom.GetId());
2133  pfrom.fDisconnect = true;
2134  return;
2135  }
2136  // Avoid leaking prune-height by never sending blocks below the NODE_NETWORK_LIMITED threshold
2137  if (!pfrom.HasPermission(NetPermissionFlags::NoBan) && (
2138  (((peer.m_our_services & NODE_NETWORK_LIMITED) == NODE_NETWORK_LIMITED) && ((peer.m_our_services & NODE_NETWORK) != NODE_NETWORK) && (m_chainman.ActiveChain().Tip()->nHeight - pindex->nHeight > (int)NODE_NETWORK_LIMITED_MIN_BLOCKS + 2 /* add two blocks buffer extension for possible races */) )
2139  )) {
2140  LogPrint(BCLog::NET, "Ignore block request below NODE_NETWORK_LIMITED threshold, disconnect peer=%d\n", pfrom.GetId());
2141  //disconnect node and prevent it from stalling (would otherwise wait for the missing block)
2142  pfrom.fDisconnect = true;
2143  return;
2144  }
2145  // Pruned nodes may have deleted the block, so check whether
2146  // it's available before trying to send.
2147  if (!(pindex->nStatus & BLOCK_HAVE_DATA)) {
2148  return;
2149  }
2150  std::shared_ptr<const CBlock> pblock;
2151  if (a_recent_block && a_recent_block->GetHash() == pindex->GetBlockHash()) {
2152  pblock = a_recent_block;
2153  } else if (inv.IsMsgWitnessBlk()) {
2154  // Fast-path: in this case it is possible to serve the block directly from disk,
2155  // as the network format matches the format on disk
2156  std::vector<uint8_t> block_data;
2157  if (!ReadRawBlockFromDisk(block_data, pindex->GetBlockPos(), m_chainparams.MessageStart())) {
2158  assert(!"cannot load block from disk");
2159  }
2160  m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::BLOCK, Span{block_data}));
2161  // Don't set pblock as we've sent the block
2162  } else {
2163  // Send block from disk
2164  std::shared_ptr<CBlock> pblockRead = std::make_shared<CBlock>();
2165  if (!ReadBlockFromDisk(*pblockRead, pindex, m_chainparams.GetConsensus())) {
2166  assert(!"cannot load block from disk");
2167  }
2168  pblock = pblockRead;
2169  }
2170  if (pblock) {
2171  if (inv.IsMsgBlk()) {
2172  m_connman.PushMessage(&pfrom, msgMaker.Make(SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::BLOCK, *pblock));
2173  } else if (inv.IsMsgWitnessBlk()) {
2174  m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::BLOCK, *pblock));
2175  } else if (inv.IsMsgFilteredBlk()) {
2176  bool sendMerkleBlock = false;
2177  CMerkleBlock merkleBlock;
2178  if (auto tx_relay = peer.GetTxRelay(); tx_relay != nullptr) {
2179  LOCK(tx_relay->m_bloom_filter_mutex);
2180  if (tx_relay->m_bloom_filter) {
2181  sendMerkleBlock = true;
2182  merkleBlock = CMerkleBlock(*pblock, *tx_relay->m_bloom_filter);
2183  }
2184  }
2185  if (sendMerkleBlock) {
2186  m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::MERKLEBLOCK, merkleBlock));
2187  // CMerkleBlock just contains hashes, so also push any transactions in the block the client did not see
2188  // This avoids hurting performance by pointlessly requiring a round-trip
2189  // Note that there is currently no way for a node to request any single transactions we didn't send here -
2190  // they must either disconnect and retry or request the full block.
2191  // Thus, the protocol spec specified allows for us to provide duplicate txn here,
2192  // however we MUST always provide at least what the remote peer needs
2193  typedef std::pair<unsigned int, uint256> PairType;
2194  for (PairType& pair : merkleBlock.vMatchedTxn)
2195  m_connman.PushMessage(&pfrom, msgMaker.Make(SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::TX, *pblock->vtx[pair.first]));
2196  }
2197  // else
2198  // no response
2199  } else if (inv.IsMsgCmpctBlk()) {
2200  // If a peer is asking for old blocks, we're almost guaranteed
2201  // they won't have a useful mempool to match against a compact block,
2202  // and we don't feel like constructing the object for them, so
2203  // instead we respond with the full, non-compact block.
2204  if (CanDirectFetch() && pindex->nHeight >= m_chainman.ActiveChain().Height() - MAX_CMPCTBLOCK_DEPTH) {
2205  if (a_recent_compact_block && a_recent_compact_block->header.GetHash() == pindex->GetBlockHash()) {
2206  m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::CMPCTBLOCK, *a_recent_compact_block));
2207  } else {
2208  CBlockHeaderAndShortTxIDs cmpctblock{*pblock};
2209  m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::CMPCTBLOCK, cmpctblock));
2210  }
2211  } else {
2212  m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::BLOCK, *pblock));
2213  }
2214  }
2215  }
2216 
2217  {
2218  LOCK(peer.m_block_inv_mutex);
2219  // Trigger the peer node to send a getblocks request for the next batch of inventory
2220  if (inv.hash == peer.m_continuation_block) {
2221  // Send immediately. This must send even if redundant,
2222  // and we want it right after the last block so they don't
2223  // wait for other stuff first.
2224  std::vector<CInv> vInv;
2225  vInv.push_back(CInv(MSG_BLOCK, m_chainman.ActiveChain().Tip()->GetBlockHash()));
2226  m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::INV, vInv));
2227  peer.m_continuation_block.SetNull();
2228  }
2229  }
2230 }
2231 
2232 CTransactionRef PeerManagerImpl::FindTxForGetData(const CNode& peer, const GenTxid& gtxid, const std::chrono::seconds mempool_req, const std::chrono::seconds now)
2233 {
2234  auto txinfo = m_mempool.info(gtxid);
2235  if (txinfo.tx) {
2236  // If a TX could have been INVed in reply to a MEMPOOL request,
2237  // or is older than UNCONDITIONAL_RELAY_DELAY, permit the request
2238  // unconditionally.
2239  if ((mempool_req.count() && txinfo.m_time <= mempool_req) || txinfo.m_time <= now - UNCONDITIONAL_RELAY_DELAY) {
2240  return std::move(txinfo.tx);
2241  }
2242  }
2243 
2244  {
2245  LOCK(cs_main);
2246  // Otherwise, the transaction must have been announced recently.
2247  if (State(peer.GetId())->m_recently_announced_invs.contains(gtxid.GetHash())) {
2248  // If it was, it can be relayed from either the mempool...
2249  if (txinfo.tx) return std::move(txinfo.tx);
2250  // ... or the relay pool.
2251  auto mi = mapRelay.find(gtxid.GetHash());
2252  if (mi != mapRelay.end()) return mi->second;
2253  }
2254  }
2255 
2256  return {};
2257 }
2258 
2259 void PeerManagerImpl::ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic<bool>& interruptMsgProc)
2260 {
2262 
2263  auto tx_relay = peer.GetTxRelay();
2264 
2265  std::deque<CInv>::iterator it = peer.m_getdata_requests.begin();
2266  std::vector<CInv> vNotFound;
2267  const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
2268 
2269  const auto now{GetTime<std::chrono::seconds>()};
2270  // Get last mempool request time
2271  const auto mempool_req = tx_relay != nullptr ? tx_relay->m_last_mempool_req.load() : std::chrono::seconds::min();
2272 
2273  // Process as many TX items from the front of the getdata queue as
2274  // possible, since they're common and it's efficient to batch process
2275  // them.
2276  while (it != peer.m_getdata_requests.end() && it->IsGenTxMsg()) {
2277  if (interruptMsgProc) return;
2278  // The send buffer provides backpressure. If there's no space in
2279  // the buffer, pause processing until the next call.
2280  if (pfrom.fPauseSend) break;
2281 
2282  const CInv &inv = *it++;
2283 
2284  if (tx_relay == nullptr) {
2285  // Ignore GETDATA requests for transactions from block-relay-only
2286  // peers and peers that asked us not to announce transactions.
2287  continue;
2288  }
2289 
2290  CTransactionRef tx = FindTxForGetData(pfrom, ToGenTxid(inv), mempool_req, now);
2291  if (tx) {
2292  // WTX and WITNESS_TX imply we serialize with witness
2293  int nSendFlags = (inv.IsMsgTx() ? SERIALIZE_TRANSACTION_NO_WITNESS : 0);
2294  m_connman.PushMessage(&pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *tx));
2295  m_mempool.RemoveUnbroadcastTx(tx->GetHash());
2296  // As we're going to send tx, make sure its unconfirmed parents are made requestable.
2297  std::vector<uint256> parent_ids_to_add;
2298  {
2299  LOCK(m_mempool.cs);
2300  auto txiter = m_mempool.GetIter(tx->GetHash());
2301  if (txiter) {
2302  const CTxMemPoolEntry::Parents& parents = (*txiter)->GetMemPoolParentsConst();
2303  parent_ids_to_add.reserve(parents.size());
2304  for (const CTxMemPoolEntry& parent : parents) {
2305  if (parent.GetTime() > now - UNCONDITIONAL_RELAY_DELAY) {
2306  parent_ids_to_add.push_back(parent.GetTx().GetHash());
2307  }
2308  }
2309  }
2310  }
2311  for (const uint256& parent_txid : parent_ids_to_add) {
2312  // Relaying a transaction with a recent but unconfirmed parent.
2313  if (WITH_LOCK(tx_relay->m_tx_inventory_mutex, return !tx_relay->m_tx_inventory_known_filter.contains(parent_txid))) {
2314  LOCK(cs_main);
2315  State(pfrom.GetId())->m_recently_announced_invs.insert(parent_txid);
2316  }
2317  }
2318  } else {
2319  vNotFound.push_back(inv);
2320  }
2321  }
2322 
2323  // Only process one BLOCK item per call, since they're uncommon and can be
2324  // expensive to process.
2325  if (it != peer.m_getdata_requests.end() && !pfrom.fPauseSend) {
2326  const CInv &inv = *it++;
2327  if (inv.IsGenBlkMsg()) {
2328  ProcessGetBlockData(pfrom, peer, inv);
2329  }
2330  // else: If the first item on the queue is an unknown type, we erase it
2331  // and continue processing the queue on the next call.
2332  }
2333 
2334  peer.m_getdata_requests.erase(peer.m_getdata_requests.begin(), it);
2335 
2336  if (!vNotFound.empty()) {
2337  // Let the peer know that we didn't find what it asked for, so it doesn't
2338  // have to wait around forever.
2339  // SPV clients care about this message: it's needed when they are
2340  // recursively walking the dependencies of relevant unconfirmed
2341  // transactions. SPV clients want to do that because they want to know
2342  // about (and store and rebroadcast and risk analyze) the dependencies
2343  // of transactions relevant to them, without having to download the
2344  // entire memory pool.
2345  // Also, other nodes can use these messages to automatically request a
2346  // transaction from some other peer that annnounced it, and stop
2347  // waiting for us to respond.
2348  // In normal operation, we often send NOTFOUND messages for parents of
2349  // transactions that we relay; if a peer is missing a parent, they may
2350  // assume we have them and request the parents from us.
2351  m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::NOTFOUND, vNotFound));
2352  }
2353 }
2354 
2355 uint32_t PeerManagerImpl::GetFetchFlags(const Peer& peer) const
2356 {
2357  uint32_t nFetchFlags = 0;
2358  if (CanServeWitnesses(peer)) {
2359  nFetchFlags |= MSG_WITNESS_FLAG;
2360  }
2361  return nFetchFlags;
2362 }
2363 
2364 void PeerManagerImpl::SendBlockTransactions(CNode& pfrom, Peer& peer, const CBlock& block, const BlockTransactionsRequest& req)
2365 {
2366  BlockTransactions resp(req);
2367  for (size_t i = 0; i < req.indexes.size(); i++) {
2368  if (req.indexes[i] >= block.vtx.size()) {
2369  Misbehaving(peer, 100, "getblocktxn with out-of-bounds tx indices");
2370  return;
2371  }
2372  resp.txn[i] = block.vtx[req.indexes[i]];
2373  }
2374 
2375  const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
2376  m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::BLOCKTXN, resp));
2377 }
2378 
2379 bool PeerManagerImpl::CheckHeadersPoW(const std::vector<CBlockHeader>& headers, const Consensus::Params& consensusParams, Peer& peer)
2380 {
2381  // Do these headers have proof-of-work matching what's claimed?
2382  if (!HasValidProofOfWork(headers, consensusParams)) {
2383  Misbehaving(peer, 100, "header with invalid proof of work");
2384  return false;
2385  }
2386 
2387  // Are these headers connected to each other?
2388  if (!CheckHeadersAreContinuous(headers)) {
2389  Misbehaving(peer, 20, "non-continuous headers sequence");
2390  return false;
2391  }
2392  return true;
2393 }
2394 
2395 arith_uint256 PeerManagerImpl::GetAntiDoSWorkThreshold()
2396 {
2397  arith_uint256 near_chaintip_work = 0;
2398  LOCK(cs_main);
2399  if (m_chainman.ActiveChain().Tip() != nullptr) {
2400  const CBlockIndex *tip = m_chainman.ActiveChain().Tip();
2401  // Use a 144 block buffer, so that we'll accept headers that fork from
2402  // near our tip.
2403  near_chaintip_work = tip->nChainWork - std::min<arith_uint256>(144*GetBlockProof(*tip), tip->nChainWork);
2404  }
2405  return std::max(near_chaintip_work, arith_uint256(nMinimumChainWork));
2406 }
2407 
2420 void PeerManagerImpl::HandleFewUnconnectingHeaders(CNode& pfrom, Peer& peer,
2421  const std::vector<CBlockHeader>& headers)
2422 {
2423  const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
2424 
2425  LOCK(cs_main);
2426  CNodeState *nodestate = State(pfrom.GetId());
2427 
2428  nodestate->nUnconnectingHeaders++;
2429  // Try to fill in the missing headers.
2430  if (MaybeSendGetHeaders(pfrom, GetLocator(m_chainman.m_best_header), peer)) {
2431  LogPrint(BCLog::NET, "received header %s: missing prev block %s, sending getheaders (%d) to end (peer=%d, nUnconnectingHeaders=%d)\n",
2432  headers[0].GetHash().ToString(),
2433  headers[0].hashPrevBlock.ToString(),
2434  m_chainman.m_best_header->nHeight,
2435  pfrom.GetId(), nodestate->nUnconnectingHeaders);
2436  }
2437  // Set hashLastUnknownBlock for this peer, so that if we
2438  // eventually get the headers - even from a different peer -
2439  // we can use this peer to download.
2440  UpdateBlockAvailability(pfrom.GetId(), headers.back().GetHash());
2441 
2442  // The peer may just be broken, so periodically assign DoS points if this
2443  // condition persists.
2444  if (nodestate->nUnconnectingHeaders % MAX_UNCONNECTING_HEADERS == 0) {
2445  Misbehaving(peer, 20, strprintf("%d non-connecting headers", nodestate->nUnconnectingHeaders));
2446  }
2447 }
2448 
2449 bool PeerManagerImpl::CheckHeadersAreContinuous(const std::vector<CBlockHeader>& headers) const
2450 {
2451  uint256 hashLastBlock;
2452  for (const CBlockHeader& header : headers) {
2453  if (!hashLastBlock.IsNull() && header.hashPrevBlock != hashLastBlock) {
2454  return false;
2455  }
2456  hashLastBlock = header.GetHash();
2457  }
2458  return true;
2459 }
2460 
2461 bool PeerManagerImpl::IsContinuationOfLowWorkHeadersSync(Peer& peer, CNode& pfrom, std::vector<CBlockHeader>& headers)
2462 {
2463  if (peer.m_headers_sync) {
2464  auto result = peer.m_headers_sync->ProcessNextHeaders(headers, headers.size() == MAX_HEADERS_RESULTS);
2465  if (result.request_more) {
2466  auto locator = peer.m_headers_sync->NextHeadersRequestLocator();
2467  // If we were instructed to ask for a locator, it should not be empty.
2468  Assume(!locator.vHave.empty());
2469  if (!locator.vHave.empty()) {
2470  // It should be impossible for the getheaders request to fail,
2471  // because we should have cleared the last getheaders timestamp
2472  // when processing the headers that triggered this call. But
2473  // it may be possible to bypass this via compactblock
2474  // processing, so check the result before logging just to be
2475  // safe.
2476  bool sent_getheaders = MaybeSendGetHeaders(pfrom, locator, peer);
2477  if (sent_getheaders) {
2478  LogPrint(BCLog::NET, "more getheaders (from %s) to peer=%d\n",
2479  locator.vHave.front().ToString(), pfrom.GetId());
2480  } else {
2481  LogPrint(BCLog::NET, "error sending next getheaders (from %s) to continue sync with peer=%d\n",
2482  locator.vHave.front().ToString(), pfrom.GetId());
2483  }
2484  }
2485  }
2486 
2487  if (peer.m_headers_sync->GetState() == HeadersSyncState::State::FINAL) {
2488  peer.m_headers_sync.reset(nullptr);
2489 
2490  // Delete this peer's entry in m_headers_presync_stats.
2491  // If this is m_headers_presync_bestpeer, it will be replaced later
2492  // by the next peer that triggers the else{} branch below.
2493  LOCK(m_headers_presync_mutex);
2494  m_headers_presync_stats.erase(pfrom.GetId());
2495  } else {
2496  // Build statistics for this peer's sync.
2497  HeadersPresyncStats stats;
2498  stats.first = peer.m_headers_sync->GetPresyncWork();
2499  if (peer.m_headers_sync->GetState() == HeadersSyncState::State::PRESYNC) {
2500  stats.second = {peer.m_headers_sync->GetPresyncHeight(),
2501  peer.m_headers_sync->GetPresyncTime()};
2502  }
2503 
2504  // Update statistics in stats.
2505  LOCK(m_headers_presync_mutex);
2506  m_headers_presync_stats[pfrom.GetId()] = stats;
2507  auto best_it = m_headers_presync_stats.find(m_headers_presync_bestpeer);
2508  bool best_updated = false;
2509  if (best_it == m_headers_presync_stats.end()) {
2510  // If the cached best peer is outdated, iterate over all remaining ones (including
2511  // newly updated one) to find the best one.
2512  NodeId peer_best{-1};
2513  const HeadersPresyncStats* stat_best{nullptr};
2514  for (const auto& [peer, stat] : m_headers_presync_stats) {
2515  if (!stat_best || stat > *stat_best) {
2516  peer_best = peer;
2517  stat_best = &stat;
2518  }
2519  }
2520  m_headers_presync_bestpeer = peer_best;
2521  best_updated = (peer_best == pfrom.GetId());
2522  } else if (best_it->first == pfrom.GetId() || stats > best_it->second) {
2523  // pfrom was and remains the best peer, or pfrom just became best.
2524  m_headers_presync_bestpeer = pfrom.GetId();
2525  best_updated = true;
2526  }
2527  if (best_updated && stats.second.has_value()) {
2528  // If the best peer updated, and it is in its first phase, signal.
2529  m_headers_presync_should_signal = true;
2530  }
2531  }
2532 
2533  if (result.success) {
2534  // We only overwrite the headers passed in if processing was
2535  // successful.
2536  headers.swap(result.pow_validated_headers);
2537  }
2538 
2539  return result.success;
2540  }
2541  // Either we didn't have a sync in progress, or something went wrong
2542  // processing these headers, or we are returning headers to the caller to
2543  // process.
2544  return false;
2545 }
2546 
2547 bool PeerManagerImpl::TryLowWorkHeadersSync(Peer& peer, CNode& pfrom, const CBlockIndex* chain_start_header, std::vector<CBlockHeader>& headers)
2548 {
2549  // Calculate the total work on this chain.
2550  arith_uint256 total_work = chain_start_header->nChainWork + CalculateHeadersWork(headers);
2551 
2552  // Our dynamic anti-DoS threshold (minimum work required on a headers chain
2553  // before we'll store it)
2554  arith_uint256 minimum_chain_work = GetAntiDoSWorkThreshold();
2555 
2556  // Avoid DoS via low-difficulty-headers by only processing if the headers
2557  // are part of a chain with sufficient work.
2558  if (total_work < minimum_chain_work) {
2559  // Only try to sync with this peer if their headers message was full;
2560  // otherwise they don't have more headers after this so no point in
2561  // trying to sync their too-little-work chain.
2562  if (headers.size() == MAX_HEADERS_RESULTS) {
2563  // Note: we could advance to the last header in this set that is
2564  // known to us, rather than starting at the first header (which we
2565  // may already have); however this is unlikely to matter much since
2566  // ProcessHeadersMessage() already handles the case where all
2567  // headers in a received message are already known and are
2568  // ancestors of m_best_header or chainActive.Tip(), by skipping
2569  // this logic in that case. So even if the first header in this set
2570  // of headers is known, some header in this set must be new, so
2571  // advancing to the first unknown header would be a small effect.
2572  LOCK(peer.m_headers_sync_mutex);
2573  peer.m_headers_sync.reset(new HeadersSyncState(peer.m_id, m_chainparams.GetConsensus(),
2574  chain_start_header, minimum_chain_work));
2575 
2576  // Now a HeadersSyncState object for tracking this synchronization is created,
2577  // process the headers using it as normal.
2578  if (!IsContinuationOfLowWorkHeadersSync(peer, pfrom, headers)) {
2579  // Something went wrong, reset the headers sync.
2580  peer.m_headers_sync.reset(nullptr);
2581  LOCK(m_headers_presync_mutex);
2582  m_headers_presync_stats.erase(peer.m_id);
2583  }
2584  } else {
2585  LogPrint(BCLog::NET, "Ignoring low-work chain (height=%u) from peer=%d\n", chain_start_header->nHeight + headers.size(), pfrom.GetId());
2586  }
2587 
2588  // The peer has not yet given us a chain that meets our work threshold,
2589  // so we want to prevent further processing of the headers in any case.
2590  headers = {};
2591  return true;
2592  }
2593 
2594  return false;
2595 }
2596 
2597 bool PeerManagerImpl::IsAncestorOfBestHeaderOrTip(const CBlockIndex* header)
2598 {
2599  if (header == nullptr) {
2600  return false;
2601  } else if (m_chainman.m_best_header != nullptr && header == m_chainman.m_best_header->GetAncestor(header->nHeight)) {
2602  return true;
2603  } else if (m_chainman.ActiveChain().Contains(header)) {
2604  return true;
2605  }
2606  return false;
2607 }
2608 
2609 bool PeerManagerImpl::MaybeSendGetHeaders(CNode& pfrom, const CBlockLocator& locator, Peer& peer)
2610 {
2611  const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
2612 
2613  const auto current_time = NodeClock::now();
2614 
2615  // Only allow a new getheaders message to go out if we don't have a recent
2616  // one already in-flight
2617  if (current_time - peer.m_last_getheaders_timestamp > HEADERS_RESPONSE_TIME) {
2618  m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETHEADERS, locator, uint256()));
2619  peer.m_last_getheaders_timestamp = current_time;
2620  return true;
2621  }
2622  return false;
2623 }
2624 
2625 /*
2626  * Given a new headers tip ending in pindexLast, potentially request blocks towards that tip.
2627  * We require that the given tip have at least as much work as our tip, and for
2628  * our current tip to be "close to synced" (see CanDirectFetch()).
2629  */
2630 void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex* pindexLast)
2631 {
2632  const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
2633 
2634  LOCK(cs_main);
2635  CNodeState *nodestate = State(pfrom.GetId());
2636 
2637  if (CanDirectFetch() && pindexLast->IsValid(BLOCK_VALID_TREE) && m_chainman.ActiveChain().Tip()->nChainWork <= pindexLast->nChainWork) {
2638 
2639  std::vector<const CBlockIndex*> vToFetch;
2640  const CBlockIndex *pindexWalk = pindexLast;
2641  // Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
2642  while (pindexWalk && !m_chainman.ActiveChain().Contains(pindexWalk) && vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
2643  if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
2644  !IsBlockRequested(pindexWalk->GetBlockHash()) &&
2645  (!DeploymentActiveAt(*pindexWalk, m_chainman, Consensus::DEPLOYMENT_SEGWIT) || CanServeWitnesses(peer))) {
2646  // We don't have this block, and it's not yet in flight.
2647  vToFetch.push_back(pindexWalk);
2648  }
2649  pindexWalk = pindexWalk->pprev;
2650  }
2651  // If pindexWalk still isn't on our main chain, we're looking at a
2652  // very large reorg at a time we think we're close to caught up to
2653  // the main chain -- this shouldn't really happen. Bail out on the
2654  // direct fetch and rely on parallel download instead.
2655  if (!m_chainman.ActiveChain().Contains(pindexWalk)) {
2656  LogPrint(BCLog::NET, "Large reorg, won't direct fetch to %s (%d)\n",
2657  pindexLast->GetBlockHash().ToString(),
2658  pindexLast->nHeight);
2659  } else {
2660  std::vector<CInv> vGetData;
2661  // Download as much as possible, from earliest to latest.
2662  for (const CBlockIndex *pindex : reverse_iterate(vToFetch)) {
2663  if (nodestate->nBlocksInFlight >= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
2664  // Can't download any more from this peer
2665  break;
2666  }
2667  uint32_t nFetchFlags = GetFetchFlags(peer);
2668  vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
2669  BlockRequested(pfrom.GetId(), *pindex);
2670  LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n",
2671  pindex->GetBlockHash().ToString(), pfrom.GetId());
2672  }
2673  if (vGetData.size() > 1) {
2674  LogPrint(BCLog::NET, "Downloading blocks toward %s (%d) via headers direct fetch\n",
2675  pindexLast->GetBlockHash().ToString(), pindexLast->nHeight);
2676  }
2677  if (vGetData.size() > 0) {
2678  if (!m_ignore_incoming_txs &&
2679  nodestate->m_provides_cmpctblocks &&
2680  vGetData.size() == 1 &&
2681  mapBlocksInFlight.size() == 1 &&
2682  pindexLast->pprev->IsValid(BLOCK_VALID_CHAIN)) {
2683  // In any case, we want to download using a compact block, not a regular one
2684  vGetData[0] = CInv(MSG_CMPCT_BLOCK, vGetData[0].hash);
2685  }
2686  m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, vGetData));
2687  }
2688  }
2689  }
2690 }
2691 
2697 void PeerManagerImpl::UpdatePeerStateForReceivedHeaders(CNode& pfrom,
2698  const CBlockIndex *pindexLast, bool received_new_header, bool may_have_more_headers)
2699 {
2700  LOCK(cs_main);
2701  CNodeState *nodestate = State(pfrom.GetId());
2702  if (nodestate->nUnconnectingHeaders > 0) {
2703  LogPrint(BCLog::NET, "peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n", pfrom.GetId(), nodestate->nUnconnectingHeaders);
2704  }
2705  nodestate->nUnconnectingHeaders = 0;
2706 
2707  assert(pindexLast);
2708  UpdateBlockAvailability(pfrom.GetId(), pindexLast->GetBlockHash());
2709 
2710  // From here, pindexBestKnownBlock should be guaranteed to be non-null,
2711  // because it is set in UpdateBlockAvailability. Some nullptr checks
2712  // are still present, however, as belt-and-suspenders.
2713 
2714  if (received_new_header && pindexLast->nChainWork > m_chainman.ActiveChain().Tip()->nChainWork) {
2715  nodestate->m_last_block_announcement = GetTime();
2716  }
2717 
2718  // If we're in IBD, we want outbound peers that will serve us a useful
2719  // chain. Disconnect peers that are on chains with insufficient work.
2720  if (m_chainman.ActiveChainstate().IsInitialBlockDownload() && !may_have_more_headers) {
2721  // If the peer has no more headers to give us, then we know we have
2722  // their tip.
2723  if (nodestate->pindexBestKnownBlock && nodestate->pindexBestKnownBlock->nChainWork < nMinimumChainWork) {
2724  // This peer has too little work on their headers chain to help
2725  // us sync -- disconnect if it is an outbound disconnection
2726  // candidate.
2727  // Note: We compare their tip to nMinimumChainWork (rather than
2728  // m_chainman.ActiveChain().Tip()) because we won't start block download
2729  // until we have a headers chain that has at least
2730  // nMinimumChainWork, even if a peer has a chain past our tip,
2731  // as an anti-DoS measure.
2732  if (pfrom.IsOutboundOrBlockRelayConn()) {
2733  LogPrintf("Disconnecting outbound peer %d -- headers chain has insufficient work\n", pfrom.GetId());
2734  pfrom.fDisconnect = true;
2735  }
2736  }
2737  }
2738 
2739  // If this is an outbound full-relay peer, check to see if we should protect
2740  // it from the bad/lagging chain logic.
2741  // Note that outbound block-relay peers are excluded from this protection, and
2742  // thus always subject to eviction under the bad/lagging chain logic.
2743  // See ChainSyncTimeoutState.
2744  if (!pfrom.fDisconnect && pfrom.IsFullOutboundConn() && nodestate->pindexBestKnownBlock != nullptr) {
2745  if (m_outbound_peers_with_protect_from_disconnect < MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT && nodestate->pindexBestKnownBlock->nChainWork >= m_chainman.ActiveChain().Tip()->nChainWork && !nodestate->m_chain_sync.m_protect) {
2746  LogPrint(BCLog::NET, "Protecting outbound peer=%d from eviction\n", pfrom.GetId());
2747  nodestate->m_chain_sync.m_protect = true;
2748  ++m_outbound_peers_with_protect_from_disconnect;
2749  }
2750  }
2751 }
2752 
2753 void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
2754  std::vector<CBlockHeader>&& headers,
2755  bool via_compact_block)
2756 {
2757  size_t nCount = headers.size();
2758 
2759  if (nCount == 0) {
2760  // Nothing interesting. Stop asking this peers for more headers.
2761  // If we were in the middle of headers sync, receiving an empty headers
2762  // message suggests that the peer suddenly has nothing to give us
2763  // (perhaps it reorged to our chain). Clear download state for this peer.
2764  LOCK(peer.m_headers_sync_mutex);
2765  if (peer.m_headers_sync) {
2766  peer.m_headers_sync.reset(nullptr);
2767  LOCK(m_headers_presync_mutex);
2768  m_headers_presync_stats.erase(pfrom.GetId());
2769  }
2770  return;
2771  }
2772 
2773  // Before we do any processing, make sure these pass basic sanity checks.
2774  // We'll rely on headers having valid proof-of-work further down, as an
2775  // anti-DoS criteria (note: this check is required before passing any
2776  // headers into HeadersSyncState).
2777  if (!CheckHeadersPoW(headers, m_chainparams.GetConsensus(), peer)) {
2778  // Misbehaving() calls are handled within CheckHeadersPoW(), so we can
2779  // just return. (Note that even if a header is announced via compact
2780  // block, the header itself should be valid, so this type of error can
2781  // always be punished.)
2782  return;
2783  }
2784 
2785  const CBlockIndex *pindexLast = nullptr;
2786 
2787  // We'll set already_validated_work to true if these headers are
2788  // successfully processed as part of a low-work headers sync in progress
2789  // (either in PRESYNC or REDOWNLOAD phase).
2790  // If true, this will mean that any headers returned to us (ie during
2791  // REDOWNLOAD) can be validated without further anti-DoS checks.
2792  bool already_validated_work = false;
2793 
2794  // If we're in the middle of headers sync, let it do its magic.
2795  bool have_headers_sync = false;
2796  {
2797  LOCK(peer.m_headers_sync_mutex);
2798 
2799  already_validated_work = IsContinuationOfLowWorkHeadersSync(peer, pfrom, headers);
2800 
2801  // The headers we passed in may have been:
2802  // - untouched, perhaps if no headers-sync was in progress, or some
2803  // failure occurred
2804  // - erased, such as if the headers were successfully processed and no
2805  // additional headers processing needs to take place (such as if we
2806  // are still in PRESYNC)
2807  // - replaced with headers that are now ready for validation, such as
2808  // during the REDOWNLOAD phase of a low-work headers sync.
2809  // So just check whether we still have headers that we need to process,
2810  // or not.
2811  if (headers.empty()) {
2812  return;
2813  }
2814 
2815  have_headers_sync = !!peer.m_headers_sync;
2816  }
2817 
2818  // Do these headers connect to something in our block index?
2819  const CBlockIndex *chain_start_header{WITH_LOCK(::cs_main, return m_chainman.m_blockman.LookupBlockIndex(headers[0].hashPrevBlock))};
2820  bool headers_connect_blockindex{chain_start_header != nullptr};
2821 
2822  if (!headers_connect_blockindex) {
2823  if (nCount <= MAX_BLOCKS_TO_ANNOUNCE) {
2824  // If this looks like it could be a BIP 130 block announcement, use
2825  // special logic for handling headers that don't connect, as this
2826  // could be benign.
2827  HandleFewUnconnectingHeaders(pfrom, peer, headers);
2828  } else {
2829  Misbehaving(peer, 10, "invalid header received");
2830  }
2831  return;
2832  }
2833 
2834  // If the headers we received are already in memory and an ancestor of
2835  // m_best_header or our tip, skip anti-DoS checks. These headers will not
2836  // use any more memory (and we are not leaking information that could be
2837  // used to fingerprint us).
2838  const CBlockIndex *last_received_header{nullptr};
2839  {
2840  LOCK(cs_main);
2841  last_received_header = m_chainman.m_blockman.LookupBlockIndex(headers.back().GetHash());
2842  if (IsAncestorOfBestHeaderOrTip(last_received_header)) {
2843  already_validated_work = true;
2844  }
2845  }
2846 
2847  // If our peer has NetPermissionFlags::NoBan privileges, then bypass our
2848  // anti-DoS logic (this saves bandwidth when we connect to a trusted peer
2849  // on startup).
2851  already_validated_work = true;
2852  }
2853 
2854  // At this point, the headers connect to something in our block index.
2855  // Do anti-DoS checks to determine if we should process or store for later
2856  // processing.
2857  if (!already_validated_work && TryLowWorkHeadersSync(peer, pfrom,
2858  chain_start_header, headers)) {
2859  // If we successfully started a low-work headers sync, then there
2860  // should be no headers to process any further.
2861  Assume(headers.empty());
2862  return;
2863  }
2864 
2865  // At this point, we have a set of headers with sufficient work on them
2866  // which can be processed.
2867 
2868  // If we don't have the last header, then this peer will have given us
2869  // something new (if these headers are valid).
2870  bool received_new_header{last_received_header == nullptr};
2871 
2872  // Now process all the headers.
2873  BlockValidationState state;
2874  if (!m_chainman.ProcessNewBlockHeaders(headers, /*min_pow_checked=*/true, state, &pindexLast)) {
2875  if (state.IsInvalid()) {
2876  MaybePunishNodeForBlock(pfrom.GetId(), state, via_compact_block, "invalid header received");
2877  return;
2878  }
2879  }
2880  Assume(pindexLast);
2881 
2882  // Consider fetching more headers if we are not using our headers-sync mechanism.
2883  if (nCount == MAX_HEADERS_RESULTS && !have_headers_sync) {
2884  // Headers message had its maximum size; the peer may have more headers.
2885  if (MaybeSendGetHeaders(pfrom, GetLocator(pindexLast), peer)) {
2886  LogPrint(BCLog::NET, "more getheaders (%d) to end to peer=%d (startheight:%d)\n",
2887  pindexLast->nHeight, pfrom.GetId(), peer.m_starting_height);
2888  }
2889  }
2890 
2891  UpdatePeerStateForReceivedHeaders(pfrom, pindexLast, received_new_header, nCount == MAX_HEADERS_RESULTS);
2892 
2893  // Consider immediately downloading blocks.
2894  HeadersDirectFetchBlocks(pfrom, peer, pindexLast);
2895 
2896  return;
2897 }
2898 
2907 void PeerManagerImpl::ProcessOrphanTx(std::set<uint256>& orphan_work_set)
2908 {
2911 
2912  while (!orphan_work_set.empty()) {
2913  const uint256 orphanHash = *orphan_work_set.begin();
2914  orphan_work_set.erase(orphan_work_set.begin());
2915 
2916  const auto [porphanTx, from_peer] = m_orphanage.GetTx(orphanHash);
2917  if (porphanTx == nullptr) continue;
2918 
2919  const MempoolAcceptResult result = m_chainman.ProcessTransaction(porphanTx);
2920  const TxValidationState& state = result.m_state;
2921 
2923  LogPrint(BCLog::MEMPOOL, " accepted orphan tx %s\n", orphanHash.ToString());
2924  RelayTransaction(orphanHash, porphanTx->GetWitnessHash());
2925  m_orphanage.AddChildrenToWorkSet(*porphanTx, orphan_work_set);
2926  m_orphanage.EraseTx(orphanHash);
2927  for (const CTransactionRef& removedTx : result.m_replaced_transactions.value()) {
2928  AddToCompactExtraTransactions(removedTx);
2929  }
2930  break;
2931  } else if (state.GetResult() != TxValidationResult::TX_MISSING_INPUTS) {
2932  if (state.IsInvalid()) {
2933  LogPrint(BCLog::MEMPOOL, " invalid orphan tx %s from peer=%d. %s\n",
2934  orphanHash.ToString(),
2935  from_peer,
2936  state.ToString());
2937  // Maybe punish peer that gave us an invalid orphan tx
2938  MaybePunishNodeForTx(from_peer, state);
2939  }
2940  // Has inputs but not accepted to mempool
2941  // Probably non-standard or insufficient fee
2942  LogPrint(BCLog::MEMPOOL, " removed orphan tx %s\n", orphanHash.ToString());
2944  // We can add the wtxid of this transaction to our reject filter.
2945  // Do not add txids of witness transactions or witness-stripped
2946  // transactions to the filter, as they can have been malleated;
2947  // adding such txids to the reject filter would potentially
2948  // interfere with relay of valid transactions from peers that
2949  // do not support wtxid-based relay. See
2950  // https://github.com/bitcoin/bitcoin/issues/8279 for details.
2951  // We can remove this restriction (and always add wtxids to
2952  // the filter even for witness stripped transactions) once
2953  // wtxid-based relay is broadly deployed.
2954  // See also comments in https://github.com/bitcoin/bitcoin/pull/18044#discussion_r443419034
2955  // for concerns around weakening security of unupgraded nodes
2956  // if we start doing this too early.
2957  m_recent_rejects.insert(porphanTx->GetWitnessHash());
2958  // If the transaction failed for TX_INPUTS_NOT_STANDARD,
2959  // then we know that the witness was irrelevant to the policy
2960  // failure, since this check depends only on the txid
2961  // (the scriptPubKey being spent is covered by the txid).
2962  // Add the txid to the reject filter to prevent repeated
2963  // processing of this transaction in the event that child
2964  // transactions are later received (resulting in
2965  // parent-fetching by txid via the orphan-handling logic).
2966  if (state.GetResult() == TxValidationResult::TX_INPUTS_NOT_STANDARD && porphanTx->GetWitnessHash() != porphanTx->GetHash()) {
2967  // We only add the txid if it differs from the wtxid, to
2968  // avoid wasting entries in the rolling bloom filter.
2969  m_recent_rejects.insert(porphanTx->GetHash());
2970  }
2971  }
2972  m_orphanage.EraseTx(orphanHash);
2973  break;
2974  }
2975  }
2976 }
2977 
2978 bool PeerManagerImpl::PrepareBlockFilterRequest(CNode& node, Peer& peer,
2979  BlockFilterType filter_type, uint32_t start_height,
2980  const uint256& stop_hash, uint32_t max_height_diff,
2981  const CBlockIndex*& stop_index,
2982  BlockFilterIndex*& filter_index)
2983 {
2984  const bool supported_filter_type =
2985  (filter_type == BlockFilterType::BASIC &&
2986  (peer.m_our_services & NODE_COMPACT_FILTERS));
2987  if (!supported_filter_type) {
2988  LogPrint(BCLog::NET, "peer %d requested unsupported block filter type: %d\n",
2989  node.GetId(), static_cast<uint8_t>(filter_type));
2990  node.fDisconnect = true;
2991  return false;
2992  }
2993 
2994  {
2995  LOCK(cs_main);
2996  stop_index = m_chainman.m_blockman.LookupBlockIndex(stop_hash);
2997 
2998  // Check that the stop block exists and the peer would be allowed to fetch it.
2999  if (!stop_index || !BlockRequestAllowed(stop_index)) {
3000  LogPrint(BCLog::NET, "peer %d requested invalid block hash: %s\n",
3001  node.GetId(), stop_hash.ToString());
3002  node.fDisconnect = true;
3003  return false;
3004  }
3005  }
3006 
3007  uint32_t stop_height = stop_index->nHeight;
3008  if (start_height > stop_height) {
3009  LogPrint(BCLog::NET, "peer %d sent invalid getcfilters/getcfheaders with " /* Continued */
3010  "start height %d and stop height %d\n",
3011  node.GetId(), start_height, stop_height);
3012  node.fDisconnect = true;
3013  return false;
3014  }
3015  if (stop_height - start_height >= max_height_diff) {
3016  LogPrint(BCLog::NET, "peer %d requested too many cfilters/cfheaders: %d / %d\n",
3017  node.GetId(), stop_height - start_height + 1, max_height_diff);
3018  node.fDisconnect = true;
3019  return false;
3020  }
3021 
3022  filter_index = GetBlockFilterIndex(filter_type);
3023  if (!filter_index) {
3024  LogPrint(BCLog::NET, "Filter index for supported type %s not found\n", BlockFilterTypeName(filter_type));
3025  return false;
3026  }
3027 
3028  return true;
3029 }
3030 
3031 void PeerManagerImpl::ProcessGetCFilters(CNode& node,Peer& peer, CDataStream& vRecv)
3032 {
3033  uint8_t filter_type_ser;
3034  uint32_t start_height;
3035  uint256 stop_hash;
3036 
3037  vRecv >> filter_type_ser >> start_height >> stop_hash;
3038 
3039  const BlockFilterType filter_type = static_cast<BlockFilterType>(filter_type_ser);
3040 
3041  const CBlockIndex* stop_index;
3042  BlockFilterIndex* filter_index;
3043  if (!PrepareBlockFilterRequest(node, peer, filter_type, start_height, stop_hash,
3044  MAX_GETCFILTERS_SIZE, stop_index, filter_index)) {
3045  return;
3046  }
3047 
3048  std::vector<BlockFilter> filters;
3049  if (!filter_index->LookupFilterRange(start_height, stop_index, filters)) {
3050  LogPrint(BCLog::NET, "Failed to find block filter in index: filter_type=%s, start_height=%d, stop_hash=%s\n",
3051  BlockFilterTypeName(filter_type), start_height, stop_hash.ToString());
3052  return;
3053  }
3054 
3055  for (const auto& filter : filters) {
3056  CSerializedNetMsg msg = CNetMsgMaker(node.GetCommonVersion())
3057  .Make(NetMsgType::CFILTER, filter);
3058  m_connman.PushMessage(&node, std::move(msg));
3059  }
3060 }
3061 
3062 void PeerManagerImpl::ProcessGetCFHeaders(CNode& node, Peer& peer, CDataStream& vRecv)
3063 {
3064  uint8_t filter_type_ser;
3065  uint32_t start_height;
3066  uint256 stop_hash;
3067 
3068  vRecv >> filter_type_ser >> start_height >> stop_hash;
3069 
3070  const BlockFilterType filter_type = static_cast<BlockFilterType>(filter_type_ser);
3071 
3072  const CBlockIndex* stop_index;
3073  BlockFilterIndex* filter_index;
3074  if (!PrepareBlockFilterRequest(node, peer, filter_type, start_height, stop_hash,
3075  MAX_GETCFHEADERS_SIZE, stop_index, filter_index)) {
3076  return;
3077  }
3078 
3079  uint256 prev_header;
3080  if (start_height > 0) {
3081  const CBlockIndex* const prev_block =
3082  stop_index->GetAncestor(static_cast<int>(start_height - 1));
3083  if (!filter_index->LookupFilterHeader(prev_block, prev_header)) {
3084  LogPrint(BCLog::NET, "Failed to find block filter header in index: filter_type=%s, block_hash=%s\n",
3085  BlockFilterTypeName(filter_type), prev_block->GetBlockHash().ToString());
3086  return;
3087  }
3088  }
3089 
3090  std::vector<uint256> filter_hashes;
3091  if (!filter_index->LookupFilterHashRange(start_height, stop_index, filter_hashes)) {
3092  LogPrint(BCLog::NET, "Failed to find block filter hashes in index: filter_type=%s, start_height=%d, stop_hash=%s\n",
3093  BlockFilterTypeName(filter_type), start_height, stop_hash.ToString());
3094  return;
3095  }
3096 
3097  CSerializedNetMsg msg = CNetMsgMaker(node.GetCommonVersion())
3098  .Make(NetMsgType::CFHEADERS,
3099  filter_type_ser,
3100  stop_index->GetBlockHash(),
3101  prev_header,
3102  filter_hashes);
3103  m_connman.PushMessage(&node, std::move(msg));
3104 }
3105 
3106 void PeerManagerImpl::ProcessGetCFCheckPt(CNode& node, Peer& peer, CDataStream& vRecv)
3107 {
3108  uint8_t filter_type_ser;
3109  uint256 stop_hash;
3110 
3111  vRecv >> filter_type_ser >> stop_hash;
3112 
3113  const BlockFilterType filter_type = static_cast<BlockFilterType>(filter_type_ser);
3114 
3115  const CBlockIndex* stop_index;
3116  BlockFilterIndex* filter_index;
3117  if (!PrepareBlockFilterRequest(node, peer, filter_type, /*start_height=*/0, stop_hash,
3118  /*max_height_diff=*/std::numeric_limits<uint32_t>::max(),
3119  stop_index, filter_index)) {
3120  return;
3121  }
3122 
3123  std::vector<uint256> headers(stop_index->nHeight / CFCHECKPT_INTERVAL);
3124 
3125  // Populate headers.
3126  const CBlockIndex* block_index = stop_index;
3127  for (int i = headers.size() - 1; i >= 0; i--) {
3128  int height = (i + 1) * CFCHECKPT_INTERVAL;
3129  block_index = block_index->GetAncestor(height);
3130 
3131  if (!filter_index->LookupFilterHeader(block_index, headers[i])) {
3132  LogPrint(BCLog::NET, "Failed to find block filter header in index: filter_type=%s, block_hash=%s\n",
3133  BlockFilterTypeName(filter_type), block_index->GetBlockHash().ToString());
3134  return;
3135  }
3136  }
3137 
3138  CSerializedNetMsg msg = CNetMsgMaker(node.GetCommonVersion())
3139  .Make(NetMsgType::CFCHECKPT,
3140  filter_type_ser,
3141  stop_index->GetBlockHash(),
3142  headers);
3143  m_connman.PushMessage(&node, std::move(msg));
3144 }
3145 
3146 void PeerManagerImpl::ProcessBlock(CNode& node, const std::shared_ptr<const CBlock>& block, bool force_processing, bool min_pow_checked)
3147 {
3148  bool new_block{false};
3149  m_chainman.ProcessNewBlock(block, force_processing, min_pow_checked, &new_block);
3150  if (new_block) {
3151  node.m_last_block_time = GetTime<std::chrono::seconds>();
3152  // In case this block came from a different peer than we requested
3153  // from, we can erase the block request now anyway (as we just stored
3154  // this block to disk).
3155  LOCK(cs_main);
3156  RemoveBlockRequest(block->GetHash(), std::nullopt);
3157  } else {
3158  LOCK(cs_main);
3159  mapBlockSource.erase(block->GetHash());
3160  }
3161 }
3162 
3163 void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
3164  const std::chrono::microseconds time_received,
3165  const std::atomic<bool>& interruptMsgProc)
3166 {
3167  LogPrint(BCLog::NET, "received: %s (%u bytes) peer=%d\n", SanitizeString(msg_type), vRecv.size(), pfrom.GetId());
3168 
3169  PeerRef peer = GetPeerRef(pfrom.GetId());
3170  if (peer == nullptr) return;
3171 
3172  if (msg_type == NetMsgType::VERSION) {
3173  if (pfrom.nVersion != 0) {
3174  LogPrint(BCLog::NET, "redundant version message from peer=%d\n", pfrom.GetId());
3175  return;
3176  }
3177 
3178  int64_t nTime;
3179  CService addrMe;
3180  uint64_t nNonce = 1;
3181  ServiceFlags nServices;
3182  int nVersion;
3183  std::string cleanSubVer;
3184  int starting_height = -1;
3185  bool fRelay = true;
3186 
3187  vRecv >> nVersion >> Using<CustomUintFormatter<8>>(nServices) >> nTime;
3188  if (nTime < 0) {
3189  nTime = 0;
3190  }
3191  vRecv.ignore(8); // Ignore the addrMe service bits sent by the peer
3192  vRecv >> addrMe;
3193  if (!pfrom.IsInboundConn())
3194  {
3195  m_addrman.SetServices(pfrom.addr, nServices);
3196  }
3197  if (pfrom.ExpectServicesFromConn() && !HasAllDesirableServiceFlags(nServices))
3198  {
3199  LogPrint(BCLog::NET, "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom.GetId(), nServices, GetDesirableServiceFlags(nServices));
3200  pfrom.fDisconnect = true;
3201  return;
3202  }
3203 
3204  if (nVersion < MIN_PEER_PROTO_VERSION) {
3205  // disconnect from peers older than this proto version
3206  LogPrint(BCLog::NET, "peer=%d using obsolete version %i; disconnecting\n", pfrom.GetId(), nVersion);
3207  pfrom.fDisconnect = true;
3208  return;
3209  }
3210 
3211  if (!vRecv.empty()) {
3212  // The version message includes information about the sending node which we don't use:
3213  // - 8 bytes (service bits)
3214  // - 16 bytes (ipv6 address)
3215  // - 2 bytes (port)
3216  vRecv.ignore(26);
3217  vRecv >> nNonce;
3218  }
3219  if (!vRecv.empty()) {
3220  std::string strSubVer;
3221  vRecv >> LIMITED_STRING(strSubVer, MAX_SUBVERSION_LENGTH);
3222  cleanSubVer = SanitizeString(strSubVer);
3223  }
3224  if (!vRecv.empty()) {
3225  vRecv >> starting_height;
3226  }
3227  if (!vRecv.empty())
3228  vRecv >> fRelay;
3229  // Disconnect if we connected to ourself
3230  if (pfrom.IsInboundConn() && !m_connman.CheckIncomingNonce(nNonce))
3231  {
3232  LogPrintf("connected to self at %s, disconnecting\n", pfrom.addr.ToString());
3233  pfrom.fDisconnect = true;
3234  return;
3235  }
3236 
3237  if (pfrom.IsInboundConn() && addrMe.IsRoutable())
3238  {
3239  SeenLocal(addrMe);
3240  }
3241 
3242  // Inbound peers send us their version message when they connect.
3243  // We send our version message in response.
3244  if (pfrom.IsInboundConn()) {
3245  PushNodeVersion(pfrom, *peer);
3246  }
3247 
3248  // Change version
3249  const int greatest_common_version = std::min(nVersion, PROTOCOL_VERSION);
3250  pfrom.SetCommonVersion(greatest_common_version);
3251  pfrom.nVersion = nVersion;
3252 
3253  const CNetMsgMaker msg_maker(greatest_common_version);
3254 
3255  if (greatest_common_version >= WTXID_RELAY_VERSION) {
3256  m_connman.PushMessage(&pfrom, msg_maker.Make(NetMsgType::WTXIDRELAY));
3257  }
3258 
3259  // Signal ADDRv2 support (BIP155).
3260  if (greatest_common_version >= 70016) {
3261  // BIP155 defines addrv2 and sendaddrv2 for all protocol versions, but some
3262  // implementations reject messages they don't know. As a courtesy, don't send
3263  // it to nodes with a version before 70016, as no software is known to support
3264  // BIP155 that doesn't announce at least that protocol version number.
3265  m_connman.PushMessage(&pfrom, msg_maker.Make(NetMsgType::SENDADDRV2));
3266  }
3267 
3268  m_connman.PushMessage(&pfrom, msg_maker.Make(NetMsgType::VERACK));
3269 
3271  peer->m_their_services = nServices;
3272  pfrom.SetAddrLocal(addrMe);
3273  {
3274  LOCK(pfrom.m_subver_mutex);
3275  pfrom.cleanSubVer = cleanSubVer;
3276  }
3277  peer->m_starting_height = starting_height;
3278 
3279  // We only initialize the m_tx_relay data structure if:
3280  // - this isn't an outbound block-relay-only connection; and
3281  // - fRelay=true or we're offering NODE_BLOOM to this peer
3282  // (NODE_BLOOM means that the peer may turn on tx relay later)
3283  if (!pfrom.IsBlockOnlyConn() &&
3284  (fRelay || (peer->m_our_services & NODE_BLOOM))) {
3285  auto* const tx_relay = peer->SetTxRelay();
3286  {
3287  LOCK(tx_relay->m_bloom_filter_mutex);
3288  tx_relay->m_relay_txs = fRelay; // set to true after we get the first filter* message
3289  }
3290  if (fRelay) pfrom.m_relays_txs = true;
3291  }
3292 
3293  // Potentially mark this peer as a preferred download peer.
3294  {
3295  LOCK(cs_main);
3296  CNodeState* state = State(pfrom.GetId());
3297  state->fPreferredDownload = (!pfrom.IsInboundConn() || pfrom.HasPermission(NetPermissionFlags::NoBan)) && !pfrom.IsAddrFetchConn() && CanServeBlocks(*peer);
3298  m_num_preferred_download_peers += state->fPreferredDownload;
3299  }
3300 
3301  // Self advertisement & GETADDR logic
3302  if (!pfrom.IsInboundConn() && SetupAddressRelay(pfrom, *peer)) {
3303  // For outbound peers, we try to relay our address (so that other
3304  // nodes can try to find us more quickly, as we have no guarantee
3305  // that an outbound peer is even aware of how to reach us) and do a
3306  // one-time address fetch (to help populate/update our addrman). If
3307  // we're starting up for the first time, our addrman may be pretty
3308  // empty and no one will know who we are, so these mechanisms are
3309  // important to help us connect to the network.
3310  //
3311  // We skip this for block-relay-only peers. We want to avoid
3312  // potentially leaking addr information and we do not want to
3313  // indicate to the peer that we will participate in addr relay.
3314  if (fListen && !m_chainman.ActiveChainstate().IsInitialBlockDownload())
3315  {
3316  CAddress addr{GetLocalAddress(pfrom.addr), peer->m_our_services, Now<NodeSeconds>()};
3317  FastRandomContext insecure_rand;
3318  if (addr.IsRoutable())
3319  {
3320  LogPrint(BCLog::NET, "ProcessMessages: advertising address %s\n", addr.ToString());
3321  PushAddress(*peer, addr, insecure_rand);
3322  } else if (IsPeerAddrLocalGood(&pfrom)) {
3323  // Override just the address with whatever the peer sees us as.
3324  // Leave the port in addr as it was returned by GetLocalAddress()
3325  // above, as this is an outbound connection and the peer cannot
3326  // observe our listening port.
3327  addr.SetIP(addrMe);
3328  LogPrint(BCLog::NET, "ProcessMessages: advertising address %s\n", addr.ToString());
3329  PushAddress(*peer, addr, insecure_rand);
3330  }
3331  }
3332 
3333  // Get recent addresses
3334  m_connman.PushMessage(&pfrom, CNetMsgMaker(greatest_common_version).Make(NetMsgType::GETADDR));
3335  peer->m_getaddr_sent = true;
3336  // When requesting a getaddr, accept an additional MAX_ADDR_TO_SEND addresses in response
3337  // (bypassing the MAX_ADDR_PROCESSING_TOKEN_BUCKET limit).
3338  peer->m_addr_token_bucket += MAX_ADDR_TO_SEND;
3339  }
3340 
3341  if (!pfrom.IsInboundConn()) {
3342  // For non-inbound connections, we update the addrman to record
3343  // connection success so that addrman will have an up-to-date
3344  // notion of which peers are online and available.
3345  //
3346  // While we strive to not leak information about block-relay-only
3347  // connections via the addrman, not moving an address to the tried
3348  // table is also potentially detrimental because new-table entries
3349  // are subject to eviction in the event of addrman collisions. We
3350  // mitigate the information-leak by never calling
3351  // AddrMan::Connected() on block-relay-only peers; see
3352  // FinalizeNode().
3353  //
3354  // This moves an address from New to Tried table in Addrman,
3355  // resolves tried-table collisions, etc.
3356  m_addrman.Good(pfrom.addr);
3357  }
3358 
3359  std::string remoteAddr;
3360  if (fLogIPs)
3361  remoteAddr = ", peeraddr=" + pfrom.addr.ToString();
3362 
3363  LogPrint(BCLog::NET, "receive version message: %s: version %d, blocks=%d, us=%s, txrelay=%d, peer=%d%s\n",
3364  cleanSubVer, pfrom.nVersion,
3365  peer->m_starting_height, addrMe.ToString(), fRelay, pfrom.GetId(),
3366  remoteAddr);
3367 
3368  int64_t nTimeOffset = nTime - GetTime();
3369  pfrom.nTimeOffset = nTimeOffset;
3370  if (!pfrom.IsInboundConn()) {
3371  // Don't use timedata samples from inbound peers to make it
3372  // harder for others to tamper with our adjusted time.
3373  AddTimeData(pfrom.addr, nTimeOffset);
3374  }
3375 
3376  // If the peer is old enough to have the old alert system, send it the final alert.
3377  if (greatest_common_version <= 70012) {
3378  CDataStream finalAlert(ParseHex("60010000000000000000000000ffffff7f00000000ffffff7ffeffff7f01ffffff7f00000000ffffff7f00ffffff7f002f555247454e543a20416c657274206b657920636f6d70726f6d697365642c2075706772616465207265717569726564004630440220653febd6410f470f6bae11cad19c48413becb1ac2c17f908fd0fd53bdc3abd5202206d0e9c96fe88d4a0f01ed9dedae2b6f9e00da94cad0fecaae66ecf689bf71b50"), SER_NETWORK, PROTOCOL_VERSION);
3379  m_connman.PushMessage(&pfrom, CNetMsgMaker(greatest_common_version).Make("alert", finalAlert));
3380  }
3381 
3382  // Feeler connections exist only to verify if address is online.
3383  if (pfrom.IsFeelerConn()) {
3384  LogPrint(BCLog::NET, "feeler connection completed peer=%d; disconnecting\n", pfrom.GetId());
3385  pfrom.fDisconnect = true;
3386  }
3387  return;
3388  }
3389 
3390  if (pfrom.nVersion == 0) {
3391  // Must have a version message before anything else
3392  LogPrint(BCLog::NET, "non-version message before version handshake. Message \"%s\" from peer=%d\n", SanitizeString(msg_type), pfrom.GetId());
3393  return;
3394  }
3395 
3396  // At this point, the outgoing message serialization version can't change.
3397  const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
3398 
3399  if (msg_type == NetMsgType::VERACK) {
3400  if (pfrom.fSuccessfullyConnected) {
3401  LogPrint(BCLog::NET, "ignoring redundant verack message from peer=%d\n", pfrom.GetId());
3402  return;
3403  }
3404 
3405  if (!pfrom.IsInboundConn()) {
3406  LogPrintf("New outbound peer connected: version: %d, blocks=%d, peer=%d%s (%s)\n",
3407  pfrom.nVersion.load(), peer->m_starting_height,
3408  pfrom.GetId(), (fLogIPs ? strprintf(", peeraddr=%s", pfrom.addr.ToString()) : ""),
3409  pfrom.ConnectionTypeAsString());
3410  }
3411 
3412  if (pfrom.GetCommonVersion() >= SHORT_IDS_BLOCKS_VERSION) {
3413  // Tell our peer we are willing to provide version 2 cmpctblocks.
3414  // However, we do not request new block announcements using
3415  // cmpctblock messages.
3416  // We send this to non-NODE NETWORK peers as well, because
3417  // they may wish to request compact blocks from us
3418  m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::SENDCMPCT, /*high_bandwidth=*/false, /*version=*/CMPCTBLOCKS_VERSION));
3419  }
3420 
3421  if (auto tx_relay = peer->GetTxRelay()) {
3422  // `TxRelay::m_tx_inventory_to_send` must be empty before the
3423  // version handshake is completed as
3424  // `TxRelay::m_next_inv_send_time` is first initialised in
3425  // `SendMessages` after the verack is received. Any transactions
3426  // received during the version handshake would otherwise
3427  // immediately be advertised without random delay, potentially
3428  // leaking the time of arrival to a spy.
3429  Assume(WITH_LOCK(
3430  tx_relay->m_tx_inventory_mutex,
3431  return tx_relay->m_tx_inventory_to_send.empty() &&
3432  tx_relay->m_next_inv_send_time == 0s));
3433  }
3434 
3435  pfrom.fSuccessfullyConnected = true;
3436  return;
3437  }
3438 
3439  if (msg_type == NetMsgType::SENDHEADERS) {
3440  LOCK(cs_main);
3441  State(pfrom.GetId())->fPreferHeaders = true;
3442  return;
3443  }
3444 
3445  if (msg_type == NetMsgType::SENDCMPCT) {
3446  bool sendcmpct_hb{false};
3447  uint64_t sendcmpct_version{0};
3448  vRecv >> sendcmpct_hb >> sendcmpct_version;
3449 
3450  // Only support compact block relay with witnesses
3451  if (sendcmpct_version != CMPCTBLOCKS_VERSION) return;
3452 
3453  LOCK(cs_main);
3454  CNodeState* nodestate = State(pfrom.GetId());
3455  nodestate->m_provides_cmpctblocks = true;
3456  nodestate->m_requested_hb_cmpctblocks = sendcmpct_hb;
3457  // save whether peer selects us as BIP152 high-bandwidth peer
3458  // (receiving sendcmpct(1) signals high-bandwidth, sendcmpct(0) low-bandwidth)
3459  pfrom.m_bip152_highbandwidth_from = sendcmpct_hb;
3460  return;
3461  }
3462 
3463  // BIP339 defines feature negotiation of wtxidrelay, which must happen between
3464  // VERSION and VERACK to avoid relay problems from switching after a connection is up.
3465  if (msg_type == NetMsgType::WTXIDRELAY) {
3466  if (pfrom.fSuccessfullyConnected) {
3467  // Disconnect peers that send a wtxidrelay message after VERACK.
3468  LogPrint(BCLog::NET, "wtxidrelay received after verack from peer=%d; disconnecting\n", pfrom.GetId());
3469  pfrom.fDisconnect = true;
3470  return;
3471  }
3472  if (pfrom.GetCommonVersion() >= WTXID_RELAY_VERSION) {
3473  if (!peer->m_wtxid_relay) {
3474  peer->m_wtxid_relay = true;
3475  m_wtxid_relay_peers++;
3476  } else {
3477  LogPrint(BCLog::NET, "ignoring duplicate wtxidrelay from peer=%d\n", pfrom.GetId());
3478  }
3479  } else {
3480  LogPrint(BCLog::NET, "ignoring wtxidrelay due to old common version=%d from peer=%d\n", pfrom.GetCommonVersion(), pfrom.GetId());
3481  }
3482  return;
3483  }
3484 
3485  // BIP155 defines feature negotiation of addrv2 and sendaddrv2, which must happen
3486  // between VERSION and VERACK.
3487  if (msg_type == NetMsgType::SENDADDRV2) {
3488  if (pfrom.fSuccessfullyConnected) {
3489  // Disconnect peers that send a SENDADDRV2 message after VERACK.
3490  LogPrint(BCLog::NET, "sendaddrv2 received after verack from peer=%d; disconnecting\n", pfrom.GetId());
3491  pfrom.fDisconnect = true;
3492  return;
3493  }
3494  peer->m_wants_addrv2 = true;
3495  return;
3496  }
3497 
3498  if (!pfrom.fSuccessfullyConnected) {
3499  LogPrint(BCLog::NET, "Unsupported message \"%s\" prior to verack from peer=%d\n", SanitizeString(msg_type), pfrom.GetId());
3500  return;
3501  }
3502 
3503  if (msg_type == NetMsgType::ADDR || msg_type == NetMsgType::ADDRV2) {
3504  int stream_version = vRecv.GetVersion();
3505  if (msg_type == NetMsgType::ADDRV2) {
3506  // Add ADDRV2_FORMAT to the version so that the CNetAddr and CAddress
3507  // unserialize methods know that an address in v2 format is coming.
3508  stream_version |= ADDRV2_FORMAT;
3509  }
3510 
3511  OverrideStream<CDataStream> s(&vRecv, vRecv.GetType(), stream_version);
3512  std::vector<CAddress> vAddr;
3513 
3514  s >> vAddr;
3515 
3516  if (!SetupAddressRelay(pfrom, *peer)) {
3517  LogPrint(BCLog::NET, "ignoring %s message from %s peer=%d\n", msg_type, pfrom.ConnectionTypeAsString(), pfrom.GetId());
3518  return;
3519  }
3520 
3521  if (vAddr.size() > MAX_ADDR_TO_SEND)
3522  {
3523  Misbehaving(*peer, 20, strprintf("%s message size = %u", msg_type, vAddr.size()));
3524  return;
3525  }
3526 
3527  // Store the new addresses
3528  std::vector<CAddress> vAddrOk;
3529  const auto current_a_time{Now<NodeSeconds>()};
3530 
3531  // Update/increment addr rate limiting bucket.
3532  const auto current_time{GetTime<std::chrono::microseconds>()};
3533  if (peer->m_addr_token_bucket < MAX_ADDR_PROCESSING_TOKEN_BUCKET) {
3534  // Don't increment bucket if it's already full
3535  const auto time_diff = std::max(current_time - peer->m_addr_token_timestamp, 0us);
3536  const double increment = Ticks<SecondsDouble>(time_diff) * MAX_ADDR_RATE_PER_SECOND;
3537  peer->m_addr_token_bucket = std::min<double>(peer->m_addr_token_bucket + increment, MAX_ADDR_PROCESSING_TOKEN_BUCKET);
3538  }
3539  peer->m_addr_token_timestamp = current_time;
3540 
3541  const bool rate_limited = !pfrom.HasPermission(NetPermissionFlags::Addr);
3542  uint64_t num_proc = 0;
3543  uint64_t num_rate_limit = 0;
3544  Shuffle(vAddr.begin(), vAddr.end(), FastRandomContext());
3545  for (CAddress& addr : vAddr)
3546  {
3547  if (interruptMsgProc)
3548  return;
3549 
3550  // Apply rate limiting.
3551  if (peer->m_addr_token_bucket < 1.0) {
3552  if (rate_limited) {
3553  ++num_rate_limit;
3554  continue;
3555  }
3556  } else {
3557  peer->m_addr_token_bucket -= 1.0;
3558  }
3559  // We only bother storing full nodes, though this may include
3560  // things which we would not make an outbound connection to, in
3561  // part because we may make feeler connections to them.
3563  continue;
3564 
3565  if (addr.nTime <= NodeSeconds{100000000s} || addr.nTime > current_a_time + 10min) {
3566  addr.nTime = current_a_time - 5 * 24h;
3567  }
3568  AddAddressKnown(*peer, addr);
3569  if (m_banman && (m_banman->IsDiscouraged(addr) || m_banman->IsBanned(addr))) {
3570  // Do not process banned/discouraged addresses beyond remembering we received them
3571  continue;
3572  }
3573  ++num_proc;
3574  bool fReachable = IsReachable(addr);
3575  if (addr.nTime > current_a_time - 10min && !peer->m_getaddr_sent && vAddr.size() <= 10 && addr.IsRoutable()) {
3576  // Relay to a limited number of other nodes
3577  RelayAddress(pfrom.GetId(), addr, fReachable);
3578  }
3579  // Do not store addresses outside our network
3580  if (fReachable)
3581  vAddrOk.push_back(addr);
3582  }
3583  peer->m_addr_processed += num_proc;
3584  peer->m_addr_rate_limited += num_rate_limit;
3585  LogPrint(BCLog::NET, "Received addr: %u addresses (%u processed, %u rate-limited) from peer=%d\n",
3586  vAddr.size(), num_proc, num_rate_limit, pfrom.GetId());
3587 
3588  m_addrman.Add(vAddrOk, pfrom.addr, 2h);
3589  if (vAddr.size() < 1000) peer->m_getaddr_sent = false;
3590 
3591  // AddrFetch: Require multiple addresses to avoid disconnecting on self-announcements
3592  if (pfrom.IsAddrFetchConn() && vAddr.size() > 1) {
3593  LogPrint(BCLog::NET, "addrfetch connection completed peer=%d; disconnecting\n", pfrom.GetId());
3594  pfrom.fDisconnect = true;
3595  }
3596  return;
3597  }
3598 
3599  if (msg_type == NetMsgType::INV) {
3600  std::vector<CInv> vInv;
3601  vRecv >> vInv;
3602  if (vInv.size() > MAX_INV_SZ)
3603  {
3604  Misbehaving(*peer, 20, strprintf("inv message size = %u", vInv.size()));
3605  return;
3606  }
3607 
3608  const bool reject_tx_invs{RejectIncomingTxs(pfrom)};
3609 
3610  LOCK(cs_main);
3611 
3612  const auto current_time{GetTime<std::chrono::microseconds>()};
3613  uint256* best_block{nullptr};
3614 
3615  for (CInv& inv : vInv) {
3616  if (interruptMsgProc) return;
3617 
3618  // Ignore INVs that don't match wtxidrelay setting.
3619  // Note that orphan parent fetching always uses MSG_TX GETDATAs regardless of the wtxidrelay setting.
3620  // This is fine as no INV messages are involved in that process.
3621  if (peer->m_wtxid_relay) {
3622  if (inv.IsMsgTx()) continue;
3623  } else {
3624  if (inv.IsMsgWtx()) continue;
3625  }
3626 
3627  if (inv.IsMsgBlk()) {
3628  const bool fAlreadyHave = AlreadyHaveBlock(inv.hash);
3629  LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId());
3630 
3631  UpdateBlockAvailability(pfrom.GetId(), inv.hash);
3632  if (!fAlreadyHave && !fImporting && !fReindex && !IsBlockRequested(inv.hash)) {
3633  // Headers-first is the primary method of announcement on
3634  // the network. If a node fell back to sending blocks by
3635  // inv, it may be for a re-org, or because we haven't
3636  // completed initial headers sync. The final block hash
3637  // provided should be the highest, so send a getheaders and
3638  // then fetch the blocks we need to catch up.
3639  best_block = &inv.hash;
3640  }
3641  } else if (inv.IsGenTxMsg()) {
3642  if (reject_tx_invs) {
3643  LogPrint(BCLog::NET, "transaction (%s) inv sent in violation of protocol, disconnecting peer=%d\n", inv.hash.ToString(), pfrom.GetId());
3644  pfrom.fDisconnect = true;
3645  return;
3646  }
3647  const GenTxid gtxid = ToGenTxid(inv);
3648  const bool fAlreadyHave = AlreadyHaveTx(gtxid);
3649  LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId());
3650 
3651  AddKnownTx(*peer, inv.hash);
3652  if (!fAlreadyHave && !m_chainman.ActiveChainstate().IsInitialBlockDownload()) {
3653  AddTxAnnouncement(pfrom, gtxid, current_time);
3654  }
3655  } else {
3656  LogPrint(BCLog::NET, "Unknown inv type \"%s\" received from peer=%d\n", inv.ToString(), pfrom.GetId());
3657  }
3658  }
3659 
3660  if (best_block != nullptr) {
3661  // If we haven't started initial headers-sync with this peer, then
3662  // consider sending a getheaders now. On initial startup, there's a
3663  // reliability vs bandwidth tradeoff, where we are only trying to do
3664  // initial headers sync with one peer at a time, with a long
3665  // timeout (at which point, if the sync hasn't completed, we will
3666  // disconnect the peer and then choose another). In the meantime,
3667  // as new blocks are found, we are willing to add one new peer per
3668  // block to sync with as well, to sync quicker in the case where
3669  // our initial peer is unresponsive (but less bandwidth than we'd
3670  // use if we turned on sync with all peers).
3671  CNodeState& state{*Assert(State(pfrom.GetId()))};
3672  if (state.fSyncStarted || (!peer->m_inv_triggered_getheaders_before_sync && *best_block != m_last_block_inv_triggering_headers_sync)) {
3673  if (MaybeSendGetHeaders(pfrom, GetLocator(m_chainman.m_best_header), *peer)) {
3674  LogPrint(BCLog::NET, "getheaders (%d) %s to peer=%d\n",
3675  m_chainman.m_best_header->nHeight, best_block->ToString(),
3676  pfrom.GetId());
3677  }
3678  if (!state.fSyncStarted) {
3679  peer->m_inv_triggered_getheaders_before_sync = true;
3680  // Update the last block hash that triggered a new headers
3681  // sync, so that we don't turn on headers sync with more
3682  // than 1 new peer every new block.
3683  m_last_block_inv_triggering_headers_sync = *best_block;
3684  }
3685  }
3686  }
3687 
3688  return;
3689  }
3690 
3691  if (msg_type == NetMsgType::GETDATA) {
3692  std::vector<CInv> vInv;
3693  vRecv >> vInv;
3694  if (vInv.size() > MAX_INV_SZ)
3695  {
3696  Misbehaving(*peer, 20, strprintf("getdata message size = %u", vInv.size()));
3697  return;
3698  }
3699 
3700  LogPrint(BCLog::NET, "received getdata (%u invsz) peer=%d\n", vInv.size(), pfrom.GetId());
3701 
3702  if (vInv.size() > 0) {
3703  LogPrint(BCLog::NET, "received getdata for: %s peer=%d\n", vInv[0].ToString(), pfrom.GetId());
3704  }
3705 
3706  {
3707  LOCK(peer->m_getdata_requests_mutex);
3708  peer->m_getdata_requests.insert(peer->m_getdata_requests.end(), vInv.begin(), vInv.end());
3709  ProcessGetData(pfrom, *peer, interruptMsgProc);
3710  }
3711 
3712  return;
3713  }
3714 
3715  if (msg_type == NetMsgType::GETBLOCKS) {
3716  CBlockLocator locator;
3717  uint256 hashStop;
3718  vRecv >> locator >> hashStop;
3719 
3720  if (locator.vHave.size() > MAX_LOCATOR_SZ) {
3721  LogPrint(BCLog::NET, "getblocks locator size %lld > %d, disconnect peer=%d\n", locator.vHave.size(), MAX_LOCATOR_SZ, pfrom.GetId());
3722  pfrom.fDisconnect = true;
3723  return;
3724  }
3725 
3726  // We might have announced the currently-being-connected tip using a
3727  // compact block, which resulted in the peer sending a getblocks
3728  // request, which we would otherwise respond to without the new block.
3729  // To avoid this situation we simply verify that we are on our best
3730  // known chain now. This is super overkill, but we handle it better
3731  // for getheaders requests, and there are no known nodes which support
3732  // compact blocks but still use getblocks to request blocks.
3733  {
3734  std::shared_ptr<const CBlock> a_recent_block;
3735  {
3736  LOCK(m_most_recent_block_mutex);
3737  a_recent_block = m_most_recent_block;
3738  }
3739  BlockValidationState state;
3740  if (!m_chainman.ActiveChainstate().ActivateBestChain(state, a_recent_block)) {
3741  LogPrint(BCLog::NET, "failed to activate chain (%s)\n", state.ToString());
3742  }
3743  }
3744 
3745  LOCK(cs_main);
3746 
3747  // Find the last block the caller has in the main chain
3748  const CBlockIndex* pindex = m_chainman.ActiveChainstate().FindForkInGlobalIndex(locator);
3749 
3750  // Send the rest of the chain
3751  if (pindex)
3752  pindex = m_chainman.ActiveChain().Next(pindex);
3753  int nLimit = 500;
3754  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());
3755  for (; pindex; pindex = m_chainman.ActiveChain().Next(pindex))
3756  {
3757  if (pindex->GetBlockHash() == hashStop)
3758  {
3759  LogPrint(BCLog::NET, " getblocks stopping at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
3760  break;
3761  }
3762  // If pruning, don't inv blocks unless we have on disk and are likely to still have
3763  // for some reasonable time window (1 hour) that block relay might require.
3764  const int nPrunedBlocksLikelyToHave = MIN_BLOCKS_TO_KEEP - 3600 / m_chainparams.GetConsensus().nPowTargetSpacing;
3765  if (fPruneMode && (!(pindex->nStatus & BLOCK_HAVE_DATA) || pindex->nHeight <= m_chainman.ActiveChain().Tip()->nHeight - nPrunedBlocksLikelyToHave))
3766  {
3767  LogPrint(BCLog::NET, " getblocks stopping, pruned or too old block at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
3768  break;
3769  }
3770  WITH_LOCK(peer->m_block_inv_mutex, peer->m_blocks_for_inv_relay.push_back(pindex->GetBlockHash()));
3771  if (--nLimit <= 0) {
3772  // When this block is requested, we'll send an inv that'll
3773  // trigger the peer to getblocks the next batch of inventory.
3774  LogPrint(BCLog::NET, " getblocks stopping at limit %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
3775  WITH_LOCK(peer->m_block_inv_mutex, {peer->m_continuation_block = pindex->GetBlockHash();});
3776  break;
3777  }
3778  }
3779  return;
3780  }
3781 
3782  if (msg_type == NetMsgType::GETBLOCKTXN) {
3784  vRecv >> req;
3785 
3786  std::shared_ptr<const CBlock> recent_block;
3787  {
3788  LOCK(m_most_recent_block_mutex);
3789  if (m_most_recent_block_hash == req.blockhash)
3790  recent_block = m_most_recent_block;
3791  // Unlock m_most_recent_block_mutex to avoid cs_main lock inversion
3792  }
3793  if (recent_block) {
3794  SendBlockTransactions(pfrom, *peer, *recent_block, req);
3795  return;
3796  }
3797 
3798  {
3799  LOCK(cs_main);
3800 
3801  const CBlockIndex* pindex = m_chainman.m_blockman.LookupBlockIndex(req.blockhash);
3802  if (!pindex || !(pindex->nStatus & BLOCK_HAVE_DATA)) {
3803  LogPrint(BCLog::NET, "Peer %d sent us a getblocktxn for a block we don't have\n", pfrom.GetId());
3804  return;
3805  }
3806 
3807  if (pindex->nHeight >= m_chainman.ActiveChain().Height() - MAX_BLOCKTXN_DEPTH) {
3808  CBlock block;
3809  bool ret = ReadBlockFromDisk(block, pindex, m_chainparams.GetConsensus());
3810  assert(ret);
3811 
3812  SendBlockTransactions(pfrom, *peer, block, req);
3813  return;
3814  }
3815  }
3816 
3817  // If an older block is requested (should never happen in practice,
3818  // but can happen in tests) send a block response instead of a
3819  // blocktxn response. Sending a full block response instead of a
3820  // small blocktxn response is preferable in the case where a peer
3821  // might maliciously send lots of getblocktxn requests to trigger
3822  // expensive disk reads, because it will require the peer to
3823  // actually receive all the data read from disk over the network.
3824  LogPrint(BCLog::NET, "Peer %d sent us a getblocktxn for a block > %i deep\n", pfrom.GetId(), MAX_BLOCKTXN_DEPTH);
3825  CInv inv{MSG_WITNESS_BLOCK, req.blockhash};
3826  WITH_LOCK(peer->m_getdata_requests_mutex, peer->m_getdata_requests.push_back(inv));
3827  // The message processing loop will go around again (without pausing) and we'll respond then
3828  return;
3829  }
3830 
3831  if (msg_type == NetMsgType::GETHEADERS) {
3832  CBlockLocator locator;
3833  uint256 hashStop;
3834  vRecv >> locator >> hashStop;
3835 
3836  if (locator.vHave.size() > MAX_LOCATOR_SZ) {
3837  LogPrint(BCLog::NET, "getheaders locator size %lld > %d, disconnect peer=%d\n", locator.vHave.size(), MAX_LOCATOR_SZ, pfrom.GetId());
3838  pfrom.fDisconnect = true;
3839  return;
3840  }
3841 
3842  if (fImporting || fReindex) {
3843  LogPrint(BCLog::NET, "Ignoring getheaders from peer=%d while importing/reindexing\n", pfrom.GetId());
3844  return;
3845  }
3846 
3847  LOCK(cs_main);
3848 
3849  // Note that if we were to be on a chain that forks from the checkpointed
3850  // chain, then serving those headers to a peer that has seen the
3851  // checkpointed chain would cause that peer to disconnect us. Requiring
3852  // that our chainwork exceed nMinimumChainWork is a protection against
3853  // being fed a bogus chain when we started up for the first time and
3854  // getting partitioned off the honest network for serving that chain to
3855  // others.
3856  if (m_chainman.ActiveTip() == nullptr ||
3857  (m_chainman.ActiveTip()->nChainWork < nMinimumChainWork && !pfrom.HasPermission(NetPermissionFlags::Download))) {
3858  LogPrint(BCLog::NET, "Ignoring getheaders from peer=%d because active chain has too little work; sending empty response\n", pfrom.GetId());
3859  // Just respond with an empty headers message, to tell the peer to
3860  // go away but not treat us as unresponsive.
3861  m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::HEADERS, std::vector<CBlock>()));
3862  return;
3863  }
3864 
3865  CNodeState *nodestate = State(pfrom.GetId());
3866  const CBlockIndex* pindex = nullptr;
3867  if (locator.IsNull())
3868  {
3869  // If locator is null, return the hashStop block
3870  pindex = m_chainman.m_blockman.LookupBlockIndex(hashStop);
3871  if (!pindex) {
3872  return;
3873  }
3874 
3875  if (!BlockRequestAllowed(pindex)) {
3876  LogPrint(BCLog::NET, "%s: ignoring request from peer=%i for old block header that isn't in the main chain\n", __func__, pfrom.GetId());
3877  return;
3878  }
3879  }
3880  else
3881  {
3882  // Find the last block the caller has in the main chain
3883  pindex = m_chainman.ActiveChainstate().FindForkInGlobalIndex(locator);
3884  if (pindex)
3885  pindex = m_chainman.ActiveChain().Next(pindex);
3886  }
3887 
3888  // we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
3889  std::vector<CBlock> vHeaders;
3890  int nLimit = MAX_HEADERS_RESULTS;
3891  LogPrint(BCLog::NET, "getheaders %d to %s from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), pfrom.GetId());
3892  for (; pindex; pindex = m_chainman.ActiveChain().Next(pindex))
3893  {
3894  vHeaders.push_back(pindex->GetBlockHeader());
3895  if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
3896  break;
3897  }
3898  // pindex can be nullptr either if we sent m_chainman.ActiveChain().Tip() OR
3899  // if our peer has m_chainman.ActiveChain().Tip() (and thus we are sending an empty
3900  // headers message). In both cases it's safe to update
3901  // pindexBestHeaderSent to be our tip.
3902  //
3903  // It is important that we simply reset the BestHeaderSent value here,
3904  // and not max(BestHeaderSent, newHeaderSent). We might have announced
3905  // the currently-being-connected tip using a compact block, which
3906  // resulted in the peer sending a headers request, which we respond to
3907  // without the new block. By resetting the BestHeaderSent, we ensure we
3908  // will re-announce the new block via headers (or compact blocks again)
3909  // in the SendMessages logic.
3910  nodestate->pindexBestHeaderSent = pindex ? pindex : m_chainman.ActiveChain().Tip();
3911  m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
3912  return;
3913  }
3914 
3915  if (msg_type == NetMsgType::TX) {
3916  if (RejectIncomingTxs(pfrom)) {
3917  LogPrint(BCLog::NET, "transaction sent in violation of protocol peer=%d\n", pfrom.GetId());
3918  pfrom.fDisconnect = true;
3919  return;
3920  }
3921 
3922  // Stop processing the transaction early if we are still in IBD since we don't
3923  // have enough information to validate it yet. Sending unsolicited transactions
3924  // is not considered a protocol violation, so don't punish the peer.
3925  if (m_chainman.ActiveChainstate().IsInitialBlockDownload()) return;
3926 
3927  CTransactionRef ptx;
3928  vRecv >> ptx;
3929  const CTransaction& tx = *ptx;
3930 
3931  const uint256& txid = ptx->GetHash();
3932  const uint256& wtxid = ptx->GetWitnessHash();
3933 
3934  const uint256& hash = peer->m_wtxid_relay ? wtxid : txid;
3935  AddKnownTx(*peer, hash);
3936  if (peer->m_wtxid_relay && txid != wtxid) {
3937  // Insert txid into m_tx_inventory_known_filter, even for
3938  // wtxidrelay peers. This prevents re-adding of
3939  // unconfirmed parents to the recently_announced
3940  // filter, when a child tx is requested. See
3941  // ProcessGetData().
3942  AddKnownTx(*peer, txid);
3943  }
3944 
3946 
3947  m_txrequest.ReceivedResponse(pfrom.GetId(), txid);
3948  if (tx.HasWitness()) m_txrequest.ReceivedResponse(pfrom.GetId(), wtxid);
3949 
3950  // We do the AlreadyHaveTx() check using wtxid, rather than txid - in the
3951  // absence of witness malleation, this is strictly better, because the
3952  // recent rejects filter may contain the wtxid but rarely contains
3953  // the txid of a segwit transaction that has been rejected.
3954  // In the presence of witness malleation, it's possible that by only
3955  // doing the check with wtxid, we could overlook a transaction which
3956  // was confirmed with a different witness, or exists in our mempool
3957  // with a different witness, but this has limited downside:
3958  // mempool validation does its own lookup of whether we have the txid
3959  // already; and an adversary can already relay us old transactions
3960  // (older than our recency filter) if trying to DoS us, without any need
3961  // for witness malleation.
3962  if (AlreadyHaveTx(GenTxid::Wtxid(wtxid))) {
3964  // Always relay transactions received from peers with forcerelay
3965  // permission, even if they were already in the mempool, allowing
3966  // the node to function as a gateway for nodes hidden behind it.
3967  if (!m_mempool.exists(GenTxid::Txid(tx.GetHash()))) {
3968  LogPrintf("Not relaying non-mempool transaction %s from forcerelay peer=%d\n", tx.GetHash().ToString(), pfrom.GetId());
3969  } else {
3970  LogPrintf("Force relaying tx %s from peer=%d\n", tx.GetHash().ToString(), pfrom.GetId());
3971  RelayTransaction(tx.GetHash(), tx.GetWitnessHash());
3972  }
3973  }
3974  return;
3975  }
3976 
3977  const MempoolAcceptResult result = m_chainman.ProcessTransaction(ptx);
3978  const TxValidationState& state = result.m_state;
3979 
3981  // As this version of the transaction was acceptable, we can forget about any
3982  // requests for it.
3983  m_txrequest.ForgetTxHash(tx.GetHash());
3984  m_txrequest.ForgetTxHash(tx.GetWitnessHash());
3985  RelayTransaction(tx.GetHash(), tx.GetWitnessHash());
3986  m_orphanage.AddChildrenToWorkSet(tx, peer->m_orphan_work_set);
3987 
3988  pfrom.m_last_tx_time = GetTime<std::chrono::seconds>();
3989 
3990  LogPrint(BCLog::MEMPOOL, "AcceptToMemoryPool: peer=%d: accepted %s (poolsz %u txn, %u kB)\n",
3991  pfrom.GetId(),
3992  tx.GetHash().ToString(),
3993  m_mempool.size(), m_mempool.DynamicMemoryUsage() / 1000);
3994 
3995  for (const CTransactionRef& removedTx : result.m_replaced_transactions.value()) {
3996  AddToCompactExtraTransactions(removedTx);
3997  }
3998 
3999  // Recursively process any orphan transactions that depended on this one
4000  ProcessOrphanTx(peer->m_orphan_work_set);
4001  }
4003  {
4004  bool fRejectedParents = false; // It may be the case that the orphans parents have all been rejected
4005 
4006  // Deduplicate parent txids, so that we don't have to loop over
4007  // the same parent txid more than once down below.
4008  std::vector<uint256> unique_parents;
4009  unique_parents.reserve(tx.vin.size());
4010  for (const CTxIn& txin : tx.vin) {
4011  // We start with all parents, and then remove duplicates below.
4012  unique_parents.push_back(txin.prevout.hash);
4013  }
4014  std::sort(unique_parents.begin(), unique_parents.end());
4015  unique_parents.erase(std::unique(unique_parents.begin(), unique_parents.end()), unique_parents.end());
4016  for (const uint256& parent_txid : unique_parents) {
4017  if (m_recent_rejects.contains(parent_txid)) {
4018  fRejectedParents = true;
4019  break;
4020  }
4021  }
4022  if (!fRejectedParents) {
4023  const auto current_time{GetTime<std::chrono::microseconds>()};
4024 
4025  for (const uint256& parent_txid : unique_parents) {
4026  // Here, we only have the txid (and not wtxid) of the
4027  // inputs, so we only request in txid mode, even for
4028  // wtxidrelay peers.
4029  // Eventually we should replace this with an improved
4030  // protocol for getting all unconfirmed parents.
4031  const auto gtxid{GenTxid::Txid(parent_txid)};
4032  AddKnownTx(*peer, parent_txid);
4033  if (!AlreadyHaveTx(gtxid)) AddTxAnnouncement(pfrom, gtxid, current_time);
4034  }
4035 
4036  if (m_orphanage.AddTx(ptx, pfrom.GetId())) {
4037  AddToCompactExtraTransactions(ptx);
4038  }
4039 
4040  // Once added to the orphan pool, a tx is considered AlreadyHave, and we shouldn't request it anymore.
4041  m_txrequest.ForgetTxHash(tx.GetHash());
4042  m_txrequest.ForgetTxHash(tx.GetWitnessHash());
4043 
4044  // DoS prevention: do not allow m_orphanage to grow unbounded (see CVE-2012-3789)
4045  unsigned int nMaxOrphanTx = (unsigned int)std::max((int64_t)0, gArgs.GetIntArg("-maxorphantx", DEFAULT_MAX_ORPHAN_TRANSACTIONS));
4046  m_orphanage.LimitOrphans(nMaxOrphanTx);
4047  } else {
4048  LogPrint(BCLog::MEMPOOL, "not keeping orphan with rejected parents %s\n",tx.GetHash().ToString());
4049  // We will continue to reject this tx since it has rejected
4050  // parents so avoid re-requesting it from other peers.
4051  // Here we add both the txid and the wtxid, as we know that
4052  // regardless of what witness is provided, we will not accept
4053  // this, so we don't need to allow for redownload of this txid
4054  // from any of our non-wtxidrelay peers.
4055  m_recent_rejects.insert(tx.GetHash());
4056  m_recent_rejects.insert(tx.GetWitnessHash());
4057  m_txrequest.ForgetTxHash(tx.GetHash());
4058  m_txrequest.ForgetTxHash(tx.GetWitnessHash());
4059  }
4060  } else {
4062  // We can add the wtxid of this transaction to our reject filter.
4063  // Do not add txids of witness transactions or witness-stripped
4064  // transactions to the filter, as they can have been malleated;
4065  // adding such txids to the reject filter would potentially
4066  // interfere with relay of valid transactions from peers that
4067  // do not support wtxid-based relay. See
4068  // https://github.com/bitcoin/bitcoin/issues/8279 for details.
4069  // We can remove this restriction (and always add wtxids to
4070  // the filter even for witness stripped transactions) once
4071  // wtxid-based relay is broadly deployed.
4072  // See also comments in https://github.com/bitcoin/bitcoin/pull/18044#discussion_r443419034
4073  // for concerns around weakening security of unupgraded nodes
4074  // if we start doing this too early.
4075  m_recent_rejects.insert(tx.GetWitnessHash());
4076  m_txrequest.ForgetTxHash(tx.GetWitnessHash());
4077  // If the transaction failed for TX_INPUTS_NOT_STANDARD,
4078  // then we know that the witness was irrelevant to the policy
4079  // failure, since this check depends only on the txid
4080  // (the scriptPubKey being spent is covered by the txid).
4081  // Add the txid to the reject filter to prevent repeated
4082  // processing of this transaction in the event that child
4083  // transactions are later received (resulting in
4084  // parent-fetching by txid via the orphan-handling logic).
4086  m_recent_rejects.insert(tx.GetHash());
4087  m_txrequest.ForgetTxHash(tx.GetHash());
4088  }
4089  if (RecursiveDynamicUsage(*ptx) < 100000) {
4090  AddToCompactExtraTransactions(ptx);
4091  }
4092  }
4093  }
4094 
4095  // If a tx has been detected by m_recent_rejects, we will have reached
4096  // this point and the tx will have been ignored. Because we haven't
4097  // submitted the tx to our mempool, we won't have computed a DoS
4098  // score for it or determined exactly why we consider it invalid.
4099  //
4100  // This means we won't penalize any peer subsequently relaying a DoSy
4101  // tx (even if we penalized the first peer who gave it to us) because
4102  // we have to account for m_recent_rejects showing false positives. In
4103  // other words, we shouldn't penalize a peer if we aren't *sure* they
4104  // submitted a DoSy tx.
4105  //
4106  // Note that m_recent_rejects doesn't just record DoSy or invalid
4107  // transactions, but any tx not accepted by the mempool, which may be
4108  // due to node policy (vs. consensus). So we can't blanket penalize a
4109  // peer simply for relaying a tx that our m_recent_rejects has caught,
4110  // regardless of false positives.
4111 
4112  if (state.IsInvalid()) {
4113  LogPrint(BCLog::MEMPOOLREJ, "%s from peer=%d was not accepted: %s\n", tx.GetHash().ToString(),
4114  pfrom.GetId(),
4115  state.ToString());
4116  MaybePunishNodeForTx(pfrom.GetId(), state);
4117  }
4118  return;
4119  }
4120 
4121  if (msg_type == NetMsgType::CMPCTBLOCK)
4122  {
4123  // Ignore cmpctblock received while importing
4124  if (fImporting || fReindex) {
4125  LogPrint(BCLog::NET, "Unexpected cmpctblock message received from peer %d\n", pfrom.GetId());
4126  return;
4127  }
4128 
4129  CBlockHeaderAndShortTxIDs cmpctblock;
4130  vRecv >> cmpctblock;
4131 
4132  bool received_new_header = false;
4133 
4134  {
4135  LOCK(cs_main);
4136 
4137  const CBlockIndex* prev_block = m_chainman.m_blockman.LookupBlockIndex(cmpctblock.header.hashPrevBlock);
4138  if (!prev_block) {
4139  // Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
4140  if (!m_chainman.ActiveChainstate().IsInitialBlockDownload()) {
4141  MaybeSendGetHeaders(pfrom, GetLocator(m_chainman.m_best_header), *peer);
4142  }
4143  return;
4144  } else if (prev_block->nChainWork + CalculateHeadersWork({cmpctblock.header}) < GetAntiDoSWorkThreshold()) {
4145  // If we get a low-work header in a compact block, we can ignore it.
4146  LogPrint(BCLog::NET, "Ignoring low-work compact block from peer %d\n", pfrom.GetId());
4147  return;
4148  }
4149 
4150  if (!m_chainman.m_blockman.LookupBlockIndex(cmpctblock.header.GetHash())) {
4151  received_new_header = true;
4152  }
4153  }
4154 
4155  const CBlockIndex *pindex = nullptr;
4156  BlockValidationState state;
4157  if (!m_chainman.ProcessNewBlockHeaders({cmpctblock.header}, /*min_pow_checked=*/true, state, &pindex)) {
4158  if (state.IsInvalid()) {
4159  MaybePunishNodeForBlock(pfrom.GetId(), state, /*via_compact_block=*/true, "invalid header via cmpctblock");
4160  return;
4161  }
4162  }
4163 
4164  // When we succeed in decoding a block's txids from a cmpctblock
4165  // message we typically jump to the BLOCKTXN handling code, with a
4166  // dummy (empty) BLOCKTXN message, to re-use the logic there in
4167  // completing processing of the putative block (without cs_main).
4168  bool fProcessBLOCKTXN = false;
4170 
4171  // If we end up treating this as a plain headers message, call that as well
4172  // without cs_main.
4173  bool fRevertToHeaderProcessing = false;
4174 
4175  // Keep a CBlock for "optimistic" compactblock reconstructions (see
4176  // below)
4177  std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
4178  bool fBlockReconstructed = false;
4179 
4180  {
4182  // If AcceptBlockHeader returned true, it set pindex
4183  assert(pindex);
4184  UpdateBlockAvailability(pfrom.GetId(), pindex->GetBlockHash());
4185 
4186  CNodeState *nodestate = State(pfrom.GetId());
4187 
4188  // If this was a new header with more work than our tip, update the
4189  // peer's last block announcement time
4190  if (received_new_header && pindex->nChainWork > m_chainman.ActiveChain().Tip()->nChainWork) {
4191  nodestate->m_last_block_announcement = GetTime();
4192  }
4193 
4194  std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator blockInFlightIt = mapBlocksInFlight.find(pindex->GetBlockHash());
4195  bool fAlreadyInFlight = blockInFlightIt != mapBlocksInFlight.end();
4196 
4197  if (pindex->nStatus & BLOCK_HAVE_DATA) // Nothing to do here
4198  return;
4199 
4200  if (pindex->nChainWork <= m_chainman.ActiveChain().Tip()->nChainWork || // We know something better
4201  pindex->nTx != 0) { // We had this block at some point, but pruned it
4202  if (fAlreadyInFlight) {
4203  // We requested this block for some reason, but our mempool will probably be useless
4204  // so we just grab the block via normal getdata
4205  std::vector<CInv> vInv(1);
4206  vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(*peer), cmpctblock.header.GetHash());
4207  m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
4208  }
4209  return;
4210  }
4211 
4212  // If we're not close to tip yet, give up and let parallel block fetch work its magic
4213  if (!fAlreadyInFlight && !CanDirectFetch()) {
4214  return;
4215  }
4216 
4217  // We want to be a bit conservative just to be extra careful about DoS
4218  // possibilities in compact block processing...
4219  if (pindex->nHeight <= m_chainman.ActiveChain().Height() + 2) {
4220  if ((!fAlreadyInFlight && nodestate->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) ||
4221  (fAlreadyInFlight && blockInFlightIt->second.first == pfrom.GetId())) {
4222  std::list<QueuedBlock>::iterator* queuedBlockIt = nullptr;
4223  if (!BlockRequested(pfrom.GetId(), *pindex, &queuedBlockIt)) {
4224  if (!(*queuedBlockIt)->partialBlock)
4225  (*queuedBlockIt)->partialBlock.reset(new PartiallyDownloadedBlock(&m_mempool));
4226  else {
4227  // The block was already in flight using compact blocks from the same peer
4228  LogPrint(BCLog::NET, "Peer sent us compact block we were already syncing!\n");
4229  return;
4230  }
4231  }
4232 
4233  PartiallyDownloadedBlock& partialBlock = *(*queuedBlockIt)->partialBlock;
4234  ReadStatus status = partialBlock.InitData(cmpctblock, vExtraTxnForCompact);
4235  if (status == READ_STATUS_INVALID) {
4236  RemoveBlockRequest(pindex->GetBlockHash(), pfrom.GetId()); // Reset in-flight state in case Misbehaving does not result in a disconnect
4237  Misbehaving(*peer, 100, "invalid compact block");
4238  return;
4239  } else if (status == READ_STATUS_FAILED) {
4240  // Duplicate txindexes, the block is now in-flight, so just request it
4241  std::vector<CInv> vInv(1);
4242  vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(*peer), cmpctblock.header.GetHash());
4243  m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
4244  return;
4245  }
4246 
4248  for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
4249  if (!partialBlock.IsTxAvailable(i))
4250  req.indexes.push_back(i);
4251  }
4252  if (req.indexes.empty()) {
4253  // Dirty hack to jump to BLOCKTXN code (TODO: move message handling into their own functions)
4254  BlockTransactions txn;
4255  txn.blockhash = cmpctblock.header.GetHash();
4256  blockTxnMsg << txn;
4257  fProcessBLOCKTXN = true;
4258  } else {
4259  req.blockhash = pindex->GetBlockHash();
4260  m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETBLOCKTXN, req));
4261  }
4262  } else {
4263  // This block is either already in flight from a different
4264  // peer, or this peer has too many blocks outstanding to
4265  // download from.
4266  // Optimistically try to reconstruct anyway since we might be
4267  // able to without any round trips.
4268  PartiallyDownloadedBlock tempBlock(&m_mempool);
4269  ReadStatus status = tempBlock.InitData(cmpctblock, vExtraTxnForCompact);
4270  if (status != READ_STATUS_OK) {
4271  // TODO: don't ignore failures
4272  return;
4273  }
4274  std::vector<CTransactionRef> dummy;
4275  status = tempBlock.FillBlock(*pblock, dummy);
4276  if (status == READ_STATUS_OK) {
4277  fBlockReconstructed = true;
4278  }
4279  }
4280  } else {
4281  if (fAlreadyInFlight) {
4282  // We requested this block, but its far into the future, so our
4283  // mempool will probably be useless - request the block normally
4284  std::vector<CInv> vInv(1);
4285  vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(*peer), cmpctblock.header.GetHash());
4286  m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
4287  return;
4288  } else {
4289  // If this was an announce-cmpctblock, we want the same treatment as a header message
4290  fRevertToHeaderProcessing = true;
4291  }
4292  }
4293  } // cs_main
4294 
4295  if (fProcessBLOCKTXN) {
4296  return ProcessMessage(pfrom, NetMsgType::BLOCKTXN, blockTxnMsg, time_received, interruptMsgProc);
4297  }
4298 
4299  if (fRevertToHeaderProcessing) {
4300  // Headers received from HB compact block peers are permitted to be
4301  // relayed before full validation (see BIP 152), so we don't want to disconnect
4302  // the peer if the header turns out to be for an invalid block.
4303  // Note that if a peer tries to build on an invalid chain, that
4304  // will be detected and the peer will be disconnected/discouraged.
4305  return ProcessHeadersMessage(pfrom, *peer, {cmpctblock.header}, /*via_compact_block=*/true);
4306  }
4307 
4308  if (fBlockReconstructed) {
4309  // If we got here, we were able to optimistically reconstruct a
4310  // block that is in flight from some other peer.
4311  {
4312  LOCK(cs_main);
4313  mapBlockSource.emplace(pblock->GetHash(), std::make_pair(pfrom.GetId(), false));
4314  }
4315  // Setting force_processing to true means that we bypass some of
4316  // our anti-DoS protections in AcceptBlock, which filters
4317  // unrequested blocks that might be trying to waste our resources
4318  // (eg disk space). Because we only try to reconstruct blocks when
4319  // we're close to caught up (via the CanDirectFetch() requirement
4320  // above, combined with the behavior of not requesting blocks until
4321  // we have a chain with at least nMinimumChainWork), and we ignore
4322  // compact blocks with less work than our tip, it is safe to treat
4323  // reconstructed compact blocks as having been requested.
4324  ProcessBlock(pfrom, pblock, /*force_processing=*/true, /*min_pow_checked=*/true);
4325  LOCK(cs_main); // hold cs_main for CBlockIndex::IsValid()
4326  if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS)) {
4327  // Clear download state for this block, which is in
4328  // process from some other peer. We do this after calling
4329  // ProcessNewBlock so that a malleated cmpctblock announcement
4330  // can't be used to interfere with block relay.
4331  RemoveBlockRequest(pblock->GetHash(), std::nullopt);
4332  }
4333  }
4334  return;
4335  }
4336 
4337  if (msg_type == NetMsgType::BLOCKTXN)
4338  {
4339  // Ignore blocktxn received while importing
4340  if (fImporting || fReindex) {
4341  LogPrint(BCLog::NET, "Unexpected blocktxn message received from peer %d\n", pfrom.GetId());
4342  return;
4343  }
4344 
4345  BlockTransactions resp;
4346  vRecv >> resp;
4347 
4348  std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
4349  bool fBlockRead = false;
4350  {
4351  LOCK(cs_main);
4352 
4353  std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator it = mapBlocksInFlight.find(resp.blockhash);
4354  if (it == mapBlocksInFlight.end() || !it->second.second->partialBlock ||
4355  it->second.first != pfrom.GetId()) {
4356  LogPrint(BCLog::NET, "Peer %d sent us block transactions for block we weren't expecting\n", pfrom.GetId());
4357  return;
4358  }
4359 
4360  PartiallyDownloadedBlock& partialBlock = *it->second.second->partialBlock;
4361  ReadStatus status = partialBlock.FillBlock(*pblock, resp.txn);
4362  if (status == READ_STATUS_INVALID) {
4363  RemoveBlockRequest(resp.blockhash, pfrom.GetId()); // Reset in-flight state in case Misbehaving does not result in a disconnect
4364  Misbehaving(*peer, 100, "invalid compact block/non-matching block transactions");
4365  return;
4366  } else if (status == READ_STATUS_FAILED) {
4367  // Might have collided, fall back to getdata now :(
4368  std::vector<CInv> invs;
4369  invs.push_back(CInv(MSG_BLOCK | GetFetchFlags(*peer), resp.blockhash));
4370  m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, invs));
4371  } else {
4372  // Block is either okay, or possibly we received
4373  // READ_STATUS_CHECKBLOCK_FAILED.
4374  // Note that CheckBlock can only fail for one of a few reasons:
4375  // 1. bad-proof-of-work (impossible here, because we've already
4376  // accepted the header)
4377  // 2. merkleroot doesn't match the transactions given (already
4378  // caught in FillBlock with READ_STATUS_FAILED, so
4379  // impossible here)
4380  // 3. the block is otherwise invalid (eg invalid coinbase,
4381  // block is too big, too many legacy sigops, etc).
4382  // So if CheckBlock failed, #3 is the only possibility.
4383  // Under BIP 152, we don't discourage the peer unless proof of work is
4384  // invalid (we don't require all the stateless checks to have
4385  // been run). This is handled below, so just treat this as
4386  // though the block was successfully read, and rely on the
4387  // handling in ProcessNewBlock to ensure the block index is
4388  // updated, etc.
4389  RemoveBlockRequest(resp.blockhash, pfrom.GetId()); // it is now an empty pointer
4390  fBlockRead = true;
4391  // mapBlockSource is used for potentially punishing peers and
4392  // updating which peers send us compact blocks, so the race
4393  // between here and cs_main in ProcessNewBlock is fine.
4394  // BIP 152 permits peers to relay compact blocks after validating
4395  // the header only; we should not punish peers if the block turns
4396  // out to be invalid.
4397  mapBlockSource.emplace(resp.blockhash, std::make_pair(pfrom.GetId(), false));
4398  }
4399  } // Don't hold cs_main when we call into ProcessNewBlock
4400  if (fBlockRead) {
4401  // Since we requested this block (it was in mapBlocksInFlight), force it to be processed,
4402  // even if it would not be a candidate for new tip (missing previous block, chain not long enough, etc)
4403  // This bypasses some anti-DoS logic in AcceptBlock (eg to prevent
4404  // disk-space attacks), but this should be safe due to the
4405  // protections in the compact block handler -- see related comment
4406  // in compact block optimistic reconstruction handling.
4407  ProcessBlock(pfrom, pblock, /*force_processing=*/true, /*min_pow_checked=*/true);
4408  }
4409  return;
4410  }
4411 
4412  if (msg_type == NetMsgType::HEADERS)
4413  {
4414  // Ignore headers received while importing
4415  if (fImporting || fReindex) {
4416  LogPrint(BCLog::NET, "Unexpected headers message received from peer %d\n", pfrom.GetId());
4417  return;
4418  }
4419 
4420  // Assume that this is in response to any outstanding getheaders
4421  // request we may have sent, and clear out the time of our last request
4422  peer->m_last_getheaders_timestamp = {};
4423 
4424  std::vector<CBlockHeader> headers;
4425 
4426  // Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks.
4427  unsigned int nCount = ReadCompactSize(vRecv);
4428  if (nCount > MAX_HEADERS_RESULTS) {
4429  Misbehaving(*peer, 20, strprintf("headers message size = %u", nCount));
4430  return;
4431  }
4432  headers.resize(nCount);
4433  for (unsigned int n = 0; n < nCount; n++) {
4434  vRecv >> headers[n];
4435  ReadCompactSize(vRecv); // ignore tx count; assume it is 0.
4436  }
4437 
4438  ProcessHeadersMessage(pfrom, *peer, std::move(headers), /*via_compact_block=*/false);
4439 
4440  // Check if the headers presync progress needs to be reported to validation.
4441  // This needs to be done without holding the m_headers_presync_mutex lock.
4442  if (m_headers_presync_should_signal.exchange(false)) {
4443  HeadersPresyncStats stats;
4444  {
4445  LOCK(m_headers_presync_mutex);
4446  auto it = m_headers_presync_stats.find(m_headers_presync_bestpeer);
4447  if (it != m_headers_presync_stats.end()) stats = it->second;
4448  }
4449  if (stats.second) {
4450  m_chainman.ReportHeadersPresync(stats.first, stats.second->first, stats.second->second);
4451  }
4452  }
4453 
4454  return;
4455  }
4456 
4457  if (msg_type == NetMsgType::BLOCK)
4458  {
4459  // Ignore block received while importing
4460  if (fImporting || fReindex) {
4461  LogPrint(BCLog::NET, "Unexpected block message received from peer %d\n", pfrom.GetId());
4462  return;
4463  }
4464 
4465  std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
4466  vRecv >> *pblock;
4467 
4468  LogPrint(BCLog::NET, "received block %s peer=%d\n", pblock->GetHash().ToString(), pfrom.GetId());
4469 
4470  bool forceProcessing = false;
4471  const uint256 hash(pblock->GetHash());
4472  bool min_pow_checked = false;
4473  {
4474  LOCK(cs_main);
4475  // Always process the block if we requested it, since we may
4476  // need it even when it's not a candidate for a new best tip.
4477  forceProcessing = IsBlockRequested(hash);
4478  RemoveBlockRequest(hash, pfrom.GetId());
4479  // mapBlockSource is only used for punishing peers and setting
4480  // which peers send us compact blocks, so the race between here and
4481  // cs_main in ProcessNewBlock is fine.
4482  mapBlockSource.emplace(hash, std::make_pair(pfrom.GetId(), true));
4483 
4484  // Check work on this block against our anti-dos thresholds.
4485  const CBlockIndex* prev_block = m_chainman.m_blockman.LookupBlockIndex(pblock->hashPrevBlock);
4486  if (prev_block && prev_block->nChainWork + CalculateHeadersWork({pblock->GetBlockHeader()}) >= GetAntiDoSWorkThreshold()) {
4487  min_pow_checked = true;
4488  }
4489  }
4490  ProcessBlock(pfrom, pblock, forceProcessing, min_pow_checked);
4491  return;
4492  }
4493 
4494  if (msg_type == NetMsgType::GETADDR) {
4495  // This asymmetric behavior for inbound and outbound connections was introduced
4496  // to prevent a fingerprinting attack: an attacker can send specific fake addresses
4497  // to users' AddrMan and later request them by sending getaddr messages.
4498  // Making nodes which are behind NAT and can only make outgoing connections ignore
4499  // the getaddr message mitigates the attack.
4500  if (!pfrom.IsInboundConn()) {
4501  LogPrint(BCLog::NET, "Ignoring \"getaddr\" from %s connection. peer=%d\n", pfrom.ConnectionTypeAsString(), pfrom.GetId());
4502  return;
4503  }
4504 
4505  // Since this must be an inbound connection, SetupAddressRelay will
4506  // never fail.
4507  Assume(SetupAddressRelay(pfrom, *peer));
4508 
4509  // Only send one GetAddr response per connection to reduce resource waste
4510  // and discourage addr stamping of INV announcements.
4511  if (peer->m_getaddr_recvd) {
4512  LogPrint(BCLog::NET, "Ignoring repeated \"getaddr\". peer=%d\n", pfrom.GetId());
4513  return;
4514  }
4515  peer->m_getaddr_recvd = true;
4516 
4517  peer->m_addrs_to_send.clear();
4518  std::vector<CAddress> vAddr;
4520  vAddr = m_connman.GetAddresses(MAX_ADDR_TO_SEND, MAX_PCT_ADDR_TO_SEND, /*network=*/std::nullopt);
4521  } else {
4522  vAddr = m_connman.GetAddresses(pfrom, MAX_ADDR_TO_SEND, MAX_PCT_ADDR_TO_SEND);
4523  }
4524  FastRandomContext insecure_rand;
4525  for (const CAddress &addr : vAddr) {
4526  PushAddress(*peer, addr, insecure_rand);
4527  }
4528  return;
4529  }
4530 
4531  if (msg_type == NetMsgType::MEMPOOL) {
4532  if (!(peer->m_our_services & NODE_BLOOM) && !pfrom.HasPermission(NetPermissionFlags::Mempool))
4533  {
4535  {
4536  LogPrint(BCLog::NET, "mempool request with bloom filters disabled, disconnect peer=%d\n", pfrom.GetId());
4537  pfrom.fDisconnect = true;
4538  }
4539  return;
4540  }
4541 
4542  if (m_connman.OutboundTargetReached(false) && !pfrom.HasPermission(NetPermissionFlags::Mempool))
4543  {
4545  {
4546  LogPrint(BCLog::NET, "mempool request with bandwidth limit reached, disconnect peer=%d\n", pfrom.GetId());
4547  pfrom.fDisconnect = true;
4548  }
4549  return;
4550  }
4551 
4552  if (auto tx_relay = peer->GetTxRelay(); tx_relay != nullptr) {
4553  LOCK(tx_relay->m_tx_inventory_mutex);
4554  tx_relay->m_send_mempool = true;
4555  }
4556  return;
4557  }
4558 
4559  if (msg_type == NetMsgType::PING) {
4560  if (pfrom.GetCommonVersion() > BIP0031_VERSION) {
4561  uint64_t nonce = 0;
4562  vRecv >> nonce;
4563  // Echo the message back with the nonce. This allows for two useful features:
4564  //
4565  // 1) A remote node can quickly check if the connection is operational
4566  // 2) Remote nodes can measure the latency of the network thread. If this node
4567  // is overloaded it won't respond to pings quickly and the remote node can
4568  // avoid sending us more work, like chain download requests.
4569  //
4570  // The nonce stops the remote getting confused between different pings: without
4571  // it, if the remote node sends a ping once per second and this node takes 5
4572  // seconds to respond to each, the 5th ping the remote sends would appear to
4573  // return very quickly.
4574  m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::PONG, nonce));
4575  }
4576  return;
4577  }
4578 
4579  if (msg_type == NetMsgType::PONG) {
4580  const auto ping_end = time_received;
4581  uint64_t nonce = 0;
4582  size_t nAvail = vRecv.in_avail();
4583  bool bPingFinished = false;
4584  std::string sProblem;
4585 
4586  if (nAvail >= sizeof(nonce)) {
4587  vRecv >> nonce;
4588 
4589  // Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
4590  if (peer->m_ping_nonce_sent != 0) {
4591  if (nonce == peer->m_ping_nonce_sent) {
4592  // Matching pong received, this ping is no longer outstanding
4593  bPingFinished = true;
4594  const auto ping_time = ping_end - peer->m_ping_start.load();
4595  if (ping_time.count() >= 0) {
4596  // Let connman know about this successful ping-pong
4597  pfrom.PongReceived(ping_time);
4598  } else {
4599  // This should never happen
4600  sProblem = "Timing mishap";
4601  }
4602  } else {
4603  // Nonce mismatches are normal when pings are overlapping
4604  sProblem = "Nonce mismatch";
4605  if (nonce == 0) {
4606  // This is most likely a bug in another implementation somewhere; cancel this ping
4607  bPingFinished = true;
4608  sProblem = "Nonce zero";
4609  }
4610  }
4611  } else {
4612  sProblem = "Unsolicited pong without ping";
4613  }
4614  } else {
4615  // This is most likely a bug in another implementation somewhere; cancel this ping
4616  bPingFinished = true;
4617  sProblem = "Short payload";
4618  }
4619 
4620  if (!(sProblem.empty())) {
4621  LogPrint(BCLog::NET, "pong peer=%d: %s, %x expected, %x received, %u bytes\n",
4622  pfrom.GetId(),
4623  sProblem,
4624  peer->m_ping_nonce_sent,
4625  nonce,
4626  nAvail);
4627  }
4628  if (bPingFinished) {
4629  peer->m_ping_nonce_sent = 0;
4630  }
4631  return;
4632  }
4633 
4634  if (msg_type == NetMsgType::FILTERLOAD) {
4635  if (!(peer->m_our_services & NODE_BLOOM)) {
4636  LogPrint(BCLog::NET, "filterload received despite not offering bloom services from peer=%d; disconnecting\n", pfrom.GetId());
4637  pfrom.fDisconnect = true;
4638  return;
4639  }
4640  CBloomFilter filter;
4641  vRecv >> filter;
4642 
4643  if (!filter.IsWithinSizeConstraints())
4644  {
4645  // There is no excuse for sending a too-large filter
4646  Misbehaving(*peer, 100, "too-large bloom filter");
4647  } else if (auto tx_relay = peer->GetTxRelay(); tx_relay != nullptr) {
4648  {
4649  LOCK(tx_relay->m_bloom_filter_mutex);
4650  tx_relay->m_bloom_filter.reset(new CBloomFilter(filter));
4651  tx_relay->m_relay_txs = true;
4652  }
4653  pfrom.m_bloom_filter_loaded = true;
4654  pfrom.m_relays_txs = true;
4655  }
4656  return;
4657  }
4658 
4659  if (msg_type == NetMsgType::FILTERADD) {
4660  if (!(peer->m_our_services & NODE_BLOOM)) {
4661  LogPrint(BCLog::NET, "filteradd received despite not offering bloom services from peer=%d; disconnecting\n", pfrom.GetId());
4662  pfrom.fDisconnect = true;
4663  return;
4664  }
4665  std::vector<unsigned char> vData;
4666  vRecv >> vData;
4667 
4668  // Nodes must NEVER send a data item > 520 bytes (the max size for a script data object,
4669  // and thus, the maximum size any matched object can have) in a filteradd message
4670  bool bad = false;
4671  if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE) {
4672  bad = true;
4673  } else if (auto tx_relay = peer->GetTxRelay(); tx_relay != nullptr) {
4674  LOCK(tx_relay->m_bloom_filter_mutex);
4675  if (tx_relay->m_bloom_filter) {
4676  tx_relay->m_bloom_filter->insert(vData);
4677  } else {
4678  bad = true;
4679  }
4680  }
4681  if (bad) {
4682  Misbehaving(*peer, 100, "bad filteradd message");
4683  }
4684  return;
4685  }
4686 
4687  if (msg_type == NetMsgType::FILTERCLEAR) {
4688  if (!(peer->m_our_services & NODE_BLOOM)) {
4689  LogPrint(BCLog::NET, "filterclear received despite not offering bloom services from peer=%d; disconnecting\n", pfrom.GetId());
4690  pfrom.fDisconnect = true;
4691  return;
4692  }
4693  auto tx_relay = peer->GetTxRelay();
4694  if (!tx_relay) return;
4695 
4696  {
4697  LOCK(tx_relay->m_bloom_filter_mutex);
4698  tx_relay->m_bloom_filter = nullptr;
4699  tx_relay->m_relay_txs = true;
4700  }
4701  pfrom.m_bloom_filter_loaded = false;
4702  pfrom.m_relays_txs = true;
4703  return;
4704  }
4705 
4706  if (msg_type == NetMsgType::FEEFILTER) {
4707  CAmount newFeeFilter = 0;
4708  vRecv >> newFeeFilter;
4709  if (MoneyRange(newFeeFilter)) {
4710  if (auto tx_relay = peer->GetTxRelay(); tx_relay != nullptr) {
4711  tx_relay->m_fee_filter_received = newFeeFilter;
4712  }
4713  LogPrint(BCLog::NET, "received: feefilter of %s from peer=%d\n", CFeeRate(newFeeFilter).ToString(), pfrom.GetId());
4714  }
4715  return;
4716  }
4717 
4718  if (msg_type == NetMsgType::GETCFILTERS) {
4719  ProcessGetCFilters(pfrom, *peer, vRecv);
4720  return;
4721  }
4722 
4723  if (msg_type == NetMsgType::GETCFHEADERS) {
4724  ProcessGetCFHeaders(pfrom, *peer, vRecv);
4725  return;
4726  }
4727 
4728  if (msg_type == NetMsgType::GETCFCHECKPT) {
4729  ProcessGetCFCheckPt(pfrom, *peer, vRecv);
4730  return;
4731  }
4732 
4733  if (msg_type == NetMsgType::NOTFOUND) {
4734  std::vector<CInv> vInv;
4735  vRecv >> vInv;
4737  LOCK(::cs_main);
4738  for (CInv &inv : vInv) {
4739  if (inv.IsGenTxMsg()) {
4740  // If we receive a NOTFOUND message for a tx we requested, mark the announcement for it as
4741  // completed in TxRequestTracker.
4742  m_txrequest.ReceivedResponse(pfrom.GetId(), inv.hash);
4743  }
4744  }
4745  }
4746  return;
4747  }
4748 
4749  // Ignore unknown commands for extensibility
4750  LogPrint(BCLog::NET, "Unknown command \"%s\" from peer=%d\n", SanitizeString(msg_type), pfrom.GetId());
4751  return;
4752 }
4753 
4754 bool PeerManagerImpl::MaybeDiscourageAndDisconnect(CNode& pnode, Peer& peer)
4755 {
4756  {
4757  LOCK(peer.m_misbehavior_mutex);
4758 
4759  // There's nothing to do if the m_should_discourage flag isn't set
4760  if (!peer.m_should_discourage) return false;
4761 
4762  peer.m_should_discourage = false;
4763  } // peer.m_misbehavior_mutex
4764 
4766  // We never disconnect or discourage peers for bad behavior if they have NetPermissionFlags::NoBan permission
4767  LogPrintf("Warning: not punishing noban peer %d!\n", peer.m_id);
4768  return false;
4769  }
4770 
4771  if (pnode.IsManualConn()) {
4772  // We never disconnect or discourage manual peers for bad behavior
4773  LogPrintf("Warning: not punishing manually connected peer %d!\n", peer.m_id);
4774  return false;
4775  }
4776 
4777  if (pnode.addr.IsLocal()) {
4778  // We disconnect local peers for bad behavior but don't discourage (since that would discourage
4779  // all peers on the same local address)
4780  LogPrint(BCLog::NET, "Warning: disconnecting but not discouraging %s peer %d!\n",
4781  pnode.m_inbound_onion ? "inbound onion" : "local", peer.m_id);
4782  pnode.fDisconnect = true;
4783  return true;
4784  }
4785 
4786  // Normal case: Disconnect the peer and discourage all nodes sharing the address
4787  LogPrint(BCLog::NET, "Disconnecting and discouraging peer %d!\n", peer.m_id);
4788  if (m_banman) m_banman->Discourage(pnode.addr);
4789  m_connman.DisconnectNode(pnode.addr);
4790  return true;
4791 }
4792 
4793 bool PeerManagerImpl::ProcessMessages(CNode* pfrom, std::atomic<bool>& interruptMsgProc)
4794 {
4795  bool fMoreWork = false;
4796 
4797  PeerRef peer = GetPeerRef(pfrom->GetId());
4798  if (peer == nullptr) return false;
4799 
4800  {
4801  LOCK(peer->m_getdata_requests_mutex);
4802  if (!peer->m_getdata_requests.empty()) {
4803  ProcessGetData(*pfrom, *peer, interruptMsgProc);
4804  }
4805  }
4806 
4807  {
4809  if (!peer->m_orphan_work_set.empty()) {
4810  ProcessOrphanTx(peer->m_orphan_work_set);
4811  }
4812  }
4813 
4814  if (pfrom->fDisconnect)
4815  return false;
4816 
4817  // this maintains the order of responses
4818  // and prevents m_getdata_requests to grow unbounded
4819  {
4820  LOCK(peer->m_getdata_requests_mutex);
4821  if (!peer->m_getdata_requests.empty()) return true;
4822  }
4823 
4824  {
4825  LOCK(g_cs_orphans);
4826  if (!peer->m_orphan_work_set.empty()) return true;
4827  }
4828 
4829  // Don't bother if send buffer is too full to respond anyway
4830  if (pfrom->fPauseSend) return false;
4831 
4832  std::list<CNetMessage> msgs;
4833  {
4834  LOCK(pfrom->cs_vProcessMsg);
4835  if (pfrom->vProcessMsg.empty()) return false;
4836  // Just take one message
4837  msgs.splice(msgs.begin(), pfrom->vProcessMsg, pfrom->vProcessMsg.begin());
4838  pfrom->nProcessQueueSize -= msgs.front().m_raw_message_size;
4839  pfrom->fPauseRecv = pfrom->nProcessQueueSize > m_connman.GetReceiveFloodSize();
4840  fMoreWork = !pfrom->vProcessMsg.empty();
4841  }
4842  CNetMessage& msg(msgs.front());
4843 
4844  TRACE6(net, inbound_message,
4845  pfrom->GetId(),
4846  pfrom->m_addr_name.c_str(),
4847  pfrom->ConnectionTypeAsString().c_str(),
4848  msg.m_type.c_str(),
4849  msg.m_recv.size(),
4850  msg.m_recv.data()
4851  );
4852 
4853  if (gArgs.GetBoolArg("-capturemessages", false)) {
4854  CaptureMessage(pfrom->addr, msg.m_type, MakeUCharSpan(msg.m_recv), /*is_incoming=*/true);
4855  }
4856 
4857  msg.SetVersion(pfrom->GetCommonVersion());
4858 
4859  try {
4860  ProcessMessage(*pfrom, msg.m_type, msg.m_recv, msg.m_time, interruptMsgProc);
4861  if (interruptMsgProc) return false;
4862  {
4863  LOCK(peer->m_getdata_requests_mutex);
4864  if (!peer->m_getdata_requests.empty()) fMoreWork = true;
4865  }
4866  } catch (const std::exception& e) {
4867  LogPrint(BCLog::NET, "%s(%s, %u bytes): Exception '%s' (%s) caught\n", __func__, SanitizeString(msg.m_type), msg.m_message_size, e.what(), typeid(e).name());
4868  } catch (...) {
4869  LogPrint(BCLog::NET, "%s(%s, %u bytes): Unknown exception caught\n", __func__, SanitizeString(msg.m_type), msg.m_message_size);
4870  }
4871 
4872  return fMoreWork;
4873 }
4874 
4875 void PeerManagerImpl::ConsiderEviction(CNode& pto, Peer& peer, std::chrono::seconds time_in_seconds)
4876 {
4878 
4879  CNodeState &state = *State(pto.GetId());
4880  const CNetMsgMaker msgMaker(pto.GetCommonVersion());
4881 
4882  if (!state.m_chain_sync.m_protect && pto.IsOutboundOrBlockRelayConn() && state.fSyncStarted) {
4883  // This is an outbound peer subject to disconnection if they don't
4884  // announce a block with as much work as the current tip within
4885  // CHAIN_SYNC_TIMEOUT + HEADERS_RESPONSE_TIME seconds (note: if
4886  // their chain has more work than ours, we should sync to it,
4887  // unless it's invalid, in which case we should find that out and
4888  // disconnect from them elsewhere).
4889  if (state.pindexBestKnownBlock != nullptr && state.pindexBestKnownBlock->nChainWork >= m_chainman.ActiveChain().Tip()->nChainWork) {
4890  if (state.m_chain_sync.m_timeout != 0s) {
4891  state.m_chain_sync.m_timeout = 0s;
4892  state.m_chain_sync.m_work_header = nullptr;
4893  state.m_chain_sync.m_sent_getheaders = false;
4894  }
4895  } else if (state.m_chain_sync.m_timeout == 0s || (state.m_chain_sync.m_work_header != nullptr && state.pindexBestKnownBlock != nullptr && state.pindexBestKnownBlock->nChainWork >= state.m_chain_sync.m_work_header->nChainWork)) {
4896  // Our best block known by this peer is behind our tip, and we're either noticing
4897  // that for the first time, OR this peer was able to catch up to some earlier point
4898  // where we checked against our tip.
4899  // Either way, set a new timeout based on current tip.
4900  state.m_chain_sync.m_timeout = time_in_seconds + CHAIN_SYNC_TIMEOUT;
4901  state.m_chain_sync.m_work_header = m_chainman.ActiveChain().Tip();
4902  state.m_chain_sync.m_sent_getheaders = false;
4903  } else if (state.m_chain_sync.m_timeout > 0s && time_in_seconds > state.m_chain_sync.m_timeout) {
4904  // No evidence yet that our peer has synced to a chain with work equal to that
4905  // of our tip, when we first detected it was behind. Send a single getheaders
4906  // message to give the peer a chance to update us.
4907  if (state.m_chain_sync.m_sent_getheaders) {
4908  // They've run out of time to catch up!
4909  LogPrintf("Disconnecting outbound peer %d for old chain, best known block = %s\n", pto.GetId(), state.pindexBestKnownBlock != nullptr ? state.pindexBestKnownBlock->GetBlockHash().ToString() : "<none>");
4910  pto.fDisconnect = true;
4911  } else {
4912  assert(state.m_chain_sync.m_work_header);
4913  // Here, we assume that the getheaders message goes out,
4914  // because it'll either go out or be skipped because of a
4915  // getheaders in-flight already, in which case the peer should
4916  // still respond to us with a sufficiently high work chain tip.
4917  MaybeSendGetHeaders(pto,
4918  GetLocator(state.m_chain_sync.m_work_header->pprev),
4919  peer);
4920  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());
4921  state.m_chain_sync.m_sent_getheaders = true;
4922  // Bump the timeout to allow a response, which could clear the timeout
4923  // (if the response shows the peer has synced), reset the timeout (if
4924  // the peer syncs to the required work but not to our tip), or result
4925  // in disconnect (if we advance to the timeout and pindexBestKnownBlock
4926  // has not sufficiently progressed)
4927  state.m_chain_sync.m_timeout = time_in_seconds + HEADERS_RESPONSE_TIME;
4928  }
4929  }
4930  }
4931 }
4932 
4933 void PeerManagerImpl::EvictExtraOutboundPeers(std::chrono::seconds now)
4934 {
4935  // If we have any extra block-relay-only peers, disconnect the youngest unless
4936  // it's given us a block -- in which case, compare with the second-youngest, and
4937  // out of those two, disconnect the peer who least recently gave us a block.
4938  // The youngest block-relay-only peer would be the extra peer we connected
4939  // to temporarily in order to sync our tip; see net.cpp.
4940  // Note that we use higher nodeid as a measure for most recent connection.
4941  if (m_connman.GetExtraBlockRelayCount() > 0) {
4942  std::pair<NodeId, std::chrono::seconds> youngest_peer{-1, 0}, next_youngest_peer{-1, 0};
4943 
4944  m_connman.ForEachNode([&](CNode* pnode) {
4945  if (!pnode->IsBlockOnlyConn() || pnode->fDisconnect) return;
4946  if (pnode->GetId() > youngest_peer.first) {
4947  next_youngest_peer = youngest_peer;
4948  youngest_peer.first = pnode->GetId();
4949  youngest_peer.second = pnode->m_last_block_time;
4950  }
4951  });
4952  NodeId to_disconnect = youngest_peer.first;
4953  if (youngest_peer.second > next_youngest_peer.second) {
4954  // Our newest block-relay-only peer gave us a block more recently;
4955  // disconnect our second youngest.
4956  to_disconnect = next_youngest_peer.first;
4957  }
4958  m_connman.ForNode(to_disconnect, [&](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
4960  // Make sure we're not getting a block right now, and that
4961  // we've been connected long enough for this eviction to happen
4962  // at all.
4963  // Note that we only request blocks from a peer if we learn of a
4964  // valid headers chain with at least as much work as our tip.
4965  CNodeState *node_state = State(pnode->GetId());
4966  if (node_state == nullptr ||
4967  (now - pnode->m_connected >= MINIMUM_CONNECT_TIME && node_state->nBlocksInFlight == 0)) {
4968  pnode->fDisconnect = true;
4969  LogPrint(BCLog::NET, "disconnecting extra block-relay-only peer=%d (last block received at time %d)\n",
4970  pnode->GetId(), count_seconds(pnode->m_last_block_time));
4971  return true;
4972  } else {
4973  LogPrint(BCLog::NET, "keeping block-relay-only peer=%d chosen for eviction (connect time: %d, blocks_in_flight: %d)\n",
4974  pnode->GetId(), count_seconds(pnode->m_connected), node_state->nBlocksInFlight);
4975  }
4976  return false;
4977  });
4978  }
4979 
4980  // Check whether we have too many outbound-full-relay peers
4981  if (m_connman.GetExtraFullOutboundCount() > 0) {
4982  // If we have more outbound-full-relay peers than we target, disconnect one.
4983  // Pick the outbound-full-relay peer that least recently announced
4984  // us a new block, with ties broken by choosing the more recent
4985  // connection (higher node id)
4986  NodeId worst_peer = -1;
4987  int64_t oldest_block_announcement = std::numeric_limits<int64_t>::max();
4988 
4989  m_connman.ForEachNode([&](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
4991 
4992  // Only consider outbound-full-relay peers that are not already
4993  // marked for disconnection
4994  if (!pnode->IsFullOutboundConn() || pnode->fDisconnect) return;
4995  CNodeState *state = State(pnode->GetId());
4996  if (state == nullptr) return; // shouldn't be possible, but just in case
4997  // Don't evict our protected peers
4998  if (state->m_chain_sync.m_protect) return;
4999  if (state->m_last_block_announcement < oldest_block_announcement || (state->m_last_block_announcement == oldest_block_announcement && pnode->GetId() > worst_peer)) {
5000  worst_peer = pnode->GetId();
5001  oldest_block_announcement = state->m_last_block_announcement;
5002  }
5003  });
5004  if (worst_peer != -1) {
5005  bool disconnected = m_connman.ForNode(worst_peer, [&](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
5007 
5008  // Only disconnect a peer that has been connected to us for
5009  // some reasonable fraction of our check-frequency, to give
5010  // it time for new information to have arrived.
5011  // Also don't disconnect any peer we're trying to download a
5012  // block from.
5013  CNodeState &state = *State(pnode->GetId());
5014  if (now - pnode->m_connected > MINIMUM_CONNECT_TIME && state.nBlocksInFlight == 0) {
5015  LogPrint(BCLog::NET, "disconnecting extra outbound peer=%d (last block announcement received at time %d)\n", pnode->GetId(), oldest_block_announcement);
5016  pnode->fDisconnect = true;
5017  return true;
5018  } else {
5019  LogPrint(BCLog::NET, "keeping outbound peer=%d chosen for eviction (connect time: %d, blocks_in_flight: %d)\n",
5020  pnode->GetId(), count_seconds(pnode->m_connected), state.nBlocksInFlight);
5021  return false;
5022  }
5023  });
5024  if (disconnected) {
5025  // If we disconnected an extra peer, that means we successfully
5026  // connected to at least one peer after the last time we
5027  // detected a stale tip. Don't try any more extra peers until
5028  // we next detect a stale tip, to limit the load we put on the
5029  // network from these extra connections.
5030  m_connman.SetTryNewOutboundPeer(false);
5031  }
5032  }
5033  }
5034 }
5035 
5036 void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
5037 {
5038  LOCK(cs_main);
5039 
5040  auto now{GetTime<std::chrono::seconds>()};
5041 
5042  EvictExtraOutboundPeers(now);
5043 
5044  if (now > m_stale_tip_check_time) {
5045  // Check whether our tip is stale, and if so, allow using an extra
5046  // outbound peer
5047  if (!fImporting && !fReindex && m_connman.GetNetworkActive() && m_connman.GetUseAddrmanOutgoing() && TipMayBeStale()) {
5048  LogPrintf("Potential stale tip detected, will try using extra outbound peer (last tip update: %d seconds ago)\n",
5049  count_seconds(now - m_last_tip_update.load()));
5050  m_connman.SetTryNewOutboundPeer(true);
5051  } else if (m_connman.GetTryNewOutboundPeer()) {
5052  m_connman.SetTryNewOutboundPeer(false);
5053  }
5054  m_stale_tip_check_time = now + STALE_CHECK_INTERVAL;
5055  }
5056 
5057  if (!m_initial_sync_finished && CanDirectFetch()) {
5058  m_connman.StartExtraBlockRelayPeers();
5059  m_initial_sync_finished = true;
5060  }
5061 }
5062 
5063 void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::microseconds now)
5064 {
5065  if (m_connman.ShouldRunInactivityChecks(node_to, std::chrono::duration_cast<std::chrono::seconds>(now)) &&
5066  peer.m_ping_nonce_sent &&
5067  now > peer.m_ping_start.load() + TIMEOUT_INTERVAL)
5068  {
5069  // The ping timeout is using mocktime. To disable the check during
5070  // testing, increase -peertimeout.
5071  LogPrint(BCLog::NET, "ping timeout: %fs peer=%d\n", 0.000001 * count_microseconds(now - peer.m_ping_start.load()), peer.m_id);
5072  node_to.fDisconnect = true;
5073  return;
5074  }
5075 
5076  const CNetMsgMaker msgMaker(node_to.GetCommonVersion());
5077  bool pingSend = false;
5078 
5079  if (peer.m_ping_queued) {
5080  // RPC ping request by user
5081  pingSend = true;
5082  }
5083 
5084  if (peer.m_ping_nonce_sent == 0 && now > peer.m_ping_start.load() + PING_INTERVAL) {
5085  // Ping automatically sent as a latency probe & keepalive.
5086  pingSend = true;
5087  }
5088 
5089  if (pingSend) {
5090  uint64_t nonce;
5091  do {
5092  nonce = GetRand<uint64_t>();
5093  } while (nonce == 0);
5094  peer.m_ping_queued = false;
5095  peer.m_ping_start = now;
5096  if (node_to.GetCommonVersion() > BIP0031_VERSION) {
5097  peer.m_ping_nonce_sent = nonce;
5098  m_connman.PushMessage(&node_to, msgMaker.Make(NetMsgType::PING, nonce));
5099  } else {
5100  // Peer is too old to support ping command with nonce, pong will never arrive.
5101  peer.m_ping_nonce_sent = 0;
5102  m_connman.PushMessage(&node_to, msgMaker.Make(NetMsgType::PING));
5103  }
5104  }
5105 }
5106 
5107 void PeerManagerImpl::MaybeSendAddr(CNode& node, Peer& peer, std::chrono::microseconds current_time)
5108 {
5109  // Nothing to do for non-address-relay peers
5110  if (!peer.m_addr_relay_enabled) return;
5111 
5112  LOCK(peer.m_addr_send_times_mutex);
5113  // Periodically advertise our local address to the peer.
5114  if (fListen && !m_chainman.ActiveChainstate().IsInitialBlockDownload() &&
5115  peer.m_next_local_addr_send < current_time) {
5116  // If we've sent before, clear the bloom filter for the peer, so that our
5117  // self-announcement will actually go out.
5118  // This might be unnecessary if the bloom filter has already rolled
5119  // over since our last self-announcement, but there is only a small
5120  // bandwidth cost that we can incur by doing this (which happens
5121  // once a day on average).
5122  if (peer.m_next_local_addr_send != 0us) {
5123  peer.m_addr_known->reset();
5124  }
5125  if (std::optional<CService> local_service = GetLocalAddrForPeer(node)) {
5126  CAddress local_addr{*local_service, peer.m_our_services, Now<NodeSeconds>()};
5127  FastRandomContext insecure_rand;
5128  PushAddress(peer, local_addr, insecure_rand);
5129  }
5130  peer.m_next_local_addr_send = GetExponentialRand(current_time, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
5131  }
5132 
5133  // We sent an `addr` message to this peer recently. Nothing more to do.
5134  if (current_time <= peer.m_next_addr_send) return;
5135 
5136  peer.m_next_addr_send = GetExponentialRand(current_time, AVG_ADDRESS_BROADCAST_INTERVAL);
5137 
5138  if (!Assume(peer.m_addrs_to_send.size() <= MAX_ADDR_TO_SEND)) {
5139  // Should be impossible since we always check size before adding to
5140  // m_addrs_to_send. Recover by trimming the vector.
5141  peer.m_addrs_to_send.resize(MAX_ADDR_TO_SEND);
5142  }
5143 
5144  // Remove addr records that the peer already knows about, and add new
5145  // addrs to the m_addr_known filter on the same pass.
5146  auto addr_already_known = [&peer](const CAddress& addr) {
5147  bool ret = peer.m_addr_known->contains(addr.GetKey());
5148  if (!ret) peer.m_addr_known->insert(addr.GetKey());
5149  return ret;
5150  };
5151  peer.m_addrs_to_send.erase(std::remove_if(peer.m_addrs_to_send.begin(), peer.m_addrs_to_send.end(), addr_already_known),
5152  peer.m_addrs_to_send.end());
5153 
5154  // No addr messages to send
5155  if (peer.m_addrs_to_send.empty()) return;
5156 
5157  const char* msg_type;
5158  int make_flags;
5159  if (peer.m_wants_addrv2) {
5160  msg_type = NetMsgType::ADDRV2;
5161  make_flags = ADDRV2_FORMAT;
5162  } else {
5163  msg_type = NetMsgType::ADDR;
5164  make_flags = 0;
5165  }
5166  m_connman.PushMessage(&node, CNetMsgMaker(node.GetCommonVersion()).Make(make_flags, msg_type, peer.m_addrs_to_send));
5167  peer.m_addrs_to_send.clear();
5168 
5169  // we only send the big addr message once
5170  if (peer.m_addrs_to_send.capacity() > 40) {
5171  peer.m_addrs_to_send.shrink_to_fit();
5172  }
5173 }
5174 
5175 void PeerManagerImpl::MaybeSendSendHeaders(CNode& node, Peer& peer)
5176 {
5177  // Delay sending SENDHEADERS (BIP 130) until we're done with an
5178  // initial-headers-sync with this peer. Receiving headers announcements for
5179  // new blocks while trying to sync their headers chain is problematic,
5180  // because of the state tracking done.
5181  if (!peer.m_sent_sendheaders && node.GetCommonVersion() >= SENDHEADERS_VERSION) {
5182  LOCK(cs_main);
5183  CNodeState &state = *State(node.GetId());
5184  if (state.pindexBestKnownBlock != nullptr &&
5185  state.pindexBestKnownBlock->nChainWork > nMinimumChainWork) {
5186  // Tell our peer we prefer to receive headers rather than inv's
5187  // We send this to non-NODE NETWORK peers as well, because even
5188  // non-NODE NETWORK peers can announce blocks (such as pruning
5189  // nodes)
5190  m_connman.PushMessage(&node, CNetMsgMaker(node.GetCommonVersion()).Make(NetMsgType::SENDHEADERS));
5191  peer.m_sent_sendheaders = true;
5192  }
5193  }
5194 }
5195 
5196 void PeerManagerImpl::MaybeSendFeefilter(CNode& pto, Peer& peer, std::chrono::microseconds current_time)
5197 {
5198  if (m_ignore_incoming_txs) return;
5199  if (pto.GetCommonVersion() < FEEFILTER_VERSION) return;
5200  // peers with the forcerelay permission should not filter txs to us
5202  // Don't send feefilter messages to outbound block-relay-only peers since they should never announce
5203  // transactions to us, regardless of feefilter state.
5204  if (pto.IsBlockOnlyConn()) return;
5205 
5206  CAmount currentFilter = m_mempool.GetMinFee().GetFeePerK();
5207  static FeeFilterRounder g_filter_rounder{CFeeRate{DEFAULT_MIN_RELAY_TX_FEE}};
5208 
5209  if (m_chainman.ActiveChainstate().IsInitialBlockDownload()) {
5210  // Received tx-inv messages are discarded when the active
5211  // chainstate is in IBD, so tell the peer to not send them.
5212  currentFilter = MAX_MONEY;
5213  } else {
5214  static const CAmount MAX_FILTER{g_filter_rounder.round(MAX_MONEY)};
5215  if (peer.m_fee_filter_sent == MAX_FILTER) {
5216  // Send the current filter if we sent MAX_FILTER previously
5217  // and made it out of IBD.
5218  peer.m_next_send_feefilter = 0us;
5219  }
5220  }
5221  if (current_time > peer.m_next_send_feefilter) {
5222  CAmount filterToSend = g_filter_rounder.round(currentFilter);
5223  // We always have a fee filter of at least the min relay fee
5224  filterToSend = std::max(filterToSend, m_mempool.m_min_relay_feerate.GetFeePerK());
5225  if (filterToSend != peer.m_fee_filter_sent) {
5226  m_connman.PushMessage(&pto, CNetMsgMaker(pto.GetCommonVersion()).Make(NetMsgType::FEEFILTER, filterToSend));
5227  peer.m_fee_filter_sent = filterToSend;
5228  }
5229  peer.m_next_send_feefilter = GetExponentialRand(current_time, AVG_FEEFILTER_BROADCAST_INTERVAL);
5230  }
5231  // If the fee filter has changed substantially and it's still more than MAX_FEEFILTER_CHANGE_DELAY
5232  // until scheduled broadcast, then move the broadcast to within MAX_FEEFILTER_CHANGE_DELAY.
5233  else if (current_time + MAX_FEEFILTER_CHANGE_DELAY < peer.m_next_send_feefilter &&
5234  (currentFilter < 3 * peer.m_fee_filter_sent / 4 || currentFilter > 4 * peer.m_fee_filter_sent / 3)) {
5235  peer.m_next_send_feefilter = current_time + GetRandomDuration<std::chrono::microseconds>(MAX_FEEFILTER_CHANGE_DELAY);
5236  }
5237 }
5238 
5239 namespace {
5240 class CompareInvMempoolOrder
5241 {
5242  CTxMemPool* mp;
5243  bool m_wtxid_relay;
5244 public:
5245  explicit CompareInvMempoolOrder(CTxMemPool *_mempool, bool use_wtxid)
5246  {
5247  mp = _mempool;
5248  m_wtxid_relay = use_wtxid;
5249  }
5250 
5251  bool operator()(std::set<uint256>::iterator a, std::set<uint256>::iterator b)
5252  {
5253  /* As std::make_heap produces a max-heap, we want the entries with the
5254  * fewest ancestors/highest fee to sort later. */
5255  return mp->CompareDepthAndScore(*b, *a, m_wtxid_relay);
5256  }
5257 };
5258 } // namespace
5259 
5260 bool PeerManagerImpl::RejectIncomingTxs(const CNode& peer) const
5261 {
5262  // block-relay-only peers may never send txs to us
5263  if (peer.IsBlockOnlyConn()) return true;
5264  // In -blocksonly mode, peers need the 'relay' permission to send txs to us
5265  if (m_ignore_incoming_txs && !peer.HasPermission(NetPermissionFlags::Relay)) return true;
5266  return false;
5267 }
5268 
5269 bool PeerManagerImpl::SetupAddressRelay(const CNode& node, Peer& peer)
5270 {
5271  // We don't participate in addr relay with outbound block-relay-only
5272  // connections to prevent providing adversaries with the additional
5273  // information of addr traffic to infer the link.
5274  if (node.IsBlockOnlyConn()) return false;
5275 
5276  if (!peer.m_addr_relay_enabled.exchange(true)) {
5277  // First addr message we have received from the peer, initialize
5278  // m_addr_known
5279  peer.m_addr_known = std::make_unique<CRollingBloomFilter>(5000, 0.001);
5280  }
5281 
5282  return true;
5283 }
5284 
5285 bool PeerManagerImpl::SendMessages(CNode* pto)
5286 {
5287  PeerRef peer = GetPeerRef(pto->GetId());
5288  if (!peer) return false;
5289  const Consensus::Params& consensusParams = m_chainparams.GetConsensus();
5290 
5291  // We must call MaybeDiscourageAndDisconnect first, to ensure that we'll
5292  // disconnect misbehaving peers even before the version handshake is complete.
5293  if (MaybeDiscourageAndDisconnect(*pto, *peer)) return true;
5294 
5295  // Don't send anything until the version handshake is complete
5296  if (!pto->fSuccessfullyConnected || pto->fDisconnect)
5297  return true;
5298 
5299  // If we get here, the outgoing message serialization version is set and can't change.
5300  const CNetMsgMaker msgMaker(pto->GetCommonVersion());
5301 
5302  const auto current_time{GetTime<std::chrono::microseconds>()};
5303 
5304  if (pto->IsAddrFetchConn() && current_time - pto->m_connected > 10 * AVG_ADDRESS_BROADCAST_INTERVAL) {
5305  LogPrint(BCLog::NET, "addrfetch connection timeout; disconnecting peer=%d\n", pto->GetId());
5306  pto->fDisconnect = true;
5307  return true;
5308  }
5309 
5310  MaybeSendPing(*pto, *peer, current_time);
5311 
5312  // MaybeSendPing may have marked peer for disconnection
5313  if (pto->fDisconnect) return true;
5314 
5315  MaybeSendAddr(*pto, *peer, current_time);
5316 
5317  MaybeSendSendHeaders(*pto, *peer);
5318 
5319  {
5320  LOCK(cs_main);
5321 
5322  CNodeState &state = *State(pto->GetId());
5323 
5324  // Start block sync
5325  if (m_chainman.m_best_header == nullptr) {
5326  m_chainman.m_best_header = m_chainman.ActiveChain().Tip();
5327  }
5328 
5329  // Determine whether we might try initial headers sync or parallel
5330  // block download from this peer -- this mostly affects behavior while
5331  // in IBD (once out of IBD, we sync from all peers).
5332  bool sync_blocks_and_headers_from_peer = false;
5333  if (state.fPreferredDownload) {
5334  sync_blocks_and_headers_from_peer = true;
5335  } else if (CanServeBlocks(*peer) && !pto->IsAddrFetchConn()) {
5336  // Typically this is an inbound peer. If we don't have any outbound
5337  // peers, or if we aren't downloading any blocks from such peers,
5338  // then allow block downloads from this peer, too.
5339  // We prefer downloading blocks from outbound peers to avoid
5340  // putting undue load on (say) some home user who is just making
5341  // outbound connections to the network, but if our only source of
5342  // the latest blocks is from an inbound peer, we have to be sure to
5343  // eventually download it (and not just wait indefinitely for an
5344  // outbound peer to have it).
5345  if (m_num_preferred_download_peers == 0 || mapBlocksInFlight.empty()) {
5346  sync_blocks_and_headers_from_peer = true;
5347  }
5348  }
5349 
5350  if (!state.fSyncStarted && CanServeBlocks(*peer) && !fImporting && !fReindex) {
5351  // Only actively request headers from a single peer, unless we're close to today.
5352  if ((nSyncStarted == 0 && sync_blocks_and_headers_from_peer) || m_chainman.m_best_header->Time() > GetAdjustedTime() - 24h) {
5353  const CBlockIndex* pindexStart = m_chainman.m_best_header;
5354  /* If possible, start at the block preceding the currently
5355  best known header. This ensures that we always get a
5356  non-empty list of headers back as long as the peer
5357  is up-to-date. With a non-empty response, we can initialise
5358  the peer's known best block. This wouldn't be possible
5359  if we requested starting at m_chainman.m_best_header and
5360  got back an empty response. */
5361  if (pindexStart->pprev)
5362  pindexStart = pindexStart->pprev;
5363  if (MaybeSendGetHeaders(*pto, GetLocator(pindexStart), *peer)) {
5364  LogPrint(BCLog::NET, "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->nHeight, pto->GetId(), peer->m_starting_height);
5365 
5366  state.fSyncStarted = true;
5367  state.m_headers_sync_timeout = current_time + HEADERS_DOWNLOAD_TIMEOUT_BASE +
5368  (
5369  // Convert HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER to microseconds before scaling
5370  // to maintain precision
5371  std::chrono::microseconds{HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER} *
5372  Ticks<std::chrono::seconds>(GetAdjustedTime() - m_chainman.m_best_header->Time()) / consensusParams.nPowTargetSpacing
5373  );
5374  nSyncStarted++;
5375  }
5376  }
5377  }
5378 
5379  //
5380  // Try sending block announcements via headers
5381  //
5382  {
5383  // If we have no more than MAX_BLOCKS_TO_ANNOUNCE in our
5384  // list of block hashes we're relaying, and our peer wants
5385  // headers announcements, then find the first header
5386  // not yet known to our peer but would connect, and send.
5387  // If no header would connect, or if we have too many
5388  // blocks, or if the peer doesn't want headers, just
5389  // add all to the inv queue.
5390  LOCK(peer->m_block_inv_mutex);
5391  std::vector<CBlock> vHeaders;
5392  bool fRevertToInv = ((!state.fPreferHeaders &&
5393  (!state.m_requested_hb_cmpctblocks || peer->m_blocks_for_headers_relay.size() > 1)) ||
5394  peer->m_blocks_for_headers_relay.size() > MAX_BLOCKS_TO_ANNOUNCE);
5395  const CBlockIndex *pBestIndex = nullptr; // last header queued for delivery
5396  ProcessBlockAvailability(pto->GetId()); // ensure pindexBestKnownBlock is up-to-date
5397 
5398  if (!fRevertToInv) {
5399  bool fFoundStartingHeader = false;
5400  // Try to find first header that our peer doesn't have, and
5401  // then send all headers past that one. If we come across any
5402  // headers that aren't on m_chainman.ActiveChain(), give up.
5403  for (const uint256& hash : peer->m_blocks_for_headers_relay) {
5404  const CBlockIndex* pindex = m_chainman.m_blockman.LookupBlockIndex(hash);
5405  assert(pindex);
5406  if (m_chainman.ActiveChain()[pindex->nHeight] != pindex) {
5407  // Bail out if we reorged away from this block
5408  fRevertToInv = true;
5409  break;
5410  }
5411  if (pBestIndex != nullptr && pindex->pprev != pBestIndex) {
5412  // This means that the list of blocks to announce don't
5413  // connect to each other.
5414  // This shouldn't really be possible to hit during
5415  // regular operation (because reorgs should take us to
5416  // a chain that has some block not on the prior chain,
5417  // which should be caught by the prior check), but one
5418  // way this could happen is by using invalidateblock /
5419  // reconsiderblock repeatedly on the tip, causing it to
5420  // be added multiple times to m_blocks_for_headers_relay.
5421  // Robustly deal with this rare situation by reverting
5422  // to an inv.
5423  fRevertToInv = true;
5424  break;
5425  }
5426  pBestIndex = pindex;
5427  if (fFoundStartingHeader) {
5428  // add this to the headers message
5429  vHeaders.push_back(pindex->GetBlockHeader());
5430  } else if (PeerHasHeader(&state, pindex)) {
5431  continue; // keep looking for the first new block
5432  } else if (pindex->pprev == nullptr || PeerHasHeader(&state, pindex->pprev)) {
5433  // Peer doesn't have this header but they do have the prior one.
5434  // Start sending headers.
5435  fFoundStartingHeader = true;
5436  vHeaders.push_back(pindex->GetBlockHeader());
5437  } else {
5438  // Peer doesn't have this header or the prior one -- nothing will
5439  // connect, so bail out.
5440  fRevertToInv = true;
5441  break;
5442  }
5443  }
5444  }
5445  if (!fRevertToInv && !vHeaders.empty()) {
5446  if (vHeaders.size() == 1 && state.m_requested_hb_cmpctblocks) {
5447  // We only send up to 1 block as header-and-ids, as otherwise
5448  // probably means we're doing an initial-ish-sync or they're slow
5449  LogPrint(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", __func__,
5450  vHeaders.front().GetHash().ToString(), pto->GetId());
5451 
5452  std::optional<CSerializedNetMsg> cached_cmpctblock_msg;
5453  {
5454  LOCK(m_most_recent_block_mutex);
5455  if (m_most_recent_block_hash == pBestIndex->GetBlockHash()) {
5456  cached_cmpctblock_msg = msgMaker.Make(NetMsgType::CMPCTBLOCK, *m_most_recent_compact_block);
5457  }
5458  }
5459  if (cached_cmpctblock_msg.has_value()) {
5460  m_connman.PushMessage(pto, std::move(cached_cmpctblock_msg.value()));
5461  } else {
5462  CBlock block;
5463  bool ret = ReadBlockFromDisk(block, pBestIndex, consensusParams);
5464  assert(ret);
5465  CBlockHeaderAndShortTxIDs cmpctblock{block};
5466  m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::CMPCTBLOCK, cmpctblock));
5467  }
5468  state.pindexBestHeaderSent = pBestIndex;
5469  } else if (state.fPreferHeaders) {
5470  if (vHeaders.size() > 1) {
5471  LogPrint(BCLog::NET, "%s: %u headers, range (%s, %s), to peer=%d\n", __func__,
5472  vHeaders.size(),
5473  vHeaders.front().GetHash().ToString(),
5474  vHeaders.back().GetHash().ToString(), pto->GetId());
5475  } else {
5476  LogPrint(BCLog::NET, "%s: sending header %s to peer=%d\n", __func__,
5477  vHeaders.front().GetHash().ToString(), pto->GetId());
5478  }
5479  m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
5480  state.pindexBestHeaderSent = pBestIndex;
5481  } else
5482  fRevertToInv = true;
5483  }
5484  if (fRevertToInv) {
5485  // If falling back to using an inv, just try to inv the tip.
5486  // The last entry in m_blocks_for_headers_relay was our tip at some point
5487  // in the past.
5488  if (!peer->m_blocks_for_headers_relay.empty()) {
5489  const uint256& hashToAnnounce = peer->m_blocks_for_headers_relay.back();
5490  const CBlockIndex* pindex = m_chainman.m_blockman.LookupBlockIndex(hashToAnnounce);
5491  assert(pindex);
5492 
5493  // Warn if we're announcing a block that is not on the main chain.
5494  // This should be very rare and could be optimized out.
5495  // Just log for now.
5496  if (m_chainman.ActiveChain()[pindex->nHeight] != pindex) {
5497  LogPrint(BCLog::NET, "Announcing block %s not on main chain (tip=%s)\n",
5498  hashToAnnounce.ToString(), m_chainman.ActiveChain().Tip()->GetBlockHash().ToString());
5499  }
5500 
5501  // If the peer's chain has this block, don't inv it back.
5502  if (!PeerHasHeader(&state, pindex)) {
5503  peer->m_blocks_for_inv_relay.push_back(hashToAnnounce);
5504  LogPrint(BCLog::NET, "%s: sending inv peer=%d hash=%s\n", __func__,
5505  pto->GetId(), hashToAnnounce.ToString());
5506  }
5507  }
5508  }
5509  peer->m_blocks_for_headers_relay.clear();
5510  }
5511 
5512  //
5513  // Message: inventory
5514  //
5515  std::vector<CInv> vInv;
5516  {
5517  LOCK(peer->m_block_inv_mutex);
5518  vInv.reserve(std::max<size_t>(peer->m_blocks_for_inv_relay.size(), INVENTORY_BROADCAST_MAX));
5519 
5520  // Add blocks
5521  for (const uint256& hash : peer->m_blocks_for_inv_relay) {
5522  vInv.push_back(CInv(MSG_BLOCK, hash));
5523  if (vInv.size() == MAX_INV_SZ) {
5524  m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
5525  vInv.clear();
5526  }
5527  }
5528  peer->m_blocks_for_inv_relay.clear();
5529  }
5530 
5531  if (auto tx_relay = peer->GetTxRelay(); tx_relay != nullptr) {
5532  LOCK(tx_relay->m_tx_inventory_mutex);
5533  // Check whether periodic sends should happen
5534  bool fSendTrickle = pto->HasPermission(NetPermissionFlags::NoBan);
5535  if (tx_relay->m_next_inv_send_time < current_time) {
5536  fSendTrickle = true;
5537  if (pto->IsInboundConn()) {
5538  tx_relay->m_next_inv_send_time = NextInvToInbounds(current_time, INBOUND_INVENTORY_BROADCAST_INTERVAL);
5539  } else {
5540  tx_relay->m_next_inv_send_time = GetExponentialRand(current_time, OUTBOUND_INVENTORY_BROADCAST_INTERVAL);
5541  }
5542  }
5543 
5544  // Time to send but the peer has requested we not relay transactions.
5545  if (fSendTrickle) {
5546  LOCK(tx_relay->m_bloom_filter_mutex);
5547  if (!tx_relay->m_relay_txs) tx_relay->m_tx_inventory_to_send.clear();
5548  }
5549 
5550  // Respond to BIP35 mempool requests
5551  if (fSendTrickle && tx_relay->m_send_mempool) {
5552  auto vtxinfo = m_mempool.infoAll();
5553  tx_relay->m_send_mempool = false;
5554  const CFeeRate filterrate{tx_relay->m_fee_filter_received.load()};
5555 
5556  LOCK(tx_relay->m_bloom_filter_mutex);
5557 
5558  for (const auto& txinfo : vtxinfo) {
5559  const uint256& hash = peer->m_wtxid_relay ? txinfo.tx->GetWitnessHash() : txinfo.tx->GetHash();
5560  CInv inv(peer->m_wtxid_relay ? MSG_WTX : MSG_TX, hash);
5561  tx_relay->m_tx_inventory_to_send.erase(hash);
5562  // Don't send transactions that peers will not put into their mempool
5563  if (txinfo.fee < filterrate.GetFee(txinfo.vsize)) {
5564  continue;
5565  }
5566  if (tx_relay->m_bloom_filter) {
5567  if (!tx_relay->m_bloom_filter->IsRelevantAndUpdate(*txinfo.tx)) continue;
5568  }
5569  tx_relay->m_tx_inventory_known_filter.insert(hash);
5570  // Responses to MEMPOOL requests bypass the m_recently_announced_invs filter.
5571  vInv.push_back(inv);
5572  if (vInv.size() == MAX_INV_SZ) {
5573  m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
5574  vInv.clear();
5575  }
5576  }
5577  tx_relay->m_last_mempool_req = std::chrono::duration_cast<std::chrono::seconds>(current_time);
5578  }
5579 
5580  // Determine transactions to relay
5581  if (fSendTrickle) {
5582  // Produce a vector with all candidates for sending
5583  std::vector<std::set<uint256>::iterator> vInvTx;
5584  vInvTx.reserve(tx_relay->m_tx_inventory_to_send.size());
5585  for (std::set<uint256>::iterator it = tx_relay->m_tx_inventory_to_send.begin(); it != tx_relay->m_tx_inventory_to_send.end(); it++) {
5586  vInvTx.push_back(it);
5587  }
5588  const CFeeRate filterrate{tx_relay->m_fee_filter_received.load()};
5589  // Topologically and fee-rate sort the inventory we send for privacy and priority reasons.
5590  // A heap is used so that not all items need sorting if only a few are being sent.
5591  CompareInvMempoolOrder compareInvMempoolOrder(&m_mempool, peer->m_wtxid_relay);
5592  std::make_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
5593  // No reason to drain out at many times the network's capacity,
5594  // especially since we have many peers and some will draw much shorter delays.
5595  unsigned int nRelayedTransactions = 0;
5596  LOCK(tx_relay->m_bloom_filter_mutex);
5597  size_t broadcast_max{INVENTORY_BROADCAST_MAX + (tx_relay->m_tx_inventory_to_send.size()/1000)*5};
5598  broadcast_max = std::min<size_t>(1000, broadcast_max);
5599  while (!vInvTx.empty() && nRelayedTransactions < broadcast_max) {
5600  // Fetch the top element from the heap
5601  std::pop_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
5602  std::set<uint256>::iterator it = vInvTx.back();
5603  vInvTx.pop_back();
5604  uint256 hash = *it;
5605  CInv inv(peer->m_wtxid_relay ? MSG_WTX : MSG_TX, hash);
5606  // Remove it from the to-be-sent set
5607  tx_relay->m_tx_inventory_to_send.erase(it);
5608  // Check if not in the filter already
5609  if (tx_relay->m_tx_inventory_known_filter.contains(hash)) {
5610  continue;
5611  }
5612  // Not in the mempool anymore? don't bother sending it.
5613  auto txinfo = m_mempool.info(ToGenTxid(inv));
5614  if (!txinfo.tx) {
5615  continue;
5616  }
5617  auto txid = txinfo.tx->GetHash();
5618  auto wtxid = txinfo.tx->GetWitnessHash();
5619  // Peer told you to not send transactions at that feerate? Don't bother sending it.
5620  if (txinfo.fee < filterrate.GetFee(txinfo.vsize)) {
5621  continue;
5622  }
5623  if (tx_relay->m_bloom_filter && !tx_relay->m_bloom_filter->IsRelevantAndUpdate(*txinfo.tx)) continue;
5624  // Send
5625  State(pto->GetId())->m_recently_announced_invs.insert(hash);
5626  vInv.push_back(inv);
5627  nRelayedTransactions++;
5628  {
5629  // Expire old relay messages
5630  while (!g_relay_expiration.empty() && g_relay_expiration.front().first < current_time)
5631  {
5632  mapRelay.erase(g_relay_expiration.front().second);
5633  g_relay_expiration.pop_front();
5634  }
5635 
5636  auto ret = mapRelay.emplace(txid, std::move(txinfo.tx));
5637  if (ret.second) {
5638  g_relay_expiration.emplace_back(current_time + RELAY_TX_CACHE_TIME, ret.first);
5639  }
5640  // Add wtxid-based lookup into mapRelay as well, so that peers can request by wtxid
5641  auto ret2 = mapRelay.emplace(wtxid, ret.first->second);
5642  if (ret2.second) {
5643  g_relay_expiration.emplace_back(current_time + RELAY_TX_CACHE_TIME, ret2.first);
5644  }
5645  }
5646  if (vInv.size() == MAX_INV_SZ) {
5647  m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
5648  vInv.clear();
5649  }
5650  tx_relay->m_tx_inventory_known_filter.insert(hash);
5651  if (hash != txid) {
5652  // Insert txid into m_tx_inventory_known_filter, even for
5653  // wtxidrelay peers. This prevents re-adding of
5654  // unconfirmed parents to the recently_announced
5655  // filter, when a child tx is requested. See
5656  // ProcessGetData().
5657  tx_relay->m_tx_inventory_known_filter.insert(txid);
5658  }
5659  }
5660  }
5661  }
5662  if (!vInv.empty())
5663  m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
5664 
5665  // Detect whether we're stalling
5666  if (state.m_stalling_since.count() && state.m_stalling_since < current_time - BLOCK_STALLING_TIMEOUT) {
5667  // Stalling only triggers when the block download window cannot move. During normal steady state,
5668  // the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
5669  // should only happen during initial block download.
5670  LogPrintf("Peer=%d is stalling block download, disconnecting\n", pto->GetId());
5671  pto->fDisconnect = true;
5672  return true;
5673  }
5674  // In case there is a block that has been in flight from this peer for block_interval * (1 + 0.5 * N)
5675  // (with N the number of peers from which we're downloading validated blocks), disconnect due to timeout.
5676  // We compensate for other peers to prevent killing off peers due to our own downstream link
5677  // being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
5678  // to unreasonably increase our timeout.
5679  if (state.vBlocksInFlight.size() > 0) {
5680  QueuedBlock &queuedBlock = state.vBlocksInFlight.front();
5681  int nOtherPeersWithValidatedDownloads = m_peers_downloading_from - 1;
5682  if (current_time > state.m_downloading_since + std::chrono::seconds{consensusParams.nPowTargetSpacing} * (BLOCK_DOWNLOAD_TIMEOUT_BASE + BLOCK_DOWNLOAD_TIMEOUT_PER_PEER * nOtherPeersWithValidatedDownloads)) {
5683  LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", queuedBlock.pindex->GetBlockHash().ToString(), pto->GetId());
5684  pto->fDisconnect = true;
5685  return true;
5686  }
5687  }
5688  // Check for headers sync timeouts
5689  if (state.fSyncStarted && state.m_headers_sync_timeout < std::chrono::microseconds::max()) {
5690  // Detect whether this is a stalling initial-headers-sync peer
5691  if (m_chainman.m_best_header->Time() <= GetAdjustedTime() - 24h) {
5692  if (current_time > state.m_headers_sync_timeout && nSyncStarted == 1 && (m_num_preferred_download_peers - state.fPreferredDownload >= 1)) {
5693  // Disconnect a peer (without NetPermissionFlags::NoBan permission) if it is our only sync peer,
5694  // and we have others we could be using instead.
5695  // Note: If all our peers are inbound, then we won't
5696  // disconnect our sync peer for stalling; we have bigger
5697  // problems if we can't get any outbound peers.
5699  LogPrintf("Timeout downloading headers from peer=%d, disconnecting\n", pto->GetId());
5700  pto->fDisconnect = true;
5701  return true;
5702  } else {
5703  LogPrintf("Timeout downloading headers from noban peer=%d, not disconnecting\n", pto->GetId());
5704  // Reset the headers sync state so that we have a
5705  // chance to try downloading from a different peer.
5706  // Note: this will also result in at least one more
5707  // getheaders message to be sent to
5708  // this peer (eventually).
5709  state.fSyncStarted = false;
5710  nSyncStarted--;
5711  state.m_headers_sync_timeout = 0us;
5712  }
5713  }
5714  } else {
5715  // After we've caught up once, reset the timeout so we can't trigger
5716  // disconnect later.
5717  state.m_headers_sync_timeout = std::chrono::microseconds::max();
5718  }
5719  }
5720 
5721  // Check that outbound peers have reasonable chains
5722  // GetTime() is used by this anti-DoS logic so we can test this using mocktime
5723  ConsiderEviction(*pto, *peer, GetTime<std::chrono::seconds>());
5724 
5725  //
5726  // Message: getdata (blocks)
5727  //
5728  std::vector<CInv> vGetData;
5729  if (CanServeBlocks(*peer) && ((sync_blocks_and_headers_from_peer && !IsLimitedPeer(*peer)) || !m_chainman.ActiveChainstate().IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
5730  std::vector<const CBlockIndex*> vToDownload;
5731  NodeId staller = -1;
5732  FindNextBlocksToDownload(*peer, MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller);
5733  for (const CBlockIndex *pindex : vToDownload) {
5734  uint32_t nFetchFlags = GetFetchFlags(*peer);
5735  vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
5736  BlockRequested(pto->GetId(), *pindex);
5737  LogPrint(BCLog::NET, "Requesting block %s (%d) peer=%d\n", pindex->GetBlockHash().ToString(),
5738  pindex->nHeight, pto->GetId());
5739  }
5740  if (state.nBlocksInFlight == 0 && staller != -1) {
5741  if (State(staller)->m_stalling_since == 0us) {
5742  State(staller)->m_stalling_since = current_time;
5743  LogPrint(BCLog::NET, "Stall started peer=%d\n", staller);
5744  }
5745  }
5746  }
5747 
5748  //
5749  // Message: getdata (transactions)
5750  //
5751  std::vector<std::pair<NodeId, GenTxid>> expired;
5752  auto requestable = m_txrequest.GetRequestable(pto->GetId(), current_time, &expired);
5753  for (const auto& entry : expired) {
5754  LogPrint(BCLog::NET, "timeout of inflight %s %s from peer=%d\n", entry.second.IsWtxid() ? "wtx" : "tx",
5755  entry.second.GetHash().ToString(), entry.first);
5756  }
5757  for (const GenTxid& gtxid : requestable) {
5758  if (!AlreadyHaveTx(gtxid)) {
5759  LogPrint(BCLog::NET, "Requesting %s %s peer=%d\n", gtxid.IsWtxid() ? "wtx" : "tx",
5760  gtxid.GetHash().ToString(), pto->GetId());
5761  vGetData.emplace_back(gtxid.IsWtxid() ? MSG_WTX : (MSG_TX | GetFetchFlags(*peer)), gtxid.GetHash());
5762  if (vGetData.size() >= MAX_GETDATA_SZ) {
5763  m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::GETDATA, vGetData));
5764  vGetData.clear();
5765  }
5766  m_txrequest.RequestedTx(pto->GetId(), gtxid.GetHash(), current_time + GETDATA_TX_INTERVAL);
5767  } else {
5768  // We have already seen this transaction, no need to download. This is just a belt-and-suspenders, as
5769  // this should already be called whenever a transaction becomes AlreadyHaveTx().
5770  m_txrequest.ForgetTxHash(gtxid.GetHash());
5771  }
5772  }
5773 
5774 
5775  if (!vGetData.empty())
5776  m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::GETDATA, vGetData));
5777  } // release cs_main
5778  MaybeSendFeefilter(*pto, *peer, current_time);
5779  return true;
5780 }
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:414
static constexpr unsigned int INVENTORY_BROADCAST_PER_SECOND
Maximum rate of inventory items to send per second.
const char * GETCFILTERS
getcfilters requests compact filters for a range of blocks.
Definition: protocol.cpp:40
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: chain.h:176
RecursiveMutex g_cs_orphans
Guards orphan transactions and extra txs for compact blocks.
Definition: txorphanage.cpp:18
bool IsMsgWtx() const
Definition: protocol.h:487
enum ReadStatus_t ReadStatus
const char * PING
The ping message is sent periodically to help confirm that the receiving peer is still connected...
Definition: protocol.cpp:28
static constexpr auto TXID_RELAY_DELAY
How long to delay requesting transactions via txids, if we have wtxid-relaying peers.
std::string m_type
Definition: net.h:123
BlockFilterIndex is used to store and retrieve block filters, hashes, and headers for a range of bloc...
static constexpr auto INBOUND_INVENTORY_BROADCAST_INTERVAL
Average delay between trickled inventory transmissions for inbound peers.
int ret
const char * FILTERLOAD
The filterload message tells the receiving peer to filter all relayed transactions and requested merk...
Definition: protocol.cpp:31
const char * MERKLEBLOCK
The merkleblock message is a reply to a getdata message which requested a block using the inventory t...
Definition: protocol.cpp:20
std::atomic_bool fPauseSend
Definition: net.h:419
static const int SERIALIZE_TRANSACTION_NO_WITNESS
A flag that is ORed into the protocol version to designate that a transaction should be (un)serialize...
Definition: transaction.h:31
ArgsManager gArgs
Definition: system.cpp:86
invalid by consensus rules
std::chrono::time_point< NodeClock > time_point
Definition: time.h:19
const char * BLOCKTXN
Contains a BlockTransactions.
Definition: protocol.cpp:39
AssertLockHeld(pool.cs)
static GenTxid Wtxid(const uint256 &hash)
Definition: transaction.h:426
bool IsMsgTx() const
Definition: protocol.h:485
static const int MAX_BLOCKS_IN_TRANSIT_PER_PEER
Number of blocks that can be requested at any given time from a single peer.
static constexpr auto UNCONDITIONAL_RELAY_DELAY
How long a transaction has to be in the mempool before it can unconditionally be relayed (even when n...
static constexpr unsigned int INVENTORY_BROADCAST_MAX
Maximum number of inventory items to send per transmission.
static const int MAX_BLOCKTXN_DEPTH
Maximum depth of blocks we&#39;re willing to respond to GETBLOCKTXN requests for.
const char * SENDADDRV2
The sendaddrv2 message signals support for receiving ADDRV2 messages (BIP155).
Definition: protocol.cpp:17
Definition: banman.h:58
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
ReadStatus FillBlock(CBlock &block, const std::vector< CTransactionRef > &vtx_missing)
ServiceFlags
nServices flags
Definition: protocol.h:267
static constexpr size_t MAX_ADDR_PROCESSING_TOKEN_BUCKET
The soft limit of the address processing token bucket (the regular MAX_ADDR_RATE_PER_SECOND based inc...
bool IsLocal() const
Definition: netaddress.cpp:419
#define LogPrint(category,...)
Definition: logging.h:243
int64_t GetBlockTime() const
Definition: chain.h:284
assert(!tx.IsCoinBase())
Describes a place in the block chain to another node such that if the other node doesn&#39;t have the sam...
Definition: block.h:120
int GetVersion() const
Definition: streams.h:278
virtual void BlockDisconnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being disconnected.
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:158
CSipHasher & Write(uint64_t data)
Hash a 64-bit integer worth of data It is treated as if this was the little-endian interpretation of ...
Definition: siphash.cpp:28
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:103
uint64_t m_addr_rate_limited
unsigned int nonce
Definition: miner_tests.cpp:60
virtual void UnitTestMisbehaving(NodeId peer_id, int howmuch)=0
std::atomic_bool fReindex
std::string ToString() const
Definition: protocol.cpp:170
Definition: block.h:68
We don&#39;t have the previous block the checked one is built on.
Data structure to keep track of, and schedule, transaction downloads from peers.
Definition: txrequest.h:96
const char * GETADDR
The getaddr message requests an addr message from the receiving node, preferably one with lots of IP ...
Definition: protocol.cpp:26
bool AddTx(const CTransactionRef &tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
Add a new orphan transaction.
Definition: txorphanage.cpp:20
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
Definition: serialize.h:275
std::atomic_bool m_has_all_wanted_services
Whether this peer provides all services that we want.
Definition: net.h:493
static constexpr auto HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER
int GetType() const
Definition: streams.h:276
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:799
std::vector< uint16_t > indexes
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
bool IsOutboundOrBlockRelayConn() const
Definition: net.h:421
bool IsMsgFilteredBlk() const
Definition: protocol.h:488
const std::chrono::seconds m_connected
Unix epoch time at peer connection.
Definition: net.h:389
virtual void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds)=0
This function is used for testing the stale tip eviction logic, see denialofservice_tests.cpp.
const std::optional< std::list< CTransactionRef > > m_replaced_transactions
Mempool transactions replaced by the tx.
Definition: validation.h:151
static constexpr auto AVG_FEEFILTER_BROADCAST_INTERVAL
Verify that INVENTORY_MAX_RECENT_RELAY is enough to cache everything typically relayed before uncondi...
reverse_range< T > reverse_iterate(T &x)
std::vector< unsigned char > data
Definition: net.h:122
inv message data
Definition: protocol.h:471
invalid proof of work or time too old
const char * SENDCMPCT
Contains a 1-byte bool and 8-byte LE version number.
Definition: protocol.cpp:36
A class to track orphan transactions (failed on TX_MISSING_INPUTS) Since we cannot distinguish orphan...
Definition: txorphanage.h:21
BlockFilterIndex * GetBlockFilterIndex(BlockFilterType filter_type)
Get a block filter index by type.
static constexpr int STALE_RELAY_AGE_LIMIT
Age after which a stale block will no longer be served if requested as protection against fingerprint...
static const unsigned int MIN_BLOCKS_TO_KEEP
Block files containing a block-height within MIN_BLOCKS_TO_KEEP of ActiveChain().Tip() will not be pr...
Definition: validation.h:74
constexpr auto GetRandMillis
Definition: random.h:96
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the last common ancestor two blocks have.
Definition: chain.cpp:165
transaction was missing some of its inputs
bool IsMsgCmpctBlk() const
Definition: protocol.h:489
virtual void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr< const CBlock > &block)
Notifies listeners that a block which builds directly on our current tip has been received and connec...
bool IsFeelerConn() const
Definition: net.h:448
All parent headers found, difficulty matches, timestamp >= median previous, checkpoint.
Definition: chain.h:107
void ignore(size_t num_ignore)
Definition: streams.h:298
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:27
CBlockHeader GetBlockHeader() const
Definition: chain.h:251
static constexpr auto HEADERS_RESPONSE_TIME
How long to wait for a peer to respond to a getheaders request.
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:44
void EraseForPeer(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
Erase all orphans announced by a peer (eg, after that peer disconnects)
Definition: txorphanage.cpp:88
const uint256 & GetHash() const
Definition: transaction.h:428
static constexpr unsigned int DEFAULT_MIN_RELAY_TX_FEE
Default for -minrelaytxfee, minimum relay fee for transactions.
Definition: policy.h:57
const char * CFHEADERS
cfheaders is a response to a getcfheaders request containing a filter header and a vector of filter h...
Definition: protocol.cpp:43
Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid...
Definition: chain.h:114
void SetCommonVersion(int greatest_common_version)
Definition: net.h:563
We&#39;re done syncing with this peer and can discard any remaining state.
static const int BIP0031_VERSION
BIP 0031, pong message, is enabled for all versions AFTER this one.
Definition: version.h:21
RecursiveMutex cs_vProcessMsg
Definition: net.h:378
Defined in BIP152.
Definition: protocol.h:462
arith_uint256 nMinimumChainWork
Minimum work we will assume exists on some valid chain.
Definition: validation.cpp:132
void SetServiceFlagsIBDCache(bool state)
Set the current IBD status in order to figure out the desirable service flags.
Definition: protocol.cpp:134
RollingBloomFilter is a probabilistic "keep track of most recently inserted" set. ...
Definition: bloom.h:108
static constexpr auto AVG_ADDRESS_BROADCAST_INTERVAL
Average delay between peer address broadcasts.
const TxValidationState m_state
Contains information about why the transaction failed.
Definition: validation.h:147
static const int SENDHEADERS_VERSION
"sendheaders" command and announcing blocks with headers starts with this version ...
Definition: version.h:27
static constexpr size_t MAX_PCT_ADDR_TO_SEND
the maximum percentage of addresses from our addrman to return in response to a getaddr message...
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
Definition: chainparams.h:69
violated mempool&#39;s fee/size/descendant/RBF/etc limits
static constexpr auto NONPREF_PEER_TX_DELAY
How long to delay requesting transactions from non-preferred peers.
the block header may be on a too-little-work chain
Mutex m_subver_mutex
Definition: net.h:399
bool IsNull() const
Definition: block.h:141
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:185
inputs (covered by txid) failed policy rules
bool empty() const
Definition: streams.h:238
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:654
const uint32_t MSG_WITNESS_FLAG
getdata message type flags
Definition: protocol.h:448
const ResultType m_result_type
Result type.
Definition: validation.h:144
uint64_t GetLocalNonce() const
Definition: net.h:542
bool SeenLocal(const CService &addr)
vote for a local address
Definition: net.cpp:345
transaction spends a coinbase too early, or violates locktime/sequence locks
void EraseForBlock(const CBlock &block) LOCKS_EXCLUDED(void LimitOrphans(unsigned int max_orphans) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
Erase all orphans included in or invalidated by a new block.
Definition: txorphanage.h:44
static constexpr auto RELAY_TX_CACHE_TIME
How long to cache transactions in mapRelay for normal relay.
const char * CFILTER
cfilter is a response to a getcfilters request containing a single compact filter.
Definition: protocol.cpp:41
bool DeploymentActiveAt(const CBlockIndex &index, const Consensus::Params &params, Consensus::BuriedDeployment dep, [[maybe_unused]] VersionBitsCache &versionbitscache)
Determine if a deployment is active for this block.
static constexpr auto STALE_CHECK_INTERVAL
How frequently to check for stale tips.
const char * PONG
The pong message replies to a ping message, proving to the pinging node that the ponging node is stil...
Definition: protocol.cpp:29
unsigned char * begin()
Definition: uint256.h:61
State
The various states a (txhash,peer) pair can be in.
Definition: txrequest.cpp:38
static constexpr std::chrono::minutes TIMEOUT_INTERVAL
Time after which to disconnect, after waiting for a ping response (or inactivity).
Definition: net.h:57
static constexpr double BLOCK_DOWNLOAD_TIMEOUT_PER_PEER
Additional block download timeout per parallel downloading peer (i.e.
initial value. Tx has not yet been rejected
virtual void BlockChecked(const CBlock &, const BlockValidationState &)
Notifies listeners of a block validation result.
const char * WTXIDRELAY
Indicates that a node prefers to relay transactions via wtxid, rather than txid.
Definition: protocol.cpp:46
static constexpr uint32_t MAX_GETCFILTERS_SIZE
Maximum number of compact filters that may be requested with one getcfilters.
bool IsNull() const
Definition: uint256.h:34
const char * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
Definition: protocol.cpp:24
const char * GETCFCHECKPT
getcfcheckpt requests evenly spaced compact filter headers, enabling parallelized download and valida...
Definition: protocol.cpp:44
PRESYNC means the peer has not yet demonstrated their chain has sufficient work and we&#39;re only buildi...
bool IsRelayable() const
Whether this address should be relayed to other peers even if we can&#39;t reach it ourselves.
Definition: netaddress.h:217
static constexpr int32_t MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT
Protect at least this many outbound peers from disconnection due to slow/ behind headers chain...
const std::vector< CTxIn > vin
Definition: transaction.h:298
const char * INV
The inv message (inventory message) transmits one or more inventories of objects known to the transmi...
Definition: protocol.cpp:18
Stochastic address manager.
Definition: addrman.h:86
NodeClock::time_point GetAdjustedTime()
Definition: timedata.cpp:35
Transaction validation functions.
Definition: params.h:15
static constexpr double BLOCK_DOWNLOAD_TIMEOUT_BASE
Block download timeout base, expressed in multiples of the block interval (i.e.
size_t Size() LOCKS_EXCLUDED(
Return how many entries exist in the orphange.
Definition: txorphanage.h:51
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:88
virtual void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being connected.
bool IsValid() const
Definition: netaddress.cpp:445
int EraseTx(const uint256 &txid) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
Erase an orphan by txid.
Definition: txorphanage.cpp:56
std::function< void(const CAddress &addr, const std::string &msg_type, Span< const unsigned char > data, bool is_incoming)> CaptureMessage
Defaults to CaptureMessageToFile(), but can be overridden by unit tests.
Definition: net.cpp:2898
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static constexpr int ADDRV2_FORMAT
A flag that is ORed into the protocol version to designate that addresses should be serialized in (un...
Definition: netaddress.h:33
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25
uint256 GetBlockHash() const
Definition: chain.h:264
std::string SanitizeString(std::string_view str, int rule)
Remove unsafe chars.
bool IsValid() const
Definition: validation.h:121
static constexpr auto PING_INTERVAL
Time between pings automatically sent out for latency probing and keepalive.
BlockFilterType
Definition: blockfilter.h:89
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:109
GenTxid ToGenTxid(const CInv &inv)
Convert a TX/WITNESS_TX/WTX CInv to a GenTxid.
Definition: protocol.cpp:218
#define LOCK2(cs1, cs2)
Definition: sync.h:262
initial value. Block has not yet been rejected
bool IsGenBlkMsg() const
Definition: protocol.h:497
Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends...
Definition: chain.h:118
void SetAddrLocal(const CService &addrLocalIn) EXCLUSIVE_LOCKS_REQUIRED(!m_addr_local_mutex)
May not be called more than once.
Definition: net.cpp:608
bool HasValidProofOfWork(const std::vector< CBlockHeader > &headers, const Consensus::Params &consensusParams)
Check with the proof of work on each blockheader matches the value in nBits.
static constexpr auto EXTRA_PEER_CHECK_INTERVAL
How frequently to check for extra outbound peers and disconnect.
std::set< CTxMemPoolEntryRef, CompareIteratorByHash > Parents
Definition: txmempool.h:93
static std::unique_ptr< PeerManager > make(CConnman &connman, AddrMan &addrman, BanMan *banman, ChainstateManager &chainman, CTxMemPool &pool, bool ignore_incoming_txs)
std::vector< Byte > ParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
Used to relay blocks as header + vector<merkle branch> to filtered nodes.
Definition: merkleblock.h:124
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.cpp:22
CAmount m_fee_filter_received
virtual void InitializeNode(CNode &node, ServiceFlags our_services)=0
Initialize a peer (setup state, queue any initial messages)
std::optional< CService > GetLocalAddrForPeer(CNode &node)
Returns a local address that we should advertise to this peer.
Definition: net.cpp:233
void AddChildrenToWorkSet(const CTransaction &tx, std::set< uint256 > &orphan_work_set) const EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
Add any orphans that list a particular tx as a parent into a peer&#39;s work set (ie orphans that may hav...
uint64_t m_addr_processed
std::atomic_bool m_bloom_filter_loaded
Whether this peer has loaded a bloom filter.
Definition: net.h:503
size_type size() const
Definition: streams.h:237
Invalid by a change to consensus rules more recent than SegWit.
virtual void ProcessMessage(CNode &pfrom, const std::string &msg_type, CDataStream &vRecv, const std::chrono::microseconds time_received, const std::atomic< bool > &interruptMsgProc)=0
Process a single message from a peer.
bool HaveTx(const GenTxid &gtxid) const LOCKS_EXCLUDED(std::pair< CTransactionRef, NodeId > GetTx(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
Check if we already have an orphan transaction (by txid or wtxid)
Definition: txorphanage.h:32
Scripts & signatures ok. Implies all parents are also at least SCRIPTS.
Definition: chain.h:121
Transaction might have a witness prior to SegWit activation, or witness may have been malleated (whic...
std::chrono::microseconds GetExponentialRand(std::chrono::microseconds now, std::chrono::seconds average_interval)
Return a timestamp in the future sampled from an exponential distribution (https://en.wikipedia.org/wiki/Exponential_distribution).
Definition: random.cpp:709
this block was cached as being invalid and we didn&#39;t store the reason why
std::atomic_bool m_relays_txs
Whether we should relay transactions to this peer (their version message did not include fRelay=false...
Definition: net.h:499
An input of a transaction.
Definition: transaction.h:73
static const unsigned int NODE_NETWORK_LIMITED_MIN_BLOCKS
Minimum blocks required to signal NODE_NETWORK_LIMITED.
static constexpr auto MAX_FEEFILTER_CHANGE_DELAY
Maximum feefilter broadcast delay after significant change.
static bool HasAllDesirableServiceFlags(ServiceFlags services)
A shortcut for (services & GetDesirableServiceFlags(services)) == GetDesirableServiceFlags(services)...
Definition: protocol.h:339
const uint256 & GetWitnessHash() const
Definition: transaction.h:331
#define LOCK(cs)
Definition: sync.h:261
const char * name
Definition: rest.cpp:46
const char * ADDRV2
The addrv2 message relays connection information for peers on the network just like the addr message...
Definition: protocol.cpp:16
const uint256 & GetHash() const
Definition: transaction.h:330
std::string ToString() const
Definition: validation.h:127
the block failed to meet one of our checkpoints
bool IsPeerAddrLocalGood(CNode *pnode)
Definition: net.cpp:226
bool LookupFilterHeader(const CBlockIndex *block_index, uint256 &header_out) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_headers_cache)
Get a single filter header by block.
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:15
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:520
Fast randomness source.
Definition: random.h:142
Transport protocol agnostic message container.
Definition: net.h:229
static constexpr unsigned int INVENTORY_MAX_RECENT_RELAY
The number of most recently announced transactions a peer can request.
int64_t nPowTargetSpacing
Definition: params.h:111
const char * SENDHEADERS
Indicates that a node prefers to receive new block announcements via a "headers" message rather than ...
Definition: protocol.cpp:34
const char * MEMPOOL
The mempool message requests the TXIDs of transactions that the receiving node has verified as valid ...
Definition: protocol.cpp:27
constexpr int64_t count_microseconds(std::chrono::microseconds t)
Definition: time.h:56
static const unsigned int MAX_HEADERS_RESULTS
Number of headers sent in one getheaders result.
static const int SHORT_IDS_BLOCKS_VERSION
short-id-based block download starts with this version
Definition: version.h:33
bool IsProxy(const CNetAddr &addr)
Definition: netbase.cpp:653
bool IsGenTxMsg() const
Definition: protocol.h:493
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
Notifies listeners when the block chain tip advances.
bool IsManualConn() const
Definition: net.h:440
virtual void FinalizeNode(const CNode &node)=0
Handle removal of a peer (clear state)
A CService with information about it as peer.
Definition: protocol.h:354
static constexpr size_t MAX_ADDR_TO_SEND
The maximum number of address records permitted in an ADDR message.
static constexpr uint64_t CMPCTBLOCKS_VERSION
The compactblocks version we support.
std::vector< unsigned char > GetKey() const
Definition: netaddress.cpp:911
static constexpr auto OUTBOUND_INVENTORY_BROADCAST_INTERVAL
Average delay between trickled inventory transmissions for outbound peers.
uint256 hash
Definition: protocol.h:503
Result GetResult() const
Definition: validation.h:124
static constexpr uint64_t RANDOMIZER_ID_ADDRESS_RELAY
SHA256("main address relay")[0:8].
static constexpr uint32_t MAX_GETCFHEADERS_SIZE
Maximum number of cf hashes that may be requested with one getcfheaders.
bool LookupFilterHashRange(int start_height, const CBlockIndex *stop_index, std::vector< uint256 > &hashes_out) const
Get a range of filter hashes between two heights on a chain.
const char * ADDR
The addr (IP address) message relays connection information for peers on the network.
Definition: protocol.cpp:15
bool ExpectServicesFromConn() const
Definition: net.h:460
int64_t presync_height
int64_t NodeId
Definition: net.h:93
Definition: net.h:666
Defined in BIP144.
Definition: protocol.h:463
static const int DISCOURAGEMENT_THRESHOLD
Threshold for marking a node to be discouraged, e.g.
uint64_t Finalize() const
Compute the 64-bit SipHash-2-4 of the data written so far.
Definition: siphash.cpp:76
const char * FILTERCLEAR
The filterclear message tells the receiving peer to remove a previously-set bloom filter...
Definition: protocol.cpp:33
std::string ToString() const
Definition: uint256.cpp:64
CService GetLocalAddress(const CNetAddr &addrPeer)
Definition: net.cpp:209
std::atomic< bool > m_bip152_highbandwidth_to
Definition: net.h:488
std::vector< uint256 > vHave
Definition: block.h:122
NodeId GetId() const
Definition: net.h:538
const char * NOTFOUND
The notfound message is a reply to a getdata message which requested an object the receiving node doe...
Definition: protocol.cpp:30
NodeSeconds nTime
Always included in serialization. The behavior is unspecified if the value is not representable as ui...
Definition: protocol.h:435
const bool m_inbound_onion
Whether this peer is an inbound onion, i.e. connected via our Tor onion service.
Definition: net.h:397
Parameters that influence chain consensus.
Definition: params.h:73
static const int MAX_CMPCTBLOCK_DEPTH
Maximum depth of blocks we&#39;re willing to serve as compact blocks to peers when requested.
void PongReceived(std::chrono::microseconds ping_time)
A ping-pong round trip has completed successfully.
Definition: net.h:595
const char * BLOCK
The block message transmits a single serialized block.
Definition: protocol.cpp:25
std::atomic_bool fDisconnect
Definition: net.h:413
std::string strSubVersion
Subversion as sent to the P2P network in version messages.
Definition: net.cpp:120
const char * FEEFILTER
The feefilter message tells the receiving peer not to inv us any txs which do not meet the specified ...
Definition: protocol.cpp:35
const char * GETCFHEADERS
getcfheaders requests a compact filter header and the filter hashes for a range of blocks...
Definition: protocol.cpp:42
std::atomic< std::chrono::seconds > m_last_tx_time
UNIX epoch time of the last transaction received from this peer that we had not yet seen (e...
Definition: net.h:516
bool IsMsgWitnessBlk() const
Definition: protocol.h:490
Validation result for a single transaction mempool acceptance.
Definition: validation.h:135
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:305
bool IsRoutable() const
Definition: netaddress.cpp:484
#define Assume(val)
Assume is the identity function.
Definition: check.h:86
256-bit unsigned big integer.
bool IsWtxid() const
Definition: transaction.h:427
static bool MayHaveUsefulAddressDB(ServiceFlags services)
Checks if a peer with the given service flags may be capable of having a robust address-storage DB...
Definition: protocol.h:348
static const int MAX_UNCONNECTING_HEADERS
Maximum number of unconnecting headers announcements before DoS score.
constexpr int64_t count_seconds(std::chrono::seconds t)
Definition: time.h:54
std::chrono::microseconds m_ping_wait
Definition: init.h:25
const CAddress addr
Definition: net.h:392
const char * GETBLOCKS
The getblocks message requests an inv message that provides block header hashes starting from a parti...
Definition: protocol.cpp:21
static constexpr int32_t MAX_PEER_TX_ANNOUNCEMENTS
Maximum number of transactions to consider for requesting, per peer.
static constexpr int CFCHECKPT_INTERVAL
Interval between compact filter checkpoints.
bool IsBlockOnlyConn() const
Definition: net.h:444
Transaction is missing a witness.
bool fPruneMode
Pruning-related variables and constants.
if(!SetupNetworking())
bool IsValid(enum BlockStatus nUpTo=BLOCK_VALID_TRANSACTIONS) const EXCLUSIVE_LOCKS_REQUIRED(
Check whether this block index entry is valid up to the passed validity level.
Definition: chain.h:313
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:14
bool IsMsgBlk() const
Definition: protocol.h:486
uint256 GetHash() const
Definition: block.cpp:11
256-bit opaque blob.
Definition: uint256.h:119
void scheduleFromNow(Function f, std::chrono::milliseconds delta) EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
Call f once after the delta has passed.
Definition: scheduler.h:52
ServiceFlags their_services
invalid by consensus rules (excluding any below reasons)
static constexpr auto ROTATE_ADDR_RELAY_DEST_INTERVAL
Delay between rotating the peers we relay a particular address to.
static time_point now() noexcept
Return current system time or mocked time, if set.
Definition: time.cpp:72
bool HasWitness() const
Definition: transaction.h:360
bool IsReachable(enum Network net)
Definition: net.cpp:333
void Shuffle(I first, I last, R &&rng)
More efficient than using std::shuffle on a FastRandomContext.
Definition: random.h:271
static const unsigned int MAX_GETDATA_SZ
Limit to avoid sending big packets.
ServiceFlags nServices
Serialized as uint64_t in V1, and as CompactSize in V2.
Definition: protocol.h:437
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
std::vector< CTransactionRef > vtx
Definition: block.h:72
const char * CMPCTBLOCK
Contains a CBlockHeaderAndShortTxIDs object - providing a header and list of "short txids"...
Definition: protocol.cpp:37
static constexpr auto BLOCK_STALLING_TIMEOUT
Time during which a peer must stall block download progress before being disconnected.
the block&#39;s data didn&#39;t match the data committed to by the PoW
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:431
#define LOCKS_EXCLUDED(...)
Definition: threadsafety.h:48
static constexpr auto HEADERS_DOWNLOAD_TIMEOUT_BASE
Headers download timeout.
virtual void SendPings()=0
Send ping message to all peers.
static const int FEEFILTER_VERSION
"feefilter" tells peers to filter invs to you by fee starts with this version
Definition: version.h:30
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:13
std::vector< std::pair< unsigned int, uint256 > > vMatchedTxn
Public only for unit testing and relay testing (not relayed).
Definition: merkleblock.h:137
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:151
const CChainParams & Params()
Return the currently selected parameters.
static const unsigned int BLOCK_DOWNLOAD_WINDOW
Size of the "block download window": how far ahead of our current height do we fetch? Larger windows tolerate larger download speed differences between peer, but increase the potential degree of disordering of blocks on disk (which make reindexing and pruning harder).
static const int MIN_PEER_PROTO_VERSION
disconnect from peers older than this proto version
Definition: version.h:18
static const unsigned int MAX_INV_SZ
The maximum number of entries in an &#39;inv&#39; protocol message.
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: system.cpp:629
virtual void RelayTransaction(const uint256 &txid, const uint256 &wtxid)=0
Relay transaction to all peers.
bool IsTxAvailable(size_t index) const
static const unsigned int DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN
Default number of orphan+recently-replaced txn to keep around for block reconstruction.
A block this one builds on is invalid.
bool fLogIPs
Definition: logging.cpp:41
#define TRACE6(context, event, a, b, c, d, e, f)
Definition: trace.h:34
ServiceFlags GetDesirableServiceFlags(ServiceFlags services)
Gets the set of service flags which are "desirable" for a given peer.
Definition: protocol.cpp:127
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
Definition: script.h:24
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:121
#define LIMITED_STRING(obj, n)
Definition: serialize.h:438
std::atomic< int64_t > nTimeOffset
Definition: net.h:390
const char * GETDATA
The getdata message requests one or more data objects from another node.
Definition: protocol.cpp:19
bool fListen
Definition: net.cpp:116
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:32
static constexpr auto OVERLOADED_PEER_TX_DELAY
How long to delay requesting transactions from overloaded peers (see MAX_PEER_TX_REQUEST_IN_FLIGHT).
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span
Like the Span constructor, but for (const) unsigned char member types only.
Definition: span.h:285
static constexpr auto CHAIN_SYNC_TIMEOUT
Timeout for (unprotected) outbound peers to sync to our chainwork.
std::atomic_bool fSuccessfullyConnected
fSuccessfullyConnected is set to true on receiving VERACK from the peer.
Definition: net.h:410
SipHash-2-4.
Definition: siphash.h:13
static constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:26
#define AssertLockNotHeld(cs)
Definition: sync.h:148
bool IsInvalid() const
Definition: validation.h:122
static int count
Definition: tests.c:33
static const unsigned int MAX_SUBVERSION_LENGTH
Maximum length of the user agent string in version message.
Definition: net.h:65
std::atomic< int > nVersion
Definition: net.h:398
Invalid by a change to consensus rules more recent than SegWit.
#define GUARDED_BY(x)
Definition: threadsafety.h:38
arith_uint256 CalculateHeadersWork(const std::vector< CBlockHeader > &headers)
Return the sum of the work on a given set of headers.
std::string ConnectionTypeAsString() const
Definition: net.h:592
static size_t RecursiveDynamicUsage(const CScript &script)
Definition: core_memusage.h:12
const char * CFCHECKPT
cfcheckpt is a response to a getcfcheckpt request containing a vector of evenly spaced filter headers...
Definition: protocol.cpp:45
ReadStatus InitData(const CBlockHeaderAndShortTxIDs &cmpctblock, const std::vector< std::pair< uint256, CTransactionRef >> &extra_txn)
this node does not have a mempool so can&#39;t validate the transaction
std::string ToString() const
Definition: netaddress.cpp:933
block timestamp was > 2 hours in the future (or our clock is bad)
static const unsigned int MAX_BLOCKS_TO_ANNOUNCE
Maximum number of headers to announce when relaying blocks with headers message.
std::atomic< bool > m_bip152_highbandwidth_from
Definition: net.h:490
CBlockLocator GetLocator(const CBlockIndex *index)
Get a locator for a block index entry.
Definition: chain.cpp:50
bool ReadRawBlockFromDisk(std::vector< uint8_t > &block, const FlatFilePos &pos, const CMessageHeader::MessageStartChars &message_start)
bool IsAddrFetchConn() const
Definition: net.h:452
HeadersSyncState:
Definition: headerssync.h:101
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:131
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:96
const char * TX
The tx message transmits a single transaction.
Definition: protocol.cpp:23
static const unsigned int MAX_LOCATOR_SZ
The maximum number of entries in a locator.
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:287
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:164
FlatFilePos GetBlockPos() const EXCLUSIVE_LOCKS_REQUIRED(
Definition: chain.h:229
Information about a peer.
Definition: net.h:347
static constexpr auto MINIMUM_CONNECT_TIME
Minimum time an outbound-peer-eviction candidate must be connected for, in order to evict...
std::vector< int > vHeightInFlight
std::atomic< std::chrono::seconds > m_last_block_time
UNIX epoch time of the last block received from this peer that we had not yet seen (e...
Definition: net.h:510
Simple class for background tasks that should be run periodically or once "after a while"...
Definition: scheduler.h:38
static constexpr int32_t MAX_PEER_TX_REQUEST_IN_FLIGHT
Maximum number of in-flight transaction requests from a peer.
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: chain.cpp:120
full block available in blk*.dat
Definition: chain.h:127
std::atomic_bool fImporting
void AddTimeData(const CNetAddr &ip, int64_t nOffsetSample)
Definition: timedata.cpp:46
#define LogPrintf(...)
Definition: logging.h:234
int64_t GetTime()
DEPRECATED, see GetTime.
Definition: time.cpp:117
static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS
Default for -maxorphantx, maximum number of orphan transactions kept in memory.
Defined in BIP 339.
Definition: protocol.h:459
int GetCommonVersion() const
Definition: net.h:568
static const int INVALID_CB_NO_BAN_VERSION
not banning for invalid compact blocks starts with this version
Definition: version.h:36
const std::string m_addr_name
Definition: net.h:395
COutPoint prevout
Definition: transaction.h:76
std::atomic_bool fPauseRecv
Definition: net.h:418
bool IsAddrV1Compatible() const
Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
Definition: netaddress.cpp:499
static const int WTXID_RELAY_VERSION
"wtxidrelay" command for wtxid-based relay starts with this version
Definition: version.h:39
bool HasPermission(NetPermissionFlags permission) const
Definition: net.h:406
bool IsInboundConn() const
Definition: net.h:456
static constexpr int HISTORICAL_BLOCK_AGE
Age after which a block is considered historical for purposes of rate limiting block relay...
bool LookupFilterRange(int start_height, const CBlockIndex *stop_index, std::vector< BlockFilter > &filters_out) const
Get a range of filters between two heights on a chain.
static constexpr double MAX_ADDR_RATE_PER_SECOND
The maximum rate of address records we&#39;re willing to process on average.
Tx already in mempool or conflicts with a tx in the chain (if it conflicts with another tx in mempool...
otherwise didn&#39;t meet our local policy rules
void scheduleEvery(Function f, std::chrono::milliseconds delta) EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
Repeat f until the scheduler is stopped.
Definition: scheduler.cpp:110
A generic txid reference (txid or wtxid).
Definition: transaction.h:418
bool HaveTxsDownloaded() const
Check whether this block&#39;s and all previous blocks&#39; transactions have been downloaded (and stored to ...
Definition: chain.h:277
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:213
static constexpr auto AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL
Average delay between local address broadcasts.
unsigned int nTx
Number of transactions in this block.
Definition: chain.h:183
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:21
static GenTxid Txid(const uint256 &hash)
Definition: transaction.h:425
int in_avail() const
Definition: streams.h:273
static constexpr auto GETDATA_TX_INTERVAL
How long to wait before downloading a transaction from an additional peer.
virtual bool ProcessMessages(CNode *pnode, std::atomic< bool > &interrupt)=0
Process protocol messages received from a given node.
const char * FILTERADD
The filteradd message tells the receiving peer to add a single element to a previously-set bloom filt...
Definition: protocol.cpp:32
bool IsFullOutboundConn() const
Definition: net.h:436
#define Assert(val)
Identity function.
Definition: check.h:74
int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, const CBlockIndex &from, const CBlockIndex &tip, const Consensus::Params &params)
Return the time it would take to redo the work difference between from and to, assuming the current h...
Definition: chain.cpp:146
#define PT_GUARDED_BY(x)
Definition: threadsafety.h:39
const std::string & BlockFilterTypeName(BlockFilterType filter_type)
Get the human-readable name for a filter type.
const char * GETBLOCKTXN
Contains a BlockTransactionsRequest Peer should respond with "blocktxn" message.
Definition: protocol.cpp:38
virtual bool SendMessages(CNode *pnode) EXCLUSIVE_LOCKS_REQUIRED(pnode -> cs_sendProcessing)=0
Send queued protocol messages to a given node.
uint256 hash
Definition: transaction.h:37