Skip to content

Commit 2c45f83

Browse files
committed
[net processing] Tidy up MarkBlockAsReceived()
1 parent 6299350 commit 2c45f83

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/net_processing.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -768,23 +768,29 @@ bool PeerManagerImpl::IsBlockRequested(const uint256& hash)
768768

769769
void PeerManagerImpl::MarkBlockAsReceived(const uint256& hash)
770770
{
771-
std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
772-
if (itInFlight != mapBlocksInFlight.end()) {
773-
CNodeState *state = State(itInFlight->second.first);
774-
assert(state != nullptr);
775-
if (state->vBlocksInFlight.begin() == itInFlight->second.second) {
776-
// First block on the queue was received, update the start download time for the next one
777-
state->m_downloading_since = std::max(state->m_downloading_since, GetTime<std::chrono::microseconds>());
778-
}
779-
state->vBlocksInFlight.erase(itInFlight->second.second);
780-
state->nBlocksInFlight--;
781-
if (state->nBlocksInFlight == 0) {
782-
// Last validated block on the queue was received.
783-
m_peers_downloading_from--;
784-
}
785-
state->m_stalling_since = 0us;
786-
mapBlocksInFlight.erase(itInFlight);
771+
auto it = mapBlocksInFlight.find(hash);
772+
if (it == mapBlocksInFlight.end()) {
773+
// Block was not requested
774+
return;
775+
}
776+
777+
auto [node_id, list_it] = it->second;
778+
CNodeState *state = State(node_id);
779+
assert(state != nullptr);
780+
781+
if (state->vBlocksInFlight.begin() == list_it) {
782+
// First block on the queue was received, update the start download time for the next one
783+
state->m_downloading_since = std::max(state->m_downloading_since, GetTime<std::chrono::microseconds>());
784+
}
785+
state->vBlocksInFlight.erase(list_it);
786+
787+
state->nBlocksInFlight--;
788+
if (state->nBlocksInFlight == 0) {
789+
// Last validated block on the queue was received.
790+
m_peers_downloading_from--;
787791
}
792+
state->m_stalling_since = 0us;
793+
mapBlocksInFlight.erase(it);
788794
}
789795

790796
bool PeerManagerImpl::MarkBlockAsInFlight(NodeId nodeid, const CBlockIndex* pindex, std::list<QueuedBlock>::iterator** pit)

0 commit comments

Comments
 (0)