@@ -495,30 +495,53 @@ pub trait SimNetwork: Send + Sync {
495495}
496496
497497/// A trait for custom pathfinding implementations.
498- pub trait PathFinder < ' a > : Send + Sync {
498+ /// Finds a route from the source node to the destination node for the specified amount.
499+ ///
500+ /// # Arguments
501+ /// * `source` - The public key of the node initiating the payment.
502+ /// * `dest` - The public key of the destination node to receive the payment.
503+ /// * `amount_msat` - The amount to send in millisatoshis.
504+ /// * `pathfinding_graph` - The network graph containing channel topology and routing information.
505+ ///
506+ /// # Returns
507+ /// Returns a `Route` containing the payment path, or a `SimulationError` if no route is found.
508+
509+ pub trait PathFinder : Send + Sync + Clone {
499510 fn find_route (
500511 & self ,
501512 source : & PublicKey ,
502513 dest : PublicKey ,
503514 amount_msat : u64 ,
504- pathfinding_graph : & NetworkGraph < & ' a WrappedLog > ,
505- scorer : & ProbabilisticScorer < Arc < NetworkGraph < & ' a WrappedLog > > , & ' a WrappedLog > ,
515+ pathfinding_graph : & NetworkGraph < & ' static WrappedLog > ,
506516 ) -> Result < Route , SimulationError > ;
507517}
508518
509- /// Default pathfinder that uses LDK's pathfinding algorithm.
519+ /// The default pathfinding implementation that uses LDK's built-in pathfinding algorithm.
510520#[ derive( Clone ) ]
511521pub struct DefaultPathFinder ;
512522
513- impl < ' a > PathFinder < ' a > for DefaultPathFinder {
523+ impl DefaultPathFinder {
524+ pub fn new ( ) -> Self {
525+ Self
526+ }
527+ }
528+
529+ impl PathFinder for DefaultPathFinder {
514530 fn find_route (
515531 & self ,
516532 source : & PublicKey ,
517533 dest : PublicKey ,
518534 amount_msat : u64 ,
519- pathfinding_graph : & NetworkGraph < & ' a WrappedLog > ,
520- scorer : & ProbabilisticScorer < Arc < NetworkGraph < & ' a WrappedLog > > , & ' a WrappedLog > ,
535+ pathfinding_graph : & NetworkGraph < & ' static WrappedLog > ,
521536 ) -> Result < Route , SimulationError > {
537+ let scorer_graph = NetworkGraph :: new ( bitcoin:: Network :: Regtest , & WrappedLog { } ) ;
538+ let scorer = ProbabilisticScorer :: new (
539+ ProbabilisticScoringDecayParameters :: default ( ) ,
540+ Arc :: new ( scorer_graph) ,
541+ & WrappedLog { } ,
542+ ) ;
543+
544+ // Call LDK's find_route with the scorer (LDK-specific requirement)
522545 find_route (
523546 source,
524547 & RouteParameters {
@@ -529,10 +552,10 @@ impl<'a> PathFinder<'a> for DefaultPathFinder {
529552 final_value_msat : amount_msat,
530553 max_total_routing_fee_msat : None ,
531554 } ,
532- pathfinding_graph,
555+ pathfinding_graph, // This is the real network graph used for pathfinding
533556 None ,
534557 & WrappedLog { } ,
535- scorer,
558+ & scorer, // LDK requires a scorer, so we provide a simple one
536559 & Default :: default ( ) ,
537560 & [ 0 ; 32 ] ,
538561 )
@@ -551,38 +574,25 @@ pub struct SimNode<'a, T: SimNetwork, P: PathFinder<'a> = DefaultPathFinder> {
551574 /// Tracks the channel that will provide updates for payments by hash.
552575 in_flight : HashMap < PaymentHash , Receiver < Result < PaymentResult , LightningError > > > ,
553576 /// A read-only graph used for pathfinding.
554- pathfinding_graph : Arc < NetworkGraph < & ' a WrappedLog > > ,
555- /// Probabilistic scorer used to rank paths through the network for routing. This is reused across
556- /// multiple payments to maintain scoring state.
557- scorer : ProbabilisticScorer < Arc < NetworkGraph < & ' a WrappedLog > > , & ' a WrappedLog > ,
577+ pathfinding_graph : Arc < NetworkGraph < & ' static WrappedLog > > ,
558578 /// The pathfinder implementation to use for finding routes
559579 pathfinder : P ,
560580}
561581
562- impl < ' a , T : SimNetwork , P : PathFinder < ' a > > SimNode < ' a , T , P > {
582+ impl < T : SimNetwork , P : PathFinder > SimNode < T , P > {
563583 /// Creates a new simulation node that refers to the high level network coordinator provided to process payments
564584 /// on its behalf. The pathfinding graph is provided separately so that each node can handle its own pathfinding.
565585 pub fn new (
566586 pubkey : PublicKey ,
567587 payment_network : Arc < Mutex < T > > ,
568- pathfinding_graph : Arc < NetworkGraph < & ' a WrappedLog > > ,
588+ pathfinding_graph : Arc < NetworkGraph < & ' static WrappedLog > > ,
569589 pathfinder : P ,
570590 ) -> Self {
571- // Initialize the probabilistic scorer with default parameters for learning from payment
572- // history. These parameters control how much successful/failed payments affect routing
573- // scores and how quickly these scores decay over time.
574- let scorer = ProbabilisticScorer :: new (
575- ProbabilisticScoringDecayParameters :: default ( ) ,
576- pathfinding_graph. clone ( ) ,
577- & WrappedLog { } ,
578- ) ;
579-
580591 SimNode {
581592 info : node_info ( pubkey) ,
582593 network : payment_network,
583594 in_flight : HashMap :: new ( ) ,
584595 pathfinding_graph,
585- scorer,
586596 pathfinder,
587597 }
588598 }
@@ -664,7 +674,7 @@ fn node_info(pubkey: PublicKey) -> NodeInfo {
664674}
665675
666676#[ async_trait]
667- impl < ' a , T : SimNetwork , P : PathFinder < ' a > > LightningNode for SimNode < ' a , T , P > {
677+ impl < T : SimNetwork , P : PathFinder > LightningNode for SimNode < T , P > {
668678 fn get_info ( & self ) -> & NodeInfo {
669679 & self . info
670680 }
@@ -686,7 +696,6 @@ impl<'a, T: SimNetwork, P: PathFinder<'a>> LightningNode for SimNode<'a, T, P> {
686696 dest,
687697 amount_msat,
688698 & self . pathfinding_graph ,
689- & self . scorer ,
690699 ) {
691700 Ok ( route) => route,
692701 Err ( e) => {
@@ -1064,7 +1073,7 @@ pub async fn ln_node_from_graph<P>(
10641073 pathfinder : P ,
10651074) -> HashMap < PublicKey , Arc < Mutex < dyn LightningNode > > >
10661075where
1067- P : for < ' a > PathFinder < ' a > + Clone + ' static ,
1076+ P : PathFinder + ' static ,
10681077{
10691078 let mut nodes: HashMap < PublicKey , Arc < Mutex < dyn LightningNode > > > = HashMap :: new ( ) ;
10701079
@@ -1563,7 +1572,6 @@ mod tests {
15631572 use mockall:: mock;
15641573 use ntest:: assert_true;
15651574 use std:: time:: Duration ;
1566- use tokio:: sync:: oneshot;
15671575 use tokio:: time:: { self , timeout} ;
15681576
15691577 /// Creates a test channel policy with its maximum HTLC size set to half of the in flight limit of the channel.
@@ -1953,7 +1961,12 @@ mod tests {
19531961
19541962 // Create a simulated node for the first channel in our network.
19551963 let pk = channels[ 0 ] . node_1 . policy . pubkey ;
1956- let mut node = SimNode :: new ( pk, sim_network. clone ( ) , Arc :: new ( graph) , DefaultPathFinder ) ;
1964+ let mut node = SimNode :: new (
1965+ pk,
1966+ sim_network. clone ( ) ,
1967+ Arc :: new ( graph) ,
1968+ DefaultPathFinder :: new ( ) ,
1969+ ) ;
19571970
19581971 // Prime mock to return node info from lookup and assert that we get the pubkey we're expecting.
19591972 let lookup_pk = channels[ 3 ] . node_1 . policy . pubkey ;
@@ -2038,16 +2051,15 @@ mod tests {
20382051 }
20392052
20402053 /// Contains elements required to test dispatch_payment functionality.
2041- struct DispatchPaymentTestKit < ' a > {
2054+ struct DispatchPaymentTestKit {
20422055 graph : SimGraph ,
20432056 nodes : Vec < PublicKey > ,
2044- routing_graph : Arc < NetworkGraph < & ' a WrappedLog > > ,
2045- scorer : ProbabilisticScorer < Arc < NetworkGraph < & ' a WrappedLog > > , & ' a WrappedLog > ,
2057+ routing_graph : Arc < NetworkGraph < & ' static WrappedLog > > ,
20462058 shutdown : ( Trigger , Listener ) ,
20472059 pathfinder : DefaultPathFinder ,
20482060 }
20492061
2050- impl DispatchPaymentTestKit < ' _ > {
2062+ impl DispatchPaymentTestKit {
20512063 /// Creates a test graph with a set of nodes connected by three channels, with all the capacity of the channel
20522064 /// on the side of the first node. For example, if called with capacity = 100 it will set up the following
20532065 /// network:
@@ -2065,12 +2077,6 @@ mod tests {
20652077 populate_network_graph ( channels. clone ( ) , Arc :: new ( SystemClock { } ) ) . unwrap ( ) ,
20662078 ) ;
20672079
2068- let scorer = ProbabilisticScorer :: new (
2069- ProbabilisticScoringDecayParameters :: default ( ) ,
2070- routing_graph. clone ( ) ,
2071- & WrappedLog { } ,
2072- ) ;
2073-
20742080 // Collect pubkeys in-order, pushing the last node on separately because they don't have an outgoing
20752081 // channel (they are not node_1 in any channel, only node_2).
20762082 let mut nodes = channels
@@ -2091,9 +2097,8 @@ mod tests {
20912097 . expect ( "could not create test graph" ) ,
20922098 nodes,
20932099 routing_graph,
2094- scorer,
20952100 shutdown : shutdown_clone,
2096- pathfinder : DefaultPathFinder ,
2101+ pathfinder : DefaultPathFinder :: new ( ) ,
20972102 } ;
20982103
20992104 // Assert that our channel balance is all on the side of the channel opener when we start up.
@@ -2138,18 +2143,14 @@ mod tests {
21382143 ) -> ( Route , Result < PaymentResult , LightningError > ) {
21392144 let route = self
21402145 . pathfinder
2141- . find_route ( & source, dest, amt, & self . routing_graph , & self . scorer )
2146+ . find_route ( & source, dest, amt, & self . routing_graph )
21422147 . unwrap ( ) ;
2148+ let ( sender, receiver) = tokio:: sync:: oneshot:: channel ( ) ;
21432149
2144- let ( sender, receiver) = oneshot:: channel ( ) ;
21452150 self . graph
2146- . dispatch_payment ( source, route. clone ( ) , PaymentHash ( [ 1 ; 32 ] ) , sender) ;
2147-
2148- let payment_result = timeout ( Duration :: from_millis ( 10 ) , receiver) . await ;
2149- // Assert that we receive from the channel or fail.
2150- assert ! ( payment_result. is_ok( ) ) ;
2151+ . dispatch_payment ( source, route. clone ( ) , PaymentHash ( [ 0 ; 32 ] ) , sender) ;
21512152
2152- ( route, payment_result . unwrap ( ) . unwrap ( ) )
2153+ ( route, receiver . await . unwrap ( ) )
21532154 }
21542155
21552156 // Sets the balance on the channel to the tuple provided, used to arrange liquidity for testing.
0 commit comments