Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche.cpp
Show All 25 Lines | if (it == r.end()) { | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
return &it->second; | return &it->second; | ||||
} | } | ||||
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->isAccepted(); | ||||
} | } | ||||
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<AvalancheBlockUpdate> &updates) { | ||||
} | |||||
return false; | |||||
} | |||||
bool AvalancheProcessor::registerVotes(const AvalancheResponse &response) { | |||||
const std::vector<AvalancheVote> &votes = response.GetVotes(); | const std::vector<AvalancheVote> &votes = response.GetVotes(); | ||||
std::map<const CBlockIndex *, AvalancheVote> responseIndex; | std::map<CBlockIndex *, AvalancheVote> responseIndex; | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
for (auto &v : votes) { | for (auto &v : votes) { | ||||
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; | ||||
} | } | ||||
responseIndex.insert(std::make_pair(mi->second, v)); | responseIndex.insert(std::make_pair(mi->second, v)); | ||||
} | } | ||||
} | } | ||||
{ | { | ||||
// Register votes. | // Register votes. | ||||
auto w = vote_records.getWriteView(); | auto w = vote_records.getWriteView(); | ||||
for (auto &p : responseIndex) { | for (auto &p : responseIndex) { | ||||
const CBlockIndex *pindex = p.first; | CBlockIndex *pindex = p.first; | ||||
const AvalancheVote &v = p.second; | const AvalancheVote &v = p.second; | ||||
auto it = w->find(pindex); | auto it = w->find(pindex); | ||||
if (it == w.end()) { | if (it == w.end()) { | ||||
// We are not voting on that item anymore. | // We are not voting on that item anymore. | ||||
continue; | continue; | ||||
} | } | ||||
it->second.registerVote(v.IsValid()); | auto &vr = it->second; | ||||
if (!vr.registerVote(v.IsValid())) { | |||||
// This vote did not provide any extra information, move on. | |||||
continue; | |||||
} | |||||
if (!vr.hasFinalized()) { | |||||
// This item has note been finalized, so we have nothing more to | |||||
// do. | |||||
updates.emplace_back( | |||||
pindex, | |||||
vr.isAccepted() ? AvalancheBlockUpdate::Status::Accepted | |||||
: AvalancheBlockUpdate::Status::Rejected); | |||||
continue; | |||||
} | |||||
// We just finalized a vote. If it is valid, then let the caller | |||||
// know. Either way, remove the item from the map. | |||||
updates.emplace_back(pindex, | |||||
vr.isAccepted() | |||||
? AvalancheBlockUpdate::Status::Finalized | |||||
: AvalancheBlockUpdate::Status::Invalid); | |||||
w->erase(it); | |||||
} | } | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
namespace { | namespace { | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 77 Lines • Show Last 20 Lines |