[tests] Add -blocknotify functional test
[bitcoinplatinum.git] / src / net_processing.cpp
blob7fced41d4fa70b4ccd86f2f142d6ee10024658bd
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 // Internal stuff
65 namespace {
66 /** Number of nodes with fSyncStarted. */
67 int nSyncStarted = 0;
69 /**
70 * Sources of received blocks, saved to be able to send them reject
71 * messages or ban them when processing happens afterwards. Protected by
72 * cs_main.
73 * Set mapBlockSource[hash].second to false if the node should not be
74 * punished if the block is invalid.
76 std::map<uint256, std::pair<NodeId, bool>> mapBlockSource;
78 /**
79 * Filter for transactions that were recently rejected by
80 * AcceptToMemoryPool. These are not rerequested until the chain tip
81 * changes, at which point the entire filter is reset. Protected by
82 * cs_main.
84 * Without this filter we'd be re-requesting txs from each of our peers,
85 * increasing bandwidth consumption considerably. For instance, with 100
86 * peers, half of which relay a tx we don't accept, that might be a 50x
87 * bandwidth increase. A flooding attacker attempting to roll-over the
88 * filter using minimum-sized, 60byte, transactions might manage to send
89 * 1000/sec if we have fast peers, so we pick 120,000 to give our peers a
90 * two minute window to send invs to us.
92 * Decreasing the false positive rate is fairly cheap, so we pick one in a
93 * million to make it highly unlikely for users to have issues with this
94 * filter.
96 * Memory used: 1.3 MB
98 std::unique_ptr<CRollingBloomFilter> recentRejects;
99 uint256 hashRecentRejectsChainTip;
101 /** Blocks that are in flight, and that are in the queue to be downloaded. Protected by cs_main. */
102 struct QueuedBlock {
103 uint256 hash;
104 const CBlockIndex* pindex; //!< Optional.
105 bool fValidatedHeaders; //!< Whether this block has validated headers at the time of request.
106 std::unique_ptr<PartiallyDownloadedBlock> partialBlock; //!< Optional, used for CMPCTBLOCK downloads
108 std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> > mapBlocksInFlight;
110 /** Stack of nodes which we have set to announce using compact blocks */
111 std::list<NodeId> lNodesAnnouncingHeaderAndIDs;
113 /** Number of preferable block download peers. */
114 int nPreferredDownload = 0;
116 /** Number of peers from which we're downloading blocks. */
117 int nPeersWithValidatedDownloads = 0;
119 /** Relay map, protected by cs_main. */
120 typedef std::map<uint256, CTransactionRef> MapRelay;
121 MapRelay mapRelay;
122 /** Expiration-time ordered list of (expire time, relay map entry) pairs, protected by cs_main). */
123 std::deque<std::pair<int64_t, MapRelay::iterator>> vRelayExpiration;
124 } // namespace
126 namespace {
128 struct CBlockReject {
129 unsigned char chRejectCode;
130 std::string strRejectReason;
131 uint256 hashBlock;
135 * Maintain validation-specific state about nodes, protected by cs_main, instead
136 * by CNode's own locks. This simplifies asynchronous operation, where
137 * processing of incoming data is done after the ProcessMessage call returns,
138 * and we're no longer holding the node's locks.
140 struct CNodeState {
141 //! The peer's address
142 const CService address;
143 //! Whether we have a fully established connection.
144 bool fCurrentlyConnected;
145 //! Accumulated misbehaviour score for this peer.
146 int nMisbehavior;
147 //! Whether this peer should be disconnected and banned (unless whitelisted).
148 bool fShouldBan;
149 //! String name of this peer (debugging/logging purposes).
150 const std::string name;
151 //! List of asynchronously-determined block rejections to notify this peer about.
152 std::vector<CBlockReject> rejects;
153 //! The best known block we know this peer has announced.
154 const CBlockIndex *pindexBestKnownBlock;
155 //! The hash of the last unknown block this peer has announced.
156 uint256 hashLastUnknownBlock;
157 //! The last full block we both have.
158 const CBlockIndex *pindexLastCommonBlock;
159 //! The best header we have sent our peer.
160 const CBlockIndex *pindexBestHeaderSent;
161 //! Length of current-streak of unconnecting headers announcements
162 int nUnconnectingHeaders;
163 //! Whether we've started headers synchronization with this peer.
164 bool fSyncStarted;
165 //! When to potentially disconnect peer for stalling headers download
166 int64_t nHeadersSyncTimeout;
167 //! Since when we're stalling block download progress (in microseconds), or 0.
168 int64_t nStallingSince;
169 std::list<QueuedBlock> vBlocksInFlight;
170 //! When the first entry in vBlocksInFlight started downloading. Don't care when vBlocksInFlight is empty.
171 int64_t nDownloadingSince;
172 int nBlocksInFlight;
173 int nBlocksInFlightValidHeaders;
174 //! Whether we consider this a preferred download peer.
175 bool fPreferredDownload;
176 //! Whether this peer wants invs or headers (when possible) for block announcements.
177 bool fPreferHeaders;
178 //! Whether this peer wants invs or cmpctblocks (when possible) for block announcements.
179 bool fPreferHeaderAndIDs;
181 * Whether this peer will send us cmpctblocks if we request them.
182 * This is not used to gate request logic, as we really only care about fSupportsDesiredCmpctVersion,
183 * but is used as a flag to "lock in" the version of compact blocks (fWantsCmpctWitness) we send.
185 bool fProvidesHeaderAndIDs;
186 //! Whether this peer can give us witnesses
187 bool fHaveWitness;
188 //! Whether this peer wants witnesses in cmpctblocks/blocktxns
189 bool fWantsCmpctWitness;
191 * If we've announced NODE_WITNESS to this peer: whether the peer sends witnesses in cmpctblocks/blocktxns,
192 * otherwise: whether this peer sends non-witnesses in cmpctblocks/blocktxns.
194 bool fSupportsDesiredCmpctVersion;
196 CNodeState(CAddress addrIn, std::string addrNameIn) : address(addrIn), name(addrNameIn) {
197 fCurrentlyConnected = false;
198 nMisbehavior = 0;
199 fShouldBan = false;
200 pindexBestKnownBlock = nullptr;
201 hashLastUnknownBlock.SetNull();
202 pindexLastCommonBlock = nullptr;
203 pindexBestHeaderSent = nullptr;
204 nUnconnectingHeaders = 0;
205 fSyncStarted = false;
206 nHeadersSyncTimeout = 0;
207 nStallingSince = 0;
208 nDownloadingSince = 0;
209 nBlocksInFlight = 0;
210 nBlocksInFlightValidHeaders = 0;
211 fPreferredDownload = false;
212 fPreferHeaders = false;
213 fPreferHeaderAndIDs = false;
214 fProvidesHeaderAndIDs = false;
215 fHaveWitness = false;
216 fWantsCmpctWitness = false;
217 fSupportsDesiredCmpctVersion = false;
221 /** Map maintaining per-node state. Requires cs_main. */
222 std::map<NodeId, CNodeState> mapNodeState;
224 // Requires cs_main.
225 CNodeState *State(NodeId pnode) {
226 std::map<NodeId, CNodeState>::iterator it = mapNodeState.find(pnode);
227 if (it == mapNodeState.end())
228 return nullptr;
229 return &it->second;
232 void UpdatePreferredDownload(CNode* node, CNodeState* state)
234 nPreferredDownload -= state->fPreferredDownload;
236 // Whether this node should be marked as a preferred download node.
237 state->fPreferredDownload = (!node->fInbound || node->fWhitelisted) && !node->fOneShot && !node->fClient;
239 nPreferredDownload += state->fPreferredDownload;
242 void PushNodeVersion(CNode *pnode, CConnman* connman, int64_t nTime)
244 ServiceFlags nLocalNodeServices = pnode->GetLocalServices();
245 uint64_t nonce = pnode->GetLocalNonce();
246 int nNodeStartingHeight = pnode->GetMyStartingHeight();
247 NodeId nodeid = pnode->GetId();
248 CAddress addr = pnode->addr;
250 CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService(), addr.nServices));
251 CAddress addrMe = CAddress(CService(), nLocalNodeServices);
253 connman->PushMessage(pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t)nLocalNodeServices, nTime, addrYou, addrMe,
254 nonce, strSubVersion, nNodeStartingHeight, ::fRelayTxes));
256 if (fLogIPs) {
257 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);
258 } else {
259 LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), nodeid);
263 // Requires cs_main.
264 // Returns a bool indicating whether we requested this block.
265 // Also used if a block was /not/ received and timed out or started with another peer
266 bool MarkBlockAsReceived(const uint256& hash) {
267 std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
268 if (itInFlight != mapBlocksInFlight.end()) {
269 CNodeState *state = State(itInFlight->second.first);
270 assert(state != nullptr);
271 state->nBlocksInFlightValidHeaders -= itInFlight->second.second->fValidatedHeaders;
272 if (state->nBlocksInFlightValidHeaders == 0 && itInFlight->second.second->fValidatedHeaders) {
273 // Last validated block on the queue was received.
274 nPeersWithValidatedDownloads--;
276 if (state->vBlocksInFlight.begin() == itInFlight->second.second) {
277 // First block on the queue was received, update the start download time for the next one
278 state->nDownloadingSince = std::max(state->nDownloadingSince, GetTimeMicros());
280 state->vBlocksInFlight.erase(itInFlight->second.second);
281 state->nBlocksInFlight--;
282 state->nStallingSince = 0;
283 mapBlocksInFlight.erase(itInFlight);
284 return true;
286 return false;
289 // Requires cs_main.
290 // returns false, still setting pit, if the block was already in flight from the same peer
291 // pit will only be valid as long as the same cs_main lock is being held
292 bool MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const CBlockIndex* pindex = nullptr, std::list<QueuedBlock>::iterator** pit = nullptr) {
293 CNodeState *state = State(nodeid);
294 assert(state != nullptr);
296 // Short-circuit most stuff in case its from the same node
297 std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
298 if (itInFlight != mapBlocksInFlight.end() && itInFlight->second.first == nodeid) {
299 if (pit) {
300 *pit = &itInFlight->second.second;
302 return false;
305 // Make sure it's not listed somewhere already.
306 MarkBlockAsReceived(hash);
308 std::list<QueuedBlock>::iterator it = state->vBlocksInFlight.insert(state->vBlocksInFlight.end(),
309 {hash, pindex, pindex != nullptr, std::unique_ptr<PartiallyDownloadedBlock>(pit ? new PartiallyDownloadedBlock(&mempool) : nullptr)});
310 state->nBlocksInFlight++;
311 state->nBlocksInFlightValidHeaders += it->fValidatedHeaders;
312 if (state->nBlocksInFlight == 1) {
313 // We're starting a block download (batch) from this peer.
314 state->nDownloadingSince = GetTimeMicros();
316 if (state->nBlocksInFlightValidHeaders == 1 && pindex != nullptr) {
317 nPeersWithValidatedDownloads++;
319 itInFlight = mapBlocksInFlight.insert(std::make_pair(hash, std::make_pair(nodeid, it))).first;
320 if (pit)
321 *pit = &itInFlight->second.second;
322 return true;
325 /** Check whether the last unknown block a peer advertised is not yet known. */
326 void ProcessBlockAvailability(NodeId nodeid) {
327 CNodeState *state = State(nodeid);
328 assert(state != nullptr);
330 if (!state->hashLastUnknownBlock.IsNull()) {
331 BlockMap::iterator itOld = mapBlockIndex.find(state->hashLastUnknownBlock);
332 if (itOld != mapBlockIndex.end() && itOld->second->nChainWork > 0) {
333 if (state->pindexBestKnownBlock == nullptr || itOld->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
334 state->pindexBestKnownBlock = itOld->second;
335 state->hashLastUnknownBlock.SetNull();
340 /** Update tracking information about which blocks a peer is assumed to have. */
341 void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
342 CNodeState *state = State(nodeid);
343 assert(state != nullptr);
345 ProcessBlockAvailability(nodeid);
347 BlockMap::iterator it = mapBlockIndex.find(hash);
348 if (it != mapBlockIndex.end() && it->second->nChainWork > 0) {
349 // An actually better block was announced.
350 if (state->pindexBestKnownBlock == nullptr || it->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
351 state->pindexBestKnownBlock = it->second;
352 } else {
353 // An unknown block was announced; just assume that the latest one is the best one.
354 state->hashLastUnknownBlock = hash;
358 void MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid, CConnman* connman) {
359 AssertLockHeld(cs_main);
360 CNodeState* nodestate = State(nodeid);
361 if (!nodestate || !nodestate->fSupportsDesiredCmpctVersion) {
362 // Never ask from peers who can't provide witnesses.
363 return;
365 if (nodestate->fProvidesHeaderAndIDs) {
366 for (std::list<NodeId>::iterator it = lNodesAnnouncingHeaderAndIDs.begin(); it != lNodesAnnouncingHeaderAndIDs.end(); it++) {
367 if (*it == nodeid) {
368 lNodesAnnouncingHeaderAndIDs.erase(it);
369 lNodesAnnouncingHeaderAndIDs.push_back(nodeid);
370 return;
373 connman->ForNode(nodeid, [connman](CNode* pfrom){
374 bool fAnnounceUsingCMPCTBLOCK = false;
375 uint64_t nCMPCTBLOCKVersion = (pfrom->GetLocalServices() & NODE_WITNESS) ? 2 : 1;
376 if (lNodesAnnouncingHeaderAndIDs.size() >= 3) {
377 // As per BIP152, we only get 3 of our peers to announce
378 // blocks using compact encodings.
379 connman->ForNode(lNodesAnnouncingHeaderAndIDs.front(), [connman, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion](CNode* pnodeStop){
380 connman->PushMessage(pnodeStop, CNetMsgMaker(pnodeStop->GetSendVersion()).Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
381 return true;
383 lNodesAnnouncingHeaderAndIDs.pop_front();
385 fAnnounceUsingCMPCTBLOCK = true;
386 connman->PushMessage(pfrom, CNetMsgMaker(pfrom->GetSendVersion()).Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
387 lNodesAnnouncingHeaderAndIDs.push_back(pfrom->GetId());
388 return true;
393 // Requires cs_main
394 bool CanDirectFetch(const Consensus::Params &consensusParams)
396 return chainActive.Tip()->GetBlockTime() > GetAdjustedTime() - consensusParams.nPowTargetSpacing * 20;
399 // Requires cs_main
400 bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex)
402 if (state->pindexBestKnownBlock && pindex == state->pindexBestKnownBlock->GetAncestor(pindex->nHeight))
403 return true;
404 if (state->pindexBestHeaderSent && pindex == state->pindexBestHeaderSent->GetAncestor(pindex->nHeight))
405 return true;
406 return false;
409 /** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
410 * at most count entries. */
411 void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams) {
412 if (count == 0)
413 return;
415 vBlocks.reserve(vBlocks.size() + count);
416 CNodeState *state = State(nodeid);
417 assert(state != nullptr);
419 // Make sure pindexBestKnownBlock is up to date, we'll need it.
420 ProcessBlockAvailability(nodeid);
422 if (state->pindexBestKnownBlock == nullptr || state->pindexBestKnownBlock->nChainWork < chainActive.Tip()->nChainWork || state->pindexBestKnownBlock->nChainWork < nMinimumChainWork) {
423 // This peer has nothing interesting.
424 return;
427 if (state->pindexLastCommonBlock == nullptr) {
428 // Bootstrap quickly by guessing a parent of our best tip is the forking point.
429 // Guessing wrong in either direction is not a problem.
430 state->pindexLastCommonBlock = chainActive[std::min(state->pindexBestKnownBlock->nHeight, chainActive.Height())];
433 // If the peer reorganized, our previous pindexLastCommonBlock may not be an ancestor
434 // of its current tip anymore. Go back enough to fix that.
435 state->pindexLastCommonBlock = LastCommonAncestor(state->pindexLastCommonBlock, state->pindexBestKnownBlock);
436 if (state->pindexLastCommonBlock == state->pindexBestKnownBlock)
437 return;
439 std::vector<const CBlockIndex*> vToFetch;
440 const CBlockIndex *pindexWalk = state->pindexLastCommonBlock;
441 // Never fetch further than the best block we know the peer has, or more than BLOCK_DOWNLOAD_WINDOW + 1 beyond the last
442 // linked block we have in common with this peer. The +1 is so we can detect stalling, namely if we would be able to
443 // download that next block if the window were 1 larger.
444 int nWindowEnd = state->pindexLastCommonBlock->nHeight + BLOCK_DOWNLOAD_WINDOW;
445 int nMaxHeight = std::min<int>(state->pindexBestKnownBlock->nHeight, nWindowEnd + 1);
446 NodeId waitingfor = -1;
447 while (pindexWalk->nHeight < nMaxHeight) {
448 // Read up to 128 (or more, if more blocks than that are needed) successors of pindexWalk (towards
449 // pindexBestKnownBlock) into vToFetch. We fetch 128, because CBlockIndex::GetAncestor may be as expensive
450 // as iterating over ~100 CBlockIndex* entries anyway.
451 int nToFetch = std::min(nMaxHeight - pindexWalk->nHeight, std::max<int>(count - vBlocks.size(), 128));
452 vToFetch.resize(nToFetch);
453 pindexWalk = state->pindexBestKnownBlock->GetAncestor(pindexWalk->nHeight + nToFetch);
454 vToFetch[nToFetch - 1] = pindexWalk;
455 for (unsigned int i = nToFetch - 1; i > 0; i--) {
456 vToFetch[i - 1] = vToFetch[i]->pprev;
459 // Iterate over those blocks in vToFetch (in forward direction), adding the ones that
460 // are not yet downloaded and not in flight to vBlocks. In the mean time, update
461 // pindexLastCommonBlock as long as all ancestors are already downloaded, or if it's
462 // already part of our chain (and therefore don't need it even if pruned).
463 for (const CBlockIndex* pindex : vToFetch) {
464 if (!pindex->IsValid(BLOCK_VALID_TREE)) {
465 // We consider the chain that this peer is on invalid.
466 return;
468 if (!State(nodeid)->fHaveWitness && IsWitnessEnabled(pindex->pprev, consensusParams)) {
469 // We wouldn't download this block or its descendants from this peer.
470 return;
472 if (pindex->nStatus & BLOCK_HAVE_DATA || chainActive.Contains(pindex)) {
473 if (pindex->nChainTx)
474 state->pindexLastCommonBlock = pindex;
475 } else if (mapBlocksInFlight.count(pindex->GetBlockHash()) == 0) {
476 // The block is not already downloaded, and not yet in flight.
477 if (pindex->nHeight > nWindowEnd) {
478 // We reached the end of the window.
479 if (vBlocks.size() == 0 && waitingfor != nodeid) {
480 // We aren't able to fetch anything, but we would be if the download window was one larger.
481 nodeStaller = waitingfor;
483 return;
485 vBlocks.push_back(pindex);
486 if (vBlocks.size() == count) {
487 return;
489 } else if (waitingfor == -1) {
490 // This is the first already-in-flight block.
491 waitingfor = mapBlocksInFlight[pindex->GetBlockHash()].first;
497 } // namespace
499 void PeerLogicValidation::InitializeNode(CNode *pnode) {
500 CAddress addr = pnode->addr;
501 std::string addrName = pnode->GetAddrName();
502 NodeId nodeid = pnode->GetId();
504 LOCK(cs_main);
505 mapNodeState.emplace_hint(mapNodeState.end(), std::piecewise_construct, std::forward_as_tuple(nodeid), std::forward_as_tuple(addr, std::move(addrName)));
507 if(!pnode->fInbound)
508 PushNodeVersion(pnode, connman, GetTime());
511 void PeerLogicValidation::FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTime) {
512 fUpdateConnectionTime = false;
513 LOCK(cs_main);
514 CNodeState *state = State(nodeid);
515 assert(state != nullptr);
517 if (state->fSyncStarted)
518 nSyncStarted--;
520 if (state->nMisbehavior == 0 && state->fCurrentlyConnected) {
521 fUpdateConnectionTime = true;
524 for (const QueuedBlock& entry : state->vBlocksInFlight) {
525 mapBlocksInFlight.erase(entry.hash);
527 EraseOrphansFor(nodeid);
528 nPreferredDownload -= state->fPreferredDownload;
529 nPeersWithValidatedDownloads -= (state->nBlocksInFlightValidHeaders != 0);
530 assert(nPeersWithValidatedDownloads >= 0);
532 mapNodeState.erase(nodeid);
534 if (mapNodeState.empty()) {
535 // Do a consistency check after the last peer is removed.
536 assert(mapBlocksInFlight.empty());
537 assert(nPreferredDownload == 0);
538 assert(nPeersWithValidatedDownloads == 0);
540 LogPrint(BCLog::NET, "Cleared nodestate for peer=%d\n", nodeid);
543 bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
544 LOCK(cs_main);
545 CNodeState *state = State(nodeid);
546 if (state == nullptr)
547 return false;
548 stats.nMisbehavior = state->nMisbehavior;
549 stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
550 stats.nCommonHeight = state->pindexLastCommonBlock ? state->pindexLastCommonBlock->nHeight : -1;
551 for (const QueuedBlock& queue : state->vBlocksInFlight) {
552 if (queue.pindex)
553 stats.vHeightInFlight.push_back(queue.pindex->nHeight);
555 return true;
558 //////////////////////////////////////////////////////////////////////////////
560 // mapOrphanTransactions
563 void AddToCompactExtraTransactions(const CTransactionRef& tx)
565 size_t max_extra_txn = gArgs.GetArg("-blockreconstructionextratxn", DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN);
566 if (max_extra_txn <= 0)
567 return;
568 if (!vExtraTxnForCompact.size())
569 vExtraTxnForCompact.resize(max_extra_txn);
570 vExtraTxnForCompact[vExtraTxnForCompactIt] = std::make_pair(tx->GetWitnessHash(), tx);
571 vExtraTxnForCompactIt = (vExtraTxnForCompactIt + 1) % max_extra_txn;
574 bool AddOrphanTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
576 const uint256& hash = tx->GetHash();
577 if (mapOrphanTransactions.count(hash))
578 return false;
580 // Ignore big transactions, to avoid a
581 // send-big-orphans memory exhaustion attack. If a peer has a legitimate
582 // large transaction with a missing parent then we assume
583 // it will rebroadcast it later, after the parent transaction(s)
584 // have been mined or received.
585 // 100 orphans, each of which is at most 99,999 bytes big is
586 // at most 10 megabytes of orphans and somewhat more byprev index (in the worst case):
587 unsigned int sz = GetTransactionWeight(*tx);
588 if (sz >= MAX_STANDARD_TX_WEIGHT)
590 LogPrint(BCLog::MEMPOOL, "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString());
591 return false;
594 auto ret = mapOrphanTransactions.emplace(hash, COrphanTx{tx, peer, GetTime() + ORPHAN_TX_EXPIRE_TIME});
595 assert(ret.second);
596 for (const CTxIn& txin : tx->vin) {
597 mapOrphanTransactionsByPrev[txin.prevout].insert(ret.first);
600 AddToCompactExtraTransactions(tx);
602 LogPrint(BCLog::MEMPOOL, "stored orphan tx %s (mapsz %u outsz %u)\n", hash.ToString(),
603 mapOrphanTransactions.size(), mapOrphanTransactionsByPrev.size());
604 return true;
607 int static EraseOrphanTx(uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
609 std::map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.find(hash);
610 if (it == mapOrphanTransactions.end())
611 return 0;
612 for (const CTxIn& txin : it->second.tx->vin)
614 auto itPrev = mapOrphanTransactionsByPrev.find(txin.prevout);
615 if (itPrev == mapOrphanTransactionsByPrev.end())
616 continue;
617 itPrev->second.erase(it);
618 if (itPrev->second.empty())
619 mapOrphanTransactionsByPrev.erase(itPrev);
621 mapOrphanTransactions.erase(it);
622 return 1;
625 void EraseOrphansFor(NodeId peer)
627 int nErased = 0;
628 std::map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
629 while (iter != mapOrphanTransactions.end())
631 std::map<uint256, COrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
632 if (maybeErase->second.fromPeer == peer)
634 nErased += EraseOrphanTx(maybeErase->second.tx->GetHash());
637 if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx from peer=%d\n", nErased, peer);
641 unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
643 unsigned int nEvicted = 0;
644 static int64_t nNextSweep;
645 int64_t nNow = GetTime();
646 if (nNextSweep <= nNow) {
647 // Sweep out expired orphan pool entries:
648 int nErased = 0;
649 int64_t nMinExpTime = nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL;
650 std::map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
651 while (iter != mapOrphanTransactions.end())
653 std::map<uint256, COrphanTx>::iterator maybeErase = iter++;
654 if (maybeErase->second.nTimeExpire <= nNow) {
655 nErased += EraseOrphanTx(maybeErase->second.tx->GetHash());
656 } else {
657 nMinExpTime = std::min(maybeErase->second.nTimeExpire, nMinExpTime);
660 // Sweep again 5 minutes after the next entry that expires in order to batch the linear scan.
661 nNextSweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL;
662 if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx due to expiration\n", nErased);
664 while (mapOrphanTransactions.size() > nMaxOrphans)
666 // Evict a random orphan:
667 uint256 randomhash = GetRandHash();
668 std::map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.lower_bound(randomhash);
669 if (it == mapOrphanTransactions.end())
670 it = mapOrphanTransactions.begin();
671 EraseOrphanTx(it->first);
672 ++nEvicted;
674 return nEvicted;
677 // Requires cs_main.
678 void Misbehaving(NodeId pnode, int howmuch)
680 if (howmuch == 0)
681 return;
683 CNodeState *state = State(pnode);
684 if (state == nullptr)
685 return;
687 state->nMisbehavior += howmuch;
688 int banscore = gArgs.GetArg("-banscore", DEFAULT_BANSCORE_THRESHOLD);
689 if (state->nMisbehavior >= banscore && state->nMisbehavior - howmuch < banscore)
691 LogPrintf("%s: %s peer=%d (%d -> %d) BAN THRESHOLD EXCEEDED\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior);
692 state->fShouldBan = true;
693 } else
694 LogPrintf("%s: %s peer=%d (%d -> %d)\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior);
704 //////////////////////////////////////////////////////////////////////////////
706 // blockchain -> download logic notification
709 PeerLogicValidation::PeerLogicValidation(CConnman* connmanIn) : connman(connmanIn) {
710 // Initialize global variables that cannot be constructed at startup.
711 recentRejects.reset(new CRollingBloomFilter(120000, 0.000001));
714 void PeerLogicValidation::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex, const std::vector<CTransactionRef>& vtxConflicted) {
715 LOCK(cs_main);
717 std::vector<uint256> vOrphanErase;
719 for (const CTransactionRef& ptx : pblock->vtx) {
720 const CTransaction& tx = *ptx;
722 // Which orphan pool entries must we evict?
723 for (const auto& txin : tx.vin) {
724 auto itByPrev = mapOrphanTransactionsByPrev.find(txin.prevout);
725 if (itByPrev == mapOrphanTransactionsByPrev.end()) continue;
726 for (auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); ++mi) {
727 const CTransaction& orphanTx = *(*mi)->second.tx;
728 const uint256& orphanHash = orphanTx.GetHash();
729 vOrphanErase.push_back(orphanHash);
734 // Erase orphan transactions include or precluded by this block
735 if (vOrphanErase.size()) {
736 int nErased = 0;
737 for (uint256 &orphanHash : vOrphanErase) {
738 nErased += EraseOrphanTx(orphanHash);
740 LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx included or conflicted by block\n", nErased);
744 // All of the following cache a recent block, and are protected by cs_most_recent_block
745 static CCriticalSection cs_most_recent_block;
746 static std::shared_ptr<const CBlock> most_recent_block;
747 static std::shared_ptr<const CBlockHeaderAndShortTxIDs> most_recent_compact_block;
748 static uint256 most_recent_block_hash;
749 static bool fWitnessesPresentInMostRecentCompactBlock;
751 void PeerLogicValidation::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) {
752 std::shared_ptr<const CBlockHeaderAndShortTxIDs> pcmpctblock = std::make_shared<const CBlockHeaderAndShortTxIDs> (*pblock, true);
753 const CNetMsgMaker msgMaker(PROTOCOL_VERSION);
755 LOCK(cs_main);
757 static int nHighestFastAnnounce = 0;
758 if (pindex->nHeight <= nHighestFastAnnounce)
759 return;
760 nHighestFastAnnounce = pindex->nHeight;
762 bool fWitnessEnabled = IsWitnessEnabled(pindex->pprev, Params().GetConsensus());
763 uint256 hashBlock(pblock->GetHash());
766 LOCK(cs_most_recent_block);
767 most_recent_block_hash = hashBlock;
768 most_recent_block = pblock;
769 most_recent_compact_block = pcmpctblock;
770 fWitnessesPresentInMostRecentCompactBlock = fWitnessEnabled;
773 connman->ForEachNode([this, &pcmpctblock, pindex, &msgMaker, fWitnessEnabled, &hashBlock](CNode* pnode) {
774 // TODO: Avoid the repeated-serialization here
775 if (pnode->nVersion < INVALID_CB_NO_BAN_VERSION || pnode->fDisconnect)
776 return;
777 ProcessBlockAvailability(pnode->GetId());
778 CNodeState &state = *State(pnode->GetId());
779 // If the peer has, or we announced to them the previous block already,
780 // but we don't think they have this one, go ahead and announce it
781 if (state.fPreferHeaderAndIDs && (!fWitnessEnabled || state.fWantsCmpctWitness) &&
782 !PeerHasHeader(&state, pindex) && PeerHasHeader(&state, pindex->pprev)) {
784 LogPrint(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", "PeerLogicValidation::NewPoWValidBlock",
785 hashBlock.ToString(), pnode->GetId());
786 connman->PushMessage(pnode, msgMaker.Make(NetMsgType::CMPCTBLOCK, *pcmpctblock));
787 state.pindexBestHeaderSent = pindex;
792 void PeerLogicValidation::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
793 const int nNewHeight = pindexNew->nHeight;
794 connman->SetBestHeight(nNewHeight);
796 if (!fInitialDownload) {
797 // Find the hashes of all blocks that weren't previously in the best chain.
798 std::vector<uint256> vHashes;
799 const CBlockIndex *pindexToAnnounce = pindexNew;
800 while (pindexToAnnounce != pindexFork) {
801 vHashes.push_back(pindexToAnnounce->GetBlockHash());
802 pindexToAnnounce = pindexToAnnounce->pprev;
803 if (vHashes.size() == MAX_BLOCKS_TO_ANNOUNCE) {
804 // Limit announcements in case of a huge reorganization.
805 // Rely on the peer's synchronization mechanism in that case.
806 break;
809 // Relay inventory, but don't relay old inventory during initial block download.
810 connman->ForEachNode([nNewHeight, &vHashes](CNode* pnode) {
811 if (nNewHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : 0)) {
812 for (const uint256& hash : reverse_iterate(vHashes)) {
813 pnode->PushBlockHash(hash);
817 connman->WakeMessageHandler();
820 nTimeBestReceived = GetTime();
823 void PeerLogicValidation::BlockChecked(const CBlock& block, const CValidationState& state) {
824 LOCK(cs_main);
826 const uint256 hash(block.GetHash());
827 std::map<uint256, std::pair<NodeId, bool>>::iterator it = mapBlockSource.find(hash);
829 int nDoS = 0;
830 if (state.IsInvalid(nDoS)) {
831 // Don't send reject message with code 0 or an internal reject code.
832 if (it != mapBlockSource.end() && State(it->second.first) && state.GetRejectCode() > 0 && state.GetRejectCode() < REJECT_INTERNAL) {
833 CBlockReject reject = {(unsigned char)state.GetRejectCode(), state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), hash};
834 State(it->second.first)->rejects.push_back(reject);
835 if (nDoS > 0 && it->second.second)
836 Misbehaving(it->second.first, nDoS);
839 // Check that:
840 // 1. The block is valid
841 // 2. We're not in initial block download
842 // 3. This is currently the best block we're aware of. We haven't updated
843 // the tip yet so we have no way to check this directly here. Instead we
844 // just check that there are currently no other blocks in flight.
845 else if (state.IsValid() &&
846 !IsInitialBlockDownload() &&
847 mapBlocksInFlight.count(hash) == mapBlocksInFlight.size()) {
848 if (it != mapBlockSource.end()) {
849 MaybeSetPeerAsAnnouncingHeaderAndIDs(it->second.first, connman);
852 if (it != mapBlockSource.end())
853 mapBlockSource.erase(it);
856 //////////////////////////////////////////////////////////////////////////////
858 // Messages
862 bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
864 switch (inv.type)
866 case MSG_TX:
867 case MSG_WITNESS_TX:
869 assert(recentRejects);
870 if (chainActive.Tip()->GetBlockHash() != hashRecentRejectsChainTip)
872 // If the chain tip has changed previously rejected transactions
873 // might be now valid, e.g. due to a nLockTime'd tx becoming valid,
874 // or a double-spend. Reset the rejects filter and give those
875 // txs a second chance.
876 hashRecentRejectsChainTip = chainActive.Tip()->GetBlockHash();
877 recentRejects->reset();
880 return recentRejects->contains(inv.hash) ||
881 mempool.exists(inv.hash) ||
882 mapOrphanTransactions.count(inv.hash) ||
883 pcoinsTip->HaveCoinInCache(COutPoint(inv.hash, 0)) || // Best effort: only try output 0 and 1
884 pcoinsTip->HaveCoinInCache(COutPoint(inv.hash, 1));
886 case MSG_BLOCK:
887 case MSG_WITNESS_BLOCK:
888 return mapBlockIndex.count(inv.hash);
890 // Don't know what it is, just say we already got one
891 return true;
894 static void RelayTransaction(const CTransaction& tx, CConnman* connman)
896 CInv inv(MSG_TX, tx.GetHash());
897 connman->ForEachNode([&inv](CNode* pnode)
899 pnode->PushInventory(inv);
903 static void RelayAddress(const CAddress& addr, bool fReachable, CConnman* connman)
905 unsigned int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
907 // Relay to a limited number of other nodes
908 // Use deterministic randomness to send to the same nodes for 24 hours
909 // at a time so the addrKnowns of the chosen nodes prevent repeats
910 uint64_t hashAddr = addr.GetHash();
911 const CSipHasher hasher = connman->GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));
912 FastRandomContext insecure_rand;
914 std::array<std::pair<uint64_t, CNode*>,2> best{{{0, nullptr}, {0, nullptr}}};
915 assert(nRelayNodes <= best.size());
917 auto sortfunc = [&best, &hasher, nRelayNodes](CNode* pnode) {
918 if (pnode->nVersion >= CADDR_TIME_VERSION) {
919 uint64_t hashKey = CSipHasher(hasher).Write(pnode->GetId()).Finalize();
920 for (unsigned int i = 0; i < nRelayNodes; i++) {
921 if (hashKey > best[i].first) {
922 std::copy(best.begin() + i, best.begin() + nRelayNodes - 1, best.begin() + i + 1);
923 best[i] = std::make_pair(hashKey, pnode);
924 break;
930 auto pushfunc = [&addr, &best, nRelayNodes, &insecure_rand] {
931 for (unsigned int i = 0; i < nRelayNodes && best[i].first != 0; i++) {
932 best[i].second->PushAddress(addr, insecure_rand);
936 connman->ForEachNodeThen(std::move(sortfunc), std::move(pushfunc));
939 void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParams, CConnman* connman, const std::atomic<bool>& interruptMsgProc)
941 std::deque<CInv>::iterator it = pfrom->vRecvGetData.begin();
942 std::vector<CInv> vNotFound;
943 const CNetMsgMaker msgMaker(pfrom->GetSendVersion());
944 LOCK(cs_main);
946 while (it != pfrom->vRecvGetData.end()) {
947 // Don't bother if send buffer is too full to respond anyway
948 if (pfrom->fPauseSend)
949 break;
951 const CInv &inv = *it;
953 if (interruptMsgProc)
954 return;
956 it++;
958 if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK || inv.type == MSG_CMPCT_BLOCK || inv.type == MSG_WITNESS_BLOCK)
960 bool send = false;
961 BlockMap::iterator mi = mapBlockIndex.find(inv.hash);
962 std::shared_ptr<const CBlock> a_recent_block;
963 std::shared_ptr<const CBlockHeaderAndShortTxIDs> a_recent_compact_block;
964 bool fWitnessesPresentInARecentCompactBlock;
966 LOCK(cs_most_recent_block);
967 a_recent_block = most_recent_block;
968 a_recent_compact_block = most_recent_compact_block;
969 fWitnessesPresentInARecentCompactBlock = fWitnessesPresentInMostRecentCompactBlock;
971 if (mi != mapBlockIndex.end())
973 if (mi->second->nChainTx && !mi->second->IsValid(BLOCK_VALID_SCRIPTS) &&
974 mi->second->IsValid(BLOCK_VALID_TREE)) {
975 // If we have the block and all of its parents, but have not yet validated it,
976 // we might be in the middle of connecting it (ie in the unlock of cs_main
977 // before ActivateBestChain but after AcceptBlock).
978 // In this case, we need to run ActivateBestChain prior to checking the relay
979 // conditions below.
980 CValidationState dummy;
981 ActivateBestChain(dummy, Params(), a_recent_block);
983 if (chainActive.Contains(mi->second)) {
984 send = true;
985 } else {
986 static const int nOneMonth = 30 * 24 * 60 * 60;
987 // To prevent fingerprinting attacks, only send blocks outside of the active
988 // chain if they are valid, and no more than a month older (both in time, and in
989 // best equivalent proof of work) than the best header chain we know about.
990 send = mi->second->IsValid(BLOCK_VALID_SCRIPTS) && (pindexBestHeader != nullptr) &&
991 (pindexBestHeader->GetBlockTime() - mi->second->GetBlockTime() < nOneMonth) &&
992 (GetBlockProofEquivalentTime(*pindexBestHeader, *mi->second, *pindexBestHeader, consensusParams) < nOneMonth);
993 if (!send) {
994 LogPrintf("%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom->GetId());
998 // disconnect node in case we have reached the outbound limit for serving historical blocks
999 // never disconnect whitelisted nodes
1000 static const int nOneWeek = 7 * 24 * 60 * 60; // assume > 1 week = historical
1001 if (send && connman->OutboundTargetReached(true) && ( ((pindexBestHeader != nullptr) && (pindexBestHeader->GetBlockTime() - mi->second->GetBlockTime() > nOneWeek)) || inv.type == MSG_FILTERED_BLOCK) && !pfrom->fWhitelisted)
1003 LogPrint(BCLog::NET, "historical block serving limit reached, disconnect peer=%d\n", pfrom->GetId());
1005 //disconnect node
1006 pfrom->fDisconnect = true;
1007 send = false;
1009 // Pruned nodes may have deleted the block, so check whether
1010 // it's available before trying to send.
1011 if (send && (mi->second->nStatus & BLOCK_HAVE_DATA))
1013 std::shared_ptr<const CBlock> pblock;
1014 if (a_recent_block && a_recent_block->GetHash() == (*mi).second->GetBlockHash()) {
1015 pblock = a_recent_block;
1016 } else {
1017 // Send block from disk
1018 std::shared_ptr<CBlock> pblockRead = std::make_shared<CBlock>();
1019 if (!ReadBlockFromDisk(*pblockRead, (*mi).second, consensusParams))
1020 assert(!"cannot load block from disk");
1021 pblock = pblockRead;
1023 if (inv.type == MSG_BLOCK)
1024 connman->PushMessage(pfrom, msgMaker.Make(SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::BLOCK, *pblock));
1025 else if (inv.type == MSG_WITNESS_BLOCK)
1026 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::BLOCK, *pblock));
1027 else if (inv.type == MSG_FILTERED_BLOCK)
1029 bool sendMerkleBlock = false;
1030 CMerkleBlock merkleBlock;
1032 LOCK(pfrom->cs_filter);
1033 if (pfrom->pfilter) {
1034 sendMerkleBlock = true;
1035 merkleBlock = CMerkleBlock(*pblock, *pfrom->pfilter);
1038 if (sendMerkleBlock) {
1039 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::MERKLEBLOCK, merkleBlock));
1040 // CMerkleBlock just contains hashes, so also push any transactions in the block the client did not see
1041 // This avoids hurting performance by pointlessly requiring a round-trip
1042 // Note that there is currently no way for a node to request any single transactions we didn't send here -
1043 // they must either disconnect and retry or request the full block.
1044 // Thus, the protocol spec specified allows for us to provide duplicate txn here,
1045 // however we MUST always provide at least what the remote peer needs
1046 typedef std::pair<unsigned int, uint256> PairType;
1047 for (PairType& pair : merkleBlock.vMatchedTxn)
1048 connman->PushMessage(pfrom, msgMaker.Make(SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::TX, *pblock->vtx[pair.first]));
1050 // else
1051 // no response
1053 else if (inv.type == MSG_CMPCT_BLOCK)
1055 // If a peer is asking for old blocks, we're almost guaranteed
1056 // they won't have a useful mempool to match against a compact block,
1057 // and we don't feel like constructing the object for them, so
1058 // instead we respond with the full, non-compact block.
1059 bool fPeerWantsWitness = State(pfrom->GetId())->fWantsCmpctWitness;
1060 int nSendFlags = fPeerWantsWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
1061 if (CanDirectFetch(consensusParams) && mi->second->nHeight >= chainActive.Height() - MAX_CMPCTBLOCK_DEPTH) {
1062 if ((fPeerWantsWitness || !fWitnessesPresentInARecentCompactBlock) && a_recent_compact_block && a_recent_compact_block->header.GetHash() == mi->second->GetBlockHash()) {
1063 connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, *a_recent_compact_block));
1064 } else {
1065 CBlockHeaderAndShortTxIDs cmpctblock(*pblock, fPeerWantsWitness);
1066 connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
1068 } else {
1069 connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::BLOCK, *pblock));
1073 // Trigger the peer node to send a getblocks request for the next batch of inventory
1074 if (inv.hash == pfrom->hashContinue)
1076 // Bypass PushInventory, this must send even if redundant,
1077 // and we want it right after the last block so they don't
1078 // wait for other stuff first.
1079 std::vector<CInv> vInv;
1080 vInv.push_back(CInv(MSG_BLOCK, chainActive.Tip()->GetBlockHash()));
1081 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::INV, vInv));
1082 pfrom->hashContinue.SetNull();
1086 else if (inv.type == MSG_TX || inv.type == MSG_WITNESS_TX)
1088 // Send stream from relay memory
1089 bool push = false;
1090 auto mi = mapRelay.find(inv.hash);
1091 int nSendFlags = (inv.type == MSG_TX ? SERIALIZE_TRANSACTION_NO_WITNESS : 0);
1092 if (mi != mapRelay.end()) {
1093 connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *mi->second));
1094 push = true;
1095 } else if (pfrom->timeLastMempoolReq) {
1096 auto txinfo = mempool.info(inv.hash);
1097 // To protect privacy, do not answer getdata using the mempool when
1098 // that TX couldn't have been INVed in reply to a MEMPOOL request.
1099 if (txinfo.tx && txinfo.nTime <= pfrom->timeLastMempoolReq) {
1100 connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *txinfo.tx));
1101 push = true;
1104 if (!push) {
1105 vNotFound.push_back(inv);
1109 // Track requests for our stuff.
1110 GetMainSignals().Inventory(inv.hash);
1112 if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK || inv.type == MSG_CMPCT_BLOCK || inv.type == MSG_WITNESS_BLOCK)
1113 break;
1117 pfrom->vRecvGetData.erase(pfrom->vRecvGetData.begin(), it);
1119 if (!vNotFound.empty()) {
1120 // Let the peer know that we didn't find what it asked for, so it doesn't
1121 // have to wait around forever. Currently only SPV clients actually care
1122 // about this message: it's needed when they are recursively walking the
1123 // dependencies of relevant unconfirmed transactions. SPV clients want to
1124 // do that because they want to know about (and store and rebroadcast and
1125 // risk analyze) the dependencies of transactions relevant to them, without
1126 // having to download the entire memory pool.
1127 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::NOTFOUND, vNotFound));
1131 uint32_t GetFetchFlags(CNode* pfrom) {
1132 uint32_t nFetchFlags = 0;
1133 if ((pfrom->GetLocalServices() & NODE_WITNESS) && State(pfrom->GetId())->fHaveWitness) {
1134 nFetchFlags |= MSG_WITNESS_FLAG;
1136 return nFetchFlags;
1139 inline void static SendBlockTransactions(const CBlock& block, const BlockTransactionsRequest& req, CNode* pfrom, CConnman* connman) {
1140 BlockTransactions resp(req);
1141 for (size_t i = 0; i < req.indexes.size(); i++) {
1142 if (req.indexes[i] >= block.vtx.size()) {
1143 LOCK(cs_main);
1144 Misbehaving(pfrom->GetId(), 100);
1145 LogPrintf("Peer %d sent us a getblocktxn with out-of-bounds tx indices", pfrom->GetId());
1146 return;
1148 resp.txn[i] = block.vtx[req.indexes[i]];
1150 LOCK(cs_main);
1151 const CNetMsgMaker msgMaker(pfrom->GetSendVersion());
1152 int nSendFlags = State(pfrom->GetId())->fWantsCmpctWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
1153 connman->PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::BLOCKTXN, resp));
1156 bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, int64_t nTimeReceived, const CChainParams& chainparams, CConnman* connman, const std::atomic<bool>& interruptMsgProc)
1158 LogPrint(BCLog::NET, "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand), vRecv.size(), pfrom->GetId());
1159 if (gArgs.IsArgSet("-dropmessagestest") && GetRand(gArgs.GetArg("-dropmessagestest", 0)) == 0)
1161 LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n");
1162 return true;
1166 if (!(pfrom->GetLocalServices() & NODE_BLOOM) &&
1167 (strCommand == NetMsgType::FILTERLOAD ||
1168 strCommand == NetMsgType::FILTERADD))
1170 if (pfrom->nVersion >= NO_BLOOM_VERSION) {
1171 LOCK(cs_main);
1172 Misbehaving(pfrom->GetId(), 100);
1173 return false;
1174 } else {
1175 pfrom->fDisconnect = true;
1176 return false;
1180 if (strCommand == NetMsgType::REJECT)
1182 if (LogAcceptCategory(BCLog::NET)) {
1183 try {
1184 std::string strMsg; unsigned char ccode; std::string strReason;
1185 vRecv >> LIMITED_STRING(strMsg, CMessageHeader::COMMAND_SIZE) >> ccode >> LIMITED_STRING(strReason, MAX_REJECT_MESSAGE_LENGTH);
1187 std::ostringstream ss;
1188 ss << strMsg << " code " << itostr(ccode) << ": " << strReason;
1190 if (strMsg == NetMsgType::BLOCK || strMsg == NetMsgType::TX)
1192 uint256 hash;
1193 vRecv >> hash;
1194 ss << ": hash " << hash.ToString();
1196 LogPrint(BCLog::NET, "Reject %s\n", SanitizeString(ss.str()));
1197 } catch (const std::ios_base::failure&) {
1198 // Avoid feedback loops by preventing reject messages from triggering a new reject message.
1199 LogPrint(BCLog::NET, "Unparseable reject message received\n");
1204 else if (strCommand == NetMsgType::VERSION)
1206 // Each connection can only send one version message
1207 if (pfrom->nVersion != 0)
1209 connman->PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_DUPLICATE, std::string("Duplicate version message")));
1210 LOCK(cs_main);
1211 Misbehaving(pfrom->GetId(), 1);
1212 return false;
1215 int64_t nTime;
1216 CAddress addrMe;
1217 CAddress addrFrom;
1218 uint64_t nNonce = 1;
1219 uint64_t nServiceInt;
1220 ServiceFlags nServices;
1221 int nVersion;
1222 int nSendVersion;
1223 std::string strSubVer;
1224 std::string cleanSubVer;
1225 int nStartingHeight = -1;
1226 bool fRelay = true;
1228 vRecv >> nVersion >> nServiceInt >> nTime >> addrMe;
1229 nSendVersion = std::min(nVersion, PROTOCOL_VERSION);
1230 nServices = ServiceFlags(nServiceInt);
1231 if (!pfrom->fInbound)
1233 connman->SetServices(pfrom->addr, nServices);
1235 if (pfrom->nServicesExpected & ~nServices)
1237 LogPrint(BCLog::NET, "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom->GetId(), nServices, pfrom->nServicesExpected);
1238 connman->PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_NONSTANDARD,
1239 strprintf("Expected to offer services %08x", pfrom->nServicesExpected)));
1240 pfrom->fDisconnect = true;
1241 return false;
1244 if (nServices & ((1 << 7) | (1 << 5))) {
1245 if (GetTime() < 1533096000) {
1246 // Immediately disconnect peers that use service bits 6 or 8 until August 1st, 2018
1247 // These bits have been used as a flag to indicate that a node is running incompatible
1248 // consensus rules instead of changing the network magic, so we're stuck disconnecting
1249 // based on these service bits, at least for a while.
1250 pfrom->fDisconnect = true;
1251 return false;
1255 if (nVersion < MIN_PEER_PROTO_VERSION)
1257 // disconnect from peers older than this proto version
1258 LogPrintf("peer=%d using obsolete version %i; disconnecting\n", pfrom->GetId(), nVersion);
1259 connman->PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_OBSOLETE,
1260 strprintf("Version must be %d or greater", MIN_PEER_PROTO_VERSION)));
1261 pfrom->fDisconnect = true;
1262 return false;
1265 if (nVersion == 10300)
1266 nVersion = 300;
1267 if (!vRecv.empty())
1268 vRecv >> addrFrom >> nNonce;
1269 if (!vRecv.empty()) {
1270 vRecv >> LIMITED_STRING(strSubVer, MAX_SUBVERSION_LENGTH);
1271 cleanSubVer = SanitizeString(strSubVer);
1273 if (!vRecv.empty()) {
1274 vRecv >> nStartingHeight;
1276 if (!vRecv.empty())
1277 vRecv >> fRelay;
1278 // Disconnect if we connected to ourself
1279 if (pfrom->fInbound && !connman->CheckIncomingNonce(nNonce))
1281 LogPrintf("connected to self at %s, disconnecting\n", pfrom->addr.ToString());
1282 pfrom->fDisconnect = true;
1283 return true;
1286 if (pfrom->fInbound && addrMe.IsRoutable())
1288 SeenLocal(addrMe);
1291 // Be shy and don't send version until we hear
1292 if (pfrom->fInbound)
1293 PushNodeVersion(pfrom, connman, GetAdjustedTime());
1295 connman->PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERACK));
1297 pfrom->nServices = nServices;
1298 pfrom->SetAddrLocal(addrMe);
1300 LOCK(pfrom->cs_SubVer);
1301 pfrom->strSubVer = strSubVer;
1302 pfrom->cleanSubVer = cleanSubVer;
1304 pfrom->nStartingHeight = nStartingHeight;
1305 pfrom->fClient = !(nServices & NODE_NETWORK);
1307 LOCK(pfrom->cs_filter);
1308 pfrom->fRelayTxes = fRelay; // set to true after we get the first filter* message
1311 // Change version
1312 pfrom->SetSendVersion(nSendVersion);
1313 pfrom->nVersion = nVersion;
1315 if((nServices & NODE_WITNESS))
1317 LOCK(cs_main);
1318 State(pfrom->GetId())->fHaveWitness = true;
1321 // Potentially mark this peer as a preferred download peer.
1323 LOCK(cs_main);
1324 UpdatePreferredDownload(pfrom, State(pfrom->GetId()));
1327 if (!pfrom->fInbound)
1329 // Advertise our address
1330 if (fListen && !IsInitialBlockDownload())
1332 CAddress addr = GetLocalAddress(&pfrom->addr, pfrom->GetLocalServices());
1333 FastRandomContext insecure_rand;
1334 if (addr.IsRoutable())
1336 LogPrint(BCLog::NET, "ProcessMessages: advertising address %s\n", addr.ToString());
1337 pfrom->PushAddress(addr, insecure_rand);
1338 } else if (IsPeerAddrLocalGood(pfrom)) {
1339 addr.SetIP(addrMe);
1340 LogPrint(BCLog::NET, "ProcessMessages: advertising address %s\n", addr.ToString());
1341 pfrom->PushAddress(addr, insecure_rand);
1345 // Get recent addresses
1346 if (pfrom->fOneShot || pfrom->nVersion >= CADDR_TIME_VERSION || connman->GetAddressCount() < 1000)
1348 connman->PushMessage(pfrom, CNetMsgMaker(nSendVersion).Make(NetMsgType::GETADDR));
1349 pfrom->fGetAddr = true;
1351 connman->MarkAddressGood(pfrom->addr);
1354 std::string remoteAddr;
1355 if (fLogIPs)
1356 remoteAddr = ", peeraddr=" + pfrom->addr.ToString();
1358 LogPrintf("receive version message: %s: version %d, blocks=%d, us=%s, peer=%d%s\n",
1359 cleanSubVer, pfrom->nVersion,
1360 pfrom->nStartingHeight, addrMe.ToString(), pfrom->GetId(),
1361 remoteAddr);
1363 int64_t nTimeOffset = nTime - GetTime();
1364 pfrom->nTimeOffset = nTimeOffset;
1365 AddTimeData(pfrom->addr, nTimeOffset);
1367 // If the peer is old enough to have the old alert system, send it the final alert.
1368 if (pfrom->nVersion <= 70012) {
1369 CDataStream finalAlert(ParseHex("60010000000000000000000000ffffff7f00000000ffffff7ffeffff7f01ffffff7f00000000ffffff7f00ffffff7f002f555247454e543a20416c657274206b657920636f6d70726f6d697365642c2075706772616465207265717569726564004630440220653febd6410f470f6bae11cad19c48413becb1ac2c17f908fd0fd53bdc3abd5202206d0e9c96fe88d4a0f01ed9dedae2b6f9e00da94cad0fecaae66ecf689bf71b50"), SER_NETWORK, PROTOCOL_VERSION);
1370 connman->PushMessage(pfrom, CNetMsgMaker(nSendVersion).Make("alert", finalAlert));
1373 // Feeler connections exist only to verify if address is online.
1374 if (pfrom->fFeeler) {
1375 assert(pfrom->fInbound == false);
1376 pfrom->fDisconnect = true;
1378 return true;
1382 else if (pfrom->nVersion == 0)
1384 // Must have a version message before anything else
1385 LOCK(cs_main);
1386 Misbehaving(pfrom->GetId(), 1);
1387 return false;
1390 // At this point, the outgoing message serialization version can't change.
1391 const CNetMsgMaker msgMaker(pfrom->GetSendVersion());
1393 if (strCommand == NetMsgType::VERACK)
1395 pfrom->SetRecvVersion(std::min(pfrom->nVersion.load(), PROTOCOL_VERSION));
1397 if (!pfrom->fInbound) {
1398 // Mark this node as currently connected, so we update its timestamp later.
1399 LOCK(cs_main);
1400 State(pfrom->GetId())->fCurrentlyConnected = true;
1403 if (pfrom->nVersion >= SENDHEADERS_VERSION) {
1404 // Tell our peer we prefer to receive headers rather than inv's
1405 // We send this to non-NODE NETWORK peers as well, because even
1406 // non-NODE NETWORK peers can announce blocks (such as pruning
1407 // nodes)
1408 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::SENDHEADERS));
1410 if (pfrom->nVersion >= SHORT_IDS_BLOCKS_VERSION) {
1411 // Tell our peer we are willing to provide version 1 or 2 cmpctblocks
1412 // However, we do not request new block announcements using
1413 // cmpctblock messages.
1414 // We send this to non-NODE NETWORK peers as well, because
1415 // they may wish to request compact blocks from us
1416 bool fAnnounceUsingCMPCTBLOCK = false;
1417 uint64_t nCMPCTBLOCKVersion = 2;
1418 if (pfrom->GetLocalServices() & NODE_WITNESS)
1419 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
1420 nCMPCTBLOCKVersion = 1;
1421 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
1423 pfrom->fSuccessfullyConnected = true;
1426 else if (!pfrom->fSuccessfullyConnected)
1428 // Must have a verack message before anything else
1429 LOCK(cs_main);
1430 Misbehaving(pfrom->GetId(), 1);
1431 return false;
1434 else if (strCommand == NetMsgType::ADDR)
1436 std::vector<CAddress> vAddr;
1437 vRecv >> vAddr;
1439 // Don't want addr from older versions unless seeding
1440 if (pfrom->nVersion < CADDR_TIME_VERSION && connman->GetAddressCount() > 1000)
1441 return true;
1442 if (vAddr.size() > 1000)
1444 LOCK(cs_main);
1445 Misbehaving(pfrom->GetId(), 20);
1446 return error("message addr size() = %u", vAddr.size());
1449 // Store the new addresses
1450 std::vector<CAddress> vAddrOk;
1451 int64_t nNow = GetAdjustedTime();
1452 int64_t nSince = nNow - 10 * 60;
1453 for (CAddress& addr : vAddr)
1455 if (interruptMsgProc)
1456 return true;
1458 if ((addr.nServices & REQUIRED_SERVICES) != REQUIRED_SERVICES)
1459 continue;
1461 if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
1462 addr.nTime = nNow - 5 * 24 * 60 * 60;
1463 pfrom->AddAddressKnown(addr);
1464 bool fReachable = IsReachable(addr);
1465 if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
1467 // Relay to a limited number of other nodes
1468 RelayAddress(addr, fReachable, connman);
1470 // Do not store addresses outside our network
1471 if (fReachable)
1472 vAddrOk.push_back(addr);
1474 connman->AddNewAddresses(vAddrOk, pfrom->addr, 2 * 60 * 60);
1475 if (vAddr.size() < 1000)
1476 pfrom->fGetAddr = false;
1477 if (pfrom->fOneShot)
1478 pfrom->fDisconnect = true;
1481 else if (strCommand == NetMsgType::SENDHEADERS)
1483 LOCK(cs_main);
1484 State(pfrom->GetId())->fPreferHeaders = true;
1487 else if (strCommand == NetMsgType::SENDCMPCT)
1489 bool fAnnounceUsingCMPCTBLOCK = false;
1490 uint64_t nCMPCTBLOCKVersion = 0;
1491 vRecv >> fAnnounceUsingCMPCTBLOCK >> nCMPCTBLOCKVersion;
1492 if (nCMPCTBLOCKVersion == 1 || ((pfrom->GetLocalServices() & NODE_WITNESS) && nCMPCTBLOCKVersion == 2)) {
1493 LOCK(cs_main);
1494 // fProvidesHeaderAndIDs is used to "lock in" version of compact blocks we send (fWantsCmpctWitness)
1495 if (!State(pfrom->GetId())->fProvidesHeaderAndIDs) {
1496 State(pfrom->GetId())->fProvidesHeaderAndIDs = true;
1497 State(pfrom->GetId())->fWantsCmpctWitness = nCMPCTBLOCKVersion == 2;
1499 if (State(pfrom->GetId())->fWantsCmpctWitness == (nCMPCTBLOCKVersion == 2)) // ignore later version announces
1500 State(pfrom->GetId())->fPreferHeaderAndIDs = fAnnounceUsingCMPCTBLOCK;
1501 if (!State(pfrom->GetId())->fSupportsDesiredCmpctVersion) {
1502 if (pfrom->GetLocalServices() & NODE_WITNESS)
1503 State(pfrom->GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 2);
1504 else
1505 State(pfrom->GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 1);
1511 else if (strCommand == NetMsgType::INV)
1513 std::vector<CInv> vInv;
1514 vRecv >> vInv;
1515 if (vInv.size() > MAX_INV_SZ)
1517 LOCK(cs_main);
1518 Misbehaving(pfrom->GetId(), 20);
1519 return error("message inv size() = %u", vInv.size());
1522 bool fBlocksOnly = !fRelayTxes;
1524 // Allow whitelisted peers to send data other than blocks in blocks only mode if whitelistrelay is true
1525 if (pfrom->fWhitelisted && gArgs.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY))
1526 fBlocksOnly = false;
1528 LOCK(cs_main);
1530 uint32_t nFetchFlags = GetFetchFlags(pfrom);
1532 for (CInv &inv : vInv)
1534 if (interruptMsgProc)
1535 return true;
1537 bool fAlreadyHave = AlreadyHave(inv);
1538 LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom->GetId());
1540 if (inv.type == MSG_TX) {
1541 inv.type |= nFetchFlags;
1544 if (inv.type == MSG_BLOCK) {
1545 UpdateBlockAvailability(pfrom->GetId(), inv.hash);
1546 if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count(inv.hash)) {
1547 // We used to request the full block here, but since headers-announcements are now the
1548 // primary method of announcement on the network, and since, in the case that a node
1549 // fell back to inv we probably have a reorg which we should get the headers for first,
1550 // we now only provide a getheaders response here. When we receive the headers, we will
1551 // then ask for the blocks we need.
1552 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), inv.hash));
1553 LogPrint(BCLog::NET, "getheaders (%d) %s to peer=%d\n", pindexBestHeader->nHeight, inv.hash.ToString(), pfrom->GetId());
1556 else
1558 pfrom->AddInventoryKnown(inv);
1559 if (fBlocksOnly) {
1560 LogPrint(BCLog::NET, "transaction (%s) inv sent in violation of protocol peer=%d\n", inv.hash.ToString(), pfrom->GetId());
1561 } else if (!fAlreadyHave && !fImporting && !fReindex && !IsInitialBlockDownload()) {
1562 pfrom->AskFor(inv);
1566 // Track requests for our stuff
1567 GetMainSignals().Inventory(inv.hash);
1572 else if (strCommand == NetMsgType::GETDATA)
1574 std::vector<CInv> vInv;
1575 vRecv >> vInv;
1576 if (vInv.size() > MAX_INV_SZ)
1578 LOCK(cs_main);
1579 Misbehaving(pfrom->GetId(), 20);
1580 return error("message getdata size() = %u", vInv.size());
1583 LogPrint(BCLog::NET, "received getdata (%u invsz) peer=%d\n", vInv.size(), pfrom->GetId());
1585 if (vInv.size() > 0) {
1586 LogPrint(BCLog::NET, "received getdata for: %s peer=%d\n", vInv[0].ToString(), pfrom->GetId());
1589 pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end());
1590 ProcessGetData(pfrom, chainparams.GetConsensus(), connman, interruptMsgProc);
1594 else if (strCommand == NetMsgType::GETBLOCKS)
1596 CBlockLocator locator;
1597 uint256 hashStop;
1598 vRecv >> locator >> hashStop;
1600 // We might have announced the currently-being-connected tip using a
1601 // compact block, which resulted in the peer sending a getblocks
1602 // request, which we would otherwise respond to without the new block.
1603 // To avoid this situation we simply verify that we are on our best
1604 // known chain now. This is super overkill, but we handle it better
1605 // for getheaders requests, and there are no known nodes which support
1606 // compact blocks but still use getblocks to request blocks.
1608 std::shared_ptr<const CBlock> a_recent_block;
1610 LOCK(cs_most_recent_block);
1611 a_recent_block = most_recent_block;
1613 CValidationState dummy;
1614 ActivateBestChain(dummy, Params(), a_recent_block);
1617 LOCK(cs_main);
1619 // Find the last block the caller has in the main chain
1620 const CBlockIndex* pindex = FindForkInGlobalIndex(chainActive, locator);
1622 // Send the rest of the chain
1623 if (pindex)
1624 pindex = chainActive.Next(pindex);
1625 int nLimit = 500;
1626 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());
1627 for (; pindex; pindex = chainActive.Next(pindex))
1629 if (pindex->GetBlockHash() == hashStop)
1631 LogPrint(BCLog::NET, " getblocks stopping at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
1632 break;
1634 // If pruning, don't inv blocks unless we have on disk and are likely to still have
1635 // for some reasonable time window (1 hour) that block relay might require.
1636 const int nPrunedBlocksLikelyToHave = MIN_BLOCKS_TO_KEEP - 3600 / chainparams.GetConsensus().nPowTargetSpacing;
1637 if (fPruneMode && (!(pindex->nStatus & BLOCK_HAVE_DATA) || pindex->nHeight <= chainActive.Tip()->nHeight - nPrunedBlocksLikelyToHave))
1639 LogPrint(BCLog::NET, " getblocks stopping, pruned or too old block at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
1640 break;
1642 pfrom->PushInventory(CInv(MSG_BLOCK, pindex->GetBlockHash()));
1643 if (--nLimit <= 0)
1645 // When this block is requested, we'll send an inv that'll
1646 // trigger the peer to getblocks the next batch of inventory.
1647 LogPrint(BCLog::NET, " getblocks stopping at limit %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
1648 pfrom->hashContinue = pindex->GetBlockHash();
1649 break;
1655 else if (strCommand == NetMsgType::GETBLOCKTXN)
1657 BlockTransactionsRequest req;
1658 vRecv >> req;
1660 std::shared_ptr<const CBlock> recent_block;
1662 LOCK(cs_most_recent_block);
1663 if (most_recent_block_hash == req.blockhash)
1664 recent_block = most_recent_block;
1665 // Unlock cs_most_recent_block to avoid cs_main lock inversion
1667 if (recent_block) {
1668 SendBlockTransactions(*recent_block, req, pfrom, connman);
1669 return true;
1672 LOCK(cs_main);
1674 BlockMap::iterator it = mapBlockIndex.find(req.blockhash);
1675 if (it == mapBlockIndex.end() || !(it->second->nStatus & BLOCK_HAVE_DATA)) {
1676 LogPrintf("Peer %d sent us a getblocktxn for a block we don't have", pfrom->GetId());
1677 return true;
1680 if (it->second->nHeight < chainActive.Height() - MAX_BLOCKTXN_DEPTH) {
1681 // If an older block is requested (should never happen in practice,
1682 // but can happen in tests) send a block response instead of a
1683 // blocktxn response. Sending a full block response instead of a
1684 // small blocktxn response is preferable in the case where a peer
1685 // might maliciously send lots of getblocktxn requests to trigger
1686 // expensive disk reads, because it will require the peer to
1687 // actually receive all the data read from disk over the network.
1688 LogPrint(BCLog::NET, "Peer %d sent us a getblocktxn for a block > %i deep", pfrom->GetId(), MAX_BLOCKTXN_DEPTH);
1689 CInv inv;
1690 inv.type = State(pfrom->GetId())->fWantsCmpctWitness ? MSG_WITNESS_BLOCK : MSG_BLOCK;
1691 inv.hash = req.blockhash;
1692 pfrom->vRecvGetData.push_back(inv);
1693 ProcessGetData(pfrom, chainparams.GetConsensus(), connman, interruptMsgProc);
1694 return true;
1697 CBlock block;
1698 bool ret = ReadBlockFromDisk(block, it->second, chainparams.GetConsensus());
1699 assert(ret);
1701 SendBlockTransactions(block, req, pfrom, connman);
1705 else if (strCommand == NetMsgType::GETHEADERS)
1707 CBlockLocator locator;
1708 uint256 hashStop;
1709 vRecv >> locator >> hashStop;
1711 LOCK(cs_main);
1712 if (IsInitialBlockDownload() && !pfrom->fWhitelisted) {
1713 LogPrint(BCLog::NET, "Ignoring getheaders from peer=%d because node is in initial block download\n", pfrom->GetId());
1714 return true;
1717 CNodeState *nodestate = State(pfrom->GetId());
1718 const CBlockIndex* pindex = nullptr;
1719 if (locator.IsNull())
1721 // If locator is null, return the hashStop block
1722 BlockMap::iterator mi = mapBlockIndex.find(hashStop);
1723 if (mi == mapBlockIndex.end())
1724 return true;
1725 pindex = (*mi).second;
1727 else
1729 // Find the last block the caller has in the main chain
1730 pindex = FindForkInGlobalIndex(chainActive, locator);
1731 if (pindex)
1732 pindex = chainActive.Next(pindex);
1735 // we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
1736 std::vector<CBlock> vHeaders;
1737 int nLimit = MAX_HEADERS_RESULTS;
1738 LogPrint(BCLog::NET, "getheaders %d to %s from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), pfrom->GetId());
1739 for (; pindex; pindex = chainActive.Next(pindex))
1741 vHeaders.push_back(pindex->GetBlockHeader());
1742 if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
1743 break;
1745 // pindex can be nullptr either if we sent chainActive.Tip() OR
1746 // if our peer has chainActive.Tip() (and thus we are sending an empty
1747 // headers message). In both cases it's safe to update
1748 // pindexBestHeaderSent to be our tip.
1750 // It is important that we simply reset the BestHeaderSent value here,
1751 // and not max(BestHeaderSent, newHeaderSent). We might have announced
1752 // the currently-being-connected tip using a compact block, which
1753 // resulted in the peer sending a headers request, which we respond to
1754 // without the new block. By resetting the BestHeaderSent, we ensure we
1755 // will re-announce the new block via headers (or compact blocks again)
1756 // in the SendMessages logic.
1757 nodestate->pindexBestHeaderSent = pindex ? pindex : chainActive.Tip();
1758 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
1762 else if (strCommand == NetMsgType::TX)
1764 // Stop processing the transaction early if
1765 // We are in blocks only mode and peer is either not whitelisted or whitelistrelay is off
1766 if (!fRelayTxes && (!pfrom->fWhitelisted || !gArgs.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY)))
1768 LogPrint(BCLog::NET, "transaction sent in violation of protocol peer=%d\n", pfrom->GetId());
1769 return true;
1772 std::deque<COutPoint> vWorkQueue;
1773 std::vector<uint256> vEraseQueue;
1774 CTransactionRef ptx;
1775 vRecv >> ptx;
1776 const CTransaction& tx = *ptx;
1778 CInv inv(MSG_TX, tx.GetHash());
1779 pfrom->AddInventoryKnown(inv);
1781 LOCK(cs_main);
1783 bool fMissingInputs = false;
1784 CValidationState state;
1786 pfrom->setAskFor.erase(inv.hash);
1787 mapAlreadyAskedFor.erase(inv.hash);
1789 std::list<CTransactionRef> lRemovedTxn;
1791 if (!AlreadyHave(inv) &&
1792 AcceptToMemoryPool(mempool, state, ptx, &fMissingInputs, &lRemovedTxn, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
1793 mempool.check(pcoinsTip);
1794 RelayTransaction(tx, connman);
1795 for (unsigned int i = 0; i < tx.vout.size(); i++) {
1796 vWorkQueue.emplace_back(inv.hash, i);
1799 pfrom->nLastTXTime = GetTime();
1801 LogPrint(BCLog::MEMPOOL, "AcceptToMemoryPool: peer=%d: accepted %s (poolsz %u txn, %u kB)\n",
1802 pfrom->GetId(),
1803 tx.GetHash().ToString(),
1804 mempool.size(), mempool.DynamicMemoryUsage() / 1000);
1806 // Recursively process any orphan transactions that depended on this one
1807 std::set<NodeId> setMisbehaving;
1808 while (!vWorkQueue.empty()) {
1809 auto itByPrev = mapOrphanTransactionsByPrev.find(vWorkQueue.front());
1810 vWorkQueue.pop_front();
1811 if (itByPrev == mapOrphanTransactionsByPrev.end())
1812 continue;
1813 for (auto mi = itByPrev->second.begin();
1814 mi != itByPrev->second.end();
1815 ++mi)
1817 const CTransactionRef& porphanTx = (*mi)->second.tx;
1818 const CTransaction& orphanTx = *porphanTx;
1819 const uint256& orphanHash = orphanTx.GetHash();
1820 NodeId fromPeer = (*mi)->second.fromPeer;
1821 bool fMissingInputs2 = false;
1822 // Use a dummy CValidationState so someone can't setup nodes to counter-DoS based on orphan
1823 // resolution (that is, feeding people an invalid transaction based on LegitTxX in order to get
1824 // anyone relaying LegitTxX banned)
1825 CValidationState stateDummy;
1828 if (setMisbehaving.count(fromPeer))
1829 continue;
1830 if (AcceptToMemoryPool(mempool, stateDummy, porphanTx, &fMissingInputs2, &lRemovedTxn, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
1831 LogPrint(BCLog::MEMPOOL, " accepted orphan tx %s\n", orphanHash.ToString());
1832 RelayTransaction(orphanTx, connman);
1833 for (unsigned int i = 0; i < orphanTx.vout.size(); i++) {
1834 vWorkQueue.emplace_back(orphanHash, i);
1836 vEraseQueue.push_back(orphanHash);
1838 else if (!fMissingInputs2)
1840 int nDos = 0;
1841 if (stateDummy.IsInvalid(nDos) && nDos > 0)
1843 // Punish peer that gave us an invalid orphan tx
1844 Misbehaving(fromPeer, nDos);
1845 setMisbehaving.insert(fromPeer);
1846 LogPrint(BCLog::MEMPOOL, " invalid orphan tx %s\n", orphanHash.ToString());
1848 // Has inputs but not accepted to mempool
1849 // Probably non-standard or insufficient fee
1850 LogPrint(BCLog::MEMPOOL, " removed orphan tx %s\n", orphanHash.ToString());
1851 vEraseQueue.push_back(orphanHash);
1852 if (!orphanTx.HasWitness() && !stateDummy.CorruptionPossible()) {
1853 // Do not use rejection cache for witness transactions or
1854 // witness-stripped transactions, as they can have been malleated.
1855 // See https://github.com/bitcoin/bitcoin/issues/8279 for details.
1856 assert(recentRejects);
1857 recentRejects->insert(orphanHash);
1860 mempool.check(pcoinsTip);
1864 for (uint256 hash : vEraseQueue)
1865 EraseOrphanTx(hash);
1867 else if (fMissingInputs)
1869 bool fRejectedParents = false; // It may be the case that the orphans parents have all been rejected
1870 for (const CTxIn& txin : tx.vin) {
1871 if (recentRejects->contains(txin.prevout.hash)) {
1872 fRejectedParents = true;
1873 break;
1876 if (!fRejectedParents) {
1877 uint32_t nFetchFlags = GetFetchFlags(pfrom);
1878 for (const CTxIn& txin : tx.vin) {
1879 CInv _inv(MSG_TX | nFetchFlags, txin.prevout.hash);
1880 pfrom->AddInventoryKnown(_inv);
1881 if (!AlreadyHave(_inv)) pfrom->AskFor(_inv);
1883 AddOrphanTx(ptx, pfrom->GetId());
1885 // DoS prevention: do not allow mapOrphanTransactions to grow unbounded
1886 unsigned int nMaxOrphanTx = (unsigned int)std::max((int64_t)0, gArgs.GetArg("-maxorphantx", DEFAULT_MAX_ORPHAN_TRANSACTIONS));
1887 unsigned int nEvicted = LimitOrphanTxSize(nMaxOrphanTx);
1888 if (nEvicted > 0) {
1889 LogPrint(BCLog::MEMPOOL, "mapOrphan overflow, removed %u tx\n", nEvicted);
1891 } else {
1892 LogPrint(BCLog::MEMPOOL, "not keeping orphan with rejected parents %s\n",tx.GetHash().ToString());
1893 // We will continue to reject this tx since it has rejected
1894 // parents so avoid re-requesting it from other peers.
1895 recentRejects->insert(tx.GetHash());
1897 } else {
1898 if (!tx.HasWitness() && !state.CorruptionPossible()) {
1899 // Do not use rejection cache for witness transactions or
1900 // witness-stripped transactions, as they can have been malleated.
1901 // See https://github.com/bitcoin/bitcoin/issues/8279 for details.
1902 assert(recentRejects);
1903 recentRejects->insert(tx.GetHash());
1904 if (RecursiveDynamicUsage(*ptx) < 100000) {
1905 AddToCompactExtraTransactions(ptx);
1907 } else if (tx.HasWitness() && RecursiveDynamicUsage(*ptx) < 100000) {
1908 AddToCompactExtraTransactions(ptx);
1911 if (pfrom->fWhitelisted && gArgs.GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY)) {
1912 // Always relay transactions received from whitelisted peers, even
1913 // if they were already in the mempool or rejected from it due
1914 // to policy, allowing the node to function as a gateway for
1915 // nodes hidden behind it.
1917 // Never relay transactions that we would assign a non-zero DoS
1918 // score for, as we expect peers to do the same with us in that
1919 // case.
1920 int nDoS = 0;
1921 if (!state.IsInvalid(nDoS) || nDoS == 0) {
1922 LogPrintf("Force relaying tx %s from whitelisted peer=%d\n", tx.GetHash().ToString(), pfrom->GetId());
1923 RelayTransaction(tx, connman);
1924 } else {
1925 LogPrintf("Not relaying invalid transaction %s from whitelisted peer=%d (%s)\n", tx.GetHash().ToString(), pfrom->GetId(), FormatStateMessage(state));
1930 for (const CTransactionRef& removedTx : lRemovedTxn)
1931 AddToCompactExtraTransactions(removedTx);
1933 int nDoS = 0;
1934 if (state.IsInvalid(nDoS))
1936 LogPrint(BCLog::MEMPOOLREJ, "%s from peer=%d was not accepted: %s\n", tx.GetHash().ToString(),
1937 pfrom->GetId(),
1938 FormatStateMessage(state));
1939 if (state.GetRejectCode() > 0 && state.GetRejectCode() < REJECT_INTERNAL) // Never send AcceptToMemoryPool's internal codes over P2P
1940 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::REJECT, strCommand, (unsigned char)state.GetRejectCode(),
1941 state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), inv.hash));
1942 if (nDoS > 0) {
1943 Misbehaving(pfrom->GetId(), nDoS);
1949 else if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
1951 CBlockHeaderAndShortTxIDs cmpctblock;
1952 vRecv >> cmpctblock;
1955 LOCK(cs_main);
1957 if (mapBlockIndex.find(cmpctblock.header.hashPrevBlock) == mapBlockIndex.end()) {
1958 // Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
1959 if (!IsInitialBlockDownload())
1960 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), uint256()));
1961 return true;
1965 const CBlockIndex *pindex = nullptr;
1966 CValidationState state;
1967 if (!ProcessNewBlockHeaders({cmpctblock.header}, state, chainparams, &pindex)) {
1968 int nDoS;
1969 if (state.IsInvalid(nDoS)) {
1970 if (nDoS > 0) {
1971 LOCK(cs_main);
1972 Misbehaving(pfrom->GetId(), nDoS);
1974 LogPrintf("Peer %d sent us invalid header via cmpctblock\n", pfrom->GetId());
1975 return true;
1979 // When we succeed in decoding a block's txids from a cmpctblock
1980 // message we typically jump to the BLOCKTXN handling code, with a
1981 // dummy (empty) BLOCKTXN message, to re-use the logic there in
1982 // completing processing of the putative block (without cs_main).
1983 bool fProcessBLOCKTXN = false;
1984 CDataStream blockTxnMsg(SER_NETWORK, PROTOCOL_VERSION);
1986 // If we end up treating this as a plain headers message, call that as well
1987 // without cs_main.
1988 bool fRevertToHeaderProcessing = false;
1989 CDataStream vHeadersMsg(SER_NETWORK, PROTOCOL_VERSION);
1991 // Keep a CBlock for "optimistic" compactblock reconstructions (see
1992 // below)
1993 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
1994 bool fBlockReconstructed = false;
1997 LOCK(cs_main);
1998 // If AcceptBlockHeader returned true, it set pindex
1999 assert(pindex);
2000 UpdateBlockAvailability(pfrom->GetId(), pindex->GetBlockHash());
2002 std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator blockInFlightIt = mapBlocksInFlight.find(pindex->GetBlockHash());
2003 bool fAlreadyInFlight = blockInFlightIt != mapBlocksInFlight.end();
2005 if (pindex->nStatus & BLOCK_HAVE_DATA) // Nothing to do here
2006 return true;
2008 if (pindex->nChainWork <= chainActive.Tip()->nChainWork || // We know something better
2009 pindex->nTx != 0) { // We had this block at some point, but pruned it
2010 if (fAlreadyInFlight) {
2011 // We requested this block for some reason, but our mempool will probably be useless
2012 // so we just grab the block via normal getdata
2013 std::vector<CInv> vInv(1);
2014 vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom), cmpctblock.header.GetHash());
2015 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
2017 return true;
2020 // If we're not close to tip yet, give up and let parallel block fetch work its magic
2021 if (!fAlreadyInFlight && !CanDirectFetch(chainparams.GetConsensus()))
2022 return true;
2024 CNodeState *nodestate = State(pfrom->GetId());
2026 if (IsWitnessEnabled(pindex->pprev, chainparams.GetConsensus()) && !nodestate->fSupportsDesiredCmpctVersion) {
2027 // Don't bother trying to process compact blocks from v1 peers
2028 // after segwit activates.
2029 return true;
2032 // We want to be a bit conservative just to be extra careful about DoS
2033 // possibilities in compact block processing...
2034 if (pindex->nHeight <= chainActive.Height() + 2) {
2035 if ((!fAlreadyInFlight && nodestate->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) ||
2036 (fAlreadyInFlight && blockInFlightIt->second.first == pfrom->GetId())) {
2037 std::list<QueuedBlock>::iterator* queuedBlockIt = nullptr;
2038 if (!MarkBlockAsInFlight(pfrom->GetId(), pindex->GetBlockHash(), pindex, &queuedBlockIt)) {
2039 if (!(*queuedBlockIt)->partialBlock)
2040 (*queuedBlockIt)->partialBlock.reset(new PartiallyDownloadedBlock(&mempool));
2041 else {
2042 // The block was already in flight using compact blocks from the same peer
2043 LogPrint(BCLog::NET, "Peer sent us compact block we were already syncing!\n");
2044 return true;
2048 PartiallyDownloadedBlock& partialBlock = *(*queuedBlockIt)->partialBlock;
2049 ReadStatus status = partialBlock.InitData(cmpctblock, vExtraTxnForCompact);
2050 if (status == READ_STATUS_INVALID) {
2051 MarkBlockAsReceived(pindex->GetBlockHash()); // Reset in-flight state in case of whitelist
2052 Misbehaving(pfrom->GetId(), 100);
2053 LogPrintf("Peer %d sent us invalid compact block\n", pfrom->GetId());
2054 return true;
2055 } else if (status == READ_STATUS_FAILED) {
2056 // Duplicate txindexes, the block is now in-flight, so just request it
2057 std::vector<CInv> vInv(1);
2058 vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom), cmpctblock.header.GetHash());
2059 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
2060 return true;
2063 BlockTransactionsRequest req;
2064 for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
2065 if (!partialBlock.IsTxAvailable(i))
2066 req.indexes.push_back(i);
2068 if (req.indexes.empty()) {
2069 // Dirty hack to jump to BLOCKTXN code (TODO: move message handling into their own functions)
2070 BlockTransactions txn;
2071 txn.blockhash = cmpctblock.header.GetHash();
2072 blockTxnMsg << txn;
2073 fProcessBLOCKTXN = true;
2074 } else {
2075 req.blockhash = pindex->GetBlockHash();
2076 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETBLOCKTXN, req));
2078 } else {
2079 // This block is either already in flight from a different
2080 // peer, or this peer has too many blocks outstanding to
2081 // download from.
2082 // Optimistically try to reconstruct anyway since we might be
2083 // able to without any round trips.
2084 PartiallyDownloadedBlock tempBlock(&mempool);
2085 ReadStatus status = tempBlock.InitData(cmpctblock, vExtraTxnForCompact);
2086 if (status != READ_STATUS_OK) {
2087 // TODO: don't ignore failures
2088 return true;
2090 std::vector<CTransactionRef> dummy;
2091 status = tempBlock.FillBlock(*pblock, dummy);
2092 if (status == READ_STATUS_OK) {
2093 fBlockReconstructed = true;
2096 } else {
2097 if (fAlreadyInFlight) {
2098 // We requested this block, but its far into the future, so our
2099 // mempool will probably be useless - request the block normally
2100 std::vector<CInv> vInv(1);
2101 vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom), cmpctblock.header.GetHash());
2102 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
2103 return true;
2104 } else {
2105 // If this was an announce-cmpctblock, we want the same treatment as a header message
2106 // Dirty hack to process as if it were just a headers message (TODO: move message handling into their own functions)
2107 std::vector<CBlock> headers;
2108 headers.push_back(cmpctblock.header);
2109 vHeadersMsg << headers;
2110 fRevertToHeaderProcessing = true;
2113 } // cs_main
2115 if (fProcessBLOCKTXN)
2116 return ProcessMessage(pfrom, NetMsgType::BLOCKTXN, blockTxnMsg, nTimeReceived, chainparams, connman, interruptMsgProc);
2118 if (fRevertToHeaderProcessing)
2119 return ProcessMessage(pfrom, NetMsgType::HEADERS, vHeadersMsg, nTimeReceived, chainparams, connman, interruptMsgProc);
2121 if (fBlockReconstructed) {
2122 // If we got here, we were able to optimistically reconstruct a
2123 // block that is in flight from some other peer.
2125 LOCK(cs_main);
2126 mapBlockSource.emplace(pblock->GetHash(), std::make_pair(pfrom->GetId(), false));
2128 bool fNewBlock = false;
2129 ProcessNewBlock(chainparams, pblock, true, &fNewBlock);
2130 if (fNewBlock) {
2131 pfrom->nLastBlockTime = GetTime();
2132 } else {
2133 LOCK(cs_main);
2134 mapBlockSource.erase(pblock->GetHash());
2136 LOCK(cs_main); // hold cs_main for CBlockIndex::IsValid()
2137 if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS)) {
2138 // Clear download state for this block, which is in
2139 // process from some other peer. We do this after calling
2140 // ProcessNewBlock so that a malleated cmpctblock announcement
2141 // can't be used to interfere with block relay.
2142 MarkBlockAsReceived(pblock->GetHash());
2148 else if (strCommand == NetMsgType::BLOCKTXN && !fImporting && !fReindex) // Ignore blocks received while importing
2150 BlockTransactions resp;
2151 vRecv >> resp;
2153 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
2154 bool fBlockRead = false;
2156 LOCK(cs_main);
2158 std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator it = mapBlocksInFlight.find(resp.blockhash);
2159 if (it == mapBlocksInFlight.end() || !it->second.second->partialBlock ||
2160 it->second.first != pfrom->GetId()) {
2161 LogPrint(BCLog::NET, "Peer %d sent us block transactions for block we weren't expecting\n", pfrom->GetId());
2162 return true;
2165 PartiallyDownloadedBlock& partialBlock = *it->second.second->partialBlock;
2166 ReadStatus status = partialBlock.FillBlock(*pblock, resp.txn);
2167 if (status == READ_STATUS_INVALID) {
2168 MarkBlockAsReceived(resp.blockhash); // Reset in-flight state in case of whitelist
2169 Misbehaving(pfrom->GetId(), 100);
2170 LogPrintf("Peer %d sent us invalid compact block/non-matching block transactions\n", pfrom->GetId());
2171 return true;
2172 } else if (status == READ_STATUS_FAILED) {
2173 // Might have collided, fall back to getdata now :(
2174 std::vector<CInv> invs;
2175 invs.push_back(CInv(MSG_BLOCK | GetFetchFlags(pfrom), resp.blockhash));
2176 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, invs));
2177 } else {
2178 // Block is either okay, or possibly we received
2179 // READ_STATUS_CHECKBLOCK_FAILED.
2180 // Note that CheckBlock can only fail for one of a few reasons:
2181 // 1. bad-proof-of-work (impossible here, because we've already
2182 // accepted the header)
2183 // 2. merkleroot doesn't match the transactions given (already
2184 // caught in FillBlock with READ_STATUS_FAILED, so
2185 // impossible here)
2186 // 3. the block is otherwise invalid (eg invalid coinbase,
2187 // block is too big, too many legacy sigops, etc).
2188 // So if CheckBlock failed, #3 is the only possibility.
2189 // Under BIP 152, we don't DoS-ban unless proof of work is
2190 // invalid (we don't require all the stateless checks to have
2191 // been run). This is handled below, so just treat this as
2192 // though the block was successfully read, and rely on the
2193 // handling in ProcessNewBlock to ensure the block index is
2194 // updated, reject messages go out, etc.
2195 MarkBlockAsReceived(resp.blockhash); // it is now an empty pointer
2196 fBlockRead = true;
2197 // mapBlockSource is only used for sending reject messages and DoS scores,
2198 // so the race between here and cs_main in ProcessNewBlock is fine.
2199 // BIP 152 permits peers to relay compact blocks after validating
2200 // the header only; we should not punish peers if the block turns
2201 // out to be invalid.
2202 mapBlockSource.emplace(resp.blockhash, std::make_pair(pfrom->GetId(), false));
2204 } // Don't hold cs_main when we call into ProcessNewBlock
2205 if (fBlockRead) {
2206 bool fNewBlock = false;
2207 // Since we requested this block (it was in mapBlocksInFlight), force it to be processed,
2208 // even if it would not be a candidate for new tip (missing previous block, chain not long enough, etc)
2209 ProcessNewBlock(chainparams, pblock, true, &fNewBlock);
2210 if (fNewBlock) {
2211 pfrom->nLastBlockTime = GetTime();
2212 } else {
2213 LOCK(cs_main);
2214 mapBlockSource.erase(pblock->GetHash());
2220 else if (strCommand == NetMsgType::HEADERS && !fImporting && !fReindex) // Ignore headers received while importing
2222 std::vector<CBlockHeader> headers;
2224 // Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks.
2225 unsigned int nCount = ReadCompactSize(vRecv);
2226 if (nCount > MAX_HEADERS_RESULTS) {
2227 LOCK(cs_main);
2228 Misbehaving(pfrom->GetId(), 20);
2229 return error("headers message size = %u", nCount);
2231 headers.resize(nCount);
2232 for (unsigned int n = 0; n < nCount; n++) {
2233 vRecv >> headers[n];
2234 ReadCompactSize(vRecv); // ignore tx count; assume it is 0.
2237 if (nCount == 0) {
2238 // Nothing interesting. Stop asking this peers for more headers.
2239 return true;
2242 const CBlockIndex *pindexLast = nullptr;
2244 LOCK(cs_main);
2245 CNodeState *nodestate = State(pfrom->GetId());
2247 // If this looks like it could be a block announcement (nCount <
2248 // MAX_BLOCKS_TO_ANNOUNCE), use special logic for handling headers that
2249 // don't connect:
2250 // - Send a getheaders message in response to try to connect the chain.
2251 // - The peer can send up to MAX_UNCONNECTING_HEADERS in a row that
2252 // don't connect before giving DoS points
2253 // - Once a headers message is received that is valid and does connect,
2254 // nUnconnectingHeaders gets reset back to 0.
2255 if (mapBlockIndex.find(headers[0].hashPrevBlock) == mapBlockIndex.end() && nCount < MAX_BLOCKS_TO_ANNOUNCE) {
2256 nodestate->nUnconnectingHeaders++;
2257 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), uint256()));
2258 LogPrint(BCLog::NET, "received header %s: missing prev block %s, sending getheaders (%d) to end (peer=%d, nUnconnectingHeaders=%d)\n",
2259 headers[0].GetHash().ToString(),
2260 headers[0].hashPrevBlock.ToString(),
2261 pindexBestHeader->nHeight,
2262 pfrom->GetId(), nodestate->nUnconnectingHeaders);
2263 // Set hashLastUnknownBlock for this peer, so that if we
2264 // eventually get the headers - even from a different peer -
2265 // we can use this peer to download.
2266 UpdateBlockAvailability(pfrom->GetId(), headers.back().GetHash());
2268 if (nodestate->nUnconnectingHeaders % MAX_UNCONNECTING_HEADERS == 0) {
2269 Misbehaving(pfrom->GetId(), 20);
2271 return true;
2274 uint256 hashLastBlock;
2275 for (const CBlockHeader& header : headers) {
2276 if (!hashLastBlock.IsNull() && header.hashPrevBlock != hashLastBlock) {
2277 Misbehaving(pfrom->GetId(), 20);
2278 return error("non-continuous headers sequence");
2280 hashLastBlock = header.GetHash();
2284 CValidationState state;
2285 if (!ProcessNewBlockHeaders(headers, state, chainparams, &pindexLast)) {
2286 int nDoS;
2287 if (state.IsInvalid(nDoS)) {
2288 if (nDoS > 0) {
2289 LOCK(cs_main);
2290 Misbehaving(pfrom->GetId(), nDoS);
2292 return error("invalid header received");
2297 LOCK(cs_main);
2298 CNodeState *nodestate = State(pfrom->GetId());
2299 if (nodestate->nUnconnectingHeaders > 0) {
2300 LogPrint(BCLog::NET, "peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n", pfrom->GetId(), nodestate->nUnconnectingHeaders);
2302 nodestate->nUnconnectingHeaders = 0;
2304 assert(pindexLast);
2305 UpdateBlockAvailability(pfrom->GetId(), pindexLast->GetBlockHash());
2307 if (nCount == MAX_HEADERS_RESULTS) {
2308 // Headers message had its maximum size; the peer may have more headers.
2309 // TODO: optimize: if pindexLast is an ancestor of chainActive.Tip or pindexBestHeader, continue
2310 // from there instead.
2311 LogPrint(BCLog::NET, "more getheaders (%d) to end to peer=%d (startheight:%d)\n", pindexLast->nHeight, pfrom->GetId(), pfrom->nStartingHeight);
2312 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexLast), uint256()));
2315 bool fCanDirectFetch = CanDirectFetch(chainparams.GetConsensus());
2316 // If this set of headers is valid and ends in a block with at least as
2317 // much work as our tip, download as much as possible.
2318 if (fCanDirectFetch && pindexLast->IsValid(BLOCK_VALID_TREE) && chainActive.Tip()->nChainWork <= pindexLast->nChainWork) {
2319 std::vector<const CBlockIndex*> vToFetch;
2320 const CBlockIndex *pindexWalk = pindexLast;
2321 // Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
2322 while (pindexWalk && !chainActive.Contains(pindexWalk) && vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
2323 if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
2324 !mapBlocksInFlight.count(pindexWalk->GetBlockHash()) &&
2325 (!IsWitnessEnabled(pindexWalk->pprev, chainparams.GetConsensus()) || State(pfrom->GetId())->fHaveWitness)) {
2326 // We don't have this block, and it's not yet in flight.
2327 vToFetch.push_back(pindexWalk);
2329 pindexWalk = pindexWalk->pprev;
2331 // If pindexWalk still isn't on our main chain, we're looking at a
2332 // very large reorg at a time we think we're close to caught up to
2333 // the main chain -- this shouldn't really happen. Bail out on the
2334 // direct fetch and rely on parallel download instead.
2335 if (!chainActive.Contains(pindexWalk)) {
2336 LogPrint(BCLog::NET, "Large reorg, won't direct fetch to %s (%d)\n",
2337 pindexLast->GetBlockHash().ToString(),
2338 pindexLast->nHeight);
2339 } else {
2340 std::vector<CInv> vGetData;
2341 // Download as much as possible, from earliest to latest.
2342 for (const CBlockIndex *pindex : reverse_iterate(vToFetch)) {
2343 if (nodestate->nBlocksInFlight >= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
2344 // Can't download any more from this peer
2345 break;
2347 uint32_t nFetchFlags = GetFetchFlags(pfrom);
2348 vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
2349 MarkBlockAsInFlight(pfrom->GetId(), pindex->GetBlockHash(), pindex);
2350 LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n",
2351 pindex->GetBlockHash().ToString(), pfrom->GetId());
2353 if (vGetData.size() > 1) {
2354 LogPrint(BCLog::NET, "Downloading blocks toward %s (%d) via headers direct fetch\n",
2355 pindexLast->GetBlockHash().ToString(), pindexLast->nHeight);
2357 if (vGetData.size() > 0) {
2358 if (nodestate->fSupportsDesiredCmpctVersion && vGetData.size() == 1 && mapBlocksInFlight.size() == 1 && pindexLast->pprev->IsValid(BLOCK_VALID_CHAIN)) {
2359 // In any case, we want to download using a compact block, not a regular one
2360 vGetData[0] = CInv(MSG_CMPCT_BLOCK, vGetData[0].hash);
2362 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETDATA, vGetData));
2369 else if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
2371 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
2372 vRecv >> *pblock;
2374 LogPrint(BCLog::NET, "received block %s peer=%d\n", pblock->GetHash().ToString(), pfrom->GetId());
2376 // Process all blocks from whitelisted peers, even if not requested,
2377 // unless we're still syncing with the network.
2378 // Such an unrequested block may still be processed, subject to the
2379 // conditions in AcceptBlock().
2380 bool forceProcessing = pfrom->fWhitelisted && !IsInitialBlockDownload();
2381 const uint256 hash(pblock->GetHash());
2383 LOCK(cs_main);
2384 // Also always process if we requested the block explicitly, as we may
2385 // need it even though it is not a candidate for a new best tip.
2386 forceProcessing |= MarkBlockAsReceived(hash);
2387 // mapBlockSource is only used for sending reject messages and DoS scores,
2388 // so the race between here and cs_main in ProcessNewBlock is fine.
2389 mapBlockSource.emplace(hash, std::make_pair(pfrom->GetId(), true));
2391 bool fNewBlock = false;
2392 ProcessNewBlock(chainparams, pblock, forceProcessing, &fNewBlock);
2393 if (fNewBlock) {
2394 pfrom->nLastBlockTime = GetTime();
2395 } else {
2396 LOCK(cs_main);
2397 mapBlockSource.erase(pblock->GetHash());
2402 else if (strCommand == NetMsgType::GETADDR)
2404 // This asymmetric behavior for inbound and outbound connections was introduced
2405 // to prevent a fingerprinting attack: an attacker can send specific fake addresses
2406 // to users' AddrMan and later request them by sending getaddr messages.
2407 // Making nodes which are behind NAT and can only make outgoing connections ignore
2408 // the getaddr message mitigates the attack.
2409 if (!pfrom->fInbound) {
2410 LogPrint(BCLog::NET, "Ignoring \"getaddr\" from outbound connection. peer=%d\n", pfrom->GetId());
2411 return true;
2414 // Only send one GetAddr response per connection to reduce resource waste
2415 // and discourage addr stamping of INV announcements.
2416 if (pfrom->fSentAddr) {
2417 LogPrint(BCLog::NET, "Ignoring repeated \"getaddr\". peer=%d\n", pfrom->GetId());
2418 return true;
2420 pfrom->fSentAddr = true;
2422 pfrom->vAddrToSend.clear();
2423 std::vector<CAddress> vAddr = connman->GetAddresses();
2424 FastRandomContext insecure_rand;
2425 for (const CAddress &addr : vAddr)
2426 pfrom->PushAddress(addr, insecure_rand);
2430 else if (strCommand == NetMsgType::MEMPOOL)
2432 if (!(pfrom->GetLocalServices() & NODE_BLOOM) && !pfrom->fWhitelisted)
2434 LogPrint(BCLog::NET, "mempool request with bloom filters disabled, disconnect peer=%d\n", pfrom->GetId());
2435 pfrom->fDisconnect = true;
2436 return true;
2439 if (connman->OutboundTargetReached(false) && !pfrom->fWhitelisted)
2441 LogPrint(BCLog::NET, "mempool request with bandwidth limit reached, disconnect peer=%d\n", pfrom->GetId());
2442 pfrom->fDisconnect = true;
2443 return true;
2446 LOCK(pfrom->cs_inventory);
2447 pfrom->fSendMempool = true;
2451 else if (strCommand == NetMsgType::PING)
2453 if (pfrom->nVersion > BIP0031_VERSION)
2455 uint64_t nonce = 0;
2456 vRecv >> nonce;
2457 // Echo the message back with the nonce. This allows for two useful features:
2459 // 1) A remote node can quickly check if the connection is operational
2460 // 2) Remote nodes can measure the latency of the network thread. If this node
2461 // is overloaded it won't respond to pings quickly and the remote node can
2462 // avoid sending us more work, like chain download requests.
2464 // The nonce stops the remote getting confused between different pings: without
2465 // it, if the remote node sends a ping once per second and this node takes 5
2466 // seconds to respond to each, the 5th ping the remote sends would appear to
2467 // return very quickly.
2468 connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::PONG, nonce));
2473 else if (strCommand == NetMsgType::PONG)
2475 int64_t pingUsecEnd = nTimeReceived;
2476 uint64_t nonce = 0;
2477 size_t nAvail = vRecv.in_avail();
2478 bool bPingFinished = false;
2479 std::string sProblem;
2481 if (nAvail >= sizeof(nonce)) {
2482 vRecv >> nonce;
2484 // Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
2485 if (pfrom->nPingNonceSent != 0) {
2486 if (nonce == pfrom->nPingNonceSent) {
2487 // Matching pong received, this ping is no longer outstanding
2488 bPingFinished = true;
2489 int64_t pingUsecTime = pingUsecEnd - pfrom->nPingUsecStart;
2490 if (pingUsecTime > 0) {
2491 // Successful ping time measurement, replace previous
2492 pfrom->nPingUsecTime = pingUsecTime;
2493 pfrom->nMinPingUsecTime = std::min(pfrom->nMinPingUsecTime.load(), pingUsecTime);
2494 } else {
2495 // This should never happen
2496 sProblem = "Timing mishap";
2498 } else {
2499 // Nonce mismatches are normal when pings are overlapping
2500 sProblem = "Nonce mismatch";
2501 if (nonce == 0) {
2502 // This is most likely a bug in another implementation somewhere; cancel this ping
2503 bPingFinished = true;
2504 sProblem = "Nonce zero";
2507 } else {
2508 sProblem = "Unsolicited pong without ping";
2510 } else {
2511 // This is most likely a bug in another implementation somewhere; cancel this ping
2512 bPingFinished = true;
2513 sProblem = "Short payload";
2516 if (!(sProblem.empty())) {
2517 LogPrint(BCLog::NET, "pong peer=%d: %s, %x expected, %x received, %u bytes\n",
2518 pfrom->GetId(),
2519 sProblem,
2520 pfrom->nPingNonceSent,
2521 nonce,
2522 nAvail);
2524 if (bPingFinished) {
2525 pfrom->nPingNonceSent = 0;
2530 else if (strCommand == NetMsgType::FILTERLOAD)
2532 CBloomFilter filter;
2533 vRecv >> filter;
2535 if (!filter.IsWithinSizeConstraints())
2537 // There is no excuse for sending a too-large filter
2538 LOCK(cs_main);
2539 Misbehaving(pfrom->GetId(), 100);
2541 else
2543 LOCK(pfrom->cs_filter);
2544 delete pfrom->pfilter;
2545 pfrom->pfilter = new CBloomFilter(filter);
2546 pfrom->pfilter->UpdateEmptyFull();
2547 pfrom->fRelayTxes = true;
2552 else if (strCommand == NetMsgType::FILTERADD)
2554 std::vector<unsigned char> vData;
2555 vRecv >> vData;
2557 // Nodes must NEVER send a data item > 520 bytes (the max size for a script data object,
2558 // and thus, the maximum size any matched object can have) in a filteradd message
2559 bool bad = false;
2560 if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE) {
2561 bad = true;
2562 } else {
2563 LOCK(pfrom->cs_filter);
2564 if (pfrom->pfilter) {
2565 pfrom->pfilter->insert(vData);
2566 } else {
2567 bad = true;
2570 if (bad) {
2571 LOCK(cs_main);
2572 Misbehaving(pfrom->GetId(), 100);
2577 else if (strCommand == NetMsgType::FILTERCLEAR)
2579 LOCK(pfrom->cs_filter);
2580 if (pfrom->GetLocalServices() & NODE_BLOOM) {
2581 delete pfrom->pfilter;
2582 pfrom->pfilter = new CBloomFilter();
2584 pfrom->fRelayTxes = true;
2587 else if (strCommand == NetMsgType::FEEFILTER) {
2588 CAmount newFeeFilter = 0;
2589 vRecv >> newFeeFilter;
2590 if (MoneyRange(newFeeFilter)) {
2592 LOCK(pfrom->cs_feeFilter);
2593 pfrom->minFeeFilter = newFeeFilter;
2595 LogPrint(BCLog::NET, "received: feefilter of %s from peer=%d\n", CFeeRate(newFeeFilter).ToString(), pfrom->GetId());
2599 else if (strCommand == NetMsgType::NOTFOUND) {
2600 // We do not care about the NOTFOUND message, but logging an Unknown Command
2601 // message would be undesirable as we transmit it ourselves.
2604 else {
2605 // Ignore unknown commands for extensibility
2606 LogPrint(BCLog::NET, "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->GetId());
2611 return true;
2614 static bool SendRejectsAndCheckIfBanned(CNode* pnode, CConnman* connman)
2616 AssertLockHeld(cs_main);
2617 CNodeState &state = *State(pnode->GetId());
2619 for (const CBlockReject& reject : state.rejects) {
2620 connman->PushMessage(pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, (std::string)NetMsgType::BLOCK, reject.chRejectCode, reject.strRejectReason, reject.hashBlock));
2622 state.rejects.clear();
2624 if (state.fShouldBan) {
2625 state.fShouldBan = false;
2626 if (pnode->fWhitelisted)
2627 LogPrintf("Warning: not punishing whitelisted peer %s!\n", pnode->addr.ToString());
2628 else if (pnode->fAddnode)
2629 LogPrintf("Warning: not punishing addnoded peer %s!\n", pnode->addr.ToString());
2630 else {
2631 pnode->fDisconnect = true;
2632 if (pnode->addr.IsLocal())
2633 LogPrintf("Warning: not banning local peer %s!\n", pnode->addr.ToString());
2634 else
2636 connman->Ban(pnode->addr, BanReasonNodeMisbehaving);
2639 return true;
2641 return false;
2644 bool PeerLogicValidation::ProcessMessages(CNode* pfrom, std::atomic<bool>& interruptMsgProc)
2646 const CChainParams& chainparams = Params();
2648 // Message format
2649 // (4) message start
2650 // (12) command
2651 // (4) size
2652 // (4) checksum
2653 // (x) data
2655 bool fMoreWork = false;
2657 if (!pfrom->vRecvGetData.empty())
2658 ProcessGetData(pfrom, chainparams.GetConsensus(), connman, interruptMsgProc);
2660 if (pfrom->fDisconnect)
2661 return false;
2663 // this maintains the order of responses
2664 if (!pfrom->vRecvGetData.empty()) return true;
2666 // Don't bother if send buffer is too full to respond anyway
2667 if (pfrom->fPauseSend)
2668 return false;
2670 std::list<CNetMessage> msgs;
2672 LOCK(pfrom->cs_vProcessMsg);
2673 if (pfrom->vProcessMsg.empty())
2674 return false;
2675 // Just take one message
2676 msgs.splice(msgs.begin(), pfrom->vProcessMsg, pfrom->vProcessMsg.begin());
2677 pfrom->nProcessQueueSize -= msgs.front().vRecv.size() + CMessageHeader::HEADER_SIZE;
2678 pfrom->fPauseRecv = pfrom->nProcessQueueSize > connman->GetReceiveFloodSize();
2679 fMoreWork = !pfrom->vProcessMsg.empty();
2681 CNetMessage& msg(msgs.front());
2683 msg.SetVersion(pfrom->GetRecvVersion());
2684 // Scan for message start
2685 if (memcmp(msg.hdr.pchMessageStart, chainparams.MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) {
2686 LogPrintf("PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg.hdr.GetCommand()), pfrom->GetId());
2687 pfrom->fDisconnect = true;
2688 return false;
2691 // Read header
2692 CMessageHeader& hdr = msg.hdr;
2693 if (!hdr.IsValid(chainparams.MessageStart()))
2695 LogPrintf("PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(hdr.GetCommand()), pfrom->GetId());
2696 return fMoreWork;
2698 std::string strCommand = hdr.GetCommand();
2700 // Message size
2701 unsigned int nMessageSize = hdr.nMessageSize;
2703 // Checksum
2704 CDataStream& vRecv = msg.vRecv;
2705 const uint256& hash = msg.GetMessageHash();
2706 if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0)
2708 LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s\n", __func__,
2709 SanitizeString(strCommand), nMessageSize,
2710 HexStr(hash.begin(), hash.begin()+CMessageHeader::CHECKSUM_SIZE),
2711 HexStr(hdr.pchChecksum, hdr.pchChecksum+CMessageHeader::CHECKSUM_SIZE));
2712 return fMoreWork;
2715 // Process message
2716 bool fRet = false;
2719 fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime, chainparams, connman, interruptMsgProc);
2720 if (interruptMsgProc)
2721 return false;
2722 if (!pfrom->vRecvGetData.empty())
2723 fMoreWork = true;
2725 catch (const std::ios_base::failure& e)
2727 connman->PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_MALFORMED, std::string("error parsing message")));
2728 if (strstr(e.what(), "end of data"))
2730 // Allow exceptions from under-length message on vRecv
2731 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());
2733 else if (strstr(e.what(), "size too large"))
2735 // Allow exceptions from over-long size
2736 LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
2738 else if (strstr(e.what(), "non-canonical ReadCompactSize()"))
2740 // Allow exceptions from non-canonical encoding
2741 LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
2743 else
2745 PrintExceptionContinue(&e, "ProcessMessages()");
2748 catch (const std::exception& e) {
2749 PrintExceptionContinue(&e, "ProcessMessages()");
2750 } catch (...) {
2751 PrintExceptionContinue(nullptr, "ProcessMessages()");
2754 if (!fRet) {
2755 LogPrintf("%s(%s, %u bytes) FAILED peer=%d\n", __func__, SanitizeString(strCommand), nMessageSize, pfrom->GetId());
2758 LOCK(cs_main);
2759 SendRejectsAndCheckIfBanned(pfrom, connman);
2761 return fMoreWork;
2764 class CompareInvMempoolOrder
2766 CTxMemPool *mp;
2767 public:
2768 explicit CompareInvMempoolOrder(CTxMemPool *_mempool)
2770 mp = _mempool;
2773 bool operator()(std::set<uint256>::iterator a, std::set<uint256>::iterator b)
2775 /* As std::make_heap produces a max-heap, we want the entries with the
2776 * fewest ancestors/highest fee to sort later. */
2777 return mp->CompareDepthAndScore(*b, *a);
2781 bool PeerLogicValidation::SendMessages(CNode* pto, std::atomic<bool>& interruptMsgProc)
2783 const Consensus::Params& consensusParams = Params().GetConsensus();
2785 // Don't send anything until the version handshake is complete
2786 if (!pto->fSuccessfullyConnected || pto->fDisconnect)
2787 return true;
2789 // If we get here, the outgoing message serialization version is set and can't change.
2790 const CNetMsgMaker msgMaker(pto->GetSendVersion());
2793 // Message: ping
2795 bool pingSend = false;
2796 if (pto->fPingQueued) {
2797 // RPC ping request by user
2798 pingSend = true;
2800 if (pto->nPingNonceSent == 0 && pto->nPingUsecStart + PING_INTERVAL * 1000000 < GetTimeMicros()) {
2801 // Ping automatically sent as a latency probe & keepalive.
2802 pingSend = true;
2804 if (pingSend) {
2805 uint64_t nonce = 0;
2806 while (nonce == 0) {
2807 GetRandBytes((unsigned char*)&nonce, sizeof(nonce));
2809 pto->fPingQueued = false;
2810 pto->nPingUsecStart = GetTimeMicros();
2811 if (pto->nVersion > BIP0031_VERSION) {
2812 pto->nPingNonceSent = nonce;
2813 connman->PushMessage(pto, msgMaker.Make(NetMsgType::PING, nonce));
2814 } else {
2815 // Peer is too old to support ping command with nonce, pong will never arrive.
2816 pto->nPingNonceSent = 0;
2817 connman->PushMessage(pto, msgMaker.Make(NetMsgType::PING));
2821 TRY_LOCK(cs_main, lockMain); // Acquire cs_main for IsInitialBlockDownload() and CNodeState()
2822 if (!lockMain)
2823 return true;
2825 if (SendRejectsAndCheckIfBanned(pto, connman))
2826 return true;
2827 CNodeState &state = *State(pto->GetId());
2829 // Address refresh broadcast
2830 int64_t nNow = GetTimeMicros();
2831 if (!IsInitialBlockDownload() && pto->nNextLocalAddrSend < nNow) {
2832 AdvertiseLocal(pto);
2833 pto->nNextLocalAddrSend = PoissonNextSend(nNow, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
2837 // Message: addr
2839 if (pto->nNextAddrSend < nNow) {
2840 pto->nNextAddrSend = PoissonNextSend(nNow, AVG_ADDRESS_BROADCAST_INTERVAL);
2841 std::vector<CAddress> vAddr;
2842 vAddr.reserve(pto->vAddrToSend.size());
2843 for (const CAddress& addr : pto->vAddrToSend)
2845 if (!pto->addrKnown.contains(addr.GetKey()))
2847 pto->addrKnown.insert(addr.GetKey());
2848 vAddr.push_back(addr);
2849 // receiver rejects addr messages larger than 1000
2850 if (vAddr.size() >= 1000)
2852 connman->PushMessage(pto, msgMaker.Make(NetMsgType::ADDR, vAddr));
2853 vAddr.clear();
2857 pto->vAddrToSend.clear();
2858 if (!vAddr.empty())
2859 connman->PushMessage(pto, msgMaker.Make(NetMsgType::ADDR, vAddr));
2860 // we only send the big addr message once
2861 if (pto->vAddrToSend.capacity() > 40)
2862 pto->vAddrToSend.shrink_to_fit();
2865 // Start block sync
2866 if (pindexBestHeader == nullptr)
2867 pindexBestHeader = chainActive.Tip();
2868 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.
2869 if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex) {
2870 // Only actively request headers from a single peer, unless we're close to today.
2871 if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
2872 state.fSyncStarted = true;
2873 state.nHeadersSyncTimeout = GetTimeMicros() + HEADERS_DOWNLOAD_TIMEOUT_BASE + HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER * (GetAdjustedTime() - pindexBestHeader->GetBlockTime())/(consensusParams.nPowTargetSpacing);
2874 nSyncStarted++;
2875 const CBlockIndex *pindexStart = pindexBestHeader;
2876 /* If possible, start at the block preceding the currently
2877 best known header. This ensures that we always get a
2878 non-empty list of headers back as long as the peer
2879 is up-to-date. With a non-empty response, we can initialise
2880 the peer's known best block. This wouldn't be possible
2881 if we requested starting at pindexBestHeader and
2882 got back an empty response. */
2883 if (pindexStart->pprev)
2884 pindexStart = pindexStart->pprev;
2885 LogPrint(BCLog::NET, "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->nHeight, pto->GetId(), pto->nStartingHeight);
2886 connman->PushMessage(pto, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexStart), uint256()));
2890 // Resend wallet transactions that haven't gotten in a block yet
2891 // Except during reindex, importing and IBD, when old wallet
2892 // transactions become unconfirmed and spams other nodes.
2893 if (!fReindex && !fImporting && !IsInitialBlockDownload())
2895 GetMainSignals().Broadcast(nTimeBestReceived, connman);
2899 // Try sending block announcements via headers
2902 // If we have less than MAX_BLOCKS_TO_ANNOUNCE in our
2903 // list of block hashes we're relaying, and our peer wants
2904 // headers announcements, then find the first header
2905 // not yet known to our peer but would connect, and send.
2906 // If no header would connect, or if we have too many
2907 // blocks, or if the peer doesn't want headers, just
2908 // add all to the inv queue.
2909 LOCK(pto->cs_inventory);
2910 std::vector<CBlock> vHeaders;
2911 bool fRevertToInv = ((!state.fPreferHeaders &&
2912 (!state.fPreferHeaderAndIDs || pto->vBlockHashesToAnnounce.size() > 1)) ||
2913 pto->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE);
2914 const CBlockIndex *pBestIndex = nullptr; // last header queued for delivery
2915 ProcessBlockAvailability(pto->GetId()); // ensure pindexBestKnownBlock is up-to-date
2917 if (!fRevertToInv) {
2918 bool fFoundStartingHeader = false;
2919 // Try to find first header that our peer doesn't have, and
2920 // then send all headers past that one. If we come across any
2921 // headers that aren't on chainActive, give up.
2922 for (const uint256 &hash : pto->vBlockHashesToAnnounce) {
2923 BlockMap::iterator mi = mapBlockIndex.find(hash);
2924 assert(mi != mapBlockIndex.end());
2925 const CBlockIndex *pindex = mi->second;
2926 if (chainActive[pindex->nHeight] != pindex) {
2927 // Bail out if we reorged away from this block
2928 fRevertToInv = true;
2929 break;
2931 if (pBestIndex != nullptr && pindex->pprev != pBestIndex) {
2932 // This means that the list of blocks to announce don't
2933 // connect to each other.
2934 // This shouldn't really be possible to hit during
2935 // regular operation (because reorgs should take us to
2936 // a chain that has some block not on the prior chain,
2937 // which should be caught by the prior check), but one
2938 // way this could happen is by using invalidateblock /
2939 // reconsiderblock repeatedly on the tip, causing it to
2940 // be added multiple times to vBlockHashesToAnnounce.
2941 // Robustly deal with this rare situation by reverting
2942 // to an inv.
2943 fRevertToInv = true;
2944 break;
2946 pBestIndex = pindex;
2947 if (fFoundStartingHeader) {
2948 // add this to the headers message
2949 vHeaders.push_back(pindex->GetBlockHeader());
2950 } else if (PeerHasHeader(&state, pindex)) {
2951 continue; // keep looking for the first new block
2952 } else if (pindex->pprev == nullptr || PeerHasHeader(&state, pindex->pprev)) {
2953 // Peer doesn't have this header but they do have the prior one.
2954 // Start sending headers.
2955 fFoundStartingHeader = true;
2956 vHeaders.push_back(pindex->GetBlockHeader());
2957 } else {
2958 // Peer doesn't have this header or the prior one -- nothing will
2959 // connect, so bail out.
2960 fRevertToInv = true;
2961 break;
2965 if (!fRevertToInv && !vHeaders.empty()) {
2966 if (vHeaders.size() == 1 && state.fPreferHeaderAndIDs) {
2967 // We only send up to 1 block as header-and-ids, as otherwise
2968 // probably means we're doing an initial-ish-sync or they're slow
2969 LogPrint(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", __func__,
2970 vHeaders.front().GetHash().ToString(), pto->GetId());
2972 int nSendFlags = state.fWantsCmpctWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
2974 bool fGotBlockFromCache = false;
2976 LOCK(cs_most_recent_block);
2977 if (most_recent_block_hash == pBestIndex->GetBlockHash()) {
2978 if (state.fWantsCmpctWitness || !fWitnessesPresentInMostRecentCompactBlock)
2979 connman->PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, *most_recent_compact_block));
2980 else {
2981 CBlockHeaderAndShortTxIDs cmpctblock(*most_recent_block, state.fWantsCmpctWitness);
2982 connman->PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
2984 fGotBlockFromCache = true;
2987 if (!fGotBlockFromCache) {
2988 CBlock block;
2989 bool ret = ReadBlockFromDisk(block, pBestIndex, consensusParams);
2990 assert(ret);
2991 CBlockHeaderAndShortTxIDs cmpctblock(block, state.fWantsCmpctWitness);
2992 connman->PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
2994 state.pindexBestHeaderSent = pBestIndex;
2995 } else if (state.fPreferHeaders) {
2996 if (vHeaders.size() > 1) {
2997 LogPrint(BCLog::NET, "%s: %u headers, range (%s, %s), to peer=%d\n", __func__,
2998 vHeaders.size(),
2999 vHeaders.front().GetHash().ToString(),
3000 vHeaders.back().GetHash().ToString(), pto->GetId());
3001 } else {
3002 LogPrint(BCLog::NET, "%s: sending header %s to peer=%d\n", __func__,
3003 vHeaders.front().GetHash().ToString(), pto->GetId());
3005 connman->PushMessage(pto, msgMaker.Make(NetMsgType::HEADERS, vHeaders));
3006 state.pindexBestHeaderSent = pBestIndex;
3007 } else
3008 fRevertToInv = true;
3010 if (fRevertToInv) {
3011 // If falling back to using an inv, just try to inv the tip.
3012 // The last entry in vBlockHashesToAnnounce was our tip at some point
3013 // in the past.
3014 if (!pto->vBlockHashesToAnnounce.empty()) {
3015 const uint256 &hashToAnnounce = pto->vBlockHashesToAnnounce.back();
3016 BlockMap::iterator mi = mapBlockIndex.find(hashToAnnounce);
3017 assert(mi != mapBlockIndex.end());
3018 const CBlockIndex *pindex = mi->second;
3020 // Warn if we're announcing a block that is not on the main chain.
3021 // This should be very rare and could be optimized out.
3022 // Just log for now.
3023 if (chainActive[pindex->nHeight] != pindex) {
3024 LogPrint(BCLog::NET, "Announcing block %s not on main chain (tip=%s)\n",
3025 hashToAnnounce.ToString(), chainActive.Tip()->GetBlockHash().ToString());
3028 // If the peer's chain has this block, don't inv it back.
3029 if (!PeerHasHeader(&state, pindex)) {
3030 pto->PushInventory(CInv(MSG_BLOCK, hashToAnnounce));
3031 LogPrint(BCLog::NET, "%s: sending inv peer=%d hash=%s\n", __func__,
3032 pto->GetId(), hashToAnnounce.ToString());
3036 pto->vBlockHashesToAnnounce.clear();
3040 // Message: inventory
3042 std::vector<CInv> vInv;
3044 LOCK(pto->cs_inventory);
3045 vInv.reserve(std::max<size_t>(pto->vInventoryBlockToSend.size(), INVENTORY_BROADCAST_MAX));
3047 // Add blocks
3048 for (const uint256& hash : pto->vInventoryBlockToSend) {
3049 vInv.push_back(CInv(MSG_BLOCK, hash));
3050 if (vInv.size() == MAX_INV_SZ) {
3051 connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
3052 vInv.clear();
3055 pto->vInventoryBlockToSend.clear();
3057 // Check whether periodic sends should happen
3058 bool fSendTrickle = pto->fWhitelisted;
3059 if (pto->nNextInvSend < nNow) {
3060 fSendTrickle = true;
3061 // Use half the delay for outbound peers, as there is less privacy concern for them.
3062 pto->nNextInvSend = PoissonNextSend(nNow, INVENTORY_BROADCAST_INTERVAL >> !pto->fInbound);
3065 // Time to send but the peer has requested we not relay transactions.
3066 if (fSendTrickle) {
3067 LOCK(pto->cs_filter);
3068 if (!pto->fRelayTxes) pto->setInventoryTxToSend.clear();
3071 // Respond to BIP35 mempool requests
3072 if (fSendTrickle && pto->fSendMempool) {
3073 auto vtxinfo = mempool.infoAll();
3074 pto->fSendMempool = false;
3075 CAmount filterrate = 0;
3077 LOCK(pto->cs_feeFilter);
3078 filterrate = pto->minFeeFilter;
3081 LOCK(pto->cs_filter);
3083 for (const auto& txinfo : vtxinfo) {
3084 const uint256& hash = txinfo.tx->GetHash();
3085 CInv inv(MSG_TX, hash);
3086 pto->setInventoryTxToSend.erase(hash);
3087 if (filterrate) {
3088 if (txinfo.feeRate.GetFeePerK() < filterrate)
3089 continue;
3091 if (pto->pfilter) {
3092 if (!pto->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
3094 pto->filterInventoryKnown.insert(hash);
3095 vInv.push_back(inv);
3096 if (vInv.size() == MAX_INV_SZ) {
3097 connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
3098 vInv.clear();
3101 pto->timeLastMempoolReq = GetTime();
3104 // Determine transactions to relay
3105 if (fSendTrickle) {
3106 // Produce a vector with all candidates for sending
3107 std::vector<std::set<uint256>::iterator> vInvTx;
3108 vInvTx.reserve(pto->setInventoryTxToSend.size());
3109 for (std::set<uint256>::iterator it = pto->setInventoryTxToSend.begin(); it != pto->setInventoryTxToSend.end(); it++) {
3110 vInvTx.push_back(it);
3112 CAmount filterrate = 0;
3114 LOCK(pto->cs_feeFilter);
3115 filterrate = pto->minFeeFilter;
3117 // Topologically and fee-rate sort the inventory we send for privacy and priority reasons.
3118 // A heap is used so that not all items need sorting if only a few are being sent.
3119 CompareInvMempoolOrder compareInvMempoolOrder(&mempool);
3120 std::make_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
3121 // No reason to drain out at many times the network's capacity,
3122 // especially since we have many peers and some will draw much shorter delays.
3123 unsigned int nRelayedTransactions = 0;
3124 LOCK(pto->cs_filter);
3125 while (!vInvTx.empty() && nRelayedTransactions < INVENTORY_BROADCAST_MAX) {
3126 // Fetch the top element from the heap
3127 std::pop_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
3128 std::set<uint256>::iterator it = vInvTx.back();
3129 vInvTx.pop_back();
3130 uint256 hash = *it;
3131 // Remove it from the to-be-sent set
3132 pto->setInventoryTxToSend.erase(it);
3133 // Check if not in the filter already
3134 if (pto->filterInventoryKnown.contains(hash)) {
3135 continue;
3137 // Not in the mempool anymore? don't bother sending it.
3138 auto txinfo = mempool.info(hash);
3139 if (!txinfo.tx) {
3140 continue;
3142 if (filterrate && txinfo.feeRate.GetFeePerK() < filterrate) {
3143 continue;
3145 if (pto->pfilter && !pto->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
3146 // Send
3147 vInv.push_back(CInv(MSG_TX, hash));
3148 nRelayedTransactions++;
3150 // Expire old relay messages
3151 while (!vRelayExpiration.empty() && vRelayExpiration.front().first < nNow)
3153 mapRelay.erase(vRelayExpiration.front().second);
3154 vRelayExpiration.pop_front();
3157 auto ret = mapRelay.insert(std::make_pair(hash, std::move(txinfo.tx)));
3158 if (ret.second) {
3159 vRelayExpiration.push_back(std::make_pair(nNow + 15 * 60 * 1000000, ret.first));
3162 if (vInv.size() == MAX_INV_SZ) {
3163 connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
3164 vInv.clear();
3166 pto->filterInventoryKnown.insert(hash);
3170 if (!vInv.empty())
3171 connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
3173 // Detect whether we're stalling
3174 nNow = GetTimeMicros();
3175 if (state.nStallingSince && state.nStallingSince < nNow - 1000000 * BLOCK_STALLING_TIMEOUT) {
3176 // Stalling only triggers when the block download window cannot move. During normal steady state,
3177 // the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
3178 // should only happen during initial block download.
3179 LogPrintf("Peer=%d is stalling block download, disconnecting\n", pto->GetId());
3180 pto->fDisconnect = true;
3181 return true;
3183 // In case there is a block that has been in flight from this peer for 2 + 0.5 * N times the block interval
3184 // (with N the number of peers from which we're downloading validated blocks), disconnect due to timeout.
3185 // We compensate for other peers to prevent killing off peers due to our own downstream link
3186 // being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
3187 // to unreasonably increase our timeout.
3188 if (state.vBlocksInFlight.size() > 0) {
3189 QueuedBlock &queuedBlock = state.vBlocksInFlight.front();
3190 int nOtherPeersWithValidatedDownloads = nPeersWithValidatedDownloads - (state.nBlocksInFlightValidHeaders > 0);
3191 if (nNow > state.nDownloadingSince + consensusParams.nPowTargetSpacing * (BLOCK_DOWNLOAD_TIMEOUT_BASE + BLOCK_DOWNLOAD_TIMEOUT_PER_PEER * nOtherPeersWithValidatedDownloads)) {
3192 LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", queuedBlock.hash.ToString(), pto->GetId());
3193 pto->fDisconnect = true;
3194 return true;
3197 // Check for headers sync timeouts
3198 if (state.fSyncStarted && state.nHeadersSyncTimeout < std::numeric_limits<int64_t>::max()) {
3199 // Detect whether this is a stalling initial-headers-sync peer
3200 if (pindexBestHeader->GetBlockTime() <= GetAdjustedTime() - 24*60*60) {
3201 if (nNow > state.nHeadersSyncTimeout && nSyncStarted == 1 && (nPreferredDownload - state.fPreferredDownload >= 1)) {
3202 // Disconnect a (non-whitelisted) peer if it is our only sync peer,
3203 // and we have others we could be using instead.
3204 // Note: If all our peers are inbound, then we won't
3205 // disconnect our sync peer for stalling; we have bigger
3206 // problems if we can't get any outbound peers.
3207 if (!pto->fWhitelisted) {
3208 LogPrintf("Timeout downloading headers from peer=%d, disconnecting\n", pto->GetId());
3209 pto->fDisconnect = true;
3210 return true;
3211 } else {
3212 LogPrintf("Timeout downloading headers from whitelisted peer=%d, not disconnecting\n", pto->GetId());
3213 // Reset the headers sync state so that we have a
3214 // chance to try downloading from a different peer.
3215 // Note: this will also result in at least one more
3216 // getheaders message to be sent to
3217 // this peer (eventually).
3218 state.fSyncStarted = false;
3219 nSyncStarted--;
3220 state.nHeadersSyncTimeout = 0;
3223 } else {
3224 // After we've caught up once, reset the timeout so we can't trigger
3225 // disconnect later.
3226 state.nHeadersSyncTimeout = std::numeric_limits<int64_t>::max();
3232 // Message: getdata (blocks)
3234 std::vector<CInv> vGetData;
3235 if (!pto->fClient && (fFetch || !IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
3236 std::vector<const CBlockIndex*> vToDownload;
3237 NodeId staller = -1;
3238 FindNextBlocksToDownload(pto->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller, consensusParams);
3239 for (const CBlockIndex *pindex : vToDownload) {
3240 uint32_t nFetchFlags = GetFetchFlags(pto);
3241 vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
3242 MarkBlockAsInFlight(pto->GetId(), pindex->GetBlockHash(), pindex);
3243 LogPrint(BCLog::NET, "Requesting block %s (%d) peer=%d\n", pindex->GetBlockHash().ToString(),
3244 pindex->nHeight, pto->GetId());
3246 if (state.nBlocksInFlight == 0 && staller != -1) {
3247 if (State(staller)->nStallingSince == 0) {
3248 State(staller)->nStallingSince = nNow;
3249 LogPrint(BCLog::NET, "Stall started peer=%d\n", staller);
3255 // Message: getdata (non-blocks)
3257 while (!pto->mapAskFor.empty() && (*pto->mapAskFor.begin()).first <= nNow)
3259 const CInv& inv = (*pto->mapAskFor.begin()).second;
3260 if (!AlreadyHave(inv))
3262 LogPrint(BCLog::NET, "Requesting %s peer=%d\n", inv.ToString(), pto->GetId());
3263 vGetData.push_back(inv);
3264 if (vGetData.size() >= 1000)
3266 connman->PushMessage(pto, msgMaker.Make(NetMsgType::GETDATA, vGetData));
3267 vGetData.clear();
3269 } else {
3270 //If we're not going to ask, don't expect a response.
3271 pto->setAskFor.erase(inv.hash);
3273 pto->mapAskFor.erase(pto->mapAskFor.begin());
3275 if (!vGetData.empty())
3276 connman->PushMessage(pto, msgMaker.Make(NetMsgType::GETDATA, vGetData));
3279 // Message: feefilter
3281 // We don't want white listed peers to filter txs to us if we have -whitelistforcerelay
3282 if (pto->nVersion >= FEEFILTER_VERSION && gArgs.GetBoolArg("-feefilter", DEFAULT_FEEFILTER) &&
3283 !(pto->fWhitelisted && gArgs.GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY))) {
3284 CAmount currentFilter = mempool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFeePerK();
3285 int64_t timeNow = GetTimeMicros();
3286 if (timeNow > pto->nextSendTimeFeeFilter) {
3287 static CFeeRate default_feerate(DEFAULT_MIN_RELAY_TX_FEE);
3288 static FeeFilterRounder filterRounder(default_feerate);
3289 CAmount filterToSend = filterRounder.round(currentFilter);
3290 // We always have a fee filter of at least minRelayTxFee
3291 filterToSend = std::max(filterToSend, ::minRelayTxFee.GetFeePerK());
3292 if (filterToSend != pto->lastSentFeeFilter) {
3293 connman->PushMessage(pto, msgMaker.Make(NetMsgType::FEEFILTER, filterToSend));
3294 pto->lastSentFeeFilter = filterToSend;
3296 pto->nextSendTimeFeeFilter = PoissonNextSend(timeNow, AVG_FEEFILTER_BROADCAST_INTERVAL);
3298 // If the fee filter has changed substantially and it's still more than MAX_FEEFILTER_CHANGE_DELAY
3299 // until scheduled broadcast, then move the broadcast to within MAX_FEEFILTER_CHANGE_DELAY.
3300 else if (timeNow + MAX_FEEFILTER_CHANGE_DELAY * 1000000 < pto->nextSendTimeFeeFilter &&
3301 (currentFilter < 3 * pto->lastSentFeeFilter / 4 || currentFilter > 4 * pto->lastSentFeeFilter / 3)) {
3302 pto->nextSendTimeFeeFilter = timeNow + GetRandInt(MAX_FEEFILTER_CHANGE_DELAY) * 1000000;
3306 return true;
3309 class CNetProcessingCleanup
3311 public:
3312 CNetProcessingCleanup() {}
3313 ~CNetProcessingCleanup() {
3314 // orphan transactions
3315 mapOrphanTransactions.clear();
3316 mapOrphanTransactionsByPrev.clear();
3318 } instance_of_cnetprocessingcleanup;