diff --git a/trie/triedb/pathdb/disklayer.go b/trie/triedb/pathdb/disklayer.go index c934f2f5c3..60c891fd2c 100644 --- a/trie/triedb/pathdb/disklayer.go +++ b/trie/triedb/pathdb/disklayer.go @@ -331,14 +331,20 @@ func (dl *diskLayer) commit(bottom *diffLayer, force bool) (*diskLayer, error) { if overflow { if _, ok := dl.buffer.(*nodebufferlist); ok { persistentID := rawdb.ReadPersistentStateID(dl.db.diskdb) - if persistentID > limit { - oldest = persistentID - limit + 1 - log.Info("Forcing prune ancient under nodebufferlist", "disk_persistent_state_id", - persistentID, "truncate_tail", oldest) - } else { - log.Info("No prune ancient under nodebufferlist, less than db config state history limit") + if limit >= persistentID { + log.Debug("No prune ancient under nodebufferlist, less than db config state history limit", "persistent_id", persistentID, "limit", limit) return ndl, nil } + targetOldest := persistentID - limit + 1 + realOldest, err := dl.db.freezer.Tail() + if err == nil && targetOldest <= realOldest { + log.Info("No prune ancient under nodebufferlist due to truncate oldest less than real oldest, which maybe happened in abnormal restart", + "tartget_oldest_id", targetOldest, "real_oldest_id", realOldest, "error", err) + return ndl, nil + } + oldest = targetOldest + log.Info("Forcing prune ancient under nodebufferlist", "disk_persistent_state_id", + persistentID, "truncate_tail", oldest) } pruned, err := truncateFromTail(ndl.db.diskdb, ndl.db.freezer, oldest-1) diff --git a/trie/triedb/pathdb/history.go b/trie/triedb/pathdb/history.go index 6e3f3faaed..7d7cc71f48 100644 --- a/trie/triedb/pathdb/history.go +++ b/trie/triedb/pathdb/history.go @@ -619,7 +619,8 @@ func truncateFromTail(db ethdb.Batcher, freezer *rawdb.ResettableFreezer, ntail } // Ensure that the truncation target falls within the specified range. if otail > ntail || ntail > ohead { - return 0, fmt.Errorf("out of range, tail: %d, head: %d, target: %d", otail, ohead, ntail) + log.Warn("truncate from tail out of range", "tail:", otail, "head:", ohead, "target:", ntail) + return 0, nil } // Short circuit if nothing to truncate. if otail == ntail { diff --git a/trie/triedb/pathdb/history_test.go b/trie/triedb/pathdb/history_test.go index a3257441de..fee8ecdb1e 100644 --- a/trie/triedb/pathdb/history_test.go +++ b/trie/triedb/pathdb/history_test.go @@ -252,8 +252,8 @@ func TestTruncateOutOfRange(t *testing.T) { {0, head + 1, fmt.Errorf("out of range, tail: %d, head: %d, target: %d", tail, head, head+1)}, {0, tail - 1, fmt.Errorf("out of range, tail: %d, head: %d, target: %d", tail, head, tail-1)}, {1, tail, nil}, // nothing to delete - {1, head + 1, fmt.Errorf("out of range, tail: %d, head: %d, target: %d", tail, head, head+1)}, - {1, tail - 1, fmt.Errorf("out of range, tail: %d, head: %d, target: %d", tail, head, tail-1)}, + {1, head + 1, nil}, + {1, tail - 1, nil}, } for _, c := range cases { var gotErr error