]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Drop separate `Locked` type on `LockableScore`
authorMatt Corallo <git@bluematt.me>
Wed, 19 Jul 2023 19:20:50 +0000 (19:20 +0000)
committerMatt Corallo <git@bluematt.me>
Sat, 22 Jul 2023 01:55:56 +0000 (01:55 +0000)
While this is useful in Rust, it is simply confusing in bindings,
as we just use the same type for the locked score - a trait impl.

lightning/src/routing/router.rs
lightning/src/routing/scoring.rs

index 90b93de7a7308973143d972c0ac3c82dc8d9e0e0..822c4842cbaf8e92e134825cf0592393420a7667 100644 (file)
@@ -73,7 +73,7 @@ impl< G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref> Router for Default
                };
                find_route(
                        payer, params, &self.network_graph, first_hops, &*self.logger,
-                       &ScorerAccountingForInFlightHtlcs::new(self.scorer.lock().deref_mut(), &inflight_htlcs),
+                       &ScorerAccountingForInFlightHtlcs::new(self.scorer.lock(), &inflight_htlcs),
                        &self.score_params,
                        &random_seed_bytes
                )
@@ -112,15 +112,15 @@ pub trait Router {
 /// [`find_route`].
 ///
 /// [`Score`]: crate::routing::scoring::Score
-pub struct ScorerAccountingForInFlightHtlcs<'a, S: Score<ScoreParams = SP>, SP: Sized> {
-       scorer: &'a mut S,
+pub struct ScorerAccountingForInFlightHtlcs<'a, S: Score> {
+       scorer: S,
        // Maps a channel's short channel id and its direction to the liquidity used up.
        inflight_htlcs: &'a InFlightHtlcs,
 }
 
-impl<'a, S: Score<ScoreParams = SP>, SP: Sized> ScorerAccountingForInFlightHtlcs<'a, S, SP> {
+impl<'a, S: Score> ScorerAccountingForInFlightHtlcs<'a, S> {
        /// Initialize a new `ScorerAccountingForInFlightHtlcs`.
-       pub fn new(scorer: &'a mut S, inflight_htlcs: &'a InFlightHtlcs) -> Self {
+       pub fn new(scorer: S, inflight_htlcs: &'a InFlightHtlcs) -> Self {
                ScorerAccountingForInFlightHtlcs {
                        scorer,
                        inflight_htlcs
@@ -129,11 +129,11 @@ impl<'a, S: Score<ScoreParams = SP>, SP: Sized> ScorerAccountingForInFlightHtlcs
 }
 
 #[cfg(c_bindings)]
-impl<'a, S: Score<ScoreParams = SP>, SP: Sized> Writeable for ScorerAccountingForInFlightHtlcs<'a, S, SP> {
+impl<'a, S: Score> Writeable for ScorerAccountingForInFlightHtlcs<'a, S> {
        fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> { self.scorer.write(writer) }
 }
 
-impl<'a, S: Score<ScoreParams = SP>, SP: Sized> Score for ScorerAccountingForInFlightHtlcs<'a, S, SP>  {
+impl<'a, S: Score> Score for ScorerAccountingForInFlightHtlcs<'a, S>  {
        #[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: &crate::routing::scoring::ProbabilisticScoringFeeParameters) -> u64 {
index 451683c91bff3af128748b32591f2ccfb60453f2..b47cbbef75be1e1552d4238d74d52b63202c3886 100644 (file)
@@ -163,11 +163,8 @@ define_score!();
 ///
 /// [`find_route`]: crate::routing::router::find_route
 pub trait LockableScore<'a> {
-       /// The [`Score`] type.
-       type Score: 'a + Score;
-
        /// The locked [`Score`] type.
-       type Locked: DerefMut<Target = Self::Score> + Sized;
+       type Locked: 'a + Score;
 
        /// Returns the locked scorer.
        fn lock(&'a self) -> Self::Locked;
@@ -183,7 +180,6 @@ pub trait WriteableScore<'a>: LockableScore<'a> + Writeable {}
 impl<'a, T> WriteableScore<'a> for T where T: LockableScore<'a> + Writeable {}
 #[cfg(not(c_bindings))]
 impl<'a, T: 'a + Score> LockableScore<'a> for Mutex<T> {
-       type Score = T;
        type Locked = MutexGuard<'a, T>;
 
        fn lock(&'a self) -> Self::Locked {
@@ -193,7 +189,6 @@ impl<'a, T: 'a + Score> LockableScore<'a> for Mutex<T> {
 
 #[cfg(not(c_bindings))]
 impl<'a, T: 'a + Score> LockableScore<'a> for RefCell<T> {
-       type Score = T;
        type Locked = RefMut<'a, T>;
 
        fn lock(&'a self) -> Self::Locked {
@@ -209,7 +204,6 @@ pub struct MultiThreadedLockableScore<T: Score> {
 
 #[cfg(c_bindings)]
 impl<'a, T: 'a + Score> LockableScore<'a> for MultiThreadedLockableScore<T> {
-       type Score = T;
        type Locked = MultiThreadedScoreLock<'a, T>;
 
        fn lock(&'a self) -> Self::Locked {
@@ -247,22 +241,29 @@ impl<'a, T: 'a + Score> Writeable for MultiThreadedScoreLock<'a, T> {
 }
 
 #[cfg(c_bindings)]
-impl<'a, T: 'a + Score> DerefMut for MultiThreadedScoreLock<'a, T> {
-    fn deref_mut(&mut self) -> &mut Self::Target {
-        self.0.deref_mut()
-    }
-}
+impl<'a, T: 'a + Score> Score for MultiThreadedScoreLock<'a, T> {
+       fn channel_penalty_msat(
+               &self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage, score_params: &ProbabilisticScoringFeeParameters
+       ) -> u64 {
+               self.0.channel_penalty_msat(short_channel_id, source, target, usage, score_params)
+       }
 
-#[cfg(c_bindings)]
-impl<'a, T: 'a + Score> Deref for MultiThreadedScoreLock<'a, T> {
-       type Target = T;
+       fn payment_path_failed(&mut self, path: &Path, short_channel_id: u64) {
+               self.0.payment_path_failed(path, short_channel_id)
+       }
 
-    fn deref(&self) -> &Self::Target {
-        self.0.deref()
-    }
-}
+       fn payment_path_successful(&mut self, path: &Path) {
+               self.0.payment_path_successful(path)
+       }
 
+       fn probe_failed(&mut self, path: &Path, short_channel_id: u64) {
+               self.0.probe_failed(path, short_channel_id)
+       }
 
+       fn probe_successful(&mut self, path: &Path) {
+               self.0.probe_successful(path)
+       }
+}
 
 /// Proposed use of a channel passed as a parameter to [`Score::channel_penalty_msat`].
 #[derive(Clone, Copy, Debug, PartialEq)]