Skip to content

indexes: Don't commit ahead of the flushed chainstate - #34897

Merged
achow101 merged 5 commits into
bitcoin:masterfrom
mzumsande:202603_index_sync_dont_commit_ahead
Jul 9, 2026
Merged

achow101 merged 5 commits into
bitcoin:masterfrom
mzumsande:202603_index_sync_dont_commit_ahead

Conversation

@mzumsande

@mzumsande mzumsande commented Mar 23, 2026 •

Copy link
Copy Markdown
Contributor

If indexes commit their data ahead of the flushed chainstate, and there is an unclean shutdown, the index will be corrupted. This is especially the case for the coinstatsindex, which has state (the muhash) which can't easily be rolled back without access to the blocks. This was only partly fixed in #33212 (for reorg scenarios) but could still happen during initial sync.

Fix this more thoroughly by having the node keep track of the last flushed block, and skipping index commits if the current block of the index is not an ancestor of the node's last flushed block (similar to the suggestion by stickies-v in #33212 (review).

Fixes #33208
Fixes #34261

@DrahtBot

DrahtBot commented Mar 23, 2026 •

Copy link
Copy Markdown
Contributor

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

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/34897.

Reviews

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

Type Reviewers
ACK sedited, fjahr, achow101
Concept ACK l0rinc, arejula27
Stale ACK furszy, ryanofsky

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #35570 (refactor: Change some validation.cpp methods to return BlockValidationState by optout21)
  • #35465 (coins: compact chainstate regularly by l0rinc)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

LLM Linter (✨ experimental)

Possible places where named args for integral literals may be used (e.g. func(x, /*named_arg=*/0) in C++, and func(x, named_arg=0) in Python):

  • sync_index(false, 100, 100) in src/test/baseindex_tests.cpp
  • sync_index(false, 101, 100) in src/test/baseindex_tests.cpp

2026-07-09 14:22:11

@fjahr

fjahr commented Mar 23, 2026

Copy link
Copy Markdown
Contributor

Concept ACK

@l0rinc

l0rinc commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

I have reported something similar in #34489 (comment) and #34489 (comment) - will this PR fix those?
Concept ACK regardless, thanks for fixing them!

@arejula27 arejula27 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 [5af4d6e]

However, I'm concerned about a potential starvation scenario during IBD. Two threads run in parallel: the validation thread connects blocks and periodically flushes the UTXO set to disk (~1h intervals or when the cache is full), while the index thread (Sync()) processes already-connected blocks and calls Commit() every 30 seconds. Since index processing (filters, tx lookups) is significantly lighter than UTXO set updates, the index will typically stay ahead of the flush point, even though it's always behind the chain tip.

This means that during IBD, every Commit() call could be skipped because the index is perpetually ahead of m_last_flushed_block. A crash at any point would lose all indexing progress back to the initial LoadChainTip value, potentially hours of work. I will run experiments to verify whether this actually happens and propose a solution in that case.

I also suggest discussing an alternative approach for index commits: committing the locator up to the flushed block instead of skipping entirely. For example, if the index has processed up to block 5000 but the last flush was at block 3000, the locator could be written at 3000. This would preserve the safety invariant (locator never exceeds the flush point) while still persisting progress. That said, this would require rethinking how Commit() and CustomCommit() work across index subclasses.

This request also concerns me from a performance perspective: if index commit progress is now bounded by the chainstate flush interval, any index-side speed optimization becomes less impactful. PRs like #34489 (batch DB writes) and #26966 (parallelized index sync) focus on making indexes faster, but with this change the bottleneck shifts to how often the chainstate flushes, something entirely outside the index's control. I would love to see those optimizations land in Core, so it would be worth ensuring this safety fix doesn't inadvertently cap their benefits.

Comment thread src/index/base.cpp Resolved
Comment thread src/test/coinstatsindex_tests.cpp Resolved
Comment thread src/index/base.cpp Resolved

@mzumsande mzumsande left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

However, I'm concerned about a potential starvation scenario during IBD. Two threads run in parallel: the validation thread connects blocks and periodically flushes the UTXO set to disk (~1h intervals or when the cache is full), while the index thread (Sync()) processes already-connected blocks and calls Commit() every 30 seconds. Since index processing (filters, tx lookups) is significantly lighter than UTXO set updates, the index will typically stay ahead of the flush point, even though it's always behind the chain tip.

The Sync thread runs only temporary, when the index is behind the chainstate (e.g. newly started on a node that was running without an index before). For example, if a new node starts IBD with an index, Sync will stop immediately and never do any work.
As soon as the index has caught up with the tip, the Sync thread is stopped and we use validation signals to keep on syncing. Once that happens, the index will commit with each ChainstateFlushed validation callback, and since validation signals are processed in order, I don't think its best block can ever be ahead of the flushed chainstate then.

So I don't think the problem you describe is an issue. Commits will only be skipped in the limited time span where the Sync thread is almost caught up, due to additional blocks being connected during the earlier sync. This should be a relatively short period. They won't be skipped if the index is catching up from far behind, or once the index has reached the tip.

This request also concerns me from a performance perspective

Commit doesn't do any significant work, this is being done in ProcessBlock(), which will write things to disk independently. Commit only writes the data required to know where we should pick up at the next start (including state data such as the current MuHash of the coinstatsindex:
If we don't Commit and have an unclean restart, we might have to do some work again in case of an unclean restart, if we do Commit ahead of the tip and have an unclean restart, the index is corrupted.

I'll address the other comments later.

@ryanofsky

Copy link
Copy Markdown
Contributor

Concept ACK 5af4d6e. Good to follow up on this issue.

I feel like this could skip adding the isBlockInFlushedChain method and access the chainstate directly since #24230 moves sync thread from the node to the index. But either way seems fine.

I also implemented a similar fix in 6537f96 from #24230 which has some differences like the check being done inside Sync() instead of Commit(). Not sure if the differences are significant though.

@furszy furszy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ACK 5af4d6e , left a few suggestions. Nothing blocking.

Comment thread src/validation.cpp Outdated
Comment on lines +2814 to +2816
// Flush the chainstate (which may refer to block index entries).
empty_cache ? CoinsTip().Flush() : CoinsTip().Sync();
m_last_flushed_block = m_chain.Tip();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Coming from the lack of m_chain usage in this function, I'm wondering if we should add a sanity-check here?

Assume(m_last_flushed_block->GetBlockHash() == CoinsTip().GetBestBlock());

or.. maybe directly use

m_last_flushed_block =  m_blockman.LookupBlockIndex(CoinsTip().GetBestBlock());

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.

489bb4a validation: track last flushed block:

Could this new ChainStateFlushed locator come from the recorded flushed block instead of rereading m_chain.Tip()?

diff --git a/src/validation.cpp b/src/validation.cpp
--- a/src/validation.cpp	(revision 30c92e346762ec5d046d81d46d08811ccd9e26b3)
+++ b/src/validation.cpp	(revision 2296c4e0101ee7de230478d0dd7323b04a849fe4)
@@ -2831,7 +2831,7 @@
     }
     if (full_flush_completed && m_chainman.m_options.signals) {
         // Update best block in wallet (so we can detect restored wallets).
-        m_chainman.m_options.signals->ChainStateFlushed(this->GetRole(), GetLocator(m_chain.Tip()));
+        m_chainman.m_options.signals->ChainStateFlushed(this->GetRole(), GetLocator(Assert(m_last_flushed_block)));
     }
     } catch (const std::runtime_error& e) {
         return FatalError(m_chainman.GetNotifications(), state, strprintf(_("System error while flushing: %s"), e.what()));

@mzumsande mzumsande Jun 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added the suggested sanity check, and reused m_last_flushed. Note that we did use m_chain for the ChainStateFlushed() notification there.
However, I just noticed that some unit tests (validation_chainstate_resize_caches) don't populate m_chain and fail with the Assume. For now I have fixed this by making it conditional to a tip existing - I'll look later into changing the test instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Still doesn't work. The Assume is also hit by the validation_chainstatemanager_tests which manually changes the tip to be lagging behind the CoinsTip()- this is a dirty hack to make AssumeUtxo testable in unit tests (Snapshots will only activate when they have more work than the tip...).
I have removed the Assume() for now. Changing it to CoinsTip() would work, but then we should probably also change the notification. Would you prefer that?

@l0rinc l0rinc Jun 25, 2026 •

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.

@furszy is probably correct, the cached boundary should reflect the coins view that was actually flushed. But the ChainStateFlushed() locator change I asked about should probably stay as-is.

I understand that during IBD we can't have reorgs, but during steady state DisconnectTip(), the temporary coins view is flushed back to pindexDelete->pprev before the active tip is updated, so m_chain.Tip() can still name the disconnected block while CoinsTip().GetBestBlock() already names its parent:

struct TestChain100SetupNoMempoolHeadroom : TestChain100Setup {
    TestChain100SetupNoMempoolHeadroom() : TestChain100Setup{ChainType::REGTEST, TestOpts{.extra_args = {"-maxmempool=0"}}} {}
};

BOOST_FIXTURE_TEST_CASE(flushed_boundary_uses_disconnected_coins_tip, TestChain100SetupNoMempoolHeadroom)
{
    Chainstate& chainstate{Assert(m_node.chainman)->ActiveChainstate()};

    const CBlockIndex* tip{Assert(WITH_LOCK(::cs_main, return chainstate.m_chain.Tip()))};
    const CBlockIndex* parent{tip->pprev};

    {
        LOCK2(m_node.chainman->GetMutex(), chainstate.MempoolMutex());
        chainstate.m_coinstip_cache_size_bytes = 0;
        BlockValidationState state{};
        BOOST_REQUIRE(chainstate.DisconnectTip(state, nullptr));
    }

    BOOST_CHECK_EQUAL(WITH_LOCK(::cs_main, return chainstate.m_chain.Tip()), parent);
    BOOST_CHECK_EQUAL(WITH_LOCK(::cs_main, return chainstate.GetLastFlushedBlock()), parent);
    m_node.validation_signals->SyncWithValidationInterfaceQueue();
}

The test fails currently because GetLastFlushedBlock() records the disconnected tip instead of its parent:

m_last_flushed_block = m_blockman.LookupBlockIndex(CoinsTip().GetBestBlock());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That is an interesting situation, which I think is a bug in master. So the issue is that we call FlushStateToDisk(state, FlushStateMode::IF_NEEDED)) in DisconnectTip() after disconnecting the coins, but before setting the new tip (the IF_NEEDED makes this relatively unlikely, but it is still possible).

