Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche.cpp
Show First 20 Lines • Show All 501 Lines • ▼ Show 20 Lines | void AvalancheProcessor::clearTimedoutRequests() { | ||||
} | } | ||||
} | } | ||||
void AvalancheProcessor::runEventLoop() { | void AvalancheProcessor::runEventLoop() { | ||||
// First things first, check if we have requests that timed out and clear | // First things first, check if we have requests that timed out and clear | ||||
// them. | // them. | ||||
clearTimedoutRequests(); | clearTimedoutRequests(); | ||||
while (true) { | // Make sure there is at least one suitable node to query before gathering | ||||
// invs. | |||||
NodeId nodeid = getSuitableNodeToQuery(); | NodeId nodeid = getSuitableNodeToQuery(); | ||||
if (nodeid == NO_NODE) { | if (nodeid == NO_NODE) { | ||||
return; | return; | ||||
} | } | ||||
std::vector<CInv> invs = getInvsForNextPoll(); | |||||
if (invs.empty()) { | |||||
return; | |||||
} | |||||
do { | |||||
/** | /** | ||||
* If we lost contact to that node, then we remove it from nodeids, but | * If we lost contact to that node, then we remove it from nodeids, but | ||||
* never add the request to queries, which ensures bad nodes get cleaned | * never add the request to queries, which ensures bad nodes get cleaned | ||||
* up over time. | * up over time. | ||||
*/ | */ | ||||
std::vector<CInv> invs; | |||||
bool hasSent = connman->ForNode(nodeid, [this, &invs](CNode *pnode) { | bool hasSent = connman->ForNode(nodeid, [this, &invs](CNode *pnode) { | ||||
invs = getInvsForNextPoll(); | |||||
if (invs.empty()) { | |||||
return false; | |||||
} | |||||
uint64_t current_round = round++; | uint64_t current_round = round++; | ||||
{ | { | ||||
// Compute the time at which this requests times out. | // Compute the time at which this requests times out. | ||||
auto timeout = | auto timeout = | ||||
std::chrono::steady_clock::now() + queryTimeoutDuration; | std::chrono::steady_clock::now() + queryTimeoutDuration; | ||||
// Register the query. | // Register the query. | ||||
queries.getWriteView()->insert( | queries.getWriteView()->insert( | ||||
Show All 13 Lines | do { | ||||
pnode, | pnode, | ||||
CNetMsgMaker(pnode->GetSendVersion()) | CNetMsgMaker(pnode->GetSendVersion()) | ||||
.Make(NetMsgType::AVAPOLL, | .Make(NetMsgType::AVAPOLL, | ||||
AvalanchePoll(current_round, std::move(invs)))); | AvalanchePoll(current_round, std::move(invs)))); | ||||
return true; | return true; | ||||
}); | }); | ||||
// Success! | // Success! | ||||
if (hasSent || invs.empty()) { | if (hasSent) { | ||||
return; | return; | ||||
} | } | ||||
// This node is obsolete, delete it. | // This node is obsolete, delete it. | ||||
peerSet.getWriteView()->erase(nodeid); | peerSet.getWriteView()->erase(nodeid); | ||||
} | |||||
// Get next suitable node to try again | |||||
nodeid = getSuitableNodeToQuery(); | |||||
} while (nodeid != NO_NODE); | |||||
} | } |