Skip to content

Commit 470bbe1

Browse files
committed
fix underflow in freezer inspect for empty ancients
1 parent 97ddfa4 commit 470bbe1

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

core/rawdb/ancient_utils.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,10 @@ type freezerInfo struct {
3434
name string // The identifier of freezer
3535
head uint64 // The number of last stored item in the freezer
3636
tail uint64 // The number of first stored item in the freezer
37+
count uint64 // The number of stored items in the freezer
3738
sizes []tableSize // The storage size per table
3839
}
3940

40-
// count returns the number of stored items in the freezer.
41-
func (info *freezerInfo) count() uint64 {
42-
return info.head - info.tail + 1
43-
}
44-
4541
// size returns the storage size of the entire freezer.
4642
func (info *freezerInfo) size() common.StorageSize {
4743
var total common.StorageSize
@@ -51,6 +47,41 @@ func (info *freezerInfo) size() common.StorageSize {
5147
return total
5248
}
5349

50+
func inspect(name string, order map[string]freezerTableConfig, reader ctxcdb.AncientReader) (freezerInfo, error) {
51+
info := freezerInfo{name: name}
52+
for t := range order {
53+
size, err := reader.AncientSize(t)
54+
if err != nil {
55+
return freezerInfo{}, err
56+
}
57+
info.sizes = append(info.sizes, tableSize{name: t, size: common.StorageSize(size)})
58+
}
59+
// Retrieve the number of last stored item
60+
ancients, err := reader.Ancients()
61+
if err != nil {
62+
return freezerInfo{}, err
63+
}
64+
if ancients > 0 {
65+
info.head = ancients - 1
66+
} else {
67+
info.head = 0
68+
}
69+
70+
// Retrieve the number of first stored item
71+
tail, err := reader.Tail()
72+
if err != nil {
73+
return freezerInfo{}, err
74+
}
75+
info.tail = tail
76+
77+
if ancients == 0 {
78+
info.count = 0
79+
} else {
80+
info.count = info.head - info.tail + 1
81+
}
82+
return info, nil
83+
}
84+
5485
// inspectFreezers inspects all freezers registered in the system.
5586
func inspectFreezers(db ctxcdb.Database) ([]freezerInfo, error) {
5687
var infos []freezerInfo

core/rawdb/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ func InspectDatabase(db ctxcdb.Database, keyPrefix, keyStart []byte) error {
604604
fmt.Sprintf("Ancient store (%s)", strings.Title(ancient.name)),
605605
strings.Title(table.name),
606606
table.size.String(),
607-
fmt.Sprintf("%d", ancient.count()),
607+
fmt.Sprintf("%d", ancient.count),
608608
})
609609
}
610610
total.Add(uint64(ancient.size()))

0 commit comments

Comments
 (0)