What changed, and why it matters
This commit is a simple code cleanup: it removes a redundant constant named BLOCK_FAILED_MASK and replaces its uses with BLOCK_FAILED_VALID, because the two had become identical in meaning. There is no functional change to how Bitcoin Core validates blocks or handles invalid chains.
No security action required. Treat as a normal refactoring/review commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch eliminates the BLOCK_FAILED_MASK enum value (defined as BLOCK_FAILED_VALID | BLOCK_FAILED_CHILD) and substitutes BLOCK_FAILED_VALID everywhere BLOCK_FAILED_MASK was used. The commit message explicitly states the reason: BLOCK_FAILED_MASK is now the same as BLOCK_FAILED_VALID. BLOCK_FAILED_CHILD was already unused. All replacements are bitwise checks against nStatus, so behavior is preserved. No consensus, networking, RPC, or storage semantics are altered.
Changed components
src/chain.hsrc/node/blockstorage.cppsrc/rpc/blockchain.cppsrc/rpc/mining.cppsrc/test/blockchain_tests.cppsrc/test/fuzz/block_index_tree.cppsrc/test/fuzz/chain.cppsrc/validation.cppInspect captured patch +23 / −25
diff --git a/src/chain.h b/src/chain.h
index 459a83aa..c2782920 100644
--- a/src/chain.h
+++ b/src/chain.h
@@ -78,7 +78,6 @@ enum BlockStatus : uint32_t {
BLOCK_FAILED_VALID = 32, //!< stage after last reached validness failed
BLOCK_FAILED_CHILD = 64, //!< Unused flag that was previously set when descending from failed block
- BLOCK_FAILED_MASK = BLOCK_FAILED_VALID | BLOCK_FAILED_CHILD,
BLOCK_OPT_WITNESS = 128, //!< block data in blk*.dat was received with a witness-enforcing client
@@ -253,7 +252,7 @@ public:
{
AssertLockHeld(::cs_main);
assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
- if (nStatus & BLOCK_FAILED_MASK)
+ if (nStatus & BLOCK_FAILED_VALID)
return false;
return ((nStatus & BLOCK_VALID_MASK) >= nUpTo);
}
@@ -264,7 +263,7 @@ public:
{
AssertLockHeld(::cs_main);
assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
- if (nStatus & BLOCK_FAILED_MASK) return false;
+ if (nStatus & BLOCK_FAILED_VALID) return false;
if ((nStatus & BLOCK_VALID_MASK) < nUpTo) {
nStatus = (nStatus & ~BLOCK_VALID_MASK) | nUpTo;
diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp
index a8d5018f..218ee222 100644
--- a/src/node/blockstorage.cpp
+++ b/src/node/blockstorage.cpp
@@ -493,7 +493,7 @@ bool BlockManager::LoadBlockIndex(const std::optional<uint256>& snapshot_blockha
pindex->nStatus = (pindex->nStatus & ~BLOCK_FAILED_CHILD) | BLOCK_FAILED_VALID;
m_dirty_blockindex.insert(pindex);
}
- if (!(pindex->nStatus & BLOCK_FAILED_MASK) && pindex->pprev && (pindex->pprev->nStatus & BLOCK_FAILED_MASK)) {
+ if (!(pindex->nStatus & BLOCK_FAILED_VALID) && pindex->pprev && (pindex->pprev->nStatus & BLOCK_FAILED_VALID)) {
// All descendants of invalid blocks are invalid too.
pindex->nStatus |= BLOCK_FAILED_VALID;
m_dirty_blockindex.insert(pindex);
diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp
index d5c99876..2b5f3bcd 100644
--- a/src/rpc/blockchain.cpp
+++ b/src/rpc/blockchain.cpp
@@ -1595,7 +1595,7 @@ static RPCHelpMan getchaintips()
if (active_chain.Contains(block)) {
// This block is part of the currently active chain.
status = "active";
- } else if (block->nStatus & BLOCK_FAILED_MASK) {
+ } else if (block->nStatus & BLOCK_FAILED_VALID) {
// This block or one of its ancestors is invalid.
status = "invalid";
} else if (!block->HaveNumChainTxs()) {
diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp
index d0efa84f..89cec6f6 100644
--- a/src/rpc/mining.cpp
+++ b/src/rpc/mining.cpp
@@ -742,7 +742,7 @@ static RPCHelpMan getblocktemplate()
if (pindex) {
if (pindex->IsValid(BLOCK_VALID_SCRIPTS))
return "duplicate";
- if (pindex->nStatus & BLOCK_FAILED_MASK)
+ if (pindex->nStatus & BLOCK_FAILED_VALID)
return "duplicate-invalid";
return "duplicate-inconclusive";
}
diff --git a/src/test/blockchain_tests.cpp b/src/test/blockchain_tests.cpp
index 8c297975..d80aa836 100644
--- a/src/test/blockchain_tests.cpp
+++ b/src/test/blockchain_tests.cpp
@@ -135,7 +135,7 @@ BOOST_FIXTURE_TEST_CASE(invalidate_block, TestChain100Setup)
auto pindex = tip_to_invalidate->pprev;
while (pindex) {
WITH_LOCK(::cs_main, assert(pindex->IsValid(BLOCK_VALID_TRANSACTIONS)));
- WITH_LOCK(::cs_main, assert((pindex->nStatus & BLOCK_FAILED_MASK) == 0));
+ WITH_LOCK(::cs_main, assert((pindex->nStatus & BLOCK_FAILED_VALID) == 0));
pindex = pindex->pprev;
}
diff --git a/src/test/fuzz/block_index_tree.cpp b/src/test/fuzz/block_index_tree.cpp
index a4bbe958..5112a7e0 100644
--- a/src/test/fuzz/block_index_tree.cpp
+++ b/src/test/fuzz/block_index_tree.cpp
@@ -61,7 +61,7 @@ FUZZ_TARGET(block_index_tree, .init = initialize_block_index_tree)
// Receive a header building on an existing valid one. This assumes headers are valid, so PoW is not relevant here.
LOCK(cs_main);
CBlockIndex* prev_block = PickValue(fuzzed_data_provider, blocks);
- if (!(prev_block->nStatus & BLOCK_FAILED_MASK)) {
+ if (!(prev_block->nStatus & BLOCK_FAILED_VALID)) {
CBlockHeader header = ConsumeBlockHeader(fuzzed_data_provider, prev_block->GetBlockHash(), nonce_counter);
CBlockIndex* index = blockman.AddToBlockIndex(header, chainman.m_best_header);
assert(index->nStatus & BLOCK_VALID_TREE);
@@ -74,7 +74,7 @@ FUZZ_TARGET(block_index_tree, .init = initialize_block_index_tree)
LOCK(cs_main);
CBlockIndex* index = PickValue(fuzzed_data_provider, blocks);
// Must be new to us and not known to be invalid (e.g. because of an invalid ancestor).
- if (index->nTx == 0 && !(index->nStatus & BLOCK_FAILED_MASK)) {
+ if (index->nTx == 0 && !(index->nStatus & BLOCK_FAILED_VALID)) {
if (fuzzed_data_provider.ConsumeBool()) { // Invalid
BlockValidationState state;
state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "consensus-invalid");
@@ -124,7 +124,7 @@ FUZZ_TARGET(block_index_tree, .init = initialize_block_index_tree)
}
// Connect blocks, possibly fail
for (CBlockIndex* block : to_connect | std::views::reverse) {
- assert(!(block->nStatus & BLOCK_FAILED_MASK));
+ assert(!(block->nStatus & BLOCK_FAILED_VALID));
assert(block->nStatus & BLOCK_HAVE_DATA);
if (!block->IsValid(BLOCK_VALID_SCRIPTS)) {
if (fuzzed_data_provider.ConsumeBool()) { // Invalid
diff --git a/src/test/fuzz/chain.cpp b/src/test/fuzz/chain.cpp
index 2c294fd5..4d3a6f42 100644
--- a/src/test/fuzz/chain.cpp
+++ b/src/test/fuzz/chain.cpp
@@ -50,7 +50,6 @@ FUZZ_TARGET(chain)
BlockStatus::BLOCK_HAVE_UNDO,
BlockStatus::BLOCK_HAVE_MASK,
BlockStatus::BLOCK_FAILED_VALID,
- BlockStatus::BLOCK_FAILED_MASK,
BlockStatus::BLOCK_OPT_WITNESS,
});
if (block_status & ~BLOCK_VALID_MASK) {
diff --git a/src/validation.cpp b/src/validation.cpp
index 7dee16d9..1285edc2 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -3173,7 +3173,7 @@ CBlockIndex* Chainstate::FindMostWorkChain()
// which block files have been deleted. Remove those as candidates
// for the most work chain if we come across them; we can't switch
// to a chain unless we have all the non-active-chain parent blocks.
- bool fFailedChain = pindexTest->nStatus & BLOCK_FAILED_MASK;
+ bool fFailedChain = pindexTest->nStatus & BLOCK_FAILED_VALID;
bool fMissingData = !(pindexTest->nStatus & BLOCK_HAVE_DATA);
if (fFailedChain || fMissingData) {
// Candidate chain is not usable (either invalid or missing data)
@@ -3590,7 +3590,7 @@ bool Chainstate::InvalidateBlock(BlockValidationState& state, CBlockIndex* pinde
// at least as good with CBlockIndexWorkComparator as the new tip.
if (!m_chain.Contains(candidate) &&
!CBlockIndexWorkComparator()(candidate, pindex->pprev) &&
- !(candidate->nStatus & BLOCK_FAILED_MASK)) {
+ !(candidate->nStatus & BLOCK_FAILED_VALID)) {
highpow_outofchain_headers.insert({candidate->nChainWork, candidate});
}
}
@@ -3689,7 +3689,7 @@ bool Chainstate::InvalidateBlock(BlockValidationState& state, CBlockIndex* pinde
}
// Mark pindex as invalid if it never was in the main chain
- if (!pindex_was_in_chain && !(pindex->nStatus & BLOCK_FAILED_MASK)) {
+ if (!pindex_was_in_chain && !(pindex->nStatus & BLOCK_FAILED_VALID)) {
pindex->nStatus |= BLOCK_FAILED_VALID;
m_blockman.m_dirty_blockindex.insert(pindex);
setBlockIndexCandidates.erase(pindex);
@@ -3753,8 +3753,8 @@ void Chainstate::ResetBlockFailureFlags(CBlockIndex *pindex) {
// Remove the invalidity flag from this block and all its descendants and ancestors.
for (auto& [_, block_index] : m_blockman.m_block_index) {
- if ((block_index.nStatus & BLOCK_FAILED_MASK) && (block_index.GetAncestor(nHeight) == pindex || pindex->GetAncestor(block_index.nHeight) == &block_index)) {
- block_index.nStatus &= ~BLOCK_FAILED_MASK;
+ if ((block_index.nStatus & BLOCK_FAILED_VALID) && (block_index.GetAncestor(nHeight) == pindex || pindex->GetAncestor(block_index.nHeight) == &block_index)) {
+ block_index.nStatus &= ~BLOCK_FAILED_VALID;
m_blockman.m_dirty_blockindex.insert(&block_index);
if (block_index.IsValid(BLOCK_VALID_TRANSACTIONS) && block_index.HaveNumChainTxs() && setBlockIndexCandidates.value_comp()(m_chain.Tip(), &block_index)) {
setBlockIndexCandidates.insert(&block_index);
@@ -4237,7 +4237,7 @@ bool ChainstateManager::AcceptBlockHeader(const CBlockHeader& block, BlockValida
CBlockIndex* pindex = &(miSelf->second);
if (ppindex)
*ppindex = pindex;
- if (pindex->nStatus & BLOCK_FAILED_MASK) {
+ if (pindex->nStatus & BLOCK_FAILED_VALID) {
LogDebug(BCLog::VALIDATION, "%s: block %s is marked invalid\n", __func__, hash.ToString());
return state.Invalid(BlockValidationResult::BLOCK_CACHED_INVALID, "duplicate-invalid",
strprintf("block %s was previously marked invalid", hash.ToString()));
@@ -4258,7 +4258,7 @@ bool ChainstateManager::AcceptBlockHeader(const CBlockHeader& block, BlockValida
return state.Invalid(BlockValidationResult::BLOCK_MISSING_PREV, "prev-blk-not-found");
}
pindexPrev = &((*mi).second);
- if (pindexPrev->nStatus & BLOCK_FAILED_MASK) {
+ if (pindexPrev->nStatus & BLOCK_FAILED_VALID) {
LogDebug(BCLog::VALIDATION, "header %s has prev block invalid: %s\n", hash.ToString(), block.hashPrevBlock.ToString());
return state.Invalid(BlockValidationResult::BLOCK_INVALID_PREV, "bad-prevblk");
}
@@ -4952,7 +4952,7 @@ bool ChainstateManager::LoadBlockIndex()
chainstate->TryAddBlockIndexCandidate(pindex);
}
}
- if (pindex->nStatus & BLOCK_FAILED_MASK && (!m_best_invalid || pindex->nChainWork > m_best_invalid->nChainWork)) {
+ if (pindex->nStatus & BLOCK_FAILED_VALID && (!m_best_invalid || pindex->nChainWork > m_best_invalid->nChainWork)) {
m_best_invalid = pindex;
}
if (pindex->IsValid(BLOCK_VALID_TREE) && (m_best_header == nullptr || CBlockIndexWorkComparator()(m_best_header, pindex)))
@@ -5192,7 +5192,7 @@ void ChainstateManager::CheckBlockIndex() const
// are not yet validated.
CChain best_hdr_chain;
assert(m_best_header);
- assert(!(m_best_header->nStatus & BLOCK_FAILED_MASK));
+ assert(!(m_best_header->nStatus & BLOCK_FAILED_VALID));
best_hdr_chain.SetTip(*m_best_header);
std::multimap<const CBlockIndex*, const CBlockIndex*> forward;
@@ -5307,9 +5307,9 @@ void ChainstateManager::CheckBlockIndex() const
if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_SCRIPTS) assert(pindexFirstNotScriptsValid == nullptr); // SCRIPTS valid implies all parents are SCRIPTS valid
if (pindexFirstInvalid == nullptr) {
// Checks for not-invalid blocks.
- assert((pindex->nStatus & BLOCK_FAILED_MASK) == 0); // The failed mask cannot be set for blocks without invalid parents.
+ assert((pindex->nStatus & BLOCK_FAILED_VALID) == 0); // The failed flag cannot be set for blocks without invalid parents.
} else {
- assert(pindex->nStatus & BLOCK_FAILED_MASK); // Invalid blocks and their descendants must be marked as invalid
+ assert(pindex->nStatus & BLOCK_FAILED_VALID); // Invalid blocks and their descendants must be marked as invalid
}
// Make sure m_chain_tx_count sum is correctly computed.
if (!pindex->pprev) {
@@ -5324,7 +5324,7 @@ void ChainstateManager::CheckBlockIndex() const
assert((pindex->m_chain_tx_count != 0) == (pindex == snap_base));
}
// There should be no block with more work than m_best_header, unless it's known to be invalid
- assert((pindex->nStatus & BLOCK_FAILED_MASK) || pindex->nChainWork <= m_best_header->nChainWork);
+ assert((pindex->nStatus & BLOCK_FAILED_VALID) || pindex->nChainWork <= m_best_header->nChainWork);
// Chainstate-specific checks on setBlockIndexCandidates
for (const auto& c : m_chainstates) {
@@ -5640,7 +5640,7 @@ util::Result<CBlockIndex*> ChainstateManager::ActivateSnapshot(
base_blockhash.ToString()))};
}
- bool start_block_invalid = snapshot_start_block->nStatus & BLOCK_FAILED_MASK;
+ bool start_block_invalid = snapshot_start_block->nStatus & BLOCK_FAILED_VALID;
if (start_block_invalid) {
return util::Error{Untranslated(strprintf("The base block header (%s) is part of an invalid chain", base_blockhash.ToString()))};
}
@@ -6285,7 +6285,7 @@ void ChainstateManager::RecalculateBestHeader()
AssertLockHeld(cs_main);
m_best_header = ActiveChain().Tip();
for (auto& entry : m_blockman.m_block_index) {
- if (!(entry.second.nStatus & BLOCK_FAILED_MASK) && m_best_header->nChainWork < entry.second.nChainWork) {
+ if (!(entry.second.nStatus & BLOCK_FAILED_VALID) && m_best_header->nChainWork < entry.second.nChainWork) {
m_best_header = &entry.second;
}
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.