Skip to content

Commit 9f2318c

Browse files
committed
validation: MaybeRebalanceCaches when chain leaves IBD
Check to see if we need to rebalance caches across chainstates when a chain leaves IBD.
1 parent 434495a commit 9f2318c

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/validation.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3192,6 +3192,7 @@ bool Chainstate::ActivateBestChain(BlockValidationState& state, std::shared_ptr<
31923192

31933193
CBlockIndex *pindexMostWork = nullptr;
31943194
CBlockIndex *pindexNewTip = nullptr;
3195+
bool exited_ibd{false};
31953196
do {
31963197
// Block until the validation queue drains. This should largely
31973198
// never happen in normal operation, however may happen during
@@ -3205,6 +3206,7 @@ bool Chainstate::ActivateBestChain(BlockValidationState& state, std::shared_ptr<
32053206
LOCK(cs_main);
32063207
// Lock transaction pool for at least as long as it takes for connectTrace to be consumed
32073208
LOCK(MempoolMutex());
3209+
const bool was_in_ibd = m_chainman.IsInitialBlockDownload();
32083210
CBlockIndex* starting_tip = m_chain.Tip();
32093211
bool blocks_connected = false;
32103212
do {
@@ -3252,16 +3254,21 @@ bool Chainstate::ActivateBestChain(BlockValidationState& state, std::shared_ptr<
32523254
if (!blocks_connected) return true;
32533255

32543256
const CBlockIndex* pindexFork = m_chain.FindFork(starting_tip);
3255-
bool fInitialDownload = m_chainman.IsInitialBlockDownload();
3257+
bool still_in_ibd = m_chainman.IsInitialBlockDownload();
3258+
3259+
if (was_in_ibd && !still_in_ibd) {
3260+
// Active chainstate has exited IBD.
3261+
exited_ibd = true;
3262+
}
32563263

32573264
// Notify external listeners about the new tip.
32583265
// Enqueue while holding cs_main to ensure that UpdatedBlockTip is called in the order in which blocks are connected
32593266
if (pindexFork != pindexNewTip) {
32603267
// Notify ValidationInterface subscribers
3261-
GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload);
3268+
GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, still_in_ibd);
32623269

32633270
// Always notify the UI if a new block tip was connected
3264-
if (kernel::IsInterrupted(m_chainman.GetNotifications().blockTip(GetSynchronizationState(fInitialDownload), *pindexNewTip))) {
3271+
if (kernel::IsInterrupted(m_chainman.GetNotifications().blockTip(GetSynchronizationState(still_in_ibd), *pindexNewTip))) {
32653272
// Just breaking and returning success for now. This could
32663273
// be changed to bubble up the kernel::Interrupted value to
32673274
// the caller so the caller could distinguish between
@@ -3272,6 +3279,13 @@ bool Chainstate::ActivateBestChain(BlockValidationState& state, std::shared_ptr<
32723279
}
32733280
// When we reach this point, we switched to a new tip (stored in pindexNewTip).
32743281

3282+
if (exited_ibd) {
3283+
// If a background chainstate is in use, we may need to rebalance our
3284+
// allocation of caches once a chainstate exits initial block download.
3285+
LOCK(::cs_main);
3286+
m_chainman.MaybeRebalanceCaches();
3287+
}
3288+
32753289
if (WITH_LOCK(::cs_main, return m_disabled)) {
32763290
// Background chainstate has reached the snapshot base block, so exit.
32773291
break;

0 commit comments

Comments
 (0)