Skip to content

Commit cddd4db

Browse files
committed
Add a scoring decay method to the ScoreUpdate trait
Rather than relying on fetching the current time during routefinding, here we introduce a new trait method to `ScoreUpdate` to do so. This largely mirrors what we do with the `NetworkGraph`, and allows us to take on much more expensive operations (floating point exponentiation) in our decaying.
1 parent 9b1d570 commit cddd4db

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ macro_rules! define_run_body {
287287
let mut last_scorer_persist_call = $get_timer(SCORER_PERSIST_TIMER);
288288
let mut last_rebroadcast_call = $get_timer(REBROADCAST_TIMER);
289289
let mut have_pruned = false;
290+
let mut have_decayed_scorer = false;
290291

291292
loop {
292293
$process_channel_manager_events;
@@ -388,9 +389,21 @@ macro_rules! define_run_body {
388389
last_prune_call = $get_timer(prune_timer);
389390
}
390391

392+
if !have_decayed_scorer {
393+
if let Some(ref scorer) = $scorer {
394+
if let Some(duration_since_epoch) = $time_fetch() {
395+
scorer.write_lock().decay_liquidity_certainty(duration_since_epoch);
396+
}
397+
}
398+
have_decayed_scorer = true;
399+
}
400+
391401
if $timer_elapsed(&mut last_scorer_persist_call, SCORER_PERSIST_TIMER) {
392402
if let Some(ref scorer) = $scorer {
393-
log_trace!($logger, "Persisting scorer");
403+
log_trace!($logger, "Decaying and persisting scorer");
404+
if let Some(duration_since_epoch) = $time_fetch() {
405+
scorer.write_lock().decay_liquidity_certainty(duration_since_epoch);
406+
}
394407
if let Err(e) = $persister.persist_scorer(&scorer) {
395408
log_error!($logger, "Error: Failed to persist scorer, check your disk and permissions {}", e)
396409
}
@@ -1167,6 +1180,7 @@ mod tests {
11671180
}
11681181
}
11691182
}
1183+
fn decay_liquidity_certainty(&mut self, _: Duration) {}
11701184
}
11711185

11721186
#[cfg(c_bindings)]
@@ -1571,7 +1585,7 @@ mod tests {
15711585

15721586
loop {
15731587
let log_entries = nodes[0].logger.lines.lock().unwrap();
1574-
let expected_log = "Persisting scorer".to_string();
1588+
let expected_log = "Decaying and persisting scorer".to_string();
15751589
if log_entries.get(&("lightning_background_processor".to_string(), expected_log)).is_some() {
15761590
break
15771591
}

lightning/src/routing/scoring.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ pub trait ScoreUpdate {
120120

121121
/// Handles updating channel penalties after a probe over the given path succeeded.
122122
fn probe_successful(&mut self, path: &Path, duration_since_epoch: Duration);
123+
124+
/// Scorers may wich to reduce their certainty of channel liquidity information over time.
125+
/// Thus, this method is provided to allow scorers to observe the passage of time - the holder
126+
/// of this object should call this method regularly (generally via the
127+
/// `lightning-background-processor` trait).
128+
fn decay_liquidity_certainty(&mut self, duration_since_epoch: Duration);
123129
}
124130

125131
/// A trait which can both lookup and update routing channel penalty scores.
@@ -160,6 +166,10 @@ impl<S: ScoreUpdate, T: DerefMut<Target=S>> ScoreUpdate for T {
160166
fn probe_successful(&mut self, path: &Path, duration_since_epoch: Duration) {
161167
self.deref_mut().probe_successful(path, duration_since_epoch)
162168
}
169+
170+
fn decay_liquidity_certainty(&mut self, duration_since_epoch: Duration) {
171+
self.deref_mut().decay_liquidity_certainty(duration_since_epoch)
172+
}
163173
}
164174
} }
165175

@@ -362,6 +372,10 @@ impl<'a, T: Score> ScoreUpdate for MultiThreadedScoreLockWrite<'a, T> {
362372
fn probe_successful(&mut self, path: &Path, duration_since_epoch: Duration) {
363373
self.0.probe_successful(path, duration_since_epoch)
364374
}
375+
376+
fn decay_liquidity_certainty(&mut self, duration_since_epoch: Duration) {
377+
self.0.decay_liquidity_certainty(duration_since_epoch)
378+
}
365379
}
366380

367381

@@ -407,6 +421,8 @@ impl ScoreUpdate for FixedPenaltyScorer {
407421
fn probe_failed(&mut self, _path: &Path, _short_channel_id: u64, _duration_since_epoch: Duration) {}
408422

409423
fn probe_successful(&mut self, _path: &Path, _duration_since_epoch: Duration) {}
424+
425+
fn decay_liquidity_certainty(&mut self, _duration_since_epoch: Duration) {}
410426
}
411427

412428
impl Writeable for FixedPenaltyScorer {
@@ -1457,6 +1473,8 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
14571473
fn probe_successful(&mut self, path: &Path, duration_since_epoch: Duration) {
14581474
self.payment_path_failed(path, u64::max_value(), duration_since_epoch)
14591475
}
1476+
1477+
fn decay_liquidity_certainty(&mut self, _duration_since_epoch: Duration) {}
14601478
}
14611479

14621480
#[cfg(c_bindings)]

lightning/src/util/test_utils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,8 @@ impl ScoreUpdate for TestScorer {
12991299
fn probe_failed(&mut self, _actual_path: &Path, _: u64, _duration_since_epoch: Duration) {}
13001300

13011301
fn probe_successful(&mut self, _actual_path: &Path, _duration_since_epoch: Duration) {}
1302+
1303+
fn decay_liquidity_certainty(&mut self, _duration_since_epoch: Duration) {}
13021304
}
13031305

13041306
impl Drop for TestScorer {

0 commit comments

Comments
 (0)