Skip to content

Commit c59c647

Browse files
jsvisarjl493456442
andauthored
triedb: reset state indexer after snap synced (#32104)
Fix the issue after initial snap sync with `gcmode=archive` enabled. ``` NewPayload: inserting block failed error="history indexing is out of order, last: null, requested: 1" ``` --------- Signed-off-by: Delweng <delweng@gmail.com> Co-authored-by: Gary Rong <garyrong0905@gmail.com>
1 parent 87d7c2a commit c59c647

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

triedb/pathdb/database.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,16 @@ func (db *Database) Enable(root common.Hash) error {
520520
// Re-construct a new disk layer backed by persistent state
521521
// and schedule the state snapshot generation if it's permitted.
522522
db.tree.init(generateSnapshot(db, root, db.isVerkle || db.config.SnapshotNoBuild))
523+
524+
// After snap sync, the state of the database may have changed completely.
525+
// To ensure the history indexer always matches the current state, we must:
526+
// 1. Close any existing indexer
527+
// 2. Re-initialize the indexer so it starts indexing from the new state root.
528+
if db.indexer != nil && db.freezer != nil && db.config.EnableStateIndexing {
529+
db.indexer.close()
530+
db.indexer = newHistoryIndexer(db.diskdb, db.freezer, db.tree.bottom().stateID())
531+
log.Info("Re-enabled state history indexing")
532+
}
523533
log.Info("Rebuilt trie database", "root", root)
524534
return nil
525535
}

triedb/pathdb/history_indexer.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,18 @@ func (i *indexIniter) index(done chan struct{}, interrupt *atomic.Int32, lastID
494494
// when the state is reverted manually (chain.SetHead) or the deep reorg is
495495
// encountered. In such cases, no indexing should be scheduled.
496496
if beginID > lastID {
497-
log.Debug("State history is fully indexed", "last", lastID)
497+
if lastID == 0 && beginID == 1 {
498+
// Initialize the indexing flag if the state history is empty by
499+
// using zero as the disk layer ID. This is a common case that
500+
// can occur after snap sync.
501+
//
502+
// This step is essential to avoid spinning up indexing thread
503+
// endlessly until a history object is produced.
504+
storeIndexMetadata(i.disk, 0)
505+
log.Info("Initialized history indexing flag")
506+
} else {
507+
log.Debug("State history is fully indexed", "last", lastID)
508+
}
498509
return
499510
}
500511
log.Info("Start history indexing", "beginID", beginID, "lastID", lastID)

0 commit comments

Comments
 (0)