Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/processor.cpp
Show First 20 Lines • Show All 527 Lines • ▼ Show 20 Lines | |||||
bool Processor::stopEventLoop() { | bool Processor::stopEventLoop() { | ||||
return eventLoop.stopEventLoop(); | return eventLoop.stopEventLoop(); | ||||
} | } | ||||
std::vector<CInv> Processor::getInvsForNextPoll(bool forPoll) { | std::vector<CInv> Processor::getInvsForNextPoll(bool forPoll) { | ||||
std::vector<CInv> invs; | std::vector<CInv> invs; | ||||
auto extractVoteRecordsToInvs = [&](auto vrBegin, auto vrEnd, | |||||
size_t maxInvs, | |||||
auto buildInvFromVoteItem) { | |||||
auto it = vrBegin; | |||||
while (it != vrEnd && invs.size() < maxInvs) { | |||||
deadalnix: I see no good reason to use iterators here rather than a range style loop. | |||||
const bool shouldPoll = | |||||
forPoll ? it->second.registerPoll() : it->second.shouldPoll(); | |||||
if (shouldPoll) { | |||||
invs.emplace_back(buildInvFromVoteItem(it->first)); | |||||
} | |||||
++it; | |||||
} | |||||
}; | |||||
auto proofVoteRecordsReadView = proofVoteRecords.getReadView(); | auto proofVoteRecordsReadView = proofVoteRecords.getReadView(); | ||||
auto pit = proofVoteRecordsReadView.begin(); | |||||
// Clamp to AVALANCHE_MAX_ELEMENT_POLL - 1 so we're always able to poll | // 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 | // for a new block. Since the proofs are sorted by score, the most | ||||
// valuable are voted first. | // valuable are voted first. | ||||
while (pit != proofVoteRecordsReadView.end() && | extractVoteRecordsToInvs( | ||||
invs.size() < AVALANCHE_MAX_ELEMENT_POLL - 1) { | proofVoteRecordsReadView.begin(), proofVoteRecordsReadView.end(), | ||||
const bool shouldPoll = | AVALANCHE_MAX_ELEMENT_POLL - 1, [](const ProofRef &proof) { | ||||
deadalnixUnsubmitted Not Done Inline ActionsI really don't understand why you want to vote on one block rather than selecting the proof to use. This make no sense. deadalnix: I really don't understand why you want to vote on one block rather than selecting the proof to… | |||||
forPoll ? pit->second.registerPoll() : pit->second.shouldPoll(); | return CInv(MSG_AVA_PROOF, proof->getId()); | ||||
if (shouldPoll) { | }); | ||||
invs.emplace_back(MSG_AVA_PROOF, pit->first->getId()); | |||||
} | |||||
++pit; | |||||
} | |||||
// First remove all blocks that are not worth polling. | // First remove all blocks that are not worth polling. | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
auto w = blockVoteRecords.getWriteView(); | auto w = blockVoteRecords.getWriteView(); | ||||
for (auto it = w->begin(); it != w->end();) { | for (auto it = w->begin(); it != w->end();) { | ||||
const CBlockIndex *pindex = it->first; | const CBlockIndex *pindex = it->first; | ||||
if (!IsWorthPolling(pindex)) { | if (!IsWorthPolling(pindex)) { | ||||
w->erase(it++); | w->erase(it++); | ||||
} else { | } else { | ||||
++it; | ++it; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
auto r = blockVoteRecords.getReadView(); | auto r = blockVoteRecords.getReadView(); | ||||
for (const std::pair<const CBlockIndex *const, VoteRecord> &p : | extractVoteRecordsToInvs(r.rbegin(), r.rend(), AVALANCHE_MAX_ELEMENT_POLL, | ||||
reverse_iterate(r)) { | [](const CBlockIndex *pindex) { | ||||
// Check if we can run poll. | return CInv(MSG_BLOCK, pindex->GetBlockHash()); | ||||
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; | |||||
} | |||||
deadalnixUnsubmitted Not Done Inline ActionsThat loop structure seems much better to me than the one you selected. new C++ version even allow you to destructure the pair, making the whole thing more elegant. deadalnix: That loop structure seems much better to me than the one you selected. new C++ version even… | |||||
} | |||||
return invs; | return invs; | ||||
} | } | ||||
NodeId Processor::getSuitableNodeToQuery() { | NodeId Processor::getSuitableNodeToQuery() { | ||||
LOCK(cs_peerManager); | LOCK(cs_peerManager); | ||||
return peerManager->selectNode(); | return peerManager->selectNode(); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 125 Lines • Show Last 20 Lines |
I see no good reason to use iterators here rather than a range style loop.