Skip to content

Commit 3e72658

Browse files
committed
Remove LDK specific scoring from PathFinder trait
1 parent aaef04d commit 3e72658

File tree

2 files changed

+87
-99
lines changed

2 files changed

+87
-99
lines changed

sim-cli/src/parsing.rs

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ struct NodeMapping {
225225
alias_node_map: HashMap<String, NodeInfo>,
226226
}
227227

228-
pub async fn create_simulation_with_network<P: for<'a> PathFinder<'a> + Clone + 'static>(
228+
pub async fn create_simulation_with_network<P: PathFinder + Clone + 'static>(
229229
cli: &Cli,
230230
sim_params: &SimParams,
231231
tasks: TaskTracker,
@@ -278,7 +278,6 @@ pub async fn create_simulation_with_network<P: for<'a> PathFinder<'a> + Clone +
278278
.map_err(|e| SimulationError::SimulatedNetworkError(format!("{:?}", e)))?,
279279
);
280280

281-
// Pass the pathfinder to ln_node_from_graph
282281
let nodes = ln_node_from_graph(simulation_graph.clone(), routing_graph, pathfinder).await;
283282
let validated_activities =
284283
get_validated_activities(&nodes, nodes_info, sim_params.activity.clone()).await?;
@@ -621,7 +620,7 @@ mod tests {
621620
(secret_key, public_key)
622621
}
623622

