From 3b50a5e27efe734ccefbed5e5492b7ad0ccb7144 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Tue, 1 Jul 2025 10:34:26 +0800 Subject: [PATCH] triedb/pathdb: persist the zero indexing tracker --- triedb/pathdb/database.go | 3 +-- triedb/pathdb/history_indexer.go | 13 ++++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/triedb/pathdb/database.go b/triedb/pathdb/database.go index 7945f65b3c..23a7d383b5 100644 --- a/triedb/pathdb/database.go +++ b/triedb/pathdb/database.go @@ -528,9 +528,8 @@ func (db *Database) Enable(root common.Hash) error { 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("Re-enabled state history indexing") } - log.Info("Rebuilt trie database", "root", root) return nil } diff --git a/triedb/pathdb/history_indexer.go b/triedb/pathdb/history_indexer.go index 6df74de61c..90816e6ef6 100644 --- a/triedb/pathdb/history_indexer.go +++ b/triedb/pathdb/history_indexer.go @@ -494,7 +494,18 @@ func (i *indexIniter) index(done chan struct{}, interrupt *atomic.Int32, lastID // when the state is reverted manually (chain.SetHead) or the deep reorg is // encountered. In such cases, no indexing should be scheduled. if beginID > lastID { - log.Debug("State history is fully indexed", "last", lastID) + if lastID == 0 && beginID == 1 { + // Initialize the indexing flag if the state history is empty by + // using zero as the disk layer ID. This is a common case that + // can occur after snap sync. + // + // This step is essential to avoid spinning up indexing thread + // endlessly until a history object is produced. + storeIndexMetadata(i.disk, 0) + log.Info("Initialized history indexing flag") + } else { + log.Debug("State history is fully indexed", "last", lastID) + } return } log.Info("Start history indexing", "beginID", beginID, "lastID", lastID)