p2p: add address info to peer event reporting (#19716)

This commit is contained in:
Martin Holst Swende 2019-07-05 20:27:13 +02:00 committed by Felix Lange
parent dcc4adfcd7
commit 7fd82a0e3e
3 changed files with 40 additions and 26 deletions

View File

@ -255,16 +255,20 @@ type msgEventer struct {
feed *event.Feed feed *event.Feed
peerID enode.ID peerID enode.ID
Protocol string Protocol string
localAddress string
remoteAddress string
} }
// newMsgEventer returns a msgEventer which sends message events to the given // newMsgEventer returns a msgEventer which sends message events to the given
// feed // feed
func newMsgEventer(rw MsgReadWriter, feed *event.Feed, peerID enode.ID, proto string) *msgEventer { func newMsgEventer(rw MsgReadWriter, feed *event.Feed, peerID enode.ID, proto, remote, local string) *msgEventer {
return &msgEventer{ return &msgEventer{
MsgReadWriter: rw, MsgReadWriter: rw,
feed: feed, feed: feed,
peerID: peerID, peerID: peerID,
Protocol: proto, Protocol: proto,
remoteAddress: remote,
localAddress: local,
} }
} }
@ -281,6 +285,8 @@ func (ev *msgEventer) ReadMsg() (Msg, error) {
Protocol: ev.Protocol, Protocol: ev.Protocol,
MsgCode: &msg.Code, MsgCode: &msg.Code,
MsgSize: &msg.Size, MsgSize: &msg.Size,
LocalAddress: ev.localAddress,
RemoteAddress: ev.remoteAddress,
}) })
return msg, nil return msg, nil
} }
@ -298,6 +304,8 @@ func (ev *msgEventer) WriteMsg(msg Msg) error {
Protocol: ev.Protocol, Protocol: ev.Protocol,
MsgCode: &msg.Code, MsgCode: &msg.Code,
MsgSize: &msg.Size, MsgSize: &msg.Size,
LocalAddress: ev.localAddress,
RemoteAddress: ev.remoteAddress,
}) })
return nil return nil
} }

View File

@ -97,6 +97,8 @@ type PeerEvent struct {
Protocol string `json:"protocol,omitempty"` Protocol string `json:"protocol,omitempty"`
MsgCode *uint64 `json:"msg_code,omitempty"` MsgCode *uint64 `json:"msg_code,omitempty"`
MsgSize *uint32 `json:"msg_size,omitempty"` MsgSize *uint32 `json:"msg_size,omitempty"`
LocalAddress string `json:"local,omitempty"`
RemoteAddress string `json:"remote,omitempty"`
} }
// Peer represents a connected remote node. // Peer represents a connected remote node.
@ -354,7 +356,7 @@ func (p *Peer) startProtocols(writeStart <-chan struct{}, writeErr chan<- error)
proto.werr = writeErr proto.werr = writeErr
var rw MsgReadWriter = proto var rw MsgReadWriter = proto
if p.events != nil { if p.events != nil {
rw = newMsgEventer(rw, p.events, p.ID(), proto.Name) rw = newMsgEventer(rw, p.events, p.ID(), proto.Name, p.Info().Network.RemoteAddress, p.Info().Network.LocalAddress)
} }
p.log.Trace(fmt.Sprintf("Starting protocol %s/%d", proto.Name, proto.Version)) p.log.Trace(fmt.Sprintf("Starting protocol %s/%d", proto.Name, proto.Version))
go func() { go func() {

View File

@ -1018,6 +1018,8 @@ func (srv *Server) runPeer(p *Peer) {
srv.peerFeed.Send(&PeerEvent{ srv.peerFeed.Send(&PeerEvent{
Type: PeerEventTypeAdd, Type: PeerEventTypeAdd,
Peer: p.ID(), Peer: p.ID(),
RemoteAddress: p.RemoteAddr().String(),
LocalAddress: p.LocalAddr().String(),
}) })
// run the protocol // run the protocol
@ -1028,6 +1030,8 @@ func (srv *Server) runPeer(p *Peer) {
Type: PeerEventTypeDrop, Type: PeerEventTypeDrop,
Peer: p.ID(), Peer: p.ID(),
Error: err.Error(), Error: err.Error(),
RemoteAddress: p.RemoteAddr().String(),
LocalAddress: p.LocalAddr().String(),
}) })
// Note: run waits for existing peers to be sent on srv.delpeer // Note: run waits for existing peers to be sent on srv.delpeer