-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathsyncer_backoff_test.go
More file actions
355 lines (299 loc) · 11.3 KB
/
Copy pathsyncer_backoff_test.go
File metadata and controls
355 lines (299 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package syncing
import (
"context"
"errors"
"testing"
"testing/synctest"
"time"
"github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/evstack/ev-node/block/internal/cache"
"github.com/evstack/ev-node/block/internal/common"
"github.com/evstack/ev-node/core/execution"
"github.com/evstack/ev-node/pkg/config"
datypes "github.com/evstack/ev-node/pkg/da/types"
"github.com/evstack/ev-node/pkg/genesis"
"github.com/evstack/ev-node/pkg/store"
extmocks "github.com/evstack/ev-node/test/mocks/external"
"github.com/evstack/ev-node/types"
)
// TestSyncer_BackoffOnDAError verifies that the syncer implements proper backoff
// behavior when encountering different types of DA layer errors.
func TestSyncer_BackoffOnDAError(t *testing.T) {
tests := map[string]struct {
daBlockTime time.Duration
error error
expectsBackoff bool
description string
}{
"generic_error_triggers_backoff": {
daBlockTime: 1 * time.Second,
error: errors.New("network failure"),
expectsBackoff: true,
description: "Generic DA errors should trigger backoff",
},
"height_from_future_triggers_backoff": {
daBlockTime: 500 * time.Millisecond,
error: datypes.ErrHeightFromFuture,
expectsBackoff: true,
description: "Height from future should trigger backoff",
},
"blob_not_found_no_backoff": {
daBlockTime: 1 * time.Second,
error: datypes.ErrBlobNotFound,
expectsBackoff: false,
description: "ErrBlobNotFound should not trigger backoff",
},
"zero_block_time_fallback": {
daBlockTime: 0, // Should fallback to 2s
error: errors.New("some error"),
expectsBackoff: true,
description: "Zero block time should use 2s fallback",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
defer cancel()
// Setup syncer
syncer := setupTestSyncer(t, tc.daBlockTime)
syncer.ctx = ctx
// Setup mocks
daRetriever := NewMockDARetriever(t)
p2pHandler := newMockp2pHandler(t)
p2pHandler.On("ProcessHeight", mock.Anything, mock.Anything, mock.Anything).Return(nil).Maybe()
syncer.daRetriever = daRetriever
syncer.p2pHandler = p2pHandler
p2pHandler.On("SetProcessedHeight", mock.Anything).Return().Maybe()
// Mock PopPriorityHeight to always return 0 (no priority heights)
daRetriever.On("PopPriorityHeight").Return(uint64(0)).Maybe()
// Create mock stores for P2P
mockHeaderStore := extmocks.NewMockStore[*types.SignedHeader](t)
mockHeaderStore.EXPECT().Height().Return(uint64(0)).Maybe()
mockDataStore := extmocks.NewMockStore[*types.Data](t)
mockDataStore.EXPECT().Height().Return(uint64(0)).Maybe()
var callTimes []time.Time
callCount := 0
// First call - returns test error
daRetriever.On("RetrieveFromDA", mock.Anything, uint64(100)).
Run(func(args mock.Arguments) {
callTimes = append(callTimes, time.Now())
callCount++
}).
Return(nil, tc.error).Once()
if tc.expectsBackoff {
// Second call should be delayed due to backoff
daRetriever.On("RetrieveFromDA", mock.Anything, uint64(100)).
Run(func(args mock.Arguments) {
callTimes = append(callTimes, time.Now())
callCount++
// Cancel to end test
cancel()
}).
Return(nil, datypes.ErrBlobNotFound).Once()
} else {
// For ErrBlobNotFound, DA height should increment
daRetriever.On("RetrieveFromDA", mock.Anything, uint64(101)).
Run(func(args mock.Arguments) {
callTimes = append(callTimes, time.Now())
callCount++
cancel()
}).
Return(nil, datypes.ErrBlobNotFound).Once()
}
// Run sync loop
syncer.startSyncWorkers(t.Context())
<-ctx.Done()
syncer.wg.Wait()
// Verify behavior
if tc.expectsBackoff {
require.Len(t, callTimes, 2, "should make exactly 2 calls with backoff")
timeBetweenCalls := callTimes[1].Sub(callTimes[0])
expectedDelay := tc.daBlockTime
if expectedDelay == 0 {
expectedDelay = 2 * time.Second
}
assert.GreaterOrEqual(t, timeBetweenCalls, expectedDelay,
"second call should be delayed by backoff duration (expected ~%v, got %v)",
expectedDelay, timeBetweenCalls)
} else {
assert.GreaterOrEqual(t, callCount, 2, "should continue without significant delay")
if len(callTimes) >= 2 {
timeBetweenCalls := callTimes[1].Sub(callTimes[0])
assert.Less(t, timeBetweenCalls, 120*time.Millisecond,
"should not have backoff delay for ErrBlobNotFound")
}
}
})
})
}
}
// TestSyncer_BackoffResetOnSuccess verifies that backoff is properly reset
// after a successful DA retrieval, allowing the syncer to continue at normal speed.
func TestSyncer_BackoffResetOnSuccess(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
defer cancel()
syncer := setupTestSyncer(t, 1*time.Second)
syncer.ctx = ctx
addr, pub, signer := buildSyncTestSigner(t)
gen := syncer.genesis
daRetriever := NewMockDARetriever(t)
p2pHandler := newMockp2pHandler(t)
p2pHandler.On("ProcessHeight", mock.Anything, mock.Anything, mock.Anything).Return(nil).Maybe()
syncer.daRetriever = daRetriever
syncer.p2pHandler = p2pHandler
p2pHandler.On("SetProcessedHeight", mock.Anything).Return().Maybe()
// Mock PopPriorityHeight to always return 0 (no priority heights)
daRetriever.On("PopPriorityHeight").Return(uint64(0)).Maybe()
// Create mock stores for P2P
mockHeaderStore := extmocks.NewMockStore[*types.SignedHeader](t)
mockHeaderStore.EXPECT().Height().Return(uint64(0)).Maybe()
mockDataStore := extmocks.NewMockStore[*types.Data](t)
mockDataStore.EXPECT().Height().Return(uint64(0)).Maybe()
var callTimes []time.Time
// First call - error (should trigger backoff)
daRetriever.On("RetrieveFromDA", mock.Anything, uint64(100)).
Run(func(args mock.Arguments) {
callTimes = append(callTimes, time.Now())
}).
Return(nil, errors.New("temporary failure")).Once()
// Second call - success (should reset backoff and increment DA height)
_, header := makeSignedHeaderBytes(t, gen.ChainID, 1, addr, pub, signer, nil, nil, nil)
data := &types.Data{
Metadata: &types.Metadata{
ChainID: gen.ChainID,
Height: 1,
Time: uint64(time.Now().UnixNano()),
},
}
event := common.DAHeightEvent{
Header: header,
Data: data,
DaHeight: 100,
}
daRetriever.On("RetrieveFromDA", mock.Anything, uint64(100)).
Run(func(args mock.Arguments) {
callTimes = append(callTimes, time.Now())
}).
Return([]common.DAHeightEvent{event}, nil).Once()
// Third call - should happen immediately after success (DA height incremented to 101)
daRetriever.On("RetrieveFromDA", mock.Anything, uint64(101)).
Run(func(args mock.Arguments) {
callTimes = append(callTimes, time.Now())
cancel()
}).
Return(nil, datypes.ErrBlobNotFound).Once()
// Start process loop to handle events
go syncer.processLoop()
// Run workers
syncer.startSyncWorkers(t.Context())
<-ctx.Done()
syncer.wg.Wait()
require.Len(t, callTimes, 3, "should make exactly 3 calls")
// Verify backoff between first and second call
delay1to2 := callTimes[1].Sub(callTimes[0])
assert.GreaterOrEqual(t, delay1to2, 1*time.Second,
"should have backed off between error and success (got %v)", delay1to2)
// Verify no backoff between second and third call (backoff reset)
delay2to3 := callTimes[2].Sub(callTimes[1])
assert.Less(t, delay2to3, 100*time.Millisecond,
"should continue immediately after success (got %v)", delay2to3)
})
}
// TestSyncer_BackoffBehaviorIntegration tests the complete backoff flow:
// error -> backoff delay -> recovery -> normal operation.
func TestSyncer_BackoffBehaviorIntegration(t *testing.T) {
// Test simpler backoff behavior: error -> backoff -> success -> continue
synctest.Test(t, func(t *testing.T) {
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
defer cancel()
syncer := setupTestSyncer(t, 500*time.Millisecond)
syncer.ctx = ctx
daRetriever := NewMockDARetriever(t)
p2pHandler := newMockp2pHandler(t)
p2pHandler.On("ProcessHeight", mock.Anything, mock.Anything, mock.Anything).Return(nil).Maybe()
syncer.daRetriever = daRetriever
syncer.p2pHandler = p2pHandler
// Mock PopPriorityHeight to always return 0 (no priority heights)
daRetriever.On("PopPriorityHeight").Return(uint64(0)).Maybe()
// Create mock stores for P2P
mockHeaderStore := extmocks.NewMockStore[*types.SignedHeader](t)
mockHeaderStore.EXPECT().Height().Return(uint64(0)).Maybe()
mockDataStore := extmocks.NewMockStore[*types.Data](t)
mockDataStore.EXPECT().Height().Return(uint64(0)).Maybe()
var callTimes []time.Time
p2pHandler.On("SetProcessedHeight", mock.Anything).Return().Maybe()
// First call - error (triggers backoff)
daRetriever.On("RetrieveFromDA", mock.Anything, uint64(100)).
Run(func(args mock.Arguments) {
callTimes = append(callTimes, time.Now())
}).
Return(nil, errors.New("network error")).Once()
// Second call - should be delayed due to backoff
daRetriever.On("RetrieveFromDA", mock.Anything, uint64(100)).
Run(func(args mock.Arguments) {
callTimes = append(callTimes, time.Now())
}).
Return(nil, datypes.ErrBlobNotFound).Once()
// Third call - should continue without delay (DA height incremented)
daRetriever.On("RetrieveFromDA", mock.Anything, uint64(101)).
Run(func(args mock.Arguments) {
callTimes = append(callTimes, time.Now())
cancel()
}).
Return(nil, datypes.ErrBlobNotFound).Once()
go syncer.processLoop()
syncer.startSyncWorkers(t.Context())
<-ctx.Done()
syncer.wg.Wait()
require.Len(t, callTimes, 3, "should make exactly 3 calls")
// First to second call should be delayed (backoff)
delay1to2 := callTimes[1].Sub(callTimes[0])
assert.GreaterOrEqual(t, delay1to2, 500*time.Millisecond,
"should have backoff delay between first and second call (got %v)", delay1to2)
// Second to third call should be immediate (no backoff after ErrBlobNotFound)
delay2to3 := callTimes[2].Sub(callTimes[1])
assert.Less(t, delay2to3, 100*time.Millisecond,
"should continue immediately after ErrBlobNotFound (got %v)", delay2to3)
})
}
func setupTestSyncer(t *testing.T, daBlockTime time.Duration) *Syncer {
t.Helper()
ds := dssync.MutexWrap(datastore.NewMapDatastore())
st := store.New(ds)
cm, err := cache.NewManager(config.DefaultConfig(), st, zerolog.Nop())
require.NoError(t, err)
addr, _, _ := buildSyncTestSigner(t)
cfg := config.DefaultConfig()
cfg.DA.BlockTime.Duration = daBlockTime
gen := genesis.Genesis{
ChainID: "test-chain",
InitialHeight: 1,
StartTime: time.Now().Add(-time.Hour), // Start in past
ProposerAddress: addr,
DAStartHeight: 100,
}
syncer := NewSyncer(
st,
execution.NewDummyExecutor(),
nil,
cm,
common.NopMetrics(),
cfg,
gen,
extmocks.NewMockStore[*types.P2PSignedHeader](t),
extmocks.NewMockStore[*types.P2PData](t),
zerolog.Nop(),
common.DefaultBlockOptions(),
make(chan error, 1),
nil,
)
require.NoError(t, syncer.initializeState())
return syncer
}