Skip to content

Commit

Permalink
Status: Implement {Inbound,Outbound}Connected{TCP,QUIC}.
Browse files Browse the repository at this point in the history
  • Loading branch information
nalepae committed Apr 2, 2024
1 parent f2cc676 commit f898546
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 2 deletions.
57 changes: 55 additions & 2 deletions beacon-chain/p2p/peers/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"context"
"math"
"sort"
"strings"
"time"

"github.com/ethereum/go-ethereum/p2p/enr"
Expand Down Expand Up @@ -449,6 +450,32 @@ func (p *Status) InboundConnected() []peer.ID {
return peers
}

// InboundConnectedTCP returns the current batch of inbound peers that are connected using TCP.
func (p *Status) InboundConnectedTCP() []peer.ID {
p.store.RLock()
defer p.store.RUnlock()
peers := make([]peer.ID, 0)
for pid, peerData := range p.store.Peers() {
if peerData.ConnState == PeerConnected && peerData.Direction == network.DirInbound && strings.Contains(peerData.Address.String(), "tcp") {
peers = append(peers, pid)
}
}
return peers
}

// InboundConnectedTCP returns the current batch of inbound peers that are connected using QUIC.
func (p *Status) InboundConnectedQUIC() []peer.ID {
p.store.RLock()
defer p.store.RUnlock()
peers := make([]peer.ID, 0)
for pid, peerData := range p.store.Peers() {
if peerData.ConnState == PeerConnected && peerData.Direction == network.DirInbound && strings.Contains(peerData.Address.String(), "quic") {
peers = append(peers, pid)
}
}
return peers
}

