From afd8674c4bb56e5b4b2ac0395ac9059ee3a6e9c4 Mon Sep 17 00:00:00 2001
From: Alex Sharov <AskAlexSharov@gmail.com>
Date: Sun, 4 Jul 2021 14:51:08 +0700
Subject: [PATCH] Peer log (#2294)

* Revert "Revert "better handshake""

This reverts commit 4141c54c952ed32a5a506883b102c7ff8c70c50f.

* better peers logs

* better peers logs
---
 cmd/sentry/download/sentry.go | 12 +++++++-----
 eth/backend.go                | 20 ++++++++++++++++++++
 eth/protocols/eth/protocol.go |  5 +++++
 p2p/dial.go                   |  5 ++---
 p2p/server.go                 |  1 -
 5 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/cmd/sentry/download/sentry.go b/cmd/sentry/download/sentry.go
index 634ff3c5f2..91b5a27356 100644
--- a/cmd/sentry/download/sentry.go
+++ b/cmd/sentry/download/sentry.go
@@ -260,8 +260,7 @@ func runPeer(
 				peerPrinted = true
 			}
 		}
-		var err error
-		if err = common.Stopped(ctx.Done()); err != nil {
+		if err := common.Stopped(ctx.Done()); err != nil {
 			return err
 		}
 		if peerInfo.Removed() {
@@ -844,8 +843,7 @@ func (ss *SentryServerImpl) SetStatus(_ context.Context, statusData *proto_sentr
 	return reply, nil
 }
 
-func (ss *SentryServerImpl) PeerCount(_ context.Context, req *proto_sentry.PeerCountRequest) (*proto_sentry.PeerCountReply, error) {
-	var pc uint64 = 0
+func (ss *SentryServerImpl) SimplePeerCount() (pc int) {
 	ss.Peers.Range(func(key, value interface{}) bool {
 		peerID := key.(string)
 		x, _ := ss.Peers.Load(peerID)
@@ -856,7 +854,11 @@ func (ss *SentryServerImpl) PeerCount(_ context.Context, req *proto_sentry.PeerC
 		pc++
 		return true
 	})
-	return &proto_sentry.PeerCountReply{Count: pc}, nil
+	return pc
+}
+
+func (ss *SentryServerImpl) PeerCount(_ context.Context, req *proto_sentry.PeerCountRequest) (*proto_sentry.PeerCountReply, error) {
+	return &proto_sentry.PeerCountReply{Count: uint64(ss.SimplePeerCount())}, nil
 }
 
 // setupDiscovery creates the node discovery source for the `eth` and `snap`
diff --git a/eth/backend.go b/eth/backend.go
index b1fcac9069..12eecc51cb 100644
--- a/eth/backend.go
+++ b/eth/backend.go
@@ -28,6 +28,7 @@ import (
 	"os"
 	"path"
 	"reflect"
+	"strconv"
 	"sync"
 	"time"
 
@@ -377,6 +378,25 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
 		server65 := download.NewSentryServer(backend.downloadV2Ctx, d65, readNodeInfo, &cfg65, eth.ETH65)
 		backend.sentryServers = append(backend.sentryServers, server65)
 		backend.sentries = append(backend.sentries, remote.NewSentryClientDirect(eth.ETH65, server65))
+		go func() {
+			logEvery := time.NewTicker(60 * time.Second)
+			defer logEvery.Stop()
+
+			var logItems []interface{}
+
+			for {
+				select {
+				case <-backend.downloadV2Ctx.Done():
+					return
+				case <-logEvery.C:
+					logItems = logItems[:0]
+					for _, srv := range backend.sentryServers {
+						logItems = append(logItems, eth.ProtocolToString[srv.Protocol.Version], strconv.Itoa(srv.SimplePeerCount()))
+					}
+					log.Info("[p2p] Peers", logItems...)
+				}
+			}
+		}()
 	}
 	backend.downloadServer, err = download.NewControlServer(chainKv, stack.Config().NodeName(), chainConfig, genesis.Hash(), backend.engine, backend.config.NetworkID, backend.sentries, config.BlockDownloaderWindow)
 	if err != nil {
diff --git a/eth/protocols/eth/protocol.go b/eth/protocols/eth/protocol.go
index 7ce8bee56f..97539aee51 100644
--- a/eth/protocols/eth/protocol.go
+++ b/eth/protocols/eth/protocol.go
@@ -37,6 +37,11 @@ const (
 	ETH66 = 66
 )
 
+var ProtocolToString = map[uint]string{
+	ETH65: "eth65",
+	ETH66: "eth66",
+}
+
 // ProtocolName is the official short name of the `eth` protocol used during
 // devp2p capability negotiation.
 const ProtocolName = "eth"
diff --git a/p2p/dial.go b/p2p/dial.go
index ce1a6e4ebd..961b458442 100644
--- a/p2p/dial.go
+++ b/p2p/dial.go
@@ -247,7 +247,7 @@ loop:
 			nodesCh = nil
 		}
 		d.rearmHistoryTimer(historyExp)
-		d.logStats()
+		//d.logStats()
 
 		select {
 		case <-d.ctx.Done():
@@ -338,9 +338,8 @@ func (d *dialScheduler) readNodes(it enode.Iterator) {
 	}
 }
 
-// logStats prints dialer statistics to the log. The message is suppressed when enough
-// peers are connected because users should only see it while their client is starting up
 // or comes back online.
+//nolint
 func (d *dialScheduler) logStats() {
 	now := d.clock.Now()
 	if d.lastStatsLog.Add(dialStatsLogInterval) > now {
diff --git a/p2p/server.go b/p2p/server.go
index 3883f763e6..57b5ae5b37 100644
--- a/p2p/server.go
+++ b/p2p/server.go
@@ -748,7 +748,6 @@ running:
 		case <-srv.quit:
 			// The server was stopped. Run the cleanup logic.
 			break running
-
 		case n := <-srv.addtrusted:
 			// This channel is used by AddTrustedPeer to add a node
 			// to the trusted node set.
-- 
GitLab