2016-01-05 19:53:42 +03:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-06-21 07:55:07 +03:00
|
|
|
|
|
|
|
"github.com/roasbeef/btcd/wire"
|
2016-01-05 19:53:42 +03:00
|
|
|
)
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// ErrorGeneric represents a generic error bound to an exact channel. The
|
|
|
|
// message format is purposefully general in order to allow expressino of a wide
|
|
|
|
// array of possible errors. Each ErrorGeneric message is directed at a particular
|
2016-06-21 07:55:07 +03:00
|
|
|
// open channel referenced by ChannelPoint.
|
2016-01-05 19:53:42 +03:00
|
|
|
type ErrorGeneric struct {
|
2016-06-21 07:55:07 +03:00
|
|
|
// ChannelPoint references the active channel in which the error occured
|
|
|
|
// within. A ChannelPoint of zeroHash:0 denotes this error applies to
|
|
|
|
// the entire established connection.
|
|
|
|
ChannelPoint *wire.OutPoint
|
2016-05-23 23:54:34 +03:00
|
|
|
|
2016-05-31 02:49:48 +03:00
|
|
|
// ErrorID quickly defines the nature of the error according to error
|
|
|
|
// type.
|
|
|
|
ErrorID uint16
|
2016-05-23 23:54:34 +03:00
|
|
|
|
|
|
|
// Problem is a human-readable string further elaborating upon the
|
|
|
|
// nature of the exact error. The maxmium allowed length of this
|
|
|
|
// message is 8192 bytes.
|
2016-01-05 19:53:42 +03:00
|
|
|
Problem string
|
2016-05-31 02:49:48 +03:00
|
|
|
}
|
2016-05-23 23:54:34 +03:00
|
|
|
|
2016-05-31 02:49:48 +03:00
|
|
|
// NewErrorGeneric creates a new ErrorGeneric message.
|
|
|
|
func NewErrorGeneric() *ErrorGeneric {
|
|
|
|
return &ErrorGeneric{}
|
2016-01-05 19:53:42 +03:00
|
|
|
}
|
|
|
|
|
2016-05-31 02:49:48 +03:00
|
|
|
// A compile time check to ensure ErrorGeneric implements the lnwire.Message
|
|
|
|
// interface.
|
|
|
|
var _ Message = (*ErrorGeneric)(nil)
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Decode deserializes a serialized ErrorGeneric message stored in the
|
|
|
|
// passed io.Reader observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:53:42 +03:00
|
|
|
func (c *ErrorGeneric) Decode(r io.Reader, pver uint32) error {
|
2016-06-21 07:55:07 +03:00
|
|
|
// ChannelPoint(8)
|
2016-01-17 04:14:35 +03:00
|
|
|
// Problem
|
2016-01-05 19:53:42 +03:00
|
|
|
err := readElements(r,
|
2016-06-21 07:55:07 +03:00
|
|
|
&c.ChannelPoint,
|
2016-05-31 02:49:48 +03:00
|
|
|
&c.ErrorID,
|
2016-01-05 19:53:42 +03:00
|
|
|
&c.Problem,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Encode serializes the target ErrorGeneric into the passed io.Writer
|
|
|
|
// observing the protocol version specified.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:53:42 +03:00
|
|
|
func (c *ErrorGeneric) Encode(w io.Writer, pver uint32) error {
|
|
|
|
err := writeElements(w,
|
2016-06-21 07:55:07 +03:00
|
|
|
c.ChannelPoint,
|
2016-05-31 02:49:48 +03:00
|
|
|
c.ErrorID,
|
2016-01-05 19:53:42 +03:00
|
|
|
c.Problem,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Command returns the integer uniquely identifying an ErrorGeneric message on
|
|
|
|
// the wire.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:53:42 +03:00
|
|
|
func (c *ErrorGeneric) Command() uint32 {
|
|
|
|
return CmdErrorGeneric
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// MaxPayloadLength returns the maximum allowed payload size for a
|
|
|
|
// ErrorGeneric complete message observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:53:42 +03:00
|
|
|
func (c *ErrorGeneric) MaxPayloadLength(uint32) uint32 {
|
2016-01-17 04:14:35 +03:00
|
|
|
// 8+8192
|
2016-01-05 19:53:42 +03:00
|
|
|
return 8208
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// Validate performs any necessary sanity checks to ensure all fields present
|
|
|
|
// on the ErrorGeneric are valid.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:53:42 +03:00
|
|
|
func (c *ErrorGeneric) Validate() error {
|
|
|
|
if len(c.Problem) > 8192 {
|
2016-05-23 23:54:34 +03:00
|
|
|
return fmt.Errorf("problem string length too long")
|
2016-01-05 19:53:42 +03:00
|
|
|
}
|
2016-05-23 23:54:34 +03:00
|
|
|
|
2016-01-17 04:14:35 +03:00
|
|
|
// We're good!
|
2016-01-05 19:53:42 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// String returns the string representation of the target ErrorGeneric.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2016-01-05 19:53:42 +03:00
|
|
|
func (c *ErrorGeneric) String() string {
|
|
|
|
return fmt.Sprintf("\n--- Begin ErrorGeneric ---\n") +
|
2016-06-21 07:55:07 +03:00
|
|
|
fmt.Sprintf("ChannelPoint:\t%d\n", c.ChannelPoint) +
|
2016-05-31 02:49:48 +03:00
|
|
|
fmt.Sprintf("ErrorID:\t%d\n", c.ErrorID) +
|
2016-01-05 19:53:42 +03:00
|
|
|
fmt.Sprintf("Problem:\t%s\n", c.Problem) +
|
|
|
|
fmt.Sprintf("--- End ErrorGeneric ---\n")
|
|
|
|
}
|