@@ -3,7 +3,6 @@ package store
33import (
44 "context"
55 "crypto/sha256"
6- "errors"
76 "testing"
87 "time"
98
@@ -113,10 +112,12 @@ func TestHeaderStoreAdapter_GetFromStore(t *testing.T) {
113112 require .NoError (t , batch .SetHeight (1 ))
114113 require .NoError (t , batch .Commit ())
115114
116- // Now create adapter and verify we can get from store
115+ // Create adapter after data is in store
117116 adapter := NewHeaderStoreAdapter (store , testGenesis ())
118117
119- retrieved , err := adapter .GetByHeight (ctx , 1 )
118+ // Get by hash - need to use the index hash (sha256 of marshaled SignedHeader)
119+ hash := computeHeaderIndexHash (h1 )
120+ retrieved , err := adapter .Get (ctx , hash )
120121 require .NoError (t , err )
121122 assert .Equal (t , h1 .Height (), retrieved .Height ())
122123
@@ -143,13 +144,12 @@ func TestHeaderStoreAdapter_Has(t *testing.T) {
143144
144145 adapter := NewHeaderStoreAdapter (store , testGenesis ())
145146
146- p2pH1 := wrapHeader (h1 )
147- // Has should return true for existing hash
148- has , err := adapter .Has (ctx , p2pH1 .Hash ())
147+ // Has should return true for existing header - use index hash
148+ has , err := adapter .Has (ctx , computeHeaderIndexHash (h1 ))
149149 require .NoError (t , err )
150150 assert .True (t , has )
151151
152- // Has should return false for non-existent hash
152+ // Has should return false for non-existent
153153 has , err = adapter .Has (ctx , []byte ("nonexistent" ))
154154 require .NoError (t , err )
155155 assert .False (t , has )
@@ -536,76 +536,6 @@ func TestHeaderStoreAdapter_ContextTimeout(t *testing.T) {
536536 _ = adapter .Append (ctx , wrapHeader (h1 ))
537537}
538538
539- func TestHeaderStoreAdapter_GetRangePartial (t * testing.T ) {
540- t .Parallel ()
541- // Use a short timeout since GetByHeight now blocks waiting for the height
542- ctx , cancel := context .WithTimeout (context .Background (), 500 * time .Millisecond )
543- defer cancel ()
544-
545- ds , err := NewTestInMemoryKVStore ()
546- require .NoError (t , err )
547- store := New (ds )
548- adapter := NewHeaderStoreAdapter (store , testGenesis ())
549-
550- // Only append headers for heights 1 and 2, not 3
551- h1 , _ := types .GetRandomBlock (1 , 1 , "test-chain" )
552- h2 , _ := types .GetRandomBlock (2 , 1 , "test-chain" )
553- require .NoError (t , adapter .Append (ctx , wrapHeader (h1 ), wrapHeader (h2 )))
554-
555- // GetRange [1, 5) should return headers 1 and 2 (partial result)
556- headers , err := adapter .GetRange (ctx , 1 , 5 )
557- require .NoError (t , err )
558- require .Len (t , headers , 2 )
559- assert .Equal (t , uint64 (1 ), headers [0 ].Height ())
560- assert .Equal (t , uint64 (2 ), headers [1 ].Height ())
561- }
562-
563- func TestHeaderStoreAdapter_GetRangeEmpty (t * testing.T ) {
564- t .Parallel ()
565- // Use a short timeout since GetByHeight now blocks waiting for the height
566- ctx , cancel := context .WithTimeout (context .Background (), 100 * time .Millisecond )
567- defer cancel ()
568-
569- ds , err := NewTestInMemoryKVStore ()
570- require .NoError (t , err )
571- store := New (ds )
572- adapter := NewHeaderStoreAdapter (store , testGenesis ())
573-
574- // GetRange on empty store will block until context timeout
575- _ , err = adapter .GetRange (ctx , 1 , 5 )
576- // GetByHeight now blocks - we may get context.DeadlineExceeded or ErrNotFound depending on timing
577- assert .True (t , errors .Is (err , context .DeadlineExceeded ) || errors .Is (err , header .ErrNotFound ),
578- "expected DeadlineExceeded or ErrNotFound, got: %v" , err )
579- }
580-
581- func TestHeaderStoreAdapter_MultipleAppends (t * testing.T ) {
582- t .Parallel ()
583- ctx := context .Background ()
584-
585- ds , err := NewTestInMemoryKVStore ()
586- require .NoError (t , err )
587- store := New (ds )
588- adapter := NewHeaderStoreAdapter (store , testGenesis ())
589-
590- // Append headers in multiple batches
591- h1 , _ := types .GetRandomBlock (1 , 1 , "test-chain" )
592- require .NoError (t , adapter .Append (ctx , wrapHeader (h1 )))
593- assert .Equal (t , uint64 (1 ), adapter .Height ())
594-
595- h2 , _ := types .GetRandomBlock (2 , 1 , "test-chain" )
596- require .NoError (t , adapter .Append (ctx , wrapHeader (h2 )))
597- assert .Equal (t , uint64 (2 ), adapter .Height ())
598-
599- h3 , _ := types .GetRandomBlock (3 , 1 , "test-chain" )
600- require .NoError (t , adapter .Append (ctx , wrapHeader (h3 )))
601- assert .Equal (t , uint64 (3 ), adapter .Height ())
602-
603- // Verify all headers are retrievable
604- for h := uint64 (1 ); h <= 3 ; h ++ {
605- assert .True (t , adapter .HasAt (ctx , h ))
606- }
607- }
608-
609539func TestHeaderStoreAdapter_PendingAndStoreInteraction (t * testing.T ) {
610540 t .Parallel ()
611541 ctx := context .Background ()
@@ -678,11 +608,10 @@ func TestHeaderStoreAdapter_GetFromPendingByHash(t *testing.T) {
678608
679609 // Add header to pending
680610 h1 , _ := types .GetRandomBlock (1 , 1 , "test-chain" )
681- p2pH1 := wrapHeader (h1 )
682- require .NoError (t , adapter .Append (ctx , p2pH1 ))
611+ require .NoError (t , adapter .Append (ctx , wrapHeader (h1 )))
683612
684613 // Get by hash from pending (uses header's Hash() method)
685- retrieved , err := adapter .Get (ctx , p2pH1 .Hash ())
614+ retrieved , err := adapter .Get (ctx , h1 .Hash ())
686615 require .NoError (t , err )
687616 assert .Equal (t , h1 .Height (), retrieved .Height ())
688617}
0 commit comments