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
// the trienode history, ensuring both histories remain aligned with
// the state.
head, err := states.Ancients()
shead, err := states.Ancients()
if err != nil {
return nil, nil, err
}
if stateID > head {
return nil, nil, fmt.Errorf("gap between state [#%d] and state history [#%d]", 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, shead)
}
truncTo := min(shead, stateID)
if trienodes != nil {
th, err := trienodes.Ancients()
thead, err := trienodes.Ancients()
if err != nil {
return nil, nil, err
}
if stateID > th {
return nil, nil, fmt.Errorf("gap between state [#%d] and trienode history [#%d]", stateID, th)
}
if th != head {
log.Info("Histories are not aligned with each other", "state", head, "trienode", th)
head = min(head, th)
if stateID <= thead {
truncTo = min(truncTo, thead)
} else {
ttail, err := trienodes.Tail()
if err != nil {
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
// aligned with the state. It might happen after an unclean shutdown.
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)
}
}
truncate(states, typeStateHistory, head)
truncate(trienodes, typeTrienodeHistory, head)
truncate(states, typeStateHistory, truncTo)
truncate(trienodes, typeTrienodeHistory, truncTo)
return states, trienodes, nil
}