Skip to content

Commit 88edeb1

Browse files
committed
Fix type restrained to only MutexGuard
1 parent 0f2c4c0 commit 88edeb1

File tree

6 files changed

+59
-24
lines changed

6 files changed

+59
-24
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ mod tests {
859859
use lightning::ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor, IgnoringMessageHandler};
860860
use lightning::routing::gossip::{NetworkGraph, NodeId, P2PGossipSync};
861861
use lightning::routing::router::{DefaultRouter, Path, RouteHop};
862-
use lightning::routing::scoring::{ChannelUsage, Score};
862+
use lightning::routing::scoring::{ChannelUsage, Score, LockableScore};
863863
use lightning::util::config::UserConfig;
864864
use lightning::util::ser::Writeable;
865865
use lightning::util::test_utils;
@@ -886,7 +886,22 @@ mod tests {
886886
fn disconnect_socket(&mut self) {}
887887
}
888888

889-
type ChannelManager = channelmanager::ChannelManager<Arc<ChainMonitor>, Arc<test_utils::TestBroadcaster>, Arc<KeysManager>, Arc<KeysManager>, Arc<KeysManager>, Arc<test_utils::TestFeeEstimator>, Arc<DefaultRouter<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestLogger>, Arc<Mutex<TestScorer>>, (), TestScorer>>, Arc<test_utils::TestLogger>>;
889+
type ChannelManager =
890+
channelmanager::ChannelManager<
891+
Arc<ChainMonitor>,
892+
Arc<test_utils::TestBroadcaster>,
893+
Arc<KeysManager>,
894+
Arc<KeysManager>,
895+
Arc<KeysManager>,
896+
Arc<test_utils::TestFeeEstimator>,
897+
Arc<DefaultRouter<
898+
Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
899+
Arc<test_utils::TestLogger>,
900+
Arc<Mutex<TestScorer>>,
901+
(),
902+
TestScorer>
903+
>,
904+
Arc<test_utils::TestLogger>>;
890905

891906
type ChainMonitor = chainmonitor::ChainMonitor<InMemorySigner, Arc<test_utils::TestChainSource>, Arc<test_utils::TestBroadcaster>, Arc<test_utils::TestFeeEstimator>, Arc<test_utils::TestLogger>, Arc<FilesystemPersister>>;
892907

