From e44a96ef7820dfe67d76640afc66e48f21c8db26 Mon Sep 17 00:00:00 2001 From: Viacheslav Gonkivskyi Date: Thu, 22 Sep 2022 12:28:57 +0300 Subject: [PATCH] chore: rename methods --- header/p2p/exchange.go | 26 +++++++++++++++----------- header/p2p/exchange_test.go | 33 +++++++++++++++++++-------------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/header/p2p/exchange.go b/header/p2p/exchange.go index 70c90413ac..97d91577d7 100644 --- a/header/p2p/exchange.go +++ b/header/p2p/exchange.go @@ -9,7 +9,6 @@ import ( "time" logging "github.com/ipfs/go-log/v2" - "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/protocol" @@ -64,11 +63,12 @@ func (ex *Exchange) Head(ctx context.Context) (*header.ExtendedHeader, error) { } headerCh := make(chan *header.ExtendedHeader) + // request head from each trusted peer for _, from := range ex.trustedPeers { go func(from peer.ID) { - headers, err := doRequest(ctx, from, ex.host, req) + headers, err := request(ctx, from, ex.host, req) if err != nil { - log.Errorw("head from trusted peer failed", "trustedPeer", from, "err", err) + log.Errorw("head request to trusted peer failed", "trustedPeer", from, "err", err) headerCh <- nil return } @@ -89,7 +89,7 @@ func (ex *Exchange) Head(ctx context.Context) (*header.ExtendedHeader, error) { } } - return parseHeads(result) + return bestHead(result) } // GetByHeight performs a request for the ExtendedHeader at the given @@ -159,11 +159,11 @@ func (ex *Exchange) performRequest( //nolint:gosec // G404: Use of weak random number generator index := rand.Intn(len(ex.trustedPeers)) - return doRequest(ctx, ex.trustedPeers[index], ex.host, req) + return request(ctx, ex.trustedPeers[index], ex.host, req) } -// doRequest sends the ExtendedHeaderRequest to a remote peer. -func doRequest( +// request sends the ExtendedHeaderRequest to a remote peer. +func request( ctx context.Context, to peer.ID, host host.Host, @@ -206,18 +206,21 @@ func doRequest( headers[i] = header } + if err = stream.Close(); err != nil { + log.Errorw("while closing stream", err) + } // ensure at least one header was retrieved if len(headers) == 0 { return nil, header.ErrNotFound } - return headers, stream.Close() + return headers, nil } -// parseHeads chooses ExtendedHeader that matches the conditions: +// bestHead chooses ExtendedHeader that matches the conditions: // * should have max height among received; // * should be received at least from 2 peers; -// If both conditions are not met, then ExtendedHeader with the biggest Height will be returned. -func parseHeads(result []*header.ExtendedHeader) (*header.ExtendedHeader, error) { +// If neither condition is met, then latest ExtendedHeader will be returned (header of the highest height). +func bestHead(result []*header.ExtendedHeader) (*header.ExtendedHeader, error) { if len(result) == 0 { return nil, header.ErrNotFound } @@ -237,6 +240,7 @@ func parseHeads(result []*header.ExtendedHeader) (*header.ExtendedHeader, error) return res, nil } } + log.Debug("could not find header received from at least two peers.Returning header with the max height") // otherwise return header with the max height return result[0], nil } diff --git a/header/p2p/exchange_test.go b/header/p2p/exchange_test.go index a8a64eadb9..d5e63e0b27 100644 --- a/header/p2p/exchange_test.go +++ b/header/p2p/exchange_test.go @@ -116,7 +116,10 @@ func Test_parseHeads(t *testing.T) { headerHeight[2]=3 -> 1 result -> headerHeight[2] */ - {precondition: gen, expectedHeight: 3}, + { + precondition: gen, + expectedHeight: 3, + }, /* Height -> Amount headerHeight[0]=1 -> 2 @@ -124,11 +127,12 @@ func Test_parseHeads(t *testing.T) { headerHeight[2]=3 -> 1 result -> headerHeight[0] */ - {precondition: func() []*header.ExtendedHeader { - res := gen() - res = append(res, res[0]) - return res - }, + { + precondition: func() []*header.ExtendedHeader { + res := gen() + res = append(res, res[0]) + return res + }, expectedHeight: 1, }, /* @@ -138,19 +142,20 @@ func Test_parseHeads(t *testing.T) { headerHeight[2]=3 -> 1 result -> headerHeight[1] */ - {precondition: func() []*header.ExtendedHeader { - res := gen() - res = append(res, res[0]) - res = append(res, res[0]) - res = append(res, res[1]) - return res - }, + { + precondition: func() []*header.ExtendedHeader { + res := gen() + res = append(res, res[0]) + res = append(res, res[0]) + res = append(res, res[1]) + return res + }, expectedHeight: 2, }, } for _, tt := range testCases { res := tt.precondition() - header, err := parseHeads(res) + header, err := bestHead(res) require.NoError(t, err) require.True(t, header.Height == tt.expectedHeight) }