@@ -16,9 +16,16 @@ import (
1616 "github.com/evstack/ev-node/types"
1717)
1818
19- // AsyncEpochFetcher handles background prefetching of DA epoch data
19+ // AsyncEpochFetcher provides background prefetching of DA epoch data
20+ type AsyncEpochFetcher interface {
21+ Start ()
22+ Stop ()
23+ GetCachedEpoch (ctx context.Context , daHeight uint64 ) (* ForcedInclusionEvent , error )
24+ }
25+
26+ // asyncEpochFetcher handles background prefetching of DA epoch data
2027// to speed up processing at epoch boundaries.
21- type AsyncEpochFetcher struct {
28+ type asyncEpochFetcher struct {
2229 client Client
2330 logger zerolog.Logger
2431 daEpochSize uint64
@@ -35,7 +42,6 @@ type AsyncEpochFetcher struct {
3542
3643 // Current DA height tracking
3744 currentDAHeight uint64
38- heightMu sync.RWMutex
3945
4046 // Prefetch window - how many epochs ahead to prefetch
4147 prefetchWindow uint64
@@ -51,7 +57,7 @@ func NewAsyncEpochFetcher(
5157 daStartHeight , daEpochSize uint64 ,
5258 prefetchWindow uint64 ,
5359 pollInterval time.Duration ,
54- ) * AsyncEpochFetcher {
60+ ) AsyncEpochFetcher {
5561 if prefetchWindow == 0 {
5662 prefetchWindow = 1 // Default: prefetch next epoch
5763 }
@@ -61,7 +67,7 @@ func NewAsyncEpochFetcher(
6167
6268 ctx , cancel := context .WithCancel (context .Background ())
6369
64- return & AsyncEpochFetcher {
70+ return & asyncEpochFetcher {
6571 client : client ,
6672 logger : logger .With ().Str ("component" , "async_epoch_fetcher" ).Logger (),
6773 daStartHeight : daStartHeight ,
@@ -76,7 +82,7 @@ func NewAsyncEpochFetcher(
7682}
7783
7884// Start begins the background prefetching process.
79- func (f * AsyncEpochFetcher ) Start () {
85+ func (f * asyncEpochFetcher ) Start () {
8086 f .wg .Add (1 )
8187 go f .backgroundFetchLoop ()
8288 f .logger .Info ().
@@ -88,35 +94,16 @@ func (f *AsyncEpochFetcher) Start() {
8894}
8995
9096// Stop gracefully stops the background prefetching process.
91- func (f * AsyncEpochFetcher ) Stop () {
97+ func (f * asyncEpochFetcher ) Stop () {
9298 f .logger .Info ().Msg ("stopping async epoch fetcher" )
9399 f .cancel ()
94100 f .wg .Wait ()
95101 f .logger .Info ().Msg ("async epoch fetcher stopped" )
96102}
97103
98- // SetDAHeight updates the current DA height being processed.
99- // This is called by sequencers to inform the fetcher of progress.
100- func (f * AsyncEpochFetcher ) SetDAHeight (height uint64 ) {
101- f .heightMu .Lock ()
102- defer f .heightMu .Unlock ()
103-
104- if height > f .currentDAHeight {
105- f .currentDAHeight = height
106- f .logger .Debug ().Uint64 ("da_height" , height ).Msg ("updated current DA height" )
107- }
108- }
109-
110- // GetDAHeight returns the current DA height.
111- func (f * AsyncEpochFetcher ) GetDAHeight () uint64 {
112- f .heightMu .RLock ()
113- defer f .heightMu .RUnlock ()
114- return f .currentDAHeight
115- }
116-
117104// GetCachedEpoch retrieves a cached epoch from memory.
118105// Returns nil if the epoch is not cached.
119- func (f * AsyncEpochFetcher ) GetCachedEpoch (ctx context.Context , daHeight uint64 ) (* ForcedInclusionEvent , error ) {
106+ func (f * asyncEpochFetcher ) GetCachedEpoch (ctx context.Context , daHeight uint64 ) (* ForcedInclusionEvent , error ) {
120107 if ! f .client .HasForcedInclusionNamespace () {
121108 return nil , ErrForceInclusionNotConfigured
122109 }
@@ -167,7 +154,7 @@ func (f *AsyncEpochFetcher) GetCachedEpoch(ctx context.Context, daHeight uint64)
167154}
168155
169156// backgroundFetchLoop runs in the background and prefetches epochs ahead of time.
170- func (f * AsyncEpochFetcher ) backgroundFetchLoop () {
157+ func (f * asyncEpochFetcher ) backgroundFetchLoop () {
171158 defer f .wg .Done ()
172159
173160 ticker := time .NewTicker (f .pollInterval )
@@ -184,15 +171,13 @@ func (f *AsyncEpochFetcher) backgroundFetchLoop() {
184171}
185172
186173// prefetchEpochs prefetches epochs within the prefetch window.
187- func (f * AsyncEpochFetcher ) prefetchEpochs () {
174+ func (f * asyncEpochFetcher ) prefetchEpochs () {
188175 if ! f .client .HasForcedInclusionNamespace () {
189176 return
190177 }
191178
192- currentHeight := f .GetDAHeight ()
193-
194179 // Calculate the current epoch and epochs to prefetch
195- _ , currentEpochEnd , _ := types .CalculateEpochBoundaries (currentHeight , f .daStartHeight , f .daEpochSize )
180+ _ , currentEpochEnd , _ := types .CalculateEpochBoundaries (f . currentDAHeight , f .daStartHeight , f .daEpochSize )
196181
197182 // Prefetch upcoming epochs
198183 for i := uint64 (0 ); i < f .prefetchWindow ; i ++ {
@@ -218,7 +203,7 @@ func (f *AsyncEpochFetcher) prefetchEpochs() {
218203}
219204
220205// fetchAndCacheEpoch fetches an epoch and stores it in the cache.
221- func (f * AsyncEpochFetcher ) fetchAndCacheEpoch (epochEnd uint64 ) {
206+ func (f * asyncEpochFetcher ) fetchAndCacheEpoch (epochEnd uint64 ) {
222207 epochStart := epochEnd - (f .daEpochSize - 1 )
223208 if epochStart < f .daStartHeight {
224209 epochStart = f .daStartHeight
@@ -313,7 +298,7 @@ func (f *AsyncEpochFetcher) fetchAndCacheEpoch(epochEnd uint64) {
313298}
314299
315300// processForcedInclusionBlobs processes blobs from a single DA height for forced inclusion.
316- func (f * AsyncEpochFetcher ) processForcedInclusionBlobs (
301+ func (f * asyncEpochFetcher ) processForcedInclusionBlobs (
317302 event * ForcedInclusionEvent ,
318303 result datypes.ResultRetrieve ,
319304 height uint64 ,
@@ -347,7 +332,7 @@ func (f *AsyncEpochFetcher) processForcedInclusionBlobs(
347332}
348333
349334// cleanupOldEpochs removes epochs older than the current epoch from cache.
350- func (f * AsyncEpochFetcher ) cleanupOldEpochs (currentEpochEnd uint64 ) {
335+ func (f * asyncEpochFetcher ) cleanupOldEpochs (currentEpochEnd uint64 ) {
351336 // Remove epochs older than current - 1
352337 // Keep current and previous in case of reorgs or restarts
353338 cleanupThreshold := currentEpochEnd - f .daEpochSize
0 commit comments