github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/p2p/peer_error.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package p2p 18 19 import ( 20 "errors" 21 "fmt" 22 ) 23 24 const ( 25 errInvalidMsgCode = iota 26 errInvalidMsg 27 ) 28 29 // Quorum 30 // 31 // Constants for peer connection errors 32 const ( 33 // When permissioning is enabled, and node is not permissioned in the network 34 errPermissionDenied = iota + 100 35 // Unauthorized node joining existing raft cluster 36 errNotInRaftCluster 37 ) 38 39 var errorToString = map[int]string{ 40 errInvalidMsgCode: "invalid message code", 41 errInvalidMsg: "invalid message", 42 // Quorum 43 errPermissionDenied: "permission denied", 44 errNotInRaftCluster: "not in raft cluster", 45 } 46 47 type peerError struct { 48 code int 49 message string 50 } 51 52 func newPeerError(code int, format string, v ...interface{}) *peerError { 53 desc, ok := errorToString[code] 54 if !ok { 55 panic("invalid error code") 56 } 57 err := &peerError{code, desc} 58 if format != "" { 59 err.message += ": " + fmt.Sprintf(format, v...) 60 } 61 return err 62 } 63 64 func (pe *peerError) Error() string { 65 return pe.message 66 } 67 68 var errProtocolReturned = errors.New("protocol returned") 69 70 type DiscReason uint 71 72 const ( 73 DiscRequested DiscReason = iota 74 DiscNetworkError 75 DiscProtocolError 76 DiscUselessPeer 77 DiscTooManyPeers 78 DiscAlreadyConnected 79 DiscIncompatibleVersion 80 DiscInvalidIdentity 81 DiscQuitting 82 DiscUnexpectedIdentity 83 DiscSelf 84 DiscReadTimeout 85 DiscSubprotocolError = 0x10 86 ) 87 88 var discReasonToString = [...]string{ 89 DiscRequested: "disconnect requested", 90 DiscNetworkError: "network error", 91 DiscProtocolError: "breach of protocol", 92 DiscUselessPeer: "useless peer", 93 DiscTooManyPeers: "too many peers", 94 DiscAlreadyConnected: "already connected", 95 DiscIncompatibleVersion: "incompatible p2p protocol version", 96 DiscInvalidIdentity: "invalid node identity", 97 DiscQuitting: "client quitting", 98 DiscUnexpectedIdentity: "unexpected identity", 99 DiscSelf: "connected to self", 100 DiscReadTimeout: "read timeout", 101 DiscSubprotocolError: "subprotocol error", 102 } 103 104 func (d DiscReason) String() string { 105 if len(discReasonToString) < int(d) { 106 return fmt.Sprintf("unknown disconnect reason %d", d) 107 } 108 return discReasonToString[d] 109 } 110 111 func (d DiscReason) Error() string { 112 return d.String() 113 } 114 115 func discReasonForError(err error) DiscReason { 116 if reason, ok := err.(DiscReason); ok { 117 return reason 118 } 119 if err == errProtocolReturned { 120 return DiscQuitting 121 } 122 peerError, ok := err.(*peerError) 123 if ok { 124 switch peerError.code { 125 case errInvalidMsgCode, errInvalidMsg: 126 return DiscProtocolError 127 default: 128 return DiscSubprotocolError 129 } 130 } 131 return DiscSubprotocolError 132 }