Skip to content

Commit

Permalink
improvement(header/p2p): get bestHead if timeout is reached (#1319)
Browse files Browse the repository at this point in the history
сloses #1309
  • Loading branch information
vgonkivs authored Nov 11, 2022
1 parent a95b114 commit 8356c21
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion header/p2p/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -85,14 +89,15 @@ 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:
if h != nil {
result = append(result, h)
}
case <-ctx.Done():
return nil, ctx.Err()
break LOOP
}
}

Expand Down

0 comments on commit 8356c21

Please sign in to comment.