But the ChainStateFlushed() locator change I asked about should probably stay as-is.

why? It gives the wrong locator for the notification in this situation.
I changed it to use CoinsTip().GetBestBlock() for the last flushed block and also added a commit at the end to no longer use m_chain.Tip() for the existing ChainStateFlushed notification.

Comment thread src/test/coinstatsindex_tests.cpp Resolved
Comment thread src/test/coinstatsindex_tests.cpp Resolved
Comment thread src/test/coinstatsindex_tests.cpp Resolved
Comment thread src/index/base.cpp Outdated
// Don't commit if the index is ahead of the chainstate's last flushed block
const CBlockIndex* index_tip = m_best_block_index.load();
if (index_tip && !m_chain->isBlockInFlushedChain(index_tip->GetBlockHash(), index_tip->nHeight)) {
LogInfo("Skipping commit, index is ahead of flushed chainstate");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: Could log where the index is at vs the chain height, so the diff gets logged.

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.

nit: Also LogInfo on every skip seems a bit excessive. Maybe make this a debug log?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added the heights. It should be quite rare though (only happens at the end of Sync), so I'm not sure if it's worth adding a new debug log category for indexes just for this.

Comment thread src/index/base.cpp Resolved
Comment thread src/index/base.cpp Resolved

@arejula27 arejula27 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.

ACK 5af4d6e

I ran the experiments I suggested in my previous Concept ACK.

The skip path does trigger, but the loss window is bounded by 30s, which is negligible compared to a multi-day IBD, so the changes are correct.

I wopudl encorague again to add a few more tests:

  • Reorg scenario: Verify behavior when the index is on one fork and the flushed chainstate is on another.
  • Recovery: Verify the index resumes from the last chainstate flush point. I think will be interesting to verify the corret initialization behaviour of isBlockInFlushedChain

@sedited

sedited commented May 29, 2026

Copy link
Copy Markdown
Contributor

@mzumsande There's a silent merge conflict here:

/home/drgrid/bitcoin/src/test/coinstatsindex_tests.cpp:154:72: error: too many arguments to function call, expected 2, have 3
  154 |             const CBlock block = this->CreateBlock({}, script_pub_key, chainstate);
      |                                  ~~~~~~~~~~~~~~~~~                     ^~~~~~~~~~
/home/drgrid/bitcoin/src/test/util/setup_common.h:153:12: note: 'CreateBlock' declared here
  153 |     CBlock CreateBlock(
      |            ^
  154 |         const std::vector<CMutableTransaction>& txns,
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  155 |         const CScript& scriptPubKey);
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~

@fanquake fanquake added this to the 32.0 milestone May 29, 2026

@fjahr fjahr 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.

ACK 5af4d6e

Would be ok for me to merge as-is but I think particularly @furszy 's comments here and here would be good to address. Maybe some of mine as well but I don't consider them strictly blocking and I would open a follow-up if they are not addressed. I will re-review quickly if you decide to address the open comments.

EDIT: Never mind, I guess you have to push anyway due to the silent merge conflict.

Comment thread src/validation.h Resolved
Comment thread src/index/base.cpp Outdated
// Don't commit if the index is ahead of the chainstate's last flushed block
const CBlockIndex* index_tip = m_best_block_index.load();
if (index_tip && !m_chain->isBlockInFlushedChain(index_tip->GetBlockHash(), index_tip->nHeight)) {
LogInfo("Skipping commit, index is ahead of flushed chainstate");

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.

nit: Also LogInfo on every skip seems a bit excessive. Maybe make this a debug log?

Comment thread src/node/interfaces.cpp Resolved
Comment thread src/index/base.cpp Resolved
Comment thread src/test/coinstatsindex_tests.cpp Resolved
Comment thread src/test/coinstatsindex_tests.cpp Resolved
@DrahtBot
DrahtBot requested a review from arejula27 May 29, 2026 22:09

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

Left a few more comments - given the other comments and the silent merge conflict.
It would also be good to confirm whether the same unsafe restart boundary applies to other indexes using eager write paths, such as BlockFilterIndex.

Comment thread src/test/coinstatsindex_tests.cpp Resolved
Comment thread src/test/coinstatsindex_tests.cpp Outdated
// flushed block. If it did, a subsequent unclean shutdown would corrupt
// the index, because during reverting it would require blocks that were
// never flushed to disk.
BOOST_FIXTURE_TEST_CASE(coinstatsindex_no_commit_ahead_of_flush, TestChain100Setup)

@l0rinc l0rinc May 31, 2026 •

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.

5af4d6e test: add test to ensure indexes dont commit too early:

This seems like a good example for #35260 - it would help me with the review to know what the state was before the change and which commit changes the assumption and how and where it changes it.
Maybe something like this documenting the state before the fix:

BOOST_FIXTURE_TEST_CASE(coinstatsindex_commits_ahead_of_chainstate_flush, TestChain100Setup)
{
    Chainstate& chainstate = Assert(m_node.chainman)->ActiveChainstate();
    BlockValidationState state;

    auto current_tip{[&] { return Assert(WITH_LOCK(::cs_main, return chainstate.m_chain.Tip())); }};
    auto coinsdb_best_is{[&](const CBlockIndex& block) {
        LOCK(::cs_main);
        BOOST_CHECK(chainstate.CoinsDB().GetBestBlock() == block.GetBlockHash());
    }};
    auto sync_fresh_index{[&](int expected_height) {
        CoinStatsIndex index{interfaces::MakeChain(m_node), /*n_cache_size=*/1_MiB, /*f_memory=*/false, /*f_wipe=*/true};
        BOOST_REQUIRE(index.Init());
        index.Sync();
        BOOST_CHECK_EQUAL(index.GetSummary().best_block_height, expected_height);
        index.Stop();
    }};
    auto read_persisted_index_height{[&] {
        CoinStatsIndex index{interfaces::MakeChain(m_node), /*n_cache_size=*/1_MiB};
        BOOST_REQUIRE(index.Init());
        const int height{index.GetSummary().best_block_height};
        index.Stop();
        return height;
    }};
    auto flush_and_commit_index{[&] {
        CoinStatsIndex index{interfaces::MakeChain(m_node), /*n_cache_size=*/1_MiB};
        BOOST_REQUIRE(index.Init());
        index.Sync();
        BOOST_REQUIRE(chainstate.FlushStateToDisk(state, FlushStateMode::FORCE_FLUSH));
        m_node.validation_signals->SyncWithValidationInterfaceQueue();
        index.Stop();
    }};
    // For one unflushed index tip, check the durable height before and after the chainstate catches up.
    auto check_unflushed_index_commit{[&](const CBlockIndex& index_tip, int expected_persisted_height) {
        sync_fresh_index(index_tip.nHeight);
        BOOST_CHECK_EQUAL(read_persisted_index_height(), expected_persisted_height);
        flush_and_commit_index();
        BOOST_CHECK_EQUAL(read_persisted_index_height(), index_tip.nHeight);
    }};

    // Start from a known durable chainstate boundary.
    BOOST_REQUIRE(chainstate.FlushStateToDisk(state, FlushStateMode::FORCE_FLUSH));
    auto* flushed_tip{current_tip()};
    coinsdb_best_is(*flushed_tip);

    // Straight-line case: connect one more block without flushing the chainstate.
    CreateAndProcessBlock({}, CScript() << OP_TRUE);
    auto* unflushed_tip{current_tip()};
    BOOST_REQUIRE_GT(unflushed_tip->nHeight, flushed_tip->nHeight);
    coinsdb_best_is(*flushed_tip);

    check_unflushed_index_commit(*unflushed_tip, /*expected_persisted_height=*/unflushed_tip->nHeight); // TODO: Persists ahead of the flushed chainstate.

    auto* reorg_flushed_tip{current_tip()};
    coinsdb_best_is(*reorg_flushed_tip);

    // Fork case: leave the flushed chainstate behind, then index a competing unflushed tip.
    BOOST_REQUIRE(chainstate.InvalidateBlock(state, reorg_flushed_tip));
    NodeClockContext clock_ctx{};
    clock_ctx += 1s;
    CreateAndProcessBlock({}, CScript() << OP_TRUE);
    clock_ctx += 1s;
    CreateAndProcessBlock({}, CScript() << OP_TRUE);

    auto* fork_tip{current_tip()};
    BOOST_REQUIRE_EQUAL(fork_tip->nHeight, reorg_flushed_tip->nHeight + 1);
    BOOST_CHECK(fork_tip->GetAncestor(reorg_flushed_tip->nHeight) != reorg_flushed_tip);
    coinsdb_best_is(*reorg_flushed_tip);

    check_unflushed_index_commit(*fork_tip, /*expected_persisted_height=*/fork_tip->nHeight); // TODO: Persists a fork from the flushed chainstate.
}

so that the commit fixing it would have a minimal diff of what it affects exactly:

diff --git a/src/test/coinstatsindex_tests.cpp b/src/test/coinstatsindex_tests.cpp
--- a/src/test/coinstatsindex_tests.cpp	(revision 7177975c1d50fd4e0164fa6093a06b68302f0115)
+++ b/src/test/coinstatsindex_tests.cpp	(revision e8197142a66c98e72d2f8c6f138f4e29a9d83b94)
@@ -124,7 +124,7 @@
     }
 }
 
