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 }