error.go 3.4 KB

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