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-11-24 11:49:18 +03:00
|
|
|
"google.golang.org/grpc/codes"
|
2016-01-05 19:53:42 +03:00
|
|
|
)
|
|
|
|
|
2016-10-23 23:39:54 +03:00
|
|
|
// ErrorCode represents the short error code for each of the defined errors
|
|
|
|
// within the Lightning Network protocol spec.
|
|
|
|
type ErrorCode uint16
|
|
|
|
|
2016-11-24 11:49:18 +03:00
|
|
|
// ToGrpcCode is used to generate gRPC specific code which will be propagated
|
|
|
|
// to the ln rpc client. This code is used to have more detailed view of what
|
|
|
|
// goes wrong and also in order to have the ability pragmatically determine
|
|
|
|
// the error and take specific actions on the client side.
|
|
|
|
func (e ErrorCode) ToGrpcCode() codes.Code {
|
|
|
|
return (codes.Code)(e) + 100
|
|
|
|
}
|
|
|
|
|
2016-10-15 16:24:56 +03:00
|
|
|
const (
|
2017-01-25 04:12:51 +03:00
|
|
|
// ErrMaxPendingChannels is returned by remote peer when the number of
|
|
|
|
// active pending channels exceeds their maximum policy limit.
|
|
|
|
ErrMaxPendingChannels ErrorCode = 1
|
|
|
|
|
|
|
|
// ErrSynchronizingChain is returned by a remote peer that receives a
|
|
|
|
// channel update or a funding request while their still syncing to the
|
|
|
|
// latest state of the blockchain.
|
|
|
|
ErrSynchronizingChain ErrorCode = 2
|
2016-10-15 16:24:56 +03:00
|
|
|
)
|
|
|
|
|
2016-05-23 23:54:34 +03:00
|
|
|
// 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
|
2016-05-23 23:54:34 +03:00
|
|
|
// 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-10-23 23:39:54 +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.
|
2017-02-16 15:34:44 +03:00
|
|
|
ChannelPoint wire.OutPoint
|
2016-05-23 23:54:34 +03:00
|
|
|
|
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
|
2016-10-23 23:39:54 +03:00
|
|
|
|
|
|
|
// Code is the short error ID which describes the nature of the error.
|
|
|
|
Code ErrorCode
|
|
|
|
|
|
|
|
// Problem is a human-readable string further elaborating upon the
|
|
|
|
// nature of the exact error. The maximum allowed length of this
|
|
|
|
// message is 8192 bytes.
|
|
|
|
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
|
2017-02-23 21:59:50 +03:00
|
|
|
return readElements(r,
|
2016-06-21 07:55:07 +03:00
|
|
|
&c.ChannelPoint,
|
2016-10-23 23:39:54 +03:00
|
|
|
&c.Code,
|
2016-01-05 19:53:42 +03:00
|
|
|
&c.Problem,
|
2016-10-15 16:19:11 +03:00
|
|
|
&c.PendingChannelID,
|
2016-01-05 19:53:42 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-02-23 21:59:50 +03:00
|
|
|
return writeElements(w,
|
2016-06-21 07:55:07 +03:00
|
|
|
c.ChannelPoint,
|
2016-10-23 23:39:54 +03:00
|
|
|
c.Code,
|
2016-01-05 19:53:42 +03:00
|
|
|
c.Problem,
|
2016-10-15 16:19:11 +03:00
|
|
|
c.PendingChannelID,
|
2016-01-05 19:53:42 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|