Skip to content

Commit daaa24b

Browse files
committed
routing: let BlindedPaymentPathSet handle FinalCLTV logic
Instead of needing to remember how to handle the FinalCLTV value of a blinded payment path at various points in the code base, we hide the logic behind a unified FinalCLTVDelta method on the blinded path.
1 parent 8df03de commit daaa24b

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

routing/blinding.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ type BlindedPaymentPathSet struct {
5151
// moment we require that all paths for the same payment have the
5252
// same feature set.
5353
features *lnwire.FeatureVector
54+
55+
// finalCLTV is the final hop's expiry delta of _any_ path in the set.
56+
// For any multi-hop path, the final CLTV delta should be seen as zero
57+
// since the final hop's final CLTV delta is accounted for in the
58+
// accumulated path policy values. The only edge case is for when the
59+
// final hop in the path is also the introduction node in which case
60+
// that path's FinalCLTV must be the non-zero min CLTV of the final hop
61+
// so that it is accounted for in path finding. For this reason, if
62+
// we have any single path in the set with only one hop, then we throw
63+
// away all the other paths. This should be fine to do since if there is
64+
// a path where the intro node is also the destination node, then there
65+
// isn't any need to try any other longer blinded path. In other words,
66+
// if this value is non-zero, then there is only one path in this
67+
// blinded path set and that path only has a single hop: the
68+
// introduction node.
69+
finalCLTV uint16
5470
}
5571

5672
// NewBlindedPaymentPathSet constructs a new BlindedPaymentPathSet from a set of
@@ -95,19 +111,25 @@ func NewBlindedPaymentPathSet(paths []*BlindedPayment) (*BlindedPaymentPathSet,
95111
}
96112
targetPub := targetPriv.PubKey()
97113

114+
var (
115+
pathSet = paths
116+
finalCLTVDelta uint16
117+
)
98118
// If any provided blinded path only has a single hop (ie, the
99119
// destination node is also the introduction node), then we discard all
100120
// other paths since we know the real pub key of the destination node.
101-
// For a single hop path, there is also no need for the pseudo target
102-
// pub key replacement, so our target pub key in this case just remains
103-
// the real introduction node ID.
104-
var pathSet = paths
121+
// We also then set the final CLTV delta to the path's delta since
122+
// there are no other edge hints that will account for it. For a single
123+
// hop path, there is also no need for the pseudo target pub key
124+
// replacement, so our target pub key in this case just remains the
125+
// real introduction node ID.
105126
for _, path := range paths {
106127
if len(path.BlindedPath.BlindedHops) != 1 {
107128
continue
108129
}
109130

110131
pathSet = []*BlindedPayment{path}
132+
finalCLTVDelta = path.CltvExpiryDelta
111133
targetPub = path.BlindedPath.IntroductionPoint
112134

113135
break
@@ -117,6 +139,7 @@ func NewBlindedPaymentPathSet(paths []*BlindedPayment) (*BlindedPaymentPathSet,
117139
paths: pathSet,
118140
targetPubKey: targetPub,
119141
features: features,
142+
finalCLTV: finalCLTVDelta,
120143
}, nil
121144
}
122145

@@ -137,6 +160,15 @@ func (s *BlindedPaymentPathSet) GetPath() *BlindedPayment {
137160
return s.paths[0]
138161
}
139162

163+
// FinalCLTVDelta is the minimum CLTV delta to use for the final hop on the
164+
// route. In most cases this will return zero since the value is accounted for
165+
// in the path's accumulated CLTVExpiryDelta. Only in the edge case of the path
166+
// set only including a single path which only includes an introduction node
167+
// will this return a non-zero value.
168+
func (s *BlindedPaymentPathSet) FinalCLTVDelta() uint16 {
169+
return s.finalCLTV
170+
}
171+
140172
// LargestLastHopPayloadPath returns the BlindedPayment in the set that has the
141173
// largest last-hop payload. This is to be used for onion size estimation in
142174
// path finding.

routing/pathfind.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,21 +210,12 @@ func newRoute(sourceVertex route.Vertex,
210210
// reporting through RPC. Set to zero for the final hop.
211211
fee = 0
212212

213-
// Only include the final hop CLTV delta in the total
214-
// time lock value if this is not a route to a blinded
215-
// path. For blinded paths, the total time-lock from the
216-
// whole path will be deduced from the introduction
217-
// node's CLTV delta. The exception is for the case
218-
// where the final hop is the blinded path introduction
219-
// node.
220-
if blindedPathSet == nil ||
221-
len(blindedPathSet.GetPath().BlindedPath.
222-
BlindedHops) == 1 {
223-
224-
// As this is the last hop, we'll use the
225-
// specified final CLTV delta value instead of
226-
// the value from the last link in the route.
213+
if blindedPathSet == nil {
227214
totalTimeLock += uint32(finalHop.cltvDelta)
215+
} else {
216+
totalTimeLock += uint32(
217+
blindedPathSet.FinalCLTVDelta(),
218+
)
228219
}
229220
outgoingTimeLock = totalTimeLock
230221

routing/router.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -524,17 +524,7 @@ func NewRouteRequest(source route.Vertex, target *route.Vertex,
524524
return nil, ErrExpiryAndBlinded
525525
}
526526

527-
// If we have a blinded path with 1 hop, the cltv expiry
528-
// will not be included in any hop hints (since we're just
529-
// sending to the introduction node and need no blinded hints).
530-
// In this case, we include it to make sure that the final
531-
// cltv delta is accounted for (since it's part of the blinded
532-
// delta). In the case of a multi-hop route, we set our final
533-
// cltv to zero, since it's going to be accounted for in the
534-
// delta for our hints.
535-
if len(blindedPayment.BlindedPath.BlindedHops) == 1 {
536-
requestExpiry = blindedPayment.CltvExpiryDelta
537-
}
527+
requestExpiry = blindedPathSet.FinalCLTVDelta()
538528

539529
requestHints, err = blindedPathSet.ToRouteHints()
540530
if err != nil {

0 commit comments

Comments
 (0)