@@ -609,12 +609,18 @@ fn compute_fees(amount_msat: u64, channel_fees: RoutingFees) -> Option<u64> {
609
609
pub fn find_route < L : Deref , S : Score > (
610
610
our_node_pubkey : & PublicKey , route_params : & RouteParameters , network : & NetworkGraph ,
611
611
first_hops : Option < & [ & ChannelDetails ] > , logger : L , scorer : & S , random_seed_bytes : & [ u8 ; 32 ]
612
- ) -> Result < Route , LightningError >
612
+ ) -> Result < Route , LightningError >
613
613
where L :: Target : Logger {
614
- get_route (
614
+ match get_route (
615
615
our_node_pubkey, & route_params. payment_params , network, first_hops, route_params. final_value_msat ,
616
616
route_params. final_cltv_expiry_delta , logger, scorer, random_seed_bytes
617
- )
617
+ ) {
618
+ Ok ( mut route) => {
619
+ add_random_cltv_offset ( & mut route, & route_params. payment_params , network, random_seed_bytes) ;
620
+ Ok ( route)
621
+ } ,
622
+ Err ( err) => Err ( err) ,
623
+ }
618
624
}
619
625
620
626
pub ( crate ) fn get_route < L : Deref , S : Score > (
@@ -664,7 +670,6 @@ where L::Target: Logger {
664
670
// 8. The last path in every selected route is likely to be more than we need.
665
671
// Reduce its value-to-transfer and recompute fees.
666
672
// 9. Choose the best route by the lowest total fee.
667
- // 10. Add a random 'shadow route' offset to the CLTV expiry delta values to improve privacy.
668
673
669
674
// As for the actual search algorithm,
670
675
// we do a payee-to-payer pseudo-Dijkstra's sorting by each node's distance from the payee
@@ -1515,14 +1520,27 @@ where L::Target: Logger {
1515
1520
}
1516
1521
}
1517
1522
1518
- // Step (10).
1519
- // Add a random 'shadow route' offset to the CLTV expiry delta values to improve privacy.
1520
- for path in selected_paths. iter_mut ( ) {
1523
+
1524
+ let route = Route {
1525
+ paths : selected_paths. into_iter ( ) . map ( |path| path. into_iter ( ) . collect ( ) ) . collect :: < Result < Vec < _ > , _ > > ( ) ?,
1526
+ payment_params : Some ( payment_params. clone ( ) ) ,
1527
+ } ;
1528
+ log_info ! ( logger, "Got route to {}: {}" , payment_params. payee_pubkey, log_route!( route) ) ;
1529
+ Ok ( route)
1530
+ }
1531
+
1532
+ // Add a random 'shadow route' offset to the CLTV expiry delta values to improve privacy.
1533
+ fn add_random_cltv_offset ( route : & mut Route , payment_params : & PaymentParameters , network : & NetworkGraph , random_seed_bytes : & [ u8 ; 32 ] ) {
1534
+ let network_graph = network. read_only ( ) ;
1535
+ let network_channels = network_graph. channels ( ) ;
1536
+ let network_nodes = network_graph. nodes ( ) ;
1537
+
1538
+ for path in route. paths . iter_mut ( ) {
1521
1539
let mut shadow_ctlv_expiry_delta_offset: u32 = 0 ;
1522
1540
1523
1541
// Choose the last publicly known node as the starting point for the random walk
1524
- if let Some ( starting_hop) = path. iter ( ) . rev ( ) . find ( |h| network_nodes. contains_key ( & NodeId :: from_pubkey ( & h. as_ref ( ) . unwrap ( ) . pubkey ) ) ) {
1525
- let mut cur_node_id = NodeId :: from_pubkey ( & starting_hop. as_ref ( ) . unwrap ( ) . pubkey ) ;
1542
+ if let Some ( starting_hop) = path. iter ( ) . rev ( ) . find ( |h| network_nodes. contains_key ( & NodeId :: from_pubkey ( & h. pubkey ) ) ) {
1543
+ let mut cur_node_id = NodeId :: from_pubkey ( & starting_hop. pubkey ) ;
1526
1544
1527
1545
// Init PRNG with path nonce
1528
1546
let mut path_nonce = [ 0u8 ; 8 ] ;
@@ -1574,28 +1592,19 @@ where L::Target: Logger {
1574
1592
1575
1593
// Limit the offset so we never exceed the max_total_cltv_expiry_delta
1576
1594
let max_path_offset = payment_params. max_total_cltv_expiry_delta
1577
- . checked_sub ( path. iter ( ) . fold ( 0 , |acc, b| b. as_ref ( ) . unwrap ( ) . cltv_expiry_delta . max ( acc) ) )
1595
+ . checked_sub ( path. iter ( ) . fold ( 0 , |acc, b| b. cltv_expiry_delta . max ( acc) ) )
1578
1596
. unwrap_or ( shadow_ctlv_expiry_delta_offset) ;
1579
1597
shadow_ctlv_expiry_delta_offset = cmp:: min ( shadow_ctlv_expiry_delta_offset,
1580
1598
max_path_offset. checked_sub ( max_path_offset. wrapping_rem ( 40 ) ) . unwrap_or ( max_path_offset) ) ;
1581
1599
1582
1600
// Add 'shadow' CLTV offset to all hops but the final one
1583
- for h in path {
1584
- if let Ok ( hop) = h {
1585
- if hop. pubkey != payment_params. payee_pubkey {
1586
- hop. cltv_expiry_delta = hop. cltv_expiry_delta
1587
- . checked_add ( shadow_ctlv_expiry_delta_offset) . unwrap_or ( hop. cltv_expiry_delta ) ;
1588
- }
1601
+ for hop in path {
1602
+ if hop. pubkey != payment_params. payee_pubkey {
1603
+ hop. cltv_expiry_delta = hop. cltv_expiry_delta
1604
+ . checked_add ( shadow_ctlv_expiry_delta_offset) . unwrap_or ( hop. cltv_expiry_delta ) ;
1589
1605
}
1590
1606
}
1591
1607
}
1592
-
1593
- let route = Route {
1594
- paths : selected_paths. into_iter ( ) . map ( |path| path. into_iter ( ) . collect ( ) ) . collect :: < Result < Vec < _ > , _ > > ( ) ?,
1595
- payment_params : Some ( payment_params. clone ( ) ) ,
1596
- } ;
1597
- log_info ! ( logger, "Got route to {}: {}" , payment_params. payee_pubkey, log_route!( route) ) ;
1598
- Ok ( route)
1599
1608
}
1600
1609
1601
1610
#[ cfg( test) ]
0 commit comments