Skip to content

Commit

Permalink
fix(p2p/session): return err if peer tracker is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Apr 2, 2024
1 parent 2511ced commit 4f48548
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
6 changes: 5 additions & 1 deletion p2p/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,13 @@ func (ex *Exchange[H]) GetRangeByHeight(
attribute.Int64("to", int64(to)),
))
defer span.End()
session := newSession[H](
session, err := newSession[H](
ex.ctx, ex.host, ex.peerTracker, ex.protocolID, ex.Params.RangeRequestTimeout, ex.metrics, withValidation(from),
)
// TODO(@vgonkivs): decide what to do with this error. Maybe we should fall into "discovery mode" and try to collect peers???
if err != nil {
return nil, err
}
defer session.close()
// we request the next header height that we don't have: `fromHead`+1
amount := to - (from.Height() + 1)
Expand Down
11 changes: 8 additions & 3 deletions p2p/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,28 @@ func newSession[H header.Header[H]](
requestTimeout time.Duration,
metrics *exchangeMetrics,
options ...option[H],
) *session[H] {
) (*session[H], error) {
ctx, cancel := context.WithCancel(ctx)
ses := &session[H]{
ctx: ctx,
cancel: cancel,
protocolID: protocolID,
host: h,
queue: newPeerQueue(ctx, peerTracker.peers()),
peerTracker: peerTracker,
requestTimeout: requestTimeout,
metrics: metrics,
}

peers := peerTracker.peers()
if len(peers) == 0 {
return nil, errors.New("empty peer tracker")
}
ses.queue = newPeerQueue(ctx, peers)

for _, opt := range options {
opt(ses)
}
return ses
return ses, nil
}

// getRangeByHeight requests headers from different peers.
Expand Down
28 changes: 20 additions & 8 deletions p2p/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"time"

"github.com/libp2p/go-libp2p/core/peer"
blankhost "github.com/libp2p/go-libp2p/p2p/host/blank"
swarm "github.com/libp2p/go-libp2p/p2p/net/swarm/testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand All @@ -25,34 +27,44 @@ func Test_PrepareRequests(t *testing.T) {
func Test_Validate(t *testing.T) {
suite := headertest.NewTestSuite(t)
head := suite.Head()
ses := newSession(
peerId := peer.ID("test")
pT := &peerTracker{trackedPeers: make(map[peer.ID]struct{})}
pT.trackedPeers[peerId] = struct{}{}
pT.host = blankhost.NewBlankHost(swarm.GenSwarm(t))
ses, err := newSession(
context.Background(),
nil,
&peerTracker{trackedPeers: make(map[peer.ID]struct{})},
pT,
"", time.Second, nil,
withValidation(head),
)

require.NoError(t, err)
headers := suite.GenDummyHeaders(5)
err := ses.verify(headers)
err = ses.verify(headers)
assert.NoError(t, err)
}

// Test_ValidateFails ensures that non-adjacent range will return an error.
func Test_ValidateFails(t *testing.T) {
suite := headertest.NewTestSuite(t)
head := suite.Head()
ses := newSession(

peerId := peer.ID("test")
pT := &peerTracker{trackedPeers: make(map[peer.ID]struct{})}
pT.trackedPeers[peerId] = struct{}{}
pT.host = blankhost.NewBlankHost(swarm.GenSwarm(t))
ses, err := newSession(
context.Background(),
nil,
&peerTracker{trackedPeers: make(map[peer.ID]struct{})},
blankhost.NewBlankHost(swarm.GenSwarm(t)),
pT,
"", time.Second, nil,
withValidation(head),
)

require.NoError(t, err)
headers := suite.GenDummyHeaders(5)
// break adjacency
headers[2] = headers[4]
err := ses.verify(headers)
err = ses.verify(headers)
assert.Error(t, err)
}

0 comments on commit 4f48548

Please sign in to comment.