p2p: wrap internal error in new error type

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2025-04-14 17:56:52 +02:00
parent f5e0eadad2
commit f2020470f2
No known key found for this signature in database
GPG key ID: 0FE274EE8C95166E
2 changed files with 10 additions and 5 deletions

View file

@ -68,6 +68,7 @@ func markDialError(err error) {
return return
} }
var e *protoHandshakeError
switch { switch {
case errors.Is(err, DiscTooManyPeers): case errors.Is(err, DiscTooManyPeers):
dialTooManyPeers.Mark(1) dialTooManyPeers.Mark(1)
@ -81,7 +82,7 @@ func markDialError(err error) {
dialUnexpectedIdentity.Mark(1) dialUnexpectedIdentity.Mark(1)
case errors.Is(err, errEncHandshakeError): case errors.Is(err, errEncHandshakeError):
dialEncHandshakeError.Mark(1) dialEncHandshakeError.Mark(1)
case errors.Is(err, errProtoHandshakeError): case errors.As(err, &e):
dialProtoHandshakeError.Mark(1) dialProtoHandshakeError.Mark(1)
} }
} }

View file

@ -68,9 +68,13 @@ const (
var ( var (
errServerStopped = errors.New("server stopped") errServerStopped = errors.New("server stopped")
errEncHandshakeError = errors.New("rlpx enc error") errEncHandshakeError = errors.New("rlpx enc error")
errProtoHandshakeError = errors.New("rlpx proto error")
) )
type protoHandshakeError struct{ err error }
func (e *protoHandshakeError) Error() string { return fmt.Sprintf("rlpx proto error: %s", e.err) }
func (e *protoHandshakeError) Unwrap() error { return e.err }
// Server manages all peer connections. // Server manages all peer connections.
type Server struct { type Server struct {
// Config fields may not be modified while the server is running. // Config fields may not be modified while the server is running.
@ -908,7 +912,7 @@ func (srv *Server) setupConn(c *conn, dialDest *enode.Node) error {
if err != nil { if err != nil {
clog.Trace("Failed p2p handshake", "err", err) clog.Trace("Failed p2p handshake", "err", err)
//Wrapping both errors for later inspection //Wrapping both errors for later inspection
return fmt.Errorf("%w: %w", errProtoHandshakeError, err) return &protoHandshakeError{err: err}
} }
if id := c.node.ID(); !bytes.Equal(crypto.Keccak256(phs.ID), id[:]) { if id := c.node.ID(); !bytes.Equal(crypto.Keccak256(phs.ID), id[:]) {
clog.Trace("Wrong devp2p handshake identity", "phsid", hex.EncodeToString(phs.ID)) clog.Trace("Wrong devp2p handshake identity", "phsid", hex.EncodeToString(phs.ID))