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"
9 #include "arith_uint256.h"
10 #include "blockencodings.h"
11 #include "chainparams.h"
12 #include "consensus/validation.h"
15 #include "validation.h"
16 #include "merkleblock.h"
18 #include "netmessagemaker.h"
20 #include "policy/fees.h"
21 #include "policy/policy.h"
22 #include "primitives/block.h"
23 #include "primitives/transaction.h"
25 #include "reverse_iterator.h"
26 #include "tinyformat.h"
27 #include "txmempool.h"
28 #include "ui_interface.h"
30 #include "utilmoneystr.h"
31 #include "utilstrencodings.h"
32 #include "validationinterface.h"
35 # error "Bitcoin cannot be compiled without assertions."
38 std::atomic
<int64_t> nTimeBestReceived(0); // Used only to inform the wallet of when we last received a block
40 struct IteratorComparator
43 bool operator()(const I
& a
, const I
& b
)
50 // When modifying, adapt the copy of this definition in tests/DoS_tests.
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]
66 /** Number of nodes with fSyncStarted. */
70 * Sources of received blocks, saved to be able to send them reject
71 * messages or ban them when processing happens afterwards. Protected by
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
;
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
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
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. */
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
;
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
;
128 struct CBlockReject
{
129 unsigned char chRejectCode
;
130 std::string strRejectReason
;
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.
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.
147 //! Whether this peer should be disconnected and banned (unless whitelisted).
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.
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
;
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.
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
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;
200 pindexBestKnownBlock
= nullptr;
201 hashLastUnknownBlock
.SetNull();
202 pindexLastCommonBlock
= nullptr;
203 pindexBestHeaderSent
= nullptr;
204 nUnconnectingHeaders
= 0;
205 fSyncStarted
= false;
206 nHeadersSyncTimeout
= 0;
208 nDownloadingSince
= 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
;
225 CNodeState
*State(NodeId pnode
) {
226 std::map
<NodeId
, CNodeState
>::iterator it
= mapNodeState
.find(pnode
);
227 if (it
== mapNodeState
.end())
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
));
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
);
259 LogPrint(BCLog::NET
, "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION
, nNodeStartingHeight
, addrMe
.ToString(), nodeid
);
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
);
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
) {
300 *pit
= &itInFlight
->second
.second
;
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
;
321 *pit
= &itInFlight
->second
.second
;
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
;
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.
365 if (nodestate
->fProvidesHeaderAndIDs
) {
366 for (std::list
<NodeId
>::iterator it
= lNodesAnnouncingHeaderAndIDs
.begin(); it
!= lNodesAnnouncingHeaderAndIDs
.end(); it
++) {
368 lNodesAnnouncingHeaderAndIDs
.erase(it
);
369 lNodesAnnouncingHeaderAndIDs
.push_back(nodeid
);
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
));
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());
394 bool CanDirectFetch(const Consensus::Params
&consensusParams
)
396 return chainActive
.Tip()->GetBlockTime() > GetAdjustedTime() - consensusParams
.nPowTargetSpacing
* 20;
400 bool PeerHasHeader(CNodeState
*state
, const CBlockIndex
*pindex
)
402 if (state
->pindexBestKnownBlock
&& pindex
== state
->pindexBestKnownBlock
->GetAncestor(pindex
->nHeight
))
404 if (state
->pindexBestHeaderSent
&& pindex
== state
->pindexBestHeaderSent
->GetAncestor(pindex
->nHeight
))
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
) {
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.
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
)
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.
468 if (!State(nodeid
)->fHaveWitness
&& IsWitnessEnabled(pindex
->pprev
, consensusParams
)) {
469 // We wouldn't download this block or its descendants from this peer.
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
;
485 vBlocks
.push_back(pindex
);
486 if (vBlocks
.size() == count
) {
489 } else if (waitingfor
== -1) {
490 // This is the first already-in-flight block.
491 waitingfor
= mapBlocksInFlight
[pindex
->GetBlockHash()].first
;
499 void PeerLogicValidation::InitializeNode(CNode
*pnode
) {
500 CAddress addr
= pnode
->addr
;
501 std::string addrName
= pnode
->GetAddrName();
502 NodeId nodeid
= pnode
->GetId();
505 mapNodeState
.emplace_hint(mapNodeState
.end(), std::piecewise_construct
, std::forward_as_tuple(nodeid
), std::forward_as_tuple(addr
, std::move(addrName
)));
508 PushNodeVersion(pnode
, connman
, GetTime());
511 void PeerLogicValidation::FinalizeNode(NodeId nodeid
, bool& fUpdateConnectionTime
) {
512 fUpdateConnectionTime
= false;
514 CNodeState
*state
= State(nodeid
);
515 assert(state
!= nullptr);
517 if (state
->fSyncStarted
)
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
) {
545 CNodeState
*state
= State(nodeid
);
546 if (state
== nullptr)
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
) {
553 stats
.vHeightInFlight
.push_back(queue
.pindex
->nHeight
);
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)
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
))
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());
594 auto ret
= mapOrphanTransactions
.emplace(hash
, COrphanTx
{tx
, peer
, GetTime() + ORPHAN_TX_EXPIRE_TIME
});
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());
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())
612 for (const CTxIn
& txin
: it
->second
.tx
->vin
)
614 auto itPrev
= mapOrphanTransactionsByPrev
.find(txin
.prevout
);
615 if (itPrev
== mapOrphanTransactionsByPrev
.end())
617 itPrev
->second
.erase(it
);
618 if (itPrev
->second
.empty())
619 mapOrphanTransactionsByPrev
.erase(itPrev
);
621 mapOrphanTransactions
.erase(it
);
625 void EraseOrphansFor(NodeId peer
)
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:
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());
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
);
678 void Misbehaving(NodeId pnode
, int howmuch
)
683 CNodeState
*state
= State(pnode
);
684 if (state
== nullptr)
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;
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
) {
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()) {
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
);
757 static int nHighestFastAnnounce
= 0;
758 if (pindex
->nHeight
<= nHighestFastAnnounce
)
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
)
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.
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
) {
826 const uint256
hash(block
.GetHash());
827 std::map
<uint256
, std::pair
<NodeId
, bool>>::iterator it
= mapBlockSource
.find(hash
);
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
);
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 //////////////////////////////////////////////////////////////////////////////
862 bool static AlreadyHave(const CInv
& inv
) EXCLUSIVE_LOCKS_REQUIRED(cs_main
)
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));
887 case MSG_WITNESS_BLOCK
:
888 return mapBlockIndex
.count(inv
.hash
);
890 // Don't know what it is, just say we already got one
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
);
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());
946 while (it
!= pfrom
->vRecvGetData
.end()) {
947 // Don't bother if send buffer is too full to respond anyway
948 if (pfrom
->fPauseSend
)
951 const CInv
&inv
= *it
;
953 if (interruptMsgProc
)
958 if (inv
.type
== MSG_BLOCK
|| inv
.type
== MSG_FILTERED_BLOCK
|| inv
.type
== MSG_CMPCT_BLOCK
|| inv
.type
== MSG_WITNESS_BLOCK
)
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
980 CValidationState dummy
;
981 ActivateBestChain(dummy
, Params(), a_recent_block
);
983 if (chainActive
.Contains(mi
->second
)) {
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
);
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());
1006 pfrom
->fDisconnect
= true;
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
;
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
]));
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
));
1065 CBlockHeaderAndShortTxIDs
cmpctblock(*pblock
, fPeerWantsWitness
);
1066 connman
->PushMessage(pfrom
, msgMaker
.Make(nSendFlags
, NetMsgType::CMPCTBLOCK
, cmpctblock
));
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
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
));
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
));
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
)
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
;
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()) {
1144 Misbehaving(pfrom
->GetId(), 100);
1145 LogPrintf("Peer %d sent us a getblocktxn with out-of-bounds tx indices", pfrom
->GetId());
1148 resp
.txn
[i
] = block
.vtx
[req
.indexes
[i
]];
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");
1166 if (!(pfrom
->GetLocalServices() & NODE_BLOOM
) &&
1167 (strCommand
== NetMsgType::FILTERLOAD
||
1168 strCommand
== NetMsgType::FILTERADD
))
1170 if (pfrom
->nVersion
>= NO_BLOOM_VERSION
) {
1172 Misbehaving(pfrom
->GetId(), 100);
1175 pfrom
->fDisconnect
= true;
1180 if (strCommand
== NetMsgType::REJECT
)
1182 if (LogAcceptCategory(BCLog::NET
)) {
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
)
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")));
1211 Misbehaving(pfrom
->GetId(), 1);
1218 uint64_t nNonce
= 1;
1219 uint64_t nServiceInt
;
1220 ServiceFlags nServices
;
1223 std::string strSubVer
;
1224 std::string cleanSubVer
;
1225 int nStartingHeight
= -1;
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;
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;
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;
1265 if (nVersion
== 10300)
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
;
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;
1286 if (pfrom
->fInbound
&& addrMe
.IsRoutable())
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
1312 pfrom
->SetSendVersion(nSendVersion
);
1313 pfrom
->nVersion
= nVersion
;
1315 if((nServices
& NODE_WITNESS
))
1318 State(pfrom
->GetId())->fHaveWitness
= true;
1321 // Potentially mark this peer as a preferred download peer.
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
)) {
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
;
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(),
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;
1382 else if (pfrom
->nVersion
== 0)
1384 // Must have a version message before anything else
1386 Misbehaving(pfrom
->GetId(), 1);
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.
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
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
1430 Misbehaving(pfrom
->GetId(), 1);
1434 else if (strCommand
== NetMsgType::ADDR
)
1436 std::vector
<CAddress
> vAddr
;
1439 // Don't want addr from older versions unless seeding
1440 if (pfrom
->nVersion
< CADDR_TIME_VERSION
&& connman
->GetAddressCount() > 1000)
1442 if (vAddr
.size() > 1000)
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
)
1458 if ((addr
.nServices
& REQUIRED_SERVICES
) != REQUIRED_SERVICES
)
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
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
)
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)) {
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);
1505 State(pfrom
->GetId())->fSupportsDesiredCmpctVersion
= (nCMPCTBLOCKVersion
== 1);
1511 else if (strCommand
== NetMsgType::INV
)
1513 std::vector
<CInv
> vInv
;
1515 if (vInv
.size() > MAX_INV_SZ
)
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;
1530 uint32_t nFetchFlags
= GetFetchFlags(pfrom
);
1532 for (CInv
&inv
: vInv
)
1534 if (interruptMsgProc
)
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());
1558 pfrom
->AddInventoryKnown(inv
);
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()) {
1566 // Track requests for our stuff
1567 GetMainSignals().Inventory(inv
.hash
);
1572 else if (strCommand
== NetMsgType::GETDATA
)
1574 std::vector
<CInv
> vInv
;
1576 if (vInv
.size() > MAX_INV_SZ
)
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
;
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
);
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
1624 pindex
= chainActive
.Next(pindex
);
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());
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());
1642 pfrom
->PushInventory(CInv(MSG_BLOCK
, pindex
->GetBlockHash()));
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();
1655 else if (strCommand
== NetMsgType::GETBLOCKTXN
)
1657 BlockTransactionsRequest 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
1668 SendBlockTransactions(*recent_block
, req
, pfrom
, connman
);
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());
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
);
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
);
1698 bool ret
= ReadBlockFromDisk(block
, it
->second
, chainparams
.GetConsensus());
1701 SendBlockTransactions(block
, req
, pfrom
, connman
);
1705 else if (strCommand
== NetMsgType::GETHEADERS
)
1707 CBlockLocator locator
;
1709 vRecv
>> locator
>> hashStop
;
1712 if (IsInitialBlockDownload() && !pfrom
->fWhitelisted
) {
1713 LogPrint(BCLog::NET
, "Ignoring getheaders from peer=%d because node is in initial block download\n", pfrom
->GetId());
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())
1725 pindex
= (*mi
).second
;
1729 // Find the last block the caller has in the main chain
1730 pindex
= FindForkInGlobalIndex(chainActive
, locator
);
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
)
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());
1772 std::deque
<COutPoint
> vWorkQueue
;
1773 std::vector
<uint256
> vEraseQueue
;
1774 CTransactionRef ptx
;
1776 const CTransaction
& tx
= *ptx
;
1778 CInv
inv(MSG_TX
, tx
.GetHash());
1779 pfrom
->AddInventoryKnown(inv
);
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
) && AcceptToMemoryPool(mempool
, state
, ptx
, true, &fMissingInputs
, &lRemovedTxn
)) {
1792 mempool
.check(pcoinsTip
);
1793 RelayTransaction(tx
, connman
);
1794 for (unsigned int i
= 0; i
< tx
.vout
.size(); i
++) {
1795 vWorkQueue
.emplace_back(inv
.hash
, i
);
1798 pfrom
->nLastTXTime
= GetTime();
1800 LogPrint(BCLog::MEMPOOL
, "AcceptToMemoryPool: peer=%d: accepted %s (poolsz %u txn, %u kB)\n",
1802 tx
.GetHash().ToString(),
1803 mempool
.size(), mempool
.DynamicMemoryUsage() / 1000);
1805 // Recursively process any orphan transactions that depended on this one
1806 std::set
<NodeId
> setMisbehaving
;
1807 while (!vWorkQueue
.empty()) {
1808 auto itByPrev
= mapOrphanTransactionsByPrev
.find(vWorkQueue
.front());
1809 vWorkQueue
.pop_front();
1810 if (itByPrev
== mapOrphanTransactionsByPrev
.end())
1812 for (auto mi
= itByPrev
->second
.begin();
1813 mi
!= itByPrev
->second
.end();
1816 const CTransactionRef
& porphanTx
= (*mi
)->second
.tx
;
1817 const CTransaction
& orphanTx
= *porphanTx
;
1818 const uint256
& orphanHash
= orphanTx
.GetHash();
1819 NodeId fromPeer
= (*mi
)->second
.fromPeer
;
1820 bool fMissingInputs2
= false;
1821 // Use a dummy CValidationState so someone can't setup nodes to counter-DoS based on orphan
1822 // resolution (that is, feeding people an invalid transaction based on LegitTxX in order to get
1823 // anyone relaying LegitTxX banned)
1824 CValidationState stateDummy
;
1827 if (setMisbehaving
.count(fromPeer
))
1829 if (AcceptToMemoryPool(mempool
, stateDummy
, porphanTx
, true, &fMissingInputs2
, &lRemovedTxn
)) {
1830 LogPrint(BCLog::MEMPOOL
, " accepted orphan tx %s\n", orphanHash
.ToString());
1831 RelayTransaction(orphanTx
, connman
);
1832 for (unsigned int i
= 0; i
< orphanTx
.vout
.size(); i
++) {
1833 vWorkQueue
.emplace_back(orphanHash
, i
);
1835 vEraseQueue
.push_back(orphanHash
);
1837 else if (!fMissingInputs2
)
1840 if (stateDummy
.IsInvalid(nDos
) && nDos
> 0)
1842 // Punish peer that gave us an invalid orphan tx
1843 Misbehaving(fromPeer
, nDos
);
1844 setMisbehaving
.insert(fromPeer
);
1845 LogPrint(BCLog::MEMPOOL
, " invalid orphan tx %s\n", orphanHash
.ToString());
1847 // Has inputs but not accepted to mempool
1848 // Probably non-standard or insufficient fee
1849 LogPrint(BCLog::MEMPOOL
, " removed orphan tx %s\n", orphanHash
.ToString());
1850 vEraseQueue
.push_back(orphanHash
);
1851 if (!orphanTx
.HasWitness() && !stateDummy
.CorruptionPossible()) {
1852 // Do not use rejection cache for witness transactions or
1853 // witness-stripped transactions, as they can have been malleated.
1854 // See https://github.com/bitcoin/bitcoin/issues/8279 for details.
1855 assert(recentRejects
);
1856 recentRejects
->insert(orphanHash
);
1859 mempool
.check(pcoinsTip
);
1863 for (uint256 hash
: vEraseQueue
)
1864 EraseOrphanTx(hash
);
1866 else if (fMissingInputs
)
1868 bool fRejectedParents
= false; // It may be the case that the orphans parents have all been rejected
1869 for (const CTxIn
& txin
: tx
.vin
) {
1870 if (recentRejects
->contains(txin
.prevout
.hash
)) {
1871 fRejectedParents
= true;
1875 if (!fRejectedParents
) {
1876 uint32_t nFetchFlags
= GetFetchFlags(pfrom
);
1877 for (const CTxIn
& txin
: tx
.vin
) {
1878 CInv
_inv(MSG_TX
| nFetchFlags
, txin
.prevout
.hash
);
1879 pfrom
->AddInventoryKnown(_inv
);
1880 if (!AlreadyHave(_inv
)) pfrom
->AskFor(_inv
);
1882 AddOrphanTx(ptx
, pfrom
->GetId());
1884 // DoS prevention: do not allow mapOrphanTransactions to grow unbounded
1885 unsigned int nMaxOrphanTx
= (unsigned int)std::max((int64_t)0, gArgs
.GetArg("-maxorphantx", DEFAULT_MAX_ORPHAN_TRANSACTIONS
));
1886 unsigned int nEvicted
= LimitOrphanTxSize(nMaxOrphanTx
);
1888 LogPrint(BCLog::MEMPOOL
, "mapOrphan overflow, removed %u tx\n", nEvicted
);
1891 LogPrint(BCLog::MEMPOOL
, "not keeping orphan with rejected parents %s\n",tx
.GetHash().ToString());
1892 // We will continue to reject this tx since it has rejected
1893 // parents so avoid re-requesting it from other peers.
1894 recentRejects
->insert(tx
.GetHash());
1897 if (!tx
.HasWitness() && !state
.CorruptionPossible()) {
1898 // Do not use rejection cache for witness transactions or
1899 // witness-stripped transactions, as they can have been malleated.
1900 // See https://github.com/bitcoin/bitcoin/issues/8279 for details.
1901 assert(recentRejects
);
1902 recentRejects
->insert(tx
.GetHash());
1903 if (RecursiveDynamicUsage(*ptx
) < 100000) {
1904 AddToCompactExtraTransactions(ptx
);
1906 } else if (tx
.HasWitness() && RecursiveDynamicUsage(*ptx
) < 100000) {
1907 AddToCompactExtraTransactions(ptx
);
1910 if (pfrom
->fWhitelisted
&& gArgs
.GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY
)) {
1911 // Always relay transactions received from whitelisted peers, even
1912 // if they were already in the mempool or rejected from it due
1913 // to policy, allowing the node to function as a gateway for
1914 // nodes hidden behind it.
1916 // Never relay transactions that we would assign a non-zero DoS
1917 // score for, as we expect peers to do the same with us in that
1920 if (!state
.IsInvalid(nDoS
) || nDoS
== 0) {
1921 LogPrintf("Force relaying tx %s from whitelisted peer=%d\n", tx
.GetHash().ToString(), pfrom
->GetId());
1922 RelayTransaction(tx
, connman
);
1924 LogPrintf("Not relaying invalid transaction %s from whitelisted peer=%d (%s)\n", tx
.GetHash().ToString(), pfrom
->GetId(), FormatStateMessage(state
));
1929 for (const CTransactionRef
& removedTx
: lRemovedTxn
)
1930 AddToCompactExtraTransactions(removedTx
);
1933 if (state
.IsInvalid(nDoS
))
1935 LogPrint(BCLog::MEMPOOLREJ
, "%s from peer=%d was not accepted: %s\n", tx
.GetHash().ToString(),
1937 FormatStateMessage(state
));
1938 if (state
.GetRejectCode() > 0 && state
.GetRejectCode() < REJECT_INTERNAL
) // Never send AcceptToMemoryPool's internal codes over P2P
1939 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::REJECT
, strCommand
, (unsigned char)state
.GetRejectCode(),
1940 state
.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH
), inv
.hash
));
1942 Misbehaving(pfrom
->GetId(), nDoS
);
1948 else if (strCommand
== NetMsgType::CMPCTBLOCK
&& !fImporting
&& !fReindex
) // Ignore blocks received while importing
1950 CBlockHeaderAndShortTxIDs cmpctblock
;
1951 vRecv
>> cmpctblock
;
1956 if (mapBlockIndex
.find(cmpctblock
.header
.hashPrevBlock
) == mapBlockIndex
.end()) {
1957 // Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
1958 if (!IsInitialBlockDownload())
1959 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETHEADERS
, chainActive
.GetLocator(pindexBestHeader
), uint256()));
1964 const CBlockIndex
*pindex
= nullptr;
1965 CValidationState state
;
1966 if (!ProcessNewBlockHeaders({cmpctblock
.header
}, state
, chainparams
, &pindex
)) {
1968 if (state
.IsInvalid(nDoS
)) {
1971 Misbehaving(pfrom
->GetId(), nDoS
);
1973 LogPrintf("Peer %d sent us invalid header via cmpctblock\n", pfrom
->GetId());
1978 // When we succeed in decoding a block's txids from a cmpctblock
1979 // message we typically jump to the BLOCKTXN handling code, with a
1980 // dummy (empty) BLOCKTXN message, to re-use the logic there in
1981 // completing processing of the putative block (without cs_main).
1982 bool fProcessBLOCKTXN
= false;
1983 CDataStream
blockTxnMsg(SER_NETWORK
, PROTOCOL_VERSION
);
1985 // If we end up treating this as a plain headers message, call that as well
1987 bool fRevertToHeaderProcessing
= false;
1988 CDataStream
vHeadersMsg(SER_NETWORK
, PROTOCOL_VERSION
);
1990 // Keep a CBlock for "optimistic" compactblock reconstructions (see
1992 std::shared_ptr
<CBlock
> pblock
= std::make_shared
<CBlock
>();
1993 bool fBlockReconstructed
= false;
1997 // If AcceptBlockHeader returned true, it set pindex
1999 UpdateBlockAvailability(pfrom
->GetId(), pindex
->GetBlockHash());
2001 std::map
<uint256
, std::pair
<NodeId
, std::list
<QueuedBlock
>::iterator
> >::iterator blockInFlightIt
= mapBlocksInFlight
.find(pindex
->GetBlockHash());
2002 bool fAlreadyInFlight
= blockInFlightIt
!= mapBlocksInFlight
.end();
2004 if (pindex
->nStatus
& BLOCK_HAVE_DATA
) // Nothing to do here
2007 if (pindex
->nChainWork
<= chainActive
.Tip()->nChainWork
|| // We know something better
2008 pindex
->nTx
!= 0) { // We had this block at some point, but pruned it
2009 if (fAlreadyInFlight
) {
2010 // We requested this block for some reason, but our mempool will probably be useless
2011 // so we just grab the block via normal getdata
2012 std::vector
<CInv
> vInv(1);
2013 vInv
[0] = CInv(MSG_BLOCK
| GetFetchFlags(pfrom
), cmpctblock
.header
.GetHash());
2014 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETDATA
, vInv
));
2019 // If we're not close to tip yet, give up and let parallel block fetch work its magic
2020 if (!fAlreadyInFlight
&& !CanDirectFetch(chainparams
.GetConsensus()))
2023 CNodeState
*nodestate
= State(pfrom
->GetId());
2025 if (IsWitnessEnabled(pindex
->pprev
, chainparams
.GetConsensus()) && !nodestate
->fSupportsDesiredCmpctVersion
) {
2026 // Don't bother trying to process compact blocks from v1 peers
2027 // after segwit activates.
2031 // We want to be a bit conservative just to be extra careful about DoS
2032 // possibilities in compact block processing...
2033 if (pindex
->nHeight
<= chainActive
.Height() + 2) {
2034 if ((!fAlreadyInFlight
&& nodestate
->nBlocksInFlight
< MAX_BLOCKS_IN_TRANSIT_PER_PEER
) ||
2035 (fAlreadyInFlight
&& blockInFlightIt
->second
.first
== pfrom
->GetId())) {
2036 std::list
<QueuedBlock
>::iterator
* queuedBlockIt
= nullptr;
2037 if (!MarkBlockAsInFlight(pfrom
->GetId(), pindex
->GetBlockHash(), pindex
, &queuedBlockIt
)) {
2038 if (!(*queuedBlockIt
)->partialBlock
)
2039 (*queuedBlockIt
)->partialBlock
.reset(new PartiallyDownloadedBlock(&mempool
));
2041 // The block was already in flight using compact blocks from the same peer
2042 LogPrint(BCLog::NET
, "Peer sent us compact block we were already syncing!\n");
2047 PartiallyDownloadedBlock
& partialBlock
= *(*queuedBlockIt
)->partialBlock
;
2048 ReadStatus status
= partialBlock
.InitData(cmpctblock
, vExtraTxnForCompact
);
2049 if (status
== READ_STATUS_INVALID
) {
2050 MarkBlockAsReceived(pindex
->GetBlockHash()); // Reset in-flight state in case of whitelist
2051 Misbehaving(pfrom
->GetId(), 100);
2052 LogPrintf("Peer %d sent us invalid compact block\n", pfrom
->GetId());
2054 } else if (status
== READ_STATUS_FAILED
) {
2055 // Duplicate txindexes, the block is now in-flight, so just request it
2056 std::vector
<CInv
> vInv(1);
2057 vInv
[0] = CInv(MSG_BLOCK
| GetFetchFlags(pfrom
), cmpctblock
.header
.GetHash());
2058 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETDATA
, vInv
));
2062 BlockTransactionsRequest req
;
2063 for (size_t i
= 0; i
< cmpctblock
.BlockTxCount(); i
++) {
2064 if (!partialBlock
.IsTxAvailable(i
))
2065 req
.indexes
.push_back(i
);
2067 if (req
.indexes
.empty()) {
2068 // Dirty hack to jump to BLOCKTXN code (TODO: move message handling into their own functions)
2069 BlockTransactions txn
;
2070 txn
.blockhash
= cmpctblock
.header
.GetHash();
2072 fProcessBLOCKTXN
= true;
2074 req
.blockhash
= pindex
->GetBlockHash();
2075 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETBLOCKTXN
, req
));
2078 // This block is either already in flight from a different
2079 // peer, or this peer has too many blocks outstanding to
2081 // Optimistically try to reconstruct anyway since we might be
2082 // able to without any round trips.
2083 PartiallyDownloadedBlock
tempBlock(&mempool
);
2084 ReadStatus status
= tempBlock
.InitData(cmpctblock
, vExtraTxnForCompact
);
2085 if (status
!= READ_STATUS_OK
) {
2086 // TODO: don't ignore failures
2089 std::vector
<CTransactionRef
> dummy
;
2090 status
= tempBlock
.FillBlock(*pblock
, dummy
);
2091 if (status
== READ_STATUS_OK
) {
2092 fBlockReconstructed
= true;
2096 if (fAlreadyInFlight
) {
2097 // We requested this block, but its far into the future, so our
2098 // mempool will probably be useless - request the block normally
2099 std::vector
<CInv
> vInv(1);
2100 vInv
[0] = CInv(MSG_BLOCK
| GetFetchFlags(pfrom
), cmpctblock
.header
.GetHash());
2101 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETDATA
, vInv
));
2104 // If this was an announce-cmpctblock, we want the same treatment as a header message
2105 // Dirty hack to process as if it were just a headers message (TODO: move message handling into their own functions)
2106 std::vector
<CBlock
> headers
;
2107 headers
.push_back(cmpctblock
.header
);
2108 vHeadersMsg
<< headers
;
2109 fRevertToHeaderProcessing
= true;
2114 if (fProcessBLOCKTXN
)
2115 return ProcessMessage(pfrom
, NetMsgType::BLOCKTXN
, blockTxnMsg
, nTimeReceived
, chainparams
, connman
, interruptMsgProc
);
2117 if (fRevertToHeaderProcessing
)
2118 return ProcessMessage(pfrom
, NetMsgType::HEADERS
, vHeadersMsg
, nTimeReceived
, chainparams
, connman
, interruptMsgProc
);
2120 if (fBlockReconstructed
) {
2121 // If we got here, we were able to optimistically reconstruct a
2122 // block that is in flight from some other peer.
2125 mapBlockSource
.emplace(pblock
->GetHash(), std::make_pair(pfrom
->GetId(), false));
2127 bool fNewBlock
= false;
2128 ProcessNewBlock(chainparams
, pblock
, true, &fNewBlock
);
2130 pfrom
->nLastBlockTime
= GetTime();
2133 mapBlockSource
.erase(pblock
->GetHash());
2135 LOCK(cs_main
); // hold cs_main for CBlockIndex::IsValid()
2136 if (pindex
->IsValid(BLOCK_VALID_TRANSACTIONS
)) {
2137 // Clear download state for this block, which is in
2138 // process from some other peer. We do this after calling
2139 // ProcessNewBlock so that a malleated cmpctblock announcement
2140 // can't be used to interfere with block relay.
2141 MarkBlockAsReceived(pblock
->GetHash());
2147 else if (strCommand
== NetMsgType::BLOCKTXN
&& !fImporting
&& !fReindex
) // Ignore blocks received while importing
2149 BlockTransactions resp
;
2152 std::shared_ptr
<CBlock
> pblock
= std::make_shared
<CBlock
>();
2153 bool fBlockRead
= false;
2157 std::map
<uint256
, std::pair
<NodeId
, std::list
<QueuedBlock
>::iterator
> >::iterator it
= mapBlocksInFlight
.find(resp
.blockhash
);
2158 if (it
== mapBlocksInFlight
.end() || !it
->second
.second
->partialBlock
||
2159 it
->second
.first
!= pfrom
->GetId()) {
2160 LogPrint(BCLog::NET
, "Peer %d sent us block transactions for block we weren't expecting\n", pfrom
->GetId());
2164 PartiallyDownloadedBlock
& partialBlock
= *it
->second
.second
->partialBlock
;
2165 ReadStatus status
= partialBlock
.FillBlock(*pblock
, resp
.txn
);
2166 if (status
== READ_STATUS_INVALID
) {
2167 MarkBlockAsReceived(resp
.blockhash
); // Reset in-flight state in case of whitelist
2168 Misbehaving(pfrom
->GetId(), 100);
2169 LogPrintf("Peer %d sent us invalid compact block/non-matching block transactions\n", pfrom
->GetId());
2171 } else if (status
== READ_STATUS_FAILED
) {
2172 // Might have collided, fall back to getdata now :(
2173 std::vector
<CInv
> invs
;
2174 invs
.push_back(CInv(MSG_BLOCK
| GetFetchFlags(pfrom
), resp
.blockhash
));
2175 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETDATA
, invs
));
2177 // Block is either okay, or possibly we received
2178 // READ_STATUS_CHECKBLOCK_FAILED.
2179 // Note that CheckBlock can only fail for one of a few reasons:
2180 // 1. bad-proof-of-work (impossible here, because we've already
2181 // accepted the header)
2182 // 2. merkleroot doesn't match the transactions given (already
2183 // caught in FillBlock with READ_STATUS_FAILED, so
2185 // 3. the block is otherwise invalid (eg invalid coinbase,
2186 // block is too big, too many legacy sigops, etc).
2187 // So if CheckBlock failed, #3 is the only possibility.
2188 // Under BIP 152, we don't DoS-ban unless proof of work is
2189 // invalid (we don't require all the stateless checks to have
2190 // been run). This is handled below, so just treat this as
2191 // though the block was successfully read, and rely on the
2192 // handling in ProcessNewBlock to ensure the block index is
2193 // updated, reject messages go out, etc.
2194 MarkBlockAsReceived(resp
.blockhash
); // it is now an empty pointer
2196 // mapBlockSource is only used for sending reject messages and DoS scores,
2197 // so the race between here and cs_main in ProcessNewBlock is fine.
2198 // BIP 152 permits peers to relay compact blocks after validating
2199 // the header only; we should not punish peers if the block turns
2200 // out to be invalid.
2201 mapBlockSource
.emplace(resp
.blockhash
, std::make_pair(pfrom
->GetId(), false));
2203 } // Don't hold cs_main when we call into ProcessNewBlock
2205 bool fNewBlock
= false;
2206 // Since we requested this block (it was in mapBlocksInFlight), force it to be processed,
2207 // even if it would not be a candidate for new tip (missing previous block, chain not long enough, etc)
2208 ProcessNewBlock(chainparams
, pblock
, true, &fNewBlock
);
2210 pfrom
->nLastBlockTime
= GetTime();
2213 mapBlockSource
.erase(pblock
->GetHash());
2219 else if (strCommand
== NetMsgType::HEADERS
&& !fImporting
&& !fReindex
) // Ignore headers received while importing
2221 std::vector
<CBlockHeader
> headers
;
2223 // Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks.
2224 unsigned int nCount
= ReadCompactSize(vRecv
);
2225 if (nCount
> MAX_HEADERS_RESULTS
) {
2227 Misbehaving(pfrom
->GetId(), 20);
2228 return error("headers message size = %u", nCount
);
2230 headers
.resize(nCount
);
2231 for (unsigned int n
= 0; n
< nCount
; n
++) {
2232 vRecv
>> headers
[n
];
2233 ReadCompactSize(vRecv
); // ignore tx count; assume it is 0.
2237 // Nothing interesting. Stop asking this peers for more headers.
2241 const CBlockIndex
*pindexLast
= nullptr;
2244 CNodeState
*nodestate
= State(pfrom
->GetId());
2246 // If this looks like it could be a block announcement (nCount <
2247 // MAX_BLOCKS_TO_ANNOUNCE), use special logic for handling headers that
2249 // - Send a getheaders message in response to try to connect the chain.
2250 // - The peer can send up to MAX_UNCONNECTING_HEADERS in a row that
2251 // don't connect before giving DoS points
2252 // - Once a headers message is received that is valid and does connect,
2253 // nUnconnectingHeaders gets reset back to 0.
2254 if (mapBlockIndex
.find(headers
[0].hashPrevBlock
) == mapBlockIndex
.end() && nCount
< MAX_BLOCKS_TO_ANNOUNCE
) {
2255 nodestate
->nUnconnectingHeaders
++;
2256 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETHEADERS
, chainActive
.GetLocator(pindexBestHeader
), uint256()));
2257 LogPrint(BCLog::NET
, "received header %s: missing prev block %s, sending getheaders (%d) to end (peer=%d, nUnconnectingHeaders=%d)\n",
2258 headers
[0].GetHash().ToString(),
2259 headers
[0].hashPrevBlock
.ToString(),
2260 pindexBestHeader
->nHeight
,
2261 pfrom
->GetId(), nodestate
->nUnconnectingHeaders
);
2262 // Set hashLastUnknownBlock for this peer, so that if we
2263 // eventually get the headers - even from a different peer -
2264 // we can use this peer to download.
2265 UpdateBlockAvailability(pfrom
->GetId(), headers
.back().GetHash());
2267 if (nodestate
->nUnconnectingHeaders
% MAX_UNCONNECTING_HEADERS
== 0) {
2268 Misbehaving(pfrom
->GetId(), 20);
2273 uint256 hashLastBlock
;
2274 for (const CBlockHeader
& header
: headers
) {
2275 if (!hashLastBlock
.IsNull() && header
.hashPrevBlock
!= hashLastBlock
) {
2276 Misbehaving(pfrom
->GetId(), 20);
2277 return error("non-continuous headers sequence");
2279 hashLastBlock
= header
.GetHash();
2283 CValidationState state
;
2284 if (!ProcessNewBlockHeaders(headers
, state
, chainparams
, &pindexLast
)) {
2286 if (state
.IsInvalid(nDoS
)) {
2289 Misbehaving(pfrom
->GetId(), nDoS
);
2291 return error("invalid header received");
2297 CNodeState
*nodestate
= State(pfrom
->GetId());
2298 if (nodestate
->nUnconnectingHeaders
> 0) {
2299 LogPrint(BCLog::NET
, "peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n", pfrom
->GetId(), nodestate
->nUnconnectingHeaders
);
2301 nodestate
->nUnconnectingHeaders
= 0;
2304 UpdateBlockAvailability(pfrom
->GetId(), pindexLast
->GetBlockHash());
2306 if (nCount
== MAX_HEADERS_RESULTS
) {
2307 // Headers message had its maximum size; the peer may have more headers.
2308 // TODO: optimize: if pindexLast is an ancestor of chainActive.Tip or pindexBestHeader, continue
2309 // from there instead.
2310 LogPrint(BCLog::NET
, "more getheaders (%d) to end to peer=%d (startheight:%d)\n", pindexLast
->nHeight
, pfrom
->GetId(), pfrom
->nStartingHeight
);
2311 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETHEADERS
, chainActive
.GetLocator(pindexLast
), uint256()));
2314 bool fCanDirectFetch
= CanDirectFetch(chainparams
.GetConsensus());
2315 // If this set of headers is valid and ends in a block with at least as
2316 // much work as our tip, download as much as possible.
2317 if (fCanDirectFetch
&& pindexLast
->IsValid(BLOCK_VALID_TREE
) && chainActive
.Tip()->nChainWork
<= pindexLast
->nChainWork
) {
2318 std::vector
<const CBlockIndex
*> vToFetch
;
2319 const CBlockIndex
*pindexWalk
= pindexLast
;
2320 // Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
2321 while (pindexWalk
&& !chainActive
.Contains(pindexWalk
) && vToFetch
.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER
) {
2322 if (!(pindexWalk
->nStatus
& BLOCK_HAVE_DATA
) &&
2323 !mapBlocksInFlight
.count(pindexWalk
->GetBlockHash()) &&
2324 (!IsWitnessEnabled(pindexWalk
->pprev
, chainparams
.GetConsensus()) || State(pfrom
->GetId())->fHaveWitness
)) {
2325 // We don't have this block, and it's not yet in flight.
2326 vToFetch
.push_back(pindexWalk
);
2328 pindexWalk
= pindexWalk
->pprev
;
2330 // If pindexWalk still isn't on our main chain, we're looking at a
2331 // very large reorg at a time we think we're close to caught up to
2332 // the main chain -- this shouldn't really happen. Bail out on the
2333 // direct fetch and rely on parallel download instead.
2334 if (!chainActive
.Contains(pindexWalk
)) {
2335 LogPrint(BCLog::NET
, "Large reorg, won't direct fetch to %s (%d)\n",
2336 pindexLast
->GetBlockHash().ToString(),
2337 pindexLast
->nHeight
);
2339 std::vector
<CInv
> vGetData
;
2340 // Download as much as possible, from earliest to latest.
2341 for (const CBlockIndex
*pindex
: reverse_iterate(vToFetch
)) {
2342 if (nodestate
->nBlocksInFlight
>= MAX_BLOCKS_IN_TRANSIT_PER_PEER
) {
2343 // Can't download any more from this peer
2346 uint32_t nFetchFlags
= GetFetchFlags(pfrom
);
2347 vGetData
.push_back(CInv(MSG_BLOCK
| nFetchFlags
, pindex
->GetBlockHash()));
2348 MarkBlockAsInFlight(pfrom
->GetId(), pindex
->GetBlockHash(), pindex
);
2349 LogPrint(BCLog::NET
, "Requesting block %s from peer=%d\n",
2350 pindex
->GetBlockHash().ToString(), pfrom
->GetId());
2352 if (vGetData
.size() > 1) {
2353 LogPrint(BCLog::NET
, "Downloading blocks toward %s (%d) via headers direct fetch\n",
2354 pindexLast
->GetBlockHash().ToString(), pindexLast
->nHeight
);
2356 if (vGetData
.size() > 0) {
2357 if (nodestate
->fSupportsDesiredCmpctVersion
&& vGetData
.size() == 1 && mapBlocksInFlight
.size() == 1 && pindexLast
->pprev
->IsValid(BLOCK_VALID_CHAIN
)) {
2358 // In any case, we want to download using a compact block, not a regular one
2359 vGetData
[0] = CInv(MSG_CMPCT_BLOCK
, vGetData
[0].hash
);
2361 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETDATA
, vGetData
));
2368 else if (strCommand
== NetMsgType::BLOCK
&& !fImporting
&& !fReindex
) // Ignore blocks received while importing
2370 std::shared_ptr
<CBlock
> pblock
= std::make_shared
<CBlock
>();
2373 LogPrint(BCLog::NET
, "received block %s peer=%d\n", pblock
->GetHash().ToString(), pfrom
->GetId());
2375 // Process all blocks from whitelisted peers, even if not requested,
2376 // unless we're still syncing with the network.
2377 // Such an unrequested block may still be processed, subject to the
2378 // conditions in AcceptBlock().
2379 bool forceProcessing
= pfrom
->fWhitelisted
&& !IsInitialBlockDownload();
2380 const uint256
hash(pblock
->GetHash());
2383 // Also always process if we requested the block explicitly, as we may
2384 // need it even though it is not a candidate for a new best tip.
2385 forceProcessing
|= MarkBlockAsReceived(hash
);
2386 // mapBlockSource is only used for sending reject messages and DoS scores,
2387 // so the race between here and cs_main in ProcessNewBlock is fine.
2388 mapBlockSource
.emplace(hash
, std::make_pair(pfrom
->GetId(), true));
2390 bool fNewBlock
= false;
2391 ProcessNewBlock(chainparams
, pblock
, forceProcessing
, &fNewBlock
);
2393 pfrom
->nLastBlockTime
= GetTime();
2396 mapBlockSource
.erase(pblock
->GetHash());
2401 else if (strCommand
== NetMsgType::GETADDR
)
2403 // This asymmetric behavior for inbound and outbound connections was introduced
2404 // to prevent a fingerprinting attack: an attacker can send specific fake addresses
2405 // to users' AddrMan and later request them by sending getaddr messages.
2406 // Making nodes which are behind NAT and can only make outgoing connections ignore
2407 // the getaddr message mitigates the attack.
2408 if (!pfrom
->fInbound
) {
2409 LogPrint(BCLog::NET
, "Ignoring \"getaddr\" from outbound connection. peer=%d\n", pfrom
->GetId());
2413 // Only send one GetAddr response per connection to reduce resource waste
2414 // and discourage addr stamping of INV announcements.
2415 if (pfrom
->fSentAddr
) {
2416 LogPrint(BCLog::NET
, "Ignoring repeated \"getaddr\". peer=%d\n", pfrom
->GetId());
2419 pfrom
->fSentAddr
= true;
2421 pfrom
->vAddrToSend
.clear();
2422 std::vector
<CAddress
> vAddr
= connman
->GetAddresses();
2423 FastRandomContext insecure_rand
;
2424 for (const CAddress
&addr
: vAddr
)
2425 pfrom
->PushAddress(addr
, insecure_rand
);
2429 else if (strCommand
== NetMsgType::MEMPOOL
)
2431 if (!(pfrom
->GetLocalServices() & NODE_BLOOM
) && !pfrom
->fWhitelisted
)
2433 LogPrint(BCLog::NET
, "mempool request with bloom filters disabled, disconnect peer=%d\n", pfrom
->GetId());
2434 pfrom
->fDisconnect
= true;
2438 if (connman
->OutboundTargetReached(false) && !pfrom
->fWhitelisted
)
2440 LogPrint(BCLog::NET
, "mempool request with bandwidth limit reached, disconnect peer=%d\n", pfrom
->GetId());
2441 pfrom
->fDisconnect
= true;
2445 LOCK(pfrom
->cs_inventory
);
2446 pfrom
->fSendMempool
= true;
2450 else if (strCommand
== NetMsgType::PING
)
2452 if (pfrom
->nVersion
> BIP0031_VERSION
)
2456 // Echo the message back with the nonce. This allows for two useful features:
2458 // 1) A remote node can quickly check if the connection is operational
2459 // 2) Remote nodes can measure the latency of the network thread. If this node
2460 // is overloaded it won't respond to pings quickly and the remote node can
2461 // avoid sending us more work, like chain download requests.
2463 // The nonce stops the remote getting confused between different pings: without
2464 // it, if the remote node sends a ping once per second and this node takes 5
2465 // seconds to respond to each, the 5th ping the remote sends would appear to
2466 // return very quickly.
2467 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::PONG
, nonce
));
2472 else if (strCommand
== NetMsgType::PONG
)
2474 int64_t pingUsecEnd
= nTimeReceived
;
2476 size_t nAvail
= vRecv
.in_avail();
2477 bool bPingFinished
= false;
2478 std::string sProblem
;
2480 if (nAvail
>= sizeof(nonce
)) {
2483 // Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
2484 if (pfrom
->nPingNonceSent
!= 0) {
2485 if (nonce
== pfrom
->nPingNonceSent
) {
2486 // Matching pong received, this ping is no longer outstanding
2487 bPingFinished
= true;
2488 int64_t pingUsecTime
= pingUsecEnd
- pfrom
->nPingUsecStart
;
2489 if (pingUsecTime
> 0) {
2490 // Successful ping time measurement, replace previous
2491 pfrom
->nPingUsecTime
= pingUsecTime
;
2492 pfrom
->nMinPingUsecTime
= std::min(pfrom
->nMinPingUsecTime
.load(), pingUsecTime
);
2494 // This should never happen
2495 sProblem
= "Timing mishap";
2498 // Nonce mismatches are normal when pings are overlapping
2499 sProblem
= "Nonce mismatch";
2501 // This is most likely a bug in another implementation somewhere; cancel this ping
2502 bPingFinished
= true;
2503 sProblem
= "Nonce zero";
2507 sProblem
= "Unsolicited pong without ping";
2510 // This is most likely a bug in another implementation somewhere; cancel this ping
2511 bPingFinished
= true;
2512 sProblem
= "Short payload";
2515 if (!(sProblem
.empty())) {
2516 LogPrint(BCLog::NET
, "pong peer=%d: %s, %x expected, %x received, %u bytes\n",
2519 pfrom
->nPingNonceSent
,
2523 if (bPingFinished
) {
2524 pfrom
->nPingNonceSent
= 0;
2529 else if (strCommand
== NetMsgType::FILTERLOAD
)
2531 CBloomFilter filter
;
2534 if (!filter
.IsWithinSizeConstraints())
2536 // There is no excuse for sending a too-large filter
2538 Misbehaving(pfrom
->GetId(), 100);
2542 LOCK(pfrom
->cs_filter
);
2543 delete pfrom
->pfilter
;
2544 pfrom
->pfilter
= new CBloomFilter(filter
);
2545 pfrom
->pfilter
->UpdateEmptyFull();
2546 pfrom
->fRelayTxes
= true;
2551 else if (strCommand
== NetMsgType::FILTERADD
)
2553 std::vector
<unsigned char> vData
;
2556 // Nodes must NEVER send a data item > 520 bytes (the max size for a script data object,
2557 // and thus, the maximum size any matched object can have) in a filteradd message
2559 if (vData
.size() > MAX_SCRIPT_ELEMENT_SIZE
) {
2562 LOCK(pfrom
->cs_filter
);
2563 if (pfrom
->pfilter
) {
2564 pfrom
->pfilter
->insert(vData
);
2571 Misbehaving(pfrom
->GetId(), 100);
2576 else if (strCommand
== NetMsgType::FILTERCLEAR
)
2578 LOCK(pfrom
->cs_filter
);
2579 if (pfrom
->GetLocalServices() & NODE_BLOOM
) {
2580 delete pfrom
->pfilter
;
2581 pfrom
->pfilter
= new CBloomFilter();
2583 pfrom
->fRelayTxes
= true;
2586 else if (strCommand
== NetMsgType::FEEFILTER
) {
2587 CAmount newFeeFilter
= 0;
2588 vRecv
>> newFeeFilter
;
2589 if (MoneyRange(newFeeFilter
)) {
2591 LOCK(pfrom
->cs_feeFilter
);
2592 pfrom
->minFeeFilter
= newFeeFilter
;
2594 LogPrint(BCLog::NET
, "received: feefilter of %s from peer=%d\n", CFeeRate(newFeeFilter
).ToString(), pfrom
->GetId());
2598 else if (strCommand
== NetMsgType::NOTFOUND
) {
2599 // We do not care about the NOTFOUND message, but logging an Unknown Command
2600 // message would be undesirable as we transmit it ourselves.
2604 // Ignore unknown commands for extensibility
2605 LogPrint(BCLog::NET
, "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand
), pfrom
->GetId());
2613 static bool SendRejectsAndCheckIfBanned(CNode
* pnode
, CConnman
* connman
)
2615 AssertLockHeld(cs_main
);
2616 CNodeState
&state
= *State(pnode
->GetId());
2618 for (const CBlockReject
& reject
: state
.rejects
) {
2619 connman
->PushMessage(pnode
, CNetMsgMaker(INIT_PROTO_VERSION
).Make(NetMsgType::REJECT
, (std::string
)NetMsgType::BLOCK
, reject
.chRejectCode
, reject
.strRejectReason
, reject
.hashBlock
));
2621 state
.rejects
.clear();
2623 if (state
.fShouldBan
) {
2624 state
.fShouldBan
= false;
2625 if (pnode
->fWhitelisted
)
2626 LogPrintf("Warning: not punishing whitelisted peer %s!\n", pnode
->addr
.ToString());
2627 else if (pnode
->fAddnode
)
2628 LogPrintf("Warning: not punishing addnoded peer %s!\n", pnode
->addr
.ToString());
2630 pnode
->fDisconnect
= true;
2631 if (pnode
->addr
.IsLocal())
2632 LogPrintf("Warning: not banning local peer %s!\n", pnode
->addr
.ToString());
2635 connman
->Ban(pnode
->addr
, BanReasonNodeMisbehaving
);
2643 bool PeerLogicValidation::ProcessMessages(CNode
* pfrom
, std::atomic
<bool>& interruptMsgProc
)
2645 const CChainParams
& chainparams
= Params();
2648 // (4) message start
2654 bool fMoreWork
= false;
2656 if (!pfrom
->vRecvGetData
.empty())
2657 ProcessGetData(pfrom
, chainparams
.GetConsensus(), connman
, interruptMsgProc
);
2659 if (pfrom
->fDisconnect
)
2662 // this maintains the order of responses
2663 if (!pfrom
->vRecvGetData
.empty()) return true;
2665 // Don't bother if send buffer is too full to respond anyway
2666 if (pfrom
->fPauseSend
)
2669 std::list
<CNetMessage
> msgs
;
2671 LOCK(pfrom
->cs_vProcessMsg
);
2672 if (pfrom
->vProcessMsg
.empty())
2674 // Just take one message
2675 msgs
.splice(msgs
.begin(), pfrom
->vProcessMsg
, pfrom
->vProcessMsg
.begin());
2676 pfrom
->nProcessQueueSize
-= msgs
.front().vRecv
.size() + CMessageHeader::HEADER_SIZE
;
2677 pfrom
->fPauseRecv
= pfrom
->nProcessQueueSize
> connman
->GetReceiveFloodSize();
2678 fMoreWork
= !pfrom
->vProcessMsg
.empty();
2680 CNetMessage
& msg(msgs
.front());
2682 msg
.SetVersion(pfrom
->GetRecvVersion());
2683 // Scan for message start
2684 if (memcmp(msg
.hdr
.pchMessageStart
, chainparams
.MessageStart(), CMessageHeader::MESSAGE_START_SIZE
) != 0) {
2685 LogPrintf("PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg
.hdr
.GetCommand()), pfrom
->GetId());
2686 pfrom
->fDisconnect
= true;
2691 CMessageHeader
& hdr
= msg
.hdr
;
2692 if (!hdr
.IsValid(chainparams
.MessageStart()))
2694 LogPrintf("PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(hdr
.GetCommand()), pfrom
->GetId());
2697 std::string strCommand
= hdr
.GetCommand();
2700 unsigned int nMessageSize
= hdr
.nMessageSize
;
2703 CDataStream
& vRecv
= msg
.vRecv
;
2704 const uint256
& hash
= msg
.GetMessageHash();
2705 if (memcmp(hash
.begin(), hdr
.pchChecksum
, CMessageHeader::CHECKSUM_SIZE
) != 0)
2707 LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s\n", __func__
,
2708 SanitizeString(strCommand
), nMessageSize
,
2709 HexStr(hash
.begin(), hash
.begin()+CMessageHeader::CHECKSUM_SIZE
),
2710 HexStr(hdr
.pchChecksum
, hdr
.pchChecksum
+CMessageHeader::CHECKSUM_SIZE
));
2718 fRet
= ProcessMessage(pfrom
, strCommand
, vRecv
, msg
.nTime
, chainparams
, connman
, interruptMsgProc
);
2719 if (interruptMsgProc
)
2721 if (!pfrom
->vRecvGetData
.empty())
2724 catch (const std::ios_base::failure
& e
)
2726 connman
->PushMessage(pfrom
, CNetMsgMaker(INIT_PROTO_VERSION
).Make(NetMsgType::REJECT
, strCommand
, REJECT_MALFORMED
, std::string("error parsing message")));
2727 if (strstr(e
.what(), "end of data"))
2729 // Allow exceptions from under-length message on vRecv
2730 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());
2732 else if (strstr(e
.what(), "size too large"))
2734 // Allow exceptions from over-long size
2735 LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__
, SanitizeString(strCommand
), nMessageSize
, e
.what());
2737 else if (strstr(e
.what(), "non-canonical ReadCompactSize()"))
2739 // Allow exceptions from non-canonical encoding
2740 LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__
, SanitizeString(strCommand
), nMessageSize
, e
.what());
2744 PrintExceptionContinue(&e
, "ProcessMessages()");
2747 catch (const std::exception
& e
) {
2748 PrintExceptionContinue(&e
, "ProcessMessages()");
2750 PrintExceptionContinue(nullptr, "ProcessMessages()");
2754 LogPrintf("%s(%s, %u bytes) FAILED peer=%d\n", __func__
, SanitizeString(strCommand
), nMessageSize
, pfrom
->GetId());
2758 SendRejectsAndCheckIfBanned(pfrom
, connman
);
2763 class CompareInvMempoolOrder
2767 explicit CompareInvMempoolOrder(CTxMemPool
*_mempool
)
2772 bool operator()(std::set
<uint256
>::iterator a
, std::set
<uint256
>::iterator b
)
2774 /* As std::make_heap produces a max-heap, we want the entries with the
2775 * fewest ancestors/highest fee to sort later. */
2776 return mp
->CompareDepthAndScore(*b
, *a
);
2780 bool PeerLogicValidation::SendMessages(CNode
* pto
, std::atomic
<bool>& interruptMsgProc
)
2782 const Consensus::Params
& consensusParams
= Params().GetConsensus();
2784 // Don't send anything until the version handshake is complete
2785 if (!pto
->fSuccessfullyConnected
|| pto
->fDisconnect
)
2788 // If we get here, the outgoing message serialization version is set and can't change.
2789 const CNetMsgMaker
msgMaker(pto
->GetSendVersion());
2794 bool pingSend
= false;
2795 if (pto
->fPingQueued
) {
2796 // RPC ping request by user
2799 if (pto
->nPingNonceSent
== 0 && pto
->nPingUsecStart
+ PING_INTERVAL
* 1000000 < GetTimeMicros()) {
2800 // Ping automatically sent as a latency probe & keepalive.
2805 while (nonce
== 0) {
2806 GetRandBytes((unsigned char*)&nonce
, sizeof(nonce
));
2808 pto
->fPingQueued
= false;
2809 pto
->nPingUsecStart
= GetTimeMicros();
2810 if (pto
->nVersion
> BIP0031_VERSION
) {
2811 pto
->nPingNonceSent
= nonce
;
2812 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::PING
, nonce
));
2814 // Peer is too old to support ping command with nonce, pong will never arrive.
2815 pto
->nPingNonceSent
= 0;
2816 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::PING
));
2820 TRY_LOCK(cs_main
, lockMain
); // Acquire cs_main for IsInitialBlockDownload() and CNodeState()
2824 if (SendRejectsAndCheckIfBanned(pto
, connman
))
2826 CNodeState
&state
= *State(pto
->GetId());
2828 // Address refresh broadcast
2829 int64_t nNow
= GetTimeMicros();
2830 if (!IsInitialBlockDownload() && pto
->nNextLocalAddrSend
< nNow
) {
2831 AdvertiseLocal(pto
);
2832 pto
->nNextLocalAddrSend
= PoissonNextSend(nNow
, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL
);
2838 if (pto
->nNextAddrSend
< nNow
) {
2839 pto
->nNextAddrSend
= PoissonNextSend(nNow
, AVG_ADDRESS_BROADCAST_INTERVAL
);
2840 std::vector
<CAddress
> vAddr
;
2841 vAddr
.reserve(pto
->vAddrToSend
.size());
2842 for (const CAddress
& addr
: pto
->vAddrToSend
)
2844 if (!pto
->addrKnown
.contains(addr
.GetKey()))
2846 pto
->addrKnown
.insert(addr
.GetKey());
2847 vAddr
.push_back(addr
);
2848 // receiver rejects addr messages larger than 1000
2849 if (vAddr
.size() >= 1000)
2851 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::ADDR
, vAddr
));
2856 pto
->vAddrToSend
.clear();
2858 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::ADDR
, vAddr
));
2859 // we only send the big addr message once
2860 if (pto
->vAddrToSend
.capacity() > 40)
2861 pto
->vAddrToSend
.shrink_to_fit();
2865 if (pindexBestHeader
== nullptr)
2866 pindexBestHeader
= chainActive
.Tip();
2867 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.
2868 if (!state
.fSyncStarted
&& !pto
->fClient
&& !fImporting
&& !fReindex
) {
2869 // Only actively request headers from a single peer, unless we're close to today.
2870 if ((nSyncStarted
== 0 && fFetch
) || pindexBestHeader
->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
2871 state
.fSyncStarted
= true;
2872 state
.nHeadersSyncTimeout
= GetTimeMicros() + HEADERS_DOWNLOAD_TIMEOUT_BASE
+ HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER
* (GetAdjustedTime() - pindexBestHeader
->GetBlockTime())/(consensusParams
.nPowTargetSpacing
);
2874 const CBlockIndex
*pindexStart
= pindexBestHeader
;
2875 /* If possible, start at the block preceding the currently
2876 best known header. This ensures that we always get a
2877 non-empty list of headers back as long as the peer
2878 is up-to-date. With a non-empty response, we can initialise
2879 the peer's known best block. This wouldn't be possible
2880 if we requested starting at pindexBestHeader and
2881 got back an empty response. */
2882 if (pindexStart
->pprev
)
2883 pindexStart
= pindexStart
->pprev
;
2884 LogPrint(BCLog::NET
, "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart
->nHeight
, pto
->GetId(), pto
->nStartingHeight
);
2885 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::GETHEADERS
, chainActive
.GetLocator(pindexStart
), uint256()));
2889 // Resend wallet transactions that haven't gotten in a block yet
2890 // Except during reindex, importing and IBD, when old wallet
2891 // transactions become unconfirmed and spams other nodes.
2892 if (!fReindex
&& !fImporting
&& !IsInitialBlockDownload())
2894 GetMainSignals().Broadcast(nTimeBestReceived
, connman
);
2898 // Try sending block announcements via headers
2901 // If we have less than MAX_BLOCKS_TO_ANNOUNCE in our
2902 // list of block hashes we're relaying, and our peer wants
2903 // headers announcements, then find the first header
2904 // not yet known to our peer but would connect, and send.
2905 // If no header would connect, or if we have too many
2906 // blocks, or if the peer doesn't want headers, just
2907 // add all to the inv queue.
2908 LOCK(pto
->cs_inventory
);
2909 std::vector
<CBlock
> vHeaders
;
2910 bool fRevertToInv
= ((!state
.fPreferHeaders
&&
2911 (!state
.fPreferHeaderAndIDs
|| pto
->vBlockHashesToAnnounce
.size() > 1)) ||
2912 pto
->vBlockHashesToAnnounce
.size() > MAX_BLOCKS_TO_ANNOUNCE
);
2913 const CBlockIndex
*pBestIndex
= nullptr; // last header queued for delivery
2914 ProcessBlockAvailability(pto
->GetId()); // ensure pindexBestKnownBlock is up-to-date
2916 if (!fRevertToInv
) {
2917 bool fFoundStartingHeader
= false;
2918 // Try to find first header that our peer doesn't have, and
2919 // then send all headers past that one. If we come across any
2920 // headers that aren't on chainActive, give up.
2921 for (const uint256
&hash
: pto
->vBlockHashesToAnnounce
) {
2922 BlockMap::iterator mi
= mapBlockIndex
.find(hash
);
2923 assert(mi
!= mapBlockIndex
.end());
2924 const CBlockIndex
*pindex
= mi
->second
;
2925 if (chainActive
[pindex
->nHeight
] != pindex
) {
2926 // Bail out if we reorged away from this block
2927 fRevertToInv
= true;
2930 if (pBestIndex
!= nullptr && pindex
->pprev
!= pBestIndex
) {
2931 // This means that the list of blocks to announce don't
2932 // connect to each other.
2933 // This shouldn't really be possible to hit during
2934 // regular operation (because reorgs should take us to
2935 // a chain that has some block not on the prior chain,
2936 // which should be caught by the prior check), but one
2937 // way this could happen is by using invalidateblock /
2938 // reconsiderblock repeatedly on the tip, causing it to
2939 // be added multiple times to vBlockHashesToAnnounce.
2940 // Robustly deal with this rare situation by reverting
2942 fRevertToInv
= true;
2945 pBestIndex
= pindex
;
2946 if (fFoundStartingHeader
) {
2947 // add this to the headers message
2948 vHeaders
.push_back(pindex
->GetBlockHeader());
2949 } else if (PeerHasHeader(&state
, pindex
)) {
2950 continue; // keep looking for the first new block
2951 } else if (pindex
->pprev
== nullptr || PeerHasHeader(&state
, pindex
->pprev
)) {
2952 // Peer doesn't have this header but they do have the prior one.
2953 // Start sending headers.
2954 fFoundStartingHeader
= true;
2955 vHeaders
.push_back(pindex
->GetBlockHeader());
2957 // Peer doesn't have this header or the prior one -- nothing will
2958 // connect, so bail out.
2959 fRevertToInv
= true;
2964 if (!fRevertToInv
&& !vHeaders
.empty()) {
2965 if (vHeaders
.size() == 1 && state
.fPreferHeaderAndIDs
) {
2966 // We only send up to 1 block as header-and-ids, as otherwise
2967 // probably means we're doing an initial-ish-sync or they're slow
2968 LogPrint(BCLog::NET
, "%s sending header-and-ids %s to peer=%d\n", __func__
,
2969 vHeaders
.front().GetHash().ToString(), pto
->GetId());
2971 int nSendFlags
= state
.fWantsCmpctWitness
? 0 : SERIALIZE_TRANSACTION_NO_WITNESS
;
2973 bool fGotBlockFromCache
= false;
2975 LOCK(cs_most_recent_block
);
2976 if (most_recent_block_hash
== pBestIndex
->GetBlockHash()) {
2977 if (state
.fWantsCmpctWitness
|| !fWitnessesPresentInMostRecentCompactBlock
)
2978 connman
->PushMessage(pto
, msgMaker
.Make(nSendFlags
, NetMsgType::CMPCTBLOCK
, *most_recent_compact_block
));
2980 CBlockHeaderAndShortTxIDs
cmpctblock(*most_recent_block
, state
.fWantsCmpctWitness
);
2981 connman
->PushMessage(pto
, msgMaker
.Make(nSendFlags
, NetMsgType::CMPCTBLOCK
, cmpctblock
));
2983 fGotBlockFromCache
= true;
2986 if (!fGotBlockFromCache
) {
2988 bool ret
= ReadBlockFromDisk(block
, pBestIndex
, consensusParams
);
2990 CBlockHeaderAndShortTxIDs
cmpctblock(block
, state
.fWantsCmpctWitness
);
2991 connman
->PushMessage(pto
, msgMaker
.Make(nSendFlags
, NetMsgType::CMPCTBLOCK
, cmpctblock
));
2993 state
.pindexBestHeaderSent
= pBestIndex
;
2994 } else if (state
.fPreferHeaders
) {
2995 if (vHeaders
.size() > 1) {
2996 LogPrint(BCLog::NET
, "%s: %u headers, range (%s, %s), to peer=%d\n", __func__
,
2998 vHeaders
.front().GetHash().ToString(),
2999 vHeaders
.back().GetHash().ToString(), pto
->GetId());
3001 LogPrint(BCLog::NET
, "%s: sending header %s to peer=%d\n", __func__
,
3002 vHeaders
.front().GetHash().ToString(), pto
->GetId());
3004 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::HEADERS
, vHeaders
));
3005 state
.pindexBestHeaderSent
= pBestIndex
;
3007 fRevertToInv
= true;
3010 // If falling back to using an inv, just try to inv the tip.
3011 // The last entry in vBlockHashesToAnnounce was our tip at some point
3013 if (!pto
->vBlockHashesToAnnounce
.empty()) {
3014 const uint256
&hashToAnnounce
= pto
->vBlockHashesToAnnounce
.back();
3015 BlockMap::iterator mi
= mapBlockIndex
.find(hashToAnnounce
);
3016 assert(mi
!= mapBlockIndex
.end());
3017 const CBlockIndex
*pindex
= mi
->second
;
3019 // Warn if we're announcing a block that is not on the main chain.
3020 // This should be very rare and could be optimized out.
3021 // Just log for now.
3022 if (chainActive
[pindex
->nHeight
] != pindex
) {
3023 LogPrint(BCLog::NET
, "Announcing block %s not on main chain (tip=%s)\n",
3024 hashToAnnounce
.ToString(), chainActive
.Tip()->GetBlockHash().ToString());
3027 // If the peer's chain has this block, don't inv it back.
3028 if (!PeerHasHeader(&state
, pindex
)) {
3029 pto
->PushInventory(CInv(MSG_BLOCK
, hashToAnnounce
));
3030 LogPrint(BCLog::NET
, "%s: sending inv peer=%d hash=%s\n", __func__
,
3031 pto
->GetId(), hashToAnnounce
.ToString());
3035 pto
->vBlockHashesToAnnounce
.clear();
3039 // Message: inventory
3041 std::vector
<CInv
> vInv
;
3043 LOCK(pto
->cs_inventory
);
3044 vInv
.reserve(std::max
<size_t>(pto
->vInventoryBlockToSend
.size(), INVENTORY_BROADCAST_MAX
));
3047 for (const uint256
& hash
: pto
->vInventoryBlockToSend
) {
3048 vInv
.push_back(CInv(MSG_BLOCK
, hash
));
3049 if (vInv
.size() == MAX_INV_SZ
) {
3050 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::INV
, vInv
));
3054 pto
->vInventoryBlockToSend
.clear();
3056 // Check whether periodic sends should happen
3057 bool fSendTrickle
= pto
->fWhitelisted
;
3058 if (pto
->nNextInvSend
< nNow
) {
3059 fSendTrickle
= true;
3060 // Use half the delay for outbound peers, as there is less privacy concern for them.
3061 pto
->nNextInvSend
= PoissonNextSend(nNow
, INVENTORY_BROADCAST_INTERVAL
>> !pto
->fInbound
);
3064 // Time to send but the peer has requested we not relay transactions.
3066 LOCK(pto
->cs_filter
);
3067 if (!pto
->fRelayTxes
) pto
->setInventoryTxToSend
.clear();
3070 // Respond to BIP35 mempool requests
3071 if (fSendTrickle
&& pto
->fSendMempool
) {
3072 auto vtxinfo
= mempool
.infoAll();
3073 pto
->fSendMempool
= false;
3074 CAmount filterrate
= 0;
3076 LOCK(pto
->cs_feeFilter
);
3077 filterrate
= pto
->minFeeFilter
;
3080 LOCK(pto
->cs_filter
);
3082 for (const auto& txinfo
: vtxinfo
) {
3083 const uint256
& hash
= txinfo
.tx
->GetHash();
3084 CInv
inv(MSG_TX
, hash
);
3085 pto
->setInventoryTxToSend
.erase(hash
);
3087 if (txinfo
.feeRate
.GetFeePerK() < filterrate
)
3091 if (!pto
->pfilter
->IsRelevantAndUpdate(*txinfo
.tx
)) continue;
3093 pto
->filterInventoryKnown
.insert(hash
);
3094 vInv
.push_back(inv
);
3095 if (vInv
.size() == MAX_INV_SZ
) {
3096 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::INV
, vInv
));
3100 pto
->timeLastMempoolReq
= GetTime();
3103 // Determine transactions to relay
3105 // Produce a vector with all candidates for sending
3106 std::vector
<std::set
<uint256
>::iterator
> vInvTx
;
3107 vInvTx
.reserve(pto
->setInventoryTxToSend
.size());
3108 for (std::set
<uint256
>::iterator it
= pto
->setInventoryTxToSend
.begin(); it
!= pto
->setInventoryTxToSend
.end(); it
++) {
3109 vInvTx
.push_back(it
);
3111 CAmount filterrate
= 0;
3113 LOCK(pto
->cs_feeFilter
);
3114 filterrate
= pto
->minFeeFilter
;
3116 // Topologically and fee-rate sort the inventory we send for privacy and priority reasons.
3117 // A heap is used so that not all items need sorting if only a few are being sent.
3118 CompareInvMempoolOrder
compareInvMempoolOrder(&mempool
);
3119 std::make_heap(vInvTx
.begin(), vInvTx
.end(), compareInvMempoolOrder
);
3120 // No reason to drain out at many times the network's capacity,
3121 // especially since we have many peers and some will draw much shorter delays.
3122 unsigned int nRelayedTransactions
= 0;
3123 LOCK(pto
->cs_filter
);
3124 while (!vInvTx
.empty() && nRelayedTransactions
< INVENTORY_BROADCAST_MAX
) {
3125 // Fetch the top element from the heap
3126 std::pop_heap(vInvTx
.begin(), vInvTx
.end(), compareInvMempoolOrder
);
3127 std::set
<uint256
>::iterator it
= vInvTx
.back();
3130 // Remove it from the to-be-sent set
3131 pto
->setInventoryTxToSend
.erase(it
);
3132 // Check if not in the filter already
3133 if (pto
->filterInventoryKnown
.contains(hash
)) {
3136 // Not in the mempool anymore? don't bother sending it.
3137 auto txinfo
= mempool
.info(hash
);
3141 if (filterrate
&& txinfo
.feeRate
.GetFeePerK() < filterrate
) {
3144 if (pto
->pfilter
&& !pto
->pfilter
->IsRelevantAndUpdate(*txinfo
.tx
)) continue;
3146 vInv
.push_back(CInv(MSG_TX
, hash
));
3147 nRelayedTransactions
++;
3149 // Expire old relay messages
3150 while (!vRelayExpiration
.empty() && vRelayExpiration
.front().first
< nNow
)
3152 mapRelay
.erase(vRelayExpiration
.front().second
);
3153 vRelayExpiration
.pop_front();
3156 auto ret
= mapRelay
.insert(std::make_pair(hash
, std::move(txinfo
.tx
)));
3158 vRelayExpiration
.push_back(std::make_pair(nNow
+ 15 * 60 * 1000000, ret
.first
));
3161 if (vInv
.size() == MAX_INV_SZ
) {
3162 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::INV
, vInv
));
3165 pto
->filterInventoryKnown
.insert(hash
);
3170 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::INV
, vInv
));
3172 // Detect whether we're stalling
3173 nNow
= GetTimeMicros();
3174 if (state
.nStallingSince
&& state
.nStallingSince
< nNow
- 1000000 * BLOCK_STALLING_TIMEOUT
) {
3175 // Stalling only triggers when the block download window cannot move. During normal steady state,
3176 // the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
3177 // should only happen during initial block download.
3178 LogPrintf("Peer=%d is stalling block download, disconnecting\n", pto
->GetId());
3179 pto
->fDisconnect
= true;
3182 // In case there is a block that has been in flight from this peer for 2 + 0.5 * N times the block interval
3183 // (with N the number of peers from which we're downloading validated blocks), disconnect due to timeout.
3184 // We compensate for other peers to prevent killing off peers due to our own downstream link
3185 // being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
3186 // to unreasonably increase our timeout.
3187 if (state
.vBlocksInFlight
.size() > 0) {
3188 QueuedBlock
&queuedBlock
= state
.vBlocksInFlight
.front();
3189 int nOtherPeersWithValidatedDownloads
= nPeersWithValidatedDownloads
- (state
.nBlocksInFlightValidHeaders
> 0);
3190 if (nNow
> state
.nDownloadingSince
+ consensusParams
.nPowTargetSpacing
* (BLOCK_DOWNLOAD_TIMEOUT_BASE
+ BLOCK_DOWNLOAD_TIMEOUT_PER_PEER
* nOtherPeersWithValidatedDownloads
)) {
3191 LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", queuedBlock
.hash
.ToString(), pto
->GetId());
3192 pto
->fDisconnect
= true;
3196 // Check for headers sync timeouts
3197 if (state
.fSyncStarted
&& state
.nHeadersSyncTimeout
< std::numeric_limits
<int64_t>::max()) {
3198 // Detect whether this is a stalling initial-headers-sync peer
3199 if (pindexBestHeader
->GetBlockTime() <= GetAdjustedTime() - 24*60*60) {
3200 if (nNow
> state
.nHeadersSyncTimeout
&& nSyncStarted
== 1 && (nPreferredDownload
- state
.fPreferredDownload
>= 1)) {
3201 // Disconnect a (non-whitelisted) peer if it is our only sync peer,
3202 // and we have others we could be using instead.
3203 // Note: If all our peers are inbound, then we won't
3204 // disconnect our sync peer for stalling; we have bigger
3205 // problems if we can't get any outbound peers.
3206 if (!pto
->fWhitelisted
) {
3207 LogPrintf("Timeout downloading headers from peer=%d, disconnecting\n", pto
->GetId());
3208 pto
->fDisconnect
= true;
3211 LogPrintf("Timeout downloading headers from whitelisted peer=%d, not disconnecting\n", pto
->GetId());
3212 // Reset the headers sync state so that we have a
3213 // chance to try downloading from a different peer.
3214 // Note: this will also result in at least one more
3215 // getheaders message to be sent to
3216 // this peer (eventually).
3217 state
.fSyncStarted
= false;
3219 state
.nHeadersSyncTimeout
= 0;
3223 // After we've caught up once, reset the timeout so we can't trigger
3224 // disconnect later.
3225 state
.nHeadersSyncTimeout
= std::numeric_limits
<int64_t>::max();
3231 // Message: getdata (blocks)
3233 std::vector
<CInv
> vGetData
;
3234 if (!pto
->fClient
&& (fFetch
|| !IsInitialBlockDownload()) && state
.nBlocksInFlight
< MAX_BLOCKS_IN_TRANSIT_PER_PEER
) {
3235 std::vector
<const CBlockIndex
*> vToDownload
;
3236 NodeId staller
= -1;
3237 FindNextBlocksToDownload(pto
->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER
- state
.nBlocksInFlight
, vToDownload
, staller
, consensusParams
);
3238 for (const CBlockIndex
*pindex
: vToDownload
) {
3239 uint32_t nFetchFlags
= GetFetchFlags(pto
);
3240 vGetData
.push_back(CInv(MSG_BLOCK
| nFetchFlags
, pindex
->GetBlockHash()));
3241 MarkBlockAsInFlight(pto
->GetId(), pindex
->GetBlockHash(), pindex
);
3242 LogPrint(BCLog::NET
, "Requesting block %s (%d) peer=%d\n", pindex
->GetBlockHash().ToString(),
3243 pindex
->nHeight
, pto
->GetId());
3245 if (state
.nBlocksInFlight
== 0 && staller
!= -1) {
3246 if (State(staller
)->nStallingSince
== 0) {
3247 State(staller
)->nStallingSince
= nNow
;
3248 LogPrint(BCLog::NET
, "Stall started peer=%d\n", staller
);
3254 // Message: getdata (non-blocks)
3256 while (!pto
->mapAskFor
.empty() && (*pto
->mapAskFor
.begin()).first
<= nNow
)
3258 const CInv
& inv
= (*pto
->mapAskFor
.begin()).second
;
3259 if (!AlreadyHave(inv
))
3261 LogPrint(BCLog::NET
, "Requesting %s peer=%d\n", inv
.ToString(), pto
->GetId());
3262 vGetData
.push_back(inv
);
3263 if (vGetData
.size() >= 1000)
3265 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::GETDATA
, vGetData
));
3269 //If we're not going to ask, don't expect a response.
3270 pto
->setAskFor
.erase(inv
.hash
);
3272 pto
->mapAskFor
.erase(pto
->mapAskFor
.begin());
3274 if (!vGetData
.empty())
3275 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::GETDATA
, vGetData
));
3278 // Message: feefilter
3280 // We don't want white listed peers to filter txs to us if we have -whitelistforcerelay
3281 if (pto
->nVersion
>= FEEFILTER_VERSION
&& gArgs
.GetBoolArg("-feefilter", DEFAULT_FEEFILTER
) &&
3282 !(pto
->fWhitelisted
&& gArgs
.GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY
))) {
3283 CAmount currentFilter
= mempool
.GetMinFee(gArgs
.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE
) * 1000000).GetFeePerK();
3284 int64_t timeNow
= GetTimeMicros();
3285 if (timeNow
> pto
->nextSendTimeFeeFilter
) {
3286 static CFeeRate
default_feerate(DEFAULT_MIN_RELAY_TX_FEE
);
3287 static FeeFilterRounder
filterRounder(default_feerate
);
3288 CAmount filterToSend
= filterRounder
.round(currentFilter
);
3289 // We always have a fee filter of at least minRelayTxFee
3290 filterToSend
= std::max(filterToSend
, ::minRelayTxFee
.GetFeePerK());
3291 if (filterToSend
!= pto
->lastSentFeeFilter
) {
3292 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::FEEFILTER
, filterToSend
));
3293 pto
->lastSentFeeFilter
= filterToSend
;
3295 pto
->nextSendTimeFeeFilter
= PoissonNextSend(timeNow
, AVG_FEEFILTER_BROADCAST_INTERVAL
);
3297 // If the fee filter has changed substantially and it's still more than MAX_FEEFILTER_CHANGE_DELAY
3298 // until scheduled broadcast, then move the broadcast to within MAX_FEEFILTER_CHANGE_DELAY.
3299 else if (timeNow
+ MAX_FEEFILTER_CHANGE_DELAY
* 1000000 < pto
->nextSendTimeFeeFilter
&&
3300 (currentFilter
< 3 * pto
->lastSentFeeFilter
/ 4 || currentFilter
> 4 * pto
->lastSentFeeFilter
/ 3)) {
3301 pto
->nextSendTimeFeeFilter
= timeNow
+ GetRandInt(MAX_FEEFILTER_CHANGE_DELAY
) * 1000000;
3308 class CNetProcessingCleanup
3311 CNetProcessingCleanup() {}
3312 ~CNetProcessingCleanup() {
3313 // orphan transactions
3314 mapOrphanTransactions
.clear();
3315 mapOrphanTransactionsByPrev
.clear();
3317 } instance_of_cnetprocessingcleanup
;