From ee31ddfd5a9d816354387c1a1dc8a06fb489ba8a Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 19 Jul 2023 19:20:50 +0000 Subject: [PATCH] Drop separate `Locked` type on `LockableScore` 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 | 14 ++++++------ lightning/src/routing/scoring.rs | 39 ++++++++++++++++---------------- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index 90b93de7a..822c4842c 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -73,7 +73,7 @@ impl< G: Deref>, 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, 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, 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, SP: Sized> ScorerAccountingForInFlightHtlcs } #[cfg(c_bindings)] -impl<'a, S: Score, SP: Sized> Writeable for ScorerAccountingForInFlightHtlcs<'a, S, SP> { +impl<'a, S: Score> Writeable for ScorerAccountingForInFlightHtlcs<'a, S> { fn write(&self, writer: &mut W) -> Result<(), io::Error> { self.scorer.write(writer) } } -impl<'a, S: Score, 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 { diff --git a/lightning/src/routing/scoring.rs b/lightning/src/routing/scoring.rs index 451683c91..b47cbbef7 100644 --- a/lightning/src/routing/scoring.rs +++ b/lightning/src/routing/scoring.rs @@ -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 + 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 { - 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 { #[cfg(not(c_bindings))] impl<'a, T: 'a + Score> LockableScore<'a> for RefCell { - type Score = T; type Locked = RefMut<'a, T>; fn lock(&'a self) -> Self::Locked { @@ -209,7 +204,6 @@ pub struct MultiThreadedLockableScore { #[cfg(c_bindings)] impl<'a, T: 'a + Score> LockableScore<'a> for MultiThreadedLockableScore { - 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)] -- 2.39.5