diff --git a/src/avalanche/processor.cpp b/src/avalanche/processor.cpp --- a/src/avalanche/processor.cpp +++ b/src/avalanche/processor.cpp @@ -533,21 +533,32 @@ std::vector Processor::getInvsForNextPoll(bool forPoll) { std::vector invs; + auto extractVoteRecordsToInvs = [&](auto vrBegin, auto vrEnd, + size_t maxInvs, + auto buildInvFromVoteItem) { + auto it = vrBegin; + while (it != vrEnd && invs.size() < maxInvs) { + const bool shouldPoll = + forPoll ? it->second.registerPoll() : it->second.shouldPoll(); + + if (shouldPoll) { + invs.emplace_back(buildInvFromVoteItem(it->first)); + } + + ++it; + } + }; + auto proofVoteRecordsReadView = proofVoteRecords.getReadView(); - auto pit = proofVoteRecordsReadView.begin(); // Clamp to AVALANCHE_MAX_ELEMENT_POLL - 1 so we're always able to poll // for a new block. Since the proofs are sorted by score, the most // valuable are voted first. - while (pit != proofVoteRecordsReadView.end() && - invs.size() < AVALANCHE_MAX_ELEMENT_POLL - 1) { - const bool shouldPoll = - forPoll ? pit->second.registerPoll() : pit->second.shouldPoll(); - if (shouldPoll) { - invs.emplace_back(MSG_AVA_PROOF, pit->first->getId()); - } - ++pit; - } + extractVoteRecordsToInvs( + proofVoteRecordsReadView.begin(), proofVoteRecordsReadView.end(), + AVALANCHE_MAX_ELEMENT_POLL - 1, [](const ProofRef &proof) { + return CInv(MSG_AVA_PROOF, proof->getId()); + }); // First remove all blocks that are not worth polling. { @@ -564,23 +575,10 @@ } auto r = blockVoteRecords.getReadView(); - for (const std::pair &p : - reverse_iterate(r)) { - // Check if we can run poll. - const bool shouldPoll = - forPoll ? p.second.registerPoll() : p.second.shouldPoll(); - if (!shouldPoll) { - continue; - } - - // We don't have a decision, we need more votes. - invs.emplace_back(MSG_BLOCK, p.first->GetBlockHash()); - if (invs.size() >= AVALANCHE_MAX_ELEMENT_POLL) { - // Make sure we do not produce more invs than specified by the - // protocol. - return invs; - } - } + extractVoteRecordsToInvs(r.rbegin(), r.rend(), AVALANCHE_MAX_ELEMENT_POLL, + [](const CBlockIndex *pindex) { + return CInv(MSG_BLOCK, pindex->GetBlockHash()); + }); return invs; }