@@ -2,7 +2,6 @@ package cache
22
33import (
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.
4839func 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) {
6960func 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.
153144func 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.
182153func 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
244215func 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
273244func 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
292263func 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
309280func 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
367338func 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 )
0 commit comments