Skip to content

Commit

Permalink
review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Jan 24, 2025
1 parent b506084 commit 7b5b282
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
24 changes: 6 additions & 18 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,7 @@ func (s *Store[H]) Stop(ctx context.Context) error {
}

func (s *Store[H]) Height() uint64 {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

head, err := s.Head(ctx)
if err != nil {
if errors.Is(err, context.Canceled) ||
errors.Is(err, context.DeadlineExceeded) ||
errors.Is(err, datastore.ErrNotFound) {
return 0
}
panic(err)
}
return head.Height()
return s.heightSub.Height()
}

func (s *Store[H]) Head(ctx context.Context, _ ...header.HeadOption[H]) (H, error) {
Expand Down Expand Up @@ -231,8 +219,8 @@ func (s *Store[H]) GetByHeight(ctx context.Context, height uint64) (H, error) {
return zero, errors.New("header/store: height must be bigger than zero")
}

if h, err := s.getByHeight(ctx, height); err == nil || ctx.Err() != nil {
return h, err
if h, err := s.getByHeight(ctx, height); err == nil {
return h, nil
}

// if the requested 'height' was not yet published
Expand Down Expand Up @@ -537,17 +525,17 @@ func (s *Store[H]) advanceContiguousHead(ctx context.Context, headers ...H) {
}

if currHeight > prevHeight {
s.updateContiguousHead(newHead, currHeight)
s.updateContiguousHead(ctx, newHead, currHeight)
}
}

func (s *Store[H]) updateContiguousHead(newHead H, newHeight uint64) {
func (s *Store[H]) updateContiguousHead(ctx context.Context, newHead H, newHeight uint64) {
s.contiguousHead.Store(&newHead)
s.heightSub.SetHeight(newHeight)
log.Infow("new head", "height", newHead.Height(), "hash", newHead.Hash())
s.metrics.newHead(newHead.Height())

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

b, err := newHead.Hash().MarshalJSON()
Expand Down
28 changes: 28 additions & 0 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,34 @@ func TestStoreGetByHeight_whenGaps(t *testing.T) {
}
}

func TestStoreGetByHeight_earlyAvailable(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
t.Cleanup(cancel)

suite := headertest.NewTestSuite(t)

ds := sync.MutexWrap(datastore.NewMapDatastore())
store := NewTestStore(t, ctx, ds, suite.Head(), WithWriteBatchSize(10))

const skippedHeaders = 15
suite.GenDummyHeaders(skippedHeaders)
lastChunk := suite.GenDummyHeaders(1)

{
err := store.Append(ctx, lastChunk...)
require.NoError(t, err)

// wait for batch to be written.
time.Sleep(100 * time.Millisecond)
}

{
h, err := store.GetByHeight(ctx, lastChunk[0].Height())
require.NoError(t, err)
require.Equal(t, h, lastChunk[0])
}
}

// TestStore_GetRange tests possible combinations of requests and ensures that
// the store can handle them adequately (even malformed requests)
func TestStore_GetRange(t *testing.T) {
Expand Down

0 comments on commit 7b5b282

Please sign in to comment.