From cbd8ffcc3ad0cb0e8a1ea9cdce103ba4e745f448 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Wed, 30 Apr 2025 08:59:41 +0800 Subject: [PATCH] core, ethdb: fix a flaw in SetHeadWithTimestamp --- core/headerchain.go | 8 ++++++-- ethdb/database.go | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/core/headerchain.go b/core/headerchain.go index abe8086cf8..6e70dfa865 100644 --- a/core/headerchain.go +++ b/core/headerchain.go @@ -625,8 +625,12 @@ func (hc *HeaderChain) setHead(headBlock uint64, headTime uint64, updateFn Updat if delFn != nil { // Ignore the error here since light client won't hit this path frozen, _ := hc.chainDb.Ancients() - if headBlock+1 < frozen { - _, err := hc.chainDb.TruncateHead(headBlock + 1) + header := hc.CurrentHeader() + + // Truncate the excessive chain segment above the current chain head + // in the ancient store. + if header.Number.Uint64()+1 < frozen { + _, err := hc.chainDb.TruncateHead(header.Number.Uint64() + 1) if err != nil { log.Crit("Failed to truncate head block", "err", err) } diff --git a/ethdb/database.go b/ethdb/database.go index fbf142e554..7f421752c4 100644 --- a/ethdb/database.go +++ b/ethdb/database.go @@ -57,7 +57,7 @@ type KeyValueStater interface { Stat() (string, error) } -// KeyValueSyncer wraps the Sync method of a backing data store. +// KeyValueSyncer wraps the SyncKeyValue method of a backing data store. type KeyValueSyncer interface { // SyncKeyValue ensures that all pending writes are flushed to disk, // guaranteeing data durability up to the point.