refactor: consolidate 3 separate locks into one block

The main lock needs to be taken 3 separate times.
It simplifies readability to take it once, and is more efficient.
This commit is contained in:
Andrew Toth
2025-05-04 13:35:29 -04:00
parent 41479ed1d2
commit c1e554d3e5

View File

@@ -3560,19 +3560,24 @@ bool Chainstate::ActivateBestChain(BlockValidationState& state, std::shared_ptr<
} // release cs_main
// When we reach this point, we switched to a new tip (stored in pindexNewTip).
if (exited_ibd) {
// If a background chainstate is in use, we may need to rebalance our
// allocation of caches once a chainstate exits initial block download.
LOCK(::cs_main);
m_chainman.MaybeRebalanceCaches();
bool disabled{false};
{
LOCK(m_chainman.GetMutex());
if (exited_ibd) {
// If a background chainstate is in use, we may need to rebalance our
// allocation of caches once a chainstate exits initial block download.
m_chainman.MaybeRebalanceCaches();
}
// Write changes periodically to disk, after relay.
if (!FlushStateToDisk(state, FlushStateMode::PERIODIC)) {
return false;
}
disabled = m_disabled;
}
// Write changes periodically to disk, after relay.
if (!FlushStateToDisk(state, FlushStateMode::PERIODIC)) {
return false;
}
if (WITH_LOCK(::cs_main, return m_disabled)) {
if (disabled) {
// Background chainstate has reached the snapshot base block, so exit.
// Restart indexes to resume indexing for all blocks unique to the snapshot