Эх сурвалжийг харах

Add initial P2P json logs

Taylor Gerring 10 жил өмнө
parent
commit
c14900dbb0
1 өөрчлөгдсөн 61 нэмэгдсэн , 1 устгасан
  1. 61 1
      p2p/server.go

+ 61 - 1
p2p/server.go

@@ -8,6 +8,7 @@ import (
 	"sync"
 	"sync"
 	"time"
 	"time"
 
 
+	"github.com/ethereum/go-ethereum/ethutil"
 	"github.com/ethereum/go-ethereum/logger"
 	"github.com/ethereum/go-ethereum/logger"
 )
 )
 
 
@@ -20,6 +21,8 @@ const (
 
 
 var srvlog = logger.NewLogger("P2P Server")
 var srvlog = logger.NewLogger("P2P Server")
 
 
+var jsonlogger = logger.NewJsonLogger()
+
 // Server manages all peer connections.
 // Server manages all peer connections.
 //
 //
 // The fields of Server are used as configuration parameters.
 // The fields of Server are used as configuration parameters.
@@ -353,9 +356,25 @@ func (srv *Server) dialLoop() {
 // connect to peer via dial out
 // connect to peer via dial out
 func (srv *Server) dialPeer(desc *peerAddr, slot int) {
 func (srv *Server) dialPeer(desc *peerAddr, slot int) {
 	srvlog.Debugf("Dialing %v (slot %d)\n", desc, slot)
 	srvlog.Debugf("Dialing %v (slot %d)\n", desc, slot)
+	evd := map[string]interface{}{
+		"remote_id":       ethutil.Bytes2Hex(desc.Pubkey),
+		"remote_endpoint": desc.String(),
+		"level":           "debug",
+		"guid":            ethutil.Bytes2Hex(srv.Identity.Pubkey()),
+		"num_connections": srv.PeerCount(),
+	}
+	jsonlogger.LogJson("p2p.connecting", evd)
 	conn, err := srv.Dialer.Dial(desc.Network(), desc.String())
 	conn, err := srv.Dialer.Dial(desc.Network(), desc.String())
 	if err != nil {
 	if err != nil {
 		srvlog.DebugDetailf("dial error: %v", err)
 		srvlog.DebugDetailf("dial error: %v", err)
+		evd := map[string]interface{}{
+			"reason":          "dial error",
+			"remote_id":       desc.String(),
+			"level":           "debug",
+			"guid":            ethutil.Bytes2Hex(srv.Identity.Pubkey()),
+			"num_connections": srv.PeerCount(),
+		}
+		jsonlogger.LogJson("p2p.disconnecting", evd)
 		srv.peerSlots <- slot
 		srv.peerSlots <- slot
 		return
 		return
 	}
 	}
@@ -375,7 +394,17 @@ func (srv *Server) addPeer(conn net.Conn, desc *peerAddr, slot int) *Peer {
 	peer.slot = slot
 	peer.slot = slot
 	srv.peers[slot] = peer
 	srv.peers[slot] = peer
 	srv.peerCount++
 	srv.peerCount++
-	go func() { peer.loop(); srv.peerDisconnect <- peer }()
+	go func() {
+		evd := map[string]interface{}{
+			"guid":            ethutil.Bytes2Hex(srv.Identity.Pubkey()),
+			"num_connections": srv.PeerCount(),
+			"remote_id":       desc.String(),
+			"level":           "debug",
+		}
+		jsonlogger.LogJson("p2p.connected", evd)
+		peer.loop()
+		srv.peerDisconnect <- peer
+	}()
 	return peer
 	return peer
 }
 }
 
 
@@ -393,13 +422,36 @@ func (srv *Server) removePeer(peer *Peer) {
 	srv.peers[peer.slot] = nil
 	srv.peers[peer.slot] = nil
 	// release slot to signal need for a new peer, last!
 	// release slot to signal need for a new peer, last!
 	srv.peerSlots <- peer.slot
 	srv.peerSlots <- peer.slot
+	evd := map[string]interface{}{
+		"guid":            ethutil.Bytes2Hex(srv.Identity.Pubkey()),
+		"num_connections": srv.PeerCount(),
+		"remote_id":       ethutil.Bytes2Hex(peer.Identity().Pubkey()),
+		"level":           "debug",
+	}
+	jsonlogger.LogJson("p2p.disconnected", evd)
 }
 }
 
 
 func (srv *Server) verifyPeer(addr *peerAddr) error {
 func (srv *Server) verifyPeer(addr *peerAddr) error {
 	if srv.Blacklist.Exists(addr.Pubkey) {
 	if srv.Blacklist.Exists(addr.Pubkey) {
+		evd := map[string]interface{}{
+			"reason":          "blacklisted",
+			"remote_id":       addr.String(),
+			"level":           "debug",
+			"guid":            ethutil.Bytes2Hex(srv.Identity.Pubkey()),
+			"num_connections": srv.PeerCount(),
+		}
+		jsonlogger.LogJson("p2p.disconnecting.reputation", evd)
 		return errors.New("blacklisted")
 		return errors.New("blacklisted")
 	}
 	}
 	if bytes.Equal(srv.Identity.Pubkey()[1:], addr.Pubkey) {
 	if bytes.Equal(srv.Identity.Pubkey()[1:], addr.Pubkey) {
+		evd := map[string]interface{}{
+			"reason":          "not allowed to connect to srv",
+			"remote_id":       addr.String(),
+			"level":           "debug",
+			"guid":            ethutil.Bytes2Hex(srv.Identity.Pubkey()),
+			"num_connections": srv.PeerCount(),
+		}
+		jsonlogger.LogJson("p2p.disconnecting", evd)
 		return newPeerError(errPubkeyForbidden, "not allowed to connect to srv")
 		return newPeerError(errPubkeyForbidden, "not allowed to connect to srv")
 	}
 	}
 	srv.lock.RLock()
 	srv.lock.RLock()
@@ -408,6 +460,14 @@ func (srv *Server) verifyPeer(addr *peerAddr) error {
 		if peer != nil {
 		if peer != nil {
 			id := peer.Identity()
 			id := peer.Identity()
 			if id != nil && bytes.Equal(id.Pubkey(), addr.Pubkey) {
 			if id != nil && bytes.Equal(id.Pubkey(), addr.Pubkey) {
+				evd := map[string]interface{}{
+					"reason":          "already connected",
+					"remote_id":       addr.String(),
+					"level":           "debug",
+					"guid":            ethutil.Bytes2Hex(srv.Identity.Pubkey()),
+					"num_connections": srv.PeerCount(),
+				}
+				jsonlogger.LogJson("p2p.disconnecting", evd)
 				return errors.New("already connected")
 				return errors.New("already connected")
 			}
 			}
 		}
 		}