Skip to content

Commit 9d73fe6

Browse files
committed
Impl decaying in ProbabilisticScorer::decay_liquidity_certainty
This implements decaying in the `ProbabilisticScorer`'s `ScoreLookup::decay_liquidity_certainty` implementation, using floats for accuracy since we're no longer particularly time-sensitive. Further, it (finally) removes score entries which have decayed to zero.
1 parent d045482 commit 9d73fe6

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

lightning/src/routing/scoring.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,6 @@ where L::Target: Logger {
493493
decay_params: ProbabilisticScoringDecayParameters,
494494
network_graph: G,
495495
logger: L,
496-
// TODO: Remove entries of closed channels.
497496
channel_liquidities: HashMap<u64, ChannelLiquidity<T>>,
498497
}
499498

@@ -1073,6 +1072,16 @@ impl<T: Time> ChannelLiquidity<T> {
10731072
decay_params: decay_params,
10741073
}
10751074
}
1075+
1076+
fn decayed_offset(&self, offset: u64, decay_params: ProbabilisticScoringDecayParameters) -> u64 {
1077+
let half_life = decay_params.liquidity_offset_half_life.as_secs();
1078+
if half_life != 0 {
1079+
let elapsed_time = T::now().duration_since(self.last_updated).as_secs() as f64;
1080+
((offset as f64) * powf64(0.5, elapsed_time / (half_life as f64))) as u64
1081+
} else {
1082+
0
1083+
}
1084+
}
10761085
}
10771086

10781087
/// Bounds `-log10` to avoid excessive liquidity penalties for payments with low success
@@ -1490,7 +1499,30 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
14901499
self.payment_path_failed(path, u64::max_value(), duration_since_epoch)
14911500
}
14921501

1493-
fn time_passed(&mut self, _duration_since_epoch: Duration) {}
1502+
fn time_passed(&mut self, _duration_since_epoch: Duration) {
1503+
let decay_params = self.decay_params;
1504+
self.channel_liquidities.retain(|_scid, liquidity| {
1505+
liquidity.min_liquidity_offset_msat = liquidity.decayed_offset(liquidity.min_liquidity_offset_msat, decay_params);
1506+
liquidity.max_liquidity_offset_msat = liquidity.decayed_offset(liquidity.max_liquidity_offset_msat, decay_params);
1507+
liquidity.last_updated = T::now();
1508+
let elapsed_time =
1509+
T::now().duration_since(liquidity.offset_history_last_updated);
1510+
if elapsed_time > decay_params.historical_no_updates_half_life {
1511+
let half_life = decay_params.historical_no_updates_half_life.as_secs() as f64;
1512+
let divisor = powf64(2048.0, (elapsed_time.as_secs() as f64) / half_life) as u64;
1513+
for bucket in liquidity.min_liquidity_offset_history.buckets.iter_mut() {
1514+
*bucket = ((*bucket as u64) * 1024 / divisor) as u16;
1515+
}
1516+
for bucket in liquidity.max_liquidity_offset_history.buckets.iter_mut() {
1517+
*bucket = ((*bucket as u64) * 1024 / divisor) as u16;
1518+
}
1519+
liquidity.offset_history_last_updated = T::now();
1520+
}
1521+
liquidity.min_liquidity_offset_msat != 0 || liquidity.max_liquidity_offset_msat != 0 ||
1522+
liquidity.min_liquidity_offset_history.buckets != [0; 32] ||
1523+
liquidity.max_liquidity_offset_history.buckets != [0; 32]
1524+
});
1525+
}
14941526
}
14951527

14961528
#[cfg(c_bindings)]
@@ -1928,7 +1960,7 @@ mod bucketed_history {
19281960
/// in each of 32 buckets.
19291961
#[derive(Clone, Copy)]
19301962
pub(super) struct HistoricalBucketRangeTracker {
1931-
buckets: [u16; 32],
1963+
pub(super) buckets: [u16; 32],
19321964
}
19331965

19341966
/// Buckets are stored in fixed point numbers with a 5 bit fractional part. Thus, the value

0 commit comments

Comments
 (0)