From c919283b0ca8e3a2038e76ada5b82db2d431260c Mon Sep 17 00:00:00 2001 From: ledgerwatch Date: Fri, 19 May 2023 23:08:45 +0100 Subject: [PATCH] [devnet] separate logging p2p (#7549) Co-authored-by: Alex Sharp --- cmd/bootnode/main.go | 2 +- cmd/observer/observer/server.go | 2 +- p2p/discover/v4_lookup_test.go | 10 +++++++--- p2p/nat/nat.go | 14 +++++++------- p2p/server.go | 4 ++-- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/cmd/bootnode/main.go b/cmd/bootnode/main.go index 58c732ae5..ee1bb44e6 100644 --- a/cmd/bootnode/main.go +++ b/cmd/bootnode/main.go @@ -108,7 +108,7 @@ func main() { realaddr := conn.LocalAddr().(*net.UDPAddr) if natm != nil { if !realaddr.IP.IsLoopback() && natm.SupportsMapping() { - go nat.Map(natm, nil, "udp", realaddr.Port, realaddr.Port, "ethereum discovery") + go nat.Map(natm, nil, "udp", realaddr.Port, realaddr.Port, "ethereum discovery", logger) } if ext, err := natm.ExternalIP(); err == nil { realaddr = &net.UDPAddr{IP: ext, Port: realaddr.Port} diff --git a/cmd/observer/observer/server.go b/cmd/observer/observer/server.go index f785561a7..b10879a89 100644 --- a/cmd/observer/observer/server.go +++ b/cmd/observer/observer/server.go @@ -133,7 +133,7 @@ func (server *Server) mapNATPort(ctx context.Context, realAddr *net.UDPAddr) { go func() { defer debug.LogPanic() - nat.Map(server.natInterface, ctx.Done(), "udp", realAddr.Port, realAddr.Port, "ethereum discovery") + nat.Map(server.natInterface, ctx.Done(), "udp", realAddr.Port, realAddr.Port, "ethereum discovery", server.logger) }() } diff --git a/p2p/discover/v4_lookup_test.go b/p2p/discover/v4_lookup_test.go index bd1fb591c..f4083764b 100644 --- a/p2p/discover/v4_lookup_test.go +++ b/p2p/discover/v4_lookup_test.go @@ -29,6 +29,7 @@ import ( "github.com/ledgerwatch/erigon/crypto" "github.com/ledgerwatch/erigon/p2p/discover/v4wire" "github.com/ledgerwatch/erigon/p2p/enode" + "github.com/ledgerwatch/log/v3" ) func TestUDPv4_Lookup(t *testing.T) { @@ -36,11 +37,12 @@ func TestUDPv4_Lookup(t *testing.T) { t.Skip("fix me on win please") } t.Parallel() + logger := log.New() ctx := context.Background() ctx = contextWithReplyTimeout(ctx, time.Second) - test := newUDPTestContext(ctx, t) + test := newUDPTestContext(ctx, t, logger) defer test.close() // Lookup on empty table returns no nodes. @@ -74,6 +76,7 @@ func TestUDPv4_LookupIterator(t *testing.T) { t.Skip("fix me on win please") } t.Parallel() + logger := log.New() // Set up RandomNodes() to use expected keys instead of generating random ones. testNetPrivateKeys := lookupTestnet.privateKeys() @@ -86,7 +89,7 @@ func TestUDPv4_LookupIterator(t *testing.T) { ctx = contextWithReplyTimeout(ctx, time.Second) ctx = contextWithPrivateKeyGenerator(ctx, privateKeyGenerator) - test := newUDPTestContext(ctx, t) + test := newUDPTestContext(ctx, t, logger) defer test.close() // Seed table with initial nodes. @@ -121,7 +124,8 @@ func TestUDPv4_LookupIterator(t *testing.T) { // method is called. func TestUDPv4_LookupIteratorClose(t *testing.T) { t.Parallel() - test := newUDPTest(t) + logger := log.New() + test := newUDPTest(t, logger) defer test.close() // Seed table with initial nodes. diff --git a/p2p/nat/nat.go b/p2p/nat/nat.go index 9e328ecf6..b2262955e 100644 --- a/p2p/nat/nat.go +++ b/p2p/nat/nat.go @@ -110,22 +110,22 @@ const ( // Map adds a port mapping on m and keeps it alive until c is closed. // This function is typically invoked in its own goroutine. -func Map(m Interface, c <-chan struct{}, protocol string, extport, intport int, name string) { +func Map(m Interface, c <-chan struct{}, protocol string, extport, intport int, name string, logger log.Logger) { if !m.SupportsMapping() { panic("Port mapping is not supported") } - logger := log.New("proto", protocol, "extport", extport, "intport", intport, "interface", m) + logger1 := logger.New("proto", protocol, "extport", extport, "intport", intport, "interface", m) refresh := time.NewTimer(mapTimeout) defer func() { refresh.Stop() - logger.Trace("Deleting port mapping") + logger1.Trace("Deleting port mapping") m.DeleteMapping(protocol, extport, intport) }() if err := m.AddMapping(protocol, extport, intport, name, mapTimeout); err != nil { - logger.Debug("Couldn't add port mapping", "err", err) + logger1.Debug("Couldn't add port mapping", "err", err) } else { - logger.Info("Mapped network port") + logger1.Info("Mapped network port") } for { select { @@ -134,9 +134,9 @@ func Map(m Interface, c <-chan struct{}, protocol string, extport, intport int, return } case <-refresh.C: - logger.Trace("Refreshing port mapping") + logger1.Trace("Refreshing port mapping") if err := m.AddMapping(protocol, extport, intport, name, mapTimeout); err != nil { - logger.Debug("Couldn't add port mapping", "err", err) + logger1.Debug("Couldn't add port mapping", "err", err) } refresh.Reset(mapTimeout) } diff --git a/p2p/server.go b/p2p/server.go index 6fa4f8766..e03a25611 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -609,7 +609,7 @@ func (srv *Server) setupDiscovery(ctx context.Context) error { go func() { defer debug.LogPanic() defer srv.loopWG.Done() - nat.Map(srv.NAT, srv.quit, "udp", realaddr.Port, realaddr.Port, "ethereum discovery") + nat.Map(srv.NAT, srv.quit, "udp", realaddr.Port, realaddr.Port, "ethereum discovery", srv.logger) }() } } @@ -721,7 +721,7 @@ func (srv *Server) setupListening(ctx context.Context) error { go func() { defer debug.LogPanic() defer srv.loopWG.Done() - nat.Map(srv.NAT, srv.quit, "tcp", tcp.Port, tcp.Port, "ethereum p2p") + nat.Map(srv.NAT, srv.quit, "tcp", tcp.Port, tcp.Port, "ethereum p2p", srv.logger) }() } }