github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/p2p/errors.go (about)

     1  package p2p
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  )
     7  
     8  // FilterTimeoutError indicates that a filter operation timed out.
     9  type FilterTimeoutError struct{}
    10  
    11  func (e FilterTimeoutError) Error() string {
    12  	return "filter timed out"
    13  }
    14  
    15  // RejectedError indicates that a Peer was rejected carrying additional
    16  // information as to the reason.
    17  type RejectedError 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 RejectedError) Addr() NetAddress {
    32  	return e.addr
    33  }
    34  
    35  func (e RejectedError) 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.IsZero() {
    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.IsZero() {
    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 RejectedError) IsAuthFailure() bool { return e.isAuthFailure }
    83  
    84  // IsDuplicate when Peer ID or IP are present already.
    85  func (e RejectedError) IsDuplicate() bool { return e.isDuplicate }
    86  
    87  // IsFiltered when Peer ID or IP was filtered.
    88  func (e RejectedError) IsFiltered() bool { return e.isFiltered }
    89  
    90  // IsIncompatible when Peer NodeInfo is not compatible with our own.
    91  func (e RejectedError) IsIncompatible() bool { return e.isIncompatible }
    92  
    93  // IsNodeInfoInvalid when the sent NodeInfo is not valid.
    94  func (e RejectedError) IsNodeInfoInvalid() bool { return e.isNodeInfoInvalid }
    95  
    96  // IsSelf when Peer is our own node.
    97  func (e RejectedError) IsSelf() bool { return e.isSelf }
    98  
    99  // SwitchDuplicatePeerIDError to be raised when a peer is connecting with a known
   100  // ID.
   101  type SwitchDuplicatePeerIDError struct {
   102  	ID ID
   103  }
   104  
   105  func (e SwitchDuplicatePeerIDError) Error() string {
   106  	return fmt.Sprintf("duplicate peer ID %v", e.ID)
   107  }
   108  
   109  // SwitchDuplicatePeerIPError to be raised when a peer is connecting with a known
   110  // IP.
   111  type SwitchDuplicatePeerIPError struct {
   112  	IP net.IP
   113  }
   114  
   115  func (e SwitchDuplicatePeerIPError) Error() string {
   116  	return fmt.Sprintf("duplicate peer IP %v", e.IP.String())
   117  }
   118  
   119  // SwitchConnectToSelfError to be raised when trying to connect to itself.
   120  type SwitchConnectToSelfError struct {
   121  	Addr *NetAddress
   122  }
   123  
   124  func (e SwitchConnectToSelfError) Error() string {
   125  	return fmt.Sprintf("connect to self: %v", e.Addr)
   126  }
   127  
   128  type SwitchAuthenticationFailureError struct {
   129  	Dialed *NetAddress
   130  	Got    ID
   131  }
   132  
   133  func (e SwitchAuthenticationFailureError) 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  // TransportClosedError is raised when the Transport has been closed.
   142  type TransportClosedError struct{}
   143  
   144  func (e TransportClosedError) Error() string {
   145  	return "transport has been closed"
   146  }
   147  
   148  // -------------------------------------------------------------------
   149  
   150  type NetAddressNoIDError struct {
   151  	Addr string
   152  }
   153  
   154  func (e NetAddressNoIDError) Error() string {
   155  	return fmt.Sprintf("address (%s) does not contain ID", e.Addr)
   156  }
   157  
   158  type NetAddressInvalidError struct {
   159  	Addr string
   160  	Err  error
   161  }
   162  
   163  func (e NetAddressInvalidError) Error() string {
   164  	return fmt.Sprintf("invalid address (%s): %v", e.Addr, e.Err)
   165  }
   166  
   167  type NetAddressLookupError struct {
   168  	Addr string
   169  	Err  error
   170  }
   171  
   172  func (e NetAddressLookupError) Error() string {
   173  	return fmt.Sprintf("error looking up host (%s): %v", e.Addr, e.Err)
   174  }
   175  
   176  // CurrentlyDialingOrExistingAddressError indicates that we're currently
   177  // dialing this address or it belongs to an existing peer.
   178  type CurrentlyDialingOrExistingAddressError struct {
   179  	Addr string
   180  }
   181  
   182  func (e CurrentlyDialingOrExistingAddressError) Error() string {
   183  	return fmt.Sprintf("connection with %s has been established or dialed", e.Addr)
   184  }