Skip to content

Commit c03c044

Browse files
committed
Pipe Duration-based time information through scoring pipeline
In the coming commits, the `T: Time` bound on `ProbabilisticScorer` will be removed. In order to enable that, we need to pass the current time (as a `Duration` since the unix epoch) through the score updating pipeline, allowing us to keep the `*last_updated_time` fields up-to-date as we go.
1 parent a75fd62 commit c03c044

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

lightning/src/routing/scoring.rs

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,9 @@ impl<T: Time> ChannelLiquidity<T> {
10641064
}
10651065
}
10661066

1067-
fn decayed_offset(&self, offset: u64, decay_params: ProbabilisticScoringDecayParameters) -> u64 {
1067+
fn decayed_offset(&self, offset: u64, duration_since_epoch: Duration,
1068+
decay_params: ProbabilisticScoringDecayParameters
1069+
) -> u64 {
10681070
let half_life = decay_params.liquidity_offset_half_life.as_secs_f64();
10691071
if half_life != 0.0 {
10701072
let elapsed_time = T::now().duration_since(self.last_updated).as_secs_f64();
@@ -1266,44 +1268,50 @@ impl<L: Deref<Target = u64>, BRT: Deref<Target = HistoricalBucketRangeTracker>,
12661268

12671269
impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTracker>, T: Time, U: DerefMut<Target = T>> DirectedChannelLiquidity<L, BRT, T, U> {
12681270
/// Adjusts the channel liquidity balance bounds when failing to route `amount_msat`.
1269-
fn failed_at_channel<Log: Deref>(&mut self, amount_msat: u64, chan_descr: fmt::Arguments, logger: &Log) where Log::Target: Logger {
1271+
fn failed_at_channel<Log: Deref>(
1272+
&mut self, amount_msat: u64, duration_since_epoch: Duration, chan_descr: fmt::Arguments, logger: &Log
1273+
) where Log::Target: Logger {
12701274
let existing_max_msat = self.max_liquidity_msat();
12711275
if amount_msat < existing_max_msat {
12721276
log_debug!(logger, "Setting max liquidity of {} from {} to {}", chan_descr, existing_max_msat, amount_msat);
1273-
self.set_max_liquidity_msat(amount_msat);
1277+
self.set_max_liquidity_msat(amount_msat, duration_since_epoch);
12741278
} else {
12751279
log_trace!(logger, "Max liquidity of {} is {} (already less than or equal to {})",
12761280
chan_descr, existing_max_msat, amount_msat);
12771281
}
1278-
self.update_history_buckets(0);
1282+
self.update_history_buckets(0, duration_since_epoch);
12791283
}
12801284

12811285
/// Adjusts the channel liquidity balance bounds when failing to route `amount_msat` downstream.
1282-
fn failed_downstream<Log: Deref>(&mut self, amount_msat: u64, chan_descr: fmt::Arguments, logger: &Log) where Log::Target: Logger {
1286+
fn failed_downstream<Log: Deref>(
1287+
&mut self, amount_msat: u64, duration_since_epoch: Duration, chan_descr: fmt::Arguments, logger: &Log
1288+
) where Log::Target: Logger {
12831289
let existing_min_msat = self.min_liquidity_msat();
12841290
if amount_msat > existing_min_msat {
12851291
log_debug!(logger, "Setting min liquidity of {} from {} to {}", existing_min_msat, chan_descr, amount_msat);
1286-
self.set_min_liquidity_msat(amount_msat);
1292+
self.set_min_liquidity_msat(amount_msat, duration_since_epoch);
12871293
} else {
12881294
log_trace!(logger, "Min liquidity of {} is {} (already greater than or equal to {})",
12891295
chan_descr, existing_min_msat, amount_msat);
12901296
}
1291-
self.update_history_buckets(0);
1297+
self.update_history_buckets(0, duration_since_epoch);
12921298
}
12931299

12941300
/// Adjusts the channel liquidity balance bounds when successfully routing `amount_msat`.
1295-
fn successful<Log: Deref>(&mut self, amount_msat: u64, chan_descr: fmt::Arguments, logger: &Log) where Log::Target: Logger {
1301+
fn successful<Log: Deref>(&mut self,
1302+
amount_msat: u64, duration_since_epoch: Duration, chan_descr: fmt::Arguments, logger: &Log
1303+
) where Log::Target: Logger {
12961304
let max_liquidity_msat = self.max_liquidity_msat().checked_sub(amount_msat).unwrap_or(0);
12971305
log_debug!(logger, "Subtracting {} from max liquidity of {} (setting it to {})", amount_msat, chan_descr, max_liquidity_msat);
1298-
self.set_max_liquidity_msat(max_liquidity_msat);
1299-
self.update_history_buckets(amount_msat);
1306+
self.set_max_liquidity_msat(max_liquidity_msat, duration_since_epoch);
1307+
self.update_history_buckets(amount_msat, duration_since_epoch);
13001308
}
13011309

13021310
/// Updates the history buckets for this channel. Because the history buckets track what we now
13031311
/// know about the channel's state *prior to our payment* (i.e. what we assume is "steady
13041312
/// state"), we allow the caller to set an offset applied to our liquidity bounds which
13051313
/// represents the amount of the successful payment we just made.
1306-
fn update_history_buckets(&mut self, bucket_offset_msat: u64) {
1314+
fn update_history_buckets(&mut self, bucket_offset_msat: u64, duration_since_epoch: Duration) {
13071315
let half_lives = self.now.duration_since(*self.offset_history_last_updated).as_secs()
13081316
.checked_div(self.decay_params.historical_no_updates_half_life.as_secs())
13091317
.map(|v| v.try_into().unwrap_or(u32::max_value())).unwrap_or(u32::max_value());
@@ -1322,7 +1330,7 @@ impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTrac
13221330
}
13231331

13241332
/// Adjusts the lower bound of the channel liquidity balance in this direction.
1325-
fn set_min_liquidity_msat(&mut self, amount_msat: u64) {
1333+
fn set_min_liquidity_msat(&mut self, amount_msat: u64, duration_since_epoch: Duration) {
13261334
*self.min_liquidity_offset_msat = amount_msat;
13271335
*self.max_liquidity_offset_msat = if amount_msat > self.max_liquidity_msat() {
13281336
0
@@ -1333,7 +1341,7 @@ impl<L: DerefMut<Target = u64>, BRT: DerefMut<Target = HistoricalBucketRangeTrac
13331341
}
13341342

13351343
/// Adjusts the upper bound of the channel liquidity balance in this direction.
1336-
fn set_max_liquidity_msat(&mut self, amount_msat: u64) {
1344+
fn set_max_liquidity_msat(&mut self, amount_msat: u64, duration_since_epoch: Duration) {
13371345
*self.max_liquidity_offset_msat = self.capacity_msat.checked_sub(amount_msat).unwrap_or(0);
13381346
*self.min_liquidity_offset_msat = if amount_msat < self.min_liquidity_msat() {
13391347
0
@@ -1396,7 +1404,7 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreLookUp for Prob
13961404
}
13971405

13981406
impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for ProbabilisticScorerUsingTime<G, L, T> where L::Target: Logger {
1399-
fn payment_path_failed(&mut self, path: &Path, short_channel_id: u64, _duration_since_epoch: Duration) {
1407+
fn payment_path_failed(&mut self, path: &Path, short_channel_id: u64, duration_since_epoch: Duration) {
14001408
let amount_msat = path.final_value_msat();
14011409
log_trace!(self.logger, "Scoring path through to SCID {} as having failed at {} msat", short_channel_id, amount_msat);
14021410
let network_graph = self.network_graph.read_only();
@@ -1419,13 +1427,15 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
14191427
.entry(hop.short_channel_id)
14201428
.or_insert_with(ChannelLiquidity::new)
14211429
.as_directed_mut(source, &target, capacity_msat, self.decay_params)
1422-
.failed_at_channel(amount_msat, format_args!("SCID {}, towards {:?}", hop.short_channel_id, target), &self.logger);
1430+
.failed_at_channel(amount_msat, duration_since_epoch,
1431+
format_args!("SCID {}, towards {:?}", hop.short_channel_id, target), &self.logger);
14231432
} else {
14241433
self.channel_liquidities
14251434
.entry(hop.short_channel_id)
14261435
.or_insert_with(ChannelLiquidity::new)
14271436
.as_directed_mut(source, &target, capacity_msat, self.decay_params)
1428-
.failed_downstream(amount_msat, format_args!("SCID {}, towards {:?}", hop.short_channel_id, target), &self.logger);
1437+
.failed_downstream(amount_msat, duration_since_epoch,
1438+
format_args!("SCID {}, towards {:?}", hop.short_channel_id, target), &self.logger);
14291439
}
14301440
} else {
14311441
log_debug!(self.logger, "Not able to penalize channel with SCID {} as we do not have graph info for it (likely a route-hint last-hop).",
@@ -1435,7 +1445,7 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
14351445
}
14361446
}
14371447

1438-
fn payment_path_successful(&mut self, path: &Path, _duration_since_epoch: Duration) {
1448+
fn payment_path_successful(&mut self, path: &Path, duration_since_epoch: Duration) {
14391449
let amount_msat = path.final_value_msat();
14401450
log_trace!(self.logger, "Scoring path through SCID {} as having succeeded at {} msat.",
14411451
path.hops.split_last().map(|(hop, _)| hop.short_channel_id).unwrap_or(0), amount_msat);
@@ -1453,7 +1463,8 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
14531463
.entry(hop.short_channel_id)
14541464
.or_insert_with(ChannelLiquidity::new)
14551465
.as_directed_mut(source, &target, capacity_msat, self.decay_params)
1456-
.successful(amount_msat, format_args!("SCID {}, towards {:?}", hop.short_channel_id, target), &self.logger);
1466+
.successful(amount_msat, duration_since_epoch,
1467+
format_args!("SCID {}, towards {:?}", hop.short_channel_id, target), &self.logger);
14571468
} else {
14581469
log_debug!(self.logger, "Not able to learn for channel with SCID {} as we do not have graph info for it (likely a route-hint last-hop).",
14591470
hop.short_channel_id);
@@ -1469,12 +1480,15 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ScoreUpdate for Prob
14691480
self.payment_path_failed(path, u64::max_value(), duration_since_epoch)
14701481
}
14711482

1472-
fn decay_liquidity_certainty(&mut self, _duration_since_epoch: Duration) {
1483+
fn decay_liquidity_certainty(&mut self, duration_since_epoch: Duration) {
14731484
let decay_params = self.decay_params;
14741485
self.channel_liquidities.retain(|_scid, liquidity| {
1475-
liquidity.min_liquidity_offset_msat = liquidity.decayed_offset(liquidity.min_liquidity_offset_msat, decay_params);
1476-
liquidity.max_liquidity_offset_msat = liquidity.decayed_offset(liquidity.max_liquidity_offset_msat, decay_params);
1486+
liquidity.min_liquidity_offset_msat =
1487+
liquidity.decayed_offset(liquidity.min_liquidity_offset_msat, duration_since_epoch, decay_params);
1488+
liquidity.max_liquidity_offset_msat =
1489+
liquidity.decayed_offset(liquidity.max_liquidity_offset_msat, duration_since_epoch, decay_params);
14771490
liquidity.last_updated = T::now();
1491+
14781492
let elapsed_time =
14791493
T::now().duration_since(liquidity.offset_history_last_updated);
14801494
if elapsed_time > decay_params.historical_no_updates_half_life {
@@ -2408,7 +2422,7 @@ mod tests {
24082422

24092423
scorer.channel_liquidities.get_mut(&42).unwrap()
24102424
.as_directed_mut(&source, &target, 1_000, decay_params)
2411-
.set_min_liquidity_msat(200);
2425+
.set_min_liquidity_msat(200, Duration::ZERO);
24122426

24132427
let liquidity = scorer.channel_liquidities.get(&42).unwrap()
24142428
.as_directed(&source, &target, 1_000, decay_params);
@@ -2434,7 +2448,7 @@ mod tests {
24342448

24352449
scorer.channel_liquidities.get_mut(&43).unwrap()
24362450
.as_directed_mut(&target, &recipient, 1_000, decay_params)
2437-
.set_max_liquidity_msat(200);
2451+
.set_max_liquidity_msat(200, Duration::ZERO);
24382452

24392453
let liquidity = scorer.channel_liquidities.get(&43).unwrap()
24402454
.as_directed(&target, &recipient, 1_000, decay_params);
@@ -2480,7 +2494,7 @@ mod tests {
24802494
// Reset from source to target.
24812495
scorer.channel_liquidities.get_mut(&42).unwrap()
24822496
.as_directed_mut(&source, &target, 1_000, decay_params)
2483-
.set_min_liquidity_msat(900);
2497+
.set_min_liquidity_msat(900, Duration::ZERO);
24842498

24852499
let liquidity = scorer.channel_liquidities.get(&42).unwrap()
24862500
.as_directed(&source, &target, 1_000, decay_params);
@@ -2495,7 +2509,7 @@ mod tests {
24952509
// Reset from target to source.
24962510
scorer.channel_liquidities.get_mut(&42).unwrap()
24972511
.as_directed_mut(&target, &source, 1_000, decay_params)
2498-
.set_min_liquidity_msat(400);
2512+
.set_min_liquidity_msat(400, Duration::ZERO);
24992513

25002514
let liquidity = scorer.channel_liquidities.get(&42).unwrap()
25012515
.as_directed(&source, &target, 1_000, decay_params);
@@ -2541,7 +2555,7 @@ mod tests {
25412555
// Reset from source to target.
25422556
scorer.channel_liquidities.get_mut(&42).unwrap()
25432557
.as_directed_mut(&source, &target, 1_000, decay_params)
2544-
.set_max_liquidity_msat(300);
2558+
.set_max_liquidity_msat(300, Duration::ZERO);
25452559

25462560
let liquidity = scorer.channel_liquidities.get(&42).unwrap()
25472561
.as_directed(&source, &target, 1_000, decay_params);
@@ -2556,7 +2570,7 @@ mod tests {
25562570
// Reset from target to source.
25572571
scorer.channel_liquidities.get_mut(&42).unwrap()
25582572
.as_directed_mut(&target, &source, 1_000, decay_params)
2559-
.set_max_liquidity_msat(600);
2573+
.set_max_liquidity_msat(600, Duration::ZERO);
25602574

25612575
let liquidity = scorer.channel_liquidities.get(&42).unwrap()
25622576
.as_directed(&source, &target, 1_000, decay_params);

0 commit comments

Comments
 (0)