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