Skip to content

Commit

Permalink
Merge pull request #911 from iotaledger/remove/p2p-first-packet-check
Browse files Browse the repository at this point in the history
Remove p2p first packet received check
  • Loading branch information
muXxer authored Apr 15, 2024
2 parents 698bfd4 + d2abde3 commit 2c5aeb3
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 19 deletions.
2 changes: 0 additions & 2 deletions pkg/network/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ var (
ErrLoopbackPeer = ierrors.New("loopback connection not allowed")
// ErrDuplicatePeer is returned when the same peer is added more than once.
ErrDuplicatePeer = ierrors.New("already connected")
// ErrFirstPacketNotReceived is returned when the first packet from a peer is not received.
ErrFirstPacketNotReceived = ierrors.New("first packet not received")
// ErrMaxAutopeeringPeersReached is returned when the maximum number of autopeering peers is reached.
ErrMaxAutopeeringPeersReached = ierrors.New("max autopeering peers reached")
)
20 changes: 3 additions & 17 deletions pkg/network/p2p/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package p2p

import (
"context"
"time"

"github.com/libp2p/go-libp2p/core/host"
p2pnetwork "github.com/libp2p/go-libp2p/core/network"
Expand Down Expand Up @@ -145,7 +144,7 @@ func (m *Manager) DialPeer(ctx context.Context, peer *network.Peer) error {
return ierrors.Wrapf(err, "failed to update peer %s", peer.ID.String())
}

if err := m.addNeighbor(ctx, peer, ps); err != nil {
if err := m.addNeighbor(peer, ps); err != nil {
m.closeStream(stream)

return ierrors.Wrapf(err, "failed to add neighbor %s", peer.ID.String())
Expand Down Expand Up @@ -347,7 +346,7 @@ func (m *Manager) handleStream(stream p2pnetwork.Stream) {
return
}

if err := m.addNeighbor(m.ctx, networkPeer, ps); err != nil {
if err := m.addNeighbor(networkPeer, ps); err != nil {
m.logger.LogErrorf("failed to add neighbor, peerID: %s, error: %s", peerID.String(), err.Error())
m.closeStream(stream)

Expand Down Expand Up @@ -376,7 +375,7 @@ func (m *Manager) neighbor(id peer.ID) (*neighbor, error) {
return nbr, nil
}

func (m *Manager) addNeighbor(ctx context.Context, peer *network.Peer, ps *PacketsStream) error {
func (m *Manager) addNeighbor(peer *network.Peer, ps *PacketsStream) error {
if peer.ID == m.libp2pHost.ID() {
return ierrors.WithStack(network.ErrLoopbackPeer)
}
Expand All @@ -392,9 +391,6 @@ func (m *Manager) addNeighbor(ctx context.Context, peer *network.Peer, ps *Packe
return ierrors.WithStack(network.ErrDuplicatePeer)
}

firstPacketReceivedCtx, firstPacketReceivedCancel := context.WithDeadline(ctx, time.Now().Add(5*time.Second))
defer firstPacketReceivedCancel()

var innerErr error
nbr := newNeighbor(m.logger, peer, ps, func(nbr *neighbor, packet proto.Message) {
m.protocolHandlerMutex.RLock()
Expand All @@ -410,7 +406,6 @@ func (m *Manager) addNeighbor(ctx context.Context, peer *network.Peer, ps *Packe
}, func(nbr *neighbor) {
nbr.logger.LogInfof("Neighbor connected: %s", nbr.Peer().ID.String())
nbr.Peer().SetConnStatus(network.ConnStatusConnected)
firstPacketReceivedCancel()
m.neighborAdded.Trigger(nbr)
}, func(nbr *neighbor) {
m.deleteNeighbor(nbr)
Expand All @@ -426,15 +421,6 @@ func (m *Manager) addNeighbor(ctx context.Context, peer *network.Peer, ps *Packe
nbr.readLoop()
nbr.writeLoop()

<-firstPacketReceivedCtx.Done()

if ierrors.Is(firstPacketReceivedCtx.Err(), context.DeadlineExceeded) {
nbr.logger.LogErrorf("First packet not received within deadline")
nbr.Close()

return ierrors.WithStack(network.ErrFirstPacketNotReceived)
}

return innerErr
}

Expand Down

0 comments on commit 2c5aeb3

Please sign in to comment.