Skip to content

Commit 6b9671f

Browse files
cristalolegWondertan
authored andcommitted
fix(store): return header.ErrNotFound where appropriate (#277)
There are some places when we return error from datastore. However, there are places where we replace not found error with our `header.ErrNotFound`. Aligning all datastore calls to the same behaviour. Also, changed 2 places to use `hashKey` helper.
1 parent f58fe79 commit 6b9671f

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

store/store.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (s *Store[H]) Start(ctx context.Context) error {
144144

145145
if err := s.loadContiguousHead(ctx); err != nil {
146146
// we might start on an empty datastore, no key is okay.
147-
if !errors.Is(err, datastore.ErrNotFound) {
147+
if !errors.Is(err, header.ErrNotFound) {
148148
return fmt.Errorf("header/store: cannot load headKey: %w", err)
149149
}
150150
}
@@ -338,7 +338,11 @@ func (s *Store[H]) Has(ctx context.Context, hash header.Hash) (bool, error) {
338338
return ok, nil
339339
}
340340

341-
return s.ds.Has(ctx, datastore.NewKey(hash.String()))
341+
ok, err := s.ds.Has(ctx, hashKey(hash))
342+
if errors.Is(err, datastore.ErrNotFound) {
343+
return false, header.ErrNotFound
344+
}
345+
return ok, err
342346
}
343347

344348
func (s *Store[H]) HasAt(_ context.Context, height uint64) bool {
@@ -581,6 +585,9 @@ func (s *Store[H]) readHead(ctx context.Context) (H, error) {
581585
var zero H
582586
b, err := s.ds.Get(ctx, headKey)
583587
if err != nil {
588+
if errors.Is(err, datastore.ErrNotFound) {
589+
return zero, header.ErrNotFound
590+
}
584591
return zero, err
585592
}
586593

@@ -598,6 +605,9 @@ func (s *Store[H]) readTail(ctx context.Context) (H, error) {
598605
var zero H
599606
b, err := s.ds.Get(ctx, tailKey)
600607
if err != nil {
608+
if errors.Is(err, datastore.ErrNotFound) {
609+
return zero, header.ErrNotFound
610+
}
601611
return zero, err
602612
}
603613

@@ -611,7 +621,7 @@ func (s *Store[H]) readTail(ctx context.Context) (H, error) {
611621

612622
func (s *Store[H]) get(ctx context.Context, hash header.Hash) ([]byte, error) {
613623
startTime := time.Now()
614-
data, err := s.ds.Get(ctx, datastore.NewKey(hash.String()))
624+
data, err := s.ds.Get(ctx, hashKey(hash))
615625
if err != nil {
616626
s.metrics.readSingle(ctx, time.Since(startTime), true)
617627
if errors.Is(err, datastore.ErrNotFound) {

0 commit comments

Comments
 (0)