Skip to content

Commit 9659c06

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 f0f8194 commit 9659c06

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

lightning/src/routing/scoring.rs

Lines changed: 37 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_f64();
1078+
if half_life != 0.0 {
1079+
let elapsed_time = T::now().duration_since(self.last_updated).as_secs_f64();
1080+
((offset as f64) * powf64(0.5, elapsed_time / half_life)) 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,32 @@ 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_f64();
1512+
if half_life != 0.0 {
1513+
let divisor = powf64(2048.0, elapsed_time.as_secs_f64() / half_life) as u64;
1514+
for bucket in liquidity.min_liquidity_offset_history.buckets.iter_mut() {
1515+
*bucket = ((*bucket as u64) * 1024 / divisor) as u16;
1516+
}
1517+
for bucket in liquidity.max_liquidity_offset_history.buckets.iter_mut() {
1518+
*bucket = ((*bucket as u64) * 1024 / divisor) as u16;
1519+
}
1520+
liquidity.offset_history_last_updated = T::now();
1521+
}
1522+
}
1523+
liquidity.min_liquidity_offset_msat != 0 || liquidity.max_liquidity_offset_msat != 0 ||
1524+
liquidity.min_liquidity_offset_history.buckets != [0; 32] ||
1525+
liquidity.max_liquidity_offset_history.buckets != [0; 32]
1526+
});
1527+
}
14941528
}
14951529

14961530
#[cfg(c_bindings)]
@@ -1928,7 +1962,7 @@ mod bucketed_history {
19281962
/// in each of 32 buckets.
19291963
#[derive(Clone, Copy)]
19301964
pub(super) struct HistoricalBucketRangeTracker {
1931-
buckets: [u16; 32],
1965+
pub(super) buckets: [u16; 32],
19321966
}
19331967

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

0 commit comments

Comments
 (0)