Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche.cpp
Show First 20 Lines • Show All 277 Lines • ▼ Show 20 Lines | NodeId AvalancheProcessor::getSuitableNodeToQuery() { | ||||
if (it->nextRequestTime <= std::chrono::steady_clock::now()) { | if (it->nextRequestTime <= std::chrono::steady_clock::now()) { | ||||
return it->nodeid; | return it->nodeid; | ||||
} | } | ||||
return NO_NODE; | return NO_NODE; | ||||
} | } | ||||
void AvalancheProcessor::runEventLoop() { | void AvalancheProcessor::runEventLoop() { | ||||
auto now = std::chrono::steady_clock::now(); | |||||
{ | |||||
// Clear expired requests. | |||||
auto w = queries.getWriteView(); | |||||
auto it = w->get<query_timeout>().begin(); | |||||
while (it != w->get<query_timeout>().end() && it->timeout < now) { | |||||
w->get<query_timeout>().erase(it++); | |||||
} | |||||
} | |||||
std::vector<CInv> invs = getInvsForNextPoll(); | std::vector<CInv> invs = getInvsForNextPoll(); | ||||
if (invs.empty()) { | if (invs.empty()) { | ||||
// If there are no invs to poll, we are done. | // If there are no invs to poll, we are done. | ||||
return; | return; | ||||
} | } | ||||
while (true) { | while (true) { | ||||
NodeId nodeid = getSuitableNodeToQuery(); | NodeId nodeid = getSuitableNodeToQuery(); | ||||
if (nodeid == NO_NODE) { | if (nodeid == NO_NODE) { | ||||
return; | return; | ||||
} | } | ||||
/** | /** | ||||
* 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. | ||||
*/ | */ | ||||
bool hasSent = connman->ForNode(nodeid, [this, &invs](CNode *pnode) { | bool hasSent = connman->ForNode(nodeid, [this, &invs](CNode *pnode) { | ||||
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() + std::chrono::seconds(10); | std::chrono::steady_clock::now() + queryTimeoutDuration; | ||||
// Register the query. | // Register the query. | ||||
queries.getWriteView()->insert( | queries.getWriteView()->insert( | ||||
{pnode->GetId(), current_round, timeout, invs}); | {pnode->GetId(), current_round, timeout, invs}); | ||||
// Set the timeout. | // Set the timeout. | ||||
auto w = peerSet.getWriteView(); | auto w = peerSet.getWriteView(); | ||||
auto it = w->find(pnode->GetId()); | auto it = w->find(pnode->GetId()); | ||||
if (it != w->end()) { | if (it != w->end()) { | ||||
w->modify(it, [&timeout](Peer &p) { | w->modify(it, [&timeout](Peer &p) { | ||||
Show All 23 Lines |