From 8356c21700e65ac50b1aa8b55f0855f1f760e276 Mon Sep 17 00:00:00 2001 From: Viacheslav Date: Fri, 11 Nov 2022 16:16:13 +0200 Subject: [PATCH] improvement(header/p2p): get bestHead if timeout is reached (#1319) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit сloses https://github.com/celestiaorg/celestia-node/issues/1309 --- header/p2p/exchange.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/header/p2p/exchange.go b/header/p2p/exchange.go index f7fd4f996d..11a803f541 100644 --- a/header/p2p/exchange.go +++ b/header/p2p/exchange.go @@ -61,6 +61,10 @@ func NewExchange(host host.Host, peers peer.IDSlice, protocolSuffix string) *Exc // Head requests the latest ExtendedHeader. Note that the ExtendedHeader // must be verified thereafter. +// NOTE: +// It is fine to continue handling head request if the timeout will be reached. +// As we are requesting head from multiple trusted peers, +// we may already have some headers when the timeout will be reached. func (ex *Exchange) Head(ctx context.Context) (*header.ExtendedHeader, error) { log.Debug("requesting head") // create request @@ -85,6 +89,7 @@ func (ex *Exchange) Head(ctx context.Context) (*header.ExtendedHeader, error) { } result := make([]*header.ExtendedHeader, 0, len(ex.trustedPeers)) +LOOP: for range ex.trustedPeers { select { case h := <-headerCh: @@ -92,7 +97,7 @@ func (ex *Exchange) Head(ctx context.Context) (*header.ExtendedHeader, error) { result = append(result, h) } case <-ctx.Done(): - return nil, ctx.Err() + break LOOP } }