triedb: re init indexer after snap synced

Signed-off-by: Delweng <delweng@gmail.com>
This commit is contained in:
Delweng 2025-06-27 23:13:47 +08:00
parent 6bc13d8004
commit 537b173506

View file

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