624-
/// Helper function to create simulated channels for testing
623+
/// Helper function to create simulated channels for testing.
625624
fn create_simulated_channels(num_channels: usize, capacity_msat: u64) -> Vec<SimulatedChannel> {
626625
let mut channels = Vec::new();
627626
for i in 0..num_channels {
@@ -657,66 +656,67 @@ mod tests {
657656
channels
658657
}
659658

660-
/// A pathfinder that always fails to find a path
659+
/// A pathfinder that always fails to find a path.
661660
#[derive(Clone)]
662661
pub struct AlwaysFailPathFinder;
663662

664-
impl<'a> PathFinder<'a> for AlwaysFailPathFinder {
663+
impl PathFinder for AlwaysFailPathFinder {
665664
fn find_route(
666665
&self,
667666
_source: &PublicKey,
668667
_dest: PublicKey,
669668
_amount_msat: u64,
670-
_pathfinding_graph: &NetworkGraph<&'a WrappedLog>,
671-
_scorer: &ProbabilisticScorer<Arc<NetworkGraph<&'a WrappedLog>>, &'a WrappedLog>,
669+
_pathfinding_graph: &NetworkGraph<&'static WrappedLog>,
672670
) -> Result<Route, SimulationError> {
673671
Err(SimulationError::SimulatedNetworkError(
674672
"No route found".to_string(),
675673
))
676674
}
677675
}
678676

679-
/// A pathfinder that only returns single-hop paths
677+
/// A pathfinder that only returns single-hop paths.
680678
#[derive(Clone)]
681679
pub struct SingleHopOnlyPathFinder;
682680

683-
impl<'a> PathFinder<'a> for SingleHopOnlyPathFinder {
681+
impl PathFinder for SingleHopOnlyPathFinder {
684682
fn find_route(
685683
&self,
686684
source: &PublicKey,
687685
dest: PublicKey,
688686
amount_msat: u64,
689-
pathfinding_graph: &NetworkGraph<&'a WrappedLog>,
690-
scorer: &ProbabilisticScorer<Arc<NetworkGraph<&'a WrappedLog>>, &'a WrappedLog>,
687+
pathfinding_graph: &NetworkGraph<&'static WrappedLog>,
691688
) -> Result<Route, SimulationError> {
692-
// Try to find a direct route only (single hop)
693-
let route_params = RouteParameters {
694-
payment_params: PaymentParameters::from_node_id(dest, 0)
695-
.with_max_total_cltv_expiry_delta(u32::MAX)
696-
.with_max_path_count(1)
697-
.with_max_channel_saturation_power_of_half(1),
698-
final_value_msat: amount_msat,
699-
max_total_routing_fee_msat: None,
700-
};
701-
702-
// Try to find a route - if it fails or has more than one hop, return an error
689+
let scorer = ProbabilisticScorer::new(
690+
ProbabilisticScoringDecayParameters::default(),
691+
pathfinding_graph,
692+
&WrappedLog {},
693+
);
694+
695+
// Try to find a route - if it fails or has more than one hop, return an error.
703696
match find_route(
704697
source,
705-
&route_params,
698+
&RouteParameters {
699+
payment_params: PaymentParameters::from_node_id(dest, 0)
700+
.with_max_total_cltv_expiry_delta(u32::MAX)
701+
.with_max_path_count(1)
702+
.with_max_channel_saturation_power_of_half(1),
703+
final_value_msat: amount_msat,
704+
max_total_routing_fee_msat: None,
705+
},
706706
pathfinding_graph,
707707
None,
708708
&WrappedLog {},
709-
scorer,
709+
&scorer,
710710
&Default::default(),
711711
&[0; 32],
712712
) {
713713
Ok(route) => {
714-
// Check if the route has exactly one hop
714+
// Only allow single-hop routes.
715715
if route.paths.len() == 1 && route.paths[0].hops.len() == 1 {
716716
Ok(route)
717717
} else {
718718
Err(SimulationError::SimulatedNetworkError(
719-
"No direct route found".to_string(),
719+
"Only single-hop routes allowed".to_string(),
720720
))
721721
}
722722
},
@@ -735,15 +735,9 @@ mod tests {
735735
let source = channels[0].get_node_1_pubkey();
736736
let dest = channels[2].get_node_2_pubkey();
737737

738-
let scorer = ProbabilisticScorer::new(
739-
ProbabilisticScoringDecayParameters::default(),
740-
routing_graph.clone(),
741-
&WrappedLog {},
742-
);
743-
744-
let result = pathfinder.find_route(&source, dest, 100_000, &routing_graph, &scorer);
738+
let result = pathfinder.find_route(&source, dest, 100_000, &routing_graph);
745739

746-
// Should always fail
740+
// Should always fail.
747741
assert!(result.is_err());
748742
}
749743

@@ -756,31 +750,24 @@ mod tests {
756750
let pathfinder = SingleHopOnlyPathFinder;
757751
let source = channels[0].get_node_1_pubkey();
758752

759-
let scorer = ProbabilisticScorer::new(
760-
ProbabilisticScoringDecayParameters::default(),
761-
routing_graph.clone(),
762-
&WrappedLog {},
763-
);
764-
765-
// Test direct connection (should work)
753+
// Test direct connection (should work).
766754
let direct_dest = channels[0].get_node_2_pubkey();
767-
let result = pathfinder.find_route(&source, direct_dest, 100_000, &routing_graph, &scorer);
755+
let result = pathfinder.find_route(&source, direct_dest, 100_000, &routing_graph);
768756

769757
if result.is_ok() {
770758
let route = result.unwrap();
771759
assert_eq!(route.paths[0].hops.len(), 1); // Only one hop
772760
}
773761

774-
// Test indirect connection (should fail)
762+
// Test indirect connection (should fail).
775763
let indirect_dest = channels[2].get_node_2_pubkey();
776-
let _result =
777-
pathfinder.find_route(&source, indirect_dest, 100_000, &routing_graph, &scorer);
764+
let _result = pathfinder.find_route(&source, indirect_dest, 100_000, &routing_graph);
778765

779-
// May fail because no direct route exists
766+
// May fail because no direct route exists.
780767
// (depends on your test network topology)
781768
}
782769

783-
/// Test that different pathfinders produce different behavior in payments
770+
/// Test that different pathfinders produce different behavior in payments.
784771
#[tokio::test]
785772
async fn test_pathfinder_affects_payment_behavior() {
786773
let channels = create_simulated_channels(3, 1_000_000_000);
@@ -798,7 +785,7 @@ mod tests {
798785
let routing_graph =
799786
Arc::new(populate_network_graph(channels.clone(), Arc::new(SystemClock {})).unwrap());
800787

801-
// Create nodes with different pathfinders
788+
// Create nodes with different pathfinders.
802789
let nodes_default = ln_node_from_graph(
803790
sim_graph.clone(),
804791
routing_graph.clone(),
@@ -813,7 +800,7 @@ mod tests {
813800
)
814801
.await;
815802

816-
// Both should create the same structure
803+
// Both should create the same structure.
817804
assert_eq!(nodes_default.len(), nodes_fail.len());
818805
}
819806
}

0 commit comments

Comments
 (0)