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