Skip to content

Commit 4a22ec8

Browse files
committed
routing: pass BlindedPaymentPathSet around everywhere
Building on from the previous commit, here we pass the PathSet around everywhere where we previously passed around the single BlindedPayment.
1 parent 3d5f20b commit 4a22ec8

File tree

7 files changed

+94
-92
lines changed

7 files changed

+94
-92
lines changed

lnrpc/routerrpc/router_backend.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,10 @@ func (r *RouterBackend) parseQueryRoutesRequest(in *lnrpc.QueryRoutesRequest) (
388388
fromNode, toNode, amt, capacity,
389389
)
390390
},
391-
DestCustomRecords: record.CustomSet(in.DestCustomRecords),
392-
CltvLimit: cltvLimit,
393-
DestFeatures: destinationFeatures,
394-
BlindedPayment: blindedPathSet.GetPath(),
391+
DestCustomRecords: record.CustomSet(in.DestCustomRecords),
392+
CltvLimit: cltvLimit,
393+
DestFeatures: destinationFeatures,
394+
BlindedPaymentPathSet: blindedPathSet,
395395
}
396396

397397
// Pass along an outgoing channel restriction if specified.
@@ -420,7 +420,7 @@ func (r *RouterBackend) parseQueryRoutesRequest(in *lnrpc.QueryRoutesRequest) (
420420

421421
return routing.NewRouteRequest(
422422
sourcePubKey, targetPubKey, amt, in.TimePref, restrictions,
423-
customRecords, routeHintEdges, blindedPathSet.GetPath(),
423+
customRecords, routeHintEdges, blindedPathSet,
424424
finalCLTVDelta,
425425
)
426426
}
@@ -1007,7 +1007,7 @@ func (r *RouterBackend) extractIntentFromSendRequest(
10071007
if err != nil {
10081008
return nil, err
10091009
}
1010-
payIntent.BlindedPayment = pathSet.GetPath()
1010+
payIntent.BlindedPathSet = pathSet
10111011

10121012
// Replace the target node with the target public key
10131013
// of the blinded path set.

routing/pathfind.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,8 @@ type finalHopParams struct {
136136
// NOTE: If a non-nil blinded path is provided it is assumed to have been
137137
// validated by the caller.
138138
func newRoute(sourceVertex route.Vertex,
139-
pathEdges []*unifiedEdge, currentHeight uint32,
140-
finalHop finalHopParams, blindedPath *sphinx.BlindedPath) (
141-
*route.Route, error) {
139+
pathEdges []*unifiedEdge, currentHeight uint32, finalHop finalHopParams,
140+
blindedPathSet *BlindedPaymentPathSet) (*route.Route, error) {
142141

143142
var (
144143
hops []*route.Hop
@@ -153,8 +152,14 @@ func newRoute(sourceVertex route.Vertex,
153152
// backwards below, this next hop gets closer and closer to the
154153
// sender of the payment.
155154
nextIncomingAmount lnwire.MilliSatoshi
155+
156+
blindedPath *sphinx.BlindedPath
156157
)
157158

159+
if blindedPathSet != nil {
160+
blindedPath = blindedPathSet.GetPath().BlindedPath
161+
}
162+
158163
pathLength := len(pathEdges)
159164
for i := pathLength - 1; i >= 0; i-- {
160165
// Now we'll start to calculate the items within the per-hop
@@ -437,9 +442,9 @@ type RestrictParams struct {
437442
// the payee.
438443
Metadata []byte
439444

440-
// BlindedPayment is necessary to determine the hop size of the
445+
// BlindedPaymentPathSet is necessary to determine the hop size of the
441446
// last/exit hop.
442-
BlindedPayment *BlindedPayment
447+
BlindedPaymentPathSet *BlindedPaymentPathSet
443448
}
444449

445450
// PathFindingConfig defines global parameters that control the trade-off in
@@ -1365,9 +1370,11 @@ func getProbabilityBasedDist(weight int64, probability float64,
13651370
func lastHopPayloadSize(r *RestrictParams, finalHtlcExpiry int32,
13661371
amount lnwire.MilliSatoshi) uint64 {
13671372

1368-
if r.BlindedPayment != nil {
1369-
blindedPath := r.BlindedPayment.BlindedPath.BlindedHops
1370-
blindedPoint := r.BlindedPayment.BlindedPath.BlindingPoint
1373+
if r.BlindedPaymentPathSet != nil {
1374+
paymentPath := r.BlindedPaymentPathSet.
1375+
LargestLastHopPayloadPath()
1376+
blindedPath := paymentPath.BlindedPath.BlindedHops
1377+
blindedPoint := paymentPath.BlindedPath.BlindingPoint
13711378

13721379
encryptedData := blindedPath[len(blindedPath)-1].CipherText
13731380
finalHop := route.Hop{

routing/pathfind_test.go

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3277,6 +3277,11 @@ func TestBlindedRouteConstruction(t *testing.T) {
32773277

32783278
require.NoError(t, blindedPayment.Validate())
32793279

3280+
blindedPathSet, err := NewBlindedPaymentPathSet(
3281+
[]*BlindedPayment{blindedPayment},
3282+
)
3283+
require.NoError(t, err)
3284+
32803285
// Generate route hints from our blinded payment and a set of edges
32813286
// that make up the graph we'll give to route construction. The hints
32823287
// map is keyed by source node, so we can retrieve our blinded edges
@@ -3382,7 +3387,7 @@ func TestBlindedRouteConstruction(t *testing.T) {
33823387

33833388
route, err := newRoute(
33843389
sourceVertex, edges, currentHeight, finalHopParams,
3385-
blindedPath,
3390+
blindedPathSet,
33863391
)
33873392
require.NoError(t, err)
33883393
require.Equal(t, expectedRoute, route)
@@ -3409,31 +3414,38 @@ func TestLastHopPayloadSize(t *testing.T) {
34093414
amtToForward = lnwire.MilliSatoshi(10000)
34103415
finalHopExpiry int32 = 144
34113416

3412-
oneHopBlindedPayment = &BlindedPayment{
3413-
BlindedPath: &sphinx.BlindedPath{
3414-
BlindedHops: []*sphinx.BlindedHopInfo{
3415-
{
3416-
CipherText: encrypedData,
3417-
},
3417+
oneHopPath = &sphinx.BlindedPath{
3418+
BlindedHops: []*sphinx.BlindedHopInfo{
3419+
{
3420+
CipherText: encrypedData,
34183421
},
3419-
BlindingPoint: blindedPoint,
34203422
},
3423+
BlindingPoint: blindedPoint,
34213424
}
3422-
twoHopBlindedPayment = &BlindedPayment{
3423-
BlindedPath: &sphinx.BlindedPath{
3424-
BlindedHops: []*sphinx.BlindedHopInfo{
3425-
{
3426-
CipherText: encrypedData,
3427-
},
3428-
{
3429-
CipherText: encrypedData,
3430-
},
3425+
3426+
twoHopPath = &sphinx.BlindedPath{
3427+
BlindedHops: []*sphinx.BlindedHopInfo{
3428+
{
3429+
CipherText: encrypedData,
3430+
},
3431+
{
3432+
CipherText: encrypedData,
34313433
},
3432-
BlindingPoint: blindedPoint,
34333434
},
3435+
BlindingPoint: blindedPoint,
34343436
}
34353437
)
34363438

3439+
oneHopBlindedPayment, err := NewBlindedPaymentPathSet(
3440+
[]*BlindedPayment{{BlindedPath: oneHopPath}},
3441+
)
3442+
require.NoError(t, err)
3443+
3444+
twoHopBlindedPayment, err := NewBlindedPaymentPathSet(
3445+
[]*BlindedPayment{{BlindedPath: twoHopPath}},
3446+
)
3447+
require.NoError(t, err)
3448+
34373449
testCases := []struct {
34383450
name string
34393451
restrictions *RestrictParams
@@ -3454,15 +3466,15 @@ func TestLastHopPayloadSize(t *testing.T) {
34543466
{
34553467
name: "Blinded final hop introduction point",
34563468
restrictions: &RestrictParams{
3457-
BlindedPayment: oneHopBlindedPayment,
3469+
BlindedPaymentPathSet: oneHopBlindedPayment,
34583470
},
34593471
amount: amtToForward,
34603472
finalHopExpiry: finalHopExpiry,
34613473
},
34623474
{
34633475
name: "Blinded final hop of a two hop payment",
34643476
restrictions: &RestrictParams{
3465-
BlindedPayment: twoHopBlindedPayment,
3477+
BlindedPaymentPathSet: twoHopBlindedPayment,
34663478
},
34673479
amount: amtToForward,
34683480
finalHopExpiry: finalHopExpiry,
@@ -3490,12 +3502,11 @@ func TestLastHopPayloadSize(t *testing.T) {
34903502
}
34913503

34923504
var finalHop route.Hop
3493-
if tc.restrictions.BlindedPayment != nil {
3494-
blindedPath := tc.restrictions.BlindedPayment.
3495-
BlindedPath.BlindedHops
3496-
3497-
blindedPoint := tc.restrictions.BlindedPayment.
3498-
BlindedPath.BlindingPoint
3505+
if tc.restrictions.BlindedPaymentPathSet != nil {
3506+
path := tc.restrictions.BlindedPaymentPathSet.
3507+
LargestLastHopPayloadPath()
3508+
blindedPath := path.BlindedPath.BlindedHops
3509+
blindedPoint := path.BlindedPath.BlindingPoint
34993510

35003511
//nolint:lll
35013512
finalHop = route.Hop{

routing/payment_session.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/btcsuite/btcd/btcec/v2"
77
"github.com/btcsuite/btclog"
8-
sphinx "github.com/lightningnetwork/lightning-onion"
98
"github.com/lightningnetwork/lnd/build"
109
"github.com/lightningnetwork/lnd/channeldb"
1110
"github.com/lightningnetwork/lnd/channeldb/models"
@@ -206,13 +205,13 @@ func newPaymentSession(p *LightningPayment, selfNode route.Vertex,
206205
return nil, err
207206
}
208207

209-
if p.BlindedPayment != nil {
208+
if p.BlindedPathSet != nil {
210209
if len(edges) != 0 {
211210
return nil, fmt.Errorf("cannot have both route hints " +
212211
"and blinded path")
213212
}
214213

215-
edges, err = p.BlindedPayment.toRouteHints()
214+
edges, err = p.BlindedPathSet.ToRouteHints()
216215
if err != nil {
217216
return nil, err
218217
}
@@ -342,7 +341,7 @@ func (p *paymentSession) RequestRoute(maxAmt, feeLimit lnwire.MilliSatoshi,
342341
// can split. Split payments to blinded paths won't have
343342
// MPP records.
344343
if p.payment.PaymentAddr == nil &&
345-
p.payment.BlindedPayment == nil {
344+
p.payment.BlindedPathSet == nil {
346345

347346
p.log.Debugf("not splitting because payment " +
348347
"address is unspecified")
@@ -407,11 +406,6 @@ func (p *paymentSession) RequestRoute(maxAmt, feeLimit lnwire.MilliSatoshi,
407406
return nil, err
408407
}
409408

410-
var blindedPath *sphinx.BlindedPath
411-
if p.payment.BlindedPayment != nil {
412-
blindedPath = p.payment.BlindedPayment.BlindedPath
413-
}
414-
415409
// With the next candidate path found, we'll attempt to turn
416410
// this into a route by applying the time-lock and fee
417411
// requirements.
@@ -424,7 +418,7 @@ func (p *paymentSession) RequestRoute(maxAmt, feeLimit lnwire.MilliSatoshi,
424418
records: p.payment.DestCustomRecords,
425419
paymentAddr: p.payment.PaymentAddr,
426420
metadata: p.payment.Metadata,
427-
}, blindedPath,
421+
}, p.payment.BlindedPathSet,
428422
)
429423
if err != nil {
430424
return nil, err

routing/router.go

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -477,10 +477,10 @@ type RouteRequest struct {
477477
// in blinded payment.
478478
FinalExpiry uint16
479479

480-
// BlindedPayment contains an optional blinded path and parameters
481-
// used to reach a target node via a blinded path. This field is
480+
// BlindedPathSet contains a set of optional blinded paths and
481+
// parameters used to reach a target node blinded paths. This field is
482482
// mutually exclusive with the Target field.
483-
BlindedPayment *BlindedPayment
483+
BlindedPathSet *BlindedPaymentPathSet
484484
}
485485

486486
// RouteHints is an alias type for a set of route hints, with the source node
@@ -494,7 +494,7 @@ type RouteHints map[route.Vertex][]AdditionalEdge
494494
func NewRouteRequest(source route.Vertex, target *route.Vertex,
495495
amount lnwire.MilliSatoshi, timePref float64,
496496
restrictions *RestrictParams, customRecords record.CustomSet,
497-
routeHints RouteHints, blindedPayment *BlindedPayment,
497+
routeHints RouteHints, blindedPathSet *BlindedPaymentPathSet,
498498
finalExpiry uint16) (*RouteRequest, error) {
499499

500500
var (
@@ -504,11 +504,8 @@ func NewRouteRequest(source route.Vertex, target *route.Vertex,
504504
err error
505505
)
506506

507-
if blindedPayment != nil {
508-
if err := blindedPayment.Validate(); err != nil {
509-
return nil, fmt.Errorf("invalid blinded payment: %w",
510-
err)
511-
}
507+
if blindedPathSet != nil {
508+
blindedPayment := blindedPathSet.GetPath()
512509

513510
introVertex := route.NewVertex(
514511
blindedPayment.BlindedPath.IntroductionPoint,
@@ -539,13 +536,13 @@ func NewRouteRequest(source route.Vertex, target *route.Vertex,
539536
requestExpiry = blindedPayment.CltvExpiryDelta
540537
}
541538

542-
requestHints, err = blindedPayment.toRouteHints()
539+
requestHints, err = blindedPathSet.ToRouteHints()
543540
if err != nil {
544541
return nil, err
545542
}
546543
}
547544

548-
requestTarget, err := getTargetNode(target, blindedPayment)
545+
requestTarget, err := getTargetNode(target, blindedPathSet)
549546
if err != nil {
550547
return nil, err
551548
}
@@ -559,15 +556,15 @@ func NewRouteRequest(source route.Vertex, target *route.Vertex,
559556
CustomRecords: customRecords,
560557
RouteHints: requestHints,
561558
FinalExpiry: requestExpiry,
562-
BlindedPayment: blindedPayment,
559+
BlindedPathSet: blindedPathSet,
563560
}, nil
564561
}
565562

566-
func getTargetNode(target *route.Vertex, blindedPayment *BlindedPayment) (
567-
route.Vertex, error) {
563+
func getTargetNode(target *route.Vertex,
564+
blindedPathSet *BlindedPaymentPathSet) (route.Vertex, error) {
568565

569566
var (
570-
blinded = blindedPayment != nil
567+
blinded = blindedPathSet != nil
571568
targetSet = target != nil
572569
)
573570

@@ -576,6 +573,8 @@ func getTargetNode(target *route.Vertex, blindedPayment *BlindedPayment) (
576573
return route.Vertex{}, ErrTargetAndBlinded
577574

578575
case blinded:
576+
blindedPayment := blindedPathSet.GetPath()
577+
579578
// If we're dealing with an edge-case blinded path that just
580579
// has an introduction node (first hop expected to be the intro
581580
// hop), then we return the unblinded introduction node as our
@@ -597,16 +596,6 @@ func getTargetNode(target *route.Vertex, blindedPayment *BlindedPayment) (
597596
}
598597
}
599598

600-
// blindedPath returns the request's blinded path, which is set if the payment
601-
// is to a blinded route.
602-
func (r *RouteRequest) blindedPath() *sphinx.BlindedPath {
603-
if r.BlindedPayment == nil {
604-
return nil
605-
}
606-
607-
return r.BlindedPayment.BlindedPath
608-
}
609-
610599
// FindRoute attempts to query the ChannelRouter for the optimum path to a
611600
// particular target destination to which it is able to send `amt` after
612601
// factoring in channel capacities and cumulative fees along the route.
@@ -664,7 +653,7 @@ func (r *ChannelRouter) FindRoute(req *RouteRequest) (*route.Route, float64,
664653
totalAmt: req.Amount,
665654
cltvDelta: req.FinalExpiry,
666655
records: req.CustomRecords,
667-
}, req.blindedPath(),
656+
}, req.BlindedPathSet,
668657
)
669658
if err != nil {
670659
return nil, 0, err
@@ -926,14 +915,10 @@ type LightningPayment struct {
926915
// BlindedPayment field.
927916
RouteHints [][]zpay32.HopHint
928917

929-
// BlindedPayment holds the information about a blinded path to the
930-
// payment recipient. This is mutually exclusive to the RouteHints
918+
// BlindedPathSet holds the information about a set of blinded paths to
919+
// the payment recipient. This is mutually exclusive to the RouteHints
931920
// field.
932-
//
933-
// NOTE: a recipient may provide multiple blinded payment paths in the
934-
// same invoice. Currently, LND will only attempt to use the first one.
935-
// A future PR will handle multiple blinded payment paths.
936-
BlindedPayment *BlindedPayment
921+
BlindedPathSet *BlindedPaymentPathSet
937922

938923
// OutgoingChannelIDs is the list of channels that are allowed for the
939924
// first hop. If nil, any channel may be used.

0 commit comments

Comments
 (0)