Skip to content

Commit 34d9ef5

Browse files
committed
fix head
1 parent 5473b09 commit 34d9ef5

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

sync/sync_head.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ func (s *Syncer[H]) Tail(ctx context.Context) (H, error) {
6161
tail, err := s.store.Tail(ctx)
6262
switch {
6363
case errors.Is(err, header.ErrEmptyStore):
64+
// TODO(@Wondertan): This is a temporary solution requesting the head directly from the network instead of
65+
// calling general Head path. This is needed to ensure Tail is written to the store first.
66+
head, err := s.head.Head(ctx)
67+
if err != nil {
68+
return head, err
69+
}
70+
6471
switch {
6572
case s.Params.SyncFromHash != nil:
6673
tail, err = s.getter.Get(ctx, s.Params.SyncFromHash)
@@ -73,23 +80,23 @@ func (s *Syncer[H]) Tail(ctx context.Context) (H, error) {
7380
return tail, fmt.Errorf("getting tail header(%d): %w", s.Params.SyncFromHeight, err)
7481
}
7582
default:
76-
head, err := s.Head(ctx)
77-
if err != nil {
78-
return head, err
79-
}
80-
8183
tailHeight := estimateTail(head, s.Params.blockTime, s.Params.TrustingPeriod)
8284
tail, err = s.getter.GetByHeight(ctx, tailHeight)
8385
if err != nil {
8486
return tail, fmt.Errorf("getting estimated tail header(%d): %w", tailHeight, err)
8587
}
8688
}
8789

88-
err = s.store.Store.Append(ctx, tail)
90+
err = s.store.Append(ctx, tail)
8991
if err != nil {
9092
return tail, fmt.Errorf("appending tail header: %w", err)
9193
}
9294

95+
err = s.incomingNetworkHead(ctx, head)
96+
if err != nil {
97+
return tail, fmt.Errorf("applying head from trusted peers: %w", err)
98+
}
99+
93100
case !s.isTailActual(tail):
94101
if s.Params.SyncFromHash != nil {
95102
tail, err = s.getter.Get(ctx, s.Params.SyncFromHash)

sync/sync_head_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestSyncer_Tail(t *testing.T) {
2323
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
2424
t.Cleanup(cancel)
2525

26-
remoteStore := headertest.NewDummyStore(t)
26+
remoteStore := headertest.NewStore[*headertest.DummyHeader](t, headertest.NewTestSuite(t), 100)
2727

2828
ds := dssync.MutexWrap(datastore.NewMapDatastore())
2929
localStore, err := store.NewStore[*headertest.DummyHeader](ds)
@@ -44,9 +44,17 @@ func TestSyncer_Tail(t *testing.T) {
4444
require.NoError(t, err)
4545
assert.NotNil(t, tail)
4646

47+
err = syncer.Start(ctx)
48+
require.NoError(t, err)
49+
50+
time.Sleep(time.Millisecond * 100)
4751
storeTail, err := localStore.Tail(ctx)
4852
require.NoError(t, err)
4953
assert.EqualValues(t, tail.Height(), storeTail.Height())
54+
55+
storeHead, err := localStore.Head(ctx)
56+
require.NoError(t, err)
57+
assert.EqualValues(t, remoteStore.Height(), storeHead.Height())
5058
}
5159

5260
func TestSyncer_HeadConcurrencyError(t *testing.T) {
@@ -435,7 +443,7 @@ type errorGetter struct{}
435443

436444
func (e errorGetter) Head(
437445
context.Context,
438-
...header.HeadOption[*headertest.DummyHeader],
446+
...header.HeadOption[*headertest.DummyHeader],
439447
) (*headertest.DummyHeader, error) {
440448
time.Sleep(time.Millisecond * 1)
441449
return nil, fmt.Errorf("error")

sync/sync_store.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,20 @@ 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) {
52-
panic(err)
51+
if err != nil && !errors.Is(err, context.Canceled) && !errors.Is(err, header.ErrEmptyStore) {
52+
return err
5353
}
5454

55-
for _, h := range headers {
56-
if h.Height() != head.Height()+1 {
57-
return &errNonAdjacent{
58-
Head: head.Height(),
59-
Attempted: h.Height(),
55+
if !errors.Is(err, header.ErrEmptyStore) {
56+
for _, h := range headers {
57+
if h.Height() != head.Height()+1 {
58+
return &errNonAdjacent{
59+
Head: head.Height(),
60+
Attempted: h.Height(),
61+
}
6062
}
63+
head = h
6164
}
62-
head = h
6365
}
6466

6567
if err := s.Store.Append(ctx, headers...); err != nil {

0 commit comments

Comments
 (0)