Skip to content

Commit

Permalink
feat(store): add readTail method (#215)
Browse files Browse the repository at this point in the history
## Overview

Adding method that will return tail header from the underlying datastore
when `Store[H]` will be started. Similar to `readHead` method in
`Store[H]`.

Should be merged after #210
  • Loading branch information
cristaloleg authored Aug 13, 2024
1 parent 7d9aad6 commit 00be07c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion store/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
"github.com/celestiaorg/go-header"
)

var headKey = datastore.NewKey("head")
var (
headKey = datastore.NewKey("head")
tailKey = datastore.NewKey("tail")
)

func heightKey(h uint64) datastore.Key {
return datastore.NewKey(strconv.Itoa(int(h)))
Expand Down
16 changes: 16 additions & 0 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,22 @@ func (s *Store[H]) readHead(ctx context.Context) (H, error) {
return s.Get(ctx, head)
}

// readTail loads the tail from the datastore.
func (s *Store[H]) readTail(ctx context.Context) (H, error) {
var zero H
b, err := s.ds.Get(ctx, tailKey)
if err != nil {
return zero, err
}

var tail header.Hash
if err := tail.UnmarshalJSON(b); err != nil {
return zero, err
}

return s.Get(ctx, tail)
}

func (s *Store[H]) get(ctx context.Context, hash header.Hash) ([]byte, error) {
startTime := time.Now()
data, err := s.ds.Get(ctx, datastore.NewKey(hash.String()))
Expand Down

0 comments on commit 00be07c

Please sign in to comment.