@@ -261,17 +261,43 @@ pub struct Route {
261
261
pub payment_params : Option < PaymentParameters > ,
262
262
}
263
263
264
+ // This trait is deleted in the next commit
264
265
pub ( crate ) trait RoutePath {
265
266
/// Gets the fees for a given path, excluding any excess paid to the recipient.
266
- fn get_path_fees ( & self ) -> u64 ;
267
+ fn fee_msat ( & self ) -> u64 ;
268
+
269
+ /// Gets the total amount paid on this path, excluding the fees.
270
+ fn final_value_msat ( & self ) -> u64 ;
271
+
272
+ /// Gets the final hop's CLTV expiry delta.
273
+ fn final_cltv_expiry_delta ( & self ) -> u32 ;
267
274
}
268
275
impl RoutePath for Vec < RouteHop > {
269
- fn get_path_fees ( & self ) -> u64 {
276
+ fn fee_msat ( & self ) -> u64 {
270
277
// Do not count last hop of each path since that's the full value of the payment
271
278
self . split_last ( ) . map ( |( _, path_prefix) | path_prefix) . unwrap_or ( & [ ] )
272
279
. iter ( ) . map ( |hop| & hop. fee_msat )
273
280
. sum ( )
274
281
}
282
+ fn final_value_msat ( & self ) -> u64 {
283
+ self . last ( ) . map_or ( 0 , |hop| hop. fee_msat )
284
+ }
285
+ fn final_cltv_expiry_delta ( & self ) -> u32 {
286
+ self . last ( ) . map_or ( 0 , |hop| hop. cltv_expiry_delta )
287
+ }
288
+ }
289
+ impl RoutePath for & [ & RouteHop ] {
290
+ fn fee_msat ( & self ) -> u64 {
291
+ self . split_last ( ) . map ( |( _, path_prefix) | path_prefix) . unwrap_or ( & [ ] )
292
+ . iter ( ) . map ( |hop| & hop. fee_msat )
293
+ . sum ( )
294
+ }
295
+ fn final_value_msat ( & self ) -> u64 {
296
+ self . last ( ) . map_or ( 0 , |hop| hop. fee_msat )
297
+ }
298
+ fn final_cltv_expiry_delta ( & self ) -> u32 {
299
+ self . last ( ) . map_or ( 0 , |hop| hop. cltv_expiry_delta )
300
+ }
275
301
}
276
302
277
303
impl Route {
@@ -280,15 +306,13 @@ impl Route {
280
306
/// This doesn't include any extra payment made to the recipient, which can happen in excess of
281
307
/// the amount passed to [`find_route`]'s `params.final_value_msat`.
282
308
pub fn get_total_fees ( & self ) -> u64 {
283
- self . paths . iter ( ) . map ( |path| path. get_path_fees ( ) ) . sum ( )
309
+ self . paths . iter ( ) . map ( |path| path. fee_msat ( ) ) . sum ( )
284
310
}
285
311
286
312
/// Returns the total amount paid on this [`Route`], excluding the fees. Might be more than
287
313
/// requested if we had to reach htlc_minimum_msat.
288
314
pub fn get_total_amount ( & self ) -> u64 {
289
- return self . paths . iter ( )
290
- . map ( |path| path. split_last ( ) . map ( |( hop, _) | hop. fee_msat ) . unwrap_or ( 0 ) )
291
- . sum ( ) ;
315
+ return self . paths . iter ( ) . map ( |path|path. final_value_msat ( ) ) . sum ( )
292
316
}
293
317
}
294
318
@@ -2183,7 +2207,7 @@ mod tests {
2183
2207
use crate :: routing:: gossip:: { NetworkGraph , P2PGossipSync , NodeId , EffectiveCapacity } ;
2184
2208
use crate :: routing:: utxo:: UtxoResult ;
2185
2209
use crate :: routing:: router:: { get_route, build_route_from_hops_internal, add_random_cltv_offset, default_node_features,
2186
- PaymentParameters , Route , RouteHint , RouteHintHop , RouteHop , RoutingFees ,
2210
+ PaymentParameters , Route , RouteHint , RouteHintHop , RouteHop , RoutingFees , RoutePath ,
2187
2211
DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA , MAX_PATH_LENGTH_ESTIMATE } ;
2188
2212
use crate :: routing:: scoring:: { ChannelUsage , FixedPenaltyScorer , Score , ProbabilisticScorer , ProbabilisticScoringParameters } ;
2189
2213
use crate :: routing:: test_utils:: { add_channel, add_or_update_node, build_graph, build_line_graph, id_to_feature_flags, get_nodes, update_channel} ;
@@ -3487,7 +3511,7 @@ mod tests {
3487
3511
let path = route. paths . last ( ) . unwrap ( ) ;
3488
3512
assert_eq ! ( path. len( ) , 2 ) ;
3489
3513
assert_eq ! ( path. last( ) . unwrap( ) . pubkey, nodes[ 2 ] ) ;
3490
- assert_eq ! ( path. last ( ) . unwrap ( ) . fee_msat , 250_000_000 ) ;
3514
+ assert_eq ! ( path. final_value_msat ( ) , 250_000_000 ) ;
3491
3515
}
3492
3516
3493
3517
// Check that setting next_outbound_htlc_limit_msat in first_hops limits the channels.
@@ -3523,7 +3547,7 @@ mod tests {
3523
3547
let path = route. paths . last ( ) . unwrap ( ) ;
3524
3548
assert_eq ! ( path. len( ) , 2 ) ;
3525
3549
assert_eq ! ( path. last( ) . unwrap( ) . pubkey, nodes[ 2 ] ) ;
3526
- assert_eq ! ( path. last ( ) . unwrap ( ) . fee_msat , 200_000_000 ) ;
3550
+ assert_eq ! ( path. final_value_msat ( ) , 200_000_000 ) ;
3527
3551
}
3528
3552
3529
3553
// Enable channel #1 back.
@@ -3570,7 +3594,7 @@ mod tests {
3570
3594
let path = route. paths . last ( ) . unwrap ( ) ;
3571
3595
assert_eq ! ( path. len( ) , 2 ) ;
3572
3596
assert_eq ! ( path. last( ) . unwrap( ) . pubkey, nodes[ 2 ] ) ;
3573
- assert_eq ! ( path. last ( ) . unwrap ( ) . fee_msat , 15_000 ) ;
3597
+ assert_eq ! ( path. final_value_msat ( ) , 15_000 ) ;
3574
3598
}
3575
3599
3576
3600
// Now let's see if routing works if we know only capacity from the UTXO.
@@ -3641,7 +3665,7 @@ mod tests {
3641
3665
let path = route. paths . last ( ) . unwrap ( ) ;
3642
3666
assert_eq ! ( path. len( ) , 2 ) ;
3643
3667
assert_eq ! ( path. last( ) . unwrap( ) . pubkey, nodes[ 2 ] ) ;
3644
- assert_eq ! ( path. last ( ) . unwrap ( ) . fee_msat , 15_000 ) ;
3668
+ assert_eq ! ( path. final_value_msat ( ) , 15_000 ) ;
3645
3669
}
3646
3670
3647
3671
// Now let's see if routing chooses htlc_maximum_msat over UTXO capacity.
@@ -3673,7 +3697,7 @@ mod tests {
3673
3697
let path = route. paths . last ( ) . unwrap ( ) ;
3674
3698
assert_eq ! ( path. len( ) , 2 ) ;
3675
3699
assert_eq ! ( path. last( ) . unwrap( ) . pubkey, nodes[ 2 ] ) ;
3676
- assert_eq ! ( path. last ( ) . unwrap ( ) . fee_msat , 10_000 ) ;
3700
+ assert_eq ! ( path. final_value_msat ( ) , 10_000 ) ;
3677
3701
}
3678
3702
}
3679
3703
@@ -3786,7 +3810,7 @@ mod tests {
3786
3810
for path in & route. paths {
3787
3811
assert_eq ! ( path. len( ) , 4 ) ;
3788
3812
assert_eq ! ( path. last( ) . unwrap( ) . pubkey, nodes[ 3 ] ) ;
3789
- total_amount_paid_msat += path. last ( ) . unwrap ( ) . fee_msat ;
3813
+ total_amount_paid_msat += path. final_value_msat ( ) ;
3790
3814
}
3791
3815
assert_eq ! ( total_amount_paid_msat, 49_000 ) ;
3792
3816
}
@@ -3799,7 +3823,7 @@ mod tests {
3799
3823
for path in & route. paths {
3800
3824
assert_eq ! ( path. len( ) , 4 ) ;
3801
3825
assert_eq ! ( path. last( ) . unwrap( ) . pubkey, nodes[ 3 ] ) ;
3802
- total_amount_paid_msat += path. last ( ) . unwrap ( ) . fee_msat ;
3826
+ total_amount_paid_msat += path. final_value_msat ( ) ;
3803
3827
}
3804
3828
assert_eq ! ( total_amount_paid_msat, 50_000 ) ;
3805
3829
}
@@ -3847,7 +3871,7 @@ mod tests {
3847
3871
for path in & route. paths {
3848
3872
assert_eq ! ( path. len( ) , 2 ) ;
3849
3873
assert_eq ! ( path. last( ) . unwrap( ) . pubkey, nodes[ 2 ] ) ;
3850
- total_amount_paid_msat += path. last ( ) . unwrap ( ) . fee_msat ;
3874
+ total_amount_paid_msat += path. final_value_msat ( ) ;
3851
3875
}
3852
3876
assert_eq ! ( total_amount_paid_msat, 50_000 ) ;
3853
3877
}
@@ -3993,7 +4017,7 @@ mod tests {
3993
4017
for path in & route. paths {
3994
4018
assert_eq ! ( path. len( ) , 2 ) ;
3995
4019
assert_eq ! ( path. last( ) . unwrap( ) . pubkey, nodes[ 2 ] ) ;
3996
- total_amount_paid_msat += path. last ( ) . unwrap ( ) . fee_msat ;
4020
+ total_amount_paid_msat += path. final_value_msat ( ) ;
3997
4021
}
3998
4022
assert_eq ! ( total_amount_paid_msat, 250_000 ) ;
3999
4023
}
@@ -4007,7 +4031,7 @@ mod tests {
4007
4031
for path in & route. paths {
4008
4032
assert_eq ! ( path. len( ) , 2 ) ;
4009
4033
assert_eq ! ( path. last( ) . unwrap( ) . pubkey, nodes[ 2 ] ) ;
4010
- total_amount_paid_msat += path. last ( ) . unwrap ( ) . fee_msat ;
4034
+ total_amount_paid_msat += path. final_value_msat ( ) ;
4011
4035
}
4012
4036
assert_eq ! ( total_amount_paid_msat, 290_000 ) ;
4013
4037
}
@@ -4171,7 +4195,7 @@ mod tests {
4171
4195
let mut total_amount_paid_msat = 0 ;
4172
4196
for path in & route. paths {
4173
4197
assert_eq ! ( path. last( ) . unwrap( ) . pubkey, nodes[ 3 ] ) ;
4174
- total_amount_paid_msat += path. last ( ) . unwrap ( ) . fee_msat ;
4198
+ total_amount_paid_msat += path. final_value_msat ( ) ;
4175
4199
}
4176
4200
assert_eq ! ( total_amount_paid_msat, 300_000 ) ;
4177
4201
}
@@ -4333,7 +4357,7 @@ mod tests {
4333
4357
let mut total_paid_msat = 0 ;
4334
4358
for path in & route. paths {
4335
4359
assert_eq ! ( path. last( ) . unwrap( ) . pubkey, nodes[ 3 ] ) ;
4336
- total_value_transferred_msat += path. last ( ) . unwrap ( ) . fee_msat ;
4360
+ total_value_transferred_msat += path. final_value_msat ( ) ;
4337
4361
for hop in path {
4338
4362
total_paid_msat += hop. fee_msat ;
4339
4363
}
@@ -4510,7 +4534,7 @@ mod tests {
4510
4534
let mut total_amount_paid_msat = 0 ;
4511
4535
for path in & route. paths {
4512
4536
assert_eq ! ( path. last( ) . unwrap( ) . pubkey, nodes[ 3 ] ) ;
4513
- total_amount_paid_msat += path. last ( ) . unwrap ( ) . fee_msat ;
4537
+ total_amount_paid_msat += path. final_value_msat ( ) ;
4514
4538
}
4515
4539
assert_eq ! ( total_amount_paid_msat, 200_000 ) ;
4516
4540
assert_eq ! ( route. get_total_fees( ) , 150_000 ) ;
@@ -4737,7 +4761,7 @@ mod tests {
4737
4761
for path in & route. paths {
4738
4762
assert_eq ! ( path. len( ) , 2 ) ;
4739
4763
assert_eq ! ( path. last( ) . unwrap( ) . pubkey, nodes[ 2 ] ) ;
4740
- total_amount_paid_msat += path. last ( ) . unwrap ( ) . fee_msat ;
4764
+ total_amount_paid_msat += path. final_value_msat ( ) ;
4741
4765
}
4742
4766
assert_eq ! ( total_amount_paid_msat, 125_000 ) ;
4743
4767
}
@@ -4750,7 +4774,7 @@ mod tests {
4750
4774
for path in & route. paths {
4751
4775
assert_eq ! ( path. len( ) , 2 ) ;
4752
4776
assert_eq ! ( path. last( ) . unwrap( ) . pubkey, nodes[ 2 ] ) ;
4753
- total_amount_paid_msat += path. last ( ) . unwrap ( ) . fee_msat ;
4777
+ total_amount_paid_msat += path. final_value_msat ( ) ;
4754
4778
}
4755
4779
assert_eq ! ( total_amount_paid_msat, 90_000 ) ;
4756
4780
}
0 commit comments