@@ -43,6 +43,76 @@ type processingRegions struct {
4343 newHeaders headerRegion
4444}
4545
46+ // syncModes encapsulates the verification and append modes for header
47+ // synchronization. It is specifically designed for handling the divergence
48+ // and new headers regions internally between import and target sources.
49+ //
50+ // Target sources may have divergence (different heights for block vs filter
51+ // stores) due to interrupted prior imports or partial sync failures. This
52+ // requires flexible sync modes to validate the leading store and catch up the
53+ // lagging store independently.
54+ type syncModes struct {
55+ // verify specifies the verification strategy for the divergence headers
56+ // region.
57+ verify verifyMode
58+
59+ // append specifies the append strategy for divergence and new headers
60+ // regions.
61+ append appendMode
62+ }
63+
64+ // verifyMode represents the verification strategy for the divergence headers
65+ // region.
66+ type verifyMode uint8
67+
68+ const (
69+ // verifyBlockAndFilter indicates both block and filter headers should
70+ // be verified against the corresponding block and filter headers from
71+ // import source. Reserved for scenarios requiring verification of both
72+ // types.
73+ verifyBlockAndFilter verifyMode = iota
74+
75+ // verifyBlockOnly indicates only block headers should be verified
76+ // against the corresponding block headers from import source. This
77+ // happens in case of divergence headers region where the target block
78+ // headers store leads the target filter headers store.
79+ verifyBlockOnly
80+
81+ // verifyFilterOnly indicates only filter headers should be verified
82+ // against the corresponding filter headers from import source. This
83+ // happens in case of divergence headers region where the target
84+ // filter headers store leads the target block headers store.
85+ verifyFilterOnly
86+ )
87+
88+ // appendMode specifies which header types to append during synchronization. It
89+ // is specifically designed for handling the divergence and new headers regions
90+ // internally between import and target sources.
91+ type appendMode uint8
92+
93+ const (
94+ // appendBlockAndFilter indicates both block and filter headers should
95+ // be appended during synchronization using the corresponding block and
96+ // filter headers from import sources. This happens in case of new
97+ // headers region where the target stores are at same height with no
98+ // divergence detected.
99+ appendBlockAndFilter appendMode = iota
100+
101+ // appendBlockOnly indicates only block headers should be appended
102+ // during synchronization using the corresponding block headers from
103+ // import source. This happens in case of divergence headers region
104+ // where the target block headers store lags behind the target filter
105+ // headers store.
106+ appendBlockOnly
107+
108+ // appendFilterOnly indicates only filter headers should be appended
109+ // during synchronization using corresponding filter headers from
110+ // import source. This happens in case of divergence headers region
111+ // where the target filter headers store lags behind the target block
112+ // headers store.
113+ appendFilterOnly
114+ )
115+
46116// headerRegion represents a contiguous range of headers.
47117type headerRegion struct {
48118 // start is the beginning height of this header region.
@@ -53,6 +123,9 @@ type headerRegion struct {
53123
54124 // exists indicates whether this region has headers to process.
55125 exists bool
126+
127+ // syncModes contains the synchronization modes for the header region.
128+ syncModes syncModes
56129}
57130
58131// headersImport orchestrates the import of blockchain headers from external
@@ -458,10 +531,15 @@ func (h *headersImport) determineProcessingRegions() (*processingRegions, error)
458531 // reconciliation.
459532 divergeStart := effectiveTipHeight + 1
460533 divergeEnd := min (max (bTipHeight , fTipHeight ), importEndHeight )
534+ divergeExists := bTipHeight != fTipHeight && divergeStart <= divergeEnd
535+ divergenceSyncModes := h .determineDivergenceSyncModes (
536+ bTipHeight , fTipHeight ,
537+ )
461538 regions .divergence = headerRegion {
462- start : divergeStart ,
463- end : divergeEnd ,
464- exists : bTipHeight != fTipHeight && divergeStart <= divergeEnd ,
539+ start : divergeStart ,
540+ end : divergeEnd ,
541+ exists : divergeExists ,
542+ syncModes : divergenceSyncModes ,
465543 }
466544
467545 // 2. New Headers region.
@@ -482,11 +560,42 @@ func (h *headersImport) determineProcessingRegions() (*processingRegions, error)
482560 start : newStart ,
483561 end : newEnd ,
484562 exists : newStart <= newEnd ,
563+ syncModes : syncModes {
564+ append : appendBlockAndFilter ,
565+ },
485566 }
486567
487568 return regions , nil
488569}
489570
571+ // determineDivergenceSyncModes determines the appropriate sync modes for
572+ // processing divergence headers region based on which target store is leading.
573+ // It returns different verification and append strategies depending on whether
574+ // the block or filter headers store has a higher tip height.
575+ func (h * headersImport ) determineDivergenceSyncModes (blockTipHeight ,
576+ filterTipHeight uint32 ) syncModes {
577+
578+ switch {
579+ case blockTipHeight > filterTipHeight :
580+ return syncModes {
581+ verify : verifyBlockOnly ,
582+ append : appendFilterOnly ,
583+ }
584+
585+ case blockTipHeight < filterTipHeight :
586+ return syncModes {
587+ verify : verifyFilterOnly ,
588+ append : appendBlockOnly ,
589+ }
590+
591+ default :
592+ return syncModes {
593+ verify : verifyBlockAndFilter ,
594+ append : appendBlockAndFilter ,
595+ }
596+ }
597+ }
598+
490599// processNewHeadersRegion imports headers from the specified region into the
491600// target stores. This method handles the case where headers exist in the import
492601// source but not in the target stores.
0 commit comments