mirror of
https://github.com/ElementsProject/elements.git
synced 2026-08-16 13:01:19 +02:00
Merge 8c0bd871fc into merged_master (Bitcoin PR bitcoin/bitcoin#23785)
This commit is contained in:
commit
413786c613
5 changed files with 78 additions and 75 deletions
|
|
@ -139,11 +139,6 @@ arith_uint256 nMinimumChainWork;
|
|||
|
||||
CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);
|
||||
|
||||
// Internal stuff
|
||||
namespace {
|
||||
CBlockIndex* pindexBestInvalid = nullptr;
|
||||
} // namespace
|
||||
|
||||
// Internal stuff from blockstorage ...
|
||||
extern RecursiveMutex cs_LastBlockFile;
|
||||
extern std::vector<CBlockFileInfo> vinfoBlockFile;
|
||||
|
|
@ -161,23 +156,24 @@ CBlockIndex* BlockManager::LookupBlockIndex(const uint256& hash) const
|
|||
return it == m_block_index.end() ? nullptr : it->second;
|
||||
}
|
||||
|
||||
CBlockIndex* BlockManager::FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator)
|
||||
CBlockIndex* CChainState::FindForkInGlobalIndex(const CBlockLocator& locator) const
|
||||
{
|
||||
AssertLockHeld(cs_main);
|
||||
|
||||
// Find the latest block common to locator and chain - we expect that
|
||||
// locator.vHave is sorted descending by height.
|
||||
for (const uint256& hash : locator.vHave) {
|
||||
CBlockIndex* pindex = LookupBlockIndex(hash);
|
||||
CBlockIndex* pindex{m_blockman.LookupBlockIndex(hash)};
|
||||
if (pindex) {
|
||||
if (chain.Contains(pindex))
|
||||
if (m_chain.Contains(pindex)) {
|
||||
return pindex;
|
||||
if (pindex->GetAncestor(chain.Height()) == chain.Tip()) {
|
||||
return chain.Tip();
|
||||
}
|
||||
if (pindex->GetAncestor(m_chain.Height()) == m_chain.Tip()) {
|
||||
return m_chain.Tip();
|
||||
}
|
||||
}
|
||||
}
|
||||
return chain.Genesis();
|
||||
return m_chain.Genesis();
|
||||
}
|
||||
|
||||
bool CheckInputScripts(const CTransaction& tx, TxValidationState& state,
|
||||
|
|
@ -1569,7 +1565,7 @@ void CChainState::CheckForkWarningConditions()
|
|||
return;
|
||||
}
|
||||
|
||||
if (pindexBestInvalid && pindexBestInvalid->nChainWork > m_chain.Tip()->nChainWork + (GetBlockProof(*m_chain.Tip()) * 6)) {
|
||||
if (m_chainman.m_best_invalid && m_chainman.m_best_invalid->nChainWork > m_chain.Tip()->nChainWork + (GetBlockProof(*m_chain.Tip()) * 6)) {
|
||||
LogPrintf("%s: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n", __func__);
|
||||
SetfLargeWorkInvalidChainFound(true);
|
||||
} else {
|
||||
|
|
@ -1580,8 +1576,9 @@ void CChainState::CheckForkWarningConditions()
|
|||
// Called both upon regular invalid block discovery *and* InvalidateBlock
|
||||
void CChainState::InvalidChainFound(CBlockIndex* pindexNew)
|
||||
{
|
||||
if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork)
|
||||
pindexBestInvalid = pindexNew;
|
||||
if (!m_chainman.m_best_invalid || pindexNew->nChainWork > m_chainman.m_best_invalid->nChainWork) {
|
||||
m_chainman.m_best_invalid = pindexNew;
|
||||
}
|
||||
if (pindexBestHeader != nullptr && pindexBestHeader->GetAncestor(pindexNew->nHeight) == pindexNew) {
|
||||
pindexBestHeader = m_chain.Tip();
|
||||
}
|
||||
|
|
@ -1603,7 +1600,7 @@ void CChainState::InvalidBlockFound(CBlockIndex* pindex, const BlockValidationSt
|
|||
{
|
||||
if (state.GetResult() != BlockValidationResult::BLOCK_MUTATED) {
|
||||
pindex->nStatus |= BLOCK_FAILED_VALID;
|
||||
m_blockman.m_failed_blocks.insert(pindex);
|
||||
m_chainman.m_failed_blocks.insert(pindex);
|
||||
setDirtyBlockIndex.insert(pindex);
|
||||
setBlockIndexCandidates.erase(pindex);
|
||||
InvalidChainFound(pindex);
|
||||
|
|
@ -2932,8 +2929,9 @@ CBlockIndex* CChainState::FindMostWorkChain() {
|
|||
bool fMissingData = !(pindexTest->nStatus & BLOCK_HAVE_DATA);
|
||||
if (fFailedChain || fMissingData) {
|
||||
// Candidate chain is not usable (either invalid or missing data)
|
||||
if (fFailedChain && (pindexBestInvalid == nullptr || pindexNew->nChainWork > pindexBestInvalid->nChainWork))
|
||||
pindexBestInvalid = pindexNew;
|
||||
if (fFailedChain && (m_chainman.m_best_invalid == nullptr || pindexNew->nChainWork > m_chainman.m_best_invalid->nChainWork)) {
|
||||
m_chainman.m_best_invalid = pindexNew;
|
||||
}
|
||||
CBlockIndex *pindexFailed = pindexNew;
|
||||
// Remove the entire chain from the set.
|
||||
while (pindexTest != pindexFailed) {
|
||||
|
|
@ -3362,7 +3360,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
|
|||
to_mark_failed->nStatus |= BLOCK_FAILED_VALID;
|
||||
setDirtyBlockIndex.insert(to_mark_failed);
|
||||
setBlockIndexCandidates.erase(to_mark_failed);
|
||||
m_blockman.m_failed_blocks.insert(to_mark_failed);
|
||||
m_chainman.m_failed_blocks.insert(to_mark_failed);
|
||||
|
||||
// If any new blocks somehow arrived while we were disconnecting
|
||||
// (above), then the pre-calculation of what should go into
|
||||
|
|
@ -3403,11 +3401,11 @@ void CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) {
|
|||
if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->HaveTxsDownloaded() && setBlockIndexCandidates.value_comp()(m_chain.Tip(), it->second)) {
|
||||
setBlockIndexCandidates.insert(it->second);
|
||||
}
|
||||
if (it->second == pindexBestInvalid) {
|
||||
if (it->second == m_chainman.m_best_invalid) {
|
||||
// Reset invalid block marker if it was pointing to one of those.
|
||||
pindexBestInvalid = nullptr;
|
||||
m_chainman.m_best_invalid = nullptr;
|
||||
}
|
||||
m_blockman.m_failed_blocks.erase(it->second);
|
||||
m_chainman.m_failed_blocks.erase(it->second);
|
||||
}
|
||||
it++;
|
||||
}
|
||||
|
|
@ -3417,7 +3415,7 @@ void CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) {
|
|||
if (pindex->nStatus & BLOCK_FAILED_MASK) {
|
||||
pindex->nStatus &= ~BLOCK_FAILED_MASK;
|
||||
setDirtyBlockIndex.insert(pindex);
|
||||
m_blockman.m_failed_blocks.erase(pindex);
|
||||
m_chainman.m_failed_blocks.erase(pindex);
|
||||
}
|
||||
pindex = pindex->pprev;
|
||||
}
|
||||
|
|
@ -3901,17 +3899,17 @@ static bool ContextualCheckBlock(const CBlock& block, BlockValidationState& stat
|
|||
return true;
|
||||
}
|
||||
|
||||
bool BlockManager::AcceptBlockHeader(const CBlockHeader& block, BlockValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool* duplicate)
|
||||
bool ChainstateManager::AcceptBlockHeader(const CBlockHeader& block, BlockValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool* duplicate)
|
||||
{
|
||||
AssertLockHeld(cs_main);
|
||||
// Check for duplicate
|
||||
uint256 hash = block.GetHash();
|
||||
BlockMap::iterator miSelf = m_block_index.find(hash);
|
||||
BlockMap::iterator miSelf{m_blockman.m_block_index.find(hash)};
|
||||
if (duplicate) {
|
||||
*duplicate = false;
|
||||
}
|
||||
if (hash != chainparams.GetConsensus().hashGenesisBlock) {
|
||||
if (miSelf != m_block_index.end()) {
|
||||
if (miSelf != m_blockman.m_block_index.end()) {
|
||||
// Block header is already known.
|
||||
CBlockIndex* pindex = miSelf->second;
|
||||
if (duplicate) {
|
||||
|
|
@ -3933,8 +3931,8 @@ bool BlockManager::AcceptBlockHeader(const CBlockHeader& block, BlockValidationS
|
|||
|
||||
// Get prev block index
|
||||
CBlockIndex* pindexPrev = nullptr;
|
||||
BlockMap::iterator mi = m_block_index.find(block.hashPrevBlock);
|
||||
if (mi == m_block_index.end()) {
|
||||
BlockMap::iterator mi{m_blockman.m_block_index.find(block.hashPrevBlock)};
|
||||
if (mi == m_blockman.m_block_index.end()) {
|
||||
LogPrint(BCLog::VALIDATION, "%s: %s prev block not found\n", __func__, hash.ToString());
|
||||
return state.Invalid(BlockValidationResult::BLOCK_MISSING_PREV, "prev-blk-not-found");
|
||||
}
|
||||
|
|
@ -3943,7 +3941,7 @@ bool BlockManager::AcceptBlockHeader(const CBlockHeader& block, BlockValidationS
|
|||
LogPrint(BCLog::VALIDATION, "%s: %s prev block invalid\n", __func__, hash.ToString());
|
||||
return state.Invalid(BlockValidationResult::BLOCK_INVALID_PREV, "bad-prevblk");
|
||||
}
|
||||
if (!ContextualCheckBlockHeader(block, state, *this, chainparams, pindexPrev, GetAdjustedTime())) {
|
||||
if (!ContextualCheckBlockHeader(block, state, m_blockman, chainparams, pindexPrev, GetAdjustedTime())) {
|
||||
LogPrint(BCLog::VALIDATION, "%s: Consensus::ContextualCheckBlockHeader: %s, %s\n", __func__, hash.ToString(), state.ToString());
|
||||
return false;
|
||||
}
|
||||
|
|
@ -3987,7 +3985,7 @@ bool BlockManager::AcceptBlockHeader(const CBlockHeader& block, BlockValidationS
|
|||
}
|
||||
}
|
||||
}
|
||||
CBlockIndex* pindex = AddToBlockIndex(block);
|
||||
CBlockIndex* pindex{m_blockman.AddToBlockIndex(block)};
|
||||
|
||||
if (ppindex)
|
||||
*ppindex = pindex;
|
||||
|
|
@ -4007,8 +4005,7 @@ bool ChainstateManager::ProcessNewBlockHeaders(const std::vector<CBlockHeader>&
|
|||
bool duplicate = false;
|
||||
for (const CBlockHeader& header : headers) {
|
||||
CBlockIndex *pindex = nullptr; // Use a temp pindex instead of ppindex to avoid a const_cast
|
||||
bool accepted = m_blockman.AcceptBlockHeader(
|
||||
header, state, chainparams, &pindex, &duplicate);
|
||||
bool accepted{AcceptBlockHeader(header, state, chainparams, &pindex, &duplicate)};
|
||||
ActiveChainstate().CheckBlockIndex();
|
||||
|
||||
if (all_duplicate) {
|
||||
|
|
@ -4041,7 +4038,7 @@ bool CChainState::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, Block
|
|||
CBlockIndex *pindexDummy = nullptr;
|
||||
CBlockIndex *&pindex = ppindex ? *ppindex : pindexDummy;
|
||||
|
||||
bool accepted_header = m_blockman.AcceptBlockHeader(block, state, m_params, &pindex);
|
||||
bool accepted_header{m_chainman.AcceptBlockHeader(block, state, m_params, &pindex)};
|
||||
CheckBlockIndex();
|
||||
|
||||
if (!accepted_header)
|
||||
|
|
@ -4460,8 +4457,9 @@ bool BlockManager::LoadBlockIndex(
|
|||
}
|
||||
}
|
||||
}
|
||||
if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
|
||||
pindexBestInvalid = pindex;
|
||||
if (pindex->nStatus & BLOCK_FAILED_MASK && (!chainman.m_best_invalid || pindex->nChainWork > chainman.m_best_invalid->nChainWork)) {
|
||||
chainman.m_best_invalid = pindex;
|
||||
}
|
||||
if (pindex->pprev)
|
||||
pindex->BuildSkip();
|
||||
if (pindex->IsValid(BLOCK_VALID_TREE) && (pindexBestHeader == nullptr || CBlockIndexWorkComparator()(pindexBestHeader, pindex)))
|
||||
|
|
@ -4472,7 +4470,6 @@ bool BlockManager::LoadBlockIndex(
|
|||
}
|
||||
|
||||
void BlockManager::Unload() {
|
||||
m_failed_blocks.clear();
|
||||
m_blocks_unlinked.clear();
|
||||
|
||||
for (const BlockMap::value_type& entry : m_block_index) {
|
||||
|
|
@ -4811,7 +4808,6 @@ void UnloadBlockIndex(CTxMemPool* mempool, ChainstateManager& chainman)
|
|||
{
|
||||
LOCK(cs_main);
|
||||
chainman.Unload();
|
||||
pindexBestInvalid = nullptr;
|
||||
pindexBestHeader = nullptr;
|
||||
if (mempool) mempool->clear();
|
||||
vinfoBlockFile.clear();
|
||||
|
|
@ -5787,7 +5783,9 @@ void ChainstateManager::Unload()
|
|||
chainstate->UnloadBlockIndex();
|
||||
}
|
||||
|
||||
m_failed_blocks.clear();
|
||||
m_blockman.Unload();
|
||||
m_best_invalid = nullptr;
|
||||
}
|
||||
|
||||
void ChainstateManager::Reset()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue