Skip to content

Commit 3a0d6b0

Browse files
authored
Merge pull request #242 from ethpandaops/bbusa/fix-list-finalized-slots-underflow-gloas
fix(gloas): prevent uint64 underflow in ListFinalizedSlots on short-lived chains
2 parents 9326d5c + a0dfdef commit 3a0d6b0

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

pkg/beacon/default.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -748,10 +748,18 @@ func (d *Default) ListFinalizedSlots(ctx context.Context) ([]phase0.Slot, error)
748748
return slots, errors.New("no finalized checkpoint available")
749749
}
750750

751-
latestSlot := phase0.Slot(uint64(finality.Finalized.Epoch) * uint64(sp.SlotsPerEpoch))
752-
751+
latestSlot := uint64(finality.Finalized.Epoch) * uint64(sp.SlotsPerEpoch)
753752
//nolint:gosec // HistoricalEpochCount is validated as positive in config.
754-
for i, val := uint64(latestSlot), uint64(latestSlot)-uint64(sp.SlotsPerEpoch)*uint64(d.config.HistoricalEpochCount); i > val; i -= uint64(sp.SlotsPerEpoch) {
753+
lookback := uint64(sp.SlotsPerEpoch) * uint64(d.config.HistoricalEpochCount)
754+
755+
// Clamp the lower bound at 0 so short-lived chains (finalized epoch <
756+
// HistoricalEpochCount) don't underflow uint64 and skip the loop entirely.
757+
var earliest uint64
758+
if latestSlot > lookback {
759+
earliest = latestSlot - lookback
760+
}
761+
762+
for i := latestSlot; i > earliest; i -= uint64(sp.SlotsPerEpoch) {
755763
slots = append(slots, phase0.Slot(i))
756764
}
757765

0 commit comments

Comments
 (0)