Bitcoin Core  0.19.99
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 <chainparams.h>
12 #include <consensus/validation.h>
13 #include <hash.h>
14 #include <validation.h>
15 #include <merkleblock.h>
16 #include <netmessagemaker.h>
17 #include <netbase.h>
18 #include <policy/fees.h>
19 #include <policy/policy.h>
20 #include <primitives/block.h>
21 #include <primitives/transaction.h>
22 #include <random.h>
23 #include <reverse_iterator.h>
24 #include <scheduler.h>
25 #include <tinyformat.h>
26 #include <txmempool.h>
27 #include <util/system.h>
28 #include <util/strencodings.h>
29 #include <util/validation.h>
30 
31 #include <memory>
32 #include <typeinfo>
33 
34 #if defined(NDEBUG)
35 # error "Bitcoin cannot be compiled without assertions."
36 #endif
37 
39 static constexpr int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60;
41 static constexpr int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60;
43 static constexpr std::chrono::seconds RELAY_TX_CACHE_TIME{15 * 60};
46 static constexpr int64_t HEADERS_DOWNLOAD_TIMEOUT_BASE = 15 * 60 * 1000000; // 15 minutes
47 static constexpr int64_t HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER = 1000; // 1ms/header
51 static constexpr int32_t MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT = 4;
53 static constexpr int64_t CHAIN_SYNC_TIMEOUT = 20 * 60; // 20 minutes
55 static constexpr int64_t STALE_CHECK_INTERVAL = 10 * 60; // 10 minutes
57 static constexpr int64_t EXTRA_PEER_CHECK_INTERVAL = 45;
59 static constexpr int64_t MINIMUM_CONNECT_TIME = 30;
61 static constexpr uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL;
64 static constexpr int STALE_RELAY_AGE_LIMIT = 30 * 24 * 60 * 60;
67 static constexpr int HISTORICAL_BLOCK_AGE = 7 * 24 * 60 * 60;
69 static constexpr int32_t MAX_PEER_TX_IN_FLIGHT = 100;
71 static constexpr int32_t MAX_PEER_TX_ANNOUNCEMENTS = 2 * MAX_INV_SZ;
73 static constexpr std::chrono::microseconds INBOUND_PEER_TX_DELAY{std::chrono::seconds{2}};
75 static constexpr std::chrono::microseconds GETDATA_TX_INTERVAL{std::chrono::seconds{60}};
77 static constexpr std::chrono::microseconds MAX_GETDATA_RANDOM_DELAY{std::chrono::seconds{2}};
79 static constexpr std::chrono::microseconds TX_EXPIRY_INTERVAL{GETDATA_TX_INTERVAL * 10};
81 "To preserve security, MAX_GETDATA_RANDOM_DELAY should not exceed INBOUND_PEER_DELAY");
83 static const unsigned int MAX_GETDATA_SZ = 1000;
84 
85 
86 struct COrphanTx {
87  // When modifying, adapt the copy of this definition in tests/DoS_tests.
90  int64_t nTimeExpire;
91  size_t list_pos;
92 };
94 std::map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY(g_cs_orphans);
95 
96 void EraseOrphansFor(NodeId peer);
97 
99 void Misbehaving(NodeId nodeid, int howmuch, const std::string& message="") EXCLUSIVE_LOCKS_REQUIRED(cs_main);
100 
102 static constexpr unsigned int AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL = 24 * 60 * 60;
104 static const unsigned int AVG_ADDRESS_BROADCAST_INTERVAL = 30;
107 static const unsigned int INVENTORY_BROADCAST_INTERVAL = 5;
110 static constexpr unsigned int INVENTORY_BROADCAST_MAX = 7 * INVENTORY_BROADCAST_INTERVAL;
112 static constexpr unsigned int AVG_FEEFILTER_BROADCAST_INTERVAL = 10 * 60;
114 static constexpr unsigned int MAX_FEEFILTER_CHANGE_DELAY = 5 * 60;
115 
116 // Internal stuff
117 namespace {
119  int nSyncStarted GUARDED_BY(cs_main) = 0;
120 
127  std::map<uint256, std::pair<NodeId, bool>> mapBlockSource GUARDED_BY(cs_main);
128 
148  std::unique_ptr<CRollingBloomFilter> recentRejects GUARDED_BY(cs_main);
149  uint256 hashRecentRejectsChainTip GUARDED_BY(cs_main);
150 
151  /*
152  * Filter for transactions that have been recently confirmed.
153  * We use this to avoid requesting transactions that have already been
154  * confirnmed.
155  */
156  RecursiveMutex g_cs_recent_confirmed_transactions;
157  std::unique_ptr<CRollingBloomFilter> g_recent_confirmed_transactions GUARDED_BY(g_cs_recent_confirmed_transactions);
158 
160  struct QueuedBlock {
161  uint256 hash;
162  const CBlockIndex* pindex;
163  bool fValidatedHeaders;
164  std::unique_ptr<PartiallyDownloadedBlock> partialBlock;
165  };
166  std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> > mapBlocksInFlight GUARDED_BY(cs_main);
167 
169  std::list<NodeId> lNodesAnnouncingHeaderAndIDs GUARDED_BY(cs_main);
170 
172  int nPreferredDownload GUARDED_BY(cs_main) = 0;
173 
175  int nPeersWithValidatedDownloads GUARDED_BY(cs_main) = 0;
176 
178  int g_outbound_peers_with_protect_from_disconnect GUARDED_BY(cs_main) = 0;
179 
181  std::atomic<int64_t> g_last_tip_update(0);
182 
184  typedef std::map<uint256, CTransactionRef> MapRelay;
185  MapRelay mapRelay GUARDED_BY(cs_main);
187  std::deque<std::pair<int64_t, MapRelay::iterator>> vRelayExpiration GUARDED_BY(cs_main);
188 
189  struct IteratorComparator
190  {
191  template<typename I>
192  bool operator()(const I& a, const I& b) const
193  {
194  return &(*a) < &(*b);
195  }
196  };
197  std::map<COutPoint, std::set<std::map<uint256, COrphanTx>::iterator, IteratorComparator>> mapOrphanTransactionsByPrev GUARDED_BY(g_cs_orphans);
198 
199  std::vector<std::map<uint256, COrphanTx>::iterator> g_orphan_list GUARDED_BY(g_cs_orphans);
200 
201  static size_t vExtraTxnForCompactIt GUARDED_BY(g_cs_orphans) = 0;
202  static std::vector<std::pair<uint256, CTransactionRef>> vExtraTxnForCompact GUARDED_BY(g_cs_orphans);
203 } // namespace
204 
205 namespace {
212 struct CNodeState {
214  const CService address;
216  bool fCurrentlyConnected;
218  int nMisbehavior;
220  bool fShouldBan;
222  const std::string name;
224  const CBlockIndex *pindexBestKnownBlock;
226  uint256 hashLastUnknownBlock;
228  const CBlockIndex *pindexLastCommonBlock;
230  const CBlockIndex *pindexBestHeaderSent;
232  int nUnconnectingHeaders;
234  bool fSyncStarted;
236  int64_t nHeadersSyncTimeout;
238  int64_t nStallingSince;
239  std::list<QueuedBlock> vBlocksInFlight;
241  int64_t nDownloadingSince;
242  int nBlocksInFlight;
243  int nBlocksInFlightValidHeaders;
245  bool fPreferredDownload;
247  bool fPreferHeaders;
249  bool fPreferHeaderAndIDs;
255  bool fProvidesHeaderAndIDs;
257  bool fHaveWitness;
259  bool fWantsCmpctWitness;
264  bool fSupportsDesiredCmpctVersion;
265 
280  struct ChainSyncTimeoutState {
282  int64_t m_timeout;
284  const CBlockIndex * m_work_header;
286  bool m_sent_getheaders;
288  bool m_protect;
289  };
290 
291  ChainSyncTimeoutState m_chain_sync;
292 
294  int64_t m_last_block_announcement;
295 
296  /*
297  * State associated with transaction download.
298  *
299  * Tx download algorithm:
300  *
301  * When inv comes in, queue up (process_time, txid) inside the peer's
302  * CNodeState (m_tx_process_time) as long as m_tx_announced for the peer
303  * isn't too big (MAX_PEER_TX_ANNOUNCEMENTS).
304  *
305  * The process_time for a transaction is set to nNow for outbound peers,
306  * nNow + 2 seconds for inbound peers. This is the time at which we'll
307  * consider trying to request the transaction from the peer in
308  * SendMessages(). The delay for inbound peers is to allow outbound peers
309  * a chance to announce before we request from inbound peers, to prevent
310  * an adversary from using inbound connections to blind us to a
311  * transaction (InvBlock).
312  *
313  * When we call SendMessages() for a given peer,
314  * we will loop over the transactions in m_tx_process_time, looking
315  * at the transactions whose process_time <= nNow. We'll request each
316  * such transaction that we don't have already and that hasn't been
317  * requested from another peer recently, up until we hit the
318  * MAX_PEER_TX_IN_FLIGHT limit for the peer. Then we'll update
319  * g_already_asked_for for each requested txid, storing the time of the
320  * GETDATA request. We use g_already_asked_for to coordinate transaction
321  * requests amongst our peers.
322  *
323  * For transactions that we still need but we have already recently
324  * requested from some other peer, we'll reinsert (process_time, txid)
325  * back into the peer's m_tx_process_time at the point in the future at
326  * which the most recent GETDATA request would time out (ie
327  * GETDATA_TX_INTERVAL + the request time stored in g_already_asked_for).
328  * We add an additional delay for inbound peers, again to prefer
329  * attempting download from outbound peers first.
330  * We also add an extra small random delay up to 2 seconds
331  * to avoid biasing some peers over others. (e.g., due to fixed ordering
332  * of peer processing in ThreadMessageHandler).
333  *
334  * When we receive a transaction from a peer, we remove the txid from the
335  * peer's m_tx_in_flight set and from their recently announced set
336  * (m_tx_announced). We also clear g_already_asked_for for that entry, so
337  * that if somehow the transaction is not accepted but also not added to
338  * the reject filter, then we will eventually redownload from other
339  * peers.
340  */
341  struct TxDownloadState {
342  /* Track when to attempt download of announced transactions (process
343  * time in micros -> txid)
344  */
345  std::multimap<std::chrono::microseconds, uint256> m_tx_process_time;
346 
348  std::set<uint256> m_tx_announced;
349 
351  std::map<uint256, std::chrono::microseconds> m_tx_in_flight;
352 
354  std::chrono::microseconds m_check_expiry_timer{0};
355  };
356 
357  TxDownloadState m_tx_download;
358 
360  bool m_is_inbound;
361 
363  bool m_is_manual_connection;
364 
365  CNodeState(CAddress addrIn, std::string addrNameIn, bool is_inbound, bool is_manual) :
366  address(addrIn), name(std::move(addrNameIn)), m_is_inbound(is_inbound),
367  m_is_manual_connection (is_manual)
368  {
369  fCurrentlyConnected = false;
370  nMisbehavior = 0;
371  fShouldBan = false;
372  pindexBestKnownBlock = nullptr;
373  hashLastUnknownBlock.SetNull();
374  pindexLastCommonBlock = nullptr;
375  pindexBestHeaderSent = nullptr;
376  nUnconnectingHeaders = 0;
377  fSyncStarted = false;
378  nHeadersSyncTimeout = 0;
379  nStallingSince = 0;
380  nDownloadingSince = 0;
381  nBlocksInFlight = 0;
382  nBlocksInFlightValidHeaders = 0;
383  fPreferredDownload = false;
384  fPreferHeaders = false;
385  fPreferHeaderAndIDs = false;
386  fProvidesHeaderAndIDs = false;
387  fHaveWitness = false;
388  fWantsCmpctWitness = false;
389  fSupportsDesiredCmpctVersion = false;
390  m_chain_sync = { 0, nullptr, false, false };
391  m_last_block_announcement = 0;
392  }
393 };
394 
395 // Keeps track of the time (in microseconds) when transactions were requested last time
397 
399 static std::map<NodeId, CNodeState> mapNodeState GUARDED_BY(cs_main);
400 
401 static CNodeState *State(NodeId pnode) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
402  std::map<NodeId, CNodeState>::iterator it = mapNodeState.find(pnode);
403  if (it == mapNodeState.end())
404  return nullptr;
405  return &it->second;
406 }
407 
408 static void UpdatePreferredDownload(CNode* node, CNodeState* state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
409 {
410  nPreferredDownload -= state->fPreferredDownload;
411 
412  // Whether this node should be marked as a preferred download node.
413  state->fPreferredDownload = (!node->fInbound || node->HasPermission(PF_NOBAN)) && !node->fOneShot && !node->fClient;
414 
415  nPreferredDownload += state->fPreferredDownload;
416 }
417 
418 static void PushNodeVersion(CNode *pnode, CConnman* connman, int64_t nTime)
419 {
420  // Note that pnode->GetLocalServices() is a reflection of the local
421  // services we were offering when the CNode object was created for this
422  // peer.
423  ServiceFlags nLocalNodeServices = pnode->GetLocalServices();
424  uint64_t nonce = pnode->GetLocalNonce();
425  int nNodeStartingHeight = pnode->GetMyStartingHeight();
426  NodeId nodeid = pnode->GetId();
427  CAddress addr = pnode->addr;
428 
429  CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService(), addr.nServices));
430  CAddress addrMe = CAddress(CService(), nLocalNodeServices);
431 
432  connman->PushMessage(pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t)nLocalNodeServices, nTime, addrYou, addrMe,
433  nonce, strSubVersion, nNodeStartingHeight, ::g_relay_txes && pnode->m_tx_relay != nullptr));
434 
435  if (fLogIPs) {
436  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);
437  } else {
438  LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), nodeid);
439  }
440 }
441 
442 // Returns a bool indicating whether we requested this block.
443 // Also used if a block was /not/ received and timed out or started with another peer
444 static bool MarkBlockAsReceived(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
445  std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
446  if (itInFlight != mapBlocksInFlight.end()) {
447  CNodeState *state = State(itInFlight->second.first);
448  assert(state != nullptr);
449  state->nBlocksInFlightValidHeaders -= itInFlight->second.second->fValidatedHeaders;
450  if (state->nBlocksInFlightValidHeaders == 0 && itInFlight->second.second->fValidatedHeaders) {
451  // Last validated block on the queue was received.
452  nPeersWithValidatedDownloads--;
453  }
454  if (state->vBlocksInFlight.begin() == itInFlight->second.second) {
455  // First block on the queue was received, update the start download time for the next one
456  state->nDownloadingSince = std::max(state->nDownloadingSince, GetTimeMicros());
457  }
458  state->vBlocksInFlight.erase(itInFlight->second.second);
459  state->nBlocksInFlight--;
460  state->nStallingSince = 0;
461  mapBlocksInFlight.erase(itInFlight);
462  return true;
463  }
464  return false;
465 }
466 
467 // returns false, still setting pit, if the block was already in flight from the same peer
468 // pit will only be valid as long as the same cs_main lock is being held
469 static bool MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const CBlockIndex* pindex = nullptr, std::list<QueuedBlock>::iterator** pit = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
470  CNodeState *state = State(nodeid);
471  assert(state != nullptr);
472 
473  // Short-circuit most stuff in case it is from the same node
474  std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
475  if (itInFlight != mapBlocksInFlight.end() && itInFlight->second.first == nodeid) {
476  if (pit) {
477  *pit = &itInFlight->second.second;
478  }
479  return false;
480  }
481 
482  // Make sure it's not listed somewhere already.
483  MarkBlockAsReceived(hash);
484 
485  std::list<QueuedBlock>::iterator it = state->vBlocksInFlight.insert(state->vBlocksInFlight.end(),
486  {hash, pindex, pindex != nullptr, std::unique_ptr<PartiallyDownloadedBlock>(pit ? new PartiallyDownloadedBlock(&mempool) : nullptr)});
487  state->nBlocksInFlight++;
488  state->nBlocksInFlightValidHeaders += it->fValidatedHeaders;
489  if (state->nBlocksInFlight == 1) {
490  // We're starting a block download (batch) from this peer.
491  state->nDownloadingSince = GetTimeMicros();
492  }
493  if (state->nBlocksInFlightValidHeaders == 1 && pindex != nullptr) {
494  nPeersWithValidatedDownloads++;
495  }
496  itInFlight = mapBlocksInFlight.insert(std::make_pair(hash, std::make_pair(nodeid, it))).first;
497  if (pit)
498  *pit = &itInFlight->second.second;
499  return true;
500 }
501 
503 static void ProcessBlockAvailability(NodeId nodeid) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
504  CNodeState *state = State(nodeid);
505  assert(state != nullptr);
506 
507  if (!state->hashLastUnknownBlock.IsNull()) {
508  const CBlockIndex* pindex = LookupBlockIndex(state->hashLastUnknownBlock);
509  if (pindex && pindex->nChainWork > 0) {
510  if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork) {
511  state->pindexBestKnownBlock = pindex;
512  }
513  state->hashLastUnknownBlock.SetNull();
514  }
515  }
516 }
517 
519 static void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
520  CNodeState *state = State(nodeid);
521  assert(state != nullptr);
522 
523  ProcessBlockAvailability(nodeid);
524 
525  const CBlockIndex* pindex = LookupBlockIndex(hash);
526  if (pindex && pindex->nChainWork > 0) {
527  // An actually better block was announced.
528  if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork) {
529  state->pindexBestKnownBlock = pindex;
530  }
531  } else {
532  // An unknown block was announced; just assume that the latest one is the best one.
533  state->hashLastUnknownBlock = hash;
534  }
535 }
536 
543 static void MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid, CConnman* connman) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
544 {
546  CNodeState* nodestate = State(nodeid);
547  if (!nodestate || !nodestate->fSupportsDesiredCmpctVersion) {
548  // Never ask from peers who can't provide witnesses.
549  return;
550  }
551  if (nodestate->fProvidesHeaderAndIDs) {
552  for (std::list<NodeId>::iterator it = lNodesAnnouncingHeaderAndIDs.begin(); it != lNodesAnnouncingHeaderAndIDs.end(); it++) {
553  if (*it == nodeid) {
554  lNodesAnnouncingHeaderAndIDs.erase(it);
555  lNodesAnnouncingHeaderAndIDs.push_back(nodeid);
556  return;
557  }
558  }
559  connman->ForNode(nodeid, [connman](CNode* pfrom){
561  uint64_t nCMPCTBLOCKVersion = (pfrom->GetLocalServices() & NODE_WITNESS) ? 2 : 1;
562  if (lNodesAnnouncingHeaderAndIDs.size() >= 3) {
563  // As per BIP152, we only get 3 of our peers to announce
564  // blocks using compact encodings.
565  connman->ForNode(lNodesAnnouncingHeaderAndIDs.front(), [connman, nCMPCTBLOCKVersion](CNode* pnodeStop){
567  connman->PushMessage(pnodeStop, CNetMsgMaker(pnodeStop->GetSendVersion()).Make(NetMsgType::SENDCMPCT, /*fAnnounceUsingCMPCTBLOCK=*/false, nCMPCTBLOCKVersion));
568  return true;
569  });
570  lNodesAnnouncingHeaderAndIDs.pop_front();
571  }
572  connman->PushMessage(pfrom, CNetMsgMaker(pfrom->GetSendVersion()).Make(NetMsgType::SENDCMPCT, /*fAnnounceUsingCMPCTBLOCK=*/true, nCMPCTBLOCKVersion));
573  lNodesAnnouncingHeaderAndIDs.push_back(pfrom->GetId());
574  return true;
575  });
576  }
577 }
578 
579 static bool TipMayBeStale(const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
580 {
582  if (g_last_tip_update == 0) {
583  g_last_tip_update = GetTime();
584  }
585  return g_last_tip_update < GetTime() - consensusParams.nPowTargetSpacing * 3 && mapBlocksInFlight.empty();
586 }
587 
588 static bool CanDirectFetch(const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
589 {
590  return ::ChainActive().Tip()->GetBlockTime() > GetAdjustedTime() - consensusParams.nPowTargetSpacing * 20;
591 }
592 
593 static bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
594 {
595  if (state->pindexBestKnownBlock && pindex == state->pindexBestKnownBlock->GetAncestor(pindex->nHeight))
596  return true;
597  if (state->pindexBestHeaderSent && pindex == state->pindexBestHeaderSent->GetAncestor(pindex->nHeight))
598  return true;
599  return false;
600 }
601 
604 static void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
605 {
606  if (count == 0)
607  return;
608 
609  vBlocks.reserve(vBlocks.size() + count);
610  CNodeState *state = State(nodeid);
611  assert(state != nullptr);
612 
613  // Make sure pindexBestKnownBlock is up to date, we'll need it.
614  ProcessBlockAvailability(nodeid);
615 
616  if (state->pindexBestKnownBlock == nullptr || state->pindexBestKnownBlock->nChainWork < ::ChainActive().Tip()->nChainWork || state->pindexBestKnownBlock->nChainWork < nMinimumChainWork) {
617  // This peer has nothing interesting.
618  return;
619  }
620 
621  if (state->pindexLastCommonBlock == nullptr) {
622  // Bootstrap quickly by guessing a parent of our best tip is the forking point.
623  // Guessing wrong in either direction is not a problem.
624  state->pindexLastCommonBlock = ::ChainActive()[std::min(state->pindexBestKnownBlock->nHeight, ::ChainActive().Height())];
625  }
626 
627  // If the peer reorganized, our previous pindexLastCommonBlock may not be an ancestor
628  // of its current tip anymore. Go back enough to fix that.
629  state->pindexLastCommonBlock = LastCommonAncestor(state->pindexLastCommonBlock, state->pindexBestKnownBlock);
630  if (state->pindexLastCommonBlock == state->pindexBestKnownBlock)
631  return;
632 
633  std::vector<const CBlockIndex*> vToFetch;
634  const CBlockIndex *pindexWalk = state->pindexLastCommonBlock;
635  // Never fetch further than the best block we know the peer has, or more than BLOCK_DOWNLOAD_WINDOW + 1 beyond the last
636  // linked block we have in common with this peer. The +1 is so we can detect stalling, namely if we would be able to
637  // download that next block if the window were 1 larger.
638  int nWindowEnd = state->pindexLastCommonBlock->nHeight + BLOCK_DOWNLOAD_WINDOW;
639  int nMaxHeight = std::min<int>(state->pindexBestKnownBlock->nHeight, nWindowEnd + 1);
640  NodeId waitingfor = -1;
641  while (pindexWalk->nHeight < nMaxHeight) {
642  // Read up to 128 (or more, if more blocks than that are needed) successors of pindexWalk (towards
643  // pindexBestKnownBlock) into vToFetch. We fetch 128, because CBlockIndex::GetAncestor may be as expensive
644  // as iterating over ~100 CBlockIndex* entries anyway.
645  int nToFetch = std::min(nMaxHeight - pindexWalk->nHeight, std::max<int>(count - vBlocks.size(), 128));
646  vToFetch.resize(nToFetch);
647  pindexWalk = state->pindexBestKnownBlock->GetAncestor(pindexWalk->nHeight + nToFetch);
648  vToFetch[nToFetch - 1] = pindexWalk;
649  for (unsigned int i = nToFetch - 1; i > 0; i--) {
650  vToFetch[i - 1] = vToFetch[i]->pprev;
651  }
652 
653  // Iterate over those blocks in vToFetch (in forward direction), adding the ones that
654  // are not yet downloaded and not in flight to vBlocks. In the meantime, update
655  // pindexLastCommonBlock as long as all ancestors are already downloaded, or if it's
656  // already part of our chain (and therefore don't need it even if pruned).
657  for (const CBlockIndex* pindex : vToFetch) {
658  if (!pindex->IsValid(BLOCK_VALID_TREE)) {
659  // We consider the chain that this peer is on invalid.
660  return;
661  }
662  if (!State(nodeid)->fHaveWitness && IsWitnessEnabled(pindex->pprev, consensusParams)) {
663  // We wouldn't download this block or its descendants from this peer.
664  return;
665  }
666  if (pindex->nStatus & BLOCK_HAVE_DATA || ::ChainActive().Contains(pindex)) {
667  if (pindex->HaveTxsDownloaded())
668  state->pindexLastCommonBlock = pindex;
669  } else if (mapBlocksInFlight.count(pindex->GetBlockHash()) == 0) {
670  // The block is not already downloaded, and not yet in flight.
671  if (pindex->nHeight > nWindowEnd) {
672  // We reached the end of the window.
673  if (vBlocks.size() == 0 && waitingfor != nodeid) {
674  // We aren't able to fetch anything, but we would be if the download window was one larger.
675  nodeStaller = waitingfor;
676  }
677  return;
678  }
679  vBlocks.push_back(pindex);
680  if (vBlocks.size() == count) {
681  return;
682  }
683  } else if (waitingfor == -1) {
684  // This is the first already-in-flight block.
685  waitingfor = mapBlocksInFlight[pindex->GetBlockHash()].first;
686  }
687  }
688  }
689 }
690 
691 void EraseTxRequest(const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
692 {
693  g_already_asked_for.erase(txid);
694 }
695 
696 std::chrono::microseconds GetTxRequestTime(const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
697 {
698  auto it = g_already_asked_for.find(txid);
699  if (it != g_already_asked_for.end()) {
700  return it->second;
701  }
702  return {};
703 }
704 
705 void UpdateTxRequestTime(const uint256& txid, std::chrono::microseconds request_time) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
706 {
707  auto it = g_already_asked_for.find(txid);
708  if (it == g_already_asked_for.end()) {
709  g_already_asked_for.insert(std::make_pair(txid, request_time));
710  } else {
711  g_already_asked_for.update(it, request_time);
712  }
713 }
714 
715 std::chrono::microseconds CalculateTxGetDataTime(const uint256& txid, std::chrono::microseconds current_time, bool use_inbound_delay) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
716 {
717  std::chrono::microseconds process_time;
718  const auto last_request_time = GetTxRequestTime(txid);
719  // First time requesting this tx
720  if (last_request_time.count() == 0) {
721  process_time = current_time;
722  } else {
723  // Randomize the delay to avoid biasing some peers over others (such as due to
724  // fixed ordering of peer processing in ThreadMessageHandler)
725  process_time = last_request_time + GETDATA_TX_INTERVAL + GetRandMicros(MAX_GETDATA_RANDOM_DELAY);
726  }
727 
728  // We delay processing announcements from inbound peers
729  if (use_inbound_delay) process_time += INBOUND_PEER_TX_DELAY;
730 
731  return process_time;
732 }
733 
734 void RequestTx(CNodeState* state, const uint256& txid, std::chrono::microseconds current_time) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
735 {
736  CNodeState::TxDownloadState& peer_download_state = state->m_tx_download;
737  if (peer_download_state.m_tx_announced.size() >= MAX_PEER_TX_ANNOUNCEMENTS ||
738  peer_download_state.m_tx_process_time.size() >= MAX_PEER_TX_ANNOUNCEMENTS ||
739  peer_download_state.m_tx_announced.count(txid)) {
740  // Too many queued announcements from this peer, or we already have
741  // this announcement
742  return;
743  }
744  peer_download_state.m_tx_announced.insert(txid);
745 
746  // Calculate the time to try requesting this transaction. Use
747  // fPreferredDownload as a proxy for outbound peers.
748  const auto process_time = CalculateTxGetDataTime(txid, current_time, !state->fPreferredDownload);
749 
750  peer_download_state.m_tx_process_time.emplace(process_time, txid);
751 }
752 
753 } // namespace
754 
755 // This function is used for testing the stale tip eviction logic, see
756 // denialofservice_tests.cpp
757 void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds)
758 {
759  LOCK(cs_main);
760  CNodeState *state = State(node);
761  if (state) state->m_last_block_announcement = time_in_seconds;
762 }
763 
764 // Returns true for outbound peers, excluding manual connections, feelers, and
765 // one-shots.
766 static bool IsOutboundDisconnectionCandidate(const CNode *node)
767 {
768  return !(node->fInbound || node->m_manual_connection || node->fFeeler || node->fOneShot);
769 }
770 
772  CAddress addr = pnode->addr;
773  std::string addrName = pnode->GetAddrName();
774  NodeId nodeid = pnode->GetId();
775  {
776  LOCK(cs_main);
777  mapNodeState.emplace_hint(mapNodeState.end(), std::piecewise_construct, std::forward_as_tuple(nodeid), std::forward_as_tuple(addr, std::move(addrName), pnode->fInbound, pnode->m_manual_connection));
778  }
779  if(!pnode->fInbound)
780  PushNodeVersion(pnode, connman, GetTime());
781 }
782 
783 void PeerLogicValidation::FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTime) {
784  fUpdateConnectionTime = false;
785  LOCK(cs_main);
786  CNodeState *state = State(nodeid);
787  assert(state != nullptr);
788 
789  if (state->fSyncStarted)
790  nSyncStarted--;
791 
792  if (state->nMisbehavior == 0 && state->fCurrentlyConnected) {
793  fUpdateConnectionTime = true;
794  }
795 
796  for (const QueuedBlock& entry : state->vBlocksInFlight) {
797  mapBlocksInFlight.erase(entry.hash);
798  }
799  EraseOrphansFor(nodeid);
800  nPreferredDownload -= state->fPreferredDownload;
801  nPeersWithValidatedDownloads -= (state->nBlocksInFlightValidHeaders != 0);
802  assert(nPeersWithValidatedDownloads >= 0);
803  g_outbound_peers_with_protect_from_disconnect -= state->m_chain_sync.m_protect;
804  assert(g_outbound_peers_with_protect_from_disconnect >= 0);
805 
806  mapNodeState.erase(nodeid);
807 
808  if (mapNodeState.empty()) {
809  // Do a consistency check after the last peer is removed.
810  assert(mapBlocksInFlight.empty());
811  assert(nPreferredDownload == 0);
812  assert(nPeersWithValidatedDownloads == 0);
813  assert(g_outbound_peers_with_protect_from_disconnect == 0);
814  }
815  LogPrint(BCLog::NET, "Cleared nodestate for peer=%d\n", nodeid);
816 }
817 
819  LOCK(cs_main);
820  CNodeState *state = State(nodeid);
821  if (state == nullptr)
822  return false;
823  stats.nMisbehavior = state->nMisbehavior;
824  stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
825  stats.nCommonHeight = state->pindexLastCommonBlock ? state->pindexLastCommonBlock->nHeight : -1;
826  for (const QueuedBlock& queue : state->vBlocksInFlight) {
827  if (queue.pindex)
828  stats.vHeightInFlight.push_back(queue.pindex->nHeight);
829  }
830  return true;
831 }
832 
834 //
835 // mapOrphanTransactions
836 //
837 
839 {
840  size_t max_extra_txn = gArgs.GetArg("-blockreconstructionextratxn", DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN);
841  if (max_extra_txn <= 0)
842  return;
843  if (!vExtraTxnForCompact.size())
844  vExtraTxnForCompact.resize(max_extra_txn);
845  vExtraTxnForCompact[vExtraTxnForCompactIt] = std::make_pair(tx->GetWitnessHash(), tx);
846  vExtraTxnForCompactIt = (vExtraTxnForCompactIt + 1) % max_extra_txn;
847 }
848 
850 {
851  const uint256& hash = tx->GetHash();
852  if (mapOrphanTransactions.count(hash))
853  return false;
854 
855  // Ignore big transactions, to avoid a
856  // send-big-orphans memory exhaustion attack. If a peer has a legitimate
857  // large transaction with a missing parent then we assume
858  // it will rebroadcast it later, after the parent transaction(s)
859  // have been mined or received.
860  // 100 orphans, each of which is at most 100,000 bytes big is
861  // at most 10 megabytes of orphans and somewhat more byprev index (in the worst case):
862  unsigned int sz = GetTransactionWeight(*tx);
863  if (sz > MAX_STANDARD_TX_WEIGHT)
864  {
865  LogPrint(BCLog::MEMPOOL, "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString());
866  return false;
867  }
868 
869  auto ret = mapOrphanTransactions.emplace(hash, COrphanTx{tx, peer, GetTime() + ORPHAN_TX_EXPIRE_TIME, g_orphan_list.size()});
870  assert(ret.second);
871  g_orphan_list.push_back(ret.first);
872  for (const CTxIn& txin : tx->vin) {
873  mapOrphanTransactionsByPrev[txin.prevout].insert(ret.first);
874  }
875 
877 
878  LogPrint(BCLog::MEMPOOL, "stored orphan tx %s (mapsz %u outsz %u)\n", hash.ToString(),
879  mapOrphanTransactions.size(), mapOrphanTransactionsByPrev.size());
880  return true;
881 }
882 
883 int static EraseOrphanTx(uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
884 {
885  std::map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.find(hash);
886  if (it == mapOrphanTransactions.end())
887  return 0;
888  for (const CTxIn& txin : it->second.tx->vin)
889  {
890  auto itPrev = mapOrphanTransactionsByPrev.find(txin.prevout);
891  if (itPrev == mapOrphanTransactionsByPrev.end())
892  continue;
893  itPrev->second.erase(it);
894  if (itPrev->second.empty())
895  mapOrphanTransactionsByPrev.erase(itPrev);
896  }
897 
898  size_t old_pos = it->second.list_pos;
899  assert(g_orphan_list[old_pos] == it);
900  if (old_pos + 1 != g_orphan_list.size()) {
901  // Unless we're deleting the last entry in g_orphan_list, move the last
902  // entry to the position we're deleting.
903  auto it_last = g_orphan_list.back();
904  g_orphan_list[old_pos] = it_last;
905  it_last->second.list_pos = old_pos;
906  }
907  g_orphan_list.pop_back();
908 
909  mapOrphanTransactions.erase(it);
910  return 1;
911 }
912 
914 {
915  LOCK(g_cs_orphans);
916  int nErased = 0;
917  std::map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
918  while (iter != mapOrphanTransactions.end())
919  {
920  std::map<uint256, COrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
921  if (maybeErase->second.fromPeer == peer)
922  {
923  nErased += EraseOrphanTx(maybeErase->second.tx->GetHash());
924  }
925  }
926  if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx from peer=%d\n", nErased, peer);
927 }
928 
929 
930 unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
931 {
932  LOCK(g_cs_orphans);
933 
934  unsigned int nEvicted = 0;
935  static int64_t nNextSweep;
936  int64_t nNow = GetTime();
937  if (nNextSweep <= nNow) {
938  // Sweep out expired orphan pool entries:
939  int nErased = 0;
940  int64_t nMinExpTime = nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL;
941  std::map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
942  while (iter != mapOrphanTransactions.end())
943  {
944  std::map<uint256, COrphanTx>::iterator maybeErase = iter++;
945  if (maybeErase->second.nTimeExpire <= nNow) {
946  nErased += EraseOrphanTx(maybeErase->second.tx->GetHash());
947  } else {
948  nMinExpTime = std::min(maybeErase->second.nTimeExpire, nMinExpTime);
949  }
950  }
951  // Sweep again 5 minutes after the next entry that expires in order to batch the linear scan.
952  nNextSweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL;
953  if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx due to expiration\n", nErased);
954  }
955  FastRandomContext rng;
956  while (mapOrphanTransactions.size() > nMaxOrphans)
957  {
958  // Evict a random orphan:
959  size_t randompos = rng.randrange(g_orphan_list.size());
960  EraseOrphanTx(g_orphan_list[randompos]->first);
961  ++nEvicted;
962  }
963  return nEvicted;
964 }
965 
969 void Misbehaving(NodeId pnode, int howmuch, const std::string& message) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
970 {
971  if (howmuch == 0)
972  return;
973 
974  CNodeState *state = State(pnode);
975  if (state == nullptr)
976  return;
977 
978  state->nMisbehavior += howmuch;
979  int banscore = gArgs.GetArg("-banscore", DEFAULT_BANSCORE_THRESHOLD);
980  std::string message_prefixed = message.empty() ? "" : (": " + message);
981  if (state->nMisbehavior >= banscore && state->nMisbehavior - howmuch < banscore)
982  {
983  LogPrint(BCLog::NET, "%s: %s peer=%d (%d -> %d) BAN THRESHOLD EXCEEDED%s\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior, message_prefixed);
984  state->fShouldBan = true;
985  } else
986  LogPrint(BCLog::NET, "%s: %s peer=%d (%d -> %d)%s\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior, message_prefixed);
987 }
988 
996 }
997 
1008 static bool MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationState& state, bool via_compact_block, const std::string& message = "") {
1009  switch (state.GetResult()) {
1011  break;
1012  // The node is providing invalid data:
1015  if (!via_compact_block) {
1016  LOCK(cs_main);
1017  Misbehaving(nodeid, 100, message);
1018  return true;
1019  }
1020  break;
1022  {
1023  LOCK(cs_main);
1024  CNodeState *node_state = State(nodeid);
1025  if (node_state == nullptr) {
1026  break;
1027  }
1028 
1029  // Ban outbound (but not inbound) peers if on an invalid chain.
1030  // Exempt HB compact block peers and manual connections.
1031  if (!via_compact_block && !node_state->m_is_inbound && !node_state->m_is_manual_connection) {
1032  Misbehaving(nodeid, 100, message);
1033  return true;
1034  }
1035  break;
1036  }
1040  {
1041  LOCK(cs_main);
1042  Misbehaving(nodeid, 100, message);
1043  }
1044  return true;
1045  // Conflicting (but not necessarily invalid) data or different policy:
1047  {
1048  // TODO: Handle this much more gracefully (10 DoS points is super arbitrary)
1049  LOCK(cs_main);
1050  Misbehaving(nodeid, 10, message);
1051  }
1052  return true;
1055  break;
1056  }
1057  if (message != "") {
1058  LogPrint(BCLog::NET, "peer=%d: %s\n", nodeid, message);
1059  }
1060  return false;
1061 }
1062 
1070 static bool MaybePunishNodeForTx(NodeId nodeid, const TxValidationState& state, const std::string& message = "") {
1071  switch (state.GetResult()) {
1073  break;
1074  // The node is providing invalid data:
1076  {
1077  LOCK(cs_main);
1078  Misbehaving(nodeid, 100, message);
1079  return true;
1080  }
1081  // Conflicting (but not necessarily invalid) data or different policy:
1089  break;
1090  }
1091  if (message != "") {
1092  LogPrint(BCLog::NET, "peer=%d: %s\n", nodeid, message);
1093  }
1094  return false;
1095 }
1096 
1097 
1098 
1099 
1100 
1101 
1102 
1104 //
1105 // blockchain -> download logic notification
1106 //
1107 
1108 // To prevent fingerprinting attacks, only send blocks/headers outside of the
1109 // active chain if they are no more than a month older (both in time, and in
1110 // best equivalent proof of work) than the best header chain we know about and
1111 // we fully-validated them at some point.
1112 static bool BlockRequestAllowed(const CBlockIndex* pindex, const Consensus::Params& consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1113 {
1115  if (::ChainActive().Contains(pindex)) return true;
1116  return pindex->IsValid(BLOCK_VALID_SCRIPTS) && (pindexBestHeader != nullptr) &&
1117  (pindexBestHeader->GetBlockTime() - pindex->GetBlockTime() < STALE_RELAY_AGE_LIMIT) &&
1119 }
1120 
1122  : connman(connmanIn), m_banman(banman), m_stale_tip_check_time(0)
1123 {
1124  // Initialize global variables that cannot be constructed at startup.
1125  recentRejects.reset(new CRollingBloomFilter(120000, 0.000001));
1126 
1127  // Blocks don't typically have more than 4000 transactions, so this should
1128  // be at least six blocks (~1 hr) worth of transactions that we can store.
1129  // If the number of transactions appearing in a block goes up, or if we are
1130  // seeing getdata requests more than an hour after initial announcement, we
1131  // can increase this number.
1132  // The false positive rate of 1/1M should come out to less than 1
1133  // transaction per day that would be inadvertently ignored (which is the
1134  // same probability that we have in the reject filter).
1135  g_recent_confirmed_transactions.reset(new CRollingBloomFilter(24000, 0.000001));
1136 
1137  const Consensus::Params& consensusParams = Params().GetConsensus();
1138  // Stale tip checking and peer eviction are on two different timers, but we
1139  // don't want them to get out of sync due to drift in the scheduler, so we
1140  // combine them in one function and schedule at the quicker (peer-eviction)
1141  // timer.
1142  static_assert(EXTRA_PEER_CHECK_INTERVAL < STALE_CHECK_INTERVAL, "peer eviction timer should be less than stale tip check timer");
1143  scheduler.scheduleEvery(std::bind(&PeerLogicValidation::CheckForStaleTipAndEvictPeers, this, consensusParams), EXTRA_PEER_CHECK_INTERVAL * 1000);
1144 }
1145 
1150 void PeerLogicValidation::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex, const std::vector<CTransactionRef>& vtxConflicted)
1151 {
1152  {
1153  LOCK(g_cs_orphans);
1154 
1155  std::vector<uint256> vOrphanErase;
1156 
1157  for (const CTransactionRef& ptx : pblock->vtx) {
1158  const CTransaction& tx = *ptx;
1159 
1160  // Which orphan pool entries must we evict?
1161  for (const auto& txin : tx.vin) {
1162  auto itByPrev = mapOrphanTransactionsByPrev.find(txin.prevout);
1163  if (itByPrev == mapOrphanTransactionsByPrev.end()) continue;
1164  for (auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); ++mi) {
1165  const CTransaction& orphanTx = *(*mi)->second.tx;
1166  const uint256& orphanHash = orphanTx.GetHash();
1167  vOrphanErase.push_back(orphanHash);
1168  }
1169  }
1170  }
1171 
1172  // Erase orphan transactions included or precluded by this block
1173  if (vOrphanErase.size()) {
1174  int nErased = 0;
1175  for (const uint256& orphanHash : vOrphanErase) {
1176  nErased += EraseOrphanTx(orphanHash);
1177  }
1178  LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx included or conflicted by block\n", nErased);
1179  }
1180 
1181  g_last_tip_update = GetTime();
1182  }
1183  {
1184  LOCK(g_cs_recent_confirmed_transactions);
1185  for (const auto& ptx : pblock->vtx) {
1186  g_recent_confirmed_transactions->insert(ptx->GetHash());
1187  }
1188  }
1189 }
1190 
1191 void PeerLogicValidation::BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex)
1192 {
1193  // To avoid relay problems with transactions that were previously
1194  // confirmed, clear our filter of recently confirmed transactions whenever
1195  // there's a reorg.
1196  // This means that in a 1-block reorg (where 1 block is disconnected and
1197  // then another block reconnected), our filter will drop to having only one
1198  // block's worth of transactions in it, but that should be fine, since
1199  // presumably the most common case of relaying a confirmed transaction
1200  // should be just after a new block containing it is found.
1201  LOCK(g_cs_recent_confirmed_transactions);
1202  g_recent_confirmed_transactions->reset();
1203 }
1204 
1205 // All of the following cache a recent block, and are protected by cs_most_recent_block
1207 static std::shared_ptr<const CBlock> most_recent_block GUARDED_BY(cs_most_recent_block);
1208 static std::shared_ptr<const CBlockHeaderAndShortTxIDs> most_recent_compact_block GUARDED_BY(cs_most_recent_block);
1209 static uint256 most_recent_block_hash GUARDED_BY(cs_most_recent_block);
1210 static bool fWitnessesPresentInMostRecentCompactBlock GUARDED_BY(cs_most_recent_block);
1211 
1216 void PeerLogicValidation::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) {
1217  std::shared_ptr<const CBlockHeaderAndShortTxIDs> pcmpctblock = std::make_shared<const CBlockHeaderAndShortTxIDs> (*pblock, true);
1218  const CNetMsgMaker msgMaker(PROTOCOL_VERSION);
1219 
1220  LOCK(cs_main);
1221 
1222  static int nHighestFastAnnounce = 0;
1223  if (pindex->nHeight <= nHighestFastAnnounce)
1224  return;
1225  nHighestFastAnnounce = pindex->nHeight;
1226 
1227  bool fWitnessEnabled = IsWitnessEnabled(pindex->pprev, Params().GetConsensus());
1228  uint256 hashBlock(pblock->GetHash());
1229 
1230  {
1231  LOCK(cs_most_recent_block);
1232  most_recent_block_hash = hashBlock;
1233  most_recent_block = pblock;
1234  most_recent_compact_block = pcmpctblock;
1235  fWitnessesPresentInMostRecentCompactBlock = fWitnessEnabled;
1236  }
1237 
1238  connman->ForEachNode([this, &pcmpctblock, pindex, &msgMaker, fWitnessEnabled, &hashBlock](CNode* pnode) {
1240 
1241  // TODO: Avoid the repeated-serialization here
1242  if (pnode->nVersion < INVALID_CB_NO_BAN_VERSION || pnode->fDisconnect)
1243  return;
1244  ProcessBlockAvailability(pnode->GetId());
1245  CNodeState &state = *State(pnode->GetId());
1246  // If the peer has, or we announced to them the previous block already,
1247  // but we don't think they have this one, go ahead and announce it
1248  if (state.fPreferHeaderAndIDs && (!fWitnessEnabled || state.fWantsCmpctWitness) &&
1249  !PeerHasHeader(&state, pindex) && PeerHasHeader(&state, pindex->pprev)) {
1250 
1251  LogPrint(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", "PeerLogicValidation::NewPoWValidBlock",
1252  hashBlock.ToString(), pnode->GetId());
1253  connman->PushMessage(pnode, msgMaker.Make(NetMsgType::CMPCTBLOCK, *pcmpctblock));
1254  state.pindexBestHeaderSent = pindex;
1255  }
1256  });
1257 }
1258 
1263 void PeerLogicValidation::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
1264  const int nNewHeight = pindexNew->nHeight;
1265  connman->SetBestHeight(nNewHeight);
1266 
1267  SetServiceFlagsIBDCache(!fInitialDownload);
1268  if (!fInitialDownload) {
1269  // Find the hashes of all blocks that weren't previously in the best chain.
1270  std::vector<uint256> vHashes;
1271  const CBlockIndex *pindexToAnnounce = pindexNew;
1272  while (pindexToAnnounce != pindexFork) {
1273  vHashes.push_back(pindexToAnnounce->GetBlockHash());
1274  pindexToAnnounce = pindexToAnnounce->pprev;
1275  if (vHashes.size() == MAX_BLOCKS_TO_ANNOUNCE) {
1276  // Limit announcements in case of a huge reorganization.
1277  // Rely on the peer's synchronization mechanism in that case.
1278  break;
1279  }
1280  }
1281  // Relay inventory, but don't relay old inventory during initial block download.
1282  connman->ForEachNode([nNewHeight, &vHashes](CNode* pnode) {
1283  if (nNewHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : 0)) {
1284  for (const uint256& hash : reverse_iterate(vHashes)) {
1285  pnode->PushBlockHash(hash);
1286  }
1287  }
1288  });
1290  }
1291 }
1292 
1298  LOCK(cs_main);
1299 
1300  const uint256 hash(block.GetHash());
1301  std::map<uint256, std::pair<NodeId, bool>>::iterator it = mapBlockSource.find(hash);
1302 
1303  // If the block failed validation, we know where it came from and we're still connected
1304  // to that peer, maybe punish.
1305  if (state.IsInvalid() &&
1306  it != mapBlockSource.end() &&
1307  State(it->second.first)) {
1308  MaybePunishNodeForBlock(/*nodeid=*/ it->second.first, state, /*via_compact_block=*/ !it->second.second);
1309  }
1310  // Check that:
1311  // 1. The block is valid
1312  // 2. We're not in initial block download
1313  // 3. This is currently the best block we're aware of. We haven't updated
1314  // the tip yet so we have no way to check this directly here. Instead we
1315  // just check that there are currently no other blocks in flight.
1316  else if (state.IsValid() &&
1318  mapBlocksInFlight.count(hash) == mapBlocksInFlight.size()) {
1319  if (it != mapBlockSource.end()) {
1320  MaybeSetPeerAsAnnouncingHeaderAndIDs(it->second.first, connman);
1321  }
1322  }
1323  if (it != mapBlockSource.end())
1324  mapBlockSource.erase(it);
1325 }
1326 
1328 //
1329 // Messages
1330 //
1331 
1332 
1334 {
1335  switch (inv.type)
1336  {
1337  case MSG_TX:
1338  case MSG_WITNESS_TX:
1339  {
1340  assert(recentRejects);
1341  if (::ChainActive().Tip()->GetBlockHash() != hashRecentRejectsChainTip)
1342  {
1343  // If the chain tip has changed previously rejected transactions
1344  // might be now valid, e.g. due to a nLockTime'd tx becoming valid,
1345  // or a double-spend. Reset the rejects filter and give those
1346  // txs a second chance.
1347  hashRecentRejectsChainTip = ::ChainActive().Tip()->GetBlockHash();
1348  recentRejects->reset();
1349  }
1350 
1351  {
1352  LOCK(g_cs_orphans);
1353  if (mapOrphanTransactions.count(inv.hash)) return true;
1354  }
1355 
1356  {
1357  LOCK(g_cs_recent_confirmed_transactions);
1358  if (g_recent_confirmed_transactions->contains(inv.hash)) return true;
1359  }
1360 
1361  return recentRejects->contains(inv.hash) ||
1362  mempool.exists(inv.hash);
1363  }
1364  case MSG_BLOCK:
1365  case MSG_WITNESS_BLOCK:
1366  return LookupBlockIndex(inv.hash) != nullptr;
1367  }
1368  // Don't know what it is, just say we already got one
1369  return true;
1370 }
1371 
1372 void RelayTransaction(const uint256& txid, const CConnman& connman)
1373 {
1374  CInv inv(MSG_TX, txid);
1375  connman.ForEachNode([&inv](CNode* pnode)
1376  {
1377  pnode->PushInventory(inv);
1378  });
1379 }
1380 
1381 static void RelayAddress(const CAddress& addr, bool fReachable, CConnman* connman)
1382 {
1383  unsigned int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
1384 
1385  // Relay to a limited number of other nodes
1386  // Use deterministic randomness to send to the same nodes for 24 hours
1387  // at a time so the m_addr_knowns of the chosen nodes prevent repeats
1388  uint64_t hashAddr = addr.GetHash();
1389  const CSipHasher hasher = connman->GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));
1390  FastRandomContext insecure_rand;
1391 
1392  std::array<std::pair<uint64_t, CNode*>,2> best{{{0, nullptr}, {0, nullptr}}};
1393  assert(nRelayNodes <= best.size());
1394 
1395  auto sortfunc = [&best, &hasher, nRelayNodes](CNode* pnode) {
1396  if (pnode->nVersion >= CADDR_TIME_VERSION && pnode->IsAddrRelayPeer()) {
1397  uint64_t hashKey = CSipHasher(hasher).Write(pnode->GetId()).Finalize();
1398  for (unsigned int i = 0; i < nRelayNodes; i++) {
1399  if (hashKey > best[i].first) {
1400  std::copy(best.begin() + i, best.begin() + nRelayNodes - 1, best.begin() + i + 1);
1401  best[i] = std::make_pair(hashKey, pnode);
1402  break;
1403  }
1404  }
1405  }
1406  };
1407 
1408  auto pushfunc = [&addr, &best, nRelayNodes, &insecure_rand] {
1409  for (unsigned int i = 0; i < nRelayNodes && best[i].first != 0; i++) {
1410  best[i].second->PushAddress(addr, insecure_rand);
1411  }
1412  };
1413 
1414  connman->ForEachNodeThen(std::move(sortfunc), std::move(pushfunc));
1415 }
1416 
1417 void static ProcessGetBlockData(CNode* pfrom, const CChainParams& chainparams, const CInv& inv, CConnman* connman)
1418 {
1419  bool send = false;
1420  std::shared_ptr<const CBlock> a_recent_block;
1421  std::shared_ptr<const CBlockHeaderAndShortTxIDs> a_recent_compact_block;
1422  bool fWitnessesPresentInARecentCompactBlock;
1423  const Consensus::Params& consensusParams = chainparams.GetConsensus();
1424  {
1425  LOCK(cs_most_recent_block);
1426  a_recent_block = most_recent_block;
1427  a_recent_compact_block = most_recent_compact_block;
1428  fWitnessesPresentInARecentCompactBlock = fWitnessesPresentInMostRecentCompactBlock;
1429  }
1430 
1431  bool need_activate_chain = false;
1432  {
1433  LOCK(cs_main);
1434  const CBlockIndex* pindex = LookupBlockIndex(inv.hash);
1435  if (pindex) {
1436  if (pindex->HaveTxsDownloaded() && !pindex->IsValid(BLOCK_VALID_SCRIPTS) &&
1437  pindex->IsValid(BLOCK_VALID_TREE)) {
1438  // If we have the block and all of its parents, but have not yet validated it,
1439  // we might be in the middle of connecting it (ie in the unlock of cs_main
1440  // before ActivateBestChain but after AcceptBlock).
1441  // In this case, we need to run ActivateBestChain prior to checking the relay
1442  // conditions below.
1443  need_activate_chain = true;
1444  }
1445  }
1446  } // release cs_main before calling ActivateBestChain
1447  if (need_activate_chain) {
1448  BlockValidationState state;
1449  if (!ActivateBestChain(state, Params(), a_recent_block)) {
1450  LogPrint(BCLog::NET, "failed to activate chain (%s)\n", FormatStateMessage(state));
1451  }
1452  }
1453 
1454  LOCK(cs_main);
1455  const CBlockIndex* pindex = LookupBlockIndex(inv.hash);
1456  if (pindex) {
1457  send = BlockRequestAllowed(pindex, consensusParams);
1458  if (!send) {
1459  LogPrint(BCLog::NET, "%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom->GetId());
1460  }
1461  }
1462  const CNetMsgMaker msgMaker(pfrom->GetSendVersion());
1463  // disconnect node in case we have reached the outbound limit for serving historical blocks
1464  // never disconnect whitelisted nodes
1465  if (send && connman->OutboundTargetReached(true) && ( ((pindexBestHeader != nullptr) && (pindexBestHeader->GetBlockTime() - pindex->GetBlockTime() > HISTORICAL_BLOCK_AGE)) || inv.type == MSG_FILTERED_BLOCK) && !pfrom->HasPermission(PF_NOBAN))
1466  {
1467  LogPrint(BCLog::NET, "historical block serving limit reached, disconnect peer=%d\n", pfrom->GetId());
1468 
1469  //disconnect node
1470  pfrom->fDisconnect = true;
1471  send = false;
1472  }
1473  // Avoid leaking prune-height by never sending blocks below the NODE_NETWORK_LIMITED threshold
1474  if (send && !pfrom->HasPermission(PF_NOBAN) && (
1475  (((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 */) )
1476  )) {
1477  LogPrint(BCLog::NET, "Ignore block request below NODE_NETWORK_LIMITED threshold from peer=%d\n", pfrom->GetId());
1478 
1479  //disconnect node and prevent it from stalling (would otherwise wait for the missing block)
1480  pfrom->fDisconnect = true;
1481  send = false;
1482  }
1483  // Pruned nodes may have deleted the block, so check whether
1484  // it's available before trying to send.
1485  if (send && (pindex->nStatus & BLOCK_HAVE_DATA))
1486  {
1487  std::shared_ptr<const CBlock> pblock;
1488  if (a_recent_block && a_recent_block->GetHash() == pindex->GetBlockHash()) {
1489  pblock = a_recent_block;
1490  } else if (inv.type == MSG_WITNESS_BLOCK) {
1491  // Fast-path: in this case it is possible to serve the block directly from disk,
1492  // as the network format matches the format on disk
1493  std::vector<uint8_t> block_data;
1494  if (!ReadRawBlockFromDisk(block_data, pindex, chainparams.MessageStart())) {
1495  assert(!"cannot load block from disk");
1496  }
1497  connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::BLOCK, MakeSpan(block_data)));
1498  // Don't set pblock as we've sent the block
1499  } else {
1500  // Send block from disk
1501  std::shared_ptr<CBlock> pblockRead = std::make_shared<CBlock>();
1502  if (!ReadBlockFromDisk(*pblockRead, pindex, consensusParams))
1503  assert(!"cannot load block from disk");
1504  pblock = pblockRead;
1505  }
1506  if (pblock) {
1507  if (inv.type == MSG_BLOCK)
1508  connman->PushMessage(pfrom, msgMaker.Make(SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::BLOCK, *pblock));
1509  else if (inv.type == MSG_WITNESS_BLOCK)
1510  connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::BLOCK, *pblock));
1511  else if (inv.type == MSG_FILTERED_BLOCK)
1512  {
1513  bool sendMerkleBlock = false;
1514  CMerkleBlock merkleBlock;
1515  if (pfrom->m_tx_relay != nullptr) {
1516  LOCK(pfrom->m_tx_relay->cs_filter);
1517  if (pfrom->m_tx_relay->pfilter) {
1518  sendMerkleBlock = true;
1519  merkleBlock = CMerkleBlock(*pblock, *pfrom->m_tx_relay->pfilter);
1520  }
1521  }
1522  if (sendMerkleBlock) {
1523  connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::MERKLEBLOCK, merkleBlock));
1524  // CMerkleBlock just contains hashes, so also push any transactions in the block the client did not see
1525  // This avoids hurting performance by pointlessly requiring a round-trip
1526  // Note that there is currently no way for a node to request any single transactions we didn't send here -
1527  // they must either disconnect and retry or request the full block.
1528  // Thus, the protocol spec specified allows for us to provide duplicate txn here,
1529  // however we MUST always provide at least what the remote peer needs
1530  typedef std::pair<unsigned int, uint256> PairType;
1531  for (PairType& pair : merkleBlock.vMatchedTxn)
1532  connman->PushMessage(pfrom, msgMaker.Make(SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::TX, *pblock->vtx[pair.first]));
1533  }
1534  // else
1535  // no response
1536  }
1537  else if (inv.type == MSG_CMPCT_BLOCK)
1538  {
1539  // If a peer is asking for old blocks, we're almost guaranteed
1540  // they won't have a useful mempool to match against a compact block,
1541  // and we don't feel like constructing the object for them, so
1542  // instead we respond with the full, non-compact block.
1543  bool fPeerWantsWitness = State(pfrom->GetId())->fWantsCmpctWitness;
1544  int nSendFlags = fPeerWantsWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
1545  if (CanDirectFetch(consensusParams) && pindex->nHeight >= ::ChainActive().Height() - MAX_CMPCTBLOCK_DEPTH) {
1546  if ((fPeerWantsWitness || !fWitnessesPresentInARecentCompactBlock) && a_recent_compact_block && a_recent_compact_block->header.GetHash() == pindex->GetBlockHash()) {
1547  connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, *a_recent_compact_block));
1548  } else {
1549  CBlockHeaderAndShortTxIDs cmpctblock(*pblock, fPeerWantsWitness);
1550  connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
1551  }
1552  } else {
1553  connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::BLOCK, *pblock));
1554  }
1555  }
1556  }
1557 
1558  // Trigger the peer node to send a getblocks request for the next batch of inventory
1559  if (inv.hash == pfrom->hashContinue)
1560  {
1561  // Bypass PushInventory, this must send even if redundant,
1562  // and we want it right after the last block so they don't
1563  // wait for other stuff first.
1564  std::vector<CInv> vInv;
1565  vInv.push_back(CInv(MSG_BLOCK, ::ChainActive().Tip()->GetBlockHash()));
1566  connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::INV, vInv));
1567  pfrom->hashContinue.SetNull();
1568  }
1569  }
1570 }
1571 
1572 void static ProcessGetData(CNode* pfrom, const CChainParams& chainparams, CConnman* connman, const std::atomic<bool>& interruptMsgProc) LOCKS_EXCLUDED(cs_main)
1573 {
1575 
1576  std::deque<CInv>::iterator it = pfrom->vRecvGetData.begin();
1577  std::vector<CInv> vNotFound;
1578  const CNetMsgMaker msgMaker(pfrom->GetSendVersion());
1579 
1580  // Note that if we receive a getdata for a MSG_TX or MSG_WITNESS_TX from a
1581  // block-relay-only outbound peer, we will stop processing further getdata
1582  // messages from this peer (likely resulting in our peer eventually
1583  // disconnecting us).
1584  if (pfrom->m_tx_relay != nullptr) {
1585  // mempool entries added before this time have likely expired from mapRelay
1586  const std::chrono::seconds longlived_mempool_time = GetTime<std::chrono::seconds>() - RELAY_TX_CACHE_TIME;
1587  const std::chrono::seconds mempool_req = pfrom->m_tx_relay->m_last_mempool_req.load();
1588 
1589  LOCK(cs_main);
1590 
1591  while (it != pfrom->vRecvGetData.end() && (it->type == MSG_TX || it->type == MSG_WITNESS_TX)) {
1592  if (interruptMsgProc)
1593  return;
1594  // Don't bother if send buffer is too full to respond anyway
1595  if (pfrom->fPauseSend)
1596  break;
1597 
1598  const CInv &inv = *it;
1599  it++;
1600 
1601  // Send stream from relay memory
1602  bool push = false;
1603  auto mi = mapRelay.find(inv.hash);
1604  int nSendFlags = (inv.type == MSG_TX ? SERIALIZE_TRANSACTION_NO_WITNESS : 0);
1605  if (mi != mapRelay.end()) {
1606  connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *mi->second));
1607  push = true;
1608  } else {
1609  auto txinfo = mempool.info(inv.hash);
1610  // To protect privacy, do not answer getdata using the mempool when
1611  // that TX couldn't have been INVed in reply to a MEMPOOL request,
1612  // or when it's too recent to have expired from mapRelay.
1613  if (txinfo.tx && (
1614  (mempool_req.count() && txinfo.m_time <= mempool_req)
1615  || (txinfo.m_time <= longlived_mempool_time)))
1616  {
1617  connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *txinfo.tx));
1618  push = true;
1619  }
1620  }
1621  if (!push) {
1622  vNotFound.push_back(inv);
1623  }
1624  }
1625  } // release cs_main
1626 
1627  if (it != pfrom->vRecvGetData.end() && !pfrom->fPauseSend) {
1628  const CInv &inv = *it;
1629  if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK || inv.type == MSG_CMPCT_BLOCK || inv.type == MSG_WITNESS_BLOCK) {
1630  it++;
1631  ProcessGetBlockData(pfrom, chainparams, inv, connman);
1632  }
1633  }
1634 
1635  // Unknown types in the GetData stay in vRecvGetData and block any future
1636  // message from this peer, see vRecvGetData check in ProcessMessages().
1637  // Depending on future p2p changes, we might either drop unknown getdata on
1638  // the floor or disconnect the peer.
1639 
1640  pfrom->vRecvGetData.erase(pfrom->vRecvGetData.begin(), it);
1641 
1642  if (!vNotFound.empty()) {
1643  // Let the peer know that we didn't find what it asked for, so it doesn't
1644  // have to wait around forever.
1645  // SPV clients care about this message: it's needed when they are
1646  // recursively walking the dependencies of relevant unconfirmed
1647  // transactions. SPV clients want to do that because they want to know
1648  // about (and store and rebroadcast and risk analyze) the dependencies
1649  // of transactions relevant to them, without having to download the
1650  // entire memory pool.
1651  // Also, other nodes can use these messages to automatically request a
1652  // transaction from some other peer that annnounced it, and stop
1653  // waiting for us to respond.
1654  // In normal operation, we often send NOTFOUND messages for parents of
1655  // transactions that we relay; if a peer is missing a parent, they may
1656  // assume we have them and request the parents from us.
1657  connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::NOTFOUND, vNotFound));
1658  }
1659 }
1660 
1662  uint32_t nFetchFlags = 0;
1663  if ((pfrom->GetLocalServices() & NODE_WITNESS) && State(pfrom->GetId())->fHaveWitness) {
1664  nFetchFlags |= MSG_WITNESS_FLAG;
1665  }
1666  return nFetchFlags;
1667 }
1668 
1669 inline void static SendBlockTransactions(const CBlock& block, const BlockTransactionsRequest& req, CNode* pfrom, CConnman* connman) {
1670  BlockTransactions resp(req);
1671  for (size_t i = 0; i < req.indexes.size(); i++) {
1672  if (req.indexes[i] >= block.vtx.size()) {
1673  LOCK(cs_main);
1674  Misbehaving(pfrom->GetId(), 100, strprintf("Peer %d sent us a getblocktxn with out-of-bounds tx indices", pfrom->GetId()));
1675  return;
1676  }
1677  resp.txn[i] = block.vtx[req.indexes[i]];
1678  }
1679  LOCK(cs_main);
1680  const CNetMsgMaker msgMaker(pfrom->GetSendVersion());
1681  int nSendFlags = State(pfrom->GetId())->fWantsCmpctWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
1682  connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::BLOCKTXN, resp));
1683 }
1684 
1685 bool static ProcessHeadersMessage(CNode *pfrom, CConnman *connman, const std::vector<CBlockHeader>& headers, const CChainParams& chainparams, bool via_compact_block)
1686 {
1687  const CNetMsgMaker msgMaker(pfrom->GetSendVersion());
1688  size_t nCount = headers.size();
1689 
1690  if (nCount == 0) {
1691  // Nothing interesting. Stop asking this peers for more headers.
1692  return true;
1693  }
1694 
1695  bool received_new_header = false;
1696  const CBlockIndex *pindexLast = nullptr;
1697  {
1698  LOCK(cs_main);
1699  CNodeState *nodestate = State(pfrom->GetId());
1700 
1701  // If this looks like it could be a block announcement (nCount <
1702  // MAX_BLOCKS_TO_ANNOUNCE), use special logic for handling headers that
1703  // don't connect:
1704  // - Send a getheaders message in response to try to connect the chain.
1705  // - The peer can send up to MAX_UNCONNECTING_HEADERS in a row that
1706  // don't connect before giving DoS points
1707  // - Once a headers message is received that is valid and does connect,
1708  // nUnconnectingHeaders gets reset back to 0.
1709  if (!LookupBlockIndex(headers[0].hashPrevBlock) && nCount < MAX_BLOCKS_TO_ANNOUNCE) {
1710  nodestate->nUnconnectingHeaders++;
1711  connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, ::ChainActive().GetLocator(pindexBestHeader), uint256()));
1712  LogPrint(BCLog::NET, "received header %s: missing prev block %s, sending getheaders (%d) to end (peer=%d, nUnconnectingHeaders=%d)\n",
1713  headers[0].GetHash().ToString(),
1714  headers[0].hashPrevBlock.ToString(),
1716  pfrom->GetId(), nodestate->nUnconnectingHeaders);
1717  // Set hashLastUnknownBlock for this peer, so that if we
1718  // eventually get the headers - even from a different peer -
1719  // we can use this peer to download.
1720  UpdateBlockAvailability(pfrom->GetId(), headers.back().GetHash());
1721 
1722  if (nodestate->nUnconnectingHeaders % MAX_UNCONNECTING_HEADERS == 0) {
1723  Misbehaving(pfrom->GetId(), 20);
1724  }
1725  return true;
1726  }
1727 
1728  uint256 hashLastBlock;
1729  for (const CBlockHeader& header : headers) {
1730  if (!hashLastBlock.IsNull() && header.hashPrevBlock != hashLastBlock) {
1731  Misbehaving(pfrom->GetId(), 20, "non-continuous headers sequence");
1732  return false;
1733  }
1734  hashLastBlock = header.GetHash();
1735  }
1736 
1737  // If we don't have the last header, then they'll have given us
1738  // something new (if these headers are valid).
1739  if (!LookupBlockIndex(hashLastBlock)) {
1740  received_new_header = true;
1741  }
1742  }
1743 
1744  BlockValidationState state;
1745  if (!ProcessNewBlockHeaders(headers, state, chainparams, &pindexLast)) {
1746  if (state.IsInvalid()) {
1747  MaybePunishNodeForBlock(pfrom->GetId(), state, via_compact_block, "invalid header received");
1748  return false;
1749  }
1750  }
1751 
1752  {
1753  LOCK(cs_main);
1754  CNodeState *nodestate = State(pfrom->GetId());
1755  if (nodestate->nUnconnectingHeaders > 0) {
1756  LogPrint(BCLog::NET, "peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n", pfrom->GetId(), nodestate->nUnconnectingHeaders);
1757  }
1758  nodestate->nUnconnectingHeaders = 0;
1759 
1760  assert(pindexLast);
1761  UpdateBlockAvailability(pfrom->GetId(), pindexLast->GetBlockHash());
1762 
1763  // From here, pindexBestKnownBlock should be guaranteed to be non-null,
1764  // because it is set in UpdateBlockAvailability. Some nullptr checks
1765  // are still present, however, as belt-and-suspenders.
1766 
1767  if (received_new_header && pindexLast->nChainWork > ::ChainActive().Tip()->nChainWork) {
1768  nodestate->m_last_block_announcement = GetTime();
1769  }
1770 
1771  if (nCount == MAX_HEADERS_RESULTS) {
1772  // Headers message had its maximum size; the peer may have more headers.
1773  // TODO: optimize: if pindexLast is an ancestor of ::ChainActive().Tip or pindexBestHeader, continue
1774  // from there instead.
1775  LogPrint(BCLog::NET, "more getheaders (%d) to end to peer=%d (startheight:%d)\n", pindexLast->nHeight, pfrom->GetId(), pfrom->nStartingHeight);
1776  connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, ::ChainActive().GetLocator(pindexLast), uint256()));
1777  }
1778 
1779  bool fCanDirectFetch = CanDirectFetch(chainparams.GetConsensus());
1780  // If this set of headers is valid and ends in a block with at least as
1781  // much work as our tip, download as much as possible.
1782  if (fCanDirectFetch && pindexLast->IsValid(BLOCK_VALID_TREE) && ::ChainActive().Tip()->nChainWork <= pindexLast->nChainWork) {
1783  std::vector<const CBlockIndex*> vToFetch;
1784  const CBlockIndex *pindexWalk = pindexLast;
1785  // Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
1786  while (pindexWalk && !::ChainActive().Contains(pindexWalk) && vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
1787  if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
1788  !mapBlocksInFlight.count(pindexWalk->GetBlockHash()) &&
1789  (!IsWitnessEnabled(pindexWalk->pprev, chainparams.GetConsensus()) || State(pfrom->GetId())->fHaveWitness)) {
1790  // We don't have this block, and it's not yet in flight.
1791  vToFetch.push_back(pindexWalk);
1792  }
1793  pindexWalk = pindexWalk->pprev;
1794  }
1795  // If pindexWalk still isn't on our main chain, we're looking at a
1796  // very large reorg at a time we think we're close to caught up to
1797  // the main chain -- this shouldn't really happen. Bail out on the
1798  // direct fetch and rely on parallel download instead.
1799  if (!::ChainActive().Contains(pindexWalk)) {
1800  LogPrint(BCLog::NET, "Large reorg, won't direct fetch to %s (%d)\n",
1801  pindexLast->GetBlockHash().ToString(),
1802  pindexLast->nHeight);
1803  } else {
1804  std::vector<CInv> vGetData;
1805  // Download as much as possible, from earliest to latest.
1806  for (const CBlockIndex *pindex : reverse_iterate(vToFetch)) {
1807  if (nodestate->nBlocksInFlight >= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
1808  // Can't download any more from this peer
1809  break;
1810  }
1811  uint32_t nFetchFlags = GetFetchFlags(pfrom);
1812  vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
1813  MarkBlockAsInFlight(pfrom->GetId(), pindex->GetBlockHash(), pindex);
1814  LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n",
1815  pindex->GetBlockHash().ToString(), pfrom->GetId());
1816  }
1817  if (vGetData.size() > 1) {
1818  LogPrint(BCLog::NET, "Downloading blocks toward %s (%d) via headers direct fetch\n",
1819  pindexLast->GetBlockHash().ToString(), pindexLast->nHeight);
1820  }
1821  if (vGetData.size() > 0) {
1822  if (nodestate->fSupportsDesiredCmpctVersion && vGetData.size() == 1 && mapBlocksInFlight.size() == 1 && pindexLast->pprev->IsValid(BLOCK_VALID_CHAIN)) {
1823  // In any case, we want to download using a compact block, not a regular one
1824  vGetData[0] = CInv(MSG_CMPCT_BLOCK, vGetData[0].hash);
1825  }
1826  connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vGetData));
1827  }
1828  }
1829  }
1830  // If we're in IBD, we want outbound peers that will serve us a useful
1831  // chain. Disconnect peers that are on chains with insufficient work.
1833  // When nCount < MAX_HEADERS_RESULTS, we know we have no more
1834  // headers to fetch from this peer.
1835  if (nodestate->pindexBestKnownBlock && nodestate->pindexBestKnownBlock->nChainWork < nMinimumChainWork) {
1836  // This peer has too little work on their headers chain to help
1837  // us sync -- disconnect if using an outbound slot (unless
1838  // whitelisted or addnode).
1839  // Note: We compare their tip to nMinimumChainWork (rather than
1840  // ::ChainActive().Tip()) because we won't start block download
1841  // until we have a headers chain that has at least
1842  // nMinimumChainWork, even if a peer has a chain past our tip,
1843  // as an anti-DoS measure.
1844  if (IsOutboundDisconnectionCandidate(pfrom)) {
1845  LogPrintf("Disconnecting outbound peer %d -- headers chain has insufficient work\n", pfrom->GetId());
1846  pfrom->fDisconnect = true;
1847  }
1848  }
1849  }
1850 
1851  if (!pfrom->fDisconnect && IsOutboundDisconnectionCandidate(pfrom) && nodestate->pindexBestKnownBlock != nullptr && pfrom->m_tx_relay != nullptr) {
1852  // If this is an outbound full-relay peer, check to see if we should protect
1853  // it from the bad/lagging chain logic.
1854  // Note that block-relay-only peers are already implicitly protected, so we
1855  // only consider setting m_protect for the full-relay peers.
1856  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) {
1857  LogPrint(BCLog::NET, "Protecting outbound peer=%d from eviction\n", pfrom->GetId());
1858  nodestate->m_chain_sync.m_protect = true;
1859  ++g_outbound_peers_with_protect_from_disconnect;
1860  }
1861  }
1862  }
1863 
1864  return true;
1865 }
1866 
1867 void static ProcessOrphanTx(CConnman* connman, std::set<uint256>& orphan_work_set, std::list<CTransactionRef>& removed_txn) EXCLUSIVE_LOCKS_REQUIRED(cs_main, g_cs_orphans)
1868 {
1870  AssertLockHeld(g_cs_orphans);
1871  std::set<NodeId> setMisbehaving;
1872  bool done = false;
1873  while (!done && !orphan_work_set.empty()) {
1874  const uint256 orphanHash = *orphan_work_set.begin();
1875  orphan_work_set.erase(orphan_work_set.begin());
1876 
1877  auto orphan_it = mapOrphanTransactions.find(orphanHash);
1878  if (orphan_it == mapOrphanTransactions.end()) continue;
1879 
1880  const CTransactionRef porphanTx = orphan_it->second.tx;
1881  const CTransaction& orphanTx = *porphanTx;
1882  NodeId fromPeer = orphan_it->second.fromPeer;
1883  // Use a new TxValidationState because orphans come from different peers (and we call
1884  // MaybePunishNodeForTx based on the source peer from the orphan map, not based on the peer
1885  // that relayed the previous transaction).
1886  TxValidationState orphan_state;
1887 
1888  if (setMisbehaving.count(fromPeer)) continue;
1889  if (AcceptToMemoryPool(mempool, orphan_state, porphanTx, &removed_txn, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
1890  LogPrint(BCLog::MEMPOOL, " accepted orphan tx %s\n", orphanHash.ToString());
1891  RelayTransaction(orphanHash, *connman);
1892  for (unsigned int i = 0; i < orphanTx.vout.size(); i++) {
1893  auto it_by_prev = mapOrphanTransactionsByPrev.find(COutPoint(orphanHash, i));
1894  if (it_by_prev != mapOrphanTransactionsByPrev.end()) {
1895  for (const auto& elem : it_by_prev->second) {
1896  orphan_work_set.insert(elem->first);
1897  }
1898  }
1899  }
1900  EraseOrphanTx(orphanHash);
1901  done = true;
1902  } else if (orphan_state.GetResult() != TxValidationResult::TX_MISSING_INPUTS) {
1903  if (orphan_state.IsInvalid()) {
1904  // Punish peer that gave us an invalid orphan tx
1905  if (MaybePunishNodeForTx(fromPeer, orphan_state)) {
1906  setMisbehaving.insert(fromPeer);
1907  }
1908  LogPrint(BCLog::MEMPOOL, " invalid orphan tx %s\n", orphanHash.ToString());
1909  }
1910  // Has inputs but not accepted to mempool
1911  // Probably non-standard or insufficient fee
1912  LogPrint(BCLog::MEMPOOL, " removed orphan tx %s\n", orphanHash.ToString());
1913  if (!orphanTx.HasWitness() && orphan_state.GetResult() != TxValidationResult::TX_WITNESS_MUTATED) {
1914  // Do not use rejection cache for witness transactions or
1915  // witness-stripped transactions, as they can have been malleated.
1916  // See https://github.com/bitcoin/bitcoin/issues/8279 for details.
1917  assert(recentRejects);
1918  recentRejects->insert(orphanHash);
1919  }
1920  EraseOrphanTx(orphanHash);
1921  done = true;
1922  }
1923  mempool.check(&::ChainstateActive().CoinsTip());
1924  }
1925 }
1926 
1927 bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, int64_t nTimeReceived, const CChainParams& chainparams, CConnman* connman, BanMan* banman, const std::atomic<bool>& interruptMsgProc)
1928 {
1929  LogPrint(BCLog::NET, "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand), vRecv.size(), pfrom->GetId());
1930  if (gArgs.IsArgSet("-dropmessagestest") && GetRand(gArgs.GetArg("-dropmessagestest", 0)) == 0)
1931  {
1932  LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n");
1933  return true;
1934  }
1935 
1936 
1937  if (!(pfrom->GetLocalServices() & NODE_BLOOM) &&
1938  (strCommand == NetMsgType::FILTERLOAD ||
1939  strCommand == NetMsgType::FILTERADD))
1940  {
1941  if (pfrom->nVersion >= NO_BLOOM_VERSION) {
1942  LOCK(cs_main);
1943  Misbehaving(pfrom->GetId(), 100);
1944  return false;
1945  } else {
1946  pfrom->fDisconnect = true;
1947  return false;
1948  }
1949  }
1950 
1951  if (strCommand == NetMsgType::VERSION) {
1952  // Each connection can only send one version message
1953  if (pfrom->nVersion != 0)
1954  {
1955  LOCK(cs_main);
1956  Misbehaving(pfrom->GetId(), 1);
1957  return false;
1958  }
1959 
1960  int64_t nTime;
1961  CAddress addrMe;
1962  CAddress addrFrom;
1963  uint64_t nNonce = 1;
1964  uint64_t nServiceInt;
1965  ServiceFlags nServices;
1966  int nVersion;
1967  int nSendVersion;
1968  std::string cleanSubVer;
1969  int nStartingHeight = -1;
1970  bool fRelay = true;
1971 
1972  vRecv >> nVersion >> nServiceInt >> nTime >> addrMe;
1973  nSendVersion = std::min(nVersion, PROTOCOL_VERSION);
1974  nServices = ServiceFlags(nServiceInt);
1975  if (!pfrom->fInbound)
1976  {
1977  connman->SetServices(pfrom->addr, nServices);
1978  }
1979  if (!pfrom->fInbound && !pfrom->fFeeler && !pfrom->m_manual_connection && !HasAllDesirableServiceFlags(nServices))
1980  {
1981  LogPrint(BCLog::NET, "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom->GetId(), nServices, GetDesirableServiceFlags(nServices));
1982  pfrom->fDisconnect = true;
1983  return false;
1984  }
1985 
1986  if (nVersion < MIN_PEER_PROTO_VERSION) {
1987  // disconnect from peers older than this proto version
1988  LogPrint(BCLog::NET, "peer=%d using obsolete version %i; disconnecting\n", pfrom->GetId(), nVersion);
1989  pfrom->fDisconnect = true;
1990  return false;
1991  }
1992 
1993  if (!vRecv.empty())
1994  vRecv >> addrFrom >> nNonce;
1995  if (!vRecv.empty()) {
1996  std::string strSubVer;
1997  vRecv >> LIMITED_STRING(strSubVer, MAX_SUBVERSION_LENGTH);
1998  cleanSubVer = SanitizeString(strSubVer);
1999  }
2000  if (!vRecv.empty()) {
2001  vRecv >> nStartingHeight;
2002  }
2003  if (!vRecv.empty())
2004  vRecv >> fRelay;
2005  // Disconnect if we connected to ourself
2006  if (pfrom->fInbound && !connman->CheckIncomingNonce(nNonce))
2007  {
2008  LogPrintf("connected to self at %s, disconnecting\n", pfrom->addr.ToString());
2009  pfrom->fDisconnect = true;
2010  return true;
2011  }
2012 
2013  if (pfrom->fInbound && addrMe.IsRoutable())
2014  {
2015  SeenLocal(addrMe);
2016  }
2017 
2018  // Be shy and don't send version until we hear
2019  if (pfrom->fInbound)
2020  PushNodeVersion(pfrom, connman, GetAdjustedTime());
2021 
2023 
2024  pfrom->nServices = nServices;
2025  pfrom->SetAddrLocal(addrMe);
2026  {
2027  LOCK(pfrom->cs_SubVer);
2028  pfrom->cleanSubVer = cleanSubVer;
2029  }
2030  pfrom->nStartingHeight = nStartingHeight;
2031 
2032  // set nodes not relaying blocks and tx and not serving (parts) of the historical blockchain as "clients"
2033  pfrom->fClient = (!(nServices & NODE_NETWORK) && !(nServices & NODE_NETWORK_LIMITED));
2034 
2035  // set nodes not capable of serving the complete blockchain history as "limited nodes"
2036  pfrom->m_limited_node = (!(nServices & NODE_NETWORK) && (nServices & NODE_NETWORK_LIMITED));
2037 
2038  if (pfrom->m_tx_relay != nullptr) {
2039  LOCK(pfrom->m_tx_relay->cs_filter);
2040  pfrom->m_tx_relay->fRelayTxes = fRelay; // set to true after we get the first filter* message
2041  }
2042 
2043  // Change version
2044  pfrom->SetSendVersion(nSendVersion);
2045  pfrom->nVersion = nVersion;
2046 
2047  if((nServices & NODE_WITNESS))
2048  {
2049  LOCK(cs_main);
2050  State(pfrom->GetId())->fHaveWitness = true;
2051  }
2052 
2053  // Potentially mark this peer as a preferred download peer.
2054  {
2055  LOCK(cs_main);
2056  UpdatePreferredDownload(pfrom, State(pfrom->GetId()));
2057  }
2058 
2059  if (!pfrom->fInbound && pfrom->IsAddrRelayPeer())
2060  {
2061  // Advertise our address
2063  {
2064  CAddress addr = GetLocalAddress(&pfrom->addr, pfrom->GetLocalServices());
2065  FastRandomContext insecure_rand;
2066  if (addr.IsRoutable())
2067  {
2068  LogPrint(BCLog::NET, "ProcessMessages: advertising address %s\n", addr.ToString());
2069  pfrom->PushAddress(addr, insecure_rand);
2070  } else if (IsPeerAddrLocalGood(pfrom)) {
2071  addr.SetIP(addrMe);
2072  LogPrint(BCLog::NET, "ProcessMessages: advertising address %s\n", addr.ToString());
2073  pfrom->PushAddress(addr, insecure_rand);
2074  }
2075  }
2076 
2077  // Get recent addresses
2078  if (pfrom->fOneShot || pfrom->nVersion >= CADDR_TIME_VERSION || connman->GetAddressCount() < 1000)
2079  {
2080  connman->PushMessage(pfrom, CNetMsgMaker(nSendVersion).Make(NetMsgType::GETADDR));
2081  pfrom->fGetAddr = true;
2082  }
2083  connman->MarkAddressGood(pfrom->addr);
2084  }
2085 
2086  std::string remoteAddr;
2087  if (fLogIPs)
2088  remoteAddr = ", peeraddr=" + pfrom->addr.ToString();
2089 
2090  LogPrint(BCLog::NET, "receive version message: %s: version %d, blocks=%d, us=%s, peer=%d%s\n",
2091  cleanSubVer, pfrom->nVersion,
2092  pfrom->nStartingHeight, addrMe.ToString(), pfrom->GetId(),
2093  remoteAddr);
2094 
2095  int64_t nTimeOffset = nTime - GetTime();
2096  pfrom->nTimeOffset = nTimeOffset;
2097  AddTimeData(pfrom->addr, nTimeOffset);
2098 
2099  // If the peer is old enough to have the old alert system, send it the final alert.
2100  if (pfrom->nVersion <= 70012) {
2101  CDataStream finalAlert(ParseHex("60010000000000000000000000ffffff7f00000000ffffff7ffeffff7f01ffffff7f00000000ffffff7f00ffffff7f002f555247454e543a20416c657274206b657920636f6d70726f6d697365642c2075706772616465207265717569726564004630440220653febd6410f470f6bae11cad19c48413becb1ac2c17f908fd0fd53bdc3abd5202206d0e9c96fe88d4a0f01ed9dedae2b6f9e00da94cad0fecaae66ecf689bf71b50"), SER_NETWORK, PROTOCOL_VERSION);
2102  connman->PushMessage(pfrom, CNetMsgMaker(nSendVersion).Make("alert", finalAlert));
2103  }
2104 
2105  // Feeler connections exist only to verify if address is online.
2106  if (pfrom->fFeeler) {
2107  assert(pfrom->fInbound == false);
2108  pfrom->fDisconnect = true;
2109  }
2110  return true;
2111  }
2112 
2113  if (pfrom->nVersion == 0) {
2114  // Must have a version message before anything else
2115  LOCK(cs_main);
2116  Misbehaving(pfrom->GetId(), 1);
2117  return false;
2118  }
2119 
2120  // At this point, the outgoing message serialization version can't change.
2121  const CNetMsgMaker msgMaker(pfrom->GetSendVersion());
2122 
2123  if (strCommand == NetMsgType::VERACK)
2124  {
2125  pfrom->SetRecvVersion(std::min(pfrom->nVersion.load(), PROTOCOL_VERSION));
2126 
2127  if (!pfrom->fInbound) {
2128  // Mark this node as currently connected, so we update its timestamp later.
2129  LOCK(cs_main);
2130  State(pfrom->GetId())->fCurrentlyConnected = true;
2131  LogPrintf("New outbound peer connected: version: %d, blocks=%d, peer=%d%s (%s)\n",
2132  pfrom->nVersion.load(), pfrom->nStartingHeight,
2133  pfrom->GetId(), (fLogIPs ? strprintf(", peeraddr=%s", pfrom->addr.ToString()) : ""),
2134  pfrom->m_tx_relay == nullptr ? "block-relay" : "full-relay");
2135  }
2136 
2137  if (pfrom->nVersion >= SENDHEADERS_VERSION) {
2138  // Tell our peer we prefer to receive headers rather than inv's
2139  // We send this to non-NODE NETWORK peers as well, because even
2140  // non-NODE NETWORK peers can announce blocks (such as pruning
2141  // nodes)
2142  connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::SENDHEADERS));
2143  }
2144  if (pfrom->nVersion >= SHORT_IDS_BLOCKS_VERSION) {
2145  // Tell our peer we are willing to provide version 1 or 2 cmpctblocks
2146  // However, we do not request new block announcements using
2147  // cmpctblock messages.
2148  // We send this to non-NODE NETWORK peers as well, because
2149  // they may wish to request compact blocks from us
2150  bool fAnnounceUsingCMPCTBLOCK = false;
2151  uint64_t nCMPCTBLOCKVersion = 2;
2152  if (pfrom->GetLocalServices() & NODE_WITNESS)
2153  connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
2154  nCMPCTBLOCKVersion = 1;
2155  connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
2156  }
2157  pfrom->fSuccessfullyConnected = true;
2158  return true;
2159  }
2160 
2161  if (!pfrom->fSuccessfullyConnected) {
2162  // Must have a verack message before anything else
2163  LOCK(cs_main);
2164  Misbehaving(pfrom->GetId(), 1);
2165  return false;
2166  }
2167 
2168  if (strCommand == NetMsgType::ADDR) {
2169  std::vector<CAddress> vAddr;
2170  vRecv >> vAddr;
2171 
2172  // Don't want addr from older versions unless seeding
2173  if (pfrom->nVersion < CADDR_TIME_VERSION && connman->GetAddressCount() > 1000)
2174  return true;
2175  if (!pfrom->IsAddrRelayPeer()) {
2176  return true;
2177  }
2178  if (vAddr.size() > 1000)
2179  {
2180  LOCK(cs_main);
2181  Misbehaving(pfrom->GetId(), 20, strprintf("message addr size() = %u", vAddr.size()));
2182  return false;
2183  }
2184 
2185  // Store the new addresses
2186  std::vector<CAddress> vAddrOk;
2187  int64_t nNow = GetAdjustedTime();
2188  int64_t nSince = nNow - 10 * 60;
2189  for (CAddress& addr : vAddr)
2190  {
2191  if (interruptMsgProc)
2192  return true;
2193 
2194  // We only bother storing full nodes, though this may include
2195  // things which we would not make an outbound connection to, in
2196  // part because we may make feeler connections to them.
2197  if (!MayHaveUsefulAddressDB(addr.nServices) && !HasAllDesirableServiceFlags(addr.nServices))
2198  continue;
2199 
2200  if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
2201  addr.nTime = nNow - 5 * 24 * 60 * 60;
2202  pfrom->AddAddressKnown(addr);
2203  if (banman->IsBanned(addr)) continue; // Do not process banned addresses beyond remembering we received them
2204  bool fReachable = IsReachable(addr);
2205  if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
2206  {
2207  // Relay to a limited number of other nodes
2208  RelayAddress(addr, fReachable, connman);
2209  }
2210  // Do not store addresses outside our network
2211  if (fReachable)
2212  vAddrOk.push_back(addr);
2213  }
2214  connman->AddNewAddresses(vAddrOk, pfrom->addr, 2 * 60 * 60);
2215  if (vAddr.size() < 1000)
2216  pfrom->fGetAddr = false;
2217  if (pfrom->fOneShot)
2218  pfrom->fDisconnect = true;
2219  return true;
2220  }
2221 
2222  if (strCommand == NetMsgType::SENDHEADERS) {
2223  LOCK(cs_main);
2224  State(pfrom->GetId())->fPreferHeaders = true;
2225  return true;
2226  }
2227 
2228  if (strCommand == NetMsgType::SENDCMPCT) {
2229  bool fAnnounceUsingCMPCTBLOCK = false;
2230  uint64_t nCMPCTBLOCKVersion = 0;
2231  vRecv >> fAnnounceUsingCMPCTBLOCK >> nCMPCTBLOCKVersion;
2232  if (nCMPCTBLOCKVersion == 1 || ((pfrom->GetLocalServices() & NODE_WITNESS) && nCMPCTBLOCKVersion == 2)) {
2233  LOCK(cs_main);
2234  // fProvidesHeaderAndIDs is used to "lock in" version of compact blocks we send (fWantsCmpctWitness)
2235  if (!State(pfrom->GetId())->fProvidesHeaderAndIDs) {
2236  State(pfrom->GetId())->fProvidesHeaderAndIDs = true;
2237  State(pfrom->GetId())->fWantsCmpctWitness = nCMPCTBLOCKVersion == 2;
2238  }
2239  if (State(pfrom->GetId())->fWantsCmpctWitness == (nCMPCTBLOCKVersion == 2)) // ignore later version announces
2240  State(pfrom->GetId())->fPreferHeaderAndIDs = fAnnounceUsingCMPCTBLOCK;
2241  if (!State(pfrom->GetId())->fSupportsDesiredCmpctVersion) {
2242  if (pfrom->GetLocalServices() & NODE_WITNESS)
2243  State(pfrom->GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 2);
2244  else
2245  State(pfrom->GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 1);
2246  }
2247  }
2248  return true;
2249  }
2250 
2251  if (strCommand == NetMsgType::INV) {
2252  std::vector<CInv> vInv;
2253  vRecv >> vInv;
2254  if (vInv.size() > MAX_INV_SZ)
2255  {
2256  LOCK(cs_main);
2257  Misbehaving(pfrom->GetId(), 20, strprintf("message inv size() = %u", vInv.size()));
2258  return false;
2259  }
2260 
2261  // We won't accept tx inv's if we're in blocks-only mode, or this is a
2262  // block-relay-only peer
2263  bool fBlocksOnly = !g_relay_txes || (pfrom->m_tx_relay == nullptr);
2264 
2265  // Allow whitelisted peers to send data other than blocks in blocks only mode if whitelistrelay is true
2266  if (pfrom->HasPermission(PF_RELAY))
2267  fBlocksOnly = false;
2268 
2269  LOCK(cs_main);
2270 
2271  uint32_t nFetchFlags = GetFetchFlags(pfrom);
2272  const auto current_time = GetTime<std::chrono::microseconds>();
2273 
2274  for (CInv &inv : vInv)
2275  {
2276  if (interruptMsgProc)
2277  return true;
2278 
2279  bool fAlreadyHave = AlreadyHave(inv);
2280  LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom->GetId());
2281 
2282  if (inv.type == MSG_TX) {
2283  inv.type |= nFetchFlags;
2284  }
2285 
2286  if (inv.type == MSG_BLOCK) {
2287  UpdateBlockAvailability(pfrom->GetId(), inv.hash);
2288  if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count(inv.hash)) {
2289  // We used to request the full block here, but since headers-announcements are now the
2290  // primary method of announcement on the network, and since, in the case that a node
2291  // fell back to inv we probably have a reorg which we should get the headers for first,
2292  // we now only provide a getheaders response here. When we receive the headers, we will
2293  // then ask for the blocks we need.
2294  connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, ::ChainActive().GetLocator(pindexBestHeader), inv.hash));
2295  LogPrint(BCLog::NET, "getheaders (%d) %s to peer=%d\n", pindexBestHeader->nHeight, inv.hash.ToString(), pfrom->GetId());
2296  }
2297  }
2298  else
2299  {
2300  pfrom->AddInventoryKnown(inv);
2301  if (fBlocksOnly) {
2302  LogPrint(BCLog::NET, "transaction (%s) inv sent in violation of protocol, disconnecting peer=%d\n", inv.hash.ToString(), pfrom->GetId());
2303  pfrom->fDisconnect = true;
2304  return true;
2305  } else if (!fAlreadyHave && !fImporting && !fReindex && !::ChainstateActive().IsInitialBlockDownload()) {
2306  RequestTx(State(pfrom->GetId()), inv.hash, current_time);
2307  }
2308  }
2309  }
2310  return true;
2311  }
2312 
2313  if (strCommand == NetMsgType::GETDATA) {
2314  std::vector<CInv> vInv;
2315  vRecv >> vInv;
2316  if (vInv.size() > MAX_INV_SZ)
2317  {
2318  LOCK(cs_main);
2319  Misbehaving(pfrom->GetId(), 20, strprintf("message getdata size() = %u", vInv.size()));
2320  return false;
2321  }
2322 
2323  LogPrint(BCLog::NET, "received getdata (%u invsz) peer=%d\n", vInv.size(), pfrom->GetId());
2324 
2325  if (vInv.size() > 0) {
2326  LogPrint(BCLog::NET, "received getdata for: %s peer=%d\n", vInv[0].ToString(), pfrom->GetId());
2327  }
2328 
2329  pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end());
2330  ProcessGetData(pfrom, chainparams, connman, interruptMsgProc);
2331  return true;
2332  }
2333 
2334  if (strCommand == NetMsgType::GETBLOCKS) {
2335  CBlockLocator locator;
2336  uint256 hashStop;
2337  vRecv >> locator >> hashStop;
2338 
2339  if (locator.vHave.size() > MAX_LOCATOR_SZ) {
2340  LogPrint(BCLog::NET, "getblocks locator size %lld > %d, disconnect peer=%d\n", locator.vHave.size(), MAX_LOCATOR_SZ, pfrom->GetId());
2341  pfrom->fDisconnect = true;
2342  return true;
2343  }
2344 
2345  // We might have announced the currently-being-connected tip using a
2346  // compact block, which resulted in the peer sending a getblocks
2347  // request, which we would otherwise respond to without the new block.
2348  // To avoid this situation we simply verify that we are on our best
2349  // known chain now. This is super overkill, but we handle it better
2350  // for getheaders requests, and there are no known nodes which support
2351  // compact blocks but still use getblocks to request blocks.
2352  {
2353  std::shared_ptr<const CBlock> a_recent_block;
2354  {
2355  LOCK(cs_most_recent_block);
2356  a_recent_block = most_recent_block;
2357  }
2358  BlockValidationState state;
2359  if (!ActivateBestChain(state, Params(), a_recent_block)) {
2360  LogPrint(BCLog::NET, "failed to activate chain (%s)\n", FormatStateMessage(state));
2361  }
2362  }
2363 
2364  LOCK(cs_main);
2365 
2366  // Find the last block the caller has in the main chain
2367  const CBlockIndex* pindex = FindForkInGlobalIndex(::ChainActive(), locator);
2368 
2369  // Send the rest of the chain
2370  if (pindex)
2371  pindex = ::ChainActive().Next(pindex);
2372  int nLimit = 500;
2373  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());
2374  for (; pindex; pindex = ::ChainActive().Next(pindex))
2375  {
2376  if (pindex->GetBlockHash() == hashStop)
2377  {
2378  LogPrint(BCLog::NET, " getblocks stopping at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
2379  break;
2380  }
2381  // If pruning, don't inv blocks unless we have on disk and are likely to still have
2382  // for some reasonable time window (1 hour) that block relay might require.
2383  const int nPrunedBlocksLikelyToHave = MIN_BLOCKS_TO_KEEP - 3600 / chainparams.GetConsensus().nPowTargetSpacing;
2384  if (fPruneMode && (!(pindex->nStatus & BLOCK_HAVE_DATA) || pindex->nHeight <= ::ChainActive().Tip()->nHeight - nPrunedBlocksLikelyToHave))
2385  {
2386  LogPrint(BCLog::NET, " getblocks stopping, pruned or too old block at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
2387  break;
2388  }
2389  pfrom->PushInventory(CInv(MSG_BLOCK, pindex->GetBlockHash()));
2390  if (--nLimit <= 0)
2391  {
2392  // When this block is requested, we'll send an inv that'll
2393  // trigger the peer to getblocks the next batch of inventory.
2394  LogPrint(BCLog::NET, " getblocks stopping at limit %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
2395  pfrom->hashContinue = pindex->GetBlockHash();
2396  break;
2397  }
2398  }
2399  return true;
2400  }
2401 
2402  if (strCommand == NetMsgType::GETBLOCKTXN) {
2404  vRecv >> req;
2405 
2406  std::shared_ptr<const CBlock> recent_block;
2407  {
2408  LOCK(cs_most_recent_block);
2409  if (most_recent_block_hash == req.blockhash)
2410  recent_block = most_recent_block;
2411  // Unlock cs_most_recent_block to avoid cs_main lock inversion
2412  }
2413  if (recent_block) {
2414  SendBlockTransactions(*recent_block, req, pfrom, connman);
2415  return true;
2416  }
2417 
2418  LOCK(cs_main);
2419 
2420  const CBlockIndex* pindex = LookupBlockIndex(req.blockhash);
2421  if (!pindex || !(pindex->nStatus & BLOCK_HAVE_DATA)) {
2422  LogPrint(BCLog::NET, "Peer %d sent us a getblocktxn for a block we don't have\n", pfrom->GetId());
2423  return true;
2424  }
2425 
2426  if (pindex->nHeight < ::ChainActive().Height() - MAX_BLOCKTXN_DEPTH) {
2427  // If an older block is requested (should never happen in practice,
2428  // but can happen in tests) send a block response instead of a
2429  // blocktxn response. Sending a full block response instead of a
2430  // small blocktxn response is preferable in the case where a peer
2431  // might maliciously send lots of getblocktxn requests to trigger
2432  // expensive disk reads, because it will require the peer to
2433  // actually receive all the data read from disk over the network.
2434  LogPrint(BCLog::NET, "Peer %d sent us a getblocktxn for a block > %i deep\n", pfrom->GetId(), MAX_BLOCKTXN_DEPTH);
2435  CInv inv;
2436  inv.type = State(pfrom->GetId())->fWantsCmpctWitness ? MSG_WITNESS_BLOCK : MSG_BLOCK;
2437  inv.hash = req.blockhash;
2438  pfrom->vRecvGetData.push_back(inv);
2439  // The message processing loop will go around again (without pausing) and we'll respond then (without cs_main)
2440  return true;
2441  }
2442 
2443  CBlock block;
2444  bool ret = ReadBlockFromDisk(block, pindex, chainparams.GetConsensus());
2445  assert(ret);
2446 
2447  SendBlockTransactions(block, req, pfrom, connman);
2448  return true;
2449  }
2450 
2451  if (strCommand == NetMsgType::GETHEADERS) {
2452  CBlockLocator locator;
2453  uint256 hashStop;
2454  vRecv >> locator >> hashStop;
2455 
2456  if (locator.vHave.size() > MAX_LOCATOR_SZ) {
2457  LogPrint(BCLog::NET, "getheaders locator size %lld > %d, disconnect peer=%d\n", locator.vHave.size(), MAX_LOCATOR_SZ, pfrom->GetId());
2458  pfrom->fDisconnect = true;
2459  return true;
2460  }
2461 
2462  LOCK(cs_main);
2463  if (::ChainstateActive().IsInitialBlockDownload() && !pfrom->HasPermission(PF_NOBAN)) {
2464  LogPrint(BCLog::NET, "Ignoring getheaders from peer=%d because node is in initial block download\n", pfrom->GetId());
2465  return true;
2466  }
2467 
2468  CNodeState *nodestate = State(pfrom->GetId());
2469  const CBlockIndex* pindex = nullptr;
2470  if (locator.IsNull())
2471  {
2472  // If locator is null, return the hashStop block
2473  pindex = LookupBlockIndex(hashStop);
2474  if (!pindex) {
2475  return true;
2476  }
2477 
2478  if (!BlockRequestAllowed(pindex, chainparams.GetConsensus())) {
2479  LogPrint(BCLog::NET, "%s: ignoring request from peer=%i for old block header that isn't in the main chain\n", __func__, pfrom->GetId());
2480  return true;
2481  }
2482  }
2483  else
2484  {
2485  // Find the last block the caller has in the main chain
2486  pindex = FindForkInGlobalIndex(::ChainActive(), locator);
2487  if (pindex)
2488  pindex = ::ChainActive().Next(pindex);
2489  }
2490 
2491  // we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
2492  std::vector<CBlock> vHeaders;
2493  int nLimit = MAX_HEADERS_RESULTS;
2494  LogPrint(BCLog::NET, "getheaders %d to %s from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), pfrom->GetId());
2495  for (; pindex; pindex = ::ChainActive().Next(pindex))
2496  {
2497  vHeaders.push_back(pindex->GetBlockHeader());
2498  if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
2499  break;
2500  }
2501  // pindex can be nullptr either if we sent ::ChainActive().Tip() OR
2502  // if our peer has ::ChainActive().Tip() (and thus we are sending an empty
2503  // headers message). In both cases it's safe to update
2504  // pindexBestHeaderSent to be our tip.
2505  //
2506  // It is important that we simply reset the BestHeaderSent value here,
2507  // and not max(BestHeaderSent, newHeaderSent). We might have announced
2508  // the currently-being-connected tip using a compact block, which
2509  // resulted in the peer sending a headers request, which we respond to
2510  // without the new block. By resetting the BestHeaderSent, we ensure we
2511  // will re-announce the new block via headers (or compact blocks again)
2512  // in the SendMessages logic.
2513  nodestate->pindexBestHeaderSent = pindex ? pindex : ::ChainActive().Tip();
2514  connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
2515  return true;
2516  }
2517 
2518  if (strCommand == NetMsgType::TX) {
2519  // Stop processing the transaction early if
2520  // We are in blocks only mode and peer is either not whitelisted or whitelistrelay is off
2521  // or if this peer is supposed to be a block-relay-only peer
2522  if ((!g_relay_txes && !pfrom->HasPermission(PF_RELAY)) || (pfrom->m_tx_relay == nullptr))
2523  {
2524  LogPrint(BCLog::NET, "transaction sent in violation of protocol peer=%d\n", pfrom->GetId());
2525  pfrom->fDisconnect = true;
2526  return true;
2527  }
2528 
2529  CTransactionRef ptx;
2530  vRecv >> ptx;
2531  const CTransaction& tx = *ptx;
2532 
2533  CInv inv(MSG_TX, tx.GetHash());
2534  pfrom->AddInventoryKnown(inv);
2535 
2536  LOCK2(cs_main, g_cs_orphans);
2537 
2538  TxValidationState state;
2539 
2540  CNodeState* nodestate = State(pfrom->GetId());
2541  nodestate->m_tx_download.m_tx_announced.erase(inv.hash);
2542  nodestate->m_tx_download.m_tx_in_flight.erase(inv.hash);
2543  EraseTxRequest(inv.hash);
2544 
2545  std::list<CTransactionRef> lRemovedTxn;
2546 
2547  if (!AlreadyHave(inv) &&
2548  AcceptToMemoryPool(mempool, state, ptx, &lRemovedTxn, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
2549  mempool.check(&::ChainstateActive().CoinsTip());
2551  for (unsigned int i = 0; i < tx.vout.size(); i++) {
2552  auto it_by_prev = mapOrphanTransactionsByPrev.find(COutPoint(inv.hash, i));
2553  if (it_by_prev != mapOrphanTransactionsByPrev.end()) {
2554  for (const auto& elem : it_by_prev->second) {
2555  pfrom->orphan_work_set.insert(elem->first);
2556  }
2557  }
2558  }
2559 
2560  pfrom->nLastTXTime = GetTime();
2561 
2562  LogPrint(BCLog::MEMPOOL, "AcceptToMemoryPool: peer=%d: accepted %s (poolsz %u txn, %u kB)\n",
2563  pfrom->GetId(),
2564  tx.GetHash().ToString(),
2565  mempool.size(), mempool.DynamicMemoryUsage() / 1000);
2566 
2567  // Recursively process any orphan transactions that depended on this one
2568  ProcessOrphanTx(connman, pfrom->orphan_work_set, lRemovedTxn);
2569  }
2571  {
2572  bool fRejectedParents = false; // It may be the case that the orphans parents have all been rejected
2573  for (const CTxIn& txin : tx.vin) {
2574  if (recentRejects->contains(txin.prevout.hash)) {
2575  fRejectedParents = true;
2576  break;
2577  }
2578  }
2579  if (!fRejectedParents) {
2580  uint32_t nFetchFlags = GetFetchFlags(pfrom);
2581  const auto current_time = GetTime<std::chrono::microseconds>();
2582 
2583  for (const CTxIn& txin : tx.vin) {
2584  CInv _inv(MSG_TX | nFetchFlags, txin.prevout.hash);
2585  pfrom->AddInventoryKnown(_inv);
2586  if (!AlreadyHave(_inv)) RequestTx(State(pfrom->GetId()), _inv.hash, current_time);
2587  }
2588  AddOrphanTx(ptx, pfrom->GetId());
2589 
2590  // DoS prevention: do not allow mapOrphanTransactions to grow unbounded (see CVE-2012-3789)
2591  unsigned int nMaxOrphanTx = (unsigned int)std::max((int64_t)0, gArgs.GetArg("-maxorphantx", DEFAULT_MAX_ORPHAN_TRANSACTIONS));
2592  unsigned int nEvicted = LimitOrphanTxSize(nMaxOrphanTx);
2593  if (nEvicted > 0) {
2594  LogPrint(BCLog::MEMPOOL, "mapOrphan overflow, removed %u tx\n", nEvicted);
2595  }
2596  } else {
2597  LogPrint(BCLog::MEMPOOL, "not keeping orphan with rejected parents %s\n",tx.GetHash().ToString());
2598  // We will continue to reject this tx since it has rejected
2599  // parents so avoid re-requesting it from other peers.
2600  recentRejects->insert(tx.GetHash());
2601  }
2602  } else {
2604  // Do not use rejection cache for witness transactions or
2605  // witness-stripped transactions, as they can have been malleated.
2606  // See https://github.com/bitcoin/bitcoin/issues/8279 for details.
2607  assert(recentRejects);
2608  recentRejects->insert(tx.GetHash());
2609  if (RecursiveDynamicUsage(*ptx) < 100000) {
2611  }
2612  } else if (tx.HasWitness() && RecursiveDynamicUsage(*ptx) < 100000) {
2614  }
2615 
2616  if (pfrom->HasPermission(PF_FORCERELAY)) {
2617  // Always relay transactions received from whitelisted peers, even
2618  // if they were already in the mempool or rejected from it due
2619  // to policy, allowing the node to function as a gateway for
2620  // nodes hidden behind it.
2621  //
2622  // Never relay transactions that might result in being
2623  // disconnected (or banned).
2624  if (state.IsInvalid() && TxRelayMayResultInDisconnect(state)) {
2625  LogPrintf("Not relaying invalid transaction %s from whitelisted peer=%d (%s)\n", tx.GetHash().ToString(), pfrom->GetId(), FormatStateMessage(state));
2626  } else {
2627  LogPrintf("Force relaying tx %s from whitelisted peer=%d\n", tx.GetHash().ToString(), pfrom->GetId());
2629  }
2630  }
2631  }
2632 
2633  for (const CTransactionRef& removedTx : lRemovedTxn)
2634  AddToCompactExtraTransactions(removedTx);
2635 
2636  // If a tx has been detected by recentRejects, we will have reached
2637  // this point and the tx will have been ignored. Because we haven't run
2638  // the tx through AcceptToMemoryPool, we won't have computed a DoS
2639  // score for it or determined exactly why we consider it invalid.
2640  //
2641  // This means we won't penalize any peer subsequently relaying a DoSy
2642  // tx (even if we penalized the first peer who gave it to us) because
2643  // we have to account for recentRejects showing false positives. In
2644  // other words, we shouldn't penalize a peer if we aren't *sure* they
2645  // submitted a DoSy tx.
2646  //
2647  // Note that recentRejects doesn't just record DoSy or invalid
2648  // transactions, but any tx not accepted by the mempool, which may be
2649  // due to node policy (vs. consensus). So we can't blanket penalize a
2650  // peer simply for relaying a tx that our recentRejects has caught,
2651  // regardless of false positives.
2652 
2653  if (state.IsInvalid())
2654  {
2655  LogPrint(BCLog::MEMPOOLREJ, "%s from peer=%d was not accepted: %s\n", tx.GetHash().ToString(),
2656  pfrom->GetId(),
2657  FormatStateMessage(state));
2658  MaybePunishNodeForTx(pfrom->GetId(), state);
2659  }
2660  return true;
2661  }
2662 
2663  if (strCommand == NetMsgType::CMPCTBLOCK)
2664  {
2665  // Ignore cmpctblock received while importing
2666  if (fImporting || fReindex) {
2667  LogPrint(BCLog::NET, "Unexpected cmpctblock message received from peer %d\n", pfrom->GetId());
2668  return true;
2669  }
2670 
2671  CBlockHeaderAndShortTxIDs cmpctblock;
2672  vRecv >> cmpctblock;
2673 
2674  bool received_new_header = false;
2675 
2676  {
2677  LOCK(cs_main);
2678 
2679  if (!LookupBlockIndex(cmpctblock.header.hashPrevBlock)) {
2680  // Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
2681  if (!::ChainstateActive().IsInitialBlockDownload())
2682  connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, ::ChainActive().GetLocator(pindexBestHeader), uint256()));
2683  return true;
2684  }
2685 
2686  if (!LookupBlockIndex(cmpctblock.header.GetHash())) {
2687  received_new_header = true;
2688  }
2689  }
2690 
2691  const CBlockIndex *pindex = nullptr;
2692  BlockValidationState state;
2693  if (!ProcessNewBlockHeaders({cmpctblock.header}, state, chainparams, &pindex)) {
2694  if (state.IsInvalid()) {
2695  MaybePunishNodeForBlock(pfrom->GetId(), state, /*via_compact_block*/ true, "invalid header via cmpctblock");
2696  return true;
2697  }
2698  }
2699 
2700  // When we succeed in decoding a block's txids from a cmpctblock
2701  // message we typically jump to the BLOCKTXN handling code, with a
2702  // dummy (empty) BLOCKTXN message, to re-use the logic there in
2703  // completing processing of the putative block (without cs_main).
2704  bool fProcessBLOCKTXN = false;
2706 
2707  // If we end up treating this as a plain headers message, call that as well
2708  // without cs_main.
2709  bool fRevertToHeaderProcessing = false;
2710 
2711  // Keep a CBlock for "optimistic" compactblock reconstructions (see
2712  // below)
2713  std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
2714  bool fBlockReconstructed = false;
2715 
2716  {
2717  LOCK2(cs_main, g_cs_orphans);
2718  // If AcceptBlockHeader returned true, it set pindex
2719  assert(pindex);
2720  UpdateBlockAvailability(pfrom->GetId(), pindex->GetBlockHash());
2721 
2722  CNodeState *nodestate = State(pfrom->GetId());
2723 
2724  // If this was a new header with more work than our tip, update the
2725  // peer's last block announcement time
2726  if (received_new_header && pindex->nChainWork > ::ChainActive().Tip()->nChainWork) {
2727  nodestate->m_last_block_announcement = GetTime();
2728  }
2729 
2730  std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator blockInFlightIt = mapBlocksInFlight.find(pindex->GetBlockHash());
2731  bool fAlreadyInFlight = blockInFlightIt != mapBlocksInFlight.end();
2732 
2733  if (pindex->nStatus & BLOCK_HAVE_DATA) // Nothing to do here
2734  return true;
2735 
2736  if (pindex->nChainWork <= ::ChainActive().Tip()->nChainWork || // We know something better
2737  pindex->nTx != 0) { // We had this block at some point, but pruned it
2738  if (fAlreadyInFlight) {
2739  // We requested this block for some reason, but our mempool will probably be useless
2740  // so we just grab the block via normal getdata
2741  std::vector<CInv> vInv(1);
2742  vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom), cmpctblock.header.GetHash());
2743  connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
2744  }
2745  return true;
2746  }
2747 
2748  // If we're not close to tip yet, give up and let parallel block fetch work its magic
2749  if (!fAlreadyInFlight && !CanDirectFetch(chainparams.GetConsensus()))
2750  return true;
2751 
2752  if (IsWitnessEnabled(pindex->pprev, chainparams.GetConsensus()) && !nodestate->fSupportsDesiredCmpctVersion) {
2753  // Don't bother trying to process compact blocks from v1 peers
2754  // after segwit activates.
2755  return true;
2756  }
2757 
2758  // We want to be a bit conservative just to be extra careful about DoS
2759  // possibilities in compact block processing...
2760  if (pindex->nHeight <= ::ChainActive().Height() + 2) {
2761  if ((!fAlreadyInFlight && nodestate->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) ||
2762  (fAlreadyInFlight && blockInFlightIt->second.first == pfrom->GetId())) {
2763  std::list<QueuedBlock>::iterator* queuedBlockIt = nullptr;
2764  if (!MarkBlockAsInFlight(pfrom->GetId(), pindex->GetBlockHash(), pindex, &queuedBlockIt)) {
2765  if (!(*queuedBlockIt)->partialBlock)
2766  (*queuedBlockIt)->partialBlock.reset(new PartiallyDownloadedBlock(&mempool));
2767  else {
2768  // The block was already in flight using compact blocks from the same peer
2769  LogPrint(BCLog::NET, "Peer sent us compact block we were already syncing!\n");
2770  return true;
2771  }
2772  }
2773 
2774  PartiallyDownloadedBlock& partialBlock = *(*queuedBlockIt)->partialBlock;
2775  ReadStatus status = partialBlock.InitData(cmpctblock, vExtraTxnForCompact);
2776  if (status == READ_STATUS_INVALID) {
2777  MarkBlockAsReceived(pindex->GetBlockHash()); // Reset in-flight state in case of whitelist
2778  Misbehaving(pfrom->GetId(), 100, strprintf("Peer %d sent us invalid compact block\n", pfrom->GetId()));
2779  return true;
2780  } else if (status == READ_STATUS_FAILED) {
2781  // Duplicate txindexes, the block is now in-flight, so just request it
2782  std::vector<CInv> vInv(1);
2783  vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom), cmpctblock.header.GetHash());
2784  connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
2785  return true;
2786  }
2787 
2789  for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
2790  if (!partialBlock.IsTxAvailable(i))
2791  req.indexes.push_back(i);
2792  }
2793  if (req.indexes.empty()) {
2794  // Dirty hack to jump to BLOCKTXN code (TODO: move message handling into their own functions)
2795  BlockTransactions txn;
2796  txn.blockhash = cmpctblock.header.GetHash();
2797  blockTxnMsg << txn;
2798  fProcessBLOCKTXN = true;
2799  } else {
2800  req.blockhash = pindex->GetBlockHash();
2801  connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETBLOCKTXN, req));
2802  }
2803  } else {
2804  // This block is either already in flight from a different
2805  // peer, or this peer has too many blocks outstanding to
2806  // download from.
2807  // Optimistically try to reconstruct anyway since we might be
2808  // able to without any round trips.
2809  PartiallyDownloadedBlock tempBlock(&mempool);
2810  ReadStatus status = tempBlock.InitData(cmpctblock, vExtraTxnForCompact);
2811  if (status != READ_STATUS_OK) {
2812  // TODO: don't ignore failures
2813  return true;
2814  }
2815  std::vector<CTransactionRef> dummy;
2816  status = tempBlock.FillBlock(*pblock, dummy);
2817  if (status == READ_STATUS_OK) {
2818  fBlockReconstructed = true;
2819  }
2820  }
2821  } else {
2822  if (fAlreadyInFlight) {
2823  // We requested this block, but its far into the future, so our
2824  // mempool will probably be useless - request the block normally
2825  std::vector<CInv> vInv(1);
2826  vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom), cmpctblock.header.GetHash());
2827  connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
2828  return true;
2829  } else {
2830  // If this was an announce-cmpctblock, we want the same treatment as a header message
2831  fRevertToHeaderProcessing = true;
2832  }
2833  }
2834  } // cs_main
2835 
2836  if (fProcessBLOCKTXN)
2837  return ProcessMessage(pfrom, NetMsgType::BLOCKTXN, blockTxnMsg, nTimeReceived, chainparams, connman, banman, interruptMsgProc);
2838 
2839  if (fRevertToHeaderProcessing) {
2840  // Headers received from HB compact block peers are permitted to be
2841  // relayed before full validation (see BIP 152), so we don't want to disconnect
2842  // the peer if the header turns out to be for an invalid block.
2843  // Note that if a peer tries to build on an invalid chain, that
2844  // will be detected and the peer will be banned.
2845  return ProcessHeadersMessage(pfrom, connman, {cmpctblock.header}, chainparams, /*via_compact_block=*/true);
2846  }
2847 
2848  if (fBlockReconstructed) {
2849  // If we got here, we were able to optimistically reconstruct a
2850  // block that is in flight from some other peer.
2851  {
2852  LOCK(cs_main);
2853  mapBlockSource.emplace(pblock->GetHash(), std::make_pair(pfrom->GetId(), false));
2854  }
2855  bool fNewBlock = false;
2856  // Setting fForceProcessing to true means that we bypass some of
2857  // our anti-DoS protections in AcceptBlock, which filters
2858  // unrequested blocks that might be trying to waste our resources
2859  // (eg disk space). Because we only try to reconstruct blocks when
2860  // we're close to caught up (via the CanDirectFetch() requirement
2861  // above, combined with the behavior of not requesting blocks until
2862  // we have a chain with at least nMinimumChainWork), and we ignore
2863  // compact blocks with less work than our tip, it is safe to treat
2864  // reconstructed compact blocks as having been requested.
2865  ProcessNewBlock(chainparams, pblock, /*fForceProcessing=*/true, &fNewBlock);
2866  if (fNewBlock) {
2867  pfrom->nLastBlockTime = GetTime();
2868  } else {
2869  LOCK(cs_main);
2870  mapBlockSource.erase(pblock->GetHash());
2871  }
2872  LOCK(cs_main); // hold cs_main for CBlockIndex::IsValid()
2873  if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS)) {
2874  // Clear download state for this block, which is in
2875  // process from some other peer. We do this after calling
2876  // ProcessNewBlock so that a malleated cmpctblock announcement
2877  // can't be used to interfere with block relay.
2878  MarkBlockAsReceived(pblock->GetHash());
2879  }
2880  }
2881  return true;
2882  }
2883 
2884  if (strCommand == NetMsgType::BLOCKTXN)
2885  {
2886  // Ignore blocktxn received while importing
2887  if (fImporting || fReindex) {
2888  LogPrint(BCLog::NET, "Unexpected blocktxn message received from peer %d\n", pfrom->GetId());
2889  return true;
2890  }
2891 
2892  BlockTransactions resp;
2893  vRecv >> resp;
2894 
2895  std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
2896  bool fBlockRead = false;
2897  {
2898  LOCK(cs_main);
2899 
2900  std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator it = mapBlocksInFlight.find(resp.blockhash);
2901  if (it == mapBlocksInFlight.end() || !it->second.second->partialBlock ||
2902  it->second.first != pfrom->GetId()) {
2903  LogPrint(BCLog::NET, "Peer %d sent us block transactions for block we weren't expecting\n", pfrom->GetId());
2904  return true;
2905  }
2906 
2907  PartiallyDownloadedBlock& partialBlock = *it->second.second->partialBlock;
2908  ReadStatus status = partialBlock.FillBlock(*pblock, resp.txn);
2909  if (status == READ_STATUS_INVALID) {
2910  MarkBlockAsReceived(resp.blockhash); // Reset in-flight state in case of whitelist
2911  Misbehaving(pfrom->GetId(), 100, strprintf("Peer %d sent us invalid compact block/non-matching block transactions\n", pfrom->GetId()));
2912  return true;
2913  } else if (status == READ_STATUS_FAILED) {
2914  // Might have collided, fall back to getdata now :(
2915  std::vector<CInv> invs;
2916  invs.push_back(CInv(MSG_BLOCK | GetFetchFlags(pfrom), resp.blockhash));
2917  connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, invs));
2918  } else {
2919  // Block is either okay, or possibly we received
2920  // READ_STATUS_CHECKBLOCK_FAILED.
2921  // Note that CheckBlock can only fail for one of a few reasons:
2922  // 1. bad-proof-of-work (impossible here, because we've already
2923  // accepted the header)
2924  // 2. merkleroot doesn't match the transactions given (already
2925  // caught in FillBlock with READ_STATUS_FAILED, so
2926  // impossible here)
2927  // 3. the block is otherwise invalid (eg invalid coinbase,
2928  // block is too big, too many legacy sigops, etc).
2929  // So if CheckBlock failed, #3 is the only possibility.
2930  // Under BIP 152, we don't DoS-ban unless proof of work is
2931  // invalid (we don't require all the stateless checks to have
2932  // been run). This is handled below, so just treat this as
2933  // though the block was successfully read, and rely on the
2934  // handling in ProcessNewBlock to ensure the block index is
2935  // updated, etc.
2936  MarkBlockAsReceived(resp.blockhash); // it is now an empty pointer
2937  fBlockRead = true;
2938  // mapBlockSource is used for potentially punishing peers and
2939  // updating which peers send us compact blocks, so the race
2940  // between here and cs_main in ProcessNewBlock is fine.
2941  // BIP 152 permits peers to relay compact blocks after validating
2942  // the header only; we should not punish peers if the block turns
2943  // out to be invalid.
2944  mapBlockSource.emplace(resp.blockhash, std::make_pair(pfrom->GetId(), false));
2945  }
2946  } // Don't hold cs_main when we call into ProcessNewBlock
2947  if (fBlockRead) {
2948  bool fNewBlock = false;
2949  // Since we requested this block (it was in mapBlocksInFlight), force it to be processed,
2950  // even if it would not be a candidate for new tip (missing previous block, chain not long enough, etc)
2951  // This bypasses some anti-DoS logic in AcceptBlock (eg to prevent
2952  // disk-space attacks), but this should be safe due to the
2953  // protections in the compact block handler -- see related comment
2954  // in compact block optimistic reconstruction handling.
2955  ProcessNewBlock(chainparams, pblock, /*fForceProcessing=*/true, &fNewBlock);
2956  if (fNewBlock) {
2957  pfrom->nLastBlockTime = GetTime();
2958  } else {
2959  LOCK(cs_main);
2960  mapBlockSource.erase(pblock->GetHash());
2961  }
2962  }
2963  return true;
2964  }
2965 
2966  if (strCommand == NetMsgType::HEADERS)
2967  {
2968  // Ignore headers received while importing
2969  if (fImporting || fReindex) {
2970  LogPrint(BCLog::NET, "Unexpected headers message received from peer %d\n", pfrom->GetId());
2971  return true;
2972  }
2973 
2974  std::vector<CBlockHeader> headers;
2975 
2976  // Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks.
2977  unsigned int nCount = ReadCompactSize(vRecv);
2978  if (nCount > MAX_HEADERS_RESULTS) {
2979  LOCK(cs_main);
2980  Misbehaving(pfrom->GetId(), 20, strprintf("headers message size = %u", nCount));
2981  return false;
2982  }
2983  headers.resize(nCount);
2984  for (unsigned int n = 0; n < nCount; n++) {
2985  vRecv >> headers[n];
2986  ReadCompactSize(vRecv); // ignore tx count; assume it is 0.
2987  }
2988 
2989  return ProcessHeadersMessage(pfrom, connman, headers, chainparams, /*via_compact_block=*/false);
2990  }
2991 
2992  if (strCommand == NetMsgType::BLOCK)
2993  {
2994  // Ignore block received while importing
2995  if (fImporting || fReindex) {
2996  LogPrint(BCLog::NET, "Unexpected block message received from peer %d\n", pfrom->GetId());
2997  return true;
2998  }
2999 
3000  std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
3001  vRecv >> *pblock;
3002 
3003  LogPrint(BCLog::NET, "received block %s peer=%d\n", pblock->GetHash().ToString(), pfrom->GetId());
3004 
3005  bool forceProcessing = false;
3006  const uint256 hash(pblock->GetHash());
3007  {
3008  LOCK(cs_main);
3009  // Also always process if we requested the block explicitly, as we may
3010  // need it even though it is not a candidate for a new best tip.
3011  forceProcessing |= MarkBlockAsReceived(hash);
3012  // mapBlockSource is only used for punishing peers and setting
3013  // which peers send us compact blocks, so the race between here and
3014  // cs_main in ProcessNewBlock is fine.
3015  mapBlockSource.emplace(hash, std::make_pair(pfrom->GetId(), true));
3016  }
3017  bool fNewBlock = false;
3018  ProcessNewBlock(chainparams, pblock, forceProcessing, &fNewBlock);
3019  if (fNewBlock) {
3020  pfrom->nLastBlockTime = GetTime();
3021  } else {
3022  LOCK(cs_main);
3023  mapBlockSource.erase(pblock->GetHash());
3024  }
3025  return true;
3026  }
3027 
3028  if (strCommand == NetMsgType::GETADDR) {
3029  // This asymmetric behavior for inbound and outbound connections was introduced
3030  // to prevent a fingerprinting attack: an attacker can send specific fake addresses
3031  // to users' AddrMan and later request them by sending getaddr messages.
3032  // Making nodes which are behind NAT and can only make outgoing connections ignore
3033  // the getaddr message mitigates the attack.
3034  if (!pfrom->fInbound) {
3035  LogPrint(BCLog::NET, "Ignoring \"getaddr\" from outbound connection. peer=%d\n", pfrom->GetId());
3036  return true;
3037  }
3038  if (!pfrom->IsAddrRelayPeer()) {
3039  LogPrint(BCLog::NET, "Ignoring \"getaddr\" from block-relay-only connection. peer=%d\n", pfrom->GetId());
3040  return true;
3041  }
3042 
3043  // Only send one GetAddr response per connection to reduce resource waste
3044  // and discourage addr stamping of INV announcements.
3045  if (pfrom->fSentAddr) {
3046  LogPrint(BCLog::NET, "Ignoring repeated \"getaddr\". peer=%d\n", pfrom->GetId());
3047  return true;
3048  }
3049  pfrom->fSentAddr = true;
3050 
3051  pfrom->vAddrToSend.clear();
3052  std::vector<CAddress> vAddr = connman->GetAddresses();
3053  FastRandomContext insecure_rand;
3054  for (const CAddress &addr : vAddr) {
3055  if (!banman->IsBanned(addr)) {
3056  pfrom->PushAddress(addr, insecure_rand);
3057  }
3058  }
3059  return true;
3060  }
3061 
3062  if (strCommand == NetMsgType::MEMPOOL) {
3063  if (!(pfrom->GetLocalServices() & NODE_BLOOM) && !pfrom->HasPermission(PF_MEMPOOL))
3064  {
3065  if (!pfrom->HasPermission(PF_NOBAN))
3066  {
3067  LogPrint(BCLog::NET, "mempool request with bloom filters disabled, disconnect peer=%d\n", pfrom->GetId());
3068  pfrom->fDisconnect = true;
3069  }
3070  return true;
3071  }
3072 
3073  if (connman->OutboundTargetReached(false) && !pfrom->HasPermission(PF_MEMPOOL))
3074  {
3075  if (!pfrom->HasPermission(PF_NOBAN))
3076  {
3077  LogPrint(BCLog::NET, "mempool request with bandwidth limit reached, disconnect peer=%d\n", pfrom->GetId());
3078  pfrom->fDisconnect = true;
3079  }
3080  return true;
3081  }
3082 
3083  if (pfrom->m_tx_relay != nullptr) {
3084  LOCK(pfrom->m_tx_relay->cs_tx_inventory);
3085  pfrom->m_tx_relay->fSendMempool = true;
3086  }
3087  return true;
3088  }
3089 
3090  if (strCommand == NetMsgType::PING) {
3091  if (pfrom->nVersion > BIP0031_VERSION)
3092  {
3093  uint64_t nonce = 0;
3094  vRecv >> nonce;
3095  // Echo the message back with the nonce. This allows for two useful features:
3096  //
3097  // 1) A remote node can quickly check if the connection is operational
3098  // 2) Remote nodes can measure the latency of the network thread. If this node
3099  // is overloaded it won't respond to pings quickly and the remote node can
3100  // avoid sending us more work, like chain download requests.
3101  //
3102  // The nonce stops the remote getting confused between different pings: without
3103  // it, if the remote node sends a ping once per second and this node takes 5
3104  // seconds to respond to each, the 5th ping the remote sends would appear to
3105  // return very quickly.
3106  connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::PONG, nonce));
3107  }
3108  return true;
3109  }
3110 
3111  if (strCommand == NetMsgType::PONG) {
3112  int64_t pingUsecEnd = nTimeReceived;
3113  uint64_t nonce = 0;
3114  size_t nAvail = vRecv.in_avail();
3115  bool bPingFinished = false;
3116  std::string sProblem;
3117 
3118  if (nAvail >= sizeof(nonce)) {
3119  vRecv >> nonce;
3120 
3121  // Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
3122  if (pfrom->nPingNonceSent != 0) {
3123  if (nonce == pfrom->nPingNonceSent) {
3124  // Matching pong received, this ping is no longer outstanding
3125  bPingFinished = true;
3126  int64_t pingUsecTime = pingUsecEnd - pfrom->nPingUsecStart;
3127  if (pingUsecTime > 0) {
3128  // Successful ping time measurement, replace previous
3129  pfrom->nPingUsecTime = pingUsecTime;
3130  pfrom->nMinPingUsecTime = std::min(pfrom->nMinPingUsecTime.load(), pingUsecTime);
3131  } else {
3132  // This should never happen
3133  sProblem = "Timing mishap";
3134  }
3135  } else {
3136  // Nonce mismatches are normal when pings are overlapping
3137  sProblem = "Nonce mismatch";
3138  if (nonce == 0) {
3139  // This is most likely a bug in another implementation somewhere; cancel this ping
3140  bPingFinished = true;
3141  sProblem = "Nonce zero";
3142  }
3143  }
3144  } else {
3145  sProblem = "Unsolicited pong without ping";
3146  }
3147  } else {
3148  // This is most likely a bug in another implementation somewhere; cancel this ping
3149  bPingFinished = true;
3150  sProblem = "Short payload";
3151  }
3152 
3153  if (!(sProblem.empty())) {
3154  LogPrint(BCLog::NET, "pong peer=%d: %s, %x expected, %x received, %u bytes\n",
3155  pfrom->GetId(),
3156  sProblem,
3157  pfrom->nPingNonceSent,
3158  nonce,
3159  nAvail);
3160  }
3161  if (bPingFinished) {
3162  pfrom->nPingNonceSent = 0;
3163  }
3164  return true;
3165  }
3166 
3167  if (strCommand == NetMsgType::FILTERLOAD) {
3168  CBloomFilter filter;
3169  vRecv >> filter;
3170 
3171  if (!filter.IsWithinSizeConstraints())
3172  {
3173  // There is no excuse for sending a too-large filter
3174  LOCK(cs_main);
3175  Misbehaving(pfrom->GetId(), 100);
3176  }
3177  else if (pfrom->m_tx_relay != nullptr)
3178  {
3179  LOCK(pfrom->m_tx_relay->cs_filter);
3180  pfrom->m_tx_relay->pfilter.reset(new CBloomFilter(filter));
3181  pfrom->m_tx_relay->pfilter->UpdateEmptyFull();
3182  pfrom->m_tx_relay->fRelayTxes = true;
3183  }
3184  return true;
3185  }
3186 
3187  if (strCommand == NetMsgType::FILTERADD) {
3188  std::vector<unsigned char> vData;
3189  vRecv >> vData;
3190 
3191  // Nodes must NEVER send a data item > 520 bytes (the max size for a script data object,
3192  // and thus, the maximum size any matched object can have) in a filteradd message
3193  bool bad = false;
3194  if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE) {
3195  bad = true;
3196  } else if (pfrom->m_tx_relay != nullptr) {
3197  LOCK(pfrom->m_tx_relay->cs_filter);
3198  if (pfrom->m_tx_relay->pfilter) {
3199  pfrom->m_tx_relay->pfilter->insert(vData);
3200  } else {
3201  bad = true;
3202  }
3203  }
3204  if (bad) {
3205  LOCK(cs_main);
3206  Misbehaving(pfrom->GetId(), 100);
3207  }
3208  return true;
3209  }
3210 
3211  if (strCommand == NetMsgType::FILTERCLEAR) {
3212  if (pfrom->m_tx_relay == nullptr) {
3213  return true;
3214  }
3215  LOCK(pfrom->m_tx_relay->cs_filter);
3216  if (pfrom->GetLocalServices() & NODE_BLOOM) {
3217  pfrom->m_tx_relay->pfilter.reset(new CBloomFilter());
3218  }
3219  pfrom->m_tx_relay->fRelayTxes = true;
3220  return true;
3221  }
3222 
3223  if (strCommand == NetMsgType::FEEFILTER) {
3224  CAmount newFeeFilter = 0;
3225  vRecv >> newFeeFilter;
3226  if (MoneyRange(newFeeFilter)) {
3227  if (pfrom->m_tx_relay != nullptr) {
3228  LOCK(pfrom->m_tx_relay->cs_feeFilter);
3229  pfrom->m_tx_relay->minFeeFilter = newFeeFilter;
3230  }
3231  LogPrint(BCLog::NET, "received: feefilter of %s from peer=%d\n", CFeeRate(newFeeFilter).ToString(), pfrom->GetId());
3232  }
3233  return true;
3234  }
3235 
3236  if (strCommand == NetMsgType::NOTFOUND) {
3237  // Remove the NOTFOUND transactions from the peer
3238  LOCK(cs_main);
3239  CNodeState *state = State(pfrom->GetId());
3240  std::vector<CInv> vInv;
3241  vRecv >> vInv;
3243  for (CInv &inv : vInv) {
3244  if (inv.type == MSG_TX || inv.type == MSG_WITNESS_TX) {
3245  // If we receive a NOTFOUND message for a txid we requested, erase
3246  // it from our data structures for this peer.
3247  auto in_flight_it = state->m_tx_download.m_tx_in_flight.find(inv.hash);
3248  if (in_flight_it == state->m_tx_download.m_tx_in_flight.end()) {
3249  // Skip any further work if this is a spurious NOTFOUND
3250  // message.
3251  continue;
3252  }
3253  state->m_tx_download.m_tx_in_flight.erase(in_flight_it);
3254  state->m_tx_download.m_tx_announced.erase(inv.hash);
3255  }
3256  }
3257  }
3258  return true;
3259  }
3260 
3261  // Ignore unknown commands for extensibility
3262  LogPrint(BCLog::NET, "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->GetId());
3263  return true;
3264 }
3265 
3267 {
3269  CNodeState &state = *State(pnode->GetId());
3270 
3271  if (state.fShouldBan) {
3272  state.fShouldBan = false;
3273  if (pnode->HasPermission(PF_NOBAN))
3274  LogPrintf("Warning: not punishing whitelisted peer %s!\n", pnode->addr.ToString());
3275  else if (pnode->m_manual_connection)
3276  LogPrintf("Warning: not punishing manually-connected peer %s!\n", pnode->addr.ToString());
3277  else if (pnode->addr.IsLocal()) {
3278  // Disconnect but don't ban _this_ local node
3279  LogPrintf("Warning: disconnecting but not banning local peer %s!\n", pnode->addr.ToString());
3280  pnode->fDisconnect = true;
3281  } else {
3282  // Disconnect and ban all nodes sharing the address
3283  if (m_banman) {
3285  }
3286  connman->DisconnectNode(pnode->addr);
3287  }
3288  return true;
3289  }
3290  return false;
3291 }
3292 
3293 bool PeerLogicValidation::ProcessMessages(CNode* pfrom, std::atomic<bool>& interruptMsgProc)
3294 {
3295  const CChainParams& chainparams = Params();
3296  //
3297  // Message format
3298  // (4) message start
3299  // (12) command
3300  // (4) size
3301  // (4) checksum
3302  // (x) data
3303  //
3304  bool fMoreWork = false;
3305 
3306  if (!pfrom->vRecvGetData.empty())
3307  ProcessGetData(pfrom, chainparams, connman, interruptMsgProc);
3308 
3309  if (!pfrom->orphan_work_set.empty()) {
3310  std::list<CTransactionRef> removed_txn;
3311  LOCK2(cs_main, g_cs_orphans);
3312  ProcessOrphanTx(connman, pfrom->orphan_work_set, removed_txn);
3313  for (const CTransactionRef& removedTx : removed_txn) {
3314  AddToCompactExtraTransactions(removedTx);
3315  }
3316  }
3317 
3318  if (pfrom->fDisconnect)
3319  return false;
3320 
3321  // this maintains the order of responses
3322  // and prevents vRecvGetData to grow unbounded
3323  if (!pfrom->vRecvGetData.empty()) return true;
3324  if (!pfrom->orphan_work_set.empty()) return true;
3325 
3326  // Don't bother if send buffer is too full to respond anyway
3327  if (pfrom->fPauseSend)
3328  return false;
3329 
3330  std::list<CNetMessage> msgs;
3331  {
3332  LOCK(pfrom->cs_vProcessMsg);
3333  if (pfrom->vProcessMsg.empty())
3334  return false;
3335  // Just take one message
3336  msgs.splice(msgs.begin(), pfrom->vProcessMsg, pfrom->vProcessMsg.begin());
3337  pfrom->nProcessQueueSize -= msgs.front().m_raw_message_size;
3338  pfrom->fPauseRecv = pfrom->nProcessQueueSize > connman->GetReceiveFloodSize();
3339  fMoreWork = !pfrom->vProcessMsg.empty();
3340  }
3341  CNetMessage& msg(msgs.front());
3342 
3343  msg.SetVersion(pfrom->GetRecvVersion());
3344  // Check network magic
3345  if (!msg.m_valid_netmagic) {
3346  LogPrint(BCLog::NET, "PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg.m_command), pfrom->GetId());
3347  pfrom->fDisconnect = true;
3348  return false;
3349  }
3350 
3351  // Check header
3352  if (!msg.m_valid_header)
3353  {
3354  LogPrint(BCLog::NET, "PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(msg.m_command), pfrom->GetId());
3355  return fMoreWork;
3356  }
3357  const std::string& strCommand = msg.m_command;
3358 
3359  // Message size
3360  unsigned int nMessageSize = msg.m_message_size;
3361 
3362  // Checksum
3363  CDataStream& vRecv = msg.m_recv;
3364  if (!msg.m_valid_checksum)
3365  {
3366  LogPrint(BCLog::NET, "%s(%s, %u bytes): CHECKSUM ERROR peer=%d\n", __func__,
3367  SanitizeString(strCommand), nMessageSize, pfrom->GetId());
3368  return fMoreWork;
3369  }
3370 
3371  // Process message
3372  bool fRet = false;
3373  try
3374  {
3375  fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.m_time, chainparams, connman, m_banman, interruptMsgProc);
3376  if (interruptMsgProc)
3377  return false;
3378  if (!pfrom->vRecvGetData.empty())
3379  fMoreWork = true;
3380  } catch (const std::exception& e) {
3381  LogPrint(BCLog::NET, "%s(%s, %u bytes): Exception '%s' (%s) caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what(), typeid(e).name());
3382  } catch (...) {
3383  LogPrint(BCLog::NET, "%s(%s, %u bytes): Unknown exception caught\n", __func__, SanitizeString(strCommand), nMessageSize);
3384  }
3385 
3386  if (!fRet) {
3387  LogPrint(BCLog::NET, "%s(%s, %u bytes) FAILED peer=%d\n", __func__, SanitizeString(strCommand), nMessageSize, pfrom->GetId());
3388  }
3389 
3390  LOCK(cs_main);
3391  CheckIfBanned(pfrom);
3392 
3393  return fMoreWork;
3394 }
3395 
3396 void PeerLogicValidation::ConsiderEviction(CNode *pto, int64_t time_in_seconds)
3397 {
3399 
3400  CNodeState &state = *State(pto->GetId());
3401  const CNetMsgMaker msgMaker(pto->GetSendVersion());
3402 
3403  if (!state.m_chain_sync.m_protect && IsOutboundDisconnectionCandidate(pto) && state.fSyncStarted) {
3404  // This is an outbound peer subject to disconnection if they don't
3405  // announce a block with as much work as the current tip within
3406  // CHAIN_SYNC_TIMEOUT + HEADERS_RESPONSE_TIME seconds (note: if
3407  // their chain has more work than ours, we should sync to it,
3408  // unless it's invalid, in which case we should find that out and
3409  // disconnect from them elsewhere).
3410  if (state.pindexBestKnownBlock != nullptr && state.pindexBestKnownBlock->nChainWork >= ::ChainActive().Tip()->nChainWork) {
3411  if (state.m_chain_sync.m_timeout != 0) {
3412  state.m_chain_sync.m_timeout = 0;
3413  state.m_chain_sync.m_work_header = nullptr;
3414  state.m_chain_sync.m_sent_getheaders = false;
3415  }
3416  } 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)) {
3417  // Our best block known by this peer is behind our tip, and we're either noticing
3418  // that for the first time, OR this peer was able to catch up to some earlier point
3419  // where we checked against our tip.
3420  // Either way, set a new timeout based on current tip.
3421  state.m_chain_sync.m_timeout = time_in_seconds + CHAIN_SYNC_TIMEOUT;
3422  state.m_chain_sync.m_work_header = ::ChainActive().Tip();
3423  state.m_chain_sync.m_sent_getheaders = false;
3424  } else if (state.m_chain_sync.m_timeout > 0 && time_in_seconds > state.m_chain_sync.m_timeout) {
3425  // No evidence yet that our peer has synced to a chain with work equal to that
3426  // of our tip, when we first detected it was behind. Send a single getheaders
3427  // message to give the peer a chance to update us.
3428  if (state.m_chain_sync.m_sent_getheaders) {
3429  // They've run out of time to catch up!
3430  LogPrintf("Disconnecting outbound peer %d for old chain, best known block = %s\n", pto->GetId(), state.pindexBestKnownBlock != nullptr ? state.pindexBestKnownBlock->GetBlockHash().ToString() : "<none>");
3431  pto->fDisconnect = true;
3432  } else {
3433  assert(state.m_chain_sync.m_work_header);
3434  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());
3435  connman->PushMessage(pto, msgMaker.Make(NetMsgType::GETHEADERS, ::ChainActive().GetLocator(state.m_chain_sync.m_work_header->pprev), uint256()));
3436  state.m_chain_sync.m_sent_getheaders = true;
3437  constexpr int64_t HEADERS_RESPONSE_TIME = 120; // 2 minutes
3438  // Bump the timeout to allow a response, which could clear the timeout
3439  // (if the response shows the peer has synced), reset the timeout (if
3440  // the peer syncs to the required work but not to our tip), or result
3441  // in disconnect (if we advance to the timeout and pindexBestKnownBlock
3442  // has not sufficiently progressed)
3443  state.m_chain_sync.m_timeout = time_in_seconds + HEADERS_RESPONSE_TIME;
3444  }
3445  }
3446  }
3447 }
3448 
3450 {
3451  // Check whether we have too many outbound peers
3452  int extra_peers = connman->GetExtraOutboundCount();
3453  if (extra_peers > 0) {
3454  // If we have more outbound peers than we target, disconnect one.
3455  // Pick the outbound peer that least recently announced
3456  // us a new block, with ties broken by choosing the more recent
3457  // connection (higher node id)
3458  NodeId worst_peer = -1;
3459  int64_t oldest_block_announcement = std::numeric_limits<int64_t>::max();
3460 
3461  connman->ForEachNode([&](CNode* pnode) {
3463 
3464  // Ignore non-outbound peers, or nodes marked for disconnect already
3465  if (!IsOutboundDisconnectionCandidate(pnode) || pnode->fDisconnect) return;
3466  CNodeState *state = State(pnode->GetId());
3467  if (state == nullptr) return; // shouldn't be possible, but just in case
3468  // Don't evict our protected peers
3469  if (state->m_chain_sync.m_protect) return;
3470  // Don't evict our block-relay-only peers.
3471  if (pnode->m_tx_relay == nullptr) return;
3472  if (state->m_last_block_announcement < oldest_block_announcement || (state->m_last_block_announcement == oldest_block_announcement && pnode->GetId() > worst_peer)) {
3473  worst_peer = pnode->GetId();
3474  oldest_block_announcement = state->m_last_block_announcement;
3475  }
3476  });
3477  if (worst_peer != -1) {
3478  bool disconnected = connman->ForNode(worst_peer, [&](CNode *pnode) {
3480 
3481  // Only disconnect a peer that has been connected to us for
3482  // some reasonable fraction of our check-frequency, to give
3483  // it time for new information to have arrived.
3484  // Also don't disconnect any peer we're trying to download a
3485  // block from.
3486  CNodeState &state = *State(pnode->GetId());
3487  if (time_in_seconds - pnode->nTimeConnected > MINIMUM_CONNECT_TIME && state.nBlocksInFlight == 0) {
3488  LogPrint(BCLog::NET, "disconnecting extra outbound peer=%d (last block announcement received at time %d)\n", pnode->GetId(), oldest_block_announcement);
3489  pnode->fDisconnect = true;
3490  return true;
3491  } else {
3492  LogPrint(BCLog::NET, "keeping outbound peer=%d chosen for eviction (connect time: %d, blocks_in_flight: %d)\n", pnode->GetId(), pnode->nTimeConnected, state.nBlocksInFlight);
3493  return false;
3494  }
3495  });
3496  if (disconnected) {
3497  // If we disconnected an extra peer, that means we successfully
3498  // connected to at least one peer after the last time we
3499  // detected a stale tip. Don't try any more extra peers until
3500  // we next detect a stale tip, to limit the load we put on the
3501  // network from these extra connections.
3502  connman->SetTryNewOutboundPeer(false);
3503  }
3504  }
3505  }
3506 }
3507 
3509 {
3510  LOCK(cs_main);
3511 
3512  if (connman == nullptr) return;
3513 
3514  int64_t time_in_seconds = GetTime();
3515 
3516  EvictExtraOutboundPeers(time_in_seconds);
3517 
3518  if (time_in_seconds > m_stale_tip_check_time) {
3519  // Check whether our tip is stale, and if so, allow using an extra
3520  // outbound peer
3521  if (!fImporting && !fReindex && connman->GetNetworkActive() && connman->GetUseAddrmanOutgoing() && TipMayBeStale(consensusParams)) {
3522  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);
3523  connman->SetTryNewOutboundPeer(true);
3524  } else if (connman->GetTryNewOutboundPeer()) {
3525  connman->SetTryNewOutboundPeer(false);
3526  }
3527  m_stale_tip_check_time = time_in_seconds + STALE_CHECK_INTERVAL;
3528  }
3529 }
3530 
3531 namespace {
3532 class CompareInvMempoolOrder
3533 {
3534  CTxMemPool *mp;
3535 public:
3536  explicit CompareInvMempoolOrder(CTxMemPool *_mempool)
3537  {
3538  mp = _mempool;
3539  }
3540 
3541  bool operator()(std::set<uint256>::iterator a, std::set<uint256>::iterator b)
3542  {
3543  /* As std::make_heap produces a max-heap, we want the entries with the
3544  * fewest ancestors/highest fee to sort later. */
3545  return mp->CompareDepthAndScore(*b, *a);
3546  }
3547 };
3548 }
3549 
3551 {
3552  const Consensus::Params& consensusParams = Params().GetConsensus();
3553  {
3554  // Don't send anything until the version handshake is complete
3555  if (!pto->fSuccessfullyConnected || pto->fDisconnect)
3556  return true;
3557 
3558  // If we get here, the outgoing message serialization version is set and can't change.
3559  const CNetMsgMaker msgMaker(pto->GetSendVersion());
3560 
3561  //
3562  // Message: ping
3563  //
3564  bool pingSend = false;
3565  if (pto->fPingQueued) {
3566  // RPC ping request by user
3567  pingSend = true;
3568  }
3569  if (pto->nPingNonceSent == 0 && pto->nPingUsecStart + PING_INTERVAL * 1000000 < GetTimeMicros()) {
3570  // Ping automatically sent as a latency probe & keepalive.
3571  pingSend = true;
3572  }
3573  if (pingSend) {
3574  uint64_t nonce = 0;
3575  while (nonce == 0) {
3576  GetRandBytes((unsigned char*)&nonce, sizeof(nonce));
3577  }
3578  pto->fPingQueued = false;
3579  pto->nPingUsecStart = GetTimeMicros();
3580  if (pto->nVersion > BIP0031_VERSION) {
3581  pto->nPingNonceSent = nonce;
3582  connman->PushMessage(pto, msgMaker.Make(NetMsgType::PING, nonce));
3583  } else {
3584  // Peer is too old to support ping command with nonce, pong will never arrive.
3585  pto->nPingNonceSent = 0;
3586  connman->PushMessage(pto, msgMaker.Make(NetMsgType::PING));
3587  }
3588  }
3589 
3590  TRY_LOCK(cs_main, lockMain);
3591  if (!lockMain)
3592  return true;
3593 
3594  if (CheckIfBanned(pto)) return true;
3595 
3596  CNodeState &state = *State(pto->GetId());
3597 
3598  // Address refresh broadcast
3599  int64_t nNow = GetTimeMicros();
3600  auto current_time = GetTime<std::chrono::microseconds>();
3601 
3602  if (pto->IsAddrRelayPeer() && !::ChainstateActive().IsInitialBlockDownload() && pto->nNextLocalAddrSend < nNow) {
3603  AdvertiseLocal(pto);
3604  pto->nNextLocalAddrSend = PoissonNextSend(nNow, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
3605  }
3606 
3607  //
3608  // Message: addr
3609  //
3610  if (pto->IsAddrRelayPeer() && pto->nNextAddrSend < nNow) {
3611  pto->nNextAddrSend = PoissonNextSend(nNow, AVG_ADDRESS_BROADCAST_INTERVAL);
3612  std::vector<CAddress> vAddr;
3613  vAddr.reserve(pto->vAddrToSend.size());
3614  assert(pto->m_addr_known);
3615  for (const CAddress& addr : pto->vAddrToSend)
3616  {
3617  if (!pto->m_addr_known->contains(addr.GetKey()))
3618  {
3619  pto->m_addr_known->insert(addr.GetKey());
3620  vAddr.push_back(addr);
3621  // receiver rejects addr messages larger than 1000
3622  if (vAddr.size() >= 1000)
3623  {
3624  connman->PushMessage(pto, msgMaker.Make(NetMsgType::ADDR, vAddr));
3625  vAddr.clear();
3626  }
3627  }
3628  }
3629  pto->vAddrToSend.clear();
3630  if (!vAddr.empty())
3631  connman->PushMessage(pto, msgMaker.Make(NetMsgType::ADDR, vAddr));
3632  // we only send the big addr message once
3633  if (pto->vAddrToSend.capacity() > 40)
3634  pto->vAddrToSend.shrink_to_fit();
3635  }
3636 
3637  // Start block sync
3638  if (pindexBestHeader == nullptr)
3640  bool fFetch = state.fPreferredDownload || (nPreferredDownload == 0 && !pto->fClient && !pto->fOneShot); // Download if this is a nice peer, or we have no nice peers and this one might do.
3641  if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex) {
3642  // Only actively request headers from a single peer, unless we're close to today.
3643  if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
3644  state.fSyncStarted = true;
3646  nSyncStarted++;
3647  const CBlockIndex *pindexStart = pindexBestHeader;
3648  /* If possible, start at the block preceding the currently
3649  best known header. This ensures that we always get a
3650  non-empty list of headers back as long as the peer
3651  is up-to-date. With a non-empty response, we can initialise
3652  the peer's known best block. This wouldn't be possible
3653  if we requested starting at pindexBestHeader and
3654  got back an empty response. */
3655  if (pindexStart->pprev)
3656  pindexStart = pindexStart->pprev;
3657  LogPrint(BCLog::NET, "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->nHeight, pto->GetId(), pto->nStartingHeight);
3658  connman->PushMessage(pto, msgMaker.Make(NetMsgType::GETHEADERS, ::ChainActive().GetLocator(pindexStart), uint256()));
3659  }
3660  }
3661 
3662  //
3663  // Try sending block announcements via headers
3664  //
3665  {
3666  // If we have less than MAX_BLOCKS_TO_ANNOUNCE in our
3667  // list of block hashes we're relaying, and our peer wants
3668  // headers announcements, then find the first header
3669  // not yet known to our peer but would connect, and send.
3670  // If no header would connect, or if we have too many
3671  // blocks, or if the peer doesn't want headers, just
3672  // add all to the inv queue.
3673  LOCK(pto->cs_inventory);
3674  std::vector<CBlock> vHeaders;
3675  bool fRevertToInv = ((!state.fPreferHeaders &&
3676  (!state.fPreferHeaderAndIDs || pto->vBlockHashesToAnnounce.size() > 1)) ||
3677  pto->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE);
3678  const CBlockIndex *pBestIndex = nullptr; // last header queued for delivery
3679  ProcessBlockAvailability(pto->GetId()); // ensure pindexBestKnownBlock is up-to-date
3680 
3681  if (!fRevertToInv) {
3682  bool fFoundStartingHeader = false;
3683  // Try to find first header that our peer doesn't have, and
3684  // then send all headers past that one. If we come across any
3685  // headers that aren't on ::ChainActive(), give up.
3686  for (const uint256 &hash : pto->vBlockHashesToAnnounce) {
3687  const CBlockIndex* pindex = LookupBlockIndex(hash);
3688  assert(pindex);
3689  if (::ChainActive()[pindex->nHeight] != pindex) {
3690  // Bail out if we reorged away from this block
3691  fRevertToInv = true;
3692  break;
3693  }
3694  if (pBestIndex != nullptr && pindex->pprev != pBestIndex) {
3695  // This means that the list of blocks to announce don't
3696  // connect to each other.
3697  // This shouldn't really be possible to hit during
3698  // regular operation (because reorgs should take us to
3699  // a chain that has some block not on the prior chain,
3700  // which should be caught by the prior check), but one
3701  // way this could happen is by using invalidateblock /
3702  // reconsiderblock repeatedly on the tip, causing it to
3703  // be added multiple times to vBlockHashesToAnnounce.
3704  // Robustly deal with this rare situation by reverting
3705  // to an inv.
3706  fRevertToInv = true;
3707  break;
3708  }
3709  pBestIndex = pindex;
3710  if (fFoundStartingHeader) {
3711  // add this to the headers message
3712  vHeaders.push_back(pindex->GetBlockHeader());
3713  } else if (PeerHasHeader(&state, pindex)) {
3714  continue; // keep looking for the first new block
3715  } else if (pindex->pprev == nullptr || PeerHasHeader(&state, pindex->pprev)) {
3716  // Peer doesn't have this header but they do have the prior one.
3717  // Start sending headers.
3718  fFoundStartingHeader = true;
3719  vHeaders.push_back(pindex->GetBlockHeader());
3720  } else {
3721  // Peer doesn't have this header or the prior one -- nothing will
3722  // connect, so bail out.
3723  fRevertToInv = true;
3724  break;
3725  }
3726  }
3727  }
3728  if (!fRevertToInv && !vHeaders.empty()) {
3729  if (vHeaders.size() == 1 && state.fPreferHeaderAndIDs) {
3730  // We only send up to 1 block as header-and-ids, as otherwise
3731  // probably means we're doing an initial-ish-sync or they're slow
3732  LogPrint(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", __func__,
3733  vHeaders.front().GetHash().ToString(), pto->GetId());
3734 
3735  int nSendFlags = state.fWantsCmpctWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
3736 
3737  bool fGotBlockFromCache = false;
3738  {
3739  LOCK(cs_most_recent_block);
3740  if (most_recent_block_hash == pBestIndex->GetBlockHash()) {
3741  if (state.fWantsCmpctWitness || !fWitnessesPresentInMostRecentCompactBlock)
3742  connman->PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, *most_recent_compact_block));
3743  else {
3744  CBlockHeaderAndShortTxIDs cmpctblock(*most_recent_block, state.fWantsCmpctWitness);
3745  connman->PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
3746  }
3747  fGotBlockFromCache = true;
3748  }
3749  }
3750  if (!fGotBlockFromCache) {
3751  CBlock block;
3752  bool ret = ReadBlockFromDisk(block, pBestIndex, consensusParams);
3753  assert(ret);
3754  CBlockHeaderAndShortTxIDs cmpctblock(block, state.fWantsCmpctWitness);
3755  connman->PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
3756  }
3757  state.pindexBestHeaderSent = pBestIndex;
3758  } else if (state.fPreferHeaders) {
3759  if (vHeaders.size() > 1) {
3760  LogPrint(BCLog::NET, "%s: %u headers, range (%s, %s), to peer=%d\n", __func__,
3761  vHeaders.size(),
3762  vHeaders.front().GetHash().ToString(),
3763  vHeaders.back().GetHash().ToString(), pto->GetId());
3764  } else {
3765  LogPrint(BCLog::NET, "%s: sending header %s to peer=%d\n", __func__,
3766  vHeaders.front().GetHash().ToString(), pto->GetId());
3767  }
3768  connman->PushMessage(pto, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
3769  state.pindexBestHeaderSent = pBestIndex;
3770  } else
3771  fRevertToInv = true;
3772  }
3773  if (fRevertToInv) {
3774  // If falling back to using an inv, just try to inv the tip.
3775  // The last entry in vBlockHashesToAnnounce was our tip at some point
3776  // in the past.
3777  if (!pto->vBlockHashesToAnnounce.empty()) {
3778  const uint256 &hashToAnnounce = pto->vBlockHashesToAnnounce.back();
3779  const CBlockIndex* pindex = LookupBlockIndex(hashToAnnounce);
3780  assert(pindex);
3781 
3782  // Warn if we're announcing a block that is not on the main chain.
3783  // This should be very rare and could be optimized out.
3784  // Just log for now.
3785  if (::ChainActive()[pindex->nHeight] != pindex) {
3786  LogPrint(BCLog::NET, "Announcing block %s not on main chain (tip=%s)\n",
3787  hashToAnnounce.ToString(), ::ChainActive().Tip()->GetBlockHash().ToString());
3788  }
3789 
3790  // If the peer's chain has this block, don't inv it back.
3791  if (!PeerHasHeader(&state, pindex)) {
3792  pto->PushInventory(CInv(MSG_BLOCK, hashToAnnounce));
3793  LogPrint(BCLog::NET, "%s: sending inv peer=%d hash=%s\n", __func__,
3794  pto->GetId(), hashToAnnounce.ToString());
3795  }
3796  }
3797  }
3798  pto->vBlockHashesToAnnounce.clear();
3799  }
3800 
3801  //
3802  // Message: inventory
3803  //
3804  std::vector<CInv> vInv;
3805  {
3806  LOCK(pto->cs_inventory);
3807  vInv.reserve(std::max<size_t>(pto->vInventoryBlockToSend.size(), INVENTORY_BROADCAST_MAX));
3808 
3809  // Add blocks
3810  for (const uint256& hash : pto->vInventoryBlockToSend) {
3811  vInv.push_back(CInv(MSG_BLOCK, hash));
3812  if (vInv.size() == MAX_INV_SZ) {
3813  connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
3814  vInv.clear();
3815  }
3816  }
3817  pto->vInventoryBlockToSend.clear();
3818 
3819  if (pto->m_tx_relay != nullptr) {
3820  LOCK(pto->m_tx_relay->cs_tx_inventory);
3821  // Check whether periodic sends should happen
3822  bool fSendTrickle = pto->HasPermission(PF_NOBAN);
3823  if (pto->m_tx_relay->nNextInvSend < current_time) {
3824  fSendTrickle = true;
3825  if (pto->fInbound) {
3826  pto->m_tx_relay->nNextInvSend = std::chrono::microseconds{connman->PoissonNextSendInbound(nNow, INVENTORY_BROADCAST_INTERVAL)};
3827  } else {
3828  // Use half the delay for outbound peers, as there is less privacy concern for them.
3829  pto->m_tx_relay->nNextInvSend = PoissonNextSend(current_time, std::chrono::seconds{INVENTORY_BROADCAST_INTERVAL >> 1});
3830  }
3831  }
3832 
3833  // Time to send but the peer has requested we not relay transactions.
3834  if (fSendTrickle) {
3835  LOCK(pto->m_tx_relay->cs_filter);
3836  if (!pto->m_tx_relay->fRelayTxes) pto->m_tx_relay->setInventoryTxToSend.clear();
3837  }
3838 
3839  // Respond to BIP35 mempool requests
3840  if (fSendTrickle && pto->m_tx_relay->fSendMempool) {
3841  auto vtxinfo = mempool.infoAll();
3842  pto->m_tx_relay->fSendMempool = false;
3843  CFeeRate filterrate;
3844  {
3845  LOCK(pto->m_tx_relay->cs_feeFilter);
3846  filterrate = CFeeRate(pto->m_tx_relay->minFeeFilter);
3847  }
3848 
3849  LOCK(pto->m_tx_relay->cs_filter);
3850 
3851  for (const auto& txinfo : vtxinfo) {
3852  const uint256& hash = txinfo.tx->GetHash();
3853  CInv inv(MSG_TX, hash);
3854  pto->m_tx_relay->setInventoryTxToSend.erase(hash);
3855  // Don't send transactions that peers will not put into their mempool
3856  if (txinfo.fee < filterrate.GetFee(txinfo.vsize)) {
3857  continue;
3858  }
3859  if (pto->m_tx_relay->pfilter) {
3860  if (!pto->m_tx_relay->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
3861  }
3862  pto->m_tx_relay->filterInventoryKnown.insert(hash);
3863  vInv.push_back(inv);
3864  if (vInv.size() == MAX_INV_SZ) {
3865  connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
3866  vInv.clear();
3867  }
3868  }
3869  pto->m_tx_relay->m_last_mempool_req = GetTime<std::chrono::seconds>();
3870  }
3871 
3872  // Determine transactions to relay
3873  if (fSendTrickle) {
3874  // Produce a vector with all candidates for sending
3875  std::vector<std::set<uint256>::iterator> vInvTx;
3876  vInvTx.reserve(pto->m_tx_relay->setInventoryTxToSend.size());
3877  for (std::set<uint256>::iterator it = pto->m_tx_relay->setInventoryTxToSend.begin(); it != pto->m_tx_relay->setInventoryTxToSend.end(); it++) {
3878  vInvTx.push_back(it);
3879  }
3880  CFeeRate filterrate;
3881  {
3882  LOCK(pto->m_tx_relay->cs_feeFilter);
3883  filterrate = CFeeRate(pto->m_tx_relay->minFeeFilter);
3884  }
3885  // Topologically and fee-rate sort the inventory we send for privacy and priority reasons.
3886  // A heap is used so that not all items need sorting if only a few are being sent.
3887  CompareInvMempoolOrder compareInvMempoolOrder(&mempool);
3888  std::make_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
3889  // No reason to drain out at many times the network's capacity,
3890  // especially since we have many peers and some will draw much shorter delays.
3891  unsigned int nRelayedTransactions = 0;
3892  LOCK(pto->m_tx_relay->cs_filter);
3893  while (!vInvTx.empty() && nRelayedTransactions < INVENTORY_BROADCAST_MAX) {
3894  // Fetch the top element from the heap
3895  std::pop_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
3896  std::set<uint256>::iterator it = vInvTx.back();
3897  vInvTx.pop_back();
3898  uint256 hash = *it;
3899  // Remove it from the to-be-sent set
3900  pto->m_tx_relay->setInventoryTxToSend.erase(it);
3901  // Check if not in the filter already
3902  if (pto->m_tx_relay->filterInventoryKnown.contains(hash)) {
3903  continue;
3904  }
3905  // Not in the mempool anymore? don't bother sending it.
3906  auto txinfo = mempool.info(hash);
3907  if (!txinfo.tx) {
3908  continue;
3909  }
3910  // Peer told you to not send transactions at that feerate? Don't bother sending it.
3911  if (txinfo.fee < filterrate.GetFee(txinfo.vsize)) {
3912  continue;
3913  }
3914  if (pto->m_tx_relay->pfilter && !pto->m_tx_relay->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
3915  // Send
3916  vInv.push_back(CInv(MSG_TX, hash));
3917  nRelayedTransactions++;
3918  {
3919  // Expire old relay messages
3920  while (!vRelayExpiration.empty() && vRelayExpiration.front().first < nNow)
3921  {
3922  mapRelay.erase(vRelayExpiration.front().second);
3923  vRelayExpiration.pop_front();
3924  }
3925 
3926  auto ret = mapRelay.insert(std::make_pair(hash, std::move(txinfo.tx)));
3927  if (ret.second) {
3928  vRelayExpiration.push_back(std::make_pair(nNow + std::chrono::microseconds{RELAY_TX_CACHE_TIME}.count(), ret.first));
3929  }
3930  }
3931  if (vInv.size() == MAX_INV_SZ) {
3932  connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
3933  vInv.clear();
3934  }
3935  pto->m_tx_relay->filterInventoryKnown.insert(hash);
3936  }
3937  }
3938  }
3939  }
3940  if (!vInv.empty())
3941  connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
3942 
3943  // Detect whether we're stalling
3944  current_time = GetTime<std::chrono::microseconds>();
3945  // nNow is the current system time (GetTimeMicros is not mockable) and
3946  // should be replaced by the mockable current_time eventually
3947  nNow = GetTimeMicros();
3948  if (state.nStallingSince && state.nStallingSince < nNow - 1000000 * BLOCK_STALLING_TIMEOUT) {
3949  // Stalling only triggers when the block download window cannot move. During normal steady state,
3950  // the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
3951  // should only happen during initial block download.
3952  LogPrintf("Peer=%d is stalling block download, disconnecting\n", pto->GetId());
3953  pto->fDisconnect = true;
3954  return true;
3955  }
3956  // In case there is a block that has been in flight from this peer for 2 + 0.5 * N times the block interval
3957  // (with N the number of peers from which we're downloading validated blocks), disconnect due to timeout.
3958  // We compensate for other peers to prevent killing off peers due to our own downstream link
3959  // being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
3960  // to unreasonably increase our timeout.
3961  if (state.vBlocksInFlight.size() > 0) {
3962  QueuedBlock &queuedBlock = state.vBlocksInFlight.front();
3963  int nOtherPeersWithValidatedDownloads = nPeersWithValidatedDownloads - (state.nBlocksInFlightValidHeaders > 0);
3964  if (nNow > state.nDownloadingSince + consensusParams.nPowTargetSpacing * (BLOCK_DOWNLOAD_TIMEOUT_BASE + BLOCK_DOWNLOAD_TIMEOUT_PER_PEER * nOtherPeersWithValidatedDownloads)) {
3965  LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", queuedBlock.hash.ToString(), pto->GetId());
3966  pto->fDisconnect = true;
3967  return true;
3968  }
3969  }
3970  // Check for headers sync timeouts
3971  if (state.fSyncStarted && state.nHeadersSyncTimeout < std::numeric_limits<int64_t>::max()) {
3972  // Detect whether this is a stalling initial-headers-sync peer
3973  if (pindexBestHeader->GetBlockTime() <= GetAdjustedTime() - 24*60*60) {
3974  if (nNow > state.nHeadersSyncTimeout && nSyncStarted == 1 && (nPreferredDownload - state.fPreferredDownload >= 1)) {
3975  // Disconnect a (non-whitelisted) peer if it is our only sync peer,
3976  // and we have others we could be using instead.
3977  // Note: If all our peers are inbound, then we won't
3978  // disconnect our sync peer for stalling; we have bigger
3979  // problems if we can't get any outbound peers.
3980  if (!pto->HasPermission(PF_NOBAN)) {
3981  LogPrintf("Timeout downloading headers from peer=%d, disconnecting\n", pto->GetId());
3982  pto->fDisconnect = true;
3983  return true;
3984  } else {
3985  LogPrintf("Timeout downloading headers from whitelisted peer=%d, not disconnecting\n", pto->GetId());
3986  // Reset the headers sync state so that we have a
3987  // chance to try downloading from a different peer.
3988  // Note: this will also result in at least one more
3989  // getheaders message to be sent to
3990  // this peer (eventually).
3991  state.fSyncStarted = false;
3992  nSyncStarted--;
3993  state.nHeadersSyncTimeout = 0;
3994  }
3995  }
3996  } else {
3997  // After we've caught up once, reset the timeout so we can't trigger
3998  // disconnect later.
3999  state.nHeadersSyncTimeout = std::numeric_limits<int64_t>::max();
4000  }
4001  }
4002 
4003  // Check that outbound peers have reasonable chains
4004  // GetTime() is used by this anti-DoS logic so we can test this using mocktime
4005  ConsiderEviction(pto, GetTime());
4006 
4007  //
4008  // Message: getdata (blocks)
4009  //
4010  std::vector<CInv> vGetData;
4011  if (!pto->fClient && ((fFetch && !pto->m_limited_node) || !::ChainstateActive().IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
4012  std::vector<const CBlockIndex*> vToDownload;
4013  NodeId staller = -1;
4014  FindNextBlocksToDownload(pto->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller, consensusParams);
4015  for (const CBlockIndex *pindex : vToDownload) {
4016  uint32_t nFetchFlags = GetFetchFlags(pto);
4017  vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
4018  MarkBlockAsInFlight(pto->GetId(), pindex->GetBlockHash(), pindex);
4019  LogPrint(BCLog::NET, "Requesting block %s (%d) peer=%d\n", pindex->GetBlockHash().ToString(),
4020  pindex->nHeight, pto->GetId());
4021  }
4022  if (state.nBlocksInFlight == 0 && staller != -1) {
4023  if (State(staller)->nStallingSince == 0) {
4024  State(staller)->nStallingSince = nNow;
4025  LogPrint(BCLog::NET, "Stall started peer=%d\n", staller);
4026  }
4027  }
4028  }
4029 
4030  //
4031  // Message: getdata (non-blocks)
4032  //
4033 
4034  // For robustness, expire old requests after a long timeout, so that
4035  // we can resume downloading transactions from a peer even if they
4036  // were unresponsive in the past.
4037  // Eventually we should consider disconnecting peers, but this is
4038  // conservative.
4039  if (state.m_tx_download.m_check_expiry_timer <= current_time) {
4040  for (auto it=state.m_tx_download.m_tx_in_flight.begin(); it != state.m_tx_download.m_tx_in_flight.end();) {
4041  if (it->second <= current_time - TX_EXPIRY_INTERVAL) {
4042  LogPrint(BCLog::NET, "timeout of inflight tx %s from peer=%d\n", it->first.ToString(), pto->GetId());
4043  state.m_tx_download.m_tx_announced.erase(it->first);
4044  state.m_tx_download.m_tx_in_flight.erase(it++);
4045  } else {
4046  ++it;
4047  }
4048  }
4049  // On average, we do this check every TX_EXPIRY_INTERVAL. Randomize
4050  // so that we're not doing this for all peers at the same time.
4051  state.m_tx_download.m_check_expiry_timer = current_time + TX_EXPIRY_INTERVAL / 2 + GetRandMicros(TX_EXPIRY_INTERVAL);
4052  }
4053 
4054  auto& tx_process_time = state.m_tx_download.m_tx_process_time;
4055  while (!tx_process_time.empty() && tx_process_time.begin()->first <= current_time && state.m_tx_download.m_tx_in_flight.size() < MAX_PEER_TX_IN_FLIGHT) {
4056  const uint256 txid = tx_process_time.begin()->second;
4057  // Erase this entry from tx_process_time (it may be added back for
4058  // processing at a later time, see below)
4059  tx_process_time.erase(tx_process_time.begin());
4060  CInv inv(MSG_TX | GetFetchFlags(pto), txid);
4061  if (!AlreadyHave(inv)) {
4062  // If this transaction was last requested more than 1 minute ago,
4063  // then request.
4064  const auto last_request_time = GetTxRequestTime(inv.hash);
4065  if (last_request_time <= current_time - GETDATA_TX_INTERVAL) {
4066  LogPrint(BCLog::NET, "Requesting %s peer=%d\n", inv.ToString(), pto->GetId());
4067  vGetData.push_back(inv);
4068  if (vGetData.size() >= MAX_GETDATA_SZ) {
4069  connman->PushMessage(pto, msgMaker.Make(NetMsgType::GETDATA, vGetData));
4070  vGetData.clear();
4071  }
4072  UpdateTxRequestTime(inv.hash, current_time);
4073  state.m_tx_download.m_tx_in_flight.emplace(inv.hash, current_time);
4074  } else {
4075  // This transaction is in flight from someone else; queue
4076  // up processing to happen after the download times out
4077  // (with a slight delay for inbound peers, to prefer
4078  // requests to outbound peers).
4079  const auto next_process_time = CalculateTxGetDataTime(txid, current_time, !state.fPreferredDownload);
4080  tx_process_time.emplace(next_process_time, txid);
4081  }
4082  } else {
4083  // We have already seen this transaction, no need to download.
4084  state.m_tx_download.m_tx_announced.erase(inv.hash);
4085  state.m_tx_download.m_tx_in_flight.erase(inv.hash);
4086  }
4087  }
4088 
4089 
4090  if (!vGetData.empty())
4091  connman->PushMessage(pto, msgMaker.Make(NetMsgType::GETDATA, vGetData));
4092 
4093  //
4094  // Message: feefilter
4095  //
4096  // We don't want white listed peers to filter txs to us if we have -whitelistforcerelay
4097  if (pto->m_tx_relay != nullptr && pto->nVersion >= FEEFILTER_VERSION && gArgs.GetBoolArg("-feefilter", DEFAULT_FEEFILTER) &&
4098  !pto->HasPermission(PF_FORCERELAY)) {
4099  CAmount currentFilter = mempool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFeePerK();
4100  int64_t timeNow = GetTimeMicros();
4101  if (timeNow > pto->m_tx_relay->nextSendTimeFeeFilter) {
4102  static CFeeRate default_feerate(DEFAULT_MIN_RELAY_TX_FEE);
4103  static FeeFilterRounder filterRounder(default_feerate);
4104  CAmount filterToSend = filterRounder.round(currentFilter);
4105  // We always have a fee filter of at least minRelayTxFee
4106  filterToSend = std::max(filterToSend, ::minRelayTxFee.GetFeePerK());
4107  if (filterToSend != pto->m_tx_relay->lastSentFeeFilter) {
4108  connman->PushMessage(pto, msgMaker.Make(NetMsgType::FEEFILTER, filterToSend));
4109  pto->m_tx_relay->lastSentFeeFilter = filterToSend;
4110  }
4111  pto->m_tx_relay->nextSendTimeFeeFilter = PoissonNextSend(timeNow, AVG_FEEFILTER_BROADCAST_INTERVAL);
4112  }
4113  // If the fee filter has changed substantially and it's still more than MAX_FEEFILTER_CHANGE_DELAY
4114  // until scheduled broadcast, then move the broadcast to within MAX_FEEFILTER_CHANGE_DELAY.
4115  else if (timeNow + MAX_FEEFILTER_CHANGE_DELAY * 1000000 < pto->m_tx_relay->nextSendTimeFeeFilter &&
4116  (currentFilter < 3 * pto->m_tx_relay->lastSentFeeFilter / 4 || currentFilter > 4 * pto->m_tx_relay->lastSentFeeFilter / 3)) {
4117  pto->m_tx_relay->nextSendTimeFeeFilter = timeNow + GetRandInt(MAX_FEEFILTER_CHANGE_DELAY) * 1000000;
4118  }
4119  }
4120  }
4121  return true;
4122 }
4123 
4125 {
4126 public:
4129  // orphan transactions
4130  mapOrphanTransactions.clear();
4131  mapOrphanTransactionsByPrev.clear();
4132  }
4133 };
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:408
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
static int64_t GetTransactionWeight(const CTransaction &tx)
Definition: validation.h:148
static constexpr int64_t MINIMUM_CONNECT_TIME
Minimum time an outbound-peer-eviction candidate must be connected for, in order to evict...
static constexpr std::chrono::microseconds GETDATA_TX_INTERVAL
How long to wait (in microseconds) before downloading a transaction from an additional peer...
std::string SanitizeString(const std::string &str, int rule)
Remove unsafe chars.
enum ReadStatus_t ReadStatus
const char * PING
The ping message is sent periodically to help confirm that the receiving peer is still connected...
Definition: protocol.cpp:31
CTxMemPool mempool
void Misbehaving(NodeId nodeid, int howmuch, const std::string &message="") EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Increase a node&#39;s misbehavior score.
std::atomic< uint64_t > nPingNonceSent
Definition: net.h:836
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: system.cpp:362
if(expired !=0)
Definition: validation.cpp:317
const char * FILTERLOAD
The filterload message tells the receiving peer to filter all relayed transactions and requested merk...
Definition: protocol.cpp:34
const char * MERKLEBLOCK
The merkleblock message is a reply to a getdata message which requested a block using the inventory t...
Definition: protocol.cpp:23
std::atomic_bool fPauseSend
Definition: net.h:771
static const int MAX_BLOCKTXN_DEPTH
Maximum depth of blocks we&#39;re willing to respond to GETBLOCKTXN requests for.
Definition: validation.h:94
uint64_t GetRand(uint64_t nMax) noexcept
Definition: random.cpp:588
static const int SERIALIZE_TRANSACTION_NO_WITNESS
Definition: transaction.h:15
invalid by consensus rules
int GetSendVersion() const
Definition: net.cpp:619
const char * BLOCKTXN
Contains a BlockTransactions.
Definition: protocol.cpp:42
bool fPruneMode
True if we&#39;re running in -prune mode.
Definition: validation.cpp:116
static constexpr unsigned int INVENTORY_BROADCAST_MAX
Maximum number of inventory items to send per transmission.
Definition: banman.h:38
ReadStatus FillBlock(CBlock &block, const std::vector< CTransactionRef > &vtx_missing)
ServiceFlags
nServices flags
Definition: protocol.h:239
bool IsLocal() const
Definition: netaddress.cpp:224
void SetNull()
Definition: uint256.h:38
#define LogPrint(category,...)
Definition: logging.h:179
int64_t GetBlockTime() const
Definition: chain.h:247
CConnman *const connman
Describes a place in the block chain to another node such that if the other node doesn&#39;t have the sam...
Definition: block.h:126
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:144
std::vector< TxMempoolInfo > infoAll() const
Definition: txmempool.cpp:788
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
#define TRY_LOCK(cs, name)
Definition: sync.h:183
STL-like map container that only keeps the N elements with the highest value.
Definition: limitedmap.h:13
uint32_t nStatus
Verification status of this block. See enum BlockStatus.
Definition: chain.h:174
size_t GetAddressCount() const
Definition: net.cpp:2435
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:28
void WakeMessageHandler()
Definition: net.cpp:1441
bool IsValid() const
Definition: validation.h:106
void SetServices(const CService &addr, ServiceFlags nServices)
Definition: net.cpp:2440
std::string ToString() const
Definition: protocol.cpp:189
Definition: block.h:72
Defined in BIP144.
Definition: protocol.h:370
uint64_t ReadCompactSize(Stream &is)
Definition: serialize.h:337
We don&#39;t have the previous block the checked one is built on.
CChain & ChainActive()
Definition: validation.cpp:91
const char * GETADDR
The getaddr message requests an addr message from the receiving node, preferably one with lots of IP ...
Definition: protocol.cpp:29
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override
Overridden from CValidationInterface.
int64_t nTimeExpire
static const unsigned int DEFAULT_MAX_MEMPOOL_SIZE
Default for -maxmempool, maximum megabytes of mempool memory usage.
Definition: policy.h:32
Defined in BIP152.
Definition: protocol.h:368
std::vector< uint16_t > indexes
int GetRecvVersion() const
Definition: net.h:911
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
void CheckForStaleTipAndEvictPeers(const Consensus::Params &consensusParams)
Evict extra outbound peers.
bool SendMessages(CNode *pto) override EXCLUSIVE_LOCKS_REQUIRED(pto -> cs_sendProcessing)
Send queued protocol messages to be sent to a give node.
static bool AlreadyHave(const CInv &inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats)
Get statistics from node state.
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:915
reverse_range< T > reverse_iterate(T &x)
bool ReadRawBlockFromDisk(std::vector< uint8_t > &block, const FlatFilePos &pos, const CMessageHeader::MessageStartChars &message_start)
inv message data
Definition: protocol.h:375
static const int64_t BLOCK_DOWNLOAD_TIMEOUT_BASE
Block download timeout base, expressed in millionths of the block interval (i.e.
Definition: validation.h:105
invalid proof of work or time too old
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
bool IsInvalid() const
Definition: validation.h:107
RecursiveMutex cs_inventory
Definition: net.h:794
const char * SENDCMPCT
Contains a 1-byte bool and 8-byte LE version number.
Definition: protocol.cpp:39
static constexpr int STALE_RELAY_AGE_LIMIT
Age after which a stale block will no longer be served if requested as protection against fingerprint...
static const unsigned int MIN_BLOCKS_TO_KEEP
Block files containing a block-height within MIN_BLOCKS_TO_KEEP of ChainActive().Tip() will not be pr...
Definition: validation.h:178
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the last common ancestor two blocks have.
Definition: chain.cpp:156
transaction was missing some of its inputs
unsigned int nHeight
TxMempoolInfo info(const uint256 &hash) const
Definition: txmempool.cpp:811
All parent headers found, difficulty matches, timestamp >= median previous, checkpoint.
Definition: chain.h:101
static void ProcessOrphanTx(CConnman *connman, std::set< uint256 > &orphan_work_set, std::list< CTransactionRef > &removed_txn) EXCLUSIVE_LOCKS_REQUIRED(cs_main
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:26
CBlockHeader GetBlockHeader() const
Definition: chain.h:220
std::vector< unsigned char > ParseHex(const char *psz)
int Height() const
Return the maximal height in the chain.
Definition: chain.h:421
Defined in BIP144.
Definition: protocol.h:369
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:44
static bool BlockRequestAllowed(const CBlockIndex *pindex, const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool GetTryNewOutboundPeer()
Definition: net.cpp:1673
CTransactionRef tx
unsigned long size() const
Definition: txmempool.h:678
Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid...
Definition: chain.h:108
static const int BIP0031_VERSION
BIP 0031, pong message, is enabled for all versions AFTER this one.
Definition: version.h:28
void PushMessage(CNode *pnode, CSerializedNetMsg &&msg)
Definition: net.cpp:2720
RecursiveMutex cs_vProcessMsg
Definition: net.h:724
arith_uint256 nMinimumChainWork
Minimum work we will assume exists on some valid chain.
Definition: validation.cpp:125
void SetVersion(int nVersionIn)
Definition: net.h:631
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:163
void SetServiceFlagsIBDCache(bool state)
Set the current IBD status in order to figure out the desirable service flags.
Definition: protocol.cpp:137
RollingBloomFilter is a probabilistic "keep track of most recently inserted" set. ...
Definition: bloom.h:115
static bool IsOutboundDisconnectionCandidate(const CNode *node)
std::atomic< int64_t > nPingUsecStart
Definition: net.h:838
CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
Definition: net.cpp:154
void scheduleEvery(Function f, int64_t deltaMilliSeconds)
Definition: scheduler.cpp:145
static const int SENDHEADERS_VERSION
"sendheaders" command and announcing blocks with headers starts with this version ...
Definition: version.h:34
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
Definition: chainparams.h:47
violated mempool&#39;s fee/size/descendant/RBF/etc limits
bool IsNull() const
Definition: block.h:149
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:201
bool empty() const
Definition: streams.h:293
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:384
void SetTryNewOutboundPeer(bool flag)
Definition: net.cpp:1678
const uint32_t MSG_WITNESS_FLAG
getdata message type flags
Definition: protocol.h:354
uint64_t GetLocalNonce() const
Definition: net.h:891
bool SeenLocal(const CService &addr)
vote for a local address
Definition: net.cpp:267
std::vector< CAddress > vAddrToSend
Definition: net.h:782
void GetRandBytes(unsigned char *buf, int num) noexcept
Overall design of the RNG and entropy sources.
Definition: random.cpp:581
transaction spends a coinbase too early, or violates locktime/sequence locks
std::atomic< int > nStartingHeight
Definition: net.h:779
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
Definition: script.h:23
void PushAddress(const CAddress &_addr, FastRandomContext &insecure_rand)
Definition: net.h:941
bool ProcessNewBlock(const CChainParams &chainparams, const std::shared_ptr< const CBlock > pblock, bool fForceProcessing, bool *fNewBlock)
Process an incoming block.
static uint32_t GetFetchFlags(CNode *pfrom) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
constexpr Span< A > MakeSpan(A(&a)[N])
Create a span to a container exposing data() and size().
Definition: span.h:55
void SetRecvVersion(int nVersionIn)
Definition: net.h:907
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:32
unsigned char * begin()
Definition: uint256.h:54
static constexpr std::chrono::seconds RELAY_TX_CACHE_TIME
How long to cache transactions in mapRelay for normal relay.
initial value. Tx has not yet been rejected
bool IsNull() const
Definition: uint256.h:30
bool ProcessMessages(CNode *pfrom, std::atomic< bool > &interrupt) override
Process protocol messages received from a given node.
const char * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
Definition: protocol.cpp:27
void PushInventory(const CInv &inv)
Definition: net.h:965
std::set< uint256 > orphan_work_set
Definition: net.h:846
std::atomic< ServiceFlags > nServices
Definition: net.h:714
static constexpr int32_t MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT
Protect at least this many outbound peers from disconnection due to slow/ behind headers chain...
const std::vector< CTxIn > vin
Definition: transaction.h:287
void SetAddrLocal(const CService &addrLocalIn)
May not be called more than once.
Definition: net.cpp:490
std::chrono::microseconds GetRandMicros(std::chrono::microseconds duration_max) noexcept
Definition: random.cpp:593
std::deque< CInv > vRecvGetData
Definition: net.h:730
const char * INV
The inv message (inventory message) transmits one or more inventories of objects known to the transmi...
Definition: protocol.cpp:21
bool AddOrphanTx(const CTransactionRef &tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
bool ForNode(NodeId id, std::function< bool(CNode *pnode)> func)
Definition: net.cpp:2757
const std::unique_ptr< CRollingBloomFilter > m_addr_known
Definition: net.h:783
PeerLogicValidation(CConnman *connman, BanMan *banman, CScheduler &scheduler)
static constexpr std::chrono::microseconds MAX_GETDATA_RANDOM_DELAY
Maximum delay (in microseconds) for transaction requests to avoid biasing some peers over others...
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:610
static constexpr int32_t MAX_PEER_TX_IN_FLIGHT
Maximum number of in-flight transactions from a peer.
bool DisconnectNode(const std::string &node)
Definition: net.cpp:2510
static const unsigned int MAX_LOCATOR_SZ
The maximum number of entries in a locator.
Definition: net.h:57
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
uint256 GetBlockHash() const
Definition: chain.h:233
BlockValidationResult GetResult() const
Definition: validation.h:141
bool IsAddrRelayPeer() const
Definition: net.h:788
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
bool done
bool fSentAddr
Definition: net.h:765
std::atomic< int64_t > nPingUsecTime
Definition: net.h:840
static bool MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationState &state, bool via_compact_block, const std::string &message="")
Potentially ban a node based on the contents of a BlockValidationState object.
std::atomic< int64_t > nMinPingUsecTime
Definition: net.h:842
int GetMyStartingHeight() const
Definition: net.h:895
#define LOCK2(cs1, cs2)
Definition: sync.h:180
initial value. Block has not yet been rejected
static constexpr int64_t EXTRA_PEER_CHECK_INTERVAL
How frequently to check for extra outbound peers and disconnect, in seconds.
ServiceFlags GetLocalServices() const
Definition: net.h:988
Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends...
Definition: chain.h:112
static const int64_t BLOCK_DOWNLOAD_TIMEOUT_PER_PEER
Additional block download timeout per parallel downloading peer (i.e.
Definition: validation.h:107
bool fClient
Definition: net.h:758
Used to relay blocks as header + vector<merkle branch> to filtered nodes.
Definition: merkleblock.h:133
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.cpp:25
static const unsigned int NODE_NETWORK_LIMITED_MIN_BLOCKS
Minimum blocks required to signal NODE_NETWORK_LIMITED.
Definition: validation.h:180
bool IsBanned(CNetAddr net_addr)
Definition: banman.cpp:92
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&#39;ve been behind our tip...
BanMan *const m_banman
std::string FormatStateMessage(const ValidationState &state)
Convert ValidationState to a human-readable message for logging.
Definition: validation.cpp:11
size_type size() const
Definition: streams.h:292
Invalid by a change to consensus rules more recent than SegWit.
static constexpr int64_t ORPHAN_TX_EXPIRE_TIME
Expiration time for orphan transactions in seconds.
size_t nProcessQueueSize
Definition: net.h:726
Scripts & signatures ok. Implies all parents are also at least SCRIPTS.
Definition: chain.h:115
Transaction might be missing a witness, have a witness prior to SegWit activation, or witness may have been malleated (which includes non-standard witnesses).
CFeeRate minRelayTxFee
A fee rate smaller than this is considered zero fee (for relaying, mining and transaction creation) ...
Definition: validation.cpp:127
CBlockIndex * pindexBestHeader
Best header we&#39;ve seen so far (used for getheaders queries&#39; starting points).
Definition: validation.cpp:108
std::vector< CTransactionRef > txn
static RecursiveMutex cs_most_recent_block
this block was cached as being invalid and we didn&#39;t store the reason why
bool exists(const uint256 &hash) const
Definition: txmempool.h:690
bool fOneShot
Definition: net.h:756
An input of a transaction.
Definition: transaction.h:63
static bool HasAllDesirableServiceFlags(ServiceFlags services)
A shortcut for (services & GetDesirableServiceFlags(services)) == GetDesirableServiceFlags(services)...
Definition: protocol.h:305
bool AcceptToMemoryPool(CTxMemPool &pool, TxValidationState &state, const CTransactionRef &tx, std::list< CTransactionRef > *plTxnReplaced, bool bypass_limits, const CAmount nAbsurdFee, bool test_accept)
(try to) add transaction to memory pool plTxnReplaced will be appended to with all transactions repla...
#define LOCK(cs)
Definition: sync.h:179
const char * name
Definition: rest.cpp:40
static const int MAX_BLOCKS_IN_TRANSIT_PER_PEER
Number of blocks that can be requested at any given time from a single peer.
Definition: validation.h:84
static void ProcessGetData(CNode *pfrom, const CChainParams &chainparams, CConnman *connman, const std::atomic< bool > &interruptMsgProc) LOCKS_EXCLUDED(cs_main)
const uint256 & GetHash() const
Definition: transaction.h:322
the block failed to meet one of our checkpoints
bool IsPeerAddrLocalGood(CNode *pnode)
Definition: net.cpp:174
int type
Definition: protocol.h:396
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:15
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:408
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:150
Fast randomness source.
Definition: random.h:106
Transport protocol agnostic message container.
Definition: net.h:618
bool g_relay_txes
Definition: net.cpp:87
static constexpr unsigned int MAX_FEEFILTER_CHANGE_DELAY
Maximum feefilter broadcast delay after significant change.
int64_t PoissonNextSendInbound(int64_t now, int average_interval_seconds)
Attempts to obfuscate tx time through exponentially distributed emitting.
Definition: net.cpp:2770
bool OutboundTargetReached(bool historicalBlockServingLimit)
check if the outbound target is reached if param historicalBlockServingLimit is set true...
Definition: net.cpp:2617
static const unsigned int MAX_HEADERS_RESULTS
Number of headers sent in one getheaders result.
Definition: validation.h:89
int64_t nPowTargetSpacing
Definition: params.h:78
static constexpr int64_t HEADERS_DOWNLOAD_TIMEOUT_BASE
Headers download timeout expressed in microseconds Timeout = base + per_header * (expected number of ...
std::vector< CAddress > GetAddresses()
Definition: net.cpp:2455
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:413
const char * SENDHEADERS
Indicates that a node prefers to receive new block announcements via a "headers" message rather than ...
Definition: protocol.cpp:37
static constexpr int64_t HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER
const char * MEMPOOL
The mempool message requests the TXIDs of transactions that the receiving node has verified as valid ...
Definition: protocol.cpp:30
NodeId fromPeer
static const unsigned int DEFAULT_MIN_RELAY_TX_FEE
Default for -minrelaytxfee, minimum relay fee for transactions.
Definition: validation.h:53
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.
TxValidationResult GetResult() const
Definition: validation.h:127
static const int SHORT_IDS_BLOCKS_VERSION
short-id-based block download starts with this version
Definition: version.h:40
void ForEachNodeThen(Callable &&pre, CallableAfter &&post)
Definition: net.h:234
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:106
bool IsProxy(const CNetAddr &addr)
Definition: netbase.cpp:765
bool m_manual_connection
Definition: net.h:757
static constexpr int64_t CHAIN_SYNC_TIMEOUT
Timeout for (unprotected) outbound peers to sync to our chainwork, in seconds.
const std::vector< CTxOut > vout
Definition: transaction.h:288
A CService with information about it as peer.
Definition: protocol.h:318
std::map< uint256, COrphanTx > mapOrphanTransactions GUARDED_BY(g_cs_orphans)
std::vector< unsigned char > GetKey() const
Definition: netaddress.cpp:721
static int EraseOrphanTx(uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
static const unsigned int AVG_ADDRESS_BROADCAST_INTERVAL
Average delay between peer address broadcasts in seconds.
uint256 hash
Definition: protocol.h:397
static constexpr uint64_t RANDOMIZER_ID_ADDRESS_RELAY
SHA256("main address relay")[0:8].
static const bool DEFAULT_FEEFILTER
Default for using fee filter.
Definition: validation.h:120
const char * ADDR
The addr (IP address) message relays connection information for peers on the network.
Definition: protocol.cpp:20
const CMessageHeader::MessageStartChars & MessageStart() const
Definition: chainparams.h:61
int64_t NodeId
Definition: net.h:93
Definition: net.h:121
void AddNewAddresses(const std::vector< CAddress > &vAddr, const CAddress &addrFrom, int64_t nTimePenalty=0)
Definition: net.cpp:2450
bool GetNetworkActive() const
Definition: net.h:203
static const unsigned int INVENTORY_BROADCAST_INTERVAL
Average delay between trickled inventory transmissions in seconds.
const char * FILTERCLEAR
The filterclear message tells the receiving peer to remove a previously-set bloom filter...
Definition: protocol.cpp:36
bool fGetAddr
Definition: net.h:784
std::atomic_bool fImporting
std::string ToString() const
Definition: uint256.cpp:60
std::vector< uint256 > vHave
Definition: block.h:128
static bool ProcessHeadersMessage(CNode *pfrom, CConnman *connman, const std::vector< CBlockHeader > &headers, const CChainParams &chainparams, bool via_compact_block)
NodeId GetId() const
Definition: net.h:887
const char * NOTFOUND
The notfound message is a reply to a getdata message which requested an object the receiving node doe...
Definition: protocol.cpp:33
CSipHasher GetDeterministicRandomizer(uint64_t id) const
Get a unique deterministic randomizer.
Definition: net.cpp:2786
Parameters that influence chain consensus.
Definition: params.h:45
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:18
const char * BLOCK
The block message transmits a single serialized block.
Definition: protocol.cpp:28
static void SendBlockTransactions(const CBlock &block, const BlockTransactionsRequest &req, CNode *pfrom, CConnman *connman)
std::atomic_bool fDisconnect
Definition: net.h:764
std::string strSubVersion
Subversion as sent to the P2P network in version messages.
Definition: net.cpp:91
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:38
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:991
RecursiveMutex g_cs_orphans
void ForEachNode(Callable &&func)
Definition: net.h:214
bool IsRoutable() const
Definition: netaddress.cpp:296
uint64_t GetHash() const
Definition: netaddress.cpp:541
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:313
unsigned int GetReceiveFloodSize() const
Definition: net.cpp:2673
RecursiveMutex cs_SubVer
Definition: net.h:743
static const int MAX_UNCONNECTING_HEADERS
Maximum number of unconnecting headers announcements before DoS score.
Definition: validation.h:126
void Ban(const CNetAddr &net_addr, const BanReason &ban_reason, int64_t ban_time_offset=0, bool since_unix_epoch=false)
Definition: banman.cpp:121
bool CheckIncomingNonce(uint64_t nonce)
Definition: net.cpp:330
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 RelayTransaction(const uint256 &txid, const CConnman &connman)
Relay transaction to every node.
unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
const CAddress addr
Definition: net.h:739
static constexpr std::chrono::microseconds TX_EXPIRY_INTERVAL
How long to wait (in microseconds) before expiring an in-flight getdata request to a peer...
const char * GETBLOCKS
The getblocks message requests an inv message that provides block header hashes starting from a parti...
Definition: protocol.cpp:24
static constexpr int32_t MAX_PEER_TX_ANNOUNCEMENTS
Maximum number of announced transactions from a peer.
const int64_t nTimeConnected
Definition: net.h:736
int64_t GetTimeMicros()
Returns the system time (not mockable)
Definition: time.cpp:62
int64_t m_stale_tip_check_time
Next time to check for stale tip.
void BlockConnected(const std::shared_ptr< const CBlock > &pblock, const CBlockIndex *pindexConnected, const std::vector< CTransactionRef > &vtxConflicted) override
Overridden from CValidationInterface.
std::atomic_bool fReindex
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:19
static bool ProcessMessage(CNode *pfrom, const std::string &strCommand, CDataStream &vRecv, int64_t nTimeReceived, const CChainParams &chainparams, CConnman *connman, BanMan *banman, const std::atomic< bool > &interruptMsgProc)
uint256 GetHash() const
Definition: block.cpp:11
CBlockIndex * LookupBlockIndex(const uint256 &hash)
Definition: validation.cpp:152
std::atomic< bool > fPingQueued
Definition: net.h:844
256-bit opaque blob.
Definition: uint256.h:120
invalid by consensus rules (excluding any below reasons)
void AddInventoryKnown(const CInv &inv)
Definition: net.h:957
bool HasWitness() const
Definition: transaction.h:354
bool IsReachable(enum Network net)
Definition: net.cpp:255
CChainState & ChainstateActive()
Definition: validation.cpp:86
static const unsigned int MAX_GETDATA_SZ
Limit to avoid sending big packets.
ServiceFlags nServices
Definition: protocol.h:347
void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr< const CBlock > &pblock) override
Overridden from CValidationInterface.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
std::vector< CTransactionRef > vtx
Definition: block.h:76
std::set< NodeId > setMisbehaving
const char * CMPCTBLOCK
Contains a CBlockHeaderAndShortTxIDs object - providing a header and list of "short txids"...
Definition: protocol.cpp:40
void BlockDisconnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being disconnected.
the block&#39;s data didn&#39;t match the data committed to by the PoW
bool fFeeler
Definition: net.h:755
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:444
std::atomic< int64_t > nLastTXTime
Definition: net.h:832
const bool fInbound
Definition: net.h:760
bool CompareDepthAndScore(const uint256 &hasha, const uint256 &hashb)
Definition: txmempool.cpp:726
#define LOCKS_EXCLUDED(...)
Definition: threadsafety.h:50
static constexpr std::chrono::microseconds INBOUND_PEER_TX_DELAY
How many microseconds to delay requesting transactions from inbound peers.
static void RelayAddress(const CAddress &addr, bool fReachable, CConnman *connman)
static const int FEEFILTER_VERSION
"feefilter" tells peers to filter invs to you by fee starts with this version
Definition: version.h:37
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:18
std::vector< std::pair< unsigned int, uint256 > > vMatchedTxn
Public only for unit testing and relay testing (not relayed).
Definition: merkleblock.h:146
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:137
const CChainParams & Params()
Return the currently selected parameters.
uint256 hashContinue
Definition: net.h:778
void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds)
static const int MIN_PEER_PROTO_VERSION
disconnect from peers older than this proto version
Definition: version.h:21
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
static const unsigned int MAX_STANDARD_TX_WEIGHT
The maximum weight for transactions we&#39;re willing to relay/mine.
Definition: policy.h:24
bool IsTxAvailable(size_t index) const
static const unsigned int DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN
Default number of orphan+recently-replaced txn to keep around for block reconstruction.
A block this one builds on is invalid.
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:372
CBlockIndex * FindForkInGlobalIndex(const CChain &chain, const CBlockLocator &locator)
Find the last common block between the parameter chain and a locator.
Definition: validation.cpp:159
bool fLogIPs
Definition: logging.cpp:35
int64_t GetAdjustedTime()
Definition: timedata.cpp:35
ServiceFlags GetDesirableServiceFlags(ServiceFlags services)
Gets the set of service flags which are "desirable" for a given peer.
Definition: protocol.cpp:130
static void ProcessGetBlockData(CNode *pfrom, const CChainParams &chainparams, const CInv &inv, CConnman *connman)
void SetSendVersion(int nVersionIn)
Definition: net.cpp:605
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:390
void SetBestHeight(int height)
Definition: net.cpp:2663
bool IsInitialBlockDownload() const
Check whether we are doing an initial block download (synchronizing from disk or network) ...
#define LIMITED_STRING(obj, n)
Definition: serialize.h:499
static const int CADDR_TIME_VERSION
nTime field added to CAddress, starting with this version; if possible, avoid requesting addresses no...
Definition: version.h:25
void EraseOrphansFor(NodeId peer)
std::atomic< int64_t > nTimeOffset
Definition: net.h:737
static const unsigned int MAX_BLOCKS_TO_ANNOUNCE
Maximum number of headers to announce when relaying blocks with headers message.
Definition: validation.h:123
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:2781
ArgsManager gArgs
Definition: system.cpp:76
const char * GETDATA
The getdata message requests one or more data objects from another node.
Definition: protocol.cpp:22
bool fListen
Definition: net.cpp:86
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:19
std::atomic_bool fSuccessfullyConnected
Definition: net.h:761
SipHash-2-4.
Definition: siphash.h:13
#define AssertLockNotHeld(cs)
Definition: sync.h:72
static int count
Definition: tests.c:45
static const unsigned int MAX_SUBVERSION_LENGTH
Maximum length of the user agent string in version message.
Definition: net.h:63
std::atomic< int > nVersion
Definition: net.h:742
Invalid by a change to consensus rules more recent than SegWit.
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...
static bool TxRelayMayResultInDisconnect(const TxValidationState &state)
Returns true if the given validation state result may result in a peer banning/disconnecting us...
static size_t RecursiveDynamicUsage(const CScript &script)
Definition: core_memusage.h:12
ReadStatus InitData(const CBlockHeaderAndShortTxIDs &cmpctblock, const std::vector< std::pair< uint256, CTransactionRef >> &extra_txn)
void FinalizeNode(NodeId nodeid, bool &fUpdateConnectionTime) override
Handle removal of a peer by updating various state and removing it from mapNodeState.
static const int PING_INTERVAL
Time between pings automatically sent out for latency probing and keepalive (in seconds).
Definition: net.h:49
static CNetProcessingCleanup instance_of_cnetprocessingcleanup
bool m_limited_node
Definition: net.h:759
std::string ToString() const
Definition: netaddress.cpp:745
static const int NO_BLOOM_VERSION
"filter*" commands are disabled without NODE_BLOOM after and including this version ...
Definition: version.h:31
block timestamp was > 2 hours in the future (or our clock is bad)
int GetExtraOutboundCount()
Definition: net.cpp:1690
static const unsigned int DEFAULT_BANSCORE_THRESHOLD
Definition: validation.h:116
static constexpr unsigned int AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL
Average delay between local address broadcasts in seconds.
static const unsigned int BLOCK_STALLING_TIMEOUT
Timeout in seconds during which a peer must stall block download progress before being disconnected...
Definition: validation.h:86
static const int MAX_CMPCTBLOCK_DEPTH
Maximum depth of blocks we&#39;re willing to serve as compact blocks to peers when requested.
Definition: validation.h:92
const char * TX
The tx message transmits a single transaction.
Definition: protocol.cpp:26
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
void MarkAddressGood(const CAddress &addr)
Definition: net.cpp:2445
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:150
Information about a peer.
Definition: net.h:707
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:60
std::vector< int > vHeightInFlight
CAmount round(CAmount currentMinFee)
Quantize a minimum fee for privacy purpose before broadcast.
Definition: fees.cpp:971
bool CheckIfBanned(CNode *pnode) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
full block available in blk*.dat
Definition: chain.h:121
bool ProcessNewBlockHeaders(const std::vector< CBlockHeader > &headers, BlockValidationState &state, const CChainParams &chainparams, const CBlockIndex **ppindex)
Process incoming block headers.
std::string GetAddrName() const
Definition: net.cpp:473
void AddTimeData(const CNetAddr &ip, int64_t nOffsetSample)
Definition: timedata.cpp:47
static void AddToCompactExtraTransactions(const CTransactionRef &tx) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
AssertLockHeld(g_cs_orphans)
void AddAddressKnown(const CAddress &_addr)
Definition: net.h:935
void InitializeNode(CNode *pnode) override
Initialize a peer by adding it to mapNodeState and pushing a message requesting its version...
int64_t GetTime()
Return system time (or mocked time, if set)
Definition: time.cpp:20
auto it
Definition: validation.cpp:362
static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS
Default for -maxorphantx, maximum number of orphan transactions kept in memory.
static const int INVALID_CB_NO_BAN_VERSION
not banning for invalid compact blocks starts with this version
Definition: version.h:43
COutPoint prevout
Definition: transaction.h:66
std::atomic_bool fPauseRecv
Definition: net.h:770
int GetRandInt(int nMax) noexcept
Definition: random.cpp:598
bool HasPermission(NetPermissionFlags permission) const
Definition: net.h:750
static const unsigned int MAX_INV_SZ
The maximum number of entries in an &#39;inv&#39; protocol message.
Definition: net.h:55
static constexpr int HISTORICAL_BLOCK_AGE
Age after which a block is considered historical for purposes of rate limiting block relay...
std::atomic< int64_t > nLastBlockTime
Definition: net.h:831
Tx already in mempool or conflicts with a tx in the chain (if it conflicts with another tx in mempool...
static constexpr int64_t STALE_CHECK_INTERVAL
How frequently to check for stale tips, in seconds.
didn&#39;t meet our local policy rules
bool HaveTxsDownloaded() const
Check whether this block&#39;s and all previous blocks&#39; transactions have been downloaded (and stored to ...
Definition: chain.h:245
static constexpr int64_t ORPHAN_TX_EXPIRE_INTERVAL
Minimum time between orphan transactions expire time checks in seconds.
void BlockChecked(const CBlock &block, const BlockValidationState &state) override
Overridden from CValidationInterface.
CAmount GetFeePerK() const
Return the fee in satoshis for a size of 1000 bytes.
Definition: feerate.h:41
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:173
void AdvertiseLocal(CNode *pnode)
Definition: net.cpp:182
bool GetUseAddrmanOutgoing() const
Definition: net.h:204
unsigned int nTx
Number of transactions in this block.
Definition: chain.h:166
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:20
static const unsigned int BLOCK_DOWNLOAD_WINDOW
Size of the "block download window": how far ahead of our current height do we fetch? Larger windows tolerate larger download speed differences between peer, but increase the potential degree of disordering of blocks on disk (which make reindexing and pruning harder).
Definition: validation.h:99
int in_avail() const
Definition: streams.h:390
Defined in BIP37.
Definition: protocol.h:367
const char * FILTERADD
The filteradd message tells the receiving peer to add a single element to a previously-set bloom filt...
Definition: protocol.cpp:35
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
size_t list_pos
const char * GETBLOCKTXN
Contains a BlockTransactionsRequest Peer should respond with "blocktxn" message.
Definition: protocol.cpp:41
std::unique_ptr< TxRelay > m_tx_relay
Definition: net.h:825
CAmount GetFee(size_t nBytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:23
uint256 hash
Definition: transaction.h:21
static bool MaybePunishNodeForTx(NodeId nodeid, const TxValidationState &state, const std::string &message="")
Potentially ban a node based on the contents of a TxValidationState object.
static constexpr unsigned int AVG_FEEFILTER_BROADCAST_INTERVAL
Average delay between feefilter broadcasts in seconds.