Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche.cpp
// Copyright (c) 2018 The Bitcoin developers | // Copyright (c) 2018 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include "avalanche.h" | #include "avalanche.h" | ||||
#include "chain.h" | #include "chain.h" | ||||
#include "netmessagemaker.h" | #include "netmessagemaker.h" | ||||
#include "scheduler.h" | #include "scheduler.h" | ||||
#include "validation.h" | #include "validation.h" | ||||
std::unique_ptr<AvalancheProcessor> g_avalanche; | std::unique_ptr<AvalancheProcessor> g_avalanche; | ||||
bool AvalancheProcessor::addBlockToReconcile(const CBlockIndex *pindex) { | bool AvalancheProcessor::addBlockToReconcile(const CBlockIndex *pindex) { | ||||
bool isAccepted; | |||||
{ | |||||
LOCK(cs_main); | |||||
isAccepted = chainActive.Contains(pindex); | |||||
} | |||||
return vote_records.getWriteView() | return vote_records.getWriteView() | ||||
->insert(std::make_pair(pindex, VoteRecord())) | ->insert(std::make_pair(pindex, VoteRecord(isAccepted))) | ||||
.second; | .second; | ||||
} | } | ||||
static const VoteRecord * | static const VoteRecord * | ||||
GetRecord(const RWCollection<BlockVoteMap> &vote_records, | GetRecord(const RWCollection<BlockVoteMap> &vote_records, | ||||
const CBlockIndex *pindex) { | const CBlockIndex *pindex) { | ||||
auto r = vote_records.getReadView(); | auto r = vote_records.getReadView(); | ||||
auto it = r->find(pindex); | auto it = r->find(pindex); | ||||
if (it == r.end()) { | 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::registerVotes(NodeId nodeid, | bool AvalancheProcessor::registerVotes(NodeId nodeid, | ||||
const AvalancheResponse &response, | const AvalancheResponse &response, | ||||
std::vector<CBlockIndex *> &accepted, | std::vector<CBlockIndex *> &accepted, | ||||
▲ Show 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | std::map<CBlockIndex *, AvalancheVote> responseIndex; | ||||
if (!vr.hasFinalized()) { | if (!vr.hasFinalized()) { | ||||
// This item has note been finalized, so we have nothing more to | // This item has note been finalized, so we have nothing more to | ||||
// do. | // do. | ||||
continue; | continue; | ||||
} | } | ||||
// We just finalized a vote. If it is valid, then let the caller | // We just finalized a vote. If it is valid, then let the caller | ||||
// know. Either way, remove the item from the map. | // know. Either way, remove the item from the map. | ||||
if (vr.isValid()) { | if (vr.isAccepted()) { | ||||
accepted.push_back(pindex); | accepted.push_back(pindex); | ||||
} else { | } else { | ||||
rejected.push_back(pindex); | rejected.push_back(pindex); | ||||
} | } | ||||
w->erase(it); | w->erase(it); | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 146 Lines • Show Last 20 Lines |