2020-06-19 18:25:47 +00:00
|
|
|
package debug
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-03-12 00:03:19 +00:00
|
|
|
"github.com/golang/protobuf/ptypes/empty"
|
2022-10-07 07:24:51 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/network"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/p2p"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
|
2020-06-19 18:25:47 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetPeer returns the data known about the peer defined by the provided peer id.
|
2021-12-07 17:52:39 +00:00
|
|
|
func (ds *Server) GetPeer(_ context.Context, peerReq *ethpb.PeerRequest) (*ethpb.DebugPeerResponse, error) {
|
2020-06-19 18:25:47 +00:00
|
|
|
pid, err := peer.Decode(peerReq.PeerId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "Unable to parse provided peer id: %v", err)
|
|
|
|
}
|
|
|
|
return ds.getPeer(pid)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListPeers returns all peers known to the host node, irregardless of if they are connected/
|
|
|
|
// disconnected.
|
2021-12-07 17:52:39 +00:00
|
|
|
func (ds *Server) ListPeers(_ context.Context, _ *empty.Empty) (*ethpb.DebugPeerResponses, error) {
|
|
|
|
var responses []*ethpb.DebugPeerResponse
|
2020-06-19 18:25:47 +00:00
|
|
|
for _, pid := range ds.PeersFetcher.Peers().All() {
|
|
|
|
resp, err := ds.getPeer(pid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
responses = append(responses, resp)
|
|
|
|
}
|
2021-12-07 17:52:39 +00:00
|
|
|
return ðpb.DebugPeerResponses{Responses: responses}, nil
|
2020-06-19 18:25:47 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 17:52:39 +00:00
|
|
|
func (ds *Server) getPeer(pid peer.ID) (*ethpb.DebugPeerResponse, error) {
|
2020-06-19 18:25:47 +00:00
|
|
|
peers := ds.PeersFetcher.Peers()
|
|
|
|
peerStore := ds.PeerManager.Host().Peerstore()
|
|
|
|
addr, err := peers.Address(pid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "Requested peer does not exist: %v", err)
|
|
|
|
}
|
|
|
|
dir, err := peers.Direction(pid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "Requested peer does not exist: %v", err)
|
|
|
|
}
|
|
|
|
pbDirection := ethpb.PeerDirection_UNKNOWN
|
|
|
|
switch dir {
|
|
|
|
case network.DirInbound:
|
|
|
|
pbDirection = ethpb.PeerDirection_INBOUND
|
|
|
|
case network.DirOutbound:
|
|
|
|
pbDirection = ethpb.PeerDirection_OUTBOUND
|
|
|
|
}
|
|
|
|
connState, err := peers.ConnectionState(pid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "Requested peer does not exist: %v", err)
|
|
|
|
}
|
|
|
|
record, err := peers.ENR(pid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "Requested peer does not exist: %v", err)
|
|
|
|
}
|
|
|
|
enr := ""
|
|
|
|
if record != nil {
|
|
|
|
enr, err = p2p.SerializeENR(record)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Unable to serialize enr: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
metadata, err := peers.Metadata(pid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "Requested peer does not exist: %v", err)
|
|
|
|
}
|
|
|
|
protocols, err := peerStore.GetProtocols(pid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "Requested peer does not exist: %v", err)
|
|
|
|
}
|
2020-07-29 09:26:46 +00:00
|
|
|
resp, err := peers.Scorers().BadResponsesScorer().Count(pid)
|
2020-06-19 18:25:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "Requested peer does not exist: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rawPversion, err := peerStore.Get(pid, "ProtocolVersion")
|
|
|
|
pVersion, ok := rawPversion.(string)
|
|
|
|
if err != nil || !ok {
|
|
|
|
pVersion = ""
|
|
|
|
}
|
|
|
|
rawAversion, err := peerStore.Get(pid, "AgentVersion")
|
|
|
|
aVersion, ok := rawAversion.(string)
|
|
|
|
if err != nil || !ok {
|
|
|
|
aVersion = ""
|
|
|
|
}
|
2021-12-07 17:52:39 +00:00
|
|
|
peerInfo := ðpb.DebugPeerResponse_PeerInfo{
|
2020-06-19 18:25:47 +00:00
|
|
|
Protocols: protocols,
|
|
|
|
FaultCount: uint64(resp),
|
|
|
|
ProtocolVersion: pVersion,
|
|
|
|
AgentVersion: aVersion,
|
|
|
|
PeerLatency: uint64(peerStore.LatencyEWMA(pid).Milliseconds()),
|
|
|
|
}
|
2021-06-02 05:44:34 +00:00
|
|
|
if metadata != nil && !metadata.IsNil() {
|
|
|
|
switch {
|
|
|
|
case metadata.MetadataObjV0() != nil:
|
|
|
|
peerInfo.MetadataV0 = metadata.MetadataObjV0()
|
|
|
|
case metadata.MetadataObjV1() != nil:
|
|
|
|
peerInfo.MetadataV1 = metadata.MetadataObjV1()
|
|
|
|
}
|
|
|
|
}
|
2020-06-19 18:25:47 +00:00
|
|
|
addresses := peerStore.Addrs(pid)
|
2020-10-12 08:11:05 +00:00
|
|
|
var stringAddrs []string
|
2020-06-23 09:00:09 +00:00
|
|
|
if addr != nil {
|
|
|
|
stringAddrs = append(stringAddrs, addr.String())
|
|
|
|
}
|
2020-06-19 18:25:47 +00:00
|
|
|
for _, a := range addresses {
|
|
|
|
// Do not double count address
|
2020-06-23 09:00:09 +00:00
|
|
|
if addr != nil && addr.String() == a.String() {
|
2020-06-19 18:25:47 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
stringAddrs = append(stringAddrs, a.String())
|
|
|
|
}
|
|
|
|
pStatus, err := peers.ChainState(pid)
|
|
|
|
if err != nil {
|
2021-09-15 05:49:50 +00:00
|
|
|
// In the event chain state is non existent, we
|
|
|
|
// initialize with the zero value.
|
|
|
|
pStatus = new(ethpb.Status)
|
2020-06-19 18:25:47 +00:00
|
|
|
}
|
|
|
|
lastUpdated, err := peers.ChainStateLastUpdated(pid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "Requested peer does not exist: %v", err)
|
|
|
|
}
|
|
|
|
unixTime := uint64(0)
|
|
|
|
if !lastUpdated.IsZero() {
|
|
|
|
unixTime = uint64(lastUpdated.Unix())
|
|
|
|
}
|
2021-01-19 12:13:08 +00:00
|
|
|
gScore, bPenalty, topicMaps, err := peers.Scorers().GossipScorer().GossipData(pid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "Requested peer does not exist: %v", err)
|
|
|
|
}
|
2021-12-07 17:52:39 +00:00
|
|
|
scoreInfo := ðpb.ScoreInfo{
|
2021-01-19 12:13:08 +00:00
|
|
|
OverallScore: float32(peers.Scorers().Score(pid)),
|
|
|
|
ProcessedBlocks: peers.Scorers().BlockProviderScorer().ProcessedBlocks(pid),
|
|
|
|
BlockProviderScore: float32(peers.Scorers().BlockProviderScorer().Score(pid)),
|
|
|
|
TopicScores: topicMaps,
|
|
|
|
GossipScore: float32(gScore),
|
|
|
|
BehaviourPenalty: float32(bPenalty),
|
|
|
|
ValidationError: errorToString(peers.Scorers().ValidationError(pid)),
|
|
|
|
}
|
2021-12-07 17:52:39 +00:00
|
|
|
return ðpb.DebugPeerResponse{
|
2020-06-19 18:25:47 +00:00
|
|
|
ListeningAddresses: stringAddrs,
|
|
|
|
Direction: pbDirection,
|
|
|
|
ConnectionState: ethpb.ConnectionState(connState),
|
|
|
|
PeerId: pid.String(),
|
|
|
|
Enr: enr,
|
|
|
|
PeerInfo: peerInfo,
|
|
|
|
PeerStatus: pStatus,
|
|
|
|
LastUpdated: unixTime,
|
2021-01-19 12:13:08 +00:00
|
|
|
ScoreInfo: scoreInfo,
|
2020-06-19 18:25:47 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2021-01-19 12:13:08 +00:00
|
|
|
|
|
|
|
func errorToString(err error) string {
|
|
|
|
if err == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return err.Error()
|
|
|
|
}
|