diff --git a/store/init.go b/store/init.go deleted file mode 100644 index 5d26a97..0000000 --- a/store/init.go +++ /dev/null @@ -1,25 +0,0 @@ -package store - -import ( - "context" - "errors" - - "github.com/celestiaorg/go-header" -) - -// Init ensures a Store is initialized. If it is not already initialized, -// it initializes the Store by requesting the header with the given hash. -func Init[H header.Header[H]](ctx context.Context, store header.Store[H], ex header.Exchange[H], hash header.Hash) error { - _, err := store.Head(ctx) - switch { - default: - return err - case errors.Is(err, header.ErrNoHead): - initial, err := ex.Get(ctx, hash) - if err != nil { - return err - } - - return store.Init(ctx, initial) - } -} diff --git a/store/init_test.go b/store/init_test.go index bad02ea..9673602 100644 --- a/store/init_test.go +++ b/store/init_test.go @@ -2,6 +2,7 @@ package store import ( "context" + "errors" "testing" "time" @@ -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" ) @@ -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) @@ -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 +}