Skip to content

Commit 6299350

Browse files
committed
[net processing] Add IsBlockRequested() function
MarkBlockAsReceived() should not be used for both removing the block from mapBlocksInFlight and checking whether it was in the map.
1 parent 4e90d2d commit 6299350

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/net_processing.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -463,10 +463,14 @@ class PeerManagerImpl final : public PeerManager
463463
Mutex m_recent_confirmed_transactions_mutex;
464464
std::unique_ptr<CRollingBloomFilter> m_recent_confirmed_transactions GUARDED_BY(m_recent_confirmed_transactions_mutex);
465465

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
468472
*/
469-
bool MarkBlockAsReceived(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
473+
void MarkBlockAsReceived(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
470474

471475
/* Mark a block as in flight
472476
* 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
757761
nPreferredDownload += state->fPreferredDownload;
758762
}
759763

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)
761770
{
762771
std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
763772
if (itInFlight != mapBlocksInFlight.end()) {
@@ -775,9 +784,7 @@ bool PeerManagerImpl::MarkBlockAsReceived(const uint256& hash)
775784
}
776785
state->m_stalling_since = 0us;
777786
mapBlocksInFlight.erase(itInFlight);
778-
return true;
779787
}
780-
return false;
781788
}
782789

783790
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
976983
if (pindex->nStatus & BLOCK_HAVE_DATA || m_chainman.ActiveChain().Contains(pindex)) {
977984
if (pindex->HaveTxsDownloaded())
978985
state->pindexLastCommonBlock = pindex;
979-
} else if (mapBlocksInFlight.count(pindex->GetBlockHash()) == 0) {
986+
} else if (!IsBlockRequested(pindex->GetBlockHash())) {
980987
// The block is not already downloaded, and not yet in flight.
981988
if (pindex->nHeight > nWindowEnd) {
982989
// We reached the end of the window.
@@ -2054,7 +2061,7 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, const Peer& peer,
20542061
// Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
20552062
while (pindexWalk && !m_chainman.ActiveChain().Contains(pindexWalk) && vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
20562063
if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
2057-
!mapBlocksInFlight.count(pindexWalk->GetBlockHash()) &&
2064+
!IsBlockRequested(pindexWalk->GetBlockHash()) &&
20582065
(!IsWitnessEnabled(pindexWalk->pprev, m_chainparams.GetConsensus()) || State(pfrom.GetId())->fHaveWitness)) {
20592066
// We don't have this block, and it's not yet in flight.
20602067
vToFetch.push_back(pindexWalk);
@@ -2825,7 +2832,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
28252832
LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId());
28262833

28272834
UpdateBlockAvailability(pfrom.GetId(), inv.hash);
2828-
if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count(inv.hash)) {
2835+
if (!fAlreadyHave && !fImporting && !fReindex && !IsBlockRequested(inv.hash)) {
28292836
// Headers-first is the primary method of announcement on
28302837
// the network. If a node fell back to sending blocks by inv,
28312838
// 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,
36133620
const uint256 hash(pblock->GetHash());
36143621
{
36153622
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);
36193627
// mapBlockSource is only used for punishing peers and setting
36203628
// which peers send us compact blocks, so the race between here and
36213629
// cs_main in ProcessNewBlock is fine.

0 commit comments

Comments
 (0)