2017-04-17 01:23:40 +03:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
2019-09-20 11:55:21 +03:00
|
|
|
"fmt"
|
2017-04-17 01:23:40 +03:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2019-09-20 11:55:21 +03:00
|
|
|
// FundingError represents a set of errors that can be encountered and sent
|
|
|
|
// during the funding workflow.
|
|
|
|
type FundingError uint8
|
2017-04-17 01:23:40 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
// ErrMaxPendingChannels is returned by remote peer when the number of
|
|
|
|
// active pending channels exceeds their maximum policy limit.
|
2019-09-20 11:55:21 +03:00
|
|
|
ErrMaxPendingChannels FundingError = 1
|
2017-04-17 01:23:40 +03:00
|
|
|
|
|
|
|
// ErrSynchronizingChain is returned by a remote peer that receives a
|
2020-11-26 00:12:29 +03:00
|
|
|
// channel update or a funding request while it's still syncing to the
|
2017-04-17 01:23:40 +03:00
|
|
|
// latest state of the blockchain.
|
2019-09-20 11:55:21 +03:00
|
|
|
ErrSynchronizingChain FundingError = 2
|
2017-08-06 01:08:05 +03:00
|
|
|
|
2017-08-22 08:44:58 +03:00
|
|
|
// ErrChanTooLarge is returned by a remote peer that receives a
|
2017-08-06 01:08:05 +03:00
|
|
|
// FundingOpen request for a channel that is above their current
|
|
|
|
// soft-limit.
|
2019-09-20 11:55:21 +03:00
|
|
|
ErrChanTooLarge FundingError = 3
|
2017-04-17 01:23:40 +03:00
|
|
|
)
|
|
|
|
|
2019-09-20 11:55:21 +03:00
|
|
|
// String returns a human readable version of the target FundingError.
|
|
|
|
func (e FundingError) String() string {
|
2017-08-22 08:44:58 +03:00
|
|
|
switch e {
|
|
|
|
case ErrMaxPendingChannels:
|
|
|
|
return "Number of pending channels exceed maximum"
|
|
|
|
case ErrSynchronizingChain:
|
|
|
|
return "Synchronizing blockchain"
|
|
|
|
case ErrChanTooLarge:
|
|
|
|
return "channel too large"
|
|
|
|
default:
|
|
|
|
return "unknown error"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 00:12:29 +03:00
|
|
|
// Error returns the human readable version of the target FundingError.
|
2018-02-27 20:36:30 +03:00
|
|
|
//
|
2019-09-20 11:55:21 +03:00
|
|
|
// NOTE: Satisfies the Error interface.
|
|
|
|
func (e FundingError) Error() string {
|
2018-02-27 20:36:30 +03:00
|
|
|
return e.String()
|
|
|
|
}
|
|
|
|
|
2017-04-17 01:23:40 +03:00
|
|
|
// ErrorData is a set of bytes associated with a particular sent error. A
|
|
|
|
// receiving node SHOULD only print out data verbatim if the string is composed
|
|
|
|
// solely of printable ASCII characters. For reference, the printable character
|
|
|
|
// set includes byte values 32 through 127 inclusive.
|
|
|
|
type ErrorData []byte
|
|
|
|
|
|
|
|
// Error represents a generic error bound to an exact channel. The message
|
|
|
|
// format is purposefully general in order to allow expression of a wide array
|
|
|
|
// of possible errors. Each Error message is directed at a particular open
|
|
|
|
// channel referenced by ChannelPoint.
|
|
|
|
type Error struct {
|
|
|
|
// ChanID references the active channel in which the error occurred
|
2017-12-18 05:40:05 +03:00
|
|
|
// within. If the ChanID is all zeros, then this error applies to the
|
2017-04-17 01:23:40 +03:00
|
|
|
// entire established connection.
|
|
|
|
ChanID ChannelID
|
|
|
|
|
|
|
|
// Data is the attached error data that describes the exact failure
|
|
|
|
// which caused the error message to be sent.
|
|
|
|
Data ErrorData
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewError creates a new Error message.
|
|
|
|
func NewError() *Error {
|
|
|
|
return &Error{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A compile time check to ensure Error implements the lnwire.Message
|
|
|
|
// interface.
|
|
|
|
var _ Message = (*Error)(nil)
|
|
|
|
|
2019-09-20 11:55:21 +03:00
|
|
|
// Error returns the string representation to Error.
|
|
|
|
//
|
|
|
|
// NOTE: Satisfies the error interface.
|
|
|
|
func (c *Error) Error() string {
|
|
|
|
errMsg := "non-ascii data"
|
|
|
|
if isASCII(c.Data) {
|
|
|
|
errMsg = string(c.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("chan_id=%v, err=%v", c.ChanID, errMsg)
|
|
|
|
}
|
|
|
|
|
2017-04-17 01:23:40 +03:00
|
|
|
// Decode deserializes a serialized Error message stored in the passed
|
|
|
|
// io.Reader observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
|
|
|
func (c *Error) Decode(r io.Reader, pver uint32) error {
|
2018-12-10 05:27:41 +03:00
|
|
|
return ReadElements(r,
|
2017-04-17 01:23:40 +03:00
|
|
|
&c.ChanID,
|
|
|
|
&c.Data,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode serializes the target Error into the passed io.Writer observing the
|
|
|
|
// protocol version specified.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
|
|
|
func (c *Error) Encode(w io.Writer, pver uint32) error {
|
2018-12-10 05:27:41 +03:00
|
|
|
return WriteElements(w,
|
2017-04-17 01:23:40 +03:00
|
|
|
c.ChanID,
|
|
|
|
c.Data,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-04-20 01:57:43 +03:00
|
|
|
// MsgType returns the integer uniquely identifying an Error message on the
|
2017-04-17 01:23:40 +03:00
|
|
|
// wire.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-04-20 01:57:43 +03:00
|
|
|
func (c *Error) MsgType() MessageType {
|
|
|
|
return MsgError
|
2017-04-17 01:23:40 +03:00
|
|
|
}
|
|
|
|
|
2018-04-18 05:02:04 +03:00
|
|
|
// MaxPayloadLength returns the maximum allowed payload size for an Error
|
2017-04-17 01:23:40 +03:00
|
|
|
// complete message observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
|
|
|
func (c *Error) MaxPayloadLength(uint32) uint32 {
|
2020-01-28 04:25:36 +03:00
|
|
|
return MaxMsgBody
|
2017-04-17 01:23:40 +03:00
|
|
|
}
|
2019-09-20 11:55:21 +03:00
|
|
|
|
|
|
|
// isASCII is a helper method that checks whether all bytes in `data` would be
|
|
|
|
// printable ASCII characters if interpreted as a string.
|
|
|
|
func isASCII(data []byte) bool {
|
|
|
|
for _, c := range data {
|
|
|
|
if c < 32 || c > 126 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|