forked from evstack/ev-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_block_retriever_test.go
More file actions
378 lines (312 loc) · 11.6 KB
/
async_block_retriever_test.go
File metadata and controls
378 lines (312 loc) · 11.6 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package da
import (
"context"
"testing"
"time"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
datypes "github.com/evstack/ev-node/pkg/da/types"
mocks "github.com/evstack/ev-node/test/mocks"
pb "github.com/evstack/ev-node/types/pb/evnode/v1"
)
func TestAsyncBlockRetriever_GetCachedBlock_NoNamespace(t *testing.T) {
client := &mocks.MockClient{}
logger := zerolog.Nop()
fetcher := NewAsyncBlockRetriever(client, logger, nil, 100*time.Millisecond, 100, 10)
ctx := context.Background()
block, err := fetcher.GetCachedBlock(ctx, 100)
assert.NoError(t, err)
assert.Nil(t, block)
}
func TestAsyncBlockRetriever_GetCachedBlock_CacheMiss(t *testing.T) {
client := &mocks.MockClient{}
fiNs := datypes.NamespaceFromString("test-fi-ns").Bytes()
logger := zerolog.Nop()
fetcher := NewAsyncBlockRetriever(client, logger, fiNs, 100*time.Millisecond, 100, 10)
ctx := context.Background()
// Nothing cached yet
block, err := fetcher.GetCachedBlock(ctx, 100)
require.NoError(t, err)
assert.Nil(t, block) // Cache miss
}
func TestAsyncBlockRetriever_SubscriptionDrivenCaching(t *testing.T) {
// Test that blobs arriving via subscription are cached inline.
testBlobs := [][]byte{
[]byte("tx1"),
[]byte("tx2"),
}
client := &mocks.MockClient{}
fiNs := datypes.NamespaceFromString("test-fi-ns").Bytes()
// Create a subscription channel that delivers one event then blocks.
subCh := make(chan datypes.SubscriptionEvent, 1)
subCh <- datypes.SubscriptionEvent{
Height: 100,
Blobs: testBlobs,
}
client.On("Subscribe", mock.Anything, fiNs, mock.Anything).Return((<-chan datypes.SubscriptionEvent)(subCh), nil).Once()
// Catchup loop may call Retrieve for heights beyond 100 — stub those.
client.On("Retrieve", mock.Anything, mock.Anything, fiNs).Return(datypes.ResultRetrieve{
BaseResult: datypes.BaseResult{Code: datypes.StatusHeightFromFuture},
}).Maybe()
// On second subscribe (after watchdog timeout) just block forever.
blockCh := make(chan datypes.SubscriptionEvent)
client.On("Subscribe", mock.Anything, fiNs, mock.Anything).Return((<-chan datypes.SubscriptionEvent)(blockCh), nil).Maybe()
logger := zerolog.Nop()
fetcher := NewAsyncBlockRetriever(client, logger, fiNs, 200*time.Millisecond, 100, 5)
ctx := t.Context()
fetcher.Start(ctx)
defer fetcher.Stop()
// Wait for the subscription event to be processed.
var block *BlockData
var err error
for range 40 {
block, err = fetcher.GetCachedBlock(ctx, 100)
require.NoError(t, err)
if block != nil {
break
}
time.Sleep(25 * time.Millisecond)
}
require.NotNil(t, block, "block should be cached after subscription event")
assert.Equal(t, uint64(100), block.Height)
assert.Equal(t, 2, len(block.Blobs))
assert.Equal(t, []byte("tx1"), block.Blobs[0])
assert.Equal(t, []byte("tx2"), block.Blobs[1])
}
func TestAsyncBlockRetriever_CatchupFillsGaps(t *testing.T) {
// When subscription reports height 105 but current is 100,
// catchup loop should Retrieve heights 100-114 (100 + prefetch window).
testBlobs := [][]byte{[]byte("gap-tx")}
client := &mocks.MockClient{}
fiNs := datypes.NamespaceFromString("test-fi-ns").Bytes()
// Subscription delivers height 105 (no blobs — just a signal).
subCh := make(chan datypes.SubscriptionEvent, 1)
subCh <- datypes.SubscriptionEvent{Height: 105}
client.On("Subscribe", mock.Anything, fiNs, mock.Anything).Return((<-chan datypes.SubscriptionEvent)(subCh), nil).Once()
blockCh := make(chan datypes.SubscriptionEvent)
client.On("Subscribe", mock.Anything, fiNs, mock.Anything).Return((<-chan datypes.SubscriptionEvent)(blockCh), nil).Maybe()
// Height 102 has blobs; rest return not found or future.
client.On("Retrieve", mock.Anything, uint64(102), fiNs).Return(datypes.ResultRetrieve{
BaseResult: datypes.BaseResult{
Code: datypes.StatusSuccess,
Timestamp: time.Unix(2000, 0),
},
Data: testBlobs,
}).Maybe()
client.On("Retrieve", mock.Anything, mock.Anything, fiNs).Return(datypes.ResultRetrieve{
BaseResult: datypes.BaseResult{Code: datypes.StatusNotFound},
}).Maybe()
logger := zerolog.Nop()
fetcher := NewAsyncBlockRetriever(client, logger, fiNs, 100*time.Millisecond, 100, 10)
ctx := t.Context()
fetcher.Start(ctx)
defer fetcher.Stop()
// Wait for catchup to fill the gap.
var block *BlockData
var err error
for range 40 {
block, err = fetcher.GetCachedBlock(ctx, 102)
require.NoError(t, err)
if block != nil {
break
}
time.Sleep(50 * time.Millisecond)
}
require.NotNil(t, block, "block at 102 should be cached via catchup")
assert.Equal(t, uint64(102), block.Height)
assert.Equal(t, 1, len(block.Blobs))
assert.Equal(t, []byte("gap-tx"), block.Blobs[0])
}
func TestAsyncBlockRetriever_HeightFromFuture(t *testing.T) {
client := &mocks.MockClient{}
fiNs := datypes.NamespaceFromString("test-fi-ns").Bytes()
// Subscription delivers height 100 with no blobs.
subCh := make(chan datypes.SubscriptionEvent)
client.On("Subscribe", mock.Anything, fiNs, mock.Anything).Return((<-chan datypes.SubscriptionEvent)(subCh), nil).Once()
blockCh := make(chan datypes.SubscriptionEvent)
client.On("Subscribe", mock.Anything, fiNs, mock.Anything).Return((<-chan datypes.SubscriptionEvent)(blockCh), nil).Maybe()
// All Retrieve calls return HeightFromFuture.
client.On("Retrieve", mock.Anything, mock.Anything, fiNs).Return(datypes.ResultRetrieve{
BaseResult: datypes.BaseResult{Code: datypes.StatusHeightFromFuture},
}).Maybe()
logger := zerolog.Nop()
fetcher := NewAsyncBlockRetriever(client, logger, fiNs, time.Millisecond, 100, 10)
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
fetcher.Start(ctx)
defer fetcher.Stop()
// Wait a bit for catchup to attempt fetches.
require.Eventually(t, func() bool {
return fetcher.(*asyncBlockRetriever).subscriber.HasReachedHead()
}, time.Second, time.Millisecond)
// Cache should be empty since all heights are from the future.
block, err := fetcher.GetCachedBlock(ctx, 100)
require.NoError(t, err)
assert.Nil(t, block)
}
func TestAsyncBlockRetriever_StopGracefully(t *testing.T) {
client := &mocks.MockClient{}
fiNs := datypes.NamespaceFromString("test-fi-ns").Bytes()
blockCh := make(chan datypes.SubscriptionEvent)
client.On("Subscribe", mock.Anything, fiNs, mock.Anything).Return((<-chan datypes.SubscriptionEvent)(blockCh), nil).Maybe()
client.On("Retrieve", mock.Anything, mock.Anything, fiNs).Return(datypes.ResultRetrieve{
BaseResult: datypes.BaseResult{Code: datypes.StatusHeightFromFuture},
}).Maybe()
logger := zerolog.Nop()
fetcher := NewAsyncBlockRetriever(client, logger, fiNs, 100*time.Millisecond, 100, 10)
ctx := t.Context()
fetcher.Start(ctx)
time.Sleep(100 * time.Millisecond)
// Should stop gracefully without panic
fetcher.Stop()
}
func TestAsyncBlockRetriever_ReconnectOnSubscriptionError(t *testing.T) {
// Verify that the follow loop reconnects after a subscription channel closes.
testBlobs := [][]byte{[]byte("reconnect-tx")}
client := &mocks.MockClient{}
fiNs := datypes.NamespaceFromString("test-fi-ns").Bytes()
// First subscription closes immediately (simulating error).
closedCh := make(chan datypes.SubscriptionEvent)
close(closedCh)
client.On("Subscribe", mock.Anything, fiNs, mock.Anything).Return((<-chan datypes.SubscriptionEvent)(closedCh), nil).Once()
// Second subscription delivers a blob.
subCh := make(chan datypes.SubscriptionEvent, 1)
subCh <- datypes.SubscriptionEvent{
Height: 100,
Blobs: testBlobs,
}
client.On("Subscribe", mock.Anything, fiNs, mock.Anything).Return((<-chan datypes.SubscriptionEvent)(subCh), nil).Once()
// Third+ subscribe returns a blocking channel so it doesn't loop forever.
blockCh := make(chan datypes.SubscriptionEvent)
client.On("Subscribe", mock.Anything, fiNs, mock.Anything).Return((<-chan datypes.SubscriptionEvent)(blockCh), nil).Maybe()
// Stub Retrieve for catchup.
client.On("Retrieve", mock.Anything, mock.Anything, fiNs).Return(datypes.ResultRetrieve{
BaseResult: datypes.BaseResult{Code: datypes.StatusHeightFromFuture},
}).Maybe()
logger := zerolog.Nop()
// Very short backoff so reconnect is fast in tests.
fetcher := NewAsyncBlockRetriever(client, logger, fiNs, 50*time.Millisecond, 100, 5)
ctx := t.Context()
fetcher.Start(ctx)
defer fetcher.Stop()
// Wait for reconnect + event processing.
var block *BlockData
var err error
for range 60 {
block, err = fetcher.GetCachedBlock(ctx, 100)
require.NoError(t, err)
if block != nil {
break
}
time.Sleep(50 * time.Millisecond)
}
require.NotNil(t, block, "block should be cached after reconnect")
assert.Equal(t, 1, len(block.Blobs))
assert.Equal(t, []byte("reconnect-tx"), block.Blobs[0])
}
func TestAsyncBlockRetriever_FetchAndCacheBlock_ErrorResponse(t *testing.T) {
fiNs := datypes.NamespaceFromString("test-fi-ns").Bytes()
specs := map[string]struct {
code datypes.StatusCode
message string
wantErr string
}{
"status_error": {
code: datypes.StatusError,
message: "rpc failure",
wantErr: "retrieve block at height 123: rpc failure",
},
"status_context_deadline": {
code: datypes.StatusContextDeadline,
message: "timeout",
wantErr: "retrieve block at height 123: timeout",
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
client := &mocks.MockClient{}
client.
On("Retrieve", mock.Anything, uint64(123), fiNs).
Return(datypes.ResultRetrieve{
BaseResult: datypes.BaseResult{
Code: spec.code,
Message: spec.message,
},
}).
Once()
logger := zerolog.Nop()
fetcher := NewAsyncBlockRetriever(client, logger, fiNs, 100*time.Millisecond, 100, 10).(*asyncBlockRetriever)
err := fetcher.fetchAndCacheBlock(t.Context(), 123)
require.EqualError(t, err, spec.wantErr)
block, getErr := fetcher.GetCachedBlock(t.Context(), 123)
require.NoError(t, getErr)
assert.Nil(t, block)
client.AssertExpectations(t)
})
}
}
func TestBlockData_Serialization(t *testing.T) {
block := &BlockData{
Height: 100,
Timestamp: time.Unix(12345, 123456789).UTC(),
Blobs: [][]byte{
[]byte("blob1"),
[]byte("blob2"),
[]byte("another_blob"),
},
}
// Serialize using protobuf
pbBlock := &pb.BlockData{
Height: block.Height,
Timestamp: block.Timestamp.UnixNano(),
Blobs: block.Blobs,
}
data, err := proto.Marshal(pbBlock)
require.NoError(t, err)
assert.Greater(t, len(data), 0)
// Deserialize using protobuf
var decodedPb pb.BlockData
err = proto.Unmarshal(data, &decodedPb)
require.NoError(t, err)
decoded := &BlockData{
Height: decodedPb.Height,
Timestamp: time.Unix(0, decodedPb.Timestamp).UTC(),
Blobs: decodedPb.Blobs,
}
assert.Equal(t, block.Timestamp.UnixNano(), decoded.Timestamp.UnixNano())
assert.Equal(t, block.Height, decoded.Height)
assert.Equal(t, len(block.Blobs), len(decoded.Blobs))
for i := range block.Blobs {
assert.Equal(t, block.Blobs[i], decoded.Blobs[i])
}
}
func TestBlockData_SerializationEmpty(t *testing.T) {
block := &BlockData{
Height: 100,
Timestamp: time.Unix(0, 0).UTC(),
Blobs: [][]byte{},
}
// Serialize using protobuf
pbBlock := &pb.BlockData{
Height: block.Height,
Timestamp: block.Timestamp.UnixNano(),
Blobs: block.Blobs,
}
data, err := proto.Marshal(pbBlock)
require.NoError(t, err)
// Deserialize using protobuf
var decodedPb pb.BlockData
err = proto.Unmarshal(data, &decodedPb)
require.NoError(t, err)
decoded := &BlockData{
Height: decodedPb.Height,
Timestamp: time.Unix(0, decodedPb.Timestamp).UTC(),
Blobs: decodedPb.Blobs,
}
assert.Equal(t, uint64(100), decoded.Height)
assert.Equal(t, time.Unix(0, 0).UTC(), decoded.Timestamp)
assert.Equal(t, 0, len(decoded.Blobs))
}