net: add a flag to indicate when a node's process queue is full
[bitcoinplatinum.git] / src / net_processing.cpp
blob93b6e2ec014ec56172ab0247dea68204ff2c7d00
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.
6 #include "net_processing.h"
8 #include "addrman.h"
9 #include "arith_uint256.h"
10 #include "blockencodings.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 "netmessagemaker.h"
19 #include "netbase.h"
20 #include "policy/fees.h"
21 #include "policy/policy.h"
22 #include "primitives/block.h"
23 #include "primitives/transaction.h"
24 #include "random.h"
25 #include "tinyformat.h"
26 #include "txmempool.h"
27 #include "ui_interface.h"
28 #include "util.h"
29 #include "utilmoneystr.h"
30 #include "utilstrencodings.h"
31 #include "validationinterface.h"
33 #include <boost/thread.hpp>
35 using namespace std;
37 #if defined(NDEBUG)
38 # error "Bitcoin cannot be compiled without assertions."
39 #endif
41 int64_t nTimeBestReceived = 0; // Used only to inform the wallet of when we last received a block
43 struct IteratorComparator
45 template<typename I>
46 bool operator()(const I& a, const I& b)
48 return &(*a) < &(*b);
52 struct COrphanTx {
53 // When modifying, adapt the copy of this definition in tests/DoS_tests.
54 CTransactionRef tx;
55 NodeId fromPeer;
56 int64_t nTimeExpire;
58 map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY(cs_main);
59 map<COutPoint, set<map<uint256, COrphanTx>::iterator, IteratorComparator>> mapOrphanTransactionsByPrev GUARDED_BY(cs_main);
60 void EraseOrphansFor(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
62 static const uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL; // SHA256("main address relay")[0:8]
64 // Internal stuff
65 namespace {
66 /** Number of nodes with fSyncStarted. */
67 int nSyncStarted = 0;
69 /**
70 * Sources of received blocks, saved to be able to send them reject
71 * messages or ban them when processing happens afterwards. Protected by
72 * cs_main.
73 * Set mapBlockSource[hash].second to false if the node should not be
74 * punished if the block is invalid.
76 map<uint256, std::pair<NodeId, bool>> mapBlockSource;
78 /**
79 * Filter for transactions that were recently rejected by
80 * AcceptToMemoryPool. These are not rerequested until the chain tip
81 * changes, at which point the entire filter is reset. Protected by
82 * cs_main.
84 * Without this filter we'd be re-requesting txs from each of our peers,
85 * increasing bandwidth consumption considerably. For instance, with 100
86 * peers, half of which relay a tx we don't accept, that might be a 50x
87 * bandwidth increase. A flooding attacker attempting to roll-over the
88 * filter using minimum-sized, 60byte, transactions might manage to send
89 * 1000/sec if we have fast peers, so we pick 120,000 to give our peers a
90 * two minute window to send invs to us.
92 * Decreasing the false positive rate is fairly cheap, so we pick one in a
93 * million to make it highly unlikely for users to have issues with this
94 * filter.
96 * Memory used: 1.3 MB
98 std::unique_ptr<CRollingBloomFilter> recentRejects;
99 uint256 hashRecentRejectsChainTip;
101 /** Blocks that are in flight, and that are in the queue to be downloaded. Protected by cs_main. */
102 struct QueuedBlock {
103 uint256 hash;
104 CBlockIndex* pindex; //!< Optional.
105 bool fValidatedHeaders; //!< Whether this block has validated headers at the time of request.
106 std::unique_ptr<PartiallyDownloadedBlock> partialBlock; //!< Optional, used for CMPCTBLOCK downloads
108 map<uint256, pair<NodeId, list<QueuedBlock>::iterator> > mapBlocksInFlight;
110 /** Stack of nodes which we have set to announce using compact blocks */
111 list<NodeId> lNodesAnnouncingHeaderAndIDs;
113 /** Number of preferable block download peers. */
114 int nPreferredDownload = 0;
116 /** Number of peers from which we're downloading blocks. */
117 int nPeersWithValidatedDownloads = 0;
119 /** Relay map, protected by cs_main. */
120 typedef std::map<uint256, CTransactionRef> MapRelay;
121 MapRelay mapRelay;
122 /** Expiration-time ordered list of (expire time, relay map entry) pairs, protected by cs_main). */
123 std::deque<std::pair<int64_t, MapRelay::iterator>> vRelayExpiration;
124 } // anon namespace
126 //////////////////////////////////////////////////////////////////////////////
128 // Registration of network node signals.
131 namespace {
133 struct CBlockReject {
134 unsigned char chRejectCode;
135 string strRejectReason;
136 uint256 hashBlock;
140 * Maintain validation-specific state about nodes, protected by cs_main, instead
141 * by CNode's own locks. This simplifies asynchronous operation, where
142 * processing of incoming data is done after the ProcessMessage call returns,
143 * and we're no longer holding the node's locks.
145 struct CNodeState {
146 //! The peer's address
147 const CService address;
148 //! Whether we have a fully established connection.
149 bool fCurrentlyConnected;
150 //! Accumulated misbehaviour score for this peer.
151 int nMisbehavior;
152 //! Whether this peer should be disconnected and banned (unless whitelisted).
153 bool fShouldBan;
154 //! String name of this peer (debugging/logging purposes).
155 const std::string name;
156 //! List of asynchronously-determined block rejections to notify this peer about.
157 std::vector<CBlockReject> rejects;
158 //! The best known block we know this peer has announced.
159 CBlockIndex *pindexBestKnownBlock;
160 //! The hash of the last unknown block this peer has announced.
161 uint256 hashLastUnknownBlock;
162 //! The last full block we both have.
163 CBlockIndex *pindexLastCommonBlock;
164 //! The best header we have sent our peer.
165 CBlockIndex *pindexBestHeaderSent;
166 //! Length of current-streak of unconnecting headers announcements
167 int nUnconnectingHeaders;
168 //! Whether we've started headers synchronization with this peer.
169 bool fSyncStarted;
170 //! Since when we're stalling block download progress (in microseconds), or 0.
171 int64_t nStallingSince;
172 list<QueuedBlock> vBlocksInFlight;
173 //! When the first entry in vBlocksInFlight started downloading. Don't care when vBlocksInFlight is empty.
174 int64_t nDownloadingSince;
175 int nBlocksInFlight;
176 int nBlocksInFlightValidHeaders;
177 //! Whether we consider this a preferred download peer.
178 bool fPreferredDownload;
179 //! Whether this peer wants invs or headers (when possible) for block announcements.
180 bool fPreferHeaders;
181 //! Whether this peer wants invs or cmpctblocks (when possible) for block announcements.
182 bool fPreferHeaderAndIDs;
184 * Whether this peer will send us cmpctblocks if we request them.
185 * This is not used to gate request logic, as we really only care about fSupportsDesiredCmpctVersion,
186 * but is used as a flag to "lock in" the version of compact blocks (fWantsCmpctWitness) we send.
188 bool fProvidesHeaderAndIDs;
189 //! Whether this peer can give us witnesses
190 bool fHaveWitness;
191 //! Whether this peer wants witnesses in cmpctblocks/blocktxns
192 bool fWantsCmpctWitness;
194 * If we've announced NODE_WITNESS to this peer: whether the peer sends witnesses in cmpctblocks/blocktxns,
195 * otherwise: whether this peer sends non-witnesses in cmpctblocks/blocktxns.
197 bool fSupportsDesiredCmpctVersion;
199 CNodeState(CAddress addrIn, std::string addrNameIn) : address(addrIn), name(addrNameIn) {
200 fCurrentlyConnected = false;
201 nMisbehavior = 0;
202 fShouldBan = false;
203 pindexBestKnownBlock = NULL;
204 hashLastUnknownBlock.SetNull();
205 pindexLastCommonBlock = NULL;
206 pindexBestHeaderSent = NULL;
207 nUnconnectingHeaders = 0;
208 fSyncStarted = false;
209 nStallingSince = 0;
210 nDownloadingSince = 0;
211 nBlocksInFlight = 0;
212 nBlocksInFlightValidHeaders = 0;
213 fPreferredDownload = false;
214 fPreferHeaders = false;
215 fPreferHeaderAndIDs = false;
216 fProvidesHeaderAndIDs = false;
217 fHaveWitness = false;
218 fWantsCmpctWitness = false;
219 fSupportsDesiredCmpctVersion = false;
223 /** Map maintaining per-node state. Requires cs_main. */
224 map<NodeId, CNodeState> mapNodeState;
226 // Requires cs_main.
227 CNodeState *State(NodeId pnode) {
228 map<NodeId, CNodeState>::iterator it = mapNodeState.find(pnode);
229 if (it == mapNodeState.end())
230 return NULL;
231 return &it->second;
234 void UpdatePreferredDownload(CNode* node, CNodeState* state)
236 nPreferredDownload -= state->fPreferredDownload;
238 // Whether this node should be marked as a preferred download node.
239 state->fPreferredDownload = (!node->fInbound || node->fWhitelisted) && !node->fOneShot && !node->fClient;
241 nPreferredDownload += state->fPreferredDownload;
244 void PushNodeVersion(CNode *pnode, CConnman& connman, int64_t nTime)
246 ServiceFlags nLocalNodeServices = pnode->GetLocalServices();
247 uint64_t nonce = pnode->GetLocalNonce();
248 int nNodeStartingHeight = pnode->GetMyStartingHeight();
249 NodeId nodeid = pnode->GetId();
250 CAddress addr = pnode->addr;
252 CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService(), addr.nServices));
253 CAddress addrMe = CAddress(CService(), nLocalNodeServices);
255 connman.PushMessage(pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t)nLocalNodeServices, nTime, addrYou, addrMe,
256 nonce, strSubVersion, nNodeStartingHeight, ::fRelayTxes));
258 if (fLogIPs)
259 LogPrint("net", "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), addrYou.ToString(), nodeid);
260 else
261 LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), nodeid);
264 void InitializeNode(CNode *pnode, CConnman& connman) {
265 CAddress addr = pnode->addr;
266 std::string addrName = pnode->addrName;
267 NodeId nodeid = pnode->GetId();
269 LOCK(cs_main);
270 mapNodeState.emplace_hint(mapNodeState.end(), std::piecewise_construct, std::forward_as_tuple(nodeid), std::forward_as_tuple(addr, std::move(addrName)));
272 if(!pnode->fInbound)
273 PushNodeVersion(pnode, connman, GetTime());
276 void FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTime) {
277 fUpdateConnectionTime = false;
278 LOCK(cs_main);
279 CNodeState *state = State(nodeid);
281 if (state->fSyncStarted)
282 nSyncStarted--;
284 if (state->nMisbehavior == 0 && state->fCurrentlyConnected) {
285 fUpdateConnectionTime = true;
288 BOOST_FOREACH(const QueuedBlock& entry, state->vBlocksInFlight) {
289 mapBlocksInFlight.erase(entry.hash);
291 EraseOrphansFor(nodeid);
292 nPreferredDownload -= state->fPreferredDownload;
293 nPeersWithValidatedDownloads -= (state->nBlocksInFlightValidHeaders != 0);
294 assert(nPeersWithValidatedDownloads >= 0);
296 mapNodeState.erase(nodeid);
298 if (mapNodeState.empty()) {
299 // Do a consistency check after the last peer is removed.
300 assert(mapBlocksInFlight.empty());
301 assert(nPreferredDownload == 0);
302 assert(nPeersWithValidatedDownloads == 0);
306 // Requires cs_main.
307 // Returns a bool indicating whether we requested this block.
308 // Also used if a block was /not/ received and timed out or started with another peer
309 bool MarkBlockAsReceived(const uint256& hash) {
310 map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
311 if (itInFlight != mapBlocksInFlight.end()) {
312 CNodeState *state = State(itInFlight->second.first);
313 state->nBlocksInFlightValidHeaders -= itInFlight->second.second->fValidatedHeaders;
314 if (state->nBlocksInFlightValidHeaders == 0 && itInFlight->second.second->fValidatedHeaders) {
315 // Last validated block on the queue was received.
316 nPeersWithValidatedDownloads--;
318 if (state->vBlocksInFlight.begin() == itInFlight->second.second) {
319 // First block on the queue was received, update the start download time for the next one
320 state->nDownloadingSince = std::max(state->nDownloadingSince, GetTimeMicros());
322 state->vBlocksInFlight.erase(itInFlight->second.second);
323 state->nBlocksInFlight--;
324 state->nStallingSince = 0;
325 mapBlocksInFlight.erase(itInFlight);
326 return true;
328 return false;
331 // Requires cs_main.
332 // returns false, still setting pit, if the block was already in flight from the same peer
333 // pit will only be valid as long as the same cs_main lock is being held
334 bool MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const Consensus::Params& consensusParams, CBlockIndex *pindex = NULL, list<QueuedBlock>::iterator **pit = NULL) {
335 CNodeState *state = State(nodeid);
336 assert(state != NULL);
338 // Short-circuit most stuff in case its from the same node
339 map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
340 if (itInFlight != mapBlocksInFlight.end() && itInFlight->second.first == nodeid) {
341 *pit = &itInFlight->second.second;
342 return false;
345 // Make sure it's not listed somewhere already.
346 MarkBlockAsReceived(hash);
348 list<QueuedBlock>::iterator it = state->vBlocksInFlight.insert(state->vBlocksInFlight.end(),
349 {hash, pindex, pindex != NULL, std::unique_ptr<PartiallyDownloadedBlock>(pit ? new PartiallyDownloadedBlock(&mempool) : NULL)});
350 state->nBlocksInFlight++;
351 state->nBlocksInFlightValidHeaders += it->fValidatedHeaders;
352 if (state->nBlocksInFlight == 1) {
353 // We're starting a block download (batch) from this peer.
354 state->nDownloadingSince = GetTimeMicros();
356 if (state->nBlocksInFlightValidHeaders == 1 && pindex != NULL) {
357 nPeersWithValidatedDownloads++;
359 itInFlight = mapBlocksInFlight.insert(std::make_pair(hash, std::make_pair(nodeid, it))).first;
360 if (pit)
361 *pit = &itInFlight->second.second;
362 return true;
365 /** Check whether the last unknown block a peer advertised is not yet known. */
366 void ProcessBlockAvailability(NodeId nodeid) {
367 CNodeState *state = State(nodeid);
368 assert(state != NULL);
370 if (!state->hashLastUnknownBlock.IsNull()) {
371 BlockMap::iterator itOld = mapBlockIndex.find(state->hashLastUnknownBlock);
372 if (itOld != mapBlockIndex.end() && itOld->second->nChainWork > 0) {
373 if (state->pindexBestKnownBlock == NULL || itOld->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
374 state->pindexBestKnownBlock = itOld->second;
375 state->hashLastUnknownBlock.SetNull();
380 /** Update tracking information about which blocks a peer is assumed to have. */
381 void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
382 CNodeState *state = State(nodeid);
383 assert(state != NULL);
385 ProcessBlockAvailability(nodeid);
387 BlockMap::iterator it = mapBlockIndex.find(hash);
388 if (it != mapBlockIndex.end() && it->second->nChainWork > 0) {
389 // An actually better block was announced.
390 if (state->pindexBestKnownBlock == NULL || it->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
391 state->pindexBestKnownBlock = it->second;
392 } else {
393 // An unknown block was announced; just assume that the latest one is the best one.
394 state->hashLastUnknownBlock = hash;
398 void MaybeSetPeerAsAnnouncingHeaderAndIDs(const CNodeState* nodestate, CNode* pfrom, CConnman& connman) {
399 if (!nodestate->fSupportsDesiredCmpctVersion) {
400 // Never ask from peers who can't provide witnesses.
401 return;
403 if (nodestate->fProvidesHeaderAndIDs) {
404 for (std::list<NodeId>::iterator it = lNodesAnnouncingHeaderAndIDs.begin(); it != lNodesAnnouncingHeaderAndIDs.end(); it++) {
405 if (*it == pfrom->GetId()) {
406 lNodesAnnouncingHeaderAndIDs.erase(it);
407 lNodesAnnouncingHeaderAndIDs.push_back(pfrom->GetId());
408 return;
411 bool fAnnounceUsingCMPCTBLOCK = false;
412 uint64_t nCMPCTBLOCKVersion = (pfrom->GetLocalServices() & NODE_WITNESS) ? 2 : 1;
413 if (lNodesAnnouncingHeaderAndIDs.size() >= 3) {
414 // As per BIP152, we only get 3 of our peers to announce
415 // blocks using compact encodings.
416 connman.ForNode(lNodesAnnouncingHeaderAndIDs.front(), [&connman, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion](CNode* pnodeStop){
417 connman.PushMessage(pnodeStop, CNetMsgMaker(pnodeStop->GetSendVersion()).Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
418 return true;
420 lNodesAnnouncingHeaderAndIDs.pop_front();
422 fAnnounceUsingCMPCTBLOCK = true;
423 connman.PushMessage(pfrom, CNetMsgMaker(pfrom->GetSendVersion()).Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
424 lNodesAnnouncingHeaderAndIDs.push_back(pfrom->GetId());
428 // Requires cs_main
429 bool CanDirectFetch(const Consensus::Params &consensusParams)
431 return chainActive.Tip()->GetBlockTime() > GetAdjustedTime() - consensusParams.nPowTargetSpacing * 20;
434 // Requires cs_main
435 bool PeerHasHeader(CNodeState *state, CBlockIndex *pindex)
437 if (state->pindexBestKnownBlock && pindex == state->pindexBestKnownBlock->GetAncestor(pindex->nHeight))
438 return true;
439 if (state->pindexBestHeaderSent && pindex == state->pindexBestHeaderSent->GetAncestor(pindex->nHeight))
440 return true;
441 return false;
444 /** Find the last common ancestor two blocks have.
445 * Both pa and pb must be non-NULL. */
446 CBlockIndex* LastCommonAncestor(CBlockIndex* pa, CBlockIndex* pb) {
447 if (pa->nHeight > pb->nHeight) {
448 pa = pa->GetAncestor(pb->nHeight);
449 } else if (pb->nHeight > pa->nHeight) {
450 pb = pb->GetAncestor(pa->nHeight);
453 while (pa != pb && pa && pb) {
454 pa = pa->pprev;
455 pb = pb->pprev;
458 // Eventually all chain branches meet at the genesis block.
459 assert(pa == pb);
460 return pa;
463 /** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
464 * at most count entries. */
465 void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams) {
466 if (count == 0)
467 return;
469 vBlocks.reserve(vBlocks.size() + count);
470 CNodeState *state = State(nodeid);
471 assert(state != NULL);
473 // Make sure pindexBestKnownBlock is up to date, we'll need it.
474 ProcessBlockAvailability(nodeid);
476 if (state->pindexBestKnownBlock == NULL || state->pindexBestKnownBlock->nChainWork < chainActive.Tip()->nChainWork) {
477 // This peer has nothing interesting.
478 return;
481 if (state->pindexLastCommonBlock == NULL) {
482 // Bootstrap quickly by guessing a parent of our best tip is the forking point.
483 // Guessing wrong in either direction is not a problem.
484 state->pindexLastCommonBlock = chainActive[std::min(state->pindexBestKnownBlock->nHeight, chainActive.Height())];
487 // If the peer reorganized, our previous pindexLastCommonBlock may not be an ancestor
488 // of its current tip anymore. Go back enough to fix that.
489 state->pindexLastCommonBlock = LastCommonAncestor(state->pindexLastCommonBlock, state->pindexBestKnownBlock);
490 if (state->pindexLastCommonBlock == state->pindexBestKnownBlock)
491 return;
493 std::vector<CBlockIndex*> vToFetch;
494 CBlockIndex *pindexWalk = state->pindexLastCommonBlock;
495 // Never fetch further than the best block we know the peer has, or more than BLOCK_DOWNLOAD_WINDOW + 1 beyond the last
496 // linked block we have in common with this peer. The +1 is so we can detect stalling, namely if we would be able to
497 // download that next block if the window were 1 larger.
498 int nWindowEnd = state->pindexLastCommonBlock->nHeight + BLOCK_DOWNLOAD_WINDOW;
499 int nMaxHeight = std::min<int>(state->pindexBestKnownBlock->nHeight, nWindowEnd + 1);
500 NodeId waitingfor = -1;
501 while (pindexWalk->nHeight < nMaxHeight) {
502 // Read up to 128 (or more, if more blocks than that are needed) successors of pindexWalk (towards
503 // pindexBestKnownBlock) into vToFetch. We fetch 128, because CBlockIndex::GetAncestor may be as expensive
504 // as iterating over ~100 CBlockIndex* entries anyway.
505 int nToFetch = std::min(nMaxHeight - pindexWalk->nHeight, std::max<int>(count - vBlocks.size(), 128));
506 vToFetch.resize(nToFetch);
507 pindexWalk = state->pindexBestKnownBlock->GetAncestor(pindexWalk->nHeight + nToFetch);
508 vToFetch[nToFetch - 1] = pindexWalk;
509 for (unsigned int i = nToFetch - 1; i > 0; i--) {
510 vToFetch[i - 1] = vToFetch[i]->pprev;
513 // Iterate over those blocks in vToFetch (in forward direction), adding the ones that
514 // are not yet downloaded and not in flight to vBlocks. In the mean time, update
515 // pindexLastCommonBlock as long as all ancestors are already downloaded, or if it's
516 // already part of our chain (and therefore don't need it even if pruned).
517 BOOST_FOREACH(CBlockIndex* pindex, vToFetch) {
518 if (!pindex->IsValid(BLOCK_VALID_TREE)) {
519 // We consider the chain that this peer is on invalid.
520 return;
522 if (!State(nodeid)->fHaveWitness && IsWitnessEnabled(pindex->pprev, consensusParams)) {
523 // We wouldn't download this block or its descendants from this peer.
524 return;
526 if (pindex->nStatus & BLOCK_HAVE_DATA || chainActive.Contains(pindex)) {
527 if (pindex->nChainTx)
528 state->pindexLastCommonBlock = pindex;
529 } else if (mapBlocksInFlight.count(pindex->GetBlockHash()) == 0) {
530 // The block is not already downloaded, and not yet in flight.
531 if (pindex->nHeight > nWindowEnd) {
532 // We reached the end of the window.
533 if (vBlocks.size() == 0 && waitingfor != nodeid) {
534 // We aren't able to fetch anything, but we would be if the download window was one larger.
535 nodeStaller = waitingfor;
537 return;
539 vBlocks.push_back(pindex);
540 if (vBlocks.size() == count) {
541 return;
543 } else if (waitingfor == -1) {
544 // This is the first already-in-flight block.
545 waitingfor = mapBlocksInFlight[pindex->GetBlockHash()].first;
551 } // anon namespace
553 bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
554 LOCK(cs_main);
555 CNodeState *state = State(nodeid);
556 if (state == NULL)
557 return false;
558 stats.nMisbehavior = state->nMisbehavior;
559 stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
560 stats.nCommonHeight = state->pindexLastCommonBlock ? state->pindexLastCommonBlock->nHeight : -1;
561 BOOST_FOREACH(const QueuedBlock& queue, state->vBlocksInFlight) {
562 if (queue.pindex)
563 stats.vHeightInFlight.push_back(queue.pindex->nHeight);
565 return true;
568 void RegisterNodeSignals(CNodeSignals& nodeSignals)
570 nodeSignals.ProcessMessages.connect(&ProcessMessages);
571 nodeSignals.SendMessages.connect(&SendMessages);
572 nodeSignals.InitializeNode.connect(&InitializeNode);
573 nodeSignals.FinalizeNode.connect(&FinalizeNode);
576 void UnregisterNodeSignals(CNodeSignals& nodeSignals)
578 nodeSignals.ProcessMessages.disconnect(&ProcessMessages);
579 nodeSignals.SendMessages.disconnect(&SendMessages);
580 nodeSignals.InitializeNode.disconnect(&InitializeNode);
581 nodeSignals.FinalizeNode.disconnect(&FinalizeNode);
584 //////////////////////////////////////////////////////////////////////////////
586 // mapOrphanTransactions
589 bool AddOrphanTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
591 const uint256& hash = tx->GetHash();
592 if (mapOrphanTransactions.count(hash))
593 return false;
595 // Ignore big transactions, to avoid a
596 // send-big-orphans memory exhaustion attack. If a peer has a legitimate
597 // large transaction with a missing parent then we assume
598 // it will rebroadcast it later, after the parent transaction(s)
599 // have been mined or received.
600 // 100 orphans, each of which is at most 99,999 bytes big is
601 // at most 10 megabytes of orphans and somewhat more byprev index (in the worst case):
602 unsigned int sz = GetTransactionWeight(*tx);
603 if (sz >= MAX_STANDARD_TX_WEIGHT)
605 LogPrint("mempool", "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString());
606 return false;
609 auto ret = mapOrphanTransactions.emplace(hash, COrphanTx{tx, peer, GetTime() + ORPHAN_TX_EXPIRE_TIME});
610 assert(ret.second);
611 BOOST_FOREACH(const CTxIn& txin, tx->vin) {
612 mapOrphanTransactionsByPrev[txin.prevout].insert(ret.first);
615 LogPrint("mempool", "stored orphan tx %s (mapsz %u outsz %u)\n", hash.ToString(),
616 mapOrphanTransactions.size(), mapOrphanTransactionsByPrev.size());
617 return true;
620 int static EraseOrphanTx(uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
622 map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.find(hash);
623 if (it == mapOrphanTransactions.end())
624 return 0;
625 BOOST_FOREACH(const CTxIn& txin, it->second.tx->vin)
627 auto itPrev = mapOrphanTransactionsByPrev.find(txin.prevout);
628 if (itPrev == mapOrphanTransactionsByPrev.end())
629 continue;
630 itPrev->second.erase(it);
631 if (itPrev->second.empty())
632 mapOrphanTransactionsByPrev.erase(itPrev);
634 mapOrphanTransactions.erase(it);
635 return 1;
638 void EraseOrphansFor(NodeId peer)
640 int nErased = 0;
641 map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
642 while (iter != mapOrphanTransactions.end())
644 map<uint256, COrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
645 if (maybeErase->second.fromPeer == peer)
647 nErased += EraseOrphanTx(maybeErase->second.tx->GetHash());
650 if (nErased > 0) LogPrint("mempool", "Erased %d orphan tx from peer %d\n", nErased, peer);
654 unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
656 unsigned int nEvicted = 0;
657 static int64_t nNextSweep;
658 int64_t nNow = GetTime();
659 if (nNextSweep <= nNow) {
660 // Sweep out expired orphan pool entries:
661 int nErased = 0;
662 int64_t nMinExpTime = nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL;
663 map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
664 while (iter != mapOrphanTransactions.end())
666 map<uint256, COrphanTx>::iterator maybeErase = iter++;
667 if (maybeErase->second.nTimeExpire <= nNow) {
668 nErased += EraseOrphanTx(maybeErase->second.tx->GetHash());
669 } else {
670 nMinExpTime = std::min(maybeErase->second.nTimeExpire, nMinExpTime);
673 // Sweep again 5 minutes after the next entry that expires in order to batch the linear scan.
674 nNextSweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL;
675 if (nErased > 0) LogPrint("mempool", "Erased %d orphan tx due to expiration\n", nErased);
677 while (mapOrphanTransactions.size() > nMaxOrphans)
679 // Evict a random orphan:
680 uint256 randomhash = GetRandHash();
681 map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.lower_bound(randomhash);
682 if (it == mapOrphanTransactions.end())
683 it = mapOrphanTransactions.begin();
684 EraseOrphanTx(it->first);
685 ++nEvicted;
687 return nEvicted;
690 // Requires cs_main.
691 void Misbehaving(NodeId pnode, int howmuch)
693 if (howmuch == 0)
694 return;
696 CNodeState *state = State(pnode);
697 if (state == NULL)
698 return;
700 state->nMisbehavior += howmuch;
701 int banscore = GetArg("-banscore", DEFAULT_BANSCORE_THRESHOLD);
702 if (state->nMisbehavior >= banscore && state->nMisbehavior - howmuch < banscore)
704 LogPrintf("%s: %s peer=%d (%d -> %d) BAN THRESHOLD EXCEEDED\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior);
705 state->fShouldBan = true;
706 } else
707 LogPrintf("%s: %s peer=%d (%d -> %d)\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior);
717 //////////////////////////////////////////////////////////////////////////////
719 // blockchain -> download logic notification
722 PeerLogicValidation::PeerLogicValidation(CConnman* connmanIn) : connman(connmanIn) {
723 // Initialize global variables that cannot be constructed at startup.
724 recentRejects.reset(new CRollingBloomFilter(120000, 0.000001));
727 void PeerLogicValidation::SyncTransaction(const CTransaction& tx, const CBlockIndex* pindex, int nPosInBlock) {
728 if (nPosInBlock == CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK)
729 return;
731 LOCK(cs_main);
733 std::vector<uint256> vOrphanErase;
734 // Which orphan pool entries must we evict?
735 for (size_t j = 0; j < tx.vin.size(); j++) {
736 auto itByPrev = mapOrphanTransactionsByPrev.find(tx.vin[j].prevout);
737 if (itByPrev == mapOrphanTransactionsByPrev.end()) continue;
738 for (auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); ++mi) {
739 const CTransaction& orphanTx = *(*mi)->second.tx;
740 const uint256& orphanHash = orphanTx.GetHash();
741 vOrphanErase.push_back(orphanHash);
745 // Erase orphan transactions include or precluded by this block
746 if (vOrphanErase.size()) {
747 int nErased = 0;
748 BOOST_FOREACH(uint256 &orphanHash, vOrphanErase) {
749 nErased += EraseOrphanTx(orphanHash);
751 LogPrint("mempool", "Erased %d orphan tx included or conflicted by block\n", nErased);
755 void PeerLogicValidation::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
756 const int nNewHeight = pindexNew->nHeight;
757 connman->SetBestHeight(nNewHeight);
759 if (!fInitialDownload) {
760 // Find the hashes of all blocks that weren't previously in the best chain.
761 std::vector<uint256> vHashes;
762 const CBlockIndex *pindexToAnnounce = pindexNew;
763 while (pindexToAnnounce != pindexFork) {
764 vHashes.push_back(pindexToAnnounce->GetBlockHash());
765 pindexToAnnounce = pindexToAnnounce->pprev;
766 if (vHashes.size() == MAX_BLOCKS_TO_ANNOUNCE) {
767 // Limit announcements in case of a huge reorganization.
768 // Rely on the peer's synchronization mechanism in that case.
769 break;
772 // Relay inventory, but don't relay old inventory during initial block download.
773 connman->ForEachNode([nNewHeight, &vHashes](CNode* pnode) {
774 if (nNewHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : 0)) {
775 BOOST_REVERSE_FOREACH(const uint256& hash, vHashes) {
776 pnode->PushBlockHash(hash);
782 nTimeBestReceived = GetTime();
785 void PeerLogicValidation::BlockChecked(const CBlock& block, const CValidationState& state) {
786 LOCK(cs_main);
788 const uint256 hash(block.GetHash());
789 std::map<uint256, std::pair<NodeId, bool>>::iterator it = mapBlockSource.find(hash);
791 int nDoS = 0;
792 if (state.IsInvalid(nDoS)) {
793 if (it != mapBlockSource.end() && State(it->second.first)) {
794 assert (state.GetRejectCode() < REJECT_INTERNAL); // Blocks are never rejected with internal reject codes
795 CBlockReject reject = {(unsigned char)state.GetRejectCode(), state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), hash};
796 State(it->second.first)->rejects.push_back(reject);
797 if (nDoS > 0 && it->second.second)
798 Misbehaving(it->second.first, nDoS);
801 if (it != mapBlockSource.end())
802 mapBlockSource.erase(it);
805 //////////////////////////////////////////////////////////////////////////////
807 // Messages
811 bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
813 switch (inv.type)
815 case MSG_TX:
816 case MSG_WITNESS_TX:
818 assert(recentRejects);
819 if (chainActive.Tip()->GetBlockHash() != hashRecentRejectsChainTip)
821 // If the chain tip has changed previously rejected transactions
822 // might be now valid, e.g. due to a nLockTime'd tx becoming valid,
823 // or a double-spend. Reset the rejects filter and give those
824 // txs a second chance.
825 hashRecentRejectsChainTip = chainActive.Tip()->GetBlockHash();
826 recentRejects->reset();
829 // Use pcoinsTip->HaveCoinsInCache as a quick approximation to exclude
830 // requesting or processing some txs which have already been included in a block
831 return recentRejects->contains(inv.hash) ||
832 mempool.exists(inv.hash) ||
833 mapOrphanTransactions.count(inv.hash) ||
834 pcoinsTip->HaveCoinsInCache(inv.hash);
836 case MSG_BLOCK:
837 case MSG_WITNESS_BLOCK:
838 return mapBlockIndex.count(inv.hash);
840 // Don't know what it is, just say we already got one
841 return true;
844 static void RelayTransaction(const CTransaction& tx, CConnman& connman)
846 CInv inv(MSG_TX, tx.GetHash());
847 connman.ForEachNode([&inv](CNode* pnode)
849 pnode->PushInventory(inv);
853 static void RelayAddress(const CAddress& addr, bool fReachable, CConnman& connman)
855 unsigned int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
857 // Relay to a limited number of other nodes
858 // Use deterministic randomness to send to the same nodes for 24 hours
859 // at a time so the addrKnowns of the chosen nodes prevent repeats
860 uint64_t hashAddr = addr.GetHash();
861 const CSipHasher hasher = connman.GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));
862 FastRandomContext insecure_rand;
864 std::array<std::pair<uint64_t, CNode*>,2> best{{{0, nullptr}, {0, nullptr}}};
865 assert(nRelayNodes <= best.size());
867 auto sortfunc = [&best, &hasher, nRelayNodes](CNode* pnode) {
868 if (pnode->nVersion >= CADDR_TIME_VERSION) {
869 uint64_t hashKey = CSipHasher(hasher).Write(pnode->id).Finalize();
870 for (unsigned int i = 0; i < nRelayNodes; i++) {
871 if (hashKey > best[i].first) {
872 std::copy(best.begin() + i, best.begin() + nRelayNodes - 1, best.begin() + i + 1);
873 best[i] = std::make_pair(hashKey, pnode);
874 break;
880 auto pushfunc = [&addr, &best, nRelayNodes, &insecure_rand] {
881 for (unsigned int i = 0; i < nRelayNodes && best[i].first != 0; i++) {
882 best[i].second->PushAddress(addr, insecure_rand);
886 connman.ForEachNodeThen(std::move(sortfunc), std::move(pushfunc));
889 void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParams, CConnman& connman, std::atomic<bool>& interruptMsgProc)
891 std::deque<CInv>::iterator it = pfrom->vRecvGetData.begin();
892 unsigned int nMaxSendBufferSize = connman.GetSendBufferSize();
893 vector<CInv> vNotFound;
894 CNetMsgMaker msgMaker(pfrom->GetSendVersion());
895 LOCK(cs_main);
897 while (it != pfrom->vRecvGetData.end()) {
898 // Don't bother if send buffer is too full to respond anyway
899 if (pfrom->nSendSize >= nMaxSendBufferSize)
900 break;
902 const CInv &inv = *it;
904 if (interruptMsgProc)
905 return;
907 it++;
909 if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK || inv.type == MSG_CMPCT_BLOCK || inv.type == MSG_WITNESS_BLOCK)
911 bool send = false;
912 BlockMap::iterator mi = mapBlockIndex.find(inv.hash);
913 if (mi != mapBlockIndex.end())
915 if (chainActive.Contains(mi->second)) {
916 send = true;
917 } else {
918 static const int nOneMonth = 30 * 24 * 60 * 60;
919 // To prevent fingerprinting attacks, only send blocks outside of the active
920 // chain if they are valid, and no more than a month older (both in time, and in
921 // best equivalent proof of work) than the best header chain we know about.
922 send = mi->second->IsValid(BLOCK_VALID_SCRIPTS) && (pindexBestHeader != NULL) &&
923 (pindexBestHeader->GetBlockTime() - mi->second->GetBlockTime() < nOneMonth) &&
924 (GetBlockProofEquivalentTime(*pindexBestHeader, *mi->second, *pindexBestHeader, consensusParams) < nOneMonth);
925 if (!send) {
926 LogPrintf("%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom->GetId());
930 // disconnect node in case we have reached the outbound limit for serving historical blocks
931 // never disconnect whitelisted nodes
932 static const int nOneWeek = 7 * 24 * 60 * 60; // assume > 1 week = historical
933 if (send && connman.OutboundTargetReached(true) && ( ((pindexBestHeader != NULL) && (pindexBestHeader->GetBlockTime() - mi->second->GetBlockTime() > nOneWeek)) || inv.type == MSG_FILTERED_BLOCK) && !pfrom->fWhitelisted)
935 LogPrint("net", "historical block serving limit reached, disconnect peer=%d\n", pfrom->GetId());
937 //disconnect node
938 pfrom->fDisconnect = true;
939 send = false;
941 // Pruned nodes may have deleted the block, so check whether
942 // it's available before trying to send.
943 if (send && (mi->second->nStatus & BLOCK_HAVE_DATA))
945 // Send block from disk
946 CBlock block;
947 if (!ReadBlockFromDisk(block, (*mi).second, consensusParams))
948 assert(!"cannot load block from disk");
949 if (inv.type == MSG_BLOCK)
950 connman.PushMessage(pfrom, msgMaker.Make(SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::BLOCK, block));
951 else if (inv.type == MSG_WITNESS_BLOCK)
952 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::BLOCK, block));
953 else if (inv.type == MSG_FILTERED_BLOCK)
955 bool sendMerkleBlock = false;
956 CMerkleBlock merkleBlock;
958 LOCK(pfrom->cs_filter);
959 if (pfrom->pfilter) {
960 sendMerkleBlock = true;
961 merkleBlock = CMerkleBlock(block, *pfrom->pfilter);
964 if (sendMerkleBlock) {
965 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::MERKLEBLOCK, merkleBlock));
966 // CMerkleBlock just contains hashes, so also push any transactions in the block the client did not see
967 // This avoids hurting performance by pointlessly requiring a round-trip
968 // Note that there is currently no way for a node to request any single transactions we didn't send here -
969 // they must either disconnect and retry or request the full block.
970 // Thus, the protocol spec specified allows for us to provide duplicate txn here,
971 // however we MUST always provide at least what the remote peer needs
972 typedef std::pair<unsigned int, uint256> PairType;
973 BOOST_FOREACH(PairType& pair, merkleBlock.vMatchedTxn)
974 connman.PushMessage(pfrom, msgMaker.Make(SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::TX, *block.vtx[pair.first]));
976 // else
977 // no response
979 else if (inv.type == MSG_CMPCT_BLOCK)
981 // If a peer is asking for old blocks, we're almost guaranteed
982 // they won't have a useful mempool to match against a compact block,
983 // and we don't feel like constructing the object for them, so
984 // instead we respond with the full, non-compact block.
985 bool fPeerWantsWitness = State(pfrom->GetId())->fWantsCmpctWitness;
986 int nSendFlags = fPeerWantsWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
987 if (CanDirectFetch(consensusParams) && mi->second->nHeight >= chainActive.Height() - MAX_CMPCTBLOCK_DEPTH) {
988 CBlockHeaderAndShortTxIDs cmpctblock(block, fPeerWantsWitness);
989 connman.PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
990 } else
991 connman.PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::BLOCK, block));
994 // Trigger the peer node to send a getblocks request for the next batch of inventory
995 if (inv.hash == pfrom->hashContinue)
997 // Bypass PushInventory, this must send even if redundant,
998 // and we want it right after the last block so they don't
999 // wait for other stuff first.
1000 vector<CInv> vInv;
1001 vInv.push_back(CInv(MSG_BLOCK, chainActive.Tip()->GetBlockHash()));
1002 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::INV, vInv));
1003 pfrom->hashContinue.SetNull();
1007 else if (inv.type == MSG_TX || inv.type == MSG_WITNESS_TX)
1009 // Send stream from relay memory
1010 bool push = false;
1011 auto mi = mapRelay.find(inv.hash);
1012 int nSendFlags = (inv.type == MSG_TX ? SERIALIZE_TRANSACTION_NO_WITNESS : 0);
1013 if (mi != mapRelay.end()) {
1014 connman.PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *mi->second));
1015 push = true;
1016 } else if (pfrom->timeLastMempoolReq) {
1017 auto txinfo = mempool.info(inv.hash);
1018 // To protect privacy, do not answer getdata using the mempool when
1019 // that TX couldn't have been INVed in reply to a MEMPOOL request.
1020 if (txinfo.tx && txinfo.nTime <= pfrom->timeLastMempoolReq) {
1021 connman.PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *txinfo.tx));
1022 push = true;
1025 if (!push) {
1026 vNotFound.push_back(inv);
1030 // Track requests for our stuff.
1031 GetMainSignals().Inventory(inv.hash);
1033 if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK || inv.type == MSG_CMPCT_BLOCK || inv.type == MSG_WITNESS_BLOCK)
1034 break;
1038 pfrom->vRecvGetData.erase(pfrom->vRecvGetData.begin(), it);
1040 if (!vNotFound.empty()) {
1041 // Let the peer know that we didn't find what it asked for, so it doesn't
1042 // have to wait around forever. Currently only SPV clients actually care
1043 // about this message: it's needed when they are recursively walking the
1044 // dependencies of relevant unconfirmed transactions. SPV clients want to
1045 // do that because they want to know about (and store and rebroadcast and
1046 // risk analyze) the dependencies of transactions relevant to them, without
1047 // having to download the entire memory pool.
1048 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::NOTFOUND, vNotFound));
1052 uint32_t GetFetchFlags(CNode* pfrom, CBlockIndex* pprev, const Consensus::Params& chainparams) {
1053 uint32_t nFetchFlags = 0;
1054 if ((pfrom->GetLocalServices() & NODE_WITNESS) && State(pfrom->GetId())->fHaveWitness) {
1055 nFetchFlags |= MSG_WITNESS_FLAG;
1057 return nFetchFlags;
1060 bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, int64_t nTimeReceived, const CChainParams& chainparams, CConnman& connman, std::atomic<bool>& interruptMsgProc)
1062 LogPrint("net", "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand), vRecv.size(), pfrom->id);
1063 if (IsArgSet("-dropmessagestest") && GetRand(GetArg("-dropmessagestest", 0)) == 0)
1065 LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n");
1066 return true;
1070 if (!(pfrom->GetLocalServices() & NODE_BLOOM) &&
1071 (strCommand == NetMsgType::FILTERLOAD ||
1072 strCommand == NetMsgType::FILTERADD))
1074 if (pfrom->nVersion >= NO_BLOOM_VERSION) {
1075 LOCK(cs_main);
1076 Misbehaving(pfrom->GetId(), 100);
1077 return false;
1078 } else {
1079 pfrom->fDisconnect = true;
1080 return false;
1085 if (strCommand == NetMsgType::VERSION)
1087 // Each connection can only send one version message
1088 if (pfrom->nVersion != 0)
1090 connman.PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_DUPLICATE, string("Duplicate version message")));
1091 LOCK(cs_main);
1092 Misbehaving(pfrom->GetId(), 1);
1093 return false;
1096 int64_t nTime;
1097 CAddress addrMe;
1098 CAddress addrFrom;
1099 uint64_t nNonce = 1;
1100 uint64_t nServiceInt;
1101 vRecv >> pfrom->nVersion >> nServiceInt >> nTime >> addrMe;
1102 pfrom->nServices = ServiceFlags(nServiceInt);
1103 if (!pfrom->fInbound)
1105 connman.SetServices(pfrom->addr, pfrom->nServices);
1107 if (pfrom->nServicesExpected & ~pfrom->nServices)
1109 LogPrint("net", "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom->id, pfrom->nServices, pfrom->nServicesExpected);
1110 connman.PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_NONSTANDARD,
1111 strprintf("Expected to offer services %08x", pfrom->nServicesExpected)));
1112 pfrom->fDisconnect = true;
1113 return false;
1116 if (pfrom->nVersion < MIN_PEER_PROTO_VERSION)
1118 // disconnect from peers older than this proto version
1119 LogPrintf("peer=%d using obsolete version %i; disconnecting\n", pfrom->id, pfrom->nVersion);
1120 connman.PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_OBSOLETE,
1121 strprintf("Version must be %d or greater", MIN_PEER_PROTO_VERSION)));
1122 pfrom->fDisconnect = true;
1123 return false;
1126 if (pfrom->nVersion == 10300)
1127 pfrom->nVersion = 300;
1128 if (!vRecv.empty())
1129 vRecv >> addrFrom >> nNonce;
1130 if (!vRecv.empty()) {
1131 vRecv >> LIMITED_STRING(pfrom->strSubVer, MAX_SUBVERSION_LENGTH);
1132 pfrom->cleanSubVer = SanitizeString(pfrom->strSubVer);
1134 if (!vRecv.empty()) {
1135 vRecv >> pfrom->nStartingHeight;
1138 LOCK(pfrom->cs_filter);
1139 if (!vRecv.empty())
1140 vRecv >> pfrom->fRelayTxes; // set to true after we get the first filter* message
1141 else
1142 pfrom->fRelayTxes = true;
1145 // Disconnect if we connected to ourself
1146 if (pfrom->fInbound && !connman.CheckIncomingNonce(nNonce))
1148 LogPrintf("connected to self at %s, disconnecting\n", pfrom->addr.ToString());
1149 pfrom->fDisconnect = true;
1150 return true;
1153 pfrom->addrLocal = addrMe;
1154 if (pfrom->fInbound && addrMe.IsRoutable())
1156 SeenLocal(addrMe);
1159 // Be shy and don't send version until we hear
1160 if (pfrom->fInbound)
1161 PushNodeVersion(pfrom, connman, GetAdjustedTime());
1163 pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);
1165 if((pfrom->nServices & NODE_WITNESS))
1167 LOCK(cs_main);
1168 State(pfrom->GetId())->fHaveWitness = true;
1171 // Potentially mark this peer as a preferred download peer.
1173 LOCK(cs_main);
1174 UpdatePreferredDownload(pfrom, State(pfrom->GetId()));
1177 // Change version
1178 connman.PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERACK));
1179 int nSendVersion = std::min(pfrom->nVersion, PROTOCOL_VERSION);
1180 pfrom->SetSendVersion(nSendVersion);
1182 if (!pfrom->fInbound)
1184 // Advertise our address
1185 if (fListen && !IsInitialBlockDownload())
1187 CAddress addr = GetLocalAddress(&pfrom->addr, pfrom->GetLocalServices());
1188 FastRandomContext insecure_rand;
1189 if (addr.IsRoutable())
1191 LogPrint("net", "ProcessMessages: advertising address %s\n", addr.ToString());
1192 pfrom->PushAddress(addr, insecure_rand);
1193 } else if (IsPeerAddrLocalGood(pfrom)) {
1194 addr.SetIP(pfrom->addrLocal);
1195 LogPrint("net", "ProcessMessages: advertising address %s\n", addr.ToString());
1196 pfrom->PushAddress(addr, insecure_rand);
1200 // Get recent addresses
1201 if (pfrom->fOneShot || pfrom->nVersion >= CADDR_TIME_VERSION || connman.GetAddressCount() < 1000)
1203 connman.PushMessage(pfrom, CNetMsgMaker(nSendVersion).Make(NetMsgType::GETADDR));
1204 pfrom->fGetAddr = true;
1206 connman.MarkAddressGood(pfrom->addr);
1209 pfrom->fSuccessfullyConnected = true;
1211 string remoteAddr;
1212 if (fLogIPs)
1213 remoteAddr = ", peeraddr=" + pfrom->addr.ToString();
1215 LogPrintf("receive version message: %s: version %d, blocks=%d, us=%s, peer=%d%s\n",
1216 pfrom->cleanSubVer, pfrom->nVersion,
1217 pfrom->nStartingHeight, addrMe.ToString(), pfrom->id,
1218 remoteAddr);
1220 int64_t nTimeOffset = nTime - GetTime();
1221 pfrom->nTimeOffset = nTimeOffset;
1222 AddTimeData(pfrom->addr, nTimeOffset);
1224 // Feeler connections exist only to verify if address is online.
1225 if (pfrom->fFeeler) {
1226 assert(pfrom->fInbound == false);
1227 pfrom->fDisconnect = true;
1229 return true;
1233 else if (pfrom->nVersion == 0)
1235 // Must have a version message before anything else
1236 LOCK(cs_main);
1237 Misbehaving(pfrom->GetId(), 1);
1238 return false;
1241 // At this point, the outgoing message serialization version can't change.
1242 CNetMsgMaker msgMaker(pfrom->GetSendVersion());
1244 if (strCommand == NetMsgType::VERACK)
1246 pfrom->SetRecvVersion(min(pfrom->nVersion, PROTOCOL_VERSION));
1248 if (!pfrom->fInbound) {
1249 // Mark this node as currently connected, so we update its timestamp later.
1250 LOCK(cs_main);
1251 State(pfrom->GetId())->fCurrentlyConnected = true;
1254 if (pfrom->nVersion >= SENDHEADERS_VERSION) {
1255 // Tell our peer we prefer to receive headers rather than inv's
1256 // We send this to non-NODE NETWORK peers as well, because even
1257 // non-NODE NETWORK peers can announce blocks (such as pruning
1258 // nodes)
1259 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::SENDHEADERS));
1261 if (pfrom->nVersion >= SHORT_IDS_BLOCKS_VERSION) {
1262 // Tell our peer we are willing to provide version 1 or 2 cmpctblocks
1263 // However, we do not request new block announcements using
1264 // cmpctblock messages.
1265 // We send this to non-NODE NETWORK peers as well, because
1266 // they may wish to request compact blocks from us
1267 bool fAnnounceUsingCMPCTBLOCK = false;
1268 uint64_t nCMPCTBLOCKVersion = 2;
1269 if (pfrom->GetLocalServices() & NODE_WITNESS)
1270 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
1271 nCMPCTBLOCKVersion = 1;
1272 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
1277 else if (strCommand == NetMsgType::ADDR)
1279 vector<CAddress> vAddr;
1280 vRecv >> vAddr;
1282 // Don't want addr from older versions unless seeding
1283 if (pfrom->nVersion < CADDR_TIME_VERSION && connman.GetAddressCount() > 1000)
1284 return true;
1285 if (vAddr.size() > 1000)
1287 LOCK(cs_main);
1288 Misbehaving(pfrom->GetId(), 20);
1289 return error("message addr size() = %u", vAddr.size());
1292 // Store the new addresses
1293 vector<CAddress> vAddrOk;
1294 int64_t nNow = GetAdjustedTime();
1295 int64_t nSince = nNow - 10 * 60;
1296 BOOST_FOREACH(CAddress& addr, vAddr)
1298 if (interruptMsgProc)
1299 return true;
1301 if ((addr.nServices & REQUIRED_SERVICES) != REQUIRED_SERVICES)
1302 continue;
1304 if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
1305 addr.nTime = nNow - 5 * 24 * 60 * 60;
1306 pfrom->AddAddressKnown(addr);
1307 bool fReachable = IsReachable(addr);
1308 if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
1310 // Relay to a limited number of other nodes
1311 RelayAddress(addr, fReachable, connman);
1313 // Do not store addresses outside our network
1314 if (fReachable)
1315 vAddrOk.push_back(addr);
1317 connman.AddNewAddresses(vAddrOk, pfrom->addr, 2 * 60 * 60);
1318 if (vAddr.size() < 1000)
1319 pfrom->fGetAddr = false;
1320 if (pfrom->fOneShot)
1321 pfrom->fDisconnect = true;
1324 else if (strCommand == NetMsgType::SENDHEADERS)
1326 LOCK(cs_main);
1327 State(pfrom->GetId())->fPreferHeaders = true;
1330 else if (strCommand == NetMsgType::SENDCMPCT)
1332 bool fAnnounceUsingCMPCTBLOCK = false;
1333 uint64_t nCMPCTBLOCKVersion = 0;
1334 vRecv >> fAnnounceUsingCMPCTBLOCK >> nCMPCTBLOCKVersion;
1335 if (nCMPCTBLOCKVersion == 1 || ((pfrom->GetLocalServices() & NODE_WITNESS) && nCMPCTBLOCKVersion == 2)) {
1336 LOCK(cs_main);
1337 // fProvidesHeaderAndIDs is used to "lock in" version of compact blocks we send (fWantsCmpctWitness)
1338 if (!State(pfrom->GetId())->fProvidesHeaderAndIDs) {
1339 State(pfrom->GetId())->fProvidesHeaderAndIDs = true;
1340 State(pfrom->GetId())->fWantsCmpctWitness = nCMPCTBLOCKVersion == 2;
1342 if (State(pfrom->GetId())->fWantsCmpctWitness == (nCMPCTBLOCKVersion == 2)) // ignore later version announces
1343 State(pfrom->GetId())->fPreferHeaderAndIDs = fAnnounceUsingCMPCTBLOCK;
1344 if (!State(pfrom->GetId())->fSupportsDesiredCmpctVersion) {
1345 if (pfrom->GetLocalServices() & NODE_WITNESS)
1346 State(pfrom->GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 2);
1347 else
1348 State(pfrom->GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 1);
1354 else if (strCommand == NetMsgType::INV)
1356 vector<CInv> vInv;
1357 vRecv >> vInv;
1358 if (vInv.size() > MAX_INV_SZ)
1360 LOCK(cs_main);
1361 Misbehaving(pfrom->GetId(), 20);
1362 return error("message inv size() = %u", vInv.size());
1365 bool fBlocksOnly = !fRelayTxes;
1367 // Allow whitelisted peers to send data other than blocks in blocks only mode if whitelistrelay is true
1368 if (pfrom->fWhitelisted && GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY))
1369 fBlocksOnly = false;
1371 LOCK(cs_main);
1373 uint32_t nFetchFlags = GetFetchFlags(pfrom, chainActive.Tip(), chainparams.GetConsensus());
1375 std::vector<CInv> vToFetch;
1377 for (unsigned int nInv = 0; nInv < vInv.size(); nInv++)
1379 CInv &inv = vInv[nInv];
1381 if (interruptMsgProc)
1382 return true;
1384 bool fAlreadyHave = AlreadyHave(inv);
1385 LogPrint("net", "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom->id);
1387 if (inv.type == MSG_TX) {
1388 inv.type |= nFetchFlags;
1391 if (inv.type == MSG_BLOCK) {
1392 UpdateBlockAvailability(pfrom->GetId(), inv.hash);
1393 if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count(inv.hash)) {
1394 // We used to request the full block here, but since headers-announcements are now the
1395 // primary method of announcement on the network, and since, in the case that a node
1396 // fell back to inv we probably have a reorg which we should get the headers for first,
1397 // we now only provide a getheaders response here. When we receive the headers, we will
1398 // then ask for the blocks we need.
1399 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), inv.hash));
1400 LogPrint("net", "getheaders (%d) %s to peer=%d\n", pindexBestHeader->nHeight, inv.hash.ToString(), pfrom->id);
1403 else
1405 pfrom->AddInventoryKnown(inv);
1406 if (fBlocksOnly)
1407 LogPrint("net", "transaction (%s) inv sent in violation of protocol peer=%d\n", inv.hash.ToString(), pfrom->id);
1408 else if (!fAlreadyHave && !fImporting && !fReindex && !IsInitialBlockDownload())
1409 pfrom->AskFor(inv);
1412 // Track requests for our stuff
1413 GetMainSignals().Inventory(inv.hash);
1416 if (!vToFetch.empty())
1417 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vToFetch));
1421 else if (strCommand == NetMsgType::GETDATA)
1423 vector<CInv> vInv;
1424 vRecv >> vInv;
1425 if (vInv.size() > MAX_INV_SZ)
1427 LOCK(cs_main);
1428 Misbehaving(pfrom->GetId(), 20);
1429 return error("message getdata size() = %u", vInv.size());
1432 if (fDebug || (vInv.size() != 1))
1433 LogPrint("net", "received getdata (%u invsz) peer=%d\n", vInv.size(), pfrom->id);
1435 if ((fDebug && vInv.size() > 0) || (vInv.size() == 1))
1436 LogPrint("net", "received getdata for: %s peer=%d\n", vInv[0].ToString(), pfrom->id);
1438 pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end());
1439 ProcessGetData(pfrom, chainparams.GetConsensus(), connman, interruptMsgProc);
1443 else if (strCommand == NetMsgType::GETBLOCKS)
1445 CBlockLocator locator;
1446 uint256 hashStop;
1447 vRecv >> locator >> hashStop;
1449 LOCK(cs_main);
1451 // Find the last block the caller has in the main chain
1452 CBlockIndex* pindex = FindForkInGlobalIndex(chainActive, locator);
1454 // Send the rest of the chain
1455 if (pindex)
1456 pindex = chainActive.Next(pindex);
1457 int nLimit = 500;
1458 LogPrint("net", "getblocks %d to %s limit %d from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), nLimit, pfrom->id);
1459 for (; pindex; pindex = chainActive.Next(pindex))
1461 if (pindex->GetBlockHash() == hashStop)
1463 LogPrint("net", " getblocks stopping at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
1464 break;
1466 // If pruning, don't inv blocks unless we have on disk and are likely to still have
1467 // for some reasonable time window (1 hour) that block relay might require.
1468 const int nPrunedBlocksLikelyToHave = MIN_BLOCKS_TO_KEEP - 3600 / chainparams.GetConsensus().nPowTargetSpacing;
1469 if (fPruneMode && (!(pindex->nStatus & BLOCK_HAVE_DATA) || pindex->nHeight <= chainActive.Tip()->nHeight - nPrunedBlocksLikelyToHave))
1471 LogPrint("net", " getblocks stopping, pruned or too old block at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
1472 break;
1474 pfrom->PushInventory(CInv(MSG_BLOCK, pindex->GetBlockHash()));
1475 if (--nLimit <= 0)
1477 // When this block is requested, we'll send an inv that'll
1478 // trigger the peer to getblocks the next batch of inventory.
1479 LogPrint("net", " getblocks stopping at limit %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
1480 pfrom->hashContinue = pindex->GetBlockHash();
1481 break;
1487 else if (strCommand == NetMsgType::GETBLOCKTXN)
1489 BlockTransactionsRequest req;
1490 vRecv >> req;
1492 LOCK(cs_main);
1494 BlockMap::iterator it = mapBlockIndex.find(req.blockhash);
1495 if (it == mapBlockIndex.end() || !(it->second->nStatus & BLOCK_HAVE_DATA)) {
1496 LogPrintf("Peer %d sent us a getblocktxn for a block we don't have", pfrom->id);
1497 return true;
1500 if (it->second->nHeight < chainActive.Height() - MAX_BLOCKTXN_DEPTH) {
1501 // If an older block is requested (should never happen in practice,
1502 // but can happen in tests) send a block response instead of a
1503 // blocktxn response. Sending a full block response instead of a
1504 // small blocktxn response is preferable in the case where a peer
1505 // might maliciously send lots of getblocktxn requests to trigger
1506 // expensive disk reads, because it will require the peer to
1507 // actually receive all the data read from disk over the network.
1508 LogPrint("net", "Peer %d sent us a getblocktxn for a block > %i deep", pfrom->id, MAX_BLOCKTXN_DEPTH);
1509 CInv inv;
1510 inv.type = State(pfrom->GetId())->fWantsCmpctWitness ? MSG_WITNESS_BLOCK : MSG_BLOCK;
1511 inv.hash = req.blockhash;
1512 pfrom->vRecvGetData.push_back(inv);
1513 ProcessGetData(pfrom, chainparams.GetConsensus(), connman, interruptMsgProc);
1514 return true;
1517 CBlock block;
1518 bool ret = ReadBlockFromDisk(block, it->second, chainparams.GetConsensus());
1519 assert(ret);
1521 BlockTransactions resp(req);
1522 for (size_t i = 0; i < req.indexes.size(); i++) {
1523 if (req.indexes[i] >= block.vtx.size()) {
1524 Misbehaving(pfrom->GetId(), 100);
1525 LogPrintf("Peer %d sent us a getblocktxn with out-of-bounds tx indices", pfrom->id);
1526 return true;
1528 resp.txn[i] = block.vtx[req.indexes[i]];
1530 int nSendFlags = State(pfrom->GetId())->fWantsCmpctWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
1531 connman.PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::BLOCKTXN, resp));
1535 else if (strCommand == NetMsgType::GETHEADERS)
1537 CBlockLocator locator;
1538 uint256 hashStop;
1539 vRecv >> locator >> hashStop;
1541 LOCK(cs_main);
1542 if (IsInitialBlockDownload() && !pfrom->fWhitelisted) {
1543 LogPrint("net", "Ignoring getheaders from peer=%d because node is in initial block download\n", pfrom->id);
1544 return true;
1547 CNodeState *nodestate = State(pfrom->GetId());
1548 CBlockIndex* pindex = NULL;
1549 if (locator.IsNull())
1551 // If locator is null, return the hashStop block
1552 BlockMap::iterator mi = mapBlockIndex.find(hashStop);
1553 if (mi == mapBlockIndex.end())
1554 return true;
1555 pindex = (*mi).second;
1557 else
1559 // Find the last block the caller has in the main chain
1560 pindex = FindForkInGlobalIndex(chainActive, locator);
1561 if (pindex)
1562 pindex = chainActive.Next(pindex);
1565 // we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
1566 vector<CBlock> vHeaders;
1567 int nLimit = MAX_HEADERS_RESULTS;
1568 LogPrint("net", "getheaders %d to %s from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), pfrom->id);
1569 for (; pindex; pindex = chainActive.Next(pindex))
1571 vHeaders.push_back(pindex->GetBlockHeader());
1572 if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
1573 break;
1575 // pindex can be NULL either if we sent chainActive.Tip() OR
1576 // if our peer has chainActive.Tip() (and thus we are sending an empty
1577 // headers message). In both cases it's safe to update
1578 // pindexBestHeaderSent to be our tip.
1579 nodestate->pindexBestHeaderSent = pindex ? pindex : chainActive.Tip();
1580 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
1584 else if (strCommand == NetMsgType::TX)
1586 // Stop processing the transaction early if
1587 // We are in blocks only mode and peer is either not whitelisted or whitelistrelay is off
1588 if (!fRelayTxes && (!pfrom->fWhitelisted || !GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY)))
1590 LogPrint("net", "transaction sent in violation of protocol peer=%d\n", pfrom->id);
1591 return true;
1594 deque<COutPoint> vWorkQueue;
1595 vector<uint256> vEraseQueue;
1596 CTransactionRef ptx;
1597 vRecv >> ptx;
1598 const CTransaction& tx = *ptx;
1600 CInv inv(MSG_TX, tx.GetHash());
1601 pfrom->AddInventoryKnown(inv);
1603 LOCK(cs_main);
1605 bool fMissingInputs = false;
1606 CValidationState state;
1608 pfrom->setAskFor.erase(inv.hash);
1609 mapAlreadyAskedFor.erase(inv.hash);
1611 if (!AlreadyHave(inv) && AcceptToMemoryPool(mempool, state, ptx, true, &fMissingInputs)) {
1612 mempool.check(pcoinsTip);
1613 RelayTransaction(tx, connman);
1614 for (unsigned int i = 0; i < tx.vout.size(); i++) {
1615 vWorkQueue.emplace_back(inv.hash, i);
1618 pfrom->nLastTXTime = GetTime();
1620 LogPrint("mempool", "AcceptToMemoryPool: peer=%d: accepted %s (poolsz %u txn, %u kB)\n",
1621 pfrom->id,
1622 tx.GetHash().ToString(),
1623 mempool.size(), mempool.DynamicMemoryUsage() / 1000);
1625 // Recursively process any orphan transactions that depended on this one
1626 set<NodeId> setMisbehaving;
1627 while (!vWorkQueue.empty()) {
1628 auto itByPrev = mapOrphanTransactionsByPrev.find(vWorkQueue.front());
1629 vWorkQueue.pop_front();
1630 if (itByPrev == mapOrphanTransactionsByPrev.end())
1631 continue;
1632 for (auto mi = itByPrev->second.begin();
1633 mi != itByPrev->second.end();
1634 ++mi)
1636 const CTransactionRef& porphanTx = (*mi)->second.tx;
1637 const CTransaction& orphanTx = *porphanTx;
1638 const uint256& orphanHash = orphanTx.GetHash();
1639 NodeId fromPeer = (*mi)->second.fromPeer;
1640 bool fMissingInputs2 = false;
1641 // Use a dummy CValidationState so someone can't setup nodes to counter-DoS based on orphan
1642 // resolution (that is, feeding people an invalid transaction based on LegitTxX in order to get
1643 // anyone relaying LegitTxX banned)
1644 CValidationState stateDummy;
1647 if (setMisbehaving.count(fromPeer))
1648 continue;
1649 if (AcceptToMemoryPool(mempool, stateDummy, porphanTx, true, &fMissingInputs2)) {
1650 LogPrint("mempool", " accepted orphan tx %s\n", orphanHash.ToString());
1651 RelayTransaction(orphanTx, connman);
1652 for (unsigned int i = 0; i < orphanTx.vout.size(); i++) {
1653 vWorkQueue.emplace_back(orphanHash, i);
1655 vEraseQueue.push_back(orphanHash);
1657 else if (!fMissingInputs2)
1659 int nDos = 0;
1660 if (stateDummy.IsInvalid(nDos) && nDos > 0)
1662 // Punish peer that gave us an invalid orphan tx
1663 Misbehaving(fromPeer, nDos);
1664 setMisbehaving.insert(fromPeer);
1665 LogPrint("mempool", " invalid orphan tx %s\n", orphanHash.ToString());
1667 // Has inputs but not accepted to mempool
1668 // Probably non-standard or insufficient fee/priority
1669 LogPrint("mempool", " removed orphan tx %s\n", orphanHash.ToString());
1670 vEraseQueue.push_back(orphanHash);
1671 if (!orphanTx.HasWitness() && !stateDummy.CorruptionPossible()) {
1672 // Do not use rejection cache for witness transactions or
1673 // witness-stripped transactions, as they can have been malleated.
1674 // See https://github.com/bitcoin/bitcoin/issues/8279 for details.
1675 assert(recentRejects);
1676 recentRejects->insert(orphanHash);
1679 mempool.check(pcoinsTip);
1683 BOOST_FOREACH(uint256 hash, vEraseQueue)
1684 EraseOrphanTx(hash);
1686 else if (fMissingInputs)
1688 bool fRejectedParents = false; // It may be the case that the orphans parents have all been rejected
1689 BOOST_FOREACH(const CTxIn& txin, tx.vin) {
1690 if (recentRejects->contains(txin.prevout.hash)) {
1691 fRejectedParents = true;
1692 break;
1695 if (!fRejectedParents) {
1696 uint32_t nFetchFlags = GetFetchFlags(pfrom, chainActive.Tip(), chainparams.GetConsensus());
1697 BOOST_FOREACH(const CTxIn& txin, tx.vin) {
1698 CInv _inv(MSG_TX | nFetchFlags, txin.prevout.hash);
1699 pfrom->AddInventoryKnown(_inv);
1700 if (!AlreadyHave(_inv)) pfrom->AskFor(_inv);
1702 AddOrphanTx(ptx, pfrom->GetId());
1704 // DoS prevention: do not allow mapOrphanTransactions to grow unbounded
1705 unsigned int nMaxOrphanTx = (unsigned int)std::max((int64_t)0, GetArg("-maxorphantx", DEFAULT_MAX_ORPHAN_TRANSACTIONS));
1706 unsigned int nEvicted = LimitOrphanTxSize(nMaxOrphanTx);
1707 if (nEvicted > 0)
1708 LogPrint("mempool", "mapOrphan overflow, removed %u tx\n", nEvicted);
1709 } else {
1710 LogPrint("mempool", "not keeping orphan with rejected parents %s\n",tx.GetHash().ToString());
1712 } else {
1713 if (!tx.HasWitness() && !state.CorruptionPossible()) {
1714 // Do not use rejection cache for witness transactions or
1715 // witness-stripped transactions, as they can have been malleated.
1716 // See https://github.com/bitcoin/bitcoin/issues/8279 for details.
1717 assert(recentRejects);
1718 recentRejects->insert(tx.GetHash());
1721 if (pfrom->fWhitelisted && GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY)) {
1722 // Always relay transactions received from whitelisted peers, even
1723 // if they were already in the mempool or rejected from it due
1724 // to policy, allowing the node to function as a gateway for
1725 // nodes hidden behind it.
1727 // Never relay transactions that we would assign a non-zero DoS
1728 // score for, as we expect peers to do the same with us in that
1729 // case.
1730 int nDoS = 0;
1731 if (!state.IsInvalid(nDoS) || nDoS == 0) {
1732 LogPrintf("Force relaying tx %s from whitelisted peer=%d\n", tx.GetHash().ToString(), pfrom->id);
1733 RelayTransaction(tx, connman);
1734 } else {
1735 LogPrintf("Not relaying invalid transaction %s from whitelisted peer=%d (%s)\n", tx.GetHash().ToString(), pfrom->id, FormatStateMessage(state));
1739 int nDoS = 0;
1740 if (state.IsInvalid(nDoS))
1742 LogPrint("mempoolrej", "%s from peer=%d was not accepted: %s\n", tx.GetHash().ToString(),
1743 pfrom->id,
1744 FormatStateMessage(state));
1745 if (state.GetRejectCode() < REJECT_INTERNAL) // Never send AcceptToMemoryPool's internal codes over P2P
1746 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::REJECT, strCommand, (unsigned char)state.GetRejectCode(),
1747 state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), inv.hash));
1748 if (nDoS > 0) {
1749 Misbehaving(pfrom->GetId(), nDoS);
1755 else if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
1757 CBlockHeaderAndShortTxIDs cmpctblock;
1758 vRecv >> cmpctblock;
1761 LOCK(cs_main);
1763 if (mapBlockIndex.find(cmpctblock.header.hashPrevBlock) == mapBlockIndex.end()) {
1764 // Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
1765 if (!IsInitialBlockDownload())
1766 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), uint256()));
1767 return true;
1771 CBlockIndex *pindex = NULL;
1772 CValidationState state;
1773 if (!ProcessNewBlockHeaders({cmpctblock.header}, state, chainparams, &pindex)) {
1774 int nDoS;
1775 if (state.IsInvalid(nDoS)) {
1776 if (nDoS > 0) {
1777 LOCK(cs_main);
1778 Misbehaving(pfrom->GetId(), nDoS);
1780 LogPrintf("Peer %d sent us invalid header via cmpctblock\n", pfrom->id);
1781 return true;
1785 // When we succeed in decoding a block's txids from a cmpctblock
1786 // message we typically jump to the BLOCKTXN handling code, with a
1787 // dummy (empty) BLOCKTXN message, to re-use the logic there in
1788 // completing processing of the putative block (without cs_main).
1789 bool fProcessBLOCKTXN = false;
1790 CDataStream blockTxnMsg(SER_NETWORK, PROTOCOL_VERSION);
1792 // If we end up treating this as a plain headers message, call that as well
1793 // without cs_main.
1794 bool fRevertToHeaderProcessing = false;
1795 CDataStream vHeadersMsg(SER_NETWORK, PROTOCOL_VERSION);
1797 // Keep a CBlock for "optimistic" compactblock reconstructions (see
1798 // below)
1799 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
1800 bool fBlockReconstructed = false;
1803 LOCK(cs_main);
1804 // If AcceptBlockHeader returned true, it set pindex
1805 assert(pindex);
1806 UpdateBlockAvailability(pfrom->GetId(), pindex->GetBlockHash());
1808 std::map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator blockInFlightIt = mapBlocksInFlight.find(pindex->GetBlockHash());
1809 bool fAlreadyInFlight = blockInFlightIt != mapBlocksInFlight.end();
1811 if (pindex->nStatus & BLOCK_HAVE_DATA) // Nothing to do here
1812 return true;
1814 if (pindex->nChainWork <= chainActive.Tip()->nChainWork || // We know something better
1815 pindex->nTx != 0) { // We had this block at some point, but pruned it
1816 if (fAlreadyInFlight) {
1817 // We requested this block for some reason, but our mempool will probably be useless
1818 // so we just grab the block via normal getdata
1819 std::vector<CInv> vInv(1);
1820 vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom, pindex->pprev, chainparams.GetConsensus()), cmpctblock.header.GetHash());
1821 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
1823 return true;
1826 // If we're not close to tip yet, give up and let parallel block fetch work its magic
1827 if (!fAlreadyInFlight && !CanDirectFetch(chainparams.GetConsensus()))
1828 return true;
1830 CNodeState *nodestate = State(pfrom->GetId());
1832 if (IsWitnessEnabled(pindex->pprev, chainparams.GetConsensus()) && !nodestate->fSupportsDesiredCmpctVersion) {
1833 // Don't bother trying to process compact blocks from v1 peers
1834 // after segwit activates.
1835 return true;
1838 // We want to be a bit conservative just to be extra careful about DoS
1839 // possibilities in compact block processing...
1840 if (pindex->nHeight <= chainActive.Height() + 2) {
1841 if ((!fAlreadyInFlight && nodestate->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) ||
1842 (fAlreadyInFlight && blockInFlightIt->second.first == pfrom->GetId())) {
1843 list<QueuedBlock>::iterator *queuedBlockIt = NULL;
1844 if (!MarkBlockAsInFlight(pfrom->GetId(), pindex->GetBlockHash(), chainparams.GetConsensus(), pindex, &queuedBlockIt)) {
1845 if (!(*queuedBlockIt)->partialBlock)
1846 (*queuedBlockIt)->partialBlock.reset(new PartiallyDownloadedBlock(&mempool));
1847 else {
1848 // The block was already in flight using compact blocks from the same peer
1849 LogPrint("net", "Peer sent us compact block we were already syncing!\n");
1850 return true;
1854 PartiallyDownloadedBlock& partialBlock = *(*queuedBlockIt)->partialBlock;
1855 ReadStatus status = partialBlock.InitData(cmpctblock);
1856 if (status == READ_STATUS_INVALID) {
1857 MarkBlockAsReceived(pindex->GetBlockHash()); // Reset in-flight state in case of whitelist
1858 Misbehaving(pfrom->GetId(), 100);
1859 LogPrintf("Peer %d sent us invalid compact block\n", pfrom->id);
1860 return true;
1861 } else if (status == READ_STATUS_FAILED) {
1862 // Duplicate txindexes, the block is now in-flight, so just request it
1863 std::vector<CInv> vInv(1);
1864 vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom, pindex->pprev, chainparams.GetConsensus()), cmpctblock.header.GetHash());
1865 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
1866 return true;
1869 if (!fAlreadyInFlight && mapBlocksInFlight.size() == 1 && pindex->pprev->IsValid(BLOCK_VALID_CHAIN)) {
1870 // We seem to be rather well-synced, so it appears pfrom was the first to provide us
1871 // with this block! Let's get them to announce using compact blocks in the future.
1872 MaybeSetPeerAsAnnouncingHeaderAndIDs(nodestate, pfrom, connman);
1875 BlockTransactionsRequest req;
1876 for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
1877 if (!partialBlock.IsTxAvailable(i))
1878 req.indexes.push_back(i);
1880 if (req.indexes.empty()) {
1881 // Dirty hack to jump to BLOCKTXN code (TODO: move message handling into their own functions)
1882 BlockTransactions txn;
1883 txn.blockhash = cmpctblock.header.GetHash();
1884 blockTxnMsg << txn;
1885 fProcessBLOCKTXN = true;
1886 } else {
1887 req.blockhash = pindex->GetBlockHash();
1888 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETBLOCKTXN, req));
1890 } else {
1891 // This block is either already in flight from a different
1892 // peer, or this peer has too many blocks outstanding to
1893 // download from.
1894 // Optimistically try to reconstruct anyway since we might be
1895 // able to without any round trips.
1896 PartiallyDownloadedBlock tempBlock(&mempool);
1897 ReadStatus status = tempBlock.InitData(cmpctblock);
1898 if (status != READ_STATUS_OK) {
1899 // TODO: don't ignore failures
1900 return true;
1902 std::vector<CTransactionRef> dummy;
1903 status = tempBlock.FillBlock(*pblock, dummy);
1904 if (status == READ_STATUS_OK) {
1905 fBlockReconstructed = true;
1908 } else {
1909 if (fAlreadyInFlight) {
1910 // We requested this block, but its far into the future, so our
1911 // mempool will probably be useless - request the block normally
1912 std::vector<CInv> vInv(1);
1913 vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom, pindex->pprev, chainparams.GetConsensus()), cmpctblock.header.GetHash());
1914 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
1915 return true;
1916 } else {
1917 // If this was an announce-cmpctblock, we want the same treatment as a header message
1918 // Dirty hack to process as if it were just a headers message (TODO: move message handling into their own functions)
1919 std::vector<CBlock> headers;
1920 headers.push_back(cmpctblock.header);
1921 vHeadersMsg << headers;
1922 fRevertToHeaderProcessing = true;
1925 } // cs_main
1927 if (fProcessBLOCKTXN)
1928 return ProcessMessage(pfrom, NetMsgType::BLOCKTXN, blockTxnMsg, nTimeReceived, chainparams, connman, interruptMsgProc);
1930 if (fRevertToHeaderProcessing)
1931 return ProcessMessage(pfrom, NetMsgType::HEADERS, vHeadersMsg, nTimeReceived, chainparams, connman, interruptMsgProc);
1933 if (fBlockReconstructed) {
1934 // If we got here, we were able to optimistically reconstruct a
1935 // block that is in flight from some other peer.
1937 LOCK(cs_main);
1938 mapBlockSource.emplace(pblock->GetHash(), std::make_pair(pfrom->GetId(), false));
1940 bool fNewBlock = false;
1941 ProcessNewBlock(chainparams, pblock, true, &fNewBlock);
1942 if (fNewBlock)
1943 pfrom->nLastBlockTime = GetTime();
1945 LOCK(cs_main); // hold cs_main for CBlockIndex::IsValid()
1946 if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS)) {
1947 // Clear download state for this block, which is in
1948 // process from some other peer. We do this after calling
1949 // ProcessNewBlock so that a malleated cmpctblock announcement
1950 // can't be used to interfere with block relay.
1951 MarkBlockAsReceived(pblock->GetHash());
1957 else if (strCommand == NetMsgType::BLOCKTXN && !fImporting && !fReindex) // Ignore blocks received while importing
1959 BlockTransactions resp;
1960 vRecv >> resp;
1962 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
1963 bool fBlockRead = false;
1965 LOCK(cs_main);
1967 map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator it = mapBlocksInFlight.find(resp.blockhash);
1968 if (it == mapBlocksInFlight.end() || !it->second.second->partialBlock ||
1969 it->second.first != pfrom->GetId()) {
1970 LogPrint("net", "Peer %d sent us block transactions for block we weren't expecting\n", pfrom->id);
1971 return true;
1974 PartiallyDownloadedBlock& partialBlock = *it->second.second->partialBlock;
1975 ReadStatus status = partialBlock.FillBlock(*pblock, resp.txn);
1976 if (status == READ_STATUS_INVALID) {
1977 MarkBlockAsReceived(resp.blockhash); // Reset in-flight state in case of whitelist
1978 Misbehaving(pfrom->GetId(), 100);
1979 LogPrintf("Peer %d sent us invalid compact block/non-matching block transactions\n", pfrom->id);
1980 return true;
1981 } else if (status == READ_STATUS_FAILED) {
1982 // Might have collided, fall back to getdata now :(
1983 std::vector<CInv> invs;
1984 invs.push_back(CInv(MSG_BLOCK | GetFetchFlags(pfrom, chainActive.Tip(), chainparams.GetConsensus()), resp.blockhash));
1985 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, invs));
1986 } else {
1987 // Block is either okay, or possibly we received
1988 // READ_STATUS_CHECKBLOCK_FAILED.
1989 // Note that CheckBlock can only fail for one of a few reasons:
1990 // 1. bad-proof-of-work (impossible here, because we've already
1991 // accepted the header)
1992 // 2. merkleroot doesn't match the transactions given (already
1993 // caught in FillBlock with READ_STATUS_FAILED, so
1994 // impossible here)
1995 // 3. the block is otherwise invalid (eg invalid coinbase,
1996 // block is too big, too many legacy sigops, etc).
1997 // So if CheckBlock failed, #3 is the only possibility.
1998 // Under BIP 152, we don't DoS-ban unless proof of work is
1999 // invalid (we don't require all the stateless checks to have
2000 // been run). This is handled below, so just treat this as
2001 // though the block was successfully read, and rely on the
2002 // handling in ProcessNewBlock to ensure the block index is
2003 // updated, reject messages go out, etc.
2004 MarkBlockAsReceived(resp.blockhash); // it is now an empty pointer
2005 fBlockRead = true;
2006 // mapBlockSource is only used for sending reject messages and DoS scores,
2007 // so the race between here and cs_main in ProcessNewBlock is fine.
2008 // BIP 152 permits peers to relay compact blocks after validating
2009 // the header only; we should not punish peers if the block turns
2010 // out to be invalid.
2011 mapBlockSource.emplace(resp.blockhash, std::make_pair(pfrom->GetId(), false));
2013 } // Don't hold cs_main when we call into ProcessNewBlock
2014 if (fBlockRead) {
2015 bool fNewBlock = false;
2016 // Since we requested this block (it was in mapBlocksInFlight), force it to be processed,
2017 // even if it would not be a candidate for new tip (missing previous block, chain not long enough, etc)
2018 ProcessNewBlock(chainparams, pblock, true, &fNewBlock);
2019 if (fNewBlock)
2020 pfrom->nLastBlockTime = GetTime();
2025 else if (strCommand == NetMsgType::HEADERS && !fImporting && !fReindex) // Ignore headers received while importing
2027 std::vector<CBlockHeader> headers;
2029 // Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks.
2030 unsigned int nCount = ReadCompactSize(vRecv);
2031 if (nCount > MAX_HEADERS_RESULTS) {
2032 LOCK(cs_main);
2033 Misbehaving(pfrom->GetId(), 20);
2034 return error("headers message size = %u", nCount);
2036 headers.resize(nCount);
2037 for (unsigned int n = 0; n < nCount; n++) {
2038 vRecv >> headers[n];
2039 ReadCompactSize(vRecv); // ignore tx count; assume it is 0.
2042 if (nCount == 0) {
2043 // Nothing interesting. Stop asking this peers for more headers.
2044 return true;
2047 CBlockIndex *pindexLast = NULL;
2049 LOCK(cs_main);
2050 CNodeState *nodestate = State(pfrom->GetId());
2052 // If this looks like it could be a block announcement (nCount <
2053 // MAX_BLOCKS_TO_ANNOUNCE), use special logic for handling headers that
2054 // don't connect:
2055 // - Send a getheaders message in response to try to connect the chain.
2056 // - The peer can send up to MAX_UNCONNECTING_HEADERS in a row that
2057 // don't connect before giving DoS points
2058 // - Once a headers message is received that is valid and does connect,
2059 // nUnconnectingHeaders gets reset back to 0.
2060 if (mapBlockIndex.find(headers[0].hashPrevBlock) == mapBlockIndex.end() && nCount < MAX_BLOCKS_TO_ANNOUNCE) {
2061 nodestate->nUnconnectingHeaders++;
2062 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), uint256()));
2063 LogPrint("net", "received header %s: missing prev block %s, sending getheaders (%d) to end (peer=%d, nUnconnectingHeaders=%d)\n",
2064 headers[0].GetHash().ToString(),
2065 headers[0].hashPrevBlock.ToString(),
2066 pindexBestHeader->nHeight,
2067 pfrom->id, nodestate->nUnconnectingHeaders);
2068 // Set hashLastUnknownBlock for this peer, so that if we
2069 // eventually get the headers - even from a different peer -
2070 // we can use this peer to download.
2071 UpdateBlockAvailability(pfrom->GetId(), headers.back().GetHash());
2073 if (nodestate->nUnconnectingHeaders % MAX_UNCONNECTING_HEADERS == 0) {
2074 Misbehaving(pfrom->GetId(), 20);
2076 return true;
2079 uint256 hashLastBlock;
2080 for (const CBlockHeader& header : headers) {
2081 if (!hashLastBlock.IsNull() && header.hashPrevBlock != hashLastBlock) {
2082 Misbehaving(pfrom->GetId(), 20);
2083 return error("non-continuous headers sequence");
2085 hashLastBlock = header.GetHash();
2089 CValidationState state;
2090 if (!ProcessNewBlockHeaders(headers, state, chainparams, &pindexLast)) {
2091 int nDoS;
2092 if (state.IsInvalid(nDoS)) {
2093 if (nDoS > 0) {
2094 LOCK(cs_main);
2095 Misbehaving(pfrom->GetId(), nDoS);
2097 return error("invalid header received");
2102 LOCK(cs_main);
2103 CNodeState *nodestate = State(pfrom->GetId());
2104 if (nodestate->nUnconnectingHeaders > 0) {
2105 LogPrint("net", "peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n", pfrom->id, nodestate->nUnconnectingHeaders);
2107 nodestate->nUnconnectingHeaders = 0;
2109 assert(pindexLast);
2110 UpdateBlockAvailability(pfrom->GetId(), pindexLast->GetBlockHash());
2112 if (nCount == MAX_HEADERS_RESULTS) {
2113 // Headers message had its maximum size; the peer may have more headers.
2114 // TODO: optimize: if pindexLast is an ancestor of chainActive.Tip or pindexBestHeader, continue
2115 // from there instead.
2116 LogPrint("net", "more getheaders (%d) to end to peer=%d (startheight:%d)\n", pindexLast->nHeight, pfrom->id, pfrom->nStartingHeight);
2117 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexLast), uint256()));
2120 bool fCanDirectFetch = CanDirectFetch(chainparams.GetConsensus());
2121 // If this set of headers is valid and ends in a block with at least as
2122 // much work as our tip, download as much as possible.
2123 if (fCanDirectFetch && pindexLast->IsValid(BLOCK_VALID_TREE) && chainActive.Tip()->nChainWork <= pindexLast->nChainWork) {
2124 vector<CBlockIndex *> vToFetch;
2125 CBlockIndex *pindexWalk = pindexLast;
2126 // Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
2127 while (pindexWalk && !chainActive.Contains(pindexWalk) && vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
2128 if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
2129 !mapBlocksInFlight.count(pindexWalk->GetBlockHash()) &&
2130 (!IsWitnessEnabled(pindexWalk->pprev, chainparams.GetConsensus()) || State(pfrom->GetId())->fHaveWitness)) {
2131 // We don't have this block, and it's not yet in flight.
2132 vToFetch.push_back(pindexWalk);
2134 pindexWalk = pindexWalk->pprev;
2136 // If pindexWalk still isn't on our main chain, we're looking at a
2137 // very large reorg at a time we think we're close to caught up to
2138 // the main chain -- this shouldn't really happen. Bail out on the
2139 // direct fetch and rely on parallel download instead.
2140 if (!chainActive.Contains(pindexWalk)) {
2141 LogPrint("net", "Large reorg, won't direct fetch to %s (%d)\n",
2142 pindexLast->GetBlockHash().ToString(),
2143 pindexLast->nHeight);
2144 } else {
2145 vector<CInv> vGetData;
2146 // Download as much as possible, from earliest to latest.
2147 BOOST_REVERSE_FOREACH(CBlockIndex *pindex, vToFetch) {
2148 if (nodestate->nBlocksInFlight >= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
2149 // Can't download any more from this peer
2150 break;
2152 uint32_t nFetchFlags = GetFetchFlags(pfrom, pindex->pprev, chainparams.GetConsensus());
2153 vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
2154 MarkBlockAsInFlight(pfrom->GetId(), pindex->GetBlockHash(), chainparams.GetConsensus(), pindex);
2155 LogPrint("net", "Requesting block %s from peer=%d\n",
2156 pindex->GetBlockHash().ToString(), pfrom->id);
2158 if (vGetData.size() > 1) {
2159 LogPrint("net", "Downloading blocks toward %s (%d) via headers direct fetch\n",
2160 pindexLast->GetBlockHash().ToString(), pindexLast->nHeight);
2162 if (vGetData.size() > 0) {
2163 if (nodestate->fSupportsDesiredCmpctVersion && vGetData.size() == 1 && mapBlocksInFlight.size() == 1 && pindexLast->pprev->IsValid(BLOCK_VALID_CHAIN)) {
2164 // We seem to be rather well-synced, so it appears pfrom was the first to provide us
2165 // with this block! Let's get them to announce using compact blocks in the future.
2166 MaybeSetPeerAsAnnouncingHeaderAndIDs(nodestate, pfrom, connman);
2167 // In any case, we want to download using a compact block, not a regular one
2168 vGetData[0] = CInv(MSG_CMPCT_BLOCK, vGetData[0].hash);
2170 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vGetData));
2177 else if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
2179 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
2180 vRecv >> *pblock;
2182 LogPrint("net", "received block %s peer=%d\n", pblock->GetHash().ToString(), pfrom->id);
2184 // Process all blocks from whitelisted peers, even if not requested,
2185 // unless we're still syncing with the network.
2186 // Such an unrequested block may still be processed, subject to the
2187 // conditions in AcceptBlock().
2188 bool forceProcessing = pfrom->fWhitelisted && !IsInitialBlockDownload();
2189 const uint256 hash(pblock->GetHash());
2191 LOCK(cs_main);
2192 // Also always process if we requested the block explicitly, as we may
2193 // need it even though it is not a candidate for a new best tip.
2194 forceProcessing |= MarkBlockAsReceived(hash);
2195 // mapBlockSource is only used for sending reject messages and DoS scores,
2196 // so the race between here and cs_main in ProcessNewBlock is fine.
2197 mapBlockSource.emplace(hash, std::make_pair(pfrom->GetId(), true));
2199 bool fNewBlock = false;
2200 ProcessNewBlock(chainparams, pblock, forceProcessing, &fNewBlock);
2201 if (fNewBlock)
2202 pfrom->nLastBlockTime = GetTime();
2206 else if (strCommand == NetMsgType::GETADDR)
2208 // This asymmetric behavior for inbound and outbound connections was introduced
2209 // to prevent a fingerprinting attack: an attacker can send specific fake addresses
2210 // to users' AddrMan and later request them by sending getaddr messages.
2211 // Making nodes which are behind NAT and can only make outgoing connections ignore
2212 // the getaddr message mitigates the attack.
2213 if (!pfrom->fInbound) {
2214 LogPrint("net", "Ignoring \"getaddr\" from outbound connection. peer=%d\n", pfrom->id);
2215 return true;
2218 // Only send one GetAddr response per connection to reduce resource waste
2219 // and discourage addr stamping of INV announcements.
2220 if (pfrom->fSentAddr) {
2221 LogPrint("net", "Ignoring repeated \"getaddr\". peer=%d\n", pfrom->id);
2222 return true;
2224 pfrom->fSentAddr = true;
2226 pfrom->vAddrToSend.clear();
2227 vector<CAddress> vAddr = connman.GetAddresses();
2228 FastRandomContext insecure_rand;
2229 BOOST_FOREACH(const CAddress &addr, vAddr)
2230 pfrom->PushAddress(addr, insecure_rand);
2234 else if (strCommand == NetMsgType::MEMPOOL)
2236 if (!(pfrom->GetLocalServices() & NODE_BLOOM) && !pfrom->fWhitelisted)
2238 LogPrint("net", "mempool request with bloom filters disabled, disconnect peer=%d\n", pfrom->GetId());
2239 pfrom->fDisconnect = true;
2240 return true;
2243 if (connman.OutboundTargetReached(false) && !pfrom->fWhitelisted)
2245 LogPrint("net", "mempool request with bandwidth limit reached, disconnect peer=%d\n", pfrom->GetId());
2246 pfrom->fDisconnect = true;
2247 return true;
2250 LOCK(pfrom->cs_inventory);
2251 pfrom->fSendMempool = true;
2255 else if (strCommand == NetMsgType::PING)
2257 if (pfrom->nVersion > BIP0031_VERSION)
2259 uint64_t nonce = 0;
2260 vRecv >> nonce;
2261 // Echo the message back with the nonce. This allows for two useful features:
2263 // 1) A remote node can quickly check if the connection is operational
2264 // 2) Remote nodes can measure the latency of the network thread. If this node
2265 // is overloaded it won't respond to pings quickly and the remote node can
2266 // avoid sending us more work, like chain download requests.
2268 // The nonce stops the remote getting confused between different pings: without
2269 // it, if the remote node sends a ping once per second and this node takes 5
2270 // seconds to respond to each, the 5th ping the remote sends would appear to
2271 // return very quickly.
2272 connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::PONG, nonce));
2277 else if (strCommand == NetMsgType::PONG)
2279 int64_t pingUsecEnd = nTimeReceived;
2280 uint64_t nonce = 0;
2281 size_t nAvail = vRecv.in_avail();
2282 bool bPingFinished = false;
2283 std::string sProblem;
2285 if (nAvail >= sizeof(nonce)) {
2286 vRecv >> nonce;
2288 // Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
2289 if (pfrom->nPingNonceSent != 0) {
2290 if (nonce == pfrom->nPingNonceSent) {
2291 // Matching pong received, this ping is no longer outstanding
2292 bPingFinished = true;
2293 int64_t pingUsecTime = pingUsecEnd - pfrom->nPingUsecStart;
2294 if (pingUsecTime > 0) {
2295 // Successful ping time measurement, replace previous
2296 pfrom->nPingUsecTime = pingUsecTime;
2297 pfrom->nMinPingUsecTime = std::min(pfrom->nMinPingUsecTime, pingUsecTime);
2298 } else {
2299 // This should never happen
2300 sProblem = "Timing mishap";
2302 } else {
2303 // Nonce mismatches are normal when pings are overlapping
2304 sProblem = "Nonce mismatch";
2305 if (nonce == 0) {
2306 // This is most likely a bug in another implementation somewhere; cancel this ping
2307 bPingFinished = true;
2308 sProblem = "Nonce zero";
2311 } else {
2312 sProblem = "Unsolicited pong without ping";
2314 } else {
2315 // This is most likely a bug in another implementation somewhere; cancel this ping
2316 bPingFinished = true;
2317 sProblem = "Short payload";
2320 if (!(sProblem.empty())) {
2321 LogPrint("net", "pong peer=%d: %s, %x expected, %x received, %u bytes\n",
2322 pfrom->id,
2323 sProblem,
2324 pfrom->nPingNonceSent,
2325 nonce,
2326 nAvail);
2328 if (bPingFinished) {
2329 pfrom->nPingNonceSent = 0;
2334 else if (strCommand == NetMsgType::FILTERLOAD)
2336 CBloomFilter filter;
2337 vRecv >> filter;
2339 if (!filter.IsWithinSizeConstraints())
2341 // There is no excuse for sending a too-large filter
2342 LOCK(cs_main);
2343 Misbehaving(pfrom->GetId(), 100);
2345 else
2347 LOCK(pfrom->cs_filter);
2348 delete pfrom->pfilter;
2349 pfrom->pfilter = new CBloomFilter(filter);
2350 pfrom->pfilter->UpdateEmptyFull();
2351 pfrom->fRelayTxes = true;
2356 else if (strCommand == NetMsgType::FILTERADD)
2358 vector<unsigned char> vData;
2359 vRecv >> vData;
2361 // Nodes must NEVER send a data item > 520 bytes (the max size for a script data object,
2362 // and thus, the maximum size any matched object can have) in a filteradd message
2363 bool bad = false;
2364 if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE) {
2365 bad = true;
2366 } else {
2367 LOCK(pfrom->cs_filter);
2368 if (pfrom->pfilter) {
2369 pfrom->pfilter->insert(vData);
2370 } else {
2371 bad = true;
2374 if (bad) {
2375 LOCK(cs_main);
2376 Misbehaving(pfrom->GetId(), 100);
2381 else if (strCommand == NetMsgType::FILTERCLEAR)
2383 LOCK(pfrom->cs_filter);
2384 if (pfrom->GetLocalServices() & NODE_BLOOM) {
2385 delete pfrom->pfilter;
2386 pfrom->pfilter = new CBloomFilter();
2388 pfrom->fRelayTxes = true;
2392 else if (strCommand == NetMsgType::REJECT)
2394 if (fDebug) {
2395 try {
2396 string strMsg; unsigned char ccode; string strReason;
2397 vRecv >> LIMITED_STRING(strMsg, CMessageHeader::COMMAND_SIZE) >> ccode >> LIMITED_STRING(strReason, MAX_REJECT_MESSAGE_LENGTH);
2399 ostringstream ss;
2400 ss << strMsg << " code " << itostr(ccode) << ": " << strReason;
2402 if (strMsg == NetMsgType::BLOCK || strMsg == NetMsgType::TX)
2404 uint256 hash;
2405 vRecv >> hash;
2406 ss << ": hash " << hash.ToString();
2408 LogPrint("net", "Reject %s\n", SanitizeString(ss.str()));
2409 } catch (const std::ios_base::failure&) {
2410 // Avoid feedback loops by preventing reject messages from triggering a new reject message.
2411 LogPrint("net", "Unparseable reject message received\n");
2416 else if (strCommand == NetMsgType::FEEFILTER) {
2417 CAmount newFeeFilter = 0;
2418 vRecv >> newFeeFilter;
2419 if (MoneyRange(newFeeFilter)) {
2421 LOCK(pfrom->cs_feeFilter);
2422 pfrom->minFeeFilter = newFeeFilter;
2424 LogPrint("net", "received: feefilter of %s from peer=%d\n", CFeeRate(newFeeFilter).ToString(), pfrom->id);
2428 else if (strCommand == NetMsgType::NOTFOUND) {
2429 // We do not care about the NOTFOUND message, but logging an Unknown Command
2430 // message would be undesirable as we transmit it ourselves.
2433 else {
2434 // Ignore unknown commands for extensibility
2435 LogPrint("net", "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->id);
2440 return true;
2443 // requires LOCK(cs_vRecvMsg)
2444 bool ProcessMessages(CNode* pfrom, CConnman& connman, std::atomic<bool>& interruptMsgProc)
2446 const CChainParams& chainparams = Params();
2447 unsigned int nMaxSendBufferSize = connman.GetSendBufferSize();
2449 // Message format
2450 // (4) message start
2451 // (12) command
2452 // (4) size
2453 // (4) checksum
2454 // (x) data
2456 bool fMoreWork = false;
2458 if (!pfrom->vRecvGetData.empty())
2459 ProcessGetData(pfrom, chainparams.GetConsensus(), connman, interruptMsgProc);
2461 if (pfrom->fDisconnect)
2462 return false;
2464 // this maintains the order of responses
2465 if (!pfrom->vRecvGetData.empty()) return true;
2467 // Don't bother if send buffer is too full to respond anyway
2468 if (pfrom->nSendSize >= nMaxSendBufferSize)
2469 return false;
2471 std::list<CNetMessage> msgs;
2473 LOCK(pfrom->cs_vProcessMsg);
2474 if (pfrom->vProcessMsg.empty())
2475 return false;
2476 // Just take one message
2477 msgs.splice(msgs.begin(), pfrom->vProcessMsg, pfrom->vProcessMsg.begin());
2478 pfrom->nProcessQueueSize -= msgs.front().vRecv.size() + CMessageHeader::HEADER_SIZE;
2479 pfrom->fPauseRecv = pfrom->nProcessQueueSize > connman.GetReceiveFloodSize();
2480 fMoreWork = !pfrom->vProcessMsg.empty();
2482 CNetMessage& msg(msgs.front());
2484 msg.SetVersion(pfrom->GetRecvVersion());
2485 // Scan for message start
2486 if (memcmp(msg.hdr.pchMessageStart, chainparams.MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) {
2487 LogPrintf("PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg.hdr.GetCommand()), pfrom->id);
2488 pfrom->fDisconnect = true;
2489 return false;
2492 // Read header
2493 CMessageHeader& hdr = msg.hdr;
2494 if (!hdr.IsValid(chainparams.MessageStart()))
2496 LogPrintf("PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(hdr.GetCommand()), pfrom->id);
2497 return fMoreWork;
2499 string strCommand = hdr.GetCommand();
2501 // Message size
2502 unsigned int nMessageSize = hdr.nMessageSize;
2504 // Checksum
2505 CDataStream& vRecv = msg.vRecv;
2506 const uint256& hash = msg.GetMessageHash();
2507 if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0)
2509 LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s\n", __func__,
2510 SanitizeString(strCommand), nMessageSize,
2511 HexStr(hash.begin(), hash.begin()+CMessageHeader::CHECKSUM_SIZE),
2512 HexStr(hdr.pchChecksum, hdr.pchChecksum+CMessageHeader::CHECKSUM_SIZE));
2513 return fMoreWork;
2516 // Process message
2517 bool fRet = false;
2520 fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime, chainparams, connman, interruptMsgProc);
2521 if (interruptMsgProc)
2522 return false;
2523 if (!pfrom->vRecvGetData.empty())
2524 fMoreWork = true;
2526 catch (const std::ios_base::failure& e)
2528 connman.PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_MALFORMED, string("error parsing message")));
2529 if (strstr(e.what(), "end of data"))
2531 // Allow exceptions from under-length message on vRecv
2532 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());
2534 else if (strstr(e.what(), "size too large"))
2536 // Allow exceptions from over-long size
2537 LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
2539 else if (strstr(e.what(), "non-canonical ReadCompactSize()"))
2541 // Allow exceptions from non-canonical encoding
2542 LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
2544 else
2546 PrintExceptionContinue(&e, "ProcessMessages()");
2549 catch (const std::exception& e) {
2550 PrintExceptionContinue(&e, "ProcessMessages()");
2551 } catch (...) {
2552 PrintExceptionContinue(NULL, "ProcessMessages()");
2555 if (!fRet)
2556 LogPrintf("%s(%s, %u bytes) FAILED peer=%d\n", __func__, SanitizeString(strCommand), nMessageSize, pfrom->id);
2558 return fMoreWork;
2561 class CompareInvMempoolOrder
2563 CTxMemPool *mp;
2564 public:
2565 CompareInvMempoolOrder(CTxMemPool *_mempool)
2567 mp = _mempool;
2570 bool operator()(std::set<uint256>::iterator a, std::set<uint256>::iterator b)
2572 /* As std::make_heap produces a max-heap, we want the entries with the
2573 * fewest ancestors/highest fee to sort later. */
2574 return mp->CompareDepthAndScore(*b, *a);
2578 bool SendMessages(CNode* pto, CConnman& connman, std::atomic<bool>& interruptMsgProc)
2580 const Consensus::Params& consensusParams = Params().GetConsensus();
2582 // Don't send anything until we get its version message
2583 if (pto->nVersion == 0 || pto->fDisconnect)
2584 return true;
2586 // If we get here, the outgoing message serialization version is set and can't change.
2587 CNetMsgMaker msgMaker(pto->GetSendVersion());
2590 // Message: ping
2592 bool pingSend = false;
2593 if (pto->fPingQueued) {
2594 // RPC ping request by user
2595 pingSend = true;
2597 if (pto->nPingNonceSent == 0 && pto->nPingUsecStart + PING_INTERVAL * 1000000 < GetTimeMicros()) {
2598 // Ping automatically sent as a latency probe & keepalive.
2599 pingSend = true;
2601 if (pingSend) {
2602 uint64_t nonce = 0;
2603 while (nonce == 0) {
2604 GetRandBytes((unsigned char*)&nonce, sizeof(nonce));
2606 pto->fPingQueued = false;
2607 pto->nPingUsecStart = GetTimeMicros();
2608 if (pto->nVersion > BIP0031_VERSION) {
2609 pto->nPingNonceSent = nonce;
2610 connman.PushMessage(pto, msgMaker.Make(NetMsgType::PING, nonce));
2611 } else {
2612 // Peer is too old to support ping command with nonce, pong will never arrive.
2613 pto->nPingNonceSent = 0;
2614 connman.PushMessage(pto, msgMaker.Make(NetMsgType::PING));
2618 TRY_LOCK(cs_main, lockMain); // Acquire cs_main for IsInitialBlockDownload() and CNodeState()
2619 if (!lockMain)
2620 return true;
2622 CNodeState &state = *State(pto->GetId());
2624 BOOST_FOREACH(const CBlockReject& reject, state.rejects)
2625 connman.PushMessage(pto, msgMaker.Make(NetMsgType::REJECT, (string)NetMsgType::BLOCK, reject.chRejectCode, reject.strRejectReason, reject.hashBlock));
2626 state.rejects.clear();
2628 if (state.fShouldBan) {
2629 state.fShouldBan = false;
2630 if (pto->fWhitelisted)
2631 LogPrintf("Warning: not punishing whitelisted peer %s!\n", pto->addr.ToString());
2632 else {
2633 pto->fDisconnect = true;
2634 if (pto->addr.IsLocal())
2635 LogPrintf("Warning: not banning local peer %s!\n", pto->addr.ToString());
2636 else
2638 connman.Ban(pto->addr, BanReasonNodeMisbehaving);
2640 return true;
2644 // Address refresh broadcast
2645 int64_t nNow = GetTimeMicros();
2646 if (!IsInitialBlockDownload() && pto->nNextLocalAddrSend < nNow) {
2647 AdvertiseLocal(pto);
2648 pto->nNextLocalAddrSend = PoissonNextSend(nNow, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
2652 // Message: addr
2654 if (pto->nNextAddrSend < nNow) {
2655 pto->nNextAddrSend = PoissonNextSend(nNow, AVG_ADDRESS_BROADCAST_INTERVAL);
2656 vector<CAddress> vAddr;
2657 vAddr.reserve(pto->vAddrToSend.size());
2658 BOOST_FOREACH(const CAddress& addr, pto->vAddrToSend)
2660 if (!pto->addrKnown.contains(addr.GetKey()))
2662 pto->addrKnown.insert(addr.GetKey());
2663 vAddr.push_back(addr);
2664 // receiver rejects addr messages larger than 1000
2665 if (vAddr.size() >= 1000)
2667 connman.PushMessage(pto, msgMaker.Make(NetMsgType::ADDR, vAddr));
2668 vAddr.clear();
2672 pto->vAddrToSend.clear();
2673 if (!vAddr.empty())
2674 connman.PushMessage(pto, msgMaker.Make(NetMsgType::ADDR, vAddr));
2675 // we only send the big addr message once
2676 if (pto->vAddrToSend.capacity() > 40)
2677 pto->vAddrToSend.shrink_to_fit();
2680 // Start block sync
2681 if (pindexBestHeader == NULL)
2682 pindexBestHeader = chainActive.Tip();
2683 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.
2684 if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex) {
2685 // Only actively request headers from a single peer, unless we're close to today.
2686 if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
2687 state.fSyncStarted = true;
2688 nSyncStarted++;
2689 const CBlockIndex *pindexStart = pindexBestHeader;
2690 /* If possible, start at the block preceding the currently
2691 best known header. This ensures that we always get a
2692 non-empty list of headers back as long as the peer
2693 is up-to-date. With a non-empty response, we can initialise
2694 the peer's known best block. This wouldn't be possible
2695 if we requested starting at pindexBestHeader and
2696 got back an empty response. */
2697 if (pindexStart->pprev)
2698 pindexStart = pindexStart->pprev;
2699 LogPrint("net", "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->nHeight, pto->id, pto->nStartingHeight);
2700 connman.PushMessage(pto, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexStart), uint256()));
2704 // Resend wallet transactions that haven't gotten in a block yet
2705 // Except during reindex, importing and IBD, when old wallet
2706 // transactions become unconfirmed and spams other nodes.
2707 if (!fReindex && !fImporting && !IsInitialBlockDownload())
2709 GetMainSignals().Broadcast(nTimeBestReceived, &connman);
2713 // Try sending block announcements via headers
2716 // If we have less than MAX_BLOCKS_TO_ANNOUNCE in our
2717 // list of block hashes we're relaying, and our peer wants
2718 // headers announcements, then find the first header
2719 // not yet known to our peer but would connect, and send.
2720 // If no header would connect, or if we have too many
2721 // blocks, or if the peer doesn't want headers, just
2722 // add all to the inv queue.
2723 LOCK(pto->cs_inventory);
2724 vector<CBlock> vHeaders;
2725 bool fRevertToInv = ((!state.fPreferHeaders &&
2726 (!state.fPreferHeaderAndIDs || pto->vBlockHashesToAnnounce.size() > 1)) ||
2727 pto->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE);
2728 CBlockIndex *pBestIndex = NULL; // last header queued for delivery
2729 ProcessBlockAvailability(pto->id); // ensure pindexBestKnownBlock is up-to-date
2731 if (!fRevertToInv) {
2732 bool fFoundStartingHeader = false;
2733 // Try to find first header that our peer doesn't have, and
2734 // then send all headers past that one. If we come across any
2735 // headers that aren't on chainActive, give up.
2736 BOOST_FOREACH(const uint256 &hash, pto->vBlockHashesToAnnounce) {
2737 BlockMap::iterator mi = mapBlockIndex.find(hash);
2738 assert(mi != mapBlockIndex.end());
2739 CBlockIndex *pindex = mi->second;
2740 if (chainActive[pindex->nHeight] != pindex) {
2741 // Bail out if we reorged away from this block
2742 fRevertToInv = true;
2743 break;
2745 if (pBestIndex != NULL && pindex->pprev != pBestIndex) {
2746 // This means that the list of blocks to announce don't
2747 // connect to each other.
2748 // This shouldn't really be possible to hit during
2749 // regular operation (because reorgs should take us to
2750 // a chain that has some block not on the prior chain,
2751 // which should be caught by the prior check), but one
2752 // way this could happen is by using invalidateblock /
2753 // reconsiderblock repeatedly on the tip, causing it to
2754 // be added multiple times to vBlockHashesToAnnounce.
2755 // Robustly deal with this rare situation by reverting
2756 // to an inv.
2757 fRevertToInv = true;
2758 break;
2760 pBestIndex = pindex;
2761 if (fFoundStartingHeader) {
2762 // add this to the headers message
2763 vHeaders.push_back(pindex->GetBlockHeader());
2764 } else if (PeerHasHeader(&state, pindex)) {
2765 continue; // keep looking for the first new block
2766 } else if (pindex->pprev == NULL || PeerHasHeader(&state, pindex->pprev)) {
2767 // Peer doesn't have this header but they do have the prior one.
2768 // Start sending headers.
2769 fFoundStartingHeader = true;
2770 vHeaders.push_back(pindex->GetBlockHeader());
2771 } else {
2772 // Peer doesn't have this header or the prior one -- nothing will
2773 // connect, so bail out.
2774 fRevertToInv = true;
2775 break;
2779 if (!fRevertToInv && !vHeaders.empty()) {
2780 if (vHeaders.size() == 1 && state.fPreferHeaderAndIDs) {
2781 // We only send up to 1 block as header-and-ids, as otherwise
2782 // probably means we're doing an initial-ish-sync or they're slow
2783 LogPrint("net", "%s sending header-and-ids %s to peer %d\n", __func__,
2784 vHeaders.front().GetHash().ToString(), pto->id);
2785 //TODO: Shouldn't need to reload block from disk, but requires refactor
2786 CBlock block;
2787 bool ret = ReadBlockFromDisk(block, pBestIndex, consensusParams);
2788 assert(ret);
2789 CBlockHeaderAndShortTxIDs cmpctblock(block, state.fWantsCmpctWitness);
2790 int nSendFlags = state.fWantsCmpctWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
2791 connman.PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
2792 state.pindexBestHeaderSent = pBestIndex;
2793 } else if (state.fPreferHeaders) {
2794 if (vHeaders.size() > 1) {
2795 LogPrint("net", "%s: %u headers, range (%s, %s), to peer=%d\n", __func__,
2796 vHeaders.size(),
2797 vHeaders.front().GetHash().ToString(),
2798 vHeaders.back().GetHash().ToString(), pto->id);
2799 } else {
2800 LogPrint("net", "%s: sending header %s to peer=%d\n", __func__,
2801 vHeaders.front().GetHash().ToString(), pto->id);
2803 connman.PushMessage(pto, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
2804 state.pindexBestHeaderSent = pBestIndex;
2805 } else
2806 fRevertToInv = true;
2808 if (fRevertToInv) {
2809 // If falling back to using an inv, just try to inv the tip.
2810 // The last entry in vBlockHashesToAnnounce was our tip at some point
2811 // in the past.
2812 if (!pto->vBlockHashesToAnnounce.empty()) {
2813 const uint256 &hashToAnnounce = pto->vBlockHashesToAnnounce.back();
2814 BlockMap::iterator mi = mapBlockIndex.find(hashToAnnounce);
2815 assert(mi != mapBlockIndex.end());
2816 CBlockIndex *pindex = mi->second;
2818 // Warn if we're announcing a block that is not on the main chain.
2819 // This should be very rare and could be optimized out.
2820 // Just log for now.
2821 if (chainActive[pindex->nHeight] != pindex) {
2822 LogPrint("net", "Announcing block %s not on main chain (tip=%s)\n",
2823 hashToAnnounce.ToString(), chainActive.Tip()->GetBlockHash().ToString());
2826 // If the peer's chain has this block, don't inv it back.
2827 if (!PeerHasHeader(&state, pindex)) {
2828 pto->PushInventory(CInv(MSG_BLOCK, hashToAnnounce));
2829 LogPrint("net", "%s: sending inv peer=%d hash=%s\n", __func__,
2830 pto->id, hashToAnnounce.ToString());
2834 pto->vBlockHashesToAnnounce.clear();
2838 // Message: inventory
2840 vector<CInv> vInv;
2842 LOCK(pto->cs_inventory);
2843 vInv.reserve(std::max<size_t>(pto->vInventoryBlockToSend.size(), INVENTORY_BROADCAST_MAX));
2845 // Add blocks
2846 BOOST_FOREACH(const uint256& hash, pto->vInventoryBlockToSend) {
2847 vInv.push_back(CInv(MSG_BLOCK, hash));
2848 if (vInv.size() == MAX_INV_SZ) {
2849 connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
2850 vInv.clear();
2853 pto->vInventoryBlockToSend.clear();
2855 // Check whether periodic sends should happen
2856 bool fSendTrickle = pto->fWhitelisted;
2857 if (pto->nNextInvSend < nNow) {
2858 fSendTrickle = true;
2859 // Use half the delay for outbound peers, as there is less privacy concern for them.
2860 pto->nNextInvSend = PoissonNextSend(nNow, INVENTORY_BROADCAST_INTERVAL >> !pto->fInbound);
2863 // Time to send but the peer has requested we not relay transactions.
2864 if (fSendTrickle) {
2865 LOCK(pto->cs_filter);
2866 if (!pto->fRelayTxes) pto->setInventoryTxToSend.clear();
2869 // Respond to BIP35 mempool requests
2870 if (fSendTrickle && pto->fSendMempool) {
2871 auto vtxinfo = mempool.infoAll();
2872 pto->fSendMempool = false;
2873 CAmount filterrate = 0;
2875 LOCK(pto->cs_feeFilter);
2876 filterrate = pto->minFeeFilter;
2879 LOCK(pto->cs_filter);
2881 for (const auto& txinfo : vtxinfo) {
2882 const uint256& hash = txinfo.tx->GetHash();
2883 CInv inv(MSG_TX, hash);
2884 pto->setInventoryTxToSend.erase(hash);
2885 if (filterrate) {
2886 if (txinfo.feeRate.GetFeePerK() < filterrate)
2887 continue;
2889 if (pto->pfilter) {
2890 if (!pto->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
2892 pto->filterInventoryKnown.insert(hash);
2893 vInv.push_back(inv);
2894 if (vInv.size() == MAX_INV_SZ) {
2895 connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
2896 vInv.clear();
2899 pto->timeLastMempoolReq = GetTime();
2902 // Determine transactions to relay
2903 if (fSendTrickle) {
2904 // Produce a vector with all candidates for sending
2905 vector<std::set<uint256>::iterator> vInvTx;
2906 vInvTx.reserve(pto->setInventoryTxToSend.size());
2907 for (std::set<uint256>::iterator it = pto->setInventoryTxToSend.begin(); it != pto->setInventoryTxToSend.end(); it++) {
2908 vInvTx.push_back(it);
2910 CAmount filterrate = 0;
2912 LOCK(pto->cs_feeFilter);
2913 filterrate = pto->minFeeFilter;
2915 // Topologically and fee-rate sort the inventory we send for privacy and priority reasons.
2916 // A heap is used so that not all items need sorting if only a few are being sent.
2917 CompareInvMempoolOrder compareInvMempoolOrder(&mempool);
2918 std::make_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
2919 // No reason to drain out at many times the network's capacity,
2920 // especially since we have many peers and some will draw much shorter delays.
2921 unsigned int nRelayedTransactions = 0;
2922 LOCK(pto->cs_filter);
2923 while (!vInvTx.empty() && nRelayedTransactions < INVENTORY_BROADCAST_MAX) {
2924 // Fetch the top element from the heap
2925 std::pop_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
2926 std::set<uint256>::iterator it = vInvTx.back();
2927 vInvTx.pop_back();
2928 uint256 hash = *it;
2929 // Remove it from the to-be-sent set
2930 pto->setInventoryTxToSend.erase(it);
2931 // Check if not in the filter already
2932 if (pto->filterInventoryKnown.contains(hash)) {
2933 continue;
2935 // Not in the mempool anymore? don't bother sending it.
2936 auto txinfo = mempool.info(hash);
2937 if (!txinfo.tx) {
2938 continue;
2940 if (filterrate && txinfo.feeRate.GetFeePerK() < filterrate) {
2941 continue;
2943 if (pto->pfilter && !pto->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
2944 // Send
2945 vInv.push_back(CInv(MSG_TX, hash));
2946 nRelayedTransactions++;
2948 // Expire old relay messages
2949 while (!vRelayExpiration.empty() && vRelayExpiration.front().first < nNow)
2951 mapRelay.erase(vRelayExpiration.front().second);
2952 vRelayExpiration.pop_front();
2955 auto ret = mapRelay.insert(std::make_pair(hash, std::move(txinfo.tx)));
2956 if (ret.second) {
2957 vRelayExpiration.push_back(std::make_pair(nNow + 15 * 60 * 1000000, ret.first));
2960 if (vInv.size() == MAX_INV_SZ) {
2961 connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
2962 vInv.clear();
2964 pto->filterInventoryKnown.insert(hash);
2968 if (!vInv.empty())
2969 connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
2971 // Detect whether we're stalling
2972 nNow = GetTimeMicros();
2973 if (state.nStallingSince && state.nStallingSince < nNow - 1000000 * BLOCK_STALLING_TIMEOUT) {
2974 // Stalling only triggers when the block download window cannot move. During normal steady state,
2975 // the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
2976 // should only happen during initial block download.
2977 LogPrintf("Peer=%d is stalling block download, disconnecting\n", pto->id);
2978 pto->fDisconnect = true;
2979 return true;
2981 // In case there is a block that has been in flight from this peer for 2 + 0.5 * N times the block interval
2982 // (with N the number of peers from which we're downloading validated blocks), disconnect due to timeout.
2983 // We compensate for other peers to prevent killing off peers due to our own downstream link
2984 // being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
2985 // to unreasonably increase our timeout.
2986 if (state.vBlocksInFlight.size() > 0) {
2987 QueuedBlock &queuedBlock = state.vBlocksInFlight.front();
2988 int nOtherPeersWithValidatedDownloads = nPeersWithValidatedDownloads - (state.nBlocksInFlightValidHeaders > 0);
2989 if (nNow > state.nDownloadingSince + consensusParams.nPowTargetSpacing * (BLOCK_DOWNLOAD_TIMEOUT_BASE + BLOCK_DOWNLOAD_TIMEOUT_PER_PEER * nOtherPeersWithValidatedDownloads)) {
2990 LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", queuedBlock.hash.ToString(), pto->id);
2991 pto->fDisconnect = true;
2992 return true;
2997 // Message: getdata (blocks)
2999 vector<CInv> vGetData;
3000 if (!pto->fClient && (fFetch || !IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
3001 vector<CBlockIndex*> vToDownload;
3002 NodeId staller = -1;
3003 FindNextBlocksToDownload(pto->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller, consensusParams);
3004 BOOST_FOREACH(CBlockIndex *pindex, vToDownload) {
3005 uint32_t nFetchFlags = GetFetchFlags(pto, pindex->pprev, consensusParams);
3006 vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
3007 MarkBlockAsInFlight(pto->GetId(), pindex->GetBlockHash(), consensusParams, pindex);
3008 LogPrint("net", "Requesting block %s (%d) peer=%d\n", pindex->GetBlockHash().ToString(),
3009 pindex->nHeight, pto->id);
3011 if (state.nBlocksInFlight == 0 && staller != -1) {
3012 if (State(staller)->nStallingSince == 0) {
3013 State(staller)->nStallingSince = nNow;
3014 LogPrint("net", "Stall started peer=%d\n", staller);
3020 // Message: getdata (non-blocks)
3022 while (!pto->mapAskFor.empty() && (*pto->mapAskFor.begin()).first <= nNow)
3024 const CInv& inv = (*pto->mapAskFor.begin()).second;
3025 if (!AlreadyHave(inv))
3027 if (fDebug)
3028 LogPrint("net", "Requesting %s peer=%d\n", inv.ToString(), pto->id);
3029 vGetData.push_back(inv);
3030 if (vGetData.size() >= 1000)
3032 connman.PushMessage(pto, msgMaker.Make(NetMsgType::GETDATA, vGetData));
3033 vGetData.clear();
3035 } else {
3036 //If we're not going to ask, don't expect a response.
3037 pto->setAskFor.erase(inv.hash);
3039 pto->mapAskFor.erase(pto->mapAskFor.begin());
3041 if (!vGetData.empty())
3042 connman.PushMessage(pto, msgMaker.Make(NetMsgType::GETDATA, vGetData));
3045 // Message: feefilter
3047 // We don't want white listed peers to filter txs to us if we have -whitelistforcerelay
3048 if (pto->nVersion >= FEEFILTER_VERSION && GetBoolArg("-feefilter", DEFAULT_FEEFILTER) &&
3049 !(pto->fWhitelisted && GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY))) {
3050 CAmount currentFilter = mempool.GetMinFee(GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFeePerK();
3051 int64_t timeNow = GetTimeMicros();
3052 if (timeNow > pto->nextSendTimeFeeFilter) {
3053 static CFeeRate default_feerate(DEFAULT_MIN_RELAY_TX_FEE);
3054 static FeeFilterRounder filterRounder(default_feerate);
3055 CAmount filterToSend = filterRounder.round(currentFilter);
3056 // If we don't allow free transactions, then we always have a fee filter of at least minRelayTxFee
3057 if (GetArg("-limitfreerelay", DEFAULT_LIMITFREERELAY) <= 0)
3058 filterToSend = std::max(filterToSend, ::minRelayTxFee.GetFeePerK());
3059 if (filterToSend != pto->lastSentFeeFilter) {
3060 connman.PushMessage(pto, msgMaker.Make(NetMsgType::FEEFILTER, filterToSend));
3061 pto->lastSentFeeFilter = filterToSend;
3063 pto->nextSendTimeFeeFilter = PoissonNextSend(timeNow, AVG_FEEFILTER_BROADCAST_INTERVAL);
3065 // If the fee filter has changed substantially and it's still more than MAX_FEEFILTER_CHANGE_DELAY
3066 // until scheduled broadcast, then move the broadcast to within MAX_FEEFILTER_CHANGE_DELAY.
3067 else if (timeNow + MAX_FEEFILTER_CHANGE_DELAY * 1000000 < pto->nextSendTimeFeeFilter &&
3068 (currentFilter < 3 * pto->lastSentFeeFilter / 4 || currentFilter > 4 * pto->lastSentFeeFilter / 3)) {
3069 pto->nextSendTimeFeeFilter = timeNow + GetRandInt(MAX_FEEFILTER_CHANGE_DELAY) * 1000000;
3073 return true;
3076 class CNetProcessingCleanup
3078 public:
3079 CNetProcessingCleanup() {}
3080 ~CNetProcessingCleanup() {
3081 // orphan transactions
3082 mapOrphanTransactions.clear();
3083 mapOrphanTransactionsByPrev.clear();
3085 } instance_of_cnetprocessingcleanup;