]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Hard-code scorer parameters to `ProbabilisticScoringFeeParameters`
authorMatt Corallo <git@bluematt.me>
Sat, 21 Oct 2023 02:28:53 +0000 (02:28 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 14 Oct 2024 19:15:11 +0000 (19:15 +0000)
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
lightning/src/ln/channelmanager.rs
lightning/src/routing/router.rs
lightning/src/routing/scoring.rs
lightning/src/util/test_utils.rs

index bbe1545d044c8ea363d52dda2c99d1e425693c62..410f28a2e19d9e22cf0b258828c6d599e069e077 100644 (file)
@@ -1141,8 +1141,6 @@ mod tests {
                                Arc<test_utils::TestLogger>,
                                Arc<KeysManager>,
                                Arc<LockingWrapper<TestScorer>>,
-                               (),
-                               TestScorer,
                        >,
                >,
                Arc<test_utils::TestLogger>,
@@ -1404,10 +1402,10 @@ mod tests {
        }
 
        impl ScoreLookUp for TestScorer {
+               #[cfg(not(c_bindings))]
                type ScoreParams = ();
                fn channel_penalty_msat(
-                       &self, _candidate: &CandidateRouteHop, _usage: ChannelUsage,
-                       _score_params: &Self::ScoreParams,
+                       &self, _candidate: &CandidateRouteHop, _usage: ChannelUsage, _score_params: &lightning::routing::scoring::ProbabilisticScoringFeeParameters
                ) -> u64 {
                        unimplemented!();
                }
index 4333e7e50dc5c64e2453f87eca47ca7d74f74982..6bcf3099b6de39a01c3b323aa261a2931dbaee7a 100644 (file)
@@ -1165,8 +1165,6 @@ pub type SimpleArcChannelManager<M, T, F, L> = ChannelManager<
                Arc<L>,
                Arc<KeysManager>,
                Arc<RwLock<ProbabilisticScorer<Arc<NetworkGraph<Arc<L>>>, Arc<L>>>>,
-               ProbabilisticScoringFeeParameters,
-               ProbabilisticScorer<Arc<NetworkGraph<Arc<L>>>, Arc<L>>,
        >>,
        Arc<L>
 >;
@@ -1196,8 +1194,6 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, M, T, F, L> =
                        &'g L,
                        &'c KeysManager,
                        &'h RwLock<ProbabilisticScorer<&'f NetworkGraph<&'g L>, &'g L>>,
-                       ProbabilisticScoringFeeParameters,
-                       ProbabilisticScorer<&'f NetworkGraph<&'g L>, &'g L>
                >,
                &'g L
        >;
index a25609c89bbe8f85414989f6abeef2044a58fb89..969b60f39e2ba439a534fc9a658ccc1c35259cc8 100644 (file)
@@ -45,32 +45,32 @@ pub use lightning_types::routing::{RouteHint, RouteHintHop};
 ///
 /// Implements [`MessageRouter`] by delegating to [`DefaultMessageRouter`]. See those docs for
 /// privacy implications.
-pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> where
+pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref, S: Deref> where
        L::Target: Logger,
-       S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>,
+       S::Target: for <'a> LockableScore<'a>,
        ES::Target: EntropySource,
 {
        network_graph: G,
        logger: L,
        entropy_source: ES,
        scorer: S,
-       score_params: SP,
+       score_params: crate::routing::scoring::ProbabilisticScoringFeeParameters,
 }
 
-impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> DefaultRouter<G, L, ES, S, SP, Sc> where
+impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref, S: Deref> DefaultRouter<G, L, ES, S> where
        L::Target: Logger,
-       S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>,
+       S::Target: for <'a> LockableScore<'a>,
        ES::Target: EntropySource,
 {
        /// Creates a new router.
-       pub fn new(network_graph: G, logger: L, entropy_source: ES, scorer: S, score_params: SP) -> Self {
+       pub fn new(network_graph: G, logger: L, entropy_source: ES, scorer: S, score_params: crate::routing::scoring::ProbabilisticScoringFeeParameters) -> Self {
                Self { network_graph, logger, entropy_source, scorer, score_params }
        }
 }
 
-impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> Router for DefaultRouter<G, L, ES, S, SP, Sc> where
+impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref, S: Deref> Router for DefaultRouter<G, L, ES, S> where
        L::Target: Logger,
-       S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>,
+       S::Target: for <'a> LockableScore<'a>,
        ES::Target: EntropySource,
 {
        fn find_route(
@@ -183,9 +183,9 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref, S: Deref, SP: Size
        }
 }
 
-impl< G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref, S: Deref, SP: Sized, Sc: ScoreLookUp<ScoreParams = SP>> MessageRouter for DefaultRouter<G, L, ES, S, SP, Sc> where
+impl< G: Deref<Target = NetworkGraph<L>>, L: Deref, ES: Deref, S: Deref> MessageRouter for DefaultRouter<G, L, ES, S> where
        L::Target: Logger,
-       S::Target: for <'a> LockableScore<'a, ScoreLookUp = Sc>,
+       S::Target: for <'a> LockableScore<'a>,
        ES::Target: EntropySource,
 {
        fn find_path(
@@ -270,8 +270,9 @@ impl<'a, S: Deref> ScorerAccountingForInFlightHtlcs<'a, S> where S::Target: Scor
 }
 
 impl<'a, S: Deref> ScoreLookUp for ScorerAccountingForInFlightHtlcs<'a, S> where S::Target: ScoreLookUp {
+       #[cfg(not(c_bindings))]
        type ScoreParams = <S::Target as ScoreLookUp>::ScoreParams;
-       fn channel_penalty_msat(&self, candidate: &CandidateRouteHop, usage: ChannelUsage, score_params: &Self::ScoreParams) -> u64 {
+       fn channel_penalty_msat(&self, candidate: &CandidateRouteHop, usage: ChannelUsage, score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters) -> u64 {
                let target = match candidate.target() {
                        Some(target) => target,
                        None => return self.scorer.channel_penalty_msat(candidate, usage, score_params),
@@ -2079,7 +2080,7 @@ fn sort_first_hop_channels(
 pub fn find_route<L: Deref, GL: Deref, S: ScoreLookUp>(
        our_node_pubkey: &PublicKey, route_params: &RouteParameters,
        network_graph: &NetworkGraph<GL>, 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<Route, LightningError>
 where L::Target: Logger, GL::Target: Logger {
        let graph_lock = network_graph.read_only();
@@ -2091,7 +2092,7 @@ where L::Target: Logger, GL::Target: Logger {
 
 pub(crate) fn get_route<L: Deref, S: ScoreLookUp>(
        our_node_pubkey: &PublicKey, route_params: &RouteParameters, network_graph: &ReadOnlyNetworkGraph,
-       first_hops: Option<&[&ChannelDetails]>, logger: L, scorer: &S, score_params: &S::ScoreParams,
+       first_hops: Option<&[&ChannelDetails]>, logger: L, scorer: &S, score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters,
        _random_seed_bytes: &[u8; 32]
 ) -> Result<Route, LightningError>
 where L::Target: Logger {
@@ -3509,9 +3510,10 @@ fn build_route_from_hops_internal<L: Deref>(
        }
 
        impl ScoreLookUp for HopScorer {
+               #[cfg(not(c_bindings))]
                type ScoreParams = ();
                fn channel_penalty_msat(&self, candidate: &CandidateRouteHop,
-                       _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() {
@@ -7010,8 +7012,9 @@ mod tests {
                fn write<W: Writer>(&self, _w: &mut W) -> Result<(), crate::io::Error> { unimplemented!() }
        }
        impl ScoreLookUp for BadChannelScorer {
+               #[cfg(not(c_bindings))]
                type ScoreParams = ();
-               fn channel_penalty_msat(&self, candidate: &CandidateRouteHop, _: ChannelUsage, _score_params:&Self::ScoreParams) -> u64 {
+               fn channel_penalty_msat(&self, candidate: &CandidateRouteHop, _: ChannelUsage, _score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters) -> u64 {
                        if candidate.short_channel_id() == Some(self.short_channel_id) { u64::max_value()  } else { 0  }
                }
        }
@@ -7026,8 +7029,9 @@ mod tests {
        }
 
        impl ScoreLookUp for BadNodeScorer {
+               #[cfg(not(c_bindings))]
                type ScoreParams = ();
-               fn channel_penalty_msat(&self, candidate: &CandidateRouteHop, _: ChannelUsage, _score_params:&Self::ScoreParams) -> u64 {
+               fn channel_penalty_msat(&self, candidate: &CandidateRouteHop, _: ChannelUsage, _score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters) -> u64 {
                        if candidate.target() == Some(self.node_id) { u64::max_value() } else { 0 }
                }
        }
@@ -8817,7 +8821,7 @@ pub(crate) mod bench_utils {
        }
 
        pub(crate) fn generate_test_routes<S: ScoreLookUp + ScoreUpdate>(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();
@@ -8947,7 +8951,7 @@ pub mod benches {
 
        fn generate_routes<S: ScoreLookUp + ScoreUpdate>(
                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,
        ) {
                // First, get 100 (source, destination) pairs for which route-getting actually succeeds...
index 61b0086ebc63cb2e71a5d16e3af7588b67b838e9..42ce01a034c8b85555a652a7b8e3fe054dbadd56 100644 (file)
@@ -88,9 +88,13 @@ macro_rules! define_score { ($($supertrait: path)*) => {
 ///
 /// Scoring is in terms of fees willing to be paid in order to avoid routing through a channel.
 pub trait ScoreLookUp {
+       #[cfg(not(c_bindings))]
        /// A configurable type which should contain various passed-in parameters for configuring the scorer,
        /// on a per-routefinding-call basis through to the scorer methods,
        /// which are used to determine the parameters for the suitability of channels for use.
+       ///
+       /// Note that due to limitations in many other languages' generics features, language bindings
+       /// use [`ProbabilisticScoringFeeParameters`] for the parameters on all scorers.
        type ScoreParams;
        /// Returns the fee in msats willing to be paid to avoid routing `send_amt_msat` through the
        /// given channel in the direction from `source` to `target`.
@@ -101,7 +105,7 @@ pub trait ScoreLookUp {
        /// [`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, candidate: &CandidateRouteHop, usage: ChannelUsage, score_params: &Self::ScoreParams
+               &self, candidate: &CandidateRouteHop, usage: ChannelUsage, score_params: &ProbabilisticScoringFeeParameters
        ) -> u64;
 }
 
@@ -139,9 +143,10 @@ impl<T: ScoreLookUp + ScoreUpdate $(+ $supertrait)*> Score for T {}
 
 #[cfg(not(c_bindings))]
 impl<S: ScoreLookUp, T: Deref<Target=S>> ScoreLookUp for T {
+       #[cfg(not(c_bindings))]
        type ScoreParams = S::ScoreParams;
        fn channel_penalty_msat(
-               &self, candidate: &CandidateRouteHop, usage: ChannelUsage, score_params: &Self::ScoreParams
+               &self, candidate: &CandidateRouteHop, usage: ChannelUsage, score_params: &ProbabilisticScoringFeeParameters
        ) -> u64 {
                self.deref().channel_penalty_msat(candidate, usage, score_params)
        }
@@ -322,8 +327,9 @@ impl<'a, T: 'a + Score> Deref for MultiThreadedScoreLockRead<'a, T> {
 
 #[cfg(c_bindings)]
 impl<'a, T: Score> ScoreLookUp for MultiThreadedScoreLockRead<'a, T> {
+       #[cfg(not(c_bindings))]
        type ScoreParams = T::ScoreParams;
-       fn channel_penalty_msat(&self, candidate:&CandidateRouteHop, usage: ChannelUsage, score_params: &Self::ScoreParams
+       fn channel_penalty_msat(&self, candidate:&CandidateRouteHop, usage: ChannelUsage, score_params: &ProbabilisticScoringFeeParameters
        ) -> u64 {
                self.0.channel_penalty_msat(candidate, usage, score_params)
        }
@@ -404,8 +410,9 @@ impl FixedPenaltyScorer {
 }
 
 impl ScoreLookUp for FixedPenaltyScorer {
+       #[cfg(not(c_bindings))]
        type ScoreParams = ();
-       fn channel_penalty_msat(&self, _: &CandidateRouteHop, _: ChannelUsage, _score_params: &Self::ScoreParams) -> u64 {
+       fn channel_penalty_msat(&self, _: &CandidateRouteHop, _: ChannelUsage, _score_params: &ProbabilisticScoringFeeParameters) -> u64 {
                self.penalty_msat
        }
 }
@@ -1315,6 +1322,7 @@ DirectedChannelLiquidity<L, HT, T> {
 }
 
 impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ScoreLookUp for ProbabilisticScorer<G, L> where L::Target: Logger {
+       #[cfg(not(c_bindings))]
        type ScoreParams = ProbabilisticScoringFeeParameters;
        fn channel_penalty_msat(
                &self, candidate: &CandidateRouteHop, usage: ChannelUsage, score_params: &ProbabilisticScoringFeeParameters
index 7252162e269015a295546b7fd29537ac361b4f60..5f8f27bde3b50ae8a199ff1a5f9ec08efa58a3e8 100644 (file)
@@ -121,8 +121,6 @@ pub struct TestRouter<'a> {
                &'a TestLogger,
                Arc<RandomBytes>,
                &'a RwLock<TestScorer>,
-               (),
-               TestScorer,
        >,
        pub network_graph: Arc<NetworkGraph<&'a TestLogger>>,
        pub next_routes: Mutex<VecDeque<(RouteParameters, Option<Result<Route, LightningError>>)>>,
@@ -1493,9 +1491,10 @@ impl crate::util::ser::Writeable for TestScorer {
 }
 
 impl ScoreLookUp for TestScorer {
+       #[cfg(not(c_bindings))]
        type ScoreParams = ();
        fn channel_penalty_msat(
-               &self, candidate: &CandidateRouteHop, usage: ChannelUsage, _score_params: &Self::ScoreParams
+               &self, candidate: &CandidateRouteHop, usage: ChannelUsage, _score_params: &crate::routing::scoring::ProbabilisticScoringFeeParameters
        ) -> u64 {
                let short_channel_id = match candidate.globally_unique_short_channel_id() {
                        Some(scid) => scid,