Skip to content

Commit ac08c4e

Browse files
committed
fix(store): Tail aware HasAt
1 parent 31ef719 commit ac08c4e

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

store/store.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,22 @@ func (s *Store[H]) Has(ctx context.Context, hash header.Hash) (bool, error) {
324324
return ok, err
325325
}
326326

327-
func (s *Store[H]) HasAt(_ context.Context, height uint64) bool {
328-
return height != uint64(0) && s.Height() >= height
327+
func (s *Store[H]) HasAt(ctx context.Context, height uint64) bool {
328+
if height == uint64(0) {
329+
return false
330+
}
331+
332+
head, err := s.Head(ctx)
333+
if err != nil {
334+
return false
335+
}
336+
337+
tail, err := s.Tail(ctx)
338+
if err != nil {
339+
return false
340+
}
341+
342+
return head.Height() >= height && height >= tail.Height()
329343
}
330344

331345
// DeleteTo implements [header.Store] interface.

store/store_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,3 +829,41 @@ func TestStoreInit(t *testing.T) {
829829
err = reopenedStore.Stop(ctx)
830830
require.NoError(t, err)
831831
}
832+
833+
func TestStore_HasAt(t *testing.T) {
834+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
835+
t.Cleanup(cancel)
836+
837+
suite := headertest.NewTestSuite(t)
838+
839+
ds := sync.MutexWrap(datastore.NewMapDatastore())
840+
store, err := NewStore[*headertest.DummyHeader](ds)
841+
require.NoError(t, err)
842+
843+
err = store.Start(ctx)
844+
require.NoError(t, err)
845+
846+
headers := suite.GenDummyHeaders(100)
847+
848+
err = store.Append(ctx, headers...)
849+
require.NoError(t, err)
850+
time.Sleep(100 * time.Millisecond)
851+
852+
err = store.DeleteTo(ctx, 50)
853+
require.NoError(t, err)
854+
855+
has := store.HasAt(ctx, 100)
856+
assert.True(t, has)
857+
858+
has = store.HasAt(ctx, 50)
859+
assert.True(t, has)
860+
861+
has = store.HasAt(ctx, 49)
862+
assert.False(t, has)
863+
864+
has = store.HasAt(ctx, 10)
865+
assert.False(t, has)
866+
867+
has = store.HasAt(ctx, 0)
868+
assert.False(t, has)
869+
}

0 commit comments

Comments
 (0)