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 <scheduler.h>
27 #include <tinyformat.h>
28 #include <txmempool.h>
29 #include <ui_interface.h>
31 #include <utilmoneystr.h>
32 #include <utilstrencodings.h>
33 #include <validationinterface.h>
36 # error "Bitcoin cannot be compiled without assertions."
39 std::atomic
<int64_t> nTimeBestReceived(0); // Used only to inform the wallet of when we last received a block
41 struct IteratorComparator
44 bool operator()(const I
& a
, const I
& b
)
51 // When modifying, adapt the copy of this definition in tests/DoS_tests.
56 std::map
<uint256
, COrphanTx
> mapOrphanTransactions
GUARDED_BY(cs_main
);
57 std::map
<COutPoint
, std::set
<std::map
<uint256
, COrphanTx
>::iterator
, IteratorComparator
>> mapOrphanTransactionsByPrev
GUARDED_BY(cs_main
);
58 void EraseOrphansFor(NodeId peer
) EXCLUSIVE_LOCKS_REQUIRED(cs_main
);
60 static size_t vExtraTxnForCompactIt
= 0;
61 static std::vector
<std::pair
<uint256
, CTransactionRef
>> vExtraTxnForCompact
GUARDED_BY(cs_main
);
63 static const uint64_t RANDOMIZER_ID_ADDRESS_RELAY
= 0x3cac0035b5866b90ULL
; // SHA256("main address relay")[0:8]
65 /// Age after which a stale block will no longer be served if requested as
66 /// protection against fingerprinting. Set to one month, denominated in seconds.
67 static const int STALE_RELAY_AGE_LIMIT
= 30 * 24 * 60 * 60;
69 /// Age after which a block is considered historical for purposes of rate
70 /// limiting block relay. Set to one week, denominated in seconds.
71 static const int HISTORICAL_BLOCK_AGE
= 7 * 24 * 60 * 60;
75 /** Number of nodes with fSyncStarted. */
79 * Sources of received blocks, saved to be able to send them reject
80 * messages or ban them when processing happens afterwards. Protected by
82 * Set mapBlockSource[hash].second to false if the node should not be
83 * punished if the block is invalid.
85 std::map
<uint256
, std::pair
<NodeId
, bool>> mapBlockSource
;
88 * Filter for transactions that were recently rejected by
89 * AcceptToMemoryPool. These are not rerequested until the chain tip
90 * changes, at which point the entire filter is reset. Protected by
93 * Without this filter we'd be re-requesting txs from each of our peers,
94 * increasing bandwidth consumption considerably. For instance, with 100
95 * peers, half of which relay a tx we don't accept, that might be a 50x
96 * bandwidth increase. A flooding attacker attempting to roll-over the
97 * filter using minimum-sized, 60byte, transactions might manage to send
98 * 1000/sec if we have fast peers, so we pick 120,000 to give our peers a
99 * two minute window to send invs to us.
101 * Decreasing the false positive rate is fairly cheap, so we pick one in a
102 * million to make it highly unlikely for users to have issues with this
105 * Memory used: 1.3 MB
107 std::unique_ptr
<CRollingBloomFilter
> recentRejects
;
108 uint256 hashRecentRejectsChainTip
;
110 /** Blocks that are in flight, and that are in the queue to be downloaded. Protected by cs_main. */
113 const CBlockIndex
* pindex
; //!< Optional.
114 bool fValidatedHeaders
; //!< Whether this block has validated headers at the time of request.
115 std::unique_ptr
<PartiallyDownloadedBlock
> partialBlock
; //!< Optional, used for CMPCTBLOCK downloads
117 std::map
<uint256
, std::pair
<NodeId
, std::list
<QueuedBlock
>::iterator
> > mapBlocksInFlight
;
119 /** Stack of nodes which we have set to announce using compact blocks */
120 std::list
<NodeId
> lNodesAnnouncingHeaderAndIDs
;
122 /** Number of preferable block download peers. */
123 int nPreferredDownload
= 0;
125 /** Number of peers from which we're downloading blocks. */
126 int nPeersWithValidatedDownloads
= 0;
128 /** Number of outbound peers with m_chain_sync.m_protect. */
129 int g_outbound_peers_with_protect_from_disconnect
= 0;
131 /** When our tip was last updated. */
132 int64_t g_last_tip_update
= 0;
134 /** Relay map, protected by cs_main. */
135 typedef std::map
<uint256
, CTransactionRef
> MapRelay
;
137 /** Expiration-time ordered list of (expire time, relay map entry) pairs, protected by cs_main). */
138 std::deque
<std::pair
<int64_t, MapRelay::iterator
>> vRelayExpiration
;
143 struct CBlockReject
{
144 unsigned char chRejectCode
;
145 std::string strRejectReason
;
150 * Maintain validation-specific state about nodes, protected by cs_main, instead
151 * by CNode's own locks. This simplifies asynchronous operation, where
152 * processing of incoming data is done after the ProcessMessage call returns,
153 * and we're no longer holding the node's locks.
156 //! The peer's address
157 const CService address
;
158 //! Whether we have a fully established connection.
159 bool fCurrentlyConnected
;
160 //! Accumulated misbehaviour score for this peer.
162 //! Whether this peer should be disconnected and banned (unless whitelisted).
164 //! String name of this peer (debugging/logging purposes).
165 const std::string name
;
166 //! List of asynchronously-determined block rejections to notify this peer about.
167 std::vector
<CBlockReject
> rejects
;
168 //! The best known block we know this peer has announced.
169 const CBlockIndex
*pindexBestKnownBlock
;
170 //! The hash of the last unknown block this peer has announced.
171 uint256 hashLastUnknownBlock
;
172 //! The last full block we both have.
173 const CBlockIndex
*pindexLastCommonBlock
;
174 //! The best header we have sent our peer.
175 const CBlockIndex
*pindexBestHeaderSent
;
176 //! Length of current-streak of unconnecting headers announcements
177 int nUnconnectingHeaders
;
178 //! Whether we've started headers synchronization with this peer.
180 //! When to potentially disconnect peer for stalling headers download
181 int64_t nHeadersSyncTimeout
;
182 //! Since when we're stalling block download progress (in microseconds), or 0.
183 int64_t nStallingSince
;
184 std::list
<QueuedBlock
> vBlocksInFlight
;
185 //! When the first entry in vBlocksInFlight started downloading. Don't care when vBlocksInFlight is empty.
186 int64_t nDownloadingSince
;
188 int nBlocksInFlightValidHeaders
;
189 //! Whether we consider this a preferred download peer.
190 bool fPreferredDownload
;
191 //! Whether this peer wants invs or headers (when possible) for block announcements.
193 //! Whether this peer wants invs or cmpctblocks (when possible) for block announcements.
194 bool fPreferHeaderAndIDs
;
196 * Whether this peer will send us cmpctblocks if we request them.
197 * This is not used to gate request logic, as we really only care about fSupportsDesiredCmpctVersion,
198 * but is used as a flag to "lock in" the version of compact blocks (fWantsCmpctWitness) we send.
200 bool fProvidesHeaderAndIDs
;
201 //! Whether this peer can give us witnesses
203 //! Whether this peer wants witnesses in cmpctblocks/blocktxns
204 bool fWantsCmpctWitness
;
206 * If we've announced NODE_WITNESS to this peer: whether the peer sends witnesses in cmpctblocks/blocktxns,
207 * otherwise: whether this peer sends non-witnesses in cmpctblocks/blocktxns.
209 bool fSupportsDesiredCmpctVersion
;
211 /** State used to enforce CHAIN_SYNC_TIMEOUT
212 * Only in effect for outbound, non-manual connections, with
214 * Algorithm: if a peer's best known block has less work than our tip,
215 * set a timeout CHAIN_SYNC_TIMEOUT seconds in the future:
216 * - If at timeout their best known block now has more work than our tip
217 * when the timeout was set, then either reset the timeout or clear it
218 * (after comparing against our current tip's work)
219 * - If at timeout their best known block still has less work than our
220 * tip did when the timeout was set, then send a getheaders message,
221 * and set a shorter timeout, HEADERS_RESPONSE_TIME seconds in future.
222 * If their best known block is still behind when that new timeout is
223 * reached, disconnect.
225 struct ChainSyncTimeoutState
{
226 //! A timeout used for checking whether our peer has sufficiently synced
228 //! A header with the work we require on our peer's chain
229 const CBlockIndex
* m_work_header
;
230 //! After timeout is reached, set to true after sending getheaders
231 bool m_sent_getheaders
;
232 //! Whether this peer is protected from disconnection due to a bad/slow chain
236 ChainSyncTimeoutState m_chain_sync
;
238 //! Time of last new block announcement
239 int64_t m_last_block_announcement
;
241 CNodeState(CAddress addrIn
, std::string addrNameIn
) : address(addrIn
), name(addrNameIn
) {
242 fCurrentlyConnected
= false;
245 pindexBestKnownBlock
= nullptr;
246 hashLastUnknownBlock
.SetNull();
247 pindexLastCommonBlock
= nullptr;
248 pindexBestHeaderSent
= nullptr;
249 nUnconnectingHeaders
= 0;
250 fSyncStarted
= false;
251 nHeadersSyncTimeout
= 0;
253 nDownloadingSince
= 0;
255 nBlocksInFlightValidHeaders
= 0;
256 fPreferredDownload
= false;
257 fPreferHeaders
= false;
258 fPreferHeaderAndIDs
= false;
259 fProvidesHeaderAndIDs
= false;
260 fHaveWitness
= false;
261 fWantsCmpctWitness
= false;
262 fSupportsDesiredCmpctVersion
= false;
263 m_chain_sync
= { 0, nullptr, false, false };
264 m_last_block_announcement
= 0;
268 /** Map maintaining per-node state. Requires cs_main. */
269 std::map
<NodeId
, CNodeState
> mapNodeState
;
272 CNodeState
*State(NodeId pnode
) {
273 std::map
<NodeId
, CNodeState
>::iterator it
= mapNodeState
.find(pnode
);
274 if (it
== mapNodeState
.end())
279 void UpdatePreferredDownload(CNode
* node
, CNodeState
* state
)
281 nPreferredDownload
-= state
->fPreferredDownload
;
283 // Whether this node should be marked as a preferred download node.
284 state
->fPreferredDownload
= (!node
->fInbound
|| node
->fWhitelisted
) && !node
->fOneShot
&& !node
->fClient
;
286 nPreferredDownload
+= state
->fPreferredDownload
;
289 void PushNodeVersion(CNode
*pnode
, CConnman
* connman
, int64_t nTime
)
291 ServiceFlags nLocalNodeServices
= pnode
->GetLocalServices();
292 uint64_t nonce
= pnode
->GetLocalNonce();
293 int nNodeStartingHeight
= pnode
->GetMyStartingHeight();
294 NodeId nodeid
= pnode
->GetId();
295 CAddress addr
= pnode
->addr
;
297 CAddress addrYou
= (addr
.IsRoutable() && !IsProxy(addr
) ? addr
: CAddress(CService(), addr
.nServices
));
298 CAddress addrMe
= CAddress(CService(), nLocalNodeServices
);
300 connman
->PushMessage(pnode
, CNetMsgMaker(INIT_PROTO_VERSION
).Make(NetMsgType::VERSION
, PROTOCOL_VERSION
, (uint64_t)nLocalNodeServices
, nTime
, addrYou
, addrMe
,
301 nonce
, strSubVersion
, nNodeStartingHeight
, ::fRelayTxes
));
304 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
);
306 LogPrint(BCLog::NET
, "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION
, nNodeStartingHeight
, addrMe
.ToString(), nodeid
);
311 // Returns a bool indicating whether we requested this block.
312 // Also used if a block was /not/ received and timed out or started with another peer
313 bool MarkBlockAsReceived(const uint256
& hash
) {
314 std::map
<uint256
, std::pair
<NodeId
, std::list
<QueuedBlock
>::iterator
> >::iterator itInFlight
= mapBlocksInFlight
.find(hash
);
315 if (itInFlight
!= mapBlocksInFlight
.end()) {
316 CNodeState
*state
= State(itInFlight
->second
.first
);
317 assert(state
!= nullptr);
318 state
->nBlocksInFlightValidHeaders
-= itInFlight
->second
.second
->fValidatedHeaders
;
319 if (state
->nBlocksInFlightValidHeaders
== 0 && itInFlight
->second
.second
->fValidatedHeaders
) {
320 // Last validated block on the queue was received.
321 nPeersWithValidatedDownloads
--;
323 if (state
->vBlocksInFlight
.begin() == itInFlight
->second
.second
) {
324 // First block on the queue was received, update the start download time for the next one
325 state
->nDownloadingSince
= std::max(state
->nDownloadingSince
, GetTimeMicros());
327 state
->vBlocksInFlight
.erase(itInFlight
->second
.second
);
328 state
->nBlocksInFlight
--;
329 state
->nStallingSince
= 0;
330 mapBlocksInFlight
.erase(itInFlight
);
337 // returns false, still setting pit, if the block was already in flight from the same peer
338 // pit will only be valid as long as the same cs_main lock is being held
339 bool MarkBlockAsInFlight(NodeId nodeid
, const uint256
& hash
, const CBlockIndex
* pindex
= nullptr, std::list
<QueuedBlock
>::iterator
** pit
= nullptr) {
340 CNodeState
*state
= State(nodeid
);
341 assert(state
!= nullptr);
343 // Short-circuit most stuff in case its from the same node
344 std::map
<uint256
, std::pair
<NodeId
, std::list
<QueuedBlock
>::iterator
> >::iterator itInFlight
= mapBlocksInFlight
.find(hash
);
345 if (itInFlight
!= mapBlocksInFlight
.end() && itInFlight
->second
.first
== nodeid
) {
347 *pit
= &itInFlight
->second
.second
;
352 // Make sure it's not listed somewhere already.
353 MarkBlockAsReceived(hash
);
355 std::list
<QueuedBlock
>::iterator it
= state
->vBlocksInFlight
.insert(state
->vBlocksInFlight
.end(),
356 {hash
, pindex
, pindex
!= nullptr, std::unique_ptr
<PartiallyDownloadedBlock
>(pit
? new PartiallyDownloadedBlock(&mempool
) : nullptr)});
357 state
->nBlocksInFlight
++;
358 state
->nBlocksInFlightValidHeaders
+= it
->fValidatedHeaders
;
359 if (state
->nBlocksInFlight
== 1) {
360 // We're starting a block download (batch) from this peer.
361 state
->nDownloadingSince
= GetTimeMicros();
363 if (state
->nBlocksInFlightValidHeaders
== 1 && pindex
!= nullptr) {
364 nPeersWithValidatedDownloads
++;
366 itInFlight
= mapBlocksInFlight
.insert(std::make_pair(hash
, std::make_pair(nodeid
, it
))).first
;
368 *pit
= &itInFlight
->second
.second
;
372 /** Check whether the last unknown block a peer advertised is not yet known. */
373 void ProcessBlockAvailability(NodeId nodeid
) {
374 CNodeState
*state
= State(nodeid
);
375 assert(state
!= nullptr);
377 if (!state
->hashLastUnknownBlock
.IsNull()) {
378 BlockMap::iterator itOld
= mapBlockIndex
.find(state
->hashLastUnknownBlock
);
379 if (itOld
!= mapBlockIndex
.end() && itOld
->second
->nChainWork
> 0) {
380 if (state
->pindexBestKnownBlock
== nullptr || itOld
->second
->nChainWork
>= state
->pindexBestKnownBlock
->nChainWork
)
381 state
->pindexBestKnownBlock
= itOld
->second
;
382 state
->hashLastUnknownBlock
.SetNull();
387 /** Update tracking information about which blocks a peer is assumed to have. */
388 void UpdateBlockAvailability(NodeId nodeid
, const uint256
&hash
) {
389 CNodeState
*state
= State(nodeid
);
390 assert(state
!= nullptr);
392 ProcessBlockAvailability(nodeid
);
394 BlockMap::iterator it
= mapBlockIndex
.find(hash
);
395 if (it
!= mapBlockIndex
.end() && it
->second
->nChainWork
> 0) {
396 // An actually better block was announced.
397 if (state
->pindexBestKnownBlock
== nullptr || it
->second
->nChainWork
>= state
->pindexBestKnownBlock
->nChainWork
)
398 state
->pindexBestKnownBlock
= it
->second
;
400 // An unknown block was announced; just assume that the latest one is the best one.
401 state
->hashLastUnknownBlock
= hash
;
405 void MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid
, CConnman
* connman
) {
406 AssertLockHeld(cs_main
);
407 CNodeState
* nodestate
= State(nodeid
);
408 if (!nodestate
|| !nodestate
->fSupportsDesiredCmpctVersion
) {
409 // Never ask from peers who can't provide witnesses.
412 if (nodestate
->fProvidesHeaderAndIDs
) {
413 for (std::list
<NodeId
>::iterator it
= lNodesAnnouncingHeaderAndIDs
.begin(); it
!= lNodesAnnouncingHeaderAndIDs
.end(); it
++) {
415 lNodesAnnouncingHeaderAndIDs
.erase(it
);
416 lNodesAnnouncingHeaderAndIDs
.push_back(nodeid
);
420 connman
->ForNode(nodeid
, [connman
](CNode
* pfrom
){
421 uint64_t nCMPCTBLOCKVersion
= (pfrom
->GetLocalServices() & NODE_WITNESS
) ? 2 : 1;
422 if (lNodesAnnouncingHeaderAndIDs
.size() >= 3) {
423 // As per BIP152, we only get 3 of our peers to announce
424 // blocks using compact encodings.
425 connman
->ForNode(lNodesAnnouncingHeaderAndIDs
.front(), [connman
, nCMPCTBLOCKVersion
](CNode
* pnodeStop
){
426 connman
->PushMessage(pnodeStop
, CNetMsgMaker(pnodeStop
->GetSendVersion()).Make(NetMsgType::SENDCMPCT
, /*fAnnounceUsingCMPCTBLOCK=*/false, nCMPCTBLOCKVersion
));
429 lNodesAnnouncingHeaderAndIDs
.pop_front();
431 connman
->PushMessage(pfrom
, CNetMsgMaker(pfrom
->GetSendVersion()).Make(NetMsgType::SENDCMPCT
, /*fAnnounceUsingCMPCTBLOCK=*/true, nCMPCTBLOCKVersion
));
432 lNodesAnnouncingHeaderAndIDs
.push_back(pfrom
->GetId());
438 bool TipMayBeStale(const Consensus::Params
&consensusParams
)
440 AssertLockHeld(cs_main
);
441 if (g_last_tip_update
== 0) {
442 g_last_tip_update
= GetTime();
444 return g_last_tip_update
< GetTime() - consensusParams
.nPowTargetSpacing
* 3 && mapBlocksInFlight
.empty();
448 bool CanDirectFetch(const Consensus::Params
&consensusParams
)
450 return chainActive
.Tip()->GetBlockTime() > GetAdjustedTime() - consensusParams
.nPowTargetSpacing
* 20;
454 bool PeerHasHeader(CNodeState
*state
, const CBlockIndex
*pindex
)
456 if (state
->pindexBestKnownBlock
&& pindex
== state
->pindexBestKnownBlock
->GetAncestor(pindex
->nHeight
))
458 if (state
->pindexBestHeaderSent
&& pindex
== state
->pindexBestHeaderSent
->GetAncestor(pindex
->nHeight
))
463 /** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
464 * at most count entries. */
465 void FindNextBlocksToDownload(NodeId nodeid
, unsigned int count
, std::vector
<const CBlockIndex
*>& vBlocks
, NodeId
& nodeStaller
, const Consensus::Params
& consensusParams
) {
469 vBlocks
.reserve(vBlocks
.size() + count
);
470 CNodeState
*state
= State(nodeid
);
471 assert(state
!= nullptr);
473 // Make sure pindexBestKnownBlock is up to date, we'll need it.
474 ProcessBlockAvailability(nodeid
);
476 if (state
->pindexBestKnownBlock
== nullptr || state
->pindexBestKnownBlock
->nChainWork
< chainActive
.Tip()->nChainWork
|| state
->pindexBestKnownBlock
->nChainWork
< nMinimumChainWork
) {
477 // This peer has nothing interesting.
481 if (state
->pindexLastCommonBlock
== nullptr) {
482 // Bootstrap quickly by guessing a parent of our best tip is the forking point.
483 // Guessing wrong in either direction is not a problem.
484 state
->pindexLastCommonBlock
= chainActive
[std::min(state
->pindexBestKnownBlock
->nHeight
, chainActive
.Height())];
487 // If the peer reorganized, our previous pindexLastCommonBlock may not be an ancestor
488 // of its current tip anymore. Go back enough to fix that.
489 state
->pindexLastCommonBlock
= LastCommonAncestor(state
->pindexLastCommonBlock
, state
->pindexBestKnownBlock
);
490 if (state
->pindexLastCommonBlock
== state
->pindexBestKnownBlock
)
493 std::vector
<const CBlockIndex
*> vToFetch
;
494 const CBlockIndex
*pindexWalk
= state
->pindexLastCommonBlock
;
495 // Never fetch further than the best block we know the peer has, or more than BLOCK_DOWNLOAD_WINDOW + 1 beyond the last
496 // linked block we have in common with this peer. The +1 is so we can detect stalling, namely if we would be able to
497 // download that next block if the window were 1 larger.
498 int nWindowEnd
= state
->pindexLastCommonBlock
->nHeight
+ BLOCK_DOWNLOAD_WINDOW
;
499 int nMaxHeight
= std::min
<int>(state
->pindexBestKnownBlock
->nHeight
, nWindowEnd
+ 1);
500 NodeId waitingfor
= -1;
501 while (pindexWalk
->nHeight
< nMaxHeight
) {
502 // Read up to 128 (or more, if more blocks than that are needed) successors of pindexWalk (towards
503 // pindexBestKnownBlock) into vToFetch. We fetch 128, because CBlockIndex::GetAncestor may be as expensive
504 // as iterating over ~100 CBlockIndex* entries anyway.
505 int nToFetch
= std::min(nMaxHeight
- pindexWalk
->nHeight
, std::max
<int>(count
- vBlocks
.size(), 128));
506 vToFetch
.resize(nToFetch
);
507 pindexWalk
= state
->pindexBestKnownBlock
->GetAncestor(pindexWalk
->nHeight
+ nToFetch
);
508 vToFetch
[nToFetch
- 1] = pindexWalk
;
509 for (unsigned int i
= nToFetch
- 1; i
> 0; i
--) {
510 vToFetch
[i
- 1] = vToFetch
[i
]->pprev
;
513 // Iterate over those blocks in vToFetch (in forward direction), adding the ones that
514 // are not yet downloaded and not in flight to vBlocks. In the mean time, update
515 // pindexLastCommonBlock as long as all ancestors are already downloaded, or if it's
516 // already part of our chain (and therefore don't need it even if pruned).
517 for (const CBlockIndex
* pindex
: vToFetch
) {
518 if (!pindex
->IsValid(BLOCK_VALID_TREE
)) {
519 // We consider the chain that this peer is on invalid.
522 if (!State(nodeid
)->fHaveWitness
&& IsWitnessEnabled(pindex
->pprev
, consensusParams
)) {
523 // We wouldn't download this block or its descendants from this peer.
526 if (pindex
->nStatus
& BLOCK_HAVE_DATA
|| chainActive
.Contains(pindex
)) {
527 if (pindex
->nChainTx
)
528 state
->pindexLastCommonBlock
= pindex
;
529 } else if (mapBlocksInFlight
.count(pindex
->GetBlockHash()) == 0) {
530 // The block is not already downloaded, and not yet in flight.
531 if (pindex
->nHeight
> nWindowEnd
) {
532 // We reached the end of the window.
533 if (vBlocks
.size() == 0 && waitingfor
!= nodeid
) {
534 // We aren't able to fetch anything, but we would be if the download window was one larger.
535 nodeStaller
= waitingfor
;
539 vBlocks
.push_back(pindex
);
540 if (vBlocks
.size() == count
) {
543 } else if (waitingfor
== -1) {
544 // This is the first already-in-flight block.
545 waitingfor
= mapBlocksInFlight
[pindex
->GetBlockHash()].first
;
553 // This function is used for testing the stale tip eviction logic, see
555 void UpdateLastBlockAnnounceTime(NodeId node
, int64_t time_in_seconds
)
558 CNodeState
*state
= State(node
);
559 if (state
) state
->m_last_block_announcement
= time_in_seconds
;
562 // Returns true for outbound peers, excluding manual connections, feelers, and
564 bool IsOutboundDisconnectionCandidate(const CNode
*node
)
566 return !(node
->fInbound
|| node
->m_manual_connection
|| node
->fFeeler
|| node
->fOneShot
);
569 void PeerLogicValidation::InitializeNode(CNode
*pnode
) {
570 CAddress addr
= pnode
->addr
;
571 std::string addrName
= pnode
->GetAddrName();
572 NodeId nodeid
= pnode
->GetId();
575 mapNodeState
.emplace_hint(mapNodeState
.end(), std::piecewise_construct
, std::forward_as_tuple(nodeid
), std::forward_as_tuple(addr
, std::move(addrName
)));
578 PushNodeVersion(pnode
, connman
, GetTime());
581 void PeerLogicValidation::FinalizeNode(NodeId nodeid
, bool& fUpdateConnectionTime
) {
582 fUpdateConnectionTime
= false;
584 CNodeState
*state
= State(nodeid
);
585 assert(state
!= nullptr);
587 if (state
->fSyncStarted
)
590 if (state
->nMisbehavior
== 0 && state
->fCurrentlyConnected
) {
591 fUpdateConnectionTime
= true;
594 for (const QueuedBlock
& entry
: state
->vBlocksInFlight
) {
595 mapBlocksInFlight
.erase(entry
.hash
);
597 EraseOrphansFor(nodeid
);
598 nPreferredDownload
-= state
->fPreferredDownload
;
599 nPeersWithValidatedDownloads
-= (state
->nBlocksInFlightValidHeaders
!= 0);
600 assert(nPeersWithValidatedDownloads
>= 0);
601 g_outbound_peers_with_protect_from_disconnect
-= state
->m_chain_sync
.m_protect
;
602 assert(g_outbound_peers_with_protect_from_disconnect
>= 0);
604 mapNodeState
.erase(nodeid
);
606 if (mapNodeState
.empty()) {
607 // Do a consistency check after the last peer is removed.
608 assert(mapBlocksInFlight
.empty());
609 assert(nPreferredDownload
== 0);
610 assert(nPeersWithValidatedDownloads
== 0);
611 assert(g_outbound_peers_with_protect_from_disconnect
== 0);
613 LogPrint(BCLog::NET
, "Cleared nodestate for peer=%d\n", nodeid
);
616 bool GetNodeStateStats(NodeId nodeid
, CNodeStateStats
&stats
) {
618 CNodeState
*state
= State(nodeid
);
619 if (state
== nullptr)
621 stats
.nMisbehavior
= state
->nMisbehavior
;
622 stats
.nSyncHeight
= state
->pindexBestKnownBlock
? state
->pindexBestKnownBlock
->nHeight
: -1;
623 stats
.nCommonHeight
= state
->pindexLastCommonBlock
? state
->pindexLastCommonBlock
->nHeight
: -1;
624 for (const QueuedBlock
& queue
: state
->vBlocksInFlight
) {
626 stats
.vHeightInFlight
.push_back(queue
.pindex
->nHeight
);
631 //////////////////////////////////////////////////////////////////////////////
633 // mapOrphanTransactions
636 void AddToCompactExtraTransactions(const CTransactionRef
& tx
) EXCLUSIVE_LOCKS_REQUIRED(cs_main
)
638 size_t max_extra_txn
= gArgs
.GetArg("-blockreconstructionextratxn", DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN
);
639 if (max_extra_txn
<= 0)
641 if (!vExtraTxnForCompact
.size())
642 vExtraTxnForCompact
.resize(max_extra_txn
);
643 vExtraTxnForCompact
[vExtraTxnForCompactIt
] = std::make_pair(tx
->GetWitnessHash(), tx
);
644 vExtraTxnForCompactIt
= (vExtraTxnForCompactIt
+ 1) % max_extra_txn
;
647 bool AddOrphanTx(const CTransactionRef
& tx
, NodeId peer
) EXCLUSIVE_LOCKS_REQUIRED(cs_main
)
649 const uint256
& hash
= tx
->GetHash();
650 if (mapOrphanTransactions
.count(hash
))
653 // Ignore big transactions, to avoid a
654 // send-big-orphans memory exhaustion attack. If a peer has a legitimate
655 // large transaction with a missing parent then we assume
656 // it will rebroadcast it later, after the parent transaction(s)
657 // have been mined or received.
658 // 100 orphans, each of which is at most 99,999 bytes big is
659 // at most 10 megabytes of orphans and somewhat more byprev index (in the worst case):
660 unsigned int sz
= GetTransactionWeight(*tx
);
661 if (sz
>= MAX_STANDARD_TX_WEIGHT
)
663 LogPrint(BCLog::MEMPOOL
, "ignoring large orphan tx (size: %u, hash: %s)\n", sz
, hash
.ToString());
667 auto ret
= mapOrphanTransactions
.emplace(hash
, COrphanTx
{tx
, peer
, GetTime() + ORPHAN_TX_EXPIRE_TIME
});
669 for (const CTxIn
& txin
: tx
->vin
) {
670 mapOrphanTransactionsByPrev
[txin
.prevout
].insert(ret
.first
);
673 AddToCompactExtraTransactions(tx
);
675 LogPrint(BCLog::MEMPOOL
, "stored orphan tx %s (mapsz %u outsz %u)\n", hash
.ToString(),
676 mapOrphanTransactions
.size(), mapOrphanTransactionsByPrev
.size());
680 int static EraseOrphanTx(uint256 hash
) EXCLUSIVE_LOCKS_REQUIRED(cs_main
)
682 std::map
<uint256
, COrphanTx
>::iterator it
= mapOrphanTransactions
.find(hash
);
683 if (it
== mapOrphanTransactions
.end())
685 for (const CTxIn
& txin
: it
->second
.tx
->vin
)
687 auto itPrev
= mapOrphanTransactionsByPrev
.find(txin
.prevout
);
688 if (itPrev
== mapOrphanTransactionsByPrev
.end())
690 itPrev
->second
.erase(it
);
691 if (itPrev
->second
.empty())
692 mapOrphanTransactionsByPrev
.erase(itPrev
);
694 mapOrphanTransactions
.erase(it
);
698 void EraseOrphansFor(NodeId peer
)
701 std::map
<uint256
, COrphanTx
>::iterator iter
= mapOrphanTransactions
.begin();
702 while (iter
!= mapOrphanTransactions
.end())
704 std::map
<uint256
, COrphanTx
>::iterator maybeErase
= iter
++; // increment to avoid iterator becoming invalid
705 if (maybeErase
->second
.fromPeer
== peer
)
707 nErased
+= EraseOrphanTx(maybeErase
->second
.tx
->GetHash());
710 if (nErased
> 0) LogPrint(BCLog::MEMPOOL
, "Erased %d orphan tx from peer=%d\n", nErased
, peer
);
714 unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans
) EXCLUSIVE_LOCKS_REQUIRED(cs_main
)
716 unsigned int nEvicted
= 0;
717 static int64_t nNextSweep
;
718 int64_t nNow
= GetTime();
719 if (nNextSweep
<= nNow
) {
720 // Sweep out expired orphan pool entries:
722 int64_t nMinExpTime
= nNow
+ ORPHAN_TX_EXPIRE_TIME
- ORPHAN_TX_EXPIRE_INTERVAL
;
723 std::map
<uint256
, COrphanTx
>::iterator iter
= mapOrphanTransactions
.begin();
724 while (iter
!= mapOrphanTransactions
.end())
726 std::map
<uint256
, COrphanTx
>::iterator maybeErase
= iter
++;
727 if (maybeErase
->second
.nTimeExpire
<= nNow
) {
728 nErased
+= EraseOrphanTx(maybeErase
->second
.tx
->GetHash());
730 nMinExpTime
= std::min(maybeErase
->second
.nTimeExpire
, nMinExpTime
);
733 // Sweep again 5 minutes after the next entry that expires in order to batch the linear scan.
734 nNextSweep
= nMinExpTime
+ ORPHAN_TX_EXPIRE_INTERVAL
;
735 if (nErased
> 0) LogPrint(BCLog::MEMPOOL
, "Erased %d orphan tx due to expiration\n", nErased
);
737 while (mapOrphanTransactions
.size() > nMaxOrphans
)
739 // Evict a random orphan:
740 uint256 randomhash
= GetRandHash();
741 std::map
<uint256
, COrphanTx
>::iterator it
= mapOrphanTransactions
.lower_bound(randomhash
);
742 if (it
== mapOrphanTransactions
.end())
743 it
= mapOrphanTransactions
.begin();
744 EraseOrphanTx(it
->first
);
751 void Misbehaving(NodeId pnode
, int howmuch
)
756 CNodeState
*state
= State(pnode
);
757 if (state
== nullptr)
760 state
->nMisbehavior
+= howmuch
;
761 int banscore
= gArgs
.GetArg("-banscore", DEFAULT_BANSCORE_THRESHOLD
);
762 if (state
->nMisbehavior
>= banscore
&& state
->nMisbehavior
- howmuch
< banscore
)
764 LogPrintf("%s: %s peer=%d (%d -> %d) BAN THRESHOLD EXCEEDED\n", __func__
, state
->name
, pnode
, state
->nMisbehavior
-howmuch
, state
->nMisbehavior
);
765 state
->fShouldBan
= true;
767 LogPrintf("%s: %s peer=%d (%d -> %d)\n", __func__
, state
->name
, pnode
, state
->nMisbehavior
-howmuch
, state
->nMisbehavior
);
777 //////////////////////////////////////////////////////////////////////////////
779 // blockchain -> download logic notification
782 // To prevent fingerprinting attacks, only send blocks/headers outside of the
783 // active chain if they are no more than a month older (both in time, and in
784 // best equivalent proof of work) than the best header chain we know about and
785 // we fully-validated them at some point.
786 static bool BlockRequestAllowed(const CBlockIndex
* pindex
, const Consensus::Params
& consensusParams
)
788 AssertLockHeld(cs_main
);
789 if (chainActive
.Contains(pindex
)) return true;
790 return pindex
->IsValid(BLOCK_VALID_SCRIPTS
) && (pindexBestHeader
!= nullptr) &&
791 (pindexBestHeader
->GetBlockTime() - pindex
->GetBlockTime() < STALE_RELAY_AGE_LIMIT
) &&
792 (GetBlockProofEquivalentTime(*pindexBestHeader
, *pindex
, *pindexBestHeader
, consensusParams
) < STALE_RELAY_AGE_LIMIT
);
795 PeerLogicValidation::PeerLogicValidation(CConnman
* connmanIn
, CScheduler
&scheduler
) : connman(connmanIn
), m_stale_tip_check_time(0) {
796 // Initialize global variables that cannot be constructed at startup.
797 recentRejects
.reset(new CRollingBloomFilter(120000, 0.000001));
799 const Consensus::Params
& consensusParams
= Params().GetConsensus();
800 // Stale tip checking and peer eviction are on two different timers, but we
801 // don't want them to get out of sync due to drift in the scheduler, so we
802 // combine them in one function and schedule at the quicker (peer-eviction)
804 static_assert(EXTRA_PEER_CHECK_INTERVAL
< STALE_CHECK_INTERVAL
, "peer eviction timer should be less than stale tip check timer");
805 scheduler
.scheduleEvery(std::bind(&PeerLogicValidation::CheckForStaleTipAndEvictPeers
, this, consensusParams
), EXTRA_PEER_CHECK_INTERVAL
* 1000);
808 void PeerLogicValidation::BlockConnected(const std::shared_ptr
<const CBlock
>& pblock
, const CBlockIndex
* pindex
, const std::vector
<CTransactionRef
>& vtxConflicted
) {
811 std::vector
<uint256
> vOrphanErase
;
813 for (const CTransactionRef
& ptx
: pblock
->vtx
) {
814 const CTransaction
& tx
= *ptx
;
816 // Which orphan pool entries must we evict?
817 for (const auto& txin
: tx
.vin
) {
818 auto itByPrev
= mapOrphanTransactionsByPrev
.find(txin
.prevout
);
819 if (itByPrev
== mapOrphanTransactionsByPrev
.end()) continue;
820 for (auto mi
= itByPrev
->second
.begin(); mi
!= itByPrev
->second
.end(); ++mi
) {
821 const CTransaction
& orphanTx
= *(*mi
)->second
.tx
;
822 const uint256
& orphanHash
= orphanTx
.GetHash();
823 vOrphanErase
.push_back(orphanHash
);
828 // Erase orphan transactions include or precluded by this block
829 if (vOrphanErase
.size()) {
831 for (uint256
&orphanHash
: vOrphanErase
) {
832 nErased
+= EraseOrphanTx(orphanHash
);
834 LogPrint(BCLog::MEMPOOL
, "Erased %d orphan tx included or conflicted by block\n", nErased
);
837 g_last_tip_update
= GetTime();
840 // All of the following cache a recent block, and are protected by cs_most_recent_block
841 static CCriticalSection cs_most_recent_block
;
842 static std::shared_ptr
<const CBlock
> most_recent_block
;
843 static std::shared_ptr
<const CBlockHeaderAndShortTxIDs
> most_recent_compact_block
;
844 static uint256 most_recent_block_hash
;
845 static bool fWitnessesPresentInMostRecentCompactBlock
;
847 void PeerLogicValidation::NewPoWValidBlock(const CBlockIndex
*pindex
, const std::shared_ptr
<const CBlock
>& pblock
) {
848 std::shared_ptr
<const CBlockHeaderAndShortTxIDs
> pcmpctblock
= std::make_shared
<const CBlockHeaderAndShortTxIDs
> (*pblock
, true);
849 const CNetMsgMaker
msgMaker(PROTOCOL_VERSION
);
853 static int nHighestFastAnnounce
= 0;
854 if (pindex
->nHeight
<= nHighestFastAnnounce
)
856 nHighestFastAnnounce
= pindex
->nHeight
;
858 bool fWitnessEnabled
= IsWitnessEnabled(pindex
->pprev
, Params().GetConsensus());
859 uint256
hashBlock(pblock
->GetHash());
862 LOCK(cs_most_recent_block
);
863 most_recent_block_hash
= hashBlock
;
864 most_recent_block
= pblock
;
865 most_recent_compact_block
= pcmpctblock
;
866 fWitnessesPresentInMostRecentCompactBlock
= fWitnessEnabled
;
869 connman
->ForEachNode([this, &pcmpctblock
, pindex
, &msgMaker
, fWitnessEnabled
, &hashBlock
](CNode
* pnode
) {
870 // TODO: Avoid the repeated-serialization here
871 if (pnode
->nVersion
< INVALID_CB_NO_BAN_VERSION
|| pnode
->fDisconnect
)
873 ProcessBlockAvailability(pnode
->GetId());
874 CNodeState
&state
= *State(pnode
->GetId());
875 // If the peer has, or we announced to them the previous block already,
876 // but we don't think they have this one, go ahead and announce it
877 if (state
.fPreferHeaderAndIDs
&& (!fWitnessEnabled
|| state
.fWantsCmpctWitness
) &&
878 !PeerHasHeader(&state
, pindex
) && PeerHasHeader(&state
, pindex
->pprev
)) {
880 LogPrint(BCLog::NET
, "%s sending header-and-ids %s to peer=%d\n", "PeerLogicValidation::NewPoWValidBlock",
881 hashBlock
.ToString(), pnode
->GetId());
882 connman
->PushMessage(pnode
, msgMaker
.Make(NetMsgType::CMPCTBLOCK
, *pcmpctblock
));
883 state
.pindexBestHeaderSent
= pindex
;
888 void PeerLogicValidation::UpdatedBlockTip(const CBlockIndex
*pindexNew
, const CBlockIndex
*pindexFork
, bool fInitialDownload
) {
889 const int nNewHeight
= pindexNew
->nHeight
;
890 connman
->SetBestHeight(nNewHeight
);
892 if (!fInitialDownload
) {
893 // Find the hashes of all blocks that weren't previously in the best chain.
894 std::vector
<uint256
> vHashes
;
895 const CBlockIndex
*pindexToAnnounce
= pindexNew
;
896 while (pindexToAnnounce
!= pindexFork
) {
897 vHashes
.push_back(pindexToAnnounce
->GetBlockHash());
898 pindexToAnnounce
= pindexToAnnounce
->pprev
;
899 if (vHashes
.size() == MAX_BLOCKS_TO_ANNOUNCE
) {
900 // Limit announcements in case of a huge reorganization.
901 // Rely on the peer's synchronization mechanism in that case.
905 // Relay inventory, but don't relay old inventory during initial block download.
906 connman
->ForEachNode([nNewHeight
, &vHashes
](CNode
* pnode
) {
907 if (nNewHeight
> (pnode
->nStartingHeight
!= -1 ? pnode
->nStartingHeight
- 2000 : 0)) {
908 for (const uint256
& hash
: reverse_iterate(vHashes
)) {
909 pnode
->PushBlockHash(hash
);
913 connman
->WakeMessageHandler();
916 nTimeBestReceived
= GetTime();
919 void PeerLogicValidation::BlockChecked(const CBlock
& block
, const CValidationState
& state
) {
922 const uint256
hash(block
.GetHash());
923 std::map
<uint256
, std::pair
<NodeId
, bool>>::iterator it
= mapBlockSource
.find(hash
);
926 if (state
.IsInvalid(nDoS
)) {
927 // Don't send reject message with code 0 or an internal reject code.
928 if (it
!= mapBlockSource
.end() && State(it
->second
.first
) && state
.GetRejectCode() > 0 && state
.GetRejectCode() < REJECT_INTERNAL
) {
929 CBlockReject reject
= {(unsigned char)state
.GetRejectCode(), state
.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH
), hash
};
930 State(it
->second
.first
)->rejects
.push_back(reject
);
931 if (nDoS
> 0 && it
->second
.second
)
932 Misbehaving(it
->second
.first
, nDoS
);
936 // 1. The block is valid
937 // 2. We're not in initial block download
938 // 3. This is currently the best block we're aware of. We haven't updated
939 // the tip yet so we have no way to check this directly here. Instead we
940 // just check that there are currently no other blocks in flight.
941 else if (state
.IsValid() &&
942 !IsInitialBlockDownload() &&
943 mapBlocksInFlight
.count(hash
) == mapBlocksInFlight
.size()) {
944 if (it
!= mapBlockSource
.end()) {
945 MaybeSetPeerAsAnnouncingHeaderAndIDs(it
->second
.first
, connman
);
948 if (it
!= mapBlockSource
.end())
949 mapBlockSource
.erase(it
);
952 //////////////////////////////////////////////////////////////////////////////
958 bool static AlreadyHave(const CInv
& inv
) EXCLUSIVE_LOCKS_REQUIRED(cs_main
)
965 assert(recentRejects
);
966 if (chainActive
.Tip()->GetBlockHash() != hashRecentRejectsChainTip
)
968 // If the chain tip has changed previously rejected transactions
969 // might be now valid, e.g. due to a nLockTime'd tx becoming valid,
970 // or a double-spend. Reset the rejects filter and give those
971 // txs a second chance.
972 hashRecentRejectsChainTip
= chainActive
.Tip()->GetBlockHash();
973 recentRejects
->reset();
976 return recentRejects
->contains(inv
.hash
) ||
977 mempool
.exists(inv
.hash
) ||
978 mapOrphanTransactions
.count(inv
.hash
) ||
979 pcoinsTip
->HaveCoinInCache(COutPoint(inv
.hash
, 0)) || // Best effort: only try output 0 and 1
980 pcoinsTip
->HaveCoinInCache(COutPoint(inv
.hash
, 1));
983 case MSG_WITNESS_BLOCK
:
984 return mapBlockIndex
.count(inv
.hash
);
986 // Don't know what it is, just say we already got one
990 static void RelayTransaction(const CTransaction
& tx
, CConnman
* connman
)
992 CInv
inv(MSG_TX
, tx
.GetHash());
993 connman
->ForEachNode([&inv
](CNode
* pnode
)
995 pnode
->PushInventory(inv
);
999 static void RelayAddress(const CAddress
& addr
, bool fReachable
, CConnman
* connman
)
1001 unsigned int nRelayNodes
= fReachable
? 2 : 1; // limited relaying of addresses outside our network(s)
1003 // Relay to a limited number of other nodes
1004 // Use deterministic randomness to send to the same nodes for 24 hours
1005 // at a time so the addrKnowns of the chosen nodes prevent repeats
1006 uint64_t hashAddr
= addr
.GetHash();
1007 const CSipHasher hasher
= connman
->GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY
).Write(hashAddr
<< 32).Write((GetTime() + hashAddr
) / (24*60*60));
1008 FastRandomContext insecure_rand
;
1010 std::array
<std::pair
<uint64_t, CNode
*>,2> best
{{{0, nullptr}, {0, nullptr}}};
1011 assert(nRelayNodes
<= best
.size());
1013 auto sortfunc
= [&best
, &hasher
, nRelayNodes
](CNode
* pnode
) {
1014 if (pnode
->nVersion
>= CADDR_TIME_VERSION
) {
1015 uint64_t hashKey
= CSipHasher(hasher
).Write(pnode
->GetId()).Finalize();
1016 for (unsigned int i
= 0; i
< nRelayNodes
; i
++) {
1017 if (hashKey
> best
[i
].first
) {
1018 std::copy(best
.begin() + i
, best
.begin() + nRelayNodes
- 1, best
.begin() + i
+ 1);
1019 best
[i
] = std::make_pair(hashKey
, pnode
);
1026 auto pushfunc
= [&addr
, &best
, nRelayNodes
, &insecure_rand
] {
1027 for (unsigned int i
= 0; i
< nRelayNodes
&& best
[i
].first
!= 0; i
++) {
1028 best
[i
].second
->PushAddress(addr
, insecure_rand
);
1032 connman
->ForEachNodeThen(std::move(sortfunc
), std::move(pushfunc
));
1035 void static ProcessGetData(CNode
* pfrom
, const Consensus::Params
& consensusParams
, CConnman
* connman
, const std::atomic
<bool>& interruptMsgProc
)
1037 std::deque
<CInv
>::iterator it
= pfrom
->vRecvGetData
.begin();
1038 std::vector
<CInv
> vNotFound
;
1039 const CNetMsgMaker
msgMaker(pfrom
->GetSendVersion());
1042 while (it
!= pfrom
->vRecvGetData
.end()) {
1043 // Don't bother if send buffer is too full to respond anyway
1044 if (pfrom
->fPauseSend
)
1047 const CInv
&inv
= *it
;
1049 if (interruptMsgProc
)
1054 if (inv
.type
== MSG_BLOCK
|| inv
.type
== MSG_FILTERED_BLOCK
|| inv
.type
== MSG_CMPCT_BLOCK
|| inv
.type
== MSG_WITNESS_BLOCK
)
1057 BlockMap::iterator mi
= mapBlockIndex
.find(inv
.hash
);
1058 std::shared_ptr
<const CBlock
> a_recent_block
;
1059 std::shared_ptr
<const CBlockHeaderAndShortTxIDs
> a_recent_compact_block
;
1060 bool fWitnessesPresentInARecentCompactBlock
;
1062 LOCK(cs_most_recent_block
);
1063 a_recent_block
= most_recent_block
;
1064 a_recent_compact_block
= most_recent_compact_block
;
1065 fWitnessesPresentInARecentCompactBlock
= fWitnessesPresentInMostRecentCompactBlock
;
1067 if (mi
!= mapBlockIndex
.end())
1069 if (mi
->second
->nChainTx
&& !mi
->second
->IsValid(BLOCK_VALID_SCRIPTS
) &&
1070 mi
->second
->IsValid(BLOCK_VALID_TREE
)) {
1071 // If we have the block and all of its parents, but have not yet validated it,
1072 // we might be in the middle of connecting it (ie in the unlock of cs_main
1073 // before ActivateBestChain but after AcceptBlock).
1074 // In this case, we need to run ActivateBestChain prior to checking the relay
1075 // conditions below.
1076 CValidationState dummy
;
1077 ActivateBestChain(dummy
, Params(), a_recent_block
);
1079 send
= BlockRequestAllowed(mi
->second
, consensusParams
);
1081 LogPrintf("%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__
, pfrom
->GetId());
1084 // disconnect node in case we have reached the outbound limit for serving historical blocks
1085 // never disconnect whitelisted nodes
1086 if (send
&& connman
->OutboundTargetReached(true) && ( ((pindexBestHeader
!= nullptr) && (pindexBestHeader
->GetBlockTime() - mi
->second
->GetBlockTime() > HISTORICAL_BLOCK_AGE
)) || inv
.type
== MSG_FILTERED_BLOCK
) && !pfrom
->fWhitelisted
)
1088 LogPrint(BCLog::NET
, "historical block serving limit reached, disconnect peer=%d\n", pfrom
->GetId());
1091 pfrom
->fDisconnect
= true;
1094 // Pruned nodes may have deleted the block, so check whether
1095 // it's available before trying to send.
1096 if (send
&& (mi
->second
->nStatus
& BLOCK_HAVE_DATA
))
1098 std::shared_ptr
<const CBlock
> pblock
;
1099 if (a_recent_block
&& a_recent_block
->GetHash() == (*mi
).second
->GetBlockHash()) {
1100 pblock
= a_recent_block
;
1102 // Send block from disk
1103 std::shared_ptr
<CBlock
> pblockRead
= std::make_shared
<CBlock
>();
1104 if (!ReadBlockFromDisk(*pblockRead
, (*mi
).second
, consensusParams
))
1105 assert(!"cannot load block from disk");
1106 pblock
= pblockRead
;
1108 if (inv
.type
== MSG_BLOCK
)
1109 connman
->PushMessage(pfrom
, msgMaker
.Make(SERIALIZE_TRANSACTION_NO_WITNESS
, NetMsgType::BLOCK
, *pblock
));
1110 else if (inv
.type
== MSG_WITNESS_BLOCK
)
1111 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::BLOCK
, *pblock
));
1112 else if (inv
.type
== MSG_FILTERED_BLOCK
)
1114 bool sendMerkleBlock
= false;
1115 CMerkleBlock merkleBlock
;
1117 LOCK(pfrom
->cs_filter
);
1118 if (pfrom
->pfilter
) {
1119 sendMerkleBlock
= true;
1120 merkleBlock
= CMerkleBlock(*pblock
, *pfrom
->pfilter
);
1123 if (sendMerkleBlock
) {
1124 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::MERKLEBLOCK
, merkleBlock
));
1125 // CMerkleBlock just contains hashes, so also push any transactions in the block the client did not see
1126 // This avoids hurting performance by pointlessly requiring a round-trip
1127 // Note that there is currently no way for a node to request any single transactions we didn't send here -
1128 // they must either disconnect and retry or request the full block.
1129 // Thus, the protocol spec specified allows for us to provide duplicate txn here,
1130 // however we MUST always provide at least what the remote peer needs
1131 typedef std::pair
<unsigned int, uint256
> PairType
;
1132 for (PairType
& pair
: merkleBlock
.vMatchedTxn
)
1133 connman
->PushMessage(pfrom
, msgMaker
.Make(SERIALIZE_TRANSACTION_NO_WITNESS
, NetMsgType::TX
, *pblock
->vtx
[pair
.first
]));
1138 else if (inv
.type
== MSG_CMPCT_BLOCK
)
1140 // If a peer is asking for old blocks, we're almost guaranteed
1141 // they won't have a useful mempool to match against a compact block,
1142 // and we don't feel like constructing the object for them, so
1143 // instead we respond with the full, non-compact block.
1144 bool fPeerWantsWitness
= State(pfrom
->GetId())->fWantsCmpctWitness
;
1145 int nSendFlags
= fPeerWantsWitness
? 0 : SERIALIZE_TRANSACTION_NO_WITNESS
;
1146 if (CanDirectFetch(consensusParams
) && mi
->second
->nHeight
>= chainActive
.Height() - MAX_CMPCTBLOCK_DEPTH
) {
1147 if ((fPeerWantsWitness
|| !fWitnessesPresentInARecentCompactBlock
) && a_recent_compact_block
&& a_recent_compact_block
->header
.GetHash() == mi
->second
->GetBlockHash()) {
1148 connman
->PushMessage(pfrom
, msgMaker
.Make(nSendFlags
, NetMsgType::CMPCTBLOCK
, *a_recent_compact_block
));
1150 CBlockHeaderAndShortTxIDs
cmpctblock(*pblock
, fPeerWantsWitness
);
1151 connman
->PushMessage(pfrom
, msgMaker
.Make(nSendFlags
, NetMsgType::CMPCTBLOCK
, cmpctblock
));
1154 connman
->PushMessage(pfrom
, msgMaker
.Make(nSendFlags
, NetMsgType::BLOCK
, *pblock
));
1158 // Trigger the peer node to send a getblocks request for the next batch of inventory
1159 if (inv
.hash
== pfrom
->hashContinue
)
1161 // Bypass PushInventory, this must send even if redundant,
1162 // and we want it right after the last block so they don't
1163 // wait for other stuff first.
1164 std::vector
<CInv
> vInv
;
1165 vInv
.push_back(CInv(MSG_BLOCK
, chainActive
.Tip()->GetBlockHash()));
1166 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::INV
, vInv
));
1167 pfrom
->hashContinue
.SetNull();
1171 else if (inv
.type
== MSG_TX
|| inv
.type
== MSG_WITNESS_TX
)
1173 // Send stream from relay memory
1175 auto mi
= mapRelay
.find(inv
.hash
);
1176 int nSendFlags
= (inv
.type
== MSG_TX
? SERIALIZE_TRANSACTION_NO_WITNESS
: 0);
1177 if (mi
!= mapRelay
.end()) {
1178 connman
->PushMessage(pfrom
, msgMaker
.Make(nSendFlags
, NetMsgType::TX
, *mi
->second
));
1180 } else if (pfrom
->timeLastMempoolReq
) {
1181 auto txinfo
= mempool
.info(inv
.hash
);
1182 // To protect privacy, do not answer getdata using the mempool when
1183 // that TX couldn't have been INVed in reply to a MEMPOOL request.
1184 if (txinfo
.tx
&& txinfo
.nTime
<= pfrom
->timeLastMempoolReq
) {
1185 connman
->PushMessage(pfrom
, msgMaker
.Make(nSendFlags
, NetMsgType::TX
, *txinfo
.tx
));
1190 vNotFound
.push_back(inv
);
1194 // Track requests for our stuff.
1195 GetMainSignals().Inventory(inv
.hash
);
1197 if (inv
.type
== MSG_BLOCK
|| inv
.type
== MSG_FILTERED_BLOCK
|| inv
.type
== MSG_CMPCT_BLOCK
|| inv
.type
== MSG_WITNESS_BLOCK
)
1202 pfrom
->vRecvGetData
.erase(pfrom
->vRecvGetData
.begin(), it
);
1204 if (!vNotFound
.empty()) {
1205 // Let the peer know that we didn't find what it asked for, so it doesn't
1206 // have to wait around forever. Currently only SPV clients actually care
1207 // about this message: it's needed when they are recursively walking the
1208 // dependencies of relevant unconfirmed transactions. SPV clients want to
1209 // do that because they want to know about (and store and rebroadcast and
1210 // risk analyze) the dependencies of transactions relevant to them, without
1211 // having to download the entire memory pool.
1212 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::NOTFOUND
, vNotFound
));
1216 uint32_t GetFetchFlags(CNode
* pfrom
) {
1217 uint32_t nFetchFlags
= 0;
1218 if ((pfrom
->GetLocalServices() & NODE_WITNESS
) && State(pfrom
->GetId())->fHaveWitness
) {
1219 nFetchFlags
|= MSG_WITNESS_FLAG
;
1224 inline void static SendBlockTransactions(const CBlock
& block
, const BlockTransactionsRequest
& req
, CNode
* pfrom
, CConnman
* connman
) {
1225 BlockTransactions
resp(req
);
1226 for (size_t i
= 0; i
< req
.indexes
.size(); i
++) {
1227 if (req
.indexes
[i
] >= block
.vtx
.size()) {
1229 Misbehaving(pfrom
->GetId(), 100);
1230 LogPrintf("Peer %d sent us a getblocktxn with out-of-bounds tx indices", pfrom
->GetId());
1233 resp
.txn
[i
] = block
.vtx
[req
.indexes
[i
]];
1236 const CNetMsgMaker
msgMaker(pfrom
->GetSendVersion());
1237 int nSendFlags
= State(pfrom
->GetId())->fWantsCmpctWitness
? 0 : SERIALIZE_TRANSACTION_NO_WITNESS
;
1238 connman
->PushMessage(pfrom
, msgMaker
.Make(nSendFlags
, NetMsgType::BLOCKTXN
, resp
));
1241 bool static ProcessHeadersMessage(CNode
*pfrom
, CConnman
*connman
, const std::vector
<CBlockHeader
>& headers
, const CChainParams
& chainparams
, bool punish_duplicate_invalid
)
1243 const CNetMsgMaker
msgMaker(pfrom
->GetSendVersion());
1244 size_t nCount
= headers
.size();
1247 // Nothing interesting. Stop asking this peers for more headers.
1251 bool received_new_header
= false;
1252 const CBlockIndex
*pindexLast
= nullptr;
1255 CNodeState
*nodestate
= State(pfrom
->GetId());
1257 // If this looks like it could be a block announcement (nCount <
1258 // MAX_BLOCKS_TO_ANNOUNCE), use special logic for handling headers that
1260 // - Send a getheaders message in response to try to connect the chain.
1261 // - The peer can send up to MAX_UNCONNECTING_HEADERS in a row that
1262 // don't connect before giving DoS points
1263 // - Once a headers message is received that is valid and does connect,
1264 // nUnconnectingHeaders gets reset back to 0.
1265 if (mapBlockIndex
.find(headers
[0].hashPrevBlock
) == mapBlockIndex
.end() && nCount
< MAX_BLOCKS_TO_ANNOUNCE
) {
1266 nodestate
->nUnconnectingHeaders
++;
1267 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETHEADERS
, chainActive
.GetLocator(pindexBestHeader
), uint256()));
1268 LogPrint(BCLog::NET
, "received header %s: missing prev block %s, sending getheaders (%d) to end (peer=%d, nUnconnectingHeaders=%d)\n",
1269 headers
[0].GetHash().ToString(),
1270 headers
[0].hashPrevBlock
.ToString(),
1271 pindexBestHeader
->nHeight
,
1272 pfrom
->GetId(), nodestate
->nUnconnectingHeaders
);
1273 // Set hashLastUnknownBlock for this peer, so that if we
1274 // eventually get the headers - even from a different peer -
1275 // we can use this peer to download.
1276 UpdateBlockAvailability(pfrom
->GetId(), headers
.back().GetHash());
1278 if (nodestate
->nUnconnectingHeaders
% MAX_UNCONNECTING_HEADERS
== 0) {
1279 Misbehaving(pfrom
->GetId(), 20);
1284 uint256 hashLastBlock
;
1285 for (const CBlockHeader
& header
: headers
) {
1286 if (!hashLastBlock
.IsNull() && header
.hashPrevBlock
!= hashLastBlock
) {
1287 Misbehaving(pfrom
->GetId(), 20);
1288 return error("non-continuous headers sequence");
1290 hashLastBlock
= header
.GetHash();
1293 // If we don't have the last header, then they'll have given us
1294 // something new (if these headers are valid).
1295 if (mapBlockIndex
.find(hashLastBlock
) == mapBlockIndex
.end()) {
1296 received_new_header
= true;
1300 CValidationState state
;
1301 CBlockHeader first_invalid_header
;
1302 if (!ProcessNewBlockHeaders(headers
, state
, chainparams
, &pindexLast
, &first_invalid_header
)) {
1304 if (state
.IsInvalid(nDoS
)) {
1307 Misbehaving(pfrom
->GetId(), nDoS
);
1309 if (punish_duplicate_invalid
&& mapBlockIndex
.find(first_invalid_header
.GetHash()) != mapBlockIndex
.end()) {
1310 // Goal: don't allow outbound peers to use up our outbound
1311 // connection slots if they are on incompatible chains.
1313 // We ask the caller to set punish_invalid appropriately based
1314 // on the peer and the method of header delivery (compact
1315 // blocks are allowed to be invalid in some circumstances,
1317 // Here, we try to detect the narrow situation that we have a
1318 // valid block header (ie it was valid at the time the header
1319 // was received, and hence stored in mapBlockIndex) but know the
1320 // block is invalid, and that a peer has announced that same
1321 // block as being on its active chain.
1322 // Disconnect the peer in such a situation.
1324 // Note: if the header that is invalid was not accepted to our
1325 // mapBlockIndex at all, that may also be grounds for
1326 // disconnecting the peer, as the chain they are on is likely
1327 // to be incompatible. However, there is a circumstance where
1328 // that does not hold: if the header's timestamp is more than
1329 // 2 hours ahead of our current time. In that case, the header
1330 // may become valid in the future, and we don't want to
1331 // disconnect a peer merely for serving us one too-far-ahead
1332 // block header, to prevent an attacker from splitting the
1333 // network by mining a block right at the 2 hour boundary.
1335 // TODO: update the DoS logic (or, rather, rewrite the
1336 // DoS-interface between validation and net_processing) so that
1337 // the interface is cleaner, and so that we disconnect on all the
1338 // reasons that a peer's headers chain is incompatible
1339 // with ours (eg block->nVersion softforks, MTP violations,
1340 // etc), and not just the duplicate-invalid case.
1341 pfrom
->fDisconnect
= true;
1343 return error("invalid header received");
1349 CNodeState
*nodestate
= State(pfrom
->GetId());
1350 if (nodestate
->nUnconnectingHeaders
> 0) {
1351 LogPrint(BCLog::NET
, "peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n", pfrom
->GetId(), nodestate
->nUnconnectingHeaders
);
1353 nodestate
->nUnconnectingHeaders
= 0;
1356 UpdateBlockAvailability(pfrom
->GetId(), pindexLast
->GetBlockHash());
1358 // From here, pindexBestKnownBlock should be guaranteed to be non-null,
1359 // because it is set in UpdateBlockAvailability. Some nullptr checks
1360 // are still present, however, as belt-and-suspenders.
1362 if (received_new_header
&& pindexLast
->nChainWork
> chainActive
.Tip()->nChainWork
) {
1363 nodestate
->m_last_block_announcement
= GetTime();
1366 if (nCount
== MAX_HEADERS_RESULTS
) {
1367 // Headers message had its maximum size; the peer may have more headers.
1368 // TODO: optimize: if pindexLast is an ancestor of chainActive.Tip or pindexBestHeader, continue
1369 // from there instead.
1370 LogPrint(BCLog::NET
, "more getheaders (%d) to end to peer=%d (startheight:%d)\n", pindexLast
->nHeight
, pfrom
->GetId(), pfrom
->nStartingHeight
);
1371 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETHEADERS
, chainActive
.GetLocator(pindexLast
), uint256()));
1374 bool fCanDirectFetch
= CanDirectFetch(chainparams
.GetConsensus());
1375 // If this set of headers is valid and ends in a block with at least as
1376 // much work as our tip, download as much as possible.
1377 if (fCanDirectFetch
&& pindexLast
->IsValid(BLOCK_VALID_TREE
) && chainActive
.Tip()->nChainWork
<= pindexLast
->nChainWork
) {
1378 std::vector
<const CBlockIndex
*> vToFetch
;
1379 const CBlockIndex
*pindexWalk
= pindexLast
;
1380 // Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
1381 while (pindexWalk
&& !chainActive
.Contains(pindexWalk
) && vToFetch
.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER
) {
1382 if (!(pindexWalk
->nStatus
& BLOCK_HAVE_DATA
) &&
1383 !mapBlocksInFlight
.count(pindexWalk
->GetBlockHash()) &&
1384 (!IsWitnessEnabled(pindexWalk
->pprev
, chainparams
.GetConsensus()) || State(pfrom
->GetId())->fHaveWitness
)) {
1385 // We don't have this block, and it's not yet in flight.
1386 vToFetch
.push_back(pindexWalk
);
1388 pindexWalk
= pindexWalk
->pprev
;
1390 // If pindexWalk still isn't on our main chain, we're looking at a
1391 // very large reorg at a time we think we're close to caught up to
1392 // the main chain -- this shouldn't really happen. Bail out on the
1393 // direct fetch and rely on parallel download instead.
1394 if (!chainActive
.Contains(pindexWalk
)) {
1395 LogPrint(BCLog::NET
, "Large reorg, won't direct fetch to %s (%d)\n",
1396 pindexLast
->GetBlockHash().ToString(),
1397 pindexLast
->nHeight
);
1399 std::vector
<CInv
> vGetData
;
1400 // Download as much as possible, from earliest to latest.
1401 for (const CBlockIndex
*pindex
: reverse_iterate(vToFetch
)) {
1402 if (nodestate
->nBlocksInFlight
>= MAX_BLOCKS_IN_TRANSIT_PER_PEER
) {
1403 // Can't download any more from this peer
1406 uint32_t nFetchFlags
= GetFetchFlags(pfrom
);
1407 vGetData
.push_back(CInv(MSG_BLOCK
| nFetchFlags
, pindex
->GetBlockHash()));
1408 MarkBlockAsInFlight(pfrom
->GetId(), pindex
->GetBlockHash(), pindex
);
1409 LogPrint(BCLog::NET
, "Requesting block %s from peer=%d\n",
1410 pindex
->GetBlockHash().ToString(), pfrom
->GetId());
1412 if (vGetData
.size() > 1) {
1413 LogPrint(BCLog::NET
, "Downloading blocks toward %s (%d) via headers direct fetch\n",
1414 pindexLast
->GetBlockHash().ToString(), pindexLast
->nHeight
);
1416 if (vGetData
.size() > 0) {
1417 if (nodestate
->fSupportsDesiredCmpctVersion
&& vGetData
.size() == 1 && mapBlocksInFlight
.size() == 1 && pindexLast
->pprev
->IsValid(BLOCK_VALID_CHAIN
)) {
1418 // In any case, we want to download using a compact block, not a regular one
1419 vGetData
[0] = CInv(MSG_CMPCT_BLOCK
, vGetData
[0].hash
);
1421 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETDATA
, vGetData
));
1425 // If we're in IBD, we want outbound peers that will serve us a useful
1426 // chain. Disconnect peers that are on chains with insufficient work.
1427 if (IsInitialBlockDownload() && nCount
!= MAX_HEADERS_RESULTS
) {
1428 // When nCount < MAX_HEADERS_RESULTS, we know we have no more
1429 // headers to fetch from this peer.
1430 if (nodestate
->pindexBestKnownBlock
&& nodestate
->pindexBestKnownBlock
->nChainWork
< nMinimumChainWork
) {
1431 // This peer has too little work on their headers chain to help
1432 // us sync -- disconnect if using an outbound slot (unless
1433 // whitelisted or addnode).
1434 // Note: We compare their tip to nMinimumChainWork (rather than
1435 // chainActive.Tip()) because we won't start block download
1436 // until we have a headers chain that has at least
1437 // nMinimumChainWork, even if a peer has a chain past our tip,
1438 // as an anti-DoS measure.
1439 if (IsOutboundDisconnectionCandidate(pfrom
)) {
1440 LogPrintf("Disconnecting outbound peer %d -- headers chain has insufficient work\n", pfrom
->GetId());
1441 pfrom
->fDisconnect
= true;
1446 if (!pfrom
->fDisconnect
&& IsOutboundDisconnectionCandidate(pfrom
) && nodestate
->pindexBestKnownBlock
!= nullptr) {
1447 // If this is an outbound peer, check to see if we should protect
1448 // it from the bad/lagging chain logic.
1449 if (g_outbound_peers_with_protect_from_disconnect
< MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT
&& nodestate
->pindexBestKnownBlock
->nChainWork
>= chainActive
.Tip()->nChainWork
&& !nodestate
->m_chain_sync
.m_protect
) {
1450 LogPrint(BCLog::NET
, "Protecting outbound peer=%d from eviction\n", pfrom
->GetId());
1451 nodestate
->m_chain_sync
.m_protect
= true;
1452 ++g_outbound_peers_with_protect_from_disconnect
;
1460 bool static ProcessMessage(CNode
* pfrom
, const std::string
& strCommand
, CDataStream
& vRecv
, int64_t nTimeReceived
, const CChainParams
& chainparams
, CConnman
* connman
, const std::atomic
<bool>& interruptMsgProc
)
1462 LogPrint(BCLog::NET
, "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand
), vRecv
.size(), pfrom
->GetId());
1463 if (gArgs
.IsArgSet("-dropmessagestest") && GetRand(gArgs
.GetArg("-dropmessagestest", 0)) == 0)
1465 LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n");
1470 if (!(pfrom
->GetLocalServices() & NODE_BLOOM
) &&
1471 (strCommand
== NetMsgType::FILTERLOAD
||
1472 strCommand
== NetMsgType::FILTERADD
))
1474 if (pfrom
->nVersion
>= NO_BLOOM_VERSION
) {
1476 Misbehaving(pfrom
->GetId(), 100);
1479 pfrom
->fDisconnect
= true;
1484 if (strCommand
== NetMsgType::REJECT
)
1486 if (LogAcceptCategory(BCLog::NET
)) {
1488 std::string strMsg
; unsigned char ccode
; std::string strReason
;
1489 vRecv
>> LIMITED_STRING(strMsg
, CMessageHeader::COMMAND_SIZE
) >> ccode
>> LIMITED_STRING(strReason
, MAX_REJECT_MESSAGE_LENGTH
);
1491 std::ostringstream ss
;
1492 ss
<< strMsg
<< " code " << itostr(ccode
) << ": " << strReason
;
1494 if (strMsg
== NetMsgType::BLOCK
|| strMsg
== NetMsgType::TX
)
1498 ss
<< ": hash " << hash
.ToString();
1500 LogPrint(BCLog::NET
, "Reject %s\n", SanitizeString(ss
.str()));
1501 } catch (const std::ios_base::failure
&) {
1502 // Avoid feedback loops by preventing reject messages from triggering a new reject message.
1503 LogPrint(BCLog::NET
, "Unparseable reject message received\n");
1508 else if (strCommand
== NetMsgType::VERSION
)
1510 // Each connection can only send one version message
1511 if (pfrom
->nVersion
!= 0)
1513 connman
->PushMessage(pfrom
, CNetMsgMaker(INIT_PROTO_VERSION
).Make(NetMsgType::REJECT
, strCommand
, REJECT_DUPLICATE
, std::string("Duplicate version message")));
1515 Misbehaving(pfrom
->GetId(), 1);
1522 uint64_t nNonce
= 1;
1523 uint64_t nServiceInt
;
1524 ServiceFlags nServices
;
1527 std::string strSubVer
;
1528 std::string cleanSubVer
;
1529 int nStartingHeight
= -1;
1532 vRecv
>> nVersion
>> nServiceInt
>> nTime
>> addrMe
;
1533 nSendVersion
= std::min(nVersion
, PROTOCOL_VERSION
);
1534 nServices
= ServiceFlags(nServiceInt
);
1535 if (!pfrom
->fInbound
)
1537 connman
->SetServices(pfrom
->addr
, nServices
);
1539 if (!pfrom
->fInbound
&& !pfrom
->fFeeler
&& !pfrom
->m_manual_connection
&& !HasAllDesirableServiceFlags(nServices
))
1541 LogPrint(BCLog::NET
, "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom
->GetId(), nServices
, GetDesirableServiceFlags(nServices
));
1542 connman
->PushMessage(pfrom
, CNetMsgMaker(INIT_PROTO_VERSION
).Make(NetMsgType::REJECT
, strCommand
, REJECT_NONSTANDARD
,
1543 strprintf("Expected to offer services %08x", GetDesirableServiceFlags(nServices
))));
1544 pfrom
->fDisconnect
= true;
1548 if (nServices
& ((1 << 7) | (1 << 5))) {
1549 if (GetTime() < 1533096000) {
1550 // Immediately disconnect peers that use service bits 6 or 8 until August 1st, 2018
1551 // These bits have been used as a flag to indicate that a node is running incompatible
1552 // consensus rules instead of changing the network magic, so we're stuck disconnecting
1553 // based on these service bits, at least for a while.
1554 pfrom
->fDisconnect
= true;
1559 if (nVersion
< MIN_PEER_PROTO_VERSION
)
1561 // disconnect from peers older than this proto version
1562 LogPrintf("peer=%d using obsolete version %i; disconnecting\n", pfrom
->GetId(), nVersion
);
1563 connman
->PushMessage(pfrom
, CNetMsgMaker(INIT_PROTO_VERSION
).Make(NetMsgType::REJECT
, strCommand
, REJECT_OBSOLETE
,
1564 strprintf("Version must be %d or greater", MIN_PEER_PROTO_VERSION
)));
1565 pfrom
->fDisconnect
= true;
1569 if (nVersion
== 10300)
1572 vRecv
>> addrFrom
>> nNonce
;
1573 if (!vRecv
.empty()) {
1574 vRecv
>> LIMITED_STRING(strSubVer
, MAX_SUBVERSION_LENGTH
);
1575 cleanSubVer
= SanitizeString(strSubVer
);
1577 if (!vRecv
.empty()) {
1578 vRecv
>> nStartingHeight
;
1582 // Disconnect if we connected to ourself
1583 if (pfrom
->fInbound
&& !connman
->CheckIncomingNonce(nNonce
))
1585 LogPrintf("connected to self at %s, disconnecting\n", pfrom
->addr
.ToString());
1586 pfrom
->fDisconnect
= true;
1590 if (pfrom
->fInbound
&& addrMe
.IsRoutable())
1595 // Be shy and don't send version until we hear
1596 if (pfrom
->fInbound
)
1597 PushNodeVersion(pfrom
, connman
, GetAdjustedTime());
1599 connman
->PushMessage(pfrom
, CNetMsgMaker(INIT_PROTO_VERSION
).Make(NetMsgType::VERACK
));
1601 pfrom
->nServices
= nServices
;
1602 pfrom
->SetAddrLocal(addrMe
);
1604 LOCK(pfrom
->cs_SubVer
);
1605 pfrom
->strSubVer
= strSubVer
;
1606 pfrom
->cleanSubVer
= cleanSubVer
;
1608 pfrom
->nStartingHeight
= nStartingHeight
;
1609 pfrom
->fClient
= !(nServices
& NODE_NETWORK
);
1611 LOCK(pfrom
->cs_filter
);
1612 pfrom
->fRelayTxes
= fRelay
; // set to true after we get the first filter* message
1616 pfrom
->SetSendVersion(nSendVersion
);
1617 pfrom
->nVersion
= nVersion
;
1619 if((nServices
& NODE_WITNESS
))
1622 State(pfrom
->GetId())->fHaveWitness
= true;
1625 // Potentially mark this peer as a preferred download peer.
1628 UpdatePreferredDownload(pfrom
, State(pfrom
->GetId()));
1631 if (!pfrom
->fInbound
)
1633 // Advertise our address
1634 if (fListen
&& !IsInitialBlockDownload())
1636 CAddress addr
= GetLocalAddress(&pfrom
->addr
, pfrom
->GetLocalServices());
1637 FastRandomContext insecure_rand
;
1638 if (addr
.IsRoutable())
1640 LogPrint(BCLog::NET
, "ProcessMessages: advertising address %s\n", addr
.ToString());
1641 pfrom
->PushAddress(addr
, insecure_rand
);
1642 } else if (IsPeerAddrLocalGood(pfrom
)) {
1644 LogPrint(BCLog::NET
, "ProcessMessages: advertising address %s\n", addr
.ToString());
1645 pfrom
->PushAddress(addr
, insecure_rand
);
1649 // Get recent addresses
1650 if (pfrom
->fOneShot
|| pfrom
->nVersion
>= CADDR_TIME_VERSION
|| connman
->GetAddressCount() < 1000)
1652 connman
->PushMessage(pfrom
, CNetMsgMaker(nSendVersion
).Make(NetMsgType::GETADDR
));
1653 pfrom
->fGetAddr
= true;
1655 connman
->MarkAddressGood(pfrom
->addr
);
1658 std::string remoteAddr
;
1660 remoteAddr
= ", peeraddr=" + pfrom
->addr
.ToString();
1662 LogPrintf("receive version message: %s: version %d, blocks=%d, us=%s, peer=%d%s\n",
1663 cleanSubVer
, pfrom
->nVersion
,
1664 pfrom
->nStartingHeight
, addrMe
.ToString(), pfrom
->GetId(),
1667 int64_t nTimeOffset
= nTime
- GetTime();
1668 pfrom
->nTimeOffset
= nTimeOffset
;
1669 AddTimeData(pfrom
->addr
, nTimeOffset
);
1671 // If the peer is old enough to have the old alert system, send it the final alert.
1672 if (pfrom
->nVersion
<= 70012) {
1673 CDataStream
finalAlert(ParseHex("60010000000000000000000000ffffff7f00000000ffffff7ffeffff7f01ffffff7f00000000ffffff7f00ffffff7f002f555247454e543a20416c657274206b657920636f6d70726f6d697365642c2075706772616465207265717569726564004630440220653febd6410f470f6bae11cad19c48413becb1ac2c17f908fd0fd53bdc3abd5202206d0e9c96fe88d4a0f01ed9dedae2b6f9e00da94cad0fecaae66ecf689bf71b50"), SER_NETWORK
, PROTOCOL_VERSION
);
1674 connman
->PushMessage(pfrom
, CNetMsgMaker(nSendVersion
).Make("alert", finalAlert
));
1677 // Feeler connections exist only to verify if address is online.
1678 if (pfrom
->fFeeler
) {
1679 assert(pfrom
->fInbound
== false);
1680 pfrom
->fDisconnect
= true;
1686 else if (pfrom
->nVersion
== 0)
1688 // Must have a version message before anything else
1690 Misbehaving(pfrom
->GetId(), 1);
1694 // At this point, the outgoing message serialization version can't change.
1695 const CNetMsgMaker
msgMaker(pfrom
->GetSendVersion());
1697 if (strCommand
== NetMsgType::VERACK
)
1699 pfrom
->SetRecvVersion(std::min(pfrom
->nVersion
.load(), PROTOCOL_VERSION
));
1701 if (!pfrom
->fInbound
) {
1702 // Mark this node as currently connected, so we update its timestamp later.
1704 State(pfrom
->GetId())->fCurrentlyConnected
= true;
1707 if (pfrom
->nVersion
>= SENDHEADERS_VERSION
) {
1708 // Tell our peer we prefer to receive headers rather than inv's
1709 // We send this to non-NODE NETWORK peers as well, because even
1710 // non-NODE NETWORK peers can announce blocks (such as pruning
1712 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::SENDHEADERS
));
1714 if (pfrom
->nVersion
>= SHORT_IDS_BLOCKS_VERSION
) {
1715 // Tell our peer we are willing to provide version 1 or 2 cmpctblocks
1716 // However, we do not request new block announcements using
1717 // cmpctblock messages.
1718 // We send this to non-NODE NETWORK peers as well, because
1719 // they may wish to request compact blocks from us
1720 bool fAnnounceUsingCMPCTBLOCK
= false;
1721 uint64_t nCMPCTBLOCKVersion
= 2;
1722 if (pfrom
->GetLocalServices() & NODE_WITNESS
)
1723 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::SENDCMPCT
, fAnnounceUsingCMPCTBLOCK
, nCMPCTBLOCKVersion
));
1724 nCMPCTBLOCKVersion
= 1;
1725 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::SENDCMPCT
, fAnnounceUsingCMPCTBLOCK
, nCMPCTBLOCKVersion
));
1727 pfrom
->fSuccessfullyConnected
= true;
1730 else if (!pfrom
->fSuccessfullyConnected
)
1732 // Must have a verack message before anything else
1734 Misbehaving(pfrom
->GetId(), 1);
1738 else if (strCommand
== NetMsgType::ADDR
)
1740 std::vector
<CAddress
> vAddr
;
1743 // Don't want addr from older versions unless seeding
1744 if (pfrom
->nVersion
< CADDR_TIME_VERSION
&& connman
->GetAddressCount() > 1000)
1746 if (vAddr
.size() > 1000)
1749 Misbehaving(pfrom
->GetId(), 20);
1750 return error("message addr size() = %u", vAddr
.size());
1753 // Store the new addresses
1754 std::vector
<CAddress
> vAddrOk
;
1755 int64_t nNow
= GetAdjustedTime();
1756 int64_t nSince
= nNow
- 10 * 60;
1757 for (CAddress
& addr
: vAddr
)
1759 if (interruptMsgProc
)
1762 // We only bother storing full nodes, though this may include
1763 // things which we would not make an outbound connection to, in
1764 // part because we may make feeler connections to them.
1765 if (!MayHaveUsefulAddressDB(addr
.nServices
))
1768 if (addr
.nTime
<= 100000000 || addr
.nTime
> nNow
+ 10 * 60)
1769 addr
.nTime
= nNow
- 5 * 24 * 60 * 60;
1770 pfrom
->AddAddressKnown(addr
);
1771 bool fReachable
= IsReachable(addr
);
1772 if (addr
.nTime
> nSince
&& !pfrom
->fGetAddr
&& vAddr
.size() <= 10 && addr
.IsRoutable())
1774 // Relay to a limited number of other nodes
1775 RelayAddress(addr
, fReachable
, connman
);
1777 // Do not store addresses outside our network
1779 vAddrOk
.push_back(addr
);
1781 connman
->AddNewAddresses(vAddrOk
, pfrom
->addr
, 2 * 60 * 60);
1782 if (vAddr
.size() < 1000)
1783 pfrom
->fGetAddr
= false;
1784 if (pfrom
->fOneShot
)
1785 pfrom
->fDisconnect
= true;
1788 else if (strCommand
== NetMsgType::SENDHEADERS
)
1791 State(pfrom
->GetId())->fPreferHeaders
= true;
1794 else if (strCommand
== NetMsgType::SENDCMPCT
)
1796 bool fAnnounceUsingCMPCTBLOCK
= false;
1797 uint64_t nCMPCTBLOCKVersion
= 0;
1798 vRecv
>> fAnnounceUsingCMPCTBLOCK
>> nCMPCTBLOCKVersion
;
1799 if (nCMPCTBLOCKVersion
== 1 || ((pfrom
->GetLocalServices() & NODE_WITNESS
) && nCMPCTBLOCKVersion
== 2)) {
1801 // fProvidesHeaderAndIDs is used to "lock in" version of compact blocks we send (fWantsCmpctWitness)
1802 if (!State(pfrom
->GetId())->fProvidesHeaderAndIDs
) {
1803 State(pfrom
->GetId())->fProvidesHeaderAndIDs
= true;
1804 State(pfrom
->GetId())->fWantsCmpctWitness
= nCMPCTBLOCKVersion
== 2;
1806 if (State(pfrom
->GetId())->fWantsCmpctWitness
== (nCMPCTBLOCKVersion
== 2)) // ignore later version announces
1807 State(pfrom
->GetId())->fPreferHeaderAndIDs
= fAnnounceUsingCMPCTBLOCK
;
1808 if (!State(pfrom
->GetId())->fSupportsDesiredCmpctVersion
) {
1809 if (pfrom
->GetLocalServices() & NODE_WITNESS
)
1810 State(pfrom
->GetId())->fSupportsDesiredCmpctVersion
= (nCMPCTBLOCKVersion
== 2);
1812 State(pfrom
->GetId())->fSupportsDesiredCmpctVersion
= (nCMPCTBLOCKVersion
== 1);
1818 else if (strCommand
== NetMsgType::INV
)
1820 std::vector
<CInv
> vInv
;
1822 if (vInv
.size() > MAX_INV_SZ
)
1825 Misbehaving(pfrom
->GetId(), 20);
1826 return error("message inv size() = %u", vInv
.size());
1829 bool fBlocksOnly
= !fRelayTxes
;
1831 // Allow whitelisted peers to send data other than blocks in blocks only mode if whitelistrelay is true
1832 if (pfrom
->fWhitelisted
&& gArgs
.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY
))
1833 fBlocksOnly
= false;
1837 uint32_t nFetchFlags
= GetFetchFlags(pfrom
);
1839 for (CInv
&inv
: vInv
)
1841 if (interruptMsgProc
)
1844 bool fAlreadyHave
= AlreadyHave(inv
);
1845 LogPrint(BCLog::NET
, "got inv: %s %s peer=%d\n", inv
.ToString(), fAlreadyHave
? "have" : "new", pfrom
->GetId());
1847 if (inv
.type
== MSG_TX
) {
1848 inv
.type
|= nFetchFlags
;
1851 if (inv
.type
== MSG_BLOCK
) {
1852 UpdateBlockAvailability(pfrom
->GetId(), inv
.hash
);
1853 if (!fAlreadyHave
&& !fImporting
&& !fReindex
&& !mapBlocksInFlight
.count(inv
.hash
)) {
1854 // We used to request the full block here, but since headers-announcements are now the
1855 // primary method of announcement on the network, and since, in the case that a node
1856 // fell back to inv we probably have a reorg which we should get the headers for first,
1857 // we now only provide a getheaders response here. When we receive the headers, we will
1858 // then ask for the blocks we need.
1859 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETHEADERS
, chainActive
.GetLocator(pindexBestHeader
), inv
.hash
));
1860 LogPrint(BCLog::NET
, "getheaders (%d) %s to peer=%d\n", pindexBestHeader
->nHeight
, inv
.hash
.ToString(), pfrom
->GetId());
1865 pfrom
->AddInventoryKnown(inv
);
1867 LogPrint(BCLog::NET
, "transaction (%s) inv sent in violation of protocol peer=%d\n", inv
.hash
.ToString(), pfrom
->GetId());
1868 } else if (!fAlreadyHave
&& !fImporting
&& !fReindex
&& !IsInitialBlockDownload()) {
1873 // Track requests for our stuff
1874 GetMainSignals().Inventory(inv
.hash
);
1879 else if (strCommand
== NetMsgType::GETDATA
)
1881 std::vector
<CInv
> vInv
;
1883 if (vInv
.size() > MAX_INV_SZ
)
1886 Misbehaving(pfrom
->GetId(), 20);
1887 return error("message getdata size() = %u", vInv
.size());
1890 LogPrint(BCLog::NET
, "received getdata (%u invsz) peer=%d\n", vInv
.size(), pfrom
->GetId());
1892 if (vInv
.size() > 0) {
1893 LogPrint(BCLog::NET
, "received getdata for: %s peer=%d\n", vInv
[0].ToString(), pfrom
->GetId());
1896 pfrom
->vRecvGetData
.insert(pfrom
->vRecvGetData
.end(), vInv
.begin(), vInv
.end());
1897 ProcessGetData(pfrom
, chainparams
.GetConsensus(), connman
, interruptMsgProc
);
1901 else if (strCommand
== NetMsgType::GETBLOCKS
)
1903 CBlockLocator locator
;
1905 vRecv
>> locator
>> hashStop
;
1907 // We might have announced the currently-being-connected tip using a
1908 // compact block, which resulted in the peer sending a getblocks
1909 // request, which we would otherwise respond to without the new block.
1910 // To avoid this situation we simply verify that we are on our best
1911 // known chain now. This is super overkill, but we handle it better
1912 // for getheaders requests, and there are no known nodes which support
1913 // compact blocks but still use getblocks to request blocks.
1915 std::shared_ptr
<const CBlock
> a_recent_block
;
1917 LOCK(cs_most_recent_block
);
1918 a_recent_block
= most_recent_block
;
1920 CValidationState dummy
;
1921 ActivateBestChain(dummy
, Params(), a_recent_block
);
1926 // Find the last block the caller has in the main chain
1927 const CBlockIndex
* pindex
= FindForkInGlobalIndex(chainActive
, locator
);
1929 // Send the rest of the chain
1931 pindex
= chainActive
.Next(pindex
);
1933 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());
1934 for (; pindex
; pindex
= chainActive
.Next(pindex
))
1936 if (pindex
->GetBlockHash() == hashStop
)
1938 LogPrint(BCLog::NET
, " getblocks stopping at %d %s\n", pindex
->nHeight
, pindex
->GetBlockHash().ToString());
1941 // If pruning, don't inv blocks unless we have on disk and are likely to still have
1942 // for some reasonable time window (1 hour) that block relay might require.
1943 const int nPrunedBlocksLikelyToHave
= MIN_BLOCKS_TO_KEEP
- 3600 / chainparams
.GetConsensus().nPowTargetSpacing
;
1944 if (fPruneMode
&& (!(pindex
->nStatus
& BLOCK_HAVE_DATA
) || pindex
->nHeight
<= chainActive
.Tip()->nHeight
- nPrunedBlocksLikelyToHave
))
1946 LogPrint(BCLog::NET
, " getblocks stopping, pruned or too old block at %d %s\n", pindex
->nHeight
, pindex
->GetBlockHash().ToString());
1949 pfrom
->PushInventory(CInv(MSG_BLOCK
, pindex
->GetBlockHash()));
1952 // When this block is requested, we'll send an inv that'll
1953 // trigger the peer to getblocks the next batch of inventory.
1954 LogPrint(BCLog::NET
, " getblocks stopping at limit %d %s\n", pindex
->nHeight
, pindex
->GetBlockHash().ToString());
1955 pfrom
->hashContinue
= pindex
->GetBlockHash();
1962 else if (strCommand
== NetMsgType::GETBLOCKTXN
)
1964 BlockTransactionsRequest req
;
1967 std::shared_ptr
<const CBlock
> recent_block
;
1969 LOCK(cs_most_recent_block
);
1970 if (most_recent_block_hash
== req
.blockhash
)
1971 recent_block
= most_recent_block
;
1972 // Unlock cs_most_recent_block to avoid cs_main lock inversion
1975 SendBlockTransactions(*recent_block
, req
, pfrom
, connman
);
1981 BlockMap::iterator it
= mapBlockIndex
.find(req
.blockhash
);
1982 if (it
== mapBlockIndex
.end() || !(it
->second
->nStatus
& BLOCK_HAVE_DATA
)) {
1983 LogPrintf("Peer %d sent us a getblocktxn for a block we don't have", pfrom
->GetId());
1987 if (it
->second
->nHeight
< chainActive
.Height() - MAX_BLOCKTXN_DEPTH
) {
1988 // If an older block is requested (should never happen in practice,
1989 // but can happen in tests) send a block response instead of a
1990 // blocktxn response. Sending a full block response instead of a
1991 // small blocktxn response is preferable in the case where a peer
1992 // might maliciously send lots of getblocktxn requests to trigger
1993 // expensive disk reads, because it will require the peer to
1994 // actually receive all the data read from disk over the network.
1995 LogPrint(BCLog::NET
, "Peer %d sent us a getblocktxn for a block > %i deep", pfrom
->GetId(), MAX_BLOCKTXN_DEPTH
);
1997 inv
.type
= State(pfrom
->GetId())->fWantsCmpctWitness
? MSG_WITNESS_BLOCK
: MSG_BLOCK
;
1998 inv
.hash
= req
.blockhash
;
1999 pfrom
->vRecvGetData
.push_back(inv
);
2000 ProcessGetData(pfrom
, chainparams
.GetConsensus(), connman
, interruptMsgProc
);
2005 bool ret
= ReadBlockFromDisk(block
, it
->second
, chainparams
.GetConsensus());
2008 SendBlockTransactions(block
, req
, pfrom
, connman
);
2012 else if (strCommand
== NetMsgType::GETHEADERS
)
2014 CBlockLocator locator
;
2016 vRecv
>> locator
>> hashStop
;
2019 if (IsInitialBlockDownload() && !pfrom
->fWhitelisted
) {
2020 LogPrint(BCLog::NET
, "Ignoring getheaders from peer=%d because node is in initial block download\n", pfrom
->GetId());
2024 CNodeState
*nodestate
= State(pfrom
->GetId());
2025 const CBlockIndex
* pindex
= nullptr;
2026 if (locator
.IsNull())
2028 // If locator is null, return the hashStop block
2029 BlockMap::iterator mi
= mapBlockIndex
.find(hashStop
);
2030 if (mi
== mapBlockIndex
.end())
2032 pindex
= (*mi
).second
;
2034 if (!BlockRequestAllowed(pindex
, chainparams
.GetConsensus())) {
2035 LogPrintf("%s: ignoring request from peer=%i for old block header that isn't in the main chain\n", __func__
, pfrom
->GetId());
2041 // Find the last block the caller has in the main chain
2042 pindex
= FindForkInGlobalIndex(chainActive
, locator
);
2044 pindex
= chainActive
.Next(pindex
);
2047 // we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
2048 std::vector
<CBlock
> vHeaders
;
2049 int nLimit
= MAX_HEADERS_RESULTS
;
2050 LogPrint(BCLog::NET
, "getheaders %d to %s from peer=%d\n", (pindex
? pindex
->nHeight
: -1), hashStop
.IsNull() ? "end" : hashStop
.ToString(), pfrom
->GetId());
2051 for (; pindex
; pindex
= chainActive
.Next(pindex
))
2053 vHeaders
.push_back(pindex
->GetBlockHeader());
2054 if (--nLimit
<= 0 || pindex
->GetBlockHash() == hashStop
)
2057 // pindex can be nullptr either if we sent chainActive.Tip() OR
2058 // if our peer has chainActive.Tip() (and thus we are sending an empty
2059 // headers message). In both cases it's safe to update
2060 // pindexBestHeaderSent to be our tip.
2062 // It is important that we simply reset the BestHeaderSent value here,
2063 // and not max(BestHeaderSent, newHeaderSent). We might have announced
2064 // the currently-being-connected tip using a compact block, which
2065 // resulted in the peer sending a headers request, which we respond to
2066 // without the new block. By resetting the BestHeaderSent, we ensure we
2067 // will re-announce the new block via headers (or compact blocks again)
2068 // in the SendMessages logic.
2069 nodestate
->pindexBestHeaderSent
= pindex
? pindex
: chainActive
.Tip();
2070 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::HEADERS
, vHeaders
));
2074 else if (strCommand
== NetMsgType::TX
)
2076 // Stop processing the transaction early if
2077 // We are in blocks only mode and peer is either not whitelisted or whitelistrelay is off
2078 if (!fRelayTxes
&& (!pfrom
->fWhitelisted
|| !gArgs
.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY
)))
2080 LogPrint(BCLog::NET
, "transaction sent in violation of protocol peer=%d\n", pfrom
->GetId());
2084 std::deque
<COutPoint
> vWorkQueue
;
2085 std::vector
<uint256
> vEraseQueue
;
2086 CTransactionRef ptx
;
2088 const CTransaction
& tx
= *ptx
;
2090 CInv
inv(MSG_TX
, tx
.GetHash());
2091 pfrom
->AddInventoryKnown(inv
);
2095 bool fMissingInputs
= false;
2096 CValidationState state
;
2098 pfrom
->setAskFor
.erase(inv
.hash
);
2099 mapAlreadyAskedFor
.erase(inv
.hash
);
2101 std::list
<CTransactionRef
> lRemovedTxn
;
2103 if (!AlreadyHave(inv
) &&
2104 AcceptToMemoryPool(mempool
, state
, ptx
, &fMissingInputs
, &lRemovedTxn
, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
2105 mempool
.check(pcoinsTip
.get());
2106 RelayTransaction(tx
, connman
);
2107 for (unsigned int i
= 0; i
< tx
.vout
.size(); i
++) {
2108 vWorkQueue
.emplace_back(inv
.hash
, i
);
2111 pfrom
->nLastTXTime
= GetTime();
2113 LogPrint(BCLog::MEMPOOL
, "AcceptToMemoryPool: peer=%d: accepted %s (poolsz %u txn, %u kB)\n",
2115 tx
.GetHash().ToString(),
2116 mempool
.size(), mempool
.DynamicMemoryUsage() / 1000);
2118 // Recursively process any orphan transactions that depended on this one
2119 std::set
<NodeId
> setMisbehaving
;
2120 while (!vWorkQueue
.empty()) {
2121 auto itByPrev
= mapOrphanTransactionsByPrev
.find(vWorkQueue
.front());
2122 vWorkQueue
.pop_front();
2123 if (itByPrev
== mapOrphanTransactionsByPrev
.end())
2125 for (auto mi
= itByPrev
->second
.begin();
2126 mi
!= itByPrev
->second
.end();
2129 const CTransactionRef
& porphanTx
= (*mi
)->second
.tx
;
2130 const CTransaction
& orphanTx
= *porphanTx
;
2131 const uint256
& orphanHash
= orphanTx
.GetHash();
2132 NodeId fromPeer
= (*mi
)->second
.fromPeer
;
2133 bool fMissingInputs2
= false;
2134 // Use a dummy CValidationState so someone can't setup nodes to counter-DoS based on orphan
2135 // resolution (that is, feeding people an invalid transaction based on LegitTxX in order to get
2136 // anyone relaying LegitTxX banned)
2137 CValidationState stateDummy
;
2140 if (setMisbehaving
.count(fromPeer
))
2142 if (AcceptToMemoryPool(mempool
, stateDummy
, porphanTx
, &fMissingInputs2
, &lRemovedTxn
, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
2143 LogPrint(BCLog::MEMPOOL
, " accepted orphan tx %s\n", orphanHash
.ToString());
2144 RelayTransaction(orphanTx
, connman
);
2145 for (unsigned int i
= 0; i
< orphanTx
.vout
.size(); i
++) {
2146 vWorkQueue
.emplace_back(orphanHash
, i
);
2148 vEraseQueue
.push_back(orphanHash
);
2150 else if (!fMissingInputs2
)
2153 if (stateDummy
.IsInvalid(nDos
) && nDos
> 0)
2155 // Punish peer that gave us an invalid orphan tx
2156 Misbehaving(fromPeer
, nDos
);
2157 setMisbehaving
.insert(fromPeer
);
2158 LogPrint(BCLog::MEMPOOL
, " invalid orphan tx %s\n", orphanHash
.ToString());
2160 // Has inputs but not accepted to mempool
2161 // Probably non-standard or insufficient fee
2162 LogPrint(BCLog::MEMPOOL
, " removed orphan tx %s\n", orphanHash
.ToString());
2163 vEraseQueue
.push_back(orphanHash
);
2164 if (!orphanTx
.HasWitness() && !stateDummy
.CorruptionPossible()) {
2165 // Do not use rejection cache for witness transactions or
2166 // witness-stripped transactions, as they can have been malleated.
2167 // See https://github.com/bitcoin/bitcoin/issues/8279 for details.
2168 assert(recentRejects
);
2169 recentRejects
->insert(orphanHash
);
2172 mempool
.check(pcoinsTip
.get());
2176 for (uint256 hash
: vEraseQueue
)
2177 EraseOrphanTx(hash
);
2179 else if (fMissingInputs
)
2181 bool fRejectedParents
= false; // It may be the case that the orphans parents have all been rejected
2182 for (const CTxIn
& txin
: tx
.vin
) {
2183 if (recentRejects
->contains(txin
.prevout
.hash
)) {
2184 fRejectedParents
= true;
2188 if (!fRejectedParents
) {
2189 uint32_t nFetchFlags
= GetFetchFlags(pfrom
);
2190 for (const CTxIn
& txin
: tx
.vin
) {
2191 CInv
_inv(MSG_TX
| nFetchFlags
, txin
.prevout
.hash
);
2192 pfrom
->AddInventoryKnown(_inv
);
2193 if (!AlreadyHave(_inv
)) pfrom
->AskFor(_inv
);
2195 AddOrphanTx(ptx
, pfrom
->GetId());
2197 // DoS prevention: do not allow mapOrphanTransactions to grow unbounded
2198 unsigned int nMaxOrphanTx
= (unsigned int)std::max((int64_t)0, gArgs
.GetArg("-maxorphantx", DEFAULT_MAX_ORPHAN_TRANSACTIONS
));
2199 unsigned int nEvicted
= LimitOrphanTxSize(nMaxOrphanTx
);
2201 LogPrint(BCLog::MEMPOOL
, "mapOrphan overflow, removed %u tx\n", nEvicted
);
2204 LogPrint(BCLog::MEMPOOL
, "not keeping orphan with rejected parents %s\n",tx
.GetHash().ToString());
2205 // We will continue to reject this tx since it has rejected
2206 // parents so avoid re-requesting it from other peers.
2207 recentRejects
->insert(tx
.GetHash());
2210 if (!tx
.HasWitness() && !state
.CorruptionPossible()) {
2211 // Do not use rejection cache for witness transactions or
2212 // witness-stripped transactions, as they can have been malleated.
2213 // See https://github.com/bitcoin/bitcoin/issues/8279 for details.
2214 assert(recentRejects
);
2215 recentRejects
->insert(tx
.GetHash());
2216 if (RecursiveDynamicUsage(*ptx
) < 100000) {
2217 AddToCompactExtraTransactions(ptx
);
2219 } else if (tx
.HasWitness() && RecursiveDynamicUsage(*ptx
) < 100000) {
2220 AddToCompactExtraTransactions(ptx
);
2223 if (pfrom
->fWhitelisted
&& gArgs
.GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY
)) {
2224 // Always relay transactions received from whitelisted peers, even
2225 // if they were already in the mempool or rejected from it due
2226 // to policy, allowing the node to function as a gateway for
2227 // nodes hidden behind it.
2229 // Never relay transactions that we would assign a non-zero DoS
2230 // score for, as we expect peers to do the same with us in that
2233 if (!state
.IsInvalid(nDoS
) || nDoS
== 0) {
2234 LogPrintf("Force relaying tx %s from whitelisted peer=%d\n", tx
.GetHash().ToString(), pfrom
->GetId());
2235 RelayTransaction(tx
, connman
);
2237 LogPrintf("Not relaying invalid transaction %s from whitelisted peer=%d (%s)\n", tx
.GetHash().ToString(), pfrom
->GetId(), FormatStateMessage(state
));
2242 for (const CTransactionRef
& removedTx
: lRemovedTxn
)
2243 AddToCompactExtraTransactions(removedTx
);
2246 if (state
.IsInvalid(nDoS
))
2248 LogPrint(BCLog::MEMPOOLREJ
, "%s from peer=%d was not accepted: %s\n", tx
.GetHash().ToString(),
2250 FormatStateMessage(state
));
2251 if (state
.GetRejectCode() > 0 && state
.GetRejectCode() < REJECT_INTERNAL
) // Never send AcceptToMemoryPool's internal codes over P2P
2252 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::REJECT
, strCommand
, (unsigned char)state
.GetRejectCode(),
2253 state
.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH
), inv
.hash
));
2255 Misbehaving(pfrom
->GetId(), nDoS
);
2261 else if (strCommand
== NetMsgType::CMPCTBLOCK
&& !fImporting
&& !fReindex
) // Ignore blocks received while importing
2263 CBlockHeaderAndShortTxIDs cmpctblock
;
2264 vRecv
>> cmpctblock
;
2266 bool received_new_header
= false;
2271 if (mapBlockIndex
.find(cmpctblock
.header
.hashPrevBlock
) == mapBlockIndex
.end()) {
2272 // Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
2273 if (!IsInitialBlockDownload())
2274 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETHEADERS
, chainActive
.GetLocator(pindexBestHeader
), uint256()));
2278 if (mapBlockIndex
.find(cmpctblock
.header
.GetHash()) == mapBlockIndex
.end()) {
2279 received_new_header
= true;
2283 const CBlockIndex
*pindex
= nullptr;
2284 CValidationState state
;
2285 if (!ProcessNewBlockHeaders({cmpctblock
.header
}, state
, chainparams
, &pindex
)) {
2287 if (state
.IsInvalid(nDoS
)) {
2290 Misbehaving(pfrom
->GetId(), nDoS
);
2292 LogPrintf("Peer %d sent us invalid header via cmpctblock\n", pfrom
->GetId());
2297 // When we succeed in decoding a block's txids from a cmpctblock
2298 // message we typically jump to the BLOCKTXN handling code, with a
2299 // dummy (empty) BLOCKTXN message, to re-use the logic there in
2300 // completing processing of the putative block (without cs_main).
2301 bool fProcessBLOCKTXN
= false;
2302 CDataStream
blockTxnMsg(SER_NETWORK
, PROTOCOL_VERSION
);
2304 // If we end up treating this as a plain headers message, call that as well
2306 bool fRevertToHeaderProcessing
= false;
2308 // Keep a CBlock for "optimistic" compactblock reconstructions (see
2310 std::shared_ptr
<CBlock
> pblock
= std::make_shared
<CBlock
>();
2311 bool fBlockReconstructed
= false;
2315 // If AcceptBlockHeader returned true, it set pindex
2317 UpdateBlockAvailability(pfrom
->GetId(), pindex
->GetBlockHash());
2319 CNodeState
*nodestate
= State(pfrom
->GetId());
2321 // If this was a new header with more work than our tip, update the
2322 // peer's last block announcement time
2323 if (received_new_header
&& pindex
->nChainWork
> chainActive
.Tip()->nChainWork
) {
2324 nodestate
->m_last_block_announcement
= GetTime();
2327 std::map
<uint256
, std::pair
<NodeId
, std::list
<QueuedBlock
>::iterator
> >::iterator blockInFlightIt
= mapBlocksInFlight
.find(pindex
->GetBlockHash());
2328 bool fAlreadyInFlight
= blockInFlightIt
!= mapBlocksInFlight
.end();
2330 if (pindex
->nStatus
& BLOCK_HAVE_DATA
) // Nothing to do here
2333 if (pindex
->nChainWork
<= chainActive
.Tip()->nChainWork
|| // We know something better
2334 pindex
->nTx
!= 0) { // We had this block at some point, but pruned it
2335 if (fAlreadyInFlight
) {
2336 // We requested this block for some reason, but our mempool will probably be useless
2337 // so we just grab the block via normal getdata
2338 std::vector
<CInv
> vInv(1);
2339 vInv
[0] = CInv(MSG_BLOCK
| GetFetchFlags(pfrom
), cmpctblock
.header
.GetHash());
2340 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETDATA
, vInv
));
2345 // If we're not close to tip yet, give up and let parallel block fetch work its magic
2346 if (!fAlreadyInFlight
&& !CanDirectFetch(chainparams
.GetConsensus()))
2349 if (IsWitnessEnabled(pindex
->pprev
, chainparams
.GetConsensus()) && !nodestate
->fSupportsDesiredCmpctVersion
) {
2350 // Don't bother trying to process compact blocks from v1 peers
2351 // after segwit activates.
2355 // We want to be a bit conservative just to be extra careful about DoS
2356 // possibilities in compact block processing...
2357 if (pindex
->nHeight
<= chainActive
.Height() + 2) {
2358 if ((!fAlreadyInFlight
&& nodestate
->nBlocksInFlight
< MAX_BLOCKS_IN_TRANSIT_PER_PEER
) ||
2359 (fAlreadyInFlight
&& blockInFlightIt
->second
.first
== pfrom
->GetId())) {
2360 std::list
<QueuedBlock
>::iterator
* queuedBlockIt
= nullptr;
2361 if (!MarkBlockAsInFlight(pfrom
->GetId(), pindex
->GetBlockHash(), pindex
, &queuedBlockIt
)) {
2362 if (!(*queuedBlockIt
)->partialBlock
)
2363 (*queuedBlockIt
)->partialBlock
.reset(new PartiallyDownloadedBlock(&mempool
));
2365 // The block was already in flight using compact blocks from the same peer
2366 LogPrint(BCLog::NET
, "Peer sent us compact block we were already syncing!\n");
2371 PartiallyDownloadedBlock
& partialBlock
= *(*queuedBlockIt
)->partialBlock
;
2372 ReadStatus status
= partialBlock
.InitData(cmpctblock
, vExtraTxnForCompact
);
2373 if (status
== READ_STATUS_INVALID
) {
2374 MarkBlockAsReceived(pindex
->GetBlockHash()); // Reset in-flight state in case of whitelist
2375 Misbehaving(pfrom
->GetId(), 100);
2376 LogPrintf("Peer %d sent us invalid compact block\n", pfrom
->GetId());
2378 } else if (status
== READ_STATUS_FAILED
) {
2379 // Duplicate txindexes, the block is now in-flight, so just request it
2380 std::vector
<CInv
> vInv(1);
2381 vInv
[0] = CInv(MSG_BLOCK
| GetFetchFlags(pfrom
), cmpctblock
.header
.GetHash());
2382 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETDATA
, vInv
));
2386 BlockTransactionsRequest req
;
2387 for (size_t i
= 0; i
< cmpctblock
.BlockTxCount(); i
++) {
2388 if (!partialBlock
.IsTxAvailable(i
))
2389 req
.indexes
.push_back(i
);
2391 if (req
.indexes
.empty()) {
2392 // Dirty hack to jump to BLOCKTXN code (TODO: move message handling into their own functions)
2393 BlockTransactions txn
;
2394 txn
.blockhash
= cmpctblock
.header
.GetHash();
2396 fProcessBLOCKTXN
= true;
2398 req
.blockhash
= pindex
->GetBlockHash();
2399 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETBLOCKTXN
, req
));
2402 // This block is either already in flight from a different
2403 // peer, or this peer has too many blocks outstanding to
2405 // Optimistically try to reconstruct anyway since we might be
2406 // able to without any round trips.
2407 PartiallyDownloadedBlock
tempBlock(&mempool
);
2408 ReadStatus status
= tempBlock
.InitData(cmpctblock
, vExtraTxnForCompact
);
2409 if (status
!= READ_STATUS_OK
) {
2410 // TODO: don't ignore failures
2413 std::vector
<CTransactionRef
> dummy
;
2414 status
= tempBlock
.FillBlock(*pblock
, dummy
);
2415 if (status
== READ_STATUS_OK
) {
2416 fBlockReconstructed
= true;
2420 if (fAlreadyInFlight
) {
2421 // We requested this block, but its far into the future, so our
2422 // mempool will probably be useless - request the block normally
2423 std::vector
<CInv
> vInv(1);
2424 vInv
[0] = CInv(MSG_BLOCK
| GetFetchFlags(pfrom
), cmpctblock
.header
.GetHash());
2425 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETDATA
, vInv
));
2428 // If this was an announce-cmpctblock, we want the same treatment as a header message
2429 fRevertToHeaderProcessing
= true;
2434 if (fProcessBLOCKTXN
)
2435 return ProcessMessage(pfrom
, NetMsgType::BLOCKTXN
, blockTxnMsg
, nTimeReceived
, chainparams
, connman
, interruptMsgProc
);
2437 if (fRevertToHeaderProcessing
) {
2438 // Headers received from HB compact block peers are permitted to be
2439 // relayed before full validation (see BIP 152), so we don't want to disconnect
2440 // the peer if the header turns out to be for an invalid block.
2441 // Note that if a peer tries to build on an invalid chain, that
2442 // will be detected and the peer will be banned.
2443 return ProcessHeadersMessage(pfrom
, connman
, {cmpctblock
.header
}, chainparams
, /*punish_duplicate_invalid=*/false);
2446 if (fBlockReconstructed
) {
2447 // If we got here, we were able to optimistically reconstruct a
2448 // block that is in flight from some other peer.
2451 mapBlockSource
.emplace(pblock
->GetHash(), std::make_pair(pfrom
->GetId(), false));
2453 bool fNewBlock
= false;
2454 // Setting fForceProcessing to true means that we bypass some of
2455 // our anti-DoS protections in AcceptBlock, which filters
2456 // unrequested blocks that might be trying to waste our resources
2457 // (eg disk space). Because we only try to reconstruct blocks when
2458 // we're close to caught up (via the CanDirectFetch() requirement
2459 // above, combined with the behavior of not requesting blocks until
2460 // we have a chain with at least nMinimumChainWork), and we ignore
2461 // compact blocks with less work than our tip, it is safe to treat
2462 // reconstructed compact blocks as having been requested.
2463 ProcessNewBlock(chainparams
, pblock
, /*fForceProcessing=*/true, &fNewBlock
);
2465 pfrom
->nLastBlockTime
= GetTime();
2468 mapBlockSource
.erase(pblock
->GetHash());
2470 LOCK(cs_main
); // hold cs_main for CBlockIndex::IsValid()
2471 if (pindex
->IsValid(BLOCK_VALID_TRANSACTIONS
)) {
2472 // Clear download state for this block, which is in
2473 // process from some other peer. We do this after calling
2474 // ProcessNewBlock so that a malleated cmpctblock announcement
2475 // can't be used to interfere with block relay.
2476 MarkBlockAsReceived(pblock
->GetHash());
2482 else if (strCommand
== NetMsgType::BLOCKTXN
&& !fImporting
&& !fReindex
) // Ignore blocks received while importing
2484 BlockTransactions resp
;
2487 std::shared_ptr
<CBlock
> pblock
= std::make_shared
<CBlock
>();
2488 bool fBlockRead
= false;
2492 std::map
<uint256
, std::pair
<NodeId
, std::list
<QueuedBlock
>::iterator
> >::iterator it
= mapBlocksInFlight
.find(resp
.blockhash
);
2493 if (it
== mapBlocksInFlight
.end() || !it
->second
.second
->partialBlock
||
2494 it
->second
.first
!= pfrom
->GetId()) {
2495 LogPrint(BCLog::NET
, "Peer %d sent us block transactions for block we weren't expecting\n", pfrom
->GetId());
2499 PartiallyDownloadedBlock
& partialBlock
= *it
->second
.second
->partialBlock
;
2500 ReadStatus status
= partialBlock
.FillBlock(*pblock
, resp
.txn
);
2501 if (status
== READ_STATUS_INVALID
) {
2502 MarkBlockAsReceived(resp
.blockhash
); // Reset in-flight state in case of whitelist
2503 Misbehaving(pfrom
->GetId(), 100);
2504 LogPrintf("Peer %d sent us invalid compact block/non-matching block transactions\n", pfrom
->GetId());
2506 } else if (status
== READ_STATUS_FAILED
) {
2507 // Might have collided, fall back to getdata now :(
2508 std::vector
<CInv
> invs
;
2509 invs
.push_back(CInv(MSG_BLOCK
| GetFetchFlags(pfrom
), resp
.blockhash
));
2510 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETDATA
, invs
));
2512 // Block is either okay, or possibly we received
2513 // READ_STATUS_CHECKBLOCK_FAILED.
2514 // Note that CheckBlock can only fail for one of a few reasons:
2515 // 1. bad-proof-of-work (impossible here, because we've already
2516 // accepted the header)
2517 // 2. merkleroot doesn't match the transactions given (already
2518 // caught in FillBlock with READ_STATUS_FAILED, so
2520 // 3. the block is otherwise invalid (eg invalid coinbase,
2521 // block is too big, too many legacy sigops, etc).
2522 // So if CheckBlock failed, #3 is the only possibility.
2523 // Under BIP 152, we don't DoS-ban unless proof of work is
2524 // invalid (we don't require all the stateless checks to have
2525 // been run). This is handled below, so just treat this as
2526 // though the block was successfully read, and rely on the
2527 // handling in ProcessNewBlock to ensure the block index is
2528 // updated, reject messages go out, etc.
2529 MarkBlockAsReceived(resp
.blockhash
); // it is now an empty pointer
2531 // mapBlockSource is only used for sending reject messages and DoS scores,
2532 // so the race between here and cs_main in ProcessNewBlock is fine.
2533 // BIP 152 permits peers to relay compact blocks after validating
2534 // the header only; we should not punish peers if the block turns
2535 // out to be invalid.
2536 mapBlockSource
.emplace(resp
.blockhash
, std::make_pair(pfrom
->GetId(), false));
2538 } // Don't hold cs_main when we call into ProcessNewBlock
2540 bool fNewBlock
= false;
2541 // Since we requested this block (it was in mapBlocksInFlight), force it to be processed,
2542 // even if it would not be a candidate for new tip (missing previous block, chain not long enough, etc)
2543 // This bypasses some anti-DoS logic in AcceptBlock (eg to prevent
2544 // disk-space attacks), but this should be safe due to the
2545 // protections in the compact block handler -- see related comment
2546 // in compact block optimistic reconstruction handling.
2547 ProcessNewBlock(chainparams
, pblock
, /*fForceProcessing=*/true, &fNewBlock
);
2549 pfrom
->nLastBlockTime
= GetTime();
2552 mapBlockSource
.erase(pblock
->GetHash());
2558 else if (strCommand
== NetMsgType::HEADERS
&& !fImporting
&& !fReindex
) // Ignore headers received while importing
2560 std::vector
<CBlockHeader
> headers
;
2562 // Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks.
2563 unsigned int nCount
= ReadCompactSize(vRecv
);
2564 if (nCount
> MAX_HEADERS_RESULTS
) {
2566 Misbehaving(pfrom
->GetId(), 20);
2567 return error("headers message size = %u", nCount
);
2569 headers
.resize(nCount
);
2570 for (unsigned int n
= 0; n
< nCount
; n
++) {
2571 vRecv
>> headers
[n
];
2572 ReadCompactSize(vRecv
); // ignore tx count; assume it is 0.
2575 // Headers received via a HEADERS message should be valid, and reflect
2576 // the chain the peer is on. If we receive a known-invalid header,
2577 // disconnect the peer if it is using one of our outbound connection
2579 bool should_punish
= !pfrom
->fInbound
&& !pfrom
->m_manual_connection
;
2580 return ProcessHeadersMessage(pfrom
, connman
, headers
, chainparams
, should_punish
);
2583 else if (strCommand
== NetMsgType::BLOCK
&& !fImporting
&& !fReindex
) // Ignore blocks received while importing
2585 std::shared_ptr
<CBlock
> pblock
= std::make_shared
<CBlock
>();
2588 LogPrint(BCLog::NET
, "received block %s peer=%d\n", pblock
->GetHash().ToString(), pfrom
->GetId());
2590 bool forceProcessing
= false;
2591 const uint256
hash(pblock
->GetHash());
2594 // Also always process if we requested the block explicitly, as we may
2595 // need it even though it is not a candidate for a new best tip.
2596 forceProcessing
|= MarkBlockAsReceived(hash
);
2597 // mapBlockSource is only used for sending reject messages and DoS scores,
2598 // so the race between here and cs_main in ProcessNewBlock is fine.
2599 mapBlockSource
.emplace(hash
, std::make_pair(pfrom
->GetId(), true));
2601 bool fNewBlock
= false;
2602 ProcessNewBlock(chainparams
, pblock
, forceProcessing
, &fNewBlock
);
2604 pfrom
->nLastBlockTime
= GetTime();
2607 mapBlockSource
.erase(pblock
->GetHash());
2612 else if (strCommand
== NetMsgType::GETADDR
)
2614 // This asymmetric behavior for inbound and outbound connections was introduced
2615 // to prevent a fingerprinting attack: an attacker can send specific fake addresses
2616 // to users' AddrMan and later request them by sending getaddr messages.
2617 // Making nodes which are behind NAT and can only make outgoing connections ignore
2618 // the getaddr message mitigates the attack.
2619 if (!pfrom
->fInbound
) {
2620 LogPrint(BCLog::NET
, "Ignoring \"getaddr\" from outbound connection. peer=%d\n", pfrom
->GetId());
2624 // Only send one GetAddr response per connection to reduce resource waste
2625 // and discourage addr stamping of INV announcements.
2626 if (pfrom
->fSentAddr
) {
2627 LogPrint(BCLog::NET
, "Ignoring repeated \"getaddr\". peer=%d\n", pfrom
->GetId());
2630 pfrom
->fSentAddr
= true;
2632 pfrom
->vAddrToSend
.clear();
2633 std::vector
<CAddress
> vAddr
= connman
->GetAddresses();
2634 FastRandomContext insecure_rand
;
2635 for (const CAddress
&addr
: vAddr
)
2636 pfrom
->PushAddress(addr
, insecure_rand
);
2640 else if (strCommand
== NetMsgType::MEMPOOL
)
2642 if (!(pfrom
->GetLocalServices() & NODE_BLOOM
) && !pfrom
->fWhitelisted
)
2644 LogPrint(BCLog::NET
, "mempool request with bloom filters disabled, disconnect peer=%d\n", pfrom
->GetId());
2645 pfrom
->fDisconnect
= true;
2649 if (connman
->OutboundTargetReached(false) && !pfrom
->fWhitelisted
)
2651 LogPrint(BCLog::NET
, "mempool request with bandwidth limit reached, disconnect peer=%d\n", pfrom
->GetId());
2652 pfrom
->fDisconnect
= true;
2656 LOCK(pfrom
->cs_inventory
);
2657 pfrom
->fSendMempool
= true;
2661 else if (strCommand
== NetMsgType::PING
)
2663 if (pfrom
->nVersion
> BIP0031_VERSION
)
2667 // Echo the message back with the nonce. This allows for two useful features:
2669 // 1) A remote node can quickly check if the connection is operational
2670 // 2) Remote nodes can measure the latency of the network thread. If this node
2671 // is overloaded it won't respond to pings quickly and the remote node can
2672 // avoid sending us more work, like chain download requests.
2674 // The nonce stops the remote getting confused between different pings: without
2675 // it, if the remote node sends a ping once per second and this node takes 5
2676 // seconds to respond to each, the 5th ping the remote sends would appear to
2677 // return very quickly.
2678 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::PONG
, nonce
));
2683 else if (strCommand
== NetMsgType::PONG
)
2685 int64_t pingUsecEnd
= nTimeReceived
;
2687 size_t nAvail
= vRecv
.in_avail();
2688 bool bPingFinished
= false;
2689 std::string sProblem
;
2691 if (nAvail
>= sizeof(nonce
)) {
2694 // Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
2695 if (pfrom
->nPingNonceSent
!= 0) {
2696 if (nonce
== pfrom
->nPingNonceSent
) {
2697 // Matching pong received, this ping is no longer outstanding
2698 bPingFinished
= true;
2699 int64_t pingUsecTime
= pingUsecEnd
- pfrom
->nPingUsecStart
;
2700 if (pingUsecTime
> 0) {
2701 // Successful ping time measurement, replace previous
2702 pfrom
->nPingUsecTime
= pingUsecTime
;
2703 pfrom
->nMinPingUsecTime
= std::min(pfrom
->nMinPingUsecTime
.load(), pingUsecTime
);
2705 // This should never happen
2706 sProblem
= "Timing mishap";
2709 // Nonce mismatches are normal when pings are overlapping
2710 sProblem
= "Nonce mismatch";
2712 // This is most likely a bug in another implementation somewhere; cancel this ping
2713 bPingFinished
= true;
2714 sProblem
= "Nonce zero";
2718 sProblem
= "Unsolicited pong without ping";
2721 // This is most likely a bug in another implementation somewhere; cancel this ping
2722 bPingFinished
= true;
2723 sProblem
= "Short payload";
2726 if (!(sProblem
.empty())) {
2727 LogPrint(BCLog::NET
, "pong peer=%d: %s, %x expected, %x received, %u bytes\n",
2730 pfrom
->nPingNonceSent
,
2734 if (bPingFinished
) {
2735 pfrom
->nPingNonceSent
= 0;
2740 else if (strCommand
== NetMsgType::FILTERLOAD
)
2742 CBloomFilter filter
;
2745 if (!filter
.IsWithinSizeConstraints())
2747 // There is no excuse for sending a too-large filter
2749 Misbehaving(pfrom
->GetId(), 100);
2753 LOCK(pfrom
->cs_filter
);
2754 pfrom
->pfilter
.reset(new CBloomFilter(filter
));
2755 pfrom
->pfilter
->UpdateEmptyFull();
2756 pfrom
->fRelayTxes
= true;
2761 else if (strCommand
== NetMsgType::FILTERADD
)
2763 std::vector
<unsigned char> vData
;
2766 // Nodes must NEVER send a data item > 520 bytes (the max size for a script data object,
2767 // and thus, the maximum size any matched object can have) in a filteradd message
2769 if (vData
.size() > MAX_SCRIPT_ELEMENT_SIZE
) {
2772 LOCK(pfrom
->cs_filter
);
2773 if (pfrom
->pfilter
) {
2774 pfrom
->pfilter
->insert(vData
);
2781 Misbehaving(pfrom
->GetId(), 100);
2786 else if (strCommand
== NetMsgType::FILTERCLEAR
)
2788 LOCK(pfrom
->cs_filter
);
2789 if (pfrom
->GetLocalServices() & NODE_BLOOM
) {
2790 pfrom
->pfilter
.reset(new CBloomFilter());
2792 pfrom
->fRelayTxes
= true;
2795 else if (strCommand
== NetMsgType::FEEFILTER
) {
2796 CAmount newFeeFilter
= 0;
2797 vRecv
>> newFeeFilter
;
2798 if (MoneyRange(newFeeFilter
)) {
2800 LOCK(pfrom
->cs_feeFilter
);
2801 pfrom
->minFeeFilter
= newFeeFilter
;
2803 LogPrint(BCLog::NET
, "received: feefilter of %s from peer=%d\n", CFeeRate(newFeeFilter
).ToString(), pfrom
->GetId());
2807 else if (strCommand
== NetMsgType::NOTFOUND
) {
2808 // We do not care about the NOTFOUND message, but logging an Unknown Command
2809 // message would be undesirable as we transmit it ourselves.
2813 // Ignore unknown commands for extensibility
2814 LogPrint(BCLog::NET
, "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand
), pfrom
->GetId());
2822 static bool SendRejectsAndCheckIfBanned(CNode
* pnode
, CConnman
* connman
)
2824 AssertLockHeld(cs_main
);
2825 CNodeState
&state
= *State(pnode
->GetId());
2827 for (const CBlockReject
& reject
: state
.rejects
) {
2828 connman
->PushMessage(pnode
, CNetMsgMaker(INIT_PROTO_VERSION
).Make(NetMsgType::REJECT
, (std::string
)NetMsgType::BLOCK
, reject
.chRejectCode
, reject
.strRejectReason
, reject
.hashBlock
));
2830 state
.rejects
.clear();
2832 if (state
.fShouldBan
) {
2833 state
.fShouldBan
= false;
2834 if (pnode
->fWhitelisted
)
2835 LogPrintf("Warning: not punishing whitelisted peer %s!\n", pnode
->addr
.ToString());
2836 else if (pnode
->m_manual_connection
)
2837 LogPrintf("Warning: not punishing manually-connected peer %s!\n", pnode
->addr
.ToString());
2839 pnode
->fDisconnect
= true;
2840 if (pnode
->addr
.IsLocal())
2841 LogPrintf("Warning: not banning local peer %s!\n", pnode
->addr
.ToString());
2844 connman
->Ban(pnode
->addr
, BanReasonNodeMisbehaving
);
2852 bool PeerLogicValidation::ProcessMessages(CNode
* pfrom
, std::atomic
<bool>& interruptMsgProc
)
2854 const CChainParams
& chainparams
= Params();
2857 // (4) message start
2863 bool fMoreWork
= false;
2865 if (!pfrom
->vRecvGetData
.empty())
2866 ProcessGetData(pfrom
, chainparams
.GetConsensus(), connman
, interruptMsgProc
);
2868 if (pfrom
->fDisconnect
)
2871 // this maintains the order of responses
2872 if (!pfrom
->vRecvGetData
.empty()) return true;
2874 // Don't bother if send buffer is too full to respond anyway
2875 if (pfrom
->fPauseSend
)
2878 std::list
<CNetMessage
> msgs
;
2880 LOCK(pfrom
->cs_vProcessMsg
);
2881 if (pfrom
->vProcessMsg
.empty())
2883 // Just take one message
2884 msgs
.splice(msgs
.begin(), pfrom
->vProcessMsg
, pfrom
->vProcessMsg
.begin());
2885 pfrom
->nProcessQueueSize
-= msgs
.front().vRecv
.size() + CMessageHeader::HEADER_SIZE
;
2886 pfrom
->fPauseRecv
= pfrom
->nProcessQueueSize
> connman
->GetReceiveFloodSize();
2887 fMoreWork
= !pfrom
->vProcessMsg
.empty();
2889 CNetMessage
& msg(msgs
.front());
2891 msg
.SetVersion(pfrom
->GetRecvVersion());
2892 // Scan for message start
2893 if (memcmp(msg
.hdr
.pchMessageStart
, chainparams
.MessageStart(), CMessageHeader::MESSAGE_START_SIZE
) != 0) {
2894 LogPrintf("PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg
.hdr
.GetCommand()), pfrom
->GetId());
2895 pfrom
->fDisconnect
= true;
2900 CMessageHeader
& hdr
= msg
.hdr
;
2901 if (!hdr
.IsValid(chainparams
.MessageStart()))
2903 LogPrintf("PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(hdr
.GetCommand()), pfrom
->GetId());
2906 std::string strCommand
= hdr
.GetCommand();
2909 unsigned int nMessageSize
= hdr
.nMessageSize
;
2912 CDataStream
& vRecv
= msg
.vRecv
;
2913 const uint256
& hash
= msg
.GetMessageHash();
2914 if (memcmp(hash
.begin(), hdr
.pchChecksum
, CMessageHeader::CHECKSUM_SIZE
) != 0)
2916 LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s\n", __func__
,
2917 SanitizeString(strCommand
), nMessageSize
,
2918 HexStr(hash
.begin(), hash
.begin()+CMessageHeader::CHECKSUM_SIZE
),
2919 HexStr(hdr
.pchChecksum
, hdr
.pchChecksum
+CMessageHeader::CHECKSUM_SIZE
));
2927 fRet
= ProcessMessage(pfrom
, strCommand
, vRecv
, msg
.nTime
, chainparams
, connman
, interruptMsgProc
);
2928 if (interruptMsgProc
)
2930 if (!pfrom
->vRecvGetData
.empty())
2933 catch (const std::ios_base::failure
& e
)
2935 connman
->PushMessage(pfrom
, CNetMsgMaker(INIT_PROTO_VERSION
).Make(NetMsgType::REJECT
, strCommand
, REJECT_MALFORMED
, std::string("error parsing message")));
2936 if (strstr(e
.what(), "end of data"))
2938 // Allow exceptions from under-length message on vRecv
2939 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());
2941 else if (strstr(e
.what(), "size too large"))
2943 // Allow exceptions from over-long size
2944 LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__
, SanitizeString(strCommand
), nMessageSize
, e
.what());
2946 else if (strstr(e
.what(), "non-canonical ReadCompactSize()"))
2948 // Allow exceptions from non-canonical encoding
2949 LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__
, SanitizeString(strCommand
), nMessageSize
, e
.what());
2953 PrintExceptionContinue(&e
, "ProcessMessages()");
2956 catch (const std::exception
& e
) {
2957 PrintExceptionContinue(&e
, "ProcessMessages()");
2959 PrintExceptionContinue(nullptr, "ProcessMessages()");
2963 LogPrintf("%s(%s, %u bytes) FAILED peer=%d\n", __func__
, SanitizeString(strCommand
), nMessageSize
, pfrom
->GetId());
2967 SendRejectsAndCheckIfBanned(pfrom
, connman
);
2972 void PeerLogicValidation::ConsiderEviction(CNode
*pto
, int64_t time_in_seconds
)
2974 AssertLockHeld(cs_main
);
2976 CNodeState
&state
= *State(pto
->GetId());
2977 const CNetMsgMaker
msgMaker(pto
->GetSendVersion());
2979 if (!state
.m_chain_sync
.m_protect
&& IsOutboundDisconnectionCandidate(pto
) && state
.fSyncStarted
) {
2980 // This is an outbound peer subject to disconnection if they don't
2981 // announce a block with as much work as the current tip within
2982 // CHAIN_SYNC_TIMEOUT + HEADERS_RESPONSE_TIME seconds (note: if
2983 // their chain has more work than ours, we should sync to it,
2984 // unless it's invalid, in which case we should find that out and
2985 // disconnect from them elsewhere).
2986 if (state
.pindexBestKnownBlock
!= nullptr && state
.pindexBestKnownBlock
->nChainWork
>= chainActive
.Tip()->nChainWork
) {
2987 if (state
.m_chain_sync
.m_timeout
!= 0) {
2988 state
.m_chain_sync
.m_timeout
= 0;
2989 state
.m_chain_sync
.m_work_header
= nullptr;
2990 state
.m_chain_sync
.m_sent_getheaders
= false;
2992 } else if (state
.m_chain_sync
.m_timeout
== 0 || (state
.m_chain_sync
.m_work_header
!= nullptr && state
.pindexBestKnownBlock
!= nullptr && state
.pindexBestKnownBlock
->nChainWork
>= state
.m_chain_sync
.m_work_header
->nChainWork
)) {
2993 // Our best block known by this peer is behind our tip, and we're either noticing
2994 // that for the first time, OR this peer was able to catch up to some earlier point
2995 // where we checked against our tip.
2996 // Either way, set a new timeout based on current tip.
2997 state
.m_chain_sync
.m_timeout
= time_in_seconds
+ CHAIN_SYNC_TIMEOUT
;
2998 state
.m_chain_sync
.m_work_header
= chainActive
.Tip();
2999 state
.m_chain_sync
.m_sent_getheaders
= false;
3000 } else if (state
.m_chain_sync
.m_timeout
> 0 && time_in_seconds
> state
.m_chain_sync
.m_timeout
) {
3001 // No evidence yet that our peer has synced to a chain with work equal to that
3002 // of our tip, when we first detected it was behind. Send a single getheaders
3003 // message to give the peer a chance to update us.
3004 if (state
.m_chain_sync
.m_sent_getheaders
) {
3005 // They've run out of time to catch up!
3006 LogPrintf("Disconnecting outbound peer %d for old chain, best known block = %s\n", pto
->GetId(), state
.pindexBestKnownBlock
!= nullptr ? state
.pindexBestKnownBlock
->GetBlockHash().ToString() : "<none>");
3007 pto
->fDisconnect
= true;
3009 assert(state
.m_chain_sync
.m_work_header
);
3010 LogPrint(BCLog::NET
, "sending getheaders to outbound peer=%d to verify chain work (current best known block:%s, benchmark blockhash: %s)\n", pto
->GetId(), state
.pindexBestKnownBlock
!= nullptr ? state
.pindexBestKnownBlock
->GetBlockHash().ToString() : "<none>", state
.m_chain_sync
.m_work_header
->GetBlockHash().ToString());
3011 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::GETHEADERS
, chainActive
.GetLocator(state
.m_chain_sync
.m_work_header
->pprev
), uint256()));
3012 state
.m_chain_sync
.m_sent_getheaders
= true;
3013 constexpr int64_t HEADERS_RESPONSE_TIME
= 120; // 2 minutes
3014 // Bump the timeout to allow a response, which could clear the timeout
3015 // (if the response shows the peer has synced), reset the timeout (if
3016 // the peer syncs to the required work but not to our tip), or result
3017 // in disconnect (if we advance to the timeout and pindexBestKnownBlock
3018 // has not sufficiently progressed)
3019 state
.m_chain_sync
.m_timeout
= time_in_seconds
+ HEADERS_RESPONSE_TIME
;
3025 void PeerLogicValidation::EvictExtraOutboundPeers(int64_t time_in_seconds
)
3027 // Check whether we have too many outbound peers
3028 int extra_peers
= connman
->GetExtraOutboundCount();
3029 if (extra_peers
> 0) {
3030 // If we have more outbound peers than we target, disconnect one.
3031 // Pick the outbound peer that least recently announced
3032 // us a new block, with ties broken by choosing the more recent
3033 // connection (higher node id)
3034 NodeId worst_peer
= -1;
3035 int64_t oldest_block_announcement
= std::numeric_limits
<int64_t>::max();
3039 connman
->ForEachNode([&](CNode
* pnode
) {
3040 // Ignore non-outbound peers, or nodes marked for disconnect already
3041 if (!IsOutboundDisconnectionCandidate(pnode
) || pnode
->fDisconnect
) return;
3042 CNodeState
*state
= State(pnode
->GetId());
3043 if (state
== nullptr) return; // shouldn't be possible, but just in case
3044 // Don't evict our protected peers
3045 if (state
->m_chain_sync
.m_protect
) return;
3046 if (state
->m_last_block_announcement
< oldest_block_announcement
|| (state
->m_last_block_announcement
== oldest_block_announcement
&& pnode
->GetId() > worst_peer
)) {
3047 worst_peer
= pnode
->GetId();
3048 oldest_block_announcement
= state
->m_last_block_announcement
;
3051 if (worst_peer
!= -1) {
3052 bool disconnected
= connman
->ForNode(worst_peer
, [&](CNode
*pnode
) {
3053 // Only disconnect a peer that has been connected to us for
3054 // some reasonable fraction of our check-frequency, to give
3055 // it time for new information to have arrived.
3056 // Also don't disconnect any peer we're trying to download a
3058 CNodeState
&state
= *State(pnode
->GetId());
3059 if (time_in_seconds
- pnode
->nTimeConnected
> MINIMUM_CONNECT_TIME
&& state
.nBlocksInFlight
== 0) {
3060 LogPrint(BCLog::NET
, "disconnecting extra outbound peer=%d (last block announcement received at time %d)\n", pnode
->GetId(), oldest_block_announcement
);
3061 pnode
->fDisconnect
= true;
3064 LogPrint(BCLog::NET
, "keeping outbound peer=%d chosen for eviction (connect time: %d, blocks_in_flight: %d)\n", pnode
->GetId(), pnode
->nTimeConnected
, state
.nBlocksInFlight
);
3069 // If we disconnected an extra peer, that means we successfully
3070 // connected to at least one peer after the last time we
3071 // detected a stale tip. Don't try any more extra peers until
3072 // we next detect a stale tip, to limit the load we put on the
3073 // network from these extra connections.
3074 connman
->SetTryNewOutboundPeer(false);
3080 void PeerLogicValidation::CheckForStaleTipAndEvictPeers(const Consensus::Params
&consensusParams
)
3082 if (connman
== nullptr) return;
3084 int64_t time_in_seconds
= GetTime();
3086 EvictExtraOutboundPeers(time_in_seconds
);
3088 if (time_in_seconds
> m_stale_tip_check_time
) {
3090 // Check whether our tip is stale, and if so, allow using an extra
3092 if (TipMayBeStale(consensusParams
)) {
3093 LogPrintf("Potential stale tip detected, will try using extra outbound peer (last tip update: %d seconds ago)\n", time_in_seconds
- g_last_tip_update
);
3094 connman
->SetTryNewOutboundPeer(true);
3095 } else if (connman
->GetTryNewOutboundPeer()) {
3096 connman
->SetTryNewOutboundPeer(false);
3098 m_stale_tip_check_time
= time_in_seconds
+ STALE_CHECK_INTERVAL
;
3102 class CompareInvMempoolOrder
3106 explicit CompareInvMempoolOrder(CTxMemPool
*_mempool
)
3111 bool operator()(std::set
<uint256
>::iterator a
, std::set
<uint256
>::iterator b
)
3113 /* As std::make_heap produces a max-heap, we want the entries with the
3114 * fewest ancestors/highest fee to sort later. */
3115 return mp
->CompareDepthAndScore(*b
, *a
);
3119 bool PeerLogicValidation::SendMessages(CNode
* pto
, std::atomic
<bool>& interruptMsgProc
)
3121 const Consensus::Params
& consensusParams
= Params().GetConsensus();
3123 // Don't send anything until the version handshake is complete
3124 if (!pto
->fSuccessfullyConnected
|| pto
->fDisconnect
)
3127 // If we get here, the outgoing message serialization version is set and can't change.
3128 const CNetMsgMaker
msgMaker(pto
->GetSendVersion());
3133 bool pingSend
= false;
3134 if (pto
->fPingQueued
) {
3135 // RPC ping request by user
3138 if (pto
->nPingNonceSent
== 0 && pto
->nPingUsecStart
+ PING_INTERVAL
* 1000000 < GetTimeMicros()) {
3139 // Ping automatically sent as a latency probe & keepalive.
3144 while (nonce
== 0) {
3145 GetRandBytes((unsigned char*)&nonce
, sizeof(nonce
));
3147 pto
->fPingQueued
= false;
3148 pto
->nPingUsecStart
= GetTimeMicros();
3149 if (pto
->nVersion
> BIP0031_VERSION
) {
3150 pto
->nPingNonceSent
= nonce
;
3151 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::PING
, nonce
));
3153 // Peer is too old to support ping command with nonce, pong will never arrive.
3154 pto
->nPingNonceSent
= 0;
3155 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::PING
));
3159 TRY_LOCK(cs_main
, lockMain
); // Acquire cs_main for IsInitialBlockDownload() and CNodeState()
3163 if (SendRejectsAndCheckIfBanned(pto
, connman
))
3165 CNodeState
&state
= *State(pto
->GetId());
3167 // Address refresh broadcast
3168 int64_t nNow
= GetTimeMicros();
3169 if (!IsInitialBlockDownload() && pto
->nNextLocalAddrSend
< nNow
) {
3170 AdvertiseLocal(pto
);
3171 pto
->nNextLocalAddrSend
= PoissonNextSend(nNow
, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL
);
3177 if (pto
->nNextAddrSend
< nNow
) {
3178 pto
->nNextAddrSend
= PoissonNextSend(nNow
, AVG_ADDRESS_BROADCAST_INTERVAL
);
3179 std::vector
<CAddress
> vAddr
;
3180 vAddr
.reserve(pto
->vAddrToSend
.size());
3181 for (const CAddress
& addr
: pto
->vAddrToSend
)
3183 if (!pto
->addrKnown
.contains(addr
.GetKey()))
3185 pto
->addrKnown
.insert(addr
.GetKey());
3186 vAddr
.push_back(addr
);
3187 // receiver rejects addr messages larger than 1000
3188 if (vAddr
.size() >= 1000)
3190 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::ADDR
, vAddr
));
3195 pto
->vAddrToSend
.clear();
3197 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::ADDR
, vAddr
));
3198 // we only send the big addr message once
3199 if (pto
->vAddrToSend
.capacity() > 40)
3200 pto
->vAddrToSend
.shrink_to_fit();
3204 if (pindexBestHeader
== nullptr)
3205 pindexBestHeader
= chainActive
.Tip();
3206 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.
3207 if (!state
.fSyncStarted
&& !pto
->fClient
&& !fImporting
&& !fReindex
) {
3208 // Only actively request headers from a single peer, unless we're close to today.
3209 if ((nSyncStarted
== 0 && fFetch
) || pindexBestHeader
->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
3210 state
.fSyncStarted
= true;
3211 state
.nHeadersSyncTimeout
= GetTimeMicros() + HEADERS_DOWNLOAD_TIMEOUT_BASE
+ HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER
* (GetAdjustedTime() - pindexBestHeader
->GetBlockTime())/(consensusParams
.nPowTargetSpacing
);
3213 const CBlockIndex
*pindexStart
= pindexBestHeader
;
3214 /* If possible, start at the block preceding the currently
3215 best known header. This ensures that we always get a
3216 non-empty list of headers back as long as the peer
3217 is up-to-date. With a non-empty response, we can initialise
3218 the peer's known best block. This wouldn't be possible
3219 if we requested starting at pindexBestHeader and
3220 got back an empty response. */
3221 if (pindexStart
->pprev
)
3222 pindexStart
= pindexStart
->pprev
;
3223 LogPrint(BCLog::NET
, "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart
->nHeight
, pto
->GetId(), pto
->nStartingHeight
);
3224 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::GETHEADERS
, chainActive
.GetLocator(pindexStart
), uint256()));
3228 // Resend wallet transactions that haven't gotten in a block yet
3229 // Except during reindex, importing and IBD, when old wallet
3230 // transactions become unconfirmed and spams other nodes.
3231 if (!fReindex
&& !fImporting
&& !IsInitialBlockDownload())
3233 GetMainSignals().Broadcast(nTimeBestReceived
, connman
);
3237 // Try sending block announcements via headers
3240 // If we have less than MAX_BLOCKS_TO_ANNOUNCE in our
3241 // list of block hashes we're relaying, and our peer wants
3242 // headers announcements, then find the first header
3243 // not yet known to our peer but would connect, and send.
3244 // If no header would connect, or if we have too many
3245 // blocks, or if the peer doesn't want headers, just
3246 // add all to the inv queue.
3247 LOCK(pto
->cs_inventory
);
3248 std::vector
<CBlock
> vHeaders
;
3249 bool fRevertToInv
= ((!state
.fPreferHeaders
&&
3250 (!state
.fPreferHeaderAndIDs
|| pto
->vBlockHashesToAnnounce
.size() > 1)) ||
3251 pto
->vBlockHashesToAnnounce
.size() > MAX_BLOCKS_TO_ANNOUNCE
);
3252 const CBlockIndex
*pBestIndex
= nullptr; // last header queued for delivery
3253 ProcessBlockAvailability(pto
->GetId()); // ensure pindexBestKnownBlock is up-to-date
3255 if (!fRevertToInv
) {
3256 bool fFoundStartingHeader
= false;
3257 // Try to find first header that our peer doesn't have, and
3258 // then send all headers past that one. If we come across any
3259 // headers that aren't on chainActive, give up.
3260 for (const uint256
&hash
: pto
->vBlockHashesToAnnounce
) {
3261 BlockMap::iterator mi
= mapBlockIndex
.find(hash
);
3262 assert(mi
!= mapBlockIndex
.end());
3263 const CBlockIndex
*pindex
= mi
->second
;
3264 if (chainActive
[pindex
->nHeight
] != pindex
) {
3265 // Bail out if we reorged away from this block
3266 fRevertToInv
= true;
3269 if (pBestIndex
!= nullptr && pindex
->pprev
!= pBestIndex
) {
3270 // This means that the list of blocks to announce don't
3271 // connect to each other.
3272 // This shouldn't really be possible to hit during
3273 // regular operation (because reorgs should take us to
3274 // a chain that has some block not on the prior chain,
3275 // which should be caught by the prior check), but one
3276 // way this could happen is by using invalidateblock /
3277 // reconsiderblock repeatedly on the tip, causing it to
3278 // be added multiple times to vBlockHashesToAnnounce.
3279 // Robustly deal with this rare situation by reverting
3281 fRevertToInv
= true;
3284 pBestIndex
= pindex
;
3285 if (fFoundStartingHeader
) {
3286 // add this to the headers message
3287 vHeaders
.push_back(pindex
->GetBlockHeader());
3288 } else if (PeerHasHeader(&state
, pindex
)) {
3289 continue; // keep looking for the first new block
3290 } else if (pindex
->pprev
== nullptr || PeerHasHeader(&state
, pindex
->pprev
)) {
3291 // Peer doesn't have this header but they do have the prior one.
3292 // Start sending headers.
3293 fFoundStartingHeader
= true;
3294 vHeaders
.push_back(pindex
->GetBlockHeader());
3296 // Peer doesn't have this header or the prior one -- nothing will
3297 // connect, so bail out.
3298 fRevertToInv
= true;
3303 if (!fRevertToInv
&& !vHeaders
.empty()) {
3304 if (vHeaders
.size() == 1 && state
.fPreferHeaderAndIDs
) {
3305 // We only send up to 1 block as header-and-ids, as otherwise
3306 // probably means we're doing an initial-ish-sync or they're slow
3307 LogPrint(BCLog::NET
, "%s sending header-and-ids %s to peer=%d\n", __func__
,
3308 vHeaders
.front().GetHash().ToString(), pto
->GetId());
3310 int nSendFlags
= state
.fWantsCmpctWitness
? 0 : SERIALIZE_TRANSACTION_NO_WITNESS
;
3312 bool fGotBlockFromCache
= false;
3314 LOCK(cs_most_recent_block
);
3315 if (most_recent_block_hash
== pBestIndex
->GetBlockHash()) {
3316 if (state
.fWantsCmpctWitness
|| !fWitnessesPresentInMostRecentCompactBlock
)
3317 connman
->PushMessage(pto
, msgMaker
.Make(nSendFlags
, NetMsgType::CMPCTBLOCK
, *most_recent_compact_block
));
3319 CBlockHeaderAndShortTxIDs
cmpctblock(*most_recent_block
, state
.fWantsCmpctWitness
);
3320 connman
->PushMessage(pto
, msgMaker
.Make(nSendFlags
, NetMsgType::CMPCTBLOCK
, cmpctblock
));
3322 fGotBlockFromCache
= true;
3325 if (!fGotBlockFromCache
) {
3327 bool ret
= ReadBlockFromDisk(block
, pBestIndex
, consensusParams
);
3329 CBlockHeaderAndShortTxIDs
cmpctblock(block
, state
.fWantsCmpctWitness
);
3330 connman
->PushMessage(pto
, msgMaker
.Make(nSendFlags
, NetMsgType::CMPCTBLOCK
, cmpctblock
));
3332 state
.pindexBestHeaderSent
= pBestIndex
;
3333 } else if (state
.fPreferHeaders
) {
3334 if (vHeaders
.size() > 1) {
3335 LogPrint(BCLog::NET
, "%s: %u headers, range (%s, %s), to peer=%d\n", __func__
,
3337 vHeaders
.front().GetHash().ToString(),
3338 vHeaders
.back().GetHash().ToString(), pto
->GetId());
3340 LogPrint(BCLog::NET
, "%s: sending header %s to peer=%d\n", __func__
,
3341 vHeaders
.front().GetHash().ToString(), pto
->GetId());
3343 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::HEADERS
, vHeaders
));
3344 state
.pindexBestHeaderSent
= pBestIndex
;
3346 fRevertToInv
= true;
3349 // If falling back to using an inv, just try to inv the tip.
3350 // The last entry in vBlockHashesToAnnounce was our tip at some point
3352 if (!pto
->vBlockHashesToAnnounce
.empty()) {
3353 const uint256
&hashToAnnounce
= pto
->vBlockHashesToAnnounce
.back();
3354 BlockMap::iterator mi
= mapBlockIndex
.find(hashToAnnounce
);
3355 assert(mi
!= mapBlockIndex
.end());
3356 const CBlockIndex
*pindex
= mi
->second
;
3358 // Warn if we're announcing a block that is not on the main chain.
3359 // This should be very rare and could be optimized out.
3360 // Just log for now.
3361 if (chainActive
[pindex
->nHeight
] != pindex
) {
3362 LogPrint(BCLog::NET
, "Announcing block %s not on main chain (tip=%s)\n",
3363 hashToAnnounce
.ToString(), chainActive
.Tip()->GetBlockHash().ToString());
3366 // If the peer's chain has this block, don't inv it back.
3367 if (!PeerHasHeader(&state
, pindex
)) {
3368 pto
->PushInventory(CInv(MSG_BLOCK
, hashToAnnounce
));
3369 LogPrint(BCLog::NET
, "%s: sending inv peer=%d hash=%s\n", __func__
,
3370 pto
->GetId(), hashToAnnounce
.ToString());
3374 pto
->vBlockHashesToAnnounce
.clear();
3378 // Message: inventory
3380 std::vector
<CInv
> vInv
;
3382 LOCK(pto
->cs_inventory
);
3383 vInv
.reserve(std::max
<size_t>(pto
->vInventoryBlockToSend
.size(), INVENTORY_BROADCAST_MAX
));
3386 for (const uint256
& hash
: pto
->vInventoryBlockToSend
) {
3387 vInv
.push_back(CInv(MSG_BLOCK
, hash
));
3388 if (vInv
.size() == MAX_INV_SZ
) {
3389 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::INV
, vInv
));
3393 pto
->vInventoryBlockToSend
.clear();
3395 // Check whether periodic sends should happen
3396 bool fSendTrickle
= pto
->fWhitelisted
;
3397 if (pto
->nNextInvSend
< nNow
) {
3398 fSendTrickle
= true;
3399 // Use half the delay for outbound peers, as there is less privacy concern for them.
3400 pto
->nNextInvSend
= PoissonNextSend(nNow
, INVENTORY_BROADCAST_INTERVAL
>> !pto
->fInbound
);
3403 // Time to send but the peer has requested we not relay transactions.
3405 LOCK(pto
->cs_filter
);
3406 if (!pto
->fRelayTxes
) pto
->setInventoryTxToSend
.clear();
3409 // Respond to BIP35 mempool requests
3410 if (fSendTrickle
&& pto
->fSendMempool
) {
3411 auto vtxinfo
= mempool
.infoAll();
3412 pto
->fSendMempool
= false;
3413 CAmount filterrate
= 0;
3415 LOCK(pto
->cs_feeFilter
);
3416 filterrate
= pto
->minFeeFilter
;
3419 LOCK(pto
->cs_filter
);
3421 for (const auto& txinfo
: vtxinfo
) {
3422 const uint256
& hash
= txinfo
.tx
->GetHash();
3423 CInv
inv(MSG_TX
, hash
);
3424 pto
->setInventoryTxToSend
.erase(hash
);
3426 if (txinfo
.feeRate
.GetFeePerK() < filterrate
)
3430 if (!pto
->pfilter
->IsRelevantAndUpdate(*txinfo
.tx
)) continue;
3432 pto
->filterInventoryKnown
.insert(hash
);
3433 vInv
.push_back(inv
);
3434 if (vInv
.size() == MAX_INV_SZ
) {
3435 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::INV
, vInv
));
3439 pto
->timeLastMempoolReq
= GetTime();
3442 // Determine transactions to relay
3444 // Produce a vector with all candidates for sending
3445 std::vector
<std::set
<uint256
>::iterator
> vInvTx
;
3446 vInvTx
.reserve(pto
->setInventoryTxToSend
.size());
3447 for (std::set
<uint256
>::iterator it
= pto
->setInventoryTxToSend
.begin(); it
!= pto
->setInventoryTxToSend
.end(); it
++) {
3448 vInvTx
.push_back(it
);
3450 CAmount filterrate
= 0;
3452 LOCK(pto
->cs_feeFilter
);
3453 filterrate
= pto
->minFeeFilter
;
3455 // Topologically and fee-rate sort the inventory we send for privacy and priority reasons.
3456 // A heap is used so that not all items need sorting if only a few are being sent.
3457 CompareInvMempoolOrder
compareInvMempoolOrder(&mempool
);
3458 std::make_heap(vInvTx
.begin(), vInvTx
.end(), compareInvMempoolOrder
);
3459 // No reason to drain out at many times the network's capacity,
3460 // especially since we have many peers and some will draw much shorter delays.
3461 unsigned int nRelayedTransactions
= 0;
3462 LOCK(pto
->cs_filter
);
3463 while (!vInvTx
.empty() && nRelayedTransactions
< INVENTORY_BROADCAST_MAX
) {
3464 // Fetch the top element from the heap
3465 std::pop_heap(vInvTx
.begin(), vInvTx
.end(), compareInvMempoolOrder
);
3466 std::set
<uint256
>::iterator it
= vInvTx
.back();
3469 // Remove it from the to-be-sent set
3470 pto
->setInventoryTxToSend
.erase(it
);
3471 // Check if not in the filter already
3472 if (pto
->filterInventoryKnown
.contains(hash
)) {
3475 // Not in the mempool anymore? don't bother sending it.
3476 auto txinfo
= mempool
.info(hash
);
3480 if (filterrate
&& txinfo
.feeRate
.GetFeePerK() < filterrate
) {
3483 if (pto
->pfilter
&& !pto
->pfilter
->IsRelevantAndUpdate(*txinfo
.tx
)) continue;
3485 vInv
.push_back(CInv(MSG_TX
, hash
));
3486 nRelayedTransactions
++;
3488 // Expire old relay messages
3489 while (!vRelayExpiration
.empty() && vRelayExpiration
.front().first
< nNow
)
3491 mapRelay
.erase(vRelayExpiration
.front().second
);
3492 vRelayExpiration
.pop_front();
3495 auto ret
= mapRelay
.insert(std::make_pair(hash
, std::move(txinfo
.tx
)));
3497 vRelayExpiration
.push_back(std::make_pair(nNow
+ 15 * 60 * 1000000, ret
.first
));
3500 if (vInv
.size() == MAX_INV_SZ
) {
3501 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::INV
, vInv
));
3504 pto
->filterInventoryKnown
.insert(hash
);
3509 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::INV
, vInv
));
3511 // Detect whether we're stalling
3512 nNow
= GetTimeMicros();
3513 if (state
.nStallingSince
&& state
.nStallingSince
< nNow
- 1000000 * BLOCK_STALLING_TIMEOUT
) {
3514 // Stalling only triggers when the block download window cannot move. During normal steady state,
3515 // the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
3516 // should only happen during initial block download.
3517 LogPrintf("Peer=%d is stalling block download, disconnecting\n", pto
->GetId());
3518 pto
->fDisconnect
= true;
3521 // In case there is a block that has been in flight from this peer for 2 + 0.5 * N times the block interval
3522 // (with N the number of peers from which we're downloading validated blocks), disconnect due to timeout.
3523 // We compensate for other peers to prevent killing off peers due to our own downstream link
3524 // being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
3525 // to unreasonably increase our timeout.
3526 if (state
.vBlocksInFlight
.size() > 0) {
3527 QueuedBlock
&queuedBlock
= state
.vBlocksInFlight
.front();
3528 int nOtherPeersWithValidatedDownloads
= nPeersWithValidatedDownloads
- (state
.nBlocksInFlightValidHeaders
> 0);
3529 if (nNow
> state
.nDownloadingSince
+ consensusParams
.nPowTargetSpacing
* (BLOCK_DOWNLOAD_TIMEOUT_BASE
+ BLOCK_DOWNLOAD_TIMEOUT_PER_PEER
* nOtherPeersWithValidatedDownloads
)) {
3530 LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", queuedBlock
.hash
.ToString(), pto
->GetId());
3531 pto
->fDisconnect
= true;
3535 // Check for headers sync timeouts
3536 if (state
.fSyncStarted
&& state
.nHeadersSyncTimeout
< std::numeric_limits
<int64_t>::max()) {
3537 // Detect whether this is a stalling initial-headers-sync peer
3538 if (pindexBestHeader
->GetBlockTime() <= GetAdjustedTime() - 24*60*60) {
3539 if (nNow
> state
.nHeadersSyncTimeout
&& nSyncStarted
== 1 && (nPreferredDownload
- state
.fPreferredDownload
>= 1)) {
3540 // Disconnect a (non-whitelisted) peer if it is our only sync peer,
3541 // and we have others we could be using instead.
3542 // Note: If all our peers are inbound, then we won't
3543 // disconnect our sync peer for stalling; we have bigger
3544 // problems if we can't get any outbound peers.
3545 if (!pto
->fWhitelisted
) {
3546 LogPrintf("Timeout downloading headers from peer=%d, disconnecting\n", pto
->GetId());
3547 pto
->fDisconnect
= true;
3550 LogPrintf("Timeout downloading headers from whitelisted peer=%d, not disconnecting\n", pto
->GetId());
3551 // Reset the headers sync state so that we have a
3552 // chance to try downloading from a different peer.
3553 // Note: this will also result in at least one more
3554 // getheaders message to be sent to
3555 // this peer (eventually).
3556 state
.fSyncStarted
= false;
3558 state
.nHeadersSyncTimeout
= 0;
3562 // After we've caught up once, reset the timeout so we can't trigger
3563 // disconnect later.
3564 state
.nHeadersSyncTimeout
= std::numeric_limits
<int64_t>::max();
3568 // Check that outbound peers have reasonable chains
3569 // GetTime() is used by this anti-DoS logic so we can test this using mocktime
3570 ConsiderEviction(pto
, GetTime());
3573 // Message: getdata (blocks)
3575 std::vector
<CInv
> vGetData
;
3576 if (!pto
->fClient
&& (fFetch
|| !IsInitialBlockDownload()) && state
.nBlocksInFlight
< MAX_BLOCKS_IN_TRANSIT_PER_PEER
) {
3577 std::vector
<const CBlockIndex
*> vToDownload
;
3578 NodeId staller
= -1;
3579 FindNextBlocksToDownload(pto
->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER
- state
.nBlocksInFlight
, vToDownload
, staller
, consensusParams
);
3580 for (const CBlockIndex
*pindex
: vToDownload
) {
3581 uint32_t nFetchFlags
= GetFetchFlags(pto
);
3582 vGetData
.push_back(CInv(MSG_BLOCK
| nFetchFlags
, pindex
->GetBlockHash()));
3583 MarkBlockAsInFlight(pto
->GetId(), pindex
->GetBlockHash(), pindex
);
3584 LogPrint(BCLog::NET
, "Requesting block %s (%d) peer=%d\n", pindex
->GetBlockHash().ToString(),
3585 pindex
->nHeight
, pto
->GetId());
3587 if (state
.nBlocksInFlight
== 0 && staller
!= -1) {
3588 if (State(staller
)->nStallingSince
== 0) {
3589 State(staller
)->nStallingSince
= nNow
;
3590 LogPrint(BCLog::NET
, "Stall started peer=%d\n", staller
);
3596 // Message: getdata (non-blocks)
3598 while (!pto
->mapAskFor
.empty() && (*pto
->mapAskFor
.begin()).first
<= nNow
)
3600 const CInv
& inv
= (*pto
->mapAskFor
.begin()).second
;
3601 if (!AlreadyHave(inv
))
3603 LogPrint(BCLog::NET
, "Requesting %s peer=%d\n", inv
.ToString(), pto
->GetId());
3604 vGetData
.push_back(inv
);
3605 if (vGetData
.size() >= 1000)
3607 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::GETDATA
, vGetData
));
3611 //If we're not going to ask, don't expect a response.
3612 pto
->setAskFor
.erase(inv
.hash
);
3614 pto
->mapAskFor
.erase(pto
->mapAskFor
.begin());
3616 if (!vGetData
.empty())
3617 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::GETDATA
, vGetData
));
3620 // Message: feefilter
3622 // We don't want white listed peers to filter txs to us if we have -whitelistforcerelay
3623 if (pto
->nVersion
>= FEEFILTER_VERSION
&& gArgs
.GetBoolArg("-feefilter", DEFAULT_FEEFILTER
) &&
3624 !(pto
->fWhitelisted
&& gArgs
.GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY
))) {
3625 CAmount currentFilter
= mempool
.GetMinFee(gArgs
.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE
) * 1000000).GetFeePerK();
3626 int64_t timeNow
= GetTimeMicros();
3627 if (timeNow
> pto
->nextSendTimeFeeFilter
) {
3628 static CFeeRate
default_feerate(DEFAULT_MIN_RELAY_TX_FEE
);
3629 static FeeFilterRounder
filterRounder(default_feerate
);
3630 CAmount filterToSend
= filterRounder
.round(currentFilter
);
3631 // We always have a fee filter of at least minRelayTxFee
3632 filterToSend
= std::max(filterToSend
, ::minRelayTxFee
.GetFeePerK());
3633 if (filterToSend
!= pto
->lastSentFeeFilter
) {
3634 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::FEEFILTER
, filterToSend
));
3635 pto
->lastSentFeeFilter
= filterToSend
;
3637 pto
->nextSendTimeFeeFilter
= PoissonNextSend(timeNow
, AVG_FEEFILTER_BROADCAST_INTERVAL
);
3639 // If the fee filter has changed substantially and it's still more than MAX_FEEFILTER_CHANGE_DELAY
3640 // until scheduled broadcast, then move the broadcast to within MAX_FEEFILTER_CHANGE_DELAY.
3641 else if (timeNow
+ MAX_FEEFILTER_CHANGE_DELAY
* 1000000 < pto
->nextSendTimeFeeFilter
&&
3642 (currentFilter
< 3 * pto
->lastSentFeeFilter
/ 4 || currentFilter
> 4 * pto
->lastSentFeeFilter
/ 3)) {
3643 pto
->nextSendTimeFeeFilter
= timeNow
+ GetRandInt(MAX_FEEFILTER_CHANGE_DELAY
) * 1000000;
3650 class CNetProcessingCleanup
3653 CNetProcessingCleanup() {}
3654 ~CNetProcessingCleanup() {
3655 // orphan transactions
3656 mapOrphanTransactions
.clear();
3657 mapOrphanTransactionsByPrev
.clear();
3659 } instance_of_cnetprocessingcleanup
;