Skip to content

Commit 3152ebd

Browse files
chainimport: process overlap headers region
1 parent 9d752d3 commit 3152ebd

File tree

2 files changed

+41
-43
lines changed

2 files changed

+41
-43
lines changed

chainimport/headers_import.go

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ const (
2222
defaultWriteBatchSizePerRegion = 16384
2323
)
2424

25-
// processingRegions currently contains regions to process. Those regions
26-
// overlap, divergence, and new headers region are all detected but only new
27-
// headers region is processed.
25+
// ProcessingRegions contains the different processing regions for a header
26+
// import. The full range of headers is partitioned into divergence, overlap,
27+
// and new headers regions, which are all processed during an import.
2828
type processingRegions struct {
2929
// importStartHeight defines the starting block height for the import
3030
// process.
@@ -260,9 +260,14 @@ func (h *headersImport) Import(ctx context.Context) (*ImportResult, error) {
260260
"headers processing failed: %w", err)
261261
}
262262

263-
// TODO(mohamedawnallah): process the overlap region. This mainly
264-
// includes a validation strategy for the overlap region between headers
265-
// from import and target sources.
263+
// Process overlap headers region.
264+
// Process headers in the overlap region by verifying the headers using
265+
// a sampling approach to efficiently validate consistency.
266+
err = h.processOverlapHeadersRegion(ctx, regions.overlap, result)
267+
if err != nil {
268+
return nil, fmt.Errorf("headers import failed: overlap "+
269+
"headers processing failed: %w", err)
270+
}
266271

267272
// Process new headers region.
268273
// Add headers from the import source to the target stores, extending
@@ -347,41 +352,10 @@ func (h *headersImport) validateChainContinuity() error {
347352

348353
default:
349354
// Import data starts before or at the target tip height. This
350-
// means there is an overlap, so we need to verify compatibility
351-
// using sampling approach for minimal processing time.
352-
353-
// First we need to determine the overlap range.
354-
overlapStart := importStartHeight
355+
// means there is an overlap, and we just need to validate the
356+
// headers connection.
355357
overlapEnd := min(effectiveTipHeight, importEndHeight)
356358

357-
// Now we can verify headers at the start of the overlap range.
358-
if err = h.verifyHeadersAtTargetHeight(
359-
overlapStart, verifyBlockAndFilter,
360-
); err != nil {
361-
return err
362-
}
363-
364-
// If overlap range is more than 1 header, we can also verify at
365-
// the end.
366-
if overlapEnd > overlapStart {
367-
if err = h.verifyHeadersAtTargetHeight(
368-
overlapEnd, verifyBlockAndFilter,
369-
); err != nil {
370-
return err
371-
}
372-
}
373-
374-
// If the overlap range is more than 2 headers, also verify at
375-
// the middle point.
376-
if overlapEnd-overlapStart > 2 {
377-
middleHeight := (overlapStart + overlapEnd) / 2
378-
if err = h.verifyHeadersAtTargetHeight(
379-
middleHeight, verifyBlockAndFilter,
380-
); err != nil {
381-
return err
382-
}
383-
}
384-
385359
// Validate headers beyond the overlap region if there are any
386360
// remaining headers to import.
387361
if overlapEnd < importEndHeight {
@@ -714,6 +688,30 @@ func (h *headersImport) validateLeadAndSyncLag(ctx context.Context,
714688
return nil
715689
}
716690

691+
// processOverlapHeadersRegion processes overlap headers by verifying the
692+
// headers using a sampling approach to efficiently validate consistency.
693+
func (h *headersImport) processOverlapHeadersRegion(ctx context.Context,
694+
region headerRegion, result *ImportResult) error {
695+
696+
if !region.exists {
697+
return nil
698+
}
699+
700+
log.Infof("Verifying %d overlap headers from heights %d to %d",
701+
region.end-region.start+1, region.start, region.end)
702+
703+
if err := h.validateHeadersSampling(
704+
ctx, region.start, region.end, region.syncModes.verify,
705+
); err != nil {
706+
return fmt.Errorf("failed to validate headers via "+
707+
"sampling: %w", err)
708+
}
709+
710+
result.SkippedCount += int(region.end - region.start + 1)
711+
712+
return nil
713+
}
714+
717715
// processNewHeadersRegion imports headers from the specified region into the
718716
// target stores. This method handles the case where headers exist in the import
719717
// source but not in the target stores.

chainimport/headers_import_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ func TestImportOperationOnFileHeaderSource(t *testing.T) {
249249
v.importResult.ProcessedCount,
250250
)
251251

252-
// Verify no headers skipped.
252+
// Verify overlap headers are detected.
253253
require.Equal(
254-
v.tc, 0, v.importResult.SkippedCount,
254+
v.tc, 1, v.importResult.SkippedCount,
255255
)
256256
},
257257
},
@@ -460,9 +460,9 @@ func TestImportOperationOnHTTPHeaderSource(t *testing.T) {
460460
v.importResult.ProcessedCount,
461461
)
462462

463-
// Verify no headers skipped.
463+
// Verify overlap headers are detected.
464464
require.Equal(
465-
v.tc, 0, v.importResult.SkippedCount,
465+
v.tc, 1, v.importResult.SkippedCount,
466466
)
467467
},
468468
},

0 commit comments

Comments
 (0)