From 9449d042b985553e6b06761d4a64df16f8bd12ad Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 22 Dec 2022 21:58:53 +0000 Subject: [PATCH] Store an owned `Score` in `ScorerAccountingForInFlightHtlcs` `ScorerAccountingForInFlightHtlcs` generally stores a `Score` reference generated by calling `LockableScore::lock`, which actually returns an arbitrary `Score`. Given `Score` is implemented directly on lock types, it makes sense to simply hold a fully owned `Score` in `ScorerAccountingForInFlightHtlcs` rather than a mutable reference to one. --- lightning-invoice/src/payment.rs | 4 ++-- lightning/src/routing/router.rs | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lightning-invoice/src/payment.rs b/lightning-invoice/src/payment.rs index 4fddedc0..03dc9361 100644 --- a/lightning-invoice/src/payment.rs +++ b/lightning-invoice/src/payment.rs @@ -1674,8 +1674,8 @@ mod tests { ) -> Result { // Simulate calling the Scorer just as you would in find_route let route = Self::route_for_value(route_params.final_value_msat); - let mut locked_scorer = self.scorer.lock(); - let scorer = ScorerAccountingForInFlightHtlcs::new(locked_scorer.deref_mut(), inflight_htlcs); + let locked_scorer = self.scorer.lock(); + let scorer = ScorerAccountingForInFlightHtlcs::new(locked_scorer, inflight_htlcs); for path in route.paths { let mut aggregate_msat = 0u64; for (idx, hop) in path.iter().rev().enumerate() { diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index c1ef01bf..4b5ff2f0 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -71,7 +71,7 @@ impl>, L: Deref, S: Deref> Router for DefaultR find_route( payer, params, &self.network_graph, first_hops, &*self.logger, - &ScorerAccountingForInFlightHtlcs::new(&mut self.scorer.lock(), inflight_htlcs), + &ScorerAccountingForInFlightHtlcs::new(self.scorer.lock(), inflight_htlcs), &random_seed_bytes ) } @@ -125,15 +125,15 @@ pub trait Router { /// [`find_route`]. /// /// [`Score`]: crate::routing::scoring::Score -pub struct ScorerAccountingForInFlightHtlcs<'a, S: Score> { - scorer: &'a mut S, +pub struct ScorerAccountingForInFlightHtlcs { + scorer: S, // Maps a channel's short channel id and its direction to the liquidity used up. inflight_htlcs: InFlightHtlcs, } -impl<'a, S: Score> ScorerAccountingForInFlightHtlcs<'a, S> { +impl ScorerAccountingForInFlightHtlcs { /// Initialize a new `ScorerAccountingForInFlightHtlcs`. - pub fn new(scorer: &'a mut S, inflight_htlcs: InFlightHtlcs) -> Self { + pub fn new(scorer: S, inflight_htlcs: InFlightHtlcs) -> Self { ScorerAccountingForInFlightHtlcs { scorer, inflight_htlcs @@ -142,11 +142,11 @@ impl<'a, S: Score> ScorerAccountingForInFlightHtlcs<'a, S> { } #[cfg(c_bindings)] -impl<'a, S:Score> Writeable for ScorerAccountingForInFlightHtlcs<'a, S> { +impl Writeable for ScorerAccountingForInFlightHtlcs { fn write(&self, writer: &mut W) -> Result<(), io::Error> { self.scorer.write(writer) } } -impl<'a, S: Score> Score for ScorerAccountingForInFlightHtlcs<'a, S> { +impl Score for ScorerAccountingForInFlightHtlcs { fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage) -> u64 { if let Some(used_liquidity) = self.inflight_htlcs.used_liquidity_msat( source, target, short_channel_id -- 2.30.2