Dash Core  0.12.2.1
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 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "net_processing.h"
7 
8 #include "alert.h"
9 #include "addrman.h"
10 #include "arith_uint256.h"
11 #include "chainparams.h"
12 #include "consensus/validation.h"
13 #include "hash.h"
14 #include "init.h"
15 #include "validation.h"
16 #include "merkleblock.h"
17 #include "net.h"
18 #include "netbase.h"
19 #include "policy/fees.h"
20 #include "policy/policy.h"
21 #include "primitives/block.h"
22 #include "primitives/transaction.h"
23 #include "random.h"
24 #include "tinyformat.h"
25 #include "txmempool.h"
26 #include "ui_interface.h"
27 #include "util.h"
28 #include "utilmoneystr.h"
29 #include "utilstrencodings.h"
30 #include "validationinterface.h"
31 
32 #include "spork.h"
33 #include "governance.h"
34 #include "instantx.h"
35 #include "masternode-payments.h"
36 #include "masternode-sync.h"
37 #include "masternodeman.h"
38 #include "privatesend-client.h"
39 #include "privatesend-server.h"
40 
41 #include <boost/thread.hpp>
42 
43 using namespace std;
44 
45 #if defined(NDEBUG)
46 # error "Dash Core cannot be compiled without assertions."
47 #endif
48 
49 int64_t nTimeBestReceived = 0; // Used only to inform the wallet of when we last received a block
50 
51 struct COrphanTx {
54 };
55 map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY(cs_main);
56 map<uint256, set<uint256> > mapOrphanTransactionsByPrev GUARDED_BY(cs_main);;
58 
59 // Internal stuff
60 namespace {
62  int nSyncStarted = 0;
63 
69  map<uint256, NodeId> mapBlockSource;
70 
91  boost::scoped_ptr<CRollingBloomFilter> recentRejects;
92  uint256 hashRecentRejectsChainTip;
93 
95  struct QueuedBlock {
96  uint256 hash;
97  CBlockIndex* pindex;
98  bool fValidatedHeaders;
99  };
100  map<uint256, pair<NodeId, list<QueuedBlock>::iterator> > mapBlocksInFlight;
101 
103  int nPreferredDownload = 0;
104 
105 
107  int nPeersWithValidatedDownloads = 0;
108 } // anon namespace
109 
111 //
112 // Registration of network node signals.
113 //
114 
115 namespace {
116 
117 struct CBlockReject {
118  unsigned char chRejectCode;
119  string strRejectReason;
120  uint256 hashBlock;
121 };
122 
129 struct CNodeState {
131  const CService address;
133  bool fCurrentlyConnected;
135  int nMisbehavior;
137  bool fShouldBan;
139  const std::string name;
141  std::vector<CBlockReject> rejects;
143  CBlockIndex *pindexBestKnownBlock;
145  uint256 hashLastUnknownBlock;
147  CBlockIndex *pindexLastCommonBlock;
149  CBlockIndex *pindexBestHeaderSent;
151  bool fSyncStarted;
153  int64_t nHeadersSyncTimeout;
155  int64_t nStallingSince;
156  list<QueuedBlock> vBlocksInFlight;
158  int64_t nDownloadingSince;
159  int nBlocksInFlight;
160  int nBlocksInFlightValidHeaders;
162  bool fPreferredDownload;
164  bool fPreferHeaders;
165 
166  CNodeState(CAddress addrIn, std::string addrNameIn) : address(addrIn), name(addrNameIn) {
167  fCurrentlyConnected = false;
168  nMisbehavior = 0;
169  fShouldBan = false;
170  pindexBestKnownBlock = NULL;
171  hashLastUnknownBlock.SetNull();
172  pindexLastCommonBlock = NULL;
173  pindexBestHeaderSent = NULL;
174  fSyncStarted = false;
175  nHeadersSyncTimeout = 0;
176  nStallingSince = 0;
177  nDownloadingSince = 0;
178  nBlocksInFlight = 0;
179  nBlocksInFlightValidHeaders = 0;
180  fPreferredDownload = false;
181  fPreferHeaders = false;
182  }
183 };
184 
186 map<NodeId, CNodeState> mapNodeState;
187 
188 // Requires cs_main.
189 CNodeState *State(NodeId pnode) {
190  map<NodeId, CNodeState>::iterator it = mapNodeState.find(pnode);
191  if (it == mapNodeState.end())
192  return NULL;
193  return &it->second;
194 }
195 
196 void UpdatePreferredDownload(CNode* node, CNodeState* state)
197 {
198  nPreferredDownload -= state->fPreferredDownload;
199 
200  // Whether this node should be marked as a preferred download node.
201  state->fPreferredDownload = (!node->fInbound || node->fWhitelisted) && !node->fOneShot && !node->fClient;
202 
203  nPreferredDownload += state->fPreferredDownload;
204 }
205 
206 void PushNodeVersion(CNode *pnode, CConnman& connman, int64_t nTime)
207 {
208  ServiceFlags nLocalNodeServices = pnode->GetLocalServices();
209  uint64_t nonce = pnode->GetLocalNonce();
210  int nNodeStartingHeight = pnode->GetMyStartingHeight();
211  NodeId nodeid = pnode->GetId();
212  CAddress addr = pnode->addr;
213 
214  CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService(), addr.nServices));
215  CAddress addrMe = CAddress(CService(), nLocalNodeServices);
216 
217  connman.PushMessageWithVersion(pnode, INIT_PROTO_VERSION, NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t)nLocalNodeServices, nTime, addrYou, addrMe,
218  nonce, strSubVersion, nNodeStartingHeight, ::fRelayTxes);
219 
220  if (fLogIPs)
221  LogPrint("net", "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), addrYou.ToString(), nodeid);
222  else
223  LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), nodeid);
224 }
225 
226 void InitializeNode(CNode *pnode, CConnman& connman) {
227  CAddress addr = pnode->addr;
228  std::string addrName = pnode->addrName;
229  NodeId nodeid = pnode->GetId();
230  {
231  LOCK(cs_main);
232  mapNodeState.emplace_hint(mapNodeState.end(), std::piecewise_construct, std::forward_as_tuple(nodeid), std::forward_as_tuple(addr, std::move(addrName)));
233  }
234  if(!pnode->fInbound)
235  PushNodeVersion(pnode, connman, GetTime());
236 }
237 
238 void FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTime) {
239  fUpdateConnectionTime = false;
240  LOCK(cs_main);
241  CNodeState *state = State(nodeid);
242 
243  if (state->fSyncStarted)
244  nSyncStarted--;
245 
246  if (state->nMisbehavior == 0 && state->fCurrentlyConnected) {
247  fUpdateConnectionTime = true;
248  }
249 
250  BOOST_FOREACH(const QueuedBlock& entry, state->vBlocksInFlight) {
251  mapBlocksInFlight.erase(entry.hash);
252  }
253  EraseOrphansFor(nodeid);
254  nPreferredDownload -= state->fPreferredDownload;
255  nPeersWithValidatedDownloads -= (state->nBlocksInFlightValidHeaders != 0);
256  assert(nPeersWithValidatedDownloads >= 0);
257 
258  mapNodeState.erase(nodeid);
259 
260  if (mapNodeState.empty()) {
261  // Do a consistency check after the last peer is removed.
262  assert(mapBlocksInFlight.empty());
263  assert(nPreferredDownload == 0);
264  assert(nPeersWithValidatedDownloads == 0);
265  }
266 }
267 
268 // Requires cs_main.
269 // Returns a bool indicating whether we requested this block.
270 bool MarkBlockAsReceived(const uint256& hash) {
271  map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
272  if (itInFlight != mapBlocksInFlight.end()) {
273  CNodeState *state = State(itInFlight->second.first);
274  state->nBlocksInFlightValidHeaders -= itInFlight->second.second->fValidatedHeaders;
275  if (state->nBlocksInFlightValidHeaders == 0 && itInFlight->second.second->fValidatedHeaders) {
276  // Last validated block on the queue was received.
277  nPeersWithValidatedDownloads--;
278  }
279  if (state->vBlocksInFlight.begin() == itInFlight->second.second) {
280  // First block on the queue was received, update the start download time for the next one
281  state->nDownloadingSince = std::max(state->nDownloadingSince, GetTimeMicros());
282  }
283  state->vBlocksInFlight.erase(itInFlight->second.second);
284  state->nBlocksInFlight--;
285  state->nStallingSince = 0;
286  mapBlocksInFlight.erase(itInFlight);
287  return true;
288  }
289  return false;
290 }
291 
292 // Requires cs_main.
293 void MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const Consensus::Params& consensusParams, CBlockIndex *pindex = NULL) {
294  CNodeState *state = State(nodeid);
295  assert(state != NULL);
296 
297  // Make sure it's not listed somewhere already.
298  MarkBlockAsReceived(hash);
299 
300  QueuedBlock newentry = {hash, pindex, pindex != NULL};
301  list<QueuedBlock>::iterator it = state->vBlocksInFlight.insert(state->vBlocksInFlight.end(), newentry);
302  state->nBlocksInFlight++;
303  state->nBlocksInFlightValidHeaders += newentry.fValidatedHeaders;
304  if (state->nBlocksInFlight == 1) {
305  // We're starting a block download (batch) from this peer.
306  state->nDownloadingSince = GetTimeMicros();
307  }
308  if (state->nBlocksInFlightValidHeaders == 1 && pindex != NULL) {
309  nPeersWithValidatedDownloads++;
310  }
311  mapBlocksInFlight[hash] = std::make_pair(nodeid, it);
312 }
313 
315 void ProcessBlockAvailability(NodeId nodeid) {
316  CNodeState *state = State(nodeid);
317  assert(state != NULL);
318 
319  if (!state->hashLastUnknownBlock.IsNull()) {
320  BlockMap::iterator itOld = mapBlockIndex.find(state->hashLastUnknownBlock);
321  if (itOld != mapBlockIndex.end() && itOld->second->nChainWork > 0) {
322  if (state->pindexBestKnownBlock == NULL || itOld->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
323  state->pindexBestKnownBlock = itOld->second;
324  state->hashLastUnknownBlock.SetNull();
325  }
326  }
327 }
328 
330 void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
331  CNodeState *state = State(nodeid);
332  assert(state != NULL);
333 
334  ProcessBlockAvailability(nodeid);
335 
336  BlockMap::iterator it = mapBlockIndex.find(hash);
337  if (it != mapBlockIndex.end() && it->second->nChainWork > 0) {
338  // An actually better block was announced.
339  if (state->pindexBestKnownBlock == NULL || it->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
340  state->pindexBestKnownBlock = it->second;
341  } else {
342  // An unknown block was announced; just assume that the latest one is the best one.
343  state->hashLastUnknownBlock = hash;
344  }
345 }
346 
347 // Requires cs_main
348 bool CanDirectFetch(const Consensus::Params &consensusParams)
349 {
350  return chainActive.Tip()->GetBlockTime() > GetAdjustedTime() - consensusParams.nPowTargetSpacing * 20;
351 }
352 
353 // Requires cs_main
354 bool PeerHasHeader(CNodeState *state, CBlockIndex *pindex)
355 {
356  if (state->pindexBestKnownBlock && pindex == state->pindexBestKnownBlock->GetAncestor(pindex->nHeight))
357  return true;
358  if (state->pindexBestHeaderSent && pindex == state->pindexBestHeaderSent->GetAncestor(pindex->nHeight))
359  return true;
360  return false;
361 }
362 
365 CBlockIndex* LastCommonAncestor(CBlockIndex* pa, CBlockIndex* pb) {
366  if (pa->nHeight > pb->nHeight) {
367  pa = pa->GetAncestor(pb->nHeight);
368  } else if (pb->nHeight > pa->nHeight) {
369  pb = pb->GetAncestor(pa->nHeight);
370  }
371 
372  while (pa != pb && pa && pb) {
373  pa = pa->pprev;
374  pb = pb->pprev;
375  }
376 
377  // Eventually all chain branches meet at the genesis block.
378  assert(pa == pb);
379  return pa;
380 }
381 
384 void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams) {
385  if (count == 0)
386  return;
387 
388  vBlocks.reserve(vBlocks.size() + count);
389  CNodeState *state = State(nodeid);
390  assert(state != NULL);
391 
392  // Make sure pindexBestKnownBlock is up to date, we'll need it.
393  ProcessBlockAvailability(nodeid);
394 
395  if (state->pindexBestKnownBlock == NULL || state->pindexBestKnownBlock->nChainWork < chainActive.Tip()->nChainWork || state->pindexBestKnownBlock->nChainWork < UintToArith256(consensusParams.nMinimumChainWork)) {
396  // This peer has nothing interesting.
397  return;
398  }
399 
400  if (state->pindexLastCommonBlock == NULL) {
401  // Bootstrap quickly by guessing a parent of our best tip is the forking point.
402  // Guessing wrong in either direction is not a problem.
403  state->pindexLastCommonBlock = chainActive[std::min(state->pindexBestKnownBlock->nHeight, chainActive.Height())];
404  }
405 
406  // If the peer reorganized, our previous pindexLastCommonBlock may not be an ancestor
407  // of its current tip anymore. Go back enough to fix that.
408  state->pindexLastCommonBlock = LastCommonAncestor(state->pindexLastCommonBlock, state->pindexBestKnownBlock);
409  if (state->pindexLastCommonBlock == state->pindexBestKnownBlock)
410  return;
411 
412  std::vector<CBlockIndex*> vToFetch;
413  CBlockIndex *pindexWalk = state->pindexLastCommonBlock;
414  // Never fetch further than the best block we know the peer has, or more than BLOCK_DOWNLOAD_WINDOW + 1 beyond the last
415  // linked block we have in common with this peer. The +1 is so we can detect stalling, namely if we would be able to
416  // download that next block if the window were 1 larger.
417  int nWindowEnd = state->pindexLastCommonBlock->nHeight + BLOCK_DOWNLOAD_WINDOW;
418  int nMaxHeight = std::min<int>(state->pindexBestKnownBlock->nHeight, nWindowEnd + 1);
419  NodeId waitingfor = -1;
420  while (pindexWalk->nHeight < nMaxHeight) {
421  // Read up to 128 (or more, if more blocks than that are needed) successors of pindexWalk (towards
422  // pindexBestKnownBlock) into vToFetch. We fetch 128, because CBlockIndex::GetAncestor may be as expensive
423  // as iterating over ~100 CBlockIndex* entries anyway.
424  int nToFetch = std::min(nMaxHeight - pindexWalk->nHeight, std::max<int>(count - vBlocks.size(), 128));
425  vToFetch.resize(nToFetch);
426  pindexWalk = state->pindexBestKnownBlock->GetAncestor(pindexWalk->nHeight + nToFetch);
427  vToFetch[nToFetch - 1] = pindexWalk;
428  for (unsigned int i = nToFetch - 1; i > 0; i--) {
429  vToFetch[i - 1] = vToFetch[i]->pprev;
430  }
431 
432  // Iterate over those blocks in vToFetch (in forward direction), adding the ones that
433  // are not yet downloaded and not in flight to vBlocks. In the mean time, update
434  // pindexLastCommonBlock as long as all ancestors are already downloaded, or if it's
435  // already part of our chain (and therefore don't need it even if pruned).
436  BOOST_FOREACH(CBlockIndex* pindex, vToFetch) {
437  if (!pindex->IsValid(BLOCK_VALID_TREE)) {
438  // We consider the chain that this peer is on invalid.
439  return;
440  }
441  if (pindex->nStatus & BLOCK_HAVE_DATA || chainActive.Contains(pindex)) {
442  if (pindex->nChainTx)
443  state->pindexLastCommonBlock = pindex;
444  } else if (mapBlocksInFlight.count(pindex->GetBlockHash()) == 0) {
445  // The block is not already downloaded, and not yet in flight.
446  if (pindex->nHeight > nWindowEnd) {
447  // We reached the end of the window.
448  if (vBlocks.size() == 0 && waitingfor != nodeid) {
449  // We aren't able to fetch anything, but we would be if the download window was one larger.
450  nodeStaller = waitingfor;
451  }
452  return;
453  }
454  vBlocks.push_back(pindex);
455  if (vBlocks.size() == count) {
456  return;
457  }
458  } else if (waitingfor == -1) {
459  // This is the first already-in-flight block.
460  waitingfor = mapBlocksInFlight[pindex->GetBlockHash()].first;
461  }
462  }
463  }
464 }
465 
466 } // anon namespace
467 
469  LOCK(cs_main);
470  CNodeState *state = State(nodeid);
471  if (state == NULL)
472  return false;
473  stats.nMisbehavior = state->nMisbehavior;
474  stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
475  stats.nCommonHeight = state->pindexLastCommonBlock ? state->pindexLastCommonBlock->nHeight : -1;
476  BOOST_FOREACH(const QueuedBlock& queue, state->vBlocksInFlight) {
477  if (queue.pindex)
478  stats.vHeightInFlight.push_back(queue.pindex->nHeight);
479  }
480  return true;
481 }
482 
484 {
485  nodeSignals.ProcessMessages.connect(&ProcessMessages);
486  nodeSignals.SendMessages.connect(&SendMessages);
487  nodeSignals.InitializeNode.connect(&InitializeNode);
488  nodeSignals.FinalizeNode.connect(&FinalizeNode);
489 }
490 
492 {
493  nodeSignals.ProcessMessages.disconnect(&ProcessMessages);
494  nodeSignals.SendMessages.disconnect(&SendMessages);
495  nodeSignals.InitializeNode.disconnect(&InitializeNode);
496  nodeSignals.FinalizeNode.disconnect(&FinalizeNode);
497 }
498 
500 //
501 // mapOrphanTransactions
502 //
503 
505 {
506  uint256 hash = tx.GetHash();
507  if (mapOrphanTransactions.count(hash))
508  return false;
509 
510  // Ignore big transactions, to avoid a
511  // send-big-orphans memory exhaustion attack. If a peer has a legitimate
512  // large transaction with a missing parent then we assume
513  // it will rebroadcast it later, after the parent transaction(s)
514  // have been mined or received.
515  // 10,000 orphans, each of which is at most 5,000 bytes big is
516  // at most 500 megabytes of orphans:
517  unsigned int sz = tx.GetSerializeSize(SER_NETWORK, CTransaction::CURRENT_VERSION);
518  if (sz > 5000)
519  {
520  LogPrint("mempool", "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString());
521  return false;
522  }
523 
524  mapOrphanTransactions[hash].tx = tx;
525  mapOrphanTransactions[hash].fromPeer = peer;
526  BOOST_FOREACH(const CTxIn& txin, tx.vin)
527  mapOrphanTransactionsByPrev[txin.prevout.hash].insert(hash);
528 
529  LogPrint("mempool", "stored orphan tx %s (mapsz %u prevsz %u)\n", hash.ToString(),
530  mapOrphanTransactions.size(), mapOrphanTransactionsByPrev.size());
531  return true;
532 }
533 
535 {
536  map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.find(hash);
537  if (it == mapOrphanTransactions.end())
538  return;
539  BOOST_FOREACH(const CTxIn& txin, it->second.tx.vin)
540  {
541  map<uint256, set<uint256> >::iterator itPrev = mapOrphanTransactionsByPrev.find(txin.prevout.hash);
542  if (itPrev == mapOrphanTransactionsByPrev.end())
543  continue;
544  itPrev->second.erase(hash);
545  if (itPrev->second.empty())
546  mapOrphanTransactionsByPrev.erase(itPrev);
547  }
548  mapOrphanTransactions.erase(it);
549 }
550 
552 {
553  int nErased = 0;
554  map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
555  while (iter != mapOrphanTransactions.end())
556  {
557  map<uint256, COrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
558  if (maybeErase->second.fromPeer == peer)
559  {
560  EraseOrphanTx(maybeErase->second.tx.GetHash());
561  ++nErased;
562  }
563  }
564  if (nErased > 0) LogPrint("mempool", "Erased %d orphan tx from peer %d\n", nErased, peer);
565 }
566 
567 
568 unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
569 {
570  unsigned int nEvicted = 0;
571  while (mapOrphanTransactions.size() > nMaxOrphans)
572  {
573  // Evict a random orphan:
574  uint256 randomhash = GetRandHash();
575  map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.lower_bound(randomhash);
576  if (it == mapOrphanTransactions.end())
577  it = mapOrphanTransactions.begin();
578  EraseOrphanTx(it->first);
579  ++nEvicted;
580  }
581  return nEvicted;
582 }
583 
584 // Requires cs_main.
585 void Misbehaving(NodeId pnode, int howmuch)
586 {
587  if (howmuch == 0)
588  return;
589 
590  CNodeState *state = State(pnode);
591  if (state == NULL)
592  return;
593 
594  state->nMisbehavior += howmuch;
595  int banscore = GetArg("-banscore", DEFAULT_BANSCORE_THRESHOLD);
596  if (state->nMisbehavior >= banscore && state->nMisbehavior - howmuch < banscore)
597  {
598  LogPrintf("%s: %s (%d -> %d) BAN THRESHOLD EXCEEDED\n", __func__, state->name, state->nMisbehavior-howmuch, state->nMisbehavior);
599  state->fShouldBan = true;
600  } else
601  LogPrintf("%s: %s (%d -> %d)\n", __func__, state->name, state->nMisbehavior-howmuch, state->nMisbehavior);
602 }
603 
604 
605 
606 
607 
608 
609 
610 
612 //
613 // blockchain -> download logic notification
614 //
615 
616 PeerLogicValidation::PeerLogicValidation(CConnman* connmanIn) : connman(connmanIn) {
617  // Initialize global variables that cannot be constructed at startup.
618  recentRejects.reset(new CRollingBloomFilter(120000, 0.000001));
619 }
620 
621 void PeerLogicValidation::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
622  const int nNewHeight = pindexNew->nHeight;
623  connman->SetBestHeight(nNewHeight);
624 
625  if (!fInitialDownload) {
626  // Find the hashes of all blocks that weren't previously in the best chain.
627  std::vector<uint256> vHashes;
628  const CBlockIndex *pindexToAnnounce = pindexNew;
629  while (pindexToAnnounce != pindexFork) {
630  vHashes.push_back(pindexToAnnounce->GetBlockHash());
631  pindexToAnnounce = pindexToAnnounce->pprev;
632  if (vHashes.size() == MAX_BLOCKS_TO_ANNOUNCE) {
633  // Limit announcements in case of a huge reorganization.
634  // Rely on the peer's synchronization mechanism in that case.
635  break;
636  }
637  }
638  // Relay inventory, but don't relay old inventory during initial block download.
639  connman->ForEachNode([nNewHeight, &vHashes](CNode* pnode) {
640  if (nNewHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : 0)) {
641  BOOST_REVERSE_FOREACH(const uint256& hash, vHashes) {
642  pnode->PushBlockHash(hash);
643  }
644  }
645  });
646  }
647 
649 }
650 
652  LOCK(cs_main);
653 
654  const uint256 hash(block.GetHash());
655  std::map<uint256, NodeId>::iterator it = mapBlockSource.find(hash);
656 
657  int nDoS = 0;
658  if (state.IsInvalid(nDoS)) {
659  if (it != mapBlockSource.end() && State(it->second)) {
660  assert (state.GetRejectCode() < REJECT_INTERNAL); // Blocks are never rejected with internal reject codes
661  CBlockReject reject = {(unsigned char)state.GetRejectCode(), state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), hash};
662  State(it->second)->rejects.push_back(reject);
663  if (nDoS > 0)
664  Misbehaving(it->second, nDoS);
665  }
666  }
667  if (it != mapBlockSource.end())
668  mapBlockSource.erase(it);
669 }
670 
672 //
673 // Messages
674 //
675 
676 
678 {
679  switch (inv.type)
680  {
681  case MSG_TX:
682  {
683  assert(recentRejects);
684  if (chainActive.Tip()->GetBlockHash() != hashRecentRejectsChainTip)
685  {
686  // If the chain tip has changed previously rejected transactions
687  // might be now valid, e.g. due to a nLockTime'd tx becoming valid,
688  // or a double-spend. Reset the rejects filter and give those
689  // txs a second chance.
690  hashRecentRejectsChainTip = chainActive.Tip()->GetBlockHash();
691  recentRejects->reset();
692  }
693 
694  return recentRejects->contains(inv.hash) ||
695  mempool.exists(inv.hash) ||
696  mapOrphanTransactions.count(inv.hash) ||
697  pcoinsTip->HaveCoins(inv.hash);
698  }
699 
700  case MSG_BLOCK:
701  return mapBlockIndex.count(inv.hash);
702 
703  /*
704  Dash Related Inventory Messages
705 
706  --
707 
708  We shouldn't update the sync times for each of the messages when we already have it.
709  We're going to be asking many nodes upfront for the full inventory list, so we'll get duplicates of these.
710  We want to only update the time on new hits, so that we can time out appropriately if needed.
711  */
712  case MSG_TXLOCK_REQUEST:
713  return instantsend.AlreadyHave(inv.hash);
714 
715  case MSG_TXLOCK_VOTE:
716  return instantsend.AlreadyHave(inv.hash);
717 
718  case MSG_SPORK:
719  return mapSporks.count(inv.hash);
720 
722  return mnpayments.mapMasternodePaymentVotes.count(inv.hash);
723 
725  {
726  BlockMap::iterator mi = mapBlockIndex.find(inv.hash);
727  return mi != mapBlockIndex.end() && mnpayments.mapMasternodeBlocks.find(mi->second->nHeight) != mnpayments.mapMasternodeBlocks.end();
728  }
729 
731  return mnodeman.mapSeenMasternodeBroadcast.count(inv.hash) && !mnodeman.IsMnbRecoveryRequested(inv.hash);
732 
733  case MSG_MASTERNODE_PING:
734  return mnodeman.mapSeenMasternodePing.count(inv.hash);
735 
736  case MSG_DSTX: {
737  return static_cast<bool>(CPrivateSend::GetDSTX(inv.hash));
738  }
739 
742  return ! governance.ConfirmInventoryRequest(inv);
743 
745  return mnodeman.mapSeenMasternodeVerification.count(inv.hash);
746  }
747 
748  // Don't know what it is, just say we already got one
749  return true;
750 }
751 
752 static void RelayAddress(const CAddress& addr, bool fReachable, CConnman& connman)
753 {
754  int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
755 
756  // Relay to a limited number of other nodes
757  // Use deterministic randomness to send to the same nodes for 24 hours
758  // at a time so the addrKnowns of the chosen nodes prevent repeats
759  static uint256 hashSalt;
760  if (hashSalt.IsNull())
761  hashSalt = GetRandHash();
762  uint64_t hashAddr = addr.GetHash();
763  uint256 hashRand = ArithToUint256(UintToArith256(hashSalt) ^ (hashAddr<<32) ^ ((GetTime()+hashAddr)/(24*60*60)));
764  hashRand = Hash(BEGIN(hashRand), END(hashRand));
765  std::multimap<uint256, CNode*> mapMix;
766 
767  auto sortfunc = [&mapMix, &hashRand](CNode* pnode) {
768  if (pnode->nVersion >= CADDR_TIME_VERSION) {
769  unsigned int nPointer;
770  memcpy(&nPointer, &pnode, sizeof(nPointer));
771  uint256 hashKey = ArithToUint256(UintToArith256(hashRand) ^ nPointer);
772  hashKey = Hash(BEGIN(hashKey), END(hashKey));
773  mapMix.emplace(hashKey, pnode);
774  }
775  };
776 
777  auto pushfunc = [&addr, &mapMix, &nRelayNodes] {
778  for (auto mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi)
779  mi->second->PushAddress(addr);
780  };
781 
782  connman.ForEachNodeThen(std::move(sortfunc), std::move(pushfunc));
783 }
784 
785 void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParams, CConnman& connman, std::atomic<bool>& interruptMsgProc)
786 {
787  std::deque<CInv>::iterator it = pfrom->vRecvGetData.begin();
788  vector<CInv> vNotFound;
789 
790  LOCK(cs_main);
791 
792  while (it != pfrom->vRecvGetData.end()) {
793  // Don't bother if send buffer is too full to respond anyway
794  if (pfrom->fPauseSend)
795  break;
796 
797  const CInv &inv = *it;
798  LogPrint("net", "ProcessGetData -- inv = %s\n", inv.ToString());
799  {
800  if (interruptMsgProc)
801  return;
802 
803  it++;
804 
805  if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK)
806  {
807  bool send = false;
808  BlockMap::iterator mi = mapBlockIndex.find(inv.hash);
809  if (mi != mapBlockIndex.end())
810  {
811  if (chainActive.Contains(mi->second)) {
812  send = true;
813  } else {
814  static const int nOneMonth = 30 * 24 * 60 * 60;
815  // To prevent fingerprinting attacks, only send blocks outside of the active
816  // chain if they are valid, and no more than a month older (both in time, and in
817  // best equivalent proof of work) than the best header chain we know about.
818  send = mi->second->IsValid(BLOCK_VALID_SCRIPTS) && (pindexBestHeader != NULL) &&
819  (pindexBestHeader->GetBlockTime() - mi->second->GetBlockTime() < nOneMonth) &&
820  (GetBlockProofEquivalentTime(*pindexBestHeader, *mi->second, *pindexBestHeader, consensusParams) < nOneMonth);
821  if (!send) {
822  LogPrintf("%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom->GetId());
823  }
824  }
825  }
826  // disconnect node in case we have reached the outbound limit for serving historical blocks
827  // never disconnect whitelisted nodes
828  static const int nOneWeek = 7 * 24 * 60 * 60; // assume > 1 week = historical
829  if (send && connman.OutboundTargetReached(true) && ( ((pindexBestHeader != NULL) && (pindexBestHeader->GetBlockTime() - mi->second->GetBlockTime() > nOneWeek)) || inv.type == MSG_FILTERED_BLOCK) && !pfrom->fWhitelisted)
830  {
831  LogPrint("net", "historical block serving limit reached, disconnect peer=%d\n", pfrom->GetId());
832 
833  //disconnect node
834  pfrom->fDisconnect = true;
835  send = false;
836  }
837  // Pruned nodes may have deleted the block, so check whether
838  // it's available before trying to send.
839  if (send && (mi->second->nStatus & BLOCK_HAVE_DATA)) {
840  // Send block from disk
841  CBlock block;
842  if (!ReadBlockFromDisk(block, (*mi).second, consensusParams))
843  assert(!"cannot load block from disk");
844  if (inv.type == MSG_BLOCK)
845  connman.PushMessage(pfrom, NetMsgType::BLOCK, block);
846  else // MSG_FILTERED_BLOCK)
847  {
848  LOCK(pfrom->cs_filter);
849  if (pfrom->pfilter)
850  {
851  CMerkleBlock merkleBlock(block, *pfrom->pfilter);
852  connman.PushMessage(pfrom, NetMsgType::MERKLEBLOCK, merkleBlock);
853  // CMerkleBlock just contains hashes, so also push any transactions in the block the client did not see
854  // This avoids hurting performance by pointlessly requiring a round-trip
855  // Note that there is currently no way for a node to request any single transactions we didn't send here -
856  // they must either disconnect and retry or request the full block.
857  // Thus, the protocol spec specified allows for us to provide duplicate txn here,
858  // however we MUST always provide at least what the remote peer needs
859  typedef std::pair<unsigned int, uint256> PairType;
860  BOOST_FOREACH(PairType& pair, merkleBlock.vMatchedTxn)
861  connman.PushMessage(pfrom, NetMsgType::TX, block.vtx[pair.first]);
862  }
863  // else
864  // no response
865  }
866 
867  // Trigger the peer node to send a getblocks request for the next batch of inventory
868  if (inv.hash == pfrom->hashContinue)
869  {
870  // Bypass PushInventory, this must send even if redundant,
871  // and we want it right after the last block so they don't
872  // wait for other stuff first.
873  vector<CInv> vInv;
874  vInv.push_back(CInv(MSG_BLOCK, chainActive.Tip()->GetBlockHash()));
875  connman.PushMessage(pfrom, NetMsgType::INV, vInv);
876  pfrom->hashContinue.SetNull();
877  }
878  }
879  }
880  else if (inv.IsKnownType())
881  {
882  // Send stream from relay memory
883  bool pushed = false;
884  {
886  {
887  LOCK(cs_mapRelay);
888  map<CInv, CDataStream>::iterator mi = mapRelay.find(inv);
889  if (mi != mapRelay.end()) {
890  ss += (*mi).second;
891  pushed = true;
892  }
893  }
894  if(pushed)
895  connman.PushMessage(pfrom, inv.GetCommand(), ss);
896  }
897 
898  if (!pushed && inv.type == MSG_TX) {
899  CTransaction tx;
900  if (mempool.lookup(inv.hash, tx)) {
902  ss.reserve(1000);
903  ss << tx;
904  connman.PushMessage(pfrom, NetMsgType::TX, ss);
905  pushed = true;
906  }
907  }
908 
909  if (!pushed && inv.type == MSG_TXLOCK_REQUEST) {
910  CTxLockRequest txLockRequest;
911  if(instantsend.GetTxLockRequest(inv.hash, txLockRequest)) {
913  ss.reserve(1000);
914  ss << txLockRequest;
915  connman.PushMessage(pfrom, NetMsgType::TXLOCKREQUEST, ss);
916  pushed = true;
917  }
918  }
919 
920  if (!pushed && inv.type == MSG_TXLOCK_VOTE) {
921  CTxLockVote vote;
922  if(instantsend.GetTxLockVote(inv.hash, vote)) {
924  ss.reserve(1000);
925  ss << vote;
926  connman.PushMessage(pfrom, NetMsgType::TXLOCKVOTE, ss);
927  pushed = true;
928  }
929  }
930 
931  if (!pushed && inv.type == MSG_SPORK) {
932  if(mapSporks.count(inv.hash)) {
934  ss.reserve(1000);
935  ss << mapSporks[inv.hash];
936  connman.PushMessage(pfrom, NetMsgType::SPORK, ss);
937  pushed = true;
938  }
939  }
940 
941  if (!pushed && inv.type == MSG_MASTERNODE_PAYMENT_VOTE) {
944  ss.reserve(1000);
947  pushed = true;
948  }
949  }
950 
951  if (!pushed && inv.type == MSG_MASTERNODE_PAYMENT_BLOCK) {
952  BlockMap::iterator mi = mapBlockIndex.find(inv.hash);
954  if (mi != mapBlockIndex.end() && mnpayments.mapMasternodeBlocks.count(mi->second->nHeight)) {
955  BOOST_FOREACH(CMasternodePayee& payee, mnpayments.mapMasternodeBlocks[mi->second->nHeight].vecPayees) {
956  std::vector<uint256> vecVoteHashes = payee.GetVoteHashes();
957  BOOST_FOREACH(uint256& hash, vecVoteHashes) {
960  ss.reserve(1000);
963  }
964  }
965  }
966  pushed = true;
967  }
968  }
969 
970  if (!pushed && inv.type == MSG_MASTERNODE_ANNOUNCE) {
973  ss.reserve(1000);
974  ss << mnodeman.mapSeenMasternodeBroadcast[inv.hash].second;
975  connman.PushMessage(pfrom, NetMsgType::MNANNOUNCE, ss);
976  pushed = true;
977  }
978  }
979 
980  if (!pushed && inv.type == MSG_MASTERNODE_PING) {
981  if(mnodeman.mapSeenMasternodePing.count(inv.hash)) {
983  ss.reserve(1000);
985  connman.PushMessage(pfrom, NetMsgType::MNPING, ss);
986  pushed = true;
987  }
988  }
989 
990  if (!pushed && inv.type == MSG_DSTX) {
992  if(dstx) {
994  ss.reserve(1000);
995  ss << dstx;
996  connman.PushMessage(pfrom, NetMsgType::DSTX, ss);
997  pushed = true;
998  }
999  }
1000 
1001  if (!pushed && inv.type == MSG_GOVERNANCE_OBJECT) {
1002  LogPrint("net", "ProcessGetData -- MSG_GOVERNANCE_OBJECT: inv = %s\n", inv.ToString());
1004  bool topush = false;
1005  {
1006  if(governance.HaveObjectForHash(inv.hash)) {
1007  ss.reserve(1000);
1008  if(governance.SerializeObjectForHash(inv.hash, ss)) {
1009  topush = true;
1010  }
1011  }
1012  }
1013  LogPrint("net", "ProcessGetData -- MSG_GOVERNANCE_OBJECT: topush = %d, inv = %s\n", topush, inv.ToString());
1014  if(topush) {
1015  connman.PushMessage(pfrom, NetMsgType::MNGOVERNANCEOBJECT, ss);
1016  pushed = true;
1017  }
1018  }
1019 
1020  if (!pushed && inv.type == MSG_GOVERNANCE_OBJECT_VOTE) {
1022  bool topush = false;
1023  {
1024  if(governance.HaveVoteForHash(inv.hash)) {
1025  ss.reserve(1000);
1026  if(governance.SerializeVoteForHash(inv.hash, ss)) {
1027  topush = true;
1028  }
1029  }
1030  }
1031  if(topush) {
1032  LogPrint("net", "ProcessGetData -- pushing: inv = %s\n", inv.ToString());
1033  connman.PushMessage(pfrom, NetMsgType::MNGOVERNANCEOBJECTVOTE, ss);
1034  pushed = true;
1035  }
1036  }
1037 
1038  if (!pushed && inv.type == MSG_MASTERNODE_VERIFY) {
1041  ss.reserve(1000);
1043  connman.PushMessage(pfrom, NetMsgType::MNVERIFY, ss);
1044  pushed = true;
1045  }
1046  }
1047 
1048  if (!pushed)
1049  vNotFound.push_back(inv);
1050  }
1051 
1052  // Track requests for our stuff.
1053  GetMainSignals().Inventory(inv.hash);
1054 
1055  if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK)
1056  break;
1057  }
1058  }
1059 
1060  pfrom->vRecvGetData.erase(pfrom->vRecvGetData.begin(), it);
1061 
1062  if (!vNotFound.empty()) {
1063  // Let the peer know that we didn't find what it asked for, so it doesn't
1064  // have to wait around forever. Currently only SPV clients actually care
1065  // about this message: it's needed when they are recursively walking the
1066  // dependencies of relevant unconfirmed transactions. SPV clients want to
1067  // do that because they want to know about (and store and rebroadcast and
1068  // risk analyze) the dependencies of transactions relevant to them, without
1069  // having to download the entire memory pool.
1070  connman.PushMessage(pfrom, NetMsgType::NOTFOUND, vNotFound);
1071  }
1072 }
1073 
1074 bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, int64_t nTimeReceived, CConnman& connman, std::atomic<bool>& interruptMsgProc)
1075 {
1076  const CChainParams& chainparams = Params();
1078 
1079  LogPrint("net", "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand), vRecv.size(), pfrom->id);
1080 
1081  if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0)
1082  {
1083  LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n");
1084  return true;
1085  }
1086 
1087  if (!(pfrom->GetLocalServices() & NODE_BLOOM) &&
1088  (strCommand == NetMsgType::FILTERLOAD ||
1089  strCommand == NetMsgType::FILTERADD ||
1090  strCommand == NetMsgType::FILTERCLEAR))
1091  {
1092  if (pfrom->nVersion >= NO_BLOOM_VERSION) {
1093  LOCK(cs_main);
1094  Misbehaving(pfrom->GetId(), 100);
1095  return false;
1096  } else if (GetBoolArg("-enforcenodebloom", false)) {
1097  pfrom->fDisconnect = true;
1098  return false;
1099  }
1100  }
1101 
1102 
1103  if (strCommand == NetMsgType::VERSION)
1104  {
1105  // Feeler connections exist only to verify if address is online.
1106  if (pfrom->fFeeler) {
1107  assert(pfrom->fInbound == false);
1108  pfrom->fDisconnect = true;
1109  }
1110 
1111  // Each connection can only send one version message
1112  if (pfrom->nVersion != 0)
1113  {
1114  connman.PushMessageWithVersion(pfrom, INIT_PROTO_VERSION, NetMsgType::REJECT, strCommand, REJECT_DUPLICATE, string("Duplicate version message"));
1115  LOCK(cs_main);
1116  Misbehaving(pfrom->GetId(), 1);
1117  return false;
1118  }
1119 
1120  int64_t nTime;
1121  CAddress addrMe;
1122  CAddress addrFrom;
1123  uint64_t nNonce = 1;
1124  uint64_t nServiceInt;
1125  ServiceFlags nServices;
1126  int nVersion;
1127  int nSendVersion;
1128  std::string strSubVer;
1129  int nStartingHeight = -1;
1130  bool fRelay = true;
1131 
1132  vRecv >> nVersion >> nServiceInt >> nTime >> addrMe;
1133  nSendVersion = std::min(nVersion, PROTOCOL_VERSION);
1134  nServices = ServiceFlags(nServiceInt);
1135  if (!pfrom->fInbound)
1136  {
1137  connman.SetServices(pfrom->addr, nServices);
1138  }
1139  if (pfrom->nServicesExpected & ~nServices)
1140  {
1141  LogPrint("net", "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom->id, nServices, pfrom->nServicesExpected);
1143  strprintf("Expected to offer services %08x", pfrom->nServicesExpected));
1144  pfrom->fDisconnect = true;
1145  return false;
1146  }
1147 
1148  if (nVersion < MIN_PEER_PROTO_VERSION)
1149  {
1150  // disconnect from peers older than this proto version
1151  LogPrintf("peer=%d using obsolete version %i; disconnecting\n", pfrom->id, nVersion);
1153  strprintf("Version must be %d or greater", MIN_PEER_PROTO_VERSION));
1154  pfrom->fDisconnect = true;
1155  return false;
1156  }
1157 
1158  if (nVersion == 10300)
1159  nVersion = 300;
1160  if (!vRecv.empty())
1161  vRecv >> addrFrom >> nNonce;
1162  if (!vRecv.empty()) {
1163  vRecv >> LIMITED_STRING(strSubVer, MAX_SUBVERSION_LENGTH);
1164  }
1165  if (!vRecv.empty()) {
1166  vRecv >> nStartingHeight;
1167  }
1168  if (!vRecv.empty())
1169  vRecv >> fRelay;
1170  // Disconnect if we connected to ourself
1171  if (pfrom->fInbound && !connman.CheckIncomingNonce(nNonce))
1172  {
1173  LogPrintf("connected to self at %s, disconnecting\n", pfrom->addr.ToString());
1174  pfrom->fDisconnect = true;
1175  return true;
1176  }
1177 
1178  if (pfrom->fInbound && addrMe.IsRoutable())
1179  {
1180  SeenLocal(addrMe);
1181  }
1182 
1183  // Be shy and don't send version until we hear
1184  if (pfrom->fInbound)
1185  PushNodeVersion(pfrom, connman, GetAdjustedTime());
1186 
1188 
1189  pfrom->nServices = nServices;
1190  pfrom->addrLocal = addrMe;
1191  pfrom->strSubVer = strSubVer;
1192  pfrom->cleanSubVer = SanitizeString(strSubVer);
1193  pfrom->nStartingHeight = nStartingHeight;
1194  pfrom->fClient = !(nServices & NODE_NETWORK);
1195  {
1196  LOCK(pfrom->cs_filter);
1197  pfrom->fRelayTxes = fRelay; // set to true after we get the first filter* message
1198  }
1199 
1200  // Change version
1201  pfrom->SetSendVersion(nSendVersion);
1202  pfrom->nVersion = nVersion;
1203 
1204  // Potentially mark this peer as a preferred download peer.
1205  {
1206  LOCK(cs_main);
1207  UpdatePreferredDownload(pfrom, State(pfrom->GetId()));
1208  }
1209 
1210  if (!pfrom->fInbound)
1211  {
1212  // Advertise our address
1213  if (fListen && !IsInitialBlockDownload())
1214  {
1215  CAddress addr = GetLocalAddress(&pfrom->addr, pfrom->GetLocalServices());
1216  if (addr.IsRoutable())
1217  {
1218  LogPrintf("ProcessMessages: advertising address %s\n", addr.ToString());
1219  pfrom->PushAddress(addr);
1220  } else if (IsPeerAddrLocalGood(pfrom)) {
1221  addr.SetIP(pfrom->addrLocal);
1222  LogPrintf("ProcessMessages: advertising address %s\n", addr.ToString());
1223  pfrom->PushAddress(addr);
1224  }
1225  }
1226 
1227  // Get recent addresses
1228  if (pfrom->fOneShot || pfrom->nVersion >= CADDR_TIME_VERSION || connman.GetAddressCount() < 1000)
1229  {
1230  connman.PushMessage(pfrom, NetMsgType::GETADDR);
1231  pfrom->fGetAddr = true;
1232  }
1233  connman.MarkAddressGood(pfrom->addr);
1234  }
1235 
1236  // Relay alerts
1237  {
1238  LOCK(cs_mapAlerts);
1239  BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
1240  item.second.RelayTo(pfrom, connman);
1241  }
1242 
1243  string remoteAddr;
1244  if (fLogIPs)
1245  remoteAddr = ", peeraddr=" + pfrom->addr.ToString();
1246 
1247  LogPrintf("receive version message: %s: version %d, blocks=%d, us=%s, peer=%d%s\n",
1248  pfrom->cleanSubVer, pfrom->nVersion,
1249  pfrom->nStartingHeight, addrMe.ToString(), pfrom->id,
1250  remoteAddr);
1251 
1252  int64_t nTimeOffset = nTime - GetTime();
1253  pfrom->nTimeOffset = nTimeOffset;
1254  AddTimeData(pfrom->addr, nTimeOffset);
1255  }
1256 
1257 
1258  else if (pfrom->nVersion == 0)
1259  {
1260  // Must have a version message before anything else
1261  LOCK(cs_main);
1262  Misbehaving(pfrom->GetId(), 1);
1263  return false;
1264  }
1265 
1266 
1267  else if (strCommand == NetMsgType::VERACK)
1268  {
1269  pfrom->SetRecvVersion(std::min(pfrom->nVersion.load(), PROTOCOL_VERSION));
1270 
1271  // Mark this node as currently connected, so we update its timestamp later.
1272  if (pfrom->fNetworkNode) {
1273  LOCK(cs_main);
1274  State(pfrom->GetId())->fCurrentlyConnected = true;
1275  }
1276 
1277  if (pfrom->nVersion >= SENDHEADERS_VERSION) {
1278  // Tell our peer we prefer to receive headers rather than inv's
1279  // We send this to non-NODE NETWORK peers as well, because even
1280  // non-NODE NETWORK peers can announce blocks (such as pruning
1281  // nodes)
1282  connman.PushMessage(pfrom, NetMsgType::SENDHEADERS);
1283  }
1284  pfrom->fSuccessfullyConnected = true;
1285  }
1286 
1287 
1288  else if (strCommand == NetMsgType::ADDR)
1289  {
1290  vector<CAddress> vAddr;
1291  vRecv >> vAddr;
1292 
1293  // Don't want addr from older versions unless seeding
1294  if (pfrom->nVersion < CADDR_TIME_VERSION && connman.GetAddressCount() > 1000)
1295  return true;
1296  if (vAddr.size() > 1000)
1297  {
1298  LOCK(cs_main);
1299  Misbehaving(pfrom->GetId(), 20);
1300  return error("message addr size() = %u", vAddr.size());
1301  }
1302 
1303  // Store the new addresses
1304  vector<CAddress> vAddrOk;
1305  int64_t nNow = GetAdjustedTime();
1306  int64_t nSince = nNow - 10 * 60;
1307  BOOST_FOREACH(CAddress& addr, vAddr)
1308  {
1309  if (interruptMsgProc)
1310  return true;
1311 
1313  continue;
1314 
1315  if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
1316  addr.nTime = nNow - 5 * 24 * 60 * 60;
1317  pfrom->AddAddressKnown(addr);
1318  bool fReachable = IsReachable(addr);
1319  if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
1320  {
1321  RelayAddress(addr, fReachable, connman);
1322  }
1323  // Do not store addresses outside our network
1324  if (fReachable)
1325  vAddrOk.push_back(addr);
1326  }
1327  connman.AddNewAddresses(vAddrOk, pfrom->addr, 2 * 60 * 60);
1328  if (vAddr.size() < 1000)
1329  pfrom->fGetAddr = false;
1330  if (pfrom->fOneShot)
1331  pfrom->fDisconnect = true;
1332  }
1333 
1334  else if (strCommand == NetMsgType::SENDHEADERS)
1335  {
1336  LOCK(cs_main);
1337  State(pfrom->GetId())->fPreferHeaders = true;
1338  }
1339 
1340 
1341  else if (strCommand == NetMsgType::INV)
1342  {
1343  vector<CInv> vInv;
1344  vRecv >> vInv;
1345  if (vInv.size() > MAX_INV_SZ)
1346  {
1347  LOCK(cs_main);
1348  Misbehaving(pfrom->GetId(), 20);
1349  return error("message inv size() = %u", vInv.size());
1350  }
1351 
1352  bool fBlocksOnly = !fRelayTxes;
1353 
1354  // Allow whitelisted peers to send data other than blocks in blocks only mode if whitelistrelay is true
1355  if (pfrom->fWhitelisted && GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY))
1356  fBlocksOnly = false;
1357 
1358  LOCK(cs_main);
1359 
1360  std::vector<CInv> vToFetch;
1361 
1362 
1363  for (unsigned int nInv = 0; nInv < vInv.size(); nInv++)
1364  {
1365  const CInv &inv = vInv[nInv];
1366 
1367  if(!inv.IsKnownType()) {
1368  LogPrint("net", "got inv of unknown type %d: %s peer=%d\n", inv.type, inv.hash.ToString(), pfrom->id);
1369  continue;
1370  }
1371 
1372  if (interruptMsgProc)
1373  return true;
1374 
1375  pfrom->AddInventoryKnown(inv);
1376 
1377  bool fAlreadyHave = AlreadyHave(inv);
1378  LogPrint("net", "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom->id);
1379 
1380  if (inv.type == MSG_BLOCK) {
1381  UpdateBlockAvailability(pfrom->GetId(), inv.hash);
1382  if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count(inv.hash)) {
1383  if (chainparams.DelayGetHeadersTime() != 0 && pindexBestHeader->GetBlockTime() < GetAdjustedTime() - chainparams.DelayGetHeadersTime()) {
1384  // We are pretty far from being completely synced at the moment. If we would initiate a new
1385  // chain of GETHEADERS/HEADERS now, we may end up downnloading the full chain from multiple
1386  // peers at the same time, slowing down the initial sync. At the same time, we don't know
1387  // if the peer we got this INV from may have a chain we don't know about yet, so we HAVE TO
1388  // send a GETHEADERS message at some point in time. This is delayed to later in SendMessages
1389  // when the headers chain has catched up enough.
1390  LogPrint("net", "delaying getheaders (%d) %s to peer=%d\n", pindexBestHeader->nHeight, inv.hash.ToString(), pfrom->id);
1391  pfrom->PushBlockHashFromINV(inv.hash);
1392  } else {
1393  // First request the headers preceding the announced block. In the normal fully-synced
1394  // case where a new block is announced that succeeds the current tip (no reorganization),
1395  // there are no such headers.
1396  // Secondly, and only when we are close to being synced, we request the announced block directly,
1397  // to avoid an extra round-trip. Note that we must *first* ask for the headers, so by the
1398  // time the block arrives, the header chain leading up to it is already validated. Not
1399  // doing this will result in the received block being rejected as an orphan in case it is
1400  // not a direct successor.
1402  CNodeState *nodestate = State(pfrom->GetId());
1403  if (CanDirectFetch(chainparams.GetConsensus()) &&
1404  nodestate->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
1405  vToFetch.push_back(inv);
1406  // Mark block as in flight already, even though the actual "getdata" message only goes out
1407  // later (within the same cs_main lock, though).
1408  MarkBlockAsInFlight(pfrom->GetId(), inv.hash, chainparams.GetConsensus());
1409  }
1410  LogPrint("net", "getheaders (%d) %s to peer=%d\n", pindexBestHeader->nHeight, inv.hash.ToString(), pfrom->id);
1411  }
1412  }
1413  }
1414  else
1415  {
1416  if (fBlocksOnly)
1417  LogPrint("net", "transaction (%s) inv sent in violation of protocol peer=%d\n", inv.hash.ToString(), pfrom->id);
1418  else if (!fAlreadyHave && !fImporting && !fReindex && !IsInitialBlockDownload())
1419  pfrom->AskFor(inv);
1420  }
1421 
1422  // Track requests for our stuff
1423  GetMainSignals().Inventory(inv.hash);
1424  }
1425 
1426  if (!vToFetch.empty())
1427  connman.PushMessage(pfrom, NetMsgType::GETDATA, vToFetch);
1428  }
1429 
1430 
1431  else if (strCommand == NetMsgType::GETDATA)
1432  {
1433  vector<CInv> vInv;
1434  vRecv >> vInv;
1435  if (vInv.size() > MAX_INV_SZ)
1436  {
1437  LOCK(cs_main);
1438  Misbehaving(pfrom->GetId(), 20);
1439  return error("message getdata size() = %u", vInv.size());
1440  }
1441 
1442  if (fDebug || (vInv.size() != 1))
1443  LogPrint("net", "received getdata (%u invsz) peer=%d\n", vInv.size(), pfrom->id);
1444 
1445  if ((fDebug && vInv.size() > 0) || (vInv.size() == 1))
1446  LogPrint("net", "received getdata for: %s peer=%d\n", vInv[0].ToString(), pfrom->id);
1447 
1448  pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end());
1449  ProcessGetData(pfrom, chainparams.GetConsensus(), connman, interruptMsgProc);
1450  }
1451 
1452 
1453  else if (strCommand == NetMsgType::GETBLOCKS)
1454  {
1455  CBlockLocator locator;
1456  uint256 hashStop;
1457  vRecv >> locator >> hashStop;
1458 
1459  LOCK(cs_main);
1460 
1461  // Find the last block the caller has in the main chain
1462  CBlockIndex* pindex = FindForkInGlobalIndex(chainActive, locator);
1463 
1464  // Send the rest of the chain
1465  if (pindex)
1466  pindex = chainActive.Next(pindex);
1467  int nLimit = 500;
1468  LogPrint("net", "getblocks %d to %s limit %d from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), nLimit, pfrom->id);
1469  for (; pindex; pindex = chainActive.Next(pindex))
1470  {
1471  if (pindex->GetBlockHash() == hashStop)
1472  {
1473  LogPrint("net", " getblocks stopping at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
1474  break;
1475  }
1476  // If pruning, don't inv blocks unless we have on disk and are likely to still have
1477  // for some reasonable time window (1 hour) that block relay might require.
1478  const int nPrunedBlocksLikelyToHave = MIN_BLOCKS_TO_KEEP - 3600 / chainparams.GetConsensus().nPowTargetSpacing;
1479  if (fPruneMode && (!(pindex->nStatus & BLOCK_HAVE_DATA) || pindex->nHeight <= chainActive.Tip()->nHeight - nPrunedBlocksLikelyToHave))
1480  {
1481  LogPrint("net", " getblocks stopping, pruned or too old block at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
1482  break;
1483  }
1484  pfrom->PushInventory(CInv(MSG_BLOCK, pindex->GetBlockHash()));
1485  if (--nLimit <= 0)
1486  {
1487  // When this block is requested, we'll send an inv that'll
1488  // trigger the peer to getblocks the next batch of inventory.
1489  LogPrint("net", " getblocks stopping at limit %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
1490  pfrom->hashContinue = pindex->GetBlockHash();
1491  break;
1492  }
1493  }
1494  }
1495 
1496 
1497  else if (strCommand == NetMsgType::GETHEADERS)
1498  {
1499  CBlockLocator locator;
1500  uint256 hashStop;
1501  vRecv >> locator >> hashStop;
1502 
1503  LOCK(cs_main);
1504  if (IsInitialBlockDownload() && !pfrom->fWhitelisted) {
1505  LogPrint("net", "Ignoring getheaders from peer=%d because node is in initial block download\n", pfrom->id);
1506  return true;
1507  }
1508 
1509  CNodeState *nodestate = State(pfrom->GetId());
1510  CBlockIndex* pindex = NULL;
1511  if (locator.IsNull())
1512  {
1513  // If locator is null, return the hashStop block
1514  BlockMap::iterator mi = mapBlockIndex.find(hashStop);
1515  if (mi == mapBlockIndex.end())
1516  return true;
1517  pindex = (*mi).second;
1518  }
1519  else
1520  {
1521  // Find the last block the caller has in the main chain
1522  pindex = FindForkInGlobalIndex(chainActive, locator);
1523  if (pindex)
1524  pindex = chainActive.Next(pindex);
1525  }
1526 
1527  // we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
1528  vector<CBlock> vHeaders;
1529  int nLimit = MAX_HEADERS_RESULTS;
1530  LogPrint("net", "getheaders %d to %s from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.ToString(), pfrom->id);
1531  for (; pindex; pindex = chainActive.Next(pindex))
1532  {
1533  vHeaders.push_back(pindex->GetBlockHeader());
1534  if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
1535  break;
1536  }
1537  // pindex can be NULL either if we sent chainActive.Tip() OR
1538  // if our peer has chainActive.Tip() (and thus we are sending an empty
1539  // headers message). In both cases it's safe to update
1540  // pindexBestHeaderSent to be our tip.
1541  nodestate->pindexBestHeaderSent = pindex ? pindex : chainActive.Tip();
1542  connman.PushMessage(pfrom, NetMsgType::HEADERS, vHeaders);
1543  }
1544 
1545 
1546  else if (strCommand == NetMsgType::TX || strCommand == NetMsgType::DSTX || strCommand == NetMsgType::TXLOCKREQUEST)
1547  {
1548  // Stop processing the transaction early if
1549  // We are in blocks only mode and peer is either not whitelisted or whitelistrelay is off
1550  if (!fRelayTxes && (!pfrom->fWhitelisted || !GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY)))
1551  {
1552  LogPrint("net", "transaction sent in violation of protocol peer=%d\n", pfrom->id);
1553  return true;
1554  }
1555 
1556  vector<uint256> vWorkQueue;
1557  vector<uint256> vEraseQueue;
1558  CTransaction tx;
1559  CTxLockRequest txLockRequest;
1560  CDarksendBroadcastTx dstx;
1561  int nInvType = MSG_TX;
1562 
1563  // Read data and assign inv type
1564  if(strCommand == NetMsgType::TX) {
1565  vRecv >> tx;
1566  } else if(strCommand == NetMsgType::TXLOCKREQUEST) {
1567  vRecv >> txLockRequest;
1568  tx = txLockRequest;
1569  nInvType = MSG_TXLOCK_REQUEST;
1570  } else if (strCommand == NetMsgType::DSTX) {
1571  vRecv >> dstx;
1572  tx = dstx.tx;
1573  nInvType = MSG_DSTX;
1574  }
1575 
1576  CInv inv(nInvType, tx.GetHash());
1577  pfrom->AddInventoryKnown(inv);
1578  pfrom->setAskFor.erase(inv.hash);
1579 
1580  // Process custom logic, no matter if tx will be accepted to mempool later or not
1581  if (strCommand == NetMsgType::TXLOCKREQUEST) {
1582  if(!instantsend.ProcessTxLockRequest(txLockRequest, connman)) {
1583  LogPrint("instantsend", "TXLOCKREQUEST -- failed %s\n", txLockRequest.GetHash().ToString());
1584  return false;
1585  }
1586  } else if (strCommand == NetMsgType::DSTX) {
1587  uint256 hashTx = tx.GetHash();
1588  if(CPrivateSend::GetDSTX(hashTx)) {
1589  LogPrint("privatesend", "DSTX -- Already have %s, skipping...\n", hashTx.ToString());
1590  return true; // not an error
1591  }
1592 
1593  CMasternode mn;
1594 
1595  if(!mnodeman.Get(dstx.vin.prevout, mn)) {
1596  LogPrint("privatesend", "DSTX -- Can't find masternode %s to verify %s\n", dstx.vin.prevout.ToStringShort(), hashTx.ToString());
1597  return false;
1598  }
1599 
1600  if(!mn.fAllowMixingTx) {
1601  LogPrint("privatesend", "DSTX -- Masternode %s is sending too many transactions %s\n", dstx.vin.prevout.ToStringShort(), hashTx.ToString());
1602  return true;
1603  // TODO: Not an error? Could it be that someone is relaying old DSTXes
1604  // we have no idea about (e.g we were offline)? How to handle them?
1605  }
1606 
1607  if(!dstx.CheckSignature(mn.pubKeyMasternode)) {
1608  LogPrint("privatesend", "DSTX -- CheckSignature() failed for %s\n", hashTx.ToString());
1609  return false;
1610  }
1611 
1612  LogPrintf("DSTX -- Got Masternode transaction %s\n", hashTx.ToString());
1613  mempool.PrioritiseTransaction(hashTx, hashTx.ToString(), 1000, 0.1*COIN);
1615  }
1616 
1617  LOCK(cs_main);
1618 
1619  bool fMissingInputs = false;
1620  CValidationState state;
1621 
1622  mapAlreadyAskedFor.erase(inv.hash);
1623 
1624  if (!AlreadyHave(inv) && AcceptToMemoryPool(mempool, state, tx, true, &fMissingInputs))
1625  {
1626  // Process custom txes, this changes AlreadyHave to "true"
1627  if (strCommand == NetMsgType::DSTX) {
1628  LogPrintf("DSTX -- Masternode transaction accepted, txid=%s, peer=%d\n",
1629  tx.GetHash().ToString(), pfrom->id);
1630  CPrivateSend::AddDSTX(dstx);
1631  } else if (strCommand == NetMsgType::TXLOCKREQUEST) {
1632  LogPrintf("TXLOCKREQUEST -- Transaction Lock Request accepted, txid=%s, peer=%d\n",
1633  tx.GetHash().ToString(), pfrom->id);
1634  instantsend.AcceptLockRequest(txLockRequest);
1635  }
1636 
1638  connman.RelayTransaction(tx);
1639  vWorkQueue.push_back(inv.hash);
1640 
1641  pfrom->nLastTXTime = GetTime();
1642 
1643  LogPrint("mempool", "AcceptToMemoryPool: peer=%d: accepted %s (poolsz %u txn, %u kB)\n",
1644  pfrom->id,
1645  tx.GetHash().ToString(),
1646  mempool.size(), mempool.DynamicMemoryUsage() / 1000);
1647 
1648  // Recursively process any orphan transactions that depended on this one
1649  set<NodeId> setMisbehaving;
1650  for (unsigned int i = 0; i < vWorkQueue.size(); i++)
1651  {
1652  map<uint256, set<uint256> >::iterator itByPrev = mapOrphanTransactionsByPrev.find(vWorkQueue[i]);
1653  if (itByPrev == mapOrphanTransactionsByPrev.end())
1654  continue;
1655  for (set<uint256>::iterator mi = itByPrev->second.begin();
1656  mi != itByPrev->second.end();
1657  ++mi)
1658  {
1659  const uint256& orphanHash = *mi;
1660  const CTransaction& orphanTx = mapOrphanTransactions[orphanHash].tx;
1661  NodeId fromPeer = mapOrphanTransactions[orphanHash].fromPeer;
1662  bool fMissingInputs2 = false;
1663  // Use a dummy CValidationState so someone can't setup nodes to counter-DoS based on orphan
1664  // resolution (that is, feeding people an invalid transaction based on LegitTxX in order to get
1665  // anyone relaying LegitTxX banned)
1666  CValidationState stateDummy;
1667 
1668 
1669  if (setMisbehaving.count(fromPeer))
1670  continue;
1671  if (AcceptToMemoryPool(mempool, stateDummy, orphanTx, true, &fMissingInputs2))
1672  {
1673  LogPrint("mempool", " accepted orphan tx %s\n", orphanHash.ToString());
1674  connman.RelayTransaction(orphanTx);
1675  vWorkQueue.push_back(orphanHash);
1676  vEraseQueue.push_back(orphanHash);
1677  }
1678  else if (!fMissingInputs2)
1679  {
1680  int nDos = 0;
1681  if (stateDummy.IsInvalid(nDos) && nDos > 0)
1682  {
1683  // Punish peer that gave us an invalid orphan tx
1684  Misbehaving(fromPeer, nDos);
1685  setMisbehaving.insert(fromPeer);
1686  LogPrint("mempool", " invalid orphan tx %s\n", orphanHash.ToString());
1687  }
1688  // Has inputs but not accepted to mempool
1689  // Probably non-standard or insufficient fee/priority
1690  LogPrint("mempool", " removed orphan tx %s\n", orphanHash.ToString());
1691  vEraseQueue.push_back(orphanHash);
1692  assert(recentRejects);
1693  recentRejects->insert(orphanHash);
1694  }
1696  }
1697  }
1698 
1699  BOOST_FOREACH(uint256 hash, vEraseQueue)
1700  EraseOrphanTx(hash);
1701  }
1702  else if (fMissingInputs)
1703  {
1704  AddOrphanTx(tx, pfrom->GetId());
1705 
1706  // DoS prevention: do not allow mapOrphanTransactions to grow unbounded
1707  unsigned int nMaxOrphanTx = (unsigned int)std::max((int64_t)0, GetArg("-maxorphantx", DEFAULT_MAX_ORPHAN_TRANSACTIONS));
1708  unsigned int nEvicted = LimitOrphanTxSize(nMaxOrphanTx);
1709  if (nEvicted > 0)
1710  LogPrint("mempool", "mapOrphan overflow, removed %u tx\n", nEvicted);
1711  } else {
1712  assert(recentRejects);
1713  recentRejects->insert(tx.GetHash());
1714 
1715  if (strCommand == NetMsgType::TXLOCKREQUEST && !AlreadyHave(inv)) {
1716  // i.e. AcceptToMemoryPool failed, probably because it's conflicting
1717  // with existing normal tx or tx lock for another tx. For the same tx lock
1718  // AlreadyHave would have return "true" already.
1719 
1720  // It's the first time we failed for this tx lock request,
1721  // this should switch AlreadyHave to "true".
1722  instantsend.RejectLockRequest(txLockRequest);
1723  // this lets other nodes to create lock request candidate i.e.
1724  // this allows multiple conflicting lock requests to compete for votes
1725  connman.RelayTransaction(tx);
1726  }
1727 
1728  if (pfrom->fWhitelisted && GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY)) {
1729  // Always relay transactions received from whitelisted peers, even
1730  // if they were already in the mempool or rejected from it due
1731  // to policy, allowing the node to function as a gateway for
1732  // nodes hidden behind it.
1733  //
1734  // Never relay transactions that we would assign a non-zero DoS
1735  // score for, as we expect peers to do the same with us in that
1736  // case.
1737  int nDoS = 0;
1738  if (!state.IsInvalid(nDoS) || nDoS == 0) {
1739  LogPrintf("Force relaying tx %s from whitelisted peer=%d\n", tx.GetHash().ToString(), pfrom->id);
1740  connman.RelayTransaction(tx);
1741  } else {
1742  LogPrintf("Not relaying invalid transaction %s from whitelisted peer=%d (%s)\n", tx.GetHash().ToString(), pfrom->id, FormatStateMessage(state));
1743  }
1744  }
1745  }
1746 
1747  int nDoS = 0;
1748  if (state.IsInvalid(nDoS))
1749  {
1750  LogPrint("mempoolrej", "%s from peer=%d was not accepted: %s\n", tx.GetHash().ToString(),
1751  pfrom->id,
1752  FormatStateMessage(state));
1753  if (state.GetRejectCode() < REJECT_INTERNAL) // Never send AcceptToMemoryPool's internal codes over P2P
1754  connman.PushMessage(pfrom, NetMsgType::REJECT, strCommand, (unsigned char)state.GetRejectCode(),
1755  state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), inv.hash);
1756  if (nDoS > 0)
1757  Misbehaving(pfrom->GetId(), nDoS);
1758  }
1759  }
1760 
1761 
1762  else if (strCommand == NetMsgType::HEADERS && !fImporting && !fReindex) // Ignore headers received while importing
1763  {
1764  std::vector<CBlockHeader> headers;
1765 
1766  // Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks.
1767  unsigned int nCount = ReadCompactSize(vRecv);
1768  if (nCount > MAX_HEADERS_RESULTS) {
1769  LOCK(cs_main);
1770  Misbehaving(pfrom->GetId(), 20);
1771  return error("headers message size = %u", nCount);
1772  }
1773  headers.resize(nCount);
1774  for (unsigned int n = 0; n < nCount; n++) {
1775  vRecv >> headers[n];
1776  ReadCompactSize(vRecv); // ignore tx count; assume it is 0.
1777  }
1778 
1779  CBlockIndex *pindexLast = NULL;
1780  {
1781  LOCK(cs_main);
1782  uint256 hashLastBlock;
1783  for (const CBlockHeader& header : headers) {
1784  if (!hashLastBlock.IsNull() && header.hashPrevBlock != hashLastBlock) {
1785  Misbehaving(pfrom->GetId(), 20);
1786  return error("non-continuous headers sequence");
1787  }
1788  hashLastBlock = header.GetHash();
1789  }
1790  }
1791 
1792  CValidationState state;
1793  if (!ProcessNewBlockHeaders(headers, state, chainparams, &pindexLast)) {
1794  int nDoS;
1795  if (state.IsInvalid(nDoS)) {
1796  if (nDoS > 0) {
1797  LOCK(cs_main);
1798  Misbehaving(pfrom->GetId(), nDoS);
1799  }
1800  return error("invalid header received");
1801  }
1802  }
1803 
1804  {
1805  LOCK(cs_main);
1806  if (pindexLast)
1807  UpdateBlockAvailability(pfrom->GetId(), pindexLast->GetBlockHash());
1808 
1809  if (nCount == MAX_HEADERS_RESULTS && pindexLast) {
1810  // Headers message had its maximum size; the peer may have more headers.
1811  // TODO: optimize: if pindexLast is an ancestor of chainActive.Tip or pindexBestHeader, continue
1812  // from there instead.
1813  LogPrint("net", "more getheaders (%d) to end to peer=%d (startheight:%d)\n", pindexLast->nHeight, pfrom->id, pfrom->nStartingHeight);
1814  connman.PushMessage(pfrom, NetMsgType::GETHEADERS, chainActive.GetLocator(pindexLast), uint256());
1815  } else {
1816  if (chainparams.DelayGetHeadersTime() != 0 && pindexBestHeader->GetBlockTime() < GetAdjustedTime() - chainparams.DelayGetHeadersTime()) {
1817  // peer has sent us a HEADERS message below maximum size and we are still quite far from being fully
1818  // synced, this means we probably got a bad peer for initial sync and need to continue with another one.
1819  // By disconnecting we force to start a new iteration of initial headers sync in SendMessages
1820  // TODO should we handle whitelisted peers here as we do in headers sync timeout handling?
1821  pfrom->fDisconnect = true;
1822  return error("detected bad peer for initial headers sync, disconnecting %d", pfrom->id);
1823  }
1824 
1825  if (nCount == 0) {
1826  // Nothing interesting. Stop asking this peers for more headers.
1827  return true;
1828  }
1829  }
1830 
1831  bool fCanDirectFetch = CanDirectFetch(chainparams.GetConsensus());
1832  CNodeState *nodestate = State(pfrom->GetId());
1833  // If this set of headers is valid and ends in a block with at least as
1834  // much work as our tip, download as much as possible.
1835  if (fCanDirectFetch && pindexLast->IsValid(BLOCK_VALID_TREE) && chainActive.Tip()->nChainWork <= pindexLast->nChainWork) {
1836  vector<CBlockIndex *> vToFetch;
1837  CBlockIndex *pindexWalk = pindexLast;
1838  // Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
1839  while (pindexWalk && !chainActive.Contains(pindexWalk) && vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
1840  if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
1841  !mapBlocksInFlight.count(pindexWalk->GetBlockHash())) {
1842  // We don't have this block, and it's not yet in flight.
1843  vToFetch.push_back(pindexWalk);
1844  }
1845  pindexWalk = pindexWalk->pprev;
1846  }
1847  // If pindexWalk still isn't on our main chain, we're looking at a
1848  // very large reorg at a time we think we're close to caught up to
1849  // the main chain -- this shouldn't really happen. Bail out on the
1850  // direct fetch and rely on parallel download instead.
1851  if (!chainActive.Contains(pindexWalk)) {
1852  LogPrint("net", "Large reorg, won't direct fetch to %s (%d)\n",
1853  pindexLast->GetBlockHash().ToString(),
1854  pindexLast->nHeight);
1855  } else {
1856  vector<CInv> vGetData;
1857  // Download as much as possible, from earliest to latest.
1858  BOOST_REVERSE_FOREACH(CBlockIndex *pindex, vToFetch) {
1859  if (nodestate->nBlocksInFlight >= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
1860  // Can't download any more from this peer
1861  break;
1862  }
1863  vGetData.push_back(CInv(MSG_BLOCK, pindex->GetBlockHash()));
1864  MarkBlockAsInFlight(pfrom->GetId(), pindex->GetBlockHash(), chainparams.GetConsensus(), pindex);
1865  LogPrint("net", "Requesting block %s from peer=%d\n",
1866  pindex->GetBlockHash().ToString(), pfrom->id);
1867  }
1868  if (vGetData.size() > 1) {
1869  LogPrint("net", "Downloading blocks toward %s (%d) via headers direct fetch\n",
1870  pindexLast->GetBlockHash().ToString(), pindexLast->nHeight);
1871  }
1872  if (vGetData.size() > 0) {
1873  connman.PushMessage(pfrom, NetMsgType::GETDATA, vGetData);
1874  }
1875  }
1876  }
1877  }
1878  }
1879 
1880  else if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
1881  {
1882  CBlock block;
1883  vRecv >> block;
1884 
1885  CInv inv(MSG_BLOCK, block.GetHash());
1886  LogPrint("net", "received block %s peer=%d\n", inv.hash.ToString(), pfrom->id);
1887 
1888  pfrom->AddInventoryKnown(inv);
1889 
1890  // Process all blocks from whitelisted peers, even if not requested,
1891  // unless we're still syncing with the network.
1892  // Such an unrequested block may still be processed, subject to the
1893  // conditions in AcceptBlock().
1894  bool forceProcessing = pfrom->fWhitelisted && !IsInitialBlockDownload();
1895  const uint256 hash(block.GetHash());
1896  {
1897  LOCK(cs_main);
1898  // Also always process if we requested the block explicitly, as we may
1899  // need it even though it is not a candidate for a new best tip.
1900  forceProcessing |= MarkBlockAsReceived(hash);
1901  // mapBlockSource is only used for sending reject messages and DoS scores,
1902  // so the race between here and cs_main in ProcessNewBlock is fine.
1903  mapBlockSource.emplace(hash, pfrom->GetId());
1904  }
1905  bool fNewBlock = false;
1906  ProcessNewBlock(chainparams, &block, forceProcessing, NULL, &fNewBlock);
1907  if (fNewBlock)
1908  pfrom->nLastBlockTime = GetTime();
1909  }
1910 
1911 
1912  else if (strCommand == NetMsgType::GETADDR)
1913  {
1914  // This asymmetric behavior for inbound and outbound connections was introduced
1915  // to prevent a fingerprinting attack: an attacker can send specific fake addresses
1916  // to users' AddrMan and later request them by sending getaddr messages.
1917  // Making nodes which are behind NAT and can only make outgoing connections ignore
1918  // the getaddr message mitigates the attack.
1919  if (!pfrom->fInbound) {
1920  LogPrint("net", "Ignoring \"getaddr\" from outbound connection. peer=%d\n", pfrom->id);
1921  return true;
1922  }
1923 
1924  pfrom->vAddrToSend.clear();
1925  vector<CAddress> vAddr = connman.GetAddresses();
1926  BOOST_FOREACH(const CAddress &addr, vAddr)
1927  pfrom->PushAddress(addr);
1928  }
1929 
1930 
1931  else if (strCommand == NetMsgType::MEMPOOL)
1932  {
1933  if (connman.OutboundTargetReached(false) && !pfrom->fWhitelisted)
1934  {
1935  LogPrint("net", "mempool request with bandwidth limit reached, disconnect peer=%d\n", pfrom->GetId());
1936  pfrom->fDisconnect = true;
1937  return true;
1938  }
1939  LOCK2(cs_main, pfrom->cs_filter);
1940 
1941  std::vector<uint256> vtxid;
1942  mempool.queryHashes(vtxid);
1943  vector<CInv> vInv;
1944  BOOST_FOREACH(uint256& hash, vtxid) {
1945  CInv inv(MSG_TX, hash);
1946  if (pfrom->pfilter) {
1947  CTransaction tx;
1948  bool fInMemPool = mempool.lookup(hash, tx);
1949  if (!fInMemPool) continue; // another thread removed since queryHashes, maybe...
1950  if (!pfrom->pfilter->IsRelevantAndUpdate(tx)) continue;
1951  }
1952  vInv.push_back(inv);
1953  if (vInv.size() == MAX_INV_SZ) {
1954  connman.PushMessage(pfrom, NetMsgType::INV, vInv);
1955  vInv.clear();
1956  }
1957  }
1958  if (vInv.size() > 0)
1959  connman.PushMessage(pfrom, NetMsgType::INV, vInv);
1960  }
1961 
1962 
1963  else if (strCommand == NetMsgType::PING)
1964  {
1965  if (pfrom->nVersion > BIP0031_VERSION)
1966  {
1967  uint64_t nonce = 0;
1968  vRecv >> nonce;
1969  // Echo the message back with the nonce. This allows for two useful features:
1970  //
1971  // 1) A remote node can quickly check if the connection is operational
1972  // 2) Remote nodes can measure the latency of the network thread. If this node
1973  // is overloaded it won't respond to pings quickly and the remote node can
1974  // avoid sending us more work, like chain download requests.
1975  //
1976  // The nonce stops the remote getting confused between different pings: without
1977  // it, if the remote node sends a ping once per second and this node takes 5
1978  // seconds to respond to each, the 5th ping the remote sends would appear to
1979  // return very quickly.
1980  connman.PushMessage(pfrom, NetMsgType::PONG, nonce);
1981  }
1982  }
1983 
1984 
1985  else if (strCommand == NetMsgType::PONG)
1986  {
1987  int64_t pingUsecEnd = nTimeReceived;
1988  uint64_t nonce = 0;
1989  size_t nAvail = vRecv.in_avail();
1990  bool bPingFinished = false;
1991  std::string sProblem;
1992 
1993  if (nAvail >= sizeof(nonce)) {
1994  vRecv >> nonce;
1995 
1996  // Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
1997  if (pfrom->nPingNonceSent != 0) {
1998  if (nonce == pfrom->nPingNonceSent) {
1999  // Matching pong received, this ping is no longer outstanding
2000  bPingFinished = true;
2001  int64_t pingUsecTime = pingUsecEnd - pfrom->nPingUsecStart;
2002  if (pingUsecTime > 0) {
2003  // Successful ping time measurement, replace previous
2004  pfrom->nPingUsecTime = pingUsecTime;
2005  pfrom->nMinPingUsecTime = std::min(pfrom->nMinPingUsecTime, pingUsecTime);
2006  } else {
2007  // This should never happen
2008  sProblem = "Timing mishap";
2009  }
2010  } else {
2011  // Nonce mismatches are normal when pings are overlapping
2012  sProblem = "Nonce mismatch";
2013  if (nonce == 0) {
2014  // This is most likely a bug in another implementation somewhere; cancel this ping
2015  bPingFinished = true;
2016  sProblem = "Nonce zero";
2017  }
2018  }
2019  } else {
2020  sProblem = "Unsolicited pong without ping";
2021  }
2022  } else {
2023  // This is most likely a bug in another implementation somewhere; cancel this ping
2024  bPingFinished = true;
2025  sProblem = "Short payload";
2026  }
2027 
2028  if (!(sProblem.empty())) {
2029  LogPrint("net", "pong peer=%d: %s, %x expected, %x received, %u bytes\n",
2030  pfrom->id,
2031  sProblem,
2032  pfrom->nPingNonceSent,
2033  nonce,
2034  nAvail);
2035  }
2036  if (bPingFinished) {
2037  pfrom->nPingNonceSent = 0;
2038  }
2039  }
2040 
2041 
2042  else if (fAlerts && strCommand == NetMsgType::ALERT)
2043  {
2044  CAlert alert;
2045  vRecv >> alert;
2046 
2047  uint256 alertHash = alert.GetHash();
2048  if (pfrom->setKnown.count(alertHash) == 0)
2049  {
2050  if (alert.ProcessAlert(chainparams.AlertKey()))
2051  {
2052  // Relay
2053  pfrom->setKnown.insert(alertHash);
2054  {
2055  connman.ForEachNode([&alert, &connman](CNode* pnode) {
2056  alert.RelayTo(pnode, connman);
2057  });
2058  }
2059  }
2060  else {
2061  // Small DoS penalty so peers that send us lots of
2062  // duplicate/expired/invalid-signature/whatever alerts
2063  // eventually get banned.
2064  // This isn't a Misbehaving(100) (immediate ban) because the
2065  // peer might be an older or different implementation with
2066  // a different signature key, etc.
2067  Misbehaving(pfrom->GetId(), 10);
2068  }
2069  }
2070  }
2071 
2072 
2073  else if (strCommand == NetMsgType::FILTERLOAD)
2074  {
2075  CBloomFilter filter;
2076  vRecv >> filter;
2077 
2078  if (!filter.IsWithinSizeConstraints())
2079  {
2080  // There is no excuse for sending a too-large filter
2081  LOCK(cs_main);
2082  Misbehaving(pfrom->GetId(), 100);
2083  }
2084  else
2085  {
2086  LOCK(pfrom->cs_filter);
2087  delete pfrom->pfilter;
2088  pfrom->pfilter = new CBloomFilter(filter);
2089  pfrom->pfilter->UpdateEmptyFull();
2090  }
2091  pfrom->fRelayTxes = true;
2092  }
2093 
2094 
2095  else if (strCommand == NetMsgType::FILTERADD)
2096  {
2097  vector<unsigned char> vData;
2098  vRecv >> vData;
2099 
2100  // Nodes must NEVER send a data item > 520 bytes (the max size for a script data object,
2101  // and thus, the maximum size any matched object can have) in a filteradd message
2102  if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE)
2103  {
2104  LOCK(cs_main);
2105  Misbehaving(pfrom->GetId(), 100);
2106  } else {
2107  LOCK(pfrom->cs_filter);
2108  if (pfrom->pfilter)
2109  pfrom->pfilter->insert(vData);
2110  else
2111  {
2112  LOCK(cs_main);
2113  Misbehaving(pfrom->GetId(), 100);
2114  }
2115  }
2116  }
2117 
2118 
2119  else if (strCommand == NetMsgType::FILTERCLEAR)
2120  {
2121  LOCK(pfrom->cs_filter);
2122  delete pfrom->pfilter;
2123  pfrom->pfilter = new CBloomFilter();
2124  pfrom->fRelayTxes = true;
2125  }
2126 
2127 
2128  else if (strCommand == NetMsgType::REJECT)
2129  {
2130  if (fDebug) {
2131  try {
2132  string strMsg; unsigned char ccode; string strReason;
2133  vRecv >> LIMITED_STRING(strMsg, CMessageHeader::COMMAND_SIZE) >> ccode >> LIMITED_STRING(strReason, MAX_REJECT_MESSAGE_LENGTH);
2134 
2135  ostringstream ss;
2136  ss << strMsg << " code " << itostr(ccode) << ": " << strReason;
2137 
2138  if (strMsg == NetMsgType::BLOCK || strMsg == NetMsgType::TX)
2139  {
2140  uint256 hash;
2141  vRecv >> hash;
2142  ss << ": hash " << hash.ToString();
2143  }
2144  LogPrint("net", "Reject %s\n", SanitizeString(ss.str()));
2145  } catch (const std::ios_base::failure&) {
2146  // Avoid feedback loops by preventing reject messages from triggering a new reject message.
2147  LogPrint("net", "Unparseable reject message received\n");
2148  }
2149  }
2150  }
2151  else
2152  {
2153  bool found = false;
2154  const std::vector<std::string> &allMessages = getAllNetMessageTypes();
2155  BOOST_FOREACH(const std::string msg, allMessages) {
2156  if(msg == strCommand) {
2157  found = true;
2158  break;
2159  }
2160  }
2161 
2162  if (found)
2163  {
2164  //probably one the extensions
2165  privateSendClient.ProcessMessage(pfrom, strCommand, vRecv, connman);
2166  privateSendServer.ProcessMessage(pfrom, strCommand, vRecv, connman);
2167  mnodeman.ProcessMessage(pfrom, strCommand, vRecv, connman);
2168  mnpayments.ProcessMessage(pfrom, strCommand, vRecv, connman);
2169  instantsend.ProcessMessage(pfrom, strCommand, vRecv, connman);
2170  sporkManager.ProcessSpork(pfrom, strCommand, vRecv, connman);
2171  masternodeSync.ProcessMessage(pfrom, strCommand, vRecv);
2172  governance.ProcessMessage(pfrom, strCommand, vRecv, connman);
2173  }
2174  else
2175  {
2176  // Ignore unknown commands for extensibility
2177  LogPrint("net", "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->id);
2178  }
2179  }
2180 
2181  return true;
2182 }
2183 
2184 bool ProcessMessages(CNode* pfrom, CConnman& connman, std::atomic<bool>& interruptMsgProc)
2185 {
2186  const CChainParams& chainparams = Params();
2187  //
2188  // Message format
2189  // (4) message start
2190  // (12) command
2191  // (4) size
2192  // (4) checksum
2193  // (x) data
2194  //
2195  bool fMoreWork = false;
2196 
2197  if (!pfrom->vRecvGetData.empty())
2198  ProcessGetData(pfrom, chainparams.GetConsensus(), connman, interruptMsgProc);
2199 
2200  if (pfrom->fDisconnect)
2201  return false;
2202 
2203  // this maintains the order of responses
2204  if (!pfrom->vRecvGetData.empty()) return true;
2205 
2206  // Don't bother if send buffer is too full to respond anyway
2207  if (pfrom->fPauseSend)
2208  return false;
2209 
2210  std::list<CNetMessage> msgs;
2211  {
2212  LOCK(pfrom->cs_vProcessMsg);
2213  if (pfrom->vProcessMsg.empty())
2214  return false;
2215  // Just take one message
2216  msgs.splice(msgs.begin(), pfrom->vProcessMsg, pfrom->vProcessMsg.begin());
2217  pfrom->nProcessQueueSize -= msgs.front().vRecv.size() + CMessageHeader::HEADER_SIZE;
2218  pfrom->fPauseRecv = pfrom->nProcessQueueSize > connman.GetReceiveFloodSize();
2219  fMoreWork = !pfrom->vProcessMsg.empty();
2220  }
2221  CNetMessage& msg(msgs.front());
2222 
2223  msg.SetVersion(pfrom->GetRecvVersion());
2224  // Scan for message start
2225  if (memcmp(msg.hdr.pchMessageStart, chainparams.MessageStart(), MESSAGE_START_SIZE) != 0) {
2226  LogPrintf("PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg.hdr.GetCommand()), pfrom->id);
2227  pfrom->fDisconnect = true;
2228  return false;
2229  }
2230 
2231  // Read header
2232  CMessageHeader& hdr = msg.hdr;
2233  if (!hdr.IsValid(chainparams.MessageStart()))
2234  {
2235  LogPrintf("PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(hdr.GetCommand()), pfrom->id);
2236  return fMoreWork;
2237  }
2238  string strCommand = hdr.GetCommand();
2239 
2240  // Message size
2241  unsigned int nMessageSize = hdr.nMessageSize;
2242 
2243  // Checksum
2244  CDataStream& vRecv = msg.vRecv;
2245  uint256 hash = Hash(vRecv.begin(), vRecv.begin() + nMessageSize);
2246  if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0)
2247  {
2248  LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s\n", __func__,
2249  SanitizeString(strCommand), nMessageSize,
2252  return fMoreWork;
2253  }
2254 
2255  // Process message
2256  bool fRet = false;
2257  try
2258  {
2259  fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime, connman, interruptMsgProc);
2260  if (interruptMsgProc)
2261  return false;
2262  if (!pfrom->vRecvGetData.empty())
2263  fMoreWork = true;
2264  }
2265  catch (const std::ios_base::failure& e)
2266  {
2267  connman.PushMessageWithVersion(pfrom, INIT_PROTO_VERSION, NetMsgType::REJECT, strCommand, REJECT_MALFORMED, string("error parsing message"));
2268  if (strstr(e.what(), "end of data"))
2269  {
2270  // Allow exceptions from under-length message on vRecv
2271  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());
2272  }
2273  else if (strstr(e.what(), "size too large"))
2274  {
2275  // Allow exceptions from over-long size
2276  LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
2277  }
2278  else
2279  {
2280  PrintExceptionContinue(&e, "ProcessMessages()");
2281  }
2282  }
2283  catch (const std::exception& e) {
2284  PrintExceptionContinue(&e, "ProcessMessages()");
2285  } catch (...) {
2286  PrintExceptionContinue(NULL, "ProcessMessages()");
2287  }
2288 
2289  if (!fRet)
2290  LogPrintf("%s(%s, %u bytes) FAILED peer=%d\n", __func__, SanitizeString(strCommand), nMessageSize, pfrom->id);
2291 
2292  return fMoreWork;
2293 }
2294 
2295 
2296 bool SendMessages(CNode* pto, CConnman& connman, std::atomic<bool>& interruptMsgProc)
2297 {
2298  const CChainParams chainParams = Params();
2299  const Consensus::Params& consensusParams = chainParams.GetConsensus();
2300  {
2301  // Don't send anything until the version handshake is complete
2302  if (!pto->fSuccessfullyConnected || pto->fDisconnect)
2303  return true;
2304 
2305  //
2306  // Message: ping
2307  //
2308  bool pingSend = false;
2309  if (pto->fPingQueued) {
2310  // RPC ping request by user
2311  pingSend = true;
2312  }
2313  if (pto->nPingNonceSent == 0 && pto->nPingUsecStart + PING_INTERVAL * 1000000 < GetTimeMicros()) {
2314  // Ping automatically sent as a latency probe & keepalive.
2315  pingSend = true;
2316  }
2317  if (pingSend) {
2318  uint64_t nonce = 0;
2319  while (nonce == 0) {
2320  GetRandBytes((unsigned char*)&nonce, sizeof(nonce));
2321  }
2322  pto->fPingQueued = false;
2323  pto->nPingUsecStart = GetTimeMicros();
2324  if (pto->nVersion > BIP0031_VERSION) {
2325  pto->nPingNonceSent = nonce;
2326  connman.PushMessage(pto, NetMsgType::PING, nonce);
2327  } else {
2328  // Peer is too old to support ping command with nonce, pong will never arrive.
2329  pto->nPingNonceSent = 0;
2330  connman.PushMessage(pto, NetMsgType::PING);
2331  }
2332  }
2333 
2334  TRY_LOCK(cs_main, lockMain); // Acquire cs_main for IsInitialBlockDownload() and CNodeState()
2335  if (!lockMain)
2336  return true;
2337 
2338  // Address refresh broadcast
2339  int64_t nNow = GetTimeMicros();
2340  if (!IsInitialBlockDownload() && pto->nNextLocalAddrSend < nNow) {
2341  AdvertiseLocal(pto);
2343  }
2344 
2345  //
2346  // Message: addr
2347  //
2348  if (pto->nNextAddrSend < nNow) {
2350  vector<CAddress> vAddr;
2351  vAddr.reserve(pto->vAddrToSend.size());
2352  BOOST_FOREACH(const CAddress& addr, pto->vAddrToSend)
2353  {
2354  if (!pto->addrKnown.contains(addr.GetKey()))
2355  {
2356  pto->addrKnown.insert(addr.GetKey());
2357  vAddr.push_back(addr);
2358  // receiver rejects addr messages larger than 1000
2359  if (vAddr.size() >= 1000)
2360  {
2361  connman.PushMessage(pto, NetMsgType::ADDR, vAddr);
2362  vAddr.clear();
2363  }
2364  }
2365  }
2366  pto->vAddrToSend.clear();
2367  if (!vAddr.empty())
2368  connman.PushMessage(pto, NetMsgType::ADDR, vAddr);
2369  }
2370 
2371  CNodeState &state = *State(pto->GetId());
2372  if (state.fShouldBan) {
2373  if (pto->fWhitelisted)
2374  LogPrintf("Warning: not punishing whitelisted peer %s!\n", pto->addr.ToString());
2375  else {
2376  pto->fDisconnect = true;
2377  if (pto->addr.IsLocal())
2378  LogPrintf("Warning: not banning local peer %s!\n", pto->addr.ToString());
2379  else
2380  {
2381  connman.Ban(pto->addr, BanReasonNodeMisbehaving);
2382  }
2383  }
2384  state.fShouldBan = false;
2385  }
2386 
2387  BOOST_FOREACH(const CBlockReject& reject, state.rejects)
2388  connman.PushMessage(pto, NetMsgType::REJECT, (string)NetMsgType::BLOCK, reject.chRejectCode, reject.strRejectReason, reject.hashBlock);
2389  state.rejects.clear();
2390 
2391  // Start block sync
2392  if (pindexBestHeader == NULL)
2394  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.
2395  if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex) {
2396  // Only actively request headers from a single peer, unless we're close to end of initial download.
2397  if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 6 * 60 * 60) { // NOTE: was "close to today" and 24h in Bitcoin
2398  state.fSyncStarted = true;
2400  nSyncStarted++;
2401  const CBlockIndex *pindexStart = pindexBestHeader;
2402  /* If possible, start at the block preceding the currently
2403  best known header. This ensures that we always get a
2404  non-empty list of headers back as long as the peer
2405  is up-to-date. With a non-empty response, we can initialise
2406  the peer's known best block. This wouldn't be possible
2407  if we requested starting at pindexBestHeader and
2408  got back an empty response. */
2409  if (pindexStart->pprev)
2410  pindexStart = pindexStart->pprev;
2411  LogPrint("net", "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->nHeight, pto->id, pto->nStartingHeight);
2412  connman.PushMessage(pto, NetMsgType::GETHEADERS, chainActive.GetLocator(pindexStart), uint256());
2413  }
2414  }
2415 
2416  if (chainParams.DelayGetHeadersTime() != 0 && pindexBestHeader->GetBlockTime() >= GetAdjustedTime() - chainParams.DelayGetHeadersTime()) {
2417  // Headers chain has catched up enough so we can send out GETHEADER messages which were initially meant to
2418  // be sent directly after INV was received
2419  LOCK(pto->cs_inventory);
2420  BOOST_FOREACH(const uint256 &hash, pto->vBlockHashesFromINV) {
2421  LogPrint("net", "process delayed getheaders (%d) to peer=%d\n", pindexBestHeader->nHeight, pto->id);
2423  }
2424  pto->vBlockHashesFromINV.clear();
2425  }
2426 
2427  // Resend wallet transactions that haven't gotten in a block yet
2428  // Except during reindex, importing and IBD, when old wallet
2429  // transactions become unconfirmed and spams other nodes.
2431  {
2433  }
2434 
2435  //
2436  // Try sending block announcements via headers
2437  //
2438  {
2439  // If we have less than MAX_BLOCKS_TO_ANNOUNCE in our
2440  // list of block hashes we're relaying, and our peer wants
2441  // headers announcements, then find the first header
2442  // not yet known to our peer but would connect, and send.
2443  // If no header would connect, or if we have too many
2444  // blocks, or if the peer doesn't want headers, just
2445  // add all to the inv queue.
2446  LOCK(pto->cs_inventory);
2447  vector<CBlock> vHeaders;
2448  bool fRevertToInv = (!state.fPreferHeaders || pto->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE);
2449  CBlockIndex *pBestIndex = NULL; // last header queued for delivery
2450  ProcessBlockAvailability(pto->id); // ensure pindexBestKnownBlock is up-to-date
2451 
2452  if (!fRevertToInv) {
2453  bool fFoundStartingHeader = false;
2454  // Try to find first header that our peer doesn't have, and
2455  // then send all headers past that one. If we come across any
2456  // headers that aren't on chainActive, give up.
2457  BOOST_FOREACH(const uint256 &hash, pto->vBlockHashesToAnnounce) {
2458  BlockMap::iterator mi = mapBlockIndex.find(hash);
2459  assert(mi != mapBlockIndex.end());
2460  CBlockIndex *pindex = mi->second;
2461  if (chainActive[pindex->nHeight] != pindex) {
2462  // Bail out if we reorged away from this block
2463  fRevertToInv = true;
2464  break;
2465  }
2466  if (pBestIndex != NULL && pindex->pprev != pBestIndex) {
2467  // This means that the list of blocks to announce don't
2468  // connect to each other.
2469  // This shouldn't really be possible to hit during
2470  // regular operation (because reorgs should take us to
2471  // a chain that has some block not on the prior chain,
2472  // which should be caught by the prior check), but one
2473  // way this could happen is by using invalidateblock /
2474  // reconsiderblock repeatedly on the tip, causing it to
2475  // be added multiple times to vBlockHashesToAnnounce.
2476  // Robustly deal with this rare situation by reverting
2477  // to an inv.
2478  fRevertToInv = true;
2479  break;
2480  }
2481  pBestIndex = pindex;
2482  if (fFoundStartingHeader) {
2483  // add this to the headers message
2484  vHeaders.push_back(pindex->GetBlockHeader());
2485  } else if (PeerHasHeader(&state, pindex)) {
2486  continue; // keep looking for the first new block
2487  } else if (pindex->pprev == NULL || PeerHasHeader(&state, pindex->pprev)) {
2488  // Peer doesn't have this header but they do have the prior one.
2489  // Start sending headers.
2490  fFoundStartingHeader = true;
2491  vHeaders.push_back(pindex->GetBlockHeader());
2492  } else {
2493  // Peer doesn't have this header or the prior one -- nothing will
2494  // connect, so bail out.
2495  fRevertToInv = true;
2496  break;
2497  }
2498  }
2499  }
2500  if (fRevertToInv) {
2501  // If falling back to using an inv, just try to inv the tip.
2502  // The last entry in vBlockHashesToAnnounce was our tip at some point
2503  // in the past.
2504  if (!pto->vBlockHashesToAnnounce.empty()) {
2505  const uint256 &hashToAnnounce = pto->vBlockHashesToAnnounce.back();
2506  BlockMap::iterator mi = mapBlockIndex.find(hashToAnnounce);
2507  assert(mi != mapBlockIndex.end());
2508  CBlockIndex *pindex = mi->second;
2509 
2510  // Warn if we're announcing a block that is not on the main chain.
2511  // This should be very rare and could be optimized out.
2512  // Just log for now.
2513  if (chainActive[pindex->nHeight] != pindex) {
2514  LogPrint("net", "Announcing block %s not on main chain (tip=%s)\n",
2515  hashToAnnounce.ToString(), chainActive.Tip()->GetBlockHash().ToString());
2516  }
2517 
2518  // If the peer announced this block to us, don't inv it back.
2519  // (Since block announcements may not be via inv's, we can't solely rely on
2520  // setInventoryKnown to track this.)
2521  if (!PeerHasHeader(&state, pindex)) {
2522  pto->PushInventory(CInv(MSG_BLOCK, hashToAnnounce));
2523  LogPrint("net", "%s: sending inv peer=%d hash=%s\n", __func__,
2524  pto->id, hashToAnnounce.ToString());
2525  }
2526  }
2527  } else if (!vHeaders.empty()) {
2528  if (vHeaders.size() > 1) {
2529  LogPrint("net", "%s: %u headers, range (%s, %s), to peer=%d\n", __func__,
2530  vHeaders.size(),
2531  vHeaders.front().GetHash().ToString(),
2532  vHeaders.back().GetHash().ToString(), pto->id);
2533  } else {
2534  LogPrint("net", "%s: sending header %s to peer=%d\n", __func__,
2535  vHeaders.front().GetHash().ToString(), pto->id);
2536  }
2537  connman.PushMessage(pto, NetMsgType::HEADERS, vHeaders);
2538  state.pindexBestHeaderSent = pBestIndex;
2539  }
2540  pto->vBlockHashesToAnnounce.clear();
2541  }
2542 
2543  //
2544  // Message: inventory
2545  //
2546  vector<CInv> vInv;
2547  vector<CInv> vInvWait;
2548  {
2549  bool fSendTrickle = pto->fWhitelisted;
2550  if (pto->nNextInvSend < nNow) {
2551  fSendTrickle = true;
2553  }
2554  LOCK(pto->cs_inventory);
2555  vInv.reserve(std::min<size_t>(1000, pto->vInventoryToSend.size()));
2556  vInvWait.reserve(pto->vInventoryToSend.size());
2557  BOOST_FOREACH(const CInv& inv, pto->vInventoryToSend)
2558  {
2559  if (inv.type == MSG_TX && pto->filterInventoryKnown.contains(inv.hash))
2560  continue;
2561 
2562  // trickle out tx inv to protect privacy
2563  if (inv.type == MSG_TX && !fSendTrickle)
2564  {
2565  // 1/4 of tx invs blast to all immediately
2566  static uint256 hashSalt;
2567  if (hashSalt.IsNull())
2568  hashSalt = GetRandHash();
2569  uint256 hashRand = ArithToUint256(UintToArith256(inv.hash) ^ UintToArith256(hashSalt));
2570  hashRand = Hash(BEGIN(hashRand), END(hashRand));
2571  bool fTrickleWait = ((UintToArith256(hashRand) & 3) != 0);
2572 
2573  if (fTrickleWait)
2574  {
2575  LogPrint("net", "SendMessages -- queued inv(vInvWait): %s index=%d peer=%d\n", inv.ToString(), vInvWait.size(), pto->id);
2576  vInvWait.push_back(inv);
2577  continue;
2578  }
2579  }
2580 
2581  pto->filterInventoryKnown.insert(inv.hash);
2582 
2583  LogPrint("net", "SendMessages -- queued inv: %s index=%d peer=%d\n", inv.ToString(), vInv.size(), pto->id);
2584  vInv.push_back(inv);
2585  if (vInv.size() >= 1000)
2586  {
2587  LogPrint("net", "SendMessages -- pushing inv's: count=%d peer=%d\n", vInv.size(), pto->id);
2588  connman.PushMessage(pto, NetMsgType::INV, vInv);
2589  vInv.clear();
2590  }
2591  }
2592  pto->vInventoryToSend = vInvWait;
2593  }
2594  if (!vInv.empty()) {
2595  LogPrint("net", "SendMessages -- pushing tailing inv's: count=%d peer=%d\n", vInv.size(), pto->id);
2596  connman.PushMessage(pto, NetMsgType::INV, vInv);
2597  }
2598 
2599  // Detect whether we're stalling
2600  nNow = GetTimeMicros();
2601  if (!pto->fDisconnect && state.nStallingSince && state.nStallingSince < nNow - 1000000 * BLOCK_STALLING_TIMEOUT) {
2602  // Stalling only triggers when the block download window cannot move. During normal steady state,
2603  // the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
2604  // should only happen during initial block download.
2605  LogPrintf("Peer=%d is stalling block download, disconnecting\n", pto->id);
2606  pto->fDisconnect = true;
2607  }
2608  // In case there is a block that has been in flight from this peer for 2 + 0.5 * N times the block interval
2609  // (with N the number of peers from which we're downloading validated blocks), disconnect due to timeout.
2610  // We compensate for other peers to prevent killing off peers due to our own downstream link
2611  // being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
2612  // to unreasonably increase our timeout.
2613  if (!pto->fDisconnect && state.vBlocksInFlight.size() > 0) {
2614  QueuedBlock &queuedBlock = state.vBlocksInFlight.front();
2615  int nOtherPeersWithValidatedDownloads = nPeersWithValidatedDownloads - (state.nBlocksInFlightValidHeaders > 0);
2616  if (nNow > state.nDownloadingSince + consensusParams.nPowTargetSpacing * (BLOCK_DOWNLOAD_TIMEOUT_BASE + BLOCK_DOWNLOAD_TIMEOUT_PER_PEER * nOtherPeersWithValidatedDownloads)) {
2617  LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", queuedBlock.hash.ToString(), pto->id);
2618  pto->fDisconnect = true;
2619  }
2620  }
2621  // Check for headers sync timeouts
2622  if (state.fSyncStarted && state.nHeadersSyncTimeout < std::numeric_limits<int64_t>::max()) {
2623  // Detect whether this is a stalling initial-headers-sync peer
2624  if (pindexBestHeader->GetBlockTime() <= GetAdjustedTime() - 6*60*60) { // was 24*60*60 in bitcoin
2625  if (nNow > state.nHeadersSyncTimeout && nSyncStarted == 1 && (nPreferredDownload - state.fPreferredDownload >= 1)) {
2626  // Disconnect a (non-whitelisted) peer if it is our only sync peer,
2627  // and we have others we could be using instead.
2628  // Note: If all our peers are inbound, then we won't
2629  // disconnect our sync peer for stalling; we have bigger
2630  // problems if we can't get any outbound peers.
2631  if (!pto->fWhitelisted) {
2632  LogPrintf("Timeout downloading headers from peer=%d, disconnecting\n", pto->GetId());
2633  pto->fDisconnect = true;
2634  return true;
2635  } else {
2636  LogPrintf("Timeout downloading headers from whitelisted peer=%d, not disconnecting\n", pto->GetId());
2637  // Reset the headers sync state so that we have a
2638  // chance to try downloading from a different peer.
2639  // Note: this will also result in at least one more
2640  // getheaders message to be sent to
2641  // this peer (eventually).
2642  state.fSyncStarted = false;
2643  nSyncStarted--;
2644  state.nHeadersSyncTimeout = 0;
2645  }
2646  }
2647  } else {
2648  // After we've caught up once, reset the timeout so we can't trigger
2649  // disconnect later.
2650  state.nHeadersSyncTimeout = std::numeric_limits<int64_t>::max();
2651  }
2652  }
2653 
2654 
2655  //
2656  // Message: getdata (blocks)
2657  //
2658  vector<CInv> vGetData;
2659  if (!pto->fDisconnect && !pto->fClient && (fFetch || !IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
2660  vector<CBlockIndex*> vToDownload;
2661  NodeId staller = -1;
2662  FindNextBlocksToDownload(pto->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller, consensusParams);
2663  BOOST_FOREACH(CBlockIndex *pindex, vToDownload) {
2664  vGetData.push_back(CInv(MSG_BLOCK, pindex->GetBlockHash()));
2665  MarkBlockAsInFlight(pto->GetId(), pindex->GetBlockHash(), consensusParams, pindex);
2666  LogPrint("net", "Requesting block %s (%d) peer=%d\n", pindex->GetBlockHash().ToString(),
2667  pindex->nHeight, pto->id);
2668  }
2669  if (state.nBlocksInFlight == 0 && staller != -1) {
2670  if (State(staller)->nStallingSince == 0) {
2671  State(staller)->nStallingSince = nNow;
2672  LogPrint("net", "Stall started peer=%d\n", staller);
2673  }
2674  }
2675  }
2676 
2677  //
2678  // Message: getdata (non-blocks)
2679  //
2680  while (!pto->fDisconnect && !pto->mapAskFor.empty() && (*pto->mapAskFor.begin()).first <= nNow)
2681  {
2682  const CInv& inv = (*pto->mapAskFor.begin()).second;
2683  if (!AlreadyHave(inv))
2684  {
2685  LogPrint("net", "SendMessages -- GETDATA -- requesting inv = %s peer=%d\n", inv.ToString(), pto->id);
2686  vGetData.push_back(inv);
2687  if (vGetData.size() >= 1000)
2688  {
2689  connman.PushMessage(pto, NetMsgType::GETDATA, vGetData);
2690  LogPrint("net", "SendMessages -- GETDATA -- pushed size = %lu peer=%d\n", vGetData.size(), pto->id);
2691  vGetData.clear();
2692  }
2693  } else {
2694  //If we're not going to ask, don't expect a response.
2695  LogPrint("net", "SendMessages -- GETDATA -- already have inv = %s peer=%d\n", inv.ToString(), pto->id);
2696  pto->setAskFor.erase(inv.hash);
2697  }
2698  pto->mapAskFor.erase(pto->mapAskFor.begin());
2699  }
2700  if (!vGetData.empty()) {
2701  connman.PushMessage(pto, NetMsgType::GETDATA, vGetData);
2702  LogPrint("net", "SendMessages -- GETDATA -- pushed size = %lu peer=%d\n", vGetData.size(), pto->id);
2703  }
2704 
2705  }
2706  return true;
2707 }
2708 
2710 {
2711 public:
2714  // orphan transactions
2715  mapOrphanTransactions.clear();
2716  mapOrphanTransactionsByPrev.clear();
2717  }
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: chain.h:125
static void EraseOrphanTx(uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void ProcessSpork(CNode *pfrom, std::string &strCommand, CDataStream &vRecv, CConnman &connman)
Definition: spork.cpp:20
bool GetBlockHash(uint256 &hashRet, int nBlockHeight)
unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void MarkAddressGood(const CAddress &addr)
Definition: net.cpp:2385
int GetMyStartingHeight() const
Definition: net.h:798
bool IsLocal() const
Definition: netaddress.cpp:166
const char * MNPING
Definition: protocol.cpp:53
CMasternodeMan mnodeman
CMasternodeSync masternodeSync
CRollingBloomFilter addrKnown
Definition: net.h:733
const char * ADDR
Definition: protocol.cpp:18
const char * VERACK
Definition: protocol.cpp:17
bool fPruneMode
Definition: validation.cpp:77
CPrivateSendServer privateSendServer
void Ban(const CNetAddr &netAddr, const BanReason &reason, int64_t bantimeoffset=0, bool sinceUnixEpoch=false)
Definition: net.cpp:517
uint256 GetRandHash()
Definition: random.cpp:114
std::set< uint256 > setAskFor
Definition: net.h:743
const std::vector< unsigned char > & AlertKey() const
Definition: chainparams.h:57
unsigned long size()
Definition: txmempool.h:551
std::vector< CAddress > vAddrToSend
Definition: net.h:732
CRollingBloomFilter filterInventoryKnown
Definition: net.h:740
ServiceFlags
Definition: protocol.h:253
void insert(const std::vector< unsigned char > &vKey)
Definition: bloom.cpp:232
boost::signals2::signal< void(NodeId, bool &)> FinalizeNode
Definition: net.h:536
const char * DSTX
Definition: protocol.cpp:60
std::string strSubVer
Definition: net.h:697
std::atomic_bool fPauseSend
Definition: net.h:721
bool fFeeler
Definition: net.h:699
void SetNull()
Definition: uint256.h:41
void RegisterNodeSignals(CNodeSignals &nodeSignals)
void RelayTransaction(const CTransaction &tx)
Definition: net.cpp:2477
static const unsigned int AVG_ADDRESS_BROADCAST_INTERVAL
Definition: validation.h:107
static const int32_t CURRENT_VERSION
Definition: transaction.h:219
boost::signals2::signal< void(CNode *, CConnman &)> InitializeNode
Definition: net.h:535
void PushMessageWithVersion(CNode *pnode, int nVersion, const std::string &sCommand, Args &&... args)
Definition: net.h:193
std::vector< uint256 > vBlockHashesFromINV
Definition: net.h:751
#define TRY_LOCK(cs, name)
Definition: sync.h:170
bool IsMnbRecoveryRequested(const uint256 &hash)
std::vector< uint256 > vBlockHashesToAnnounce
Definition: net.h:748
void queryHashes(std::vector< uint256 > &vtxid)
Definition: txmempool.cpp:859
std::string GetRejectReason() const
Definition: validation.h:81
static const unsigned int AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL
Definition: validation.h:105
const char * MASTERNODEPAYMENTVOTE
Definition: protocol.cpp:43
boost::signals2::signal< bool(CNode *, CConnman &, std::atomic< bool > &), CombinerAll > SendMessages
Definition: net.h:534
std::vector< unsigned char > GetKey() const
Definition: netaddress.cpp:544
bool GetTxLockVote(const uint256 &hash, CTxLockVote &txLockVoteRet)
Definition: instantx.cpp:747
#define MESSAGE_START_SIZE
Definition: protocol.h:21
static const ServiceFlags REQUIRED_SERVICES
Definition: net.h:89
bool fDisconnect
Definition: net.h:705
bool fDebug
Definition: util.cpp:124
uint64_t ReadCompactSize(Stream &is)
Definition: serialize.h:288
void AcceptLockRequest(const CTxLockRequest &txLockRequest)
Definition: instantx.cpp:718
#define strprintf
Definition: tinyformat.h:1011
static bool AlreadyHave(const CInv &inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats)
uint64_t GetHash(const uint256 &salt) const
Definition: uint256.cpp:126
int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, const CBlockIndex &from, const CBlockIndex &tip, const Consensus::Params &params)
Definition: pow.cpp:272
bool HaveObjectForHash(uint256 nHash)
Definition: governance.cpp:44
static const int64_t BLOCK_DOWNLOAD_TIMEOUT_BASE
Definition: validation.h:112
void RandAddSeedPerfmon()
Definition: random.cpp:46
bool empty() const
Definition: streams.h:123
uint256 hash
Definition: protocol.h:339
static constexpr int64_t HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:55
static const CAmount COIN
Definition: amount.h:16
const char * NOTFOUND
Definition: protocol.cpp:32
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:253
bool Get(const COutPoint &outpoint, CMasternode &masternodeRet)
Versions of Find that are safe to use from outside the class.
static const unsigned int MIN_BLOCKS_TO_KEEP
Definition: validation.h:188
const char * BLOCK
Definition: protocol.cpp:26
static const unsigned int MAX_REJECT_MESSAGE_LENGTH
Definition: validation.h:103
CCriticalSection cs_main
Definition: validation.cpp:62
std::atomic_bool fPauseRecv
Definition: net.h:720
bool fReindex
Definition: validation.cpp:71
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
const char * INV
Definition: protocol.cpp:19
bool contains(const std::vector< unsigned char > &vKey) const
Definition: bloom.cpp:252
static int64_t nTimeOffset
Definition: timedata.cpp:18
CAddress addr
Definition: net.h:688
static const int BIP0031_VERSION
BIP 0031, pong message, is enabled for all versions AFTER this one.
Definition: version.h:33
bool IsNull() const
Definition: block.h:157
const char * MEMPOOL
Definition: protocol.cpp:28
int64_t nPingUsecTime
Definition: net.h:763
Definition: net.h:108
int64_t nNextLocalAddrSend
Definition: net.h:737
void PrintExceptionContinue(const std::exception *pex, const char *pszThread)
Definition: util.cpp:509
const char * TX
Definition: protocol.cpp:24
NodeId id
Definition: net.h:718
int in_avail()
Definition: streams.h:218
CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
Definition: net.cpp:155
static const int SENDHEADERS_VERSION
"sendheaders" command and announcing blocks with headers starts with this version ...
Definition: version.h:42
int64_t GetTimeMicros()
Definition: utiltime.cpp:42
bool fNetworkNode
Definition: net.h:703
std::map< uint256, CMasternodePing > mapSeenMasternodePing
Definition: masternodeman.h:87
void ProcessMessage(CNode *pfrom, std::string &strCommand, CDataStream &vRecv, CConnman &connman)
bool IsKnownType() const
Definition: protocol.cpp:254
const char * HEADERS
Definition: protocol.cpp:25
bool AcceptToMemoryPool(CTxMemPool &pool, CValidationState &state, const CTransaction &tx, bool fLimitFree, bool *pfMissingInputs, bool fOverrideMempoolLimit, bool fRejectAbsurdFee, bool fDryRun)
static void RelayAddress(const CAddress &addr, bool fReachable, CConnman &connman)
int GetRecvVersion()
Definition: net.h:815
CPubKey pubKeyMasternode
Definition: masternode.h:117
std::vector< int > vHeightInFlight
bool AlreadyHave(const uint256 &hash)
Definition: instantx.cpp:710
bool SerializeVoteForHash(uint256 nHash, CDataStream &ss)
Definition: governance.cpp:83
bool HaveCoins(const uint256 &txid) const
Definition: coins.cpp:138
bool SeenLocal(const CService &addr)
Definition: net.cpp:265
bool IsValid(const MessageStartChars &messageStart) const
Definition: protocol.cpp:172
std::map< uint256, std::pair< int64_t, CMasternodeBroadcast > > mapSeenMasternodeBroadcast
Definition: masternodeman.h:85
static bool ProcessMessage(CNode *pfrom, string strCommand, CDataStream &vRecv, int64_t nTimeReceived, CConnman &connman, std::atomic< bool > &interruptMsgProc)
std::string ToString(bool fUseGetnameinfo=true) const
Definition: netaddress.cpp:568
void ProcessMessage(CNode *pfrom, std::string &strCommand, CDataStream &vRecv, CConnman &connman)
Definition: governance.cpp:101
size_t GetAddressCount() const
Definition: net.cpp:2375
const char * FILTERLOAD
Definition: protocol.cpp:33
ServiceFlags nServicesExpected
Definition: net.h:667
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
Definition: script.h:22
class CNetProcessingCleanup instance_of_cnetprocessingcleanup
void AddNewAddresses(const std::vector< CAddress > &vAddr, const CAddress &addrFrom, int64_t nTimePenalty=0)
Definition: net.cpp:2395
void EraseOrphansFor(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
const char * MNVERIFY
Definition: protocol.cpp:67
uint64_t GetLocalNonce() const
Definition: net.h:794
const char * FILTERADD
Definition: protocol.cpp:34
CCriticalSection cs_mapMasternodeBlocks
CBloomFilter * pfilter
Definition: net.h:716
std::list< CNetMessage > vProcessMsg
Definition: net.h:676
Definition: alert.h:77
unsigned char * begin()
Definition: uint256.h:55
static const unsigned char REJECT_OBSOLETE
Definition: validation.h:14
boost::signals2::signal< void(const uint256 &)> Inventory
static void AddDSTX(const CDarksendBroadcastTx &dstx)
bool IsNull() const
Definition: uint256.h:33
bool CheckSignature(const CPubKey &pubKeyMasternode)
uint32_t nMessageSize
Definition: protocol.h:64
bool fClient
Definition: net.h:701
static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS
Definition: validation.h:64
const char * GETBLOCKS
Definition: protocol.cpp:22
const char * REJECT
Definition: protocol.cpp:36
unsigned int GetReceiveFloodSize() const
Definition: net.cpp:2662
int64_t nTimeOffset
Definition: net.h:686
boost::signals2::signal< void(int64_t nBestBlockTime, CConnman *connman)> Broadcast
arith_uint256 UintToArith256(const uint256 &a)
void insert(const std::vector< unsigned char > &vKey)
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:23
std::vector< CTransaction > vtx
Definition: block.h:77
void reserve(size_type n)
Definition: streams.h:125
bool lookup(uint256 hash, CTransaction &result) const
Definition: txmempool.cpp:869
const char * TXLOCKVOTE
Definition: protocol.cpp:40
static const unsigned int REJECT_INTERNAL
Definition: validation.h:860
bool ProcessNewBlock(const CChainParams &chainparams, const CBlock *pblock, bool fForceProcessing, const CDiskBlockPos *dbp, bool *fNewBlock)
std::atomic_bool fSuccessfullyConnected
Definition: net.h:704
bool Contains(const CBlockIndex *pindex) const
Definition: chain.h:384
std::map< CInv, CDataStream > mapRelay
Definition: net.cpp:85
void RejectLockRequest(const CTxLockRequest &txLockRequest)
Definition: instantx.cpp:724
std::deque< CInv > vRecvGetData
Definition: net.h:679
bool exists(uint256 hash) const
Definition: txmempool.h:563
bool fPingQueued
Definition: net.h:767
bool ConfirmInventoryRequest(const CInv &inv)
Definition: governance.cpp:678
const char * SENDHEADERS
Definition: protocol.cpp:37
NodeId GetId() const
Definition: net.h:790
PeerLogicValidation(CConnman *connmanIn)
const char * GetCommand() const
Definition: protocol.cpp:259
#define BEGIN(a)
CCoinsViewCache * pcoinsTip
Definition: validation.cpp:187
const char * ALERT
Definition: protocol.cpp:31
#define LOCK2(cs1, cs2)
Definition: sync.h:169
void AddAddressKnown(const CAddress &addr)
Definition: net.h:838
std::string GetCommand() const
Definition: protocol.cpp:167
static const int64_t BLOCK_DOWNLOAD_TIMEOUT_PER_PEER
Definition: validation.h:114
bool fRelayTxes
Definition: net.cpp:78
virtual void BlockChecked(const CBlock &block, const CValidationState &state)
void UpdateEmptyFull()
Checks for empty and full filters to avoid wasting cpu.
Definition: bloom.cpp:206
void Misbehaving(NodeId pnode, int howmuch)
static const unsigned int AVG_INVENTORY_BROADCAST_INTERVAL
Definition: validation.h:110
bool GetBoolArg(const std::string &strArg, bool fDefault)
Definition: util.cpp:455
#define LogPrintf(...)
Definition: util.h:98
static const bool DEFAULT_WHITELISTRELAY
Definition: validation.h:53
void AskFor(const CInv &inv)
Definition: net.cpp:2745
void SetRecvVersion(int nVersionIn)
Definition: net.h:811
bool fAllowMixingTx
Definition: masternode.h:163
COutPoint prevout
Definition: transaction.h:61
bool fRelayTxes
Definition: net.h:710
void ForEachNodeThen(const Condition &cond, Callable &&pre, CallableAfter &&post)
Definition: net.h:271
bool CheckIncomingNonce(uint64_t nonce)
Definition: net.cpp:337
CService addrLocal
Definition: net.h:690
Definition: net.h:661
CMasternodePayments mnpayments
bool DisallowMixing(const COutPoint &outpoint)
static int LogPrint(const char *category, const char *format)
Definition: util.h:126
CBlockIndex * pindexBestHeader
Definition: validation.cpp:66
const_iterator begin() const
Definition: streams.h:118
#define LOCK(cs)
Definition: sync.h:168
const char * name
Definition: rest.cpp:37
static const int MAX_BLOCKS_IN_TRANSIT_PER_PEER
Definition: validation.h:87
void UnregisterNodeSignals(CNodeSignals &nodeSignals)
bool IsPeerAddrLocalGood(CNode *pnode)
Definition: net.cpp:176
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:16
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:1000
static const unsigned int MAX_HEADERS_RESULTS
Definition: validation.h:92
static const unsigned char REJECT_NONSTANDARD
Definition: validation.h:16
std::vector< uint256 > GetVoteHashes()
bool fInbound
Definition: net.h:702
uint64_t nPingNonceSent
Definition: net.h:759
size_t nProcessQueueSize
Definition: net.h:677
static bool error(const char *format)
Definition: util.h:131
bool IsProxy(const CNetAddr &addr)
Definition: netbase.cpp:566
int Height() const
Definition: chain.h:397
void check(const CCoinsViewCache *pcoins) const
Definition: txmempool.cpp:751
std::string cleanSubVer
Definition: net.h:697
bool IsInitialBlockDownload()
bool OutboundTargetReached(bool historicalBlockServingLimit)
check if the outbound target is reached
Definition: net.cpp:2606
void ProcessMessage(CNode *pfrom, std::string &strCommand, CDataStream &vRecv, CConnman &connman)
Definition: instantx.cpp:43
const std::vector< std::string > & getAllNetMessageTypes()
Definition: protocol.cpp:275
int64_t nPowTargetSpacing
Definition: params.h:80
CInstantSend instantsend
Definition: instantx.cpp:30
void PushInventory(const CInv &inv)
Definition: net.h:866
void ProcessMessage(CNode *pfrom, std::string &strCommand, CDataStream &vRecv, CConnman &connman)
bool SerializeObjectForHash(uint256 nHash, CDataStream &ss)
Definition: governance.cpp:49
const char * GETDATA
Definition: protocol.cpp:20
std::map< uint256, CMasternodeVerification > mapSeenMasternodeVerification
Definition: masternodeman.h:89
#define END(a)
map< uint256, CAlert > mapAlerts
Definition: alert.cpp:28
CMainSignals & GetMainSignals()
void SetBestHeight(int height)
Definition: net.cpp:2652
CCriticalSection cs_mapAlerts
Definition: alert.cpp:29
bool fGetAddr
Definition: net.h:734
uint256 Hash(const T1 pbegin, const T1 pend)
Definition: hash.h:123
const char * FILTERCLEAR
Definition: protocol.cpp:35
uint64_t GetHash() const
Definition: netaddress.cpp:369
CChain chainActive
Definition: validation.cpp:65
map< uint256, COrphanTx > mapOrphanTransactions GUARDED_BY(cs_main)
std::string ToString() const
Definition: uint256.cpp:65
const char * PONG
Definition: protocol.cpp:30
ServiceFlags GetLocalServices() const
Definition: net.h:897
std::string strSubVersion
Definition: net.cpp:83
void SetServices(const CService &addr, ServiceFlags nServices)
Definition: net.cpp:2380
ServiceFlags nServices
Definition: protocol.h:307
static const unsigned char REJECT_DUPLICATE
Definition: validation.h:15
const char * MNGOVERNANCEOBJECT
Definition: protocol.cpp:65
unsigned int GetRejectCode() const
Definition: validation.h:80
static constexpr int64_t HEADERS_DOWNLOAD_TIMEOUT_BASE
const char * VERSION
Definition: protocol.cpp:16
const char * SPORK
Definition: protocol.cpp:41
int64_t nNextAddrSend
Definition: net.h:736
const CMessageHeader::MessageStartChars & MessageStart() const
Definition: chainparams.h:56
void ProcessMessage(CNode *pfrom, std::string &strCommand, CDataStream &vRecv)
CTransaction tx
int64_t nPingUsecStart
Definition: net.h:761
std::set< uint256 > setKnown
Definition: net.h:735
static void ProcessGetData(CNode *pfrom, const Consensus::Params &consensusParams, CConnman &connman, std::atomic< bool > &interruptMsgProc)
CSporkManager sporkManager
Definition: spork.cpp:14
std::string ToString() const
Definition: protocol.cpp:266
bool fWhitelisted
Definition: net.h:698
const char * GETHEADERS
Definition: protocol.cpp:23
bool fLogIPs
Definition: util.cpp:133
CPrivateSendClient privateSendClient
CCriticalSection cs_inventory
Definition: net.h:742
int64_t nMinPingUsecTime
Definition: net.h:765
uint256 hashContinue
Definition: net.h:728
std::atomic< int64_t > nLastBlockTime
Definition: net.h:754
bool IsReachable(enum Network net)
Definition: net.cpp:285
bool fAlerts
Definition: validation.cpp:85
const char * TXLOCKREQUEST
Definition: protocol.cpp:39
int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds)
Definition: net.cpp:2892
uint256 ArithToUint256(const arith_uint256 &a)
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:50
std::string FormatStateMessage(const CValidationState &state)
Definition: validation.cpp:541
const char * MNGOVERNANCEOBJECTVOTE
Definition: protocol.cpp:66
CTxMemPool mempool
CCriticalSection cs_mapRelay
Definition: net.cpp:87
unsigned int nTime
Definition: protocol.h:310
uint256 nMinimumChainWork
Definition: params.h:83
bool ProcessMessages(CNode *pfrom, CConnman &connman, std::atomic< bool > &interruptMsgProc)
const char * GETADDR
Definition: protocol.cpp:27
CBlockIndex * Next(const CBlockIndex *pindex) const
Definition: chain.h:389
const CChainParams & Params()
void * memcpy(void *a, const void *b, size_t c)
void AddInventoryKnown(const CInv &inv)
Definition: net.h:858
std::atomic< int64_t > nLastTXTime
Definition: net.h:755
static const int MIN_PEER_PROTO_VERSION
disconnect from peers older than this proto version
Definition: version.h:22
static const int PROTOCOL_VERSION
Definition: version.h:13
const uint256 & GetHash() const
Definition: transaction.h:262
void ProcessMessage(CNode *pfrom, std::string &strCommand, CDataStream &vRecv, CConnman &connman)
bool AddOrphanTx(const CTransaction &tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool SendMessages(CNode *pto, CConnman &connman, std::atomic< bool > &interruptMsgProc)
CCriticalSection cs_filter
Definition: net.h:715
boost::signals2::signal< bool(CNode *, CConnman &, std::atomic< bool > &), CombinerAll > ProcessMessages
Definition: net.h:533
bool ProcessTxLockRequest(const CTxLockRequest &txLockRequest, CConnman &connman)
Definition: instantx.cpp:80
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: chain.cpp:78
unsigned int nStatus
Verification status of this block. See enum BlockStatus.
Definition: chain.h:137
CBlockIndex * FindForkInGlobalIndex(const CChain &chain, const CBlockLocator &locator)
Definition: validation.cpp:172
int64_t GetAdjustedTime()
Definition: timedata.cpp:33
int NodeId
Definition: net.h:94
bool fImporting
Definition: validation.cpp:70
int nStartingHeight
Definition: net.h:729
uint256 GetBlockHash() const
Definition: chain.h:218
bool IsRoutable() const
Definition: netaddress.cpp:222
bool IsRelevantAndUpdate(const CTransaction &tx)
Also adds any outputs which match the filter to the filter (to match their spending txes) ...
Definition: bloom.cpp:136
const char * PING
Definition: protocol.cpp:29
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
bool IsInvalid() const
Definition: validation.h:64
bool ProcessNewBlockHeaders(const std::vector< CBlockHeader > &headers, CValidationState &state, const CChainParams &chainparams, CBlockIndex **ppindex)
#define LIMITED_STRING(obj, n)
Definition: serialize.h:389
static const int CADDR_TIME_VERSION
Definition: version.h:26
static const unsigned int MAX_BLOCKS_TO_ANNOUNCE
Definition: validation.h:134
bool fListen
Definition: net.cpp:77
int type
Definition: protocol.h:338
void PushBlockHashFromINV(const uint256 &hash)
Definition: net.h:885
Definition: protocol.h:314
Scripts & signatures ok. Implies all parents are also at least SCRIPTS.
Definition: chain.h:80
void GetRandBytes(unsigned char *buf, int num)
Definition: random.cpp:86
static int count
Definition: tests.c:41
static const unsigned int MAX_SUBVERSION_LENGTH
Definition: net.h:61
NodeId fromPeer
unsigned int nChainTx
Definition: chain.h:134
CBlockIndex * Tip() const
Definition: chain.h:366
bool HaveVoteForHash(uint256 nHash)
Definition: governance.cpp:62
int64_t nTimeBestReceived
void ProcessMessage(CNode *pfrom, std::string &strCommand, CDataStream &vRecv, CConnman &connman)
static const int PING_INTERVAL
Definition: net.h:47
std::multimap< int64_t, CInv > mapAskFor
Definition: net.h:744
static const int NO_BLOOM_VERSION
"filter*" commands are disabled without NODE_BLOOM after and including this version ...
Definition: version.h:39
int64_t nNextInvSend
Definition: net.h:745
int64_t GetBlockTime() const
Definition: chain.h:223
static const unsigned int DEFAULT_BANSCORE_THRESHOLD
Definition: validation.h:127
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Definition: util.cpp:441
bool GetTxLockRequest(const uint256 &txHash, CTxLockRequest &txLockRequestRet)
Definition: instantx.cpp:736
const char * MERKLEBLOCK
Definition: protocol.cpp:21
void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, const CAmount &nFeeDelta)
Definition: txmempool.cpp:934
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:107
static const unsigned int BLOCK_STALLING_TIMEOUT
Definition: validation.h:89
std::string addrName
Definition: net.h:689
int64_t GetTime()
For unit testing.
Definition: utiltime.cpp:20
int64_t DelayGetHeadersTime() const
Definition: chainparams.h:68
CGovernanceManager governance
Definition: governance.cpp:17
bool ReadBlockFromDisk(CBlock &block, const CDiskBlockPos &pos, const Consensus::Params &consensusParams)
CBlockLocator GetLocator(const CBlockIndex *pindex=NULL) const
Definition: chain.cpp:25
size_type size() const
Definition: streams.h:122
static const unsigned char REJECT_MALFORMED
Definition: validation.h:12
std::map< uint256, CMasternodePaymentVote > mapMasternodePaymentVotes
void AddTimeData(const CNetAddr &ip, int64_t nOffsetSample)
Definition: timedata.cpp:45
std::vector< CAddress > GetAddresses()
Definition: net.cpp:2400
Definition: block.h:73
bool fOneShot
Definition: net.h:700
bool RelayTo(CNode *pnode, CConnman &connman) const
Definition: alert.cpp:128
uint256 GetHash() const
Definition: block.cpp:13
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:113
static const bool DEFAULT_WHITELISTFORCERELAY
Definition: validation.h:55
std::string ToStringShort() const
Definition: transaction.cpp:17
static const unsigned int MAX_INV_SZ
Definition: net.h:55
limitedmap< uint256, int64_t > mapAlreadyAskedFor(MAX_INV_SZ)
void ForEachNode(const Condition &cond, Callable &&func)
Definition: net.h:239
#define PAIRTYPE(t1, t2)
void PushAddress(const CAddress &addr)
Definition: net.h:843
const char * MNANNOUNCE
Definition: protocol.cpp:52
uint256 hash
Definition: transaction.h:18
std::map< uint256, CSporkMessage > mapSporks
Definition: spork.cpp:18
void SetSendVersion(int nVersionIn)
Definition: net.cpp:728
BlockMap mapBlockIndex
Definition: validation.cpp:64
ServiceFlags nServices
Definition: net.h:666
void AdvertiseLocal(CNode *pnode)
Definition: net.cpp:183
static CDarksendBroadcastTx GetDSTX(const uint256 &hash)
void PushMessage(CNode *pnode, const std::string &sCommand, Args &&... args)
Definition: net.h:199
std::map< int, CMasternodeBlockPayees > mapMasternodeBlocks
static const unsigned int BLOCK_DOWNLOAD_WINDOW
Definition: validation.h:97
string SanitizeString(const string &str, int rule)
std::atomic< int > nVersion
Definition: net.h:692
std::vector< std::pair< unsigned int, uint256 > > vMatchedTxn
Definition: merkleblock.h:133
std::vector< CInv > vInventoryToSend
Definition: net.h:741
std::string itostr(int n)
int atoi(const std::string &str)
bool HasVerifiedPaymentVote(uint256 hashIn)
map< string, string > mapArgs
Definition: util.cpp:122
uint64_t GetRand(uint64_t nMax)
Definition: random.cpp:94
uint8_t pchChecksum[CHECKSUM_SIZE]
Definition: protocol.h:65
CCriticalSection cs_vProcessMsg
Definition: net.h:675