Skip to content

Commit 3cfc753

Browse files
committed
test: Clear block index flags when testing snapshots
When simulating a snapshot, remove the HAVE_DATA status for blocks below the snapshot height, to simulate never having downloaded them at all. This makes tests more realistic (and more closely match what will happen when using assumeutxo).
1 parent 272fbc3 commit 3cfc753

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/test/util/chainstate.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ CreateAndActivateUTXOSnapshot(
7171
// This is a stripped-down version of node::LoadChainstate which
7272
// preserves the block index.
7373
LOCK(::cs_main);
74+
CBlockIndex *orig_tip = node.chainman->ActiveChainstate().m_chain.Tip();
7475
uint256 gen_hash = node.chainman->ActiveChainstate().m_chain[0]->GetBlockHash();
7576
node.chainman->ResetChainstates();
7677
node.chainman->InitializeChainstate(node.mempool.get());
@@ -83,6 +84,22 @@ CreateAndActivateUTXOSnapshot(
8384
chain.setBlockIndexCandidates.insert(node.chainman->m_blockman.LookupBlockIndex(gen_hash));
8485
chain.LoadChainTip();
8586
node.chainman->MaybeRebalanceCaches();
87+
88+
// Reset the HAVE_DATA flags below the snapshot height, simulating
89+
// never-having-downloaded them in the first place.
90+
// TODO: perhaps we could improve this by using pruning to delete
91+
// these blocks instead
92+
CBlockIndex *pindex = orig_tip;
93+
while (pindex && pindex != chain.m_chain.Tip()) {
94+
pindex->nStatus &= ~BLOCK_HAVE_DATA;
95+
pindex->nStatus &= ~BLOCK_HAVE_UNDO;
96+
// We have to set the ASSUMED_VALID flag, because otherwise it
97+
// would not be possible to have a block index entry without HAVE_DATA
98+
// and with nTx > 0 (since we aren't setting the pruned flag);
99+
// see CheckBlockIndex().
100+
pindex->nStatus |= BLOCK_ASSUMED_VALID;
101+
pindex = pindex->pprev;
102+
}
86103
}
87104
BlockValidationState state;
88105
if (!node.chainman->ActiveChainstate().ActivateBestChain(state)) {

0 commit comments

Comments
 (0)