Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/processor.cpp
Show All 29 Lines | |||||
*/ | */ | ||||
static constexpr std::chrono::milliseconds AVALANCHE_TIME_STEP{10}; | static constexpr std::chrono::milliseconds AVALANCHE_TIME_STEP{10}; | ||||
// Unfortunately, the bitcoind codebase is full of global and we are kinda | // Unfortunately, the bitcoind codebase is full of global and we are kinda | ||||
// forced into it here. | // forced into it here. | ||||
std::unique_ptr<avalanche::Processor> g_avalanche; | std::unique_ptr<avalanche::Processor> g_avalanche; | ||||
namespace avalanche { | namespace avalanche { | ||||
static bool IsWorthPolling(const CBlockIndex *pindex) | |||||
EXCLUSIVE_LOCKS_REQUIRED(cs_main) { | |||||
AssertLockHeld(cs_main); | |||||
if (pindex->nStatus.isInvalid()) { | |||||
// No point polling invalid blocks. | |||||
return false; | |||||
} | |||||
if (::ChainstateActive().IsBlockFinalized(pindex)) { | |||||
// There is no point polling finalized block. | |||||
return false; | |||||
} | |||||
return true; | |||||
} | |||||
static bool VerifyProof(const Proof &proof, bilingual_str &error) { | static bool VerifyProof(const Proof &proof, bilingual_str &error) { | ||||
ProofValidationState proof_state; | ProofValidationState proof_state; | ||||
if (!proof.verify(proof_state)) { | if (!proof.verify(proof_state)) { | ||||
switch (proof_state.GetResult()) { | switch (proof_state.GetResult()) { | ||||
case ProofValidationResult::NO_STAKE: | case ProofValidationResult::NO_STAKE: | ||||
error = _("The avalanche proof has no stake."); | error = _("The avalanche proof has no stake."); | ||||
return false; | return false; | ||||
▲ Show 20 Lines • Show All 266 Lines • ▼ Show 20 Lines | return std::unique_ptr<Processor>(new Processor( | ||||
Proof::amountToScore(minQuorumStake), minQuorumConnectedStakeRatio, | Proof::amountToScore(minQuorumStake), minQuorumConnectedStakeRatio, | ||||
minAvaproofsNodeCount, staleVoteThreshold, staleVoteFactor)); | minAvaproofsNodeCount, staleVoteThreshold, staleVoteFactor)); | ||||
} | } | ||||
bool Processor::addBlockToReconcile(const CBlockIndex *pindex) { | bool Processor::addBlockToReconcile(const CBlockIndex *pindex) { | ||||
bool isAccepted; | bool isAccepted; | ||||
if (!pindex) { | if (!pindex) { | ||||
// IsWorthPolling expects this to be non-null, so bail early. | // isWorthPolling expects this to be non-null, so bail early. | ||||
return false; | return false; | ||||
} | } | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
if (!IsWorthPolling(pindex)) { | if (!isWorthPolling(pindex)) { | ||||
// There is no point polling this block. | // There is no point polling this block. | ||||
return false; | return false; | ||||
} | } | ||||
isAccepted = ::ChainActive().Contains(pindex); | isAccepted = ::ChainActive().Contains(pindex); | ||||
} | } | ||||
return blockVoteRecords.getWriteView() | return blockVoteRecords.getWriteView() | ||||
▲ Show 20 Lines • Show All 159 Lines • ▼ Show 20 Lines | for (size_t i = 0; i < size; i++) { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
pindex = g_chainman.m_blockman.LookupBlockIndex( | pindex = g_chainman.m_blockman.LookupBlockIndex( | ||||
BlockHash(votes[i].GetHash())); | BlockHash(votes[i].GetHash())); | ||||
if (!pindex) { | if (!pindex) { | ||||
// This should not happen, but just in case... | // This should not happen, but just in case... | ||||
continue; | continue; | ||||
} | } | ||||
if (!IsWorthPolling(pindex)) { | if (!isWorthPolling(pindex)) { | ||||
// There is no point polling this block. | // There is no point polling this block. | ||||
continue; | continue; | ||||
} | } | ||||
} | } | ||||
responseIndex.insert(std::make_pair(pindex, votes[i])); | responseIndex.insert(std::make_pair(pindex, votes[i])); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 345 Lines • ▼ Show 20 Lines | std::vector<CInv> Processor::getInvsForNextPoll(bool forPoll) { | ||||
} | } | ||||
// First remove all blocks that are not worth polling. | // First remove all blocks that are not worth polling. | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
auto w = blockVoteRecords.getWriteView(); | auto w = blockVoteRecords.getWriteView(); | ||||
for (auto it = w->begin(); it != w->end();) { | for (auto it = w->begin(); it != w->end();) { | ||||
const CBlockIndex *pindex = it->first; | const CBlockIndex *pindex = it->first; | ||||
if (!IsWorthPolling(pindex)) { | if (!isWorthPolling(pindex)) { | ||||
w->erase(it++); | w->erase(it++); | ||||
} else { | } else { | ||||
++it; | ++it; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
auto r = blockVoteRecords.getReadView(); | auto r = blockVoteRecords.getReadView(); | ||||
Show All 14 Lines | uint256 Processor::buildLocalSighash(CNode *pfrom) const { | ||||
hasher << peerData->delegation.getId(); | hasher << peerData->delegation.getId(); | ||||
hasher << pfrom->GetLocalNonce(); | hasher << pfrom->GetLocalNonce(); | ||||
hasher << pfrom->nRemoteHostNonce; | hasher << pfrom->nRemoteHostNonce; | ||||
hasher << pfrom->GetLocalExtraEntropy(); | hasher << pfrom->GetLocalExtraEntropy(); | ||||
hasher << pfrom->nRemoteExtraEntropy; | hasher << pfrom->nRemoteExtraEntropy; | ||||
return hasher.GetHash(); | return hasher.GetHash(); | ||||
} | } | ||||
bool Processor::isWorthPolling(const CBlockIndex *pindex) const { | |||||
AssertLockHeld(cs_main); | |||||
if (pindex->nStatus.isInvalid()) { | |||||
// No point polling invalid blocks. | |||||
return false; | |||||
} | |||||
if (::ChainstateActive().IsBlockFinalized(pindex)) { | |||||
// There is no point polling finalized block. | |||||
return false; | |||||
} | |||||
return true; | |||||
} | |||||
} // namespace avalanche | } // namespace avalanche |