Skip to content

Commit c6e5696

Browse files
committed
fixes
1 parent 19835e8 commit c6e5696

2 files changed

Lines changed: 18 additions & 90 deletions

File tree

pkg/store/data_store_adapter_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,12 @@ func TestDataStoreAdapter_GetFromStore(t *testing.T) {
114114
require.NoError(t, batch.SetHeight(1))
115115
require.NoError(t, batch.Commit())
116116

117-
// Now create adapter and verify we can get from store
117+
// Create adapter after data is in store
118118
adapter := NewDataStoreAdapter(store, testGenesisData())
119119

120-
retrieved, err := adapter.GetByHeight(ctx, 1)
120+
// Get by hash - need to use the index hash (sha256 of marshaled SignedHeader)
121+
hash := computeDataIndexHash(h1)
122+
retrieved, err := adapter.Get(ctx, hash)
121123
require.NoError(t, err)
122124
assert.Equal(t, d1.Height(), retrieved.Height())
123125

@@ -144,14 +146,12 @@ func TestDataStoreAdapter_Has(t *testing.T) {
144146
// Create adapter after data is in store
145147
adapter := NewDataStoreAdapter(store, testGenesisData())
146148

147-
require.NoError(t, adapter.Append(ctx, wrapData(d1)))
148-
149-
// Has should return true for existing hash
150-
has, err := adapter.Has(ctx, d1.Hash())
149+
// Has should return true for existing data - use index hash
150+
has, err := adapter.Has(ctx, computeDataIndexHash(h1))
151151
require.NoError(t, err)
152152
assert.True(t, has)
153153

154-
// Has should return false for non-existent hash
154+
// Has should return false for non-existent
155155
has, err = adapter.Has(ctx, []byte("nonexistent"))
156156
require.NoError(t, err)
157157
assert.False(t, has)
@@ -680,11 +680,10 @@ func TestDataStoreAdapter_GetFromPendingByHash(t *testing.T) {
680680

681681
// Add data to pending
682682
_, d1 := types.GetRandomBlock(1, 1, "test-chain")
683-
p2pD1 := wrapData(d1)
684-
require.NoError(t, adapter.Append(ctx, p2pD1))
683+
require.NoError(t, adapter.Append(ctx, wrapData(d1)))
685684

686685
// Get by hash from pending (uses data's Hash() method)
687-
retrieved, err := adapter.Get(ctx, p2pD1.Hash())
686+
retrieved, err := adapter.Get(ctx, d1.Hash())
688687
require.NoError(t, err)
689688
assert.Equal(t, d1.Height(), retrieved.Height())
690689
}

pkg/store/header_store_adapter_test.go

Lines changed: 9 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package store
33
import (
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-
609539
func 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

Comments
 (0)