Skip to content

Commit fa1a2d2

Browse files
committed
tests for reinit
1 parent 1a7a831 commit fa1a2d2

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

sync/sync_head_test.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ func TestSyncer_TailInit(t *testing.T) {
2929
require.NoError(t, err)
3030

3131
tests := []struct {
32-
name string
33-
option Option
34-
expected func() *headertest.DummyHeader
32+
name string
33+
option Option
34+
expected func() *headertest.DummyHeader
35+
expectedAfterRestart func() *headertest.DummyHeader
3536
}{
3637
{
3738
"Estimate",
@@ -41,27 +42,42 @@ func TestSyncer_TailInit(t *testing.T) {
4142
require.NoError(t, err)
4243
return remoteTail
4344
},
45+
func() *headertest.DummyHeader {
46+
remoteTail, err := remoteStore.Tail(ctx)
47+
require.NoError(t, err)
48+
return remoteTail
49+
},
4450
},
4551
{
4652
"SyncFromHash",
4753
WithSyncFromHash(expectedTail.Hash()),
4854
func() *headertest.DummyHeader {
4955
return expectedTail
5056
},
57+
func() *headertest.DummyHeader {
58+
expectedTail, err := remoteStore.GetByHeight(ctx, expectedTail.Height()+10)
59+
require.NoError(t, err)
60+
return expectedTail
61+
},
5162
},
5263
{
5364
"SyncFromHeight",
5465
WithSyncFromHeight(expectedTail.Height()),
5566
func() *headertest.DummyHeader {
5667
return expectedTail
5768
},
69+
func() *headertest.DummyHeader {
70+
expectedTail, err := remoteStore.GetByHeight(ctx, expectedTail.Height()-10)
71+
require.NoError(t, err)
72+
return expectedTail
73+
},
5874
},
5975
}
6076

6177
for _, test := range tests {
6278
t.Run(test.name, func(t *testing.T) {
6379
ds := dssync.MutexWrap(datastore.NewMapDatastore())
64-
localStore, err := store.NewStore[*headertest.DummyHeader](ds)
80+
localStore, err := store.NewStore[*headertest.DummyHeader](ds, store.WithWriteBatchSize(1))
6581
require.NoError(t, err)
6682
err = localStore.Start(ctx)
6783
require.NoError(t, err)
@@ -77,18 +93,30 @@ func TestSyncer_TailInit(t *testing.T) {
7793

7894
err = syncer.Start(ctx)
7995
require.NoError(t, err)
80-
8196
time.Sleep(time.Millisecond * 100)
8297

98+
// check that the syncer has the expected tail and head
8399
expectedTail := test.expected()
84-
85100
storeTail, err := localStore.Tail(ctx)
86101
require.NoError(t, err)
87102
assert.EqualValues(t, expectedTail.Height(), storeTail.Height())
88-
89103
storeHead, err := localStore.Head(ctx)
90104
require.NoError(t, err)
91105
assert.EqualValues(t, remoteStore.Height(), storeHead.Height())
106+
107+
// restart the Syncer and set a new tail
108+
err = syncer.Stop(ctx)
109+
require.NoError(t, err)
110+
expectedTail = test.expectedAfterRestart()
111+
syncer.Params.SyncFromHeight = expectedTail.Height()
112+
syncer.Params.SyncFromHash = expectedTail.Hash()
113+
err = syncer.Start(ctx)
114+
require.NoError(t, err)
115+
116+
// ensure that the Syncer moved to the new tail after restart
117+
storeTail, err = localStore.Tail(ctx)
118+
require.NoError(t, err)
119+
assert.EqualValues(t, expectedTail.Height(), storeTail.Height())
92120
})
93121
}
94122
}

sync/sync_store.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,41 @@ func (s *syncStore[H]) Append(ctx context.Context, headers ...H) error {
4848
}
4949

5050
head, err := s.Head(ctx)
51-
if err != nil && !errors.Is(err, context.Canceled) && !errors.Is(err, header.ErrEmptyStore) {
51+
if errors.Is(err, header.ErrEmptyStore) {
52+
// short-circuit for an initialization path
53+
if err := s.Store.Append(ctx, headers...); err != nil {
54+
return err
55+
}
56+
57+
s.head.Store(&headers[len(headers)-1])
58+
return nil
59+
}
60+
if err != nil {
5261
return err
5362
}
5463

55-
if !errors.Is(err, header.ErrEmptyStore) {
64+
// TODO(@Wondertan): As store evolved, certain invariants it had were removed.
65+
// However, Syncer has yet to be refactored to not assume those invariants and until then
66+
// this method is a shim that allows using store with old assumptions.
67+
// To be reworked by bsync.
68+
if headers[0].Height() >= head.Height() {
5669
for _, h := range headers {
5770
if h.Height() != head.Height()+1 {
5871
return &errNonAdjacent{
5972
Head: head.Height(),
6073
Attempted: h.Height(),
6174
}
6275
}
76+
6377
head = h
6478
}
79+
80+
s.head.Store(&head)
6581
}
6682

6783
if err := s.Store.Append(ctx, headers...); err != nil {
6884
return err
6985
}
7086

71-
s.head.Store(&headers[len(headers)-1])
7287
return nil
7388
}

0 commit comments

Comments
 (0)