From 0ff2a53b2fa56454c765a6a9c3d432bc94b9e29b Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Fri, 8 Jan 2021 19:08:11 +0800 Subject: [PATCH] Add Peer Logger (#8226) Co-authored-by: Victor Farazdagi --- beacon-chain/p2p/BUILD.bazel | 1 + beacon-chain/p2p/peers/status.go | 26 ++++++++++++++++++++++++++ beacon-chain/p2p/service.go | 10 ++++++++++ 3 files changed, 37 insertions(+) diff --git a/beacon-chain/p2p/BUILD.bazel b/beacon-chain/p2p/BUILD.bazel index 65fda862c..f5c173e72 100644 --- a/beacon-chain/p2p/BUILD.bazel +++ b/beacon-chain/p2p/BUILD.bazel @@ -71,6 +71,7 @@ go_library( "@com_github_kevinms_leakybucket_go//:go_default_library", "@com_github_libp2p_go_libp2p//:go_default_library", "@com_github_libp2p_go_libp2p//config:go_default_library", + "@com_github_libp2p_go_libp2p//p2p/protocol/identify:go_default_library", "@com_github_libp2p_go_libp2p_core//connmgr:go_default_library", "@com_github_libp2p_go_libp2p_core//control:go_default_library", "@com_github_libp2p_go_libp2p_core//crypto:go_default_library", diff --git a/beacon-chain/p2p/peers/status.go b/beacon-chain/p2p/peers/status.go index 70f682fb7..0bc15a528 100644 --- a/beacon-chain/p2p/peers/status.go +++ b/beacon-chain/p2p/peers/status.go @@ -369,6 +369,32 @@ func (p *Status) Connected() []peer.ID { return peers } +// Inbound returns the current batch of inbound peers that are connected. +func (p *Status) Inbound() []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 { + peers = append(peers, pid) + } + } + return peers +} + +// Outbound returns the current batch of outbound peers that are connected. +func (p *Status) Outbound() []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 { + peers = append(peers, pid) + } + } + return peers +} + // Active returns the peers that are connecting or connected. func (p *Status) Active() []peer.ID { p.store.RLock() diff --git a/beacon-chain/p2p/service.go b/beacon-chain/p2p/service.go index b41d13e7c..ec0e03d7b 100644 --- a/beacon-chain/p2p/service.go +++ b/beacon-chain/p2p/service.go @@ -19,6 +19,7 @@ import ( "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/protocol" pubsub "github.com/libp2p/go-libp2p-pubsub" + "github.com/libp2p/go-libp2p/p2p/protocol/identify" "github.com/multiformats/go-multiaddr" "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/beacon-chain/core/feed" @@ -32,6 +33,7 @@ import ( "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/runutil" "github.com/prysmaticlabs/prysm/shared/slotutil" + "github.com/sirupsen/logrus" "go.opencensus.io/trace" ) @@ -127,6 +129,7 @@ func NewService(ctx context.Context, cfg *Config) (*Service, error) { } s.host = h + s.host.RemoveStreamHandler(identify.IDDelta) // Gossipsub registration is done before we add in any new peers // due to libp2p's gossipsub implementation not taking into @@ -229,6 +232,13 @@ func (s *Service) Start() { runutil.RunEvery(s.ctx, refreshRate, func() { s.RefreshENR() }) + runutil.RunEvery(s.ctx, 1*time.Minute, func() { + log.WithFields(logrus.Fields{ + "inbound": len(s.peers.Inbound()), + "outbound": len(s.peers.Outbound()), + "activePeers": len(s.peers.Active()), + }).Info("Peer summary") + }) multiAddrs := s.host.Network().ListenAddresses() logIPAddr(s.host.ID(), multiAddrs...)