Changeset View
Changeset View
Standalone View
Standalone View
src/validation.cpp
- This file is larger than 256 KB, so syntax highlighting is disabled by default.
Show First 20 Lines • Show All 106 Lines • ▼ Show 20 Lines | |||||
}; | }; | ||||
GlobalMutex g_best_block_mutex; | GlobalMutex g_best_block_mutex; | ||||
std::condition_variable g_best_block_cv; | std::condition_variable g_best_block_cv; | ||||
uint256 g_best_block; | uint256 g_best_block; | ||||
bool fCheckBlockIndex = false; | bool fCheckBlockIndex = false; | ||||
bool fCheckpointsEnabled = DEFAULT_CHECKPOINTS_ENABLED; | bool fCheckpointsEnabled = DEFAULT_CHECKPOINTS_ENABLED; | ||||
arith_uint256 nMinimumChainWork; | |||||
BlockValidationOptions::BlockValidationOptions(const Config &config) | BlockValidationOptions::BlockValidationOptions(const Config &config) | ||||
: excessiveBlockSize(config.GetMaxBlockSize()), checkPoW(true), | : excessiveBlockSize(config.GetMaxBlockSize()), checkPoW(true), | ||||
checkMerkleRoot(true) {} | checkMerkleRoot(true) {} | ||||
const CBlockIndex * | const CBlockIndex * | ||||
Chainstate::FindForkInGlobalIndex(const CBlockLocator &locator) const { | Chainstate::FindForkInGlobalIndex(const CBlockLocator &locator) const { | ||||
AssertLockHeld(cs_main); | AssertLockHeld(cs_main); | ||||
▲ Show 20 Lines • Show All 1,041 Lines • ▼ Show 20 Lines | if (m_cached_finished_ibd.load(std::memory_order_relaxed)) { | ||||
return false; | return false; | ||||
} | } | ||||
if (m_chainman.m_blockman.LoadingBlocks()) { | if (m_chainman.m_blockman.LoadingBlocks()) { | ||||
return true; | return true; | ||||
} | } | ||||
if (m_chain.Tip() == nullptr) { | if (m_chain.Tip() == nullptr) { | ||||
return true; | return true; | ||||
} | } | ||||
if (m_chain.Tip()->nChainWork < nMinimumChainWork) { | if (m_chain.Tip()->nChainWork < m_chainman.MinimumChainWork()) { | ||||
return true; | return true; | ||||
} | } | ||||
if (m_chain.Tip()->Time() < | if (m_chain.Tip()->Time() < | ||||
Now<NodeSeconds>() - m_chainman.m_options.max_tip_age) { | Now<NodeSeconds>() - m_chainman.m_options.max_tip_age) { | ||||
return true; | return true; | ||||
} | } | ||||
LogPrintf("Leaving InitialBlockDownload (latching to false)\n"); | LogPrintf("Leaving InitialBlockDownload (latching to false)\n"); | ||||
m_cached_finished_ibd.store(true, std::memory_order_relaxed); | m_cached_finished_ibd.store(true, std::memory_order_relaxed); | ||||
▲ Show 20 Lines • Show All 579 Lines • ▼ Show 20 Lines | if (!m_chainman.AssumedValidBlock().IsNull()) { | ||||
// selection of any particular chain but makes validating some faster by | // selection of any particular chain but makes validating some faster by | ||||
// effectively caching the result of part of the verification. | // effectively caching the result of part of the verification. | ||||
BlockMap::const_iterator it{ | BlockMap::const_iterator it{ | ||||
m_blockman.m_block_index.find(m_chainman.AssumedValidBlock())}; | m_blockman.m_block_index.find(m_chainman.AssumedValidBlock())}; | ||||
if (it != m_blockman.m_block_index.end()) { | if (it != m_blockman.m_block_index.end()) { | ||||
if (it->second.GetAncestor(pindex->nHeight) == pindex && | if (it->second.GetAncestor(pindex->nHeight) == pindex && | ||||
m_chainman.m_best_header->GetAncestor(pindex->nHeight) == | m_chainman.m_best_header->GetAncestor(pindex->nHeight) == | ||||
pindex && | pindex && | ||||
m_chainman.m_best_header->nChainWork >= nMinimumChainWork) { | m_chainman.m_best_header->nChainWork >= | ||||
m_chainman.MinimumChainWork()) { | |||||
// This block is a member of the assumed verified chain and an | // This block is a member of the assumed verified chain and an | ||||
// ancestor of the best header. | // ancestor of the best header. | ||||
// Script verification is skipped when connecting blocks under | // Script verification is skipped when connecting blocks under | ||||
// the assumevalid block. Assuming the assumevalid block is | // the assumevalid block. Assuming the assumevalid block is | ||||
// valid this is safe because block merkle hashes are still | // valid this is safe because block merkle hashes are still | ||||
// computed and checked, Of course, if an assumed valid block is | // computed and checked, Of course, if an assumed valid block is | ||||
// invalid due to false scriptSigs this optimization would allow | // invalid due to false scriptSigs this optimization would allow | ||||
// an invalid chain to be accepted. | // an invalid chain to be accepted. | ||||
// The equivalent time check discourages hash power from | // The equivalent time check discourages hash power from | ||||
// extorting the network via DOS attack into accepting an | // extorting the network via DOS attack into accepting an | ||||
// invalid block through telling users they must manually set | // invalid block through telling users they must manually set | ||||
// assumevalid. Requiring a software change or burying the | // assumevalid. Requiring a software change or burying the | ||||
// invalid block, regardless of the setting, makes it hard to | // invalid block, regardless of the setting, makes it hard to | ||||
// hide the implication of the demand. This also avoids having | // hide the implication of the demand. This also avoids having | ||||
// release candidates that are hardly doing any signature | // release candidates that are hardly doing any signature | ||||
// verification at all in testing without having to artificially | // verification at all in testing without having to artificially | ||||
// set the default assumed verified block further back. The test | // set the default assumed verified block further back. The test | ||||
// against nMinimumChainWork prevents the skipping when denied | // against the minimum chain work prevents the skipping when | ||||
// access to any chain at least as good as the expected chain. | // denied access to any chain at least as good as the expected | ||||
// chain. | |||||
fScriptChecks = (GetBlockProofEquivalentTime( | fScriptChecks = (GetBlockProofEquivalentTime( | ||||
*m_chainman.m_best_header, *pindex, | *m_chainman.m_best_header, *pindex, | ||||
*m_chainman.m_best_header, | *m_chainman.m_best_header, | ||||
consensusParams) <= 60 * 60 * 24 * 7 * 2); | consensusParams) <= 60 * 60 * 24 * 7 * 2); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 2,584 Lines • ▼ Show 20 Lines | if (!fRequested) { | ||||
if (fTooFarAhead) { | if (fTooFarAhead) { | ||||
return true; | return true; | ||||
} | } | ||||
// Protect against DoS attacks from low-work chains. | // Protect against DoS attacks from low-work chains. | ||||
// If our tip is behind, a peer could try to send us | // If our tip is behind, a peer could try to send us | ||||
// low-work blocks on a fake chain that we would never | // low-work blocks on a fake chain that we would never | ||||
// request; don't process these. | // request; don't process these. | ||||
if (pindex->nChainWork < nMinimumChainWork) { | if (pindex->nChainWork < m_chainman.MinimumChainWork()) { | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
const CChainParams ¶ms{m_chainman.GetParams()}; | const CChainParams ¶ms{m_chainman.GetParams()}; | ||||
const Consensus::Params &consensusParams = params.GetConsensus(); | const Consensus::Params &consensusParams = params.GetConsensus(); | ||||
if (!CheckBlock(block, state, consensusParams, | if (!CheckBlock(block, state, consensusParams, | ||||
▲ Show 20 Lines • Show All 1,964 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
/** | /** | ||||
* Apply default chain params to nullopt members. | * Apply default chain params to nullopt members. | ||||
* This helps to avoid coding errors around the accidental use of the compare | * This helps to avoid coding errors around the accidental use of the compare | ||||
* operators that accept nullopt, thus ignoring the intended default value. | * operators that accept nullopt, thus ignoring the intended default value. | ||||
*/ | */ | ||||
static ChainstateManager::Options &&Flatten(ChainstateManager::Options &&opts) { | static ChainstateManager::Options &&Flatten(ChainstateManager::Options &&opts) { | ||||
if (!opts.minimum_chain_work.has_value()) { | |||||
opts.minimum_chain_work = UintToArith256( | |||||
opts.config.GetChainParams().GetConsensus().nMinimumChainWork); | |||||
} | |||||
if (!opts.assumed_valid_block.has_value()) { | if (!opts.assumed_valid_block.has_value()) { | ||||
opts.assumed_valid_block = | opts.assumed_valid_block = | ||||
opts.config.GetChainParams().GetConsensus().defaultAssumeValid; | opts.config.GetChainParams().GetConsensus().defaultAssumeValid; | ||||
} | } | ||||
Assert(opts.adjusted_time_callback); | Assert(opts.adjusted_time_callback); | ||||
return std::move(opts); | return std::move(opts); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 173 Lines • Show Last 20 Lines |