Skip to content

Commit

Permalink
feat(store,sync): drop NewStoreWithHead
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed May 29, 2024
1 parent ea28e37 commit b9f86f6
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 57 deletions.
2 changes: 1 addition & 1 deletion store/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func TestInitStore_NoReinit(t *testing.T) {

suite := headertest.NewTestSuite(t)
head := suite.Head()
exchange := local.NewExchange(NewTestStore(ctx, t, head))

ds := sync.MutexWrap(datastore.NewMapDatastore())
exchange := local.NewExchange(newStoreWithHead(t, ctx, ds, head))
store, err := NewStore[*headertest.DummyHeader](ds)
require.NoError(t, err)

Expand Down
25 changes: 10 additions & 15 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ func TestStore(t *testing.T) {
suite := headertest.NewTestSuite(t)

ds := sync.MutexWrap(datastore.NewMapDatastore())
store, err := NewStoreWithHead(ctx, ds, suite.Head())
require.NoError(t, err)
store := newStoreWithHead(t, ctx, ds, suite.Head())

err = store.Start(ctx)
err := store.Start(ctx)
require.NoError(t, err)

head, err := store.Head(ctx)
Expand Down Expand Up @@ -107,10 +106,9 @@ func TestStore_GetRangeByHeight_ExpectedRange(t *testing.T) {
suite := headertest.NewTestSuite(t)

ds := sync.MutexWrap(datastore.NewMapDatastore())
store, err := NewStoreWithHead(ctx, ds, suite.Head())
require.NoError(t, err)
store := newStoreWithHead(t, ctx, ds, suite.Head())

err = store.Start(ctx)
err := store.Start(ctx)
require.NoError(t, err)

head, err := store.Head(ctx)
Expand Down Expand Up @@ -143,10 +141,9 @@ func TestStore_Append_BadHeader(t *testing.T) {
suite := headertest.NewTestSuite(t)

ds := sync.MutexWrap(datastore.NewMapDatastore())
store, err := NewStoreWithHead(ctx, ds, suite.Head())
require.NoError(t, err)
store := newStoreWithHead(t, ctx, ds, suite.Head())

err = store.Start(ctx)
err := store.Start(ctx)
require.NoError(t, err)

head, err := store.Head(ctx)
Expand All @@ -168,10 +165,9 @@ func TestStore_GetRange(t *testing.T) {
suite := headertest.NewTestSuite(t)

ds := sync.MutexWrap(datastore.NewMapDatastore())
store, err := NewStoreWithHead(ctx, ds, suite.Head())
require.NoError(t, err)
store := newStoreWithHead(t, ctx, ds, suite.Head())

err = store.Start(ctx)
err := store.Start(ctx)
require.NoError(t, err)

head, err := store.Head(ctx)
Expand Down Expand Up @@ -245,13 +241,12 @@ func TestStorePendingCacheMiss(t *testing.T) {

ds := sync.MutexWrap(datastore.NewMapDatastore())

store, err := NewStoreWithHead(ctx, ds, suite.Head(),
store := newStoreWithHead(t, ctx, ds, suite.Head(),
WithWriteBatchSize(100),
WithStoreCacheSize(100),
)
require.NoError(t, err)

err = store.Start(ctx)
err := store.Start(ctx)
require.NoError(t, err)
err = store.Append(ctx, suite.GenDummyHeaders(100)...)
require.NoError(t, err)
Expand Down
24 changes: 12 additions & 12 deletions store/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import (
"testing"

"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/sync"
"github.com/stretchr/testify/require"

"github.com/celestiaorg/go-header"
"github.com/celestiaorg/go-header/headertest"
)

// NewTestStore creates initialized and started in memory header Store which is useful for testing.
func NewTestStore(ctx context.Context, t *testing.T, head *headertest.DummyHeader) header.Store[*headertest.DummyHeader] {
store, err := NewStoreWithHead(ctx, sync.MutexWrap(datastore.NewMapDatastore()), head)
require.NoError(t, err)
// NewStoreWithHead initiates a new Store and forcefully sets a given trusted header as head.
func newStoreWithHead[H header.Header[H]](
tb testing.TB,
ctx context.Context,
ds datastore.Batching,
head H,
opts ...Option,
) *Store[H] {
store, err := newStore[H](ds, opts...)
require.NoError(tb, err)

err = store.Start(ctx)
require.NoError(t, err)
err = store.Init(ctx, head)
require.NoError(tb, err)

t.Cleanup(func() {
err := store.Stop(ctx)
require.NoError(t, err)
})
return store
}
10 changes: 3 additions & 7 deletions sync/sync_head_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ import (
"testing"
"time"

"github.com/ipfs/go-datastore"
sync2 "github.com/ipfs/go-datastore/sync"
"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"
"github.com/celestiaorg/go-header/store"
)

func TestSyncer_incomingNetworkHeadRaces(t *testing.T) {
Expand Down Expand Up @@ -61,11 +58,10 @@ func TestSyncer_HeadWithTrustedHead(t *testing.T) {
suite := headertest.NewTestSuite(t)
head := suite.Head()

localStore := store.NewTestStore(ctx, t, head)
localStore := newTestStore(t, ctx, head)
remoteStore := newTestStore(t, ctx, head)

remoteStore, err := store.NewStoreWithHead(ctx, sync2.MutexWrap(datastore.NewMapDatastore()), head)
require.NoError(t, err)
err = remoteStore.Append(ctx, suite.GenDummyHeaders(100)...)
err := remoteStore.Append(ctx, suite.GenDummyHeaders(100)...)
require.NoError(t, err)

// create a wrappedGetter to track exchange interactions
Expand Down
64 changes: 42 additions & 22 deletions sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"
"time"

"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/sync"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand All @@ -21,15 +23,15 @@ func TestSyncSimpleRequestingHead(t *testing.T) {
suite := headertest.NewTestSuite(t)
head := suite.Head()

remoteStore := store.NewTestStore(ctx, t, head)
remoteStore := newTestStore(t, ctx, head)
err := remoteStore.Append(ctx, suite.GenDummyHeaders(100)...)
require.NoError(t, err)

_, err = remoteStore.GetByHeight(ctx, 100)
require.NoError(t, err)

localStore := store.NewTestStore(ctx, t, head)
syncer, err := NewSyncer[*headertest.DummyHeader](
localStore := newTestStore(t, ctx, head)
syncer, err := NewSyncer(
local.NewExchange(remoteStore),
localStore,
headertest.NewDummySubscriber(),
Expand Down Expand Up @@ -67,9 +69,9 @@ func TestDoSyncFullRangeFromExternalPeer(t *testing.T) {
suite := headertest.NewTestSuite(t)
head := suite.Head()

remoteStore := store.NewTestStore(ctx, t, head)
localStore := store.NewTestStore(ctx, t, head)
syncer, err := NewSyncer[*headertest.DummyHeader](
remoteStore := newTestStore(t, ctx, head)
localStore := newTestStore(t, ctx, head)
syncer, err := NewSyncer(
local.NewExchange(remoteStore),
localStore,
headertest.NewDummySubscriber(),
Expand Down Expand Up @@ -106,9 +108,9 @@ func TestSyncCatchUp(t *testing.T) {
suite := headertest.NewTestSuite(t)
head := suite.Head()

remoteStore := store.NewTestStore(ctx, t, head)
localStore := store.NewTestStore(ctx, t, head)
syncer, err := NewSyncer[*headertest.DummyHeader](
remoteStore := newTestStore(t, ctx, head)
localStore := newTestStore(t, ctx, head)
syncer, err := NewSyncer(
local.NewExchange(remoteStore),
localStore,
headertest.NewDummySubscriber(),
Expand Down Expand Up @@ -157,9 +159,9 @@ func TestSyncPendingRangesWithMisses(t *testing.T) {
suite := headertest.NewTestSuite(t)
head := suite.Head()

remoteStore := store.NewTestStore(ctx, t, head)
localStore := store.NewTestStore(ctx, t, head)
syncer, err := NewSyncer[*headertest.DummyHeader](
remoteStore := newTestStore(t, ctx, head)
localStore := newTestStore(t, ctx, head)
syncer, err := NewSyncer(
local.NewExchange(remoteStore),
localStore,
headertest.NewDummySubscriber(),
Expand Down Expand Up @@ -224,9 +226,9 @@ func TestSyncer_FindHeadersReturnsCorrectRange(t *testing.T) {
suite := headertest.NewTestSuite(t)
head := suite.Head()

remoteStore := store.NewTestStore(ctx, t, head)
localStore := store.NewTestStore(ctx, t, head)
syncer, err := NewSyncer[*headertest.DummyHeader](
remoteStore := newTestStore(t, ctx, head)
localStore := newTestStore(t, ctx, head)
syncer, err := NewSyncer(
local.NewExchange(remoteStore),
localStore,
headertest.NewDummySubscriber(),
Expand Down Expand Up @@ -260,9 +262,9 @@ func TestSyncerIncomingDuplicate(t *testing.T) {
suite := headertest.NewTestSuite(t)
head := suite.Head()

remoteStore := store.NewTestStore(ctx, t, head)
localStore := store.NewTestStore(ctx, t, head)
syncer, err := NewSyncer[*headertest.DummyHeader](
remoteStore := newTestStore(t, ctx, head)
localStore := newTestStore(t, ctx, head)
syncer, err := NewSyncer(
&delayedGetter[*headertest.DummyHeader]{Getter: local.NewExchange(remoteStore)},
localStore,
headertest.NewDummySubscriber(),
Expand Down Expand Up @@ -301,12 +303,12 @@ func TestSync_InvalidSyncTarget(t *testing.T) {
head := suite.Head()

// create a local store which is initialised at genesis height
localStore := store.NewTestStore(ctx, t, head)
localStore := newTestStore(t, ctx, head)
// create a peer which is already on height 100
remoteStore := headertest.NewStore[*headertest.DummyHeader](t, suite, 100)
remoteStore := headertest.NewStore(t, suite, 100)

syncer, err := NewSyncer[*headertest.DummyHeader](
local.NewExchange[*headertest.DummyHeader](remoteStore),
syncer, err := NewSyncer(
local.NewExchange(remoteStore),
localStore,
headertest.NewDummySubscriber(),
WithBlockTime(time.Nanosecond),
Expand Down Expand Up @@ -396,3 +398,21 @@ func (d *delayedGetter[H]) GetRangeByHeight(ctx context.Context, from H, to uint
return nil, ctx.Err()
}
}

// newTestStore creates initialized and started in memory header Store which is useful for testing.
func newTestStore(tb testing.TB, ctx context.Context, head *headertest.DummyHeader) header.Store[*headertest.DummyHeader] {
store, err := store.NewStore[*headertest.DummyHeader](sync.MutexWrap(datastore.NewMapDatastore()))
require.NoError(tb, err)

err = store.Init(ctx, head)
require.NoError(tb, err)

err = store.Start(ctx)
require.NoError(tb, err)

tb.Cleanup(func() {
err := store.Stop(ctx)
require.NoError(tb, err)
})
return store
}

0 comments on commit b9f86f6

Please sign in to comment.