@@ -468,11 +468,8 @@ func (s *Store[H]) loadHeadKey(ctx context.Context) error {
468468 return err
469469 }
470470
471- newHeight := s .advanceContiguousHead (ctx , h .Height ())
472- if newHeight >= h .Height () {
473- s .contiguousHead .Store (& h )
474- s .heightSub .SetHeight (h .Height ())
475- }
471+ s .contiguousHead .Store (& h )
472+ s .heightSub .SetHeight (h .Height ())
476473 return nil
477474}
478475
@@ -510,29 +507,28 @@ func (s *Store[H]) get(ctx context.Context, hash header.Hash) ([]byte, error) {
510507
511508// advanceContiguousHead return a new highest contiguous height
512509// or returns the given height if not found.
513- func (s * Store [H ]) advanceContiguousHead (ctx context.Context , currHeight uint64 ) uint64 {
514- // TODO(cristaloleg): benchmark this timeout or make it dynamic.
515- advCtx , advCancel := context .WithTimeout (ctx , 10 * time .Second )
516- defer advCancel ()
510+ func (s * Store [H ]) advanceContiguousHead (ctx context.Context , height uint64 ) {
511+ newHead , ok := s .nextContiguous (ctx , height )
512+ if ok && newHead .Height () > height {
513+ s .contiguousHead .Store (& newHead )
514+ s .heightSub .SetHeight (newHead .Height ())
515+ log .Infow ("new head" , "height" , newHead .Height (), "hash" , newHead .Hash ())
516+ s .metrics .newHead (newHead .Height ())
517+ }
518+ }
517519
518- prevHeight := currHeight
520+ func ( s * Store [ H ]) nextContiguous ( ctx context. Context , height uint64 ) ( H , bool ) {
519521 var newHead H
522+ newHeight := height
520523 for {
521- h , err := s .getByHeight (advCtx , currHeight + 1 )
524+ h , err := s .getByHeight (ctx , newHeight + 1 )
522525 if err != nil {
523526 break
524527 }
525528 newHead = h
526- currHeight ++
527- }
528-
529- if currHeight > prevHeight {
530- s .contiguousHead .Store (& newHead )
531- s .heightSub .SetHeight (newHead .Height ())
532- log .Infow ("new head" , "height" , newHead .Height (), "hash" , newHead .Hash ())
533- s .metrics .newHead (newHead .Height ())
529+ newHeight ++
534530 }
535- return currHeight
531+ return newHead , newHeight != height
536532}
537533
538534// indexTo saves mapping between header Height and Hash to the given batch.
0 commit comments