Merge #11490: Disconnect from outbound peers with bad headers chains
[bitcoinplatinum.git] / src / net_processing.cpp
blob50ac76924b8c32d41c6fa93d22c41bd49ae23afd
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 "reverse_iterator.h"
26 #include "tinyformat.h"
27 #include "txmempool.h"
28 #include "ui_interface.h"
29 #include "util.h"
30 #include "utilmoneystr.h"
31 #include "utilstrencodings.h"
32 #include "validationinterface.h"
34 #if defined(NDEBUG)
35 # error "Bitcoin cannot be compiled without assertions."
36 #endif
38 std::atomic<int64_t> nTimeBestReceived(0); // Used only to inform the wallet of when we last received a block
40 struct IteratorComparator
42 template<typename I>
43 bool operator()(const I& a, const I& b)
45 return &(*a) < &(*b);
49 struct COrphanTx {
50 // When modifying, adapt the copy of this definition in tests/DoS_tests.
51 CTransactionRef tx;
52 NodeId fromPeer;
53 int64_t nTimeExpire;
55 std::map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY(cs_main);
56 std::map<COutPoint, std::set<std::map<uint256, COrphanTx>::iterator, IteratorComparator>> mapOrphanTransactionsByPrev GUARDED_BY(cs_main);
57 void EraseOrphansFor(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
59 static size_t vExtraTxnForCompactIt = 0;
60 static std::vector<std::pair<uint256, CTransactionRef>> vExtraTxnForCompact GUARDED_BY(cs_main);
62 static const uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL; // SHA256("main address relay")[0:8]
64 /// Age after which a stale block will no longer be served if requested as
65 /// protection against fingerprinting. Set to one month, denominated in seconds.
66 static const int STALE_RELAY_AGE_LIMIT = 30 * 24 * 60 * 60;
68 /// Age after which a block is considered historical for purposes of rate
69 /// limiting block relay. Set to one week, denominated in seconds.
70 static const int HISTORICAL_BLOCK_AGE = 7 * 24 * 60 * 60;
72 // Internal stuff
73 namespace {
74 /** Number of nodes with fSyncStarted. */
75 int nSyncStarted = 0;
77 /**
78 * Sources of received blocks, saved to be able to send them reject
79 * messages or ban them when processing happens afterwards. Protected by
80 * cs_main.
81 * Set mapBlockSource[hash].second to false if the node should not be
82 * punished if the block is invalid.
84 std::map<uint256, std::pair<NodeId, bool>> mapBlockSource;
86 /**
87 * Filter for transactions that were recently rejected by
88 * AcceptToMemoryPool. These are not rerequested until the chain tip
89 * changes, at which point the entire filter is reset. Protected by
90 * cs_main.
92 * Without this filter we'd be re-requesting txs from each of our peers,
93 * increasing bandwidth consumption considerably. For instance, with 100
94 * peers, half of which relay a tx we don't accept, that might be a 50x
95 * bandwidth increase. A flooding attacker attempting to roll-over the
96 * filter using minimum-sized, 60byte, transactions might manage to send
97 * 1000/sec if we have fast peers, so we pick 120,000 to give our peers a
98 * two minute window to send invs to us.
100 * Decreasing the false positive rate is fairly cheap, so we pick one in a
101 * million to make it highly unlikely for users to have issues with this
102 * filter.
104 * Memory used: 1.3 MB
106 std::unique_ptr<CRollingBloomFilter> recentRejects;
107 uint256 hashRecentRejectsChainTip;
109 /** Blocks that are in flight, and that are in the queue to be downloaded. Protected by cs_main. */
110 struct QueuedBlock {
111 uint256 hash;
112 const CBlockIndex* pindex; //!< Optional.
113 bool fValidatedHeaders; //!< Whether this block has validated headers at the time of request.
114 std::unique_ptr<PartiallyDownloadedBlock> partialBlock; //!< Optional, used for CMPCTBLOCK downloads
116 std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> > mapBlocksInFlight;
118 /** Stack of nodes which we have set to announce using compact blocks */
119 std::list<NodeId> lNodesAnnouncingHeaderAndIDs;
121 /** Number of preferable block download peers. */
122 int nPreferredDownload = 0;
124 /** Number of peers from which we're downloading blocks. */
125 int nPeersWithValidatedDownloads = 0;
127 /** Number of outbound peers with m_chain_sync.m_protect. */
128 int g_outbound_peers_with_protect_from_disconnect = 0;
130 /** Relay map, protected by cs_main. */
131 typedef std::map<uint256, CTransactionRef> MapRelay;
132 MapRelay mapRelay;
133 /** Expiration-time ordered list of (expire time, relay map entry) pairs, protected by cs_main). */
134 std::deque<std::pair<int64_t, MapRelay::iterator>> vRelayExpiration;
135 } // namespace
137 namespace {
139 struct CBlockReject {
140 unsigned char chRejectCode;
141 std::string strRejectReason;
142 uint256 hashBlock;
146 * Maintain validation-specific state about nodes, protected by cs_main, instead
147 * by CNode's own locks. This simplifies asynchronous operation, where
148 * processing of incoming data is done after the ProcessMessage call returns,
149 * and we're no longer holding the node's locks.
151 struct CNodeState {
152 //! The peer's address
153 const CService address;
154 //! Whether we have a fully established connection.
155 bool fCurrentlyConnected;
156 //! Accumulated misbehaviour score for this peer.
157 int nMisbehavior;
158 //! Whether this peer should be disconnected and banned (unless whitelisted).
159 bool fShouldBan;
160 //! String name of this peer (debugging/logging purposes).
161 const std::string name;
162 //! List of asynchronously-determined block rejections to notify this peer about.
163 std::vector<CBlockReject> rejects;
164 //! The best known block we know this peer has announced.
165 const CBlockIndex *pindexBestKnownBlock;
166 //! The hash of the last unknown block this peer has announced.
167 uint256 hashLastUnknownBlock;
168 //! The last full block we both have.
169 const CBlockIndex *pindexLastCommonBlock;
170 //! The best header we have sent our peer.
171 const CBlockIndex *pindexBestHeaderSent;
172 //! Length of current-streak of unconnecting headers announcements
173 int nUnconnectingHeaders;
174 //! Whether we've started headers synchronization with this peer.
175 bool fSyncStarted;
176 //! When to potentially disconnect peer for stalling headers download
177 int64_t nHeadersSyncTimeout;
178 //! Since when we're stalling block download progress (in microseconds), or 0.
179 int64_t nStallingSince;
180 std::list<QueuedBlock> vBlocksInFlight;
181 //! When the first entry in vBlocksInFlight started downloading. Don't care when vBlocksInFlight is empty.
182 int64_t nDownloadingSince;
183 int nBlocksInFlight;
184 int nBlocksInFlightValidHeaders;
185 //! Whether we consider this a preferred download peer.
186 bool fPreferredDownload;
187 //! Whether this peer wants invs or headers (when possible) for block announcements.
188 bool fPreferHeaders;
189 //! Whether this peer wants invs or cmpctblocks (when possible) for block announcements.
190 bool fPreferHeaderAndIDs;
192 * Whether this peer will send us cmpctblocks if we request them.
193 * This is not used to gate request logic, as we really only care about fSupportsDesiredCmpctVersion,
194 * but is used as a flag to "lock in" the version of compact blocks (fWantsCmpctWitness) we send.
196 bool fProvidesHeaderAndIDs;
197 //! Whether this peer can give us witnesses
198 bool fHaveWitness;
199 //! Whether this peer wants witnesses in cmpctblocks/blocktxns
200 bool fWantsCmpctWitness;
202 * If we've announced NODE_WITNESS to this peer: whether the peer sends witnesses in cmpctblocks/blocktxns,
203 * otherwise: whether this peer sends non-witnesses in cmpctblocks/blocktxns.
205 bool fSupportsDesiredCmpctVersion;
207 /** State used to enforce CHAIN_SYNC_TIMEOUT
208 * Only in effect for outbound, non-manual connections, with
209 * m_protect == false
210 * Algorithm: if a peer's best known block has less work than our tip,
211 * set a timeout CHAIN_SYNC_TIMEOUT seconds in the future:
212 * - If at timeout their best known block now has more work than our tip
213 * when the timeout was set, then either reset the timeout or clear it
214 * (after comparing against our current tip's work)
215 * - If at timeout their best known block still has less work than our
216 * tip did when the timeout was set, then send a getheaders message,
217 * and set a shorter timeout, HEADERS_RESPONSE_TIME seconds in future.
218 * If their best known block is still behind when that new timeout is
219 * reached, disconnect.
221 struct ChainSyncTimeoutState {
222 //! A timeout used for checking whether our peer has sufficiently synced
223 int64_t m_timeout;
224 //! A header with the work we require on our peer's chain
225 const CBlockIndex * m_work_header;
226 //! After timeout is reached, set to true after sending getheaders
227 bool m_sent_getheaders;
228 //! Whether this peer is protected from disconnection due to a bad/slow chain
229 bool m_protect;
232 ChainSyncTimeoutState m_chain_sync;
234 CNodeState(CAddress addrIn, std::string addrNameIn) : address(addrIn), name(addrNameIn) {
235 fCurrentlyConnected = false;
236 nMisbehavior = 0;
237 fShouldBan = false;
238 pindexBestKnownBlock = nullptr;
239 hashLastUnknownBlock.SetNull();
240 pindexLastCommonBlock = nullptr;
241 pindexBestHeaderSent = nullptr;
242 nUnconnectingHeaders = 0;
243 fSyncStarted = false;
244 nHeadersSyncTimeout = 0;
245 nStallingSince = 0;
246 nDownloadingSince = 0;
247 nBlocksInFlight = 0;
248 nBlocksInFlightValidHeaders = 0;
249 fPreferredDownload = false;
250 fPreferHeaders = false;
251 fPreferHeaderAndIDs = false;
252 fProvidesHeaderAndIDs = false;
253 fHaveWitness = false;
254 fWantsCmpctWitness = false;
255 fSupportsDesiredCmpctVersion = false;
256 m_chain_sync = { 0, nullptr, false, false };
260 /** Map maintaining per-node state. Requires cs_main. */
261 std::map<NodeId, CNodeState> mapNodeState;
263 // Requires cs_main.
264 CNodeState *State(NodeId pnode) {
265 std::map<NodeId, CNodeState>::iterator it = mapNodeState.find(pnode);
266 if (it == mapNodeState.end())
267 return nullptr;
268 return &it->second;
271 void UpdatePreferredDownload(CNode* node, CNodeState* state)
273 nPreferredDownload -= state->fPreferredDownload;
275 // Whether this node should be marked as a preferred download node.
276 state->fPreferredDownload = (!node->fInbound || node->fWhitelisted) && !node->fOneShot && !node->fClient;
278 nPreferredDownload += state->fPreferredDownload;
281 void PushNodeVersion(CNode *pnode, CConnman* connman, int64_t nTime)
283 ServiceFlags nLocalNodeServices = pnode->GetLocalServices();
284 uint64_t nonce = pnode->GetLocalNonce();
285 int nNodeStartingHeight = pnode->GetMyStartingHeight();
286 NodeId nodeid = pnode->GetId();
287 CAddress addr = pnode->addr;
289 CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService(), addr.nServices));
290 CAddress addrMe = CAddress(CService(), nLocalNodeServices);
292 connman->PushMessage(pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t)nLocalNodeServices, nTime, addrYou, addrMe,
293 nonce, strSubVersion, nNodeStartingHeight, ::fRelayTxes));
295 if (fLogIPs) {
296 LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), addrYou.ToString(), nodeid);
297 } else {
298 LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), nodeid);
302 // Requires cs_main.
303 // Returns a bool indicating whether we requested this block.
304 // Also used if a block was /not/ received and timed out or started with another peer
305 bool MarkBlockAsReceived(const uint256& hash) {
306 std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
307 if (itInFlight != mapBlocksInFlight.end()) {
308 CNodeState *state = State(itInFlight->second.first);
309 assert(state != nullptr);
310 state->nBlocksInFlightValidHeaders -= itInFlight->second.second->fValidatedHeaders;
311 if (state->nBlocksInFlightValidHeaders == 0 && itInFlight->second.second->fValidatedHeaders) {
312 // Last validated block on the queue was received.
313 nPeersWithValidatedDownloads--;
315 if (state->vBlocksInFlight.begin() == itInFlight->second.second) {
316 // First block on the queue was received, update the start download time for the next one
317 state->nDownloadingSince = std::max(state->nDownloadingSince, GetTimeMicros());
319 state->vBlocksInFlight.erase(itInFlight->second.second);
320 state->nBlocksInFlight--;
321 state->nStallingSince = 0;
322 mapBlocksInFlight.erase(itInFlight);
323 return true;
325 return false;
328 // Requires cs_main.
329 // returns false, still setting pit, if the block was already in flight from the same peer
330 // pit will only be valid as long as the same cs_main lock is being held
331 bool MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const CBlockIndex* pindex = nullptr, std::list<QueuedBlock>::iterator** pit = nullptr) {
332 CNodeState *state = State(nodeid);
333 assert(state != nullptr);
335 // Short-circuit most stuff in case its from the same node
336 std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
337 if (itInFlight != mapBlocksInFlight.end() && itInFlight->second.first == nodeid) {
338 if (pit) {
339 *pit = &itInFlight->second.second;
341 return false;
344 // Make sure it's not listed somewhere already.
345 MarkBlockAsReceived(hash);
347 std::list<QueuedBlock>::iterator it = state->vBlocksInFlight.insert(state->vBlocksInFlight.end(),
348 {hash, pindex, pindex != nullptr, std::unique_ptr<PartiallyDownloadedBlock>(pit ? new PartiallyDownloadedBlock(&mempool) : nullptr)});
349 state->nBlocksInFlight++;
350 state->nBlocksInFlightValidHeaders += it->fValidatedHeaders;
351 if (state->nBlocksInFlight == 1) {
352 // We're starting a block download (batch) from this peer.
353 state->nDownloadingSince = GetTimeMicros();
355 if (state->nBlocksInFlightValidHeaders == 1 && pindex != nullptr) {
356 nPeersWithValidatedDownloads++;
358 itInFlight = mapBlocksInFlight.insert(std::make_pair(hash, std::make_pair(nodeid, it))).first;
359 if (pit)
360 *pit = &itInFlight->second.second;
361 return true;
364 /** Check whether the last unknown block a peer advertised is not yet known. */
365 void ProcessBlockAvailability(NodeId nodeid) {
366 CNodeState *state = State(nodeid);
367 assert(state != nullptr);
369 if (!state->hashLastUnknownBlock.IsNull()) {
370 BlockMap::iterator itOld = mapBlockIndex.find(state->hashLastUnknownBlock);
371 if (itOld != mapBlockIndex.end() && itOld->second->nChainWork > 0) {
372 if (state->pindexBestKnownBlock == nullptr || itOld->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
373 state->pindexBestKnownBlock = itOld->second;
374 state->hashLastUnknownBlock.SetNull();
379 /** Update tracking information about which blocks a peer is assumed to have. */
380 void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
381 CNodeState *state = State(nodeid);
382 assert(state != nullptr);
384 ProcessBlockAvailability(nodeid);
386 BlockMap::iterator it = mapBlockIndex.find(hash);
387 if (it != mapBlockIndex.end() && it->second->nChainWork > 0) {
388 // An actually better block was announced.
389 if (state->pindexBestKnownBlock == nullptr || it->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
390 state->pindexBestKnownBlock = it->second;
391 } else {
392 // An unknown block was announced; just assume that the latest one is the best one.
393 state->hashLastUnknownBlock = hash;
397 void MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid, CConnman* connman) {
398 AssertLockHeld(cs_main);
399 CNodeState* nodestate = State(nodeid);
400 if (!nodestate || !nodestate->fSupportsDesiredCmpctVersion) {
401 // Never ask from peers who can't provide witnesses.
402 return;
404 if (nodestate->fProvidesHeaderAndIDs) {
405 for (std::list<NodeId>::iterator it = lNodesAnnouncingHeaderAndIDs.begin(); it != lNodesAnnouncingHeaderAndIDs.end(); it++) {
406 if (*it == nodeid) {
407 lNodesAnnouncingHeaderAndIDs.erase(it);
408 lNodesAnnouncingHeaderAndIDs.push_back(nodeid);
409 return;
412 connman->ForNode(nodeid, [connman](CNode* pfrom){
413 uint64_t nCMPCTBLOCKVersion = (pfrom->GetLocalServices() & NODE_WITNESS) ? 2 : 1;
414 if (lNodesAnnouncingHeaderAndIDs.size() >= 3) {
415 // As per BIP152, we only get 3 of our peers to announce
416 // blocks using compact encodings.
417 connman->ForNode(lNodesAnnouncingHeaderAndIDs.front(), [connman, nCMPCTBLOCKVersion](CNode* pnodeStop){
418 connman->PushMessage(pnodeStop, CNetMsgMaker(pnodeStop->GetSendVersion()).Make(NetMsgType::SENDCMPCT, /*fAnnounceUsingCMPCTBLOCK=*/false, nCMPCTBLOCKVersion));
419 return true;
421 lNodesAnnouncingHeaderAndIDs.pop_front();
423 connman->PushMessage(pfrom, CNetMsgMaker(pfrom->GetSendVersion()).Make(NetMsgType::SENDCMPCT, /*fAnnounceUsingCMPCTBLOCK=*/true, nCMPCTBLOCKVersion));
424 lNodesAnnouncingHeaderAndIDs.push_back(pfrom->GetId());
425 return true;
430 // Requires cs_main
431 bool CanDirectFetch(const Consensus::Params &consensusParams)
433 return chainActive.Tip()->GetBlockTime() > GetAdjustedTime() - consensusParams.nPowTargetSpacing * 20;
436 // Requires cs_main
437 bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex)
439 if (state->pindexBestKnownBlock && pindex == state->pindexBestKnownBlock->GetAncestor(pindex->nHeight))
440 return true;
441 if (state->pindexBestHeaderSent && pindex == state->pindexBestHeaderSent->GetAncestor(pindex->nHeight))
442 return true;
443 return false;
446 /** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
447 * at most count entries. */
448 void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams) {
449 if (count == 0)
450 return;
452 vBlocks.reserve(vBlocks.size() + count);
453 CNodeState *state = State(nodeid);
454 assert(state != nullptr);
456 // Make sure pindexBestKnownBlock is up to date, we'll need it.
457 ProcessBlockAvailability(nodeid);
459 if (state->pindexBestKnownBlock == nullptr || state->pindexBestKnownBlock->nChainWork < chainActive.Tip()->nChainWork || state->pindexBestKnownBlock->nChainWork < nMinimumChainWork) {
460 // This peer has nothing interesting.
461 return;
464 if (state->pindexLastCommonBlock == nullptr) {
465 // Bootstrap quickly by guessing a parent of our best tip is the forking point.
466 // Guessing wrong in either direction is not a problem.
467 state->pindexLastCommonBlock = chainActive[std::min(state->pindexBestKnownBlock->nHeight, chainActive.Height())];
470 // If the peer reorganized, our previous pindexLastCommonBlock may not be an ancestor
471 // of its current tip anymore. Go back enough to fix that.
472 state->pindexLastCommonBlock = LastCommonAncestor(state->pindexLastCommonBlock, state->pindexBestKnownBlock);
473 if (state->pindexLastCommonBlock == state->pindexBestKnownBlock)
474 return;
476 std::vector<const CBlockIndex*> vToFetch;
477 const CBlockIndex *pindexWalk = state->pindexLastCommonBlock;
478 // Never fetch further than the best block we know the peer has, or more than BLOCK_DOWNLOAD_WINDOW + 1 beyond the last
479 // linked block we have in common with this peer. The +1 is so we can detect stalling, namely if we would be able to
480 // download that next block if the window were 1 larger.
481 int nWindowEnd = state->pindexLastCommonBlock->nHeight + BLOCK_DOWNLOAD_WINDOW;
482 int nMaxHeight = std::min<int>(state->pindexBestKnownBlock->nHeight, nWindowEnd + 1);
483 NodeId waitingfor = -1;
484 while (pindexWalk->nHeight < nMaxHeight) {
485 // Read up to 128 (or more, if more blocks than that are needed) successors of pindexWalk (towards
486 // pindexBestKnownBlock) into vToFetch. We fetch 128, because CBlockIndex::GetAncestor may be as expensive
487 // as iterating over ~100 CBlockIndex* entries anyway.
488 int nToFetch = std::min(nMaxHeight - pindexWalk->nHeight, std::max<int>(count - vBlocks.size(), 128));
489 vToFetch.resize(nToFetch);
490 pindexWalk = state->pindexBestKnownBlock->GetAncestor(pindexWalk->nHeight + nToFetch);
491 vToFetch[nToFetch - 1] = pindexWalk;
492 for (unsigned int i = nToFetch - 1; i > 0; i--) {
493 vToFetch[i - 1] = vToFetch[i]->pprev;
496 // Iterate over those blocks in vToFetch (in forward direction), adding the ones that
497 // are not yet downloaded and not in flight to vBlocks. In the mean time, update
498 // pindexLastCommonBlock as long as all ancestors are already downloaded, or if it's
499 // already part of our chain (and therefore don't need it even if pruned).
500 for (const CBlockIndex* pindex : vToFetch) {
501 if (!pindex->IsValid(BLOCK_VALID_TREE)) {
502 // We consider the chain that this peer is on invalid.
503 return;
505 if (!State(nodeid)->fHaveWitness && IsWitnessEnabled(pindex->pprev, consensusParams)) {
506 // We wouldn't download this block or its descendants from this peer.
507 return;
509 if (pindex->nStatus & BLOCK_HAVE_DATA || chainActive.Contains(pindex)) {
510 if (pindex->nChainTx)
511 state->pindexLastCommonBlock = pindex;
512 } else if (mapBlocksInFlight.count(pindex->GetBlockHash()) == 0) {
513 // The block is not already downloaded, and not yet in flight.
514 if (pindex->nHeight > nWindowEnd) {
515 // We reached the end of the window.
516 if (vBlocks.size() == 0 && waitingfor != nodeid) {
517 // We aren't able to fetch anything, but we would be if the download window was one larger.
518 nodeStaller = waitingfor;
520 return;
522 vBlocks.push_back(pindex);
523 if (vBlocks.size() == count) {
524 return;
526 } else if (waitingfor == -1) {
527 // This is the first already-in-flight block.
528 waitingfor = mapBlocksInFlight[pindex->GetBlockHash()].first;
534 } // namespace
536 // Returns true for outbound peers, excluding manual connections, feelers, and
537 // one-shots
538 bool IsOutboundDisconnectionCandidate(const CNode *node)
540 return !(node->fInbound || node->m_manual_connection || node->fFeeler || node->fOneShot);
543 void PeerLogicValidation::InitializeNode(CNode *pnode) {
544 CAddress addr = pnode->addr;
545 std::string addrName = pnode->GetAddrName();
546 NodeId nodeid = pnode->GetId();
548 LOCK(cs_main);
549 mapNodeState.emplace_hint(mapNodeState.end(), std::piecewise_construct, std::forward_as_tuple(nodeid), std::forward_as_tuple(addr, std::move(addrName)));
551 if(!pnode->fInbound)
552 PushNodeVersion(pnode, connman, GetTime());
555 void PeerLogicValidation::FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTime) {
556 fUpdateConnectionTime = false;
557 LOCK(cs_main);
558 CNodeState *state = State(nodeid);
559 assert(state != nullptr);
561 if (state->fSyncStarted)
562 nSyncStarted--;
564 if (state->nMisbehavior == 0 && state->fCurrentlyConnected) {
565 fUpdateConnectionTime = true;
568 for (const QueuedBlock& entry : state->vBlocksInFlight) {
569 mapBlocksInFlight.erase(entry.hash);
571 EraseOrphansFor(nodeid);
572 nPreferredDownload -= state->fPreferredDownload;
573 nPeersWithValidatedDownloads -= (state->nBlocksInFlightValidHeaders != 0);
574 assert(nPeersWithValidatedDownloads >= 0);
575 g_outbound_peers_with_protect_from_disconnect -= state->m_chain_sync.m_protect;
576 assert(g_outbound_peers_with_protect_from_disconnect >= 0);
578 mapNodeState.erase(nodeid);
580 if (mapNodeState.empty()) {
581 // Do a consistency check after the last peer is removed.
582 assert(mapBlocksInFlight.empty());
583 assert(nPreferredDownload == 0);
584 assert(nPeersWithValidatedDownloads == 0);
585 assert(g_outbound_peers_with_protect_from_disconnect == 0);
587 LogPrint(BCLog::NET, "Cleared nodestate for peer=%d\n", nodeid);
590 bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
591 LOCK(cs_main);
592 CNodeState *state = State(nodeid);
593 if (state == nullptr)
594 return false;
595 stats.nMisbehavior = state->nMisbehavior;
596 stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
597 stats.nCommonHeight = state->pindexLastCommonBlock ? state->pindexLastCommonBlock->nHeight : -1;
598 for (const QueuedBlock& queue : state->vBlocksInFlight) {
599 if (queue.pindex)
600 stats.vHeightInFlight.push_back(queue.pindex->nHeight);
602 return true;
605 //////////////////////////////////////////////////////////////////////////////
607 // mapOrphanTransactions
610 void AddToCompactExtraTransactions(const CTransactionRef& tx)
612 size_t max_extra_txn = gArgs.GetArg("-blockreconstructionextratxn", DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN);
613 if (max_extra_txn <= 0)
614 return;
615 if (!vExtraTxnForCompact.size())
616 vExtraTxnForCompact.resize(max_extra_txn);
617 vExtraTxnForCompact[vExtraTxnForCompactIt] = std::make_pair(tx->GetWitnessHash(), tx);
618 vExtraTxnForCompactIt = (vExtraTxnForCompactIt + 1) % max_extra_txn;
621 bool AddOrphanTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
623 const uint256& hash = tx->GetHash();
624 if (mapOrphanTransactions.count(hash))
625 return false;
627 // Ignore big transactions, to avoid a
628 // send-big-orphans memory exhaustion attack. If a peer has a legitimate
629 // large transaction with a missing parent then we assume
630 // it will rebroadcast it later, after the parent transaction(s)
631 // have been mined or received.
632 // 100 orphans, each of which is at most 99,999 bytes big is
633 // at most 10 megabytes of orphans and somewhat more byprev index (in the worst case):
634 unsigned int sz = GetTransactionWeight(*tx);
635 if (sz >= MAX_STANDARD_TX_WEIGHT)
637 LogPrint(BCLog::MEMPOOL, "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString());
638 return false;
641 auto ret = mapOrphanTransactions.emplace(hash, COrphanTx{tx, peer, GetTime() + ORPHAN_TX_EXPIRE_TIME});
642 assert(ret.second);
643 for (const CTxIn& txin : tx->vin) {
644 mapOrphanTransactionsByPrev[txin.prevout].insert(ret.first);
647 AddToCompactExtraTransactions(tx);
649 LogPrint(BCLog::MEMPOOL, "stored orphan tx %s (mapsz %u outsz %u)\n", hash.ToString(),
650 mapOrphanTransactions.size(), mapOrphanTransactionsByPrev.size());
651 return true;
654 int static EraseOrphanTx(uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
656 std::map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.find(hash);
657 if (it == mapOrphanTransactions.end())
658 return 0;
659 for (const CTxIn& txin : it->second.tx->vin)
661 auto itPrev = mapOrphanTransactionsByPrev.find(txin.prevout);
662 if (itPrev == mapOrphanTransactionsByPrev.end())
663 continue;
664 itPrev->second.erase(it);
665 if (itPrev->second.empty())
666 mapOrphanTransactionsByPrev.erase(itPrev);
668 mapOrphanTransactions.erase(it);
669 return 1;
672 void EraseOrphansFor(NodeId peer)
674 int nErased = 0;
675 std::map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
676 while (iter != mapOrphanTransactions.end())
678 std::map<uint256, COrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
679 if (maybeErase->second.fromPeer == peer)
681 nErased += EraseOrphanTx(maybeErase->second.tx->GetHash());
684 if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx from peer=%d\n", nErased, peer);
688 unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
690 unsigned int nEvicted = 0;
691 static int64_t nNextSweep;
692 int64_t nNow = GetTime();
693 if (nNextSweep <= nNow) {
694 // Sweep out expired orphan pool entries:
695 int nErased = 0;
696 int64_t nMinExpTime = nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL;
697 std::map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
698 while (iter != mapOrphanTransactions.end())
700 std::map<uint256, COrphanTx>::iterator maybeErase = iter++;
701 if (maybeErase->second.nTimeExpire <= nNow) {
702 nErased += EraseOrphanTx(maybeErase->second.tx->GetHash());
703 } else {
704 nMinExpTime = std::min(maybeErase->second.nTimeExpire, nMinExpTime);
707 // Sweep again 5 minutes after the next entry that expires in order to batch the linear scan.
708 nNextSweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL;
709 if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx due to expiration\n", nErased);
711 while (mapOrphanTransactions.size() > nMaxOrphans)
713 // Evict a random orphan:
714 uint256 randomhash = GetRandHash();
715 std::map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.lower_bound(randomhash);
716 if (it == mapOrphanTransactions.end())
717 it = mapOrphanTransactions.begin();
718 EraseOrphanTx(it->first);
719 ++nEvicted;
721 return nEvicted;
724 // Requires cs_main.
725 void Misbehaving(NodeId pnode, int howmuch)
727 if (howmuch == 0)
728 return;
730 CNodeState *state = State(pnode);
731 if (state == nullptr)
732 return;
734 state->nMisbehavior += howmuch;
735 int banscore = gArgs.GetArg("-banscore", DEFAULT_BANSCORE_THRESHOLD);
736 if (state->nMisbehavior >= banscore && state->nMisbehavior - howmuch < banscore)
738 LogPrintf("%s: %s peer=%d (%d -> %d) BAN THRESHOLD EXCEEDED\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior);
739 state->fShouldBan = true;
740 } else
741 LogPrintf("%s: %s peer=%d (%d -> %d)\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior);
751 //////////////////////////////////////////////////////////////////////////////
753 // blockchain -> download logic notification
756 // To prevent fingerprinting attacks, only send blocks/headers outside of the
757 // active chain if they are no more than a month older (both in time, and in
758 // best equivalent proof of work) than the best header chain we know about.
759 static bool StaleBlockRequestAllowed(const CBlockIndex* pindex, const Consensus::Params& consensusParams)
761 AssertLockHeld(cs_main);
762 return (pindexBestHeader != nullptr) &&
763 (pindexBestHeader->GetBlockTime() - pindex->GetBlockTime() < STALE_RELAY_AGE_LIMIT) &&
764 (GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, consensusParams) < STALE_RELAY_AGE_LIMIT);
767 PeerLogicValidation::PeerLogicValidation(CConnman* connmanIn) : connman(connmanIn) {
768 // Initialize global variables that cannot be constructed at startup.
769 recentRejects.reset(new CRollingBloomFilter(120000, 0.000001));
772 void PeerLogicValidation::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex, const std::vector<CTransactionRef>& vtxConflicted) {
773 LOCK(cs_main);
775 std::vector<uint256> vOrphanErase;
777 for (const CTransactionRef& ptx : pblock->vtx) {
778 const CTransaction& tx = *ptx;
780 // Which orphan pool entries must we evict?
781 for (const auto& txin : tx.vin) {
782 auto itByPrev = mapOrphanTransactionsByPrev.find(txin.prevout);
783 if (itByPrev == mapOrphanTransactionsByPrev.end()) continue;
784 for (auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); ++mi) {
785 const CTransaction& orphanTx = *(*mi)->second.tx;
786 const uint256& orphanHash = orphanTx.GetHash();
787 vOrphanErase.push_back(orphanHash);
792 // Erase orphan transactions include or precluded by this block
793 if (vOrphanErase.size()) {
794 int nErased = 0;
795 for (uint256 &orphanHash : vOrphanErase) {
796 nErased += EraseOrphanTx(orphanHash);
798 LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx included or conflicted by block\n", nErased);
802 // All of the following cache a recent block, and are protected by cs_most_recent_block
803 static CCriticalSection cs_most_recent_block;
804 static std::shared_ptr<const CBlock> most_recent_block;
805 static std::shared_ptr<const CBlockHeaderAndShortTxIDs> most_recent_compact_block;
806 static uint256 most_recent_block_hash;
807 static bool fWitnessesPresentInMostRecentCompactBlock;
809 void PeerLogicValidation::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) {
810 std::shared_ptr<const CBlockHeaderAndShortTxIDs> pcmpctblock = std::make_shared<const CBlockHeaderAndShortTxIDs> (*pblock, true);
811 const CNetMsgMaker msgMaker(PROTOCOL_VERSION);
813 LOCK(cs_main);
815 static int nHighestFastAnnounce = 0;
816 if (pindex->nHeight <= nHighestFastAnnounce)
817 return;
818 nHighestFastAnnounce = pindex->nHeight;
820 bool fWitnessEnabled = IsWitnessEnabled(pindex->pprev, Params().GetConsensus());
821 uint256 hashBlock(pblock->GetHash());
824 LOCK(cs_most_recent_block);
825 most_recent_block_hash = hashBlock;
826 most_recent_block = pblock;
827 most_recent_compact_block = pcmpctblock;
828 fWitnessesPresentInMostRecentCompactBlock = fWitnessEnabled;
831 connman->ForEachNode([this, &pcmpctblock, pindex, &msgMaker, fWitnessEnabled, &hashBlock](CNode* pnode) {
832 // TODO: Avoid the repeated-serialization here
833 if (pnode->nVersion < INVALID_CB_NO_BAN_VERSION || pnode->fDisconnect)
834 return;
835 ProcessBlockAvailability(pnode->GetId());
836 CNodeState &state = *State(pnode->GetId());
837 // If the peer has, or we announced to them the previous block already,
838 // but we don't think they have this one, go ahead and announce it
839 if (state.fPreferHeaderAndIDs && (!fWitnessEnabled || state.fWantsCmpctWitness) &&
840 !PeerHasHeader(&state, pindex) && PeerHasHeader(&state, pindex->pprev)) {
842 LogPrint(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", "PeerLogicValidation::NewPoWValidBlock",
843 hashBlock.ToString(), pnode->GetId());
844 connman->PushMessage(pnode, msgMaker.Make(NetMsgType::CMPCTBLOCK, *pcmpctblock));
845 state.pindexBestHeaderSent = pindex;
850 void PeerLogicValidation::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
851 const int nNewHeight = pindexNew->nHeight;
852 connman->SetBestHeight(nNewHeight);
854 if (!fInitialDownload) {
855 // Find the hashes of all blocks that weren't previously in the best chain.
856 std::vector<uint256> vHashes;
857 const CBlockIndex *pindexToAnnounce = pindexNew;
858 while (pindexToAnnounce != pindexFork) {
859 vHashes.push_back(pindexToAnnounce->GetBlockHash());
860 pindexToAnnounce = pindexToAnnounce->pprev;
861 if (vHashes.size() == MAX_BLOCKS_TO_ANNOUNCE) {
862 // Limit announcements in case of a huge reorganization.
863 // Rely on the peer's synchronization mechanism in that case.
864 break;
867 // Relay inventory, but don't relay old inventory during initial block download.
868 connman->ForEachNode([nNewHeight, &vHashes](CNode* pnode) {
869 if (nNewHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : 0)) {
870 for (const uint256& hash : reverse_iterate(vHashes)) {
871 pnode->PushBlockHash(hash);
875 connman->WakeMessageHandler();
878 nTimeBestReceived = GetTime();
881 void PeerLogicValidation::BlockChecked(const CBlock& block, const CValidationState& state) {
882 LOCK(cs_main);
884 const uint256 hash(block.GetHash());
885 std::map<uint256, std::pair<NodeId, bool>>::iterator it = mapBlockSource.find(hash);
887 int nDoS = 0;
888 if (state.IsInvalid(nDoS)) {
889 // Don't send reject message with code 0 or an internal reject code.
890 if (it != mapBlockSource.end() && State(it->second.first) && state.GetRejectCode() > 0 && state.GetRejectCode() < REJECT_INTERNAL) {
891 CBlockReject reject = {(unsigned char)state.GetRejectCode(), state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), hash};
892 State(it->second.first)->rejects.push_back(reject);
893 if (nDoS > 0 && it->second.second)
894 Misbehaving(it->second.first, nDoS);
897 // Check that:
898 // 1. The block is valid
899 // 2. We're not in initial block download
900 // 3. This is currently the best block we're aware of. We haven't updated
901 // the tip yet so we have no way to check this directly here. Instead we
902 // just check that there are currently no other blocks in flight.
903 else if (state.IsValid() &&
904 !IsInitialBlockDownload() &&
905 mapBlocksInFlight.count(hash) == mapBlocksInFlight.size()) {
906 if (it != mapBlockSource.end()) {
907 MaybeSetPeerAsAnnouncingHeaderAndIDs(it->second.first, connman);
910 if (it != mapBlockSource.end())
911 mapBlockSource.erase(it);
914 //////////////////////////////////////////////////////////////////////////////
916 // Messages
920 bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
922 switch (inv.type)
924 case MSG_TX:
925 case MSG_WITNESS_TX:
927 assert(recentRejects);
928 if (chainActive.Tip()->GetBlockHash() != hashRecentRejectsChainTip)
930 // If the chain tip has changed previously rejected transactions
931 // might be now valid, e.g. due to a nLockTime'd tx becoming valid,
932 // or a double-spend. Reset the rejects filter and give those
933 // txs a second chance.
934 hashRecentRejectsChainTip = chainActive.Tip()->GetBlockHash();
935 recentRejects->reset();
938 return recentRejects->contains(inv.hash) ||
939 mempool.exists(inv.hash) ||
940 mapOrphanTransactions.count(inv.hash) ||
941 pcoinsTip->HaveCoinInCache(COutPoint(inv.hash, 0)) || // Best effort: only try output 0 and 1
942 pcoinsTip->HaveCoinInCache(COutPoint(inv.hash, 1));
944 case MSG_BLOCK:
945 case MSG_WITNESS_BLOCK:
946 return mapBlockIndex.count(inv.hash);
948 // Don't know what it is, just say we already got one
949 return true;
952 static void RelayTransaction(const CTransaction& tx, CConnman* connman)
954 CInv inv(MSG_TX, tx.GetHash());
955 connman->ForEachNode([&inv](CNode* pnode)
957 pnode->PushInventory(inv);
961 static void RelayAddress(const CAddress& addr, bool fReachable, CConnman* connman)
963 unsigned int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
965 // Relay to a limited number of other nodes
966 // Use deterministic randomness to send to the same nodes for 24 hours
967 // at a time so the addrKnowns of the chosen nodes prevent repeats
968 uint64_t hashAddr = addr.GetHash();
969 const CSipHasher hasher = connman->GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));
970 FastRandomContext insecure_rand;
972 std::array<std::pair<uint64_t, CNode*>,2> best{{{0, nullptr}, {0, nullptr}}};
973 assert(nRelayNodes <= best.size());
975 auto sortfunc = [&best, &hasher, nRelayNodes](CNode* pnode) {
976 if (pnode->nVersion >= CADDR_TIME_VERSION) {
977 uint64_t hashKey = CSipHasher(hasher).Write(pnode->GetId()).Finalize();
978 for (unsigned int i = 0; i < nRelayNodes; i++) {
979 if (hashKey > best[i].first) {
980 std::copy(best.begin() + i, best.begin() + nRelayNodes - 1, best.begin() + i + 1);
981 best[i] = std::make_pair(hashKey, pnode);
982 break;
988 auto pushfunc = [&addr, &best, nRelayNodes, &insecure_rand] {
989 for (unsigned int i = 0; i < nRelayNodes && best[i].first != 0; i++) {
990 best[i].second->PushAddress(addr, insecure_rand);
994 connman->ForEachNodeThen(std::move(sortfunc), std::move(pushfunc));
997 void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParams, CConnman* connman, const std::atomic<bool>& interruptMsgProc)
999 std::deque<CInv>::iterator it = pfrom->vRecvGetData.begin();
1000 std::vector<CInv> vNotFound;
1001 const CNetMsgMaker msgMaker(pfrom->GetSendVersion());
1002 LOCK(cs_main);
1004 while (it != pfrom->vRecvGetData.end()) {
1005 // Don't bother if send buffer is too full to respond anyway
1006 if (pfrom->fPauseSend)
1007 break;
1009 const CInv &inv = *it;
1011 if (interruptMsgProc)
1012 return;
1014 it++;
1016 if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK || inv.type == MSG_CMPCT_BLOCK || inv.type == MSG_WITNESS_BLOCK)
1018 bool send = false;
1019 BlockMap::iterator mi = mapBlockIndex.find(inv.hash);
1020 std::shared_ptr<const CBlock> a_recent_block;
1021 std::shared_ptr<const CBlockHeaderAndShortTxIDs> a_recent_compact_block;
1022 bool fWitnessesPresentInARecentCompactBlock;
1024 LOCK(cs_most_recent_block);
1025 a_recent_block = most_recent_block;
1026 a_recent_compact_block = most_recent_compact_block;
1027 fWitnessesPresentInARecentCompactBlock = fWitnessesPresentInMostRecentCompactBlock;
1029 if (mi != mapBlockIndex.end())
1031 if (mi->second->nChainTx && !mi->second->IsValid(BLOCK_VALID_SCRIPTS) &&
1032 mi->second->IsValid(BLOCK_VALID_TREE)) {
1033 // If we have the block and all of its parents, but have not yet validated it,
1034 // we might be in the middle of connecting it (ie in the unlock of cs_main
1035 // before ActivateBestChain but after AcceptBlock).
1036 // In this case, we need to run ActivateBestChain prior to checking the relay
1037 // conditions below.
1038 CValidationState dummy;
1039 ActivateBestChain(dummy, Params(), a_recent_block);
1041 if (chainActive.Contains(mi->second)) {
1042 send = true;
1043 } else {
1044 send = mi->second->IsValid(BLOCK_VALID_SCRIPTS) &&
1045 StaleBlockRequestAllowed(mi->second, consensusParams);
1046 if (!send) {
1047 LogPrintf("%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom->GetId());
1051 // disconnect node in case we have reached the outbound limit for serving historical blocks
1052 // never disconnect whitelisted nodes
1053 if (send && connman->OutboundTargetReached(true) && ( ((pindexBestHeader != nullptr) && (pindexBestHeader->GetBlockTime() - mi->second->GetBlockTime() > HISTORICAL_BLOCK_AGE)) || inv.type == MSG_FILTERED_BLOCK) && !pfrom->fWhitelisted)
1055 LogPrint(BCLog::NET, "historical block serving limit reached, disconnect peer=%d\n", pfrom->GetId());
1057 //disconnect node
1058 pfrom->fDisconnect = true;
1059 send = false;
1061 // Pruned nodes may have deleted the block, so check whether
1062 // it's available before trying to send.
1063 if (send && (mi->second->nStatus & BLOCK_HAVE_DATA))
1065 std::shared_ptr<const CBlock> pblock;
1066 if (a_recent_block && a_recent_block->GetHash() == (*mi).second->GetBlockHash()) {
1067 pblock = a_recent_block;
1068 } else {
1069 // Send block from disk
1070 std::shared_ptr<CBlock> pblockRead = std::make_shared<CBlock>();
1071 if (!ReadBlockFromDisk(*pblockRead, (*mi).second, consensusParams))
1072 assert(!"cannot load block from disk");
1073 pblock = pblockRead;
1075 if (inv.type == MSG_BLOCK)
1076 connman->PushMessage(pfrom, msgMaker.Make(SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::BLOCK, *pblock));
1077 else if (inv.type == MSG_WITNESS_BLOCK)
1078 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::BLOCK, *pblock));
1079 else if (inv.type == MSG_FILTERED_BLOCK)
1081 bool sendMerkleBlock = false;
1082 CMerkleBlock merkleBlock;
1084 LOCK(pfrom->cs_filter);
1085 if (pfrom->pfilter) {
1086 sendMerkleBlock = true;
1087 merkleBlock = CMerkleBlock(*pblock, *pfrom->pfilter);
1090 if (sendMerkleBlock) {
1091 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::MERKLEBLOCK, merkleBlock));
1092 // CMerkleBlock just contains hashes, so also push any transactions in the block the client did not see
1093 // This avoids hurting performance by pointlessly requiring a round-trip
1094 // Note that there is currently no way for a node to request any single transactions we didn't send here -
1095 // they must either disconnect and retry or request the full block.
1096 // Thus, the protocol spec specified allows for us to provide duplicate txn here,
1097 // however we MUST always provide at least what the remote peer needs
1098 typedef std::pair<unsigned int, uint256> PairType;
1099 for (PairType& pair : merkleBlock.vMatchedTxn)
1100 connman->PushMessage(pfrom, msgMaker.Make(SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::TX, *pblock->vtx[pair.first]));
1102 // else
1103 // no response
1105 else if (inv.type == MSG_CMPCT_BLOCK)
1107 // If a peer is asking for old blocks, we're almost guaranteed
1108 // they won't have a useful mempool to match against a compact block,
1109 // and we don't feel like constructing the object for them, so
1110 // instead we respond with the full, non-compact block.
1111 bool fPeerWantsWitness = State(pfrom->GetId())->fWantsCmpctWitness;
1112 int nSendFlags = fPeerWantsWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
1113 if (CanDirectFetch(consensusParams) && mi->second->nHeight >= chainActive.Height() - MAX_CMPCTBLOCK_DEPTH) {
1114 if ((fPeerWantsWitness || !fWitnessesPresentInARecentCompactBlock) && a_recent_compact_block && a_recent_compact_block->header.GetHash() == mi->second->GetBlockHash()) {
1115 connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, *a_recent_compact_block));
1116 } else {
1117 CBlockHeaderAndShortTxIDs cmpctblock(*pblock, fPeerWantsWitness);
1118 connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
1120 } else {
1121 connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::BLOCK, *pblock));
1125 // Trigger the peer node to send a getblocks request for the next batch of inventory
1126 if (inv.hash == pfrom->hashContinue)
1128 // Bypass PushInventory, this must send even if redundant,
1129 // and we want it right after the last block so they don't
1130 // wait for other stuff first.
1131 std::vector<CInv> vInv;
1132 vInv.push_back(CInv(MSG_BLOCK, chainActive.Tip()->GetBlockHash()));
1133 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::INV, vInv));
1134 pfrom->hashContinue.SetNull();
1138 else if (inv.type == MSG_TX || inv.type == MSG_WITNESS_TX)
1140 // Send stream from relay memory
1141 bool push = false;
1142 auto mi = mapRelay.find(inv.hash);
1143 int nSendFlags = (inv.type == MSG_TX ? SERIALIZE_TRANSACTION_NO_WITNESS : 0);
1144 if (mi != mapRelay.end()) {
1145 connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *mi->second));
1146 push = true;
1147 } else if (pfrom->timeLastMempoolReq) {
1148 auto txinfo = mempool.info(inv.hash);
1149 // To protect privacy, do not answer getdata using the mempool when
1150 // that TX couldn't have been INVed in reply to a MEMPOOL request.
1151 if (txinfo.tx && txinfo.nTime <= pfrom->timeLastMempoolReq) {
1152 connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *txinfo.tx));
1153 push = true;
1156 if (!push) {
1157 vNotFound.push_back(inv);
1161 // Track requests for our stuff.
1162 GetMainSignals().Inventory(inv.hash);
1164 if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK || inv.type == MSG_CMPCT_BLOCK || inv.type == MSG_WITNESS_BLOCK)
1165 break;
1169 pfrom->vRecvGetData.erase(pfrom->vRecvGetData.begin(), it);
1171 if (!vNotFound.empty()) {
1172 // Let the peer know that we didn't find what it asked for, so it doesn't
1173 // have to wait around forever. Currently only SPV clients actually care
1174 // about this message: it's needed when they are recursively walking the
1175 // dependencies of relevant unconfirmed transactions. SPV clients want to
1176 // do that because they want to know about (and store and rebroadcast and
1177 // risk analyze) the dependencies of transactions relevant to them, without
1178 // having to download the entire memory pool.
1179 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::NOTFOUND, vNotFound));
1183 uint32_t GetFetchFlags(CNode* pfrom) {
1184 uint32_t nFetchFlags = 0;
1185 if ((pfrom->GetLocalServices() & NODE_WITNESS) && State(pfrom->GetId())->fHaveWitness) {
1186 nFetchFlags |= MSG_WITNESS_FLAG;
1188 return nFetchFlags;
1191 inline void static SendBlockTransactions(const CBlock& block, const BlockTransactionsRequest& req, CNode* pfrom, CConnman* connman) {
1192 BlockTransactions resp(req);
1193 for (size_t i = 0; i < req.indexes.size(); i++) {
1194 if (req.indexes[i] >= block.vtx.size()) {
1195 LOCK(cs_main);
1196 Misbehaving(pfrom->GetId(), 100);
1197 LogPrintf("Peer %d sent us a getblocktxn with out-of-bounds tx indices", pfrom->GetId());
1198 return;
1200 resp.txn[i] = block.vtx[req.indexes[i]];
1202 LOCK(cs_main);
1203 const CNetMsgMaker msgMaker(pfrom->GetSendVersion());
1204 int nSendFlags = State(pfrom->GetId())->fWantsCmpctWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
1205 connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::BLOCKTXN, resp));
1208 bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, int64_t nTimeReceived, const CChainParams& chainparams, CConnman* connman, const std::atomic<bool>& interruptMsgProc)
1210 LogPrint(BCLog::NET, "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand), vRecv.size(), pfrom->GetId());
1211 if (gArgs.IsArgSet("-dropmessagestest") && GetRand(gArgs.GetArg("-dropmessagestest", 0)) == 0)
1213 LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n");
1214 return true;
1218 if (!(pfrom->GetLocalServices() & NODE_BLOOM) &&
1219 (strCommand == NetMsgType::FILTERLOAD ||
1220 strCommand == NetMsgType::FILTERADD))
1222 if (pfrom->nVersion >= NO_BLOOM_VERSION) {
1223 LOCK(cs_main);
1224 Misbehaving(pfrom->GetId(), 100);
1225 return false;
1226 } else {
1227 pfrom->fDisconnect = true;
1228 return false;
1232 if (strCommand == NetMsgType::REJECT)
1234 if (LogAcceptCategory(BCLog::NET)) {
1235 try {
1236 std::string strMsg; unsigned char ccode; std::string strReason;
1237 vRecv >> LIMITED_STRING(strMsg, CMessageHeader::COMMAND_SIZE) >> ccode >> LIMITED_STRING(strReason, MAX_REJECT_MESSAGE_LENGTH);
1239 std::ostringstream ss;
1240 ss << strMsg << " code " << itostr(ccode) << ": " << strReason;
1242 if (strMsg == NetMsgType::BLOCK || strMsg == NetMsgType::TX)
1244 uint256 hash;
1245 vRecv >> hash;
1246 ss << ": hash " << hash.ToString();
1248 LogPrint(BCLog::NET, "Reject %s\n", SanitizeString(ss.str()));
1249 } catch (const std::ios_base::failure&) {
1250 // Avoid feedback loops by preventing reject messages from triggering a new reject message.
1251 LogPrint(BCLog::NET, "Unparseable reject message received\n");
1256 else if (strCommand == NetMsgType::VERSION)
1258 // Each connection can only send one version message
1259 if (pfrom->nVersion != 0)
1261 connman->PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_DUPLICATE, std::string("Duplicate version message")));
1262 LOCK(cs_main);
1263 Misbehaving(pfrom->GetId(), 1);
1264 return false;
1267 int64_t nTime;
1268 CAddress addrMe;
1269 CAddress addrFrom;
1270 uint64_t nNonce = 1;
1271 uint64_t nServiceInt;
1272 ServiceFlags nServices;
1273 int nVersion;
1274 int nSendVersion;
1275 std::string strSubVer;
1276 std::string cleanSubVer;
1277 int nStartingHeight = -1;
1278 bool fRelay = true;
1280 vRecv >> nVersion >> nServiceInt >> nTime >> addrMe;
1281 nSendVersion = std::min(nVersion, PROTOCOL_VERSION);
1282 nServices = ServiceFlags(nServiceInt);
1283 if (!pfrom->fInbound)
1285 connman->SetServices(pfrom->addr, nServices);
1287 if (!pfrom->fInbound && !pfrom->fFeeler && !pfrom->m_manual_connection && !HasAllDesirableServiceFlags(nServices))
1289 LogPrint(BCLog::NET, "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom->GetId(), nServices, GetDesirableServiceFlags(nServices));
1290 connman->PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_NONSTANDARD,
1291 strprintf("Expected to offer services %08x", GetDesirableServiceFlags(nServices))));
1292 pfrom->fDisconnect = true;
1293 return false;
1296 if (nServices & ((1 << 7) | (1 << 5))) {
1297 if (GetTime() < 1533096000) {
1298 // Immediately disconnect peers that use service bits 6 or 8 until August 1st, 2018
1299 // These bits have been used as a flag to indicate that a node is running incompatible
1300 // consensus rules instead of changing the network magic, so we're stuck disconnecting
1301 // based on these service bits, at least for a while.
1302 pfrom->fDisconnect = true;
1303 return false;
1307 if (nVersion < MIN_PEER_PROTO_VERSION)
1309 // disconnect from peers older than this proto version
1310 LogPrintf("peer=%d using obsolete version %i; disconnecting\n", pfrom->GetId(), nVersion);
1311 connman->PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_OBSOLETE,
1312 strprintf("Version must be %d or greater", MIN_PEER_PROTO_VERSION)));
1313 pfrom->fDisconnect = true;
1314 return false;
1317 if (nVersion == 10300)
1318 nVersion = 300;
1319 if (!vRecv.empty())
1320 vRecv >> addrFrom >> nNonce;
1321 if (!vRecv.empty()) {
1322 vRecv >> LIMITED_STRING(strSubVer, MAX_SUBVERSION_LENGTH);
1323 cleanSubVer = SanitizeString(strSubVer);
1325 if (!vRecv.empty()) {
1326 vRecv >> nStartingHeight;
1328 if (!vRecv.empty())
1329 vRecv >> fRelay;
1330 // Disconnect if we connected to ourself
1331 if (pfrom->fInbound && !connman->CheckIncomingNonce(nNonce))
1333 LogPrintf("connected to self at %s, disconnecting\n", pfrom->addr.ToString());
1334 pfrom->fDisconnect = true;
1335 return true;
1338 if (pfrom->fInbound && addrMe.IsRoutable())
1340 SeenLocal(addrMe);
1343 // Be shy and don't send version until we hear
1344 if (pfrom->fInbound)
1345 PushNodeVersion(pfrom, connman, GetAdjustedTime());
1347 connman->PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERACK));
1349 pfrom->nServices = nServices;
1350 pfrom->SetAddrLocal(addrMe);
1352 LOCK(pfrom->cs_SubVer);
1353 pfrom->strSubVer = strSubVer;
1354 pfrom->cleanSubVer = cleanSubVer;
1356 pfrom->nStartingHeight = nStartingHeight;
1357 pfrom->fClient = !(nServices & NODE_NETWORK);
1359 LOCK(pfrom->cs_filter);
1360 pfrom->fRelayTxes = fRelay; // set to true after we get the first filter* message
1363 // Change version
1364 pfrom->SetSendVersion(nSendVersion);
1365 pfrom->nVersion = nVersion;
1367 if((nServices & NODE_WITNESS))
1369 LOCK(cs_main);
1370 State(pfrom->GetId())->fHaveWitness = true;
1373 // Potentially mark this peer as a preferred download peer.
1375 LOCK(cs_main);
1376 UpdatePreferredDownload(pfrom, State(pfrom->GetId()));
1379 if (!pfrom->fInbound)
1381 // Advertise our address
1382 if (fListen && !IsInitialBlockDownload())
1384 CAddress addr = GetLocalAddress(&pfrom->addr, pfrom->GetLocalServices());
1385 FastRandomContext insecure_rand;
1386 if (addr.IsRoutable())
1388 LogPrint(BCLog::NET, "ProcessMessages: advertising address %s\n", addr.ToString());
1389 pfrom->PushAddress(addr, insecure_rand);
1390 } else if (IsPeerAddrLocalGood(pfrom)) {
1391 addr.SetIP(addrMe);
1392 LogPrint(BCLog::NET, "ProcessMessages: advertising address %s\n", addr.ToString());
1393 pfrom->PushAddress(addr, insecure_rand);
1397 // Get recent addresses
1398 if (pfrom->fOneShot || pfrom->nVersion >= CADDR_TIME_VERSION || connman->GetAddressCount() < 1000)
1400 connman->PushMessage(pfrom, CNetMsgMaker(nSendVersion).Make(NetMsgType::GETADDR));
1401 pfrom->fGetAddr = true;
1403 connman->MarkAddressGood(pfrom->addr);
1406 std::string remoteAddr;
1407 if (fLogIPs)
1408 remoteAddr = ", peeraddr=" + pfrom->addr.ToString();
1410 LogPrintf("receive version message: %s: version %d, blocks=%d, us=%s, peer=%d%s\n",
1411 cleanSubVer, pfrom->nVersion,
1412 pfrom->nStartingHeight, addrMe.ToString(), pfrom->GetId(),
1413 remoteAddr);
1415 int64_t nTimeOffset = nTime - GetTime();
1416 pfrom->nTimeOffset = nTimeOffset;
1417 AddTimeData(pfrom->addr, nTimeOffset);
1419 // If the peer is old enough to have the old alert system, send it the final alert.
1420 if (pfrom->nVersion <= 70012) {
1421 CDataStream finalAlert(ParseHex("60010000000000000000000000ffffff7f00000000ffffff7ffeffff7f01ffffff7f00000000ffffff7f00ffffff7f002f555247454e543a20416c657274206b657920636f6d70726f6d697365642c2075706772616465207265717569726564004630440220653febd6410f470f6bae11cad19c48413becb1ac2c17f908fd0fd53bdc3abd5202206d0e9c96fe88d4a0f01ed9dedae2b6f9e00da94cad0fecaae66ecf689bf71b50"), SER_NETWORK, PROTOCOL_VERSION);
1422 connman->PushMessage(pfrom, CNetMsgMaker(nSendVersion).Make("alert", finalAlert));
1425 // Feeler connections exist only to verify if address is online.
1426 if (pfrom->fFeeler) {
1427 assert(pfrom->fInbound == false);
1428 pfrom->fDisconnect = true;
1430 return true;
1434 else if (pfrom->nVersion == 0)
1436 // Must have a version message before anything else
1437 LOCK(cs_main);
1438 Misbehaving(pfrom->GetId(), 1);
1439 return false;
1442 // At this point, the outgoing message serialization version can't change.
1443 const CNetMsgMaker msgMaker(pfrom->GetSendVersion());
1445 if (strCommand == NetMsgType::VERACK)
1447 pfrom->SetRecvVersion(std::min(pfrom->nVersion.load(), PROTOCOL_VERSION));
1449 if (!pfrom->fInbound) {
1450 // Mark this node as currently connected, so we update its timestamp later.
1451 LOCK(cs_main);
1452 State(pfrom->GetId())->fCurrentlyConnected = true;
1455 if (pfrom->nVersion >= SENDHEADERS_VERSION) {
1456 // Tell our peer we prefer to receive headers rather than inv's
1457 // We send this to non-NODE NETWORK peers as well, because even
1458 // non-NODE NETWORK peers can announce blocks (such as pruning
1459 // nodes)
1460 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::SENDHEADERS));
1462 if (pfrom->nVersion >= SHORT_IDS_BLOCKS_VERSION) {
1463 // Tell our peer we are willing to provide version 1 or 2 cmpctblocks
1464 // However, we do not request new block announcements using
1465 // cmpctblock messages.
1466 // We send this to non-NODE NETWORK peers as well, because
1467 // they may wish to request compact blocks from us
1468 bool fAnnounceUsingCMPCTBLOCK = false;
1469 uint64_t nCMPCTBLOCKVersion = 2;
1470 if (pfrom->GetLocalServices() & NODE_WITNESS)
1471 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
1472 nCMPCTBLOCKVersion = 1;
1473 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
1475 pfrom->fSuccessfullyConnected = true;
1478 else if (!pfrom->fSuccessfullyConnected)
1480 // Must have a verack message before anything else
1481 LOCK(cs_main);
1482 Misbehaving(pfrom->GetId(), 1);
1483 return false;
1486 else if (strCommand == NetMsgType::ADDR)
1488 std::vector<CAddress> vAddr;
1489 vRecv >> vAddr;
1491 // Don't want addr from older versions unless seeding
1492 if (pfrom->nVersion < CADDR_TIME_VERSION && connman->GetAddressCount() > 1000)
1493 return true;
1494 if (vAddr.size() > 1000)
1496 LOCK(cs_main);
1497 Misbehaving(pfrom->GetId(), 20);
1498 return error("message addr size() = %u", vAddr.size());
1501 // Store the new addresses
1502 std::vector<CAddress> vAddrOk;
1503 int64_t nNow = GetAdjustedTime();
1504 int64_t nSince = nNow - 10 * 60;
1505 for (CAddress& addr : vAddr)
1507 if (interruptMsgProc)
1508 return true;
1510 // We only bother storing full nodes, though this may include
1511 // things which we would not make an outbound connection to, in
1512 // part because we may make feeler connections to them.
1513 if (!MayHaveUsefulAddressDB(addr.nServices))
1514 continue;
1516 if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
1517 addr.nTime = nNow - 5 * 24 * 60 * 60;
1518 pfrom->AddAddressKnown(addr);
1519 bool fReachable = IsReachable(addr);
1520 if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
1522 // Relay to a limited number of other nodes
1523 RelayAddress(addr, fReachable, connman);
1525 // Do not store addresses outside our network
1526 if (fReachable)
1527 vAddrOk.push_back(addr);
1529 connman->AddNewAddresses(vAddrOk, pfrom->addr, 2 * 60 * 60);
1530 if (vAddr.size() < 1000)
1531 pfrom->fGetAddr = false;
1532 if (pfrom->fOneShot)
1533 pfrom->fDisconnect = true;
1536 else if (strCommand == NetMsgType::SENDHEADERS)
1538 LOCK(cs_main);
1539 State(pfrom->GetId())->fPreferHeaders = true;
1542 else if (strCommand == NetMsgType::SENDCMPCT)
1544 bool fAnnounceUsingCMPCTBLOCK = false;
1545 uint64_t nCMPCTBLOCKVersion = 0;
1546 vRecv >> fAnnounceUsingCMPCTBLOCK >> nCMPCTBLOCKVersion;
1547 if (nCMPCTBLOCKVersion == 1 || ((pfrom->GetLocalServices() & NODE_WITNESS) && nCMPCTBLOCKVersion == 2)) {
1548 LOCK(cs_main);
1549 // fProvidesHeaderAndIDs is used to "lock in" version of compact blocks we send (fWantsCmpctWitness)
1550 if (!State(pfrom->GetId())->fProvidesHeaderAndIDs) {
1551 State(pfrom->GetId())->fProvidesHeaderAndIDs = true;
1552 State(pfrom->GetId())->fWantsCmpctWitness = nCMPCTBLOCKVersion == 2;
1554 if (State(pfrom->GetId())->fWantsCmpctWitness == (nCMPCTBLOCKVersion == 2)) // ignore later version announces
1555 State(pfrom->GetId())->fPreferHeaderAndIDs = fAnnounceUsingCMPCTBLOCK;
1556 if (!State(pfrom->GetId())->fSupportsDesiredCmpctVersion) {
1557 if (pfrom->GetLocalServices() & NODE_WITNESS)
1558 State(pfrom->GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 2);
1559 else
1560 State(pfrom->GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 1);
1566 else if (strCommand == NetMsgType::INV)
1568 std::vector<CInv> vInv;
1569 vRecv >> vInv;
1570 if (vInv.size() > MAX_INV_SZ)
1572 LOCK(cs_main);
1573 Misbehaving(pfrom->GetId(), 20);
1574 return error("message inv size() = %u", vInv.size());
1577 bool fBlocksOnly = !fRelayTxes;
1579 // Allow whitelisted peers to send data other than blocks in blocks only mode if whitelistrelay is true
1580 if (pfrom->fWhitelisted && gArgs.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY))
1581 fBlocksOnly = false;
1583 LOCK(cs_main);
1585 uint32_t nFetchFlags = GetFetchFlags(pfrom);
1587 for (CInv &inv : vInv)
1589 if (interruptMsgProc)
1590 return true;
1592 bool fAlreadyHave = AlreadyHave(inv);
1593 LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom->GetId());
1595 if (inv.type == MSG_TX) {
1596 inv.type |= nFetchFlags;
1599 if (inv.type == MSG_BLOCK) {
1600 UpdateBlockAvailability(pfrom->GetId(), inv.hash);
1601 if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count(inv.hash)) {
1602 // We used to request the full block here, but since headers-announcements are now the
1603 // primary method of announcement on the network, and since, in the case that a node
1604 // fell back to inv we probably have a reorg which we should get the headers for first,
1605 // we now only provide a getheaders response here. When we receive the headers, we will
1606 // then ask for the blocks we need.
1607 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), inv.hash));
1608 LogPrint(BCLog::NET, "getheaders (%d) %s to peer=%d\n", pindexBestHeader->nHeight, inv.hash.ToString(), pfrom->GetId());
1611 else
1613 pfrom->AddInventoryKnown(inv);
1614 if (fBlocksOnly) {
1615 LogPrint(BCLog::NET, "transaction (%s) inv sent in violation of protocol peer=%d\n", inv.hash.ToString(), pfrom->GetId());
1616 } else if (!fAlreadyHave && !fImporting && !fReindex && !IsInitialBlockDownload()) {
1617 pfrom->AskFor(inv);
1621 // Track requests for our stuff
1622 GetMainSignals().Inventory(inv.hash);
1627 else if (strCommand == NetMsgType::GETDATA)
1629 std::vector<CInv> vInv;
1630 vRecv >> vInv;
1631 if (vInv.size() > MAX_INV_SZ)
1633 LOCK(cs_main);
1634 Misbehaving(pfrom->GetId(), 20);
1635 return error("message getdata size() = %u", vInv.size());
1638 LogPrint(BCLog::NET, "received getdata (%u invsz) peer=%d\n", vInv.size(), pfrom->GetId());
1640 if (vInv.size() > 0) {
1641 LogPrint(BCLog::NET, "received getdata for: %s peer=%d\n", vInv[0].ToString(), pfrom->GetId());
1644 pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end());
1645 ProcessGetData(pfrom, chainparams.GetConsensus(), connman, interruptMsgProc);
1649 else if (strCommand == NetMsgType::GETBLOCKS)
1651 CBlockLocator locator;
1652 uint256 hashStop;
1653 vRecv >> locator >> hashStop;
1655 // We might have announced the currently-being-connected tip using a
1656 // compact block, which resulted in the peer sending a getblocks
1657 // request, which we would otherwise respond to without the new block.
1658 // To avoid this situation we simply verify that we are on our best
1659 // known chain now. This is super overkill, but we handle it better
1660 // for getheaders requests, and there are no known nodes which support
1661 // compact blocks but still use getblocks to request blocks.
1663 std::shared_ptr<const CBlock> a_recent_block;
1665 LOCK(cs_most_recent_block);
1666 a_recent_block = most_recent_block;
1668 CValidationState dummy;
1669 ActivateBestChain(dummy, Params(), a_recent_block);
1672 LOCK(cs_main);
1674 // Find the last block the caller has in the main chain
1675 const CBlockIndex* pindex = FindForkInGlobalIndex(chainActive, locator);
1677 // Send the rest of the chain
1678 if (pindex)
1679 pindex = chainActive.Next(pindex);
1680 int nLimit = 500;
1681 LogPrint(BCLog::NET, "getblocks %d to %s limit %d from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), nLimit, pfrom->GetId());
1682 for (; pindex; pindex = chainActive.Next(pindex))
1684 if (pindex->GetBlockHash() == hashStop)
1686 LogPrint(BCLog::NET, " getblocks stopping at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
1687 break;
1689 // If pruning, don't inv blocks unless we have on disk and are likely to still have
1690 // for some reasonable time window (1 hour) that block relay might require.
1691 const int nPrunedBlocksLikelyToHave = MIN_BLOCKS_TO_KEEP - 3600 / chainparams.GetConsensus().nPowTargetSpacing;
1692 if (fPruneMode && (!(pindex->nStatus & BLOCK_HAVE_DATA) || pindex->nHeight <= chainActive.Tip()->nHeight - nPrunedBlocksLikelyToHave))
1694 LogPrint(BCLog::NET, " getblocks stopping, pruned or too old block at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
1695 break;
1697 pfrom->PushInventory(CInv(MSG_BLOCK, pindex->GetBlockHash()));
1698 if (--nLimit <= 0)
1700 // When this block is requested, we'll send an inv that'll
1701 // trigger the peer to getblocks the next batch of inventory.
1702 LogPrint(BCLog::NET, " getblocks stopping at limit %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
1703 pfrom->hashContinue = pindex->GetBlockHash();
1704 break;
1710 else if (strCommand == NetMsgType::GETBLOCKTXN)
1712 BlockTransactionsRequest req;
1713 vRecv >> req;
1715 std::shared_ptr<const CBlock> recent_block;
1717 LOCK(cs_most_recent_block);
1718 if (most_recent_block_hash == req.blockhash)
1719 recent_block = most_recent_block;
1720 // Unlock cs_most_recent_block to avoid cs_main lock inversion
1722 if (recent_block) {
1723 SendBlockTransactions(*recent_block, req, pfrom, connman);
1724 return true;
1727 LOCK(cs_main);
1729 BlockMap::iterator it = mapBlockIndex.find(req.blockhash);
1730 if (it == mapBlockIndex.end() || !(it->second->nStatus & BLOCK_HAVE_DATA)) {
1731 LogPrintf("Peer %d sent us a getblocktxn for a block we don't have", pfrom->GetId());
1732 return true;
1735 if (it->second->nHeight < chainActive.Height() - MAX_BLOCKTXN_DEPTH) {
1736 // If an older block is requested (should never happen in practice,
1737 // but can happen in tests) send a block response instead of a
1738 // blocktxn response. Sending a full block response instead of a
1739 // small blocktxn response is preferable in the case where a peer
1740 // might maliciously send lots of getblocktxn requests to trigger
1741 // expensive disk reads, because it will require the peer to
1742 // actually receive all the data read from disk over the network.
1743 LogPrint(BCLog::NET, "Peer %d sent us a getblocktxn for a block > %i deep", pfrom->GetId(), MAX_BLOCKTXN_DEPTH);
1744 CInv inv;
1745 inv.type = State(pfrom->GetId())->fWantsCmpctWitness ? MSG_WITNESS_BLOCK : MSG_BLOCK;
1746 inv.hash = req.blockhash;
1747 pfrom->vRecvGetData.push_back(inv);
1748 ProcessGetData(pfrom, chainparams.GetConsensus(), connman, interruptMsgProc);
1749 return true;
1752 CBlock block;
1753 bool ret = ReadBlockFromDisk(block, it->second, chainparams.GetConsensus());
1754 assert(ret);
1756 SendBlockTransactions(block, req, pfrom, connman);
1760 else if (strCommand == NetMsgType::GETHEADERS)
1762 CBlockLocator locator;
1763 uint256 hashStop;
1764 vRecv >> locator >> hashStop;
1766 LOCK(cs_main);
1767 if (IsInitialBlockDownload() && !pfrom->fWhitelisted) {
1768 LogPrint(BCLog::NET, "Ignoring getheaders from peer=%d because node is in initial block download\n", pfrom->GetId());
1769 return true;
1772 CNodeState *nodestate = State(pfrom->GetId());
1773 const CBlockIndex* pindex = nullptr;
1774 if (locator.IsNull())
1776 // If locator is null, return the hashStop block
1777 BlockMap::iterator mi = mapBlockIndex.find(hashStop);
1778 if (mi == mapBlockIndex.end())
1779 return true;
1780 pindex = (*mi).second;
1782 if (!chainActive.Contains(pindex) &&
1783 !StaleBlockRequestAllowed(pindex, chainparams.GetConsensus())) {
1784 LogPrintf("%s: ignoring request from peer=%i for old block header that isn't in the main chain\n", __func__, pfrom->GetId());
1785 return true;
1788 else
1790 // Find the last block the caller has in the main chain
1791 pindex = FindForkInGlobalIndex(chainActive, locator);
1792 if (pindex)
1793 pindex = chainActive.Next(pindex);
1796 // we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
1797 std::vector<CBlock> vHeaders;
1798 int nLimit = MAX_HEADERS_RESULTS;
1799 LogPrint(BCLog::NET, "getheaders %d to %s from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), pfrom->GetId());
1800 for (; pindex; pindex = chainActive.Next(pindex))
1802 vHeaders.push_back(pindex->GetBlockHeader());
1803 if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
1804 break;
1806 // pindex can be nullptr either if we sent chainActive.Tip() OR
1807 // if our peer has chainActive.Tip() (and thus we are sending an empty
1808 // headers message). In both cases it's safe to update
1809 // pindexBestHeaderSent to be our tip.
1811 // It is important that we simply reset the BestHeaderSent value here,
1812 // and not max(BestHeaderSent, newHeaderSent). We might have announced
1813 // the currently-being-connected tip using a compact block, which
1814 // resulted in the peer sending a headers request, which we respond to
1815 // without the new block. By resetting the BestHeaderSent, we ensure we
1816 // will re-announce the new block via headers (or compact blocks again)
1817 // in the SendMessages logic.
1818 nodestate->pindexBestHeaderSent = pindex ? pindex : chainActive.Tip();
1819 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
1823 else if (strCommand == NetMsgType::TX)
1825 // Stop processing the transaction early if
1826 // We are in blocks only mode and peer is either not whitelisted or whitelistrelay is off
1827 if (!fRelayTxes && (!pfrom->fWhitelisted || !gArgs.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY)))
1829 LogPrint(BCLog::NET, "transaction sent in violation of protocol peer=%d\n", pfrom->GetId());
1830 return true;
1833 std::deque<COutPoint> vWorkQueue;
1834 std::vector<uint256> vEraseQueue;
1835 CTransactionRef ptx;
1836 vRecv >> ptx;
1837 const CTransaction& tx = *ptx;
1839 CInv inv(MSG_TX, tx.GetHash());
1840 pfrom->AddInventoryKnown(inv);
1842 LOCK(cs_main);
1844 bool fMissingInputs = false;
1845 CValidationState state;
1847 pfrom->setAskFor.erase(inv.hash);
1848 mapAlreadyAskedFor.erase(inv.hash);
1850 std::list<CTransactionRef> lRemovedTxn;
1852 if (!AlreadyHave(inv) &&
1853 AcceptToMemoryPool(mempool, state, ptx, &fMissingInputs, &lRemovedTxn, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
1854 mempool.check(pcoinsTip);
1855 RelayTransaction(tx, connman);
1856 for (unsigned int i = 0; i < tx.vout.size(); i++) {
1857 vWorkQueue.emplace_back(inv.hash, i);
1860 pfrom->nLastTXTime = GetTime();
1862 LogPrint(BCLog::MEMPOOL, "AcceptToMemoryPool: peer=%d: accepted %s (poolsz %u txn, %u kB)\n",
1863 pfrom->GetId(),
1864 tx.GetHash().ToString(),
1865 mempool.size(), mempool.DynamicMemoryUsage() / 1000);
1867 // Recursively process any orphan transactions that depended on this one
1868 std::set<NodeId> setMisbehaving;
1869 while (!vWorkQueue.empty()) {
1870 auto itByPrev = mapOrphanTransactionsByPrev.find(vWorkQueue.front());
1871 vWorkQueue.pop_front();
1872 if (itByPrev == mapOrphanTransactionsByPrev.end())
1873 continue;
1874 for (auto mi = itByPrev->second.begin();
1875 mi != itByPrev->second.end();
1876 ++mi)
1878 const CTransactionRef& porphanTx = (*mi)->second.tx;
1879 const CTransaction& orphanTx = *porphanTx;
1880 const uint256& orphanHash = orphanTx.GetHash();
1881 NodeId fromPeer = (*mi)->second.fromPeer;
1882 bool fMissingInputs2 = false;
1883 // Use a dummy CValidationState so someone can't setup nodes to counter-DoS based on orphan
1884 // resolution (that is, feeding people an invalid transaction based on LegitTxX in order to get
1885 // anyone relaying LegitTxX banned)
1886 CValidationState stateDummy;
1889 if (setMisbehaving.count(fromPeer))
1890 continue;
1891 if (AcceptToMemoryPool(mempool, stateDummy, porphanTx, &fMissingInputs2, &lRemovedTxn, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
1892 LogPrint(BCLog::MEMPOOL, " accepted orphan tx %s\n", orphanHash.ToString());
1893 RelayTransaction(orphanTx, connman);
1894 for (unsigned int i = 0; i < orphanTx.vout.size(); i++) {
1895 vWorkQueue.emplace_back(orphanHash, i);
1897 vEraseQueue.push_back(orphanHash);
1899 else if (!fMissingInputs2)
1901 int nDos = 0;
1902 if (stateDummy.IsInvalid(nDos) && nDos > 0)
1904 // Punish peer that gave us an invalid orphan tx
1905 Misbehaving(fromPeer, nDos);
1906 setMisbehaving.insert(fromPeer);
1907 LogPrint(BCLog::MEMPOOL, " invalid orphan tx %s\n", orphanHash.ToString());
1909 // Has inputs but not accepted to mempool
1910 // Probably non-standard or insufficient fee
1911 LogPrint(BCLog::MEMPOOL, " removed orphan tx %s\n", orphanHash.ToString());
1912 vEraseQueue.push_back(orphanHash);
1913 if (!orphanTx.HasWitness() && !stateDummy.CorruptionPossible()) {
1914 // Do not use rejection cache for witness transactions or
1915 // witness-stripped transactions, as they can have been malleated.
1916 // See https://github.com/bitcoin/bitcoin/issues/8279 for details.
1917 assert(recentRejects);
1918 recentRejects->insert(orphanHash);
1921 mempool.check(pcoinsTip);
1925 for (uint256 hash : vEraseQueue)
1926 EraseOrphanTx(hash);
1928 else if (fMissingInputs)
1930 bool fRejectedParents = false; // It may be the case that the orphans parents have all been rejected
1931 for (const CTxIn& txin : tx.vin) {
1932 if (recentRejects->contains(txin.prevout.hash)) {
1933 fRejectedParents = true;
1934 break;
1937 if (!fRejectedParents) {
1938 uint32_t nFetchFlags = GetFetchFlags(pfrom);
1939 for (const CTxIn& txin : tx.vin) {
1940 CInv _inv(MSG_TX | nFetchFlags, txin.prevout.hash);
1941 pfrom->AddInventoryKnown(_inv);
1942 if (!AlreadyHave(_inv)) pfrom->AskFor(_inv);
1944 AddOrphanTx(ptx, pfrom->GetId());
1946 // DoS prevention: do not allow mapOrphanTransactions to grow unbounded
1947 unsigned int nMaxOrphanTx = (unsigned int)std::max((int64_t)0, gArgs.GetArg("-maxorphantx", DEFAULT_MAX_ORPHAN_TRANSACTIONS));
1948 unsigned int nEvicted = LimitOrphanTxSize(nMaxOrphanTx);
1949 if (nEvicted > 0) {
1950 LogPrint(BCLog::MEMPOOL, "mapOrphan overflow, removed %u tx\n", nEvicted);
1952 } else {
1953 LogPrint(BCLog::MEMPOOL, "not keeping orphan with rejected parents %s\n",tx.GetHash().ToString());
1954 // We will continue to reject this tx since it has rejected
1955 // parents so avoid re-requesting it from other peers.
1956 recentRejects->insert(tx.GetHash());
1958 } else {
1959 if (!tx.HasWitness() && !state.CorruptionPossible()) {
1960 // Do not use rejection cache for witness transactions or
1961 // witness-stripped transactions, as they can have been malleated.
1962 // See https://github.com/bitcoin/bitcoin/issues/8279 for details.
1963 assert(recentRejects);
1964 recentRejects->insert(tx.GetHash());
1965 if (RecursiveDynamicUsage(*ptx) < 100000) {
1966 AddToCompactExtraTransactions(ptx);
1968 } else if (tx.HasWitness() && RecursiveDynamicUsage(*ptx) < 100000) {
1969 AddToCompactExtraTransactions(ptx);
1972 if (pfrom->fWhitelisted && gArgs.GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY)) {
1973 // Always relay transactions received from whitelisted peers, even
1974 // if they were already in the mempool or rejected from it due
1975 // to policy, allowing the node to function as a gateway for
1976 // nodes hidden behind it.
1978 // Never relay transactions that we would assign a non-zero DoS
1979 // score for, as we expect peers to do the same with us in that
1980 // case.
1981 int nDoS = 0;
1982 if (!state.IsInvalid(nDoS) || nDoS == 0) {
1983 LogPrintf("Force relaying tx %s from whitelisted peer=%d\n", tx.GetHash().ToString(), pfrom->GetId());
1984 RelayTransaction(tx, connman);
1985 } else {
1986 LogPrintf("Not relaying invalid transaction %s from whitelisted peer=%d (%s)\n", tx.GetHash().ToString(), pfrom->GetId(), FormatStateMessage(state));
1991 for (const CTransactionRef& removedTx : lRemovedTxn)
1992 AddToCompactExtraTransactions(removedTx);
1994 int nDoS = 0;
1995 if (state.IsInvalid(nDoS))
1997 LogPrint(BCLog::MEMPOOLREJ, "%s from peer=%d was not accepted: %s\n", tx.GetHash().ToString(),
1998 pfrom->GetId(),
1999 FormatStateMessage(state));
2000 if (state.GetRejectCode() > 0 && state.GetRejectCode() < REJECT_INTERNAL) // Never send AcceptToMemoryPool's internal codes over P2P
2001 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::REJECT, strCommand, (unsigned char)state.GetRejectCode(),
2002 state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), inv.hash));
2003 if (nDoS > 0) {
2004 Misbehaving(pfrom->GetId(), nDoS);
2010 else if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
2012 CBlockHeaderAndShortTxIDs cmpctblock;
2013 vRecv >> cmpctblock;
2016 LOCK(cs_main);
2018 if (mapBlockIndex.find(cmpctblock.header.hashPrevBlock) == mapBlockIndex.end()) {
2019 // Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
2020 if (!IsInitialBlockDownload())
2021 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), uint256()));
2022 return true;
2026 const CBlockIndex *pindex = nullptr;
2027 CValidationState state;
2028 if (!ProcessNewBlockHeaders({cmpctblock.header}, state, chainparams, &pindex)) {
2029 int nDoS;
2030 if (state.IsInvalid(nDoS)) {
2031 if (nDoS > 0) {
2032 LOCK(cs_main);
2033 Misbehaving(pfrom->GetId(), nDoS);
2035 LogPrintf("Peer %d sent us invalid header via cmpctblock\n", pfrom->GetId());
2036 return true;
2040 // When we succeed in decoding a block's txids from a cmpctblock
2041 // message we typically jump to the BLOCKTXN handling code, with a
2042 // dummy (empty) BLOCKTXN message, to re-use the logic there in
2043 // completing processing of the putative block (without cs_main).
2044 bool fProcessBLOCKTXN = false;
2045 CDataStream blockTxnMsg(SER_NETWORK, PROTOCOL_VERSION);
2047 // If we end up treating this as a plain headers message, call that as well
2048 // without cs_main.
2049 bool fRevertToHeaderProcessing = false;
2050 CDataStream vHeadersMsg(SER_NETWORK, PROTOCOL_VERSION);
2052 // Keep a CBlock for "optimistic" compactblock reconstructions (see
2053 // below)
2054 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
2055 bool fBlockReconstructed = false;
2058 LOCK(cs_main);
2059 // If AcceptBlockHeader returned true, it set pindex
2060 assert(pindex);
2061 UpdateBlockAvailability(pfrom->GetId(), pindex->GetBlockHash());
2063 std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator blockInFlightIt = mapBlocksInFlight.find(pindex->GetBlockHash());
2064 bool fAlreadyInFlight = blockInFlightIt != mapBlocksInFlight.end();
2066 if (pindex->nStatus & BLOCK_HAVE_DATA) // Nothing to do here
2067 return true;
2069 if (pindex->nChainWork <= chainActive.Tip()->nChainWork || // We know something better
2070 pindex->nTx != 0) { // We had this block at some point, but pruned it
2071 if (fAlreadyInFlight) {
2072 // We requested this block for some reason, but our mempool will probably be useless
2073 // so we just grab the block via normal getdata
2074 std::vector<CInv> vInv(1);
2075 vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom), cmpctblock.header.GetHash());
2076 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
2078 return true;
2081 // If we're not close to tip yet, give up and let parallel block fetch work its magic
2082 if (!fAlreadyInFlight && !CanDirectFetch(chainparams.GetConsensus()))
2083 return true;
2085 CNodeState *nodestate = State(pfrom->GetId());
2087 if (IsWitnessEnabled(pindex->pprev, chainparams.GetConsensus()) && !nodestate->fSupportsDesiredCmpctVersion) {
2088 // Don't bother trying to process compact blocks from v1 peers
2089 // after segwit activates.
2090 return true;
2093 // We want to be a bit conservative just to be extra careful about DoS
2094 // possibilities in compact block processing...
2095 if (pindex->nHeight <= chainActive.Height() + 2) {
2096 if ((!fAlreadyInFlight && nodestate->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) ||
2097 (fAlreadyInFlight && blockInFlightIt->second.first == pfrom->GetId())) {
2098 std::list<QueuedBlock>::iterator* queuedBlockIt = nullptr;
2099 if (!MarkBlockAsInFlight(pfrom->GetId(), pindex->GetBlockHash(), pindex, &queuedBlockIt)) {
2100 if (!(*queuedBlockIt)->partialBlock)
2101 (*queuedBlockIt)->partialBlock.reset(new PartiallyDownloadedBlock(&mempool));
2102 else {
2103 // The block was already in flight using compact blocks from the same peer
2104 LogPrint(BCLog::NET, "Peer sent us compact block we were already syncing!\n");
2105 return true;
2109 PartiallyDownloadedBlock& partialBlock = *(*queuedBlockIt)->partialBlock;
2110 ReadStatus status = partialBlock.InitData(cmpctblock, vExtraTxnForCompact);
2111 if (status == READ_STATUS_INVALID) {
2112 MarkBlockAsReceived(pindex->GetBlockHash()); // Reset in-flight state in case of whitelist
2113 Misbehaving(pfrom->GetId(), 100);
2114 LogPrintf("Peer %d sent us invalid compact block\n", pfrom->GetId());
2115 return true;
2116 } else if (status == READ_STATUS_FAILED) {
2117 // Duplicate txindexes, the block is now in-flight, so just request it
2118 std::vector<CInv> vInv(1);
2119 vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom), cmpctblock.header.GetHash());
2120 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
2121 return true;
2124 BlockTransactionsRequest req;
2125 for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
2126 if (!partialBlock.IsTxAvailable(i))
2127 req.indexes.push_back(i);
2129 if (req.indexes.empty()) {
2130 // Dirty hack to jump to BLOCKTXN code (TODO: move message handling into their own functions)
2131 BlockTransactions txn;
2132 txn.blockhash = cmpctblock.header.GetHash();
2133 blockTxnMsg << txn;
2134 fProcessBLOCKTXN = true;
2135 } else {
2136 req.blockhash = pindex->GetBlockHash();
2137 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETBLOCKTXN, req));
2139 } else {
2140 // This block is either already in flight from a different
2141 // peer, or this peer has too many blocks outstanding to
2142 // download from.
2143 // Optimistically try to reconstruct anyway since we might be
2144 // able to without any round trips.
2145 PartiallyDownloadedBlock tempBlock(&mempool);
2146 ReadStatus status = tempBlock.InitData(cmpctblock, vExtraTxnForCompact);
2147 if (status != READ_STATUS_OK) {
2148 // TODO: don't ignore failures
2149 return true;
2151 std::vector<CTransactionRef> dummy;
2152 status = tempBlock.FillBlock(*pblock, dummy);
2153 if (status == READ_STATUS_OK) {
2154 fBlockReconstructed = true;
2157 } else {
2158 if (fAlreadyInFlight) {
2159 // We requested this block, but its far into the future, so our
2160 // mempool will probably be useless - request the block normally
2161 std::vector<CInv> vInv(1);
2162 vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom), cmpctblock.header.GetHash());
2163 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
2164 return true;
2165 } else {
2166 // If this was an announce-cmpctblock, we want the same treatment as a header message
2167 // Dirty hack to process as if it were just a headers message (TODO: move message handling into their own functions)
2168 std::vector<CBlock> headers;
2169 headers.push_back(cmpctblock.header);
2170 vHeadersMsg << headers;
2171 fRevertToHeaderProcessing = true;
2174 } // cs_main
2176 if (fProcessBLOCKTXN)
2177 return ProcessMessage(pfrom, NetMsgType::BLOCKTXN, blockTxnMsg, nTimeReceived, chainparams, connman, interruptMsgProc);
2179 if (fRevertToHeaderProcessing)
2180 return ProcessMessage(pfrom, NetMsgType::HEADERS, vHeadersMsg, nTimeReceived, chainparams, connman, interruptMsgProc);
2182 if (fBlockReconstructed) {
2183 // If we got here, we were able to optimistically reconstruct a
2184 // block that is in flight from some other peer.
2186 LOCK(cs_main);
2187 mapBlockSource.emplace(pblock->GetHash(), std::make_pair(pfrom->GetId(), false));
2189 bool fNewBlock = false;
2190 // Setting fForceProcessing to true means that we bypass some of
2191 // our anti-DoS protections in AcceptBlock, which filters
2192 // unrequested blocks that might be trying to waste our resources
2193 // (eg disk space). Because we only try to reconstruct blocks when
2194 // we're close to caught up (via the CanDirectFetch() requirement
2195 // above, combined with the behavior of not requesting blocks until
2196 // we have a chain with at least nMinimumChainWork), and we ignore
2197 // compact blocks with less work than our tip, it is safe to treat
2198 // reconstructed compact blocks as having been requested.
2199 ProcessNewBlock(chainparams, pblock, /*fForceProcessing=*/true, &fNewBlock);
2200 if (fNewBlock) {
2201 pfrom->nLastBlockTime = GetTime();
2202 } else {
2203 LOCK(cs_main);
2204 mapBlockSource.erase(pblock->GetHash());
2206 LOCK(cs_main); // hold cs_main for CBlockIndex::IsValid()
2207 if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS)) {
2208 // Clear download state for this block, which is in
2209 // process from some other peer. We do this after calling
2210 // ProcessNewBlock so that a malleated cmpctblock announcement
2211 // can't be used to interfere with block relay.
2212 MarkBlockAsReceived(pblock->GetHash());
2218 else if (strCommand == NetMsgType::BLOCKTXN && !fImporting && !fReindex) // Ignore blocks received while importing
2220 BlockTransactions resp;
2221 vRecv >> resp;
2223 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
2224 bool fBlockRead = false;
2226 LOCK(cs_main);
2228 std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator it = mapBlocksInFlight.find(resp.blockhash);
2229 if (it == mapBlocksInFlight.end() || !it->second.second->partialBlock ||
2230 it->second.first != pfrom->GetId()) {
2231 LogPrint(BCLog::NET, "Peer %d sent us block transactions for block we weren't expecting\n", pfrom->GetId());
2232 return true;
2235 PartiallyDownloadedBlock& partialBlock = *it->second.second->partialBlock;
2236 ReadStatus status = partialBlock.FillBlock(*pblock, resp.txn);
2237 if (status == READ_STATUS_INVALID) {
2238 MarkBlockAsReceived(resp.blockhash); // Reset in-flight state in case of whitelist
2239 Misbehaving(pfrom->GetId(), 100);
2240 LogPrintf("Peer %d sent us invalid compact block/non-matching block transactions\n", pfrom->GetId());
2241 return true;
2242 } else if (status == READ_STATUS_FAILED) {
2243 // Might have collided, fall back to getdata now :(
2244 std::vector<CInv> invs;
2245 invs.push_back(CInv(MSG_BLOCK | GetFetchFlags(pfrom), resp.blockhash));
2246 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, invs));
2247 } else {
2248 // Block is either okay, or possibly we received
2249 // READ_STATUS_CHECKBLOCK_FAILED.
2250 // Note that CheckBlock can only fail for one of a few reasons:
2251 // 1. bad-proof-of-work (impossible here, because we've already
2252 // accepted the header)
2253 // 2. merkleroot doesn't match the transactions given (already
2254 // caught in FillBlock with READ_STATUS_FAILED, so
2255 // impossible here)
2256 // 3. the block is otherwise invalid (eg invalid coinbase,
2257 // block is too big, too many legacy sigops, etc).
2258 // So if CheckBlock failed, #3 is the only possibility.
2259 // Under BIP 152, we don't DoS-ban unless proof of work is
2260 // invalid (we don't require all the stateless checks to have
2261 // been run). This is handled below, so just treat this as
2262 // though the block was successfully read, and rely on the
2263 // handling in ProcessNewBlock to ensure the block index is
2264 // updated, reject messages go out, etc.
2265 MarkBlockAsReceived(resp.blockhash); // it is now an empty pointer
2266 fBlockRead = true;
2267 // mapBlockSource is only used for sending reject messages and DoS scores,
2268 // so the race between here and cs_main in ProcessNewBlock is fine.
2269 // BIP 152 permits peers to relay compact blocks after validating
2270 // the header only; we should not punish peers if the block turns
2271 // out to be invalid.
2272 mapBlockSource.emplace(resp.blockhash, std::make_pair(pfrom->GetId(), false));
2274 } // Don't hold cs_main when we call into ProcessNewBlock
2275 if (fBlockRead) {
2276 bool fNewBlock = false;
2277 // Since we requested this block (it was in mapBlocksInFlight), force it to be processed,
2278 // even if it would not be a candidate for new tip (missing previous block, chain not long enough, etc)
2279 // This bypasses some anti-DoS logic in AcceptBlock (eg to prevent
2280 // disk-space attacks), but this should be safe due to the
2281 // protections in the compact block handler -- see related comment
2282 // in compact block optimistic reconstruction handling.
2283 ProcessNewBlock(chainparams, pblock, /*fForceProcessing=*/true, &fNewBlock);
2284 if (fNewBlock) {
2285 pfrom->nLastBlockTime = GetTime();
2286 } else {
2287 LOCK(cs_main);
2288 mapBlockSource.erase(pblock->GetHash());
2294 else if (strCommand == NetMsgType::HEADERS && !fImporting && !fReindex) // Ignore headers received while importing
2296 std::vector<CBlockHeader> headers;
2298 // Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks.
2299 unsigned int nCount = ReadCompactSize(vRecv);
2300 if (nCount > MAX_HEADERS_RESULTS) {
2301 LOCK(cs_main);
2302 Misbehaving(pfrom->GetId(), 20);
2303 return error("headers message size = %u", nCount);
2305 headers.resize(nCount);
2306 for (unsigned int n = 0; n < nCount; n++) {
2307 vRecv >> headers[n];
2308 ReadCompactSize(vRecv); // ignore tx count; assume it is 0.
2311 if (nCount == 0) {
2312 // Nothing interesting. Stop asking this peers for more headers.
2313 return true;
2316 const CBlockIndex *pindexLast = nullptr;
2318 LOCK(cs_main);
2319 CNodeState *nodestate = State(pfrom->GetId());
2321 // If this looks like it could be a block announcement (nCount <
2322 // MAX_BLOCKS_TO_ANNOUNCE), use special logic for handling headers that
2323 // don't connect:
2324 // - Send a getheaders message in response to try to connect the chain.
2325 // - The peer can send up to MAX_UNCONNECTING_HEADERS in a row that
2326 // don't connect before giving DoS points
2327 // - Once a headers message is received that is valid and does connect,
2328 // nUnconnectingHeaders gets reset back to 0.
2329 if (mapBlockIndex.find(headers[0].hashPrevBlock) == mapBlockIndex.end() && nCount < MAX_BLOCKS_TO_ANNOUNCE) {
2330 nodestate->nUnconnectingHeaders++;
2331 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), uint256()));
2332 LogPrint(BCLog::NET, "received header %s: missing prev block %s, sending getheaders (%d) to end (peer=%d, nUnconnectingHeaders=%d)\n",
2333 headers[0].GetHash().ToString(),
2334 headers[0].hashPrevBlock.ToString(),
2335 pindexBestHeader->nHeight,
2336 pfrom->GetId(), nodestate->nUnconnectingHeaders);
2337 // Set hashLastUnknownBlock for this peer, so that if we
2338 // eventually get the headers - even from a different peer -
2339 // we can use this peer to download.
2340 UpdateBlockAvailability(pfrom->GetId(), headers.back().GetHash());
2342 if (nodestate->nUnconnectingHeaders % MAX_UNCONNECTING_HEADERS == 0) {
2343 Misbehaving(pfrom->GetId(), 20);
2345 return true;
2348 uint256 hashLastBlock;
2349 for (const CBlockHeader& header : headers) {
2350 if (!hashLastBlock.IsNull() && header.hashPrevBlock != hashLastBlock) {
2351 Misbehaving(pfrom->GetId(), 20);
2352 return error("non-continuous headers sequence");
2354 hashLastBlock = header.GetHash();
2358 CValidationState state;
2359 if (!ProcessNewBlockHeaders(headers, state, chainparams, &pindexLast)) {
2360 int nDoS;
2361 if (state.IsInvalid(nDoS)) {
2362 if (nDoS > 0) {
2363 LOCK(cs_main);
2364 Misbehaving(pfrom->GetId(), nDoS);
2366 return error("invalid header received");
2371 LOCK(cs_main);
2372 CNodeState *nodestate = State(pfrom->GetId());
2373 if (nodestate->nUnconnectingHeaders > 0) {
2374 LogPrint(BCLog::NET, "peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n", pfrom->GetId(), nodestate->nUnconnectingHeaders);
2376 nodestate->nUnconnectingHeaders = 0;
2378 assert(pindexLast);
2379 UpdateBlockAvailability(pfrom->GetId(), pindexLast->GetBlockHash());
2381 // From here, pindexBestKnownBlock should be guaranteed to be non-null,
2382 // because it is set in UpdateBlockAvailability. Some nullptr checks
2383 // are still present, however, as belt-and-suspenders.
2385 if (nCount == MAX_HEADERS_RESULTS) {
2386 // Headers message had its maximum size; the peer may have more headers.
2387 // TODO: optimize: if pindexLast is an ancestor of chainActive.Tip or pindexBestHeader, continue
2388 // from there instead.
2389 LogPrint(BCLog::NET, "more getheaders (%d) to end to peer=%d (startheight:%d)\n", pindexLast->nHeight, pfrom->GetId(), pfrom->nStartingHeight);
2390 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexLast), uint256()));
2393 bool fCanDirectFetch = CanDirectFetch(chainparams.GetConsensus());
2394 // If this set of headers is valid and ends in a block with at least as
2395 // much work as our tip, download as much as possible.
2396 if (fCanDirectFetch && pindexLast->IsValid(BLOCK_VALID_TREE) && chainActive.Tip()->nChainWork <= pindexLast->nChainWork) {
2397 std::vector<const CBlockIndex*> vToFetch;
2398 const CBlockIndex *pindexWalk = pindexLast;
2399 // Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
2400 while (pindexWalk && !chainActive.Contains(pindexWalk) && vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
2401 if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
2402 !mapBlocksInFlight.count(pindexWalk->GetBlockHash()) &&
2403 (!IsWitnessEnabled(pindexWalk->pprev, chainparams.GetConsensus()) || State(pfrom->GetId())->fHaveWitness)) {
2404 // We don't have this block, and it's not yet in flight.
2405 vToFetch.push_back(pindexWalk);
2407 pindexWalk = pindexWalk->pprev;
2409 // If pindexWalk still isn't on our main chain, we're looking at a
2410 // very large reorg at a time we think we're close to caught up to
2411 // the main chain -- this shouldn't really happen. Bail out on the
2412 // direct fetch and rely on parallel download instead.
2413 if (!chainActive.Contains(pindexWalk)) {
2414 LogPrint(BCLog::NET, "Large reorg, won't direct fetch to %s (%d)\n",
2415 pindexLast->GetBlockHash().ToString(),
2416 pindexLast->nHeight);
2417 } else {
2418 std::vector<CInv> vGetData;
2419 // Download as much as possible, from earliest to latest.
2420 for (const CBlockIndex *pindex : reverse_iterate(vToFetch)) {
2421 if (nodestate->nBlocksInFlight >= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
2422 // Can't download any more from this peer
2423 break;
2425 uint32_t nFetchFlags = GetFetchFlags(pfrom);
2426 vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
2427 MarkBlockAsInFlight(pfrom->GetId(), pindex->GetBlockHash(), pindex);
2428 LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n",
2429 pindex->GetBlockHash().ToString(), pfrom->GetId());
2431 if (vGetData.size() > 1) {
2432 LogPrint(BCLog::NET, "Downloading blocks toward %s (%d) via headers direct fetch\n",
2433 pindexLast->GetBlockHash().ToString(), pindexLast->nHeight);
2435 if (vGetData.size() > 0) {
2436 if (nodestate->fSupportsDesiredCmpctVersion && vGetData.size() == 1 && mapBlocksInFlight.size() == 1 && pindexLast->pprev->IsValid(BLOCK_VALID_CHAIN)) {
2437 // In any case, we want to download using a compact block, not a regular one
2438 vGetData[0] = CInv(MSG_CMPCT_BLOCK, vGetData[0].hash);
2440 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vGetData));
2444 // If we're in IBD, we want outbound peers that will serve us a useful
2445 // chain. Disconnect peers that are on chains with insufficient work.
2446 if (IsInitialBlockDownload() && nCount != MAX_HEADERS_RESULTS) {
2447 // When nCount < MAX_HEADERS_RESULTS, we know we have no more
2448 // headers to fetch from this peer.
2449 if (nodestate->pindexBestKnownBlock && nodestate->pindexBestKnownBlock->nChainWork < nMinimumChainWork) {
2450 // This peer has too little work on their headers chain to help
2451 // us sync -- disconnect if using an outbound slot (unless
2452 // whitelisted or addnode).
2453 // Note: We compare their tip to nMinimumChainWork (rather than
2454 // chainActive.Tip()) because we won't start block download
2455 // until we have a headers chain that has at least
2456 // nMinimumChainWork, even if a peer has a chain past our tip,
2457 // as an anti-DoS measure.
2458 if (IsOutboundDisconnectionCandidate(pfrom)) {
2459 LogPrintf("Disconnecting outbound peer %d -- headers chain has insufficient work\n", pfrom->GetId());
2460 pfrom->fDisconnect = true;
2465 if (!pfrom->fDisconnect && IsOutboundDisconnectionCandidate(pfrom) && nodestate->pindexBestKnownBlock != nullptr) {
2466 // If this is an outbound peer, check to see if we should protect
2467 // it from the bad/lagging chain logic.
2468 if (g_outbound_peers_with_protect_from_disconnect < MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT && nodestate->pindexBestKnownBlock->nChainWork >= chainActive.Tip()->nChainWork && !nodestate->m_chain_sync.m_protect) {
2469 nodestate->m_chain_sync.m_protect = true;
2470 ++g_outbound_peers_with_protect_from_disconnect;
2476 else if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
2478 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
2479 vRecv >> *pblock;
2481 LogPrint(BCLog::NET, "received block %s peer=%d\n", pblock->GetHash().ToString(), pfrom->GetId());
2483 // Process all blocks from whitelisted peers, even if not requested,
2484 // unless we're still syncing with the network.
2485 // Such an unrequested block may still be processed, subject to the
2486 // conditions in AcceptBlock().
2487 bool forceProcessing = pfrom->fWhitelisted && !IsInitialBlockDownload();
2488 const uint256 hash(pblock->GetHash());
2490 LOCK(cs_main);
2491 // Also always process if we requested the block explicitly, as we may
2492 // need it even though it is not a candidate for a new best tip.
2493 forceProcessing |= MarkBlockAsReceived(hash);
2494 // mapBlockSource is only used for sending reject messages and DoS scores,
2495 // so the race between here and cs_main in ProcessNewBlock is fine.
2496 mapBlockSource.emplace(hash, std::make_pair(pfrom->GetId(), true));
2498 bool fNewBlock = false;
2499 ProcessNewBlock(chainparams, pblock, forceProcessing, &fNewBlock);
2500 if (fNewBlock) {
2501 pfrom->nLastBlockTime = GetTime();
2502 } else {
2503 LOCK(cs_main);
2504 mapBlockSource.erase(pblock->GetHash());
2509 else if (strCommand == NetMsgType::GETADDR)
2511 // This asymmetric behavior for inbound and outbound connections was introduced
2512 // to prevent a fingerprinting attack: an attacker can send specific fake addresses
2513 // to users' AddrMan and later request them by sending getaddr messages.
2514 // Making nodes which are behind NAT and can only make outgoing connections ignore
2515 // the getaddr message mitigates the attack.
2516 if (!pfrom->fInbound) {
2517 LogPrint(BCLog::NET, "Ignoring \"getaddr\" from outbound connection. peer=%d\n", pfrom->GetId());
2518 return true;
2521 // Only send one GetAddr response per connection to reduce resource waste
2522 // and discourage addr stamping of INV announcements.
2523 if (pfrom->fSentAddr) {
2524 LogPrint(BCLog::NET, "Ignoring repeated \"getaddr\". peer=%d\n", pfrom->GetId());
2525 return true;
2527 pfrom->fSentAddr = true;
2529 pfrom->vAddrToSend.clear();
2530 std::vector<CAddress> vAddr = connman->GetAddresses();
2531 FastRandomContext insecure_rand;
2532 for (const CAddress &addr : vAddr)
2533 pfrom->PushAddress(addr, insecure_rand);
2537 else if (strCommand == NetMsgType::MEMPOOL)
2539 if (!(pfrom->GetLocalServices() & NODE_BLOOM) && !pfrom->fWhitelisted)
2541 LogPrint(BCLog::NET, "mempool request with bloom filters disabled, disconnect peer=%d\n", pfrom->GetId());
2542 pfrom->fDisconnect = true;
2543 return true;
2546 if (connman->OutboundTargetReached(false) && !pfrom->fWhitelisted)
2548 LogPrint(BCLog::NET, "mempool request with bandwidth limit reached, disconnect peer=%d\n", pfrom->GetId());
2549 pfrom->fDisconnect = true;
2550 return true;
2553 LOCK(pfrom->cs_inventory);
2554 pfrom->fSendMempool = true;
2558 else if (strCommand == NetMsgType::PING)
2560 if (pfrom->nVersion > BIP0031_VERSION)
2562 uint64_t nonce = 0;
2563 vRecv >> nonce;
2564 // Echo the message back with the nonce. This allows for two useful features:
2566 // 1) A remote node can quickly check if the connection is operational
2567 // 2) Remote nodes can measure the latency of the network thread. If this node
2568 // is overloaded it won't respond to pings quickly and the remote node can
2569 // avoid sending us more work, like chain download requests.
2571 // The nonce stops the remote getting confused between different pings: without
2572 // it, if the remote node sends a ping once per second and this node takes 5
2573 // seconds to respond to each, the 5th ping the remote sends would appear to
2574 // return very quickly.
2575 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::PONG, nonce));
2580 else if (strCommand == NetMsgType::PONG)
2582 int64_t pingUsecEnd = nTimeReceived;
2583 uint64_t nonce = 0;
2584 size_t nAvail = vRecv.in_avail();
2585 bool bPingFinished = false;
2586 std::string sProblem;
2588 if (nAvail >= sizeof(nonce)) {
2589 vRecv >> nonce;
2591 // Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
2592 if (pfrom->nPingNonceSent != 0) {
2593 if (nonce == pfrom->nPingNonceSent) {
2594 // Matching pong received, this ping is no longer outstanding
2595 bPingFinished = true;
2596 int64_t pingUsecTime = pingUsecEnd - pfrom->nPingUsecStart;
2597 if (pingUsecTime > 0) {
2598 // Successful ping time measurement, replace previous
2599 pfrom->nPingUsecTime = pingUsecTime;
2600 pfrom->nMinPingUsecTime = std::min(pfrom->nMinPingUsecTime.load(), pingUsecTime);
2601 } else {
2602 // This should never happen
2603 sProblem = "Timing mishap";
2605 } else {
2606 // Nonce mismatches are normal when pings are overlapping
2607 sProblem = "Nonce mismatch";
2608 if (nonce == 0) {
2609 // This is most likely a bug in another implementation somewhere; cancel this ping
2610 bPingFinished = true;
2611 sProblem = "Nonce zero";
2614 } else {
2615 sProblem = "Unsolicited pong without ping";
2617 } else {
2618 // This is most likely a bug in another implementation somewhere; cancel this ping
2619 bPingFinished = true;
2620 sProblem = "Short payload";
2623 if (!(sProblem.empty())) {
2624 LogPrint(BCLog::NET, "pong peer=%d: %s, %x expected, %x received, %u bytes\n",
2625 pfrom->GetId(),
2626 sProblem,
2627 pfrom->nPingNonceSent,
2628 nonce,
2629 nAvail);
2631 if (bPingFinished) {
2632 pfrom->nPingNonceSent = 0;
2637 else if (strCommand == NetMsgType::FILTERLOAD)
2639 CBloomFilter filter;
2640 vRecv >> filter;
2642 if (!filter.IsWithinSizeConstraints())
2644 // There is no excuse for sending a too-large filter
2645 LOCK(cs_main);
2646 Misbehaving(pfrom->GetId(), 100);
2648 else
2650 LOCK(pfrom->cs_filter);
2651 delete pfrom->pfilter;
2652 pfrom->pfilter = new CBloomFilter(filter);
2653 pfrom->pfilter->UpdateEmptyFull();
2654 pfrom->fRelayTxes = true;
2659 else if (strCommand == NetMsgType::FILTERADD)
2661 std::vector<unsigned char> vData;
2662 vRecv >> vData;
2664 // Nodes must NEVER send a data item > 520 bytes (the max size for a script data object,
2665 // and thus, the maximum size any matched object can have) in a filteradd message
2666 bool bad = false;
2667 if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE) {
2668 bad = true;
2669 } else {
2670 LOCK(pfrom->cs_filter);
2671 if (pfrom->pfilter) {
2672 pfrom->pfilter->insert(vData);
2673 } else {
2674 bad = true;
2677 if (bad) {
2678 LOCK(cs_main);
2679 Misbehaving(pfrom->GetId(), 100);
2684 else if (strCommand == NetMsgType::FILTERCLEAR)
2686 LOCK(pfrom->cs_filter);
2687 if (pfrom->GetLocalServices() & NODE_BLOOM) {
2688 delete pfrom->pfilter;
2689 pfrom->pfilter = new CBloomFilter();
2691 pfrom->fRelayTxes = true;
2694 else if (strCommand == NetMsgType::FEEFILTER) {
2695 CAmount newFeeFilter = 0;
2696 vRecv >> newFeeFilter;
2697 if (MoneyRange(newFeeFilter)) {
2699 LOCK(pfrom->cs_feeFilter);
2700 pfrom->minFeeFilter = newFeeFilter;
2702 LogPrint(BCLog::NET, "received: feefilter of %s from peer=%d\n", CFeeRate(newFeeFilter).ToString(), pfrom->GetId());
2706 else if (strCommand == NetMsgType::NOTFOUND) {
2707 // We do not care about the NOTFOUND message, but logging an Unknown Command
2708 // message would be undesirable as we transmit it ourselves.
2711 else {
2712 // Ignore unknown commands for extensibility
2713 LogPrint(BCLog::NET, "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->GetId());
2718 return true;
2721 static bool SendRejectsAndCheckIfBanned(CNode* pnode, CConnman* connman)
2723 AssertLockHeld(cs_main);
2724 CNodeState &state = *State(pnode->GetId());
2726 for (const CBlockReject& reject : state.rejects) {
2727 connman->PushMessage(pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, (std::string)NetMsgType::BLOCK, reject.chRejectCode, reject.strRejectReason, reject.hashBlock));
2729 state.rejects.clear();
2731 if (state.fShouldBan) {
2732 state.fShouldBan = false;
2733 if (pnode->fWhitelisted)
2734 LogPrintf("Warning: not punishing whitelisted peer %s!\n", pnode->addr.ToString());
2735 else if (pnode->m_manual_connection)
2736 LogPrintf("Warning: not punishing manually-connected peer %s!\n", pnode->addr.ToString());
2737 else {
2738 pnode->fDisconnect = true;
2739 if (pnode->addr.IsLocal())
2740 LogPrintf("Warning: not banning local peer %s!\n", pnode->addr.ToString());
2741 else
2743 connman->Ban(pnode->addr, BanReasonNodeMisbehaving);
2746 return true;
2748 return false;
2751 bool PeerLogicValidation::ProcessMessages(CNode* pfrom, std::atomic<bool>& interruptMsgProc)
2753 const CChainParams& chainparams = Params();
2755 // Message format
2756 // (4) message start
2757 // (12) command
2758 // (4) size
2759 // (4) checksum
2760 // (x) data
2762 bool fMoreWork = false;
2764 if (!pfrom->vRecvGetData.empty())
2765 ProcessGetData(pfrom, chainparams.GetConsensus(), connman, interruptMsgProc);
2767 if (pfrom->fDisconnect)
2768 return false;
2770 // this maintains the order of responses
2771 if (!pfrom->vRecvGetData.empty()) return true;
2773 // Don't bother if send buffer is too full to respond anyway
2774 if (pfrom->fPauseSend)
2775 return false;
2777 std::list<CNetMessage> msgs;
2779 LOCK(pfrom->cs_vProcessMsg);
2780 if (pfrom->vProcessMsg.empty())
2781 return false;
2782 // Just take one message
2783 msgs.splice(msgs.begin(), pfrom->vProcessMsg, pfrom->vProcessMsg.begin());
2784 pfrom->nProcessQueueSize -= msgs.front().vRecv.size() + CMessageHeader::HEADER_SIZE;
2785 pfrom->fPauseRecv = pfrom->nProcessQueueSize > connman->GetReceiveFloodSize();
2786 fMoreWork = !pfrom->vProcessMsg.empty();
2788 CNetMessage& msg(msgs.front());
2790 msg.SetVersion(pfrom->GetRecvVersion());
2791 // Scan for message start
2792 if (memcmp(msg.hdr.pchMessageStart, chainparams.MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) {
2793 LogPrintf("PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg.hdr.GetCommand()), pfrom->GetId());
2794 pfrom->fDisconnect = true;
2795 return false;
2798 // Read header
2799 CMessageHeader& hdr = msg.hdr;
2800 if (!hdr.IsValid(chainparams.MessageStart()))
2802 LogPrintf("PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(hdr.GetCommand()), pfrom->GetId());
2803 return fMoreWork;
2805 std::string strCommand = hdr.GetCommand();
2807 // Message size
2808 unsigned int nMessageSize = hdr.nMessageSize;
2810 // Checksum
2811 CDataStream& vRecv = msg.vRecv;
2812 const uint256& hash = msg.GetMessageHash();
2813 if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0)
2815 LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s\n", __func__,
2816 SanitizeString(strCommand), nMessageSize,
2817 HexStr(hash.begin(), hash.begin()+CMessageHeader::CHECKSUM_SIZE),
2818 HexStr(hdr.pchChecksum, hdr.pchChecksum+CMessageHeader::CHECKSUM_SIZE));
2819 return fMoreWork;
2822 // Process message
2823 bool fRet = false;
2826 fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime, chainparams, connman, interruptMsgProc);
2827 if (interruptMsgProc)
2828 return false;
2829 if (!pfrom->vRecvGetData.empty())
2830 fMoreWork = true;
2832 catch (const std::ios_base::failure& e)
2834 connman->PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_MALFORMED, std::string("error parsing message")));
2835 if (strstr(e.what(), "end of data"))
2837 // Allow exceptions from under-length message on vRecv
2838 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());
2840 else if (strstr(e.what(), "size too large"))
2842 // Allow exceptions from over-long size
2843 LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
2845 else if (strstr(e.what(), "non-canonical ReadCompactSize()"))
2847 // Allow exceptions from non-canonical encoding
2848 LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
2850 else
2852 PrintExceptionContinue(&e, "ProcessMessages()");
2855 catch (const std::exception& e) {
2856 PrintExceptionContinue(&e, "ProcessMessages()");
2857 } catch (...) {
2858 PrintExceptionContinue(nullptr, "ProcessMessages()");
2861 if (!fRet) {
2862 LogPrintf("%s(%s, %u bytes) FAILED peer=%d\n", __func__, SanitizeString(strCommand), nMessageSize, pfrom->GetId());
2865 LOCK(cs_main);
2866 SendRejectsAndCheckIfBanned(pfrom, connman);
2868 return fMoreWork;
2871 void PeerLogicValidation::ConsiderEviction(CNode *pto, int64_t time_in_seconds)
2873 AssertLockHeld(cs_main);
2875 CNodeState &state = *State(pto->GetId());
2876 const CNetMsgMaker msgMaker(pto->GetSendVersion());
2878 if (!state.m_chain_sync.m_protect && IsOutboundDisconnectionCandidate(pto) && state.fSyncStarted) {
2879 // This is an outbound peer subject to disconnection if they don't
2880 // announce a block with as much work as the current tip within
2881 // CHAIN_SYNC_TIMEOUT + HEADERS_RESPONSE_TIME seconds (note: if
2882 // their chain has more work than ours, we should sync to it,
2883 // unless it's invalid, in which case we should find that out and
2884 // disconnect from them elsewhere).
2885 if (state.pindexBestKnownBlock != nullptr && state.pindexBestKnownBlock->nChainWork >= chainActive.Tip()->nChainWork) {
2886 if (state.m_chain_sync.m_timeout != 0) {
2887 state.m_chain_sync.m_timeout = 0;
2888 state.m_chain_sync.m_work_header = nullptr;
2889 state.m_chain_sync.m_sent_getheaders = false;
2891 } else if (state.m_chain_sync.m_timeout == 0 || (state.m_chain_sync.m_work_header != nullptr && state.pindexBestKnownBlock != nullptr && state.pindexBestKnownBlock->nChainWork >= state.m_chain_sync.m_work_header->nChainWork)) {
2892 // Our best block known by this peer is behind our tip, and we're either noticing
2893 // that for the first time, OR this peer was able to catch up to some earlier point
2894 // where we checked against our tip.
2895 // Either way, set a new timeout based on current tip.
2896 state.m_chain_sync.m_timeout = time_in_seconds + CHAIN_SYNC_TIMEOUT;
2897 state.m_chain_sync.m_work_header = chainActive.Tip();
2898 state.m_chain_sync.m_sent_getheaders = false;
2899 } else if (state.m_chain_sync.m_timeout > 0 && time_in_seconds > state.m_chain_sync.m_timeout) {
2900 // No evidence yet that our peer has synced to a chain with work equal to that
2901 // of our tip, when we first detected it was behind. Send a single getheaders
2902 // message to give the peer a chance to update us.
2903 if (state.m_chain_sync.m_sent_getheaders) {
2904 // They've run out of time to catch up!
2905 LogPrintf("Disconnecting outbound peer %d for old chain, best known block = %s\n", pto->GetId(), state.pindexBestKnownBlock != nullptr ? state.pindexBestKnownBlock->GetBlockHash().ToString() : "<none>");
2906 pto->fDisconnect = true;
2907 } else {
2908 LogPrint(BCLog::NET, "sending getheaders to outbound peer=%d to verify chain work (current best known block:%s, benchmark blockhash: %s)\n", pto->GetId(), state.pindexBestKnownBlock != nullptr ? state.pindexBestKnownBlock->GetBlockHash().ToString() : "<none>", state.m_chain_sync.m_work_header->GetBlockHash().ToString());
2909 connman->PushMessage(pto, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(state.m_chain_sync.m_work_header->pprev), uint256()));
2910 state.m_chain_sync.m_sent_getheaders = true;
2911 constexpr int64_t HEADERS_RESPONSE_TIME = 120; // 2 minutes
2912 // Bump the timeout to allow a response, which could clear the timeout
2913 // (if the response shows the peer has synced), reset the timeout (if
2914 // the peer syncs to the required work but not to our tip), or result
2915 // in disconnect (if we advance to the timeout and pindexBestKnownBlock
2916 // has not sufficiently progressed)
2917 state.m_chain_sync.m_timeout = time_in_seconds + HEADERS_RESPONSE_TIME;
2923 class CompareInvMempoolOrder
2925 CTxMemPool *mp;
2926 public:
2927 explicit CompareInvMempoolOrder(CTxMemPool *_mempool)
2929 mp = _mempool;
2932 bool operator()(std::set<uint256>::iterator a, std::set<uint256>::iterator b)
2934 /* As std::make_heap produces a max-heap, we want the entries with the
2935 * fewest ancestors/highest fee to sort later. */
2936 return mp->CompareDepthAndScore(*b, *a);
2940 bool PeerLogicValidation::SendMessages(CNode* pto, std::atomic<bool>& interruptMsgProc)
2942 const Consensus::Params& consensusParams = Params().GetConsensus();
2944 // Don't send anything until the version handshake is complete
2945 if (!pto->fSuccessfullyConnected || pto->fDisconnect)
2946 return true;
2948 // If we get here, the outgoing message serialization version is set and can't change.
2949 const CNetMsgMaker msgMaker(pto->GetSendVersion());
2952 // Message: ping
2954 bool pingSend = false;
2955 if (pto->fPingQueued) {
2956 // RPC ping request by user
2957 pingSend = true;
2959 if (pto->nPingNonceSent == 0 && pto->nPingUsecStart + PING_INTERVAL * 1000000 < GetTimeMicros()) {
2960 // Ping automatically sent as a latency probe & keepalive.
2961 pingSend = true;
2963 if (pingSend) {
2964 uint64_t nonce = 0;
2965 while (nonce == 0) {
2966 GetRandBytes((unsigned char*)&nonce, sizeof(nonce));
2968 pto->fPingQueued = false;
2969 pto->nPingUsecStart = GetTimeMicros();
2970 if (pto->nVersion > BIP0031_VERSION) {
2971 pto->nPingNonceSent = nonce;
2972 connman->PushMessage(pto, msgMaker.Make(NetMsgType::PING, nonce));
2973 } else {
2974 // Peer is too old to support ping command with nonce, pong will never arrive.
2975 pto->nPingNonceSent = 0;
2976 connman->PushMessage(pto, msgMaker.Make(NetMsgType::PING));
2980 TRY_LOCK(cs_main, lockMain); // Acquire cs_main for IsInitialBlockDownload() and CNodeState()
2981 if (!lockMain)
2982 return true;
2984 if (SendRejectsAndCheckIfBanned(pto, connman))
2985 return true;
2986 CNodeState &state = *State(pto->GetId());
2988 // Address refresh broadcast
2989 int64_t nNow = GetTimeMicros();
2990 if (!IsInitialBlockDownload() && pto->nNextLocalAddrSend < nNow) {
2991 AdvertiseLocal(pto);
2992 pto->nNextLocalAddrSend = PoissonNextSend(nNow, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
2996 // Message: addr
2998 if (pto->nNextAddrSend < nNow) {
2999 pto->nNextAddrSend = PoissonNextSend(nNow, AVG_ADDRESS_BROADCAST_INTERVAL);
3000 std::vector<CAddress> vAddr;
3001 vAddr.reserve(pto->vAddrToSend.size());
3002 for (const CAddress& addr : pto->vAddrToSend)
3004 if (!pto->addrKnown.contains(addr.GetKey()))
3006 pto->addrKnown.insert(addr.GetKey());
3007 vAddr.push_back(addr);
3008 // receiver rejects addr messages larger than 1000
3009 if (vAddr.size() >= 1000)
3011 connman->PushMessage(pto, msgMaker.Make(NetMsgType::ADDR, vAddr));
3012 vAddr.clear();
3016 pto->vAddrToSend.clear();
3017 if (!vAddr.empty())
3018 connman->PushMessage(pto, msgMaker.Make(NetMsgType::ADDR, vAddr));
3019 // we only send the big addr message once
3020 if (pto->vAddrToSend.capacity() > 40)
3021 pto->vAddrToSend.shrink_to_fit();
3024 // Start block sync
3025 if (pindexBestHeader == nullptr)
3026 pindexBestHeader = chainActive.Tip();
3027 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.
3028 if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex) {
3029 // Only actively request headers from a single peer, unless we're close to today.
3030 if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
3031 state.fSyncStarted = true;
3032 state.nHeadersSyncTimeout = GetTimeMicros() + HEADERS_DOWNLOAD_TIMEOUT_BASE + HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER * (GetAdjustedTime() - pindexBestHeader->GetBlockTime())/(consensusParams.nPowTargetSpacing);
3033 nSyncStarted++;
3034 const CBlockIndex *pindexStart = pindexBestHeader;
3035 /* If possible, start at the block preceding the currently
3036 best known header. This ensures that we always get a
3037 non-empty list of headers back as long as the peer
3038 is up-to-date. With a non-empty response, we can initialise
3039 the peer's known best block. This wouldn't be possible
3040 if we requested starting at pindexBestHeader and
3041 got back an empty response. */
3042 if (pindexStart->pprev)
3043 pindexStart = pindexStart->pprev;
3044 LogPrint(BCLog::NET, "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->nHeight, pto->GetId(), pto->nStartingHeight);
3045 connman->PushMessage(pto, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexStart), uint256()));
3049 // Resend wallet transactions that haven't gotten in a block yet
3050 // Except during reindex, importing and IBD, when old wallet
3051 // transactions become unconfirmed and spams other nodes.
3052 if (!fReindex && !fImporting && !IsInitialBlockDownload())
3054 GetMainSignals().Broadcast(nTimeBestReceived, connman);
3058 // Try sending block announcements via headers
3061 // If we have less than MAX_BLOCKS_TO_ANNOUNCE in our
3062 // list of block hashes we're relaying, and our peer wants
3063 // headers announcements, then find the first header
3064 // not yet known to our peer but would connect, and send.
3065 // If no header would connect, or if we have too many
3066 // blocks, or if the peer doesn't want headers, just
3067 // add all to the inv queue.
3068 LOCK(pto->cs_inventory);
3069 std::vector<CBlock> vHeaders;
3070 bool fRevertToInv = ((!state.fPreferHeaders &&
3071 (!state.fPreferHeaderAndIDs || pto->vBlockHashesToAnnounce.size() > 1)) ||
3072 pto->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE);
3073 const CBlockIndex *pBestIndex = nullptr; // last header queued for delivery
3074 ProcessBlockAvailability(pto->GetId()); // ensure pindexBestKnownBlock is up-to-date
3076 if (!fRevertToInv) {
3077 bool fFoundStartingHeader = false;
3078 // Try to find first header that our peer doesn't have, and
3079 // then send all headers past that one. If we come across any
3080 // headers that aren't on chainActive, give up.
3081 for (const uint256 &hash : pto->vBlockHashesToAnnounce) {
3082 BlockMap::iterator mi = mapBlockIndex.find(hash);
3083 assert(mi != mapBlockIndex.end());
3084 const CBlockIndex *pindex = mi->second;
3085 if (chainActive[pindex->nHeight] != pindex) {
3086 // Bail out if we reorged away from this block
3087 fRevertToInv = true;
3088 break;
3090 if (pBestIndex != nullptr && pindex->pprev != pBestIndex) {
3091 // This means that the list of blocks to announce don't
3092 // connect to each other.
3093 // This shouldn't really be possible to hit during
3094 // regular operation (because reorgs should take us to
3095 // a chain that has some block not on the prior chain,
3096 // which should be caught by the prior check), but one
3097 // way this could happen is by using invalidateblock /
3098 // reconsiderblock repeatedly on the tip, causing it to
3099 // be added multiple times to vBlockHashesToAnnounce.
3100 // Robustly deal with this rare situation by reverting
3101 // to an inv.
3102 fRevertToInv = true;
3103 break;
3105 pBestIndex = pindex;
3106 if (fFoundStartingHeader) {
3107 // add this to the headers message
3108 vHeaders.push_back(pindex->GetBlockHeader());
3109 } else if (PeerHasHeader(&state, pindex)) {
3110 continue; // keep looking for the first new block
3111 } else if (pindex->pprev == nullptr || PeerHasHeader(&state, pindex->pprev)) {
3112 // Peer doesn't have this header but they do have the prior one.
3113 // Start sending headers.
3114 fFoundStartingHeader = true;
3115 vHeaders.push_back(pindex->GetBlockHeader());
3116 } else {
3117 // Peer doesn't have this header or the prior one -- nothing will
3118 // connect, so bail out.
3119 fRevertToInv = true;
3120 break;
3124 if (!fRevertToInv && !vHeaders.empty()) {
3125 if (vHeaders.size() == 1 && state.fPreferHeaderAndIDs) {
3126 // We only send up to 1 block as header-and-ids, as otherwise
3127 // probably means we're doing an initial-ish-sync or they're slow
3128 LogPrint(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", __func__,
3129 vHeaders.front().GetHash().ToString(), pto->GetId());
3131 int nSendFlags = state.fWantsCmpctWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
3133 bool fGotBlockFromCache = false;
3135 LOCK(cs_most_recent_block);
3136 if (most_recent_block_hash == pBestIndex->GetBlockHash()) {
3137 if (state.fWantsCmpctWitness || !fWitnessesPresentInMostRecentCompactBlock)
3138 connman->PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, *most_recent_compact_block));
3139 else {
3140 CBlockHeaderAndShortTxIDs cmpctblock(*most_recent_block, state.fWantsCmpctWitness);
3141 connman->PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
3143 fGotBlockFromCache = true;
3146 if (!fGotBlockFromCache) {
3147 CBlock block;
3148 bool ret = ReadBlockFromDisk(block, pBestIndex, consensusParams);
3149 assert(ret);
3150 CBlockHeaderAndShortTxIDs cmpctblock(block, state.fWantsCmpctWitness);
3151 connman->PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
3153 state.pindexBestHeaderSent = pBestIndex;
3154 } else if (state.fPreferHeaders) {
3155 if (vHeaders.size() > 1) {
3156 LogPrint(BCLog::NET, "%s: %u headers, range (%s, %s), to peer=%d\n", __func__,
3157 vHeaders.size(),
3158 vHeaders.front().GetHash().ToString(),
3159 vHeaders.back().GetHash().ToString(), pto->GetId());
3160 } else {
3161 LogPrint(BCLog::NET, "%s: sending header %s to peer=%d\n", __func__,
3162 vHeaders.front().GetHash().ToString(), pto->GetId());
3164 connman->PushMessage(pto, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
3165 state.pindexBestHeaderSent = pBestIndex;
3166 } else
3167 fRevertToInv = true;
3169 if (fRevertToInv) {
3170 // If falling back to using an inv, just try to inv the tip.
3171 // The last entry in vBlockHashesToAnnounce was our tip at some point
3172 // in the past.
3173 if (!pto->vBlockHashesToAnnounce.empty()) {
3174 const uint256 &hashToAnnounce = pto->vBlockHashesToAnnounce.back();
3175 BlockMap::iterator mi = mapBlockIndex.find(hashToAnnounce);
3176 assert(mi != mapBlockIndex.end());
3177 const CBlockIndex *pindex = mi->second;
3179 // Warn if we're announcing a block that is not on the main chain.
3180 // This should be very rare and could be optimized out.
3181 // Just log for now.
3182 if (chainActive[pindex->nHeight] != pindex) {
3183 LogPrint(BCLog::NET, "Announcing block %s not on main chain (tip=%s)\n",
3184 hashToAnnounce.ToString(), chainActive.Tip()->GetBlockHash().ToString());
3187 // If the peer's chain has this block, don't inv it back.
3188 if (!PeerHasHeader(&state, pindex)) {
3189 pto->PushInventory(CInv(MSG_BLOCK, hashToAnnounce));
3190 LogPrint(BCLog::NET, "%s: sending inv peer=%d hash=%s\n", __func__,
3191 pto->GetId(), hashToAnnounce.ToString());
3195 pto->vBlockHashesToAnnounce.clear();
3199 // Message: inventory
3201 std::vector<CInv> vInv;
3203 LOCK(pto->cs_inventory);
3204 vInv.reserve(std::max<size_t>(pto->vInventoryBlockToSend.size(), INVENTORY_BROADCAST_MAX));
3206 // Add blocks
3207 for (const uint256& hash : pto->vInventoryBlockToSend) {
3208 vInv.push_back(CInv(MSG_BLOCK, hash));
3209 if (vInv.size() == MAX_INV_SZ) {
3210 connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
3211 vInv.clear();
3214 pto->vInventoryBlockToSend.clear();
3216 // Check whether periodic sends should happen
3217 bool fSendTrickle = pto->fWhitelisted;
3218 if (pto->nNextInvSend < nNow) {
3219 fSendTrickle = true;
3220 // Use half the delay for outbound peers, as there is less privacy concern for them.
3221 pto->nNextInvSend = PoissonNextSend(nNow, INVENTORY_BROADCAST_INTERVAL >> !pto->fInbound);
3224 // Time to send but the peer has requested we not relay transactions.
3225 if (fSendTrickle) {
3226 LOCK(pto->cs_filter);
3227 if (!pto->fRelayTxes) pto->setInventoryTxToSend.clear();
3230 // Respond to BIP35 mempool requests
3231 if (fSendTrickle && pto->fSendMempool) {
3232 auto vtxinfo = mempool.infoAll();
3233 pto->fSendMempool = false;
3234 CAmount filterrate = 0;
3236 LOCK(pto->cs_feeFilter);
3237 filterrate = pto->minFeeFilter;
3240 LOCK(pto->cs_filter);
3242 for (const auto& txinfo : vtxinfo) {
3243 const uint256& hash = txinfo.tx->GetHash();
3244 CInv inv(MSG_TX, hash);
3245 pto->setInventoryTxToSend.erase(hash);
3246 if (filterrate) {
3247 if (txinfo.feeRate.GetFeePerK() < filterrate)
3248 continue;
3250 if (pto->pfilter) {
3251 if (!pto->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
3253 pto->filterInventoryKnown.insert(hash);
3254 vInv.push_back(inv);
3255 if (vInv.size() == MAX_INV_SZ) {
3256 connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
3257 vInv.clear();
3260 pto->timeLastMempoolReq = GetTime();
3263 // Determine transactions to relay
3264 if (fSendTrickle) {
3265 // Produce a vector with all candidates for sending
3266 std::vector<std::set<uint256>::iterator> vInvTx;
3267 vInvTx.reserve(pto->setInventoryTxToSend.size());
3268 for (std::set<uint256>::iterator it = pto->setInventoryTxToSend.begin(); it != pto->setInventoryTxToSend.end(); it++) {
3269 vInvTx.push_back(it);
3271 CAmount filterrate = 0;
3273 LOCK(pto->cs_feeFilter);
3274 filterrate = pto->minFeeFilter;
3276 // Topologically and fee-rate sort the inventory we send for privacy and priority reasons.
3277 // A heap is used so that not all items need sorting if only a few are being sent.
3278 CompareInvMempoolOrder compareInvMempoolOrder(&mempool);
3279 std::make_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
3280 // No reason to drain out at many times the network's capacity,
3281 // especially since we have many peers and some will draw much shorter delays.
3282 unsigned int nRelayedTransactions = 0;
3283 LOCK(pto->cs_filter);
3284 while (!vInvTx.empty() && nRelayedTransactions < INVENTORY_BROADCAST_MAX) {
3285 // Fetch the top element from the heap
3286 std::pop_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
3287 std::set<uint256>::iterator it = vInvTx.back();
3288 vInvTx.pop_back();
3289 uint256 hash = *it;
3290 // Remove it from the to-be-sent set
3291 pto->setInventoryTxToSend.erase(it);
3292 // Check if not in the filter already
3293 if (pto->filterInventoryKnown.contains(hash)) {
3294 continue;
3296 // Not in the mempool anymore? don't bother sending it.
3297 auto txinfo = mempool.info(hash);
3298 if (!txinfo.tx) {
3299 continue;
3301 if (filterrate && txinfo.feeRate.GetFeePerK() < filterrate) {
3302 continue;
3304 if (pto->pfilter && !pto->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
3305 // Send
3306 vInv.push_back(CInv(MSG_TX, hash));
3307 nRelayedTransactions++;
3309 // Expire old relay messages
3310 while (!vRelayExpiration.empty() && vRelayExpiration.front().first < nNow)
3312 mapRelay.erase(vRelayExpiration.front().second);
3313 vRelayExpiration.pop_front();
3316 auto ret = mapRelay.insert(std::make_pair(hash, std::move(txinfo.tx)));
3317 if (ret.second) {
3318 vRelayExpiration.push_back(std::make_pair(nNow + 15 * 60 * 1000000, ret.first));
3321 if (vInv.size() == MAX_INV_SZ) {
3322 connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
3323 vInv.clear();
3325 pto->filterInventoryKnown.insert(hash);
3329 if (!vInv.empty())
3330 connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
3332 // Detect whether we're stalling
3333 nNow = GetTimeMicros();
3334 if (state.nStallingSince && state.nStallingSince < nNow - 1000000 * BLOCK_STALLING_TIMEOUT) {
3335 // Stalling only triggers when the block download window cannot move. During normal steady state,
3336 // the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
3337 // should only happen during initial block download.
3338 LogPrintf("Peer=%d is stalling block download, disconnecting\n", pto->GetId());
3339 pto->fDisconnect = true;
3340 return true;
3342 // In case there is a block that has been in flight from this peer for 2 + 0.5 * N times the block interval
3343 // (with N the number of peers from which we're downloading validated blocks), disconnect due to timeout.
3344 // We compensate for other peers to prevent killing off peers due to our own downstream link
3345 // being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
3346 // to unreasonably increase our timeout.
3347 if (state.vBlocksInFlight.size() > 0) {
3348 QueuedBlock &queuedBlock = state.vBlocksInFlight.front();
3349 int nOtherPeersWithValidatedDownloads = nPeersWithValidatedDownloads - (state.nBlocksInFlightValidHeaders > 0);
3350 if (nNow > state.nDownloadingSince + consensusParams.nPowTargetSpacing * (BLOCK_DOWNLOAD_TIMEOUT_BASE + BLOCK_DOWNLOAD_TIMEOUT_PER_PEER * nOtherPeersWithValidatedDownloads)) {
3351 LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", queuedBlock.hash.ToString(), pto->GetId());
3352 pto->fDisconnect = true;
3353 return true;
3356 // Check for headers sync timeouts
3357 if (state.fSyncStarted && state.nHeadersSyncTimeout < std::numeric_limits<int64_t>::max()) {
3358 // Detect whether this is a stalling initial-headers-sync peer
3359 if (pindexBestHeader->GetBlockTime() <= GetAdjustedTime() - 24*60*60) {
3360 if (nNow > state.nHeadersSyncTimeout && nSyncStarted == 1 && (nPreferredDownload - state.fPreferredDownload >= 1)) {
3361 // Disconnect a (non-whitelisted) peer if it is our only sync peer,
3362 // and we have others we could be using instead.
3363 // Note: If all our peers are inbound, then we won't
3364 // disconnect our sync peer for stalling; we have bigger
3365 // problems if we can't get any outbound peers.
3366 if (!pto->fWhitelisted) {
3367 LogPrintf("Timeout downloading headers from peer=%d, disconnecting\n", pto->GetId());
3368 pto->fDisconnect = true;
3369 return true;
3370 } else {
3371 LogPrintf("Timeout downloading headers from whitelisted peer=%d, not disconnecting\n", pto->GetId());
3372 // Reset the headers sync state so that we have a
3373 // chance to try downloading from a different peer.
3374 // Note: this will also result in at least one more
3375 // getheaders message to be sent to
3376 // this peer (eventually).
3377 state.fSyncStarted = false;
3378 nSyncStarted--;
3379 state.nHeadersSyncTimeout = 0;
3382 } else {
3383 // After we've caught up once, reset the timeout so we can't trigger
3384 // disconnect later.
3385 state.nHeadersSyncTimeout = std::numeric_limits<int64_t>::max();
3389 // Check that outbound peers have reasonable chains
3390 // GetTime() is used by this anti-DoS logic so we can test this using mocktime
3391 ConsiderEviction(pto, GetTime());
3394 // Message: getdata (blocks)
3396 std::vector<CInv> vGetData;
3397 if (!pto->fClient && (fFetch || !IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
3398 std::vector<const CBlockIndex*> vToDownload;
3399 NodeId staller = -1;
3400 FindNextBlocksToDownload(pto->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller, consensusParams);
3401 for (const CBlockIndex *pindex : vToDownload) {
3402 uint32_t nFetchFlags = GetFetchFlags(pto);
3403 vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
3404 MarkBlockAsInFlight(pto->GetId(), pindex->GetBlockHash(), pindex);
3405 LogPrint(BCLog::NET, "Requesting block %s (%d) peer=%d\n", pindex->GetBlockHash().ToString(),
3406 pindex->nHeight, pto->GetId());
3408 if (state.nBlocksInFlight == 0 && staller != -1) {
3409 if (State(staller)->nStallingSince == 0) {
3410 State(staller)->nStallingSince = nNow;
3411 LogPrint(BCLog::NET, "Stall started peer=%d\n", staller);
3417 // Message: getdata (non-blocks)
3419 while (!pto->mapAskFor.empty() && (*pto->mapAskFor.begin()).first <= nNow)
3421 const CInv& inv = (*pto->mapAskFor.begin()).second;
3422 if (!AlreadyHave(inv))
3424 LogPrint(BCLog::NET, "Requesting %s peer=%d\n", inv.ToString(), pto->GetId());
3425 vGetData.push_back(inv);
3426 if (vGetData.size() >= 1000)
3428 connman->PushMessage(pto, msgMaker.Make(NetMsgType::GETDATA, vGetData));
3429 vGetData.clear();
3431 } else {
3432 //If we're not going to ask, don't expect a response.
3433 pto->setAskFor.erase(inv.hash);
3435 pto->mapAskFor.erase(pto->mapAskFor.begin());
3437 if (!vGetData.empty())
3438 connman->PushMessage(pto, msgMaker.Make(NetMsgType::GETDATA, vGetData));
3441 // Message: feefilter
3443 // We don't want white listed peers to filter txs to us if we have -whitelistforcerelay
3444 if (pto->nVersion >= FEEFILTER_VERSION && gArgs.GetBoolArg("-feefilter", DEFAULT_FEEFILTER) &&
3445 !(pto->fWhitelisted && gArgs.GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY))) {
3446 CAmount currentFilter = mempool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFeePerK();
3447 int64_t timeNow = GetTimeMicros();
3448 if (timeNow > pto->nextSendTimeFeeFilter) {
3449 static CFeeRate default_feerate(DEFAULT_MIN_RELAY_TX_FEE);
3450 static FeeFilterRounder filterRounder(default_feerate);
3451 CAmount filterToSend = filterRounder.round(currentFilter);
3452 // We always have a fee filter of at least minRelayTxFee
3453 filterToSend = std::max(filterToSend, ::minRelayTxFee.GetFeePerK());
3454 if (filterToSend != pto->lastSentFeeFilter) {
3455 connman->PushMessage(pto, msgMaker.Make(NetMsgType::FEEFILTER, filterToSend));
3456 pto->lastSentFeeFilter = filterToSend;
3458 pto->nextSendTimeFeeFilter = PoissonNextSend(timeNow, AVG_FEEFILTER_BROADCAST_INTERVAL);
3460 // If the fee filter has changed substantially and it's still more than MAX_FEEFILTER_CHANGE_DELAY
3461 // until scheduled broadcast, then move the broadcast to within MAX_FEEFILTER_CHANGE_DELAY.
3462 else if (timeNow + MAX_FEEFILTER_CHANGE_DELAY * 1000000 < pto->nextSendTimeFeeFilter &&
3463 (currentFilter < 3 * pto->lastSentFeeFilter / 4 || currentFilter > 4 * pto->lastSentFeeFilter / 3)) {
3464 pto->nextSendTimeFeeFilter = timeNow + GetRandInt(MAX_FEEFILTER_CHANGE_DELAY) * 1000000;
3468 return true;
3471 class CNetProcessingCleanup
3473 public:
3474 CNetProcessingCleanup() {}
3475 ~CNetProcessingCleanup() {
3476 // orphan transactions
3477 mapOrphanTransactions.clear();
3478 mapOrphanTransactionsByPrev.clear();
3480 } instance_of_cnetprocessingcleanup;