// Outbound returns the current batch of outbound peers.
func (p *Status) Outbound() []peer.ID {
p.store.RLock()
Expand All @@ -475,7 +502,33 @@ func (p *Status) OutboundConnected() []peer.ID {
return peers
}

// Active returns the peers that are connecting or connected.
// OutboundConnected returns the current batch of outbound peers that are connected using TCP.
func (p *Status) OutboundConnectedTCP() []peer.ID {
p.store.RLock()
defer p.store.RUnlock()
peers := make([]peer.ID, 0)
for pid, peerData := range p.store.Peers() {
if peerData.ConnState == PeerConnected && peerData.Direction == network.DirOutbound && strings.Contains(peerData.Address.String(), "tcp") {
peers = append(peers, pid)
}
}
return peers
}

// OutboundConnected returns the current batch of outbound peers that are connected using QUIC.
func (p *Status) OutboundConnectedQUIC() []peer.ID {
p.store.RLock()
defer p.store.RUnlock()
peers := make([]peer.ID, 0)
for pid, peerData := range p.store.Peers() {
if peerData.ConnState == PeerConnected && peerData.Direction == network.DirOutbound && strings.Contains(peerData.Address.String(), "quic") {
peers = append(peers, pid)
}
}
return peers
}

// Active returns the peers that are active (connecting or connected).
func (p *Status) Active() []peer.ID {
p.store.RLock()
defer p.store.RUnlock()
Expand Down Expand Up @@ -514,7 +567,7 @@ func (p *Status) Disconnected() []peer.ID {
return peers
}

// Inactive returns the peers that are disconnecting or disconnected.
// Inactive returns the peers that are inactive (disconnecting or disconnected).
func (p *Status) Inactive() []peer.ID {
p.store.RLock()
defer p.store.RUnlock()
Expand Down
136 changes: 136 additions & 0 deletions beacon-chain/p2p/peers/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,74 @@ func TestInbound(t *testing.T) {
assert.Equal(t, inbound.String(), result[0].String())
}

func TestInboundConnected(t *testing.T) {
p := peers.NewStatus(context.Background(), &peers.StatusConfig{
PeerLimit: 30,
ScorerParams: &scorers.Config{
BadResponsesScorerConfig: &scorers.BadResponsesScorerConfig{
Threshold: 0,
},
},
})

addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/33333")
require.NoError(t, err)
inbound := createPeer(t, p, addr, network.DirInbound, peers.PeerConnected)
createPeer(t, p, addr, network.DirInbound, peers.PeerConnecting)

result := p.InboundConnected()
require.Equal(t, 1, len(result))
assert.Equal(t, inbound.String(), result[0].String())
}

func TestInboundConnectedTCP(t *testing.T) {
p := peers.NewStatus(context.Background(), &peers.StatusConfig{
PeerLimit: 30,
ScorerParams: &scorers.Config{
BadResponsesScorerConfig: &scorers.BadResponsesScorerConfig{
Threshold: 0,
},
},
})

addrTCP, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/33333")
require.NoError(t, err)

addrQUIC, err := ma.NewMultiaddr("/ip4/192.168.1.3/udp/13000/quic-v1")
require.NoError(t, err)

inboundTCP := createPeer(t, p, addrTCP, network.DirInbound, peers.PeerConnected)
createPeer(t, p, addrQUIC, network.DirInbound, peers.PeerConnected)

result := p.InboundConnectedTCP()
require.Equal(t, 1, len(result))
assert.Equal(t, inboundTCP.String(), result[0].String())
}

func TestInboundConnectedQUIC(t *testing.T) {
p := peers.NewStatus(context.Background(), &peers.StatusConfig{
PeerLimit: 30,
ScorerParams: &scorers.Config{
BadResponsesScorerConfig: &scorers.BadResponsesScorerConfig{
Threshold: 0,
},
},
})

addrQUIC, err := ma.NewMultiaddr("/ip4/192.168.1.3/udp/13000/quic-v1")
require.NoError(t, err)

addrTCP, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/33333")
require.NoError(t, err)

inboundQUIC := createPeer(t, p, addrQUIC, network.DirInbound, peers.PeerConnected)
createPeer(t, p, addrTCP, network.DirInbound, peers.PeerConnected)

result := p.InboundConnectedQUIC()
require.Equal(t, 1, len(result))
assert.Equal(t, inboundQUIC.String(), result[0].String())
}

func TestOutbound(t *testing.T) {
p := peers.NewStatus(context.Background(), &peers.StatusConfig{
PeerLimit: 30,
Expand All @@ -1130,6 +1198,74 @@ func TestOutbound(t *testing.T) {
assert.Equal(t, outbound.String(), result[0].String())
}

func TestOutboundConnected(t *testing.T) {
p := peers.NewStatus(context.Background(), &peers.StatusConfig{
PeerLimit: 30,
ScorerParams: &scorers.Config{
BadResponsesScorerConfig: &scorers.BadResponsesScorerConfig{
Threshold: 0,
},
},
})

addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/33333")
require.NoError(t, err)
inbound := createPeer(t, p, addr, network.DirOutbound, peers.PeerConnected)
createPeer(t, p, addr, network.DirOutbound, peers.PeerConnecting)

result := p.OutboundConnected()
require.Equal(t, 1, len(result))
assert.Equal(t, inbound.String(), result[0].String())
}

func TestOutbondConnectedTCP(t *testing.T) {
p := peers.NewStatus(context.Background(), &peers.StatusConfig{
PeerLimit: 30,
ScorerParams: &scorers.Config{
BadResponsesScorerConfig: &scorers.BadResponsesScorerConfig{
Threshold: 0,
},
},
})

addrTCP, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/33333")
require.NoError(t, err)

addrQUIC, err := ma.NewMultiaddr("/ip4/192.168.1.3/udp/13000/quic-v1")
require.NoError(t, err)

outboundTCP := createPeer(t, p, addrTCP, network.DirOutbound, peers.PeerConnected)
createPeer(t, p, addrQUIC, network.DirOutbound, peers.PeerConnected)

result := p.OutboundConnectedTCP()
require.Equal(t, 1, len(result))
assert.Equal(t, outboundTCP.String(), result[0].String())
}

func TestOutboundConnectedQUIC(t *testing.T) {
p := peers.NewStatus(context.Background(), &peers.StatusConfig{
PeerLimit: 30,
ScorerParams: &scorers.Config{
BadResponsesScorerConfig: &scorers.BadResponsesScorerConfig{
Threshold: 0,
},
},
})

addrQUIC, err := ma.NewMultiaddr("/ip4/192.168.1.3/udp/13000/quic-v1")
require.NoError(t, err)

addrTCP, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/33333")
require.NoError(t, err)

outboundQUIC := createPeer(t, p, addrQUIC, network.DirOutbound, peers.PeerConnected)
createPeer(t, p, addrTCP, network.DirOutbound, peers.PeerConnected)

result := p.OutboundConnectedQUIC()
require.Equal(t, 1, len(result))
assert.Equal(t, outboundQUIC.String(), result[0].String())
}

// addPeer is a helper to add a peer with a given connection state)
func addPeer(t *testing.T, p *peers.Status, state peerdata.PeerConnectionState) peer.ID {
// Set up some peers with different states
Expand Down

0 comments on commit f898546

Please sign in to comment.