Skip to content

Commit b84842a

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 6c366cf commit b84842a

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ macro_rules! define_run_body {
294294
let mut last_scorer_persist_call = $get_timer(SCORER_PERSIST_TIMER);
295295
let mut last_rebroadcast_call = $get_timer(REBROADCAST_TIMER);
296296
let mut have_pruned = false;
297+
let mut have_decayed_scorer = false;
297298

298299
loop {
299300
$process_channel_manager_events;
@@ -401,9 +402,24 @@ macro_rules! define_run_body {
401402
last_prune_call = $get_timer(prune_timer);
402403
}
403404

405+
if !have_decayed_scorer {
406+
if let Some(ref scorer) = $scorer {
407+
if let Some(duration_since_epoch) = $time_fetch() {
408+
log_trace!($logger, "Calling time_passed on scorer at startup");
409+
scorer.write_lock().time_passed(duration_since_epoch);
410+
}
411+
}
412+
have_decayed_scorer = true;
413+
}
414+
404415
if $timer_elapsed(&mut last_scorer_persist_call, SCORER_PERSIST_TIMER) {
405416
if let Some(ref scorer) = $scorer {
406-
log_trace!($logger, "Persisting scorer");
417+
if let Some(duration_since_epoch) = $time_fetch() {
418+
log_trace!($logger, "Calling time_passed and persisting scorer");
419+
scorer.write_lock().time_passed(duration_since_epoch);
420+
} else {
421+
log_trace!($logger, "Persisting scorer");
422+
}
407423
if let Err(e) = $persister.persist_scorer(&scorer) {
408424
log_error!($logger, "Error: Failed to persist scorer, check your disk and permissions {}", e)
409425
}
@@ -1208,6 +1224,7 @@ mod tests {
12081224
}
12091225
}
12101226
}
1227+
fn time_passed(&mut self, _: Duration) {}
12111228
}
12121229

12131230
#[cfg(c_bindings)]
@@ -1616,7 +1633,7 @@ mod tests {
16161633

16171634
loop {
16181635
let log_entries = nodes[0].logger.lines.lock().unwrap();
1619-
let expected_log = "Persisting scorer".to_string();
1636+
let expected_log = "Calling time_passed and persisting scorer".to_string();
16201637
if log_entries.get(&("lightning_background_processor", expected_log)).is_some() {
16211638
break
16221639
}

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 wish 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` crate).
128+
fn time_passed(&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 time_passed(&mut self, duration_since_epoch: Duration) {
171+
self.deref_mut().time_passed(duration_since_epoch)
172+
}
163173
}
164174
} }
165175

@@ -361,6 +371,10 @@ impl<'a, T: Score> ScoreUpdate for MultiThreadedScoreLockWrite<'a, T> {
361371
fn probe_successful(&mut self, path: &Path, duration_since_epoch: Duration) {
362372
self.0.probe_successful(path, duration_since_epoch)
363373
}
374+
375+
fn time_passed(&mut self, duration_since_epoch: Duration) {
376+
self.0.time_passed(duration_since_epoch)
377+
}
364378
}
365379

366380

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

408422
fn probe_successful(&mut self, _path: &Path, _duration_since_epoch: Duration) {}
423+
424+
fn time_passed(&mut self, _duration_since_epoch: Duration) {}
409425
}
410426

411427
impl Writeable for FixedPenaltyScorer {
@@ -1463,6 +1479,8 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
14631479
fn probe_successful(&mut self, path: &Path, duration_since_epoch: Duration) {
14641480
self.payment_path_failed(path, u64::max_value(), duration_since_epoch)
14651481
}
1482+
1483+
fn time_passed(&mut self, _duration_since_epoch: Duration) {}
14661484
}
14671485

14681486
#[cfg(c_bindings)]

lightning/src/util/test_utils.rs

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

13591359
fn probe_successful(&mut self, _actual_path: &Path, _duration_since_epoch: Duration) {}
1360+
1361+
fn time_passed(&mut self, _duration_since_epoch: Duration) {}
13601362
}
13611363

13621364
impl Drop for TestScorer {

0 commit comments

Comments
 (0)