-BOOST_FIXTURE_TEST_CASE(coinstatsindex_commits_ahead_of_chainstate_flush, TestChain100Setup)
+BOOST_FIXTURE_TEST_CASE(coinstatsindex_commit_is_limited_by_chainstate_flush, TestChain100Setup)
 {
     Chainstate& chainstate = Assert(m_node.chainman)->ActiveChainstate();
     BlockValidationState state;
@@ -175,7 +175,7 @@
     BOOST_REQUIRE_GT(unflushed_tip->nHeight, flushed_tip->nHeight);
     coinsdb_best_is(*flushed_tip);
 
-    check_unflushed_index_commit(*unflushed_tip, /*expected_persisted_height=*/unflushed_tip->nHeight); // TODO: Persists ahead of the flushed chainstate.
+    check_unflushed_index_commit(*unflushed_tip, /*expected_persisted_height=*/0);
 
     auto* reorg_flushed_tip{current_tip()};
     coinsdb_best_is(*reorg_flushed_tip);
@@ -193,7 +193,7 @@
     BOOST_CHECK(fork_tip->GetAncestor(reorg_flushed_tip->nHeight) != reorg_flushed_tip);
     coinsdb_best_is(*reorg_flushed_tip);
 
-    check_unflushed_index_commit(*fork_tip, /*expected_persisted_height=*/fork_tip->nHeight); // TODO: Persists a fork from the flushed chainstate.
+    check_unflushed_index_commit(*fork_tip, /*expected_persisted_height=*/0);
 }
 
 BOOST_AUTO_TEST_SUITE_END()

@mzumsande mzumsande Jun 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have tried this approach in the past, I prefer to add the full test (which can be cherry-picked on master to see the failure). Otherwise, it adds a lot of churn to check every comment etc. if it already made sense before, without the context of the fix. But I did rewrite the unit test, it should be very obvious how it fails on master.

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.

re: #34897 (comment)

Agree with l0rinc, there is no significant churn here and adding the test first would make the bugfix commit easier to understand, and avoid the need for reviewers to do manual work to verify the test meaningfully checks the changed behavior, when this could be checked by the "test ancestor commits" CI job.

My diff to confirm the test was:

--- b/src/test/coinstatsindex_tests.cpp
+++ a/src/test/coinstatsindex_tests.cpp
@@ -145,7 +145,7 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_no_commit_ahead_of_flush, TestChain100Set
     {
         CoinStatsIndex index{interfaces::MakeChain(m_node), /*n_cache_size=*/1_MiB};
         BOOST_REQUIRE(index.Init());
-        BOOST_CHECK_EQUAL(index.GetSummary().best_block_height, 100);
+        BOOST_CHECK_EQUAL(index.GetSummary().best_block_height, 0);
         index.Stop();
     }
 
@@ -182,7 +182,7 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_no_commit_ahead_of_flush, TestChain100Set
     {
         CoinStatsIndex index{interfaces::MakeChain(m_node), /*n_cache_size=*/1_MiB};
         BOOST_REQUIRE(index.Init());
-        BOOST_CHECK_EQUAL(index.GetSummary().best_block_height, 101);
+        BOOST_CHECK_EQUAL(index.GetSummary().best_block_height, 100);
         index.Stop();
     }
 }

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.

I have tried this approach in the past, I prefer to add the full test (which can be cherry-picked on master to see the failure). Otherwise, it adds a lot of churn to check every comment etc.

That's exactly what this technique is solving: you don't need to cherry-pick on master, because you're already proving that the fix makes you adjust the test for it to pass. The CI already proved that the previous version passed and that the modified version also passes with the fix - no need to cherry-pick back and forth.


Also, the current regression coverage primarily demonstrates that the index will not commit if it is ahead by height. Could we extend it with a same-height competing block case, so a height-only check would fail and the ancestry requirement is tested directly?
That would make it clear that the gate is not only "height <= flushed height", but "same block or ancestor of the flushed block".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok, I changed it. There is still churn: Now I had to separate out comments that refer to the last flushed block, since they make no sense in a commit before it was introduced. Whenever I will add things to the test, I will likely have to resolve conflicts since it's now spread over two commits. Also, ideally, one should not just have a succeeding texts but also add motivating comments in the original test why the behavior is not ideal and should be fixed (and then delete these comments when fixed), which would create even more churn.

So for the future I definitely prefer to have less churn in the git history and instead have reviewers cherry-pick (and, as a frequent reviewer myself, have absolutely no problem doing that).

