Skip to content

Commit dfe1071

Browse files
committed
cleanup duplicates
1 parent 25cc36b commit dfe1071

File tree

1 file changed

+29
-37
lines changed

1 file changed

+29
-37
lines changed

pkg/store/store_adapter.go

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,10 @@ func (hs *heightSub) notifyUpTo(h uint64) {
112112
// are validated and persisted by the ev-node syncer. Once the ev-node syncer processes
113113
// a block, it writes to the underlying store, and subsequent reads will come from the store.
114114
type StoreAdapter[H header.Header[H]] struct {
115-
getter StoreGetter[H]
116-
initialHeight uint64
115+
getter StoreGetter[H]
116+
genesisInitialHeight uint64
117117

118-
// height caches the current height to avoid repeated context-based lookups.
119-
// Updated on successful reads and writes.
120-
height atomic.Uint64
121-
122-
// heightSub allows waiting for specific heights to be stored.
118+
// heightSub tracks the current height and allows waiting for specific heights.
123119
// This is required by go-header syncer for blocking GetByHeight.
124120
heightSub *heightSub
125121

@@ -141,22 +137,21 @@ func NewStoreAdapter[H header.Header[H]](getter StoreGetter[H], gen genesis.Gene
141137
// Create LRU cache for pending items - ignore error as size is constant and valid
142138
pendingCache, _ := lru.New[uint64, H](defaultPendingCacheSize)
143139

144-
// Get initial height from store
145-
initialHeight := gen.InitialHeight
146-
if h, err := getter.Height(context.Background()); err == nil && h > 0 {
147-
initialHeight = h
140+
// Get actual current height from store (0 if empty)
141+
var storeHeight uint64
142+
if h, err := getter.Height(context.Background()); err == nil {
143+
storeHeight = h
148144
}
149145

150146
adapter := &StoreAdapter[H]{
151-
getter: getter,
152-
initialHeight: initialHeight,
153-
pending: pendingCache,
154-
heightSub: newHeightSub(initialHeight),
147+
getter: getter,
148+
genesisInitialHeight: max(gen.InitialHeight, 1),
149+
pending: pendingCache,
150+
heightSub: newHeightSub(storeHeight),
155151
}
156152

157-
// Initialize height from store
158-
if initialHeight > 0 {
159-
adapter.height.Store(initialHeight)
153+
// Mark as initialized if we have data
154+
if storeHeight > 0 {
160155
adapter.initialized = true
161156
}
162157

@@ -175,7 +170,6 @@ func (a *StoreAdapter[H]) Start(ctx context.Context) error {
175170
}
176171

177172
if h > 0 {
178-
a.height.Store(h)
179173
a.heightSub.SetHeight(h)
180174
a.initialized = true
181175
}
@@ -212,21 +206,21 @@ func (a *StoreAdapter[H]) Head(ctx context.Context, _ ...header.HeadOption[H]) (
212206

213207
// Prefer pending if it's higher than store
214208
if pendingHeight > storeHeight {
215-
a.height.Store(pendingHeight)
209+
a.heightSub.SetHeight(pendingHeight)
216210
return pendingHead, nil
217211
}
218212

219213
// Try to get from store
220214
if storeHeight > 0 {
221-
a.height.Store(storeHeight)
215+
a.heightSub.SetHeight(storeHeight)
222216
if item, err := a.getter.GetByHeight(ctx, storeHeight); err == nil {
223217
return item, nil
224218
}
225219
}
226220

227221
// Fall back to pending if store failed
228222
if pendingHeight > 0 {
229-
a.height.Store(pendingHeight)
223+
a.heightSub.SetHeight(pendingHeight)
230224
return pendingHead, nil
231225
}
232226

@@ -240,7 +234,7 @@ func (a *StoreAdapter[H]) Head(ctx context.Context, _ ...header.HeadOption[H]) (
240234
func (a *StoreAdapter[H]) Tail(ctx context.Context) (H, error) {
241235
var zero H
242236

243-
height := a.height.Load()
237+
height := a.heightSub.Height()
244238
if height == 0 {
245239
// Check store
246240
h, err := a.getter.Height(ctx)
@@ -250,19 +244,19 @@ func (a *StoreAdapter[H]) Tail(ctx context.Context) (H, error) {
250244
height = h
251245
}
252246

253-
// Try initialHeight first (most common case - no pruning)
254-
item, err := a.getter.GetByHeight(ctx, a.initialHeight)
247+
// Try genesisInitialHeight first (most common case - no pruning)
248+
item, err := a.getter.GetByHeight(ctx, a.genesisInitialHeight)
255249
if err == nil {
256250
return item, nil
257251
}
258252

259-
// Check pending for initialHeight
260-
if pendingItem, ok := a.pending.Peek(a.initialHeight); ok {
253+
// Check pending for genesisInitialHeight
254+
if pendingItem, ok := a.pending.Peek(a.genesisInitialHeight); ok {
261255
return pendingItem, nil
262256
}
263257

264-
// Walk up from initialHeight to find the first available item (pruning case)
265-
for h := a.initialHeight + 1; h <= height; h++ {
258+
// Walk up from genesisInitialHeight to find the first available item (pruning case)
259+
for h := a.genesisInitialHeight + 1; h <= height; h++ {
266260
item, err = a.getter.GetByHeight(ctx, h)
267261
if err == nil {
268262
return item, nil
@@ -413,15 +407,15 @@ func (a *StoreAdapter[H]) Height() uint64 {
413407
}
414408

415409
if maxPending > h {
416-
a.height.Store(maxPending)
410+
a.heightSub.SetHeight(maxPending)
417411
return maxPending
418412
}
419-
a.height.Store(h)
413+
a.heightSub.SetHeight(h)
420414
return h
421415
}
422416

423417
// Fall back to cached height or check pending
424-
height := a.height.Load()
418+
height := a.heightSub.Height()
425419
if height > 0 {
426420
return height
427421
}
@@ -459,8 +453,7 @@ func (a *StoreAdapter[H]) Append(ctx context.Context, items ...H) error {
459453
a.pending.Add(height, item)
460454

461455
// Update cached height and notify waiters
462-
if height > a.height.Load() {
463-
a.height.Store(height)
456+
if height > a.heightSub.Height() {
464457
a.heightSub.SetHeight(height)
465458
}
466459
}
@@ -484,7 +477,6 @@ func (a *StoreAdapter[H]) Init(ctx context.Context, item H) error {
484477

485478
// Add to pending cache (LRU will evict oldest if full)
486479
a.pending.Add(item.Height(), item)
487-
a.height.Store(item.Height())
488480
a.heightSub.SetHeight(item.Height())
489481
a.initialized = true
490482

@@ -512,8 +504,8 @@ func (a *StoreAdapter[H]) DeleteRange(ctx context.Context, from, to uint64) erro
512504
}
513505

514506
// Update cached height if necessary
515-
if from <= a.height.Load() {
516-
a.height.Store(from - 1)
507+
if from <= a.heightSub.Height() {
508+
a.heightSub.SetHeight(from - 1)
517509
}
518510

519511
return nil

0 commit comments

Comments
 (0)