-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy patharchiver_test.go
456 lines (371 loc) · 14.5 KB
/
archiver_test.go
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package service
import (
"context"
"testing"
"time"
"github.com/base-org/blob-archiver/archiver/flags"
"github.com/base-org/blob-archiver/archiver/metrics"
"github.com/base-org/blob-archiver/common/beacon/beacontest"
"github.com/base-org/blob-archiver/common/blobtest"
"github.com/base-org/blob-archiver/common/storage"
"github.com/base-org/blob-archiver/common/storage/storagetest"
"github.com/ethereum-optimism/optimism/op-service/testlog"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/stretchr/testify/require"
)
func setup(t *testing.T, beacon *beacontest.StubBeaconClient) (*Archiver, *storagetest.TestFileStorage) {
l := testlog.Logger(t, log.LvlInfo)
fs := storagetest.NewTestFileStorage(t, l)
m := metrics.NewMetrics()
svc, err := NewArchiver(l, flags.ArchiverConfig{
PollInterval: 5 * time.Second,
OriginBlock: blobtest.OriginBlock,
}, fs, beacon, m)
require.NoError(t, err)
return svc, fs
}
func TestArchiver_FetchAndPersist(t *testing.T) {
svc, fs := setup(t, beacontest.NewDefaultStubBeaconClient(t))
fs.CheckNotExistsOrFail(t, blobtest.OriginBlock)
header, alreadyExists, err := svc.persistBlobsForBlockToS3(context.Background(), blobtest.OriginBlock.String(), false)
require.False(t, alreadyExists)
require.NoError(t, err)
require.NotNil(t, header)
require.Equal(t, blobtest.OriginBlock.String(), common.Hash(header.Root).String())
fs.CheckExistsOrFail(t, blobtest.OriginBlock)
header, alreadyExists, err = svc.persistBlobsForBlockToS3(context.Background(), blobtest.OriginBlock.String(), false)
require.True(t, alreadyExists)
require.NoError(t, err)
require.NotNil(t, header)
require.Equal(t, blobtest.OriginBlock.String(), common.Hash(header.Root).String())
fs.CheckExistsOrFail(t, blobtest.OriginBlock)
}
func TestArchiver_FetchAndPersistOverwriting(t *testing.T) {
beacon := beacontest.NewDefaultStubBeaconClient(t)
svc, fs := setup(t, beacon)
// Blob 5 already exists
fs.WriteOrFail(t, storage.BlobData{
Header: storage.Header{
BeaconBlockHash: blobtest.Five,
},
BlobSidecars: storage.BlobSidecars{
Data: beacon.Blobs[blobtest.Five.String()],
},
})
require.Equal(t, fs.ReadOrFail(t, blobtest.Five).BlobSidecars.Data, beacon.Blobs[blobtest.Five.String()])
// change the blob data -- this isn't possible w/out changing the hash. But it allows us to test the overwrite
beacon.Blobs[blobtest.Five.String()] = blobtest.NewBlobSidecars(t, 6)
_, exists, err := svc.persistBlobsForBlockToS3(context.Background(), blobtest.Five.String(), true)
require.NoError(t, err)
require.True(t, exists)
// It should have overwritten the blob data
require.Equal(t, fs.ReadOrFail(t, blobtest.Five).BlobSidecars.Data, beacon.Blobs[blobtest.Five.String()])
// Overwriting a non-existent blob should return exists=false
_, exists, err = svc.persistBlobsForBlockToS3(context.Background(), blobtest.Four.String(), true)
require.NoError(t, err)
require.False(t, exists)
}
func TestArchiver_BackfillToOrigin(t *testing.T) {
beacon := beacontest.NewDefaultStubBeaconClient(t)
svc, fs := setup(t, beacon)
// We have the current head, which is block 5 written to storage
err := fs.WriteBlob(context.Background(), storage.BlobData{
Header: storage.Header{
BeaconBlockHash: blobtest.Five,
},
BlobSidecars: storage.BlobSidecars{
Data: beacon.Blobs[blobtest.Five.String()],
},
})
require.NoError(t, err)
// We expect to backfill all blocks to the origin
expectedBlobs := []common.Hash{blobtest.Four, blobtest.Three, blobtest.Two, blobtest.One, blobtest.OriginBlock}
for _, blob := range expectedBlobs {
fs.CheckNotExistsOrFail(t, blob)
}
svc.backfillBlobs(context.Background(), beacon.Headers[blobtest.Five.String()])
for _, blob := range expectedBlobs {
fs.CheckExistsOrFail(t, blob)
data := fs.ReadOrFail(t, blob)
require.Equal(t, data.BlobSidecars.Data, beacon.Blobs[blob.String()])
}
}
func TestArchiver_BackfillToExistingBlock(t *testing.T) {
beacon := beacontest.NewDefaultStubBeaconClient(t)
svc, fs := setup(t, beacon)
// We have the current head, which is block 5 written to storage
err := fs.WriteBlob(context.Background(), storage.BlobData{
Header: storage.Header{
BeaconBlockHash: blobtest.Five,
},
BlobSidecars: storage.BlobSidecars{
Data: beacon.Blobs[blobtest.Five.String()],
},
})
require.NoError(t, err)
// We also have block 1 written to storage
err = fs.WriteBlob(context.Background(), storage.BlobData{
Header: storage.Header{
BeaconBlockHash: blobtest.One,
},
BlobSidecars: storage.BlobSidecars{
Data: beacon.Blobs[blobtest.One.String()],
},
})
require.NoError(t, err)
// We expect to backfill all blobs between 5 and 1
expectedBlobs := []common.Hash{blobtest.Four, blobtest.Three, blobtest.Two}
for _, blob := range expectedBlobs {
exists, err := fs.Exists(context.Background(), blob)
require.NoError(t, err)
require.False(t, exists)
}
svc.backfillBlobs(context.Background(), beacon.Headers[blobtest.Five.String()])
for _, blob := range expectedBlobs {
exists, err := fs.Exists(context.Background(), blob)
require.NoError(t, err)
require.True(t, exists)
data, err := fs.ReadBlob(context.Background(), blob)
require.NoError(t, err)
require.NotNil(t, data)
require.Equal(t, data.BlobSidecars.Data, beacon.Blobs[blob.String()])
}
}
func TestArchiver_BackfillFinishOldProcess(t *testing.T) {
beacon := beacontest.NewDefaultStubBeaconClient(t)
svc, fs := setup(t, beacon)
// We have the current head, which is block 5 written to storage
err := fs.WriteBlob(context.Background(), storage.BlobData{
Header: storage.Header{
BeaconBlockHash: blobtest.Five,
},
BlobSidecars: storage.BlobSidecars{
Data: beacon.Blobs[blobtest.Five.String()],
},
})
require.NoError(t, err)
// We also have block 3 written to storage
err = fs.WriteBlob(context.Background(), storage.BlobData{
Header: storage.Header{
BeaconBlockHash: blobtest.Three,
},
BlobSidecars: storage.BlobSidecars{
Data: beacon.Blobs[blobtest.Three.String()],
},
})
require.NoError(t, err)
// We also have block 1 written to storage
err = fs.WriteBlob(context.Background(), storage.BlobData{
Header: storage.Header{
BeaconBlockHash: blobtest.One,
},
BlobSidecars: storage.BlobSidecars{
Data: beacon.Blobs[blobtest.One.String()],
},
})
require.NoError(t, err)
// We expect to backfill blob 4 first, then 2 in a separate process
expectedBlobs := []common.Hash{blobtest.Four, blobtest.Two}
for _, blob := range expectedBlobs {
exists, err := fs.Exists(context.Background(), blob)
require.NoError(t, err)
require.False(t, exists)
}
actualProcesses, err := svc.dataStoreClient.ReadBackfillProcesses(context.Background())
expectedProcesses := make(storage.BackfillProcesses)
require.NoError(t, err)
require.Equal(t, expectedProcesses, actualProcesses)
expectedProcesses[blobtest.Three] = storage.BackfillProcess{Start: *beacon.Headers[blobtest.Three.String()], Current: *beacon.Headers[blobtest.Three.String()]}
err = svc.dataStoreClient.WriteBackfillProcesses(context.Background(), expectedProcesses)
require.NoError(t, err)
actualProcesses, err = svc.dataStoreClient.ReadBackfillProcesses(context.Background())
require.NoError(t, err)
require.Equal(t, expectedProcesses, actualProcesses)
svc.backfillBlobs(context.Background(), beacon.Headers[blobtest.Five.String()])
for _, blob := range expectedBlobs {
exists, err := fs.Exists(context.Background(), blob)
require.NoError(t, err)
require.True(t, exists)
data, err := fs.ReadBlob(context.Background(), blob)
require.NoError(t, err)
require.NotNil(t, data)
require.Equal(t, data.BlobSidecars.Data, beacon.Blobs[blob.String()])
}
actualProcesses, err = svc.dataStoreClient.ReadBackfillProcesses(context.Background())
require.NoError(t, err)
require.Equal(t, storage.BackfillProcesses{}, actualProcesses)
}
func TestArchiver_LatestStopsAtExistingBlock(t *testing.T) {
beacon := beacontest.NewDefaultStubBeaconClient(t)
svc, fs := setup(t, beacon)
// 5 is the current head, if three already exists, we should write 5 and 4 and stop at three
fs.WriteOrFail(t, storage.BlobData{
Header: storage.Header{
BeaconBlockHash: blobtest.Three,
},
BlobSidecars: storage.BlobSidecars{
Data: beacon.Blobs[blobtest.Three.String()],
},
})
fs.CheckNotExistsOrFail(t, blobtest.Five)
fs.CheckNotExistsOrFail(t, blobtest.Four)
fs.CheckExistsOrFail(t, blobtest.Three)
svc.processBlocksUntilKnownBlock(context.Background())
fs.CheckExistsOrFail(t, blobtest.Five)
five := fs.ReadOrFail(t, blobtest.Five)
require.Equal(t, five.Header.BeaconBlockHash, blobtest.Five)
require.Equal(t, five.BlobSidecars.Data, beacon.Blobs[blobtest.Five.String()])
fs.CheckExistsOrFail(t, blobtest.Four)
four := fs.ReadOrFail(t, blobtest.Four)
require.Equal(t, four.Header.BeaconBlockHash, blobtest.Four)
require.Equal(t, five.BlobSidecars.Data, beacon.Blobs[blobtest.Five.String()])
fs.CheckExistsOrFail(t, blobtest.Three)
three := fs.ReadOrFail(t, blobtest.Three)
require.Equal(t, three.Header.BeaconBlockHash, blobtest.Three)
require.Equal(t, five.BlobSidecars.Data, beacon.Blobs[blobtest.Five.String()])
}
func TestArchiver_LatestNoNewData(t *testing.T) {
beacon := beacontest.NewDefaultStubBeaconClient(t)
svc, fs := setup(t, beacon)
// 5 is the current head, if 5 already exists, this should be a no-op
fs.WriteOrFail(t, storage.BlobData{
Header: storage.Header{
BeaconBlockHash: common.Hash(beacon.Headers["head"].Root),
},
BlobSidecars: storage.BlobSidecars{
Data: beacon.Blobs[blobtest.Three.String()],
},
})
fs.CheckExistsOrFail(t, blobtest.Five)
fs.CheckNotExistsOrFail(t, blobtest.Four)
svc.processBlocksUntilKnownBlock(context.Background())
fs.CheckExistsOrFail(t, blobtest.Five)
fs.CheckNotExistsOrFail(t, blobtest.Four)
}
func TestArchiver_LatestConsumesNewBlocks(t *testing.T) {
beacon := beacontest.NewDefaultStubBeaconClient(t)
svc, fs := setup(t, beacon)
// set current head to 4, and write four
beacon.Headers["head"] = beacon.Headers[blobtest.Four.String()]
fs.WriteOrFail(t, storage.BlobData{
Header: storage.Header{
BeaconBlockHash: common.Hash(beacon.Headers[blobtest.Four.String()].Root),
},
BlobSidecars: storage.BlobSidecars{
Data: beacon.Blobs[blobtest.Four.String()],
},
})
svc.processBlocksUntilKnownBlock(context.Background())
// No new data (5) is written and latest stops at known block (4), so 3 should not exist
fs.CheckNotExistsOrFail(t, blobtest.Five)
fs.CheckExistsOrFail(t, blobtest.Four)
fs.CheckNotExistsOrFail(t, blobtest.Three)
// set current head to 5, and check it fetches new data
beacon.Headers["head"] = beacon.Headers[blobtest.Five.String()]
svc.processBlocksUntilKnownBlock(context.Background())
fs.CheckExistsOrFail(t, blobtest.Five)
fs.CheckExistsOrFail(t, blobtest.Four)
fs.CheckNotExistsOrFail(t, blobtest.Three)
}
func TestArchiver_LatestStopsAtOrigin(t *testing.T) {
beacon := beacontest.NewDefaultStubBeaconClient(t)
svc, fs := setup(t, beacon)
// 5 is the current head, if origin already exists, we should stop at origin
fs.WriteOrFail(t, storage.BlobData{
Header: storage.Header{
BeaconBlockHash: blobtest.OriginBlock,
},
BlobSidecars: storage.BlobSidecars{
Data: beacon.Blobs[blobtest.OriginBlock.String()],
},
})
// Should write all blocks back to Origin
toWrite := []common.Hash{blobtest.Five, blobtest.Four, blobtest.Three, blobtest.Two, blobtest.One}
for _, hash := range toWrite {
fs.CheckNotExistsOrFail(t, hash)
}
svc.processBlocksUntilKnownBlock(context.Background())
for _, hash := range toWrite {
fs.CheckExistsOrFail(t, hash)
data := fs.ReadOrFail(t, hash)
require.Equal(t, data.BlobSidecars.Data, beacon.Blobs[hash.String()])
}
}
func TestArchiver_LatestRetriesOnFailure(t *testing.T) {
beacon := beacontest.NewDefaultStubBeaconClient(t)
svc, fs := setup(t, beacon)
// 5 is the current head, if three already exists, we should write 5 and 4 and stop at three
fs.WriteOrFail(t, storage.BlobData{
Header: storage.Header{
BeaconBlockHash: blobtest.Three,
},
BlobSidecars: storage.BlobSidecars{
Data: beacon.Blobs[blobtest.Three.String()],
},
})
fs.CheckNotExistsOrFail(t, blobtest.Five)
fs.CheckNotExistsOrFail(t, blobtest.Four)
fs.CheckExistsOrFail(t, blobtest.Three)
// One failure is retried
fs.WritesFailTimes(1)
svc.processBlocksUntilKnownBlock(context.Background())
fs.CheckExistsOrFail(t, blobtest.Five)
fs.CheckExistsOrFail(t, blobtest.Four)
fs.CheckExistsOrFail(t, blobtest.Three)
}
func TestArchiver_LatestHaltsOnPersistentError(t *testing.T) {
beacon := beacontest.NewDefaultStubBeaconClient(t)
svc, fs := setup(t, beacon)
// 5 is the current head, if three already exists, we should write 5 and 4 and stop at three
fs.WriteOrFail(t, storage.BlobData{
Header: storage.Header{
BeaconBlockHash: blobtest.Three,
},
BlobSidecars: storage.BlobSidecars{
Data: beacon.Blobs[blobtest.Three.String()],
},
})
fs.CheckNotExistsOrFail(t, blobtest.Five)
fs.CheckNotExistsOrFail(t, blobtest.Four)
fs.CheckExistsOrFail(t, blobtest.Three)
// Retries the maximum number of times, then fails and will not write the blobs
fs.WritesFailTimes(liveFetchBlobMaximumRetries + 1)
svc.processBlocksUntilKnownBlock(context.Background())
fs.CheckNotExistsOrFail(t, blobtest.Five)
fs.CheckNotExistsOrFail(t, blobtest.Four)
fs.CheckExistsOrFail(t, blobtest.Three)
}
func TestArchiver_RearchiveRange(t *testing.T) {
beacon := beacontest.NewDefaultStubBeaconClient(t)
svc, fs := setup(t, beacon)
// 5 is the current head, if three already exists, we should write 5 and 4 and stop at three
fs.WriteOrFail(t, storage.BlobData{
Header: storage.Header{
BeaconBlockHash: blobtest.Three,
},
BlobSidecars: storage.BlobSidecars{
Data: beacon.Blobs[blobtest.Three.String()],
},
})
// startSlot+1 == One
fs.CheckNotExistsOrFail(t, blobtest.One)
fs.CheckNotExistsOrFail(t, blobtest.Two)
fs.CheckExistsOrFail(t, blobtest.Three)
fs.CheckNotExistsOrFail(t, blobtest.Four)
// this modifies the blobs at 3, purely to test the blob is rearchived
beacon.Blobs[blobtest.Three.String()] = blobtest.NewBlobSidecars(t, 6)
from, to := blobtest.StartSlot+1, blobtest.StartSlot+4
actualFrom, actualTo, err := svc.rearchiveRange(from, to)
// Should index the whole range
require.NoError(t, err)
require.Equal(t, from, actualFrom)
require.Equal(t, to, actualTo)
// Should have written all the blobs
fs.CheckExistsOrFail(t, blobtest.One)
fs.CheckExistsOrFail(t, blobtest.Two)
fs.CheckExistsOrFail(t, blobtest.Three)
fs.CheckExistsOrFail(t, blobtest.Four)
// Should have overwritten any existing blobs
require.Equal(t, fs.ReadOrFail(t, blobtest.Three).BlobSidecars.Data, beacon.Blobs[blobtest.Three.String()])
}