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) { | |||||
const std::vector<AvalancheVote> &votes = response.GetVotes(); | const std::vector<AvalancheVote> &votes = response.GetVotes(); | ||||
std::map<const CBlockIndex *, AvalancheVote> responseIndex; | std::map<const 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; | const CBlockIndex *pindex = p.first; | ||||
const AvalancheVote &v = p.second; | const AvalancheVote &v = p.second; | ||||
w[pindex].registerVote(v.IsValid()); | auto it = w->find(pindex); | ||||
if (it == w.end()) { | |||||
// We are not voting on that item anymore. | |||||
continue; | |||||
} | |||||
auto &vr = it->second; | |||||
vr.registerVote(v.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(pindex); | |||||
} else { | |||||
rejected.push_back(pindex); | |||||
} | |||||
w->erase(it); | |||||
} | } | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
namespace { | namespace { | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 76 Lines • Show Last 20 Lines |