Skip to content

Commit 0815436

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 0815436

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
@@ -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,21 @@ 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+
scorer.write_lock().decay_liquidity_certainty(duration_since_epoch);
409+
}
410+
}
411+
have_decayed_scorer = true;
412+
}
413+
404414
if $timer_elapsed(&mut last_scorer_persist_call, SCORER_PERSIST_TIMER) {
405415
if let Some(ref scorer) = $scorer {
406-
log_trace!($logger, "Persisting scorer");
416+
log_trace!($logger, "Decaying and persisting scorer");
417+
if let Some(duration_since_epoch) = $time_fetch() {
418+
scorer.write_lock().decay_liquidity_certainty(duration_since_epoch);
419+
}
407420
if let Err(e) = $persister.persist_scorer(&scorer) {
408421
log_error!($logger, "Error: Failed to persist scorer, check your disk and permissions {}", e)
409422
}
@@ -1208,6 +1221,7 @@ mod tests {
12081221
}
12091222
}
12101223
}
1224+
fn decay_liquidity_certainty(&mut self, _: Duration) {}
12111225
}
12121226

12131227
#[cfg(c_bindings)]
@@ -1616,7 +1630,7 @@ mod tests {
16161630

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

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
@@ -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 decay_liquidity_certainty(&mut self, _duration_since_epoch: Duration) {}
13601362
}
13611363

13621364
impl Drop for TestScorer {

0 commit comments

Comments
 (0)