Only log once if we failed to convert from netAddr (#264)

This commit is contained in:
Marco Munizaga 2022-06-24 07:46:19 -07:00 committed by GitHub
parent 0c14794e0d
commit b430f5b3a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 7 deletions

View File

@ -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) { func LogMisbehavingPeerNetAddr(p peer.ID, peerAddr net.Addr, component string, originalErr error, msg string) {
ma, err := manet.FromNetAddr(peerAddr) ma, err := manet.FromNetAddr(peerAddr)
if err != nil { 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) log.Errorf("CANONICAL_MISBEHAVING_PEER: peer=%s addr=%s component=%s err=%v msg=%s", p, ma, component, originalErr, msg)

View File

@ -2,6 +2,7 @@ package canonicallog
import ( import (
"fmt" "fmt"
"net"
"testing" "testing"
"github.com/libp2p/go-libp2p-core/test" "github.com/libp2p/go-libp2p-core/test"
@ -10,10 +11,7 @@ import (
func TestLogs(t *testing.T) { func TestLogs(t *testing.T) {
LogMisbehavingPeer(test.RandPeerIDFatal(t), multiaddr.StringCast("/ip4/1.2.3.4"), "somecomponent", fmt.Errorf("something"), "hi") 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" }