Skip to content

Commit 0bc0de4

Browse files
committed
Simplify score bounding with a unified type
In a few places we require a unified scorer, which implements both `ScoreLookUp` and `ScoreUpdate`. Rather than double-bounding (which the bindings generator can't handle directly), we use a top-level `Score` trait which requires both and is implemented for all implementers of both supertraits.
1 parent 748beba commit 0bc0de4

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

lightning/src/routing/router.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,6 @@ impl<'a, S: Deref> ScorerAccountingForInFlightHtlcs<'a, S> where S::Target: Scor
127127
}
128128
}
129129

130-
#[cfg(c_bindings)]
131-
impl<'a, S: Deref> Writeable for ScorerAccountingForInFlightHtlcs<'a, S> where S::Target: ScoreLookUp {
132-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> { self.scorer.write(writer) }
133-
}
134-
135130
impl<'a, S: Deref> ScoreLookUp for ScorerAccountingForInFlightHtlcs<'a, S> where S::Target: ScoreLookUp {
136131
type ScoreParams = <S::Target as ScoreLookUp>::ScoreParams;
137132
fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage, score_params: &Self::ScoreParams) -> u64 {

lightning/src/routing/scoring.rs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ macro_rules! define_score { ($($supertrait: path)*) => {
8989
/// `ScoreLookUp` is used to determine the penalty for a given channel.
9090
///
9191
/// Scoring is in terms of fees willing to be paid in order to avoid routing through a channel.
92-
pub trait ScoreLookUp $(: $supertrait)* {
92+
pub trait ScoreLookUp {
9393
/// A configurable type which should contain various passed-in parameters for configuring the scorer,
9494
/// on a per-routefinding-call basis through to the scorer methods,
9595
/// which are used to determine the parameters for the suitability of channels for use.
@@ -108,7 +108,7 @@ pub trait ScoreLookUp $(: $supertrait)* {
108108
}
109109

110110
/// `ScoreUpdate` is used to update the scorer's internal state after a payment attempt.
111-
pub trait ScoreUpdate $(: $supertrait)* {
111+
pub trait ScoreUpdate {
112112
/// Handles updating channel penalties after failing to route through a channel.
113113
fn payment_path_failed(&mut self, path: &Path, short_channel_id: u64);
114114

@@ -122,7 +122,18 @@ pub trait ScoreUpdate $(: $supertrait)* {
122122
fn probe_successful(&mut self, path: &Path);
123123
}
124124

125-
impl<S: ScoreLookUp, T: Deref<Target=S> $(+ $supertrait)*> ScoreLookUp for T {
125+
/// A trait which can both lookup and update routing channel penalty scores.
126+
///
127+
/// This is used in places where both bounds are required and implemented for all types which
128+
/// implement [`ScoreLookUp`] and [`ScoreUpdate`].
129+
///
130+
/// Bindings users may need to manually implement this for their custom scoring implementations.
131+
pub trait Score : ScoreLookUp + ScoreUpdate $(+ $supertrait)* {}
132+
133+
#[cfg(not(c_bindings))]
134+
impl<T: ScoreLookUp + ScoreUpdate $(+ $supertrait)*> Score for T {}
135+
136+
impl<S: ScoreLookUp, T: Deref<Target=S>> ScoreLookUp for T {
126137
type ScoreParams = S::ScoreParams;
127138
fn channel_penalty_msat(
128139
&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage, score_params: &Self::ScoreParams
@@ -131,7 +142,7 @@ impl<S: ScoreLookUp, T: Deref<Target=S> $(+ $supertrait)*> ScoreLookUp for T {
131142
}
132143
}
133144

134-
impl<S: ScoreUpdate, T: DerefMut<Target=S> $(+ $supertrait)*> ScoreUpdate for T {
145+
impl<S: ScoreUpdate, T: DerefMut<Target=S>> ScoreUpdate for T {
135146
fn payment_path_failed(&mut self, path: &Path, short_channel_id: u64) {
136147
self.deref_mut().payment_path_failed(path, short_channel_id)
137148
}
@@ -192,7 +203,7 @@ pub trait WriteableScore<'a>: LockableScore<'a> + Writeable {}
192203
#[cfg(not(c_bindings))]
193204
impl<'a, T> WriteableScore<'a> for T where T: LockableScore<'a> + Writeable {}
194205
#[cfg(not(c_bindings))]
195-
impl<'a, T: 'a + ScoreLookUp + ScoreUpdate> LockableScore<'a> for Mutex<T> {
206+
impl<'a, T: Score + 'a> LockableScore<'a> for Mutex<T> {
196207
type ScoreUpdate = T;
197208
type ScoreLookUp = T;
198209

@@ -209,7 +220,7 @@ impl<'a, T: 'a + ScoreLookUp + ScoreUpdate> LockableScore<'a> for Mutex<T> {
209220
}
210221

211222
#[cfg(not(c_bindings))]
212-
impl<'a, T: 'a + ScoreUpdate + ScoreLookUp> LockableScore<'a> for RefCell<T> {
223+
impl<'a, T: Score + 'a> LockableScore<'a> for RefCell<T> {
213224
type ScoreUpdate = T;
214225
type ScoreLookUp = T;
215226

@@ -226,7 +237,7 @@ impl<'a, T: 'a + ScoreUpdate + ScoreLookUp> LockableScore<'a> for RefCell<T> {
226237
}
227238

228239
#[cfg(not(c_bindings))]
229-
impl<'a, T: 'a + ScoreUpdate + ScoreLookUp> LockableScore<'a> for RwLock<T> {
240+
impl<'a, T: Score + 'a> LockableScore<'a> for RwLock<T> {
230241
type ScoreUpdate = T;
231242
type ScoreLookUp = T;
232243

@@ -244,12 +255,12 @@ impl<'a, T: 'a + ScoreUpdate + ScoreLookUp> LockableScore<'a> for RwLock<T> {
244255

245256
#[cfg(c_bindings)]
246257
/// A concrete implementation of [`LockableScore`] which supports multi-threading.
247-
pub struct MultiThreadedLockableScore<T: ScoreLookUp + ScoreUpdate> {
258+
pub struct MultiThreadedLockableScore<T: Score> {
248259
score: RwLock<T>,
249260
}
250261

251262
#[cfg(c_bindings)]
252-
impl<'a, T: 'a + ScoreLookUp + ScoreUpdate> LockableScore<'a> for MultiThreadedLockableScore<T> {
263+
impl<'a, T: Score + 'a> LockableScore<'a> for MultiThreadedLockableScore<T> {
253264
type ScoreUpdate = T;
254265
type ScoreLookUp = T;
255266
type WriteLocked = MultiThreadedScoreLockWrite<'a, Self::ScoreUpdate>;
@@ -265,17 +276,17 @@ impl<'a, T: 'a + ScoreLookUp + ScoreUpdate> LockableScore<'a> for MultiThreadedL
265276
}
266277

267278
#[cfg(c_bindings)]
268-
impl<T: ScoreUpdate + ScoreLookUp> Writeable for MultiThreadedLockableScore<T> {
279+
impl<T: Score> Writeable for MultiThreadedLockableScore<T> {
269280
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
270281
self.score.read().unwrap().write(writer)
271282
}
272283
}
273284

274285
#[cfg(c_bindings)]
275-
impl<'a, T: 'a + ScoreUpdate + ScoreLookUp> WriteableScore<'a> for MultiThreadedLockableScore<T> {}
286+
impl<'a, T: Score + 'a> WriteableScore<'a> for MultiThreadedLockableScore<T> {}
276287

277288
#[cfg(c_bindings)]
278-
impl<T: ScoreLookUp + ScoreUpdate> MultiThreadedLockableScore<T> {
289+
impl<T: Score> MultiThreadedLockableScore<T> {
279290
/// Creates a new [`MultiThreadedLockableScore`] given an underlying [`Score`].
280291
pub fn new(score: T) -> Self {
281292
MultiThreadedLockableScore { score: RwLock::new(score) }
@@ -284,14 +295,14 @@ impl<T: ScoreLookUp + ScoreUpdate> MultiThreadedLockableScore<T> {
284295

285296
#[cfg(c_bindings)]
286297
/// A locked `MultiThreadedLockableScore`.
287-
pub struct MultiThreadedScoreLockRead<'a, T: ScoreLookUp>(RwLockReadGuard<'a, T>);
298+
pub struct MultiThreadedScoreLockRead<'a, T: Score>(RwLockReadGuard<'a, T>);
288299

289300
#[cfg(c_bindings)]
290301
/// A locked `MultiThreadedLockableScore`.
291-
pub struct MultiThreadedScoreLockWrite<'a, T: ScoreUpdate>(RwLockWriteGuard<'a, T>);
302+
pub struct MultiThreadedScoreLockWrite<'a, T: Score>(RwLockWriteGuard<'a, T>);
292303

293304
#[cfg(c_bindings)]
294-
impl<'a, T: 'a + ScoreLookUp> Deref for MultiThreadedScoreLockRead<'a, T> {
305+
impl<'a, T: 'a + Score> Deref for MultiThreadedScoreLockRead<'a, T> {
295306
type Target = T;
296307

297308
fn deref(&self) -> &Self::Target {
@@ -300,14 +311,14 @@ impl<'a, T: 'a + ScoreLookUp> Deref for MultiThreadedScoreLockRead<'a, T> {
300311
}
301312

302313
#[cfg(c_bindings)]
303-
impl<'a, T: 'a + ScoreUpdate> Writeable for MultiThreadedScoreLockWrite<'a, T> {
314+
impl<'a, T: Score> Writeable for MultiThreadedScoreLockWrite<'a, T> {
304315
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
305316
self.0.write(writer)
306317
}
307318
}
308319

309320
#[cfg(c_bindings)]
310-
impl<'a, T: 'a + ScoreUpdate> Deref for MultiThreadedScoreLockWrite<'a, T> {
321+
impl<'a, T: 'a + Score> Deref for MultiThreadedScoreLockWrite<'a, T> {
311322
type Target = T;
312323

313324
fn deref(&self) -> &Self::Target {
@@ -316,7 +327,7 @@ impl<'a, T: 'a + ScoreUpdate> Deref for MultiThreadedScoreLockWrite<'a, T> {
316327
}
317328

318329
#[cfg(c_bindings)]
319-
impl<'a, T: 'a + ScoreUpdate> DerefMut for MultiThreadedScoreLockWrite<'a, T> {
330+
impl<'a, T: 'a + Score> DerefMut for MultiThreadedScoreLockWrite<'a, T> {
320331
fn deref_mut(&mut self) -> &mut Self::Target {
321332
self.0.deref_mut()
322333
}
@@ -1417,6 +1428,10 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
14171428
}
14181429
}
14191430

1431+
#[cfg(c_bindings)]
1432+
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> Score for ProbabilisticScorerUsingTime<G, L, T>
1433+
where L::Target: Logger {}
1434+
14201435
mod approx {
14211436
const BITS: u32 = 64;
14221437
const HIGHEST_BIT: u32 = BITS - 1;

0 commit comments

Comments
 (0)