Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche.cpp
Show All 29 Lines | |||||
bool AvalancheProcessor::isAccepted(const CBlockIndex *pindex) const { | bool AvalancheProcessor::isAccepted(const CBlockIndex *pindex) const { | ||||
if (auto vr = GetRecord(vote_records, pindex)) { | if (auto vr = GetRecord(vote_records, pindex)) { | ||||
return vr->isValid(); | return vr->isValid(); | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
bool AvalancheProcessor::hasFinalized(const CBlockIndex *pindex) const { | bool AvalancheProcessor::registerVotes( | ||||
if (auto vr = GetRecord(vote_records, pindex)) { | const AvalancheResponse &response, | ||||
return vr->hasFinalized(); | std::vector<const CBlockIndex *> &accepted, | ||||
} | std::vector<const CBlockIndex *> &rejected) { | ||||
return false; | |||||
} | |||||
bool AvalancheProcessor::registerVotes(const AvalancheResponse &response) { | |||||
std::vector<std::pair<const CBlockIndex *, AvalancheVote>> votes; | std::vector<std::pair<const CBlockIndex *, AvalancheVote>> votes; | ||||
votes.reserve(response.GetVotes().size()); | votes.reserve(response.GetVotes().size()); | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
for (auto &v : response.GetVotes()) { | for (auto &v : response.GetVotes()) { | ||||
BlockMap::iterator mi = mapBlockIndex.find(v.GetHash()); | BlockMap::iterator mi = mapBlockIndex.find(v.GetHash()); | ||||
if (mi == mapBlockIndex.end()) { | if (mi == mapBlockIndex.end()) { | ||||
// This should not happen, but just in case... | // This should not happen, but just in case... | ||||
continue; | continue; | ||||
} | } | ||||
votes.emplace_back(mi->second, v); | votes.emplace_back(mi->second, v); | ||||
} | } | ||||
} | } | ||||
// Register votes. | // Register votes. | ||||
auto w = vote_records.getWriteView(); | auto w = vote_records.getWriteView(); | ||||
for (auto &v : votes) { | for (auto &v : votes) { | ||||
w[v.first].registerVote(v.second.IsValid()); | auto it = w->find(v.first); | ||||
if (it == w.end()) { | |||||
// We are not voting on that item anymore. | |||||
continue; | |||||
} | |||||
auto &vr = it->second; | |||||
vr.registerVote(v.second.IsValid()); | |||||
if (!vr.hasFinalized()) { | |||||
// This item has note been finalized, so we have nothing more to do. | |||||
continue; | |||||
} | |||||
// We just finalized a vote. If it is valid, then let the caller know. | |||||
// Either way, remove the item from the map. | |||||
if (vr.isValid()) { | |||||
accepted.push_back(v.first); | |||||
} else { | |||||
rejected.push_back(v.first); | |||||
} | |||||
w->erase(it); | |||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
namespace { | namespace { | ||||
/** | /** | ||||
* Run the avalanche event loop every 10ms. | * Run the avalanche event loop every 10ms. | ||||
▲ Show 20 Lines • Show All 79 Lines • Show Last 20 Lines |