2017-06-29 16:40:45 +03:00
|
|
|
package htlcswitch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-10-17 04:15:46 +03:00
|
|
|
"fmt"
|
2017-06-29 16:40:45 +03:00
|
|
|
|
2019-06-11 11:24:19 +03:00
|
|
|
sphinx "github.com/lightningnetwork/lightning-onion"
|
2019-09-05 14:35:39 +03:00
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
2017-06-29 16:40:45 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
)
|
|
|
|
|
2020-01-14 16:07:41 +03:00
|
|
|
// ClearTextError is an interface which is implemented by errors that occur
|
|
|
|
// when we know the underlying wire failure message. These errors are the
|
|
|
|
// opposite to opaque errors which are onion-encrypted blobs only understandable
|
|
|
|
// to the initiating node. ClearTextErrors are used when we fail a htlc at our
|
|
|
|
// node, or one of our initiated payments failed and we can decrypt the onion
|
|
|
|
// encrypted error fully.
|
|
|
|
type ClearTextError interface {
|
|
|
|
error
|
|
|
|
|
|
|
|
// WireMessage extracts a valid wire failure message from an internal
|
|
|
|
// error which may contain additional metadata (which should not be
|
|
|
|
// exposed to the network). This value may be nil in the case where
|
|
|
|
// an unknown wire error is returned by one of our peers.
|
|
|
|
WireMessage() lnwire.FailureMessage
|
|
|
|
}
|
|
|
|
|
2020-01-14 16:07:41 +03:00
|
|
|
// LinkError is an implementation of the ClearTextError interface which
|
|
|
|
// represents failures that occur on our incoming or outgoing link.
|
|
|
|
type LinkError struct {
|
|
|
|
// msg returns the wire failure associated with the error.
|
|
|
|
// This value should *not* be nil, because we should always
|
|
|
|
// know the failure type for failures which occur at our own
|
|
|
|
// node.
|
|
|
|
msg lnwire.FailureMessage
|
2020-01-14 16:07:42 +03:00
|
|
|
|
|
|
|
// FailureDetail enriches the wire error with additional information.
|
|
|
|
FailureDetail
|
2020-01-14 16:07:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLinkError returns a LinkError with the failure message provided.
|
|
|
|
// The failure message provided should *not* be nil, because we should
|
|
|
|
// always know the failure type for failures which occur at our own node.
|
|
|
|
func NewLinkError(msg lnwire.FailureMessage) *LinkError {
|
|
|
|
return &LinkError{msg: msg}
|
|
|
|
}
|
|
|
|
|
2020-01-14 16:07:42 +03:00
|
|
|
// NewDetailedLinkError returns a link error that enriches a wire message with
|
|
|
|
// a failure detail.
|
|
|
|
func NewDetailedLinkError(msg lnwire.FailureMessage,
|
|
|
|
detail FailureDetail) *LinkError {
|
|
|
|
|
|
|
|
return &LinkError{
|
|
|
|
msg: msg,
|
|
|
|
FailureDetail: detail,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 16:07:41 +03:00
|
|
|
// WireMessage extracts a valid wire failure message from an internal
|
|
|
|
// error which may contain additional metadata (which should not be
|
|
|
|
// exposed to the network). This value should never be nil for LinkErrors,
|
|
|
|
// because we are the ones failing the htlc.
|
|
|
|
//
|
|
|
|
// Note this is part of the ClearTextError interface.
|
|
|
|
func (l *LinkError) WireMessage() lnwire.FailureMessage {
|
|
|
|
return l.msg
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns the string representation of a link error.
|
|
|
|
//
|
|
|
|
// Note this is part of the ClearTextError interface.
|
|
|
|
func (l *LinkError) Error() string {
|
2020-01-14 16:07:42 +03:00
|
|
|
// If the link error has no failure detail, return the wire message's
|
|
|
|
// error.
|
|
|
|
if l.FailureDetail == FailureDetailNone {
|
|
|
|
return l.msg.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%v: %v", l.msg.Error(), l.FailureDetail)
|
2020-01-14 16:07:41 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 05:31:44 +03:00
|
|
|
// ForwardingError wraps an lnwire.FailureMessage in a struct that also
|
|
|
|
// includes the source of the error.
|
|
|
|
type ForwardingError struct {
|
2019-06-11 11:24:19 +03:00
|
|
|
// FailureSourceIdx is the index of the node that sent the failure. With
|
|
|
|
// this information, the dispatcher of a payment can modify their set of
|
|
|
|
// candidate routes in response to the type of failure extracted. Index
|
|
|
|
// zero is the self node.
|
|
|
|
FailureSourceIdx int
|
2017-10-11 05:31:44 +03:00
|
|
|
|
2020-01-14 16:07:41 +03:00
|
|
|
// msg is the wire message associated with the error. This value may
|
|
|
|
// be nil in the case where we fail to decode failure message sent by
|
|
|
|
// a peer.
|
|
|
|
msg lnwire.FailureMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
// WireMessage extracts a valid wire failure message from an internal
|
|
|
|
// error which may contain additional metadata (which should not be
|
|
|
|
// exposed to the network). This value may be nil in the case where
|
|
|
|
// an unknown wire error is returned by one of our peers.
|
|
|
|
//
|
|
|
|
// Note this is part of the ClearTextError interface.
|
|
|
|
func (f *ForwardingError) WireMessage() lnwire.FailureMessage {
|
|
|
|
return f.msg
|
2017-10-11 05:31:44 +03:00
|
|
|
}
|
2017-10-11 05:36:52 +03:00
|
|
|
|
2017-10-17 04:15:46 +03:00
|
|
|
// Error implements the built-in error interface. We use this method to allow
|
|
|
|
// the switch or any callers to insert additional context to the error message
|
|
|
|
// returned.
|
|
|
|
func (f *ForwardingError) Error() string {
|
2019-11-11 14:26:48 +03:00
|
|
|
return fmt.Sprintf(
|
2020-01-14 16:07:42 +03:00
|
|
|
"%v@%v", f.msg, f.FailureSourceIdx,
|
2019-11-11 14:26:48 +03:00
|
|
|
)
|
2017-10-17 04:15:46 +03:00
|
|
|
}
|
|
|
|
|
2020-01-14 16:07:29 +03:00
|
|
|
// NewForwardingError creates a new payment error which wraps a wire error
|
|
|
|
// with additional metadata.
|
2020-01-14 16:07:42 +03:00
|
|
|
func NewForwardingError(failure lnwire.FailureMessage,
|
|
|
|
index int) *ForwardingError {
|
2020-01-14 16:07:29 +03:00
|
|
|
|
|
|
|
return &ForwardingError{
|
|
|
|
FailureSourceIdx: index,
|
2020-01-14 16:07:41 +03:00
|
|
|
msg: failure,
|
2020-01-14 16:07:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewUnknownForwardingError returns a forwarding error which has a nil failure
|
|
|
|
// message. This constructor should only be used in the case where we cannot
|
|
|
|
// decode the failure we have received from a peer.
|
|
|
|
func NewUnknownForwardingError(index int) *ForwardingError {
|
|
|
|
return &ForwardingError{
|
|
|
|
FailureSourceIdx: index,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
// ErrorDecrypter is an interface that is used to decrypt the onion encrypted
|
2017-07-15 06:08:29 +03:00
|
|
|
// failure reason an extra out a well formed error.
|
2017-10-11 05:36:52 +03:00
|
|
|
type ErrorDecrypter interface {
|
|
|
|
// DecryptError peels off each layer of onion encryption from the first
|
2017-07-15 06:08:29 +03:00
|
|
|
// hop, to the source of the error. A fully populated
|
2017-10-11 05:36:52 +03:00
|
|
|
// lnwire.FailureMessage is returned along with the source of the
|
|
|
|
// error.
|
|
|
|
DecryptError(lnwire.OpaqueReason) (*ForwardingError, error)
|
2017-06-29 16:40:45 +03:00
|
|
|
}
|
|
|
|
|
2017-12-11 02:37:21 +03:00
|
|
|
// UnknownEncrypterType is an error message used to signal that an unexpected
|
|
|
|
// EncrypterType was encountered during decoding.
|
2019-09-05 14:35:39 +03:00
|
|
|
type UnknownEncrypterType hop.EncrypterType
|
2017-12-11 02:37:21 +03:00
|
|
|
|
|
|
|
// Error returns a formatted error indicating the invalid EncrypterType.
|
|
|
|
func (e UnknownEncrypterType) Error() string {
|
|
|
|
return fmt.Sprintf("unknown error encrypter type: %d", e)
|
|
|
|
}
|
|
|
|
|
2019-06-19 16:04:17 +03:00
|
|
|
// OnionErrorDecrypter is the interface that provides onion level error
|
|
|
|
// decryption.
|
|
|
|
type OnionErrorDecrypter interface {
|
|
|
|
// DecryptError attempts to decrypt the passed encrypted error response.
|
|
|
|
// The onion failure is encrypted in backward manner, starting from the
|
|
|
|
// node where error have occurred. As a result, in order to decrypt the
|
|
|
|
// error we need get all shared secret and apply decryption in the
|
|
|
|
// reverse order.
|
|
|
|
DecryptError(encryptedData []byte) (*sphinx.DecryptedError, error)
|
|
|
|
}
|
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
// SphinxErrorDecrypter wraps the sphinx data SphinxErrorDecrypter and maps the
|
|
|
|
// returned errors to concrete lnwire.FailureMessage instances.
|
|
|
|
type SphinxErrorDecrypter struct {
|
2019-06-19 16:04:17 +03:00
|
|
|
OnionErrorDecrypter
|
2017-06-29 16:40:45 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
// DecryptError peels off each layer of onion encryption from the first hop, to
|
|
|
|
// the source of the error. A fully populated lnwire.FailureMessage is returned
|
|
|
|
// along with the source of the error.
|
2017-06-29 16:40:45 +03:00
|
|
|
//
|
2017-10-11 05:36:52 +03:00
|
|
|
// NOTE: Part of the ErrorDecrypter interface.
|
2019-06-19 16:04:17 +03:00
|
|
|
func (s *SphinxErrorDecrypter) DecryptError(reason lnwire.OpaqueReason) (
|
|
|
|
*ForwardingError, error) {
|
2017-07-15 06:08:29 +03:00
|
|
|
|
2019-06-11 11:24:19 +03:00
|
|
|
failure, err := s.OnionErrorDecrypter.DecryptError(reason)
|
2017-06-29 16:40:45 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-19 16:09:23 +03:00
|
|
|
// Decode the failure. If an error occurs, we leave the failure message
|
|
|
|
// field nil.
|
2019-06-11 11:24:19 +03:00
|
|
|
r := bytes.NewReader(failure.Message)
|
2017-10-11 05:36:52 +03:00
|
|
|
failureMsg, err := lnwire.DecodeFailure(r, 0)
|
|
|
|
if err != nil {
|
2020-01-14 16:07:29 +03:00
|
|
|
return NewUnknownForwardingError(failure.SenderIdx), nil
|
2017-10-11 05:36:52 +03:00
|
|
|
}
|
|
|
|
|
2020-01-14 16:07:42 +03:00
|
|
|
return NewForwardingError(failureMsg, failure.SenderIdx), nil
|
2017-06-29 16:40:45 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 05:36:52 +03:00
|
|
|
// A compile time check to ensure ErrorDecrypter implements the Deobfuscator
|
|
|
|
// interface.
|
|
|
|
var _ ErrorDecrypter = (*SphinxErrorDecrypter)(nil)
|