@@ -151,8 +151,8 @@ impl Readable for Route {
151
151
152
152
/// Parameters needed to find a [`Route`].
153
153
///
154
- /// Passed to [`find_route`] and also provided in [`Event::PaymentPathFailed`] for retrying a failed
155
- /// payment path.
154
+ /// Passed to [`find_route`] and [`build_route_from_hops`], but also provided in
155
+ /// [`Event::PaymentPathFailed`] for retrying a failed payment path.
156
156
///
157
157
/// [`Event::PaymentPathFailed`]: crate::util::events::Event::PaymentPathFailed
158
158
#[ derive( Clone , Debug ) ]
@@ -676,16 +676,11 @@ pub fn find_route<L: Deref, S: Score>(
676
676
) -> Result < Route , LightningError >
677
677
where L :: Target : Logger {
678
678
let network_graph = network. read_only ( ) ;
679
- match get_route (
680
- our_node_pubkey, & route_params. payment_params , & network_graph, first_hops, route_params. final_value_msat ,
681
- route_params. final_cltv_expiry_delta , logger, scorer, random_seed_bytes
682
- ) {
683
- Ok ( mut route) => {
684
- add_random_cltv_offset ( & mut route, & route_params. payment_params , & network_graph, random_seed_bytes) ;
685
- Ok ( route)
686
- } ,
687
- Err ( err) => Err ( err) ,
688
- }
679
+ let mut route = get_route ( our_node_pubkey, & route_params. payment_params , & network_graph, first_hops,
680
+ route_params. final_value_msat , route_params. final_cltv_expiry_delta , logger, scorer,
681
+ random_seed_bytes) ?;
682
+ add_random_cltv_offset ( & mut route, & route_params. payment_params , & network_graph, random_seed_bytes) ;
683
+ Ok ( route)
689
684
}
690
685
691
686
pub ( crate ) fn get_route < L : Deref , S : Score > (
@@ -1785,10 +1780,57 @@ fn add_random_cltv_offset(route: &mut Route, payment_params: &PaymentParameters,
1785
1780
}
1786
1781
}
1787
1782
1783
+ /// Build a route from us (payer) with the given hops ending at the target node (payee).
1784
+ ///
1785
+ /// Re-uses logic from `find_route`, so the restrictions described there also apply here.
1786
+ pub fn build_route_from_hops < L : Deref > (
1787
+ our_node_pubkey : & PublicKey , hops : & [ PublicKey ] , route_params : & RouteParameters , network : & NetworkGraph ,
1788
+ logger : L , random_seed_bytes : & [ u8 ; 32 ] ) -> Result < Route , LightningError >
1789
+ where L :: Target : Logger {
1790
+ let network_graph = network. read_only ( ) ;
1791
+ let mut route = build_route_from_hops_internal (
1792
+ our_node_pubkey, hops, & route_params. payment_params , & network_graph,
1793
+ route_params. final_value_msat , route_params. final_cltv_expiry_delta , logger, random_seed_bytes) ?;
1794
+ add_random_cltv_offset ( & mut route, & route_params. payment_params , & network_graph, random_seed_bytes) ;
1795
+ Ok ( route)
1796
+ }
1797
+
1798
+ fn build_route_from_hops_internal < L : Deref > (
1799
+ our_node_pubkey : & PublicKey , hops : & [ PublicKey ] , payment_params : & PaymentParameters ,
1800
+ network_graph : & ReadOnlyNetworkGraph , final_value_msat : u64 , final_cltv_expiry_delta : u32 ,
1801
+ logger : L , random_seed_bytes : & [ u8 ; 32 ] ) -> Result < Route , LightningError > where L :: Target : Logger {
1802
+
1803
+ struct HopScorer < ' a > { hops : & ' a [ PublicKey ] }
1804
+
1805
+ impl Score for HopScorer < ' _ > {
1806
+ fn channel_penalty_msat ( & self , _short_channel_id : u64 , source : & NodeId , target : & NodeId ,
1807
+ _usage : ChannelUsage ) -> u64
1808
+ {
1809
+ for i in 0 ..self . hops . len ( ) -1 {
1810
+ let cur_id = NodeId :: from_pubkey ( & self . hops [ i] ) ;
1811
+ let next_id = NodeId :: from_pubkey ( & self . hops [ i+1 ] ) ;
1812
+ if cur_id == * source && next_id == * target {
1813
+ return 0 ;
1814
+ }
1815
+ }
1816
+ u64:: max_value ( )
1817
+ }
1818
+
1819
+ fn payment_path_failed ( & mut self , _path : & [ & RouteHop ] , _short_channel_id : u64 ) { }
1820
+
1821
+ fn payment_path_successful ( & mut self , _path : & [ & RouteHop ] ) { }
1822
+ }
1823
+
1824
+ let scorer = HopScorer { hops : & [ & [ * our_node_pubkey] , hops] . concat ( ) } ;
1825
+
1826
+ get_route ( our_node_pubkey, payment_params, network_graph, None , final_value_msat,
1827
+ final_cltv_expiry_delta, logger, & scorer, random_seed_bytes)
1828
+ }
1829
+
1788
1830
#[ cfg( test) ]
1789
1831
mod tests {
1790
1832
use routing:: network_graph:: { NetworkGraph , NetGraphMsgHandler , NodeId } ;
1791
- use routing:: router:: { get_route, add_random_cltv_offset, default_node_features,
1833
+ use routing:: router:: { get_route, build_route_from_hops_internal , add_random_cltv_offset, default_node_features,
1792
1834
PaymentParameters , Route , RouteHint , RouteHintHop , RouteHop , RoutingFees ,
1793
1835
DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA , MAX_PATH_LENGTH_ESTIMATE } ;
1794
1836
use routing:: scoring:: { ChannelUsage , Score } ;
@@ -5486,6 +5528,26 @@ mod tests {
5486
5528
assert ! ( path_plausibility. iter( ) . all( |x| * x) ) ;
5487
5529
}
5488
5530
5531
+ #[ test]
5532
+ fn builds_correct_path_from_hops ( ) {
5533
+ let ( secp_ctx, network, _, _, logger) = build_graph ( ) ;
5534
+ let ( _, our_id, _, nodes) = get_nodes ( & secp_ctx) ;
5535
+ let network_graph = network. read_only ( ) ;
5536
+
5537
+ let keys_manager = test_utils:: TestKeysInterface :: new ( & [ 0u8 ; 32 ] , Network :: Testnet ) ;
5538
+ let random_seed_bytes = keys_manager. get_secure_random_bytes ( ) ;
5539
+
5540
+ let payment_params = PaymentParameters :: from_node_id ( nodes[ 3 ] ) ;
5541
+ let hops = [ nodes[ 1 ] , nodes[ 2 ] , nodes[ 4 ] , nodes[ 3 ] ] ;
5542
+ let route = build_route_from_hops_internal ( & our_id, & hops, & payment_params,
5543
+ & network_graph, 100 , 0 , Arc :: clone ( & logger) , & random_seed_bytes) . unwrap ( ) ;
5544
+ let route_hop_pubkeys = route. paths [ 0 ] . iter ( ) . map ( |hop| hop. pubkey ) . collect :: < Vec < _ > > ( ) ;
5545
+ assert_eq ! ( hops. len( ) , route. paths[ 0 ] . len( ) ) ;
5546
+ for ( idx, hop_pubkey) in hops. iter ( ) . enumerate ( ) {
5547
+ assert ! ( * hop_pubkey == route_hop_pubkeys[ idx] ) ;
5548
+ }
5549
+ }
5550
+
5489
5551
#[ cfg( not( feature = "no-std" ) ) ]
5490
5552
pub ( super ) fn random_init_seed ( ) -> u64 {
5491
5553
// Because the default HashMap in std pulls OS randomness, we can use it as a (bad) RNG.
0 commit comments