@@ -463,10 +463,14 @@ class PeerManagerImpl final : public PeerManager
463
463
Mutex m_recent_confirmed_transactions_mutex;
464
464
std::unique_ptr<CRollingBloomFilter> m_recent_confirmed_transactions GUARDED_BY (m_recent_confirmed_transactions_mutex);
465
465
466
- /* Returns a bool indicating whether we requested this block.
467
- * Also used if a block was /not/ received and timed out or started with another peer
466
+ /* * Have we requested this block from a peer */
467
+ bool IsBlockRequested (const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
468
+
469
+ /* * Remove this block from our tracked requested blocks. Called if:
470
+ * - the block has been recieved from a peer
471
+ * - the request for the block has timed out
468
472
*/
469
- bool MarkBlockAsReceived (const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
473
+ void MarkBlockAsReceived (const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
470
474
471
475
/* Mark a block as in flight
472
476
* Returns false, still setting pit, if the block was already in flight from the same peer
@@ -757,7 +761,12 @@ static void UpdatePreferredDownload(const CNode& node, CNodeState* state) EXCLUS
757
761
nPreferredDownload += state->fPreferredDownload ;
758
762
}
759
763
760
- bool PeerManagerImpl::MarkBlockAsReceived (const uint256& hash)
764
+ bool PeerManagerImpl::IsBlockRequested (const uint256& hash)
765
+ {
766
+ return mapBlocksInFlight.find (hash) != mapBlocksInFlight.end ();
767
+ }
768
+
769
+ void PeerManagerImpl::MarkBlockAsReceived (const uint256& hash)
761
770
{
762
771
std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find (hash);
763
772
if (itInFlight != mapBlocksInFlight.end ()) {
@@ -775,9 +784,7 @@ bool PeerManagerImpl::MarkBlockAsReceived(const uint256& hash)
775
784
}
776
785
state->m_stalling_since = 0us;
777
786
mapBlocksInFlight.erase (itInFlight);
778
- return true ;
779
787
}
780
- return false ;
781
788
}
782
789
783
790
bool PeerManagerImpl::MarkBlockAsInFlight (NodeId nodeid, const CBlockIndex* pindex, std::list<QueuedBlock>::iterator** pit)
@@ -976,7 +983,7 @@ void PeerManagerImpl::FindNextBlocksToDownload(NodeId nodeid, unsigned int count
976
983
if (pindex->nStatus & BLOCK_HAVE_DATA || m_chainman.ActiveChain ().Contains (pindex)) {
977
984
if (pindex->HaveTxsDownloaded ())
978
985
state->pindexLastCommonBlock = pindex;
979
- } else if (mapBlocksInFlight. count (pindex->GetBlockHash ()) == 0 ) {
986
+ } else if (! IsBlockRequested (pindex->GetBlockHash ())) {
980
987
// The block is not already downloaded, and not yet in flight.
981
988
if (pindex->nHeight > nWindowEnd) {
982
989
// We reached the end of the window.
@@ -2054,7 +2061,7 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, const Peer& peer,
2054
2061
// Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
2055
2062
while (pindexWalk && !m_chainman.ActiveChain ().Contains (pindexWalk) && vToFetch.size () <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
2056
2063
if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
2057
- !mapBlocksInFlight. count (pindexWalk->GetBlockHash ()) &&
2064
+ !IsBlockRequested (pindexWalk->GetBlockHash ()) &&
2058
2065
(!IsWitnessEnabled (pindexWalk->pprev , m_chainparams.GetConsensus ()) || State (pfrom.GetId ())->fHaveWitness )) {
2059
2066
// We don't have this block, and it's not yet in flight.
2060
2067
vToFetch.push_back (pindexWalk);
@@ -2825,7 +2832,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
2825
2832
LogPrint (BCLog::NET, " got inv: %s %s peer=%d\n " , inv.ToString (), fAlreadyHave ? " have" : " new" , pfrom.GetId ());
2826
2833
2827
2834
UpdateBlockAvailability (pfrom.GetId (), inv.hash );
2828
- if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight. count (inv.hash )) {
2835
+ if (!fAlreadyHave && !fImporting && !fReindex && !IsBlockRequested (inv.hash )) {
2829
2836
// Headers-first is the primary method of announcement on
2830
2837
// the network. If a node fell back to sending blocks by inv,
2831
2838
// it's probably for a re-org. The final block hash
@@ -3613,9 +3620,10 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
3613
3620
const uint256 hash (pblock->GetHash ());
3614
3621
{
3615
3622
LOCK (cs_main);
3616
- // Also always process if we requested the block explicitly, as we may
3617
- // need it even though it is not a candidate for a new best tip.
3618
- forceProcessing |= MarkBlockAsReceived (hash);
3623
+ // Always process the block if we requested it, since we may
3624
+ // need it even when it's not a candidate for a new best tip.
3625
+ forceProcessing = IsBlockRequested (hash);
3626
+ MarkBlockAsReceived (hash);
3619
3627
// mapBlockSource is only used for punishing peers and setting
3620
3628
// which peers send us compact blocks, so the race between here and
3621
3629
// cs_main in ProcessNewBlock is fine.
0 commit comments