From 89c2b8f374f0b72ed5c6e8e1e16ca2e4d4439d06 Mon Sep 17 00:00:00 2001 From: Viacheslav Gonkivskyi Date: Tue, 30 Jan 2024 17:31:04 +0200 Subject: [PATCH 1/4] fix(syncer): set timeout for head request --- sync/sync_head.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sync/sync_head.go b/sync/sync_head.go index 349b4c5d..19f4237f 100644 --- a/sync/sync_head.go +++ b/sync/sync_head.go @@ -38,7 +38,11 @@ func (s *Syncer[H]) Head(ctx context.Context, _ ...header.HeadOption[H]) (H, err return s.Head(ctx) } defer s.getter.Unlock() - netHead, err := s.getter.Head(ctx, header.WithTrustedHead[H](sbjHead)) + + reqCtx, cancel := context.WithTimeout(ctx, time.Second*2) + defer cancel() + + netHead, err := s.getter.Head(reqCtx, header.WithTrustedHead[H](sbjHead)) if err != nil { log.Warnw("failed to get recent head, returning current subjective", "sbjHead", sbjHead.Height(), "err", err) return s.subjectiveHead(ctx) From e4e05bdc2ed4d973e3a36189d3374d831e7e095a Mon Sep 17 00:00:00 2001 From: Viacheslav Date: Tue, 30 Jan 2024 17:45:02 +0200 Subject: [PATCH 2/4] Update sync/sync_head.go Co-authored-by: Hlib Kanunnikov --- sync/sync_head.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sync/sync_head.go b/sync/sync_head.go index 19f4237f..43b4f213 100644 --- a/sync/sync_head.go +++ b/sync/sync_head.go @@ -38,10 +38,10 @@ func (s *Syncer[H]) Head(ctx context.Context, _ ...header.HeadOption[H]) (H, err return s.Head(ctx) } defer s.getter.Unlock() - - reqCtx, cancel := context.WithTimeout(ctx, time.Second*2) + // limit time to get a recent header + // if can't get it - give what we have + reqCtx, cancel := context.WithTimeout(ctx, time.Second*2) // TODO(@vgonkivs): make timeout configurable defer cancel() - netHead, err := s.getter.Head(reqCtx, header.WithTrustedHead[H](sbjHead)) if err != nil { log.Warnw("failed to get recent head, returning current subjective", "sbjHead", sbjHead.Height(), "err", err) From 3126f41edbfb68a524897df0fb99371a9050a355 Mon Sep 17 00:00:00 2001 From: Wondertan Date: Wed, 31 Jan 2024 14:48:22 +0100 Subject: [PATCH 3/4] some docs updates --- sync/sync_head.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sync/sync_head.go b/sync/sync_head.go index 43b4f213..ec1abad4 100644 --- a/sync/sync_head.go +++ b/sync/sync_head.go @@ -11,8 +11,10 @@ import ( // Head returns the Network Head. // // Known subjective head is considered network head if it is recent enough(now-timestamp<=blocktime) -// Otherwise, head is requested from a trusted peer and +// Otherwise, we attempt to request recent network head from a trusted peer and // set as the new subjective head, assuming that trusted peer is always fully synced. +// +// The request is limited with 2 seconds and otherwise potentially unrecent header is returned. func (s *Syncer[H]) Head(ctx context.Context, _ ...header.HeadOption[H]) (H, error) { sbjHead, err := s.subjectiveHead(ctx) if err != nil { @@ -39,7 +41,7 @@ func (s *Syncer[H]) Head(ctx context.Context, _ ...header.HeadOption[H]) (H, err } defer s.getter.Unlock() // limit time to get a recent header - // if can't get it - give what we have + // if we can't get it - give what we have reqCtx, cancel := context.WithTimeout(ctx, time.Second*2) // TODO(@vgonkivs): make timeout configurable defer cancel() netHead, err := s.getter.Head(reqCtx, header.WithTrustedHead[H](sbjHead)) From 2eaee6ef406d6f9e07e268505a502577698e1f41 Mon Sep 17 00:00:00 2001 From: Wondertan Date: Wed, 31 Jan 2024 14:57:50 +0100 Subject: [PATCH 4/4] change todo --- sync/sync_head.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/sync/sync_head.go b/sync/sync_head.go index ec1abad4..462af91e 100644 --- a/sync/sync_head.go +++ b/sync/sync_head.go @@ -25,12 +25,7 @@ func (s *Syncer[H]) Head(ctx context.Context, _ ...header.HeadOption[H]) (H, err return sbjHead, nil } // otherwise, request head from the network - // - // TODO(@Wondertan): Here is another potential networking optimization: - // * From sbjHead's timestamp and current time predict the time to the next header(TNH) - // * If now >= TNH && now <= TNH + (THP) header propagation time - // * Wait for header to arrive instead of requesting it - // * This way we don't request as we know the new network header arrives exactly + // TODO: Besides requesting we should listen for new gossiped headers and cancel request if so // // single-flight protection // ensure only one Head is requested at the time