github.com/pure-x-eth/consensus_tm@v0.0.0-20230502163723-e3c2ff987250/p2p/errors.go (about)

     1  package p2p
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  )
     7  
     8  // ErrFilterTimeout indicates that a filter operation timed out.
     9  type ErrFilterTimeout struct{}
    10  
    11  func (e ErrFilterTimeout) Error() string {
    12  	return "filter timed out"
    13  }
    14  
    15  // ErrRejected indicates that a Peer was rejected carrying additional
    16  // information as to the reason.
    17  type ErrRejected struct {
    18  	addr              NetAddress
    19  	conn              net.Conn
    20  	err               error
    21  	id                ID
    22  	isAuthFailure     bool
    23  	isDuplicate       bool
    24  	isFiltered        bool
    25  	isIncompatible    bool
    26  	isNodeInfoInvalid bool
    27  	isSelf            bool
    28  }
    29  
    30  // Addr returns the NetAddress for the rejected Peer.
    31  func (e ErrRejected) Addr() NetAddress {
    32  	return e.addr
    33  }
    34  
    35  func (e ErrRejected) Error() string {
    36  	if e.isAuthFailure {
    37  		return fmt.Sprintf("auth failure: %s", e.err)
    38  	}
    39  
    40  	if e.isDuplicate {
    41  		if e.conn != nil {
    42  			return fmt.Sprintf(
    43  				"duplicate CONN<%s>",
    44  				e.conn.RemoteAddr().String(),
    45  			)
    46  		}
    47  		if e.id != "" {
    48  			return fmt.Sprintf("duplicate ID<%v>", e.id)
    49  		}
    50  	}
    51  
    52  	if e.isFiltered {
    53  		if e.conn != nil {
    54  			return fmt.Sprintf(
    55  				"filtered CONN<%s>: %s",
    56  				e.conn.RemoteAddr().String(),
    57  				e.err,
    58  			)
    59  		}
    60  
    61  		if e.id != "" {
    62  			return fmt.Sprintf("filtered ID<%v>: %s", e.id, e.err)
    63  		}
    64  	}
    65  
    66  	if e.isIncompatible {
    67  		return fmt.Sprintf("incompatible: %s", e.err)
    68  	}
    69  
    70  	if e.isNodeInfoInvalid {
    71  		return fmt.Sprintf("invalid NodeInfo: %s", e.err)
    72  	}
    73  
    74  	if e.isSelf {
    75  		return fmt.Sprintf("self ID<%v>", e.id)
    76  	}
    77  
    78  	return fmt.Sprintf("%s", e.err)
    79  }
    80  
    81  // IsAuthFailure when Peer authentication was unsuccessful.
    82  func (e ErrRejected) IsAuthFailure() bool { return e.isAuthFailure }
    83  
    84  // IsDuplicate when Peer ID or IP are present already.
    85  func (e ErrRejected) IsDuplicate() bool { return e.isDuplicate }
    86  
    87  // IsFiltered when Peer ID or IP was filtered.
    88  func (e ErrRejected) IsFiltered() bool { return e.isFiltered }
    89  
    90  // IsIncompatible when Peer NodeInfo is not compatible with our own.
    91  func (e ErrRejected) IsIncompatible() bool { return e.isIncompatible }
    92  
    93  // IsNodeInfoInvalid when the sent NodeInfo is not valid.
    94  func (e ErrRejected) IsNodeInfoInvalid() bool { return e.isNodeInfoInvalid }
    95  
    96  // IsSelf when Peer is our own node.
    97  func (e ErrRejected) IsSelf() bool { return e.isSelf }
    98  
    99  // ErrSwitchDuplicatePeerID to be raised when a peer is connecting with a known
   100  // ID.
   101  type ErrSwitchDuplicatePeerID struct {
   102  	ID ID
   103  }
   104  
   105  func (e ErrSwitchDuplicatePeerID) Error() string {
   106  	return fmt.Sprintf("duplicate peer ID %v", e.ID)
   107  }
   108  
   109  // ErrSwitchDuplicatePeerIP to be raised whena a peer is connecting with a known
   110  // IP.
   111  type ErrSwitchDuplicatePeerIP struct {
   112  	IP net.IP
   113  }
   114  
   115  func (e ErrSwitchDuplicatePeerIP) Error() string {
   116  	return fmt.Sprintf("duplicate peer IP %v", e.IP.String())
   117  }
   118  
   119  // ErrSwitchConnectToSelf to be raised when trying to connect to itself.
   120  type ErrSwitchConnectToSelf struct {
   121  	Addr *NetAddress
   122  }
   123  
   124  func (e ErrSwitchConnectToSelf) Error() string {
   125  	return fmt.Sprintf("connect to self: %v", e.Addr)
   126  }
   127  
   128  type ErrSwitchAuthenticationFailure struct {
   129  	Dialed *NetAddress
   130  	Got    ID
   131  }
   132  
   133  func (e ErrSwitchAuthenticationFailure) Error() string {
   134  	return fmt.Sprintf(
   135  		"failed to authenticate peer. Dialed %v, but got peer with ID %s",
   136  		e.Dialed,
   137  		e.Got,
   138  	)
   139  }
   140  
   141  // ErrTransportClosed is raised when the Transport has been closed.
   142  type ErrTransportClosed struct{}
   143  
   144  func (e ErrTransportClosed) Error() string {
   145  	return "transport has been closed"
   146  }
   147  
   148  // ErrPeerRemoval is raised when attempting to remove a peer results in an error.
   149  type ErrPeerRemoval struct{}
   150  
   151  func (e ErrPeerRemoval) Error() string {
   152  	return "peer removal failed"
   153  }
   154  
   155  //-------------------------------------------------------------------
   156  
   157  type ErrNetAddressNoID struct {
   158  	Addr string
   159  }
   160  
   161  func (e ErrNetAddressNoID) Error() string {
   162  	return fmt.Sprintf("address (%s) does not contain ID", e.Addr)
   163  }
   164  
   165  type ErrNetAddressInvalid struct {
   166  	Addr string
   167  	Err  error
   168  }
   169  
   170  func (e ErrNetAddressInvalid) Error() string {
   171  	return fmt.Sprintf("invalid address (%s): %v", e.Addr, e.Err)
   172  }
   173  
   174  type ErrNetAddressLookup struct {
   175  	Addr string
   176  	Err  error
   177  }
   178  
   179  func (e ErrNetAddressLookup) Error() string {
   180  	return fmt.Sprintf("error looking up host (%s): %v", e.Addr, e.Err)
   181  }
   182  
   183  // ErrCurrentlyDialingOrExistingAddress indicates that we're currently
   184  // dialing this address or it belongs to an existing peer.
   185  type ErrCurrentlyDialingOrExistingAddress struct {
   186  	Addr string
   187  }
   188  
   189  func (e ErrCurrentlyDialingOrExistingAddress) Error() string {
   190  	return fmt.Sprintf("connection with %s has been established or dialed", e.Addr)
   191  }