Comment thread src/index/base.cpp Outdated
if (ok) {
// Don't commit if the index is ahead of the chainstate's last flushed block
const CBlockIndex* index_tip = m_best_block_index.load();
if (index_tip && !m_chain->isBlockInFlushedChain(index_tip->GetBlockHash(), index_tip->nHeight)) {

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.

7933e27 index: Don't commit ahead of the flushed chainstate:

coinstatsindex doesn't seem to be only index with a similar race, see: #34489 (comment)

Can we cover it in this PR?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

All indexes are affected by it because we don't have a special logic for indexes without state. I added it to the coinstatsindex file because that index has state and actually needs to rewind. I think it would make sense to add a base_index tests for generic functionality that affects all indexes (and run the test for all index types, or maybe pick a random one), but I think it would be better to do that in a follow-up because it should affect other existing tests too.

Comment thread src/test/coinstatsindex_tests.cpp Outdated
index.Stop();
}

// Reload the index and verify the committed state.

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.

Shouldn't we also test the reorg scenario?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I hope the updated test covers it.

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.

Not the same-height reorg, I still think those should be covered (and docs updated)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Can you explain more? Unless I’m missing something, this doesn’t seem like a practical scenario to me:

  1. same-height reorgs don’t really happen, outside of artificial situations such a preciousblock. You reorg to a better chain, and that chain is usually longer.
  2. Even for a reorg scenario to a longer chain, I think that the following would need to happen: The Sync() thread reaches the tip, then while the index thread pauses in between

    bitcoin/src/index/base.cpp

    Lines 228 to 231 in f0da26c

    if (!pindex_next) {
    SetBestBlockIndex(pindex);
    // No need to handle errors in Commit. See rationale above.
    Commit();
    both a reorg and a flush must happen in the main thread. This seems like a very artificial situation to me timing-wise, but it is also hard to replicate in a unit test, because we cannot just do the reorg before calling Sync() as we do in the additional block scenario:
    If the reorg happens while the index is syncing earlier block the index never notices it (it chooses the next block sync based on the current chain), if a reorg happens after Sync() finished and we are in notification mode we will never flush before also doing the reorg in the index because notifications are processed in order.

Comment thread src/index/base.cpp Outdated
if (ok) {
// Don't commit if the index is ahead of the chainstate's last flushed block
const CBlockIndex* index_tip = m_best_block_index.load();
if (index_tip && !m_chain->isBlockInFlushedChain(index_tip->GetBlockHash(), index_tip->nHeight)) {

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.

7933e27 index: Don't commit ahead of the flushed chainstate:

Since this is basically the only usage of this method, we should be able to tailor it more, to let the chain interface take just the candidate block hash instead of a hash and height pair from the caller:

Suggested change
if (index_tip && !m_chain->isBlockInFlushedChain(index_tip->GetBlockHash(), index_tip->nHeight)) {
if (!m_chain->isAncestorOfLastFlushedBlock(best_block_index->GetBlockHash())) {

calling:

bool isAncestorOfLastFlushedBlock(const uint256& block_hash) override
{
    LOCK(::cs_main);
    const CBlockIndex* block{chainman().m_blockman.LookupBlockIndex(block_hash)};
    const CBlockIndex* last_flushed{chainman().ValidatedChainstate().GetLastFlushedBlock()};
    return block && last_flushed && last_flushed->GetAncestor(block->nHeight) == block;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I took this change, seems like a simpler interface with just one parameter, and the added lookup should not be performance critical since commits are rare.

Comment thread src/interfaces/chain.h Resolved
Comment thread src/node/interfaces.cpp Resolved
Comment thread src/index/base.cpp Resolved
Comment thread src/index/base.cpp
@@ -272,7 +272,15 @@ bool BaseIndex::Commit()
// Don't commit anything if we haven't indexed any block yet
// (this could happen if init is interrupted).
bool ok = m_best_block_index != nullptr;

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.

7933e27 index: Don't commit ahead of the flushed chainstate:

This isn't a "real" nullptr check, so we could probably simplify the structure a bit by something like:

const CBlockIndex* best_block_index{m_best_block_index.load()};
if (!best_block_index) return true;

if (!m_chain->isAncestorOfLastFlushedBlock(best_block_index->GetBlockHash())) {
    return true;
}
...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this would change the logic, we currently return false if best_block_index is not set. This relates to above discussion about the (unused) return value.

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.

Yes, if we remove the return value we could apply this - which would simplify the logic, reuse, and indentation:

diff --git a/src/index/base.cpp b/src/index/base.cpp
index 0be591e7da..3ee70fdd8a 100644
--- a/src/index/base.cpp
+++ b/src/index/base.cpp
@@ -273,31 +273,29 @@ void BaseIndex::Sync()
     }
 }
 
-bool BaseIndex::Commit()
+void BaseIndex::Commit()
 {
     // Don't commit anything if we haven't indexed any block yet
     // (this could happen if init is interrupted).
-    bool ok = m_best_block_index != nullptr;
+    const CBlockIndex* index_tip{m_best_block_index.load()};
+    if (!index_tip) return;
+
+    // Don't commit if the index is ahead of the chainstate's last flushed block
+    if (!m_chain->isAncestorOfLastFlushedBlock(index_tip->GetBlockHash())) {
+        LogInfo("Skipping commit, index is ahead of flushed chainstate (index height %d, chain height %d)",
+                index_tip->nHeight, m_chain->getHeight().value_or(-1));
+        return;
+    }
+
+    CDBBatch batch(GetDB());
+    bool ok = CustomCommit(batch);
     if (ok) {
-        // Don't commit if the index is ahead of the chainstate's last flushed block
-        const CBlockIndex* index_tip = m_best_block_index.load();
-        if (!m_chain->isAncestorOfLastFlushedBlock(index_tip->GetBlockHash())) {
-            LogInfo("Skipping commit, index is ahead of flushed chainstate (index height %d, chain height %d)",
-                    index_tip->nHeight, m_chain->getHeight().value_or(-1));
-            return false;
-        }
-        CDBBatch batch(GetDB());
-        ok = CustomCommit(batch);
-        if (ok) {
-            GetDB().WriteBestBlock(batch, GetLocator(*m_chain, m_best_block_index.load()->GetBlockHash()));
-            GetDB().WriteBatch(batch);
-        }
+        GetDB().WriteBestBlock(batch, GetLocator(*m_chain, index_tip->GetBlockHash()));
+        GetDB().WriteBatch(batch);
     }
     if (!ok) {
         LogError("Failed to commit latest %s state", GetName());
-        return false;
     }
-    return true;
 }
 
 bool BaseIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip)
diff --git a/src/index/base.h b/src/index/base.h
index 4e4cd603c0..5f67b368ae 100644
--- a/src/index/base.h
+++ b/src/index/base.h
@@ -93,12 +93,11 @@ private:
     std::thread m_thread_sync;
     CThreadInterrupt m_interrupt;
 
-    /// Write the current index state (eg. chain block locator and subclass-specific items) to disk.
-    /// Will skip the commit if no block has been indexed yet or if the index's best block is
-    /// ahead of the chainstate's last flushed block. This avoids persisting state an unclean shutdown
-    /// could not roll back from. A later call commits when the chainstate has flushed far enough.
-    /// Returns whether a commit was done.
-    bool Commit();
+    /// Try to write the current index state (eg. chain block locator and subclass-specific items) to disk.
+    /// Returns without writing if no block has been indexed yet or if the index's best block is ahead of
+    /// the chainstate's last flushed block. This avoids persisting state an unclean shutdown could not roll
+    /// back from. A later call commits when the chainstate has flushed far enough.
+    void Commit();
 
     /// Loop over disconnected blocks and call CustomRemove.
     bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I did inline it, but didn't do any further refactors of the function. The suggestion above changes behavior such that we no longer log an error if m_best_block_index is nullptr. That change may be desirable, but I want to keep it out of the scope of this PR.

Comment thread src/validation.cpp Outdated
Comment on lines +2814 to +2816
// Flush the chainstate (which may refer to block index entries).
empty_cache ? CoinsTip().Flush() : CoinsTip().Sync();
m_last_flushed_block = m_chain.Tip();

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.

489bb4a validation: track last flushed block:

Could this new ChainStateFlushed locator come from the recorded flushed block instead of rereading m_chain.Tip()?

diff --git a/src/validation.cpp b/src/validation.cpp
--- a/src/validation.cpp	(revision 30c92e346762ec5d046d81d46d08811ccd9e26b3)
+++ b/src/validation.cpp	(revision 2296c4e0101ee7de230478d0dd7323b04a849fe4)
@@ -2831,7 +2831,7 @@
     }
     if (full_flush_completed && m_chainman.m_options.signals) {
         // Update best block in wallet (so we can detect restored wallets).
-        m_chainman.m_options.signals->ChainStateFlushed(this->GetRole(), GetLocator(m_chain.Tip()));
+        m_chainman.m_options.signals->ChainStateFlushed(this->GetRole(), GetLocator(Assert(m_last_flushed_block)));
     }
     } catch (const std::runtime_error& e) {
         return FatalError(m_chainman.GetNotifications(), state, strprintf(_("System error while flushing: %s"), e.what()));

@DrahtBot
DrahtBot requested a review from l0rinc May 31, 2026 22:28

@mzumsande mzumsande left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the delay, I am currently working on addressing all comments and will rebase/push a new version in a few days.

@mzumsande
mzumsande force-pushed the 202603_index_sync_dont_commit_ahead branch 2 times, most recently from 0f13529 to 37849c2 Compare June 8, 2026 18:54
@DrahtBot

DrahtBot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task macOS native: https://github.com/bitcoin/bitcoin/actions/runs/27158855154/job/80168666015
LLM reason (✨ experimental): CI failed because CTest crashed with a segmentation fault in validation_chainstate_tests (test validation_chainstate_resize_caches).

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this pull request Nov 3, 2026
The address, spent, timestamp and governance collateral indexes erase the
entries of rewound blocks in CustomRewind. Since bitcoin#34897 the rewound
locator is only committed once the new tip is an ancestor of the last flushed
block. If the chainstate's last flush is below the rewind target, the erasures
were persisted while the committed locator stayed above them, on the rewound
branch. After an unclean restart in which that branch wins again, the index
resumed from the old locator and never appended the erased blocks again. The
governance collateral index used to write its locator in the same batch as the
erasures; skipping it there opened the same gap.

When the committed locator is above the new tip on the same branch, move it to
the new tip. The committed tip was an ancestor of a flushed block when it was
written, so the new tip below it is in the block index after a restart. The
locator then matches what the index holds, whichever branch wins afterwards. A
committed locator at or below the new tip, or on another branch, is left alone:
a restart resumes or rewinds from it correctly. The governance collateral index
writes it in the same batch as its erasures, the others right after theirs, as
the full commit did before bitcoin#33212.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit dc631e5)
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this pull request Nov 3, 2026
The address, spent, timestamp and governance collateral indexes erase the
entries of rewound blocks in CustomRewind. Since bitcoin#34897 the rewound
locator is only committed once the new tip is an ancestor of the last flushed
block. If the chainstate's last flush is below the rewind target, the erasures
were persisted while the committed locator stayed above them, on the rewound
branch. After an unclean restart in which that branch wins again, the index
resumed from the old locator and never appended the erased blocks again. The
governance collateral index used to write its locator in the same batch as the
erasures; skipping it there opened the same gap.

When the committed locator is above the new tip on the same branch, move it to
the new tip. The committed tip was an ancestor of a flushed block when it was
written, so the new tip below it is in the block index after a restart. The
locator then matches what the index holds, whichever branch wins afterwards. A
committed locator at or below the new tip, or on another branch, is left alone:
a restart resumes or rewinds from it correctly. The governance collateral index
writes it in the same batch as its erasures, the others right after theirs, as
the full commit did before bitcoin#33212.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this pull request Nov 3, 2026
The address, spent, timestamp and governance collateral indexes erase the
entries of rewound blocks in CustomRewind. Since bitcoin#34897 the rewound
locator is only committed once the new tip is an ancestor of the last flushed
block. If the chainstate's last flush is below the rewind target, the erasures
were persisted while the committed locator stayed above them, on the rewound
branch. After an unclean restart in which that branch wins again, the index
resumed from the old locator and never appended the erased blocks again. The
governance collateral index used to write its locator in the same batch as the
erasures; skipping it there opened the same gap.

When the committed locator is above the new tip on the same branch, move it to
the new tip. The committed tip was an ancestor of a flushed block when it was
written, so the new tip below it is in the block index after a restart. The
locator then matches what the index holds, whichever branch wins afterwards. A
committed locator at or below the new tip, or on another branch, is left alone:
a restart resumes or rewinds from it correctly. The governance collateral index
writes it in the same batch as its erasures, the others right after theirs, as
the full commit did before bitcoin#33212.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 0abf95d)
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this pull request Nov 3, 2026
The address, spent, timestamp and governance collateral indexes erase the
entries of rewound blocks in CustomRewind. Since bitcoin#34897 the rewound
locator is only committed once the new tip is an ancestor of the last flushed
block. If the chainstate's last flush is below the rewind target, the erasures
were persisted while the committed locator stayed above them, on the rewound
branch. After an unclean restart in which that branch wins again, the index
resumed from the old locator and never appended the erased blocks again. The
governance collateral index used to write its locator in the same batch as the
erasures; skipping it there opened the same gap.

When the committed locator is above the new tip on the same branch, move it to
the new tip. The committed tip was an ancestor of a flushed block when it was
written, so the new tip below it is in the block index after a restart. The
locator then matches what the index holds, whichever branch wins afterwards. A
committed locator at or below the new tip, or on another branch, is left alone:
a restart resumes or rewinds from it correctly. The governance collateral index
writes it in the same batch as its erasures, the others right after theirs, as
the full commit did before bitcoin#33212.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this pull request Nov 3, 2026
bitcoin#33212 stopped BaseIndex::Rewind from committing, because the committed
locator could get ahead of the flushed chainstate. Upstream indexes don't erase
anything when they rewind, so the next ChainStateFlushed catches up safely. Dash's
address, spent and timestamp indexes erase the entries of the rewound blocks in
CustomRewind right away. If the node crashes after such a rewind but before the next
flush, the index restarts from the old locator, sees the same tip again and reports
itself synced without the erased entries (for example after invalidateblock).

With bitcoin#34897, Commit() only writes when the index tip is an ancestor of the
chainstate's last flushed block, so calling it again at the end of Rewind moves the
locator back whenever that is safe and skips it otherwise.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this pull request Nov 3, 2026
bitcoin#34897 announces the last flushed block in ChainStateFlushed. Upstream
only indexes listen to that signal, since bitcoin#30221 stopped the wallet from
storing its locator. Dash's wallet still writes the announced locator as its
best block. A flush inside ConnectTip() happens after the coins are updated but
before the block is part of m_chain and before its BlockConnected is queued, so
the wallet could record the block it is about to scan as the chain tip. A crash
in that window would skip the rescan on restart and miss the block's
transactions.

Announce the most recent flushed block that is part of the active chain. That
is the parent while ConnectTip() is connecting a block, which is what was
announced before bitcoin#34897, and the flushed parent after DisconnectTip(),
which keeps the upstream fix for that case.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit b3038a3)
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this pull request Nov 3, 2026
The address, spent, timestamp and governance collateral indexes erase the
entries of rewound blocks in CustomRewind. Since bitcoin#34897 the rewound
locator is only committed once the new tip is an ancestor of the last flushed
block. If the chainstate's last flush is below the rewind target, the erasures
were persisted while the committed locator stayed above them, on the rewound
branch. After an unclean restart in which that branch wins again, the index
resumed from the old locator and never appended the erased blocks again. The
governance collateral index used to write its locator in the same batch as the
erasures; skipping it there opened the same gap.

When the committed locator is above the new tip on the same branch, move it to
the new tip. The committed tip was an ancestor of a flushed block when it was
written, so the new tip below it is in the block index after a restart. The
locator then matches what the index holds, whichever branch wins afterwards. A
committed locator at or below the new tip, or on another branch, is left alone:
a restart resumes or rewinds from it correctly. The governance collateral index
writes it in the same batch as its erasures, the others right after theirs, as
the full commit did before bitcoin#33212.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 0abf95d)
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this pull request Nov 3, 2026
…d ahead of the flush

GovCollateralIndex::CustomRewind writes the rewound locator in the same batch as the
erased collaterals. Like the commit bitcoin#33212 removed from BaseIndex::Rewind, that
locator can name a block that is not in the on-disk block index yet. After an unclean
shutdown the index then fails BaseIndex::Init with "best block of the index not found",
and since this index is always on, the node does not start.

Write the locator only when the new tip is an ancestor of the chainstate's last flushed
block, the same condition bitcoin#34897 adds to BaseIndex::Commit().

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this pull request Nov 3, 2026
bitcoin#34897 announces the last flushed block in ChainStateFlushed. Upstream
only indexes listen to that signal, since bitcoin#30221 stopped the wallet from
storing its locator. Dash's wallet still writes the announced locator as its
best block. A flush inside ConnectTip() happens after the coins are updated but
before the block is part of m_chain and before its BlockConnected is queued, so
the wallet could record the block it is about to scan as the chain tip. A crash
in that window would skip the rescan on restart and miss the block's
transactions.

Announce the most recent flushed block that is part of the active chain. That
is the parent while ConnectTip() is connecting a block, which is what was
announced before bitcoin#34897, and the flushed parent after DisconnectTip(),
which keeps the upstream fix for that case.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this pull request Nov 3, 2026
The address, spent, timestamp and governance collateral indexes erase the
entries of rewound blocks in CustomRewind. Since bitcoin#33212 a rewind no
longer commits its locator, and since bitcoin#34897 a later commit only writes
it once the index tip is an ancestor of the last flushed block. If the
chainstate's last flush is below the rewind target, the erasures
were persisted while the committed locator stayed above them, on the rewound
branch. After an unclean restart in which that branch wins again, the index
resumed from the old locator and never appended the erased blocks again. The
governance collateral index used to write its locator in the same batch as the
erasures; skipping it there opened the same gap.

When the committed locator is above the new tip on the same branch, move it to
the new tip. The committed tip was an ancestor of a flushed block when it was
written, so the new tip below it is in the block index after a restart. The
locator then matches what the index holds, whichever branch wins afterwards. A
committed locator at or below the new tip, or on another branch, is left alone:
a restart resumes or rewinds from it correctly. The governance collateral index
writes it in the same batch as its erasures, the others right after theirs, as
the full commit did before bitcoin#33212. This also covers a rewind whose new
tip is already flushed, so BaseIndex::Rewind() stays as upstream left it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this pull request Nov 3, 2026
bitcoin#34897 announces the last flushed block in ChainStateFlushed. Upstream
only indexes listen to that signal, since bitcoin#30221 stopped the wallet from
storing its locator. Dash's wallet still writes the announced locator as its
best block. A flush inside ConnectTip() happens after the coins are updated but
before the block is part of m_chain and before its BlockConnected is queued, so
the wallet could record the block it is about to scan as the chain tip. A crash
in that window would skip the rescan on restart and miss the block's
transactions.

Announce the most recent flushed block that is part of the active chain. That
is the parent while ConnectTip() is connecting a block, which is what was
announced before bitcoin#34897, and the flushed parent after DisconnectTip(),
which keeps the upstream fix for that case.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit e8325a1)
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this pull request Nov 3, 2026
The address, spent, timestamp and governance collateral indexes erase the
entries of rewound blocks in CustomRewind. Since bitcoin#33212 a rewind no
longer commits its locator, and since bitcoin#34897 a later commit only writes
it once the index tip is an ancestor of the last flushed block. If the
chainstate's last flush is below the rewind target, the erasures
were persisted while the committed locator stayed above them, on the rewound
branch. After an unclean restart in which that branch wins again, the index
resumed from the old locator and never appended the erased blocks again. The
governance collateral index used to write its locator in the same batch as the
erasures; skipping it there opened the same gap.

When the committed locator is above the new tip on the same branch, move it to
the new tip. The committed tip was an ancestor of a flushed block when it was
written, so the new tip below it is in the block index after a restart. The
locator then matches what the index holds, whichever branch wins afterwards. A
committed locator at or below the new tip, or on another branch, is left alone:
a restart resumes or rewinds from it correctly. The governance collateral index
writes it in the same batch as its erasures, the others right after theirs, as
the full commit did before bitcoin#33212. This also covers a rewind whose new
tip is already flushed, so BaseIndex::Rewind() stays as upstream left it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 337c464)
knst added a commit to knst/dash that referenced this pull request Nov 3, 2026
…state

