2016-12-19 09:14:01 +03:00
|
|
|
package routing
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
|
|
|
|
import (
|
2017-06-16 23:37:36 +03:00
|
|
|
"encoding/binary"
|
2017-08-22 09:43:20 +03:00
|
|
|
"fmt"
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
"math"
|
|
|
|
|
2017-03-20 00:15:58 +03:00
|
|
|
"container/heap"
|
|
|
|
|
2018-07-31 10:17:17 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2018-03-11 06:00:57 +03:00
|
|
|
"github.com/coreos/bbolt"
|
2019-01-16 17:47:43 +03:00
|
|
|
|
2019-02-01 15:53:27 +03:00
|
|
|
sphinx "github.com/lightningnetwork/lightning-onion"
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2017-08-22 09:43:20 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// HopLimit is the maximum number hops that is permissible as a route.
|
|
|
|
// Any potential paths found that lie above this limit will be rejected
|
|
|
|
// with an error. This value is computed using the current fixed-size
|
|
|
|
// packet length of the Sphinx construction.
|
|
|
|
HopLimit = 20
|
|
|
|
|
|
|
|
// infinity is used as a starting distance in our shortest path search.
|
2018-02-13 03:27:30 +03:00
|
|
|
infinity = math.MaxInt64
|
2018-06-09 23:36:48 +03:00
|
|
|
|
|
|
|
// RiskFactorBillionths controls the influence of time lock delta
|
|
|
|
// of a channel on route selection. It is expressed as billionths
|
|
|
|
// of msat per msat sent through the channel per time lock delta
|
|
|
|
// block. See edgeWeight function below for more details.
|
|
|
|
// The chosen value is based on the previous incorrect weight function
|
|
|
|
// 1 + timelock + fee * fee. In this function, the fee penalty
|
|
|
|
// diminishes the time lock penalty for all but the smallest amounts.
|
|
|
|
// To not change the behaviour of path finding too drastically, a
|
|
|
|
// relatively small value is chosen which is still big enough to give
|
|
|
|
// some effect with smaller time lock values. The value may need
|
|
|
|
// tweaking and/or be made configurable in the future.
|
|
|
|
RiskFactorBillionths = 15
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
)
|
|
|
|
|
2018-03-27 07:00:24 +03:00
|
|
|
// HopHint is a routing hint that contains the minimum information of a channel
|
|
|
|
// required for an intermediate hop in a route to forward the payment to the
|
|
|
|
// next. This should be ideally used for private channels, since they are not
|
|
|
|
// publicly advertised to the network for routing.
|
|
|
|
type HopHint struct {
|
|
|
|
// NodeID is the public key of the node at the start of the channel.
|
|
|
|
NodeID *btcec.PublicKey
|
|
|
|
|
|
|
|
// ChannelID is the unique identifier of the channel.
|
|
|
|
ChannelID uint64
|
|
|
|
|
|
|
|
// FeeBaseMSat is the base fee of the channel in millisatoshis.
|
|
|
|
FeeBaseMSat uint32
|
|
|
|
|
|
|
|
// FeeProportionalMillionths is the fee rate, in millionths of a
|
|
|
|
// satoshi, for every satoshi sent through the channel.
|
|
|
|
FeeProportionalMillionths uint32
|
|
|
|
|
|
|
|
// CLTVExpiryDelta is the time-lock delta of the channel.
|
|
|
|
CLTVExpiryDelta uint16
|
|
|
|
}
|
|
|
|
|
2018-06-12 14:04:40 +03:00
|
|
|
// 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
|
2018-07-31 10:19:49 +03:00
|
|
|
// next hop. It is also used to encode the per-hop payload included within
|
2018-06-12 14:04:40 +03:00
|
|
|
// the Sphinx packet.
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
type Hop struct {
|
2018-08-09 16:36:28 +03:00
|
|
|
// 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
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
|
2017-06-16 23:33:50 +03:00
|
|
|
// OutgoingTimeLock is the timelock value that should be used when
|
|
|
|
// crafting the _outgoing_ HTLC from this hop.
|
|
|
|
OutgoingTimeLock uint32
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
|
|
|
|
// 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.
|
2017-08-22 09:43:20 +03:00
|
|
|
AmtToForward lnwire.MilliSatoshi
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
}
|
|
|
|
|
2018-06-04 23:10:05 +03:00
|
|
|
// edgePolicyWithSource is a helper struct to keep track of the source node
|
|
|
|
// of a channel edge. ChannelEdgePolicy only contains to destination node
|
|
|
|
// of the edge.
|
|
|
|
type edgePolicyWithSource struct {
|
|
|
|
sourceNode *channeldb.LightningNode
|
|
|
|
edge *channeldb.ChannelEdgePolicy
|
|
|
|
}
|
|
|
|
|
2017-08-22 09:43:20 +03:00
|
|
|
// computeFee computes the fee to forward an HTLC of `amt` milli-satoshis over
|
|
|
|
// the passed active payment channel. This value is currently computed as
|
|
|
|
// specified in BOLT07, but will likely change in the near future.
|
2018-02-13 03:27:30 +03:00
|
|
|
func computeFee(amt lnwire.MilliSatoshi,
|
|
|
|
edge *channeldb.ChannelEdgePolicy) lnwire.MilliSatoshi {
|
|
|
|
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
return edge.FeeBaseMSat + (amt*edge.FeeProportionalMillionths)/1000000
|
|
|
|
}
|
|
|
|
|
2017-03-21 04:15:50 +03:00
|
|
|
// isSamePath returns true if path1 and path2 travel through the exact same
|
|
|
|
// edges, and false otherwise.
|
2018-08-09 16:36:28 +03:00
|
|
|
func isSamePath(path1, path2 []*channeldb.ChannelEdgePolicy) bool {
|
2017-04-14 00:42:29 +03:00
|
|
|
if len(path1) != len(path2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-03-21 04:15:50 +03:00
|
|
|
for i := 0; i < len(path1); i++ {
|
|
|
|
if path1[i].ChannelID != path2[i].ChannelID {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// 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
|
2017-12-18 05:40:05 +03:00
|
|
|
// we don't need to pay a fee to ourself.
|
2017-08-22 09:43:20 +03:00
|
|
|
TotalFees lnwire.MilliSatoshi
|
2017-03-21 04:15:50 +03:00
|
|
|
|
|
|
|
// 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.
|
2017-08-22 09:43:20 +03:00
|
|
|
TotalAmount lnwire.MilliSatoshi
|
2017-03-21 04:15:50 +03:00
|
|
|
|
2018-11-29 18:36:37 +03:00
|
|
|
// SourcePubKey is the pubkey of the node where this route originates
|
|
|
|
// from.
|
|
|
|
SourcePubKey Vertex
|
|
|
|
|
2017-03-21 04:15:50 +03:00
|
|
|
// Hops contains details concerning the specific forwarding details at
|
|
|
|
// each hop.
|
|
|
|
Hops []*Hop
|
2018-08-09 16:36:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2017-10-11 05:41:47 +03:00
|
|
|
}
|
|
|
|
|
2017-06-16 23:37:36 +03:00
|
|
|
// ToHopPayloads converts a complete route into the series of per-hop payloads
|
|
|
|
// that is to be encoded within each HTLC using an opaque Sphinx packet.
|
|
|
|
func (r *Route) ToHopPayloads() []sphinx.HopData {
|
|
|
|
hopPayloads := make([]sphinx.HopData, len(r.Hops))
|
|
|
|
|
|
|
|
// For each hop encoded within the route, we'll convert the hop struct
|
|
|
|
// to the matching per-hop payload struct as used by the sphinx
|
|
|
|
// package.
|
|
|
|
for i, hop := range r.Hops {
|
|
|
|
hopPayloads[i] = sphinx.HopData{
|
|
|
|
// TODO(roasbeef): properly set realm, make sphinx type
|
|
|
|
// an enum actually?
|
|
|
|
Realm: 0,
|
|
|
|
ForwardAmount: uint64(hop.AmtToForward),
|
|
|
|
OutgoingCltv: hop.OutgoingTimeLock,
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2018-08-09 16:36:28 +03:00
|
|
|
nextHop = r.Hops[i+1].ChannelID
|
2017-06-16 23:37:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint64(hopPayloads[i].NextAddress[:],
|
|
|
|
nextHop)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hopPayloads
|
|
|
|
}
|
|
|
|
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
// newRoute returns a fully valid route between the source and target that's
|
|
|
|
// capable of supporting a payment of `amtToSend` after fees are fully
|
2017-03-20 01:15:24 +03:00
|
|
|
// computed. If the route is too long, or the selected path cannot support the
|
|
|
|
// fully payment including fees, then a non-nil error is returned.
|
|
|
|
//
|
2017-03-21 04:11:22 +03:00
|
|
|
// NOTE: The passed slice of ChannelHops MUST be sorted in forward order: from
|
|
|
|
// the source to the target node of the path finding attempt.
|
2019-03-05 12:37:14 +03:00
|
|
|
func newRoute(amtToSend lnwire.MilliSatoshi, sourceVertex Vertex,
|
2018-08-09 16:36:28 +03:00
|
|
|
pathEdges []*channeldb.ChannelEdgePolicy, currentHeight uint32,
|
2018-04-19 17:32:24 +03:00
|
|
|
finalCLTVDelta uint16) (*Route, error) {
|
2017-08-03 07:01:49 +03:00
|
|
|
|
2018-10-24 03:16:39 +03:00
|
|
|
var (
|
|
|
|
hops []*Hop
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
|
2018-10-24 03:16:39 +03:00
|
|
|
// totalTimeLock will accumulate the cumulative time lock
|
|
|
|
// across the entire route. This value represents how long the
|
|
|
|
// sender will need to wait in the *worst* case.
|
|
|
|
totalTimeLock = currentHeight
|
2017-10-11 05:45:09 +03:00
|
|
|
|
2018-10-24 03:16:39 +03:00
|
|
|
// nextIncomingAmount is the amount that will need to flow into
|
|
|
|
// the *next* hop. Since we're going to be walking the route
|
|
|
|
// backwards below, this next hop gets closer and closer to the
|
|
|
|
// sender of the payment.
|
|
|
|
nextIncomingAmount lnwire.MilliSatoshi
|
|
|
|
)
|
2017-10-11 05:41:47 +03:00
|
|
|
|
2018-10-24 03:16:39 +03:00
|
|
|
pathLength := len(pathEdges)
|
2018-08-09 16:36:28 +03:00
|
|
|
for i := pathLength - 1; i >= 0; i-- {
|
2017-10-25 04:27:29 +03:00
|
|
|
// Now we'll start to calculate the items within the per-hop
|
2018-10-24 03:16:39 +03:00
|
|
|
// payload for the hop this edge is leading to.
|
|
|
|
edge := pathEdges[i]
|
2018-06-12 14:04:40 +03:00
|
|
|
|
2018-10-24 03:16:39 +03:00
|
|
|
// If this is the last hop, then the hop payload will contain
|
2018-06-12 14:04:40 +03:00
|
|
|
// the exact amount. In BOLT #4: Onion Routing
|
|
|
|
// Protocol / "Payload for the Last Node", this is detailed.
|
|
|
|
amtToForward := amtToSend
|
|
|
|
|
|
|
|
// Fee is not part of the hop payload, but only used for
|
|
|
|
// reporting through RPC. Set to zero for the final hop.
|
2017-10-25 04:27:29 +03:00
|
|
|
fee := lnwire.MilliSatoshi(0)
|
|
|
|
|
2018-07-31 10:19:49 +03:00
|
|
|
// If the current hop isn't the last hop, then add enough funds
|
2018-06-12 14:04:40 +03:00
|
|
|
// to pay for transit over the next link.
|
2017-10-25 04:27:29 +03:00
|
|
|
if i != len(pathEdges)-1 {
|
2018-07-31 10:19:49 +03:00
|
|
|
// The amount that the current hop needs to forward is
|
2018-08-09 16:36:28 +03:00
|
|
|
// equal to the incoming amount of the next hop.
|
|
|
|
amtToForward = nextIncomingAmount
|
2018-06-12 14:04:40 +03:00
|
|
|
|
|
|
|
// The fee that needs to be paid to the current hop is
|
2018-07-31 10:19:49 +03:00
|
|
|
// based on the amount that this hop needs to forward
|
2018-06-12 14:04:40 +03:00
|
|
|
// and its policy for the outgoing channel. This policy
|
|
|
|
// is stored as part of the incoming channel of
|
|
|
|
// the next hop.
|
2018-08-09 16:36:28 +03:00
|
|
|
fee = computeFee(amtToForward, pathEdges[i+1])
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
}
|
|
|
|
|
2017-06-16 23:33:50 +03:00
|
|
|
// If this is the last hop, then for verification purposes, the
|
2017-09-12 22:27:41 +03:00
|
|
|
// value of the outgoing time-lock should be _exactly_ the
|
|
|
|
// absolute time out they'd expect in the HTLC.
|
2018-08-09 16:36:28 +03:00
|
|
|
var outgoingTimeLock uint32
|
2017-06-16 23:33:50 +03:00
|
|
|
if i == len(pathEdges)-1 {
|
2017-10-19 07:43:53 +03:00
|
|
|
// As this is the last hop, we'll use the specified
|
|
|
|
// final CLTV delta value instead of the value from the
|
|
|
|
// last link in the route.
|
2018-08-09 16:36:28 +03:00
|
|
|
totalTimeLock += uint32(finalCLTVDelta)
|
2017-10-19 07:43:53 +03:00
|
|
|
|
2018-08-09 16:36:28 +03:00
|
|
|
outgoingTimeLock = currentHeight + uint32(finalCLTVDelta)
|
2017-06-16 23:33:50 +03:00
|
|
|
} else {
|
2017-10-19 07:43:53 +03:00
|
|
|
// Next, increment the total timelock of the entire
|
|
|
|
// route such that each hops time lock increases as we
|
|
|
|
// walk backwards in the route, using the delta of the
|
|
|
|
// previous hop.
|
2018-06-26 06:27:46 +03:00
|
|
|
delta := uint32(pathEdges[i+1].TimeLockDelta)
|
2018-08-09 16:36:28 +03:00
|
|
|
totalTimeLock += delta
|
2017-10-19 07:43:53 +03:00
|
|
|
|
2017-06-16 23:33:50 +03:00
|
|
|
// Otherwise, the value of the outgoing time-lock will
|
|
|
|
// be the value of the time-lock for the _outgoing_
|
2017-08-03 07:01:49 +03:00
|
|
|
// HTLC, so we factor in their specified grace period
|
|
|
|
// (time lock delta).
|
2018-08-09 16:36:28 +03:00
|
|
|
outgoingTimeLock = totalTimeLock - delta
|
2017-06-16 23:33:50 +03:00
|
|
|
}
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
|
2018-10-24 03:16:39 +03:00
|
|
|
// Since we're traversing the path backwards atm, we prepend
|
|
|
|
// each new hop such that, the final slice of hops will be in
|
|
|
|
// the forwards order.
|
2018-08-09 16:36:28 +03:00
|
|
|
currentHop := &Hop{
|
|
|
|
PubKeyBytes: Vertex(edge.Node.PubKeyBytes),
|
|
|
|
ChannelID: edge.ChannelID,
|
|
|
|
AmtToForward: amtToForward,
|
|
|
|
OutgoingTimeLock: outgoingTimeLock,
|
|
|
|
}
|
|
|
|
hops = append([]*Hop{currentHop}, hops...)
|
|
|
|
|
2018-10-24 03:16:39 +03:00
|
|
|
// Finally, we update the amount that needs to flow into the
|
|
|
|
// *next* hop, which is the amount this hop needs to forward,
|
|
|
|
// accounting for the fee that it takes.
|
2018-08-09 16:36:28 +03:00
|
|
|
nextIncomingAmount = amtToForward + fee
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
}
|
|
|
|
|
2018-08-09 16:36:28 +03:00
|
|
|
// With the base routing data expressed as hops, build the full route
|
2018-12-10 11:41:03 +03:00
|
|
|
newRoute, err := NewRouteFromHops(
|
2018-10-24 03:16:39 +03:00
|
|
|
nextIncomingAmount, totalTimeLock, sourceVertex, hops,
|
|
|
|
)
|
2018-12-10 11:41:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-09 16:36:28 +03:00
|
|
|
|
|
|
|
return newRoute, nil
|
|
|
|
}
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
|
2018-10-24 03:16:39 +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.
|
2018-08-09 16:36:28 +03:00
|
|
|
func NewRouteFromHops(amtToSend lnwire.MilliSatoshi, timeLock uint32,
|
2018-12-10 11:41:03 +03:00
|
|
|
sourceVertex Vertex, hops []*Hop) (*Route, error) {
|
|
|
|
|
|
|
|
if len(hops) == 0 {
|
|
|
|
return nil, ErrNoRouteHopsProvided
|
|
|
|
}
|
2018-08-09 16:36:28 +03:00
|
|
|
|
|
|
|
// 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
|
2018-10-24 03:16:39 +03:00
|
|
|
// that is send from the source and the final amount that is received
|
|
|
|
// by the destination.
|
2018-08-09 16:36:28 +03:00
|
|
|
route := &Route{
|
2018-11-29 18:36:37 +03:00
|
|
|
SourcePubKey: sourceVertex,
|
2018-08-09 16:36:28 +03:00
|
|
|
Hops: hops,
|
|
|
|
TotalTimeLock: timeLock,
|
|
|
|
TotalAmount: amtToSend,
|
|
|
|
TotalFees: amtToSend - hops[len(hops)-1].AmtToForward,
|
|
|
|
}
|
|
|
|
|
2018-12-10 11:41:03 +03:00
|
|
|
return route, nil
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
}
|
|
|
|
|
2017-09-08 04:25:43 +03:00
|
|
|
// Vertex is a simple alias for the serialization of a compressed Bitcoin
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
// public key.
|
2017-09-08 04:25:43 +03:00
|
|
|
type Vertex [33]byte
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
|
2017-09-08 04:25:43 +03:00
|
|
|
// NewVertex returns a new Vertex given a public key.
|
|
|
|
func NewVertex(pub *btcec.PublicKey) Vertex {
|
|
|
|
var v Vertex
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
copy(v[:], pub.SerializeCompressed())
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-09-08 04:25:43 +03:00
|
|
|
// String returns a human readable version of the Vertex which is the
|
2017-10-11 05:38:50 +03:00
|
|
|
// hex-encoding of the serialized compressed public key.
|
2017-09-08 04:25:43 +03:00
|
|
|
func (v Vertex) String() string {
|
2017-10-11 05:38:50 +03:00
|
|
|
return fmt.Sprintf("%x", v[:])
|
|
|
|
}
|
|
|
|
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
// edgeWeight computes the weight of an edge. This value is used when searching
|
2018-06-09 23:36:48 +03:00
|
|
|
// for the shortest path within the channel graph between two nodes. Weight is
|
2018-07-31 10:19:49 +03:00
|
|
|
// is the fee itself plus a time lock penalty added to it. This benefits
|
|
|
|
// channels with shorter time lock deltas and shorter (hops) routes in general.
|
|
|
|
// RiskFactor controls the influence of time lock on route selection. This is
|
2018-06-09 23:36:48 +03:00
|
|
|
// currently a fixed value, but might be configurable in the future.
|
2018-06-04 23:10:05 +03:00
|
|
|
func edgeWeight(lockedAmt lnwire.MilliSatoshi, fee lnwire.MilliSatoshi,
|
|
|
|
timeLockDelta uint16) int64 {
|
2018-06-09 23:36:48 +03:00
|
|
|
// timeLockPenalty is the penalty for the time lock delta of this channel.
|
|
|
|
// It is controlled by RiskFactorBillionths and scales proportional
|
|
|
|
// to the amount that will pass through channel. Rationale is that it if
|
|
|
|
// a twice as large amount gets locked up, it is twice as bad.
|
2018-06-04 23:10:05 +03:00
|
|
|
timeLockPenalty := int64(lockedAmt) * int64(timeLockDelta) *
|
|
|
|
RiskFactorBillionths / 1000000000
|
2018-02-13 03:27:30 +03:00
|
|
|
|
2018-06-04 23:10:05 +03:00
|
|
|
return int64(fee) + timeLockPenalty
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
}
|
|
|
|
|
2018-10-25 00:06:12 +03:00
|
|
|
// graphParams wraps the set of graph parameters passed to findPath.
|
|
|
|
type graphParams struct {
|
|
|
|
// tx can be set to an existing db transaction. If not set, a new
|
|
|
|
// transaction will be started.
|
|
|
|
tx *bbolt.Tx
|
|
|
|
|
|
|
|
// graph is the ChannelGraph to be used during path finding.
|
|
|
|
graph *channeldb.ChannelGraph
|
|
|
|
|
|
|
|
// additionalEdges is an optional set of edges that should be
|
|
|
|
// considered during path finding, that is not already found in the
|
|
|
|
// channel graph.
|
|
|
|
additionalEdges map[Vertex][]*channeldb.ChannelEdgePolicy
|
|
|
|
|
|
|
|
// bandwidthHints is an optional map from channels to bandwidths that
|
|
|
|
// can be populated if the caller has a better estimate of the current
|
2018-10-26 21:41:55 +03:00
|
|
|
// channel bandwidth than what is found in the graph. If set, it will
|
|
|
|
// override the capacities and disabled flags found in the graph for
|
|
|
|
// local channels when doing path finding. In particular, it should be
|
|
|
|
// set to the current available sending bandwidth for active local
|
|
|
|
// channels, and 0 for inactive channels.
|
2018-10-25 00:06:12 +03:00
|
|
|
bandwidthHints map[uint64]lnwire.MilliSatoshi
|
|
|
|
}
|
|
|
|
|
2019-03-05 13:13:44 +03:00
|
|
|
// RestrictParams wraps the set of restrictions passed to findPath that the
|
2018-10-25 00:06:12 +03:00
|
|
|
// found path must adhere to.
|
2019-03-05 13:13:44 +03:00
|
|
|
type RestrictParams struct {
|
|
|
|
// IgnoredNodes is an optional set of nodes that should be ignored if
|
2018-10-25 00:06:12 +03:00
|
|
|
// encountered during path finding.
|
2019-03-05 13:13:44 +03:00
|
|
|
IgnoredNodes map[Vertex]struct{}
|
2018-10-25 00:06:12 +03:00
|
|
|
|
2019-03-05 13:13:44 +03:00
|
|
|
// IgnoredEdges is an optional set of edges that should be ignored if
|
2018-10-25 00:06:12 +03:00
|
|
|
// encountered during path finding.
|
2019-03-05 13:13:44 +03:00
|
|
|
IgnoredEdges map[EdgeLocator]struct{}
|
2018-10-25 00:06:12 +03:00
|
|
|
|
2019-03-05 13:13:44 +03:00
|
|
|
// FeeLimit is a maximum fee amount allowed to be used on the path from
|
2018-10-25 00:06:12 +03:00
|
|
|
// the source to the target.
|
2019-03-05 13:13:44 +03:00
|
|
|
FeeLimit lnwire.MilliSatoshi
|
2019-02-01 15:53:27 +03:00
|
|
|
|
2019-03-05 13:13:44 +03:00
|
|
|
// OutgoingChannelID is the channel that needs to be taken to the first
|
2019-02-01 15:53:27 +03:00
|
|
|
// hop. If nil, any channel may be used.
|
2019-03-05 13:13:44 +03:00
|
|
|
OutgoingChannelID *uint64
|
2018-10-25 00:06:12 +03:00
|
|
|
}
|
|
|
|
|
2017-03-21 04:15:50 +03:00
|
|
|
// findPath attempts to find a path from the source node within the
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
// ChannelGraph to the target node that's capable of supporting a payment of
|
2017-03-20 01:15:24 +03:00
|
|
|
// `amt` value. The current approach implemented is modified version of
|
|
|
|
// Dijkstra's algorithm to find a single shortest path between the source node
|
|
|
|
// and the destination. The distance metric used for edges is related to the
|
|
|
|
// time-lock+fee costs along a particular edge. If a path is found, this
|
|
|
|
// function returns a slice of ChannelHop structs which encoded the chosen path
|
2018-06-04 23:10:05 +03:00
|
|
|
// from the target to the source. The search is performed backwards from
|
|
|
|
// destination node back to source. This is to properly accumulate fees
|
|
|
|
// that need to be paid along the path and accurately check the amount
|
|
|
|
// to forward at every node against the available bandwidth.
|
2019-03-05 18:55:19 +03:00
|
|
|
func findPath(g *graphParams, r *RestrictParams, source, target Vertex,
|
2018-10-25 00:06:12 +03:00
|
|
|
amt lnwire.MilliSatoshi) ([]*channeldb.ChannelEdgePolicy, error) {
|
2017-10-11 07:39:54 +03:00
|
|
|
|
|
|
|
var err error
|
2018-10-25 00:06:12 +03:00
|
|
|
tx := g.tx
|
2017-10-11 07:39:54 +03:00
|
|
|
if tx == nil {
|
2018-10-25 00:06:12 +03:00
|
|
|
tx, err = g.graph.Database().Begin(false)
|
2017-10-11 07:39:54 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
}
|
2017-03-20 00:15:58 +03:00
|
|
|
|
2017-03-20 01:15:24 +03:00
|
|
|
// First we'll initialize an empty heap which'll help us to quickly
|
2017-03-20 00:15:58 +03:00
|
|
|
// locate the next edge we should visit next during our graph
|
|
|
|
// traversal.
|
|
|
|
var nodeHeap distanceHeap
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
|
2018-08-10 06:47:56 +03:00
|
|
|
// For each node in the graph, we create an entry in the distance map
|
|
|
|
// for the node set with a distance of "infinity". graph.ForEachNode
|
2018-06-04 23:10:05 +03:00
|
|
|
// also returns the source node, so there is no need to add the source
|
2018-09-06 11:48:46 +03:00
|
|
|
// node explicitly.
|
2017-09-08 04:25:43 +03:00
|
|
|
distance := make(map[Vertex]nodeWithDist)
|
2018-10-25 00:06:12 +03:00
|
|
|
if err := g.graph.ForEachNode(tx, func(_ *bbolt.Tx,
|
|
|
|
node *channeldb.LightningNode) error {
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
// TODO(roasbeef): with larger graph can just use disk seeks
|
|
|
|
// with a visited map
|
2018-01-31 07:26:26 +03:00
|
|
|
distance[Vertex(node.PubKeyBytes)] = nodeWithDist{
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
dist: infinity,
|
|
|
|
node: node,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-06-04 23:10:05 +03:00
|
|
|
additionalEdgesWithSrc := make(map[Vertex][]*edgePolicyWithSource)
|
2018-10-25 00:06:12 +03:00
|
|
|
for vertex, outgoingEdgePolicies := range g.additionalEdges {
|
2018-08-10 06:47:56 +03:00
|
|
|
// We'll also include all the nodes found within the additional
|
|
|
|
// edges that are not known to us yet in the distance map.
|
2018-03-27 07:14:10 +03:00
|
|
|
node := &channeldb.LightningNode{PubKeyBytes: vertex}
|
|
|
|
distance[vertex] = nodeWithDist{
|
|
|
|
dist: infinity,
|
|
|
|
node: node,
|
|
|
|
}
|
2018-06-04 23:10:05 +03:00
|
|
|
|
2018-08-10 06:47:56 +03:00
|
|
|
// Build reverse lookup to find incoming edges. Needed because
|
|
|
|
// search is taken place from target to source.
|
2018-06-04 23:10:05 +03:00
|
|
|
for _, outgoingEdgePolicy := range outgoingEdgePolicies {
|
|
|
|
toVertex := outgoingEdgePolicy.Node.PubKeyBytes
|
|
|
|
incomingEdgePolicy := &edgePolicyWithSource{
|
|
|
|
sourceNode: node,
|
|
|
|
edge: outgoingEdgePolicy,
|
|
|
|
}
|
|
|
|
|
|
|
|
additionalEdgesWithSrc[toVertex] =
|
|
|
|
append(additionalEdgesWithSrc[toVertex],
|
|
|
|
incomingEdgePolicy)
|
|
|
|
}
|
2018-03-27 07:14:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We can't always assume that the end destination is publicly
|
|
|
|
// advertised to the network and included in the graph.ForEachNode call
|
2018-08-10 06:47:56 +03:00
|
|
|
// above, so we'll manually include the target node. The target node
|
|
|
|
// charges no fee. Distance is set to 0, because this is the starting
|
|
|
|
// point of the graph traversal. We are searching backwards to get the
|
|
|
|
// fees first time right and correctly match channel bandwidth.
|
2019-03-05 18:55:19 +03:00
|
|
|
targetNode := &channeldb.LightningNode{PubKeyBytes: target}
|
|
|
|
distance[target] = nodeWithDist{
|
2018-06-04 23:10:05 +03:00
|
|
|
dist: 0,
|
|
|
|
node: targetNode,
|
|
|
|
amountToReceive: amt,
|
|
|
|
fee: 0,
|
2018-03-27 07:14:10 +03:00
|
|
|
}
|
|
|
|
|
2018-06-04 23:10:05 +03:00
|
|
|
// We'll use this map as a series of "next" hop pointers. So to get
|
|
|
|
// from `Vertex` to the target node, we'll take the edge that it's
|
|
|
|
// mapped to within `next`.
|
2018-08-09 16:36:28 +03:00
|
|
|
next := make(map[Vertex]*channeldb.ChannelEdgePolicy)
|
2018-03-27 07:14:10 +03:00
|
|
|
|
2019-03-05 13:42:25 +03:00
|
|
|
ignoredEdges := r.IgnoredEdges
|
|
|
|
if ignoredEdges == nil {
|
|
|
|
ignoredEdges = make(map[EdgeLocator]struct{})
|
|
|
|
}
|
|
|
|
ignoredNodes := r.IgnoredNodes
|
|
|
|
if ignoredNodes == nil {
|
|
|
|
ignoredNodes = make(map[Vertex]struct{})
|
|
|
|
}
|
|
|
|
|
2018-03-27 07:14:10 +03:00
|
|
|
// processEdge is a helper closure that will be used to make sure edges
|
|
|
|
// satisfy our specific requirements.
|
2018-06-04 23:10:05 +03:00
|
|
|
processEdge := func(fromNode *channeldb.LightningNode,
|
|
|
|
edge *channeldb.ChannelEdgePolicy,
|
|
|
|
bandwidth lnwire.MilliSatoshi, toNode Vertex) {
|
2018-03-27 07:14:10 +03:00
|
|
|
|
2018-06-04 23:10:05 +03:00
|
|
|
fromVertex := Vertex(fromNode.PubKeyBytes)
|
2018-03-27 07:14:10 +03:00
|
|
|
|
2018-10-26 21:41:55 +03:00
|
|
|
// If this is not a local channel and it is disabled, we will
|
|
|
|
// skip it.
|
|
|
|
// TODO(halseth): also ignore disable flags for non-local
|
|
|
|
// channels if bandwidth hint is set?
|
2019-03-05 18:55:19 +03:00
|
|
|
isSourceChan := fromVertex == source
|
2019-02-01 15:53:27 +03:00
|
|
|
|
2019-01-12 20:59:43 +03:00
|
|
|
edgeFlags := edge.ChannelFlags
|
2018-10-26 21:41:55 +03:00
|
|
|
isDisabled := edgeFlags&lnwire.ChanUpdateDisabled != 0
|
|
|
|
|
|
|
|
if !isSourceChan && isDisabled {
|
2018-03-27 07:14:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-01 15:53:27 +03:00
|
|
|
// If we have an outgoing channel restriction and this is not
|
|
|
|
// the specified channel, skip it.
|
2019-03-05 13:13:44 +03:00
|
|
|
if isSourceChan && r.OutgoingChannelID != nil &&
|
|
|
|
*r.OutgoingChannelID != edge.ChannelID {
|
2019-02-01 15:53:27 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-10 06:47:56 +03:00
|
|
|
// If this vertex or edge has been black listed, then we'll
|
|
|
|
// skip exploring this edge.
|
2019-03-05 13:42:25 +03:00
|
|
|
if _, ok := ignoredNodes[fromVertex]; ok {
|
2018-03-27 07:14:10 +03:00
|
|
|
return
|
|
|
|
}
|
2018-11-29 18:48:17 +03:00
|
|
|
|
|
|
|
locator := newEdgeLocator(edge)
|
2019-03-05 13:42:25 +03:00
|
|
|
if _, ok := ignoredEdges[*locator]; ok {
|
2018-03-27 07:14:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-04 23:10:05 +03:00
|
|
|
toNodeDist := distance[toNode]
|
|
|
|
|
|
|
|
amountToSend := toNodeDist.amountToReceive
|
|
|
|
|
2019-02-11 08:18:34 +03:00
|
|
|
// If the estimated bandwidth of the channel edge is not able
|
2018-06-04 23:10:05 +03:00
|
|
|
// to carry the amount that needs to be send, return.
|
|
|
|
if bandwidth < amountToSend {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-10 06:47:56 +03:00
|
|
|
// If the amountToSend is less than the minimum required
|
|
|
|
// amount, return.
|
2018-06-04 23:10:05 +03:00
|
|
|
if amountToSend < edge.MinHTLC {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-09 05:32:49 +03:00
|
|
|
// If this edge was constructed from a hop hint, we won't have access to
|
|
|
|
// its max HTLC. Therefore, only consider discarding this edge here if
|
|
|
|
// the field is set.
|
|
|
|
if edge.MaxHTLC != 0 && edge.MaxHTLC < amountToSend {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-04 23:10:05 +03:00
|
|
|
// Compute fee that fromNode is charging. It is based on the
|
|
|
|
// amount that needs to be sent to the next node in the route.
|
|
|
|
//
|
2018-09-06 11:48:46 +03:00
|
|
|
// Source node has no predecessor to pay a fee. Therefore set
|
2018-08-10 06:47:56 +03:00
|
|
|
// fee to zero, because it should not be included in the fee
|
|
|
|
// limit check and edge weight.
|
2018-06-04 23:10:05 +03:00
|
|
|
//
|
2018-08-10 06:47:56 +03:00
|
|
|
// Also determine the time lock delta that will be added to the
|
|
|
|
// route if fromNode is selected. If fromNode is the source
|
|
|
|
// node, no additional timelock is required.
|
2018-06-04 23:10:05 +03:00
|
|
|
var fee lnwire.MilliSatoshi
|
|
|
|
var timeLockDelta uint16
|
2019-03-05 18:55:19 +03:00
|
|
|
if fromVertex != source {
|
2018-06-04 23:10:05 +03:00
|
|
|
fee = computeFee(amountToSend, edge)
|
|
|
|
timeLockDelta = edge.TimeLockDelta
|
|
|
|
}
|
|
|
|
|
2018-08-10 06:47:56 +03:00
|
|
|
// amountToReceive is the amount that the node that is added to
|
|
|
|
// the distance map needs to receive from a (to be found)
|
|
|
|
// previous node in the route. That previous node will need to
|
|
|
|
// pay the amount that this node forwards plus the fee it
|
|
|
|
// charges.
|
2018-06-04 23:10:05 +03:00
|
|
|
amountToReceive := amountToSend + fee
|
|
|
|
|
2018-08-10 06:47:56 +03:00
|
|
|
// Check if accumulated fees would exceed fee limit when this
|
|
|
|
// node would be added to the path.
|
2018-06-04 23:10:05 +03:00
|
|
|
totalFee := amountToReceive - amt
|
2019-03-05 13:13:44 +03:00
|
|
|
if totalFee > r.FeeLimit {
|
2018-06-04 23:10:05 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// By adding fromNode in the route, there will be an extra
|
|
|
|
// weight composed of the fee that this node will charge and
|
2018-08-10 06:47:56 +03:00
|
|
|
// the amount that will be locked for timeLockDelta blocks in
|
|
|
|
// the HTLC that is handed out to fromNode.
|
2018-06-04 23:10:05 +03:00
|
|
|
weight := edgeWeight(amountToReceive, fee, timeLockDelta)
|
|
|
|
|
2018-08-10 06:47:56 +03:00
|
|
|
// Compute the tentative distance to this new channel/edge
|
|
|
|
// which is the distance from our toNode to the target node
|
|
|
|
// plus the weight of this edge.
|
2018-06-04 23:10:05 +03:00
|
|
|
tempDist := toNodeDist.dist + weight
|
2018-03-27 07:14:10 +03:00
|
|
|
|
2018-06-04 23:10:05 +03:00
|
|
|
// If this new tentative distance is not better than the current
|
|
|
|
// best known distance to this node, return.
|
|
|
|
if tempDist >= distance[fromVertex].dist {
|
|
|
|
return
|
|
|
|
}
|
2018-03-27 07:14:10 +03:00
|
|
|
|
2018-06-04 23:10:05 +03:00
|
|
|
// If the edge has no time lock delta, the payment will always
|
|
|
|
// fail, so return.
|
|
|
|
//
|
|
|
|
// TODO(joostjager): Is this really true? Can't it be that
|
|
|
|
// nodes take this risk in exchange for a extraordinary high
|
|
|
|
// fee?
|
|
|
|
if edge.TimeLockDelta == 0 {
|
|
|
|
return
|
2018-03-27 07:14:10 +03:00
|
|
|
}
|
2018-06-04 23:10:05 +03:00
|
|
|
|
|
|
|
// All conditions are met and this new tentative distance is
|
|
|
|
// better than the current best known distance to this node.
|
2018-08-10 06:47:56 +03:00
|
|
|
// The new better distance is recorded, and also our "next hop"
|
|
|
|
// map is populated with this edge.
|
2018-06-04 23:10:05 +03:00
|
|
|
distance[fromVertex] = nodeWithDist{
|
|
|
|
dist: tempDist,
|
|
|
|
node: fromNode,
|
|
|
|
amountToReceive: amountToReceive,
|
|
|
|
fee: fee,
|
|
|
|
}
|
|
|
|
|
2018-08-09 16:36:28 +03:00
|
|
|
next[fromVertex] = edge
|
2018-06-04 23:10:05 +03:00
|
|
|
|
|
|
|
// Add this new node to our heap as we'd like to further
|
|
|
|
// explore backwards through this edge.
|
|
|
|
heap.Push(&nodeHeap, distance[fromVertex])
|
2018-03-27 07:14:10 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 07:39:54 +03:00
|
|
|
// TODO(roasbeef): also add path caching
|
|
|
|
// * similar to route caching, but doesn't factor in the amount
|
|
|
|
|
2018-06-04 23:10:05 +03:00
|
|
|
// To start, our target node will the sole item within our distance
|
2017-03-20 00:15:58 +03:00
|
|
|
// heap.
|
2019-03-05 18:55:19 +03:00
|
|
|
heap.Push(&nodeHeap, distance[target])
|
2017-03-20 00:15:58 +03:00
|
|
|
|
|
|
|
for nodeHeap.Len() != 0 {
|
|
|
|
// Fetch the node within the smallest distance from our source
|
|
|
|
// from the heap.
|
2017-03-21 04:15:50 +03:00
|
|
|
partialPath := heap.Pop(&nodeHeap).(nodeWithDist)
|
|
|
|
bestNode := partialPath.node
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
|
2018-06-04 23:10:05 +03:00
|
|
|
// If we've reached our source (or we don't have any incoming
|
2017-03-09 01:24:59 +03:00
|
|
|
// edges), then we're done here and can exit the graph
|
|
|
|
// traversal early.
|
2019-03-05 18:55:19 +03:00
|
|
|
if bestNode.PubKeyBytes == source {
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we've found the next potential step to take we'll
|
2018-06-04 23:10:05 +03:00
|
|
|
// examine all the incoming edges (channels) from this node to
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
// further our graph traversal.
|
2018-01-31 07:26:26 +03:00
|
|
|
pivot := Vertex(bestNode.PubKeyBytes)
|
2018-11-30 07:04:21 +03:00
|
|
|
err := bestNode.ForEachChannel(tx, func(tx *bbolt.Tx,
|
2017-04-14 23:14:54 +03:00
|
|
|
edgeInfo *channeldb.ChannelEdgeInfo,
|
2018-06-04 23:10:05 +03:00
|
|
|
_, inEdge *channeldb.ChannelEdgePolicy) error {
|
|
|
|
|
|
|
|
// If there is no edge policy for this candidate
|
|
|
|
// node, skip. Note that we are searching backwards
|
|
|
|
// so this node would have come prior to the pivot
|
|
|
|
// node in the route.
|
|
|
|
if inEdge == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-09 01:24:59 +03:00
|
|
|
|
2018-05-08 07:04:31 +03:00
|
|
|
// We'll query the lower layer to see if we can obtain
|
|
|
|
// any more up to date information concerning the
|
|
|
|
// bandwidth of this edge.
|
2018-10-25 00:06:12 +03:00
|
|
|
edgeBandwidth, ok := g.bandwidthHints[edgeInfo.ChannelID]
|
2018-05-08 07:04:31 +03:00
|
|
|
if !ok {
|
|
|
|
// If we don't have a hint for this edge, then
|
|
|
|
// we'll just use the known Capacity as the
|
|
|
|
// available bandwidth.
|
|
|
|
edgeBandwidth = lnwire.NewMSatFromSatoshis(
|
|
|
|
edgeInfo.Capacity,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-08-10 06:47:56 +03:00
|
|
|
// Before we can process the edge, we'll need to fetch
|
|
|
|
// the node on the _other_ end of this channel as we
|
|
|
|
// may later need to iterate over the incoming edges of
|
|
|
|
// this node if we explore it further.
|
|
|
|
channelSource, err := edgeInfo.FetchOtherNode(
|
|
|
|
tx, pivot[:],
|
|
|
|
)
|
2018-06-04 23:10:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-10 06:47:56 +03:00
|
|
|
// Check if this candidate node is better than what we
|
|
|
|
// already have.
|
2018-06-04 23:10:05 +03:00
|
|
|
processEdge(channelSource, inEdge, edgeBandwidth, pivot)
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-03-27 07:14:10 +03:00
|
|
|
|
|
|
|
// Then, we'll examine all the additional edges from the node
|
|
|
|
// we're currently visiting. Since we don't know the capacity
|
|
|
|
// of the private channel, we'll assume it was selected as a
|
|
|
|
// routing hint due to having enough capacity for the payment
|
|
|
|
// and use the payment amount as its capacity.
|
2018-06-04 23:10:05 +03:00
|
|
|
bandWidth := partialPath.amountToReceive
|
|
|
|
for _, reverseEdge := range additionalEdgesWithSrc[bestNode.PubKeyBytes] {
|
2018-10-25 00:06:12 +03:00
|
|
|
processEdge(reverseEdge.sourceNode, reverseEdge.edge,
|
|
|
|
bandWidth, pivot)
|
2018-03-27 07:14:10 +03:00
|
|
|
}
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
}
|
|
|
|
|
2018-06-04 23:10:05 +03:00
|
|
|
// If the source node isn't found in the next hop map, then a path
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
// doesn't exist, so we terminate in an error.
|
2019-03-05 18:55:19 +03:00
|
|
|
if _, ok := next[source]; !ok {
|
2017-03-19 21:40:25 +03:00
|
|
|
return nil, newErrf(ErrNoPathFound, "unable to find a path to "+
|
|
|
|
"destination")
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
}
|
|
|
|
|
2018-10-25 00:06:12 +03:00
|
|
|
// Use the nextHop map to unravel the forward path from source to
|
|
|
|
// target.
|
2018-08-09 16:36:28 +03:00
|
|
|
pathEdges := make([]*channeldb.ChannelEdgePolicy, 0, len(next))
|
2019-03-05 18:55:19 +03:00
|
|
|
currentNode := source
|
|
|
|
for currentNode != target { // TODO(roasbeef): assumes no cycles
|
2018-06-04 23:10:05 +03:00
|
|
|
// Determine the next hop forward using the next map.
|
|
|
|
nextNode := next[currentNode]
|
|
|
|
|
|
|
|
// Add the next hop to the list of path edges.
|
|
|
|
pathEdges = append(pathEdges, nextNode)
|
|
|
|
|
|
|
|
// Advance current node.
|
|
|
|
currentNode = Vertex(nextNode.Node.PubKeyBytes)
|
2017-03-20 01:15:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The route is invalid if it spans more than 20 hops. The current
|
|
|
|
// Sphinx (onion routing) implementation can only encode up to 20 hops
|
|
|
|
// as the entire packet is fixed size. If this route is more than 20
|
|
|
|
// hops, then it's invalid.
|
2017-03-21 04:15:50 +03:00
|
|
|
numEdges := len(pathEdges)
|
|
|
|
if numEdges > HopLimit {
|
2017-03-19 21:40:25 +03:00
|
|
|
return nil, newErr(ErrMaxHopsExceeded, "potential path has "+
|
|
|
|
"too many hops")
|
2017-03-20 01:15:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return pathEdges, nil
|
|
|
|
}
|
2017-03-21 04:22:04 +03:00
|
|
|
|
|
|
|
// findPaths implements a k-shortest paths algorithm to find all the reachable
|
|
|
|
// paths between the passed source and target. The algorithm will continue to
|
|
|
|
// traverse the graph until all possible candidate paths have been depleted.
|
|
|
|
// This function implements a modified version of Yen's. To find each path
|
|
|
|
// itself, we utilize our modified version of Dijkstra's found above. When
|
|
|
|
// examining possible spur and root paths, rather than removing edges or
|
2017-09-08 04:25:43 +03:00
|
|
|
// Vertexes from the graph, we instead utilize a Vertex+edge black-list that
|
2017-03-21 04:22:04 +03:00
|
|
|
// will be ignored by our modified Dijkstra's algorithm. With this approach, we
|
|
|
|
// make our inner path finding algorithm aware of our k-shortest paths
|
|
|
|
// algorithm, rather than attempting to use an unmodified path finding
|
|
|
|
// algorithm in a block box manner.
|
2018-11-30 07:04:21 +03:00
|
|
|
func findPaths(tx *bbolt.Tx, graph *channeldb.ChannelGraph,
|
2019-03-05 18:55:19 +03:00
|
|
|
source, target Vertex, amt lnwire.MilliSatoshi,
|
|
|
|
restrictions *RestrictParams, numPaths uint32,
|
|
|
|
bandwidthHints map[uint64]lnwire.MilliSatoshi) (
|
|
|
|
[][]*channeldb.ChannelEdgePolicy, error) {
|
2017-03-21 04:22:04 +03:00
|
|
|
|
|
|
|
// TODO(roasbeef): modifying ordering within heap to eliminate final
|
|
|
|
// sorting step?
|
|
|
|
var (
|
2018-08-09 16:36:28 +03:00
|
|
|
shortestPaths [][]*channeldb.ChannelEdgePolicy
|
2017-03-21 04:22:04 +03:00
|
|
|
candidatePaths pathHeap
|
|
|
|
)
|
|
|
|
|
|
|
|
// First we'll find a single shortest path from the source (our
|
|
|
|
// selfNode) to the target destination that's capable of carrying amt
|
|
|
|
// satoshis along the path before fees are calculated.
|
2018-02-13 03:18:44 +03:00
|
|
|
startingPath, err := findPath(
|
2018-10-25 00:06:12 +03:00
|
|
|
&graphParams{
|
|
|
|
tx: tx,
|
|
|
|
graph: graph,
|
|
|
|
bandwidthHints: bandwidthHints,
|
|
|
|
},
|
2019-03-05 14:42:29 +03:00
|
|
|
restrictions, source, target, amt,
|
2018-02-13 03:18:44 +03:00
|
|
|
)
|
2017-03-21 04:22:04 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Unable to find path: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manually insert a "self" edge emanating from ourselves. This
|
|
|
|
// self-edge is required in order for the path finding algorithm to
|
|
|
|
// function properly.
|
2018-08-09 16:36:28 +03:00
|
|
|
firstPath := make([]*channeldb.ChannelEdgePolicy, 0, len(startingPath)+1)
|
|
|
|
firstPath = append(firstPath, &channeldb.ChannelEdgePolicy{
|
2019-03-05 18:55:19 +03:00
|
|
|
Node: &channeldb.LightningNode{PubKeyBytes: source},
|
2017-03-21 04:22:04 +03:00
|
|
|
})
|
|
|
|
firstPath = append(firstPath, startingPath...)
|
|
|
|
|
|
|
|
shortestPaths = append(shortestPaths, firstPath)
|
|
|
|
|
|
|
|
// While we still have candidate paths to explore we'll keep exploring
|
|
|
|
// the sub-graphs created to find the next k-th shortest path.
|
2018-02-13 03:18:44 +03:00
|
|
|
for k := uint32(1); k < numPaths; k++ {
|
2017-03-21 04:22:04 +03:00
|
|
|
prevShortest := shortestPaths[k-1]
|
|
|
|
|
|
|
|
// We'll examine each edge in the previous iteration's shortest
|
|
|
|
// path in order to find path deviations from each node in the
|
|
|
|
// path.
|
|
|
|
for i := 0; i < len(prevShortest)-1; i++ {
|
2017-09-08 04:25:43 +03:00
|
|
|
// These two maps will mark the edges and Vertexes
|
2017-03-21 04:22:04 +03:00
|
|
|
// we'll exclude from the next path finding attempt.
|
|
|
|
// These are required to ensure the paths are unique
|
|
|
|
// and loopless.
|
2019-03-05 13:42:25 +03:00
|
|
|
ignoredEdges := make(map[EdgeLocator]struct{})
|
|
|
|
ignoredVertexes := make(map[Vertex]struct{})
|
2017-03-21 04:22:04 +03:00
|
|
|
|
2019-03-05 14:42:29 +03:00
|
|
|
for e := range restrictions.IgnoredEdges {
|
|
|
|
ignoredEdges[e] = struct{}{}
|
|
|
|
}
|
|
|
|
for n := range restrictions.IgnoredNodes {
|
|
|
|
ignoredVertexes[n] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2017-03-21 04:22:04 +03:00
|
|
|
// Our spur node is the i-th node in the prior shortest
|
|
|
|
// path, and our root path will be all nodes in the
|
|
|
|
// path leading up to our spurNode.
|
|
|
|
spurNode := prevShortest[i].Node
|
|
|
|
rootPath := prevShortest[:i+1]
|
|
|
|
|
|
|
|
// Before we kickoff our next path finding iteration,
|
|
|
|
// we'll find all the edges we need to ignore in this
|
2018-02-13 03:18:44 +03:00
|
|
|
// next round. This ensures that we create a new unique
|
|
|
|
// path.
|
2017-03-21 04:22:04 +03:00
|
|
|
for _, path := range shortestPaths {
|
|
|
|
// If our current rootPath is a prefix of this
|
2017-04-14 00:44:53 +03:00
|
|
|
// shortest path, then we'll remove the edge
|
2017-03-21 04:22:04 +03:00
|
|
|
// directly _after_ our spur node from the
|
|
|
|
// graph so we don't repeat paths.
|
2018-11-29 18:48:17 +03:00
|
|
|
if len(path) > i+1 &&
|
|
|
|
isSamePath(rootPath, path[:i+1]) {
|
|
|
|
|
|
|
|
locator := newEdgeLocator(path[i+1])
|
|
|
|
ignoredEdges[*locator] = struct{}{}
|
2017-03-21 04:22:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next we'll remove all entries in the root path that
|
2018-02-13 03:18:44 +03:00
|
|
|
// aren't the current spur node from the graph. This
|
|
|
|
// ensures we don't create a path with loops.
|
2017-03-21 04:22:04 +03:00
|
|
|
for _, hop := range rootPath {
|
2018-01-31 07:26:26 +03:00
|
|
|
node := hop.Node.PubKeyBytes
|
|
|
|
if node == spurNode.PubKeyBytes {
|
2017-03-21 04:22:04 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-01-31 07:26:26 +03:00
|
|
|
ignoredVertexes[Vertex(node)] = struct{}{}
|
2017-03-21 04:22:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// With the edges that are part of our root path, and
|
2017-09-08 04:25:43 +03:00
|
|
|
// the Vertexes (other than the spur path) within the
|
2017-03-21 04:22:04 +03:00
|
|
|
// root path removed, we'll attempt to find another
|
|
|
|
// shortest path from the spur node to the destination.
|
2019-03-05 12:30:53 +03:00
|
|
|
//
|
|
|
|
// TODO: Fee limit passed to spur path finding isn't
|
|
|
|
// correct, because it doesn't take into account the
|
|
|
|
// fees already paid on the root path.
|
2019-03-05 14:42:29 +03:00
|
|
|
//
|
|
|
|
// TODO: Outgoing channel restriction isn't obeyed for
|
|
|
|
// spur paths.
|
|
|
|
spurRestrictions := &RestrictParams{
|
|
|
|
IgnoredEdges: ignoredEdges,
|
|
|
|
IgnoredNodes: ignoredVertexes,
|
|
|
|
FeeLimit: restrictions.FeeLimit,
|
|
|
|
}
|
|
|
|
|
2018-02-13 03:18:44 +03:00
|
|
|
spurPath, err := findPath(
|
2018-10-25 00:06:12 +03:00
|
|
|
&graphParams{
|
|
|
|
tx: tx,
|
|
|
|
graph: graph,
|
|
|
|
bandwidthHints: bandwidthHints,
|
|
|
|
},
|
2019-03-05 18:55:19 +03:00
|
|
|
spurRestrictions, spurNode.PubKeyBytes,
|
|
|
|
target, amt,
|
2018-02-13 03:18:44 +03:00
|
|
|
)
|
2017-03-21 04:22:04 +03:00
|
|
|
|
|
|
|
// If we weren't able to find a path, we'll continue to
|
|
|
|
// the next round.
|
2017-03-19 21:40:25 +03:00
|
|
|
if IsError(err, ErrNoPathFound) {
|
2017-03-21 04:22:04 +03:00
|
|
|
continue
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the new combined path by concatenating the
|
|
|
|
// rootPath to the spurPath.
|
2017-04-14 00:48:11 +03:00
|
|
|
newPathLen := len(rootPath) + len(spurPath)
|
|
|
|
newPath := path{
|
2018-08-09 16:36:28 +03:00
|
|
|
hops: make([]*channeldb.ChannelEdgePolicy, 0, newPathLen),
|
2017-04-14 00:48:11 +03:00
|
|
|
dist: newPathLen,
|
|
|
|
}
|
|
|
|
newPath.hops = append(newPath.hops, rootPath...)
|
|
|
|
newPath.hops = append(newPath.hops, spurPath...)
|
2017-03-21 04:22:04 +03:00
|
|
|
|
2017-08-16 05:56:28 +03:00
|
|
|
// TODO(roasbeef): add and consult path finger print
|
|
|
|
|
2017-03-21 04:22:04 +03:00
|
|
|
// We'll now add this newPath to the heap of candidate
|
|
|
|
// shortest paths.
|
2017-04-14 00:48:11 +03:00
|
|
|
heap.Push(&candidatePaths, newPath)
|
2017-03-21 04:22:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If our min-heap of candidate paths is empty, then we can
|
|
|
|
// exit early.
|
|
|
|
if candidatePaths.Len() == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// To conclude this latest iteration, we'll take the shortest
|
|
|
|
// path in our set of candidate paths and add it to our
|
|
|
|
// shortestPaths list as the *next* shortest path.
|
|
|
|
nextShortestPath := heap.Pop(&candidatePaths).(path).hops
|
|
|
|
shortestPaths = append(shortestPaths, nextShortestPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return shortestPaths, nil
|
routing: rewrite package to conform to BOLT07 and factor in fees+timelocks
This commit overhauls the routing package significantly to simplify the
code, conform to the rest of the coding style within the package, and
observe the new authenticated gossiping scheme outlined in BOLT07.
As a major step towards a more realistic path finding algorithm, fees
are properly calculated and observed during path finding. If a path has
sufficient capacity _before_ fees are applied, but afterwards the
finalized route would exceed the capacity of a single link, the route
is marked as invalid.
Currently a naive weighting algorithm is used which only factors in the
time-lock delta at each hop, thereby optimizing for the lowest time
lock. Fee calculation also isn’t finalized since we aren’t yet using
milli-satoshi throughout the daemon. The final TODO item within the PR
is to properly perform a multi-path search and rank the results based
on a summation heuristic rather than just return the first (out of
many) route found.
On the server side, once nodes are initially connected to the daemon,
our routing table will be synced with the peer’s using a naive “just
send everything scheme” to hold us over until I spec out some a
efficient graph reconciliation protocol. Additionally, the routing
table is now pruned by the channel router itself once new blocks arrive
rather than depending on peers to tell us when a channel flaps or is
closed.
Finally, the validation of peer announcements aren’t yet fully
implemented as they’ll be implemented within the pending discovery
package that was blocking on the completion of this package. Most off
the routing message processing will be moved out of this package and
into the discovery package where full validation will be carried out.
2016-12-27 08:20:26 +03:00
|
|
|
}
|