From f5183eb1db0ca9a505edc401ea5c2898ec686702 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Fri, 20 Jun 2025 19:25:13 +0800 Subject: [PATCH] triedb: manually truncate ancient db Signed-off-by: jsvisa --- triedb/database.go | 11 +++++++++++ triedb/pathdb/database.go | 10 ++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/triedb/database.go b/triedb/database.go index d2637bd909..0d7397ba42 100644 --- a/triedb/database.go +++ b/triedb/database.go @@ -280,6 +280,17 @@ func (db *Database) Recover(target common.Hash) error { return pdb.Recover(target) } +// TruncateHead truncates all state histories in the freezer above the bottom state layer. +// This operation is only supported by path-based database It's typically used during +// state recovery to align the state histories with the current state. +func (db *Database) TruncateHead() error { + pdb, ok := db.backend.(*pathdb.Database) + if !ok { + return errors.New("not supported") + } + return pdb.TruncateHead() +} + // Recoverable returns the indicator if the specified state is enabled to be // recovered. It's only supported by path-based database and will return an // error for others. diff --git a/triedb/pathdb/database.go b/triedb/pathdb/database.go index ae9574963e..0f55cfd881 100644 --- a/triedb/pathdb/database.go +++ b/triedb/pathdb/database.go @@ -502,14 +502,16 @@ func (db *Database) Recover(root common.Hash) error { if err := db.diskdb.SyncKeyValue(); err != nil { return err } - _, err := truncateFromHead(db.stateFreezer, dl.stateID()) - if err != nil { - return err - } log.Debug("Recovered state", "root", root, "elapsed", common.PrettyDuration(time.Since(start))) return nil } +// TruncateHead truncates all state histories in the freezer above the bottom state layer. +func (db *Database) TruncateHead() error { + _, err := truncateFromHead(db.stateFreezer, db.tree.bottom().stateID()) + return err +} + // Recoverable returns the indicator if the specified state is recoverable. // // The supplied root must be a valid trie hash value.