e9ed898 validation: Don't use m_chain.Tip() in FlushStateToDisk (Martin Zumsande)
3679f1e index: Don't commit ahead of the flushed chainstate (Martin Zumsande)
6573572 index: Remove return value from Commit() (Martin Zumsande)
09c0696 validation: track last flushed block (Martin Zumsande)
13c02b5 test: add test for index commits ahead of the last flushed block (Martin Zumsande)

Pull request description:

  If indexes commit their data ahead of the flushed chainstate, and there is an unclean shutdown, the index will be corrupted. This is especially the case for the coinstatsindex, which has state (the muhash) which can't easily be rolled back without access to the blocks. This was only partly fixed in bitcoin#33212 (for reorg scenarios) but could still happen during initial sync.

  Fix this more thoroughly by having the node keep track of the last flushed block, and skipping index commits if the current block of the index is not an ancestor of the node's last flushed block (similar to the suggestion by stickies-v in bitcoin#33212 (review).

  Fixes bitcoin#33208
  Fixes bitcoin#34261

ACKs for top commit:
  achow101:
    ACK e9ed898
  sedited:
    Re-ACK e9ed898
  fjahr:
    re-ACK e9ed898

Tree-SHA512: 7f4dc6fb942d6726587eb75dece24c79c679d8630320502aca9fa2d2b03b1d25999cd11255ec20344ebbb8985552747e2554e2557b9d2ad0c75db71652d615ab

