Repository navigation
indexes: Save progress on early flushes - #36948
LakshyaBagani wants to merge 2 commits into
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste |
|
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? |
|
@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 |
|
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. |
edc5264 to
ea1b6cb
Compare
|
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
left a comment
There was a problem hiding this 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.
| return; | ||
| } | ||
|
|
||
| // The locator can be ahead of the index, because a flush during ConnectTip() is notified |
There was a problem hiding this comment.
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);

Since #34897, a flush in
ConnectTip()can report a block before the index receivesBlockConnectedfor it. The locator check inChainStateFlushed()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