Skip to content

Commit

Permalink
introduct getByHeight
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Oct 9, 2024
1 parent 61a6b31 commit a197550
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
12 changes: 9 additions & 3 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,16 @@ func (s *Store[H]) GetByHeight(ctx context.Context, height uint64) (H, error) {
return h, nil
}

return s.getByHeight(ctx, height)
}

func (s *Store[H]) getByHeight(ctx context.Context, height uint64) (H, error) {
var zero H
hash, err := s.heightIndex.HashByHeight(ctx, height)
if err != nil {
if errors.Is(err, datastore.ErrNotFound) {
return zero, header.ErrNotFound
}

return zero, err
}

Expand Down Expand Up @@ -519,7 +523,8 @@ func (s *Store[H]) tryAdvanceHead(ctx context.Context, headers ...H) {
if headers[i].Height() != currHeight+1 {
break
}
s.writeHead.Store(&headers[i])
newHead := headers[i]
s.writeHead.Store(&newHead)
currHeight++
}

Expand All @@ -528,10 +533,11 @@ func (s *Store[H]) tryAdvanceHead(ctx context.Context, headers ...H) {

// advance based on already written headers.
for {
newHead, err := s.GetByHeight(ctx, currHeight+1)
h, err := s.getByHeight(ctx, currHeight+1)
if err != nil {
break
}
newHead := h
s.writeHead.Store(&newHead)
currHeight++
}
Expand Down
3 changes: 3 additions & 0 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func TestStore_Append(t *testing.T) {

head, err = store.Head(ctx)
assert.NoError(t, err)
assert.Equal(t, head.Height(), headers[len(headers)-1].Height())
assert.Equal(t, head.Hash(), headers[len(headers)-1].Hash())
}

Expand Down Expand Up @@ -222,6 +223,7 @@ func TestStore_Append_stableHeadWhenGaps(t *testing.T) {
// head is advanced to the last known header.
head, err := store.Head(ctx)
require.NoError(t, err)
assert.Equal(t, head.Height(), wantHead.Height())
assert.Equal(t, head.Hash(), wantHead.Hash())

// check that store height is aligned with the head.
Expand Down Expand Up @@ -253,6 +255,7 @@ func TestStore_Append_stableHeadWhenGaps(t *testing.T) {
// after appending missing headers we're on the latest header.
head, err := store.Head(ctx)
require.NoError(t, err)
assert.Equal(t, head.Height(), latestHead.Height())
assert.Equal(t, head.Hash(), latestHead.Hash())

// check that store height is aligned with the head.
Expand Down

0 comments on commit a197550

Please sign in to comment.