Skip to content

p2p: fix dial metrics not picking up the right error #31621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions p2p/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,21 @@ func markDialError(err error) {
if !metrics.Enabled() {
return
}
if err2 := errors.Unwrap(err); err2 != nil {
Copy link
Contributor

@fjl fjl Apr 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what we really want here is

var reason DiscReason
if errors.As(err, &reason) {
    switch reason {
    ...
    }
}
var phe *protoHandshakeError
if errors.As(err, &phe) {
    dialProtoHandshakeError.Mark(1)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable, and also cleaner. I will verify how it works, and come back with a patch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dialProtoHandshakeError would have to be in an else branch, and we would still need to handle dialEncHandshakeError separately.
I would say the original with Is and As is better. I've added a catch-all and some comments to make it even cleaner.

err = err2
}
switch err {
case DiscTooManyPeers:

switch {
case errors.Is(err, DiscTooManyPeers):
dialTooManyPeers.Mark(1)
case DiscAlreadyConnected:
case errors.Is(err, DiscAlreadyConnected):
dialAlreadyConnected.Mark(1)
case DiscSelf:
case errors.Is(err, DiscSelf):
dialSelf.Mark(1)
case DiscUselessPeer:
case errors.Is(err, DiscUselessPeer):
dialUselessPeer.Mark(1)
case DiscUnexpectedIdentity:
case errors.Is(err, DiscUnexpectedIdentity):
dialUnexpectedIdentity.Mark(1)
case errEncHandshakeError:
case errors.Is(err, errEncHandshakeError):
dialEncHandshakeError.Mark(1)
case errProtoHandshakeError:
case errors.Is(err, errProtoHandshakeError):
dialProtoHandshakeError.Mark(1)
}
}
Expand Down
3 changes: 2 additions & 1 deletion p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,8 @@ func (srv *Server) setupConn(c *conn, dialDest *enode.Node) error {
phs, err := c.doProtoHandshake(srv.ourHandshake)
if err != nil {
clog.Trace("Failed p2p handshake", "err", err)
return fmt.Errorf("%w: %v", errProtoHandshakeError, err)
//Wrapping both errors for later inspection
return fmt.Errorf("%w: %w", errProtoHandshakeError, err)
}
if id := c.node.ID(); !bytes.Equal(crypto.Keccak256(phs.ID), id[:]) {
clog.Trace("Wrong devp2p handshake identity", "phsid", hex.EncodeToString(phs.ID))
Expand Down