Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions pkg/p2p/libp2p/connections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1067,10 +1067,9 @@ func TestTopologyOverSaturated(t *testing.T) {
addr := serviceUnderlayAddress(t, s1)

// s2 connects to s1, thus the notifier on s1 should be called on Connect
_, err := s2.Connect(ctx, addr)
if err == nil {
t.Fatal("expected connect to fail but it didn't")
}
// Connect might return nil if the handshake completes before the server processes the rejection (protocol race).
// We verify that the peer is eventually disconnected.
_, _ = s2.Connect(ctx, addr)

expectPeers(t, s1)
expectPeersEventually(t, s2)
Expand Down Expand Up @@ -1171,9 +1170,10 @@ func TestWithBlocklistStreams(t *testing.T) {
expectPeersEventually(t, s2)
expectPeersEventually(t, s1)

if _, err := s2.Connect(ctx, s1_underlay); err == nil {
t.Fatal("expected error when connecting to blocklisted peer")
}
// s2 connects to s1, but because of blocklist it should fail
// Connect might return nil if the handshake completes before the server processes the blocklist (protocol race).
// We verify that the peer is eventually disconnected.
_, _ = s2.Connect(ctx, s1_underlay)

expectPeersEventually(t, s2)
expectPeersEventually(t, s1)
Expand Down
12 changes: 10 additions & 2 deletions pkg/p2p/libp2p/internal/handshake/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"slices"
"sync"
"sync/atomic"
"time"

Expand Down Expand Up @@ -94,6 +95,7 @@ type Service struct {
libp2pID libp2ppeer.ID
metrics metrics
picker p2p.Picker
mu sync.RWMutex
hostAddresser Addresser
}

Expand Down Expand Up @@ -136,6 +138,8 @@ func New(signer crypto.Signer, advertisableAddresser AdvertisableAddressResolver
}

func (s *Service) SetPicker(n p2p.Picker) {
s.mu.Lock()
defer s.mu.Unlock()
s.picker = n
}

Expand Down Expand Up @@ -351,8 +355,12 @@ func (s *Service) Handle(ctx context.Context, stream p2p.Stream, peerMultiaddrs

overlay := swarm.NewAddress(ack.Address.Overlay)

if s.picker != nil {
if !s.picker.Pick(p2p.Peer{Address: overlay, FullNode: ack.FullNode}) {
s.mu.RLock()
picker := s.picker
s.mu.RUnlock()

if picker != nil {
if !picker.Pick(p2p.Peer{Address: overlay, FullNode: ack.FullNode}) {
return nil, ErrPicker
}
}
Expand Down
Loading