2016-09-23 04:51:43 +03:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
2017-02-16 15:25:36 +03:00
|
|
|
"fmt"
|
2016-09-23 04:51:43 +03:00
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/roasbeef/btcd/wire"
|
|
|
|
)
|
|
|
|
|
2017-02-16 15:25:36 +03:00
|
|
|
// FailReason specifies the precise reason that an upstream HTLC was cancelled.
|
|
|
|
// Each UpdateFailHTLC message carries a FailCode which is to be passed back
|
|
|
|
// unaltered to the source of the HTLC within the route.
|
2017-01-06 08:43:25 +03:00
|
|
|
//
|
|
|
|
// TODO(roasbeef): implement proper encrypted error messages as defined in spec
|
|
|
|
// * these errors as it stands reveal the error cause to all links in the
|
|
|
|
// route and are horrible for privacy
|
2017-02-16 15:25:36 +03:00
|
|
|
type FailCode uint16
|
2017-01-06 08:43:25 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
// InsufficientCapacity indicates that a payment failed due to a link
|
|
|
|
// in the ultimate route not having enough satoshi flow to successfully
|
|
|
|
// carry the payment.
|
2017-02-16 15:25:36 +03:00
|
|
|
InsufficientCapacity FailCode = 0
|
2017-01-06 08:43:25 +03:00
|
|
|
|
|
|
|
// UpstreamTimeout indicates that an upstream link had to enforce the
|
|
|
|
// absolute HTLC timeout, removing the HTLC.
|
2017-02-16 15:25:36 +03:00
|
|
|
UpstreamTimeout FailCode = 1
|
2017-01-06 08:43:25 +03:00
|
|
|
|
2017-01-08 06:24:40 +03:00
|
|
|
// UnknownPaymentHash indicates that the destination did not recognize
|
2017-01-06 08:43:25 +03:00
|
|
|
// the payment hash.
|
2017-02-16 15:25:36 +03:00
|
|
|
UnknownPaymentHash FailCode = 2
|
2017-01-06 08:43:25 +03:00
|
|
|
|
2017-01-08 06:24:40 +03:00
|
|
|
// UnknownDestination indicates that the specified next hop within the
|
2017-01-06 08:43:25 +03:00
|
|
|
// Sphinx packet at a point in the route contained an unknown or
|
|
|
|
// invalid "next hop".
|
2017-02-16 15:25:36 +03:00
|
|
|
UnknownDestination FailCode = 3
|
2017-01-06 08:43:25 +03:00
|
|
|
|
|
|
|
// SphinxParseError indicates that an intermediate node was unable
|
|
|
|
// properly parse the HTLC.
|
2017-02-16 15:25:36 +03:00
|
|
|
SphinxParseError FailCode = 4
|
2017-01-08 06:24:40 +03:00
|
|
|
|
|
|
|
// IncorrectValue indicates that the HTLC ultimately extended to the
|
|
|
|
// destination did not match the value that was expected.
|
2017-02-16 15:25:36 +03:00
|
|
|
IncorrectValue FailCode = 5
|
2017-01-06 08:43:25 +03:00
|
|
|
)
|
|
|
|
|
2017-02-16 15:25:36 +03:00
|
|
|
// String returns a human-readable version of the FailCode type.
|
|
|
|
func (c FailCode) String() string {
|
2017-01-06 08:43:25 +03:00
|
|
|
switch c {
|
|
|
|
case InsufficientCapacity:
|
|
|
|
return "InsufficientCapacity: next hop had insufficient " +
|
|
|
|
"capacity for payment"
|
|
|
|
|
|
|
|
case UpstreamTimeout:
|
|
|
|
return "UpstreamTimeout: HTLC has timed out upstream"
|
|
|
|
|
2017-01-08 06:24:40 +03:00
|
|
|
case UnknownPaymentHash:
|
|
|
|
return "UnknownPaymentHash: the destination did not know the " +
|
2017-01-06 08:43:25 +03:00
|
|
|
"preimage"
|
|
|
|
|
2017-01-08 06:24:40 +03:00
|
|
|
case UnknownDestination:
|
|
|
|
return "UnknownDestination: next hop unknown"
|
2017-01-06 08:43:25 +03:00
|
|
|
|
|
|
|
case SphinxParseError:
|
|
|
|
return "SphinxParseError: unable to parse sphinx packet"
|
|
|
|
|
2017-01-08 06:24:40 +03:00
|
|
|
case IncorrectValue:
|
|
|
|
return "IncorrectValue: htlc value was wrong"
|
|
|
|
|
2017-01-06 08:43:25 +03:00
|
|
|
default:
|
|
|
|
return "unknown reason"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 15:25:36 +03:00
|
|
|
// UpdateFailHTLC is sent by Alice to Bob in order to remove a previously added
|
|
|
|
// HTLC. Upon receipt of an UpdateFailHTLC the HTLC should be removed from the
|
|
|
|
// next commitment transaction, with the UpdateFailHTLC propagated backwards in
|
|
|
|
// the route to fully undo the HTLC.
|
|
|
|
type UpdateFailHTLC struct {
|
|
|
|
// ChannelPoint is the particular active channel that this UpdateFailHTLC
|
2016-09-23 04:51:43 +03:00
|
|
|
// is binded to.
|
2017-02-16 15:25:36 +03:00
|
|
|
ChannelPoint wire.OutPoint
|
2016-09-23 04:51:43 +03:00
|
|
|
|
2017-02-16 15:25:36 +03:00
|
|
|
// ID references which HTLC on the remote node's commitment transaction
|
|
|
|
// has timed out.
|
|
|
|
ID uint64
|
2017-01-06 08:43:25 +03:00
|
|
|
|
2017-02-16 15:25:36 +03:00
|
|
|
// Reason is an onion-encrypted blob that details why the HTLC was
|
|
|
|
// failed. This blob is only fully decryptable by the initiator of the
|
|
|
|
// HTLC message.
|
|
|
|
// TODO(roasbeef): properly format the encrypted failure reason
|
|
|
|
Reason []byte
|
2016-09-23 04:51:43 +03:00
|
|
|
}
|
|
|
|
|
2017-02-16 15:25:36 +03:00
|
|
|
// A compile time check to ensure UpdateFailHTLC implements the lnwire.Message
|
|
|
|
// interface.
|
|
|
|
var _ Message = (*UpdateFailHTLC)(nil)
|
|
|
|
|
|
|
|
// Decode deserializes a serialized UpdateFailHTLC message stored in the passed
|
2016-09-23 04:51:43 +03:00
|
|
|
// io.Reader observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-02-16 15:25:36 +03:00
|
|
|
func (c *UpdateFailHTLC) Decode(r io.Reader, pver uint32) error {
|
2016-09-23 04:51:43 +03:00
|
|
|
// ChannelPoint(8)
|
|
|
|
// HTLCKey(8)
|
2017-02-16 15:25:36 +03:00
|
|
|
// Reason(??)
|
2016-09-23 04:51:43 +03:00
|
|
|
err := readElements(r,
|
|
|
|
&c.ChannelPoint,
|
2017-02-16 15:25:36 +03:00
|
|
|
&c.ID,
|
|
|
|
c.Reason[:],
|
2016-09-23 04:51:43 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-16 15:25:36 +03:00
|
|
|
// Encode serializes the target UpdateFailHTLC into the passed io.Writer observing
|
2016-09-23 04:51:43 +03:00
|
|
|
// the protocol version specified.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-02-16 15:25:36 +03:00
|
|
|
func (c *UpdateFailHTLC) Encode(w io.Writer, pver uint32) error {
|
2016-09-23 04:51:43 +03:00
|
|
|
err := writeElements(w,
|
|
|
|
c.ChannelPoint,
|
2017-02-16 15:25:36 +03:00
|
|
|
c.ID,
|
|
|
|
c.Reason[:],
|
2016-09-23 04:51:43 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command returns the integer uniquely identifying this message type on the
|
|
|
|
// wire.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-02-16 15:25:36 +03:00
|
|
|
func (c *UpdateFailHTLC) Command() uint32 {
|
|
|
|
return CmdUpdateFailHTLC
|
2016-09-23 04:51:43 +03:00
|
|
|
}
|
|
|
|
|
2017-02-16 15:25:36 +03:00
|
|
|
// MaxPayloadLength returns the maximum allowed payload size for a UpdateFailHTLC
|
2016-09-23 04:51:43 +03:00
|
|
|
// complete message observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-02-16 15:25:36 +03:00
|
|
|
func (c *UpdateFailHTLC) MaxPayloadLength(uint32) uint32 {
|
|
|
|
// 36 + 8 + 154
|
|
|
|
return 198
|
2016-09-23 04:51:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate performs any necessary sanity checks to ensure all fields present
|
2017-02-16 15:25:36 +03:00
|
|
|
// on the UpdateFailHTLC are valid.
|
2016-09-23 04:51:43 +03:00
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-02-16 15:25:36 +03:00
|
|
|
func (c *UpdateFailHTLC) Validate() error {
|
2016-09-23 04:51:43 +03:00
|
|
|
// We're good!
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-16 15:25:36 +03:00
|
|
|
|
|
|
|
// String returns the string representation of the target UpdateFailHTLC. This is
|
|
|
|
// part of the lnwire.Message interface.
|
|
|
|
func (c *UpdateFailHTLC) String() string {
|
|
|
|
return fmt.Sprintf("\n--- Begin UpdateFailHTLC ---\n") +
|
|
|
|
fmt.Sprintf("ChannelPoint:\t%d\n", c.ChannelPoint) +
|
|
|
|
fmt.Sprintf("ID:\t%d\n", c.ID) +
|
|
|
|
fmt.Sprintf("Reason:\t\t%x\n", c.Reason) +
|
|
|
|
fmt.Sprintf("--- End UpdateFailHTLC ---\n")
|
|
|
|
}
|