BACKPORT NOTE:
- The last flushed block is recorded right after the coins flush; Dash then commits EvoDB in the same
  flush before ChainStateFlushed is sent.
- The skip message gets Dash's explicit trailing newline; Commit() keeps Dash's LogError form.
- src/test/chainstate_write_tests.cpp does not exist in Dash, so its two assertions are not applied.
- baseindex_tests: Init() and Sync() are not callable on their own in Dash (bitcoin#27607, bitcoin#28955
  are not backported), so the test starts the index and holds back the sync thread with
  g_indexes_ready_to_sync to read the committed best block.
knst added a commit to knst/dash that referenced this pull request Nov 3, 2026
The address, spent, timestamp and governance collateral indexes erase the
entries of rewound blocks in CustomRewind. Upstream indexes keep nothing a
rewind has to undo, so bitcoin#33212 could stop committing in Rewind and
bitcoin#34897 could leave the locator where it is until the chainstate has
flushed past the new tip. With erasures persisted right away, a locator that
stays above them is wrong after an unclean restart: if the rewound branch wins
again, Init() resumes from the old locator and the erased blocks are never
indexed again. The governance collateral index used to write the rewound
locator unconditionally, which keeps bitcoin#33208 (a locator naming a block
that is not in the on-disk block index) for an index that is always on.

When the committed locator is above the new tip on the same branch, move it to
the new tip in the rewind's own batch. The committed tip was an ancestor of a
flushed block when it was written, so the new tip below it is in the on-disk
block index after a restart. A committed locator at or below the new tip needs
no change: a restart re-appends from it.

The new baseindex_tests scenario uses a test index that erases on rewind the
way the Dash indexes do: it rewinds past the last flush, restarts and must
index the erased blocks again. It fails with WriteRewindLocator as a no-op.

Co-authored-by: pasta <[email protected]>
knst added a commit to knst/dash that referenced this pull request Nov 3, 2026
…state

BACKPORT NOTE:
- The last flushed block is recorded right after the coins flush; Dash then commits EvoDB in the same
  flush before ChainStateFlushed is sent.
- The skip message gets Dash's explicit trailing newline; Commit() keeps Dash's LogError form.
- src/test/chainstate_write_tests.cpp does not exist in Dash, so its two assertions are not applied.
- baseindex_tests: Init() and Sync() are not callable on their own in Dash (bitcoin#27607, bitcoin#28955
  are not backported), so the test starts the index and holds back the sync thread with
  g_indexes_ready_to_sync to read the committed best block.
----
e9ed898 validation: Don't use m_chain.Tip() in FlushStateToDisk (Martin Zumsande)
3679f1e index: Don't commit ahead of the flushed chainstate (Martin Zumsande)
6573572 index: Remove return value from Commit() (Martin Zumsande)
09c0696 validation: track last flushed block (Martin Zumsande)
13c02b5 test: add test for index commits ahead of the last flushed block (Martin Zumsande)

Pull request description:

  If indexes commit their data ahead of the flushed chainstate, and there is an unclean shutdown, the index will be corrupted. This is especially the case for the coinstatsindex, which has state (the muhash) which can't easily be rolled back without access to the blocks. This was only partly fixed in bitcoin#33212 (for reorg scenarios) but could still happen during initial sync.

  Fix this more thoroughly by having the node keep track of the last flushed block, and skipping index commits if the current block of the index is not an ancestor of the node's last flushed block (similar to the suggestion by stickies-v in bitcoin#33212 (review).

  Fixes bitcoin#33208
  Fixes bitcoin#34261

ACKs for top commit:
  achow101:
    ACK e9ed898
  sedited:
    Re-ACK e9ed898
  fjahr:
    re-ACK e9ed898

Tree-SHA512: 7f4dc6fb942d6726587eb75dece24c79c679d8630320502aca9fa2d2b03b1d25999cd11255ec20344ebbb8985552747e2554e2557b9d2ad0c75db71652d615ab
knst added a commit to knst/dash that referenced this pull request Nov 3, 2026
The address, spent, timestamp and governance collateral indexes erase the
entries of rewound blocks in CustomRewind. Upstream indexes keep nothing a
rewind has to undo, so bitcoin#33212 could stop committing in Rewind and
bitcoin#34897 could leave the locator where it is until the chainstate has
flushed past the new tip. With erasures persisted right away, a locator that
stays above them is wrong after an unclean restart: if the rewound branch wins
again, Init() resumes from the old locator and the erased blocks are never
indexed again. The governance collateral index used to write the rewound
locator unconditionally, which keeps bitcoin#33208 (a locator naming a block
that is not in the on-disk block index) for an index that is always on.

When the committed locator is above the new tip on the same branch, move it to
the new tip in the rewind's own batch. The committed tip was an ancestor of a
flushed block when it was written, so the new tip below it is in the on-disk
block index after a restart. A committed locator at or below the new tip needs
no change: a restart re-appends from it.

The new baseindex_tests scenario uses a test index that erases on rewind the
way the Dash indexes do: it rewinds past the last flush, restarts and must
index the erased blocks again. It fails with WriteRewindLocator as a no-op.

