github.com/decred/dcrlnd@v0.7.6/htlcswitch/linkfailure.go (about)

     1  package htlcswitch
     2  
     3  import "github.com/go-errors/errors"
     4  
     5  var (
     6  	// ErrLinkShuttingDown signals that the link is shutting down.
     7  	ErrLinkShuttingDown = errors.New("link shutting down")
     8  
     9  	// ErrLinkFailedShutdown signals that a requested shutdown failed.
    10  	ErrLinkFailedShutdown = errors.New("link failed to shutdown")
    11  )
    12  
    13  // errorCode encodes the possible types of errors that will make us fail the
    14  // current link.
    15  type errorCode uint8
    16  
    17  const (
    18  	// ErrInternalError indicates that something internal in the link
    19  	// failed. In this case we will send a generic error to our peer.
    20  	ErrInternalError errorCode = iota
    21  
    22  	// ErrRemoteError indicates that our peer sent an error, prompting up
    23  	// to fail the link.
    24  	ErrRemoteError
    25  
    26  	// ErrRemoteUnresponsive indicates that our peer took too long to
    27  	// complete a commitment dance.
    28  	ErrRemoteUnresponsive
    29  
    30  	// ErrSyncError indicates that we failed synchronizing the state of the
    31  	// channel with our peer.
    32  	ErrSyncError
    33  
    34  	// ErrInvalidUpdate indicates that the peer send us an invalid update.
    35  	ErrInvalidUpdate
    36  
    37  	// ErrInvalidCommitment indicates that the remote peer sent us an
    38  	// invalid commitment signature.
    39  	ErrInvalidCommitment
    40  
    41  	// ErrInvalidRevocation indicates that the remote peer send us an
    42  	// invalid revocation message.
    43  	ErrInvalidRevocation
    44  
    45  	// ErrRecoveryError the channel was unable to be resumed, we need the
    46  	// remote party to force close the channel out on chain now as a
    47  	// result.
    48  	ErrRecoveryError
    49  )
    50  
    51  // LinkFailureError encapsulates an error that will make us fail the current
    52  // link. It contains the necessary information needed to determine if we should
    53  // force close the channel in the process, and if any error data should be sent
    54  // to the peer.
    55  type LinkFailureError struct {
    56  	// code is the type of error this LinkFailureError encapsulates.
    57  	code errorCode
    58  
    59  	// ForceClose indicates whether we should force close the channel
    60  	// because of this error.
    61  	ForceClose bool
    62  
    63  	// PermanentFailure indicates whether this failure is permanent, and
    64  	// the channel should not be attempted loaded again.
    65  	PermanentFailure bool
    66  
    67  	// SendData is a byte slice that will be sent to the peer. If nil a
    68  	// generic error will be sent.
    69  	SendData []byte
    70  }
    71  
    72  // A compile time check to ensure LinkFailureError implements the error
    73  // interface.
    74  var _ error = (*LinkFailureError)(nil)
    75  
    76  // Error returns a generic error for the LinkFailureError.
    77  //
    78  // NOTE: Part of the error interface.
    79  func (e LinkFailureError) Error() string {
    80  	switch e.code {
    81  	case ErrInternalError:
    82  		return "internal error"
    83  	case ErrRemoteError:
    84  		return "remote error"
    85  	case ErrRemoteUnresponsive:
    86  		return "remote unresponsive"
    87  	case ErrSyncError:
    88  		return "sync error"
    89  	case ErrInvalidUpdate:
    90  		return "invalid update"
    91  	case ErrInvalidCommitment:
    92  		return "invalid commitment"
    93  	case ErrInvalidRevocation:
    94  		return "invalid revocation"
    95  	case ErrRecoveryError:
    96  		return "unable to resume channel, recovery required"
    97  	default:
    98  		return "unknown error"
    99  	}
   100  }
   101  
   102  // ShouldSendToPeer indicates whether we should send an error to the peer if
   103  // the link fails with this LinkFailureError.
   104  func (e LinkFailureError) ShouldSendToPeer() bool {
   105  	switch e.code {
   106  
   107  	// Since sending an error can lead some nodes to force close the
   108  	// channel, create a whitelist of the failures we want to send so that
   109  	// newly added error codes aren't automatically sent to the remote peer.
   110  	case
   111  		ErrInternalError,
   112  		ErrRemoteError,
   113  		ErrSyncError,
   114  		ErrInvalidUpdate,
   115  		ErrInvalidCommitment,
   116  		ErrInvalidRevocation,
   117  		ErrRecoveryError:
   118  
   119  		return true
   120  
   121  	// In all other cases we will not attempt to send our peer an error.
   122  	default:
   123  		return false
   124  	}
   125  }