Skip to content

Commit

Permalink
!feat(store): drop Init func
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed May 29, 2024
1 parent ea28e37 commit fd789bc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
25 changes: 0 additions & 25 deletions store/init.go

This file was deleted.

22 changes: 21 additions & 1 deletion store/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package store

import (
"context"
"errors"
"testing"
"time"

Expand All @@ -10,6 +11,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/celestiaorg/go-header"
"github.com/celestiaorg/go-header/headertest"
"github.com/celestiaorg/go-header/local"
)
Expand All @@ -26,7 +28,7 @@ func TestInitStore_NoReinit(t *testing.T) {
store, err := NewStore[*headertest.DummyHeader](ds)
require.NoError(t, err)

err = Init[*headertest.DummyHeader](ctx, store, exchange, head.Hash())
err = initStore(ctx, store, exchange, head.Hash())
assert.NoError(t, err)

err = store.Start(ctx)
Expand Down Expand Up @@ -54,3 +56,21 @@ func TestInitStore_NoReinit(t *testing.T) {
err = reopenedStore.Stop(ctx)
require.NoError(t, err)
}

// initStore ensures a Store is initialized.
// If it is not already initialized, it initializes the Store by requesting the header with the given hash.
func initStore[H header.Header[H]](ctx context.Context, store header.Store[H], ex header.Exchange[H], hash header.Hash) error {
_, err := store.Head(ctx)
if err == nil {
return nil
}

if errors.Is(err, header.ErrNoHead) {
initial, err := ex.Get(ctx, hash)
if err != nil {
return err
}
return store.Init(ctx, initial)
}
return err
}

0 comments on commit fd789bc

Please sign in to comment.