github.com/decred/dcrlnd@v0.7.6/channeldb/migration/lnwire21/error.go (about)

     1  package lnwire
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  )
     7  
     8  // FundingError represents a set of errors that can be encountered and sent
     9  // during the funding workflow.
    10  type FundingError uint8
    11  
    12  const (
    13  	// ErrMaxPendingChannels is returned by remote peer when the number of
    14  	// active pending channels exceeds their maximum policy limit.
    15  	ErrMaxPendingChannels FundingError = 1
    16  
    17  	// ErrSynchronizingChain is returned by a remote peer that receives a
    18  	// channel update or a funding request while it's still syncing to the
    19  	// latest state of the blockchain.
    20  	ErrSynchronizingChain FundingError = 2
    21  
    22  	// ErrChanTooLarge is returned by a remote peer that receives a
    23  	// FundingOpen request for a channel that is above their current
    24  	// soft-limit.
    25  	ErrChanTooLarge FundingError = 3
    26  )
    27  
    28  // String returns a human readable version of the target FundingError.
    29  func (e FundingError) String() string {
    30  	switch e {
    31  	case ErrMaxPendingChannels:
    32  		return "Number of pending channels exceed maximum"
    33  	case ErrSynchronizingChain:
    34  		return "Synchronizing blockchain"
    35  	case ErrChanTooLarge:
    36  		return "channel too large"
    37  	default:
    38  		return "unknown error"
    39  	}
    40  }
    41  
    42  // Error returns the human readable version of the target FundingError.
    43  //
    44  // NOTE: Satisfies the Error interface.
    45  func (e FundingError) Error() string {
    46  	return e.String()
    47  }
    48  
    49  // ErrorData is a set of bytes associated with a particular sent error. A
    50  // receiving node SHOULD only print out data verbatim if the string is composed
    51  // solely of printable ASCII characters. For reference, the printable character
    52  // set includes byte values 32 through 127 inclusive.
    53  type ErrorData []byte
    54  
    55  // Error represents a generic error bound to an exact channel. The message
    56  // format is purposefully general in order to allow expression of a wide array
    57  // of possible errors. Each Error message is directed at a particular open
    58  // channel referenced by ChannelPoint.
    59  type Error struct {
    60  	// ChanID references the active channel in which the error occurred
    61  	// within. If the ChanID is all zeros, then this error applies to the
    62  	// entire established connection.
    63  	ChanID ChannelID
    64  
    65  	// Data is the attached error data that describes the exact failure
    66  	// which caused the error message to be sent.
    67  	Data ErrorData
    68  }
    69  
    70  // NewError creates a new Error message.
    71  func NewError() *Error {
    72  	return &Error{}
    73  }
    74  
    75  // A compile time check to ensure Error implements the lnwire.Message
    76  // interface.
    77  var _ Message = (*Error)(nil)
    78  
    79  // Error returns the string representation to Error.
    80  //
    81  // NOTE: Satisfies the error interface.
    82  func (c *Error) Error() string {
    83  	errMsg := "non-ascii data"
    84  	if isASCII(c.Data) {
    85  		errMsg = string(c.Data)
    86  	}
    87  
    88  	return fmt.Sprintf("chan_id=%v, err=%v", c.ChanID, errMsg)
    89  }
    90  
    91  // Decode deserializes a serialized Error message stored in the passed
    92  // io.Reader observing the specified protocol version.
    93  //
    94  // This is part of the lnwire.Message interface.
    95  func (c *Error) Decode(r io.Reader, pver uint32) error {
    96  	return ReadElements(r,
    97  		&c.ChanID,
    98  		&c.Data,
    99  	)
   100  }
   101  
   102  // Encode serializes the target Error into the passed io.Writer observing the
   103  // protocol version specified.
   104  //
   105  // This is part of the lnwire.Message interface.
   106  func (c *Error) Encode(w io.Writer, pver uint32) error {
   107  	return WriteElements(w,
   108  		c.ChanID,
   109  		c.Data,
   110  	)
   111  }
   112  
   113  // MsgType returns the integer uniquely identifying an Error message on the
   114  // wire.
   115  //
   116  // This is part of the lnwire.Message interface.
   117  func (c *Error) MsgType() MessageType {
   118  	return MsgError
   119  }
   120  
   121  // MaxPayloadLength returns the maximum allowed payload size for an Error
   122  // complete message observing the specified protocol version.
   123  //
   124  // This is part of the lnwire.Message interface.
   125  func (c *Error) MaxPayloadLength(uint32) uint32 {
   126  	// 32 + 2 + 65501
   127  	return MaxMessagePayload
   128  }
   129  
   130  // isASCII is a helper method that checks whether all bytes in `data` would be
   131  // printable ASCII characters if interpreted as a string.
   132  func isASCII(data []byte) bool {
   133  	for _, c := range data {
   134  		if c < 32 || c > 126 {
   135  			return false
   136  		}
   137  	}
   138  	return true
   139  }