diff --git a/src/avalanche/processor.cpp b/src/avalanche/processor.cpp --- a/src/avalanche/processor.cpp +++ b/src/avalanche/processor.cpp @@ -425,15 +425,13 @@ if (invs[i].IsMsgProof()) { const ProofId proofid(votes[i].GetHash()); - // TODO Use an unordered map or similar to avoid the loop - auto proofVoteRecordsReadView = proofVoteRecords.getReadView(); - for (auto it = proofVoteRecordsReadView.begin(); - it != proofVoteRecordsReadView.end(); it++) { - if (it->first->getId() == proofid) { - responseProof.insert(std::make_pair(it->first, votes[i])); - break; - } + LOCK(cs_peerManager); + const ProofRef proof = peerManager->getProof(proofid); + if (!proof) { + continue; } + + responseProof.insert(std::make_pair(proof, votes[i])); } } @@ -615,6 +613,22 @@ return; } + auto clearInflightRequest = [&](auto voteRecordsWriteView, + const auto &voteItem, uint8_t count) { + if (!voteItem) { + return false; + } + + auto it = voteRecordsWriteView->find(voteItem); + if (it == voteRecordsWriteView.end()) { + return false; + } + + it->second.clearInflightRequest(count); + + return true; + }; + // In flight request accounting. for (const auto &p : timedout_items) { const CInv &inv = p.first; @@ -624,26 +638,24 @@ { LOCK(cs_main); pindex = LookupBlockIndex(BlockHash(inv.hash)); - if (!pindex) { - continue; - } } - auto w = blockVoteRecords.getWriteView(); - auto it = w->find(pindex); - if (it == w.end()) { + if (!clearInflightRequest(blockVoteRecords.getWriteView(), pindex, + p.second)) { continue; } - - it->second.clearInflightRequest(p.second); } if (inv.IsMsgProof()) { - auto w = proofVoteRecords.getWriteView(); - for (auto it = w.begin(); it != w.end(); it++) { - if (it->first->getId() == inv.hash) { - it->second.clearInflightRequest(p.second); - } + ProofRef proof; + { + LOCK(cs_peerManager); + proof = peerManager->getProof(ProofId(inv.hash)); + } + + if (!clearInflightRequest(proofVoteRecords.getWriteView(), proof, + p.second)) { + continue; } } }