lightning/src/ln/channelmanager.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ struct PendingInboundPayment {
724724
/// of [`KeysManager`] and [`DefaultRouter`].
725725
///
726726
/// This is not exported to bindings users as Arcs don't make sense in bindings
727-
pub type SimpleArcChannelManager<M, T, F, L> = ChannelManager<
727+
pub type SimpleArcChannelManager<'a, M, T, F, L> = ChannelManager<
728728
Arc<M>,
729729
Arc<T>,
730730
Arc<KeysManager>,
@@ -734,7 +734,7 @@ pub type SimpleArcChannelManager<M, T, F, L> = ChannelManager<
734734
Arc<DefaultRouter<
735735
Arc<NetworkGraph<Arc<L>>>,
736736
Arc<L>,
737-
Arc<Mutex<ProbabilisticScorer<Arc<NetworkGraph<Arc<L>>>, Arc<L>>>>,
737+
Arc<ProbabilisticScorer<Arc<NetworkGraph<Arc<L>>>, Arc<L>>>,
738738
ProbabilisticScoringFeeParameters,
739739
ProbabilisticScorer<Arc<NetworkGraph<Arc<L>>>, Arc<L>>,
740740
>>,
@@ -752,7 +752,22 @@ pub type SimpleArcChannelManager<M, T, F, L> = ChannelManager<
752752
/// of [`KeysManager`] and [`DefaultRouter`].
753753
///
754754
/// This is not exported to bindings users as Arcs don't make sense in bindings
755-
pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, M, T, F, L> = ChannelManager<&'a M, &'b T, &'c KeysManager, &'c KeysManager, &'c KeysManager, &'d F, &'e DefaultRouter<&'f NetworkGraph<&'g L>, &'g L, &'h Mutex<ProbabilisticScorer<&'f NetworkGraph<&'g L>, &'g L>>, ProbabilisticScoringFeeParameters, ProbabilisticScorer<&'f NetworkGraph<&'g L>, &'g L>>, &'g L>;
755+
pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, M, T, F, L> =
756+
ChannelManager<
757+
&'a M,
758+
&'b T,
759+
&'c KeysManager,
760+
&'c KeysManager,
761+
&'c KeysManager,
762+
&'d F,
763+
&'e DefaultRouter<
764+
&'f NetworkGraph<&'g L>,
765+
&'g L,
766+
&'h ProbabilisticScorer<&'f NetworkGraph<&'g L>, &'g L>,
767+
ProbabilisticScoringFeeParameters,
768+
ProbabilisticScorer<&'f NetworkGraph<&'g L>, &'g L>
769+
>,
770+
&'g L>;
756771

757772
macro_rules! define_test_pub_trait { ($vis: vis) => {
758773
/// A trivial trait which describes any [`ChannelManager`] used in testing.

lightning/src/ln/peer_handler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,9 +607,9 @@ impl Peer {
607607
/// issues such as overly long function definitions.
608608
///
609609
/// This is not exported to bindings users as `Arc`s don't make sense in bindings.
610-
pub type SimpleArcPeerManager<SD, M, T, F, C, L, R> = PeerManager<
610+
pub type SimpleArcPeerManager<'a, SD, M, T, F, C, L, R> = PeerManager<
611611
SD,
612-
Arc<SimpleArcChannelManager<M, T, F, L>>,
612+
Arc<SimpleArcChannelManager<'a, M, T, F, L>>,
613613
Arc<P2PGossipSync<Arc<NetworkGraph<Arc<L>>>, Arc<C>, Arc<L>>>,
614614
Arc<SimpleArcOnionMessenger<L, R>>,
615615
Arc<L>,

lightning/src/routing/router.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ use crate::util::chacha20::ChaCha20;
2727

2828
use crate::io;
2929
use crate::prelude::*;
30-
use crate::sync::{Mutex, MutexGuard};
30+
use crate::sync::{Mutex};
3131
use alloc::collections::BinaryHeap;
3232
use core::{cmp, fmt};
33-
use core::ops::Deref;
33+
use core::ops::{Deref,DerefMut};
3434

3535
/// A [`Router`] implemented using [`find_route`].
3636
pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: Score<ScoreParams = SP>> where
3737
L::Target: Logger,
38-
S::Target: for <'a> LockableScore<'a, Locked = MutexGuard<'a, Sc>>,
38+
S::Target: for <'a> LockableScore<'a, Score = Sc>,
3939
{
4040
network_graph: G,
4141
logger: L,
@@ -46,7 +46,7 @@ pub struct DefaultRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref,
4646

4747
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: Score<ScoreParams = SP>> DefaultRouter<G, L, S, SP, Sc> where
4848
L::Target: Logger,
49-
S::Target: for <'a> LockableScore<'a, Locked = MutexGuard<'a, Sc>>,
49+
S::Target: for <'a> LockableScore<'a, Score = Sc>,
5050
{
5151
/// Creates a new router.
5252
pub fn new(network_graph: G, logger: L, random_seed_bytes: [u8; 32], scorer: S, score_params: SP) -> Self {
@@ -55,9 +55,9 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: Scor
5555
}
5656
}
5757

58-
impl< G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: Score<ScoreParams = SP>> Router for DefaultRouter<G, L, S, SP, Sc> where
58+
impl< G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: Score<ScoreParams = SP>> Router for DefaultRouter<G, L, S, SP, Sc> where
5959
L::Target: Logger,
60-
S::Target: for <'a> LockableScore<'a, Locked = MutexGuard<'a, Sc>>,
60+
S::Target: for <'a> LockableScore<'a, Score = Sc>,
6161
{
6262
fn find_route(
6363
&self,
@@ -73,7 +73,7 @@ impl< G: Deref<Target = NetworkGraph<L>>, L: Deref, S: Deref, SP: Sized, Sc: Sc
7373
};
7474
find_route(
7575
payer, params, &self.network_graph, first_hops, &*self.logger,
76-
&ScorerAccountingForInFlightHtlcs::new(self.scorer.lock(), inflight_htlcs),
76+
&ScorerAccountingForInFlightHtlcs::new(self.scorer.lock().deref_mut(), inflight_htlcs),
7777
&self.score_params,
7878
&random_seed_bytes
7979
)
@@ -104,15 +104,15 @@ pub trait Router {
104104
/// [`find_route`].
105105
///
106106
/// [`Score`]: crate::routing::scoring::Score
107-
pub struct ScorerAccountingForInFlightHtlcs<'a, S: Score> {
108-
scorer: S,
107+
pub struct ScorerAccountingForInFlightHtlcs<'a, S:Score<ScoreParams = SP>, SP: Sized> {
108+
scorer: &'a mut S,
109109
// Maps a channel's short channel id and its direction to the liquidity used up.
110110
inflight_htlcs: &'a InFlightHtlcs,
111111
}
112112

113-
impl<'a, S: Score> ScorerAccountingForInFlightHtlcs<'a, S> {
113+
impl<'a, S:Score<ScoreParams = SP>, SP: Sized> ScorerAccountingForInFlightHtlcs<'a, S, SP> {
114114
/// Initialize a new `ScorerAccountingForInFlightHtlcs`.
115-
pub fn new(scorer: S, inflight_htlcs: &'a InFlightHtlcs) -> Self {
115+
pub fn new(scorer: &'a mut S, inflight_htlcs: &'a InFlightHtlcs) -> Self {
116116
ScorerAccountingForInFlightHtlcs {
117117
scorer,
118118
inflight_htlcs
@@ -125,7 +125,7 @@ impl<'a, S: Score> Writeable for ScorerAccountingForInFlightHtlcs<'a, S> {
125125
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> { self.scorer.write(writer) }
126126
}
127127

128-
impl<'a, S: Score> Score for ScorerAccountingForInFlightHtlcs<'a, S> {
128+
impl<'a, S:Score<ScoreParams = SP>, SP: Sized> Score for ScorerAccountingForInFlightHtlcs<'a, S, SP> {
129129
type ScoreParams = S::ScoreParams;
130130
fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage, score_params: &Self::ScoreParams) -> u64 {
131131
if let Some(used_liquidity) = self.inflight_htlcs.used_liquidity_msat(

lightning/src/routing/scoring.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,11 @@ define_score!();
157157
///
158158
/// [`find_route`]: crate::routing::router::find_route
159159
pub trait LockableScore<'a> {
160+
/// The [`Score`] type.
161+
type Score: 'a + Score;
162+
160163
/// The locked [`Score`] type.
161-
type Locked: 'a + Score;
164+
type Locked: DerefMut<Target = Self::Score> + Sized;
162165

163166
/// Returns the locked scorer.
164167
fn lock(&'a self) -> Self::Locked;
@@ -174,6 +177,7 @@ pub trait WriteableScore<'a>: LockableScore<'a> + Writeable {}
174177
impl<'a, T> WriteableScore<'a> for T where T: LockableScore<'a> + Writeable {}
175178
/// This is not exported to bindings users
176179
impl<'a, T: 'a + Score> LockableScore<'a> for Mutex<T> {
180+
type Score = T;
177181
type Locked = MutexGuard<'a, T>;
178182

179183
fn lock(&'a self) -> MutexGuard<'a, T> {
@@ -182,6 +186,7 @@ impl<'a, T: 'a + Score> LockableScore<'a> for Mutex<T> {
182186
}
183187

184188
impl<'a, T: 'a + Score> LockableScore<'a> for RefCell<T> {
189+
type Score = T;
185190
type Locked = RefMut<'a, T>;
186191

187192
fn lock(&'a self) -> RefMut<'a, T> {

lightning/src/util/test_utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ use regex;
5151
use crate::io;
5252
use crate::prelude::*;
5353
use core::cell::RefCell;
54+
use core::ops::DerefMut;
5455
use core::time::Duration;
5556
use crate::sync::{Mutex, Arc};
5657
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
@@ -113,8 +114,8 @@ impl<'a> Router for TestRouter<'a> {
113114
if let Some((find_route_query, find_route_res)) = self.next_routes.lock().unwrap().pop_front() {
114115
assert_eq!(find_route_query, *params);
115116
if let Ok(ref route) = find_route_res {
116-
let locked_scorer = self.scorer.lock().unwrap();
117-
let scorer = ScorerAccountingForInFlightHtlcs::new(locked_scorer, inflight_htlcs);
117+
let mut binding = self.scorer.lock().unwrap();
118+
let scorer = ScorerAccountingForInFlightHtlcs::new(binding.deref_mut(), inflight_htlcs);
118119
for path in &route.paths {
119120
let mut aggregate_msat = 0u64;
120121
for (idx, hop) in path.hops.iter().rev().enumerate() {
@@ -139,10 +140,9 @@ impl<'a> Router for TestRouter<'a> {
139140
return find_route_res;
140141
}
141142
let logger = TestLogger::new();
142-
let scorer = self.scorer.lock().unwrap();
143143
find_route(
144144
payer, params, &self.network_graph, first_hops, &logger,
145-
&ScorerAccountingForInFlightHtlcs::new(scorer, &inflight_htlcs), &(),
145+
&ScorerAccountingForInFlightHtlcs::new(self.scorer.lock().unwrap().deref_mut(), &inflight_htlcs), &(),
146146
&[42; 32]
147147
)
148148
}

0 commit comments

Comments
 (0)