From 71095d4c004015c7e3b6837d66a42bcc67ebc67e Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 18 Jul 2023 19:25:52 +0000 Subject: [PATCH] Hard-code scorer parameters to `ProbabilisticScoringFeeParameters` The scorer currently relies on an associated type for the fee parameters. This isn't supportable at all in bindings, and for lack of a better option we simply hard-code the parameter for all scorers to `ProbabilisticScoringFeeParameters`. --- lightning-background-processor/src/lib.rs | 6 ++--- lightning/src/ln/channelmanager.rs | 4 --- lightning/src/routing/router.rs | 32 +++++++++++------------ lightning/src/routing/scoring.rs | 7 ++--- lightning/src/util/test_utils.rs | 2 +- 5 files changed, 23 insertions(+), 28 deletions(-) diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index 586fac930..904032d5c 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -897,9 +897,7 @@ mod tests { Arc>>, Arc, - Arc>, - (), - TestScorer> + Arc>> >, Arc>; #[cfg(c_bindings)] @@ -1054,7 +1052,7 @@ mod tests { #[cfg(not(c_bindings))] type ScoreParams = (); fn channel_penalty_msat( - &self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId, _usage: ChannelUsage, _score_params: &Self::ScoreParams + &self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId, _usage: ChannelUsage, _score_params: &lightning::routing::scoring::ProbabilisticScoringFeeParameters ) -> u64 { unimplemented!(); } fn payment_path_failed(&mut self, actual_path: &Path, actual_short_channel_id: u64) { diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index ec1a5e73a..0365209ba 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -743,8 +743,6 @@ pub type SimpleArcChannelManager = ChannelManager< Arc>>, Arc, Arc>>, Arc>>>, - ProbabilisticScoringFeeParameters, - ProbabilisticScorer>>, Arc>, >>, Arc >; @@ -773,8 +771,6 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, M, T, F, L> = &'f NetworkGraph<&'g L>, &'g L, &'h Mutex, &'g L>>, - ProbabilisticScoringFeeParameters, - ProbabilisticScorer<&'f NetworkGraph<&'g L>, &'g L> >, &'g L >; diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index fb65297ea..90b93de7a 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -33,31 +33,31 @@ use core::{cmp, fmt}; use core::ops::{Deref, DerefMut}; /// A [`Router`] implemented using [`find_route`]. -pub struct DefaultRouter>, L: Deref, S: Deref, SP: Sized, Sc: Score> where +pub struct DefaultRouter>, L: Deref, S: Deref> where L::Target: Logger, - S::Target: for <'a> LockableScore<'a, Score = Sc>, + S::Target: for <'a> LockableScore<'a>, { network_graph: G, logger: L, random_seed_bytes: Mutex<[u8; 32]>, scorer: S, - score_params: SP + score_params: crate::routing::scoring::ProbabilisticScoringFeeParameters, } -impl>, L: Deref, S: Deref, SP: Sized, Sc: Score> DefaultRouter where +impl>, L: Deref, S: Deref> DefaultRouter where L::Target: Logger, - S::Target: for <'a> LockableScore<'a, Score = Sc>, + S::Target: for <'a> LockableScore<'a>, { /// Creates a new router. - pub fn new(network_graph: G, logger: L, random_seed_bytes: [u8; 32], scorer: S, score_params: SP) -> Self { + pub fn new(network_graph: G, logger: L, random_seed_bytes: [u8; 32], scorer: S, score_params: crate::routing::scoring::ProbabilisticScoringFeeParameters) -> Self { let random_seed_bytes = Mutex::new(random_seed_bytes); Self { network_graph, logger, random_seed_bytes, scorer, score_params } } } -impl< G: Deref>, L: Deref, S: Deref, SP: Sized, Sc: Score> Router for DefaultRouter where +impl< G: Deref>, L: Deref, S: Deref> Router for DefaultRouter where L::Target: Logger, - S::Target: for <'a> LockableScore<'a, Score = Sc>, + S::Target: for <'a> LockableScore<'a>, { fn find_route( &self, @@ -136,7 +136,7 @@ impl<'a, S: Score, SP: Sized> Writeable for ScorerAccountingFo impl<'a, S: Score, SP: Sized> Score for ScorerAccountingForInFlightHtlcs<'a, S, SP> { #[cfg(not(c_bindings))] type ScoreParams = S::ScoreParams; - fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage, score_params: &Self::ScoreParams) -> u64 { + fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage, score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters) -> u64 { if let Some(used_liquidity) = self.inflight_htlcs.used_liquidity_msat( source, target, short_channel_id ) { @@ -1413,7 +1413,7 @@ fn sort_first_hop_channels( pub fn find_route( our_node_pubkey: &PublicKey, route_params: &RouteParameters, network_graph: &NetworkGraph, first_hops: Option<&[&ChannelDetails]>, logger: L, - scorer: &S, score_params: &S::ScoreParams, random_seed_bytes: &[u8; 32] + scorer: &S, score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters, random_seed_bytes: &[u8; 32] ) -> Result where L::Target: Logger, GL::Target: Logger { let graph_lock = network_graph.read_only(); @@ -1426,7 +1426,7 @@ where L::Target: Logger, GL::Target: Logger { pub(crate) fn get_route( our_node_pubkey: &PublicKey, payment_params: &PaymentParameters, network_graph: &ReadOnlyNetworkGraph, - first_hops: Option<&[&ChannelDetails]>, final_value_msat: u64, logger: L, scorer: &S, score_params: &S::ScoreParams, + first_hops: Option<&[&ChannelDetails]>, final_value_msat: u64, logger: L, scorer: &S, score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters, _random_seed_bytes: &[u8; 32] ) -> Result where L::Target: Logger { @@ -2618,7 +2618,7 @@ fn build_route_from_hops_internal( #[cfg(not(c_bindings))] type ScoreParams = (); fn channel_penalty_msat(&self, _short_channel_id: u64, source: &NodeId, target: &NodeId, - _usage: ChannelUsage, _score_params: &Self::ScoreParams) -> u64 + _usage: ChannelUsage, _score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters) -> u64 { let mut cur_id = self.our_node_id; for i in 0..self.hop_ids.len() { @@ -5725,7 +5725,7 @@ mod tests { impl Score for BadChannelScorer { #[cfg(not(c_bindings))] type ScoreParams = (); - fn channel_penalty_msat(&self, short_channel_id: u64, _: &NodeId, _: &NodeId, _: ChannelUsage, _score_params:&Self::ScoreParams) -> u64 { + fn channel_penalty_msat(&self, short_channel_id: u64, _: &NodeId, _: &NodeId, _: ChannelUsage, _score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters) -> u64 { if short_channel_id == self.short_channel_id { u64::max_value() } else { 0 } } @@ -5747,7 +5747,7 @@ mod tests { impl Score for BadNodeScorer { #[cfg(not(c_bindings))] type ScoreParams = (); - fn channel_penalty_msat(&self, _: u64, _: &NodeId, target: &NodeId, _: ChannelUsage, _score_params:&Self::ScoreParams) -> u64 { + fn channel_penalty_msat(&self, _: u64, _: &NodeId, target: &NodeId, _: ChannelUsage, _score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters) -> u64 { if *target == self.node_id { u64::max_value() } else { 0 } } @@ -6819,7 +6819,7 @@ pub(crate) mod bench_utils { } pub(crate) fn generate_test_routes(graph: &NetworkGraph<&TestLogger>, scorer: &mut S, - score_params: &S::ScoreParams, features: Bolt11InvoiceFeatures, mut seed: u64, + score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters, features: Bolt11InvoiceFeatures, mut seed: u64, starting_amount: u64, route_count: usize, ) -> Vec<(ChannelDetails, PaymentParameters, u64)> { let payer = payer_pubkey(); @@ -6962,7 +6962,7 @@ pub mod benches { fn generate_routes( bench: &mut Criterion, graph: &NetworkGraph<&TestLogger>, mut scorer: S, - score_params: &S::ScoreParams, features: Bolt11InvoiceFeatures, starting_amount: u64, + score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters, features: Bolt11InvoiceFeatures, starting_amount: u64, bench_name: &'static str, ) { let payer = bench_utils::payer_pubkey(); diff --git a/lightning/src/routing/scoring.rs b/lightning/src/routing/scoring.rs index ab623ccb6..451683c91 100644 --- a/lightning/src/routing/scoring.rs +++ b/lightning/src/routing/scoring.rs @@ -105,7 +105,7 @@ pub trait Score $(: $supertrait)* { /// [`u64::max_value`] is given to indicate sufficient capacity for the invoice's full amount. /// Thus, implementations should be overflow-safe. fn channel_penalty_msat( - &self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage, score_params: &Self::ScoreParams + &self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage, score_params: &ProbabilisticScoringFeeParameters ) -> u64; /// Handles updating channel penalties after failing to route through a channel. @@ -124,8 +124,9 @@ pub trait Score $(: $supertrait)* { impl $(+ $supertrait)*> Score for T { #[cfg(not(c_bindings))] type ScoreParams = S::ScoreParams; + fn channel_penalty_msat( - &self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage, score_params: &Self::ScoreParams + &self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage, score_params: &ProbabilisticScoringFeeParameters ) -> u64 { self.deref().channel_penalty_msat(short_channel_id, source, target, usage, score_params) } @@ -293,7 +294,7 @@ impl FixedPenaltyScorer { impl Score for FixedPenaltyScorer { #[cfg(not(c_bindings))] type ScoreParams = (); - fn channel_penalty_msat(&self, _: u64, _: &NodeId, _: &NodeId, _: ChannelUsage, _score_params: &Self::ScoreParams) -> u64 { + fn channel_penalty_msat(&self, _: u64, _: &NodeId, _: &NodeId, _: ChannelUsage, _score_params: &ProbabilisticScoringFeeParameters) -> u64 { self.penalty_msat } diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index 6bf1d2cd6..77132369b 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -1033,7 +1033,7 @@ impl Score for TestScorer { #[cfg(not(c_bindings))] type ScoreParams = (); fn channel_penalty_msat( - &self, short_channel_id: u64, _source: &NodeId, _target: &NodeId, usage: ChannelUsage, _score_params: &Self::ScoreParams + &self, short_channel_id: u64, _source: &NodeId, _target: &NodeId, usage: ChannelUsage, _score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters ) -> u64 { if let Some(scorer_expectations) = self.scorer_expectations.borrow_mut().as_mut() { match scorer_expectations.pop_front() { -- 2.39.5