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 (
|
2018-01-31 07:26:26 +03:00
|
|
|
"bytes"
|
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-03-11 06:00:57 +03:00
|
|
|
"github.com/coreos/bbolt"
|
2017-06-16 23:37:36 +03:00
|
|
|
"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
|
|
|
"github.com/roasbeef/btcd/btcec"
|
2017-03-09 01:24:59 +03:00
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
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/roasbeef/btcutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
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-03-21 04:15:50 +03:00
|
|
|
// ChannelHop is an intermediate hop within the network with a greater
|
|
|
|
// multi-hop payment route. This struct contains the relevant routing policy of
|
|
|
|
// the particular edge, as well as the total capacity, and origin chain of the
|
|
|
|
// channel itself.
|
|
|
|
type ChannelHop struct {
|
|
|
|
// Capacity is the total capacity of the channel being traversed. This
|
|
|
|
// value is expressed for stability in satoshis.
|
|
|
|
Capacity btcutil.Amount
|
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-03-21 04:15:50 +03:00
|
|
|
// Chain is a 32-byte has that denotes the base blockchain network of
|
|
|
|
// the channel. The 32-byte hash is the "genesis" block of the
|
|
|
|
// blockchain, or the very first block in the chain.
|
|
|
|
//
|
|
|
|
// TODO(roasbeef): store chain within edge info/policy in database.
|
|
|
|
Chain chainhash.Hash
|
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-03-21 04:15:50 +03:00
|
|
|
*channeldb.ChannelEdgePolicy
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Hop represents the forwarding details at a particular position within the
|
|
|
|
// final route. This struct houses the values necessary to create the HTLC
|
|
|
|
// which will travel along this hop, and also encode the per-hop payload
|
|
|
|
// included within the Sphinx packet.
|
|
|
|
type Hop struct {
|
2017-03-09 01:24:59 +03:00
|
|
|
// Channel is the active payment channel edge that this hop will travel
|
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
|
|
|
// along.
|
2017-03-09 01:24:59 +03:00
|
|
|
Channel *ChannelHop
|
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
|
|
|
|
|
|
|
// Fee is the total fee that this hop will subtract from the incoming
|
|
|
|
// payment, this difference nets the hop fees for forwarding the
|
|
|
|
// payment.
|
2017-08-22 09:43:20 +03:00
|
|
|
Fee 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
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func isSamePath(path1, path2 []*ChannelHop) 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
|
|
|
|
|
|
|
// Hops contains details concerning the specific forwarding details at
|
|
|
|
// each hop.
|
|
|
|
Hops []*Hop
|
2017-10-11 05:41:47 +03:00
|
|
|
|
|
|
|
// nodeIndex is a map that allows callers to quickly look up if a node
|
|
|
|
// is present in this computed route or not.
|
2017-09-08 04:25:43 +03:00
|
|
|
nodeIndex map[Vertex]struct{}
|
2017-10-11 05:41:47 +03:00
|
|
|
|
|
|
|
// chanIndex is an index that allows callers to determine if a channel
|
|
|
|
// is present in this route or not. Channels are identified by the
|
|
|
|
// uint64 version of the short channel ID.
|
|
|
|
chanIndex map[uint64]struct{}
|
|
|
|
|
|
|
|
// nextHop maps a node, to the next channel that it will pass the HTLC
|
|
|
|
// off to. With this map, we can easily look up the next outgoing
|
|
|
|
// channel or node for pruning purposes.
|
2017-09-08 04:25:43 +03:00
|
|
|
nextHopMap map[Vertex]*ChannelHop
|
2018-01-11 02:15:49 +03:00
|
|
|
|
|
|
|
// prevHop maps a node, to the channel that was directly before it
|
|
|
|
// within the route. With this map, we can easily look up the previous
|
|
|
|
// channel or node for pruning purposes.
|
|
|
|
prevHopMap map[Vertex]*ChannelHop
|
2017-10-11 05:41:47 +03:00
|
|
|
}
|
|
|
|
|
2017-09-08 04:25:43 +03:00
|
|
|
// nextHopVertex returns the next hop (by Vertex) after the target node. If the
|
2017-10-11 05:41:47 +03:00
|
|
|
// target node is not found in the route, then false is returned.
|
2017-09-08 04:25:43 +03:00
|
|
|
func (r *Route) nextHopVertex(n *btcec.PublicKey) (Vertex, bool) {
|
|
|
|
hop, ok := r.nextHopMap[NewVertex(n)]
|
2018-01-31 07:26:26 +03:00
|
|
|
return Vertex(hop.Node.PubKeyBytes), ok
|
2017-10-11 05:41:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// nextHopChannel returns the uint64 channel ID of the next hop after the
|
2018-01-11 02:15:49 +03:00
|
|
|
// target node. If the target node is not found in the route, then false is
|
2017-10-11 05:41:47 +03:00
|
|
|
// returned.
|
2018-01-11 02:15:49 +03:00
|
|
|
func (r *Route) nextHopChannel(n *btcec.PublicKey) (*ChannelHop, bool) {
|
2017-09-08 04:25:43 +03:00
|
|
|
hop, ok := r.nextHopMap[NewVertex(n)]
|
2018-01-11 02:15:49 +03:00
|
|
|
return hop, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// prevHopChannel returns the uint64 channel ID of the before hop after the
|
|
|
|
// target node. If the target node is not found in the route, then false is
|
|
|
|
// returned.
|
|
|
|
func (r *Route) prevHopChannel(n *btcec.PublicKey) (*ChannelHop, bool) {
|
|
|
|
hop, ok := r.prevHopMap[NewVertex(n)]
|
|
|
|
return hop, ok
|
2017-10-11 05:41:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// containsNode returns true if a node is present in the target route, and
|
|
|
|
// false otherwise.
|
2017-09-08 04:25:43 +03:00
|
|
|
func (r *Route) containsNode(v Vertex) bool {
|
2017-10-11 05:41:47 +03:00
|
|
|
_, ok := r.nodeIndex[v]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// containsChannel returns true if a channel is present in the target route,
|
|
|
|
// and false otherwise. The passed chanID should be the converted uint64 form
|
|
|
|
// of lnwire.ShortChannelID.
|
|
|
|
func (r *Route) containsChannel(chanID uint64) bool {
|
|
|
|
_, ok := r.chanIndex[chanID]
|
|
|
|
return ok
|
2017-03-21 04:15:50 +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 {
|
|
|
|
nextHop = r.Hops[i+1].Channel.ChannelID
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2017-09-08 04:25:43 +03:00
|
|
|
func newRoute(amtToSend lnwire.MilliSatoshi, sourceVertex Vertex,
|
2017-10-19 07:43:53 +03:00
|
|
|
pathEdges []*ChannelHop, currentHeight uint32,
|
|
|
|
finalCLTVDelta uint16) (*Route, error) {
|
2017-08-03 07:01:49 +03:00
|
|
|
|
|
|
|
// First, we'll create a new empty route with enough hops to match the
|
|
|
|
// amount of path edges. We set the TotalTimeLock to the current block
|
|
|
|
// height, as this is the basis that all of the time locks will be
|
|
|
|
// calculated from.
|
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
|
|
|
route := &Route{
|
2017-08-03 07:01:49 +03:00
|
|
|
Hops: make([]*Hop, len(pathEdges)),
|
|
|
|
TotalTimeLock: currentHeight,
|
2017-09-08 04:25:43 +03:00
|
|
|
nodeIndex: make(map[Vertex]struct{}),
|
2017-10-11 05:41:47 +03:00
|
|
|
chanIndex: make(map[uint64]struct{}),
|
2017-09-08 04:25:43 +03:00
|
|
|
nextHopMap: make(map[Vertex]*ChannelHop),
|
2018-01-11 02:15:49 +03:00
|
|
|
prevHopMap: make(map[Vertex]*ChannelHop),
|
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-04-17 01:24:11 +03:00
|
|
|
// TODO(roasbeef): need to do sanity check to ensure we don't make a
|
|
|
|
// "dust" payment: over x% of money sending to fees
|
|
|
|
|
2017-10-11 05:45:09 +03:00
|
|
|
// We'll populate the next hop map for the _source_ node with the
|
|
|
|
// information for the first hop so the mapping is sound.
|
|
|
|
route.nextHopMap[sourceVertex] = pathEdges[0]
|
|
|
|
|
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
|
|
|
// The running amount is the total amount of satoshis required at this
|
|
|
|
// point in the route. We start this value at the amount we want to
|
|
|
|
// send to the destination. This value will then get successively
|
|
|
|
// larger as we compute the fees going backwards.
|
|
|
|
runningAmt := amtToSend
|
|
|
|
pathLength := len(pathEdges)
|
2017-03-21 04:11:22 +03:00
|
|
|
for i := pathLength - 1; i >= 0; i-- {
|
|
|
|
edge := pathEdges[i]
|
|
|
|
|
2017-10-25 04:27:29 +03:00
|
|
|
// First, we'll update both the node and channel index, to
|
2017-09-08 04:25:43 +03:00
|
|
|
// indicate that this Vertex, and outgoing channel link are
|
2017-10-11 05:41:47 +03:00
|
|
|
// present within this route.
|
2018-01-31 07:26:26 +03:00
|
|
|
v := Vertex(edge.Node.PubKeyBytes)
|
2017-10-11 05:41:47 +03:00
|
|
|
route.nodeIndex[v] = struct{}{}
|
|
|
|
route.chanIndex[edge.ChannelID] = struct{}{}
|
|
|
|
|
|
|
|
// If this isn't a direct payment, and this isn't the last hop
|
|
|
|
// in the route, then we'll also populate the nextHop map to
|
|
|
|
// allow easy route traversal by callers.
|
|
|
|
if len(pathEdges) > 1 && i != len(pathEdges)-1 {
|
|
|
|
route.nextHopMap[v] = route.Hops[i+1].Channel
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:27:29 +03:00
|
|
|
// Now we'll start to calculate the items within the per-hop
|
|
|
|
// payload for this current hop.
|
|
|
|
//
|
|
|
|
// If this is the last hop, then we send the exact amount and
|
|
|
|
// pay no fee, as we're paying directly to the receiver, and
|
|
|
|
// there're no additional hops.
|
|
|
|
amtToForward := runningAmt
|
|
|
|
fee := lnwire.MilliSatoshi(0)
|
|
|
|
|
|
|
|
// If this isn't the last hop, to add enough funds to pay for
|
|
|
|
// transit over the next link.
|
|
|
|
if i != len(pathEdges)-1 {
|
|
|
|
// We'll grab the edge policy and per-hop payload of
|
|
|
|
// the prior hop so we can calculate fees properly.
|
|
|
|
prevEdge := pathEdges[i+1]
|
|
|
|
prevHop := route.Hops[i+1]
|
|
|
|
|
|
|
|
// The fee for this hop, will be based on how much the
|
|
|
|
// prior hop carried, as we'll need to increase the
|
|
|
|
// amount of satoshis incoming into this hop to
|
|
|
|
// properly pay the required fees.
|
|
|
|
prevAmount := prevHop.AmtToForward
|
2018-02-13 03:27:30 +03:00
|
|
|
fee = computeFee(prevAmount, prevEdge.ChannelEdgePolicy)
|
2017-10-25 04:27:29 +03:00
|
|
|
|
|
|
|
// With the fee computed, we increment the total amount
|
|
|
|
// as we need to pay this fee. This value represents
|
|
|
|
// the amount of funds that will come _into_ this edge.
|
|
|
|
runningAmt += fee
|
|
|
|
|
|
|
|
// Otherwise, for a node to forward an HTLC, then
|
|
|
|
// following inequality most hold true:
|
|
|
|
//
|
|
|
|
// * amt_in - fee >= amt_to_forward
|
|
|
|
amtToForward = runningAmt - fee
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we create the hop struct for this point in the route.
|
|
|
|
// The amount to forward is the running amount, and we compute
|
|
|
|
// the required fee based on this amount.
|
|
|
|
nextHop := &Hop{
|
|
|
|
Channel: edge,
|
|
|
|
AmtToForward: amtToForward,
|
|
|
|
Fee: fee,
|
|
|
|
}
|
|
|
|
|
2017-12-07 05:45:45 +03:00
|
|
|
route.TotalFees += nextHop.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
|
|
|
// As a sanity check, we ensure that the selected channel has
|
|
|
|
// enough capacity to forward the required amount which
|
|
|
|
// includes the fee dictated at each hop.
|
2017-08-22 09:43:20 +03:00
|
|
|
if nextHop.AmtToForward.ToSatoshis() > nextHop.Channel.Capacity {
|
|
|
|
err := fmt.Sprintf("channel graph has insufficient "+
|
|
|
|
"capacity for the payment: need %v, have %v",
|
|
|
|
nextHop.AmtToForward.ToSatoshis(),
|
|
|
|
nextHop.Channel.Capacity)
|
|
|
|
|
|
|
|
return nil, newErrf(ErrInsufficientCapacity, err)
|
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.
|
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.
|
|
|
|
route.TotalTimeLock += uint32(finalCLTVDelta)
|
|
|
|
|
|
|
|
nextHop.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.
|
|
|
|
route.TotalTimeLock += uint32(edge.TimeLockDelta)
|
|
|
|
|
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).
|
2017-06-16 23:33:50 +03:00
|
|
|
nextHop.OutgoingTimeLock = route.TotalTimeLock -
|
|
|
|
uint32(edge.TimeLockDelta)
|
|
|
|
}
|
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-03-21 04:11:22 +03:00
|
|
|
route.Hops[i] = nextHop
|
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-01-11 02:15:49 +03:00
|
|
|
// We'll then make a second run through our route in order to set up
|
|
|
|
// our prev hop mapping.
|
|
|
|
for _, hop := range route.Hops {
|
2018-01-31 07:26:26 +03:00
|
|
|
vertex := Vertex(hop.Channel.Node.PubKeyBytes)
|
2018-01-11 02:15:49 +03:00
|
|
|
route.prevHopMap[vertex] = hop.Channel
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// The total amount required for this route will be the value the
|
|
|
|
// source extends to the first hop in the route.
|
|
|
|
route.TotalAmount = runningAmt
|
|
|
|
|
|
|
|
return route, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// edgeWithPrev is a helper struct used in path finding that couples an
|
|
|
|
// directional edge with the node's ID in the opposite direction.
|
|
|
|
type edgeWithPrev struct {
|
2017-03-09 01:24:59 +03:00
|
|
|
edge *ChannelHop
|
2018-01-31 07:26:26 +03:00
|
|
|
prevNode [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
|
|
|
}
|
|
|
|
|
|
|
|
// edgeWeight computes the weight of an edge. This value is used when searching
|
|
|
|
// for the shortest path within the channel graph between two nodes. Currently
|
2018-02-13 03:27:30 +03:00
|
|
|
// a component is just 1 + the cltv delta value required at this hop, this
|
|
|
|
// value should be tuned with experimental and empirical data. We'll also
|
|
|
|
// factor in the "pure fee" through this hop, using the square of this fee as
|
|
|
|
// part of the weighting. The goal here is to bias more heavily towards fee
|
|
|
|
// ranking, and fallback to a time-lock based value in the case of a fee tie.
|
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): compute robust weight metric
|
2018-02-13 03:27:30 +03:00
|
|
|
func edgeWeight(amt lnwire.MilliSatoshi, e *channeldb.ChannelEdgePolicy) int64 {
|
|
|
|
// First, we'll compute the "pure" fee through this hop. We say pure,
|
|
|
|
// as this may not be what's ultimately paid as fees are properly
|
|
|
|
// calculated backwards, while we're going in the reverse direction.
|
|
|
|
pureFee := computeFee(amt, e)
|
|
|
|
|
|
|
|
// We'll then square the fee itself in order to more heavily weight our
|
|
|
|
// edge selection to bias towards lower fees.
|
|
|
|
feeWeight := int64(pureFee * pureFee)
|
|
|
|
|
|
|
|
// The final component is then 1 plus the timelock delta.
|
|
|
|
timeWeight := int64(1 + e.TimeLockDelta)
|
|
|
|
|
|
|
|
// The final weighting is: fee^2 + time_lock_delta.
|
|
|
|
return feeWeight + timeWeight
|
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-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
|
2017-03-21 04:15:50 +03:00
|
|
|
// from the target to the source.
|
2017-10-11 07:39:54 +03:00
|
|
|
func findPath(tx *bolt.Tx, graph *channeldb.ChannelGraph,
|
|
|
|
sourceNode *channeldb.LightningNode, target *btcec.PublicKey,
|
2017-09-08 04:25:43 +03:00
|
|
|
ignoredNodes map[Vertex]struct{}, ignoredEdges map[uint64]struct{},
|
2017-10-11 07:39:54 +03:00
|
|
|
amt lnwire.MilliSatoshi) ([]*ChannelHop, error) {
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if tx == nil {
|
|
|
|
tx, err = graph.Database().Begin(false)
|
|
|
|
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
|
|
|
|
2017-09-08 04:25:43 +03:00
|
|
|
// For each node/Vertex the graph we create an entry in the distance
|
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
|
|
|
// map for the node set with a distance of "infinity". We also mark
|
|
|
|
// add the node to our set of unvisited nodes.
|
2017-09-08 04:25:43 +03:00
|
|
|
distance := make(map[Vertex]nodeWithDist)
|
2017-10-17 04:48:21 +03:00
|
|
|
if err := graph.ForEachNode(tx, func(_ *bolt.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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2017-03-20 00:32:52 +03:00
|
|
|
// To start, we add the source of our path finding attempt to the
|
|
|
|
// distance map with with a distance of 0. This indicates our starting
|
|
|
|
// point in the graph traversal.
|
2018-01-31 07:26:26 +03:00
|
|
|
sourceVertex := Vertex(sourceNode.PubKeyBytes)
|
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
|
|
|
distance[sourceVertex] = nodeWithDist{
|
|
|
|
dist: 0,
|
|
|
|
node: sourceNode,
|
|
|
|
}
|
|
|
|
|
2017-03-20 00:32:52 +03:00
|
|
|
// To start, our source node will the sole item within our distance
|
2017-03-20 00:15:58 +03:00
|
|
|
// heap.
|
|
|
|
heap.Push(&nodeHeap, distance[sourceVertex])
|
|
|
|
|
2018-01-31 07:26:26 +03:00
|
|
|
targetBytes := target.SerializeCompressed()
|
|
|
|
|
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
|
|
|
// We'll use this map as a series of "previous" hop pointers. So to get
|
2017-09-08 04:25:43 +03:00
|
|
|
// to `Vertex` we'll take the edge that it's mapped to within `prev`.
|
|
|
|
prev := make(map[Vertex]edgeWithPrev)
|
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
|
|
|
|
2017-03-09 01:24:59 +03:00
|
|
|
// If we've reached our target (or we don't have any outgoing
|
|
|
|
// edges), then we're done here and can exit the graph
|
|
|
|
// traversal early.
|
2018-01-31 07:26:26 +03:00
|
|
|
if bytes.Equal(bestNode.PubKeyBytes[:], targetBytes) {
|
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
|
|
|
|
// examine all the outgoing edge (channels) from this node to
|
|
|
|
// further our graph traversal.
|
2018-01-31 07:26:26 +03:00
|
|
|
pivot := Vertex(bestNode.PubKeyBytes)
|
2017-10-11 07:39:54 +03:00
|
|
|
err := bestNode.ForEachChannel(tx, func(tx *bolt.Tx,
|
2017-04-14 23:14:54 +03:00
|
|
|
edgeInfo *channeldb.ChannelEdgeInfo,
|
2017-08-22 09:47:02 +03:00
|
|
|
outEdge, inEdge *channeldb.ChannelEdgePolicy) error {
|
2017-03-09 01:24:59 +03:00
|
|
|
|
2018-01-31 07:26:26 +03:00
|
|
|
v := Vertex(outEdge.Node.PubKeyBytes)
|
2017-08-22 09:47:02 +03:00
|
|
|
|
2017-12-01 09:31:18 +03:00
|
|
|
// If the outgoing edge is currently disabled, then
|
|
|
|
// we'll stop here, as we shouldn't attempt to route
|
|
|
|
// through it.
|
|
|
|
edgeFlags := lnwire.ChanUpdateFlag(outEdge.Flags)
|
|
|
|
if edgeFlags&lnwire.ChanUpdateDisabled == lnwire.ChanUpdateDisabled {
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-21 04:15:50 +03:00
|
|
|
|
2017-09-08 04:25:43 +03:00
|
|
|
// If this Vertex or edge has been black listed, then
|
2017-03-21 04:15:50 +03:00
|
|
|
// we'll skip exploring this edge during this
|
|
|
|
// iteration.
|
|
|
|
if _, ok := ignoredNodes[v]; ok {
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-22 09:47:02 +03:00
|
|
|
if _, ok := ignoredEdges[outEdge.ChannelID]; ok {
|
2017-03-21 04:15:50 +03:00
|
|
|
return 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
|
|
|
// Compute the tentative distance to this new
|
|
|
|
// channel/edge which is the distance to our current
|
|
|
|
// pivot node plus the weight of this edge.
|
2018-02-13 03:27:30 +03:00
|
|
|
tempDist := distance[pivot].dist + edgeWeight(amt, outEdge)
|
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
|
|
|
|
|
|
|
// If this new tentative distance is better than the
|
|
|
|
// current best known distance to this node, then we
|
|
|
|
// record the new better distance, and also populate
|
2017-03-20 00:15:58 +03:00
|
|
|
// our "next hop" map with this edge. We'll also shave
|
|
|
|
// off irrelevant edges by adding the sufficient
|
2017-12-01 09:29:38 +03:00
|
|
|
// capacity of an edge and clearing their min-htlc
|
|
|
|
// amount to our relaxation condition.
|
2017-03-20 00:11:25 +03:00
|
|
|
if tempDist < distance[v].dist &&
|
2017-12-01 09:29:38 +03:00
|
|
|
edgeInfo.Capacity >= amt.ToSatoshis() &&
|
|
|
|
amt >= outEdge.MinHTLC {
|
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
|
|
|
distance[v] = nodeWithDist{
|
|
|
|
dist: tempDist,
|
2017-08-22 09:47:02 +03:00
|
|
|
node: outEdge.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
|
|
|
}
|
|
|
|
prev[v] = edgeWithPrev{
|
2017-08-22 09:47:02 +03:00
|
|
|
// We'll use the *incoming* edge here
|
|
|
|
// as we need to use the routing policy
|
|
|
|
// specified by the node this channel
|
|
|
|
// connects to.
|
2017-03-09 01:24:59 +03:00
|
|
|
edge: &ChannelHop{
|
2017-10-19 07:51:07 +03:00
|
|
|
ChannelEdgePolicy: outEdge,
|
2017-03-09 01:24:59 +03:00
|
|
|
Capacity: edgeInfo.Capacity,
|
|
|
|
},
|
2018-01-31 07:26:26 +03:00
|
|
|
prevNode: bestNode.PubKeyBytes,
|
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-03-20 00:15:58 +03:00
|
|
|
|
|
|
|
// Add this new node to our heap as we'd like
|
|
|
|
// to further explore down this edge.
|
|
|
|
heap.Push(&nodeHeap, distance[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
|
|
|
}
|
2017-10-11 07:39:54 +03:00
|
|
|
|
2017-12-01 09:29:38 +03:00
|
|
|
// TODO(roasbeef): return min HTLC as error in end?
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the target node isn't found in the prev hop map, then a path
|
|
|
|
// doesn't exist, so we terminate in an error.
|
2017-09-08 04:25:43 +03:00
|
|
|
if _, ok := prev[NewVertex(target)]; !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
|
|
|
}
|
|
|
|
|
2017-03-20 01:15:24 +03:00
|
|
|
// If the potential route if below the max hop limit, then we'll use
|
|
|
|
// the prevHop map to unravel the path. We end up with a list of edges
|
|
|
|
// in the reverse direction which we'll use to properly calculate the
|
|
|
|
// timelock and fee values.
|
|
|
|
pathEdges := make([]*ChannelHop, 0, len(prev))
|
2017-09-08 04:25:43 +03:00
|
|
|
prevNode := NewVertex(target)
|
2017-03-20 01:15:24 +03:00
|
|
|
for prevNode != sourceVertex { // TODO(roasbeef): assumes no cycles
|
|
|
|
// Add the current hop to the limit of path edges then walk
|
|
|
|
// backwards from this hop via the prev pointer for this hop
|
|
|
|
// within the prevHop map.
|
|
|
|
pathEdges = append(pathEdges, prev[prevNode].edge)
|
2017-03-21 04:15:50 +03:00
|
|
|
|
2018-01-31 07:26:26 +03:00
|
|
|
prevNode = Vertex(prev[prevNode].prevNode)
|
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
|
|
|
}
|
|
|
|
|
2017-03-21 04:15:50 +03:00
|
|
|
// As our traversal of the prev map above walked backwards from the
|
|
|
|
// target to the source in the route, we need to reverse it before
|
|
|
|
// returning the final route.
|
|
|
|
for i := 0; i < numEdges/2; i++ {
|
|
|
|
pathEdges[i], pathEdges[numEdges-i-1] = pathEdges[numEdges-i-1], pathEdges[i]
|
|
|
|
}
|
|
|
|
|
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.
|
2017-10-11 07:39:54 +03:00
|
|
|
func findPaths(tx *bolt.Tx, graph *channeldb.ChannelGraph,
|
|
|
|
source *channeldb.LightningNode, target *btcec.PublicKey,
|
2018-02-13 03:18:44 +03:00
|
|
|
amt lnwire.MilliSatoshi, numPaths uint32) ([][]*ChannelHop, error) {
|
2017-03-21 04:22:04 +03:00
|
|
|
|
|
|
|
ignoredEdges := make(map[uint64]struct{})
|
2017-09-08 04:25:43 +03:00
|
|
|
ignoredVertexes := make(map[Vertex]struct{})
|
2017-03-21 04:22:04 +03:00
|
|
|
|
|
|
|
// TODO(roasbeef): modifying ordering within heap to eliminate final
|
|
|
|
// sorting step?
|
|
|
|
var (
|
|
|
|
shortestPaths [][]*ChannelHop
|
|
|
|
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(
|
|
|
|
tx, graph, source, target, ignoredVertexes, ignoredEdges, amt,
|
|
|
|
)
|
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.
|
|
|
|
firstPath := make([]*ChannelHop, 0, len(startingPath)+1)
|
|
|
|
firstPath = append(firstPath, &ChannelHop{
|
|
|
|
ChannelEdgePolicy: &channeldb.ChannelEdgePolicy{
|
|
|
|
Node: source,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
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.
|
|
|
|
ignoredEdges = make(map[uint64]struct{})
|
2017-09-08 04:25:43 +03:00
|
|
|
ignoredVertexes = make(map[Vertex]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.
|
2017-04-14 00:44:53 +03:00
|
|
|
if len(path) > i+1 && isSamePath(rootPath, path[:i+1]) {
|
2017-03-21 04:22:04 +03:00
|
|
|
ignoredEdges[path[i+1].ChannelID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2018-02-13 03:18:44 +03:00
|
|
|
spurPath, err := findPath(
|
|
|
|
tx, graph, spurNode, target, ignoredVertexes,
|
|
|
|
ignoredEdges, amt,
|
|
|
|
)
|
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{
|
|
|
|
hops: make([]*ChannelHop, 0, newPathLen),
|
|
|
|
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
|
|
|
}
|