Skip to content

Commit 2ed8dfc

Browse files
committed
Use Duration based time info in scoring rather than Time
In the coming commits, the `T: Time` bound on `ProbabilisticScorer` will be removed. In order to enable that, we need to switch over to using the `ScoreUpdate`-provided current time (as a `Duration` since the unix epoch), making the `T` bound entirely unused.
1 parent 4a33532 commit 2ed8dfc

File tree

1 file changed

+61
-83
lines changed

1 file changed

+61
-83
lines changed

lightning/src/routing/scoring.rs

Lines changed: 61 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,8 @@ where L::Target: Logger {
494494
decay_params: ProbabilisticScoringDecayParameters,
495495
network_graph: G,
496496
logger: L,
497-
channel_liquidities: HashMap<u64, ChannelLiquidity<T>>,
497+
channel_liquidities: HashMap<u64, ChannelLiquidity>,
498+
_unused_time: core::marker::PhantomData<T>,
498499
}
499500

500501
/// Parameters for configuring [`ProbabilisticScorer`].
@@ -798,7 +799,7 @@ impl ProbabilisticScoringDecayParameters {
798799
/// Direction is defined in terms of [`NodeId`] partial ordering, where the source node is the
799800
/// first node in the ordering of the channel's counterparties. Thus, swapping the two liquidity
800801
/// offset fields gives the opposite direction.
801-
struct ChannelLiquidity<T: Time> {
802+
struct ChannelLiquidity {
802803
/// Lower channel liquidity bound in terms of an offset from zero.
803804
min_liquidity_offset_msat: u64,
804805

@@ -808,23 +809,23 @@ struct ChannelLiquidity<T: Time> {
808809
min_liquidity_offset_history: HistoricalBucketRangeTracker,
809810
max_liquidity_offset_history: HistoricalBucketRangeTracker,
810811

811-
/// Time when the liquidity bounds were last modified.
812-
last_updated: T,
812+
/// Time when the liquidity bounds were last modified as an offset since the unix epoch.
813+
last_updated: Duration,
813814

814-
/// Time when the historical liquidity bounds were last modified.
815-
offset_history_last_updated: T,
815+
/// Time when the historical liquidity bounds were last modified as an offset against the unix
816+
/// epoch.
817+
offset_history_last_updated: Duration,
816818
}
817819

818820
/// A snapshot of [`ChannelLiquidity`] in one direction assuming a certain channel capacity and
819821
/// decayed with a given half life.
820-
struct DirectedChannelLiquidity<L: Deref<Target = u64>, BRT: Deref<Target = HistoricalBucketRangeTracker>, T: Time, U: Deref<Target = T>> {
822+
struct DirectedChannelLiquidity<L: Deref<Target = u64>, BRT: Deref<Target = HistoricalBucketRangeTracker>, T: Deref<Target = Duration>> {
821823
min_liquidity_offset_msat: L,
822824
max_liquidity_offset_msat: L,
823825
liquidity_history: HistoricalMinMaxBuckets<BRT>,
824826
capacity_msat: u64,
825-
last_updated: U,
826-
offset_history_last_updated: U,
827-
now: T,
827+
last_updated: T,
828+
offset_history_last_updated: T,
828829
decay_params: ProbabilisticScoringDecayParameters,
829830
}
830831

@@ -837,11 +838,12 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ProbabilisticScorerU
837838
network_graph,
838839
logger,
839840
channel_liquidities: HashMap::new(),
841+
_unused_time: core::marker::PhantomData,
840842
}
841843
}
842844

843845
#[cfg(test)]
844-
fn with_channel(mut self, short_channel_id: u64, liquidity: ChannelLiquidity<T>) -> Self {
846+
fn with_channel(mut self, short_channel_id: u64, liquidity: ChannelLiquidity) -> Self {
845847
assert!(self.channel_liquidities.insert(short_channel_id, liquidity).is_none());
846848
self
847849
}
@@ -994,24 +996,23 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ProbabilisticScorerU
994996
}
995997
}
996998

997-
impl<T: Time> ChannelLiquidity<T> {
998-
#[inline]
999-
fn new() -> Self {
999+
impl ChannelLiquidity {
1000+
fn new(last_updated: Duration) -> Self {
10001001
Self {
10011002
min_liquidity_offset_msat: 0,
10021003
max_liquidity_offset_msat: 0,
10031004
min_liquidity_offset_history: HistoricalBucketRangeTracker::new(),
10041005
max_liquidity_offset_history: HistoricalBucketRangeTracker::new(),
1005-
last_updated: T::now(),
1006-
offset_history_last_updated: T::now(),
1006+
last_updated,
1007+
offset_history_last_updated: last_updated,
10071008
}
10081009
}
10091010

10101011
/// Returns a view of the channel liquidity directed from `source` to `target` assuming
10111012
/// `capacity_msat`.
10121013
fn as_directed(
10131014
&self, source: &NodeId, target: &NodeId, capacity_msat: u64, decay_params: ProbabilisticScoringDecayParameters
1014-
) -> DirectedChannelLiquidity<&u64, &HistoricalBucketRangeTracker, T, &T> {
1015+
) -> DirectedChannelLiquidity<&u64, &HistoricalBucketRangeTracker, &Duration> {
10151016
let (min_liquidity_offset_msat, max_liquidity_offset_msat, min_liquidity_offset_history, max_liquidity_offset_history) =
10161017
if source < target {
10171018
(&self.min_liquidity_offset_msat, &self.max_liquidity_offset_msat,
@@ -1031,7 +1032,6 @@ impl<T: Time> ChannelLiquidity<T> {
10311032
capacity_msat,
10321033
last_updated: &self.last_updated,
10331034
offset_history_last_updated: &self.offset_history_last_updated,
1034-
now: T::now(),
10351035
decay_params: decay_params,
10361036
}
10371037
}
@@ -1040,7 +1040,7 @@ impl<T: Time> ChannelLiquidity<T> {
10401040
/// `capacity_msat`.
10411041
fn as_directed_mut(
10421042
&mut self, source: &NodeId, target: &NodeId, capacity_msat: u64, decay_params: ProbabilisticScoringDecayParameters
1043-
) -> DirectedChannelLiquidity<&mut u64, &mut HistoricalBucketRangeTracker, T, &mut T> {
1043+
) -> DirectedChannelLiquidity<&mut u64, &mut HistoricalBucketRangeTracker, &mut Duration> {
10441044
let (min_liquidity_offset_msat, max_liquidity_offset_msat, min_liquidity_offset_history, max_liquidity_offset_history) =
10451045
if source < target {
10461046
(&mut self.min_liquidity_offset_msat, &mut self.max_liquidity_offset_msat,
@@ -1060,7 +1060,6 @@ impl<T: Time> ChannelLiquidity<T> {
10601060
capacity_msat,
10611061
last_updated: &mut self.last_updated,
10621062
offset_history_last_updated: &mut self.offset_history_last_updated,
1063-
now: T::now(),
10641063
decay_params: decay_params,
10651064
}
10661065
}
@@ -1070,7 +1069,7 @@ impl<T: Time> ChannelLiquidity<T> {
10701069
) -> u64 {
10711070
let half_life = decay_params.liquidity_offset_half_life.as_secs_f64();
10721071
if half_life != 0.0 {
1073-
let elapsed_time = T::now().duration_since(self.last_updated).as_secs_f64();
1072+
let elapsed_time = duration_since_epoch.saturating_sub(self.last_updated).as_secs_f64();
10741073
((offset as f64) * powf64(0.5, elapsed_time / half_life)) as u64
10751074
} else {
10761075
0
@@ -1159,7 +1158,8 @@ fn success_probability(
11591158
(numerator, denominator)
11601159
}
11611160

1162-
impl<L: Deref<Target = u64>, BRT: Deref<Target = HistoricalBucketRangeTracker>, T: Time, U: Deref<Target = T>> DirectedChannelLiquidity< L, BRT, T, U> {
1161+
impl<L: Deref<Target = u64>, BRT: Deref<Target = HistoricalBucketRangeTracker>, T: Deref<Target = Duration>>
1162+
DirectedChannelLiquidity< L, BRT, T> {
11631163
/// Returns a liquidity penalty for routing the given HTLC `amount_msat` through the channel in
11641164
/// this direction.
11651165
fn penalty_msat(&self, amount_msat: u64, score_params: &ProbabilisticScoringFeeParameters) -> u64 {
@@ -1267,7 +1267,8 @@ impl<L: Deref<Target = u64>, BRT: Deref<Target = HistoricalBucketRangeTracker>,
12671267
}
12681268
}
12691269

1270-
impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTracker>, T: Time, U: DerefMut<Target = T>> DirectedChannelLiquidity<L, BRT, T, U> {
1270+
impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTracker>, T: DerefMut<Target = Duration>>
1271+
DirectedChannelLiquidity<L, BRT, T> {
12711272
/// Adjusts the channel liquidity balance bounds when failing to route `amount_msat`.
12721273
fn failed_at_channel<Log: Deref>(
12731274
&mut self, amount_msat: u64, duration_since_epoch: Duration, chan_descr: fmt::Arguments, logger: &Log
@@ -1313,7 +1314,9 @@ impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTrac
13131314
/// state"), we allow the caller to set an offset applied to our liquidity bounds which
13141315
/// represents the amount of the successful payment we just made.
13151316
fn update_history_buckets(&mut self, bucket_offset_msat: u64, duration_since_epoch: Duration) {
1316-
let half_lives = self.now.duration_since(*self.offset_history_last_updated).as_secs()
1317+
let half_lives =
1318+
duration_since_epoch.checked_sub(*self.offset_history_last_updated)
1319+
.unwrap_or(Duration::ZERO).as_secs()
13171320
.checked_div(self.decay_params.historical_no_updates_half_life.as_secs())
13181321
.map(|v| v.try_into().unwrap_or(u32::max_value())).unwrap_or(u32::max_value());
13191322
self.liquidity_history.min_liquidity_offset_history.time_decay_data(half_lives);
@@ -1327,29 +1330,25 @@ impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTrac
13271330
self.liquidity_history.max_liquidity_offset_history.track_datapoint(
13281331
max_liquidity_offset_msat.saturating_sub(bucket_offset_msat), self.capacity_msat
13291332
);
1330-
*self.offset_history_last_updated = self.now;
1333+
*self.offset_history_last_updated = duration_since_epoch;
13311334
}
13321335

13331336
/// Adjusts the lower bound of the channel liquidity balance in this direction.
13341337
fn set_min_liquidity_msat(&mut self, amount_msat: u64, duration_since_epoch: Duration) {
13351338
*self.min_liquidity_offset_msat = amount_msat;
1336-
*self.max_liquidity_offset_msat = if amount_msat > self.max_liquidity_msat() {
1337-
0
1338-
} else {
1339-
self.decayed_offset_msat(*self.max_liquidity_offset_msat)
1340-
};
1341-
*self.last_updated = self.now;
1339+
if amount_msat > self.max_liquidity_msat() {
1340+
*self.max_liquidity_offset_msat = 0;
1341+
}
1342+
*self.last_updated = duration_since_epoch;
13421343
}
13431344

13441345
/// Adjusts the upper bound of the channel liquidity balance in this direction.
13451346
fn set_max_liquidity_msat(&mut self, amount_msat: u64, duration_since_epoch: Duration) {
13461347
*self.max_liquidity_offset_msat = self.capacity_msat.checked_sub(amount_msat).unwrap_or(0);
1347-
*self.min_liquidity_offset_msat = if amount_msat < self.min_liquidity_msat() {
1348-
0
1349-
} else {
1350-
self.decayed_offset_msat(*self.min_liquidity_offset_msat)
1351-
};
1352-
*self.last_updated = self.now;
1348+
if amount_msat < *self.min_liquidity_offset_msat {
1349+
*self.min_liquidity_offset_msat = 0;
1350+
}
1351+
*self.last_updated = duration_since_epoch;
13531352
}
13541353
}
13551354

@@ -1389,7 +1388,7 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreLookUp for Prob
13891388
let capacity_msat = usage.effective_capacity.as_msat();
13901389
self.channel_liquidities
13911390
.get(&short_channel_id)
1392-
.unwrap_or(&ChannelLiquidity::new())
1391+
.unwrap_or(&ChannelLiquidity::new(Duration::ZERO))
13931392
.as_directed(source, target, capacity_msat, self.decay_params)
13941393
.penalty_msat(amount_msat, score_params)
13951394
.saturating_add(anti_probing_penalty_msat)
@@ -1419,14 +1418,14 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
14191418
if at_failed_channel {
14201419
self.channel_liquidities
14211420
.entry(hop.short_channel_id)
1422-
.or_insert_with(ChannelLiquidity::new)
1421+
.or_insert_with(|| ChannelLiquidity::new(duration_since_epoch))
14231422
.as_directed_mut(source, &target, capacity_msat, self.decay_params)
14241423
.failed_at_channel(amount_msat, duration_since_epoch,
14251424
format_args!("SCID {}, towards {:?}", hop.short_channel_id, target), &self.logger);
14261425
} else {
14271426
self.channel_liquidities
14281427
.entry(hop.short_channel_id)
1429-
.or_insert_with(ChannelLiquidity::new)
1428+
.or_insert_with(|| ChannelLiquidity::new(duration_since_epoch))
14301429
.as_directed_mut(source, &target, capacity_msat, self.decay_params)
14311430
.failed_downstream(amount_msat, duration_since_epoch,
14321431
format_args!("SCID {}, towards {:?}", hop.short_channel_id, target), &self.logger);
@@ -1455,7 +1454,7 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
14551454
let capacity_msat = channel.effective_capacity().as_msat();
14561455
self.channel_liquidities
14571456
.entry(hop.short_channel_id)
1458-
.or_insert_with(ChannelLiquidity::new)
1457+
.or_insert_with(|| ChannelLiquidity::new(duration_since_epoch))
14591458
.as_directed_mut(source, &target, capacity_msat, self.decay_params)
14601459
.successful(amount_msat, duration_since_epoch,
14611460
format_args!("SCID {}, towards {:?}", hop.short_channel_id, target), &self.logger);
@@ -1481,10 +1480,10 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
14811480
liquidity.decayed_offset(liquidity.min_liquidity_offset_msat, duration_since_epoch, decay_params);
14821481
liquidity.max_liquidity_offset_msat =
14831482
liquidity.decayed_offset(liquidity.max_liquidity_offset_msat, duration_since_epoch, decay_params);
1484-
liquidity.last_updated = T::now();
1483+
liquidity.last_updated = duration_since_epoch;
14851484

14861485
let elapsed_time =
1487-
T::now().duration_since(liquidity.offset_history_last_updated);
1486+
duration_since_epoch.saturating_sub(liquidity.offset_history_last_updated);
14881487
if elapsed_time > decay_params.historical_no_updates_half_life {
14891488
let half_life = decay_params.historical_no_updates_half_life.as_secs_f64();
14901489
if half_life != 0.0 {
@@ -1495,7 +1494,7 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
14951494
for bucket in liquidity.max_liquidity_offset_history.buckets.iter_mut() {
14961495
*bucket = ((*bucket as u64) * 1024 / divisor) as u16;
14971496
}
1498-
liquidity.offset_history_last_updated = T::now();
1497+
liquidity.offset_history_last_updated = duration_since_epoch;
14991498
}
15001499
}
15011500
liquidity.min_liquidity_offset_msat != 0 || liquidity.max_liquidity_offset_msat != 0 ||
@@ -2118,31 +2117,29 @@ ReadableArgs<(ProbabilisticScoringDecayParameters, G, L)> for ProbabilisticScore
21182117
network_graph,
21192118
logger,
21202119
channel_liquidities,
2120+
_unused_time: core::marker::PhantomData,
21212121
})
21222122
}
21232123
}
21242124

2125-
impl<T: Time> Writeable for ChannelLiquidity<T> {
2125+
impl Writeable for ChannelLiquidity {
21262126
#[inline]
21272127
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
2128-
let offset_history_duration_since_epoch =
2129-
T::duration_since_epoch() - self.offset_history_last_updated.elapsed();
2130-
let duration_since_epoch = T::duration_since_epoch() - self.last_updated.elapsed();
21312128
write_tlv_fields!(w, {
21322129
(0, self.min_liquidity_offset_msat, required),
21332130
// 1 was the min_liquidity_offset_history in octile form
21342131
(2, self.max_liquidity_offset_msat, required),
21352132
// 3 was the max_liquidity_offset_history in octile form
2136-
(4, duration_since_epoch, required),
2133+
(4, self.last_updated, required),
21372134
(5, Some(self.min_liquidity_offset_history), option),
21382135
(7, Some(self.max_liquidity_offset_history), option),
2139-
(9, offset_history_duration_since_epoch, required),
2136+
(9, self.offset_history_last_updated, required),
21402137
});
21412138
Ok(())
21422139
}
21432140
}
21442141

2145-
impl<T: Time> Readable for ChannelLiquidity<T> {
2142+
impl Readable for ChannelLiquidity {
21462143
#[inline]
21472144
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
21482145
let mut min_liquidity_offset_msat = 0;
@@ -2151,36 +2148,18 @@ impl<T: Time> Readable for ChannelLiquidity<T> {
21512148
let mut legacy_max_liq_offset_history: Option<LegacyHistoricalBucketRangeTracker> = None;
21522149
let mut min_liquidity_offset_history: Option<HistoricalBucketRangeTracker> = None;
21532150
let mut max_liquidity_offset_history: Option<HistoricalBucketRangeTracker> = None;
2154-
let mut duration_since_epoch = Duration::from_secs(0);
2155-
let mut offset_history_duration_since_epoch = None;
2151+
let mut last_updated = Duration::from_secs(0);
2152+
let mut offset_history_last_updated = None;
21562153
read_tlv_fields!(r, {
21572154
(0, min_liquidity_offset_msat, required),
21582155
(1, legacy_min_liq_offset_history, option),
21592156
(2, max_liquidity_offset_msat, required),
21602157
(3, legacy_max_liq_offset_history, option),
2161-
(4, duration_since_epoch, required),
2158+
(4, last_updated, required),
21622159
(5, min_liquidity_offset_history, option),
21632160
(7, max_liquidity_offset_history, option),
2164-
(9, offset_history_duration_since_epoch, option),
2161+
(9, offset_history_last_updated, option),
21652162
});
2166-
// On rust prior to 1.60 `Instant::duration_since` will panic if time goes backwards.
2167-
// We write `last_updated` as wallclock time even though its ultimately an `Instant` (which
2168-
// is a time from a monotonic clock usually represented as an offset against boot time).
2169-
// Thus, we have to construct an `Instant` by subtracting the difference in wallclock time
2170-
// from the one that was written. However, because `Instant` can panic if we construct one
2171-
// in the future, we must handle wallclock time jumping backwards, which we do by simply
2172-
// using `Instant::now()` in that case.
2173-
let wall_clock_now = T::duration_since_epoch();
2174-
let now = T::now();
2175-
let last_updated = if wall_clock_now > duration_since_epoch {
2176-
now - (wall_clock_now - duration_since_epoch)
2177-
} else { now };
2178-
2179-
let offset_history_duration_since_epoch =
2180-
offset_history_duration_since_epoch.unwrap_or(duration_since_epoch);
2181-
let offset_history_last_updated = if wall_clock_now > offset_history_duration_since_epoch {
2182-
now - (wall_clock_now - offset_history_duration_since_epoch)
2183-
} else { now };
21842163

21852164
if min_liquidity_offset_history.is_none() {
21862165
if let Some(legacy_buckets) = legacy_min_liq_offset_history {
@@ -2202,7 +2181,7 @@ impl<T: Time> Readable for ChannelLiquidity<T> {
22022181
min_liquidity_offset_history: min_liquidity_offset_history.unwrap(),
22032182
max_liquidity_offset_history: max_liquidity_offset_history.unwrap(),
22042183
last_updated,
2205-
offset_history_last_updated,
2184+
offset_history_last_updated: offset_history_last_updated.unwrap_or(last_updated),
22062185
})
22072186
}
22082187
}
@@ -2212,7 +2191,6 @@ mod tests {
22122191
use super::{ChannelLiquidity, HistoricalBucketRangeTracker, ProbabilisticScoringFeeParameters, ProbabilisticScoringDecayParameters, ProbabilisticScorerUsingTime};
22132192
use crate::blinded_path::{BlindedHop, BlindedPath};
22142193
use crate::util::config::UserConfig;
2215-
use crate::util::time::Time;
22162194
use crate::util::time::tests::SinceEpoch;
22172195

22182196
use crate::ln::channelmanager;
@@ -2381,8 +2359,8 @@ mod tests {
23812359
#[test]
23822360
fn liquidity_bounds_directed_from_lowest_node_id() {
23832361
let logger = TestLogger::new();
2384-
let last_updated = SinceEpoch::now();
2385-
let offset_history_last_updated = SinceEpoch::now();
2362+
let last_updated = Duration::ZERO;
2363+
let offset_history_last_updated = Duration::ZERO;
23862364
let network_graph = network_graph(&logger);
23872365
let decay_params = ProbabilisticScoringDecayParameters::default();
23882366
let mut scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger)
@@ -2462,8 +2440,8 @@ mod tests {
24622440
#[test]
24632441
fn resets_liquidity_upper_bound_when_crossed_by_lower_bound() {
24642442
let logger = TestLogger::new();
2465-
let last_updated = SinceEpoch::now();
2466-
let offset_history_last_updated = SinceEpoch::now();
2443+
let last_updated = Duration::ZERO;
2444+
let offset_history_last_updated = Duration::ZERO;
24672445
let network_graph = network_graph(&logger);
24682446
let decay_params = ProbabilisticScoringDecayParameters::default();
24692447
let mut scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger)
@@ -2523,8 +2501,8 @@ mod tests {
25232501
#[test]
25242502
fn resets_liquidity_lower_bound_when_crossed_by_upper_bound() {
25252503
let logger = TestLogger::new();
2526-
let last_updated = SinceEpoch::now();
2527-
let offset_history_last_updated = SinceEpoch::now();
2504+
let last_updated = Duration::ZERO;
2505+
let offset_history_last_updated = Duration::ZERO;
25282506
let network_graph = network_graph(&logger);
25292507
let decay_params = ProbabilisticScoringDecayParameters::default();
25302508
let mut scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger)
@@ -2630,8 +2608,8 @@ mod tests {
26302608
#[test]
26312609
fn constant_penalty_outside_liquidity_bounds() {
26322610
let logger = TestLogger::new();
2633-
let last_updated = SinceEpoch::now();
2634-
let offset_history_last_updated = SinceEpoch::now();
2611+
let last_updated = Duration::ZERO;
2612+
let offset_history_last_updated = Duration::ZERO;
26352613
let network_graph = network_graph(&logger);
26362614
let params = ProbabilisticScoringFeeParameters {
26372615
liquidity_penalty_multiplier_msat: 1_000,

0 commit comments

Comments
 (0)