Skip to content

Commit

Permalink
Peer count node api (#8306)
Browse files Browse the repository at this point in the history
Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
  • Loading branch information
rkapka and rauljordan authored Jan 21, 2021
1 parent 6e643ac commit 9cc1438
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
11 changes: 10 additions & 1 deletion beacon-chain/rpc/nodev1/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,16 @@ func (ns *Server) PeerCount(ctx context.Context, _ *ptypes.Empty) (*ethpb.PeerCo
ctx, span := trace.StartSpan(ctx, "nodev1.PeerCount")
defer span.End()

return nil, errors.New("unimplemented")
peerStatus := ns.PeersFetcher.Peers()

return &ethpb.PeerCountResponse{
Data: &ethpb.PeerCountResponse_PeerCount{
Disconnected: uint64(len(peerStatus.Disconnected())),
Connecting: uint64(len(peerStatus.Connecting())),
Connected: uint64(len(peerStatus.Connected())),
Disconnecting: uint64(len(peerStatus.Disconnecting())),
},
}, nil
}

// GetVersion requests that the beacon node identify information about its implementation in a
Expand Down
48 changes: 48 additions & 0 deletions beacon-chain/rpc/nodev1/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,54 @@ func TestListPeers(t *testing.T) {
}
}

func TestPeerCount(t *testing.T) {
ids := libp2ptest.GeneratePeerIDs(10)
peerFetcher := &mockp2p.MockPeersProvider{}
peerFetcher.ClearPeers()
peerStatus := peerFetcher.Peers()

for i, id := range ids {
enrRecord := &enr.Record{}
err := enrRecord.SetSig(dummyIdentity{1}, []byte{42})
require.NoError(t, err)
enrRecord.Set(enr.IPv4{127, 0, 0, byte(i)})
err = enrRecord.SetSig(dummyIdentity{}, []byte{})
require.NoError(t, err)
var p2pAddr = "/ip4/127.0.0." + strconv.Itoa(i) + "/udp/30303/p2p/QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N"
p2pMultiAddr, err := ma.NewMultiaddr(p2pAddr)
require.NoError(t, err)

var direction network.Direction
if i%2 == 0 {
direction = network.DirInbound
} else {
direction = network.DirOutbound
}
peerStatus.Add(enrRecord, id, p2pMultiAddr, direction)

switch i {
case 0:
peerStatus.SetConnectionState(id, peers.PeerConnecting)
case 1, 2:
peerStatus.SetConnectionState(id, peers.PeerConnected)
case 3, 4, 5:
peerStatus.SetConnectionState(id, peers.PeerDisconnecting)
case 6, 7, 8, 9:
peerStatus.SetConnectionState(id, peers.PeerDisconnected)
default:
t.Fatalf("Failed to set connection state for peer")
}
}

s := Server{PeersFetcher: peerFetcher}
resp, err := s.PeerCount(context.Background(), &ptypes.Empty{})
require.NoError(t, err)
assert.Equal(t, uint64(1), uint64(resp.Data.Connecting), "Wrong number of connecting peers")
assert.Equal(t, uint64(2), uint64(resp.Data.Connected), "Wrong number of connected peers")
assert.Equal(t, uint64(3), uint64(resp.Data.Disconnecting), "Wrong number of disconnecting peers")
assert.Equal(t, uint64(4), uint64(resp.Data.Disconnected), "Wrong number of disconnected peers")
}

func BenchmarkListPeers(b *testing.B) {
// We simulate having a lot of peers.
ids := libp2ptest.GeneratePeerIDs(2000)
Expand Down

0 comments on commit 9cc1438

Please sign in to comment.