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" | ||||
#include <boost/range/adaptor/reversed.hpp> | |||||
bool AvalancheProcessor::addBlockToReconcile(const CBlockIndex *pindex) { | bool AvalancheProcessor::addBlockToReconcile(const CBlockIndex *pindex) { | ||||
return vote_records.getWriteView() | return vote_records.getWriteView() | ||||
->insert(std::make_pair(pindex, VoteRecord())) | ->insert(std::make_pair(pindex, VoteRecord())) | ||||
.second; | .second; | ||||
} | } | ||||
static const VoteRecord * | static const VoteRecord * | ||||
GetRecord(const RWCollection<BlockVoteMap> &vote_records, | GetRecord(const RWCollection<BlockVoteMap> &vote_records, | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | bool AvalancheProcessor::registerVotes(const AvalancheResponse &response) { | ||||
{ | { | ||||
// 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; | |||||
} | |||||
it->second.registerVote(v.IsValid()); | |||||
} | } | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
namespace { | namespace { | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | bool AvalancheProcessor::stopEventLoop() { | ||||
stopRequest = false; | stopRequest = false; | ||||
return true; | return true; | ||||
} | } | ||||
std::vector<CInv> AvalancheProcessor::getInvsForNextPoll() const { | std::vector<CInv> AvalancheProcessor::getInvsForNextPoll() const { | ||||
std::vector<CInv> invs; | std::vector<CInv> invs; | ||||
auto r = vote_records.getReadView(); | auto r = vote_records.getReadView(); | ||||
for (const std::pair<const CBlockIndex *, VoteRecord> &p :r) { | for (const std::pair<const CBlockIndex *, VoteRecord> &p : | ||||
boost::adaptors::reverse(r)) { | |||||
const VoteRecord &v = p.second; | const VoteRecord &v = p.second; | ||||
if (v.hasFinalized()) { | if (v.hasFinalized()) { | ||||
// If this has finalized, we can just skip. | // If this has finalized, we can just skip. | ||||
continue; | continue; | ||||
} | } | ||||
// We don't have a decision, we need more votes. | // We don't have a decision, we need more votes. | ||||
invs.push_back(CInv(MSG_BLOCK, p.first->GetBlockHash())); | invs.emplace_back(MSG_BLOCK, p.first->GetBlockHash()); | ||||
if (invs.size() >= AVALANCHE_MAX_ELEMENT_POLL) { | if (invs.size() >= AVALANCHE_MAX_ELEMENT_POLL) { | ||||
// Make sure we do not produce more invs than specified by the | // Make sure we do not produce more invs than specified by the | ||||
// protocol. | // protocol. | ||||
return invs; | return invs; | ||||
} | } | ||||
} | } | ||||
return invs; | return invs; | ||||
} | } |