@@ -19,7 +19,7 @@ import (
1919)
2020
2121type BlockCache struct {
22- followDistance uint64
22+ followDistance uint32
2323
2424 specMutex sync.RWMutex
2525 specs * ChainSpec
@@ -50,7 +50,7 @@ type BlockCache struct {
5050 wallclockSlotDispatcher Dispatcher [* ethwallclock.Slot ]
5151}
5252
53- func NewBlockCache (ctx context.Context , logger logrus.FieldLogger , followDistance uint64 ) (* BlockCache , error ) {
53+ func NewBlockCache (ctx context.Context , logger logrus.FieldLogger , followDistance uint32 ) (* BlockCache , error ) {
5454 if followDistance == 0 {
5555 return nil , fmt .Errorf ("cannot initialize block cache without follow distance" )
5656 }
@@ -64,7 +64,12 @@ func NewBlockCache(ctx context.Context, logger logrus.FieldLogger, followDistanc
6464 go func () {
6565 defer func () {
6666 if err := recover (); err != nil {
67- logger .WithError (err .(error )).Errorf ("uncaught panic in BlockCache.runCacheCleanup subroutine: %v, stack: %v" , err , string (debug .Stack ()))
67+ var err2 error
68+ if errval , errok := err .(error ); errok {
69+ err2 = errval
70+ }
71+
72+ logger .WithError (err2 ).Errorf ("uncaught panic in BlockCache.runCacheCleanup subroutine: %v, stack: %v" , err , string (debug .Stack ()))
6873 }
6974 }()
7075 cache .runCacheCleanup (ctx )
@@ -94,8 +99,14 @@ func (cache *BlockCache) notifyBlockReady(block *Block) {
9499}
95100
96101func (cache * BlockCache ) SetMinFollowDistance (followDistance uint64 ) {
97- if followDistance > cache .followDistance {
98- cache .followDistance = followDistance
102+ if followDistance > 10000 {
103+ followDistance = 10000
104+ }
105+
106+ followDistance32 := uint32 (followDistance ) //nolint:gosec // no overflow possible
107+
108+ if followDistance32 > cache .followDistance {
109+ cache .followDistance = followDistance32
99110 }
100111}
101112
@@ -248,7 +259,7 @@ func (cache *BlockCache) AddBlock(root phase0.Root, slot phase0.Slot) (*Block, b
248259 return cache .blockRootMap [root ], false
249260 }
250261
251- if int64 ( slot ) < cache .maxSlotIdx - int64 (cache .followDistance ) {
262+ if cutOffSlot := cache .maxSlotIdx - int64 (cache .followDistance ); cutOffSlot > 0 && slot < phase0 . Slot ( cutOffSlot ) {
252263 return nil , false
253264 }
254265
@@ -267,8 +278,8 @@ func (cache *BlockCache) AddBlock(root phase0.Root, slot phase0.Slot) (*Block, b
267278 cache .blockSlotMap [slot ] = append (cache .blockSlotMap [slot ], cacheBlock )
268279 }
269280
270- if int64 ( slot ) > cache .maxSlotIdx {
271- cache .maxSlotIdx = int64 (slot )
281+ if cache . maxSlotIdx < 0 || slot > phase0 . Slot ( cache .maxSlotIdx ) {
282+ cache .maxSlotIdx = int64 (slot ) //nolint:gosec // no overflow possible
272283 }
273284
274285 return cacheBlock , true
0 commit comments