2019-01-24 22:50:53 +03:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2019-04-05 18:36:11 +03:00
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
2019-01-24 22:50:53 +03:00
|
|
|
)
|
|
|
|
|
2019-07-24 03:59:31 +03:00
|
|
|
// BlockPadding is used to increment the finalCltvDelta value for the last hop
|
|
|
|
// to prevent an HTLC being failed if some blocks are mined while it's in-flight.
|
|
|
|
const BlockPadding uint16 = 3
|
|
|
|
|
2019-05-23 21:05:30 +03:00
|
|
|
// PaymentSession is used during SendPayment attempts to provide routes to
|
|
|
|
// attempt. It also defines methods to give the PaymentSession additional
|
|
|
|
// information learned during the previous attempts.
|
|
|
|
type PaymentSession interface {
|
|
|
|
// RequestRoute returns the next route to attempt for routing the
|
|
|
|
// specified HTLC payment to the target node.
|
|
|
|
RequestRoute(payment *LightningPayment,
|
|
|
|
height uint32, finalCltvDelta uint16) (*route.Route, error)
|
|
|
|
}
|
|
|
|
|
2019-01-24 22:50:53 +03:00
|
|
|
// paymentSession is used during an HTLC routings session to prune the local
|
|
|
|
// chain view in response to failures, and also report those failures back to
|
2019-05-23 21:05:30 +03:00
|
|
|
// MissionControl. The snapshot copied for this session will only ever grow,
|
2019-01-24 22:50:53 +03:00
|
|
|
// and will now be pruned after a decay like the main view within mission
|
|
|
|
// control. We do this as we want to avoid the case where we continually try a
|
|
|
|
// bad edge or route multiple times in a session. This can lead to an infinite
|
|
|
|
// loop if payment attempts take long enough. An additional set of edges can
|
|
|
|
// also be provided to assist in reaching the payment's destination.
|
|
|
|
type paymentSession struct {
|
2019-04-05 18:36:11 +03:00
|
|
|
additionalEdges map[route.Vertex][]*channeldb.ChannelEdgePolicy
|
2019-01-24 22:50:53 +03:00
|
|
|
|
|
|
|
bandwidthHints map[uint64]lnwire.MilliSatoshi
|
|
|
|
|
2019-06-18 19:30:56 +03:00
|
|
|
sessionSource *SessionSource
|
2019-01-24 22:50:53 +03:00
|
|
|
|
2018-08-08 12:09:30 +03:00
|
|
|
preBuiltRoute *route.Route
|
|
|
|
preBuiltRouteTried bool
|
2019-02-13 13:35:55 +03:00
|
|
|
|
|
|
|
pathFinder pathFinder
|
2019-01-24 22:50:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// RequestRoute returns a route which is likely to be capable for successfully
|
|
|
|
// routing the specified HTLC payment to the target node. Initially the first
|
|
|
|
// set of paths returned from this method may encounter routing failure along
|
|
|
|
// the way, however as more payments are sent, mission control will start to
|
|
|
|
// build an up to date view of the network itself. With each payment a new area
|
|
|
|
// will be explored, which feeds into the recommendations made for routing.
|
|
|
|
//
|
|
|
|
// NOTE: This function is safe for concurrent access.
|
2019-05-23 21:05:30 +03:00
|
|
|
// NOTE: Part of the PaymentSession interface.
|
2019-01-24 22:50:53 +03:00
|
|
|
func (p *paymentSession) RequestRoute(payment *LightningPayment,
|
2019-04-05 18:36:11 +03:00
|
|
|
height uint32, finalCltvDelta uint16) (*route.Route, error) {
|
2019-01-24 22:50:53 +03:00
|
|
|
|
|
|
|
switch {
|
2018-08-08 12:09:30 +03:00
|
|
|
|
|
|
|
// If we have a pre-built route, use that directly.
|
|
|
|
case p.preBuiltRoute != nil && !p.preBuiltRouteTried:
|
|
|
|
p.preBuiltRouteTried = true
|
|
|
|
|
|
|
|
return p.preBuiltRoute, nil
|
|
|
|
|
|
|
|
// If the pre-built route has been tried already, the payment session is
|
|
|
|
// over.
|
|
|
|
case p.preBuiltRoute != nil:
|
|
|
|
return nil, fmt.Errorf("pre-built route already tried")
|
2019-01-24 22:50:53 +03:00
|
|
|
}
|
|
|
|
|
2019-07-24 03:59:31 +03:00
|
|
|
// Add BlockPadding to the finalCltvDelta so that the receiving node
|
|
|
|
// does not reject the HTLC if some blocks are mined while it's in-flight.
|
|
|
|
finalCltvDelta += BlockPadding
|
|
|
|
|
2019-02-13 13:53:32 +03:00
|
|
|
// If a route cltv limit was specified, we need to subtract the final
|
|
|
|
// delta before passing it into path finding. The optimal path is
|
|
|
|
// independent of the final cltv delta and the path finding algorithm is
|
|
|
|
// unaware of this value.
|
|
|
|
var cltvLimit *uint32
|
|
|
|
if payment.CltvLimit != nil {
|
|
|
|
limit := *payment.CltvLimit - uint32(finalCltvDelta)
|
|
|
|
cltvLimit = &limit
|
|
|
|
}
|
|
|
|
|
2019-01-24 22:50:53 +03:00
|
|
|
// TODO(roasbeef): sync logic amongst dist sys
|
|
|
|
|
|
|
|
// Taking into account this prune view, we'll attempt to locate a path
|
|
|
|
// to our destination, respecting the recommendations from
|
2019-05-23 21:05:30 +03:00
|
|
|
// MissionControl.
|
2019-06-18 19:30:56 +03:00
|
|
|
ss := p.sessionSource
|
|
|
|
|
|
|
|
restrictions := &RestrictParams{
|
2019-06-20 13:03:45 +03:00
|
|
|
ProbabilitySource: ss.MissionControl.GetEdgeProbability,
|
|
|
|
FeeLimit: payment.FeeLimit,
|
|
|
|
OutgoingChannelID: payment.OutgoingChannelID,
|
|
|
|
CltvLimit: cltvLimit,
|
2019-06-18 19:30:56 +03:00
|
|
|
}
|
|
|
|
|
2019-02-13 13:35:55 +03:00
|
|
|
path, err := p.pathFinder(
|
2019-01-24 22:50:53 +03:00
|
|
|
&graphParams{
|
2019-06-18 19:30:56 +03:00
|
|
|
graph: ss.Graph,
|
2019-01-24 22:50:53 +03:00
|
|
|
additionalEdges: p.additionalEdges,
|
|
|
|
bandwidthHints: p.bandwidthHints,
|
|
|
|
},
|
2019-06-20 13:03:45 +03:00
|
|
|
restrictions, &ss.PathFindingConfig,
|
|
|
|
ss.SelfNode.PubKeyBytes, payment.Target,
|
2019-03-05 18:55:19 +03:00
|
|
|
payment.Amount,
|
2019-01-24 22:50:53 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the next candidate path found, we'll attempt to turn this into
|
|
|
|
// a route by applying the time-lock and fee requirements.
|
2019-06-18 19:30:56 +03:00
|
|
|
sourceVertex := route.Vertex(ss.SelfNode.PubKeyBytes)
|
2019-01-24 22:50:53 +03:00
|
|
|
route, err := newRoute(
|
2019-03-05 12:37:14 +03:00
|
|
|
payment.Amount, sourceVertex, path, height, finalCltvDelta,
|
2019-01-24 22:50:53 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(roasbeef): return which edge/vertex didn't work
|
|
|
|
// out
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return route, err
|
|
|
|
}
|
2019-03-19 19:09:27 +03:00
|
|
|
|
|
|
|
// nodeChannel is a combination of the node pubkey and one of its channels.
|
|
|
|
type nodeChannel struct {
|
|
|
|
node route.Vertex
|
|
|
|
channel uint64
|
|
|
|
}
|