Changeset View
Changeset View
Standalone View
Standalone View
src/test/util/chainstate.h
Show First 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | static bool CreateAndActivateUTXOSnapshot(TestingSetup *fixture, | ||||
FILE *infile{fsbridge::fopen(snapshot_path, "rb")}; | FILE *infile{fsbridge::fopen(snapshot_path, "rb")}; | ||||
AutoFile auto_infile{infile}; | AutoFile auto_infile{infile}; | ||||
node::SnapshotMetadata metadata; | node::SnapshotMetadata metadata; | ||||
auto_infile >> metadata; | auto_infile >> metadata; | ||||
malleation(auto_infile, metadata); | malleation(auto_infile, metadata); | ||||
if (reset_chainstate) { | if (reset_chainstate) { | ||||
// Disable CheckBlockIndex until we are finished resetting the | |||||
// chainstate. It is enabled again and CheckBlockIndex is | |||||
// called manually after the snapshot is activated. | |||||
// This is necessary for Bitcoin ABC because, as of D4717, | |||||
// CheckBlockIndex is called more aggressively in ActivateBestChain and | |||||
// would not support resetting the chainstate while preserving the | |||||
// block index. | |||||
bool prev_check_block_index = fCheckBlockIndex; | |||||
fCheckBlockIndex = false; | |||||
{ | { | ||||
// What follows is code to selectively reset chainstate data without | // What follows is code to selectively reset chainstate data without | ||||
// disturbing the existing BlockManager instance, which is needed to | // disturbing the existing BlockManager instance, which is needed to | ||||
// recognize the headers chain previously generated by the | // recognize the headers chain previously generated by the | ||||
// chainstate we're removing. Without those headers, we can't | // chainstate we're removing. Without those headers, we can't | ||||
// activate the snapshot below. | // activate the snapshot below. | ||||
// | // | ||||
// This is a stripped-down version of node::LoadChainstate which | // This is a stripped-down version of node::LoadChainstate which | ||||
Show All 11 Lines | if (reset_chainstate) { | ||||
chain.InitCoinsCache(1 << 20); | chain.InitCoinsCache(1 << 20); | ||||
chain.CoinsTip().SetBestBlock(gen_hash); | chain.CoinsTip().SetBestBlock(gen_hash); | ||||
chain.setBlockIndexCandidates.insert( | chain.setBlockIndexCandidates.insert( | ||||
node.chainman->m_blockman.LookupBlockIndex(gen_hash)); | node.chainman->m_blockman.LookupBlockIndex(gen_hash)); | ||||
chain.LoadChainTip(); | chain.LoadChainTip(); | ||||
node.chainman->MaybeRebalanceCaches(); | node.chainman->MaybeRebalanceCaches(); | ||||
} | } | ||||
BlockValidationState state; | BlockValidationState state; | ||||
if (!node.chainman->ActiveChainstate().ActivateBestChain(state)) { | // Skip checking the block index when calling ActivateBestChain, because | ||||
// as of D4717 CheckBlockIndex is called more aggressively and would not | |||||
// support resetting the chainstate while preserving the block index. | |||||
// We call CheckBlockIndex() explicitly below, after ActivateSnapshot. | |||||
if (!node.chainman->ActiveChainstate().ActivateBestChain( | |||||
state, /*pblock=*/nullptr, /*skip_checkblockindex=*/true)) { | |||||
throw std::runtime_error( | throw std::runtime_error( | ||||
strprintf("ActivateBestChain failed. (%s)", state.ToString())); | strprintf("ActivateBestChain failed. (%s)", state.ToString())); | ||||
} | } | ||||
fCheckBlockIndex = prev_check_block_index; | |||||
Assert(0 == WITH_LOCK(node.chainman->GetMutex(), | Assert(0 == WITH_LOCK(node.chainman->GetMutex(), | ||||
return node.chainman->ActiveHeight())); | return node.chainman->ActiveHeight())); | ||||
} | } | ||||
bool ret = node.chainman->ActivateSnapshot(auto_infile, metadata, | bool ret = node.chainman->ActivateSnapshot(auto_infile, metadata, | ||||
in_memory_chainstate); | in_memory_chainstate); | ||||
if (fCheckBlockIndex) { | |||||
node.chainman->ActiveChainstate().CheckBlockIndex(); | node.chainman->ActiveChainstate().CheckBlockIndex(); | ||||
} | |||||
return ret; | return ret; | ||||
} | } | ||||
#endif // BITCOIN_TEST_UTIL_CHAINSTATE_H | #endif // BITCOIN_TEST_UTIL_CHAINSTATE_H |