indexes: Save progress on early flushes #36948

Open
LakshyaBagani wants to merge 2 commits from issue-36939 into master AGit
LakshyaBagani commented 2026-11-22 20:19:09 +00:00 (Migrated from github.com)

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

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 commented 2026-11-22 20:19:18 +00:00 (Migrated from github.com)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36948 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36948.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36948#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [fjahr](https://github.com/bitcoin/bitcoin/pull/36948#issuecomment-7133334543) | | Concept ACK | [l0rinc](https://github.com/bitcoin/bitcoin/pull/36948#pullrequestreview-6193062233) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--5faf32d7da4f0f540f40219e4f7537a3-->
LakshyaBagani commented 2026-11-22 20:22:18 +00:00 (Migrated from github.com)

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?

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?
First-time contributor

🔵 Suggestion · The flush-ahead test misses cross-branch commit rejection

The same-chain commit tests would still pass with a height-only Commit() guard, so they do not verify the cross-branch rejection now relied on after removing the callback guard.

Location: src/test/baseindex_tests.cpp:115 (head).

Review coverage was incomplete.


Reviewed: ea1b6cba295b · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:3705f06c6a27c475c77f2533c4d016ac03c829c839b7b538bba5e4768ccf54f1:1ed10c4979af6fc548d6b8d4534d505e02e84ac4b0aa4f8633555b2ba75119e8 --> <!-- ralph-base:19128e8b77553e4362fcbf3cb8f3f7dc5a2e4253 --> <!-- ralph-head:ea1b6cba295b3e23afda2643ab7460ef09cb3620 --> <!-- ralph-case:9099c1dd27e80a2db30318d7199048abfec0f894b59467fb1fe1bda76bb35bb1 --> **🔵 Suggestion · The flush-ahead test misses cross-branch commit rejection** The same-chain commit tests would still pass with a height-only `Commit()` guard, so they do not verify the cross-branch rejection now relied on after removing the callback guard. Location: [`src/test/baseindex_tests.cpp:115`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/ea1b6cba295b3e23afda2643ab7460ef09cb3620/src/test/baseindex_tests.cpp#L115>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`ea1b6cba295b`](<https://git.fish.foo/bitcoin/bitcoin/commit/ea1b6cba295b3e23afda2643ab7460ef09cb3620>) · [Review report](<https://review.fish.foo/report/36948-ea1b6cba295b3e23afda2643ab7460ef09cb3620-2944-2.html>) · [Previous reports](<https://review.fish.foo/traces/36948/>) [permalink](<https://review.fish.foo/report/36948-ea1b6cba295b3e23afda2643ab7460ef09cb3620-2944-2.html>)
fjahr commented 2026-11-22 20:51:15 +00:00 (Migrated from github.com)

@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 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 commented 2026-11-22 21:00:30 +00:00 (Migrated from github.com)

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.

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 commented 2026-11-24 13:47:12 +00:00 (Migrated from github.com)

ACK ea1b6cba29

(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.

ACK ea1b6cba295b3e23afda2643ab7460ef09cb3620 (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 (Migrated from github.com) requested changes 2026-12-04 17:09:21 +00:00
l0rinc (Migrated from github.com) left a comment

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.

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.
l0rinc (Migrated from github.com) commented 2026-12-04 17:07:08 +00:00

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);
 
> [ea1b6cb](https://github.com/bitcoin/bitcoin/pull/36948/changes/ea1b6cba295b3e23afda2643ab7460ef09cb3620) _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). <details><summary>Example patch</summary> ```diff 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); ``` </details>
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36948
No description provided.