From 9c577f3f57c91bc1a713c45f3929251d4dc5e11b Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Tue, 14 Jan 2020 11:19:52 +0100 Subject: [PATCH] routing: extract route hint conversion --- routing/payment_session_source.go | 87 ++++++++++++++++++------------- 1 file changed, 50 insertions(+), 37 deletions(-) diff --git a/routing/payment_session_source.go b/routing/payment_session_source.go index b5175cf7..05295b58 100644 --- a/routing/payment_session_source.go +++ b/routing/payment_session_source.go @@ -50,6 +50,55 @@ type SessionSource struct { func (m *SessionSource) NewPaymentSession(routeHints [][]zpay32.HopHint, target route.Vertex) (PaymentSession, error) { + edges, err := RouteHintsToEdges(routeHints, target) + if err != nil { + return nil, err + } + + sourceNode, err := m.Graph.SourceNode() + if err != nil { + return nil, err + } + + getBandwidthHints := func() (map[uint64]lnwire.MilliSatoshi, + error) { + + return generateBandwidthHints(sourceNode, m.QueryBandwidth) + } + + return &paymentSession{ + additionalEdges: edges, + getBandwidthHints: getBandwidthHints, + sessionSource: m, + pathFinder: findPath, + }, nil +} + +// NewPaymentSessionForRoute creates a new paymentSession instance that is just +// used for failure reporting to missioncontrol. +func (m *SessionSource) NewPaymentSessionForRoute(preBuiltRoute *route.Route) PaymentSession { + return &paymentSession{ + sessionSource: m, + preBuiltRoute: preBuiltRoute, + } +} + +// NewPaymentSessionEmpty creates a new paymentSession instance that is empty, +// and will be exhausted immediately. Used for failure reporting to +// missioncontrol for resumed payment we don't want to make more attempts for. +func (m *SessionSource) NewPaymentSessionEmpty() PaymentSession { + return &paymentSession{ + sessionSource: m, + preBuiltRoute: &route.Route{}, + preBuiltRouteTried: true, + } +} + +// RouteHintsToEdges converts a list of invoice route hints to an edge map that +// can be passed into pathfinding. +func RouteHintsToEdges(routeHints [][]zpay32.HopHint, target route.Vertex) ( + map[route.Vertex][]*channeldb.ChannelEdgePolicy, error) { + edges := make(map[route.Vertex][]*channeldb.ChannelEdgePolicy) // Traverse through all of the available hop hints and include them in @@ -97,41 +146,5 @@ func (m *SessionSource) NewPaymentSession(routeHints [][]zpay32.HopHint, } } - sourceNode, err := m.Graph.SourceNode() - if err != nil { - return nil, err - } - - getBandwidthHints := func() (map[uint64]lnwire.MilliSatoshi, - error) { - - return generateBandwidthHints(sourceNode, m.QueryBandwidth) - } - - return &paymentSession{ - additionalEdges: edges, - getBandwidthHints: getBandwidthHints, - sessionSource: m, - pathFinder: findPath, - }, nil -} - -// NewPaymentSessionForRoute creates a new paymentSession instance that is just -// used for failure reporting to missioncontrol. -func (m *SessionSource) NewPaymentSessionForRoute(preBuiltRoute *route.Route) PaymentSession { - return &paymentSession{ - sessionSource: m, - preBuiltRoute: preBuiltRoute, - } -} - -// NewPaymentSessionEmpty creates a new paymentSession instance that is empty, -// and will be exhausted immediately. Used for failure reporting to -// missioncontrol for resumed payment we don't want to make more attempts for. -func (m *SessionSource) NewPaymentSessionEmpty() PaymentSession { - return &paymentSession{ - sessionSource: m, - preBuiltRoute: &route.Route{}, - preBuiltRouteTried: true, - } + return edges, nil }