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 proofVoteRecordsReadView = proofVoteRecords.getReadView(); | auto extractVoteRecordsToInvs = [&](const auto &itemVoteRecordRange, | ||||
auto buildInvFromVoteItem) { | |||||
for (const auto &[item, voteRecord] : itemVoteRecordRange) { | |||||
if (invs.size() >= AVALANCHE_MAX_ELEMENT_POLL) { | |||||
// Make sure we do not produce more invs than specified by the | |||||
// protocol. | |||||
return true; | |||||
} | |||||
auto pit = proofVoteRecordsReadView.begin(); | |||||
// TODO Factorize the proof and block loops | |||||
while (pit != proofVoteRecordsReadView.end() && | |||||
invs.size() < AVALANCHE_MAX_ELEMENT_POLL) { | |||||
const bool shouldPoll = | const bool shouldPoll = | ||||
forPoll ? pit->second.registerPoll() : pit->second.shouldPoll(); | forPoll ? voteRecord.registerPoll() : voteRecord.shouldPoll(); | ||||
if (shouldPoll) { | |||||
invs.emplace_back(MSG_AVA_PROOF, pit->first->getId()); | if (!shouldPoll) { | ||||
continue; | |||||
} | } | ||||
++pit; | |||||
invs.emplace_back(buildInvFromVoteItem(item)); | |||||
} | |||||
return invs.size() >= AVALANCHE_MAX_ELEMENT_POLL; | |||||
}; | |||||
if (extractVoteRecordsToInvs(proofVoteRecords.getReadView(), | |||||
[](const ProofRef &proof) { | |||||
return CInv(MSG_AVA_PROOF, proof->getId()); | |||||
})) { | |||||
// The inventory vector is full, we're done | |||||
return invs; | |||||
} | } | ||||
// 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(reverse_iterate(r), [](const CBlockIndex *pindex) { | ||||
reverse_iterate(r)) { | return CInv(MSG_BLOCK, pindex->GetBlockHash()); | ||||
// 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; | |||||
} | |||||
} | |||||
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 |