Skip to content

Commit caae822

Browse files
committed
updates
1 parent b4ac2ed commit caae822

4 files changed

Lines changed: 37 additions & 67 deletions

File tree

block/internal/cache/generic_cache.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ func (c *Cache[T]) snapshotKey() string {
6969

7070
// NewCache creates a Cache. When store and keyPrefix are set, mutations
7171
// persist a snapshot so RestoreFromStore can recover in-flight state.
72-
// The third argument (heightKeyFn) is retained for API compatibility but unused.
73-
func NewCache[T any](s store.Store, keyPrefix string, _ func(uint64) string) *Cache[T] {
72+
func NewCache[T any](s store.Store, keyPrefix string) *Cache[T] {
7473
// LRU cache creation only fails if size <= 0, which won't happen with our defaults
7574
itemsCache, _ := lru.New[uint64, *T](DefaultItemsCacheSize)
7675
hashesCache, _ := lru.New[string, bool](DefaultHashesCacheSize)
@@ -308,7 +307,7 @@ func (c *Cache[T]) SaveToStore(ctx context.Context) error {
308307
}
309308

310309
// ClearFromStore deletes the snapshot key from the store.
311-
func (c *Cache[T]) ClearFromStore(ctx context.Context, _ []string) error {
310+
func (c *Cache[T]) ClearFromStore(ctx context.Context) error {
312311
if c.store == nil {
313312
return nil
314313
}

block/internal/cache/generic_cache_test.go

Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cache
22

33
import (
44
"context"
5-
"fmt"
65
"testing"
76

87
"github.com/stretchr/testify/assert"
@@ -31,22 +30,14 @@ func writeSnapshot(t *testing.T, st pkgstore.Store, storeKeyPrefix string, entri
3130
require.NoError(t, st.SetMetadata(context.Background(), storeKeyPrefix+"__snap", buf))
3231
}
3332

34-
// testKeyFn is a simple height-key function used by tests that don't need the
35-
// real production keys.
36-
func testKeyFn(prefix string) func(uint64) string {
37-
return func(h uint64) string {
38-
return fmt.Sprintf("%s%d", prefix, h)
39-
}
40-
}
41-
4233
// ---------------------------------------------------------------------------
4334
// MaxDAHeight
4435
// ---------------------------------------------------------------------------
4536

4637
// TestCache_MaxDAHeight verifies that daHeight tracks the maximum DA height
4738
// across successive setDAIncluded calls.
4839
func TestCache_MaxDAHeight(t *testing.T) {
49-
c := NewCache[testItem](nil, "", nil)
40+
c := NewCache[testItem](nil, "")
5041

5142
assert.Equal(t, uint64(0), c.daHeight(), "initial daHeight should be 0")
5243

@@ -69,7 +60,7 @@ func TestCache_MaxDAHeight(t *testing.T) {
6960
func TestCache_RestoreFromStore_EmptyChain(t *testing.T) {
7061
st := testMemStore(t)
7162

72-
c := NewCache[testItem](st, "hdr/", testKeyFn("hdr-da/"))
63+
c := NewCache[testItem](st, "hdr/")
7364
require.NoError(t, c.RestoreFromStore(context.Background()))
7465

7566
assert.Equal(t, 0, c.daIncluded.Len(), "no entries expected on empty chain")
@@ -87,7 +78,7 @@ func TestCache_RestoreFromStore_FullyFinalized(t *testing.T) {
8778
// empty (persistSnapshot writes an empty buf when daIncluded is empty).
8879
writeSnapshot(t, st, "hdr/", nil)
8980

90-
c := NewCache[testItem](st, "hdr/", testKeyFn("hdr-da/"))
81+
c := NewCache[testItem](st, "hdr/")
9182
require.NoError(t, c.RestoreFromStore(ctx))
9283

9384
assert.Equal(t, 0, c.daIncluded.Len(), "no in-flight entries expected")
@@ -106,7 +97,7 @@ func TestCache_RestoreFromStore_InFlightWindow(t *testing.T) {
10697
{blockHeight: 5, daHeight: 14},
10798
})
10899

109-
c := NewCache[testItem](st, "hdr/", testKeyFn("hdr-da/"))
100+
c := NewCache[testItem](st, "hdr/")
110101
require.NoError(t, c.RestoreFromStore(ctx))
111102

112103
assert.Equal(t, 2, c.daIncluded.Len(), "exactly the in-flight snapshot entries should be loaded")
@@ -136,7 +127,7 @@ func TestCache_RestoreFromStore_SingleEntry(t *testing.T) {
136127
{blockHeight: 3, daHeight: 20},
137128
})
138129

139-
c := NewCache[testItem](st, "hdr/", testKeyFn("hdr-da/"))
130+
c := NewCache[testItem](st, "hdr/")
140131
require.NoError(t, c.RestoreFromStore(ctx))
141132

142133
assert.Equal(t, 1, c.daIncluded.Len(), "one entry should be in-flight")
@@ -151,32 +142,12 @@ func TestCache_RestoreFromStore_SingleEntry(t *testing.T) {
151142
// TestCache_RestoreFromStore_NilStore verifies that RestoreFromStore is a
152143
// no-op when the cache has no backing store.
153144
func TestCache_RestoreFromStore_NilStore(t *testing.T) {
154-
c := NewCache[testItem](nil, "", nil)
145+
c := NewCache[testItem](nil, "")
155146
require.NoError(t, c.RestoreFromStore(context.Background()))
156147
assert.Equal(t, 0, c.daIncluded.Len())
157148
}
158149

159-
// TestCache_RestoreFromStore_NilHeightKeyFn verifies that RestoreFromStore
160-
// still works when no height-key function is provided: the snapshot is read
161-
// and decoded normally (the key fn is unused by the snapshot path).
162-
func TestCache_RestoreFromStore_NilHeightKeyFn(t *testing.T) {
163-
st := testMemStore(t)
164-
ctx := context.Background()
165-
166-
// Write a snapshot with one in-flight entry — keyFn is irrelevant for restore.
167-
writeSnapshot(t, st, "hdr/", []snapshotEntry{
168-
{blockHeight: 7, daHeight: 50},
169-
})
170-
171-
c := NewCache[testItem](st, "hdr/", nil) // no key fn
172-
require.NoError(t, c.RestoreFromStore(ctx))
173-
174-
// The snapshot-based restore does not use the key fn, so the entry is loaded.
175-
assert.Equal(t, 1, c.daIncluded.Len(), "snapshot entry should be loaded even without key fn")
176-
assert.Equal(t, uint64(50), c.daHeight())
177-
}
178-
179-
// TestCache_RestoreFromStore_PlaceholderOverwrittenByRealHash verifies that
150+
// TestCache_RestoreFromStore_PlaceholderOverwrittenByRealHash
180151
// when a real content-hash entry is written after restore it overwrites the
181152
// height-indexed placeholder, leaving exactly one entry per height.
182153
func TestCache_RestoreFromStore_PlaceholderOverwrittenByRealHash(t *testing.T) {
@@ -188,7 +159,7 @@ func TestCache_RestoreFromStore_PlaceholderOverwrittenByRealHash(t *testing.T) {
188159
{blockHeight: 3, daHeight: 99},
189160
})
190161

191-
c := NewCache[testItem](st, "hdr/", testKeyFn("hdr-da/"))
162+
c := NewCache[testItem](st, "hdr/")
192163
require.NoError(t, c.RestoreFromStore(ctx))
193164

194165
assert.Equal(t, 1, c.daIncluded.Len(), "one placeholder for height 3")
@@ -214,15 +185,15 @@ func TestCache_RestoreFromStore_RoundTrip(t *testing.T) {
214185
ctx := context.Background()
215186

216187
// First cache instance: write some in-flight entries.
217-
c1 := NewCache[testItem](st, "rt/", testKeyFn("rt-da/"))
188+
c1 := NewCache[testItem](st, "rt/")
218189
c1.setDAIncluded("hashA", 10, 1)
219190
c1.setDAIncluded("hashB", 20, 2)
220191
c1.setDAIncluded("hashC", 30, 3)
221192
// Remove one entry to confirm deletions are also snapshotted.
222193
c1.removeDAIncluded("hashB")
223194

224195
// Second cache instance on same store: should recover {hashA→10, hashC→30}.
225-
c2 := NewCache[testItem](st, "rt/", testKeyFn("rt-da/"))
196+
c2 := NewCache[testItem](st, "rt/")
226197
require.NoError(t, c2.RestoreFromStore(ctx))
227198

228199
assert.Equal(t, 2, c2.daIncluded.Len(), "only non-deleted entries should be restored")
@@ -242,7 +213,7 @@ func TestCache_RestoreFromStore_RoundTrip(t *testing.T) {
242213
// ---------------------------------------------------------------------------
243214

244215
func TestCache_BasicOperations(t *testing.T) {
245-
c := NewCache[testItem](nil, "", nil)
216+
c := NewCache[testItem](nil, "")
246217

247218
// setItem / getItem
248219
c.setItem(1, &testItem{V: 42})
@@ -271,7 +242,7 @@ func TestCache_BasicOperations(t *testing.T) {
271242
}
272243

273244
func TestCache_GetNextItem(t *testing.T) {
274-
c := NewCache[testItem](nil, "", nil)
245+
c := NewCache[testItem](nil, "")
275246

276247
c.setItem(1, &testItem{V: 1})
277248
c.setItem(2, &testItem{V: 2})
@@ -290,7 +261,7 @@ func TestCache_GetNextItem(t *testing.T) {
290261
}
291262

292263
func TestCache_DeleteAllForHeight(t *testing.T) {
293-
c := NewCache[testItem](nil, "", nil)
264+
c := NewCache[testItem](nil, "")
294265

295266
c.setItem(1, &testItem{V: 1})
296267
c.setItem(2, &testItem{V: 2})
@@ -307,7 +278,7 @@ func TestCache_DeleteAllForHeight(t *testing.T) {
307278
}
308279

309280
func TestCache_WithNilStore(t *testing.T) {
310-
c := NewCache[testItem](nil, "", nil)
281+
c := NewCache[testItem](nil, "")
311282
require.NotNil(t, c)
312283

313284
c.setItem(1, &testItem{V: 1})
@@ -329,7 +300,7 @@ func TestCache_SaveToStore(t *testing.T) {
329300
st := testMemStore(t)
330301
ctx := context.Background()
331302

332-
c := NewCache[testItem](st, "save-test/", nil)
303+
c := NewCache[testItem](st, "save-test/")
333304
c.setDAIncluded("hash1", 100, 1)
334305
c.setDAIncluded("hash2", 200, 2)
335306

@@ -350,11 +321,11 @@ func TestCache_ClearFromStore(t *testing.T) {
350321
st := testMemStore(t)
351322
ctx := context.Background()
352323

353-
c := NewCache[testItem](st, "clear-test/", nil)
324+
c := NewCache[testItem](st, "clear-test/")
354325
c.setDAIncluded("hash1", 100, 1)
355326
c.setDAIncluded("hash2", 200, 2)
356327

357-
require.NoError(t, c.ClearFromStore(ctx, []string{"hash1", "hash2"}))
328+
require.NoError(t, c.ClearFromStore(ctx))
358329

359330
_, err := st.GetMetadata(ctx, "clear-test/hash1")
360331
assert.Error(t, err, "key should have been removed from store")
@@ -365,7 +336,7 @@ func TestCache_ClearFromStore(t *testing.T) {
365336
// ---------------------------------------------------------------------------
366337

367338
func TestCache_LargeDataset(t *testing.T) {
368-
c := NewCache[testItem](nil, "", nil)
339+
c := NewCache[testItem](nil, "")
369340
const N = 20_000
370341
for i := N - 1; i >= 0; i-- {
371342
c.setItem(uint64(i), &testItem{V: i})
@@ -407,12 +378,12 @@ func TestCache_NoPlaceholderLeakAfterRefire(t *testing.T) {
407378
ctx := context.Background()
408379

409380
// Step 1: initial run — write a real hash for height 3.
410-
c1 := NewCache[testItem](st, "pfx/", testKeyFn("da/"))
381+
c1 := NewCache[testItem](st, "pfx/")
411382
c1.setDAIncluded("realHash3", 99, 3)
412383
// snapshot now contains [{blockHeight:3, daHeight:99}]
413384

414385
// Step 2: restart — placeholder installed for height 3.
415-
c2 := NewCache[testItem](st, "pfx/", testKeyFn("da/"))
386+
c2 := NewCache[testItem](st, "pfx/")
416387
require.NoError(t, c2.RestoreFromStore(ctx))
417388

418389
placeholder := HeightPlaceholderKey("pfx/", 3)
@@ -450,13 +421,13 @@ func TestCache_RestartIdempotent(t *testing.T) {
450421
const daH = uint64(42)
451422

452423
// ── Run 1: normal operation, height 5 in-flight ──────────────────────────
453-
c1 := NewCache[testItem](st, "pfx/", testKeyFn("da/"))
424+
c1 := NewCache[testItem](st, "pfx/")
454425
c1.setDAIncluded(realHash, daH, blockH)
455426
// snapshot: [{5, 42}]
456427

457428
for restart := 1; restart <= 3; restart++ {
458429
// ── Restart N: restore from snapshot
459-
cR := NewCache[testItem](st, "pfx/", testKeyFn("da/"))
430+
cR := NewCache[testItem](st, "pfx/")
460431
require.NoError(t, cR.RestoreFromStore(ctx), "restart %d: RestoreFromStore", restart)
461432

462433
assert.Equal(t, 1, cR.daIncluded.Len(), "restart %d: one placeholder entry", restart)

block/internal/cache/manager.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ type implementation struct {
103103

104104
// NewManager creates a new Manager, restoring or clearing persisted state as configured.
105105
func NewManager(cfg config.Config, st store.Store, logger zerolog.Logger) (Manager, error) {
106-
headerCache := NewCache[types.SignedHeader](st, HeaderDAIncludedPrefix, store.GetHeightToDAHeightHeaderKey)
107-
dataCache := NewCache[types.Data](st, DataDAIncludedPrefix, store.GetHeightToDAHeightDataKey)
108-
txCache := NewCache[struct{}](nil, "", nil)
109-
pendingEventsCache := NewCache[common.DAHeightEvent](nil, "", nil)
106+
headerCache := NewCache[types.SignedHeader](st, HeaderDAIncludedPrefix)
107+
dataCache := NewCache[types.Data](st, DataDAIncludedPrefix)
108+
txCache := NewCache[struct{}](nil, "")
109+
pendingEventsCache := NewCache[common.DAHeightEvent](nil, "")
110110

111111
pendingHeaders, err := NewPendingHeaders(st, logger)
112112
if err != nil {
@@ -371,17 +371,17 @@ func (m *implementation) RestoreFromStore() error {
371371
func (m *implementation) ClearFromStore() error {
372372
ctx := context.Background()
373373

374-
if err := m.headerCache.ClearFromStore(ctx, m.headerCache.daIncluded.Keys()); err != nil {
374+
if err := m.headerCache.ClearFromStore(ctx); err != nil {
375375
return fmt.Errorf("failed to clear header cache from store: %w", err)
376376
}
377-
if err := m.dataCache.ClearFromStore(ctx, m.dataCache.daIncluded.Keys()); err != nil {
377+
if err := m.dataCache.ClearFromStore(ctx); err != nil {
378378
return fmt.Errorf("failed to clear data cache from store: %w", err)
379379
}
380380

381-
m.headerCache = NewCache[types.SignedHeader](m.store, HeaderDAIncludedPrefix, store.GetHeightToDAHeightHeaderKey)
382-
m.dataCache = NewCache[types.Data](m.store, DataDAIncludedPrefix, store.GetHeightToDAHeightDataKey)
383-
m.txCache = NewCache[struct{}](nil, "", nil)
384-
m.pendingEventsCache = NewCache[common.DAHeightEvent](nil, "", nil)
381+
m.headerCache = NewCache[types.SignedHeader](m.store, HeaderDAIncludedPrefix)
382+
m.dataCache = NewCache[types.Data](m.store, DataDAIncludedPrefix)
383+
m.txCache = NewCache[struct{}](nil, "")
384+
m.pendingEventsCache = NewCache[common.DAHeightEvent](nil, "")
385385

386386
m.initDAHeightFromStore(ctx)
387387

block/internal/submitting/submitter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,16 +504,16 @@ func (s *Submitter) IsHeightDAIncluded(height uint64, header *types.SignedHeader
504504

505505
dataCommitment := data.DACommitment()
506506

507-
// Try real hash first; fall back to height-based placeholder for post-restart
507+
// Try real hash first; fall back to height-based lookup for post-restart
508508
// state before the DA retriever has re-fired the real hashes.
509509
_, headerIncluded := s.cache.GetHeaderDAIncluded(header.Hash().String())
510510
_, dataIncluded := s.cache.GetDataDAIncluded(dataCommitment.String())
511511

512512
if !headerIncluded {
513-
_, headerIncluded = s.cache.GetHeaderDAIncluded(cache.HeightPlaceholderKey(cache.HeaderDAIncludedPrefix, height))
513+
_, headerIncluded = s.cache.GetHeaderDAIncludedByHeight(height)
514514
}
515515
if !dataIncluded {
516-
_, dataIncluded = s.cache.GetDataDAIncluded(cache.HeightPlaceholderKey(cache.DataDAIncludedPrefix, height))
516+
_, dataIncluded = s.cache.GetDataDAIncludedByHeight(height)
517517
}
518518

519519
dataIncluded = bytes.Equal(dataCommitment, common.DataHashForEmptyTxs) || dataIncluded

0 commit comments

Comments
 (0)