Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

!feat(store): drop Init function #190

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading