Skip to content

Commit 53307d0

Browse files
committed
append missing tail manually
1 parent fa1a2d2 commit 53307d0

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

sync/sync_head.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ func (s *Syncer[H]) Tail(ctx context.Context) (H, error) {
124124
// Configured Tail has changed - get a new one and resolve the diff
125125

126126
currentTail, newTail := tail, tail
127-
128-
if s.Params.SyncFromHash != nil {
127+
switch {
128+
case s.Params.SyncFromHash != nil:
129129
// check first locally if the new Tail exists
130130
newTail, err = s.store.Get(ctx, s.Params.SyncFromHash)
131131
if err != nil {
@@ -138,8 +138,16 @@ func (s *Syncer[H]) Tail(ctx context.Context) (H, error) {
138138
err,
139139
)
140140
}
141+
err = s.store.Append(ctx, newTail)
142+
if err != nil {
143+
return tail, fmt.Errorf(
144+
"appending the new tail header(%d): %w",
145+
newTail.Height(),
146+
err,
147+
)
148+
}
141149
}
142-
} else if s.Params.SyncFromHeight != 0 {
150+
case s.Params.SyncFromHeight != 0:
143151
// check first locally if the new Tail exists
144152
newTail, err = s.store.GetByHeight(ctx, s.Params.SyncFromHeight)
145153
if err != nil {
@@ -152,6 +160,14 @@ func (s *Syncer[H]) Tail(ctx context.Context) (H, error) {
152160
err,
153161
)
154162
}
163+
err = s.store.Append(ctx, newTail)
164+
if err != nil {
165+
return tail, fmt.Errorf(
166+
"appending the new tail header(%d): %w",
167+
newTail.Height(),
168+
err,
169+
)
170+
}
155171
}
156172
}
157173

sync/sync_head_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ func TestSyncer_TailInit(t *testing.T) {
7777
for _, test := range tests {
7878
t.Run(test.name, func(t *testing.T) {
7979
ds := dssync.MutexWrap(datastore.NewMapDatastore())
80-
localStore, err := store.NewStore[*headertest.DummyHeader](ds, store.WithWriteBatchSize(1))
80+
localStore, err := store.NewStore[*headertest.DummyHeader](
81+
ds,
82+
store.WithWriteBatchSize(1),
83+
)
8184
require.NoError(t, err)
8285
err = localStore.Start(ctx)
8386
require.NoError(t, err)
@@ -113,6 +116,8 @@ func TestSyncer_TailInit(t *testing.T) {
113116
err = syncer.Start(ctx)
114117
require.NoError(t, err)
115118

119+
time.Sleep(time.Millisecond * 10)
120+
116121
// ensure that the Syncer moved to the new tail after restart
117122
storeTail, err = localStore.Tail(ctx)
118123
require.NoError(t, err)

0 commit comments

Comments
 (0)