Skip to content

Commit 4c6c240

Browse files
committed
feat(store): store recovery
1 parent 772c0dc commit 4c6c240

File tree

2 files changed

+77
-8
lines changed

2 files changed

+77
-8
lines changed

store/store.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -555,12 +555,9 @@ func (s *Store[H]) setTail(ctx context.Context, batch datastore.Batch, to uint64
555555
log.Infow("new tail", "height", newTail.Height(), "hash", newTail.Hash())
556556
s.metrics.newTail(newTail.Height())
557557

558-
// update head as well, if delete went over it
559-
head, err := s.Head(ctx)
560-
if err != nil {
561-
return err
562-
}
563-
if to > head.Height() {
558+
// update head as well, if delete went over it or head doesn't exist
559+
head, _ := s.Head(ctx)
560+
if head.IsZero() || to > head.Height() {
564561
if err := writeHeaderHashTo(ctx, batch, newTail, headKey); err != nil {
565562
return fmt.Errorf("writing headKey in batch: %w", err)
566563
}
@@ -797,14 +794,16 @@ func (s *Store[H]) nextHead(ctx context.Context) (head H, changed bool) {
797794
return head, false
798795
}
799796

800-
for {
797+
for ctx.Err() == nil {
801798
h, err := s.getByHeight(ctx, head.Height()+1)
802799
if err != nil {
803800
return head, changed
804801
}
805802
head = h
806803
changed = true
807804
}
805+
806+
return head, changed
808807
}
809808

810809
// nextTail finds the new contiguous Tail by iterating the current Tail down until the older height Tail is found.
@@ -816,14 +815,16 @@ func (s *Store[H]) nextTail(ctx context.Context) (tail H, changed bool) {
816815
return tail, false
817816
}
818817

819-
for {
818+
for ctx.Err() == nil {
820819
h, err := s.getByHeight(ctx, tail.Height()-1)
821820
if err != nil {
822821
return tail, changed
823822
}
824823
tail = h
825824
changed = true
826825
}
826+
827+
return tail, changed
827828
}
828829

829830
func (s *Store[H]) loadHeadAndTail(ctx context.Context) error {

store/store_recover.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package store
2+
3+
import (
4+
"context"
5+
6+
"github.com/celestiaorg/go-header"
7+
)
8+
9+
// ResetTail resets the tail of the store to be at the given height.
10+
// The new tail must be present in the store.
11+
// WARNING: Only use this function if you know what you are doing.
12+
func ResetTail[H header.Header[H]](ctx context.Context, store *Store[H], height uint64) error {
13+
batch, err := store.ds.Batch(ctx)
14+
if err != nil {
15+
return err
16+
}
17+
18+
err = store.setTail(ctx, batch, height)
19+
if err != nil {
20+
return err
21+
}
22+
23+
err = batch.Commit(ctx)
24+
if err != nil {
25+
return err
26+
}
27+
return nil
28+
}
29+
30+
// ResetHead resets the head of the store to be at the given height.
31+
// The new head must be present in the store.
32+
// WARNING: Only use this function if you know what you are doing.
33+
func ResetHead[H header.Header[H]](ctx context.Context, store *Store[H], height uint64) error {
34+
batch, err := store.ds.Batch(ctx)
35+
if err != nil {
36+
return err
37+
}
38+
39+
newHead, err := store.getByHeight(ctx, height)
40+
if err != nil {
41+
return err
42+
}
43+
44+
if err := writeHeaderHashTo(ctx, batch, newHead, headKey); err != nil {
45+
return err
46+
}
47+
store.contiguousHead.Store(&newHead)
48+
49+
err = batch.Commit(ctx)
50+
if err != nil {
51+
return err
52+
}
53+
return nil
54+
}
55+
56+
// FindHeader forward iterates over the store starting from the given height until it finds any stored header
57+
// or the context is cancelled.
58+
func FindHeader[H header.Header[H]](ctx context.Context, store *Store[H], startFrom uint64) (H, error) {
59+
for height := startFrom; ctx.Err() == nil; height++ {
60+
header, err := store.getByHeight(ctx, height)
61+
if err == nil {
62+
return header, nil
63+
}
64+
}
65+
66+
var zero H
67+
return zero, ctx.Err()
68+
}

0 commit comments

Comments
 (0)