2019-04-05 18:36:11 +03:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
2019-07-31 07:38:43 +03:00
|
|
|
"bytes"
|
2019-04-05 18:36:11 +03:00
|
|
|
"encoding/binary"
|
2019-08-29 14:03:30 +03:00
|
|
|
"encoding/hex"
|
2019-11-05 01:11:28 +03:00
|
|
|
"errors"
|
2019-04-05 18:36:11 +03:00
|
|
|
"fmt"
|
2019-07-31 07:38:43 +03:00
|
|
|
"io"
|
2019-01-25 12:44:21 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-04-05 18:36:11 +03:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
sphinx "github.com/lightningnetwork/lightning-onion"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2019-08-30 11:41:12 +03:00
|
|
|
"github.com/lightningnetwork/lnd/record"
|
2019-07-31 07:38:43 +03:00
|
|
|
"github.com/lightningnetwork/lnd/tlv"
|
2019-04-05 18:36:11 +03:00
|
|
|
)
|
|
|
|
|
2019-08-07 14:03:23 +03:00
|
|
|
// VertexSize is the size of the array to store a vertex.
|
|
|
|
const VertexSize = 33
|
|
|
|
|
2019-11-05 01:11:28 +03:00
|
|
|
var (
|
|
|
|
// ErrNoRouteHopsProvided is returned when a caller attempts to
|
|
|
|
// construct a new sphinx packet, but provides an empty set of hops for
|
|
|
|
// each route.
|
|
|
|
ErrNoRouteHopsProvided = fmt.Errorf("empty route hops provided")
|
|
|
|
|
|
|
|
// ErrIntermediateMPPHop is returned when a hop tries to deliver an MPP
|
|
|
|
// record to an intermediate hop, only final hops can receive MPP
|
|
|
|
// records.
|
|
|
|
ErrIntermediateMPPHop = errors.New("cannot send MPP to intermediate")
|
|
|
|
)
|
2019-04-05 18:36:11 +03:00
|
|
|
|
|
|
|
// Vertex is a simple alias for the serialization of a compressed Bitcoin
|
|
|
|
// public key.
|
2019-08-07 14:03:23 +03:00
|
|
|
type Vertex [VertexSize]byte
|
2019-04-05 18:36:11 +03:00
|
|
|
|
|
|
|
// NewVertex returns a new Vertex given a public key.
|
|
|
|
func NewVertex(pub *btcec.PublicKey) Vertex {
|
|
|
|
var v Vertex
|
|
|
|
copy(v[:], pub.SerializeCompressed())
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2019-08-07 14:03:23 +03:00
|
|
|
// NewVertexFromBytes returns a new Vertex based on a serialized pubkey in a
|
|
|
|
// byte slice.
|
|
|
|
func NewVertexFromBytes(b []byte) (Vertex, error) {
|
|
|
|
vertexLen := len(b)
|
|
|
|
if vertexLen != VertexSize {
|
|
|
|
return Vertex{}, fmt.Errorf("invalid vertex length of %v, "+
|
|
|
|
"want %v", vertexLen, VertexSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
var v Vertex
|
|
|
|
copy(v[:], b)
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2019-08-29 14:03:30 +03:00
|
|
|
// NewVertexFromStr returns a new Vertex given its hex-encoded string format.
|
|
|
|
func NewVertexFromStr(v string) (Vertex, error) {
|
|
|
|
// Return error if hex string is of incorrect length.
|
|
|
|
if len(v) != VertexSize*2 {
|
|
|
|
return Vertex{}, fmt.Errorf("invalid vertex string length of "+
|
|
|
|
"%v, want %v", len(v), VertexSize*2)
|
|
|
|
}
|
|
|
|
|
|
|
|
vertex, err := hex.DecodeString(v)
|
|
|
|
if err != nil {
|
|
|
|
return Vertex{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewVertexFromBytes(vertex)
|
|
|
|
}
|
|
|
|
|
2019-04-05 18:36:11 +03:00
|
|
|
// String returns a human readable version of the Vertex which is the
|
|
|
|
// hex-encoding of the serialized compressed public key.
|
|
|
|
func (v Vertex) String() string {
|
|
|
|
return fmt.Sprintf("%x", v[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hop represents an intermediate or final node of the route. This naming
|
|
|
|
// is in line with the definition given in BOLT #4: Onion Routing Protocol.
|
|
|
|
// The struct houses the channel along which this hop can be reached and
|
|
|
|
// the values necessary to create the HTLC that needs to be sent to the
|
|
|
|
// next hop. It is also used to encode the per-hop payload included within
|
|
|
|
// the Sphinx packet.
|
|
|
|
type Hop struct {
|
|
|
|
// PubKeyBytes is the raw bytes of the public key of the target node.
|
|
|
|
PubKeyBytes Vertex
|
|
|
|
|
|
|
|
// ChannelID is the unique channel ID for the channel. The first 3
|
|
|
|
// bytes are the block height, the next 3 the index within the block,
|
|
|
|
// and the last 2 bytes are the output index for the channel.
|
|
|
|
ChannelID uint64
|
|
|
|
|
|
|
|
// OutgoingTimeLock is the timelock value that should be used when
|
|
|
|
// crafting the _outgoing_ HTLC from this hop.
|
|
|
|
OutgoingTimeLock uint32
|
|
|
|
|
|
|
|
// AmtToForward is the amount that this hop will forward to the next
|
|
|
|
// hop. This value is less than the value that the incoming HTLC
|
|
|
|
// carries as a fee will be subtracted by the hop.
|
|
|
|
AmtToForward lnwire.MilliSatoshi
|
2019-07-31 07:38:43 +03:00
|
|
|
|
2019-11-05 01:11:28 +03:00
|
|
|
// MPP encapsulates the data required for option_mpp. This field should
|
|
|
|
// only be set for the final hop.
|
|
|
|
MPP *record.MPP
|
|
|
|
|
2019-12-11 12:52:27 +03:00
|
|
|
// CustomRecords if non-nil are a set of additional TLV records that
|
2019-07-31 07:38:43 +03:00
|
|
|
// should be included in the forwarding instructions for this node.
|
2019-12-11 12:52:27 +03:00
|
|
|
CustomRecords record.CustomSet
|
2019-07-31 07:38:43 +03:00
|
|
|
|
|
|
|
// LegacyPayload if true, then this signals that this node doesn't
|
|
|
|
// understand the new TLV payload, so we must instead use the legacy
|
|
|
|
// payload.
|
|
|
|
LegacyPayload bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// PackHopPayload writes to the passed io.Writer, the series of byes that can
|
|
|
|
// be placed directly into the per-hop payload (EOB) for this hop. This will
|
|
|
|
// include the required routing fields, as well as serializing any of the
|
|
|
|
// passed optional TLVRecords. nextChanID is the unique channel ID that
|
|
|
|
// references the _outgoing_ channel ID that follows this hop. This field
|
|
|
|
// follows the same semantics as the NextAddress field in the onion: it should
|
|
|
|
// be set to zero to indicate the terminal hop.
|
|
|
|
func (h *Hop) PackHopPayload(w io.Writer, nextChanID uint64) error {
|
|
|
|
// If this is a legacy payload, then we'll exit here as this method
|
|
|
|
// shouldn't be called.
|
|
|
|
if h.LegacyPayload == true {
|
|
|
|
return fmt.Errorf("cannot pack hop payloads for legacy " +
|
|
|
|
"payloads")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we'll need to make a new stream that includes our
|
|
|
|
// required routing fields, as well as these optional values.
|
2019-09-05 00:11:31 +03:00
|
|
|
var records []tlv.Record
|
|
|
|
|
|
|
|
// Every hop must have an amount to forward and CLTV expiry.
|
2019-07-31 07:38:43 +03:00
|
|
|
amt := uint64(h.AmtToForward)
|
2019-09-05 00:11:31 +03:00
|
|
|
records = append(records,
|
2019-08-30 11:41:12 +03:00
|
|
|
record.NewAmtToFwdRecord(&amt),
|
|
|
|
record.NewLockTimeRecord(&h.OutgoingTimeLock),
|
2019-07-31 07:38:43 +03:00
|
|
|
)
|
|
|
|
|
2019-09-04 18:39:22 +03:00
|
|
|
// BOLT 04 says the next_hop_id should be omitted for the final hop,
|
|
|
|
// but present for all others.
|
|
|
|
//
|
|
|
|
// TODO(conner): test using hop.Exit once available
|
|
|
|
if nextChanID != 0 {
|
2019-09-05 00:11:31 +03:00
|
|
|
records = append(records,
|
2019-09-04 18:39:22 +03:00
|
|
|
record.NewNextHopIDRecord(&nextChanID),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-11-05 01:11:28 +03:00
|
|
|
// If an MPP record is destined for this hop, ensure that we only ever
|
|
|
|
// attach it to the final hop. Otherwise the route was constructed
|
|
|
|
// incorrectly.
|
|
|
|
if h.MPP != nil {
|
|
|
|
if nextChanID == 0 {
|
|
|
|
records = append(records, h.MPP.Record())
|
|
|
|
} else {
|
|
|
|
return ErrIntermediateMPPHop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 00:11:31 +03:00
|
|
|
// Append any custom types destined for this hop.
|
2019-12-11 12:52:27 +03:00
|
|
|
tlvRecords := tlv.MapToRecords(h.CustomRecords)
|
|
|
|
records = append(records, tlvRecords...)
|
2019-09-05 00:11:31 +03:00
|
|
|
|
2019-07-31 07:38:43 +03:00
|
|
|
// To ensure we produce a canonical stream, we'll sort the records
|
|
|
|
// before encoding them as a stream in the hop payload.
|
2019-09-05 00:11:31 +03:00
|
|
|
tlv.SortRecords(records)
|
2019-07-31 07:38:43 +03:00
|
|
|
|
2019-09-05 00:11:31 +03:00
|
|
|
tlvStream, err := tlv.NewStream(records...)
|
2019-07-31 07:38:43 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tlvStream.Encode(w)
|
2019-04-05 18:36:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Route represents a path through the channel graph which runs over one or
|
|
|
|
// more channels in succession. This struct carries all the information
|
|
|
|
// required to craft the Sphinx onion packet, and send the payment along the
|
|
|
|
// first hop in the path. A route is only selected as valid if all the channels
|
|
|
|
// have sufficient capacity to carry the initial payment amount after fees are
|
|
|
|
// accounted for.
|
|
|
|
type Route struct {
|
|
|
|
// TotalTimeLock is the cumulative (final) time lock across the entire
|
|
|
|
// route. This is the CLTV value that should be extended to the first
|
|
|
|
// hop in the route. All other hops will decrement the time-lock as
|
|
|
|
// advertised, leaving enough time for all hops to wait for or present
|
|
|
|
// the payment preimage to complete the payment.
|
|
|
|
TotalTimeLock uint32
|
|
|
|
|
|
|
|
// TotalAmount is the total amount of funds required to complete a
|
|
|
|
// payment over this route. This value includes the cumulative fees at
|
|
|
|
// each hop. As a result, the HTLC extended to the first-hop in the
|
|
|
|
// route will need to have at least this many satoshis, otherwise the
|
|
|
|
// route will fail at an intermediate node due to an insufficient
|
|
|
|
// amount of fees.
|
|
|
|
TotalAmount lnwire.MilliSatoshi
|
|
|
|
|
|
|
|
// SourcePubKey is the pubkey of the node where this route originates
|
|
|
|
// from.
|
|
|
|
SourcePubKey Vertex
|
|
|
|
|
|
|
|
// Hops contains details concerning the specific forwarding details at
|
|
|
|
// each hop.
|
|
|
|
Hops []*Hop
|
|
|
|
}
|
|
|
|
|
|
|
|
// HopFee returns the fee charged by the route hop indicated by hopIndex.
|
|
|
|
func (r *Route) HopFee(hopIndex int) lnwire.MilliSatoshi {
|
|
|
|
var incomingAmt lnwire.MilliSatoshi
|
|
|
|
if hopIndex == 0 {
|
|
|
|
incomingAmt = r.TotalAmount
|
|
|
|
} else {
|
|
|
|
incomingAmt = r.Hops[hopIndex-1].AmtToForward
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fee is calculated as difference between incoming and outgoing amount.
|
|
|
|
return incomingAmt - r.Hops[hopIndex].AmtToForward
|
|
|
|
}
|
|
|
|
|
2019-05-15 15:37:58 +03:00
|
|
|
// TotalFees is the sum of the fees paid at each hop within the final route. In
|
|
|
|
// the case of a one-hop payment, this value will be zero as we don't need to
|
|
|
|
// pay a fee to ourself.
|
|
|
|
func (r *Route) TotalFees() lnwire.MilliSatoshi {
|
2019-05-28 12:03:14 +03:00
|
|
|
if len(r.Hops) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-05-15 15:37:58 +03:00
|
|
|
return r.TotalAmount - r.Hops[len(r.Hops)-1].AmtToForward
|
|
|
|
}
|
|
|
|
|
2019-04-05 18:36:11 +03:00
|
|
|
// NewRouteFromHops creates a new Route structure from the minimally required
|
|
|
|
// information to perform the payment. It infers fee amounts and populates the
|
|
|
|
// node, chan and prev/next hop maps.
|
|
|
|
func NewRouteFromHops(amtToSend lnwire.MilliSatoshi, timeLock uint32,
|
|
|
|
sourceVertex Vertex, hops []*Hop) (*Route, error) {
|
|
|
|
|
|
|
|
if len(hops) == 0 {
|
|
|
|
return nil, ErrNoRouteHopsProvided
|
|
|
|
}
|
|
|
|
|
|
|
|
// First, we'll create a route struct and populate it with the fields
|
|
|
|
// for which the values are provided as arguments of this function.
|
|
|
|
// TotalFees is determined based on the difference between the amount
|
|
|
|
// that is send from the source and the final amount that is received
|
|
|
|
// by the destination.
|
|
|
|
route := &Route{
|
|
|
|
SourcePubKey: sourceVertex,
|
|
|
|
Hops: hops,
|
|
|
|
TotalTimeLock: timeLock,
|
|
|
|
TotalAmount: amtToSend,
|
|
|
|
}
|
|
|
|
|
|
|
|
return route, nil
|
|
|
|
}
|
2019-01-11 07:03:07 +03:00
|
|
|
|
|
|
|
// ToSphinxPath converts a complete route into a sphinx PaymentPath that
|
|
|
|
// contains the per-hop paylods used to encoding the HTLC routing data for each
|
2019-07-31 07:38:43 +03:00
|
|
|
// hop in the route. This method also accepts an optional EOB payload for the
|
|
|
|
// final hop.
|
2019-01-11 07:03:07 +03:00
|
|
|
func (r *Route) ToSphinxPath() (*sphinx.PaymentPath, error) {
|
|
|
|
var path sphinx.PaymentPath
|
|
|
|
|
|
|
|
// For each hop encoded within the route, we'll convert the hop struct
|
|
|
|
// to an OnionHop with matching per-hop payload within the path as used
|
|
|
|
// by the sphinx package.
|
|
|
|
for i, hop := range r.Hops {
|
|
|
|
pub, err := btcec.ParsePubKey(
|
|
|
|
hop.PubKeyBytes[:], btcec.S256(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// As a base case, the next hop is set to all zeroes in order
|
|
|
|
// to indicate that the "last hop" as no further hops after it.
|
|
|
|
nextHop := uint64(0)
|
|
|
|
|
|
|
|
// If we aren't on the last hop, then we set the "next address"
|
|
|
|
// field to be the channel that directly follows it.
|
|
|
|
if i != len(r.Hops)-1 {
|
|
|
|
nextHop = r.Hops[i+1].ChannelID
|
|
|
|
}
|
|
|
|
|
2019-07-31 07:38:43 +03:00
|
|
|
var payload sphinx.HopPayload
|
|
|
|
|
|
|
|
// If this is the legacy payload, then we can just include the
|
|
|
|
// hop data as normal.
|
|
|
|
if hop.LegacyPayload {
|
|
|
|
// Before we encode this value, we'll pack the next hop
|
|
|
|
// into the NextAddress field of the hop info to ensure
|
|
|
|
// we point to the right now.
|
|
|
|
hopData := sphinx.HopData{
|
|
|
|
ForwardAmount: uint64(hop.AmtToForward),
|
|
|
|
OutgoingCltv: hop.OutgoingTimeLock,
|
|
|
|
}
|
|
|
|
binary.BigEndian.PutUint64(
|
|
|
|
hopData.NextAddress[:], nextHop,
|
|
|
|
)
|
|
|
|
|
|
|
|
payload, err = sphinx.NewHopPayload(&hopData, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// For non-legacy payloads, we'll need to pack the
|
|
|
|
// routing information, along with any extra TLV
|
|
|
|
// information into the new per-hop payload format.
|
|
|
|
// We'll also pass in the chan ID of the hop this
|
|
|
|
// channel should be forwarded to so we can construct a
|
|
|
|
// valid payload.
|
|
|
|
var b bytes.Buffer
|
|
|
|
err := hop.PackHopPayload(&b, nextHop)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(roasbeef): make better API for NewHopPayload?
|
|
|
|
payload, err = sphinx.NewHopPayload(nil, b.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
path[i] = sphinx.OnionHop{
|
|
|
|
NodePub: *pub,
|
|
|
|
HopPayload: payload,
|
|
|
|
}
|
2019-01-11 07:03:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return &path, nil
|
|
|
|
}
|
2019-01-25 12:44:21 +03:00
|
|
|
|
|
|
|
// String returns a human readable representation of the route.
|
|
|
|
func (r *Route) String() string {
|
|
|
|
var b strings.Builder
|
|
|
|
|
2019-11-11 14:25:36 +03:00
|
|
|
amt := r.TotalAmount
|
2019-01-25 12:44:21 +03:00
|
|
|
for i, hop := range r.Hops {
|
|
|
|
if i > 0 {
|
2019-11-11 14:25:36 +03:00
|
|
|
b.WriteString(" -> ")
|
2019-01-25 12:44:21 +03:00
|
|
|
}
|
2019-11-11 14:25:36 +03:00
|
|
|
b.WriteString(fmt.Sprintf("%v (%v)",
|
|
|
|
strconv.FormatUint(hop.ChannelID, 10),
|
|
|
|
amt,
|
|
|
|
))
|
|
|
|
amt = hop.AmtToForward
|
2019-01-25 12:44:21 +03:00
|
|
|
}
|
|
|
|
|
2019-11-11 14:25:36 +03:00
|
|
|
return fmt.Sprintf("%v, cltv %v",
|
|
|
|
b.String(), r.TotalTimeLock,
|
2019-01-25 12:44:21 +03:00
|
|
|
)
|
|
|
|
}
|