Skip to content

Conversation

@VolodymyrBg
Copy link
Contributor

fixed an underflow in core/rawdb/ancient_utils.go where inspect() assigned info.head = ancients - 1 unconditionally, which produced ^uint64(0) when Ancients() == 0. Although freezerInfo.count() previously happened to return 0 in the empty case due to unsigned wraparound and the invariant that Tail() == 0 for an empty freezer, the code was fragile and left head invalid. The fix adds an explicit ancients field to freezerInfo, computes the count as ancients - tail with a guard for ancients <= tail, and only sets head when ancients > 0. This aligns the implementation with the ethdb interface contract (see ethdb/database.go for Ancients() and Tail() semantics) and preserves behavior for non-empty freezers while removing reliance on overflow behavior. The changes are localized to core/rawdb/ancient_utils.go (freezerInfo, count(), and inspect()), and callers such as InspectDatabase in core/rawdb/database.go continue to function the same, now with explicit and safe arithmetic.

@rjl493456442
Copy link
Member

rjl493456442 commented Nov 17, 2025

good catch, how about this?

diff --git a/core/rawdb/ancient_utils.go b/core/rawdb/ancient_utils.go
index b940d91040..ccaa9776be 100644
--- a/core/rawdb/ancient_utils.go
+++ b/core/rawdb/ancient_utils.go
@@ -34,14 +34,10 @@ type freezerInfo struct {
 	name  string      // The identifier of freezer
 	head  uint64      // The number of last stored item in the freezer
 	tail  uint64      // The number of first stored item in the freezer
+	count uint64      // The number of stored items in the freezer
 	sizes []tableSize // The storage size per table
 }
 
-// count returns the number of stored items in the freezer.
-func (info *freezerInfo) count() uint64 {
-	return info.head - info.tail + 1
-}
-
 // size returns the storage size of the entire freezer.
 func (info *freezerInfo) size() common.StorageSize {
 	var total common.StorageSize
@@ -65,14 +61,23 @@ func inspect(name string, order map[string]freezerTableConfig, reader ethdb.Anci
 	if err != nil {
 		return freezerInfo{}, err
 	}
-	info.head = ancients - 1
-
+	if ancients == 0 {
+		info.head = 0
+	} else {
+		info.head = ancients - 1
+	}
 	// Retrieve the number of first stored item
 	tail, err := reader.Tail()
 	if err != nil {
 		return freezerInfo{}, err
 	}
 	info.tail = tail
+
+	if ancients == 0 {
+		info.count = 0
+	} else {
+		info.count = info.head - info.tail + 1
+	}
 	return info, nil
 }
 
diff --git a/core/rawdb/database.go b/core/rawdb/database.go
index d5c0f0aab2..448922d9bd 100644
--- a/core/rawdb/database.go
+++ b/core/rawdb/database.go
@@ -644,7 +644,7 @@ func InspectDatabase(db ethdb.Database, keyPrefix, keyStart []byte) error {
 				fmt.Sprintf("Ancient store (%s)", strings.Title(ancient.name)),
 				strings.Title(table.name),
 				table.size.String(),
-				fmt.Sprintf("%d", ancient.count()),
+				fmt.Sprintf("%d", ancient.count),
 			})
 		}
 		total.Add(uint64(ancient.size()))

@VolodymyrBg
Copy link
Contributor Author

good catch, how about this?

diff --git a/core/rawdb/ancient_utils.go b/core/rawdb/ancient_utils.go
index b940d91040..ccaa9776be 100644
--- a/core/rawdb/ancient_utils.go
+++ b/core/rawdb/ancient_utils.go
@@ -34,14 +34,10 @@ type freezerInfo struct {
 	name  string      // The identifier of freezer
 	head  uint64      // The number of last stored item in the freezer
 	tail  uint64      // The number of first stored item in the freezer
+	count uint64      // The number of stored items in the freezer
 	sizes []tableSize // The storage size per table
 }
 
-// count returns the number of stored items in the freezer.
-func (info *freezerInfo) count() uint64 {
-	return info.head - info.tail + 1
-}
-
 // size returns the storage size of the entire freezer.
 func (info *freezerInfo) size() common.StorageSize {
 	var total common.StorageSize
@@ -65,14 +61,23 @@ func inspect(name string, order map[string]freezerTableConfig, reader ethdb.Anci
 	if err != nil {
 		return freezerInfo{}, err
 	}
-	info.head = ancients - 1
-
+	if ancients == 0 {
+		info.head = 0
+	} else {
+		info.head = ancients - 1
+	}
 	// Retrieve the number of first stored item
 	tail, err := reader.Tail()
 	if err != nil {
 		return freezerInfo{}, err
 	}
 	info.tail = tail
+
+	if ancients == 0 {
+		info.count = 0
+	} else {
+		info.count = info.head - info.tail + 1
+	}
 	return info, nil
 }
 
diff --git a/core/rawdb/database.go b/core/rawdb/database.go
index d5c0f0aab2..448922d9bd 100644
--- a/core/rawdb/database.go
+++ b/core/rawdb/database.go
@@ -644,7 +644,7 @@ func InspectDatabase(db ethdb.Database, keyPrefix, keyStart []byte) error {
 				fmt.Sprintf("Ancient store (%s)", strings.Title(ancient.name)),
 				strings.Title(table.name),
 				table.size.String(),
-				fmt.Sprintf("%d", ancient.count()),
+				fmt.Sprintf("%d", ancient.count),
 			})
 		}
 		total.Add(uint64(ancient.size()))

Ok, implemented

func (info *freezerInfo) count() uint64 {
return info.head - info.tail + 1
name string // The identifier of freezer
ancients uint64 // The total number of stored items in the freezer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not needed, please remove it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not needed, please remove it

corrected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants