1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #include "net_processing.h"
9 #include "arith_uint256.h"
10 #include "blockencodings.h"
11 #include "chainparams.h"
12 #include "consensus/validation.h"
15 #include "validation.h"
16 #include "merkleblock.h"
18 #include "netmessagemaker.h"
20 #include "policy/fees.h"
21 #include "policy/policy.h"
22 #include "primitives/block.h"
23 #include "primitives/transaction.h"
25 #include "reverse_iterator.h"
26 #include "tinyformat.h"
27 #include "txmempool.h"
28 #include "ui_interface.h"
30 #include "utilmoneystr.h"
31 #include "utilstrencodings.h"
32 #include "validationinterface.h"
35 # error "Bitcoin cannot be compiled without assertions."
38 std::atomic
<int64_t> nTimeBestReceived(0); // Used only to inform the wallet of when we last received a block
40 struct IteratorComparator
43 bool operator()(const I
& a
, const I
& b
)
50 // When modifying, adapt the copy of this definition in tests/DoS_tests.
55 std::map
<uint256
, COrphanTx
> mapOrphanTransactions
GUARDED_BY(cs_main
);
56 std::map
<COutPoint
, std::set
<std::map
<uint256
, COrphanTx
>::iterator
, IteratorComparator
>> mapOrphanTransactionsByPrev
GUARDED_BY(cs_main
);
57 void EraseOrphansFor(NodeId peer
) EXCLUSIVE_LOCKS_REQUIRED(cs_main
);
59 static size_t vExtraTxnForCompactIt
= 0;
60 static std::vector
<std::pair
<uint256
, CTransactionRef
>> vExtraTxnForCompact
GUARDED_BY(cs_main
);
62 static const uint64_t RANDOMIZER_ID_ADDRESS_RELAY
= 0x3cac0035b5866b90ULL
; // SHA256("main address relay")[0:8]
64 /// Age after which a stale block will no longer be served if requested as
65 /// protection against fingerprinting. Set to one month, denominated in seconds.
66 static const int STALE_RELAY_AGE_LIMIT
= 30 * 24 * 60 * 60;
68 /// Age after which a block is considered historical for purposes of rate
69 /// limiting block relay. Set to one week, denominated in seconds.
70 static const int HISTORICAL_BLOCK_AGE
= 7 * 24 * 60 * 60;
74 /** Number of nodes with fSyncStarted. */
78 * Sources of received blocks, saved to be able to send them reject
79 * messages or ban them when processing happens afterwards. Protected by
81 * Set mapBlockSource[hash].second to false if the node should not be
82 * punished if the block is invalid.
84 std::map
<uint256
, std::pair
<NodeId
, bool>> mapBlockSource
;
87 * Filter for transactions that were recently rejected by
88 * AcceptToMemoryPool. These are not rerequested until the chain tip
89 * changes, at which point the entire filter is reset. Protected by
92 * Without this filter we'd be re-requesting txs from each of our peers,
93 * increasing bandwidth consumption considerably. For instance, with 100
94 * peers, half of which relay a tx we don't accept, that might be a 50x
95 * bandwidth increase. A flooding attacker attempting to roll-over the
96 * filter using minimum-sized, 60byte, transactions might manage to send
97 * 1000/sec if we have fast peers, so we pick 120,000 to give our peers a
98 * two minute window to send invs to us.
100 * Decreasing the false positive rate is fairly cheap, so we pick one in a
101 * million to make it highly unlikely for users to have issues with this
104 * Memory used: 1.3 MB
106 std::unique_ptr
<CRollingBloomFilter
> recentRejects
;
107 uint256 hashRecentRejectsChainTip
;
109 /** Blocks that are in flight, and that are in the queue to be downloaded. Protected by cs_main. */
112 const CBlockIndex
* pindex
; //!< Optional.
113 bool fValidatedHeaders
; //!< Whether this block has validated headers at the time of request.
114 std::unique_ptr
<PartiallyDownloadedBlock
> partialBlock
; //!< Optional, used for CMPCTBLOCK downloads
116 std::map
<uint256
, std::pair
<NodeId
, std::list
<QueuedBlock
>::iterator
> > mapBlocksInFlight
;
118 /** Stack of nodes which we have set to announce using compact blocks */
119 std::list
<NodeId
> lNodesAnnouncingHeaderAndIDs
;
121 /** Number of preferable block download peers. */
122 int nPreferredDownload
= 0;
124 /** Number of peers from which we're downloading blocks. */
125 int nPeersWithValidatedDownloads
= 0;
127 /** Number of outbound peers with m_chain_sync.m_protect. */
128 int g_outbound_peers_with_protect_from_disconnect
= 0;
130 /** Relay map, protected by cs_main. */
131 typedef std::map
<uint256
, CTransactionRef
> MapRelay
;
133 /** Expiration-time ordered list of (expire time, relay map entry) pairs, protected by cs_main). */
134 std::deque
<std::pair
<int64_t, MapRelay::iterator
>> vRelayExpiration
;
139 struct CBlockReject
{
140 unsigned char chRejectCode
;
141 std::string strRejectReason
;
146 * Maintain validation-specific state about nodes, protected by cs_main, instead
147 * by CNode's own locks. This simplifies asynchronous operation, where
148 * processing of incoming data is done after the ProcessMessage call returns,
149 * and we're no longer holding the node's locks.
152 //! The peer's address
153 const CService address
;
154 //! Whether we have a fully established connection.
155 bool fCurrentlyConnected
;
156 //! Accumulated misbehaviour score for this peer.
158 //! Whether this peer should be disconnected and banned (unless whitelisted).
160 //! String name of this peer (debugging/logging purposes).
161 const std::string name
;
162 //! List of asynchronously-determined block rejections to notify this peer about.
163 std::vector
<CBlockReject
> rejects
;
164 //! The best known block we know this peer has announced.
165 const CBlockIndex
*pindexBestKnownBlock
;
166 //! The hash of the last unknown block this peer has announced.
167 uint256 hashLastUnknownBlock
;
168 //! The last full block we both have.
169 const CBlockIndex
*pindexLastCommonBlock
;
170 //! The best header we have sent our peer.
171 const CBlockIndex
*pindexBestHeaderSent
;
172 //! Length of current-streak of unconnecting headers announcements
173 int nUnconnectingHeaders
;
174 //! Whether we've started headers synchronization with this peer.
176 //! When to potentially disconnect peer for stalling headers download
177 int64_t nHeadersSyncTimeout
;
178 //! Since when we're stalling block download progress (in microseconds), or 0.
179 int64_t nStallingSince
;
180 std::list
<QueuedBlock
> vBlocksInFlight
;
181 //! When the first entry in vBlocksInFlight started downloading. Don't care when vBlocksInFlight is empty.
182 int64_t nDownloadingSince
;
184 int nBlocksInFlightValidHeaders
;
185 //! Whether we consider this a preferred download peer.
186 bool fPreferredDownload
;
187 //! Whether this peer wants invs or headers (when possible) for block announcements.
189 //! Whether this peer wants invs or cmpctblocks (when possible) for block announcements.
190 bool fPreferHeaderAndIDs
;
192 * Whether this peer will send us cmpctblocks if we request them.
193 * This is not used to gate request logic, as we really only care about fSupportsDesiredCmpctVersion,
194 * but is used as a flag to "lock in" the version of compact blocks (fWantsCmpctWitness) we send.
196 bool fProvidesHeaderAndIDs
;
197 //! Whether this peer can give us witnesses
199 //! Whether this peer wants witnesses in cmpctblocks/blocktxns
200 bool fWantsCmpctWitness
;
202 * If we've announced NODE_WITNESS to this peer: whether the peer sends witnesses in cmpctblocks/blocktxns,
203 * otherwise: whether this peer sends non-witnesses in cmpctblocks/blocktxns.
205 bool fSupportsDesiredCmpctVersion
;
207 /** State used to enforce CHAIN_SYNC_TIMEOUT
208 * Only in effect for outbound, non-manual connections, with
210 * Algorithm: if a peer's best known block has less work than our tip,
211 * set a timeout CHAIN_SYNC_TIMEOUT seconds in the future:
212 * - If at timeout their best known block now has more work than our tip
213 * when the timeout was set, then either reset the timeout or clear it
214 * (after comparing against our current tip's work)
215 * - If at timeout their best known block still has less work than our
216 * tip did when the timeout was set, then send a getheaders message,
217 * and set a shorter timeout, HEADERS_RESPONSE_TIME seconds in future.
218 * If their best known block is still behind when that new timeout is
219 * reached, disconnect.
221 struct ChainSyncTimeoutState
{
222 //! A timeout used for checking whether our peer has sufficiently synced
224 //! A header with the work we require on our peer's chain
225 const CBlockIndex
* m_work_header
;
226 //! After timeout is reached, set to true after sending getheaders
227 bool m_sent_getheaders
;
228 //! Whether this peer is protected from disconnection due to a bad/slow chain
232 ChainSyncTimeoutState m_chain_sync
;
234 CNodeState(CAddress addrIn
, std::string addrNameIn
) : address(addrIn
), name(addrNameIn
) {
235 fCurrentlyConnected
= false;
238 pindexBestKnownBlock
= nullptr;
239 hashLastUnknownBlock
.SetNull();
240 pindexLastCommonBlock
= nullptr;
241 pindexBestHeaderSent
= nullptr;
242 nUnconnectingHeaders
= 0;
243 fSyncStarted
= false;
244 nHeadersSyncTimeout
= 0;
246 nDownloadingSince
= 0;
248 nBlocksInFlightValidHeaders
= 0;
249 fPreferredDownload
= false;
250 fPreferHeaders
= false;
251 fPreferHeaderAndIDs
= false;
252 fProvidesHeaderAndIDs
= false;
253 fHaveWitness
= false;
254 fWantsCmpctWitness
= false;
255 fSupportsDesiredCmpctVersion
= false;
256 m_chain_sync
= { 0, nullptr, false, false };
260 /** Map maintaining per-node state. Requires cs_main. */
261 std::map
<NodeId
, CNodeState
> mapNodeState
;
264 CNodeState
*State(NodeId pnode
) {
265 std::map
<NodeId
, CNodeState
>::iterator it
= mapNodeState
.find(pnode
);
266 if (it
== mapNodeState
.end())
271 void UpdatePreferredDownload(CNode
* node
, CNodeState
* state
)
273 nPreferredDownload
-= state
->fPreferredDownload
;
275 // Whether this node should be marked as a preferred download node.
276 state
->fPreferredDownload
= (!node
->fInbound
|| node
->fWhitelisted
) && !node
->fOneShot
&& !node
->fClient
;
278 nPreferredDownload
+= state
->fPreferredDownload
;
281 void PushNodeVersion(CNode
*pnode
, CConnman
* connman
, int64_t nTime
)
283 ServiceFlags nLocalNodeServices
= pnode
->GetLocalServices();
284 uint64_t nonce
= pnode
->GetLocalNonce();
285 int nNodeStartingHeight
= pnode
->GetMyStartingHeight();
286 NodeId nodeid
= pnode
->GetId();
287 CAddress addr
= pnode
->addr
;
289 CAddress addrYou
= (addr
.IsRoutable() && !IsProxy(addr
) ? addr
: CAddress(CService(), addr
.nServices
));
290 CAddress addrMe
= CAddress(CService(), nLocalNodeServices
);
292 connman
->PushMessage(pnode
, CNetMsgMaker(INIT_PROTO_VERSION
).Make(NetMsgType::VERSION
, PROTOCOL_VERSION
, (uint64_t)nLocalNodeServices
, nTime
, addrYou
, addrMe
,
293 nonce
, strSubVersion
, nNodeStartingHeight
, ::fRelayTxes
));
296 LogPrint(BCLog::NET
, "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION
, nNodeStartingHeight
, addrMe
.ToString(), addrYou
.ToString(), nodeid
);
298 LogPrint(BCLog::NET
, "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION
, nNodeStartingHeight
, addrMe
.ToString(), nodeid
);
303 // Returns a bool indicating whether we requested this block.
304 // Also used if a block was /not/ received and timed out or started with another peer
305 bool MarkBlockAsReceived(const uint256
& hash
) {
306 std::map
<uint256
, std::pair
<NodeId
, std::list
<QueuedBlock
>::iterator
> >::iterator itInFlight
= mapBlocksInFlight
.find(hash
);
307 if (itInFlight
!= mapBlocksInFlight
.end()) {
308 CNodeState
*state
= State(itInFlight
->second
.first
);
309 assert(state
!= nullptr);
310 state
->nBlocksInFlightValidHeaders
-= itInFlight
->second
.second
->fValidatedHeaders
;
311 if (state
->nBlocksInFlightValidHeaders
== 0 && itInFlight
->second
.second
->fValidatedHeaders
) {
312 // Last validated block on the queue was received.
313 nPeersWithValidatedDownloads
--;
315 if (state
->vBlocksInFlight
.begin() == itInFlight
->second
.second
) {
316 // First block on the queue was received, update the start download time for the next one
317 state
->nDownloadingSince
= std::max(state
->nDownloadingSince
, GetTimeMicros());
319 state
->vBlocksInFlight
.erase(itInFlight
->second
.second
);
320 state
->nBlocksInFlight
--;
321 state
->nStallingSince
= 0;
322 mapBlocksInFlight
.erase(itInFlight
);
329 // returns false, still setting pit, if the block was already in flight from the same peer
330 // pit will only be valid as long as the same cs_main lock is being held
331 bool MarkBlockAsInFlight(NodeId nodeid
, const uint256
& hash
, const CBlockIndex
* pindex
= nullptr, std::list
<QueuedBlock
>::iterator
** pit
= nullptr) {
332 CNodeState
*state
= State(nodeid
);
333 assert(state
!= nullptr);
335 // Short-circuit most stuff in case its from the same node
336 std::map
<uint256
, std::pair
<NodeId
, std::list
<QueuedBlock
>::iterator
> >::iterator itInFlight
= mapBlocksInFlight
.find(hash
);
337 if (itInFlight
!= mapBlocksInFlight
.end() && itInFlight
->second
.first
== nodeid
) {
339 *pit
= &itInFlight
->second
.second
;
344 // Make sure it's not listed somewhere already.
345 MarkBlockAsReceived(hash
);
347 std::list
<QueuedBlock
>::iterator it
= state
->vBlocksInFlight
.insert(state
->vBlocksInFlight
.end(),
348 {hash
, pindex
, pindex
!= nullptr, std::unique_ptr
<PartiallyDownloadedBlock
>(pit
? new PartiallyDownloadedBlock(&mempool
) : nullptr)});
349 state
->nBlocksInFlight
++;
350 state
->nBlocksInFlightValidHeaders
+= it
->fValidatedHeaders
;
351 if (state
->nBlocksInFlight
== 1) {
352 // We're starting a block download (batch) from this peer.
353 state
->nDownloadingSince
= GetTimeMicros();
355 if (state
->nBlocksInFlightValidHeaders
== 1 && pindex
!= nullptr) {
356 nPeersWithValidatedDownloads
++;
358 itInFlight
= mapBlocksInFlight
.insert(std::make_pair(hash
, std::make_pair(nodeid
, it
))).first
;
360 *pit
= &itInFlight
->second
.second
;
364 /** Check whether the last unknown block a peer advertised is not yet known. */
365 void ProcessBlockAvailability(NodeId nodeid
) {
366 CNodeState
*state
= State(nodeid
);
367 assert(state
!= nullptr);
369 if (!state
->hashLastUnknownBlock
.IsNull()) {
370 BlockMap::iterator itOld
= mapBlockIndex
.find(state
->hashLastUnknownBlock
);
371 if (itOld
!= mapBlockIndex
.end() && itOld
->second
->nChainWork
> 0) {
372 if (state
->pindexBestKnownBlock
== nullptr || itOld
->second
->nChainWork
>= state
->pindexBestKnownBlock
->nChainWork
)
373 state
->pindexBestKnownBlock
= itOld
->second
;
374 state
->hashLastUnknownBlock
.SetNull();
379 /** Update tracking information about which blocks a peer is assumed to have. */
380 void UpdateBlockAvailability(NodeId nodeid
, const uint256
&hash
) {
381 CNodeState
*state
= State(nodeid
);
382 assert(state
!= nullptr);
384 ProcessBlockAvailability(nodeid
);
386 BlockMap::iterator it
= mapBlockIndex
.find(hash
);
387 if (it
!= mapBlockIndex
.end() && it
->second
->nChainWork
> 0) {
388 // An actually better block was announced.
389 if (state
->pindexBestKnownBlock
== nullptr || it
->second
->nChainWork
>= state
->pindexBestKnownBlock
->nChainWork
)
390 state
->pindexBestKnownBlock
= it
->second
;
392 // An unknown block was announced; just assume that the latest one is the best one.
393 state
->hashLastUnknownBlock
= hash
;
397 void MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid
, CConnman
* connman
) {
398 AssertLockHeld(cs_main
);
399 CNodeState
* nodestate
= State(nodeid
);
400 if (!nodestate
|| !nodestate
->fSupportsDesiredCmpctVersion
) {
401 // Never ask from peers who can't provide witnesses.
404 if (nodestate
->fProvidesHeaderAndIDs
) {
405 for (std::list
<NodeId
>::iterator it
= lNodesAnnouncingHeaderAndIDs
.begin(); it
!= lNodesAnnouncingHeaderAndIDs
.end(); it
++) {
407 lNodesAnnouncingHeaderAndIDs
.erase(it
);
408 lNodesAnnouncingHeaderAndIDs
.push_back(nodeid
);
412 connman
->ForNode(nodeid
, [connman
](CNode
* pfrom
){
413 uint64_t nCMPCTBLOCKVersion
= (pfrom
->GetLocalServices() & NODE_WITNESS
) ? 2 : 1;
414 if (lNodesAnnouncingHeaderAndIDs
.size() >= 3) {
415 // As per BIP152, we only get 3 of our peers to announce
416 // blocks using compact encodings.
417 connman
->ForNode(lNodesAnnouncingHeaderAndIDs
.front(), [connman
, nCMPCTBLOCKVersion
](CNode
* pnodeStop
){
418 connman
->PushMessage(pnodeStop
, CNetMsgMaker(pnodeStop
->GetSendVersion()).Make(NetMsgType::SENDCMPCT
, /*fAnnounceUsingCMPCTBLOCK=*/false, nCMPCTBLOCKVersion
));
421 lNodesAnnouncingHeaderAndIDs
.pop_front();
423 connman
->PushMessage(pfrom
, CNetMsgMaker(pfrom
->GetSendVersion()).Make(NetMsgType::SENDCMPCT
, /*fAnnounceUsingCMPCTBLOCK=*/true, nCMPCTBLOCKVersion
));
424 lNodesAnnouncingHeaderAndIDs
.push_back(pfrom
->GetId());
431 bool CanDirectFetch(const Consensus::Params
&consensusParams
)
433 return chainActive
.Tip()->GetBlockTime() > GetAdjustedTime() - consensusParams
.nPowTargetSpacing
* 20;
437 bool PeerHasHeader(CNodeState
*state
, const CBlockIndex
*pindex
)
439 if (state
->pindexBestKnownBlock
&& pindex
== state
->pindexBestKnownBlock
->GetAncestor(pindex
->nHeight
))
441 if (state
->pindexBestHeaderSent
&& pindex
== state
->pindexBestHeaderSent
->GetAncestor(pindex
->nHeight
))
446 /** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
447 * at most count entries. */
448 void FindNextBlocksToDownload(NodeId nodeid
, unsigned int count
, std::vector
<const CBlockIndex
*>& vBlocks
, NodeId
& nodeStaller
, const Consensus::Params
& consensusParams
) {
452 vBlocks
.reserve(vBlocks
.size() + count
);
453 CNodeState
*state
= State(nodeid
);
454 assert(state
!= nullptr);
456 // Make sure pindexBestKnownBlock is up to date, we'll need it.
457 ProcessBlockAvailability(nodeid
);
459 if (state
->pindexBestKnownBlock
== nullptr || state
->pindexBestKnownBlock
->nChainWork
< chainActive
.Tip()->nChainWork
|| state
->pindexBestKnownBlock
->nChainWork
< nMinimumChainWork
) {
460 // This peer has nothing interesting.
464 if (state
->pindexLastCommonBlock
== nullptr) {
465 // Bootstrap quickly by guessing a parent of our best tip is the forking point.
466 // Guessing wrong in either direction is not a problem.
467 state
->pindexLastCommonBlock
= chainActive
[std::min(state
->pindexBestKnownBlock
->nHeight
, chainActive
.Height())];
470 // If the peer reorganized, our previous pindexLastCommonBlock may not be an ancestor
471 // of its current tip anymore. Go back enough to fix that.
472 state
->pindexLastCommonBlock
= LastCommonAncestor(state
->pindexLastCommonBlock
, state
->pindexBestKnownBlock
);
473 if (state
->pindexLastCommonBlock
== state
->pindexBestKnownBlock
)
476 std::vector
<const CBlockIndex
*> vToFetch
;
477 const CBlockIndex
*pindexWalk
= state
->pindexLastCommonBlock
;
478 // Never fetch further than the best block we know the peer has, or more than BLOCK_DOWNLOAD_WINDOW + 1 beyond the last
479 // linked block we have in common with this peer. The +1 is so we can detect stalling, namely if we would be able to
480 // download that next block if the window were 1 larger.
481 int nWindowEnd
= state
->pindexLastCommonBlock
->nHeight
+ BLOCK_DOWNLOAD_WINDOW
;
482 int nMaxHeight
= std::min
<int>(state
->pindexBestKnownBlock
->nHeight
, nWindowEnd
+ 1);
483 NodeId waitingfor
= -1;
484 while (pindexWalk
->nHeight
< nMaxHeight
) {
485 // Read up to 128 (or more, if more blocks than that are needed) successors of pindexWalk (towards
486 // pindexBestKnownBlock) into vToFetch. We fetch 128, because CBlockIndex::GetAncestor may be as expensive
487 // as iterating over ~100 CBlockIndex* entries anyway.
488 int nToFetch
= std::min(nMaxHeight
- pindexWalk
->nHeight
, std::max
<int>(count
- vBlocks
.size(), 128));
489 vToFetch
.resize(nToFetch
);
490 pindexWalk
= state
->pindexBestKnownBlock
->GetAncestor(pindexWalk
->nHeight
+ nToFetch
);
491 vToFetch
[nToFetch
- 1] = pindexWalk
;
492 for (unsigned int i
= nToFetch
- 1; i
> 0; i
--) {
493 vToFetch
[i
- 1] = vToFetch
[i
]->pprev
;
496 // Iterate over those blocks in vToFetch (in forward direction), adding the ones that
497 // are not yet downloaded and not in flight to vBlocks. In the mean time, update
498 // pindexLastCommonBlock as long as all ancestors are already downloaded, or if it's
499 // already part of our chain (and therefore don't need it even if pruned).
500 for (const CBlockIndex
* pindex
: vToFetch
) {
501 if (!pindex
->IsValid(BLOCK_VALID_TREE
)) {
502 // We consider the chain that this peer is on invalid.
505 if (!State(nodeid
)->fHaveWitness
&& IsWitnessEnabled(pindex
->pprev
, consensusParams
)) {
506 // We wouldn't download this block or its descendants from this peer.
509 if (pindex
->nStatus
& BLOCK_HAVE_DATA
|| chainActive
.Contains(pindex
)) {
510 if (pindex
->nChainTx
)
511 state
->pindexLastCommonBlock
= pindex
;
512 } else if (mapBlocksInFlight
.count(pindex
->GetBlockHash()) == 0) {
513 // The block is not already downloaded, and not yet in flight.
514 if (pindex
->nHeight
> nWindowEnd
) {
515 // We reached the end of the window.
516 if (vBlocks
.size() == 0 && waitingfor
!= nodeid
) {
517 // We aren't able to fetch anything, but we would be if the download window was one larger.
518 nodeStaller
= waitingfor
;
522 vBlocks
.push_back(pindex
);
523 if (vBlocks
.size() == count
) {
526 } else if (waitingfor
== -1) {
527 // This is the first already-in-flight block.
528 waitingfor
= mapBlocksInFlight
[pindex
->GetBlockHash()].first
;
536 // Returns true for outbound peers, excluding manual connections, feelers, and
538 bool IsOutboundDisconnectionCandidate(const CNode
*node
)
540 return !(node
->fInbound
|| node
->m_manual_connection
|| node
->fFeeler
|| node
->fOneShot
);
543 void PeerLogicValidation::InitializeNode(CNode
*pnode
) {
544 CAddress addr
= pnode
->addr
;
545 std::string addrName
= pnode
->GetAddrName();
546 NodeId nodeid
= pnode
->GetId();
549 mapNodeState
.emplace_hint(mapNodeState
.end(), std::piecewise_construct
, std::forward_as_tuple(nodeid
), std::forward_as_tuple(addr
, std::move(addrName
)));
552 PushNodeVersion(pnode
, connman
, GetTime());
555 void PeerLogicValidation::FinalizeNode(NodeId nodeid
, bool& fUpdateConnectionTime
) {
556 fUpdateConnectionTime
= false;
558 CNodeState
*state
= State(nodeid
);
559 assert(state
!= nullptr);
561 if (state
->fSyncStarted
)
564 if (state
->nMisbehavior
== 0 && state
->fCurrentlyConnected
) {
565 fUpdateConnectionTime
= true;
568 for (const QueuedBlock
& entry
: state
->vBlocksInFlight
) {
569 mapBlocksInFlight
.erase(entry
.hash
);
571 EraseOrphansFor(nodeid
);
572 nPreferredDownload
-= state
->fPreferredDownload
;
573 nPeersWithValidatedDownloads
-= (state
->nBlocksInFlightValidHeaders
!= 0);
574 assert(nPeersWithValidatedDownloads
>= 0);
575 g_outbound_peers_with_protect_from_disconnect
-= state
->m_chain_sync
.m_protect
;
576 assert(g_outbound_peers_with_protect_from_disconnect
>= 0);
578 mapNodeState
.erase(nodeid
);
580 if (mapNodeState
.empty()) {
581 // Do a consistency check after the last peer is removed.
582 assert(mapBlocksInFlight
.empty());
583 assert(nPreferredDownload
== 0);
584 assert(nPeersWithValidatedDownloads
== 0);
585 assert(g_outbound_peers_with_protect_from_disconnect
== 0);
587 LogPrint(BCLog::NET
, "Cleared nodestate for peer=%d\n", nodeid
);
590 bool GetNodeStateStats(NodeId nodeid
, CNodeStateStats
&stats
) {
592 CNodeState
*state
= State(nodeid
);
593 if (state
== nullptr)
595 stats
.nMisbehavior
= state
->nMisbehavior
;
596 stats
.nSyncHeight
= state
->pindexBestKnownBlock
? state
->pindexBestKnownBlock
->nHeight
: -1;
597 stats
.nCommonHeight
= state
->pindexLastCommonBlock
? state
->pindexLastCommonBlock
->nHeight
: -1;
598 for (const QueuedBlock
& queue
: state
->vBlocksInFlight
) {
600 stats
.vHeightInFlight
.push_back(queue
.pindex
->nHeight
);
605 //////////////////////////////////////////////////////////////////////////////
607 // mapOrphanTransactions
610 void AddToCompactExtraTransactions(const CTransactionRef
& tx
)
612 size_t max_extra_txn
= gArgs
.GetArg("-blockreconstructionextratxn", DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN
);
613 if (max_extra_txn
<= 0)
615 if (!vExtraTxnForCompact
.size())
616 vExtraTxnForCompact
.resize(max_extra_txn
);
617 vExtraTxnForCompact
[vExtraTxnForCompactIt
] = std::make_pair(tx
->GetWitnessHash(), tx
);
618 vExtraTxnForCompactIt
= (vExtraTxnForCompactIt
+ 1) % max_extra_txn
;
621 bool AddOrphanTx(const CTransactionRef
& tx
, NodeId peer
) EXCLUSIVE_LOCKS_REQUIRED(cs_main
)
623 const uint256
& hash
= tx
->GetHash();
624 if (mapOrphanTransactions
.count(hash
))
627 // Ignore big transactions, to avoid a
628 // send-big-orphans memory exhaustion attack. If a peer has a legitimate
629 // large transaction with a missing parent then we assume
630 // it will rebroadcast it later, after the parent transaction(s)
631 // have been mined or received.
632 // 100 orphans, each of which is at most 99,999 bytes big is
633 // at most 10 megabytes of orphans and somewhat more byprev index (in the worst case):
634 unsigned int sz
= GetTransactionWeight(*tx
);
635 if (sz
>= MAX_STANDARD_TX_WEIGHT
)
637 LogPrint(BCLog::MEMPOOL
, "ignoring large orphan tx (size: %u, hash: %s)\n", sz
, hash
.ToString());
641 auto ret
= mapOrphanTransactions
.emplace(hash
, COrphanTx
{tx
, peer
, GetTime() + ORPHAN_TX_EXPIRE_TIME
});
643 for (const CTxIn
& txin
: tx
->vin
) {
644 mapOrphanTransactionsByPrev
[txin
.prevout
].insert(ret
.first
);
647 AddToCompactExtraTransactions(tx
);
649 LogPrint(BCLog::MEMPOOL
, "stored orphan tx %s (mapsz %u outsz %u)\n", hash
.ToString(),
650 mapOrphanTransactions
.size(), mapOrphanTransactionsByPrev
.size());
654 int static EraseOrphanTx(uint256 hash
) EXCLUSIVE_LOCKS_REQUIRED(cs_main
)
656 std::map
<uint256
, COrphanTx
>::iterator it
= mapOrphanTransactions
.find(hash
);
657 if (it
== mapOrphanTransactions
.end())
659 for (const CTxIn
& txin
: it
->second
.tx
->vin
)
661 auto itPrev
= mapOrphanTransactionsByPrev
.find(txin
.prevout
);
662 if (itPrev
== mapOrphanTransactionsByPrev
.end())
664 itPrev
->second
.erase(it
);
665 if (itPrev
->second
.empty())
666 mapOrphanTransactionsByPrev
.erase(itPrev
);
668 mapOrphanTransactions
.erase(it
);
672 void EraseOrphansFor(NodeId peer
)
675 std::map
<uint256
, COrphanTx
>::iterator iter
= mapOrphanTransactions
.begin();
676 while (iter
!= mapOrphanTransactions
.end())
678 std::map
<uint256
, COrphanTx
>::iterator maybeErase
= iter
++; // increment to avoid iterator becoming invalid
679 if (maybeErase
->second
.fromPeer
== peer
)
681 nErased
+= EraseOrphanTx(maybeErase
->second
.tx
->GetHash());
684 if (nErased
> 0) LogPrint(BCLog::MEMPOOL
, "Erased %d orphan tx from peer=%d\n", nErased
, peer
);
688 unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans
) EXCLUSIVE_LOCKS_REQUIRED(cs_main
)
690 unsigned int nEvicted
= 0;
691 static int64_t nNextSweep
;
692 int64_t nNow
= GetTime();
693 if (nNextSweep
<= nNow
) {
694 // Sweep out expired orphan pool entries:
696 int64_t nMinExpTime
= nNow
+ ORPHAN_TX_EXPIRE_TIME
- ORPHAN_TX_EXPIRE_INTERVAL
;
697 std::map
<uint256
, COrphanTx
>::iterator iter
= mapOrphanTransactions
.begin();
698 while (iter
!= mapOrphanTransactions
.end())
700 std::map
<uint256
, COrphanTx
>::iterator maybeErase
= iter
++;
701 if (maybeErase
->second
.nTimeExpire
<= nNow
) {
702 nErased
+= EraseOrphanTx(maybeErase
->second
.tx
->GetHash());
704 nMinExpTime
= std::min(maybeErase
->second
.nTimeExpire
, nMinExpTime
);
707 // Sweep again 5 minutes after the next entry that expires in order to batch the linear scan.
708 nNextSweep
= nMinExpTime
+ ORPHAN_TX_EXPIRE_INTERVAL
;
709 if (nErased
> 0) LogPrint(BCLog::MEMPOOL
, "Erased %d orphan tx due to expiration\n", nErased
);
711 while (mapOrphanTransactions
.size() > nMaxOrphans
)
713 // Evict a random orphan:
714 uint256 randomhash
= GetRandHash();
715 std::map
<uint256
, COrphanTx
>::iterator it
= mapOrphanTransactions
.lower_bound(randomhash
);
716 if (it
== mapOrphanTransactions
.end())
717 it
= mapOrphanTransactions
.begin();
718 EraseOrphanTx(it
->first
);
725 void Misbehaving(NodeId pnode
, int howmuch
)
730 CNodeState
*state
= State(pnode
);
731 if (state
== nullptr)
734 state
->nMisbehavior
+= howmuch
;
735 int banscore
= gArgs
.GetArg("-banscore", DEFAULT_BANSCORE_THRESHOLD
);
736 if (state
->nMisbehavior
>= banscore
&& state
->nMisbehavior
- howmuch
< banscore
)
738 LogPrintf("%s: %s peer=%d (%d -> %d) BAN THRESHOLD EXCEEDED\n", __func__
, state
->name
, pnode
, state
->nMisbehavior
-howmuch
, state
->nMisbehavior
);
739 state
->fShouldBan
= true;
741 LogPrintf("%s: %s peer=%d (%d -> %d)\n", __func__
, state
->name
, pnode
, state
->nMisbehavior
-howmuch
, state
->nMisbehavior
);
751 //////////////////////////////////////////////////////////////////////////////
753 // blockchain -> download logic notification
756 // To prevent fingerprinting attacks, only send blocks/headers outside of the
757 // active chain if they are no more than a month older (both in time, and in
758 // best equivalent proof of work) than the best header chain we know about.
759 static bool StaleBlockRequestAllowed(const CBlockIndex
* pindex
, const Consensus::Params
& consensusParams
)
761 AssertLockHeld(cs_main
);
762 return (pindexBestHeader
!= nullptr) &&
763 (pindexBestHeader
->GetBlockTime() - pindex
->GetBlockTime() < STALE_RELAY_AGE_LIMIT
) &&
764 (GetBlockProofEquivalentTime(*pindexBestHeader
, *pindex
, *pindexBestHeader
, consensusParams
) < STALE_RELAY_AGE_LIMIT
);
767 PeerLogicValidation::PeerLogicValidation(CConnman
* connmanIn
) : connman(connmanIn
) {
768 // Initialize global variables that cannot be constructed at startup.
769 recentRejects
.reset(new CRollingBloomFilter(120000, 0.000001));
772 void PeerLogicValidation::BlockConnected(const std::shared_ptr
<const CBlock
>& pblock
, const CBlockIndex
* pindex
, const std::vector
<CTransactionRef
>& vtxConflicted
) {
775 std::vector
<uint256
> vOrphanErase
;
777 for (const CTransactionRef
& ptx
: pblock
->vtx
) {
778 const CTransaction
& tx
= *ptx
;
780 // Which orphan pool entries must we evict?
781 for (const auto& txin
: tx
.vin
) {
782 auto itByPrev
= mapOrphanTransactionsByPrev
.find(txin
.prevout
);
783 if (itByPrev
== mapOrphanTransactionsByPrev
.end()) continue;
784 for (auto mi
= itByPrev
->second
.begin(); mi
!= itByPrev
->second
.end(); ++mi
) {
785 const CTransaction
& orphanTx
= *(*mi
)->second
.tx
;
786 const uint256
& orphanHash
= orphanTx
.GetHash();
787 vOrphanErase
.push_back(orphanHash
);
792 // Erase orphan transactions include or precluded by this block
793 if (vOrphanErase
.size()) {
795 for (uint256
&orphanHash
: vOrphanErase
) {
796 nErased
+= EraseOrphanTx(orphanHash
);
798 LogPrint(BCLog::MEMPOOL
, "Erased %d orphan tx included or conflicted by block\n", nErased
);
802 // All of the following cache a recent block, and are protected by cs_most_recent_block
803 static CCriticalSection cs_most_recent_block
;
804 static std::shared_ptr
<const CBlock
> most_recent_block
;
805 static std::shared_ptr
<const CBlockHeaderAndShortTxIDs
> most_recent_compact_block
;
806 static uint256 most_recent_block_hash
;
807 static bool fWitnessesPresentInMostRecentCompactBlock
;
809 void PeerLogicValidation::NewPoWValidBlock(const CBlockIndex
*pindex
, const std::shared_ptr
<const CBlock
>& pblock
) {
810 std::shared_ptr
<const CBlockHeaderAndShortTxIDs
> pcmpctblock
= std::make_shared
<const CBlockHeaderAndShortTxIDs
> (*pblock
, true);
811 const CNetMsgMaker
msgMaker(PROTOCOL_VERSION
);
815 static int nHighestFastAnnounce
= 0;
816 if (pindex
->nHeight
<= nHighestFastAnnounce
)
818 nHighestFastAnnounce
= pindex
->nHeight
;
820 bool fWitnessEnabled
= IsWitnessEnabled(pindex
->pprev
, Params().GetConsensus());
821 uint256
hashBlock(pblock
->GetHash());
824 LOCK(cs_most_recent_block
);
825 most_recent_block_hash
= hashBlock
;
826 most_recent_block
= pblock
;
827 most_recent_compact_block
= pcmpctblock
;
828 fWitnessesPresentInMostRecentCompactBlock
= fWitnessEnabled
;
831 connman
->ForEachNode([this, &pcmpctblock
, pindex
, &msgMaker
, fWitnessEnabled
, &hashBlock
](CNode
* pnode
) {
832 // TODO: Avoid the repeated-serialization here
833 if (pnode
->nVersion
< INVALID_CB_NO_BAN_VERSION
|| pnode
->fDisconnect
)
835 ProcessBlockAvailability(pnode
->GetId());
836 CNodeState
&state
= *State(pnode
->GetId());
837 // If the peer has, or we announced to them the previous block already,
838 // but we don't think they have this one, go ahead and announce it
839 if (state
.fPreferHeaderAndIDs
&& (!fWitnessEnabled
|| state
.fWantsCmpctWitness
) &&
840 !PeerHasHeader(&state
, pindex
) && PeerHasHeader(&state
, pindex
->pprev
)) {
842 LogPrint(BCLog::NET
, "%s sending header-and-ids %s to peer=%d\n", "PeerLogicValidation::NewPoWValidBlock",
843 hashBlock
.ToString(), pnode
->GetId());
844 connman
->PushMessage(pnode
, msgMaker
.Make(NetMsgType::CMPCTBLOCK
, *pcmpctblock
));
845 state
.pindexBestHeaderSent
= pindex
;
850 void PeerLogicValidation::UpdatedBlockTip(const CBlockIndex
*pindexNew
, const CBlockIndex
*pindexFork
, bool fInitialDownload
) {
851 const int nNewHeight
= pindexNew
->nHeight
;
852 connman
->SetBestHeight(nNewHeight
);
854 if (!fInitialDownload
) {
855 // Find the hashes of all blocks that weren't previously in the best chain.
856 std::vector
<uint256
> vHashes
;
857 const CBlockIndex
*pindexToAnnounce
= pindexNew
;
858 while (pindexToAnnounce
!= pindexFork
) {
859 vHashes
.push_back(pindexToAnnounce
->GetBlockHash());
860 pindexToAnnounce
= pindexToAnnounce
->pprev
;
861 if (vHashes
.size() == MAX_BLOCKS_TO_ANNOUNCE
) {
862 // Limit announcements in case of a huge reorganization.
863 // Rely on the peer's synchronization mechanism in that case.
867 // Relay inventory, but don't relay old inventory during initial block download.
868 connman
->ForEachNode([nNewHeight
, &vHashes
](CNode
* pnode
) {
869 if (nNewHeight
> (pnode
->nStartingHeight
!= -1 ? pnode
->nStartingHeight
- 2000 : 0)) {
870 for (const uint256
& hash
: reverse_iterate(vHashes
)) {
871 pnode
->PushBlockHash(hash
);
875 connman
->WakeMessageHandler();
878 nTimeBestReceived
= GetTime();
881 void PeerLogicValidation::BlockChecked(const CBlock
& block
, const CValidationState
& state
) {
884 const uint256
hash(block
.GetHash());
885 std::map
<uint256
, std::pair
<NodeId
, bool>>::iterator it
= mapBlockSource
.find(hash
);
888 if (state
.IsInvalid(nDoS
)) {
889 // Don't send reject message with code 0 or an internal reject code.
890 if (it
!= mapBlockSource
.end() && State(it
->second
.first
) && state
.GetRejectCode() > 0 && state
.GetRejectCode() < REJECT_INTERNAL
) {
891 CBlockReject reject
= {(unsigned char)state
.GetRejectCode(), state
.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH
), hash
};
892 State(it
->second
.first
)->rejects
.push_back(reject
);
893 if (nDoS
> 0 && it
->second
.second
)
894 Misbehaving(it
->second
.first
, nDoS
);
898 // 1. The block is valid
899 // 2. We're not in initial block download
900 // 3. This is currently the best block we're aware of. We haven't updated
901 // the tip yet so we have no way to check this directly here. Instead we
902 // just check that there are currently no other blocks in flight.
903 else if (state
.IsValid() &&
904 !IsInitialBlockDownload() &&
905 mapBlocksInFlight
.count(hash
) == mapBlocksInFlight
.size()) {
906 if (it
!= mapBlockSource
.end()) {
907 MaybeSetPeerAsAnnouncingHeaderAndIDs(it
->second
.first
, connman
);
910 if (it
!= mapBlockSource
.end())
911 mapBlockSource
.erase(it
);
914 //////////////////////////////////////////////////////////////////////////////
920 bool static AlreadyHave(const CInv
& inv
) EXCLUSIVE_LOCKS_REQUIRED(cs_main
)
927 assert(recentRejects
);
928 if (chainActive
.Tip()->GetBlockHash() != hashRecentRejectsChainTip
)
930 // If the chain tip has changed previously rejected transactions
931 // might be now valid, e.g. due to a nLockTime'd tx becoming valid,
932 // or a double-spend. Reset the rejects filter and give those
933 // txs a second chance.
934 hashRecentRejectsChainTip
= chainActive
.Tip()->GetBlockHash();
935 recentRejects
->reset();
938 return recentRejects
->contains(inv
.hash
) ||
939 mempool
.exists(inv
.hash
) ||
940 mapOrphanTransactions
.count(inv
.hash
) ||
941 pcoinsTip
->HaveCoinInCache(COutPoint(inv
.hash
, 0)) || // Best effort: only try output 0 and 1
942 pcoinsTip
->HaveCoinInCache(COutPoint(inv
.hash
, 1));
945 case MSG_WITNESS_BLOCK
:
946 return mapBlockIndex
.count(inv
.hash
);
948 // Don't know what it is, just say we already got one
952 static void RelayTransaction(const CTransaction
& tx
, CConnman
* connman
)
954 CInv
inv(MSG_TX
, tx
.GetHash());
955 connman
->ForEachNode([&inv
](CNode
* pnode
)
957 pnode
->PushInventory(inv
);
961 static void RelayAddress(const CAddress
& addr
, bool fReachable
, CConnman
* connman
)
963 unsigned int nRelayNodes
= fReachable
? 2 : 1; // limited relaying of addresses outside our network(s)
965 // Relay to a limited number of other nodes
966 // Use deterministic randomness to send to the same nodes for 24 hours
967 // at a time so the addrKnowns of the chosen nodes prevent repeats
968 uint64_t hashAddr
= addr
.GetHash();
969 const CSipHasher hasher
= connman
->GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY
).Write(hashAddr
<< 32).Write((GetTime() + hashAddr
) / (24*60*60));
970 FastRandomContext insecure_rand
;
972 std::array
<std::pair
<uint64_t, CNode
*>,2> best
{{{0, nullptr}, {0, nullptr}}};
973 assert(nRelayNodes
<= best
.size());
975 auto sortfunc
= [&best
, &hasher
, nRelayNodes
](CNode
* pnode
) {
976 if (pnode
->nVersion
>= CADDR_TIME_VERSION
) {
977 uint64_t hashKey
= CSipHasher(hasher
).Write(pnode
->GetId()).Finalize();
978 for (unsigned int i
= 0; i
< nRelayNodes
; i
++) {
979 if (hashKey
> best
[i
].first
) {
980 std::copy(best
.begin() + i
, best
.begin() + nRelayNodes
- 1, best
.begin() + i
+ 1);
981 best
[i
] = std::make_pair(hashKey
, pnode
);
988 auto pushfunc
= [&addr
, &best
, nRelayNodes
, &insecure_rand
] {
989 for (unsigned int i
= 0; i
< nRelayNodes
&& best
[i
].first
!= 0; i
++) {
990 best
[i
].second
->PushAddress(addr
, insecure_rand
);
994 connman
->ForEachNodeThen(std::move(sortfunc
), std::move(pushfunc
));
997 void static ProcessGetData(CNode
* pfrom
, const Consensus::Params
& consensusParams
, CConnman
* connman
, const std::atomic
<bool>& interruptMsgProc
)
999 std::deque
<CInv
>::iterator it
= pfrom
->vRecvGetData
.begin();
1000 std::vector
<CInv
> vNotFound
;
1001 const CNetMsgMaker
msgMaker(pfrom
->GetSendVersion());
1004 while (it
!= pfrom
->vRecvGetData
.end()) {
1005 // Don't bother if send buffer is too full to respond anyway
1006 if (pfrom
->fPauseSend
)
1009 const CInv
&inv
= *it
;
1011 if (interruptMsgProc
)
1016 if (inv
.type
== MSG_BLOCK
|| inv
.type
== MSG_FILTERED_BLOCK
|| inv
.type
== MSG_CMPCT_BLOCK
|| inv
.type
== MSG_WITNESS_BLOCK
)
1019 BlockMap::iterator mi
= mapBlockIndex
.find(inv
.hash
);
1020 std::shared_ptr
<const CBlock
> a_recent_block
;
1021 std::shared_ptr
<const CBlockHeaderAndShortTxIDs
> a_recent_compact_block
;
1022 bool fWitnessesPresentInARecentCompactBlock
;
1024 LOCK(cs_most_recent_block
);
1025 a_recent_block
= most_recent_block
;
1026 a_recent_compact_block
= most_recent_compact_block
;
1027 fWitnessesPresentInARecentCompactBlock
= fWitnessesPresentInMostRecentCompactBlock
;
1029 if (mi
!= mapBlockIndex
.end())
1031 if (mi
->second
->nChainTx
&& !mi
->second
->IsValid(BLOCK_VALID_SCRIPTS
) &&
1032 mi
->second
->IsValid(BLOCK_VALID_TREE
)) {
1033 // If we have the block and all of its parents, but have not yet validated it,
1034 // we might be in the middle of connecting it (ie in the unlock of cs_main
1035 // before ActivateBestChain but after AcceptBlock).
1036 // In this case, we need to run ActivateBestChain prior to checking the relay
1037 // conditions below.
1038 CValidationState dummy
;
1039 ActivateBestChain(dummy
, Params(), a_recent_block
);
1041 if (chainActive
.Contains(mi
->second
)) {
1044 send
= mi
->second
->IsValid(BLOCK_VALID_SCRIPTS
) &&
1045 StaleBlockRequestAllowed(mi
->second
, consensusParams
);
1047 LogPrintf("%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__
, pfrom
->GetId());
1051 // disconnect node in case we have reached the outbound limit for serving historical blocks
1052 // never disconnect whitelisted nodes
1053 if (send
&& connman
->OutboundTargetReached(true) && ( ((pindexBestHeader
!= nullptr) && (pindexBestHeader
->GetBlockTime() - mi
->second
->GetBlockTime() > HISTORICAL_BLOCK_AGE
)) || inv
.type
== MSG_FILTERED_BLOCK
) && !pfrom
->fWhitelisted
)
1055 LogPrint(BCLog::NET
, "historical block serving limit reached, disconnect peer=%d\n", pfrom
->GetId());
1058 pfrom
->fDisconnect
= true;
1061 // Pruned nodes may have deleted the block, so check whether
1062 // it's available before trying to send.
1063 if (send
&& (mi
->second
->nStatus
& BLOCK_HAVE_DATA
))
1065 std::shared_ptr
<const CBlock
> pblock
;
1066 if (a_recent_block
&& a_recent_block
->GetHash() == (*mi
).second
->GetBlockHash()) {
1067 pblock
= a_recent_block
;
1069 // Send block from disk
1070 std::shared_ptr
<CBlock
> pblockRead
= std::make_shared
<CBlock
>();
1071 if (!ReadBlockFromDisk(*pblockRead
, (*mi
).second
, consensusParams
))
1072 assert(!"cannot load block from disk");
1073 pblock
= pblockRead
;
1075 if (inv
.type
== MSG_BLOCK
)
1076 connman
->PushMessage(pfrom
, msgMaker
.Make(SERIALIZE_TRANSACTION_NO_WITNESS
, NetMsgType::BLOCK
, *pblock
));
1077 else if (inv
.type
== MSG_WITNESS_BLOCK
)
1078 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::BLOCK
, *pblock
));
1079 else if (inv
.type
== MSG_FILTERED_BLOCK
)
1081 bool sendMerkleBlock
= false;
1082 CMerkleBlock merkleBlock
;
1084 LOCK(pfrom
->cs_filter
);
1085 if (pfrom
->pfilter
) {
1086 sendMerkleBlock
= true;
1087 merkleBlock
= CMerkleBlock(*pblock
, *pfrom
->pfilter
);
1090 if (sendMerkleBlock
) {
1091 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::MERKLEBLOCK
, merkleBlock
));
1092 // CMerkleBlock just contains hashes, so also push any transactions in the block the client did not see
1093 // This avoids hurting performance by pointlessly requiring a round-trip
1094 // Note that there is currently no way for a node to request any single transactions we didn't send here -
1095 // they must either disconnect and retry or request the full block.
1096 // Thus, the protocol spec specified allows for us to provide duplicate txn here,
1097 // however we MUST always provide at least what the remote peer needs
1098 typedef std::pair
<unsigned int, uint256
> PairType
;
1099 for (PairType
& pair
: merkleBlock
.vMatchedTxn
)
1100 connman
->PushMessage(pfrom
, msgMaker
.Make(SERIALIZE_TRANSACTION_NO_WITNESS
, NetMsgType::TX
, *pblock
->vtx
[pair
.first
]));
1105 else if (inv
.type
== MSG_CMPCT_BLOCK
)
1107 // If a peer is asking for old blocks, we're almost guaranteed
1108 // they won't have a useful mempool to match against a compact block,
1109 // and we don't feel like constructing the object for them, so
1110 // instead we respond with the full, non-compact block.
1111 bool fPeerWantsWitness
= State(pfrom
->GetId())->fWantsCmpctWitness
;
1112 int nSendFlags
= fPeerWantsWitness
? 0 : SERIALIZE_TRANSACTION_NO_WITNESS
;
1113 if (CanDirectFetch(consensusParams
) && mi
->second
->nHeight
>= chainActive
.Height() - MAX_CMPCTBLOCK_DEPTH
) {
1114 if ((fPeerWantsWitness
|| !fWitnessesPresentInARecentCompactBlock
) && a_recent_compact_block
&& a_recent_compact_block
->header
.GetHash() == mi
->second
->GetBlockHash()) {
1115 connman
->PushMessage(pfrom
, msgMaker
.Make(nSendFlags
, NetMsgType::CMPCTBLOCK
, *a_recent_compact_block
));
1117 CBlockHeaderAndShortTxIDs
cmpctblock(*pblock
, fPeerWantsWitness
);
1118 connman
->PushMessage(pfrom
, msgMaker
.Make(nSendFlags
, NetMsgType::CMPCTBLOCK
, cmpctblock
));
1121 connman
->PushMessage(pfrom
, msgMaker
.Make(nSendFlags
, NetMsgType::BLOCK
, *pblock
));
1125 // Trigger the peer node to send a getblocks request for the next batch of inventory
1126 if (inv
.hash
== pfrom
->hashContinue
)
1128 // Bypass PushInventory, this must send even if redundant,
1129 // and we want it right after the last block so they don't
1130 // wait for other stuff first.
1131 std::vector
<CInv
> vInv
;
1132 vInv
.push_back(CInv(MSG_BLOCK
, chainActive
.Tip()->GetBlockHash()));
1133 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::INV
, vInv
));
1134 pfrom
->hashContinue
.SetNull();
1138 else if (inv
.type
== MSG_TX
|| inv
.type
== MSG_WITNESS_TX
)
1140 // Send stream from relay memory
1142 auto mi
= mapRelay
.find(inv
.hash
);
1143 int nSendFlags
= (inv
.type
== MSG_TX
? SERIALIZE_TRANSACTION_NO_WITNESS
: 0);
1144 if (mi
!= mapRelay
.end()) {
1145 connman
->PushMessage(pfrom
, msgMaker
.Make(nSendFlags
, NetMsgType::TX
, *mi
->second
));
1147 } else if (pfrom
->timeLastMempoolReq
) {
1148 auto txinfo
= mempool
.info(inv
.hash
);
1149 // To protect privacy, do not answer getdata using the mempool when
1150 // that TX couldn't have been INVed in reply to a MEMPOOL request.
1151 if (txinfo
.tx
&& txinfo
.nTime
<= pfrom
->timeLastMempoolReq
) {
1152 connman
->PushMessage(pfrom
, msgMaker
.Make(nSendFlags
, NetMsgType::TX
, *txinfo
.tx
));
1157 vNotFound
.push_back(inv
);
1161 // Track requests for our stuff.
1162 GetMainSignals().Inventory(inv
.hash
);
1164 if (inv
.type
== MSG_BLOCK
|| inv
.type
== MSG_FILTERED_BLOCK
|| inv
.type
== MSG_CMPCT_BLOCK
|| inv
.type
== MSG_WITNESS_BLOCK
)
1169 pfrom
->vRecvGetData
.erase(pfrom
->vRecvGetData
.begin(), it
);
1171 if (!vNotFound
.empty()) {
1172 // Let the peer know that we didn't find what it asked for, so it doesn't
1173 // have to wait around forever. Currently only SPV clients actually care
1174 // about this message: it's needed when they are recursively walking the
1175 // dependencies of relevant unconfirmed transactions. SPV clients want to
1176 // do that because they want to know about (and store and rebroadcast and
1177 // risk analyze) the dependencies of transactions relevant to them, without
1178 // having to download the entire memory pool.
1179 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::NOTFOUND
, vNotFound
));
1183 uint32_t GetFetchFlags(CNode
* pfrom
) {
1184 uint32_t nFetchFlags
= 0;
1185 if ((pfrom
->GetLocalServices() & NODE_WITNESS
) && State(pfrom
->GetId())->fHaveWitness
) {
1186 nFetchFlags
|= MSG_WITNESS_FLAG
;
1191 inline void static SendBlockTransactions(const CBlock
& block
, const BlockTransactionsRequest
& req
, CNode
* pfrom
, CConnman
* connman
) {
1192 BlockTransactions
resp(req
);
1193 for (size_t i
= 0; i
< req
.indexes
.size(); i
++) {
1194 if (req
.indexes
[i
] >= block
.vtx
.size()) {
1196 Misbehaving(pfrom
->GetId(), 100);
1197 LogPrintf("Peer %d sent us a getblocktxn with out-of-bounds tx indices", pfrom
->GetId());
1200 resp
.txn
[i
] = block
.vtx
[req
.indexes
[i
]];
1203 const CNetMsgMaker
msgMaker(pfrom
->GetSendVersion());
1204 int nSendFlags
= State(pfrom
->GetId())->fWantsCmpctWitness
? 0 : SERIALIZE_TRANSACTION_NO_WITNESS
;
1205 connman
->PushMessage(pfrom
, msgMaker
.Make(nSendFlags
, NetMsgType::BLOCKTXN
, resp
));
1208 bool static ProcessHeadersMessage(CNode
*pfrom
, CConnman
*connman
, const std::vector
<CBlockHeader
>& headers
, const CChainParams
& chainparams
, bool punish_duplicate_invalid
)
1210 const CNetMsgMaker
msgMaker(pfrom
->GetSendVersion());
1211 size_t nCount
= headers
.size();
1214 // Nothing interesting. Stop asking this peers for more headers.
1218 const CBlockIndex
*pindexLast
= nullptr;
1221 CNodeState
*nodestate
= State(pfrom
->GetId());
1223 // If this looks like it could be a block announcement (nCount <
1224 // MAX_BLOCKS_TO_ANNOUNCE), use special logic for handling headers that
1226 // - Send a getheaders message in response to try to connect the chain.
1227 // - The peer can send up to MAX_UNCONNECTING_HEADERS in a row that
1228 // don't connect before giving DoS points
1229 // - Once a headers message is received that is valid and does connect,
1230 // nUnconnectingHeaders gets reset back to 0.
1231 if (mapBlockIndex
.find(headers
[0].hashPrevBlock
) == mapBlockIndex
.end() && nCount
< MAX_BLOCKS_TO_ANNOUNCE
) {
1232 nodestate
->nUnconnectingHeaders
++;
1233 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETHEADERS
, chainActive
.GetLocator(pindexBestHeader
), uint256()));
1234 LogPrint(BCLog::NET
, "received header %s: missing prev block %s, sending getheaders (%d) to end (peer=%d, nUnconnectingHeaders=%d)\n",
1235 headers
[0].GetHash().ToString(),
1236 headers
[0].hashPrevBlock
.ToString(),
1237 pindexBestHeader
->nHeight
,
1238 pfrom
->GetId(), nodestate
->nUnconnectingHeaders
);
1239 // Set hashLastUnknownBlock for this peer, so that if we
1240 // eventually get the headers - even from a different peer -
1241 // we can use this peer to download.
1242 UpdateBlockAvailability(pfrom
->GetId(), headers
.back().GetHash());
1244 if (nodestate
->nUnconnectingHeaders
% MAX_UNCONNECTING_HEADERS
== 0) {
1245 Misbehaving(pfrom
->GetId(), 20);
1250 uint256 hashLastBlock
;
1251 for (const CBlockHeader
& header
: headers
) {
1252 if (!hashLastBlock
.IsNull() && header
.hashPrevBlock
!= hashLastBlock
) {
1253 Misbehaving(pfrom
->GetId(), 20);
1254 return error("non-continuous headers sequence");
1256 hashLastBlock
= header
.GetHash();
1260 CValidationState state
;
1261 CBlockHeader first_invalid_header
;
1262 if (!ProcessNewBlockHeaders(headers
, state
, chainparams
, &pindexLast
, &first_invalid_header
)) {
1264 if (state
.IsInvalid(nDoS
)) {
1267 Misbehaving(pfrom
->GetId(), nDoS
);
1269 if (punish_duplicate_invalid
&& mapBlockIndex
.find(first_invalid_header
.GetHash()) != mapBlockIndex
.end()) {
1270 // Goal: don't allow outbound peers to use up our outbound
1271 // connection slots if they are on incompatible chains.
1273 // We ask the caller to set punish_invalid appropriately based
1274 // on the peer and the method of header delivery (compact
1275 // blocks are allowed to be invalid in some circumstances,
1277 // Here, we try to detect the narrow situation that we have a
1278 // valid block header (ie it was valid at the time the header
1279 // was received, and hence stored in mapBlockIndex) but know the
1280 // block is invalid, and that a peer has announced that same
1281 // block as being on its active chain.
1282 // Disconnect the peer in such a situation.
1284 // Note: if the header that is invalid was not accepted to our
1285 // mapBlockIndex at all, that may also be grounds for
1286 // disconnecting the peer, as the chain they are on is likely
1287 // to be incompatible. However, there is a circumstance where
1288 // that does not hold: if the header's timestamp is more than
1289 // 2 hours ahead of our current time. In that case, the header
1290 // may become valid in the future, and we don't want to
1291 // disconnect a peer merely for serving us one too-far-ahead
1292 // block header, to prevent an attacker from splitting the
1293 // network by mining a block right at the 2 hour boundary.
1295 // TODO: update the DoS logic (or, rather, rewrite the
1296 // DoS-interface between validation and net_processing) so that
1297 // the interface is cleaner, and so that we disconnect on all the
1298 // reasons that a peer's headers chain is incompatible
1299 // with ours (eg block->nVersion softforks, MTP violations,
1300 // etc), and not just the duplicate-invalid case.
1301 pfrom
->fDisconnect
= true;
1303 return error("invalid header received");
1309 CNodeState
*nodestate
= State(pfrom
->GetId());
1310 if (nodestate
->nUnconnectingHeaders
> 0) {
1311 LogPrint(BCLog::NET
, "peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n", pfrom
->GetId(), nodestate
->nUnconnectingHeaders
);
1313 nodestate
->nUnconnectingHeaders
= 0;
1316 UpdateBlockAvailability(pfrom
->GetId(), pindexLast
->GetBlockHash());
1318 // From here, pindexBestKnownBlock should be guaranteed to be non-null,
1319 // because it is set in UpdateBlockAvailability. Some nullptr checks
1320 // are still present, however, as belt-and-suspenders.
1322 if (nCount
== MAX_HEADERS_RESULTS
) {
1323 // Headers message had its maximum size; the peer may have more headers.
1324 // TODO: optimize: if pindexLast is an ancestor of chainActive.Tip or pindexBestHeader, continue
1325 // from there instead.
1326 LogPrint(BCLog::NET
, "more getheaders (%d) to end to peer=%d (startheight:%d)\n", pindexLast
->nHeight
, pfrom
->GetId(), pfrom
->nStartingHeight
);
1327 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETHEADERS
, chainActive
.GetLocator(pindexLast
), uint256()));
1330 bool fCanDirectFetch
= CanDirectFetch(chainparams
.GetConsensus());
1331 // If this set of headers is valid and ends in a block with at least as
1332 // much work as our tip, download as much as possible.
1333 if (fCanDirectFetch
&& pindexLast
->IsValid(BLOCK_VALID_TREE
) && chainActive
.Tip()->nChainWork
<= pindexLast
->nChainWork
) {
1334 std::vector
<const CBlockIndex
*> vToFetch
;
1335 const CBlockIndex
*pindexWalk
= pindexLast
;
1336 // Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
1337 while (pindexWalk
&& !chainActive
.Contains(pindexWalk
) && vToFetch
.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER
) {
1338 if (!(pindexWalk
->nStatus
& BLOCK_HAVE_DATA
) &&
1339 !mapBlocksInFlight
.count(pindexWalk
->GetBlockHash()) &&
1340 (!IsWitnessEnabled(pindexWalk
->pprev
, chainparams
.GetConsensus()) || State(pfrom
->GetId())->fHaveWitness
)) {
1341 // We don't have this block, and it's not yet in flight.
1342 vToFetch
.push_back(pindexWalk
);
1344 pindexWalk
= pindexWalk
->pprev
;
1346 // If pindexWalk still isn't on our main chain, we're looking at a
1347 // very large reorg at a time we think we're close to caught up to
1348 // the main chain -- this shouldn't really happen. Bail out on the
1349 // direct fetch and rely on parallel download instead.
1350 if (!chainActive
.Contains(pindexWalk
)) {
1351 LogPrint(BCLog::NET
, "Large reorg, won't direct fetch to %s (%d)\n",
1352 pindexLast
->GetBlockHash().ToString(),
1353 pindexLast
->nHeight
);
1355 std::vector
<CInv
> vGetData
;
1356 // Download as much as possible, from earliest to latest.
1357 for (const CBlockIndex
*pindex
: reverse_iterate(vToFetch
)) {
1358 if (nodestate
->nBlocksInFlight
>= MAX_BLOCKS_IN_TRANSIT_PER_PEER
) {
1359 // Can't download any more from this peer
1362 uint32_t nFetchFlags
= GetFetchFlags(pfrom
);
1363 vGetData
.push_back(CInv(MSG_BLOCK
| nFetchFlags
, pindex
->GetBlockHash()));
1364 MarkBlockAsInFlight(pfrom
->GetId(), pindex
->GetBlockHash(), pindex
);
1365 LogPrint(BCLog::NET
, "Requesting block %s from peer=%d\n",
1366 pindex
->GetBlockHash().ToString(), pfrom
->GetId());
1368 if (vGetData
.size() > 1) {
1369 LogPrint(BCLog::NET
, "Downloading blocks toward %s (%d) via headers direct fetch\n",
1370 pindexLast
->GetBlockHash().ToString(), pindexLast
->nHeight
);
1372 if (vGetData
.size() > 0) {
1373 if (nodestate
->fSupportsDesiredCmpctVersion
&& vGetData
.size() == 1 && mapBlocksInFlight
.size() == 1 && pindexLast
->pprev
->IsValid(BLOCK_VALID_CHAIN
)) {
1374 // In any case, we want to download using a compact block, not a regular one
1375 vGetData
[0] = CInv(MSG_CMPCT_BLOCK
, vGetData
[0].hash
);
1377 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETDATA
, vGetData
));
1381 // If we're in IBD, we want outbound peers that will serve us a useful
1382 // chain. Disconnect peers that are on chains with insufficient work.
1383 if (IsInitialBlockDownload() && nCount
!= MAX_HEADERS_RESULTS
) {
1384 // When nCount < MAX_HEADERS_RESULTS, we know we have no more
1385 // headers to fetch from this peer.
1386 if (nodestate
->pindexBestKnownBlock
&& nodestate
->pindexBestKnownBlock
->nChainWork
< nMinimumChainWork
) {
1387 // This peer has too little work on their headers chain to help
1388 // us sync -- disconnect if using an outbound slot (unless
1389 // whitelisted or addnode).
1390 // Note: We compare their tip to nMinimumChainWork (rather than
1391 // chainActive.Tip()) because we won't start block download
1392 // until we have a headers chain that has at least
1393 // nMinimumChainWork, even if a peer has a chain past our tip,
1394 // as an anti-DoS measure.
1395 if (IsOutboundDisconnectionCandidate(pfrom
)) {
1396 LogPrintf("Disconnecting outbound peer %d -- headers chain has insufficient work\n", pfrom
->GetId());
1397 pfrom
->fDisconnect
= true;
1402 if (!pfrom
->fDisconnect
&& IsOutboundDisconnectionCandidate(pfrom
) && nodestate
->pindexBestKnownBlock
!= nullptr) {
1403 // If this is an outbound peer, check to see if we should protect
1404 // it from the bad/lagging chain logic.
1405 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
) {
1406 nodestate
->m_chain_sync
.m_protect
= true;
1407 ++g_outbound_peers_with_protect_from_disconnect
;
1415 bool static ProcessMessage(CNode
* pfrom
, const std::string
& strCommand
, CDataStream
& vRecv
, int64_t nTimeReceived
, const CChainParams
& chainparams
, CConnman
* connman
, const std::atomic
<bool>& interruptMsgProc
)
1417 LogPrint(BCLog::NET
, "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand
), vRecv
.size(), pfrom
->GetId());
1418 if (gArgs
.IsArgSet("-dropmessagestest") && GetRand(gArgs
.GetArg("-dropmessagestest", 0)) == 0)
1420 LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n");
1425 if (!(pfrom
->GetLocalServices() & NODE_BLOOM
) &&
1426 (strCommand
== NetMsgType::FILTERLOAD
||
1427 strCommand
== NetMsgType::FILTERADD
))
1429 if (pfrom
->nVersion
>= NO_BLOOM_VERSION
) {
1431 Misbehaving(pfrom
->GetId(), 100);
1434 pfrom
->fDisconnect
= true;
1439 if (strCommand
== NetMsgType::REJECT
)
1441 if (LogAcceptCategory(BCLog::NET
)) {
1443 std::string strMsg
; unsigned char ccode
; std::string strReason
;
1444 vRecv
>> LIMITED_STRING(strMsg
, CMessageHeader::COMMAND_SIZE
) >> ccode
>> LIMITED_STRING(strReason
, MAX_REJECT_MESSAGE_LENGTH
);
1446 std::ostringstream ss
;
1447 ss
<< strMsg
<< " code " << itostr(ccode
) << ": " << strReason
;
1449 if (strMsg
== NetMsgType::BLOCK
|| strMsg
== NetMsgType::TX
)
1453 ss
<< ": hash " << hash
.ToString();
1455 LogPrint(BCLog::NET
, "Reject %s\n", SanitizeString(ss
.str()));
1456 } catch (const std::ios_base::failure
&) {
1457 // Avoid feedback loops by preventing reject messages from triggering a new reject message.
1458 LogPrint(BCLog::NET
, "Unparseable reject message received\n");
1463 else if (strCommand
== NetMsgType::VERSION
)
1465 // Each connection can only send one version message
1466 if (pfrom
->nVersion
!= 0)
1468 connman
->PushMessage(pfrom
, CNetMsgMaker(INIT_PROTO_VERSION
).Make(NetMsgType::REJECT
, strCommand
, REJECT_DUPLICATE
, std::string("Duplicate version message")));
1470 Misbehaving(pfrom
->GetId(), 1);
1477 uint64_t nNonce
= 1;
1478 uint64_t nServiceInt
;
1479 ServiceFlags nServices
;
1482 std::string strSubVer
;
1483 std::string cleanSubVer
;
1484 int nStartingHeight
= -1;
1487 vRecv
>> nVersion
>> nServiceInt
>> nTime
>> addrMe
;
1488 nSendVersion
= std::min(nVersion
, PROTOCOL_VERSION
);
1489 nServices
= ServiceFlags(nServiceInt
);
1490 if (!pfrom
->fInbound
)
1492 connman
->SetServices(pfrom
->addr
, nServices
);
1494 if (!pfrom
->fInbound
&& !pfrom
->fFeeler
&& !pfrom
->m_manual_connection
&& !HasAllDesirableServiceFlags(nServices
))
1496 LogPrint(BCLog::NET
, "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom
->GetId(), nServices
, GetDesirableServiceFlags(nServices
));
1497 connman
->PushMessage(pfrom
, CNetMsgMaker(INIT_PROTO_VERSION
).Make(NetMsgType::REJECT
, strCommand
, REJECT_NONSTANDARD
,
1498 strprintf("Expected to offer services %08x", GetDesirableServiceFlags(nServices
))));
1499 pfrom
->fDisconnect
= true;
1503 if (nServices
& ((1 << 7) | (1 << 5))) {
1504 if (GetTime() < 1533096000) {
1505 // Immediately disconnect peers that use service bits 6 or 8 until August 1st, 2018
1506 // These bits have been used as a flag to indicate that a node is running incompatible
1507 // consensus rules instead of changing the network magic, so we're stuck disconnecting
1508 // based on these service bits, at least for a while.
1509 pfrom
->fDisconnect
= true;
1514 if (nVersion
< MIN_PEER_PROTO_VERSION
)
1516 // disconnect from peers older than this proto version
1517 LogPrintf("peer=%d using obsolete version %i; disconnecting\n", pfrom
->GetId(), nVersion
);
1518 connman
->PushMessage(pfrom
, CNetMsgMaker(INIT_PROTO_VERSION
).Make(NetMsgType::REJECT
, strCommand
, REJECT_OBSOLETE
,
1519 strprintf("Version must be %d or greater", MIN_PEER_PROTO_VERSION
)));
1520 pfrom
->fDisconnect
= true;
1524 if (nVersion
== 10300)
1527 vRecv
>> addrFrom
>> nNonce
;
1528 if (!vRecv
.empty()) {
1529 vRecv
>> LIMITED_STRING(strSubVer
, MAX_SUBVERSION_LENGTH
);
1530 cleanSubVer
= SanitizeString(strSubVer
);
1532 if (!vRecv
.empty()) {
1533 vRecv
>> nStartingHeight
;
1537 // Disconnect if we connected to ourself
1538 if (pfrom
->fInbound
&& !connman
->CheckIncomingNonce(nNonce
))
1540 LogPrintf("connected to self at %s, disconnecting\n", pfrom
->addr
.ToString());
1541 pfrom
->fDisconnect
= true;
1545 if (pfrom
->fInbound
&& addrMe
.IsRoutable())
1550 // Be shy and don't send version until we hear
1551 if (pfrom
->fInbound
)
1552 PushNodeVersion(pfrom
, connman
, GetAdjustedTime());
1554 connman
->PushMessage(pfrom
, CNetMsgMaker(INIT_PROTO_VERSION
).Make(NetMsgType::VERACK
));
1556 pfrom
->nServices
= nServices
;
1557 pfrom
->SetAddrLocal(addrMe
);
1559 LOCK(pfrom
->cs_SubVer
);
1560 pfrom
->strSubVer
= strSubVer
;
1561 pfrom
->cleanSubVer
= cleanSubVer
;
1563 pfrom
->nStartingHeight
= nStartingHeight
;
1564 pfrom
->fClient
= !(nServices
& NODE_NETWORK
);
1566 LOCK(pfrom
->cs_filter
);
1567 pfrom
->fRelayTxes
= fRelay
; // set to true after we get the first filter* message
1571 pfrom
->SetSendVersion(nSendVersion
);
1572 pfrom
->nVersion
= nVersion
;
1574 if((nServices
& NODE_WITNESS
))
1577 State(pfrom
->GetId())->fHaveWitness
= true;
1580 // Potentially mark this peer as a preferred download peer.
1583 UpdatePreferredDownload(pfrom
, State(pfrom
->GetId()));
1586 if (!pfrom
->fInbound
)
1588 // Advertise our address
1589 if (fListen
&& !IsInitialBlockDownload())
1591 CAddress addr
= GetLocalAddress(&pfrom
->addr
, pfrom
->GetLocalServices());
1592 FastRandomContext insecure_rand
;
1593 if (addr
.IsRoutable())
1595 LogPrint(BCLog::NET
, "ProcessMessages: advertising address %s\n", addr
.ToString());
1596 pfrom
->PushAddress(addr
, insecure_rand
);
1597 } else if (IsPeerAddrLocalGood(pfrom
)) {
1599 LogPrint(BCLog::NET
, "ProcessMessages: advertising address %s\n", addr
.ToString());
1600 pfrom
->PushAddress(addr
, insecure_rand
);
1604 // Get recent addresses
1605 if (pfrom
->fOneShot
|| pfrom
->nVersion
>= CADDR_TIME_VERSION
|| connman
->GetAddressCount() < 1000)
1607 connman
->PushMessage(pfrom
, CNetMsgMaker(nSendVersion
).Make(NetMsgType::GETADDR
));
1608 pfrom
->fGetAddr
= true;
1610 connman
->MarkAddressGood(pfrom
->addr
);
1613 std::string remoteAddr
;
1615 remoteAddr
= ", peeraddr=" + pfrom
->addr
.ToString();
1617 LogPrintf("receive version message: %s: version %d, blocks=%d, us=%s, peer=%d%s\n",
1618 cleanSubVer
, pfrom
->nVersion
,
1619 pfrom
->nStartingHeight
, addrMe
.ToString(), pfrom
->GetId(),
1622 int64_t nTimeOffset
= nTime
- GetTime();
1623 pfrom
->nTimeOffset
= nTimeOffset
;
1624 AddTimeData(pfrom
->addr
, nTimeOffset
);
1626 // If the peer is old enough to have the old alert system, send it the final alert.
1627 if (pfrom
->nVersion
<= 70012) {
1628 CDataStream
finalAlert(ParseHex("60010000000000000000000000ffffff7f00000000ffffff7ffeffff7f01ffffff7f00000000ffffff7f00ffffff7f002f555247454e543a20416c657274206b657920636f6d70726f6d697365642c2075706772616465207265717569726564004630440220653febd6410f470f6bae11cad19c48413becb1ac2c17f908fd0fd53bdc3abd5202206d0e9c96fe88d4a0f01ed9dedae2b6f9e00da94cad0fecaae66ecf689bf71b50"), SER_NETWORK
, PROTOCOL_VERSION
);
1629 connman
->PushMessage(pfrom
, CNetMsgMaker(nSendVersion
).Make("alert", finalAlert
));
1632 // Feeler connections exist only to verify if address is online.
1633 if (pfrom
->fFeeler
) {
1634 assert(pfrom
->fInbound
== false);
1635 pfrom
->fDisconnect
= true;
1641 else if (pfrom
->nVersion
== 0)
1643 // Must have a version message before anything else
1645 Misbehaving(pfrom
->GetId(), 1);
1649 // At this point, the outgoing message serialization version can't change.
1650 const CNetMsgMaker
msgMaker(pfrom
->GetSendVersion());
1652 if (strCommand
== NetMsgType::VERACK
)
1654 pfrom
->SetRecvVersion(std::min(pfrom
->nVersion
.load(), PROTOCOL_VERSION
));
1656 if (!pfrom
->fInbound
) {
1657 // Mark this node as currently connected, so we update its timestamp later.
1659 State(pfrom
->GetId())->fCurrentlyConnected
= true;
1662 if (pfrom
->nVersion
>= SENDHEADERS_VERSION
) {
1663 // Tell our peer we prefer to receive headers rather than inv's
1664 // We send this to non-NODE NETWORK peers as well, because even
1665 // non-NODE NETWORK peers can announce blocks (such as pruning
1667 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::SENDHEADERS
));
1669 if (pfrom
->nVersion
>= SHORT_IDS_BLOCKS_VERSION
) {
1670 // Tell our peer we are willing to provide version 1 or 2 cmpctblocks
1671 // However, we do not request new block announcements using
1672 // cmpctblock messages.
1673 // We send this to non-NODE NETWORK peers as well, because
1674 // they may wish to request compact blocks from us
1675 bool fAnnounceUsingCMPCTBLOCK
= false;
1676 uint64_t nCMPCTBLOCKVersion
= 2;
1677 if (pfrom
->GetLocalServices() & NODE_WITNESS
)
1678 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::SENDCMPCT
, fAnnounceUsingCMPCTBLOCK
, nCMPCTBLOCKVersion
));
1679 nCMPCTBLOCKVersion
= 1;
1680 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::SENDCMPCT
, fAnnounceUsingCMPCTBLOCK
, nCMPCTBLOCKVersion
));
1682 pfrom
->fSuccessfullyConnected
= true;
1685 else if (!pfrom
->fSuccessfullyConnected
)
1687 // Must have a verack message before anything else
1689 Misbehaving(pfrom
->GetId(), 1);
1693 else if (strCommand
== NetMsgType::ADDR
)
1695 std::vector
<CAddress
> vAddr
;
1698 // Don't want addr from older versions unless seeding
1699 if (pfrom
->nVersion
< CADDR_TIME_VERSION
&& connman
->GetAddressCount() > 1000)
1701 if (vAddr
.size() > 1000)
1704 Misbehaving(pfrom
->GetId(), 20);
1705 return error("message addr size() = %u", vAddr
.size());
1708 // Store the new addresses
1709 std::vector
<CAddress
> vAddrOk
;
1710 int64_t nNow
= GetAdjustedTime();
1711 int64_t nSince
= nNow
- 10 * 60;
1712 for (CAddress
& addr
: vAddr
)
1714 if (interruptMsgProc
)
1717 // We only bother storing full nodes, though this may include
1718 // things which we would not make an outbound connection to, in
1719 // part because we may make feeler connections to them.
1720 if (!MayHaveUsefulAddressDB(addr
.nServices
))
1723 if (addr
.nTime
<= 100000000 || addr
.nTime
> nNow
+ 10 * 60)
1724 addr
.nTime
= nNow
- 5 * 24 * 60 * 60;
1725 pfrom
->AddAddressKnown(addr
);
1726 bool fReachable
= IsReachable(addr
);
1727 if (addr
.nTime
> nSince
&& !pfrom
->fGetAddr
&& vAddr
.size() <= 10 && addr
.IsRoutable())
1729 // Relay to a limited number of other nodes
1730 RelayAddress(addr
, fReachable
, connman
);
1732 // Do not store addresses outside our network
1734 vAddrOk
.push_back(addr
);
1736 connman
->AddNewAddresses(vAddrOk
, pfrom
->addr
, 2 * 60 * 60);
1737 if (vAddr
.size() < 1000)
1738 pfrom
->fGetAddr
= false;
1739 if (pfrom
->fOneShot
)
1740 pfrom
->fDisconnect
= true;
1743 else if (strCommand
== NetMsgType::SENDHEADERS
)
1746 State(pfrom
->GetId())->fPreferHeaders
= true;
1749 else if (strCommand
== NetMsgType::SENDCMPCT
)
1751 bool fAnnounceUsingCMPCTBLOCK
= false;
1752 uint64_t nCMPCTBLOCKVersion
= 0;
1753 vRecv
>> fAnnounceUsingCMPCTBLOCK
>> nCMPCTBLOCKVersion
;
1754 if (nCMPCTBLOCKVersion
== 1 || ((pfrom
->GetLocalServices() & NODE_WITNESS
) && nCMPCTBLOCKVersion
== 2)) {
1756 // fProvidesHeaderAndIDs is used to "lock in" version of compact blocks we send (fWantsCmpctWitness)
1757 if (!State(pfrom
->GetId())->fProvidesHeaderAndIDs
) {
1758 State(pfrom
->GetId())->fProvidesHeaderAndIDs
= true;
1759 State(pfrom
->GetId())->fWantsCmpctWitness
= nCMPCTBLOCKVersion
== 2;
1761 if (State(pfrom
->GetId())->fWantsCmpctWitness
== (nCMPCTBLOCKVersion
== 2)) // ignore later version announces
1762 State(pfrom
->GetId())->fPreferHeaderAndIDs
= fAnnounceUsingCMPCTBLOCK
;
1763 if (!State(pfrom
->GetId())->fSupportsDesiredCmpctVersion
) {
1764 if (pfrom
->GetLocalServices() & NODE_WITNESS
)
1765 State(pfrom
->GetId())->fSupportsDesiredCmpctVersion
= (nCMPCTBLOCKVersion
== 2);
1767 State(pfrom
->GetId())->fSupportsDesiredCmpctVersion
= (nCMPCTBLOCKVersion
== 1);
1773 else if (strCommand
== NetMsgType::INV
)
1775 std::vector
<CInv
> vInv
;
1777 if (vInv
.size() > MAX_INV_SZ
)
1780 Misbehaving(pfrom
->GetId(), 20);
1781 return error("message inv size() = %u", vInv
.size());
1784 bool fBlocksOnly
= !fRelayTxes
;
1786 // Allow whitelisted peers to send data other than blocks in blocks only mode if whitelistrelay is true
1787 if (pfrom
->fWhitelisted
&& gArgs
.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY
))
1788 fBlocksOnly
= false;
1792 uint32_t nFetchFlags
= GetFetchFlags(pfrom
);
1794 for (CInv
&inv
: vInv
)
1796 if (interruptMsgProc
)
1799 bool fAlreadyHave
= AlreadyHave(inv
);
1800 LogPrint(BCLog::NET
, "got inv: %s %s peer=%d\n", inv
.ToString(), fAlreadyHave
? "have" : "new", pfrom
->GetId());
1802 if (inv
.type
== MSG_TX
) {
1803 inv
.type
|= nFetchFlags
;
1806 if (inv
.type
== MSG_BLOCK
) {
1807 UpdateBlockAvailability(pfrom
->GetId(), inv
.hash
);
1808 if (!fAlreadyHave
&& !fImporting
&& !fReindex
&& !mapBlocksInFlight
.count(inv
.hash
)) {
1809 // We used to request the full block here, but since headers-announcements are now the
1810 // primary method of announcement on the network, and since, in the case that a node
1811 // fell back to inv we probably have a reorg which we should get the headers for first,
1812 // we now only provide a getheaders response here. When we receive the headers, we will
1813 // then ask for the blocks we need.
1814 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETHEADERS
, chainActive
.GetLocator(pindexBestHeader
), inv
.hash
));
1815 LogPrint(BCLog::NET
, "getheaders (%d) %s to peer=%d\n", pindexBestHeader
->nHeight
, inv
.hash
.ToString(), pfrom
->GetId());
1820 pfrom
->AddInventoryKnown(inv
);
1822 LogPrint(BCLog::NET
, "transaction (%s) inv sent in violation of protocol peer=%d\n", inv
.hash
.ToString(), pfrom
->GetId());
1823 } else if (!fAlreadyHave
&& !fImporting
&& !fReindex
&& !IsInitialBlockDownload()) {
1828 // Track requests for our stuff
1829 GetMainSignals().Inventory(inv
.hash
);
1834 else if (strCommand
== NetMsgType::GETDATA
)
1836 std::vector
<CInv
> vInv
;
1838 if (vInv
.size() > MAX_INV_SZ
)
1841 Misbehaving(pfrom
->GetId(), 20);
1842 return error("message getdata size() = %u", vInv
.size());
1845 LogPrint(BCLog::NET
, "received getdata (%u invsz) peer=%d\n", vInv
.size(), pfrom
->GetId());
1847 if (vInv
.size() > 0) {
1848 LogPrint(BCLog::NET
, "received getdata for: %s peer=%d\n", vInv
[0].ToString(), pfrom
->GetId());
1851 pfrom
->vRecvGetData
.insert(pfrom
->vRecvGetData
.end(), vInv
.begin(), vInv
.end());
1852 ProcessGetData(pfrom
, chainparams
.GetConsensus(), connman
, interruptMsgProc
);
1856 else if (strCommand
== NetMsgType::GETBLOCKS
)
1858 CBlockLocator locator
;
1860 vRecv
>> locator
>> hashStop
;
1862 // We might have announced the currently-being-connected tip using a
1863 // compact block, which resulted in the peer sending a getblocks
1864 // request, which we would otherwise respond to without the new block.
1865 // To avoid this situation we simply verify that we are on our best
1866 // known chain now. This is super overkill, but we handle it better
1867 // for getheaders requests, and there are no known nodes which support
1868 // compact blocks but still use getblocks to request blocks.
1870 std::shared_ptr
<const CBlock
> a_recent_block
;
1872 LOCK(cs_most_recent_block
);
1873 a_recent_block
= most_recent_block
;
1875 CValidationState dummy
;
1876 ActivateBestChain(dummy
, Params(), a_recent_block
);
1881 // Find the last block the caller has in the main chain
1882 const CBlockIndex
* pindex
= FindForkInGlobalIndex(chainActive
, locator
);
1884 // Send the rest of the chain
1886 pindex
= chainActive
.Next(pindex
);
1888 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());
1889 for (; pindex
; pindex
= chainActive
.Next(pindex
))
1891 if (pindex
->GetBlockHash() == hashStop
)
1893 LogPrint(BCLog::NET
, " getblocks stopping at %d %s\n", pindex
->nHeight
, pindex
->GetBlockHash().ToString());
1896 // If pruning, don't inv blocks unless we have on disk and are likely to still have
1897 // for some reasonable time window (1 hour) that block relay might require.
1898 const int nPrunedBlocksLikelyToHave
= MIN_BLOCKS_TO_KEEP
- 3600 / chainparams
.GetConsensus().nPowTargetSpacing
;
1899 if (fPruneMode
&& (!(pindex
->nStatus
& BLOCK_HAVE_DATA
) || pindex
->nHeight
<= chainActive
.Tip()->nHeight
- nPrunedBlocksLikelyToHave
))
1901 LogPrint(BCLog::NET
, " getblocks stopping, pruned or too old block at %d %s\n", pindex
->nHeight
, pindex
->GetBlockHash().ToString());
1904 pfrom
->PushInventory(CInv(MSG_BLOCK
, pindex
->GetBlockHash()));
1907 // When this block is requested, we'll send an inv that'll
1908 // trigger the peer to getblocks the next batch of inventory.
1909 LogPrint(BCLog::NET
, " getblocks stopping at limit %d %s\n", pindex
->nHeight
, pindex
->GetBlockHash().ToString());
1910 pfrom
->hashContinue
= pindex
->GetBlockHash();
1917 else if (strCommand
== NetMsgType::GETBLOCKTXN
)
1919 BlockTransactionsRequest req
;
1922 std::shared_ptr
<const CBlock
> recent_block
;
1924 LOCK(cs_most_recent_block
);
1925 if (most_recent_block_hash
== req
.blockhash
)
1926 recent_block
= most_recent_block
;
1927 // Unlock cs_most_recent_block to avoid cs_main lock inversion
1930 SendBlockTransactions(*recent_block
, req
, pfrom
, connman
);
1936 BlockMap::iterator it
= mapBlockIndex
.find(req
.blockhash
);
1937 if (it
== mapBlockIndex
.end() || !(it
->second
->nStatus
& BLOCK_HAVE_DATA
)) {
1938 LogPrintf("Peer %d sent us a getblocktxn for a block we don't have", pfrom
->GetId());
1942 if (it
->second
->nHeight
< chainActive
.Height() - MAX_BLOCKTXN_DEPTH
) {
1943 // If an older block is requested (should never happen in practice,
1944 // but can happen in tests) send a block response instead of a
1945 // blocktxn response. Sending a full block response instead of a
1946 // small blocktxn response is preferable in the case where a peer
1947 // might maliciously send lots of getblocktxn requests to trigger
1948 // expensive disk reads, because it will require the peer to
1949 // actually receive all the data read from disk over the network.
1950 LogPrint(BCLog::NET
, "Peer %d sent us a getblocktxn for a block > %i deep", pfrom
->GetId(), MAX_BLOCKTXN_DEPTH
);
1952 inv
.type
= State(pfrom
->GetId())->fWantsCmpctWitness
? MSG_WITNESS_BLOCK
: MSG_BLOCK
;
1953 inv
.hash
= req
.blockhash
;
1954 pfrom
->vRecvGetData
.push_back(inv
);
1955 ProcessGetData(pfrom
, chainparams
.GetConsensus(), connman
, interruptMsgProc
);
1960 bool ret
= ReadBlockFromDisk(block
, it
->second
, chainparams
.GetConsensus());
1963 SendBlockTransactions(block
, req
, pfrom
, connman
);
1967 else if (strCommand
== NetMsgType::GETHEADERS
)
1969 CBlockLocator locator
;
1971 vRecv
>> locator
>> hashStop
;
1974 if (IsInitialBlockDownload() && !pfrom
->fWhitelisted
) {
1975 LogPrint(BCLog::NET
, "Ignoring getheaders from peer=%d because node is in initial block download\n", pfrom
->GetId());
1979 CNodeState
*nodestate
= State(pfrom
->GetId());
1980 const CBlockIndex
* pindex
= nullptr;
1981 if (locator
.IsNull())
1983 // If locator is null, return the hashStop block
1984 BlockMap::iterator mi
= mapBlockIndex
.find(hashStop
);
1985 if (mi
== mapBlockIndex
.end())
1987 pindex
= (*mi
).second
;
1989 if (!chainActive
.Contains(pindex
) &&
1990 !StaleBlockRequestAllowed(pindex
, chainparams
.GetConsensus())) {
1991 LogPrintf("%s: ignoring request from peer=%i for old block header that isn't in the main chain\n", __func__
, pfrom
->GetId());
1997 // Find the last block the caller has in the main chain
1998 pindex
= FindForkInGlobalIndex(chainActive
, locator
);
2000 pindex
= chainActive
.Next(pindex
);
2003 // we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
2004 std::vector
<CBlock
> vHeaders
;
2005 int nLimit
= MAX_HEADERS_RESULTS
;
2006 LogPrint(BCLog::NET
, "getheaders %d to %s from peer=%d\n", (pindex
? pindex
->nHeight
: -1), hashStop
.IsNull() ? "end" : hashStop
.ToString(), pfrom
->GetId());
2007 for (; pindex
; pindex
= chainActive
.Next(pindex
))
2009 vHeaders
.push_back(pindex
->GetBlockHeader());
2010 if (--nLimit
<= 0 || pindex
->GetBlockHash() == hashStop
)
2013 // pindex can be nullptr either if we sent chainActive.Tip() OR
2014 // if our peer has chainActive.Tip() (and thus we are sending an empty
2015 // headers message). In both cases it's safe to update
2016 // pindexBestHeaderSent to be our tip.
2018 // It is important that we simply reset the BestHeaderSent value here,
2019 // and not max(BestHeaderSent, newHeaderSent). We might have announced
2020 // the currently-being-connected tip using a compact block, which
2021 // resulted in the peer sending a headers request, which we respond to
2022 // without the new block. By resetting the BestHeaderSent, we ensure we
2023 // will re-announce the new block via headers (or compact blocks again)
2024 // in the SendMessages logic.
2025 nodestate
->pindexBestHeaderSent
= pindex
? pindex
: chainActive
.Tip();
2026 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::HEADERS
, vHeaders
));
2030 else if (strCommand
== NetMsgType::TX
)
2032 // Stop processing the transaction early if
2033 // We are in blocks only mode and peer is either not whitelisted or whitelistrelay is off
2034 if (!fRelayTxes
&& (!pfrom
->fWhitelisted
|| !gArgs
.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY
)))
2036 LogPrint(BCLog::NET
, "transaction sent in violation of protocol peer=%d\n", pfrom
->GetId());
2040 std::deque
<COutPoint
> vWorkQueue
;
2041 std::vector
<uint256
> vEraseQueue
;
2042 CTransactionRef ptx
;
2044 const CTransaction
& tx
= *ptx
;
2046 CInv
inv(MSG_TX
, tx
.GetHash());
2047 pfrom
->AddInventoryKnown(inv
);
2051 bool fMissingInputs
= false;
2052 CValidationState state
;
2054 pfrom
->setAskFor
.erase(inv
.hash
);
2055 mapAlreadyAskedFor
.erase(inv
.hash
);
2057 std::list
<CTransactionRef
> lRemovedTxn
;
2059 if (!AlreadyHave(inv
) &&
2060 AcceptToMemoryPool(mempool
, state
, ptx
, &fMissingInputs
, &lRemovedTxn
, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
2061 mempool
.check(pcoinsTip
);
2062 RelayTransaction(tx
, connman
);
2063 for (unsigned int i
= 0; i
< tx
.vout
.size(); i
++) {
2064 vWorkQueue
.emplace_back(inv
.hash
, i
);
2067 pfrom
->nLastTXTime
= GetTime();
2069 LogPrint(BCLog::MEMPOOL
, "AcceptToMemoryPool: peer=%d: accepted %s (poolsz %u txn, %u kB)\n",
2071 tx
.GetHash().ToString(),
2072 mempool
.size(), mempool
.DynamicMemoryUsage() / 1000);
2074 // Recursively process any orphan transactions that depended on this one
2075 std::set
<NodeId
> setMisbehaving
;
2076 while (!vWorkQueue
.empty()) {
2077 auto itByPrev
= mapOrphanTransactionsByPrev
.find(vWorkQueue
.front());
2078 vWorkQueue
.pop_front();
2079 if (itByPrev
== mapOrphanTransactionsByPrev
.end())
2081 for (auto mi
= itByPrev
->second
.begin();
2082 mi
!= itByPrev
->second
.end();
2085 const CTransactionRef
& porphanTx
= (*mi
)->second
.tx
;
2086 const CTransaction
& orphanTx
= *porphanTx
;
2087 const uint256
& orphanHash
= orphanTx
.GetHash();
2088 NodeId fromPeer
= (*mi
)->second
.fromPeer
;
2089 bool fMissingInputs2
= false;
2090 // Use a dummy CValidationState so someone can't setup nodes to counter-DoS based on orphan
2091 // resolution (that is, feeding people an invalid transaction based on LegitTxX in order to get
2092 // anyone relaying LegitTxX banned)
2093 CValidationState stateDummy
;
2096 if (setMisbehaving
.count(fromPeer
))
2098 if (AcceptToMemoryPool(mempool
, stateDummy
, porphanTx
, &fMissingInputs2
, &lRemovedTxn
, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
2099 LogPrint(BCLog::MEMPOOL
, " accepted orphan tx %s\n", orphanHash
.ToString());
2100 RelayTransaction(orphanTx
, connman
);
2101 for (unsigned int i
= 0; i
< orphanTx
.vout
.size(); i
++) {
2102 vWorkQueue
.emplace_back(orphanHash
, i
);
2104 vEraseQueue
.push_back(orphanHash
);
2106 else if (!fMissingInputs2
)
2109 if (stateDummy
.IsInvalid(nDos
) && nDos
> 0)
2111 // Punish peer that gave us an invalid orphan tx
2112 Misbehaving(fromPeer
, nDos
);
2113 setMisbehaving
.insert(fromPeer
);
2114 LogPrint(BCLog::MEMPOOL
, " invalid orphan tx %s\n", orphanHash
.ToString());
2116 // Has inputs but not accepted to mempool
2117 // Probably non-standard or insufficient fee
2118 LogPrint(BCLog::MEMPOOL
, " removed orphan tx %s\n", orphanHash
.ToString());
2119 vEraseQueue
.push_back(orphanHash
);
2120 if (!orphanTx
.HasWitness() && !stateDummy
.CorruptionPossible()) {
2121 // Do not use rejection cache for witness transactions or
2122 // witness-stripped transactions, as they can have been malleated.
2123 // See https://github.com/bitcoin/bitcoin/issues/8279 for details.
2124 assert(recentRejects
);
2125 recentRejects
->insert(orphanHash
);
2128 mempool
.check(pcoinsTip
);
2132 for (uint256 hash
: vEraseQueue
)
2133 EraseOrphanTx(hash
);
2135 else if (fMissingInputs
)
2137 bool fRejectedParents
= false; // It may be the case that the orphans parents have all been rejected
2138 for (const CTxIn
& txin
: tx
.vin
) {
2139 if (recentRejects
->contains(txin
.prevout
.hash
)) {
2140 fRejectedParents
= true;
2144 if (!fRejectedParents
) {
2145 uint32_t nFetchFlags
= GetFetchFlags(pfrom
);
2146 for (const CTxIn
& txin
: tx
.vin
) {
2147 CInv
_inv(MSG_TX
| nFetchFlags
, txin
.prevout
.hash
);
2148 pfrom
->AddInventoryKnown(_inv
);
2149 if (!AlreadyHave(_inv
)) pfrom
->AskFor(_inv
);
2151 AddOrphanTx(ptx
, pfrom
->GetId());
2153 // DoS prevention: do not allow mapOrphanTransactions to grow unbounded
2154 unsigned int nMaxOrphanTx
= (unsigned int)std::max((int64_t)0, gArgs
.GetArg("-maxorphantx", DEFAULT_MAX_ORPHAN_TRANSACTIONS
));
2155 unsigned int nEvicted
= LimitOrphanTxSize(nMaxOrphanTx
);
2157 LogPrint(BCLog::MEMPOOL
, "mapOrphan overflow, removed %u tx\n", nEvicted
);
2160 LogPrint(BCLog::MEMPOOL
, "not keeping orphan with rejected parents %s\n",tx
.GetHash().ToString());
2161 // We will continue to reject this tx since it has rejected
2162 // parents so avoid re-requesting it from other peers.
2163 recentRejects
->insert(tx
.GetHash());
2166 if (!tx
.HasWitness() && !state
.CorruptionPossible()) {
2167 // Do not use rejection cache for witness transactions or
2168 // witness-stripped transactions, as they can have been malleated.
2169 // See https://github.com/bitcoin/bitcoin/issues/8279 for details.
2170 assert(recentRejects
);
2171 recentRejects
->insert(tx
.GetHash());
2172 if (RecursiveDynamicUsage(*ptx
) < 100000) {
2173 AddToCompactExtraTransactions(ptx
);
2175 } else if (tx
.HasWitness() && RecursiveDynamicUsage(*ptx
) < 100000) {
2176 AddToCompactExtraTransactions(ptx
);
2179 if (pfrom
->fWhitelisted
&& gArgs
.GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY
)) {
2180 // Always relay transactions received from whitelisted peers, even
2181 // if they were already in the mempool or rejected from it due
2182 // to policy, allowing the node to function as a gateway for
2183 // nodes hidden behind it.
2185 // Never relay transactions that we would assign a non-zero DoS
2186 // score for, as we expect peers to do the same with us in that
2189 if (!state
.IsInvalid(nDoS
) || nDoS
== 0) {
2190 LogPrintf("Force relaying tx %s from whitelisted peer=%d\n", tx
.GetHash().ToString(), pfrom
->GetId());
2191 RelayTransaction(tx
, connman
);
2193 LogPrintf("Not relaying invalid transaction %s from whitelisted peer=%d (%s)\n", tx
.GetHash().ToString(), pfrom
->GetId(), FormatStateMessage(state
));
2198 for (const CTransactionRef
& removedTx
: lRemovedTxn
)
2199 AddToCompactExtraTransactions(removedTx
);
2202 if (state
.IsInvalid(nDoS
))
2204 LogPrint(BCLog::MEMPOOLREJ
, "%s from peer=%d was not accepted: %s\n", tx
.GetHash().ToString(),
2206 FormatStateMessage(state
));
2207 if (state
.GetRejectCode() > 0 && state
.GetRejectCode() < REJECT_INTERNAL
) // Never send AcceptToMemoryPool's internal codes over P2P
2208 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::REJECT
, strCommand
, (unsigned char)state
.GetRejectCode(),
2209 state
.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH
), inv
.hash
));
2211 Misbehaving(pfrom
->GetId(), nDoS
);
2217 else if (strCommand
== NetMsgType::CMPCTBLOCK
&& !fImporting
&& !fReindex
) // Ignore blocks received while importing
2219 CBlockHeaderAndShortTxIDs cmpctblock
;
2220 vRecv
>> cmpctblock
;
2225 if (mapBlockIndex
.find(cmpctblock
.header
.hashPrevBlock
) == mapBlockIndex
.end()) {
2226 // Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
2227 if (!IsInitialBlockDownload())
2228 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETHEADERS
, chainActive
.GetLocator(pindexBestHeader
), uint256()));
2233 const CBlockIndex
*pindex
= nullptr;
2234 CValidationState state
;
2235 if (!ProcessNewBlockHeaders({cmpctblock
.header
}, state
, chainparams
, &pindex
)) {
2237 if (state
.IsInvalid(nDoS
)) {
2240 Misbehaving(pfrom
->GetId(), nDoS
);
2242 LogPrintf("Peer %d sent us invalid header via cmpctblock\n", pfrom
->GetId());
2247 // When we succeed in decoding a block's txids from a cmpctblock
2248 // message we typically jump to the BLOCKTXN handling code, with a
2249 // dummy (empty) BLOCKTXN message, to re-use the logic there in
2250 // completing processing of the putative block (without cs_main).
2251 bool fProcessBLOCKTXN
= false;
2252 CDataStream
blockTxnMsg(SER_NETWORK
, PROTOCOL_VERSION
);
2254 // If we end up treating this as a plain headers message, call that as well
2256 bool fRevertToHeaderProcessing
= false;
2258 // Keep a CBlock for "optimistic" compactblock reconstructions (see
2260 std::shared_ptr
<CBlock
> pblock
= std::make_shared
<CBlock
>();
2261 bool fBlockReconstructed
= false;
2265 // If AcceptBlockHeader returned true, it set pindex
2267 UpdateBlockAvailability(pfrom
->GetId(), pindex
->GetBlockHash());
2269 std::map
<uint256
, std::pair
<NodeId
, std::list
<QueuedBlock
>::iterator
> >::iterator blockInFlightIt
= mapBlocksInFlight
.find(pindex
->GetBlockHash());
2270 bool fAlreadyInFlight
= blockInFlightIt
!= mapBlocksInFlight
.end();
2272 if (pindex
->nStatus
& BLOCK_HAVE_DATA
) // Nothing to do here
2275 if (pindex
->nChainWork
<= chainActive
.Tip()->nChainWork
|| // We know something better
2276 pindex
->nTx
!= 0) { // We had this block at some point, but pruned it
2277 if (fAlreadyInFlight
) {
2278 // We requested this block for some reason, but our mempool will probably be useless
2279 // so we just grab the block via normal getdata
2280 std::vector
<CInv
> vInv(1);
2281 vInv
[0] = CInv(MSG_BLOCK
| GetFetchFlags(pfrom
), cmpctblock
.header
.GetHash());
2282 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETDATA
, vInv
));
2287 // If we're not close to tip yet, give up and let parallel block fetch work its magic
2288 if (!fAlreadyInFlight
&& !CanDirectFetch(chainparams
.GetConsensus()))
2291 CNodeState
*nodestate
= State(pfrom
->GetId());
2293 if (IsWitnessEnabled(pindex
->pprev
, chainparams
.GetConsensus()) && !nodestate
->fSupportsDesiredCmpctVersion
) {
2294 // Don't bother trying to process compact blocks from v1 peers
2295 // after segwit activates.
2299 // We want to be a bit conservative just to be extra careful about DoS
2300 // possibilities in compact block processing...
2301 if (pindex
->nHeight
<= chainActive
.Height() + 2) {
2302 if ((!fAlreadyInFlight
&& nodestate
->nBlocksInFlight
< MAX_BLOCKS_IN_TRANSIT_PER_PEER
) ||
2303 (fAlreadyInFlight
&& blockInFlightIt
->second
.first
== pfrom
->GetId())) {
2304 std::list
<QueuedBlock
>::iterator
* queuedBlockIt
= nullptr;
2305 if (!MarkBlockAsInFlight(pfrom
->GetId(), pindex
->GetBlockHash(), pindex
, &queuedBlockIt
)) {
2306 if (!(*queuedBlockIt
)->partialBlock
)
2307 (*queuedBlockIt
)->partialBlock
.reset(new PartiallyDownloadedBlock(&mempool
));
2309 // The block was already in flight using compact blocks from the same peer
2310 LogPrint(BCLog::NET
, "Peer sent us compact block we were already syncing!\n");
2315 PartiallyDownloadedBlock
& partialBlock
= *(*queuedBlockIt
)->partialBlock
;
2316 ReadStatus status
= partialBlock
.InitData(cmpctblock
, vExtraTxnForCompact
);
2317 if (status
== READ_STATUS_INVALID
) {
2318 MarkBlockAsReceived(pindex
->GetBlockHash()); // Reset in-flight state in case of whitelist
2319 Misbehaving(pfrom
->GetId(), 100);
2320 LogPrintf("Peer %d sent us invalid compact block\n", pfrom
->GetId());
2322 } else if (status
== READ_STATUS_FAILED
) {
2323 // Duplicate txindexes, the block is now in-flight, so just request it
2324 std::vector
<CInv
> vInv(1);
2325 vInv
[0] = CInv(MSG_BLOCK
| GetFetchFlags(pfrom
), cmpctblock
.header
.GetHash());
2326 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETDATA
, vInv
));
2330 BlockTransactionsRequest req
;
2331 for (size_t i
= 0; i
< cmpctblock
.BlockTxCount(); i
++) {
2332 if (!partialBlock
.IsTxAvailable(i
))
2333 req
.indexes
.push_back(i
);
2335 if (req
.indexes
.empty()) {
2336 // Dirty hack to jump to BLOCKTXN code (TODO: move message handling into their own functions)
2337 BlockTransactions txn
;
2338 txn
.blockhash
= cmpctblock
.header
.GetHash();
2340 fProcessBLOCKTXN
= true;
2342 req
.blockhash
= pindex
->GetBlockHash();
2343 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETBLOCKTXN
, req
));
2346 // This block is either already in flight from a different
2347 // peer, or this peer has too many blocks outstanding to
2349 // Optimistically try to reconstruct anyway since we might be
2350 // able to without any round trips.
2351 PartiallyDownloadedBlock
tempBlock(&mempool
);
2352 ReadStatus status
= tempBlock
.InitData(cmpctblock
, vExtraTxnForCompact
);
2353 if (status
!= READ_STATUS_OK
) {
2354 // TODO: don't ignore failures
2357 std::vector
<CTransactionRef
> dummy
;
2358 status
= tempBlock
.FillBlock(*pblock
, dummy
);
2359 if (status
== READ_STATUS_OK
) {
2360 fBlockReconstructed
= true;
2364 if (fAlreadyInFlight
) {
2365 // We requested this block, but its far into the future, so our
2366 // mempool will probably be useless - request the block normally
2367 std::vector
<CInv
> vInv(1);
2368 vInv
[0] = CInv(MSG_BLOCK
| GetFetchFlags(pfrom
), cmpctblock
.header
.GetHash());
2369 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETDATA
, vInv
));
2372 // If this was an announce-cmpctblock, we want the same treatment as a header message
2373 fRevertToHeaderProcessing
= true;
2378 if (fProcessBLOCKTXN
)
2379 return ProcessMessage(pfrom
, NetMsgType::BLOCKTXN
, blockTxnMsg
, nTimeReceived
, chainparams
, connman
, interruptMsgProc
);
2381 if (fRevertToHeaderProcessing
) {
2382 // Headers received from HB compact block peers are permitted to be
2383 // relayed before full validation (see BIP 152), so we don't want to disconnect
2384 // the peer if the header turns out to be for an invalid block.
2385 // Note that if a peer tries to build on an invalid chain, that
2386 // will be detected and the peer will be banned.
2387 return ProcessHeadersMessage(pfrom
, connman
, {cmpctblock
.header
}, chainparams
, /*punish_duplicate_invalid=*/false);
2390 if (fBlockReconstructed
) {
2391 // If we got here, we were able to optimistically reconstruct a
2392 // block that is in flight from some other peer.
2395 mapBlockSource
.emplace(pblock
->GetHash(), std::make_pair(pfrom
->GetId(), false));
2397 bool fNewBlock
= false;
2398 // Setting fForceProcessing to true means that we bypass some of
2399 // our anti-DoS protections in AcceptBlock, which filters
2400 // unrequested blocks that might be trying to waste our resources
2401 // (eg disk space). Because we only try to reconstruct blocks when
2402 // we're close to caught up (via the CanDirectFetch() requirement
2403 // above, combined with the behavior of not requesting blocks until
2404 // we have a chain with at least nMinimumChainWork), and we ignore
2405 // compact blocks with less work than our tip, it is safe to treat
2406 // reconstructed compact blocks as having been requested.
2407 ProcessNewBlock(chainparams
, pblock
, /*fForceProcessing=*/true, &fNewBlock
);
2409 pfrom
->nLastBlockTime
= GetTime();
2412 mapBlockSource
.erase(pblock
->GetHash());
2414 LOCK(cs_main
); // hold cs_main for CBlockIndex::IsValid()
2415 if (pindex
->IsValid(BLOCK_VALID_TRANSACTIONS
)) {
2416 // Clear download state for this block, which is in
2417 // process from some other peer. We do this after calling
2418 // ProcessNewBlock so that a malleated cmpctblock announcement
2419 // can't be used to interfere with block relay.
2420 MarkBlockAsReceived(pblock
->GetHash());
2426 else if (strCommand
== NetMsgType::BLOCKTXN
&& !fImporting
&& !fReindex
) // Ignore blocks received while importing
2428 BlockTransactions resp
;
2431 std::shared_ptr
<CBlock
> pblock
= std::make_shared
<CBlock
>();
2432 bool fBlockRead
= false;
2436 std::map
<uint256
, std::pair
<NodeId
, std::list
<QueuedBlock
>::iterator
> >::iterator it
= mapBlocksInFlight
.find(resp
.blockhash
);
2437 if (it
== mapBlocksInFlight
.end() || !it
->second
.second
->partialBlock
||
2438 it
->second
.first
!= pfrom
->GetId()) {
2439 LogPrint(BCLog::NET
, "Peer %d sent us block transactions for block we weren't expecting\n", pfrom
->GetId());
2443 PartiallyDownloadedBlock
& partialBlock
= *it
->second
.second
->partialBlock
;
2444 ReadStatus status
= partialBlock
.FillBlock(*pblock
, resp
.txn
);
2445 if (status
== READ_STATUS_INVALID
) {
2446 MarkBlockAsReceived(resp
.blockhash
); // Reset in-flight state in case of whitelist
2447 Misbehaving(pfrom
->GetId(), 100);
2448 LogPrintf("Peer %d sent us invalid compact block/non-matching block transactions\n", pfrom
->GetId());
2450 } else if (status
== READ_STATUS_FAILED
) {
2451 // Might have collided, fall back to getdata now :(
2452 std::vector
<CInv
> invs
;
2453 invs
.push_back(CInv(MSG_BLOCK
| GetFetchFlags(pfrom
), resp
.blockhash
));
2454 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::GETDATA
, invs
));
2456 // Block is either okay, or possibly we received
2457 // READ_STATUS_CHECKBLOCK_FAILED.
2458 // Note that CheckBlock can only fail for one of a few reasons:
2459 // 1. bad-proof-of-work (impossible here, because we've already
2460 // accepted the header)
2461 // 2. merkleroot doesn't match the transactions given (already
2462 // caught in FillBlock with READ_STATUS_FAILED, so
2464 // 3. the block is otherwise invalid (eg invalid coinbase,
2465 // block is too big, too many legacy sigops, etc).
2466 // So if CheckBlock failed, #3 is the only possibility.
2467 // Under BIP 152, we don't DoS-ban unless proof of work is
2468 // invalid (we don't require all the stateless checks to have
2469 // been run). This is handled below, so just treat this as
2470 // though the block was successfully read, and rely on the
2471 // handling in ProcessNewBlock to ensure the block index is
2472 // updated, reject messages go out, etc.
2473 MarkBlockAsReceived(resp
.blockhash
); // it is now an empty pointer
2475 // mapBlockSource is only used for sending reject messages and DoS scores,
2476 // so the race between here and cs_main in ProcessNewBlock is fine.
2477 // BIP 152 permits peers to relay compact blocks after validating
2478 // the header only; we should not punish peers if the block turns
2479 // out to be invalid.
2480 mapBlockSource
.emplace(resp
.blockhash
, std::make_pair(pfrom
->GetId(), false));
2482 } // Don't hold cs_main when we call into ProcessNewBlock
2484 bool fNewBlock
= false;
2485 // Since we requested this block (it was in mapBlocksInFlight), force it to be processed,
2486 // even if it would not be a candidate for new tip (missing previous block, chain not long enough, etc)
2487 // This bypasses some anti-DoS logic in AcceptBlock (eg to prevent
2488 // disk-space attacks), but this should be safe due to the
2489 // protections in the compact block handler -- see related comment
2490 // in compact block optimistic reconstruction handling.
2491 ProcessNewBlock(chainparams
, pblock
, /*fForceProcessing=*/true, &fNewBlock
);
2493 pfrom
->nLastBlockTime
= GetTime();
2496 mapBlockSource
.erase(pblock
->GetHash());
2502 else if (strCommand
== NetMsgType::HEADERS
&& !fImporting
&& !fReindex
) // Ignore headers received while importing
2504 std::vector
<CBlockHeader
> headers
;
2506 // Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks.
2507 unsigned int nCount
= ReadCompactSize(vRecv
);
2508 if (nCount
> MAX_HEADERS_RESULTS
) {
2510 Misbehaving(pfrom
->GetId(), 20);
2511 return error("headers message size = %u", nCount
);
2513 headers
.resize(nCount
);
2514 for (unsigned int n
= 0; n
< nCount
; n
++) {
2515 vRecv
>> headers
[n
];
2516 ReadCompactSize(vRecv
); // ignore tx count; assume it is 0.
2519 // Headers received via a HEADERS message should be valid, and reflect
2520 // the chain the peer is on. If we receive a known-invalid header,
2521 // disconnect the peer if it is using one of our outbound connection
2523 bool should_punish
= !pfrom
->fInbound
&& !pfrom
->m_manual_connection
;
2524 return ProcessHeadersMessage(pfrom
, connman
, headers
, chainparams
, should_punish
);
2527 else if (strCommand
== NetMsgType::BLOCK
&& !fImporting
&& !fReindex
) // Ignore blocks received while importing
2529 std::shared_ptr
<CBlock
> pblock
= std::make_shared
<CBlock
>();
2532 LogPrint(BCLog::NET
, "received block %s peer=%d\n", pblock
->GetHash().ToString(), pfrom
->GetId());
2534 bool forceProcessing
= false;
2535 const uint256
hash(pblock
->GetHash());
2538 // Also always process if we requested the block explicitly, as we may
2539 // need it even though it is not a candidate for a new best tip.
2540 forceProcessing
|= MarkBlockAsReceived(hash
);
2541 // mapBlockSource is only used for sending reject messages and DoS scores,
2542 // so the race between here and cs_main in ProcessNewBlock is fine.
2543 mapBlockSource
.emplace(hash
, std::make_pair(pfrom
->GetId(), true));
2545 bool fNewBlock
= false;
2546 ProcessNewBlock(chainparams
, pblock
, forceProcessing
, &fNewBlock
);
2548 pfrom
->nLastBlockTime
= GetTime();
2551 mapBlockSource
.erase(pblock
->GetHash());
2556 else if (strCommand
== NetMsgType::GETADDR
)
2558 // This asymmetric behavior for inbound and outbound connections was introduced
2559 // to prevent a fingerprinting attack: an attacker can send specific fake addresses
2560 // to users' AddrMan and later request them by sending getaddr messages.
2561 // Making nodes which are behind NAT and can only make outgoing connections ignore
2562 // the getaddr message mitigates the attack.
2563 if (!pfrom
->fInbound
) {
2564 LogPrint(BCLog::NET
, "Ignoring \"getaddr\" from outbound connection. peer=%d\n", pfrom
->GetId());
2568 // Only send one GetAddr response per connection to reduce resource waste
2569 // and discourage addr stamping of INV announcements.
2570 if (pfrom
->fSentAddr
) {
2571 LogPrint(BCLog::NET
, "Ignoring repeated \"getaddr\". peer=%d\n", pfrom
->GetId());
2574 pfrom
->fSentAddr
= true;
2576 pfrom
->vAddrToSend
.clear();
2577 std::vector
<CAddress
> vAddr
= connman
->GetAddresses();
2578 FastRandomContext insecure_rand
;
2579 for (const CAddress
&addr
: vAddr
)
2580 pfrom
->PushAddress(addr
, insecure_rand
);
2584 else if (strCommand
== NetMsgType::MEMPOOL
)
2586 if (!(pfrom
->GetLocalServices() & NODE_BLOOM
) && !pfrom
->fWhitelisted
)
2588 LogPrint(BCLog::NET
, "mempool request with bloom filters disabled, disconnect peer=%d\n", pfrom
->GetId());
2589 pfrom
->fDisconnect
= true;
2593 if (connman
->OutboundTargetReached(false) && !pfrom
->fWhitelisted
)
2595 LogPrint(BCLog::NET
, "mempool request with bandwidth limit reached, disconnect peer=%d\n", pfrom
->GetId());
2596 pfrom
->fDisconnect
= true;
2600 LOCK(pfrom
->cs_inventory
);
2601 pfrom
->fSendMempool
= true;
2605 else if (strCommand
== NetMsgType::PING
)
2607 if (pfrom
->nVersion
> BIP0031_VERSION
)
2611 // Echo the message back with the nonce. This allows for two useful features:
2613 // 1) A remote node can quickly check if the connection is operational
2614 // 2) Remote nodes can measure the latency of the network thread. If this node
2615 // is overloaded it won't respond to pings quickly and the remote node can
2616 // avoid sending us more work, like chain download requests.
2618 // The nonce stops the remote getting confused between different pings: without
2619 // it, if the remote node sends a ping once per second and this node takes 5
2620 // seconds to respond to each, the 5th ping the remote sends would appear to
2621 // return very quickly.
2622 connman
->PushMessage(pfrom
, msgMaker
.Make(NetMsgType::PONG
, nonce
));
2627 else if (strCommand
== NetMsgType::PONG
)
2629 int64_t pingUsecEnd
= nTimeReceived
;
2631 size_t nAvail
= vRecv
.in_avail();
2632 bool bPingFinished
= false;
2633 std::string sProblem
;
2635 if (nAvail
>= sizeof(nonce
)) {
2638 // Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
2639 if (pfrom
->nPingNonceSent
!= 0) {
2640 if (nonce
== pfrom
->nPingNonceSent
) {
2641 // Matching pong received, this ping is no longer outstanding
2642 bPingFinished
= true;
2643 int64_t pingUsecTime
= pingUsecEnd
- pfrom
->nPingUsecStart
;
2644 if (pingUsecTime
> 0) {
2645 // Successful ping time measurement, replace previous
2646 pfrom
->nPingUsecTime
= pingUsecTime
;
2647 pfrom
->nMinPingUsecTime
= std::min(pfrom
->nMinPingUsecTime
.load(), pingUsecTime
);
2649 // This should never happen
2650 sProblem
= "Timing mishap";
2653 // Nonce mismatches are normal when pings are overlapping
2654 sProblem
= "Nonce mismatch";
2656 // This is most likely a bug in another implementation somewhere; cancel this ping
2657 bPingFinished
= true;
2658 sProblem
= "Nonce zero";
2662 sProblem
= "Unsolicited pong without ping";
2665 // This is most likely a bug in another implementation somewhere; cancel this ping
2666 bPingFinished
= true;
2667 sProblem
= "Short payload";
2670 if (!(sProblem
.empty())) {
2671 LogPrint(BCLog::NET
, "pong peer=%d: %s, %x expected, %x received, %u bytes\n",
2674 pfrom
->nPingNonceSent
,
2678 if (bPingFinished
) {
2679 pfrom
->nPingNonceSent
= 0;
2684 else if (strCommand
== NetMsgType::FILTERLOAD
)
2686 CBloomFilter filter
;
2689 if (!filter
.IsWithinSizeConstraints())
2691 // There is no excuse for sending a too-large filter
2693 Misbehaving(pfrom
->GetId(), 100);
2697 LOCK(pfrom
->cs_filter
);
2698 delete pfrom
->pfilter
;
2699 pfrom
->pfilter
= new CBloomFilter(filter
);
2700 pfrom
->pfilter
->UpdateEmptyFull();
2701 pfrom
->fRelayTxes
= true;
2706 else if (strCommand
== NetMsgType::FILTERADD
)
2708 std::vector
<unsigned char> vData
;
2711 // Nodes must NEVER send a data item > 520 bytes (the max size for a script data object,
2712 // and thus, the maximum size any matched object can have) in a filteradd message
2714 if (vData
.size() > MAX_SCRIPT_ELEMENT_SIZE
) {
2717 LOCK(pfrom
->cs_filter
);
2718 if (pfrom
->pfilter
) {
2719 pfrom
->pfilter
->insert(vData
);
2726 Misbehaving(pfrom
->GetId(), 100);
2731 else if (strCommand
== NetMsgType::FILTERCLEAR
)
2733 LOCK(pfrom
->cs_filter
);
2734 if (pfrom
->GetLocalServices() & NODE_BLOOM
) {
2735 delete pfrom
->pfilter
;
2736 pfrom
->pfilter
= new CBloomFilter();
2738 pfrom
->fRelayTxes
= true;
2741 else if (strCommand
== NetMsgType::FEEFILTER
) {
2742 CAmount newFeeFilter
= 0;
2743 vRecv
>> newFeeFilter
;
2744 if (MoneyRange(newFeeFilter
)) {
2746 LOCK(pfrom
->cs_feeFilter
);
2747 pfrom
->minFeeFilter
= newFeeFilter
;
2749 LogPrint(BCLog::NET
, "received: feefilter of %s from peer=%d\n", CFeeRate(newFeeFilter
).ToString(), pfrom
->GetId());
2753 else if (strCommand
== NetMsgType::NOTFOUND
) {
2754 // We do not care about the NOTFOUND message, but logging an Unknown Command
2755 // message would be undesirable as we transmit it ourselves.
2759 // Ignore unknown commands for extensibility
2760 LogPrint(BCLog::NET
, "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand
), pfrom
->GetId());
2768 static bool SendRejectsAndCheckIfBanned(CNode
* pnode
, CConnman
* connman
)
2770 AssertLockHeld(cs_main
);
2771 CNodeState
&state
= *State(pnode
->GetId());
2773 for (const CBlockReject
& reject
: state
.rejects
) {
2774 connman
->PushMessage(pnode
, CNetMsgMaker(INIT_PROTO_VERSION
).Make(NetMsgType::REJECT
, (std::string
)NetMsgType::BLOCK
, reject
.chRejectCode
, reject
.strRejectReason
, reject
.hashBlock
));
2776 state
.rejects
.clear();
2778 if (state
.fShouldBan
) {
2779 state
.fShouldBan
= false;
2780 if (pnode
->fWhitelisted
)
2781 LogPrintf("Warning: not punishing whitelisted peer %s!\n", pnode
->addr
.ToString());
2782 else if (pnode
->m_manual_connection
)
2783 LogPrintf("Warning: not punishing manually-connected peer %s!\n", pnode
->addr
.ToString());
2785 pnode
->fDisconnect
= true;
2786 if (pnode
->addr
.IsLocal())
2787 LogPrintf("Warning: not banning local peer %s!\n", pnode
->addr
.ToString());
2790 connman
->Ban(pnode
->addr
, BanReasonNodeMisbehaving
);
2798 bool PeerLogicValidation::ProcessMessages(CNode
* pfrom
, std::atomic
<bool>& interruptMsgProc
)
2800 const CChainParams
& chainparams
= Params();
2803 // (4) message start
2809 bool fMoreWork
= false;
2811 if (!pfrom
->vRecvGetData
.empty())
2812 ProcessGetData(pfrom
, chainparams
.GetConsensus(), connman
, interruptMsgProc
);
2814 if (pfrom
->fDisconnect
)
2817 // this maintains the order of responses
2818 if (!pfrom
->vRecvGetData
.empty()) return true;
2820 // Don't bother if send buffer is too full to respond anyway
2821 if (pfrom
->fPauseSend
)
2824 std::list
<CNetMessage
> msgs
;
2826 LOCK(pfrom
->cs_vProcessMsg
);
2827 if (pfrom
->vProcessMsg
.empty())
2829 // Just take one message
2830 msgs
.splice(msgs
.begin(), pfrom
->vProcessMsg
, pfrom
->vProcessMsg
.begin());
2831 pfrom
->nProcessQueueSize
-= msgs
.front().vRecv
.size() + CMessageHeader::HEADER_SIZE
;
2832 pfrom
->fPauseRecv
= pfrom
->nProcessQueueSize
> connman
->GetReceiveFloodSize();
2833 fMoreWork
= !pfrom
->vProcessMsg
.empty();
2835 CNetMessage
& msg(msgs
.front());
2837 msg
.SetVersion(pfrom
->GetRecvVersion());
2838 // Scan for message start
2839 if (memcmp(msg
.hdr
.pchMessageStart
, chainparams
.MessageStart(), CMessageHeader::MESSAGE_START_SIZE
) != 0) {
2840 LogPrintf("PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg
.hdr
.GetCommand()), pfrom
->GetId());
2841 pfrom
->fDisconnect
= true;
2846 CMessageHeader
& hdr
= msg
.hdr
;
2847 if (!hdr
.IsValid(chainparams
.MessageStart()))
2849 LogPrintf("PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(hdr
.GetCommand()), pfrom
->GetId());
2852 std::string strCommand
= hdr
.GetCommand();
2855 unsigned int nMessageSize
= hdr
.nMessageSize
;
2858 CDataStream
& vRecv
= msg
.vRecv
;
2859 const uint256
& hash
= msg
.GetMessageHash();
2860 if (memcmp(hash
.begin(), hdr
.pchChecksum
, CMessageHeader::CHECKSUM_SIZE
) != 0)
2862 LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s\n", __func__
,
2863 SanitizeString(strCommand
), nMessageSize
,
2864 HexStr(hash
.begin(), hash
.begin()+CMessageHeader::CHECKSUM_SIZE
),
2865 HexStr(hdr
.pchChecksum
, hdr
.pchChecksum
+CMessageHeader::CHECKSUM_SIZE
));
2873 fRet
= ProcessMessage(pfrom
, strCommand
, vRecv
, msg
.nTime
, chainparams
, connman
, interruptMsgProc
);
2874 if (interruptMsgProc
)
2876 if (!pfrom
->vRecvGetData
.empty())
2879 catch (const std::ios_base::failure
& e
)
2881 connman
->PushMessage(pfrom
, CNetMsgMaker(INIT_PROTO_VERSION
).Make(NetMsgType::REJECT
, strCommand
, REJECT_MALFORMED
, std::string("error parsing message")));
2882 if (strstr(e
.what(), "end of data"))
2884 // Allow exceptions from under-length message on vRecv
2885 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());
2887 else if (strstr(e
.what(), "size too large"))
2889 // Allow exceptions from over-long size
2890 LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__
, SanitizeString(strCommand
), nMessageSize
, e
.what());
2892 else if (strstr(e
.what(), "non-canonical ReadCompactSize()"))
2894 // Allow exceptions from non-canonical encoding
2895 LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__
, SanitizeString(strCommand
), nMessageSize
, e
.what());
2899 PrintExceptionContinue(&e
, "ProcessMessages()");
2902 catch (const std::exception
& e
) {
2903 PrintExceptionContinue(&e
, "ProcessMessages()");
2905 PrintExceptionContinue(nullptr, "ProcessMessages()");
2909 LogPrintf("%s(%s, %u bytes) FAILED peer=%d\n", __func__
, SanitizeString(strCommand
), nMessageSize
, pfrom
->GetId());
2913 SendRejectsAndCheckIfBanned(pfrom
, connman
);
2918 void PeerLogicValidation::ConsiderEviction(CNode
*pto
, int64_t time_in_seconds
)
2920 AssertLockHeld(cs_main
);
2922 CNodeState
&state
= *State(pto
->GetId());
2923 const CNetMsgMaker
msgMaker(pto
->GetSendVersion());
2925 if (!state
.m_chain_sync
.m_protect
&& IsOutboundDisconnectionCandidate(pto
) && state
.fSyncStarted
) {
2926 // This is an outbound peer subject to disconnection if they don't
2927 // announce a block with as much work as the current tip within
2928 // CHAIN_SYNC_TIMEOUT + HEADERS_RESPONSE_TIME seconds (note: if
2929 // their chain has more work than ours, we should sync to it,
2930 // unless it's invalid, in which case we should find that out and
2931 // disconnect from them elsewhere).
2932 if (state
.pindexBestKnownBlock
!= nullptr && state
.pindexBestKnownBlock
->nChainWork
>= chainActive
.Tip()->nChainWork
) {
2933 if (state
.m_chain_sync
.m_timeout
!= 0) {
2934 state
.m_chain_sync
.m_timeout
= 0;
2935 state
.m_chain_sync
.m_work_header
= nullptr;
2936 state
.m_chain_sync
.m_sent_getheaders
= false;
2938 } 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
)) {
2939 // Our best block known by this peer is behind our tip, and we're either noticing
2940 // that for the first time, OR this peer was able to catch up to some earlier point
2941 // where we checked against our tip.
2942 // Either way, set a new timeout based on current tip.
2943 state
.m_chain_sync
.m_timeout
= time_in_seconds
+ CHAIN_SYNC_TIMEOUT
;
2944 state
.m_chain_sync
.m_work_header
= chainActive
.Tip();
2945 state
.m_chain_sync
.m_sent_getheaders
= false;
2946 } else if (state
.m_chain_sync
.m_timeout
> 0 && time_in_seconds
> state
.m_chain_sync
.m_timeout
) {
2947 // No evidence yet that our peer has synced to a chain with work equal to that
2948 // of our tip, when we first detected it was behind. Send a single getheaders
2949 // message to give the peer a chance to update us.
2950 if (state
.m_chain_sync
.m_sent_getheaders
) {
2951 // They've run out of time to catch up!
2952 LogPrintf("Disconnecting outbound peer %d for old chain, best known block = %s\n", pto
->GetId(), state
.pindexBestKnownBlock
!= nullptr ? state
.pindexBestKnownBlock
->GetBlockHash().ToString() : "<none>");
2953 pto
->fDisconnect
= true;
2955 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());
2956 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::GETHEADERS
, chainActive
.GetLocator(state
.m_chain_sync
.m_work_header
->pprev
), uint256()));
2957 state
.m_chain_sync
.m_sent_getheaders
= true;
2958 constexpr int64_t HEADERS_RESPONSE_TIME
= 120; // 2 minutes
2959 // Bump the timeout to allow a response, which could clear the timeout
2960 // (if the response shows the peer has synced), reset the timeout (if
2961 // the peer syncs to the required work but not to our tip), or result
2962 // in disconnect (if we advance to the timeout and pindexBestKnownBlock
2963 // has not sufficiently progressed)
2964 state
.m_chain_sync
.m_timeout
= time_in_seconds
+ HEADERS_RESPONSE_TIME
;
2970 class CompareInvMempoolOrder
2974 explicit CompareInvMempoolOrder(CTxMemPool
*_mempool
)
2979 bool operator()(std::set
<uint256
>::iterator a
, std::set
<uint256
>::iterator b
)
2981 /* As std::make_heap produces a max-heap, we want the entries with the
2982 * fewest ancestors/highest fee to sort later. */
2983 return mp
->CompareDepthAndScore(*b
, *a
);
2987 bool PeerLogicValidation::SendMessages(CNode
* pto
, std::atomic
<bool>& interruptMsgProc
)
2989 const Consensus::Params
& consensusParams
= Params().GetConsensus();
2991 // Don't send anything until the version handshake is complete
2992 if (!pto
->fSuccessfullyConnected
|| pto
->fDisconnect
)
2995 // If we get here, the outgoing message serialization version is set and can't change.
2996 const CNetMsgMaker
msgMaker(pto
->GetSendVersion());
3001 bool pingSend
= false;
3002 if (pto
->fPingQueued
) {
3003 // RPC ping request by user
3006 if (pto
->nPingNonceSent
== 0 && pto
->nPingUsecStart
+ PING_INTERVAL
* 1000000 < GetTimeMicros()) {
3007 // Ping automatically sent as a latency probe & keepalive.
3012 while (nonce
== 0) {
3013 GetRandBytes((unsigned char*)&nonce
, sizeof(nonce
));
3015 pto
->fPingQueued
= false;
3016 pto
->nPingUsecStart
= GetTimeMicros();
3017 if (pto
->nVersion
> BIP0031_VERSION
) {
3018 pto
->nPingNonceSent
= nonce
;
3019 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::PING
, nonce
));
3021 // Peer is too old to support ping command with nonce, pong will never arrive.
3022 pto
->nPingNonceSent
= 0;
3023 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::PING
));
3027 TRY_LOCK(cs_main
, lockMain
); // Acquire cs_main for IsInitialBlockDownload() and CNodeState()
3031 if (SendRejectsAndCheckIfBanned(pto
, connman
))
3033 CNodeState
&state
= *State(pto
->GetId());
3035 // Address refresh broadcast
3036 int64_t nNow
= GetTimeMicros();
3037 if (!IsInitialBlockDownload() && pto
->nNextLocalAddrSend
< nNow
) {
3038 AdvertiseLocal(pto
);
3039 pto
->nNextLocalAddrSend
= PoissonNextSend(nNow
, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL
);
3045 if (pto
->nNextAddrSend
< nNow
) {
3046 pto
->nNextAddrSend
= PoissonNextSend(nNow
, AVG_ADDRESS_BROADCAST_INTERVAL
);
3047 std::vector
<CAddress
> vAddr
;
3048 vAddr
.reserve(pto
->vAddrToSend
.size());
3049 for (const CAddress
& addr
: pto
->vAddrToSend
)
3051 if (!pto
->addrKnown
.contains(addr
.GetKey()))
3053 pto
->addrKnown
.insert(addr
.GetKey());
3054 vAddr
.push_back(addr
);
3055 // receiver rejects addr messages larger than 1000
3056 if (vAddr
.size() >= 1000)
3058 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::ADDR
, vAddr
));
3063 pto
->vAddrToSend
.clear();
3065 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::ADDR
, vAddr
));
3066 // we only send the big addr message once
3067 if (pto
->vAddrToSend
.capacity() > 40)
3068 pto
->vAddrToSend
.shrink_to_fit();
3072 if (pindexBestHeader
== nullptr)
3073 pindexBestHeader
= chainActive
.Tip();
3074 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.
3075 if (!state
.fSyncStarted
&& !pto
->fClient
&& !fImporting
&& !fReindex
) {
3076 // Only actively request headers from a single peer, unless we're close to today.
3077 if ((nSyncStarted
== 0 && fFetch
) || pindexBestHeader
->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
3078 state
.fSyncStarted
= true;
3079 state
.nHeadersSyncTimeout
= GetTimeMicros() + HEADERS_DOWNLOAD_TIMEOUT_BASE
+ HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER
* (GetAdjustedTime() - pindexBestHeader
->GetBlockTime())/(consensusParams
.nPowTargetSpacing
);
3081 const CBlockIndex
*pindexStart
= pindexBestHeader
;
3082 /* If possible, start at the block preceding the currently
3083 best known header. This ensures that we always get a
3084 non-empty list of headers back as long as the peer
3085 is up-to-date. With a non-empty response, we can initialise
3086 the peer's known best block. This wouldn't be possible
3087 if we requested starting at pindexBestHeader and
3088 got back an empty response. */
3089 if (pindexStart
->pprev
)
3090 pindexStart
= pindexStart
->pprev
;
3091 LogPrint(BCLog::NET
, "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart
->nHeight
, pto
->GetId(), pto
->nStartingHeight
);
3092 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::GETHEADERS
, chainActive
.GetLocator(pindexStart
), uint256()));
3096 // Resend wallet transactions that haven't gotten in a block yet
3097 // Except during reindex, importing and IBD, when old wallet
3098 // transactions become unconfirmed and spams other nodes.
3099 if (!fReindex
&& !fImporting
&& !IsInitialBlockDownload())
3101 GetMainSignals().Broadcast(nTimeBestReceived
, connman
);
3105 // Try sending block announcements via headers
3108 // If we have less than MAX_BLOCKS_TO_ANNOUNCE in our
3109 // list of block hashes we're relaying, and our peer wants
3110 // headers announcements, then find the first header
3111 // not yet known to our peer but would connect, and send.
3112 // If no header would connect, or if we have too many
3113 // blocks, or if the peer doesn't want headers, just
3114 // add all to the inv queue.
3115 LOCK(pto
->cs_inventory
);
3116 std::vector
<CBlock
> vHeaders
;
3117 bool fRevertToInv
= ((!state
.fPreferHeaders
&&
3118 (!state
.fPreferHeaderAndIDs
|| pto
->vBlockHashesToAnnounce
.size() > 1)) ||
3119 pto
->vBlockHashesToAnnounce
.size() > MAX_BLOCKS_TO_ANNOUNCE
);
3120 const CBlockIndex
*pBestIndex
= nullptr; // last header queued for delivery
3121 ProcessBlockAvailability(pto
->GetId()); // ensure pindexBestKnownBlock is up-to-date
3123 if (!fRevertToInv
) {
3124 bool fFoundStartingHeader
= false;
3125 // Try to find first header that our peer doesn't have, and
3126 // then send all headers past that one. If we come across any
3127 // headers that aren't on chainActive, give up.
3128 for (const uint256
&hash
: pto
->vBlockHashesToAnnounce
) {
3129 BlockMap::iterator mi
= mapBlockIndex
.find(hash
);
3130 assert(mi
!= mapBlockIndex
.end());
3131 const CBlockIndex
*pindex
= mi
->second
;
3132 if (chainActive
[pindex
->nHeight
] != pindex
) {
3133 // Bail out if we reorged away from this block
3134 fRevertToInv
= true;
3137 if (pBestIndex
!= nullptr && pindex
->pprev
!= pBestIndex
) {
3138 // This means that the list of blocks to announce don't
3139 // connect to each other.
3140 // This shouldn't really be possible to hit during
3141 // regular operation (because reorgs should take us to
3142 // a chain that has some block not on the prior chain,
3143 // which should be caught by the prior check), but one
3144 // way this could happen is by using invalidateblock /
3145 // reconsiderblock repeatedly on the tip, causing it to
3146 // be added multiple times to vBlockHashesToAnnounce.
3147 // Robustly deal with this rare situation by reverting
3149 fRevertToInv
= true;
3152 pBestIndex
= pindex
;
3153 if (fFoundStartingHeader
) {
3154 // add this to the headers message
3155 vHeaders
.push_back(pindex
->GetBlockHeader());
3156 } else if (PeerHasHeader(&state
, pindex
)) {
3157 continue; // keep looking for the first new block
3158 } else if (pindex
->pprev
== nullptr || PeerHasHeader(&state
, pindex
->pprev
)) {
3159 // Peer doesn't have this header but they do have the prior one.
3160 // Start sending headers.
3161 fFoundStartingHeader
= true;
3162 vHeaders
.push_back(pindex
->GetBlockHeader());
3164 // Peer doesn't have this header or the prior one -- nothing will
3165 // connect, so bail out.
3166 fRevertToInv
= true;
3171 if (!fRevertToInv
&& !vHeaders
.empty()) {
3172 if (vHeaders
.size() == 1 && state
.fPreferHeaderAndIDs
) {
3173 // We only send up to 1 block as header-and-ids, as otherwise
3174 // probably means we're doing an initial-ish-sync or they're slow
3175 LogPrint(BCLog::NET
, "%s sending header-and-ids %s to peer=%d\n", __func__
,
3176 vHeaders
.front().GetHash().ToString(), pto
->GetId());
3178 int nSendFlags
= state
.fWantsCmpctWitness
? 0 : SERIALIZE_TRANSACTION_NO_WITNESS
;
3180 bool fGotBlockFromCache
= false;
3182 LOCK(cs_most_recent_block
);
3183 if (most_recent_block_hash
== pBestIndex
->GetBlockHash()) {
3184 if (state
.fWantsCmpctWitness
|| !fWitnessesPresentInMostRecentCompactBlock
)
3185 connman
->PushMessage(pto
, msgMaker
.Make(nSendFlags
, NetMsgType::CMPCTBLOCK
, *most_recent_compact_block
));
3187 CBlockHeaderAndShortTxIDs
cmpctblock(*most_recent_block
, state
.fWantsCmpctWitness
);
3188 connman
->PushMessage(pto
, msgMaker
.Make(nSendFlags
, NetMsgType::CMPCTBLOCK
, cmpctblock
));
3190 fGotBlockFromCache
= true;
3193 if (!fGotBlockFromCache
) {
3195 bool ret
= ReadBlockFromDisk(block
, pBestIndex
, consensusParams
);
3197 CBlockHeaderAndShortTxIDs
cmpctblock(block
, state
.fWantsCmpctWitness
);
3198 connman
->PushMessage(pto
, msgMaker
.Make(nSendFlags
, NetMsgType::CMPCTBLOCK
, cmpctblock
));
3200 state
.pindexBestHeaderSent
= pBestIndex
;
3201 } else if (state
.fPreferHeaders
) {
3202 if (vHeaders
.size() > 1) {
3203 LogPrint(BCLog::NET
, "%s: %u headers, range (%s, %s), to peer=%d\n", __func__
,
3205 vHeaders
.front().GetHash().ToString(),
3206 vHeaders
.back().GetHash().ToString(), pto
->GetId());
3208 LogPrint(BCLog::NET
, "%s: sending header %s to peer=%d\n", __func__
,
3209 vHeaders
.front().GetHash().ToString(), pto
->GetId());
3211 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::HEADERS
, vHeaders
));
3212 state
.pindexBestHeaderSent
= pBestIndex
;
3214 fRevertToInv
= true;
3217 // If falling back to using an inv, just try to inv the tip.
3218 // The last entry in vBlockHashesToAnnounce was our tip at some point
3220 if (!pto
->vBlockHashesToAnnounce
.empty()) {
3221 const uint256
&hashToAnnounce
= pto
->vBlockHashesToAnnounce
.back();
3222 BlockMap::iterator mi
= mapBlockIndex
.find(hashToAnnounce
);
3223 assert(mi
!= mapBlockIndex
.end());
3224 const CBlockIndex
*pindex
= mi
->second
;
3226 // Warn if we're announcing a block that is not on the main chain.
3227 // This should be very rare and could be optimized out.
3228 // Just log for now.
3229 if (chainActive
[pindex
->nHeight
] != pindex
) {
3230 LogPrint(BCLog::NET
, "Announcing block %s not on main chain (tip=%s)\n",
3231 hashToAnnounce
.ToString(), chainActive
.Tip()->GetBlockHash().ToString());
3234 // If the peer's chain has this block, don't inv it back.
3235 if (!PeerHasHeader(&state
, pindex
)) {
3236 pto
->PushInventory(CInv(MSG_BLOCK
, hashToAnnounce
));
3237 LogPrint(BCLog::NET
, "%s: sending inv peer=%d hash=%s\n", __func__
,
3238 pto
->GetId(), hashToAnnounce
.ToString());
3242 pto
->vBlockHashesToAnnounce
.clear();
3246 // Message: inventory
3248 std::vector
<CInv
> vInv
;
3250 LOCK(pto
->cs_inventory
);
3251 vInv
.reserve(std::max
<size_t>(pto
->vInventoryBlockToSend
.size(), INVENTORY_BROADCAST_MAX
));
3254 for (const uint256
& hash
: pto
->vInventoryBlockToSend
) {
3255 vInv
.push_back(CInv(MSG_BLOCK
, hash
));
3256 if (vInv
.size() == MAX_INV_SZ
) {
3257 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::INV
, vInv
));
3261 pto
->vInventoryBlockToSend
.clear();
3263 // Check whether periodic sends should happen
3264 bool fSendTrickle
= pto
->fWhitelisted
;
3265 if (pto
->nNextInvSend
< nNow
) {
3266 fSendTrickle
= true;
3267 // Use half the delay for outbound peers, as there is less privacy concern for them.
3268 pto
->nNextInvSend
= PoissonNextSend(nNow
, INVENTORY_BROADCAST_INTERVAL
>> !pto
->fInbound
);
3271 // Time to send but the peer has requested we not relay transactions.
3273 LOCK(pto
->cs_filter
);
3274 if (!pto
->fRelayTxes
) pto
->setInventoryTxToSend
.clear();
3277 // Respond to BIP35 mempool requests
3278 if (fSendTrickle
&& pto
->fSendMempool
) {
3279 auto vtxinfo
= mempool
.infoAll();
3280 pto
->fSendMempool
= false;
3281 CAmount filterrate
= 0;
3283 LOCK(pto
->cs_feeFilter
);
3284 filterrate
= pto
->minFeeFilter
;
3287 LOCK(pto
->cs_filter
);
3289 for (const auto& txinfo
: vtxinfo
) {
3290 const uint256
& hash
= txinfo
.tx
->GetHash();
3291 CInv
inv(MSG_TX
, hash
);
3292 pto
->setInventoryTxToSend
.erase(hash
);
3294 if (txinfo
.feeRate
.GetFeePerK() < filterrate
)
3298 if (!pto
->pfilter
->IsRelevantAndUpdate(*txinfo
.tx
)) continue;
3300 pto
->filterInventoryKnown
.insert(hash
);
3301 vInv
.push_back(inv
);
3302 if (vInv
.size() == MAX_INV_SZ
) {
3303 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::INV
, vInv
));
3307 pto
->timeLastMempoolReq
= GetTime();
3310 // Determine transactions to relay
3312 // Produce a vector with all candidates for sending
3313 std::vector
<std::set
<uint256
>::iterator
> vInvTx
;
3314 vInvTx
.reserve(pto
->setInventoryTxToSend
.size());
3315 for (std::set
<uint256
>::iterator it
= pto
->setInventoryTxToSend
.begin(); it
!= pto
->setInventoryTxToSend
.end(); it
++) {
3316 vInvTx
.push_back(it
);
3318 CAmount filterrate
= 0;
3320 LOCK(pto
->cs_feeFilter
);
3321 filterrate
= pto
->minFeeFilter
;
3323 // Topologically and fee-rate sort the inventory we send for privacy and priority reasons.
3324 // A heap is used so that not all items need sorting if only a few are being sent.
3325 CompareInvMempoolOrder
compareInvMempoolOrder(&mempool
);
3326 std::make_heap(vInvTx
.begin(), vInvTx
.end(), compareInvMempoolOrder
);
3327 // No reason to drain out at many times the network's capacity,
3328 // especially since we have many peers and some will draw much shorter delays.
3329 unsigned int nRelayedTransactions
= 0;
3330 LOCK(pto
->cs_filter
);
3331 while (!vInvTx
.empty() && nRelayedTransactions
< INVENTORY_BROADCAST_MAX
) {
3332 // Fetch the top element from the heap
3333 std::pop_heap(vInvTx
.begin(), vInvTx
.end(), compareInvMempoolOrder
);
3334 std::set
<uint256
>::iterator it
= vInvTx
.back();
3337 // Remove it from the to-be-sent set
3338 pto
->setInventoryTxToSend
.erase(it
);
3339 // Check if not in the filter already
3340 if (pto
->filterInventoryKnown
.contains(hash
)) {
3343 // Not in the mempool anymore? don't bother sending it.
3344 auto txinfo
= mempool
.info(hash
);
3348 if (filterrate
&& txinfo
.feeRate
.GetFeePerK() < filterrate
) {
3351 if (pto
->pfilter
&& !pto
->pfilter
->IsRelevantAndUpdate(*txinfo
.tx
)) continue;
3353 vInv
.push_back(CInv(MSG_TX
, hash
));
3354 nRelayedTransactions
++;
3356 // Expire old relay messages
3357 while (!vRelayExpiration
.empty() && vRelayExpiration
.front().first
< nNow
)
3359 mapRelay
.erase(vRelayExpiration
.front().second
);
3360 vRelayExpiration
.pop_front();
3363 auto ret
= mapRelay
.insert(std::make_pair(hash
, std::move(txinfo
.tx
)));
3365 vRelayExpiration
.push_back(std::make_pair(nNow
+ 15 * 60 * 1000000, ret
.first
));
3368 if (vInv
.size() == MAX_INV_SZ
) {
3369 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::INV
, vInv
));
3372 pto
->filterInventoryKnown
.insert(hash
);
3377 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::INV
, vInv
));
3379 // Detect whether we're stalling
3380 nNow
= GetTimeMicros();
3381 if (state
.nStallingSince
&& state
.nStallingSince
< nNow
- 1000000 * BLOCK_STALLING_TIMEOUT
) {
3382 // Stalling only triggers when the block download window cannot move. During normal steady state,
3383 // the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
3384 // should only happen during initial block download.
3385 LogPrintf("Peer=%d is stalling block download, disconnecting\n", pto
->GetId());
3386 pto
->fDisconnect
= true;
3389 // In case there is a block that has been in flight from this peer for 2 + 0.5 * N times the block interval
3390 // (with N the number of peers from which we're downloading validated blocks), disconnect due to timeout.
3391 // We compensate for other peers to prevent killing off peers due to our own downstream link
3392 // being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
3393 // to unreasonably increase our timeout.
3394 if (state
.vBlocksInFlight
.size() > 0) {
3395 QueuedBlock
&queuedBlock
= state
.vBlocksInFlight
.front();
3396 int nOtherPeersWithValidatedDownloads
= nPeersWithValidatedDownloads
- (state
.nBlocksInFlightValidHeaders
> 0);
3397 if (nNow
> state
.nDownloadingSince
+ consensusParams
.nPowTargetSpacing
* (BLOCK_DOWNLOAD_TIMEOUT_BASE
+ BLOCK_DOWNLOAD_TIMEOUT_PER_PEER
* nOtherPeersWithValidatedDownloads
)) {
3398 LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", queuedBlock
.hash
.ToString(), pto
->GetId());
3399 pto
->fDisconnect
= true;
3403 // Check for headers sync timeouts
3404 if (state
.fSyncStarted
&& state
.nHeadersSyncTimeout
< std::numeric_limits
<int64_t>::max()) {
3405 // Detect whether this is a stalling initial-headers-sync peer
3406 if (pindexBestHeader
->GetBlockTime() <= GetAdjustedTime() - 24*60*60) {
3407 if (nNow
> state
.nHeadersSyncTimeout
&& nSyncStarted
== 1 && (nPreferredDownload
- state
.fPreferredDownload
>= 1)) {
3408 // Disconnect a (non-whitelisted) peer if it is our only sync peer,
3409 // and we have others we could be using instead.
3410 // Note: If all our peers are inbound, then we won't
3411 // disconnect our sync peer for stalling; we have bigger
3412 // problems if we can't get any outbound peers.
3413 if (!pto
->fWhitelisted
) {
3414 LogPrintf("Timeout downloading headers from peer=%d, disconnecting\n", pto
->GetId());
3415 pto
->fDisconnect
= true;
3418 LogPrintf("Timeout downloading headers from whitelisted peer=%d, not disconnecting\n", pto
->GetId());
3419 // Reset the headers sync state so that we have a
3420 // chance to try downloading from a different peer.
3421 // Note: this will also result in at least one more
3422 // getheaders message to be sent to
3423 // this peer (eventually).
3424 state
.fSyncStarted
= false;
3426 state
.nHeadersSyncTimeout
= 0;
3430 // After we've caught up once, reset the timeout so we can't trigger
3431 // disconnect later.
3432 state
.nHeadersSyncTimeout
= std::numeric_limits
<int64_t>::max();
3436 // Check that outbound peers have reasonable chains
3437 // GetTime() is used by this anti-DoS logic so we can test this using mocktime
3438 ConsiderEviction(pto
, GetTime());
3441 // Message: getdata (blocks)
3443 std::vector
<CInv
> vGetData
;
3444 if (!pto
->fClient
&& (fFetch
|| !IsInitialBlockDownload()) && state
.nBlocksInFlight
< MAX_BLOCKS_IN_TRANSIT_PER_PEER
) {
3445 std::vector
<const CBlockIndex
*> vToDownload
;
3446 NodeId staller
= -1;
3447 FindNextBlocksToDownload(pto
->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER
- state
.nBlocksInFlight
, vToDownload
, staller
, consensusParams
);
3448 for (const CBlockIndex
*pindex
: vToDownload
) {
3449 uint32_t nFetchFlags
= GetFetchFlags(pto
);
3450 vGetData
.push_back(CInv(MSG_BLOCK
| nFetchFlags
, pindex
->GetBlockHash()));
3451 MarkBlockAsInFlight(pto
->GetId(), pindex
->GetBlockHash(), pindex
);
3452 LogPrint(BCLog::NET
, "Requesting block %s (%d) peer=%d\n", pindex
->GetBlockHash().ToString(),
3453 pindex
->nHeight
, pto
->GetId());
3455 if (state
.nBlocksInFlight
== 0 && staller
!= -1) {
3456 if (State(staller
)->nStallingSince
== 0) {
3457 State(staller
)->nStallingSince
= nNow
;
3458 LogPrint(BCLog::NET
, "Stall started peer=%d\n", staller
);
3464 // Message: getdata (non-blocks)
3466 while (!pto
->mapAskFor
.empty() && (*pto
->mapAskFor
.begin()).first
<= nNow
)
3468 const CInv
& inv
= (*pto
->mapAskFor
.begin()).second
;
3469 if (!AlreadyHave(inv
))
3471 LogPrint(BCLog::NET
, "Requesting %s peer=%d\n", inv
.ToString(), pto
->GetId());
3472 vGetData
.push_back(inv
);
3473 if (vGetData
.size() >= 1000)
3475 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::GETDATA
, vGetData
));
3479 //If we're not going to ask, don't expect a response.
3480 pto
->setAskFor
.erase(inv
.hash
);
3482 pto
->mapAskFor
.erase(pto
->mapAskFor
.begin());
3484 if (!vGetData
.empty())
3485 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::GETDATA
, vGetData
));
3488 // Message: feefilter
3490 // We don't want white listed peers to filter txs to us if we have -whitelistforcerelay
3491 if (pto
->nVersion
>= FEEFILTER_VERSION
&& gArgs
.GetBoolArg("-feefilter", DEFAULT_FEEFILTER
) &&
3492 !(pto
->fWhitelisted
&& gArgs
.GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY
))) {
3493 CAmount currentFilter
= mempool
.GetMinFee(gArgs
.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE
) * 1000000).GetFeePerK();
3494 int64_t timeNow
= GetTimeMicros();
3495 if (timeNow
> pto
->nextSendTimeFeeFilter
) {
3496 static CFeeRate
default_feerate(DEFAULT_MIN_RELAY_TX_FEE
);
3497 static FeeFilterRounder
filterRounder(default_feerate
);
3498 CAmount filterToSend
= filterRounder
.round(currentFilter
);
3499 // We always have a fee filter of at least minRelayTxFee
3500 filterToSend
= std::max(filterToSend
, ::minRelayTxFee
.GetFeePerK());
3501 if (filterToSend
!= pto
->lastSentFeeFilter
) {
3502 connman
->PushMessage(pto
, msgMaker
.Make(NetMsgType::FEEFILTER
, filterToSend
));
3503 pto
->lastSentFeeFilter
= filterToSend
;
3505 pto
->nextSendTimeFeeFilter
= PoissonNextSend(timeNow
, AVG_FEEFILTER_BROADCAST_INTERVAL
);
3507 // If the fee filter has changed substantially and it's still more than MAX_FEEFILTER_CHANGE_DELAY
3508 // until scheduled broadcast, then move the broadcast to within MAX_FEEFILTER_CHANGE_DELAY.
3509 else if (timeNow
+ MAX_FEEFILTER_CHANGE_DELAY
* 1000000 < pto
->nextSendTimeFeeFilter
&&
3510 (currentFilter
< 3 * pto
->lastSentFeeFilter
/ 4 || currentFilter
> 4 * pto
->lastSentFeeFilter
/ 3)) {
3511 pto
->nextSendTimeFeeFilter
= timeNow
+ GetRandInt(MAX_FEEFILTER_CHANGE_DELAY
) * 1000000;
3518 class CNetProcessingCleanup
3521 CNetProcessingCleanup() {}
3522 ~CNetProcessingCleanup() {
3523 // orphan transactions
3524 mapOrphanTransactions
.clear();
3525 mapOrphanTransactionsByPrev
.clear();
3527 } instance_of_cnetprocessingcleanup
;