Co-authored-by: pasta <[email protected]>
PastaPastaPasta added a commit to knst/dash that referenced this pull request Nov 3, 2026
Dash's address, spent, timestamp and governance collateral indexes write a
block's entries as soon as they append it. Since bitcoin#34897, Commit() skips
the locator for a tip above the last flushed block, so a sync that ends above
the flush leaves entries on disk for blocks the locator doesn't cover. If the
node stops uncleanly and those blocks, already durable in the block index, are
reorged away before the index syncs again, Init() resumes from the older
locator and never rewinds them: the address index keeps the orphaned payment
in the history and the unspent outputs. Before bitcoin#34897 the end-of-sync
commit wrote the tip and the restart rewound it.

For these indexes a skipped commit now records the index tip under its own
key, and a successful commit erases it in the same batch as the locator.
Rewinds move it down with the locator. Init() resumes from it when the block
index has it above the locator, so the sync rewinds it if it left the active
chain, and drops it otherwise. The locator keeps the upstream meaning, so a
locator naming a block the block index lost is still a startup error, and a
recorded tip the block index lost falls back to the locator as before.

Blocks appended through BlockConnected after the sync are not covered: their
locator also waits for the next flush, as it did before bitcoin#34897.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this pull request Nov 4, 2026
…state

e9ed898 validation: Don't use m_chain.Tip() in FlushStateToDisk (Martin Zumsande)
3679f1e index: Don't commit ahead of the flushed chainstate (Martin Zumsande)
6573572 index: Remove return value from Commit() (Martin Zumsande)
09c0696 validation: track last flushed block (Martin Zumsande)
13c02b5 test: add test for index commits ahead of the last flushed block (Martin Zumsande)

Pull request description:

  If indexes commit their data ahead of the flushed chainstate, and there is an unclean shutdown, the index will be corrupted. This is especially the case for the coinstatsindex, which has state (the muhash) which can't easily be rolled back without access to the blocks. This was only partly fixed in bitcoin#33212 (for reorg scenarios) but could still happen during initial sync.

  Fix this more thoroughly by having the node keep track of the last flushed block, and skipping index commits if the current block of the index is not an ancestor of the node's last flushed block (similar to the suggestion by stickies-v in bitcoin#33212 (review).

  Fixes bitcoin#33208
  Fixes bitcoin#34261

ACKs for top commit:
  achow101:
    ACK e9ed898
  sedited:
    Re-ACK e9ed898
  fjahr:
    re-ACK e9ed898

Tree-SHA512: 7f4dc6fb942d6726587eb75dece24c79c679d8630320502aca9fa2d2b03b1d25999cd11255ec20344ebbb8985552747e2554e2557b9d2ad0c75db71652d615ab

BACKPORT NOTE:
- Commit() keeps Dash's existing LogError call; LogDebug becomes LogPrint(BCLog::COINDB, ...).
- In FlushStateToDisk the last flushed block is recorded right after the coins flush, as upstream does; Dash
  then commits EvoDB in the same flush before ChainStateFlushed is sent.
- src/test/chainstate_write_tests.cpp does not exist in Dash, so its two assertions are not ported.
- baseindex_tests uses Dash's index API: Init() and Sync() are private here, so the test starts the index
  and holds back the sync thread with g_indexes_ready_to_sync to read the committed best block.

(cherry picked from commit 582821e)
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this pull request Nov 4, 2026
bitcoin#34897 announces the last flushed block in ChainStateFlushed. Upstream
only indexes listen to that signal, since bitcoin#30221 stopped the wallet from
storing its locator. Dash's wallet still writes the announced locator as its
best block. A flush inside ConnectTip() happens after the coins are updated but
before the block is part of m_chain and before its BlockConnected is queued, so
the wallet could record the block it is about to scan as the chain tip. A crash
in that window would skip the rescan on restart and miss the block's
transactions.

Announce the most recent flushed block that is part of the active chain. That
is the parent while ConnectTip() is connecting a block, which is what was
announced before bitcoin#34897, and the flushed parent after DisconnectTip(),
which keeps the upstream fix for that case.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit e8325a1)
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this pull request Nov 4, 2026
The address, spent, timestamp and governance collateral indexes erase the
entries of rewound blocks in CustomRewind. Since bitcoin#33212 a rewind no
longer commits its locator, and since bitcoin#34897 a later commit only writes
it once the index tip is an ancestor of the last flushed block. If the
chainstate's last flush is below the rewind target, the erasures
were persisted while the committed locator stayed above them, on the rewound
branch. After an unclean restart in which that branch wins again, the index
resumed from the old locator and never appended the erased blocks again. The
governance collateral index used to write its locator in the same batch as the
erasures; skipping it there opened the same gap.

When the committed locator is above the new tip on the same branch, move it to
the new tip. The committed tip was an ancestor of a flushed block when it was
written, so the new tip below it is in the block index after a restart. The
locator then matches what the index holds, whichever branch wins afterwards. A
committed locator at or below the new tip, or on another branch, is left alone:
a restart resumes or rewinds from it correctly. The governance collateral index
writes it in the same batch as its erasures, the others right after theirs, as
the full commit did before bitcoin#33212. This also covers a rewind whose new
tip is already flushed, so BaseIndex::Rewind() stays as upstream left it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 337c464)
PastaPastaPasta pushed a commit to dashpay/dash that referenced this pull request Nov 4, 2026
…ed chainstate

BACKPORT NOTE:
- The last flushed block is recorded right after the coins flush; Dash then commits EvoDB in the same
  flush before ChainStateFlushed is sent.
- The skip message gets Dash's explicit trailing newline; Commit() keeps Dash's LogError form.
- src/test/chainstate_write_tests.cpp does not exist in Dash, so its two assertions are not applied.
- baseindex_tests: Init() and Sync() are not callable on their own in Dash (bitcoin#27607, bitcoin#28955
  are not backported), so the test starts the index and holds back the sync thread with
  g_indexes_ready_to_sync to read the committed best block.
----
e9ed898 validation: Don't use m_chain.Tip() in FlushStateToDisk (Martin Zumsande)
3679f1e index: Don't commit ahead of the flushed chainstate (Martin Zumsande)
6573572 index: Remove return value from Commit() (Martin Zumsande)
09c0696 validation: track last flushed block (Martin Zumsande)
13c02b5 test: add test for index commits ahead of the last flushed block (Martin Zumsande)

Pull request description:

  If indexes commit their data ahead of the flushed chainstate, and there is an unclean shutdown, the index will be corrupted. This is especially the case for the coinstatsindex, which has state (the muhash) which can't easily be rolled back without access to the blocks. This was only partly fixed in bitcoin#33212 (for reorg scenarios) but could still happen during initial sync.

  Fix this more thoroughly by having the node keep track of the last flushed block, and skipping index commits if the current block of the index is not an ancestor of the node's last flushed block (similar to the suggestion by stickies-v in bitcoin#33212 (review).

  Fixes bitcoin#33208
  Fixes bitcoin#34261

ACKs for top commit:
  achow101:
    ACK e9ed898
  sedited:
    Re-ACK e9ed898
  fjahr:
    re-ACK e9ed898

Tree-SHA512: 7f4dc6fb942d6726587eb75dece24c79c679d8630320502aca9fa2d2b03b1d25999cd11255ec20344ebbb8985552747e2554e2557b9d2ad0c75db71652d615ab
PastaPastaPasta added a commit to dashpay/dash that referenced this pull request Nov 4, 2026
The address, spent, timestamp and governance collateral indexes erase the
entries of rewound blocks in CustomRewind. Upstream indexes keep nothing a
rewind has to undo, so bitcoin#33212 could stop committing in Rewind and
bitcoin#34897 could leave the locator where it is until the chainstate has
flushed past the new tip. With erasures persisted right away, a locator that
stays above them is wrong after an unclean restart: if the rewound branch wins
again, Init() resumes from the old locator and the erased blocks are never
indexed again. The governance collateral index used to write the rewound
locator unconditionally, which keeps bitcoin#33208 (a locator naming a block
that is not in the on-disk block index) for an index that is always on.

When the committed locator is above the new tip on the same branch, move it to
the new tip in the rewind's own batch. The committed tip was an ancestor of a
flushed block when it was written, so the new tip below it is in the on-disk
block index after a restart. A committed locator at or below the new tip needs
no change: a restart re-appends from it.

The new baseindex_tests scenario uses a test index that erases on rewind the
way the Dash indexes do: it rewinds past the last flush, restarts and must
index the erased blocks again. It fails with WriteRewindLocator as a no-op.

Co-authored-by: pasta <[email protected]>
PastaPastaPasta added a commit to dashpay/dash that referenced this pull request Nov 4, 2026
…itcoin#32875, bitcoin#32878, bitcoin#32882, bitcoin#33212, bitcoin#34897, bitcoin#36150 (index unify + relevant fixes)

