triedb/pathdb: reset trienode freezer for initialization

This commit is contained in:
Gary Rong 2026-03-03 13:44:20 +08:00
parent 3a90107843
commit 43b1f7e9fe

View file

@ -412,28 +412,38 @@ func repairHistory(db ethdb.Database, isVerkle bool, readOnly bool, stateID uint
// Truncate excessive history entries in either the state history or // Truncate excessive history entries in either the state history or
// the trienode history, ensuring both histories remain aligned with // the trienode history, ensuring both histories remain aligned with
// the state. // the state.
head, err := states.Ancients() shead, err := states.Ancients()
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
if stateID > head { if stateID > shead { // Gap is not permitted in the state history
return nil, nil, fmt.Errorf("gap between state [#%d] and state history [#%d]", stateID, head) return nil, nil, fmt.Errorf("gap between state [#%d] and state history [#%d]", stateID, shead)
} }
truncTo := min(shead, stateID)
if trienodes != nil { if trienodes != nil {
th, err := trienodes.Ancients() thead, err := trienodes.Ancients()
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
if stateID > th { if stateID <= thead {
return nil, nil, fmt.Errorf("gap between state [#%d] and trienode history [#%d]", stateID, th) truncTo = min(truncTo, thead)
} } else {
if th != head { ttail, err := trienodes.Tail()
log.Info("Histories are not aligned with each other", "state", head, "trienode", th) if err != nil {
head = min(head, th) return nil, nil, err
}
_, err = trienodes.TruncateTail(stateID)
if err != nil {
return nil, nil, err
}
if thead == 0 {
log.Warn("Initialized trienode history")
} else {
log.Warn("Purged stale trienode history", "from", ttail, "to", thead-1, "count", thead-ttail)
}
} }
} }
head = min(head, stateID)
// Truncate the extra history elements above in freezer in case it's not // Truncate the extra history elements above in freezer in case it's not
// aligned with the state. It might happen after an unclean shutdown. // aligned with the state. It might happen after an unclean shutdown.
truncate := func(store ethdb.AncientStore, typ historyType, nhead uint64) { truncate := func(store ethdb.AncientStore, typ historyType, nhead uint64) {
@ -448,7 +458,7 @@ func repairHistory(db ethdb.Database, isVerkle bool, readOnly bool, stateID uint
log.Warn("Truncated extra histories", "typ", typ, "number", pruned) log.Warn("Truncated extra histories", "typ", typ, "number", pruned)
} }
} }
truncate(states, typeStateHistory, head) truncate(states, typeStateHistory, truncTo)
truncate(trienodes, typeTrienodeHistory, head) truncate(trienodes, typeTrienodeHistory, truncTo)
return states, trienodes, nil return states, trienodes, nil
} }