Skip to content

Commit 1771866

Browse files
TheBlueMattstratospher
authored andcommitted
validation: clarify final |= BLOCK_FAILED_VALID in InvalidateBlock
This has no functional affect, as the any CBlockIndex*s which to_mark_failed is set to will already have been marked failed. Also prevents a situation where block already marked as BLOCK_FAILED_CHILD is again unconditionally marked as BLOCK_FAILED_VALID in the final |= BLOCK_FAILED_VALID.
1 parent 6c4f03c commit 1771866

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/test/blockchain_tests.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ BOOST_FIXTURE_TEST_CASE(invalidate_block, TestChain100Setup)
147147
WITH_LOCK(::cs_main, assert(pindex->nStatus & BLOCK_FAILED_CHILD));
148148
pindex = pindex->pprev;
149149
}
150+
151+
// don't mark already invalidated block (orig_tip is BLOCK_FAILED_CHILD) with BLOCK_FAILED_VALID again
152+
m_node.chainman->ActiveChainstate().InvalidateBlock(state, orig_tip);
153+
WITH_LOCK(::cs_main, assert(orig_tip->nStatus & BLOCK_FAILED_CHILD));
154+
WITH_LOCK(::cs_main, assert((orig_tip->nStatus & BLOCK_FAILED_VALID) == 0));
150155
}
151156

152157
BOOST_AUTO_TEST_SUITE_END()

src/validation.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -3779,11 +3779,13 @@ bool Chainstate::InvalidateBlock(BlockValidationState& state, CBlockIndex* pinde
37793779
return false;
37803780
}
37813781

3782-
// Mark pindex (or the last disconnected block) as invalid, even when it never was in the main chain
3783-
to_mark_failed->nStatus |= BLOCK_FAILED_VALID;
3784-
m_blockman.m_dirty_blockindex.insert(to_mark_failed);
3785-
setBlockIndexCandidates.erase(to_mark_failed);
3786-
m_chainman.m_failed_blocks.insert(to_mark_failed);
3782+
// Mark pindex as invalid if it never was in the main chain
3783+
if (!pindex_was_in_chain && !(pindex->nStatus & BLOCK_FAILED_MASK)) {
3784+
pindex->nStatus |= BLOCK_FAILED_VALID;
3785+
m_blockman.m_dirty_blockindex.insert(pindex);
3786+
setBlockIndexCandidates.erase(pindex);
3787+
m_chainman.m_failed_blocks.insert(pindex);
3788+
}
37873789

37883790
// If any new blocks somehow arrived while we were disconnecting
37893791
// (above), then the pre-calculation of what should go into

0 commit comments

Comments
 (0)