73ba99d fix(index): reject an uncommitted tip the block index lost (pasta)
3cc304e fix(index): check the blocks governance resumes from at startup (pasta)
5e37377 fix(index): resume from the tip a skipped commit leaves behind (pasta)
0afd6c3 fix(index): write a rewound block's erasures in one batch with its locator (pasta)
cc8362d fix(index): move the committed locator below the entries a rewind erases (Konstantin Akimov)
fa8a7c5 Merge bitcoin#36150: indexes: set prune lock to genesis before first block (Konstantin Akimov)
df7ac41 partial Merge bitcoin#34897: indexes: Don't commit ahead of the flushed chainstate (Konstantin Akimov)
7ad8070 Merge bitcoin#32875: index: handle case where pindex_prev equals chain tip in NextSyncBlock() (Konstantin Akimov)
ce05350 Merge bitcoin#32882: index: remove unnecessary locator cleaning in BaseIndex::Init() (Konstantin Akimov)
4bdfb4e Merge bitcoin#33212: index: Don't commit state in BaseIndex::Rewind (Konstantin Akimov)
a4cc3c0 Merge bitcoin#32878: index: fix wrong assert of current_tip == m_best_block_index (Konstantin Akimov)
ee7021f partial Merge bitcoin#32580: wallet, test: best block locator matches scan state follow-ups (Konstantin Akimov)
7e86e46 partial Merge bitcoin#30221: wallet: Ensure best block matches wallet scan state (Konstantin Akimov)
059ce4a partial Merge bitcoin#32069: test: fix intermittent failure in wallet_reorgsrestore.py (Konstantin Akimov)
ee9bb26 test: Test bdb_ro dump of wallet without reset LSNs (Konstantin Akimov)

Pull request description:

  ## Issue being fixed or feature implemented

  bitcoin#25193 (#7883) brought back bitcoin#33208: after an unclean shutdown an index can name a block that was never flushed, and the node refuses to start until the index is rebuilt. Alternative to #8030: the upstream fixes bitcoin#33212 and bitcoin#34897 applied 1:1, with the upstream prerequisites they need instead of Dash-only adaptations.

  ## What was done?

  - bitcoin#30221, bitcoin#32580: the wallet no longer stores the locator from `ChainStateFlushed`, so the bitcoin#34897 notification applies unchanged.
  - bitcoin#26606 (one hunk), bitcoin#32069: `kill_process()` for the bitcoin#33212 functional test.
  - bitcoin#32875, bitcoin#32878, bitcoin#32882, bitcoin#36150: small `BaseIndex` fixes on the same code.
  - Two Dash-only commits: Dash's address, spent, timestamp and governance collateral indexes erase entries on rewind, so the committed locator is moved below them, and each rewound block's erasures are written in one batch with that locator so an interrupted rewind can't leave it above erased entries.

  Omitted hunks and Dash adaptations are listed in each commit's BACKPORT NOTE. bitcoin#29867 is skipped: Dash never took the bitcoin#28955 lock release it fixes.

  ## How Has This Been Tested?

  - `baseindex_tests`: committed height stays at the last flush (bitcoin#34897); erased blocks are indexed again after a restart, including after a rewind interrupted between blocks (Dash-only commits, each fails with its fix removed).
  - `blockfilter_index_tests/index_reorg_crash` (bitcoin#32878), `wallet_tests/scan_for_wallet_transactions` (bitcoin#30221).
  - `feature_coinstatsindex.py` unclean restart after a reorg (bitcoin#33212), `feature_pruning.py` new index on a pruned node (bitcoin#36150).

  ## Breaking Changes

  None. An index's committed best block may trail its in-memory tip until the next flush and re-syncs after an unclean shutdown.

  ## Checklist:
  - [x] I have performed a self-review of my own code
  - [ ] I have made corresponding changes to the documentation
  - [x] I have assigned this pull request to a milestone _(if you have permission to set one)_

Top commit has no ACKs.

Tree-SHA512: df1762b9590e46e107434d238b93fefe45c167861449fbefa35d7e3cd6a3ff2857eaa5f179897686a0f2519a5a0035bf68afa632aeae02013a849dc9ef5493c6
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Nov 4, 2026
…ed chainstate

BACKPORT NOTE:
- The last flushed block is recorded right after the coins flush; Dash then commits EvoDB in the same
  flush before ChainStateFlushed is sent.
- The skip message gets Dash's explicit trailing newline; Commit() keeps Dash's LogError form.
- src/test/chainstate_write_tests.cpp does not exist in Dash, so its two assertions are not applied.
- baseindex_tests: Init() and Sync() are not callable on their own in Dash (bitcoin#27607, bitcoin#28955
  are not backported), so the test starts the index and holds back the sync thread with
  g_indexes_ready_to_sync to read the committed best block.
----
e9ed898 validation: Don't use m_chain.Tip() in FlushStateToDisk (Martin Zumsande)
3679f1e index: Don't commit ahead of the flushed chainstate (Martin Zumsande)
6573572 index: Remove return value from Commit() (Martin Zumsande)
09c0696 validation: track last flushed block (Martin Zumsande)
13c02b5 test: add test for index commits ahead of the last flushed block (Martin Zumsande)

Pull request description:

  If indexes commit their data ahead of the flushed chainstate, and there is an unclean shutdown, the index will be corrupted. This is especially the case for the coinstatsindex, which has state (the muhash) which can't easily be rolled back without access to the blocks. This was only partly fixed in bitcoin#33212 (for reorg scenarios) but could still happen during initial sync.

  Fix this more thoroughly by having the node keep track of the last flushed block, and skipping index commits if the current block of the index is not an ancestor of the node's last flushed block (similar to the suggestion by stickies-v in bitcoin#33212 (review).

  Fixes bitcoin#33208
  Fixes bitcoin#34261

ACKs for top commit:
  achow101:
    ACK e9ed898
  sedited:
    Re-ACK e9ed898
  fjahr:
    re-ACK e9ed898

Tree-SHA512: 7f4dc6fb942d6726587eb75dece24c79c679d8630320502aca9fa2d2b03b1d25999cd11255ec20344ebbb8985552747e2554e2557b9d2ad0c75db71652d615ab
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this pull request Nov 4, 2026
BACKPORT NOTE:
- v24.0.x has no GovCollateralIndex, so its CustomRewind hunk is not applied.

The address, spent, timestamp and governance collateral indexes erase the
entries of rewound blocks in CustomRewind. Upstream indexes keep nothing a
rewind has to undo, so bitcoin#33212 could stop committing in Rewind and
bitcoin#34897 could leave the locator where it is until the chainstate has
flushed past the new tip. With erasures persisted right away, a locator that
stays above them is wrong after an unclean restart: if the rewound branch wins
again, Init() resumes from the old locator and the erased blocks are never
indexed again. The governance collateral index used to write the rewound
locator unconditionally, which keeps bitcoin#33208 (a locator naming a block
that is not in the on-disk block index) for an index that is always on.

When the committed locator is above the new tip on the same branch, move it to
the new tip in the rewind's own batch. The committed tip was an ancestor of a
flushed block when it was written, so the new tip below it is in the on-disk
block index after a restart. A committed locator at or below the new tip needs
no change: a restart re-appends from it.

The new baseindex_tests scenario uses a test index that erases on rewind the
way the Dash indexes do: it rewinds past the last flush, restarts and must
index the erased blocks again. It fails with WriteRewindLocator as a no-op.

Co-authored-by: pasta <[email protected]>
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this pull request Nov 4, 2026
BACKPORT NOTE:
- v24.0.x has no GovCollateralIndex, so its WritesEagerly() override is not applied.

Dash's address, spent, timestamp and governance collateral indexes write a
block's entries as soon as they append it. Since bitcoin#34897, Commit() skips
the locator for a tip above the last flushed block, so a sync that ends above
the flush leaves entries on disk for blocks the locator doesn't cover. If the
node stops uncleanly and those blocks, already durable in the block index, are
reorged away before the index syncs again, Init() resumes from the older
locator and never rewinds them: the address index keeps the orphaned payment
in the history and the unspent outputs. Before bitcoin#34897 the end-of-sync
commit wrote the tip and the restart rewound it.

For these indexes a skipped commit now records the index tip under its own
key, and a successful commit erases it in the same batch as the locator.
Rewinds move it down with the locator. Init() resumes from it when the block
index has it above the locator, so the sync rewinds it if it left the active
chain, and drops it otherwise. The locator keeps the upstream meaning, so a
locator naming a block the block index lost is still a startup error, and a
recorded tip the block index lost falls back to the locator as before.

Blocks appended through BlockConnected after the sync are not covered: their
locator also waits for the next flush, as it did before bitcoin#34897.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

Block filter index corruption post reorg and unclean shutdown Indexes stuck on unknown best block after unclean shutdown

10 participants