routing: correct fee calculation when converting from path to route

In this commit, we correct the fee calculation when converting from a
path to route. Previously we would apply the “no fee” case at the
_first_ hop, rather than the last hop. As a result, we needed to swap
the edges during path finding, otherwise, if the incoming and outgoing
edges had different fee rates, then we would create an invalid onion
payload.

In this commit we now properly switch fee calculation into three cases:
  * a single hop route, so there’s no fee
  * we’re at the first hop in a multi hop route, and we apply the fee
for the _next_ hop
  * we’re at an intermediate hop and the fee calculation proceeds as
normal
This commit is contained in:
Olaoluwa Osuntokun 2017-10-18 21:57:13 -07:00
parent 8df69a83a7
commit 4b82e2ec43
No known key found for this signature in database
GPG Key ID: 964EA263DD637C21

@ -280,7 +280,28 @@ func newRoute(amtToSend lnwire.MilliSatoshi, sourceVertex vertex,
// We don't pay any fees to ourselves on the first-hop channel, // We don't pay any fees to ourselves on the first-hop channel,
// so we don't tally up the running fee and amount. // so we don't tally up the running fee and amount.
if i != len(pathEdges)-1 { switch {
// If this is a single-hop payment, there's no fee required.
case i == 0 && len(pathEdges) == 1:
nextHop.Fee = 0
// If this is the "first" hop in a multi-hop payment, then we
// don't pay any fee to ourselves, but we craft the payload to
// prescribe the proper fee for the _next_ hop.
case i == 0 && len(pathEdges) > 1:
// Now, we'll compute the fee that we need to path this
// hop for its downstream transit. This is the value we
// want coming into the _next_ hop, using the fees from
// the _incoming_ node.
nextFee := computeFee(route.Hops[i+1].AmtToForward,
pathEdges[i+1])
nextHop.AmtToForward -= nextFee
nextHop.Fee = 0
// Otherwise, this is an intermediate hop, and we compute the
// fees as normal.
default:
// For a node to forward an HTLC, then following // For a node to forward an HTLC, then following
// inequality most hold true: amt_in - fee >= // inequality most hold true: amt_in - fee >=
// amt_to_forward. Therefore we add the fee this node // amt_to_forward. Therefore we add the fee this node
@ -295,8 +316,6 @@ func newRoute(amtToSend lnwire.MilliSatoshi, sourceVertex vertex,
// the amount of blocks it'll subtract from the // the amount of blocks it'll subtract from the
// incoming time lock. // incoming time lock.
route.TotalFees += nextHop.Fee route.TotalFees += nextHop.Fee
} else {
nextHop.Fee = 0
} }
// If this is the last hop, then for verification purposes, the // If this is the last hop, then for verification purposes, the