2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2015-07-23 16:35:11 +00:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 00:54:22 +00:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 16:48:40 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 00:54:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 16:48:40 +00:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 00:54:22 +00:00
|
|
|
|
2014-10-23 15:57:54 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2023-08-31 15:45:23 +00:00
|
|
|
type PeerErrorCode uint8
|
|
|
|
|
2014-10-23 15:57:54 +00:00
|
|
|
const (
|
2023-08-31 15:45:23 +00:00
|
|
|
PeerErrorInvalidMessageCode PeerErrorCode = iota
|
|
|
|
PeerErrorInvalidMessage
|
|
|
|
PeerErrorPingFailure
|
2023-09-06 15:56:03 +00:00
|
|
|
PeerErrorPongFailure
|
2023-08-31 15:45:23 +00:00
|
|
|
PeerErrorDiscReason
|
|
|
|
PeerErrorDiscReasonRemote
|
|
|
|
PeerErrorMessageReceive
|
|
|
|
PeerErrorMessageSizeLimit
|
|
|
|
PeerErrorMessageObsolete
|
|
|
|
PeerErrorMessageSend
|
|
|
|
PeerErrorLocalStatusNeeded
|
|
|
|
PeerErrorStatusSend
|
|
|
|
PeerErrorStatusReceive
|
|
|
|
PeerErrorStatusDecode
|
|
|
|
PeerErrorStatusIncompatible
|
|
|
|
PeerErrorStatusHandshakeTimeout
|
|
|
|
PeerErrorStatusUnexpected
|
|
|
|
PeerErrorFirstMessageSend
|
|
|
|
PeerErrorTest
|
2014-10-23 15:57:54 +00:00
|
|
|
)
|
|
|
|
|
2023-08-31 15:45:23 +00:00
|
|
|
var peerErrorCodeToString = map[PeerErrorCode]string{
|
|
|
|
PeerErrorInvalidMessageCode: "invalid message code",
|
|
|
|
PeerErrorInvalidMessage: "invalid message",
|
|
|
|
PeerErrorPingFailure: "ping failure",
|
2023-09-06 15:56:03 +00:00
|
|
|
PeerErrorPongFailure: "pong failure",
|
2023-08-31 15:45:23 +00:00
|
|
|
PeerErrorDiscReason: "disconnect reason",
|
|
|
|
PeerErrorDiscReasonRemote: "remote disconnect reason",
|
|
|
|
PeerErrorMessageReceive: "failed to receive a message",
|
|
|
|
PeerErrorMessageSizeLimit: "too big message",
|
|
|
|
PeerErrorMessageObsolete: "obsolete message",
|
|
|
|
PeerErrorMessageSend: "failed to send a message",
|
|
|
|
PeerErrorLocalStatusNeeded: "need a local status message",
|
|
|
|
PeerErrorStatusSend: "failed to send the local status",
|
|
|
|
PeerErrorStatusReceive: "failed to receive the remote status",
|
|
|
|
PeerErrorStatusDecode: "failed to decode the remote status",
|
|
|
|
PeerErrorStatusIncompatible: "incompatible remote status",
|
|
|
|
PeerErrorStatusHandshakeTimeout: "handshake timeout",
|
|
|
|
PeerErrorStatusUnexpected: "unexpected remote status",
|
|
|
|
PeerErrorFirstMessageSend: "failed to send the first message",
|
|
|
|
PeerErrorTest: "test error",
|
2014-10-23 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 15:45:23 +00:00
|
|
|
func (c PeerErrorCode) String() string {
|
|
|
|
if len(peerErrorCodeToString) <= int(c) {
|
|
|
|
return fmt.Sprintf("unknown code %d", c)
|
|
|
|
}
|
|
|
|
return peerErrorCodeToString[c]
|
2014-10-23 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 15:45:23 +00:00
|
|
|
func (c PeerErrorCode) Error() string {
|
|
|
|
return c.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
type PeerError struct {
|
|
|
|
Code PeerErrorCode
|
|
|
|
Reason DiscReason
|
|
|
|
Err error
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPeerError(code PeerErrorCode, reason DiscReason, err error, message string) *PeerError {
|
|
|
|
return &PeerError{
|
|
|
|
code,
|
|
|
|
reason,
|
|
|
|
err,
|
|
|
|
message,
|
2014-11-21 20:48:49 +00:00
|
|
|
}
|
2014-10-23 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 15:45:23 +00:00
|
|
|
func (pe *PeerError) String() string {
|
|
|
|
return fmt.Sprintf("PeerError(code=%s, reason=%s, err=%v, message=%s)",
|
|
|
|
pe.Code, pe.Reason, pe.Err, pe.Message)
|
2014-10-23 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 15:45:23 +00:00
|
|
|
func (pe *PeerError) Error() string {
|
|
|
|
return pe.String()
|
|
|
|
}
|
2017-02-24 08:58:04 +00:00
|
|
|
|
2022-05-06 15:19:53 +00:00
|
|
|
type DiscReason uint8
|
2014-11-21 20:48:49 +00:00
|
|
|
|
|
|
|
const (
|
2015-02-05 02:07:58 +00:00
|
|
|
DiscRequested DiscReason = iota
|
|
|
|
DiscNetworkError
|
|
|
|
DiscProtocolError
|
|
|
|
DiscUselessPeer
|
|
|
|
DiscTooManyPeers
|
|
|
|
DiscAlreadyConnected
|
|
|
|
DiscIncompatibleVersion
|
|
|
|
DiscInvalidIdentity
|
|
|
|
DiscQuitting
|
|
|
|
DiscUnexpectedIdentity
|
|
|
|
DiscSelf
|
|
|
|
DiscReadTimeout
|
2015-08-12 12:15:54 +00:00
|
|
|
DiscSubprotocolError = 0x10
|
2014-11-21 20:48:49 +00:00
|
|
|
)
|
|
|
|
|
2015-02-05 02:07:58 +00:00
|
|
|
var discReasonToString = [...]string{
|
2017-02-24 08:58:04 +00:00
|
|
|
DiscRequested: "disconnect requested",
|
|
|
|
DiscNetworkError: "network error",
|
|
|
|
DiscProtocolError: "breach of protocol",
|
|
|
|
DiscUselessPeer: "useless peer",
|
|
|
|
DiscTooManyPeers: "too many peers",
|
|
|
|
DiscAlreadyConnected: "already connected",
|
|
|
|
DiscIncompatibleVersion: "incompatible p2p protocol version",
|
|
|
|
DiscInvalidIdentity: "invalid node identity",
|
|
|
|
DiscQuitting: "client quitting",
|
|
|
|
DiscUnexpectedIdentity: "unexpected identity",
|
|
|
|
DiscSelf: "connected to self",
|
|
|
|
DiscReadTimeout: "read timeout",
|
|
|
|
DiscSubprotocolError: "subprotocol error",
|
2014-11-21 20:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d DiscReason) String() string {
|
2021-07-09 09:54:06 +00:00
|
|
|
if len(discReasonToString) <= int(d) {
|
2017-02-24 08:58:04 +00:00
|
|
|
return fmt.Sprintf("unknown disconnect reason %d", d)
|
2014-11-21 20:48:49 +00:00
|
|
|
}
|
|
|
|
return discReasonToString[d]
|
|
|
|
}
|
|
|
|
|
2015-04-08 15:37:11 +00:00
|
|
|
func (d DiscReason) Error() string {
|
|
|
|
return d.String()
|
2014-12-12 10:58:39 +00:00
|
|
|
}
|