Skip to content

Commit 9a3256e

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 04425f9 commit 9a3256e

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
@@ -295,6 +295,7 @@ macro_rules! define_run_body {
295295
let mut last_scorer_persist_call = $get_timer(SCORER_PERSIST_TIMER);
296296
let mut last_rebroadcast_call = $get_timer(REBROADCAST_TIMER);
297297
let mut have_pruned = false;
298+
let mut have_decayed_scorer = false;
298299

299300
loop {
300301
$process_channel_manager_events;
@@ -402,9 +403,21 @@ macro_rules! define_run_body {
402403
last_prune_call = $get_timer(prune_timer);
403404
}
404405

406+
if !have_decayed_scorer {
407+
if let Some(ref scorer) = $scorer {
408+
if let Some(duration_since_epoch) = $time_fetch() {
409+
scorer.write_lock().decay_liquidity_certainty(duration_since_epoch);
410+
}
411+
}
412+
have_decayed_scorer = true;
413+
}
414+
405415
if $timer_elapsed(&mut last_scorer_persist_call, SCORER_PERSIST_TIMER) {
406416
if let Some(ref scorer) = $scorer {
407-
log_trace!($logger, "Persisting scorer");
417+
log_trace!($logger, "Decaying and persisting scorer");
418+
if let Some(duration_since_epoch) = $time_fetch() {
419+
scorer.write_lock().decay_liquidity_certainty(duration_since_epoch);
420+
}
408421
if let Err(e) = $persister.persist_scorer(&scorer) {
409422
log_error!($logger, "Error: Failed to persist scorer, check your disk and permissions {}", e)
410423
}
@@ -1209,6 +1222,7 @@ mod tests {
12091222
}
12101223
}
12111224
}
1225+
fn decay_liquidity_certainty(&mut self, _: Duration) {}
12121226
}
12131227

12141228
#[cfg(c_bindings)]
@@ -1617,7 +1631,7 @@ mod tests {
16171631

16181632
loop {
16191633
let log_entries = nodes[0].logger.lines.lock().unwrap();
1620-
let expected_log = "Persisting scorer".to_string();
1634+
let expected_log = "Decaying and persisting scorer".to_string();
16211635
if log_entries.get(&("lightning_background_processor", expected_log)).is_some() {
16221636
break
16231637
}

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

@@ -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 decay_liquidity_certainty(&mut self, duration_since_epoch: Duration) {
376+
self.0.decay_liquidity_certainty(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 decay_liquidity_certainty(&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 decay_liquidity_certainty(&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
@@ -1356,6 +1356,8 @@ impl ScoreUpdate for TestScorer {
13561356
fn probe_failed(&mut self, _actual_path: &Path, _: u64, _duration_since_epoch: Duration) {}
13571357

13581358
fn probe_successful(&mut self, _actual_path: &Path, _duration_since_epoch: Duration) {}
1359+
1360+
fn decay_liquidity_certainty(&mut self, _duration_since_epoch: Duration) {}
13591361
}
13601362

13611363
impl Drop for TestScorer {

0 commit comments

Comments
 (0)