From b430f5b3a6a25b41b3138ee0d520e847c477ba37 Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Fri, 24 Jun 2022 07:46:19 -0700 Subject: [PATCH] Only log once if we failed to convert from netAddr (#264) --- canonicallog/canonicallog.go | 3 ++- canonicallog/canonicallog_test.go | 10 ++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/canonicallog/canonicallog.go b/canonicallog/canonicallog.go index 40de00e..61be75c 100644 --- a/canonicallog/canonicallog.go +++ b/canonicallog/canonicallog.go @@ -24,7 +24,8 @@ func LogMisbehavingPeer(p peer.ID, peerAddr multiaddr.Multiaddr, component strin func LogMisbehavingPeerNetAddr(p peer.ID, peerAddr net.Addr, component string, originalErr error, msg string) { ma, err := manet.FromNetAddr(peerAddr) if err != nil { - log.Errorf("CANONICAL_MISBEHAVING_PEER: peer=%s netAddr=%s component=%s err=%v msg=%s", p, peerAddr.String(), component, originalErr, msg) + log.Errorf("CANONICAL_MISBEHAVING_PEER: peer=%s netAddr=%s component=%s err=%v fromNetAddrErr=%v msg=%s", p, peerAddr.String(), component, originalErr, err, msg) + return } log.Errorf("CANONICAL_MISBEHAVING_PEER: peer=%s addr=%s component=%s err=%v msg=%s", p, ma, component, originalErr, msg) diff --git a/canonicallog/canonicallog_test.go b/canonicallog/canonicallog_test.go index 9332ee4..3b5e8e7 100644 --- a/canonicallog/canonicallog_test.go +++ b/canonicallog/canonicallog_test.go @@ -2,6 +2,7 @@ package canonicallog import ( "fmt" + "net" "testing" "github.com/libp2p/go-libp2p-core/test" @@ -10,10 +11,7 @@ import ( func TestLogs(t *testing.T) { LogMisbehavingPeer(test.RandPeerIDFatal(t), multiaddr.StringCast("/ip4/1.2.3.4"), "somecomponent", fmt.Errorf("something"), "hi") - LogMisbehavingPeerNetAddr(test.RandPeerIDFatal(t), dummyNetAddr{}, "somecomponent", fmt.Errorf("something"), "hi") + + netAddr := &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 80} + LogMisbehavingPeerNetAddr(test.RandPeerIDFatal(t), netAddr, "somecomponent", fmt.Errorf("something"), "hi") } - -type dummyNetAddr struct{} - -func (d dummyNetAddr) Network() string { return "tcp" } -func (d dummyNetAddr) String() string { return "127.0.0.1:80" }