Skip to content

indexes: Save progress on early flushes - #36948

Open
LakshyaBagani wants to merge 2 commits into
bitcoin:masterfrom
LakshyaBagani:issue-36939
Open

LakshyaBagani wants to merge 2 commits into
bitcoin:masterfrom
LakshyaBagani:issue-36939

Conversation

@LakshyaBagani

@LakshyaBagani LakshyaBagani commented Nov 22, 2026 •

Copy link
Copy Markdown

Since #34897, a flush in ConnectTip() can report a block before the index receives BlockConnected for it. The locator check in ChainStateFlushed() then skips saving the index tip. On a pruned assumeutxo node, repeated skips can leave the saved tip so far behind that the index needs blocks that were pruned by the time it restarts after background validation.

This PR uses Fabian Jahr’s two commits. The first adds a unit test that reproduces the skipped write. The second removes the locator check and updates the test to verify the saved tip after reopening the index. Commit() allows the write only if the index tip is at or is an ancestor of the chainstate’s last flushed block.

Fixes #36939

@DrahtBot

DrahtBot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK fjahr
Concept ACK l0rinc

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

@LakshyaBagani

Copy link
Copy Markdown
Author

Hi @mzumsande, @fjahr, and @l0rinc, could you take a look ?

Since the locator change in #34897, a flush can report a block before the index receives BlockConnected for it. Would you prefer letting the index save its current tip when the flushed block is ahead on the same chain, or changing the callback order in validation?

@fjahr

fjahr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

@LakshyaBagani I already developed a fix, just didn't get around to open the PR yet. You can see the branch here: https://github.com/fjahr/bitcoin/commits/2026-11-index-flush-commit/ I think that approach is better because Commit() already refuses to write unless the index's best block is an ancestor of the chainstate's last flushed block, so the locator check that you keep and even widen here can only block commits that are safe, and removing it leaves one rule in one place. If you want you maintain it you can pull my commits in here, otherwise I can open a separate PR.

@LakshyaBagani

Copy link
Copy Markdown
Author

Thanks for sharing your fix. I will bring both of your commits into this PR, credit you in the description, and follow up on reviews.

@fjahr

fjahr commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

ACK ea1b6cb

(Obviously not worth that much under the circumstances)

The CI currently doesn't run because you are a first time contributor @LakshyaBagani , I hope a maintainer can approve it soon.

@l0rinc l0rinc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

Commit() already checks whether the index tip is safe to save, so letting it handle a flush that arrives before its block notification fixes the skipped write.
It complements #37044, which fixes block filter and CoinStats restart and rewind problems after reorgs, while this one keeps the saved index tip from falling behind when a flush is notified first.
I like the characterization before the fix, but I'd prefer the test to use the real flush notification so the ordering is easy to follow.

Comment thread src/index/base.cpp
return;
}

// The locator can be ahead of the index, because a flush during ConnectTip() is notified

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ea1b6cb index: commit when the flush notification is ahead of the index:

We don't actually need the locator here anymore. Commit() already checks the index tip against the last flushed block, so the lookup doesn't affect whether the tip gets saved. The test can also use the real flush notification instead of calling ChainStateFlushed() directly. CreateAndProcessBlock() drains the queue while the index is unregistered, so registering it before ForceFlushStateToDisk() keeps the same ordering. The same test change works in the characterization commit too (where you could add a TODO to the incorrect line about to be changes i the next commit).

Example patch
diff --git a/src/index/base.cpp b/src/index/base.cpp
index eacff633eb..eb45d4a1a7 100644
--- a/src/index/base.cpp
+++ b/src/index/base.cpp
@@ -403,19 +403,6 @@ void BaseIndex::ChainStateFlushed(const ChainstateRole& role, const CBlockLocato
         return;
     }
 
-    const uint256& locator_tip_hash = locator.vHave.front();
-    const CBlockIndex* locator_tip_index;
-    {
-        LOCK(cs_main);
-        locator_tip_index = m_chainstate->m_blockman.LookupBlockIndex(locator_tip_hash);
-    }
-
-    if (!locator_tip_index) {
-        FatalErrorf("First block (hash=%s) in locator was not found",
-                   locator_tip_hash.ToString());
-        return;
-    }
-
     // The locator can be ahead of the index, because a flush during ConnectTip() is notified
     // before the BlockConnected notification for the same block. Commit() only writes the
     // index's own best block, and skips it if it is ahead of the flushed chainstate.
diff --git a/src/test/baseindex_tests.cpp b/src/test/baseindex_tests.cpp
index daf0c81505..c5f408e55f 100644
--- a/src/test/baseindex_tests.cpp
+++ b/src/test/baseindex_tests.cpp
@@ -129,17 +129,14 @@ BOOST_FIXTURE_TEST_CASE(baseindex_commit_when_flush_ahead_of_index, TestChain100
 
         // The index connects the next block, which isn't flushed yet.
         CreateAndProcessBlock({}, CScript() << OP_TRUE);
-        signals.SyncWithValidationInterfaceQueue();
         BOOST_CHECK_EQUAL(index->GetSummary().best_block_height, tip_height + 1);
 
         // Connect and flush another block, and notify the index of the flush
         // before it has connected that block.
         signals.UnregisterValidationInterface(index.get());
         CreateAndProcessBlock({}, CScript() << OP_TRUE);
-        chainstate.ForceFlushStateToDisk();
-        signals.SyncWithValidationInterfaceQueue();
         signals.RegisterValidationInterface(index.get());
-        signals.ChainStateFlushed(ChainstateRole{}, WITH_LOCK(cs_main, return GetLocator(chainstate.m_chain.Tip())));
+        chainstate.ForceFlushStateToDisk();
         signals.SyncWithValidationInterfaceQueue();
         BOOST_CHECK_EQUAL(index->GetSummary().best_block_height, tip_height + 1);
 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pruned node with blockfilterindex or coinstatsindex crashes when assumeutxo background validation completes (v32.1)

4 participants