@@ -11,8 +11,10 @@ import (
11
11
// Head returns the Network Head.
12
12
//
13
13
// Known subjective head is considered network head if it is recent enough(now-timestamp<=blocktime)
14
- // Otherwise, head is requested from a trusted peer and
14
+ // Otherwise, we attempt to request recent network head from a trusted peer and
15
15
// set as the new subjective head, assuming that trusted peer is always fully synced.
16
+ //
17
+ // The request is limited with 2 seconds and otherwise potentially unrecent header is returned.
16
18
func (s * Syncer [H ]) Head (ctx context.Context , _ ... header.HeadOption [H ]) (H , error ) {
17
19
sbjHead , err := s .subjectiveHead (ctx )
18
20
if err != nil {
@@ -23,12 +25,7 @@ func (s *Syncer[H]) Head(ctx context.Context, _ ...header.HeadOption[H]) (H, err
23
25
return sbjHead , nil
24
26
}
25
27
// otherwise, request head from the network
26
- //
27
- // TODO(@Wondertan): Here is another potential networking optimization:
28
- // * From sbjHead's timestamp and current time predict the time to the next header(TNH)
29
- // * If now >= TNH && now <= TNH + (THP) header propagation time
30
- // * Wait for header to arrive instead of requesting it
31
- // * This way we don't request as we know the new network header arrives exactly
28
+ // TODO: Besides requesting we should listen for new gossiped headers and cancel request if so
32
29
//
33
30
// single-flight protection
34
31
// ensure only one Head is requested at the time
@@ -38,7 +35,11 @@ func (s *Syncer[H]) Head(ctx context.Context, _ ...header.HeadOption[H]) (H, err
38
35
return s .Head (ctx )
39
36
}
40
37
defer s .getter .Unlock ()
41
- netHead , err := s .getter .Head (ctx , header.WithTrustedHead [H ](sbjHead ))
38
+ // limit time to get a recent header
39
+ // if we can't get it - give what we have
40
+ reqCtx , cancel := context .WithTimeout (ctx , time .Second * 2 ) // TODO(@vgonkivs): make timeout configurable
41
+ defer cancel ()
42
+ netHead , err := s .getter .Head (reqCtx , header.WithTrustedHead [H ](sbjHead ))
42
43
if err != nil {
43
44
log .Warnw ("failed to get recent head, returning current subjective" , "sbjHead" , sbjHead .Height (), "err" , err )
44
45
return s .subjectiveHead (ctx )
0 commit comments