Skip to content

Commit

Permalink
chore: rename methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Sep 29, 2022
1 parent eee9c07 commit e44a96e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
26 changes: 15 additions & 11 deletions header/p2p/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
33 changes: 19 additions & 14 deletions header/p2p/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,23 @@ 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
headerHeight[1]=2 -> 1
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,
},
/*
Expand All @@ -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)
}
Expand Down

0 comments on commit e44a96e

Please sign in to comment.