Skip to content

Commit 9e29653

Browse files
committed
test: check BlockStatus when InvalidateBlock is used
when a block is invalidated using InvalidateBlock, check that: 1. it's status is BLOCK_FAILED_VALID 2. it's children's status is BLOCK_FAILED_CHILD and not BLOCK_FAILED_VALID 3. it's ancestors are valid
1 parent c996675 commit 9e29653

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/test/blockchain_tests.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,36 @@ BOOST_AUTO_TEST_CASE(num_chain_tx_max)
117117
BOOST_CHECK_EQUAL(block_index.m_chain_tx_count, std::numeric_limits<uint64_t>::max());
118118
}
119119

120+
BOOST_FIXTURE_TEST_CASE(invalidate_block, TestChain100Setup)
121+
{
122+
const CChain& active{*WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return &Assert(m_node.chainman)->ActiveChain())};
123+
124+
// Check BlockStatus when doing InvalidateBlock()
125+
BlockValidationState state;
126+
auto* orig_tip = active.Tip();
127+
int height_to_invalidate = orig_tip->nHeight - 10;
128+
auto* tip_to_invalidate = active[height_to_invalidate];
129+
m_node.chainman->ActiveChainstate().InvalidateBlock(state, tip_to_invalidate);
130+
131+
// tip_to_invalidate just got invalidated, so it's BLOCK_FAILED_VALID
132+
WITH_LOCK(::cs_main, assert(tip_to_invalidate->nStatus & BLOCK_FAILED_VALID));
133+
WITH_LOCK(::cs_main, assert((tip_to_invalidate->nStatus & BLOCK_FAILED_CHILD) == 0));
134+
135+
// check all ancestors of the invalidated block are validated up to BLOCK_VALID_TRANSACTIONS and are not invalid
136+
auto pindex = tip_to_invalidate->pprev;
137+
while (pindex) {
138+
WITH_LOCK(::cs_main, assert(pindex->IsValid(BLOCK_VALID_TRANSACTIONS)));
139+
WITH_LOCK(::cs_main, assert((pindex->nStatus & BLOCK_FAILED_MASK) == 0));
140+
pindex = pindex->pprev;
141+
}
142+
143+
// check all descendants of the invalidated block are BLOCK_FAILED_CHILD
144+
pindex = orig_tip;
145+
while (pindex && pindex != tip_to_invalidate) {
146+
WITH_LOCK(::cs_main, assert((pindex->nStatus & BLOCK_FAILED_VALID) == 0));
147+
WITH_LOCK(::cs_main, assert(pindex->nStatus & BLOCK_FAILED_CHILD));
148+
pindex = pindex->pprev;
149+
}
150+
}
151+
120152
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)