From f8985464d84e1432c96943247f3572c95991d93b Mon Sep 17 00:00:00 2001 From: Manu NALEPA Date: Tue, 26 Mar 2024 19:22:09 +0100 Subject: [PATCH] Status: Implement `{Inbound,Outbound}Connected{TCP,QUIC}`. --- beacon-chain/p2p/peers/status.go | 57 ++++++++++- beacon-chain/p2p/peers/status_test.go | 136 ++++++++++++++++++++++++++ 2 files changed, 191 insertions(+), 2 deletions(-) diff --git a/beacon-chain/p2p/peers/status.go b/beacon-chain/p2p/peers/status.go index 83602a94ce30..c52329f0bb7f 100644 --- a/beacon-chain/p2p/peers/status.go +++ b/beacon-chain/p2p/peers/status.go @@ -26,6 +26,7 @@ import ( "context" "math" "sort" + "strings" "time" "github.com/ethereum/go-ethereum/p2p/enr" @@ -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() @@ -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() @@ -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() diff --git a/beacon-chain/p2p/peers/status_test.go b/beacon-chain/p2p/peers/status_test.go index 726ffab024cd..d2360ed4f61c 100644 --- a/beacon-chain/p2p/peers/status_test.go +++ b/beacon-chain/p2p/peers/status_test.go @@ -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, @@ -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