From 537b173506d90984ccfc0f1d9f864b1d3fa3ae9d Mon Sep 17 00:00:00 2001 From: Delweng Date: Fri, 27 Jun 2025 23:13:47 +0800 Subject: [PATCH] triedb: re init indexer after snap synced Signed-off-by: Delweng --- triedb/pathdb/database.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/triedb/pathdb/database.go b/triedb/pathdb/database.go index 7c8c327484..7945f65b3c 100644 --- a/triedb/pathdb/database.go +++ b/triedb/pathdb/database.go @@ -520,6 +520,17 @@ func (db *Database) Enable(root common.Hash) error { // Re-construct a new disk layer backed by persistent state // and schedule the state snapshot generation if it's permitted. db.tree.init(generateSnapshot(db, root, db.isVerkle || db.config.SnapshotNoBuild)) + + // After snap sync, the state of the database may have changed completely. + // To ensure the history indexer always matches the current state, we must: + // 1. Close any existing indexer + // 2. Re-initialize the indexer so it starts indexing from the new state root. + if db.indexer != nil && db.freezer != nil && db.config.EnableStateIndexing { + db.indexer.close() + db.indexer = newHistoryIndexer(db.diskdb, db.freezer, db.tree.bottom().stateID()) + log.Info("Enabled state history indexing (after snap sync)") + } + log.Info("Rebuilt trie database", "root", root) return nil }