Skip to content

Commit 9ffc042

Browse files
committed
use cache window
1 parent 10e1f2a commit 9ffc042

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

block/internal/cache/generic_cache.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"sync"
99
)
1010

11+
const cacheWindow = 1000000
12+
1113
// Cache is a generic cache that maintains items that are seen and hard confirmed
1214
type Cache[T any] struct {
1315
// itemsByHeight stores items keyed by uint64 height
@@ -99,7 +101,7 @@ func (c *Cache[T]) removeDAIncluded(hash string) {
99101
}
100102

101103
// pruneOldEntries removes entries below the current height.
102-
// It keeps entries at heights >= currentHeight.
104+
// It keeps entries at heights >= currentHeight-cacheWindow.
103105
// This prevents unbounded memory growth as the chain progresses.
104106
func (c *Cache[T]) pruneOldEntries(currentHeight uint64) {
105107
if currentHeight == 0 {
@@ -114,7 +116,7 @@ func (c *Cache[T]) pruneOldEntries(currentHeight uint64) {
114116
if !ok {
115117
return true
116118
}
117-
if height < currentHeight {
119+
if height < currentHeight-cacheWindow {
118120
itemsToDelete = append(itemsToDelete, height)
119121
}
120122
return true
@@ -129,7 +131,7 @@ func (c *Cache[T]) pruneOldEntries(currentHeight uint64) {
129131
if !ok {
130132
return true
131133
}
132-
if height < currentHeight {
134+
if height < currentHeight-cacheWindow {
133135
hashesToDelete = append(hashesToDelete, hash)
134136
}
135137
return true

block/internal/cache/manager.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type Manager interface {
6767
SaveToDisk() error
6868
LoadFromDisk() error
6969
ClearFromDisk() error
70+
ClearDAIncluded(height uint64)
7071
}
7172

7273
var _ Manager = (*implementation)(nil)
@@ -164,6 +165,11 @@ func (m *implementation) SetDataDAIncluded(hash string, daHeight uint64, blockHe
164165
m.dataCache.setDAIncluded(hash, daHeight, blockHeight)
165166
}
166167

168+
func (m *implementation) ClearDAIncluded(height uint64) {
169+
m.dataCache.pruneOldEntries(height)
170+
m.headerCache.pruneOldEntries(height)
171+
}
172+
167173
// Pending operations
168174
func (m *implementation) GetPendingHeaders(ctx context.Context) ([]*types.SignedHeader, error) {
169175
return m.pendingHeaders.GetPendingHeaders(ctx)

block/internal/submitting/submitter.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ func (s *Submitter) processDAInclusionLoop() {
234234
if err := s.store.SetMetadata(s.ctx, store.DAIncludedHeightKey, bz); err != nil {
235235
s.logger.Error().Err(err).Uint64("height", nextHeight).Msg("failed to persist DA included height")
236236
}
237+
238+
// Clear DA included height cache
239+
s.cache.ClearDAIncluded(nextHeight)
237240
}
238241
}
239242
}

0 commit comments

Comments
 (0)