lnd.xprv/lnwire/error_generic.go

125 lines
3.5 KiB
Go
Raw Normal View History

package lnwire
import (
"fmt"
"io"
"github.com/roasbeef/btcd/wire"
)
// ErrorGeneric represents a generic error bound to an exact channel. The
2016-10-15 16:18:38 +03:00
// message format is purposefully general in order to allow expression of a wide
// array of possible errors. Each ErrorGeneric message is directed at a particular
// open channel referenced by ChannelPoint.
type ErrorGeneric struct {
2016-10-15 16:18:38 +03:00
// ChannelPoint references the active channel in which the error occurred
// within. A ChannelPoint of zeroHash:0 denotes this error applies to
// the entire established connection.
ChannelPoint *wire.OutPoint
// ErrorID quickly defines the nature of the error according to error
// type.
ErrorID uint16
// Problem is a human-readable string further elaborating upon the
2016-10-15 16:18:38 +03:00
// nature of the exact error. The maximum allowed length of this
// message is 8192 bytes.
Problem string
2016-10-15 16:19:11 +03:00
// PendingChannelID allows peers communicate errors in the context of a
// particular pending channel. With this field, once a peer reads an
// ErrorGeneric message with the PendingChannelID field set, then they
// can forward the error to the fundingManager who can handle it
// properly.
PendingChannelID uint64
}
// NewErrorGeneric creates a new ErrorGeneric message.
func NewErrorGeneric() *ErrorGeneric {
return &ErrorGeneric{}
}
// A compile time check to ensure ErrorGeneric implements the lnwire.Message
// interface.
var _ Message = (*ErrorGeneric)(nil)
// 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.
func (c *ErrorGeneric) Decode(r io.Reader, pver uint32) error {
// ChannelPoint(8)
// Problem
err := readElements(r,
&c.ChannelPoint,
&c.ErrorID,
&c.Problem,
2016-10-15 16:19:11 +03:00
&c.PendingChannelID,
)
if err != nil {
return err
}
return nil
}
// Encode serializes the target ErrorGeneric into the passed io.Writer
// observing the protocol version specified.
//
// This is part of the lnwire.Message interface.
func (c *ErrorGeneric) Encode(w io.Writer, pver uint32) error {
err := writeElements(w,
c.ChannelPoint,
c.ErrorID,
c.Problem,
2016-10-15 16:19:11 +03:00
c.PendingChannelID,
)
if err != nil {
return err
}
return nil
}
// Command returns the integer uniquely identifying an ErrorGeneric message on
// the wire.
//
// This is part of the lnwire.Message interface.
func (c *ErrorGeneric) Command() uint32 {
return CmdErrorGeneric
}
// 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.
func (c *ErrorGeneric) MaxPayloadLength(uint32) uint32 {
// 8+8192
return 8208
}
// Validate performs any necessary sanity checks to ensure all fields present
// on the ErrorGeneric are valid.
//
// This is part of the lnwire.Message interface.
func (c *ErrorGeneric) Validate() error {
if len(c.Problem) > 8192 {
return fmt.Errorf("problem string length too long")
}
// We're good!
return nil
}
// String returns the string representation of the target ErrorGeneric.
//
// This is part of the lnwire.Message interface.
func (c *ErrorGeneric) String() string {
return fmt.Sprintf("\n--- Begin ErrorGeneric ---\n") +
fmt.Sprintf("ChannelPoint:\t%d\n", c.ChannelPoint) +
fmt.Sprintf("ErrorID:\t%d\n", c.ErrorID) +
fmt.Sprintf("Problem:\t%s\n", c.Problem) +
2016-10-15 16:19:11 +03:00
fmt.Sprintf("PendingChannelID:\t%s\n", c.PendingChannelID) +
fmt.Sprintf("--- End ErrorGeneric ---\n")
}